Package org.eclipse.net4j.db

# Class DBUtil

java.lang.Object
org.eclipse.net4j.db.DBUtil

public final class DBUtil extends Object
A utility class with various static factory and convenience methods.

Author:
  Eike Stepper

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`static interface`
`DBUtil.DeserializeRowHandler`
A `row handler` with a `method` that is called once per row deserialized within `DBUtil.deserializeTable()`.
`static interface`
`DBUtil.RowHandler`
Call-back interface with a `method` that is called after a number of table rows have been handled by one of the subtypes of this interface.
`static interface`
`DBUtil.RunnableWithConnection<T>`
`static interface`
`DBUtil.SerializeRowHandler`
A `row handler` with a `method` that is called once per row serialized within `DBUtil.serializeTable()`.

## Field Summary

Fields
Modifier and Type
Field
Description
`static final String[]`
`ALL_TABLE_NAME_TYPES`
`static final boolean`
`DISABLE_QUOTED_NAMES`
`static final int`
`MAX_BATCH_SIZE`
`static final String`
`PROP_ENABLE_NOISY_CLOSE`
A system property to enable noisy close, i.e.
`static final String`
`QUOTE`

## Method Summary

Modifier and Type
Method
Description
`static int`
`asInt(Object value)`
`static long`
`asLong(Object value)`
`static BatchedStatement`
`batched(PreparedStatement delegate, int batchSize)`
`static int`
`clearTable(Connection connection, String tableName)`
`static int`
`clearTable(Connection connection, IDBTable table)`
`static Exception`
`close(Connection connection)`
`static Exception`
`close(ResultSet resultSet)`
`static Exception`
`close(Statement statement)`
`static boolean`
`containsName(Set<String> names, String name, boolean caseSensitive)`
`static IDBSchema`
`copySchema(IDBSchema source)`
`static IDBConnectionProvider`
`createConnectionProvider(DataSource dataSource)`
`static IDBConnectionProvider2`
`createConnectionProvider(DataSource dataSource, String user)`
`static DataSource`
`createDataSource(Map<Object,Object> properties)`
`static DataSource`
`createDataSource(Map<Object,Object> properties, String namespace)`
`static DataSource`
`createDataSource(Map<Object,Object> properties, String namespace, String driverClassKey)`
`static IDBSchema`
`createSchema(String name)`
Creates a case-insensitive schema with the given `name`.
`static IDBSchema`
`createSchema(String name, boolean caseSensitive, boolean qualifiedTableNames)`
`static void`
`deserializeTable(org.eclipse.net4j.util.io.ExtendedDataInput in, Connection connection, IDBTable table, org.eclipse.net4j.util.om.monitor.OMMonitor monitor)`
`static void`
`deserializeTable(org.eclipse.net4j.util.io.ExtendedDataInput in, Connection connection, IDBTable table, org.eclipse.net4j.util.om.monitor.OMMonitor monitor, DBUtil.DeserializeRowHandler handler)`
`static List<Exception>`
`dropAllTables(Connection connection, String schemaName)`
`static List<Exception>`
`dropAllTables(Connection connection, String schemaName, boolean caseSensitive)`
`static void`
`dump(IDBNamedElement namedElement)`
`static void`
`dump(IDBNamedElement namedElement, Writer writer)`
`static String`
`dumpToString(IDBNamedElement namedElement)`
`static boolean`
`equalNames(String name1, String name2, boolean caseSensitive)`
`static void`
`execute(Connection connection, CharSequence sql)`
`static <T> T`
`execute(IDBConnectionProvider connectionProvider, DBUtil.RunnableWithConnection<T> runnable)`
`static void`
`executeBatch(PreparedStatement stmt, int counter)`
`static void`
`executeBatch(PreparedStatement stmt, int counter, boolean checkExactlyOne)`
`static void`
`forEachTable(Connection connection, String schemaName, boolean caseSensitive, org.eclipse.net4j.util.ConsumerWithException<String,SQLException> tableNameConsumer)`
`static List<String>`
`getAllSchemaNames(Connection connection)`
`static Set<String>`
`getAllSchemaNames(DatabaseMetaData metaData)`
`static List<String>`
`getAllSchemaTableNames(Connection connection)`
Deprecated.
As of 4.2 use `getAllSchemaNames(Connection)`.
`static List<String>`
`getAllSchemaTableNames(DatabaseMetaData metaData)`
Deprecated.
As of 4.2 use `getAllSchemaNames(DatabaseMetaData)`.
`static List<String>`
`getAllTableNames(Connection connection, String schemaName)`
`static List<String>`
`getAllTableNames(Connection connection, String schemaName, boolean caseSensitive)`
`static IDBAdapter`
`getDBAdapter(String adapterName)`
Retrieves an `adapter` from the `adapter registry`.
`static int`
`getRowCount(Connection connection, String tableName)`
Returns the number of rows contained in the given table.
`static int`
`getRowCount(ResultSet resultSet)`
Returns the number of rows contained in the given result set.
`static boolean`
`isOptional(IDBElement element)`
`static boolean`
`isTracerEnabled()`
`static String`
`name(String name)`
`static String`
`name(String name, Class<?> scope)`
`static String`
`name(String name, String scope)`
`static IDBDatabase`
`openDatabase(IDBAdapter adapter, IDBConnectionProvider connectionProvider, String schemaName)`
`static IDBDatabase`
`openDatabase(IDBAdapter adapter, IDBConnectionProvider connectionProvider, String schemaName, boolean fixNullableIndexColumns)`
`static IDBDatabase`
`openDatabase(IDBAdapter adapter, IDBConnectionProvider connectionProvider, String schemaName, boolean fixNullableIndexColumns, boolean qualifiedTableNames)`
`static String`
`quoted(String name)`
`static IDBSchema`
`readSchema(IDBAdapter adapter, Connection connection, String name)`
`static IDBSchema`
`readSchema(IDBAdapter adapter, Connection connection, String name, boolean fixNullableIndexColumns)`
`static IDBSchema`
`readSchema(IDBAdapter adapter, Connection connection, String name, boolean fixNullableIndexColumns, boolean qualifiedTableNames)`
`static void`
`readSchema(IDBAdapter adapter, Connection connection, IDBSchema schema)`
`static Exception`
`rollbackSilently(Connection connection)`
`static Object[]`
`select(Connection connection, String where, IDBField... fields)`
`static int`
`select(Connection connection, IDBRowHandler rowHandler, String where, IDBField... fields)`
`static int`
`select(Connection connection, IDBRowHandler rowHandler, IDBField... fields)`
`static int`
`selectMaximumInt(Connection connection, IDBField field, String... where)`
`static long`
`selectMaximumLong(Connection connection, IDBField field, String... where)`
`static int`
`selectMinimumInt(Connection connection, IDBField field, String... where)`
`static long`
`selectMinimumLong(Connection connection, IDBField field, String... where)`
`static void`
`serializeTable(org.eclipse.net4j.util.io.ExtendedDataOutput out, Connection connection, IDBTable table, String tableAlias, String sqlSuffix)`
`static void`
`serializeTable(org.eclipse.net4j.util.io.ExtendedDataOutput out, Connection connection, IDBTable table, String tableAlias, String sqlSuffix, DBUtil.SerializeRowHandler handler)`
`static boolean`
`setAutoCommit(Connection connection, boolean autoCommit)`
`static boolean`
`setOptional(IDBElement element, boolean optional)`
`static void`
`sqlDump(Connection conn, String sql)`
Deprecated.
Should only be used when debugging.
`static void`
`sqlDump(IDBConnectionProvider connectionProvider, String sql)`
Deprecated.
Should only be used when debugging.
`static String`
`trace(String sql)`
`static int`
`update(Connection connection, String sql)`
`static int`
`update(PreparedStatement stmt, boolean exactlyOne)`
Execute update on the given prepared statement and handle common cases of return values.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### QUOTE

public static final String QUOTE

Since:
  4.12
See Also:
  - Constant Field Values

### MAX_BATCH_SIZE

public static final int MAX_BATCH_SIZE

Since:
  4.2

### DISABLE_QUOTED_NAMES

public static final boolean DISABLE_QUOTED_NAMES

Since:
  4.12

### PROP_ENABLE_NOISY_CLOSE

public static final String PROP_ENABLE_NOISY_CLOSE
A system property to enable noisy close, i.e. exception catch in close methods are thrown as `DBException` exception.

Since:
  4.4
See Also:
  - Constant Field Values

### ALL_TABLE_NAME_TYPES

public static final String[] ALL_TABLE_NAME_TYPES

Since:
  4.12

## Method Details

### dumpToString

public static String dumpToString(IDBNamedElement namedElement)

Since:
  4.2

### dump

public static void dump(IDBNamedElement namedElement)

Since:
  4.2

### dump

public static void dump(IDBNamedElement namedElement, Writer writer) throws IOException

Throws:
  `IOException`
Since:
  4.2

### openDatabase

public static IDBDatabase openDatabase(IDBAdapter adapter, IDBConnectionProvider connectionProvider, String schemaName)

Since:
  4.2

### openDatabase

public static IDBDatabase openDatabase(IDBAdapter adapter, IDBConnectionProvider connectionProvider, String schemaName, boolean fixNullableIndexColumns)

