/** * Creates a new database callable statement. * * @param sql * The statement to be created. * @return The newly created statement. */ public CallableStatement prepareCall(String sql) { try { CallableStatement callableStatement; LOG.finest("Creating callable statement {" + sql + "}"); callableStatement = getConnection().prepareCall(sql); return callableStatement; } catch (SQLException e) { throw new OsmosisRuntimeException("Unable to create database callable statement.", e); } }
/** * Creates a new database statement. * * @return The newly created statement. */ public Statement createStatement() { try { Statement resultStatement; LOG.finest("Creating a new statement."); resultStatement = getConnection().createStatement(); return resultStatement; } catch (SQLException e) { throw new OsmosisRuntimeException("Unable to create database statement.", e); } }
/** * Creates a new database prepared statement. * * @param sql * The statement to be created. * @return The newly created statement. */ public PreparedStatement prepareStatement(String sql) { try { PreparedStatement preparedStatement; LOG.finest("Creating prepared statement {" + sql + "}"); preparedStatement = getConnection().prepareStatement(sql); return preparedStatement; } catch (SQLException e) { throw new OsmosisRuntimeException("Unable to create database prepared statement.", e); } }
/** * Indicates if the specified column exists in the database. * * @param tableName The table to check for. * @param columnName The column to check for. * @return True if the column exists, false otherwise. */ public boolean doesColumnExist(String tableName, String columnName) { LOG.finest("Checking if column {" + columnName + "} in table {" + tableName + "} exists."); try (ResultSet resultSet = getConnection().getMetaData().getColumns(null, null, tableName, columnName)) { return resultSet.next(); } catch (SQLException e) { throw new OsmosisRuntimeException( "Unable to check for the existence of column " + tableName + "." + columnName + ".", e ); } }
/** * Executes a sql statement against the database. * * @param sql * The sql statement to be invoked. */ public void executeStatement(String sql) { try { LOG.finest("Executing statement {" + sql + "}"); if (statement != null) { statement.close(); } statement = getConnection().createStatement(); statement.execute(sql); } catch (SQLException e) { throw new OsmosisRuntimeException("Unable to execute statement.", e); } }
/** * Indicates if the specified table exists in the database. * * @param tableName The table to check for. * @return True if the table exists, false otherwise. */ public boolean doesTableExist(String tableName) { LOG.finest("Checking if table {" + tableName + "} exists."); try (ResultSet resultSet = getConnection().getMetaData().getTables(null, null, tableName, new String[]{"TABLE"})) { return resultSet.next(); } catch (SQLException e) { throw new OsmosisRuntimeException( "Unable to check for the existence of table " + tableName + ".", e ); } }
statement = getConnection().createStatement( ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY
/** * Loads a table from a COPY file. * * @param dbCtx * The database connection. * @param copyFile * The file to be loaded. * @param tableName * The table to load the data into. */ public void loadCopyFile(DatabaseContext dbCtx, File copyFile, String tableName) { try (InputStream bufferedInStream = new BufferedInputStream(new FileInputStream(copyFile), 65536)) { CopyManager copyManager = new CopyManager((BaseConnection) dbCtx.getConnection()); copyManager.copyIn("COPY " + tableName + " FROM STDIN", bufferedInStream); } catch (IOException e) { throw new OsmosisRuntimeException("Unable to process COPY file " + copyFile + ".", e); } catch (SQLException e) { throw new OsmosisRuntimeException("Unable to process COPY file " + copyFile + ".", e); } }