public void close() throws SQLException { // closing an already closed Statement is a no-op. if (isClosed) return ; closeForNextExecution(); if (preparedQuery != null) preparedQuery.close(); isClosed = true; }
PgPreparedStatement(PgConnection connection, CachedQuery query, int rsType, int rsConcurrency, int rsHoldability) throws SQLException { super(connection, rsType, rsConcurrency, rsHoldability); this.preparedQuery = query; this.preparedParameters = this.preparedQuery.query.createParameterList(); // TODO: this.wantsGeneratedKeysAlways = true; setPoolable(true); // As per JDBC spec: prepared and callable statements are poolable by }
/** * Returns the SQL statement with the current template values substituted. * * @return SQL statement with the current template values substituted */ public String toString() { if (preparedQuery == null) { return super.toString(); } return preparedQuery.query.toString(preparedParameters); }
protected final void execute(CachedQuery cachedQuery, ParameterList queryParameters, int flags) throws SQLException { try { executeInternal(cachedQuery, queryParameters, flags); } catch (SQLException e) { // Don't retry composite queries as it might get partially executed if (cachedQuery.query.getSubqueries() != null || !connection.getQueryExecutor().willHealOnRetry(e)) { throw e; } cachedQuery.query.close(); // Execute the query one more time executeInternal(cachedQuery, queryParameters, flags); } }
@Override public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException { if (columnNames != null && columnNames.length == 0) { return prepareStatement(sql); } CachedQuery cachedQuery = borrowReturningQuery(sql, columnNames); PgPreparedStatement ps = new PgPreparedStatement(this, cachedQuery, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, getHoldability()); Query query = cachedQuery.query; SqlCommand sqlCommand = query.getSqlCommand(); if (sqlCommand != null) { ps.wantsGeneratedKeysAlways = sqlCommand.isReturningKeywordPresent(); } else { // If composite query is given, just ignore "generated keys" arguments } return ps; } }
boolean hasRewrites = false; for (Query query : queries) { int batchSize = query.getBatchSize(); totalRows += batchSize; hasRewrites |= batchSize > 1; for (int i = 0; i < queries.length; i++) { Query query = queries[i]; int batchSize = query.getBatchSize(); int superBatchResult = updateCounts[i]; if (batchSize == 1) {
private int findColumnIndex(String columnName) { if (columnNameIndexMap == null) { if (originalQuery != null) { columnNameIndexMap = originalQuery.getResultSetColumnNameIndexMap(); } if (columnNameIndexMap == null) { columnNameIndexMap = createColumnNameIndexMap(fields, connection.isColumnSanitiserDisabled()); } } Integer index = columnNameIndexMap.get(columnName); if (index != null) { return index; } index = columnNameIndexMap.get(columnName.toLowerCase(Locale.US)); if (index != null) { columnNameIndexMap.put(columnName, index); return index; } index = columnNameIndexMap.get(columnName.toUpperCase(Locale.US)); if (index != null) { columnNameIndexMap.put(columnName, index); return index; } return 0; }
@Override public String nativeSQL(String sql) throws SQLException { checkClosed(); CachedQuery cachedQuery = queryExecutor.createQuery(sql, false, true); return cachedQuery.query.getNativeSql(); }
private void executeTransactionCommand(Query query) throws SQLException { int flags = QueryExecutor.QUERY_NO_METADATA | QueryExecutor.QUERY_NO_RESULTS | QueryExecutor.QUERY_SUPPRESS_BEGIN; if (prepareThreshold == 0) { flags |= QueryExecutor.QUERY_ONESHOT; } try { getQueryExecutor().execute(query, null, new TransactionCommandHandler(), 0, 0, flags); } catch (SQLException e) { // Don't retry composite queries as it might get partially executed if (query.getSubqueries() != null || !queryExecutor.willHealOnRetry(e)) { throw e; } query.close(); // retry getQueryExecutor().execute(query, null, new TransactionCommandHandler(), 0, 0, flags); } }
private boolean executeCachedSql(String sql, int flags, String[] columnNames) throws SQLException { PreferQueryMode preferQueryMode = connection.getPreferQueryMode(); // Simple statements should not replace ?, ? with $1, $2 boolean shouldUseParameterized = false; QueryExecutor queryExecutor = connection.getQueryExecutor(); Object key = queryExecutor .createQueryKey(sql, replaceProcessingEnabled, shouldUseParameterized, columnNames); CachedQuery cachedQuery; boolean shouldCache = preferQueryMode == PreferQueryMode.EXTENDED_CACHE_EVERYTHING; if (shouldCache) { cachedQuery = queryExecutor.borrowQueryByKey(key); } else { cachedQuery = queryExecutor.createQueryByKey(key); } if (wantsGeneratedKeysOnce) { SqlCommand sqlCommand = cachedQuery.query.getSqlCommand(); wantsGeneratedKeysOnce = sqlCommand != null && sqlCommand.isReturningKeywordPresent(); } boolean res; try { res = executeWithFlags(cachedQuery, flags); } finally { if (shouldCache) { queryExecutor.releaseQuery(cachedQuery); } } return res; }
public String toString() { if (preparedQuery == null) return super.toString(); return preparedQuery.toString(preparedParameters); }
public static byte[][] run(QueryExecutor executor, String queryString, boolean wantResults) throws SQLException { Query query = executor.createSimpleQuery(queryString); SimpleResultHandler handler = new SimpleResultHandler(); int flags = QueryExecutor.QUERY_ONESHOT | QueryExecutor.QUERY_SUPPRESS_BEGIN | QueryExecutor.QUERY_EXECUTE_AS_SIMPLE; if (!wantResults) { flags |= QueryExecutor.QUERY_NO_RESULTS | QueryExecutor.QUERY_NO_METADATA; } try { executor.execute(query, null, handler, 0, 0, flags); } finally { query.close(); } if (!wantResults) { return null; } List<byte[][]> tuples = handler.getResults(); if (tuples == null || tuples.size() != 1) { throw new PSQLException(GT.tr("An unexpected result was returned by a query."), PSQLState.CONNECTION_UNABLE_TO_CONNECT); } return tuples.get(0); }
public AbstractJdbc2Statement(AbstractJdbc2Connection connection, String sql, boolean isCallable, int rsType, int rsConcurrency) throws SQLException { this.connection = connection; this.lastSimpleQuery = null; String parsed_sql = replaceProcessing(sql); if (isCallable) parsed_sql = modifyJdbcCall(parsed_sql); this.preparedQuery = connection.getQueryExecutor().createParameterizedQuery(parsed_sql); this.preparedParameters = preparedQuery.createParameterList(); int inParamCount = preparedParameters.getInParameterCount() + 1; this.testReturn = new int[inParamCount]; this.functionReturnType = new int[inParamCount]; resultsettype = rsType; concurrency = rsConcurrency; }
private void parseQuery() { String l_sql = originalQuery.toString(null); StringTokenizer st = new StringTokenizer(l_sql, " \r\t\n"); boolean tableFound = false; boolean tablesChecked = false; String name = ""; singleTable = true; while (!tableFound && !tablesChecked && st.hasMoreTokens()) { name = st.nextToken(); if ("from".equalsIgnoreCase(name)) { tableName = st.nextToken(); if ("only".equalsIgnoreCase(tableName)) { tableName = st.nextToken(); onlyTable = "ONLY "; } tableFound = true; } } }
protected void closeForNextExecution() throws SQLException { // Every statement execution clears any previous warnings. clearWarnings(); // Close any existing resultsets associated with this statement. while (firstUnclosedResult != null) { if (firstUnclosedResult.getResultSet() != null) firstUnclosedResult.getResultSet().close(); firstUnclosedResult = firstUnclosedResult.getNext(); } result = null; if (lastSimpleQuery != null) { lastSimpleQuery.close(); lastSimpleQuery = null; } if (generatedKeys != null) { if (generatedKeys.getResultSet() != null) { generatedKeys.getResultSet().close(); } generatedKeys = null; } }
public AbstractJdbc2Statement(AbstractJdbc2Connection connection, String sql, boolean isCallable, int rsType, int rsConcurrency) throws SQLException { this.connection = connection; this.lastSimpleQuery = null; String parsed_sql = replaceProcessing(sql); if (isCallable) parsed_sql = modifyJdbcCall(parsed_sql); this.preparedQuery = connection.getQueryExecutor().createParameterizedQuery(parsed_sql); this.preparedParameters = preparedQuery.createParameterList(); int inParamCount = preparedParameters.getInParameterCount() + 1; this.testReturn = new int[inParamCount]; this.functionReturnType = new int[inParamCount]; resultsettype = rsType; concurrency = rsConcurrency; }
private void parseQuery() { String l_sql = originalQuery.toString(null); StringTokenizer st = new StringTokenizer(l_sql, " \r\t\n"); boolean tableFound = false, tablesChecked = false; String name = ""; singleTable = true; while ( !tableFound && !tablesChecked && st.hasMoreTokens() ) { name = st.nextToken(); if ( !tableFound ) { if ("from".equalsIgnoreCase(name)) { tableName = st.nextToken(); if ("only".equalsIgnoreCase(tableName)) { tableName = st.nextToken(); onlyTable = "ONLY "; } tableFound = true; } } else { tablesChecked = true; // if the very next token is , then there are multiple tables singleTable = !name.equalsIgnoreCase(","); } } }
public static byte[][] run(ProtocolConnection protoConnection, String queryString, boolean wantResults) throws SQLException { QueryExecutor executor = protoConnection.getQueryExecutor(); Query query = executor.createSimpleQuery(queryString); SimpleResultHandler handler = new SimpleResultHandler(protoConnection); int flags = QueryExecutor.QUERY_ONESHOT | QueryExecutor.QUERY_SUPPRESS_BEGIN; if (!wantResults) flags |= QueryExecutor.QUERY_NO_RESULTS | QueryExecutor.QUERY_NO_METADATA; try { executor.execute(query, null, handler, 0, 0, flags); } finally { query.close(); } if (!wantResults) return null; Vector tuples = handler.getResults(); if (tuples == null || tuples.size() != 1) throw new PSQLException(GT.tr("An unexpected result was returned by a query."), PSQLState.CONNECTION_UNABLE_TO_CONNECT); return (byte[][]) tuples.elementAt(0); }
public AbstractJdbc2Statement(AbstractJdbc2Connection connection, String sql, boolean isCallable, int rsType, int rsConcurrency) throws SQLException { this.connection = connection; this.lastSimpleQuery = null; String parsed_sql = replaceProcessing(sql); if (isCallable) parsed_sql = modifyJdbcCall(parsed_sql); this.preparedQuery = connection.getQueryExecutor().createParameterizedQuery(parsed_sql); this.preparedParameters = preparedQuery.createParameterList(); int inParamCount = preparedParameters.getInParameterCount() + 1; this.testReturn = new int[inParamCount]; this.functionReturnType = new int[inParamCount]; resultsettype = rsType; concurrency = rsConcurrency; }
public void handleError(SQLException newError) { if (batchException == null) { int[] successCounts; if (resultIndex >= updateCounts.length) successCounts = updateCounts; else { successCounts = new int[resultIndex]; System.arraycopy(updateCounts, 0, successCounts, 0, resultIndex); } String queryString = "<unknown>"; if (resultIndex < queries.length) queryString = queries[resultIndex].toString(parameterLists[resultIndex]); batchException = new BatchUpdateException(GT.tr("Batch entry {0} {1} was aborted. Call getNextException to see the cause.", new Object[]{ new Integer(resultIndex), queryString}), newError.getSQLState(), successCounts); } batchException.setNextException(newError); }