Since:
  4.2

### openDatabase

public static IDBDatabase openDatabase(IDBAdapter adapter, IDBConnectionProvider connectionProvider, String schemaName, boolean fixNullableIndexColumns, boolean qualifiedTableNames)

Since:
  4.12

### createSchema

public static IDBSchema createSchema(String name)
Creates a case-insensitive schema with the given `name`.

### createSchema

public static IDBSchema createSchema(String name, boolean caseSensitive, boolean qualifiedTableNames)

Since:
  4.12

### readSchema

public static void readSchema(IDBAdapter adapter, Connection connection, IDBSchema schema)

Since:
  4.2

### readSchema

public static IDBSchema readSchema(IDBAdapter adapter, Connection connection, String name)

Since:
  4.2

### readSchema

public static IDBSchema readSchema(IDBAdapter adapter, Connection connection, String name, boolean fixNullableIndexColumns)

Since:
  4.2

### readSchema

public static IDBSchema readSchema(IDBAdapter adapter, Connection connection, String name, boolean fixNullableIndexColumns, boolean qualifiedTableNames)

Since:
  4.12

### copySchema

public static IDBSchema copySchema(IDBSchema source)

Since:
  4.2

### createDataSource

public static DataSource createDataSource(Map<Object,Object> properties)

### createDataSource

public static DataSource createDataSource(Map<Object,Object> properties, String namespace)

### createDataSource

public static DataSource createDataSource(Map<Object,Object> properties, String namespace, String driverClassKey)

### createConnectionProvider

public static IDBConnectionProvider createConnectionProvider(DataSource dataSource)

### createConnectionProvider

public static IDBConnectionProvider2 createConnectionProvider(DataSource dataSource, String user)

Since:
  4.3

### getDBAdapter

public static IDBAdapter getDBAdapter(String adapterName)
Retrieves an `adapter` from the `adapter 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 registry` manually.

### batched

public static BatchedStatement batched(PreparedStatement delegate, int batchSize) throws DBException

Throws:
  `DBException`
Since:
  4.5

### close

public static Exception close(ResultSet resultSet)

### close

public static Exception close(Statement statement)

### close

public static Exception close(Connection connection)

### isOptional

public static boolean isOptional(IDBElement element)

Since:
  4.6

### setOptional

public static boolean setOptional(IDBElement element, boolean optional)

Since:
  4.6

### setAutoCommit

public static boolean setAutoCommit(Connection connection, boolean autoCommit)

Since:
  4.2

### rollbackSilently

public static Exception rollbackSilently(Connection connection)

Since:
  4.2

### quoted

public static String quoted(String name)

Since:
  4.12

### name

public static String name(String name, String scope)

Since:
  4.12

### name

public static String name(String name, Class<?> scope)

Since:
  4.12

### name

public static String name(String name)

Since:
  4.12

### equalNames

public static boolean equalNames(String name1, String name2, boolean caseSensitive)

Since:
  4.12

### containsName

public static boolean containsName(Set<String> names, String name, boolean caseSensitive)

Since:
  4.12

### getAllSchemaNames

public static List<String> getAllSchemaNames(Connection connection)

Since:
  4.2

### getAllSchemaNames

public static Set<String> getAllSchemaNames(DatabaseMetaData metaData)

Since:
  4.2

### getAllTableNames

public static List<String> getAllTableNames(Connection connection, String schemaName)

### getAllTableNames

public static List<String> getAllTableNames(Connection connection, String schemaName, boolean caseSensitive)

Since:
  4.12

### forEachTable

public static void forEachTable(Connection connection, String schemaName, boolean caseSensitive, org.eclipse.net4j.util.ConsumerWithException<String,SQLException> tableNameConsumer)

Since:
  4.12

### dropAllTables

public static List<Exception> dropAllTables(Connection connection, String schemaName)

Since:
  4.0

### dropAllTables

public static List<Exception> dropAllTables(Connection connection, String schemaName, boolean caseSensitive)

Since:
  4.12

### asInt

public static int asInt(Object value)

Since:
  4.2

### asLong

public static long asLong(Object value)

Since:
  4.2

### selectMinimumInt

public static int selectMinimumInt(Connection connection, IDBField field, String... where) throws DBException

Throws:
  `DBException`
Since:
  3.0

