Package org.eclipse.net4j.db
Class DBUtil
- java.lang.Object
- 
- org.eclipse.net4j.db.DBUtil
 
- 
 public final class DBUtil extends java.lang.ObjectA utility class with various static factory and convenience methods.- Author:
- Eike Stepper
 
- 
- 
Nested Class SummaryNested Classes Modifier and Type Class Description static interfaceDBUtil.DeserializeRowHandlerArow handlerwith amethodthat is called once per row deserialized withinDBUtil.deserializeTable().static interfaceDBUtil.RowHandlerCall-back interface with amethodthat is called after a number of table rows have been handled by one of the subtypes of this interface.static interfaceDBUtil.RunnableWithConnection<T>static interfaceDBUtil.SerializeRowHandler
 - 
Field SummaryFields Modifier and Type Field Description static intMAX_BATCH_SIZEstatic java.lang.StringPROP_ENABLE_NOISY_CLOSEA system property to enable noisy close, i.e.
 - 
Method SummaryAll Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static intasInt(java.lang.Object value)static longasLong(java.lang.Object value)static BatchedStatementbatched(java.sql.PreparedStatement delegate, int batchSize)static intclearTable(java.sql.Connection connection, java.lang.String tableName)static intclearTable(java.sql.Connection connection, IDBTable table)static java.lang.Exceptionclose(java.sql.Connection connection)static java.lang.Exceptionclose(java.sql.ResultSet resultSet)static java.lang.Exceptionclose(java.sql.Statement statement)static IDBSchemacopySchema(IDBSchema source)static IDBConnectionProvidercreateConnectionProvider(javax.sql.DataSource dataSource)static IDBConnectionProvider2createConnectionProvider(javax.sql.DataSource dataSource, java.lang.String user)static javax.sql.DataSourcecreateDataSource(java.util.Map<java.lang.Object,java.lang.Object> properties)static javax.sql.DataSourcecreateDataSource(java.util.Map<java.lang.Object,java.lang.Object> properties, java.lang.String namespace)static javax.sql.DataSourcecreateDataSource(java.util.Map<java.lang.Object,java.lang.Object> properties, java.lang.String namespace, java.lang.String driverClassKey)static IDBSchemacreateSchema(java.lang.String name)static voiddeserializeTable(org.eclipse.net4j.util.io.ExtendedDataInput in, java.sql.Connection connection, IDBTable table, org.eclipse.net4j.util.om.monitor.OMMonitor monitor)static voiddeserializeTable(org.eclipse.net4j.util.io.ExtendedDataInput in, java.sql.Connection connection, IDBTable table, org.eclipse.net4j.util.om.monitor.OMMonitor monitor, DBUtil.DeserializeRowHandler handler)static java.util.List<java.lang.Exception>dropAllTables(java.sql.Connection connection, java.lang.String dbName)static voiddump(IDBNamedElement namedElement)static voiddump(IDBNamedElement namedElement, java.io.Writer writer)static java.lang.StringdumpToString(IDBNamedElement namedElement)static voidexecute(java.sql.Connection connection, java.lang.CharSequence sql)static <T> Texecute(IDBConnectionProvider connectionProvider, DBUtil.RunnableWithConnection<T> runnable)static voidexecuteBatch(java.sql.PreparedStatement stmt, int counter)static voidexecuteBatch(java.sql.PreparedStatement stmt, int counter, boolean checkExactlyOne)static java.util.List<java.lang.String>getAllSchemaNames(java.sql.Connection connection)static java.util.Set<java.lang.String>getAllSchemaNames(java.sql.DatabaseMetaData metaData)static java.util.List<java.lang.String>getAllSchemaTableNames(java.sql.Connection connection)Deprecated.As of 4.2 usegetAllSchemaNames(Connection).static java.util.List<java.lang.String>getAllSchemaTableNames(java.sql.DatabaseMetaData metaData)Deprecated.As of 4.2 usegetAllSchemaNames(DatabaseMetaData).static java.util.List<java.lang.String>getAllTableNames(java.sql.Connection connection, java.lang.String dbName)static IDBAdaptergetDBAdapter(java.lang.String adapterName)Retrieves anadapterfrom theadapter registry.static intgetRowCount(java.sql.Connection connection, java.lang.String tableName)Returns the number of rows contained in the given table.static intgetRowCount(java.sql.ResultSet resultSet)Returns the number of rows contained in the given result set.static booleanisOptional(IDBElement element)static booleanisTracerEnabled()static IDBDatabaseopenDatabase(IDBAdapter adapter, IDBConnectionProvider connectionProvider, java.lang.String schemaName)static IDBDatabaseopenDatabase(IDBAdapter adapter, IDBConnectionProvider connectionProvider, java.lang.String schemaName, boolean fixNullableIndexColumns)static IDBSchemareadSchema(IDBAdapter adapter, java.sql.Connection connection, java.lang.String name)static IDBSchemareadSchema(IDBAdapter adapter, java.sql.Connection connection, java.lang.String name, boolean fixNullableIndexColumns)static voidreadSchema(IDBAdapter adapter, java.sql.Connection connection, IDBSchema schema)static java.lang.ExceptionrollbackSilently(java.sql.Connection connection)static java.lang.Object[]select(java.sql.Connection connection, java.lang.String where, IDBField... fields)static intselect(java.sql.Connection connection, IDBRowHandler rowHandler, java.lang.String where, IDBField... fields)static intselect(java.sql.Connection connection, IDBRowHandler rowHandler, IDBField... fields)static intselectMaximumInt(java.sql.Connection connection, IDBField field, java.lang.String... where)static longselectMaximumLong(java.sql.Connection connection, IDBField field, java.lang.String... where)static intselectMinimumInt(java.sql.Connection connection, IDBField field, java.lang.String... where)static longselectMinimumLong(java.sql.Connection connection, IDBField field, java.lang.String... where)static voidserializeTable(org.eclipse.net4j.util.io.ExtendedDataOutput out, java.sql.Connection connection, IDBTable table, java.lang.String tableAlias, java.lang.String sqlSuffix)static voidserializeTable(org.eclipse.net4j.util.io.ExtendedDataOutput out, java.sql.Connection connection, IDBTable table, java.lang.String tableAlias, java.lang.String sqlSuffix, DBUtil.SerializeRowHandler handler)static booleansetAutoCommit(java.sql.Connection connection, boolean autoCommit)static booleansetOptional(IDBElement element, boolean optional)static voidsqlDump(java.sql.Connection conn, java.lang.String sql)Deprecated.Should only be used when debugging.static voidsqlDump(IDBConnectionProvider connectionProvider, java.lang.String sql)Deprecated.Should only be used when debugging.static java.lang.Stringtrace(java.lang.String sql)static intupdate(java.sql.Connection connection, java.lang.String sql)static intupdate(java.sql.PreparedStatement stmt, boolean exactlyOne)Execute update on the given prepared statement and handle common cases of return values.
 
