📄 cachedresultset.java
字号:
// // Construct an SQL INSERT statement // StringBuffer sql = new StringBuffer(128); ArrayList params = new ArrayList(); sql.append("INSERT INTO "); sql.append(tableName); int sqlLen = sql.length(); // // Create column list // sql.append(" ("); int count = 0; for (int i = 0; i < columnCount; i++) { if (insertRow[i] != null) { if (count > 0) { sql.append(", "); } sql.append(columns[i].realName); count++; } } // // Create new values list // sql.append(") VALUES("); count = 0; for (int i = 0; i < columnCount; i++) { if (insertRow[i] != null) { if (count > 0) { sql.append(", "); } sql.append('?'); insertRow[i].markerPos = sql.length()-1; params.add(insertRow[i]); count++; } } sql.append(')'); if (count == 0) { // Empty insert sql.setLength(sqlLen); if (isSybase) { sql.append(" VALUES()"); } else { sql.append(" DEFAULT VALUES"); } } ParamInfo parameters[] = (ParamInfo[]) params.toArray(new ParamInfo[params.size()]); // // execute the insert statement // updateTds.executeSQL(sql.toString(), null, parameters, false, 0, statement.getMaxRows(), statement.getMaxFieldSize(), true); int updateCount = 0; while (!updateTds.isEndOfResponse()) { if (!updateTds.getMoreResults()) { if (updateTds.isUpdateCount()) { updateCount = updateTds.getUpdateCount(); } } } updateTds.clearResponseQueue(); statement.getMessages().checkErrors(); if (updateCount < 1) { // No Insert. Probably will not get here as duplicate key etc // will have already been reported as an exception. throw new SQLException(Messages.get("error.resultset.insertfail"), "24000"); } } // if (resultSetType >= ResultSet.TYPE_SCROLL_SENSITIVE || (resultSetType == ResultSet.TYPE_FORWARD_ONLY && cursorName == null)) { // // Now insert copy of row into result set buffer // ConnectionJDBC2 con = (ConnectionJDBC2)statement.getConnection(); Object row[] = newRow(); for (int i = 0; i < insertRow.length; i++) { if (insertRow[i] != null) { row[i] = Support.convert(con, insertRow[i].value, columns[i].jdbcType, con.getCharset()); } } rowData.add(row); } rowsInResult++; initialRowCnt++; // // Clear row data // for (int i = 0; insertRow != null && i < insertRow.length; i++) { if (insertRow[i] != null) { insertRow[i].clearInValue(); } } } public void moveToCurrentRow() throws SQLException { checkOpen(); checkUpdateable(); insertRow = null; onInsertRow = false; } public void moveToInsertRow() throws SQLException { checkOpen(); checkUpdateable(); insertRow = new ParamInfo[columnCount]; onInsertRow = true; } public void refreshRow() throws SQLException { checkOpen(); if (onInsertRow) { throw new SQLException(Messages.get("error.resultset.insrow"), "24000"); } // // If row is being updated discard updates now // if (concurrency != ResultSet.CONCUR_READ_ONLY) { cancelRowUpdates(); rowUpdated = false; } if (resultSetType == ResultSet.TYPE_FORWARD_ONLY || currentRow == null) { // Do not try and refresh the row in these cases. return; } // // If result set is keyed we can refresh the row data from the // database using the key. // NB. MS SQL Server #Temporary tables with keys are not identified correctly // in the column meta data sent after 'for browse'. This means that // temporary tables can not be used with this logic. // if (isKeyed) { // OK all tables are keyed refreshKeyedRows(); } else { // No good have to use brute force approach refreshReRead(); } } public void updateRow() throws SQLException { checkOpen(); checkUpdateable(); rowUpdated = false; rowDeleted = false; if (currentRow == null) { throw new SQLException(Messages.get("error.resultset.norow"), "24000"); } if (onInsertRow) { throw new SQLException(Messages.get("error.resultset.insrow"), "24000"); } if (updateRow == null) { // Nothing to update return; } boolean keysChanged = false; // // Construct an SQL UPDATE statement // StringBuffer sql = new StringBuffer(128); ArrayList params = new ArrayList(); sql.append("UPDATE "); sql.append(tableName); // // OK now create assign new values // sql.append(" SET "); int count = 0; for (int i = 0; i < columnCount; i++) { if (updateRow[i] != null) { if (count > 0) { sql.append(", "); } sql.append(columns[i].realName); sql.append("=?"); updateRow[i].markerPos = sql.length()-1; params.add(updateRow[i]); count++; if (columns[i].isKey) { // Key is changing so in memory row will need to be deleted // and reinserted at end of row buffer. keysChanged = true; } } } if (count == 0) { // There are no columns to update in this table // so bail out now. return; } // // Now construct where clause // ParamInfo parameters[] = buildWhereClause(sql, params, false); // // Now execute update // updateTds.executeSQL(sql.toString(), null, parameters, false, 0, statement.getMaxRows(), statement.getMaxFieldSize(), true); int updateCount = 0; while (!updateTds.isEndOfResponse()) { if (!updateTds.getMoreResults()) { if (updateTds.isUpdateCount()) { updateCount = updateTds.getUpdateCount(); } } } updateTds.clearResponseQueue(); statement.getMessages().checkErrors(); if (updateCount == 0) { // No update. Possibly row was changed on database by another user? throw new SQLException(Messages.get("error.resultset.updatefail"), "24000"); } // // Update local copy of data // if (resultSetType != ResultSet.TYPE_SCROLL_INSENSITIVE) { // Make in memory copy reflect database update // Could use refreshRow but this is much faster. ConnectionJDBC2 con = (ConnectionJDBC2)statement.getConnection(); for (int i = 0; i < updateRow.length; i++) { if (updateRow[i] != null) { if (updateRow[i].value instanceof byte[] && (columns[i].jdbcType == Types.CHAR || columns[i].jdbcType == Types.VARCHAR || columns[i].jdbcType == Types.LONGVARCHAR)) { // Need to handle byte[] to varchar otherwise field // will be set to hex string rather than characters. try { currentRow[i] = new String((byte[])updateRow[i].value, con.getCharset()); } catch (UnsupportedEncodingException e) { currentRow[i] = new String((byte[])updateRow[i].value); } } else { currentRow[i] = Support.convert(con, updateRow[i].value, columns[i].jdbcType, con.getCharset()); } } } } // // Update state of cached row data // if (keysChanged && resultSetType >= ResultSet.TYPE_SCROLL_SENSITIVE) { // Leave hole at current position and add updated row to end of set rowData.add(currentRow); rowsInResult = rowData.size(); rowData.set(pos-1, null); currentRow = null; rowDeleted = true; } else { rowUpdated = true; } // // Clear update values // cancelRowUpdates(); } public boolean first() throws SQLException { checkOpen(); checkScrollable(); return cursorFetch(1); } public boolean isLast() throws SQLException { checkOpen(); return(pos == rowsInResult) && (rowsInResult != 0); } public boolean last() throws SQLException { checkOpen(); checkScrollable(); return cursorFetch(rowsInResult); } public boolean next() throws SQLException { checkOpen(); if (pos != POS_AFTER_LAST) { return cursorFetch(pos+1); } else { return false; } } public boolean previous() throws SQLException { checkOpen(); checkScrollable(); if (pos == POS_AFTER_LAST) { pos = rowsInResult+1; } return cursorFetch(pos-1); } public boolean rowDeleted() throws SQLException { checkOpen(); return rowDeleted; } public boolean rowInserted() throws SQLException { checkOpen();// return pos > initialRowCnt; return false; // Same as MSCursorResultSet } public boolean rowUpdated() throws SQLException { checkOpen();// return rowUpdated; return false; // Same as MSCursorResultSet } public boolean absolute(int row) throws SQLException { checkOpen(); checkScrollable(); if (row < 1) { row = (rowsInResult + 1) + row; } return cursorFetch(row); } public boolean relative(int row) throws SQLException { checkScrollable(); if (pos == POS_AFTER_LAST) { return absolute((rowsInResult+1)+row); } else { return absolute(pos+row); } } public String getCursorName() throws SQLException { checkOpen(); // Hide internal cursor names if (cursorName != null && !cursorName.startsWith("_jtds")) { return this.cursorName; } throw new SQLException(Messages.get("error.resultset.noposupdate"), "24000"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -