Class DatabaseUtil
- java.lang.Object
-
- jetbrains.buildServer.serverSide.db.DatabaseUtil
-
public final class DatabaseUtil extends Object
This class provides utility methods to perform DDL operations in database-agnostic way. Currently supported databases: HSQL(default diualect), MySQL (4+), PostgreSQL (8+), Oracle (10g+). TODO: Refactor: method are inconsistend some are only construct SQL DDL and some execute it.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
assignTmpInts(DBFunctions dbf, String tableName, Collection<Integer> values)
Assigns the given set of integer values to the specified temporary table.static void
assignTmpLongs(DBFunctions dbf, String tableName, Collection<Long> values)
Assigns the given set of long integer values to the specified temporary table.static void
assignTmpStrings(DBFunctions dbf, String tableName, Collection<String> values)
Assigns the given set of strings to the specified temporary table.static void
assignUnicodeTmpStrings(DBFunctions dbf, String tableName, Collection<UnicodeString> values)
Assigns the given set of objects to the specified temporary table.static void
cleanTmpTable(DBFunctions dbf, String tableName)
Cleans the specified temporary table.static void
close(Object... args)
Close anything closablestatic void
close(ResultSet rset)
Closes aResultSet
.static void
close(Statement stmt)
Closes aStatement
.static void
execute(Statement stmt, String sqlText)
Executes statement gathering as many error detail as possiblestatic String
fixTableName(DatabaseType databaseType, String name)
Converts table name to proper case for metadata quering.static char
getChar(ResultSet rset, int columnIndex, char defaultChar)
static DatabaseType
getDatabaseType(DBFunctions dbf)
Only to compatibility with legacy converters.static int
getInt(ResultSet rset, int columnIndex, int defaultValue)
static int
getInt(ResultSet rset, String columnName, int defaultValue)
static Integer
getNullableInt(ResultSet rs, String columnName)
static Collection<Integer>
getTmpInts(DBFunctions dbf, String tableName)
Retrieves integer values from the first column of the specified temporary table.static Collection<Long>
getTmpLongs(DBFunctions dbf, String tableName)
Retrieves long integer values from the first column of the specified temporary table.static Collection<String>
getTmpStrings(DBFunctions dbf, String tableName)
Retrieves strings from the first column of the specified temporary table.static boolean
tableExists(DBFunctions dbf, String tableName)
Check table existence via database metadata.
-
-
-
Method Detail
-
assignTmpInts
public static void assignTmpInts(@NotNull DBFunctions dbf, @NotNull String tableName, @NotNull Collection<Integer> values) throws DBException
Assigns the given set of integer values to the specified temporary table. The table is cleaned up before assigning.- Parameters:
dbf
- dbf-access service.tableName
- logical table name.values
- values to assign.- Throws:
DBException
- Since:
- 7.0
- See Also:
getTmpInts(DBFunctions, String)
-
assignTmpLongs
public static void assignTmpLongs(@NotNull DBFunctions dbf, @NotNull String tableName, @NotNull Collection<Long> values) throws DBException
Assigns the given set of long integer values to the specified temporary table. The table is cleaned up before assigning.- Parameters:
dbf
- dbf-access service.tableName
- logical table name.values
- values to assign.- Throws:
DBException
- Since:
- 7.0
- See Also:
getTmpLongs(DBFunctions, String)
-
assignTmpStrings
public static void assignTmpStrings(@NotNull DBFunctions dbf, @NotNull String tableName, @NotNull Collection<String> values) throws DBException
Assigns the given set of strings to the specified temporary table. The table is cleaned up before assigning.- Parameters:
dbf
- dbf-access service.tableName
- logical table name.values
- strings to assign.- Throws:
DBException
- Since:
- 7.0
- See Also:
getTmpStrings(DBFunctions, String)
-
assignUnicodeTmpStrings
public static void assignUnicodeTmpStrings(@NotNull DBFunctions dbf, @NotNull String tableName, @NotNull Collection<UnicodeString> values) throws DBException
Assigns the given set of objects to the specified temporary table. The table is cleaned up before assigning.- Parameters:
dbf
- dbf-access service.tableName
- logical table name.values
- values to assign.- Throws:
DBException
- Since:
- 7.0
-
cleanTmpTable
public static void cleanTmpTable(@NotNull DBFunctions dbf, @NotNull String tableName) throws DBException
Cleans the specified temporary table.- Parameters:
dbf
- dbf-access service.tableName
- logical table name.- Throws:
DBException
- Since:
- 7.0
-
getTmpInts
public static Collection<Integer> getTmpInts(@NotNull DBFunctions dbf, @NotNull String tableName) throws DBException
Retrieves integer values from the first column of the specified temporary table.- Parameters:
dbf
- database access service.tableName
- logical table name.- Returns:
- retrieved values, the order is unpredictable.
- Throws:
DBException
- Since:
- 7.0
- See Also:
assignTmpInts(jetbrains.buildServer.serverSide.db.DBFunctions, java.lang.String, java.util.Collection<java.lang.Integer>)
-
getTmpLongs
public static Collection<Long> getTmpLongs(@NotNull DBFunctions dbf, @NotNull String tableName) throws DBException
Retrieves long integer values from the first column of the specified temporary table.- Parameters:
dbf
- database access service.tableName
- logical table name.- Returns:
- retrieved values, the order is unpredictable.
- Throws:
DBException
- Since:
- 7.0
- See Also:
assignTmpLongs(jetbrains.buildServer.serverSide.db.DBFunctions, java.lang.String, java.util.Collection<java.lang.Long>)
-
getTmpStrings
public static Collection<String> getTmpStrings(@NotNull DBFunctions dbf, @NotNull String tableName) throws DBException
Retrieves strings from the first column of the specified temporary table.- Parameters:
dbf
- database access service.tableName
- logical table name.- Returns:
- retrieved values, the order is unpredictable.
- Throws:
DBException
- Since:
- 7.0
- See Also:
assignTmpStrings(jetbrains.buildServer.serverSide.db.DBFunctions, java.lang.String, java.util.Collection<java.lang.String>)
-
getDatabaseType
public static DatabaseType getDatabaseType(DBFunctions dbf)
Only to compatibility with legacy converters.
-
tableExists
public static boolean tableExists(@NotNull DBFunctions dbf, @NotNull String tableName)
Check table existence via database metadata.- Parameters:
connection
- connectiontableName
- table name- Returns:
- flag
- Throws:
SQLException
- raw database exception
-
fixTableName
public static String fixTableName(DatabaseType databaseType, String name)
Converts table name to proper case for metadata quering.- Parameters:
databaseType
- database typename
- table name- Returns:
- proper table name
-
execute
public static void execute(@NotNull Statement stmt, String sqlText)
Executes statement gathering as many error detail as possible- Parameters:
stmt
- statementsqlText
- original SQL or code description
-
close
public static void close(@NotNull Statement stmt)
Closes a
Statement
.- Parameters:
stmt
- theStatement
to close.- Since:
- 10.0.5
- See Also:
close(ResultSet)
,close(Object...)
-
close
public static void close(@NotNull ResultSet rset)
Closes a
ResultSet
.- Parameters:
rset
- theResultSet
to close.- Since:
- 10.0.5
- See Also:
close(Statement)
,close(Object...)
-
close
public static void close(@NotNull Object... args)
Close anything closable- Parameters:
args
- closeable objects to close- See Also:
close(Statement)
,close(ResultSet)
-
getChar
public static char getChar(@NotNull ResultSet rset, int columnIndex, char defaultChar) throws SQLException
- Throws:
SQLException
-
getInt
public static int getInt(@NotNull ResultSet rset, int columnIndex, int defaultValue) throws SQLException
- Throws:
SQLException
-
getInt
public static int getInt(@NotNull ResultSet rset, String columnName, int defaultValue) throws SQLException
- Throws:
SQLException
-
getNullableInt
@Nullable public static Integer getNullableInt(@NotNull ResultSet rs, @NotNull String columnName) throws SQLException
- Throws:
SQLException
-
-