- 
- 
- 
Field Detail- 
MAX_BATCH_SIZEpublic static final int MAX_BATCH_SIZE - Since:
- 4.2
 
 - 
PROP_ENABLE_NOISY_CLOSEpublic static final java.lang.String PROP_ENABLE_NOISY_CLOSE A system property to enable noisy close, i.e. exception catch in close methods are thrown asDBExceptionexception.- Since:
- 4.4
- See Also:
- Constant Field Values
 
 
- 
 - 
Method Detail- 
sqlDump@Deprecated public static void sqlDump(java.sql.Connection conn, java.lang.String sql)Deprecated.Should only be used when debugging.For debugging purposes ONLY!- Since:
- 3.0
 
 - 
sqlDump@Deprecated public static void sqlDump(IDBConnectionProvider connectionProvider, java.lang.String sql) Deprecated.Should only be used when debugging.For debugging purposes ONLY!- Since:
- 3.0
 
 - 
dumpToStringpublic static java.lang.String dumpToString(IDBNamedElement namedElement) - Since:
- 4.2
 
 - 
dumppublic static void dump(IDBNamedElement namedElement) - Since:
- 4.2
 
 - 
dumppublic static void dump(IDBNamedElement namedElement, java.io.Writer writer) throws java.io.IOException - Throws:
- java.io.IOException
- Since:
- 4.2
 
 - 
openDatabasepublic static IDBDatabase openDatabase(IDBAdapter adapter, IDBConnectionProvider connectionProvider, java.lang.String schemaName) - Since:
- 4.2
 
 - 
openDatabasepublic static IDBDatabase openDatabase(IDBAdapter adapter, IDBConnectionProvider connectionProvider, java.lang.String schemaName, boolean fixNullableIndexColumns) - Since:
- 4.2
 
 - 
createSchemapublic static IDBSchema createSchema(java.lang.String name) 
 - 
readSchemapublic static void readSchema(IDBAdapter adapter, java.sql.Connection connection, IDBSchema schema) - Since:
- 4.2
 
 - 
