Refine search
public void resetPrimaryKeys(DataSource dataSource) throws SQLException { Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { connection = dataSource.getConnection(); connection.setAutoCommit(false); statement = connection.createStatement(); for (String table : SqTables.TABLES) { try { resultSet = statement.executeQuery("SELECT CASE WHEN MAX(ID) IS NULL THEN 1 ELSE MAX(ID)+1 END FROM " + table); resultSet.next(); int maxId = resultSet.getInt(1); resultSet.close(); for (String resetCommand : resetSequenceSql(table, maxId)) { statement.executeUpdate(resetCommand); } connection.commit(); } catch (Exception e) { connection.rollback(); // this table has no primary key } } } finally { DbUtils.closeQuietly(connection, statement, resultSet); } } }
public TableStruct analyse(Connection connection, String table) throws SQLException { ResultSet structRs = null; ResultSet columnSet = null; TableStruct tableStruct = new TableStruct(table); try { structRs = connection.getMetaData().getPrimaryKeys(connection.getCatalog(), null, table); columnSet = connection.getMetaData().getColumns(null, "%", table, "%"); while (structRs.next()) { tableStruct.getPrimaryKeys().add(structRs.getString("COLUMN_NAME")); } while (columnSet.next()) { tableStruct.getColumns().put(columnSet.getString("COLUMN_NAME"), columnSet.getString("TYPE_NAME")); } } catch (SQLException e) { try { DbUtils.close(structRs); DbUtils.close(columnSet); } catch (SQLException ignored) { } throw e; } return tableStruct; }
private void loadInstalledTables() throws SQLException { logger.info("Searching for installed tables"); Connection conn = null; try { conn = this.dataSource.getConnection(); final ResultSet rs = conn.getMetaData().getTables(conn.getCatalog(), null, null, new String[]{"TABLE"}); while (rs.next()) { this.tables.put(rs.getString("TABLE_NAME").toLowerCase(), "2.1"); } } finally { DbUtils.commitAndCloseQuietly(conn); } }
@Test public void closeQuietlyConnectionResultSetThrowingExceptionStatement() throws Exception { Connection mockConnection = mock(Connection.class); ResultSet mockResultSet = mock(ResultSet.class); doThrow(SQLException.class).when(mockResultSet).close(); Statement mockStatement = mock(Statement.class); DbUtils.closeQuietly(mockConnection, mockStatement, mockResultSet); verify(mockConnection).close(); verify(mockResultSet).close(); verify(mockStatement).close(); }
private String readPrimaryKeyColumn(DatabaseMetaData dbMetadata) throws SQLException { String pkName = null; ResultSet rs = null; try { rs = dbMetadata.getPrimaryKeys(this.catalog, this.schema, this.name); while (rs.next()) { pkName = rs.getString("PK_NAME"); this.pkColumns.add(rs.getString(JdbcTable.COLUMN_NAME).toUpperCase()); } } finally { DbUtils.closeQuietly(rs); } return pkName; }
@CheckForNull public List<String> selectLineHashes(DbSession dbSession, String fileUuid) { Connection connection = dbSession.getConnection(); PreparedStatement pstmt = null; ResultSet rs = null; try { pstmt = connection.prepareStatement("SELECT line_hashes FROM file_sources WHERE file_uuid=? AND data_type='SOURCE'"); pstmt.setString(1, fileUuid); rs = pstmt.executeQuery(); if (rs.next()) { String string = rs.getString(1); if (string == null) { return Collections.emptyList(); } return END_OF_LINE_SPLITTER.splitToList(string); } return null; } catch (SQLException e) { throw new IllegalStateException("Fail to read FILE_SOURCES.LINE_HASHES of file " + fileUuid, e); } finally { DbUtils.closeQuietly(connection, pstmt, rs); } }
protected boolean shouldTruncate(Connection connection, String table) throws SQLException { Statement stmt = connection.createStatement(); ResultSet rs = null; try { rs = stmt.executeQuery("select count(1) from " + table); if (rs.next()) { return rs.getInt(1) > 0; } } catch (SQLException ignored) { // probably because table does not exist. That's the case with H2 tests. } finally { DbUtils.closeQuietly(rs); DbUtils.closeQuietly(stmt); } return false; }
@Override public <T> List<T> list(Select.RowReader<T> reader) throws SQLException { ResultSet rs = pstmt.executeQuery(); Select.Row row = new Select.Row(rs); try { List<T> rows = new ArrayList<>(); while (rs.next()) { rows.add(reader.read(row)); } return rows; } catch (Exception e) { throw newExceptionWithRowDetails(row, e); } finally { DbUtils.closeQuietly(rs); close(); } }
public void readLineHashesStream(DbSession dbSession, String fileUuid, Consumer<Reader> consumer) { Connection connection = dbSession.getConnection(); PreparedStatement pstmt = null; ResultSet rs = null; Reader reader = null; try { pstmt = connection.prepareStatement("SELECT line_hashes FROM file_sources WHERE file_uuid=? AND data_type='SOURCE'"); pstmt.setString(1, fileUuid); rs = pstmt.executeQuery(); if (rs.next()) { reader = rs.getCharacterStream(1); if (reader != null) { consumer.accept(reader); } } } catch (SQLException e) { throw new IllegalStateException("Fail to read FILE_SOURCES.LINE_HASHES of file " + fileUuid, e); } finally { IOUtils.closeQuietly(reader); DbUtils.closeQuietly(connection, pstmt, rs); } }
public String[] getColumns(String tableName) throws SQLException { Connection con = null; ResultSet rs = null; try { con = ds_.getConnection(); DatabaseMetaData dmd = con.getMetaData(); rs = dmd.getColumns(null, null, tableName, "%"); List<String> list = new ArrayList<String>(); while (rs.next()) { list.add(rs.getString("COLUMN_NAME")); } return list.toArray(new String[0]); } finally { DbUtils.closeQuietly(con, null, rs); } }
public void truncateDatabase(DataSource dataSource) throws SQLException { Connection connection = dataSource.getConnection(); Statement statement = null; try { connection.setAutoCommit(false); statement = connection.createStatement(); for (String table : SqTables.TABLES) { try { if (shouldTruncate(connection, table)) { statement.executeUpdate(truncateSql(table)); connection.commit(); } } catch (Exception e) { connection.rollback(); throw new IllegalStateException("Fail to truncate table " + table, e); } } } finally { DbUtils.closeQuietly(connection); DbUtils.closeQuietly(statement); } }
public boolean existsTable(Connection connection, String table) throws SQLException { ResultSet resultSet = null; try { resultSet = connection.getMetaData().getTables(null, null, table, null); if (resultSet.next()) { return true; } } catch (SQLException e) { throw e; } finally { DbUtils.close(resultSet); } return false; }
@Test public void closeQuietlyConnectionThrowingException() throws Exception { Connection mockConnection = mock(Connection.class); doThrow(SQLException.class).when(mockConnection).close(); DbUtils.closeQuietly(mockConnection); }
@Test public void closeQuietlyResultSetThrowingException() throws Exception { ResultSet mockResultSet = mock(ResultSet.class); doThrow(SQLException.class).when(mockResultSet).close(); DbUtils.closeQuietly(mockResultSet); }
@Test public void closeQuietlyStatement() throws Exception { Statement mockStatement = mock(Statement.class); DbUtils.closeQuietly(mockStatement); verify(mockStatement).close(); }