public class Db extends Object
| Constructor and Description |
|---|
Db()
open connection to default, can be used for testing purposes and opens a H2 file DB
connection to file default.h2
|
Db(String url,
String driver,
String user,
String password,
File executeQueryDumpFile,
int maxStatementsPerTransaction)
open connection to any database.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addPreprocessor(Preprocessor preprocessor)
add preprocessor which will be applied to any row being inserted on this connection
|
void |
appendToFile(String text)
append to file only
|
protected String |
beginTransactionStatement() |
void |
cleanDuplicates(String tableName,
String uniqueKeyPropertyName) |
void |
closeConnection() |
protected String |
commitStatement() |
void |
copyTableFrom(String tableName,
Db sourceDb) |
void |
copyView(String viewName,
Db sourceDb) |
boolean |
createOrReplaceTable(TableStructure tableStructure) |
boolean |
createView(ViewStructure viewStructure) |
protected void |
dropTableIfExists(String tableName) |
protected void |
dropViewIfExists(String viewName) |
protected void |
dumpQuery(String query) |
protected String |
encode(String value)
e.g.
|
protected String |
escapedQuoteLiteral()
the escaped literal for the apostrophe used within a string; something like "''" or "\'"
|
boolean |
execute(String query)
run a SQL query without a result
|
protected boolean |
executeQuery(String query) |
TableStructure |
getTableStructure(String tableName)
if used with connection-less dbs, returns first ever created definition of table, otherwise
current defintion of table
|
ViewStructure |
getViewStructure(String viewName) |
void |
insert(List<Map<String,Object>> rowsToInsert,
String targetTableName)
insert a result into table "targetTableName" on this connection.
|
void |
insert(Result rowsToInsert,
String targetTableName)
insert a result into table "targetTableName" on this connection.
|
protected boolean |
isCharacterLike(String dataType)
for a dataType, returns true if this is a "String-like" type, typically a VARCHAR.
|
protected String |
mapDataType(String originalDataType)
when copying a table to this connection, the original data type (copied from an other connection
with maybe a different DB system) can be mapped to an other datatype.
|
protected Object |
mapValue(Object o)
maps value if necessary; e.g.
|
Result |
query(String query)
run a SQL query where you know it generates a result set
|
static String |
quote(Object unquoted)
simply quote a String-like object, e.g.
|
protected String |
quoteIdentifierIfNecessary(String unquotedTableName)
if some circumstances, table and column names themselves have to be quoted, e.g.
|
protected String |
quoteIfNecessary(Object o)
makes an acceptable literal out of object o.
|
void |
switchExecuteQueryFile(File newFile) |
protected String |
tableStructureEntry(ch.brickwork.bsetl.db.PropertyStructure propertyStructure)
generates a "line" entry in a CREATE TABLE statement
|
String |
toSQLSet(Set setOfValues)
turns a set of objects into a set of the form
|
String |
wherePropertyIn(String propertyName,
Set set)
for a list of elements, returns a clause like
|
String |
wrap(Object value)
based on type of object, maps, quotes and encodes properly.
|
public Db() throws SQLException, ClassNotFoundException, IOException
public Db(String url, String driver, String user, String password, File executeQueryDumpFile, int maxStatementsPerTransaction) throws ClassNotFoundException, SQLException, IOException
url - if null, creates connection-less db (makes sense only in combination with executeQueryDumpFiledriver - user - password - maxStatementsPerTransaction - if > 0, all statements will be wrapped in BEGIN TRANSACTION..COMMIT; blocksexecuteQueryDumpFile - file to dump execute query statements (e.g. inserts), or nullClassNotFoundExceptionSQLExceptionIOExceptionpublic void appendToFile(String text) throws IOException
text - IOExceptionpublic void addPreprocessor(Preprocessor preprocessor)
preprocessor - public Result query(String query) throws SQLException
query - SQLExceptionpublic boolean execute(String query) throws SQLException, IOException
query - SQLExceptionIOExceptionprotected boolean executeQuery(String query) throws SQLException
SQLExceptionprotected void dumpQuery(String query) throws IOException
IOExceptionpublic void closeConnection()
throws SQLException,
IOException
SQLExceptionIOExceptionpublic void switchExecuteQueryFile(File newFile) throws IOException
IOExceptionpublic void insert(Result rowsToInsert, String targetTableName) throws SQLException, IOException, OperationNotAllowedException
rowsToInsert - targetTableName - SQLExceptionIOExceptionOperationNotAllowedExceptionpublic void insert(List<Map<String,Object>> rowsToInsert, String targetTableName) throws SQLException, IOException, OperationNotAllowedException
rowsToInsert - targetTableName - SQLExceptionIOExceptionOperationNotAllowedExceptionpublic void copyTableFrom(String tableName, Db sourceDb) throws SQLException, IOException, OperationNotAllowedException
public void copyView(String viewName, Db sourceDb) throws SQLException, IOException, OperationNotAllowedException
protected String beginTransactionStatement()
protected String commitStatement()
public String toSQLSet(Set setOfValues)
(o1, o2, ...)suitable for usage within a WHERE clause. Respects format based on type of object. E.g. strings will deliver ('abc', 'def'), numbers will deliver (1,2,3)
setOfValues - public String wherePropertyIn(String propertyName, Set set)
WHERE ([propertyName] IN (.,.,.) OR [propertyName] IN (.,.,.) OR ...)clause is partitioned to keep individual sets small. Max. size of a parition is 100
propertyName - set - public static String quote(Object unquoted)
unquoted - public TableStructure getTableStructure(String tableName) throws SQLException, OperationNotAllowedException
tableName - SQLExceptionOperationNotAllowedExceptionpublic boolean createOrReplaceTable(TableStructure tableStructure) throws SQLException, IOException, OperationNotAllowedException
public void cleanDuplicates(String tableName, String uniqueKeyPropertyName) throws SQLException, IOException
SQLExceptionIOExceptionprotected void dropTableIfExists(String tableName) throws SQLException, IOException, OperationNotAllowedException
public ViewStructure getViewStructure(String viewName) throws SQLException
SQLExceptionpublic boolean createView(ViewStructure viewStructure) throws SQLException, IOException, OperationNotAllowedException
protected void dropViewIfExists(String viewName) throws SQLException, IOException, OperationNotAllowedException
protected boolean isCharacterLike(String dataType)
dataType - public String wrap(Object value)
value - protected String encode(String value)
value - protected String quoteIfNecessary(Object o)
o - protected Object mapValue(Object o)
o - protected String escapedQuoteLiteral()
protected String quoteIdentifierIfNecessary(String unquotedTableName)
unquotedTableName - protected String mapDataType(String originalDataType)
originalDataType - protected String tableStructureEntry(ch.brickwork.bsetl.db.PropertyStructure propertyStructure)
propertyStructure - Copyright © 2021. All rights reserved.