readSchemapublic static IDBSchema readSchema(IDBAdapter adapter, java.sql.Connection connection, java.lang.String name) - Since:
- 4.2
 
 - 
readSchemapublic static IDBSchema readSchema(IDBAdapter adapter, java.sql.Connection connection, java.lang.String name, boolean fixNullableIndexColumns) - Since:
- 4.2
 
 - 
createDataSourcepublic static javax.sql.DataSource createDataSource(java.util.Map<java.lang.Object,java.lang.Object> properties) 
 - 
createDataSourcepublic static javax.sql.DataSource createDataSource(java.util.Map<java.lang.Object,java.lang.Object> properties, java.lang.String namespace)
 - 
createDataSourcepublic static javax.sql.DataSource createDataSource(java.util.Map<java.lang.Object,java.lang.Object> properties, java.lang.String namespace, java.lang.String driverClassKey)
 - 
createConnectionProviderpublic static IDBConnectionProvider createConnectionProvider(javax.sql.DataSource dataSource) 
 - 
createConnectionProviderpublic static IDBConnectionProvider2 createConnectionProvider(javax.sql.DataSource dataSource, java.lang.String user) - Since:
- 4.3
 
 - 
getDBAdapterpublic static IDBAdapter getDBAdapter(java.lang.String adapterName) Retrieves anadapterfrom theadapter registry.If Eclipse is running adapters are automatically created from descriptors that are contributed to the extension point org.eclipse.net4j.db.dbAdapters.In standalone scenarios the needed adapter instances must be registered with the adapter registrymanually.
 - 
batchedpublic static BatchedStatement batched(java.sql.PreparedStatement delegate, int batchSize) throws DBException - Throws:
- DBException
- Since:
- 4.5
 
 - 
closepublic static java.lang.Exception close(java.sql.ResultSet resultSet) 
 - 
closepublic static java.lang.Exception close(java.sql.Statement statement) 
 - 
closepublic static java.lang.Exception close(java.sql.Connection connection) 
 - 
isOptionalpublic static boolean isOptional(IDBElement element) - Since:
- 4.6
 
 - 
setOptionalpublic static boolean setOptional(IDBElement element, boolean optional) - Since:
- 4.6
 
 - 
setAutoCommitpublic static boolean setAutoCommit(java.sql.Connection connection, boolean autoCommit)- Since:
- 4.2
 
 - 
rollbackSilentlypublic static java.lang.Exception rollbackSilently(java.sql.Connection connection) - Since:
- 4.2
 
 - 
getAllSchemaTableNames@Deprecated public static java.util.List<java.lang.String> getAllSchemaTableNames(java.sql.Connection connection) Deprecated.As of 4.2 usegetAllSchemaNames(Connection).- Since:
- 3.0
 
 - 
getAllSchemaTableNames@Deprecated public static java.util.List<java.lang.String> getAllSchemaTableNames(java.sql.DatabaseMetaData metaData) Deprecated.As of 4.2 usegetAllSchemaNames(DatabaseMetaData).- Since:
- 3.0
 
 - 
getAllSchemaNamespublic static java.util.List<java.lang.String> getAllSchemaNames(java.sql.Connection connection) - Since:
- 4.2
 
 - 
getAllSchemaNamespublic static java.util.Set<java.lang.String> getAllSchemaNames(java.sql.DatabaseMetaData metaData) - Since:
- 4.2
 
 - 
getAllTableNamespublic static java.util.List<java.lang.String> getAllTableNames(java.sql.Connection connection, java.lang.String dbName)
 - 
dropAllTablespublic static java.util.List<java.lang.Exception> dropAllTables(java.sql.Connection connection, java.lang.String dbName)- Since:
- 4.0
 
 - 
asIntpublic static int asInt(java.lang.Object value) - Since:
- 4.2
 
 - 
asLongpublic static long asLong(java.lang.Object value) - Since:
- 4.2
 
 - 
selectMinimumIntpublic static int selectMinimumInt(java.sql.Connection connection, IDBField field, java.lang.String... where) throws DBException- Throws:
- DBException
- Since:
- 3.0
 
 - 
selectMinimumLongpublic static long selectMinimumLong(java.sql.Connection connection, IDBField field, java.lang.String... where) throws DBException- Throws:
- DBException
- Since:
- 3.0
 
 - 
selectMaximumIntpublic static int selectMaximumInt(java.sql.Connection connection, IDBField field, java.lang.String... where) throws DBException- Throws:
- DBException
- Since:
- 3.0
 
 - 