### selectMinimumLong

public static long selectMinimumLong(Connection connection, IDBField field, String... where) throws DBException

Throws:
  `DBException`
Since:
  3.0

### selectMaximumInt

public static int selectMaximumInt(Connection connection, IDBField field, String... where) throws DBException

Throws:
  `DBException`
Since:
  3.0

### selectMaximumLong

public static long selectMaximumLong(Connection connection, IDBField field, String... where) throws DBException

Throws:
  `DBException`
Since:
  3.0

### execute

public static <T> T execute(IDBConnectionProvider connectionProvider, DBUtil.RunnableWithConnection<T> runnable)

Since:
  4.2

### execute

public static void execute(Connection connection, CharSequence sql)

Since:
  4.2

### executeBatch

public static void executeBatch(PreparedStatement stmt, int counter)

Since:
  4.1

### executeBatch

public static void executeBatch(PreparedStatement stmt, int counter, boolean checkExactlyOne)

Since:
  4.1

### update

public static int update(Connection connection, String sql)

### update

public static int update(PreparedStatement stmt, boolean exactlyOne) throws SQLException
Execute 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:
  `SQLException` - if `PreparedStatement.executeUpdate()` throws it.
  `IllegalStateException` - if the check indicated by `excatlyOne` indicates an error.
Since:
  4.0

### clearTable

public static int clearTable(Connection connection, IDBTable table)

Since:
  4.1

### clearTable

public static int clearTable(Connection connection, String tableName)

Since:
  4.1

### select

public static int select(Connection connection, IDBRowHandler rowHandler, String where, IDBField... fields) throws DBException

Throws:
  `DBException`

### select

public static int select(Connection connection, IDBRowHandler rowHandler, IDBField... fields) throws DBException

Throws:
  `DBException`

### select

public static Object[] select(Connection connection, String where, IDBField... fields) throws DBException

Throws:
  `DBException`

### getRowCount

public static int getRowCount(ResultSet resultSet) throws DBException
Returns the number of rows contained in the given result set.

The `statement` of the result set must have been created with `TYPE_SCROLL_INSENSITIVE`.

Throws:
  `DBException`
Since:
  4.0

### getRowCount

public static int getRowCount(Connection connection, String tableName) throws DBException
Returns the number of rows contained in the given table.

Throws:
  `DBException`
Since:
  4.5

### serializeTable

public static void serializeTable(org.eclipse.net4j.util.io.ExtendedDataOutput out, Connection connection, IDBTable table, String tableAlias, String sqlSuffix) throws DBException, IOException

Throws:
  `DBException`
  `IOException`
Since:
  3.0

### serializeTable

public static void serializeTable(org.eclipse.net4j.util.io.ExtendedDataOutput out, Connection connection, IDBTable table, String tableAlias, String sqlSuffix, DBUtil.SerializeRowHandler handler) throws DBException, IOException

Throws:
  `DBException`
  `IOException`
Since:
  4.1

### deserializeTable

public static void deserializeTable(org.eclipse.net4j.util.io.ExtendedDataInput in, Connection connection, IDBTable table, org.eclipse.net4j.util.om.monitor.OMMonitor monitor) throws IOException

Throws:
  `IOException`
Since:
  4.0

### deserializeTable

public static void deserializeTable(org.eclipse.net4j.util.io.ExtendedDataInput in, Connection connection, IDBTable table, org.eclipse.net4j.util.om.monitor.OMMonitor monitor, DBUtil.DeserializeRowHandler handler) throws IOException

Throws:
  `IOException`
Since:
  4.1

### trace

public static String trace(String sql)

Since:
  3.0

### isTracerEnabled

public static boolean isTracerEnabled()

Since:
  4.2

### sqlDump

@Deprecated public static void sqlDump(Connection conn, String sql)
Deprecated.
Should only be used when debugging.
For debugging purposes ONLY!

Since:
  3.0

### sqlDump

@Deprecated public static void sqlDump(IDBConnectionProvider connectionProvider, String sql)
Deprecated.
Should only be used when debugging.
For debugging purposes ONLY!

Since:
  3.0

### getAllSchemaTableNames

@Deprecated public static List<String> getAllSchemaTableNames(Connection connection)
Deprecated.
As of 4.2 use `getAllSchemaNames(Connection)`.

Since:
  3.0

### getAllSchemaTableNames

@Deprecated public static List<String> getAllSchemaTableNames(DatabaseMetaData metaData)
Deprecated.
As of 4.2 use `getAllSchemaNames(DatabaseMetaData)`.

Since:
  3.0