abstractjdbc2resultset.java
来自「PostgreSQL7.4.6 for Linux」· Java 代码 · 共 1,625 行 · 第 1/3 页
JAVA
1,625 行
public synchronized void deleteRow() throws SQLException { if ( !isUpdateable() ) { throw new PSQLException( "postgresql.updateable.notupdateable" ); } if (onInsertRow) { throw new PSQLException( "postgresql.updateable.oninsertrow" ); } if (rows.size() == 0) { throw new PSQLException( "postgresql.updateable.emptydelete" ); } if (isBeforeFirst()) { throw new PSQLException( "postgresql.updateable.beforestartdelete" ); } if (isAfterLast()) { throw new PSQLException( "postgresql.updateable.afterlastdelete" ); } int numKeys = primaryKeys.size(); if ( deleteStatement == null ) { StringBuffer deleteSQL = new StringBuffer("DELETE FROM " ).append(tableName).append(" where " ); for ( int i = 0; i < numKeys; i++ ) { deleteSQL.append( ((PrimaryKey) primaryKeys.get(i)).name ).append( " = ? " ); if ( i < numKeys - 1 ) { deleteSQL.append( " and " ); } } deleteStatement = ((java.sql.Connection) connection).prepareStatement(deleteSQL.toString()); } deleteStatement.clearParameters(); for ( int i = 0; i < numKeys; i++ ) { deleteStatement.setObject(i + 1, ((PrimaryKey) primaryKeys.get(i)).getValue()); } deleteStatement.executeUpdate(); rows.removeElementAt(current_row); current_row--; moveToCurrentRow(); } public synchronized void insertRow() throws SQLException { if ( !isUpdateable() ) { throw new PSQLException( "postgresql.updateable.notupdateable" ); } if (!onInsertRow) { throw new PSQLException( "postgresql.updateable.notoninsertrow" ); } else { // loop through the keys in the insertTable and create the sql statement // we have to create the sql every time since the user could insert different // columns each time StringBuffer insertSQL = new StringBuffer("INSERT INTO ").append(tableName).append(" ("); StringBuffer paramSQL = new StringBuffer(") values (" ); Enumeration columnNames = updateValues.keys(); int numColumns = updateValues.size(); for ( int i = 0; columnNames.hasMoreElements(); i++ ) { String columnName = (String) columnNames.nextElement(); insertSQL.append( columnName ); if ( i < numColumns - 1 ) { insertSQL.append(", "); paramSQL.append("?,"); } else { paramSQL.append("?)"); } } insertSQL.append(paramSQL.toString()); insertStatement = ((java.sql.Connection) connection).prepareStatement(insertSQL.toString()); Enumeration keys = updateValues.keys(); for ( int i = 1; keys.hasMoreElements(); i++) { String key = (String) keys.nextElement(); Object o = updateValues.get(key); if (o instanceof NullObject) insertStatement.setNull(i,java.sql.Types.NULL); else insertStatement.setObject(i, o); } insertStatement.executeUpdate(); if ( usingOID ) { // we have to get the last inserted OID and put it in the resultset long insertedOID = ((AbstractJdbc2Statement) insertStatement).getLastOID(); updateValues.put("oid", new Long(insertedOID) ); } // update the underlying row to the new inserted data updateRowBuffer(); rows.addElement(rowBuffer); // we should now reflect the current data in this_row // that way getXXX will get the newly inserted data this_row = rowBuffer; // need to clear this in case of another insert clearRowBuffer(false); } } public synchronized void moveToCurrentRow() throws SQLException { if (!isUpdateable()) { throw new PSQLException( "postgresql.updateable.notupdateable" ); } if (current_row < 0) { this_row = null; rowBuffer = null; } else { this_row = (byte[][]) rows.elementAt(current_row); rowBuffer = new byte[this_row.length][]; System.arraycopy(this_row, 0, rowBuffer, 0, this_row.length); } onInsertRow = false; doingUpdates = false; } public synchronized void moveToInsertRow() throws SQLException { if ( !isUpdateable() ) { throw new PSQLException( "postgresql.updateable.notupdateable" ); } if (insertStatement != null) { insertStatement = null; } // make sure the underlying data is null clearRowBuffer(false); onInsertRow = true; doingUpdates = false; } private synchronized void clearRowBuffer(boolean copyCurrentRow) throws SQLException { // rowBuffer is the temporary storage for the row rowBuffer = new byte[fields.length][]; // inserts want an empty array while updates want a copy of the current row if (copyCurrentRow) { System.arraycopy(this_row, 0, rowBuffer, 0, this_row.length); } // clear the updateValues hashTable for the next set of updates updateValues.clear(); } public boolean rowDeleted() throws SQLException { return false; } public boolean rowInserted() throws SQLException { return false; } public boolean rowUpdated() throws SQLException { return false; } public synchronized void updateAsciiStream(int columnIndex, java.io.InputStream x, int length ) throws SQLException { if (x == null) { updateNull(columnIndex); return; } try { InputStreamReader reader = new InputStreamReader(x, "ASCII"); char data[] = new char[length]; int numRead = 0; while (true) { int n = reader.read(data, numRead, length - numRead); if (n == -1) break; numRead += n; if (numRead == length) break; } updateString(columnIndex, new String(data, 0, numRead)); } catch (UnsupportedEncodingException uee) { throw new PSQLException("postgresql.unusual", PSQLState.UNEXPECTED_ERROR, uee); } catch (IOException ie) { throw new PSQLException("postgresql.updateable.ioerror", ie); } } public synchronized void updateBigDecimal(int columnIndex, java.math.BigDecimal x ) throws SQLException { updateValue(columnIndex, x); } public synchronized void updateBinaryStream(int columnIndex, java.io.InputStream x, int length ) throws SQLException { if (x == null) { updateNull(columnIndex); return; } byte data[] = new byte[length]; int numRead = 0; try { while (true) { int n = x.read(data, numRead, length - numRead); if (n == -1) break; numRead += n; if (numRead == length) break; } } catch (IOException ie) { throw new PSQLException("postgresql.updateable.ioerror", ie); } if (numRead == length) { updateBytes(columnIndex, data); } else { // the stream contained less data than they said // perhaps this is an error? byte data2[] = new byte[numRead]; System.arraycopy(data, 0, data2, 0, numRead); updateBytes(columnIndex, data2); } } public synchronized void updateBoolean(int columnIndex, boolean x) throws SQLException { if ( Driver.logDebug ) Driver.debug("updating boolean " + fields[columnIndex - 1].getName() + "=" + x); updateValue(columnIndex, new Boolean(x)); } public synchronized void updateByte(int columnIndex, byte x) throws SQLException { updateValue(columnIndex, String.valueOf(x)); } public synchronized void updateBytes(int columnIndex, byte[] x) throws SQLException { updateValue(columnIndex, x); } public synchronized void updateCharacterStream(int columnIndex, java.io.Reader x, int length ) throws SQLException { if (x == null) { updateNull(columnIndex); return; } try { char data[] = new char[length]; int numRead = 0; while (true) { int n = x.read(data, numRead, length - numRead); if (n == -1) break; numRead += n; if (numRead == length) break; } updateString(columnIndex, new String(data, 0, numRead)); } catch (IOException ie) { throw new PSQLException("postgresql.updateable.ioerror", ie); } } public synchronized void updateDate(int columnIndex, java.sql.Date x) throws SQLException { updateValue(columnIndex, x); } public synchronized void updateDouble(int columnIndex, double x) throws SQLException { if ( Driver.logDebug ) Driver.debug("updating double " + fields[columnIndex - 1].getName() + "=" + x); updateValue(columnIndex, new Double(x)); } public synchronized void updateFloat(int columnIndex, float x) throws SQLException { if ( Driver.logDebug ) Driver.debug("updating float " + fields[columnIndex - 1].getName() + "=" + x); updateValue(columnIndex, new Float(x)); } public synchronized void updateInt(int columnIndex, int x) throws SQLException { if ( Driver.logDebug ) Driver.debug("updating int " + fields[columnIndex - 1].getName() + "=" + x); updateValue(columnIndex, new Integer(x)); } public synchronized void updateLong(int columnIndex, long x) throws SQLException { if ( Driver.logDebug ) Driver.debug("updating long " + fields[columnIndex - 1].getName() + "=" + x); updateValue(columnIndex, new Long(x)); } public synchronized void updateNull(int columnIndex) throws SQLException { updateValue(columnIndex, new NullObject()); } public synchronized void updateObject(int columnIndex, Object x) throws SQLException { if ( Driver.logDebug ) Driver.debug("updating object " + fields[columnIndex - 1].getName() + " = " + x); updateValue(columnIndex, x); } public synchronized void updateObject(int columnIndex, Object x, int scale) throws SQLException { if ( !isUpdateable() ) { throw new PSQLException( "postgresql.updateable.notupdateable" ); } this.updateObject(columnIndex, x); } public void refreshRow() throws SQLException { if ( !isUpdateable() ) { throw new PSQLException( "postgresql.updateable.notupdateable" ); } if (onInsertRow) throw new PSQLException("postgresql.res.oninsertrow"); if (isBeforeFirst() || isAfterLast()) return; StringBuffer selectSQL = new StringBuffer( "select "); final int numColumns = java.lang.reflect.Array.getLength(fields); for (int i = 0; i < numColumns; i++ ) { selectSQL.append( fields[i].getName() ); if ( i < numColumns - 1 ) { selectSQL.append(", "); } } selectSQL.append(" from " ).append(tableName).append(" where "); int numKeys = primaryKeys.size(); for ( int i = 0; i < numKeys; i++ ) { PrimaryKey primaryKey = ((PrimaryKey) primaryKeys.get(i)); selectSQL.append(primaryKey.name).append("= ?"); if ( i < numKeys - 1 ) { selectSQL.append(" and "); } } if ( Driver.logDebug ) Driver.debug("selecting " + selectSQL.toString()); selectStatement = ((java.sql.Connection) connection).prepareStatement(selectSQL.toString()); for ( int j = 0, i = 1; j < numKeys; j++, i++) { selectStatement.setObject( i, ((PrimaryKey) primaryKeys.get(j)).getValue() ); } AbstractJdbc2ResultSet rs = (AbstractJdbc2ResultSet) selectStatement.executeQuery(); if ( rs.first() ) { rowBuffer = rs.rowBuffer; } rows.setElementAt( rowBuffer, current_row ); this_row = rowBuffer; if ( Driver.logDebug ) Driver.debug("done updates"); rs.close(); selectStatement.close(); selectStatement = null; } public synchronized void updateRow() throws SQLException { if ( !isUpdateable() ) { throw new PSQLException( "postgresql.updateable.notupdateable" ); } if (isBeforeFirst() || isAfterLast()) { throw new PSQLException("postgresql.updateable.badupdateposition");
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?