📄 updatableresultset.java
字号:
this.isUpdatable = false; this.notUpdatableReason = sqlEx.getMessage(); } } /** * JDBC 2.0 Delete the current row from the result set and the underlying * database. Cannot be called when on the insert row. * * @exception SQLException * if a database-access error occurs, or if called when on * the insert row. * @throws SQLException * if the ResultSet is not updatable or some other error occurs */ public synchronized void deleteRow() throws SQLException { checkClosed(); if (!this.isUpdatable) { throw new NotUpdatable(this.notUpdatableReason); } if (this.onInsertRow) { throw SQLError.createSQLException(Messages.getString("UpdatableResultSet.1")); //$NON-NLS-1$ } else if (this.rowData.size() == 0) { throw SQLError.createSQLException(Messages.getString("UpdatableResultSet.2")); //$NON-NLS-1$ } else if (isBeforeFirst()) { throw SQLError.createSQLException(Messages.getString("UpdatableResultSet.3")); //$NON-NLS-1$ } else if (isAfterLast()) { throw SQLError.createSQLException(Messages.getString("UpdatableResultSet.4")); //$NON-NLS-1$ } if (this.deleter == null) { if (this.deleteSQL == null) { generateStatements(); } this.deleter = (PreparedStatement) this.connection .clientPrepareStatement(this.deleteSQL); } this.deleter.clearParameters(); String characterEncoding = null; if (this.connection.getUseUnicode()) { characterEncoding = this.connection.getEncoding(); } // // FIXME: Use internal routines where possible for character // conversion! try { int numKeys = this.primaryKeyIndicies.size(); if (numKeys == 1) { int index = ((Integer) this.primaryKeyIndicies.get(0)) .intValue(); String currentVal = ((characterEncoding == null) ? new String( (byte[]) this.thisRow.getColumnValue(index)) : new String( (byte[]) this.thisRow.getColumnValue(index), characterEncoding)); this.deleter.setString(1, currentVal); } else { for (int i = 0; i < numKeys; i++) { int index = ((Integer) this.primaryKeyIndicies.get(i)) .intValue(); String currentVal = ((characterEncoding == null) ? new String( (byte[]) this.thisRow.getColumnValue(index)) : new String((byte[]) this.thisRow.getColumnValue(index), characterEncoding)); this.deleter.setString(i + 1, currentVal); } } this.deleter.executeUpdate(); this.rowData.removeRow(this.rowData.getCurrentRowNumber()); } catch (java.io.UnsupportedEncodingException encodingEx) { throw SQLError.createSQLException(Messages.getString("UpdatableResultSet.39", //$NON-NLS-1$ new Object[] { this.charEncoding }) //$NON-NLS-1$ , SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$ //$NON-NLS-2$ } } private synchronized void extractDefaultValues() throws SQLException { java.sql.DatabaseMetaData dbmd = this.connection.getMetaData(); this.defaultColumnValue = new byte[this.fields.length][]; java.sql.ResultSet columnsResultSet = null; Iterator referencedDbs = this.databasesUsedToTablesUsed.entrySet().iterator(); while (referencedDbs.hasNext()) { Map.Entry dbEntry = (Map.Entry)referencedDbs.next(); String databaseName = dbEntry.getKey().toString(); Iterator referencedTables = ((Map)dbEntry.getValue()).entrySet().iterator(); while (referencedTables.hasNext()) { Map.Entry tableEntry = (Map.Entry)referencedTables.next(); String tableName = tableEntry.getKey().toString(); Map columnNamesToIndices = (Map)tableEntry.getValue(); try { columnsResultSet = dbmd.getColumns(this.catalog, null, tableName, "%"); //$NON-NLS-1$ while (columnsResultSet.next()) { String columnName = columnsResultSet.getString("COLUMN_NAME"); //$NON-NLS-1$ byte[] defaultValue = columnsResultSet.getBytes("COLUMN_DEF"); //$NON-NLS-1$ if (columnNamesToIndices.containsKey(columnName)) { int localColumnIndex = ((Integer)columnNamesToIndices.get(columnName)).intValue(); this.defaultColumnValue[localColumnIndex] = defaultValue; } // else assert? } } finally { if (columnsResultSet != null) { columnsResultSet.close(); columnsResultSet = null; } } } } } /** * JDBC 2.0 * * <p> * Moves to the first row in the result set. * </p> * * @return true if on a valid row, false if no rows in the result set. * * @exception SQLException * if a database-access error occurs, or result set type is * TYPE_FORWARD_ONLY. */ public synchronized boolean first() throws SQLException { return super.first(); } /** * Figure out whether or not this ResultSet is updateable, and if so, * generate the PreparedStatements to support updates. * * @throws SQLException * DOCUMENT ME! * @throws NotUpdatable * DOCUMENT ME! */ protected synchronized void generateStatements() throws SQLException { if (!this.isUpdatable) { this.doingUpdates = false; this.onInsertRow = false; throw new NotUpdatable(this.notUpdatableReason); } String quotedId = getQuotedIdChar(); Map tableNamesSoFar = null; if (this.connection.lowerCaseTableNames()) { tableNamesSoFar = new TreeMap(String.CASE_INSENSITIVE_ORDER); this.databasesUsedToTablesUsed = new TreeMap(String.CASE_INSENSITIVE_ORDER); } else { tableNamesSoFar = new TreeMap(); this.databasesUsedToTablesUsed = new TreeMap(); } this.primaryKeyIndicies = new ArrayList(); StringBuffer fieldValues = new StringBuffer(); StringBuffer keyValues = new StringBuffer(); StringBuffer columnNames = new StringBuffer(); StringBuffer insertPlaceHolders = new StringBuffer(); StringBuffer allTablesBuf = new StringBuffer(); Map columnIndicesToTable = new HashMap(); boolean firstTime = true; boolean keysFirstTime = true; String equalsStr = this.connection.versionMeetsMinimum(3, 23, 0) ? "<=>" : "="; for (int i = 0; i < this.fields.length; i++) { StringBuffer tableNameBuffer = new StringBuffer(); Map updColumnNameToIndex = null; // FIXME: What about no table? if (this.fields[i].getOriginalTableName() != null) { String databaseName = this.fields[i].getDatabaseName(); if ((databaseName != null) && (databaseName.length() > 0)) { tableNameBuffer.append(quotedId); tableNameBuffer.append(databaseName); tableNameBuffer.append(quotedId); tableNameBuffer.append('.'); } String tableOnlyName = this.fields[i].getOriginalTableName(); tableNameBuffer.append(quotedId); tableNameBuffer.append(tableOnlyName); tableNameBuffer.append(quotedId); String fqTableName = tableNameBuffer.toString(); if (!tableNamesSoFar.containsKey(fqTableName)) { if (!tableNamesSoFar.isEmpty()) { allTablesBuf.append(','); } allTablesBuf.append(fqTableName); tableNamesSoFar.put(fqTableName, fqTableName); } columnIndicesToTable.put(new Integer(i), fqTableName); updColumnNameToIndex = getColumnsToIndexMapForTableAndDB(databaseName, tableOnlyName); } else { String tableOnlyName = this.fields[i].getTableName(); if (tableOnlyName != null) { tableNameBuffer.append(quotedId); tableNameBuffer.append(tableOnlyName); tableNameBuffer.append(quotedId); String fqTableName = tableNameBuffer.toString(); if (!tableNamesSoFar.containsKey(fqTableName)) { if (!tableNamesSoFar.isEmpty()) { allTablesBuf.append(','); } allTablesBuf.append(fqTableName); tableNamesSoFar.put(fqTableName, fqTableName); } columnIndicesToTable.put(new Integer(i), fqTableName); updColumnNameToIndex = getColumnsToIndexMapForTableAndDB(this.catalog, tableOnlyName); } } String originalColumnName = this.fields[i].getOriginalName(); String columnName = null; if (this.connection.getIO().hasLongColumnInfo() && (originalColumnName != null) && (originalColumnName.length() > 0)) { columnName = originalColumnName; } else { columnName = this.fields[i].getName(); } if (updColumnNameToIndex != null && columnName != null) { updColumnNameToIndex.put(columnName, new Integer(i)); } String originalTableName = this.fields[i].getOriginalTableName(); String tableName = null; if (this.connection.getIO().hasLongColumnInfo() && (originalTableName != null) && (originalTableName.length() > 0)) { tableName = originalTableName; } else { tableName = this.fields[i].getTableName(); } StringBuffer fqcnBuf = new StringBuffer(); String databaseName = this.fields[i].getDatabaseName(); if (databaseName != null && databaseName.length() > 0) { fqcnBuf.append(quotedId); fqcnBuf.append(databaseName); fqcnBuf.append(quotedId); fqcnBuf.append('.'); } fqcnBuf.append(quotedId); fqcnBuf.append(tableName); fqcnBuf.append(quotedId); fqcnBuf.append('.'); fqcnBuf.append(quotedId); fqcnBuf.append(columnName); fqcnBuf.append(quotedId); String qualifiedColumnName = fqcnBuf.toString(); if (this.fields[i].isPrimaryKey()) { this.primaryKeyIndicies.add(Constants.integerValueOf(i)); if (!keysFirstTime) { keyValues.append(" AND "); //$NON-NLS-1$ } else { keysFirstTime = false; } keyValues.append(qualifiedColumnName); keyValues.append(equalsStr); keyValues.append("?"); //$NON-NLS-1$ } if (firstTime) { firstTime = false; fieldValues.append("SET "); //$NON-NLS-1$ } else { fieldValues.append(","); //$NON-NLS-1$ columnNames.append(","); //$NON-NLS-1$ insertPlaceHolders.append(","); //$NON-NLS-1$ } insertPlaceHolders.append("?"); //$NON-NLS-1$ columnNames.append(qualifiedColumnName); fieldValues.append(qualifiedColumnName); fieldValues.append("=?"); //$NON-NLS-1$ } this.qualifiedAndQuotedTableName = allTablesBuf.toString(); this.updateSQL = "UPDATE " + this.qualifiedAndQuotedTableName + " " //$NON-NLS-1$ //$NON-NLS-2$ + fieldValues.toString() //$NON-NLS-1$ //$NON-NLS-2$ + " WHERE " + keyValues.toString(); //$NON-NLS-1$ this.insertSQL = "INSERT INTO " + this.qualifiedAndQuotedTableName //$NON-NLS-1$ + " (" + columnNames.toString() //$NON-NLS-1$ //$NON-NLS-2$ + ") VALUES (" + insertPlaceHolders.toString() + ")"; //$NON-NLS-1$ //$NON-NLS-2$ this.refreshSQL = "SELECT " + columnNames.toString() + " FROM " //$NON-NLS-1$ //$NON-NLS-2$ + this.qualifiedAndQuotedTableName //$NON-NLS-1$ //$NON-NLS-2$ + " WHERE " + keyValues.toString(); //$NON-NLS-1$ this.deleteSQL = "DELETE FROM " + this.qualifiedAndQuotedTableName //$NON-NLS-1$ + " WHERE " //$NON-NLS-1$ //$NON-NLS-2$ + keyValues.toString(); } private Map getColumnsToIndexMapForTableAndDB(String databaseName, String tableName) { Map nameToIndex; Map tablesUsedToColumnsMap = (Map)this.databasesUsedToTablesUsed.get(databaseName); if (tablesUsedToColumnsMap == null) { if (this.connection.lowerCaseTableNames()) { tablesUsedToColumnsMap = new TreeMap(String.CASE_INSENSITIVE_ORDER); } else { tablesUsedToColumnsMap = new TreeMap(); } this.databasesUsedToTablesUsed.put(databaseName, tablesUsedToColumnsMap); } nameToIndex = (Map)tablesUsedToColumnsMap.get(tableName); if (nameToIndex == null) { nameToIndex = new HashMap(); tablesUsedToColumnsMap.put(tableName, nameToIndex); } return nameToIndex; } private synchronized SingleByteCharsetConverter getCharConverter() throws SQLException { if (!this.initializedCharConverter) { this.initializedCharConverter = true; if (this.connection.getUseUnicode()) { this.charEncoding = connection.getEncoding(); this.charConverter = this.connection .getCharsetConverter(this.charEncoding); } } return this.charConverter; } /** * JDBC 2.0 Return the concurrency of this result set. The concurrency used * is determined by the statement that created the result set. * * @return the concurrency type, CONCUR_READ_ONLY, etc. * * @exception SQLException * if a database-access error occurs */ public int getConcurrency() throws SQLException { return (this.isUpdatable ? CONCUR_UPDATABLE : CONCUR_READ_ONLY); } private synchronized String getQuotedIdChar() throws SQLException { if (this.quotedIdChar == null) { boolean useQuotedIdentifiers = this.connection .supportsQuotedIdentifiers(); if (useQuotedIdentifiers) { java.sql.DatabaseMetaData dbmd = this.connection.getMetaData(); this.quotedIdChar = dbmd.getIdentifierQuoteString(); } else { this.quotedIdChar = ""; //$NON-NLS-1$ } } return this.quotedIdChar; } /** * JDBC 2.0 Insert the contents of the insert row into the result set and * the database. Must be on the insert row when this method is called. * * @exception SQLException * if a database-access error occurs, if called when not on * the insert row, or if all non-nullable columns in the * insert row have not been given a value */ public synchronized void insertRow() throws SQLException { checkClosed(); if (!this.onInsertRow) { throw SQLError.createSQLException(Messages.getString("UpdatableResultSet.7")); //$NON-NLS-1$ } this.inserter.executeUpdate(); long autoIncrementId = this.inserter.getLastInsertID(); int numFields = this.fields.length; byte[][] newRow = new byte[numFields][];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -