public class Database extends Object
Databases.get(String)
.
Use createQuery(String)
to create an SQL query with built in connection management.
Use getConnection()
to obtain a regular JDBC connection (which has to be handled with some caution).
Modifier | Constructor and Description |
---|---|
protected |
Database(String name) |
Modifier and Type | Method and Description |
---|---|
SQLCall |
createFunctionCall(String fun,
Integer returnType)
Creates a new call wrapper which permits safe and convenient function calls.
|
SQLCall |
createProcedureCall(String fun)
Creates a new call wrapper which permits safe and convenient procedure calls.
|
SQLQuery |
createQuery(String sql)
Creates a new query wrapper which permits safe and convenient queries.
|
protected void |
fillValues(List<Object> valueList,
String sql,
PreparedStatement stmt) |
Connection |
getConnection()
Creates a new connection to the database.
|
DataSource |
getDatasource()
Provides access to the underlying
DataSource representing the connection pool. |
Connection |
getHostConnection()
Tries to obtain a host connection which is not bound to a specific database or schema.
|
int |
getNumActive()
Return the number of active connections
|
int |
getNumIdle()
Return the number of idle connections
|
String |
getPassword()
Returns the JDBC password supplied to the database.
|
int |
getSize()
Returns the maximal number of concurrent connections
|
String |
getUrl()
Returns the JDBC connection URL
|
String |
getUsername()
Returns the JDBC username supplied to the database.
|
boolean |
hasCapability(Capability cap)
Determines if the current driver has the requested capability.
|
Row |
insertRow(String table,
sirius.kernel.commons.Context ctx)
Generates an INSERT statement for the given table inserting all supplied parameters in ctx.
|
Row |
insertRow(String table,
Map<String,Object> row)
Boilerplate method to use
insertRow(String, sirius.kernel.commons.Context) with plan maps. |
protected void |
prepareValues(sirius.kernel.commons.Context ctx,
StringBuilder fields,
StringBuilder values,
List<Object> valueList) |
String |
toString() |
protected final String name
protected Database(String name)
public DataSource getDatasource()
DataSource
representing the connection pool.
You must ensure to close each opened connection property as otherwise the pool will lock up, once all
connections are busy. Consider using createQuery(String)
or
createFunctionCall(String, Integer)
or createProcedureCall(String)
to access the database
in a safe manner.
public Connection getConnection() throws SQLException
You must ensure to close each opened connection property as otherwise the pool will lock up, once all
connections are busy. Consider using createQuery(String)
or
createFunctionCall(String, Integer)
or createProcedureCall(String)
to access the database
in a safe manner.
Connection
to the databaseSQLException
- in case of a database errorpublic Connection getHostConnection() throws SQLException
This is used to setup test databases by executing an initial statement like CREATE DATABASE test.
SQLException
- in case of a database or configuration errorpublic SQLQuery createQuery(String sql)
Using this wrapper ensures proper connection handling and simplifies query creation.
public SQLCall createFunctionCall(String fun, Integer returnType)
fun
- name of the function to callreturnType
- the SQL type (Types
) of the return value of this functionpublic SQLCall createProcedureCall(String fun)
fun
- name of the procedure to callpublic Row insertRow(String table, sirius.kernel.commons.Context ctx) throws SQLException
table
- the target table to insert a rowctx
- contains names and values to insert into the databaseSQLException
- in case of a database errorprotected void fillValues(List<Object> valueList, String sql, PreparedStatement stmt)
protected void prepareValues(sirius.kernel.commons.Context ctx, StringBuilder fields, StringBuilder values, List<Object> valueList)
public Row insertRow(String table, Map<String,Object> row) throws SQLException
insertRow(String, sirius.kernel.commons.Context)
with plan maps.table
- the target table to insert a rowrow
- contains names and values to insert into the databaseSQLException
- in case of a database errorpublic String getUrl()
public String getUsername()
public String getPassword()
public int getSize()
public int getNumIdle()
public int getNumActive()
public boolean hasCapability(Capability cap)
cap
- the capability to determineCopyright © 2018. All rights reserved.