selectMaximumLongpublic static long selectMaximumLong(java.sql.Connection connection, IDBField field, java.lang.String... where) throws DBException- Throws:
- DBException
- Since:
- 3.0
 
 - 
executepublic static <T> T execute(IDBConnectionProvider connectionProvider, DBUtil.RunnableWithConnection<T> runnable) - Since:
- 4.2
 
 - 
executepublic static void execute(java.sql.Connection connection, java.lang.CharSequence sql)- Since:
- 4.2
 
 - 
executeBatchpublic static void executeBatch(java.sql.PreparedStatement stmt, int counter)- Since:
- 4.1
 
 - 
executeBatchpublic static void executeBatch(java.sql.PreparedStatement stmt, int counter, boolean checkExactlyOne)- Since:
- 4.1
 
 - 
updatepublic static int update(java.sql.Connection connection, java.lang.String sql)
 - 
updatepublic static int update(java.sql.PreparedStatement stmt, boolean exactlyOne) throws java.sql.SQLExceptionExecute update on the given prepared statement and handle common cases of return values.- Parameters:
- stmt- the prepared statement
- exactlyOne- if- true, the update count is checked to be- 1. Else the update result is only checked so that the update was successful (i.e. result code != Statement.EXECUTE_FAILED).
- Returns:
- the update count / execution result as returned by PreparedStatement.executeUpdate(). Can be used by the caller to perform more advanced checks.
- Throws:
- java.sql.SQLException- if- PreparedStatement.executeUpdate()throws it.
- java.lang.IllegalStateException- if the check indicated by- excatlyOneindicates an error.
- Since:
- 4.0
 
 - 
clearTablepublic static int clearTable(java.sql.Connection connection, IDBTable table)- Since:
- 4.1
 
 - 
clearTablepublic static int clearTable(java.sql.Connection connection, java.lang.String tableName)- Since:
- 4.1
 
 - 
selectpublic static int select(java.sql.Connection connection, IDBRowHandler rowHandler, java.lang.String where, IDBField... fields) throws DBException- Throws:
- DBException
 
 - 
selectpublic static int select(java.sql.Connection connection, IDBRowHandler rowHandler, IDBField... fields) throws DBException- Throws:
- DBException
 
 - 
selectpublic static java.lang.Object[] select(java.sql.Connection connection, java.lang.String where, IDBField... fields) throws DBException- Throws:
- DBException
 
 - 
getRowCountpublic static int getRowCount(java.sql.ResultSet resultSet) throws DBExceptionReturns the number of rows contained in the given result set.The statementof the result set must have been created withTYPE_SCROLL_INSENSITIVE.- Throws:
- DBException
- Since:
- 4.0
 
 - 
getRowCountpublic static int getRowCount(java.sql.Connection connection, java.lang.String tableName) throws DBExceptionReturns the number of rows contained in the given table.- Throws:
- DBException
- Since:
- 4.5
 
 - 
serializeTablepublic static void serializeTable(org.eclipse.net4j.util.io.ExtendedDataOutput out, java.sql.Connection connection, IDBTable table, java.lang.String tableAlias, java.lang.String sqlSuffix) throws DBException, java.io.IOException- Throws:
- DBException
- java.io.IOException
- Since:
- 3.0
 
 - 
serializeTablepublic static void serializeTable(org.eclipse.net4j.util.io.ExtendedDataOutput out, java.sql.Connection connection, IDBTable table, java.lang.String tableAlias, java.lang.String sqlSuffix, DBUtil.SerializeRowHandler handler) throws DBException, java.io.IOException- Throws:
- DBException
- java.io.IOException
- Since:
- 4.1
 
 - 
deserializeTablepublic static void deserializeTable(org.eclipse.net4j.util.io.ExtendedDataInput in, java.sql.Connection connection, IDBTable table, org.eclipse.net4j.util.om.monitor.OMMonitor monitor) throws java.io.IOException- Throws:
- java.io.IOException
- Since:
- 4.0
 
 - 
deserializeTablepublic static void deserializeTable(org.eclipse.net4j.util.io.ExtendedDataInput in, java.sql.Connection connection, IDBTable table, org.eclipse.net4j.util.om.monitor.OMMonitor monitor, DBUtil.DeserializeRowHandler handler) throws java.io.IOException- Throws:
- java.io.IOException
- Since:
- 4.1
 
 - 
tracepublic static java.lang.String trace(java.lang.String sql) - Since:
- 3.0
 
 - 
isTracerEnabledpublic static boolean isTracerEnabled() - Since:
- 4.2
 
 
- 
 
-