public class JdbcConnection extends Object implements AutoCloseable
Modifier and Type | Class and Description |
---|---|
static interface |
JdbcConnection.ConnectionFactory
Establishes JDBC connections.
|
static interface |
JdbcConnection.Operations
Defines multiple JDBC operations.
|
static interface |
JdbcConnection.ResultSetConsumer |
static interface |
JdbcConnection.SingleParameterResultSetConsumer |
static interface |
JdbcConnection.StatementFactory
A function to create a statement from a connection.
|
static interface |
JdbcConnection.StatementPreparer |
Modifier and Type | Field and Description |
---|---|
private Configuration |
config |
private Connection |
conn |
private JdbcConnection.ConnectionFactory |
factory |
private JdbcConnection.Operations |
initialOps |
private static org.slf4j.Logger |
LOGGER |
Modifier | Constructor and Description |
---|---|
|
JdbcConnection(Configuration config,
JdbcConnection.ConnectionFactory connectionFactory)
Create a new instance with the given configuration and connection factory.
|
|
JdbcConnection(Configuration config,
JdbcConnection.ConnectionFactory connectionFactory,
JdbcConnection.Operations initialOperations)
Create a new instance with the given configuration and connection factory, and specify the operations that should be
run against each newly-established connection.
|
protected |
JdbcConnection(Configuration config,
JdbcConnection.ConnectionFactory connectionFactory,
JdbcConnection.Operations initialOperations,
Consumer<Configuration.Builder> adapter)
Create a new instance with the given configuration and connection factory, and specify the operations that should be
run against each newly-established connection.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Close the connection and release any resources.
|
static void |
columnsFor(ResultSet resultSet,
Consumer<Column> consumer)
Determine the column definitions for the supplied result set and add each column to the specified consumer.
|
static void |
columnsFor(ResultSet resultSet,
TableEditor editor)
Use the supplied table editor to create columns for the supplied result set.
|
private static Field[] |
combineVariables(Field[] overriddenVariables,
Field... defaultVariables) |
JdbcConfiguration |
config()
Obtain the configuration for this connection.
|
JdbcConnection |
connect()
Ensure a connection to the database is established.
|
Connection |
connection() |
String |
connectionString(String urlPattern)
Returns a JDBC connection string using the current configuration and url.
|
private String |
delimiter(int columnCount,
int[] columnSizes) |
JdbcConnection |
execute(JdbcConnection.Operations operations)
Execute a series of operations as a single transaction.
|
JdbcConnection |
execute(String... sqlStatements)
Execute a series of SQL statements as a single transaction.
|
private static String |
findAndReplace(String url,
Properties props,
Field... variables) |
private int[] |
findMaxLength(ResultSet resultSet) |
boolean |
isConnected() |
private static boolean |
isNullable(int jdbcNullable) |
static JdbcConnection.ConnectionFactory |
patternBasedFactory(String urlPattern,
Field... variables)
Create a
JdbcConnection.ConnectionFactory that replaces variables in the supplied URL pattern. |
JdbcConnection |
prepareQuery(String preparedQueryString,
Collection<String> parameters,
JdbcConnection.SingleParameterResultSetConsumer resultConsumer)
Execute a SQL prepared query.
|
JdbcConnection |
prepareQuery(String preparedQueryString,
JdbcConnection.StatementPreparer preparer,
JdbcConnection.ResultSetConsumer resultConsumer)
Execute a SQL prepared query.
|
JdbcConnection |
prepareQuery(String preparedQueryString,
Stream<String> parameters,
JdbcConnection.SingleParameterResultSetConsumer resultConsumer)
Execute a SQL prepared query.
|
void |
print(ResultSet resultSet) |
void |
print(ResultSet resultSet,
Consumer<String> lines) |
JdbcConnection |
query(String query,
JdbcConnection.ResultSetConsumer resultConsumer)
Execute a SQL query.
|
JdbcConnection |
query(String query,
JdbcConnection.StatementFactory statementFactory,
JdbcConnection.ResultSetConsumer resultConsumer)
Execute a SQL query.
|
Set<String> |
readAllCatalogNames()
Get the names of all of the catalogs.
|
Set<TableId> |
readAllTableNames(String[] tableTypes)
Get the identifiers of all available tables.
|
void |
readSchema(Tables tables,
String databaseCatalog,
String schemaNamePattern,
Tables.TableNameFilter tableFilter,
Tables.ColumnNameFilter columnFilter,
boolean removeTablesNotFoundInJdbc)
Create definitions for each tables in the database, given the catalog name, schema pattern, table filter, and
column filter.
|
Set<TableId> |
readTableNames(String databaseCatalog,
String schemaNamePattern,
String tableNamePattern,
String[] tableTypes)
Get the identifiers of the tables.
|
JdbcConnection |
setAutoCommit(boolean autoCommit) |
String[] |
tableTypes() |
String |
username()
Returns the username for this connection
|
private static final org.slf4j.Logger LOGGER
private final Configuration config
private final JdbcConnection.ConnectionFactory factory
private final JdbcConnection.Operations initialOps
private volatile Connection conn
public JdbcConnection(Configuration config, JdbcConnection.ConnectionFactory connectionFactory)
config
- the configuration; may not be nullconnectionFactory
- the connection factory; may not be nullpublic JdbcConnection(Configuration config, JdbcConnection.ConnectionFactory connectionFactory, JdbcConnection.Operations initialOperations)
config
- the configuration; may not be nullconnectionFactory
- the connection factory; may not be nullinitialOperations
- the initial operations that should be run on each new connection; may be nullprotected JdbcConnection(Configuration config, JdbcConnection.ConnectionFactory connectionFactory, JdbcConnection.Operations initialOperations, Consumer<Configuration.Builder> adapter)
config
- the configuration; may not be nullconnectionFactory
- the connection factory; may not be nullinitialOperations
- the initial operations that should be run on each new connection; may be nulladapter
- the function that can be called to update the configuration with defaultspublic static JdbcConnection.ConnectionFactory patternBasedFactory(String urlPattern, Field... variables)
JdbcConnection.ConnectionFactory
that replaces variables in the supplied URL pattern. Variables include:
${hostname}
${port}
${dbname}
${username}
${password}
urlPattern
- the URL pattern string; may not be nullvariables
- any custom or overridden configuration variablesprivate static Field[] combineVariables(Field[] overriddenVariables, Field... defaultVariables)
private static String findAndReplace(String url, Properties props, Field... variables)
public JdbcConfiguration config()
public JdbcConnection setAutoCommit(boolean autoCommit) throws SQLException
SQLException
public JdbcConnection connect() throws SQLException
SQLException
- if there is an error connecting to the databasepublic JdbcConnection execute(String... sqlStatements) throws SQLException
sqlStatements
- the SQL statements that are to be performed as a single transactionSQLException
- if there is an error connecting to the database or executing the statementsexecute(Operations)
public JdbcConnection execute(JdbcConnection.Operations operations) throws SQLException
operations
- the function that will be called with a newly-created Statement
, and that performs
one or more operations on that statement objectSQLException
- if there is an error connecting to the database or executing the statementspublic JdbcConnection query(String query, JdbcConnection.ResultSetConsumer resultConsumer) throws SQLException
query
- the SQL queryresultConsumer
- the consumer of the query resultsSQLException
- if there is an error connecting to the database or executing the statementsexecute(Operations)
public JdbcConnection query(String query, JdbcConnection.StatementFactory statementFactory, JdbcConnection.ResultSetConsumer resultConsumer) throws SQLException
query
- the SQL querystatementFactory
- the function that should be used to create the statement from the connection; may not be nullresultConsumer
- the consumer of the query resultsSQLException
- if there is an error connecting to the database or executing the statementsexecute(Operations)
public JdbcConnection prepareQuery(String preparedQueryString, JdbcConnection.StatementPreparer preparer, JdbcConnection.ResultSetConsumer resultConsumer) throws SQLException
preparedQueryString
- the prepared query stringpreparer
- the function that supplied arguments to the prepared statement; may not be nullresultConsumer
- the consumer of the query resultsSQLException
- if there is an error connecting to the database or executing the statementsexecute(Operations)
public JdbcConnection prepareQuery(String preparedQueryString, Collection<String> parameters, JdbcConnection.SingleParameterResultSetConsumer resultConsumer) throws SQLException
preparedQueryString
- the prepared query stringparameters
- the collection of values for the first and only parameter in the query; may not be nullresultConsumer
- the consumer of the query resultsSQLException
- if there is an error connecting to the database or executing the statementsexecute(Operations)
public JdbcConnection prepareQuery(String preparedQueryString, Stream<String> parameters, JdbcConnection.SingleParameterResultSetConsumer resultConsumer) throws SQLException
preparedQueryString
- the prepared query stringparameters
- the stream of values for the first and only parameter in the query; may not be nullresultConsumer
- the consumer of the query resultsSQLException
- if there is an error connecting to the database or executing the statementsexecute(Operations)
public void print(ResultSet resultSet)
private String delimiter(int columnCount, int[] columnSizes)
private int[] findMaxLength(ResultSet resultSet) throws SQLException
SQLException
public boolean isConnected() throws SQLException
SQLException
public Connection connection() throws SQLException
SQLException
public void close() throws SQLException
close
in interface AutoCloseable
SQLException
public Set<String> readAllCatalogNames() throws SQLException
SQLException
- if an error occurs while accessing the database metadatapublic String[] tableTypes() throws SQLException
SQLException
public Set<TableId> readAllTableNames(String[] tableTypes) throws SQLException
tableTypes
- the set of table types to include in the results, which may be null for all table typesTableId
s; never null but possibly emptySQLException
- if an error occurs while accessing the database metadatapublic Set<TableId> readTableNames(String databaseCatalog, String schemaNamePattern, String tableNamePattern, String[] tableTypes) throws SQLException
databaseCatalog
- the name of the catalog, which is typically the database name; may be an empty string for tables
that have no catalog, or null
if the catalog name should not be used to narrow the list of table
identifiersschemaNamePattern
- the pattern used to match database schema names, which may be "" to match only those tables with
no schema or null
if the schema name should not be used to narrow the list of table
identifierstableNamePattern
- the pattern used to match database table names, which may be null to match all table namestableTypes
- the set of table types to include in the results, which may be null for all table typesTableId
s; never null but possibly emptySQLException
- if an error occurs while accessing the database metadatapublic String connectionString(String urlPattern)
urlPattern
- a String
representing a JDBC connection with variables that will be replacedString
where the variables in urlPattern
are replaced with values from the configurationpublic String username()
String
, never null
public void readSchema(Tables tables, String databaseCatalog, String schemaNamePattern, Tables.TableNameFilter tableFilter, Tables.ColumnNameFilter columnFilter, boolean removeTablesNotFoundInJdbc) throws SQLException
tables
- the set of table definitions to be modified; may not be nulldatabaseCatalog
- the name of the catalog, which is typically the database name; may be null if all accessible
databases are to be processedschemaNamePattern
- the pattern used to match database schema names, which may be "" to match only those tables with
no schema or null to process all accessible tables regardless of database schema nametableFilter
- used to determine for which tables are to be processed; may be null if all accessible tables are to be
processedcolumnFilter
- used to determine which columns should be included as fields in its table's definition; may
be null if all columns for all tables are to be includedremoveTablesNotFoundInJdbc
- true
if this method should remove from tables
any definitions for tables
that are not found in the database metadata, or false
if such tables should be left untouchedSQLException
- if an error occurs while accessing the database metadatapublic static void columnsFor(ResultSet resultSet, TableEditor editor) throws SQLException
resultSet
- the query result set; may not be nulleditor
- the consumer of the definitions; may not be nullSQLException
- if an error occurs while using the result setpublic static void columnsFor(ResultSet resultSet, Consumer<Column> consumer) throws SQLException
resultSet
- the query result set; may not be nullconsumer
- the consumer of the definitions; may not be nullSQLException
- if an error occurs while using the result setprivate static boolean isNullable(int jdbcNullable)
Copyright © 2016 JBoss by Red Hat. All rights reserved.