@Override public String getStatementComponent() { StringBuilder sb = new StringBuilder(values.size() * 3); // maximum "in" statement in Oracle takes 1000 values appendInCondition(sb, fieldName, values.size(), 1000); return sb.toString(); }
private void internalClose() { this.rs = closeResultSet(this.rs); this.stmt = closeStatement(this.stmt); this.ch.closeConnection(this.connection); this.connection = null; if (LOG.isDebugEnabled()) { LOG.debug(this.message + " -> " + this.cnt + " results in " + elapsed + "ms"); } } }
throw asDocumentStoreException(ex, "Trying to determine time difference to server"); } finally { closeResultSet(rs); closeStatement(stmt);
public static void main(String[] args) throws ClassNotFoundException, SQLException { String url = null, user = null, pw = null, db = null; try { url = args[0]; user = args[1]; pw = args[2]; db = args[3]; } catch (IndexOutOfBoundsException ex) { System.err.println("Usage: ... " + RDBCreator.class.getName() + " JDBC-URL username password databasename"); System.exit(2); } String driver = RDBJDBCTools.driverForDBType(RDBJDBCTools.jdbctype(url)); try { Class.forName(driver); } catch (ClassNotFoundException ex) { System.err.println("Attempt to load class " + driver + " failed."); } Connection c = DriverManager.getConnection(url, user, pw); Statement stmt = c.createStatement(); stmt.execute("create database " + db); stmt.close(); c.close(); System.out.println("Database " + db + " created @ " + url + " using " + driver); } }
private static void getTableMetaData(Connection con, Collection<? extends Document> col, RDBTableMetaData tmd) throws SQLException { Statement checkStatement = null; ResultSet checkResultSet = null; try { checkStatement = con.createStatement(); checkResultSet = checkStatement.executeQuery("select * from " + tmd.getName() + " where ID = '0'"); // try to discover size of DATA column and binary-ness of ID ResultSetMetaData met = checkResultSet.getMetaData(); obtainFlagsFromResultSetMeta(met, tmd); String tableInfo = RDBJDBCTools.dumpResultSetMeta(met); tmd.setSchemaInfo(tableInfo); String indexInfo = dumpIndexData(con.getMetaData(), met, tmd.getName(), null); tmd.setIndexInfo(indexInfo); } finally { closeResultSet(checkResultSet); closeStatement(checkStatement); } }
private void initialize(DataSource ds, RDBOptions options) throws Exception { this.tnData = RDBJDBCTools.createTableName(options.getTablePrefix(), "DATASTORE_DATA"); this.tnMeta = RDBJDBCTools.createTableName(options.getTablePrefix(), "DATASTORE_META"); String isolationDiags = RDBJDBCTools.isolationLevelToString(isolation); if (isolation != Connection.TRANSACTION_READ_COMMITTED) { LOG.info("Detected transaction isolation level " + isolationDiags + " is " + (isolation < Connection.TRANSACTION_READ_COMMITTED ? "lower" : "higher") + " than expected " + RDBJDBCTools.isolationLevelToString(Connection.TRANSACTION_READ_COMMITTED) + " - check datasource configuration"); tablesPresent.add(tableName); } catch (SQLException ex) { closeStatement(checkStatement); closeStatement(createStatement); this.ch.closeConnection(con);
long pstart = PERFLOG.start(PERFLOG.isDebugEnabled() ? ("reading: " + keys) : null); PreparedStatementComponent inClause = RDBJDBCTools.createInStatement("ID", keys, tmd.isIdBinary()); StringBuilder query = new StringBuilder(); if (tmd.hasSplitDocs()) { closeResultSet(rs); closeStatement(stmt);
private boolean addModifiedIndex(Connection con, String tableName) throws SQLException { boolean wasChanged = false; String statement = this.dbInfo.getModifiedIndexStatement(tableName); Statement upgradeStatement = null; try { upgradeStatement = con.createStatement(); upgradeStatement.execute(statement); upgradeStatement.close(); con.commit(); LOG.info("Added 'modified' index to " + tableName + " using '" + statement + "'"); wasChanged = true; } catch (SQLException exup) { con.rollback(); String message = String.format( "Attempted to add 'modified' index to %s using '%s', but failed with SQLException '%s' (code: %d/state: %s) - will continue without.", tableName, statement, exup.getMessage(), exup.getErrorCode(), exup.getSQLState()); if (LOG.isDebugEnabled()) { LOG.debug(message, exup); } else { LOG.info(message); } } finally { closeStatement(upgradeStatement); } return wasChanged; }
private static String getIdFromRS(RDBTableMetaData tmd, ResultSet rs, int idx) throws SQLException { if (tmd.isIdBinary()) { try { return new String(rs.getBytes(idx), "UTF-8"); } catch (UnsupportedEncodingException ex) { LOG.error("UTF-8 not supported", ex); throw asDocumentStoreException(ex, "UTF-8 not supported"); } } else { return rs.getString(idx); } }
PreparedStatementComponent inClause = RDBJDBCTools.createInStatement("ID", chunk, false); closeStatement(prepMeta); closeStatement(prepData); con.commit(); this.ch.closeConnection(con);
ex.getErrorCode(), ex.getSQLState()); } finally { closeResultSet(rs);
/** * Creates a table name based on an optional prefix and a base name. * * @throws IllegalArgumentException * upon illegal characters in name */ protected static @NotNull String createTableName(@NotNull String prefix, @NotNull String basename) throws IllegalArgumentException { String p = checkLegalTableName(prefix); String b = checkLegalTableName(basename); if (p.length() != 0 && !p.endsWith("_")) { p += "_"; } return p + b; }
public int delete(Connection connection, RDBTableMetaData tmd, List<String> allIds) throws SQLException { int count = 0; for (List<String> ids : Lists.partition(allIds, RDBJDBCTools.MAX_IN_CLAUSE)) { PreparedStatement stmt; PreparedStatementComponent inClause = RDBJDBCTools.createInStatement("ID", ids, tmd.isIdBinary()); String sql = "delete from " + tmd.getName() + " where " + inClause.getStatementComponent(); stmt = connection.prepareStatement(sql); try { inClause.setParameters(stmt, 1); int result = stmt.executeUpdate(); if (result != ids.size()) { LOG.debug("DB delete failed for " + tmd.getName() + "/" + ids); } count += result; } finally { stmt.close(); } } return count; }
/** * Return approximated string representation of table DDL. */ protected static String dumpResultSetMeta(ResultSetMetaData met) { try { StringBuilder sb = new StringBuilder(); sb.append(String.format("%s.%s: ", met.getSchemaName(1).trim(), met.getTableName(1).trim())); Map<String, Integer> types = new TreeMap<String, Integer>(); for (int i = 1; i <= met.getColumnCount(); i++) { if (i > 1) { sb.append(", "); } sb.append( dumpColumnMeta(met.getColumnName(i), met.getColumnType(i), met.getColumnTypeName(i), met.getPrecision(i))); types.put(met.getColumnTypeName(i), met.getColumnType(i)); } sb.append(" /* " + types.toString() + " */"); return sb.toString(); } catch (SQLException ex) { return "Column metadata unavailable: " + ex.getMessage(); } }
private static void getTableMetaData(Connection con, Collection<? extends Document> col, RDBTableMetaData tmd) throws SQLException { Statement checkStatement = null; ResultSet checkResultSet = null; try { checkStatement = con.createStatement(); checkResultSet = checkStatement.executeQuery("select * from " + tmd.getName() + " where ID = '0'"); // try to discover size of DATA column and binary-ness of ID ResultSetMetaData met = checkResultSet.getMetaData(); obtainFlagsFromResultSetMeta(met, tmd); String tableInfo = RDBJDBCTools.dumpResultSetMeta(met); tmd.setSchemaInfo(tableInfo); String indexInfo = dumpIndexData(con.getMetaData(), met, tmd.getName(), null); tmd.setIndexInfo(indexInfo); } finally { closeResultSet(checkResultSet); closeStatement(checkStatement); } }
private void initialize(DataSource ds, RDBOptions options) throws Exception { this.tnData = RDBJDBCTools.createTableName(options.getTablePrefix(), "DATASTORE_DATA"); this.tnMeta = RDBJDBCTools.createTableName(options.getTablePrefix(), "DATASTORE_META"); String isolationDiags = RDBJDBCTools.isolationLevelToString(isolation); if (isolation != Connection.TRANSACTION_READ_COMMITTED) { LOG.info("Detected transaction isolation level " + isolationDiags + " is " + (isolation < Connection.TRANSACTION_READ_COMMITTED ? "lower" : "higher") + " than expected " + RDBJDBCTools.isolationLevelToString(Connection.TRANSACTION_READ_COMMITTED) + " - check datasource configuration"); tablesPresent.add(tableName); } catch (SQLException ex) { closeStatement(checkStatement); closeStatement(createStatement); this.ch.closeConnection(con);
long pstart = PERFLOG.start(PERFLOG.isDebugEnabled() ? ("reading: " + keys) : null); PreparedStatementComponent inClause = RDBJDBCTools.createInStatement("ID", keys, tmd.isIdBinary()); StringBuilder query = new StringBuilder(); if (tmd.hasSplitDocs()) { closeResultSet(rs); closeStatement(stmt);
private boolean addModifiedIndex(Connection con, String tableName) throws SQLException { boolean wasChanged = false; String statement = this.dbInfo.getModifiedIndexStatement(tableName); Statement upgradeStatement = null; try { upgradeStatement = con.createStatement(); upgradeStatement.execute(statement); upgradeStatement.close(); con.commit(); LOG.info("Added 'modified' index to " + tableName + " using '" + statement + "'"); wasChanged = true; } catch (SQLException exup) { con.rollback(); String message = String.format( "Attempted to add 'modified' index to %s using '%s', but failed with SQLException '%s' (code: %d/state: %s) - will continue without.", tableName, statement, exup.getMessage(), exup.getErrorCode(), exup.getSQLState()); if (LOG.isDebugEnabled()) { LOG.debug(message, exup); } else { LOG.info(message); } } finally { closeStatement(upgradeStatement); } return wasChanged; }
private static String getIdFromRS(RDBTableMetaData tmd, ResultSet rs, int idx) throws SQLException { if (tmd.isIdBinary()) { try { return new String(rs.getBytes(idx), "UTF-8"); } catch (UnsupportedEncodingException ex) { LOG.error("UTF-8 not supported", ex); throw asDocumentStoreException(ex, "UTF-8 not supported"); } } else { return rs.getString(idx); } }
public static void main(String[] args) throws ClassNotFoundException, SQLException { String url = null, user = null, pw = null, db = null; try { url = args[0]; user = args[1]; pw = args[2]; db = args[3]; } catch (IndexOutOfBoundsException ex) { System.err.println("Usage: ... " + RDBCreator.class.getName() + " JDBC-URL username password databasename"); System.exit(2); } String driver = RDBJDBCTools.driverForDBType(RDBJDBCTools.jdbctype(url)); try { Class.forName(driver); } catch (ClassNotFoundException ex) { System.err.println("Attempt to load class " + driver + " failed."); } Connection c = DriverManager.getConnection(url, user, pw); Statement stmt = c.createStatement(); stmt.execute("create database " + db); stmt.close(); c.close(); System.out.println("Database " + db + " created @ " + url + " using " + driver); } }