📄 cachedresultset.java
字号:
} /** * Creates a parameter object for an UPDATE, DELETE or INSERT statement. * * @param pos the substitution position of the parameter marker in the SQL * @param info the <code>ColInfo</code> column descriptor * @param value the column data item * @return the new parameter as a <code>ParamInfo</code> object */ protected static ParamInfo buildParameter(int pos, ColInfo info, Object value, boolean isUnicode) throws SQLException { int length = 0; if (value instanceof String) { length = ((String)value).length(); } else if (value instanceof byte[]) { length = ((byte[])value).length; } else if (value instanceof BlobImpl) { BlobImpl blob = (BlobImpl)value; value = blob.getBinaryStream(); length = (int)blob.length(); } else if (value instanceof ClobImpl) { ClobImpl clob = (ClobImpl)value; value = clob.getCharacterStream(); length = (int)clob.length(); } ParamInfo param = new ParamInfo(info, null, value, length); param.isUnicode = info.sqlType.equals("nvarchar") || info.sqlType.equals("nchar") || info.sqlType.equals("ntext") || isUnicode; param.markerPos = pos; return param; } /** * Sets the specified column's data value. * * @param colIndex index of the column * @param value new column value * @return the value, possibly converted to an internal type */ protected Object setColValue(int colIndex, int jdbcType, Object value, int length) throws SQLException { value = super.setColValue(colIndex, jdbcType, value, length); if (!onInsertRow && currentRow == null) { throw new SQLException(Messages.get("error.resultset.norow"), "24000"); } colIndex--; ParamInfo pi; ColInfo ci = columns[colIndex]; boolean isUnicode = TdsData.isUnicode(ci); if (onInsertRow) { pi = insertRow[colIndex]; if (pi == null) { pi = new ParamInfo(-1, isUnicode); pi.collation = ci.collation; pi.charsetInfo = ci.charsetInfo; insertRow[colIndex] = pi; } } else { if (updateRow == null) { updateRow = new ParamInfo[columnCount]; } pi = updateRow[colIndex]; if (pi == null) { pi = new ParamInfo(-1, isUnicode); pi.collation = ci.collation; pi.charsetInfo = ci.charsetInfo; updateRow[colIndex] = pi; } } if (value == null) { pi.value = null; pi.length = 0; pi.jdbcType = ci.jdbcType; pi.isSet = true; } else { pi.value = value; pi.length = length; pi.jdbcType = jdbcType; pi.isSet = true; } return value; } /** * Builds a WHERE clause for UPDATE or DELETE statements. * * @param sql the SQL Statement to append the WHERE clause to * @param params the parameter descriptor array for this statement * @param select true if this WHERE clause will be used in a select * statement * @return the parameter list as a <code>ParamInfo[]</code> * @throws SQLException if an error occurs */ ParamInfo[] buildWhereClause(StringBuffer sql, ArrayList params, boolean select) throws SQLException { // // Now construct where clause // sql.append(" WHERE "); if (cursorName != null) { // // Use a positioned update // sql.append(" CURRENT OF ").append(cursorName); } else { int count = 0; for (int i = 0; i < columns.length; i++) { if (currentRow[i] == null) { if (!columns[i].sqlType.equals("text") && !columns[i].sqlType.equals("ntext") && !columns[i].sqlType.equals("image") && columns[i].tableName != null) { if (count > 0) { sql.append(" AND "); } sql.append(columns[i].realName); sql.append(" IS NULL"); } } else { if (isKeyed && select) { // For refresh select only include key columns if (columns[i].isKey) { if (count > 0) { sql.append(" AND "); } sql.append(columns[i].realName); sql.append("=?"); count++; params.add(buildParameter(sql.length() - 1, columns[i], currentRow[i], connection.isUseUnicode())); } } else { // Include all available 'searchable' columns in updates/deletes to protect // against lost updates. if (!columns[i].sqlType.equals("text") && !columns[i].sqlType.equals("ntext") && !columns[i].sqlType.equals("image") && columns[i].tableName != null) { if (count > 0) { sql.append(" AND "); } sql.append(columns[i].realName); sql.append("=?"); count++; params.add(buildParameter(sql.length() - 1, columns[i], currentRow[i], connection.isUseUnicode())); } } } } } return (ParamInfo[]) params.toArray(new ParamInfo[params.size()]); } /** * Refreshes a result set row from keyed tables. * <p/> * If all the tables in the result set have primary keys then the result * set row can be refreshed by refetching the individual table rows. * * @throws SQLException if an error occurs */ protected void refreshKeyedRows() throws SQLException { // // Construct a SELECT statement // StringBuffer sql = new StringBuffer(100 + columns.length * 10); sql.append("SELECT "); int count = 0; for (int i = 0; i < columns.length; i++) { if (!columns[i].isKey && columns[i].tableName != null) { if (count > 0) { sql.append(','); } sql.append(columns[i].realName); count++; } } if (count == 0) { // No non key columns in this table? return; } sql.append(" FROM "); sql.append(tableName); // // Construct a where clause using keyed columns only // ArrayList params = new ArrayList(); buildWhereClause(sql, params, true); ParamInfo parameters[] = (ParamInfo[]) params.toArray(new ParamInfo[params.size()]); // // Execute the select // TdsCore tds = statement.getTds(); tds.executeSQL(sql.toString(), null, parameters, false, 0, statement.getMaxRows(), statement.getMaxFieldSize(), true); if (!tds.isEndOfResponse()) { if (tds.getMoreResults() && tds.getNextRow()) { // refresh the row data Object col[] = tds.getRowData(); count = 0; for (int i = 0; i < columns.length; i++) { if (!columns[i].isKey) { currentRow[i] = col[count++]; } } } else { currentRow = null; } } else { currentRow = null; } tds.clearResponseQueue(); statement.getMessages().checkErrors(); if (currentRow == null) { rowData.set(pos-1, null); rowDeleted = true; } } /** * Refreshes the row by rereading the result set. * <p/> * Obviously very slow on large result sets but may be the only option if * tables do not have keys. */ protected void refreshReRead() throws SQLException { int savePos = pos; cursorCreate(); absolute(savePos); }//// -------------------- java.sql.ResultSet methods -------------------// public void setFetchSize(int size) throws SQLException { sizeChanged = size != fetchSize; super.setFetchSize(size); } public void afterLast() throws SQLException { checkOpen(); checkScrollable(); if (pos != POS_AFTER_LAST) { cursorFetch(rowsInResult+1); } } public void beforeFirst() throws SQLException { checkOpen(); checkScrollable(); if (pos != POS_BEFORE_FIRST) { cursorFetch(0); } } public void cancelRowUpdates() throws SQLException { checkOpen(); checkUpdateable(); if (onInsertRow) { throw new SQLException(Messages.get("error.resultset.insrow"), "24000"); } if (updateRow != null) { rowUpdated = false; for (int i = 0; i < updateRow.length; i++) { if (updateRow[i] != null) { updateRow[i].clearInValue(); } } } } public void close() throws SQLException { if (!closed) { try { cursorClose(); } finally { closed = true; statement = null; } } } public void deleteRow() throws SQLException { checkOpen(); checkUpdateable(); if (currentRow == null) { throw new SQLException(Messages.get("error.resultset.norow"), "24000"); } if (onInsertRow) { throw new SQLException(Messages.get("error.resultset.insrow"), "24000"); } // // Construct an SQL DELETE statement // StringBuffer sql = new StringBuffer(128); ArrayList params = new ArrayList(); sql.append("DELETE FROM "); sql.append(tableName); // // Create the WHERE clause // ParamInfo parameters[] = buildWhereClause(sql, params, false); // // Execute the delete 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 == 0) { // No delete. Possibly row was changed on database by another user? throw new SQLException(Messages.get("error.resultset.deletefail"), "24000"); } rowDeleted = true; currentRow = null; if (resultSetType != ResultSet.TYPE_FORWARD_ONLY) { // Leave a 'hole' in the result set array. rowData.set(pos-1, null); } } public void insertRow() throws SQLException { checkOpen(); checkUpdateable(); if (!onInsertRow) { throw new SQLException(Messages.get("error.resultset.notinsrow"), "24000"); } if (!tempResultSet) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -