Package com.dieselpoint.norm
Class Database
java.lang.Object
com.dieselpoint.norm.Database
Provides methods to access a database.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddDataSourceProperty(String name, String value) voidaddLatencyAlerter(LatencyAlerter alerter) Adds the providedLatencyAlerterinstance to the instances that are called in-order, when aQueryorTransaction.commit()call to the database exceeds the maximum latency (either the global maximum set viasetMaxLatency(long), orQuery.maxLatency(long)orTransaction.maxLatency(long)voidalertLatency(DbLatencyWarning latencyWarning) voidclose()createTable(Class<?> clazz) Simple, primitive method for creating a table based on a pojo.Delete a row in a table.<T> TReturns the first row in a query in a pojo.generatedKeyReceiver(Object generatedKeyReceiver, String... generatedKeyNames) SeegenerateKeyReceivermethod.Returns a JDBC connection.protected DataSourceProvides the DataSource used by this database.longintInsert a row into a table.Create a query with the given "order by" clause.<T> List<T>Execute a "select" query and get some results.voidsetDatabaseName(String databaseName) voidsetDataSourceClassName(String dataSourceClassName) voidsetDriverClassName(String driverClassName) voidsetJdbcUrl(String jdbcUrl) voidsetMaxLatency(long millis) voidsetMaxPoolSize(int maxPoolSize) voidsetPassword(String password) voidsetServerName(String serverName) voidsetSqlMaker(SqlMaker sqlMaker) Set the maker object for the particular flavor of sql.voidCreate a query using straight SQL.Start a database transaction.Create a query and specify which table it operates on.transaction(Transaction trans) Create a query that uses this transaction object.Update a row in a table.Upsert a row in a table.Create a query with the given where clause.
-
Field Details
-
sqlMaker
-
ds
-
dataSourceClassName
-
driverClassName
-
jdbcUrl
-
serverName
-
databaseName
-
user
-
password
-
maxPoolSize
protected int maxPoolSize -
maxLatency
protected long maxLatency -
latencyAlerters
-
dataSourceProperties
-
-
Constructor Details
-
Database
public Database()
-
-
Method Details
-
setSqlMaker
Set the maker object for the particular flavor of sql. -
getSqlMaker
-
getDataSource
Provides the DataSource used by this database. Override this method to change how the DataSource is created or configured.- Throws:
SQLException
-
addDataSourceProperty
-
sql
Create a query using straight SQL. Overrides any other methods like .where(), .orderBy(), etc.- Parameters:
sql- The SQL string to use, may include ? parameters.args- The parameter values to use in the query.
-
where
Create a query with the given where clause.- Parameters:
where- Example: "name=?"args- The parameter values to use in the where, example: "Bob"
-
orderBy
Create a query with the given "order by" clause. -
getConnection
Returns a JDBC connection. Can be useful if you need to customize how transactions work, but you shouldn't normally need to call this method. You must close the connection after you're done with it. -
createTable
Simple, primitive method for creating a table based on a pojo. Does not add indexes or implement complex data types. Probably not suitable for production use. -
insert
Insert a row into a table. The row pojo can have a @Table annotation to specify the table, or you can specify the table with the .table() method. -
generatedKeyReceiver
SeegenerateKeyReceivermethod. -
delete
Delete a row in a table. This method looks for an @Id annotation to find the row to delete by primary key, and looks for a @Table annotation to figure out which table to hit. -
results
Execute a "select" query and get some results. The system will create a new object of type "clazz" for each row in the result set and add it to a List. It will also try to extract the table name from a @Table annotation in the clazz. -
first
Returns the first row in a query in a pojo. Will return it in a Map if a class that implements Map is specified. -
update
Update a row in a table. It will match an existing row based on the primary key. -
upsert
Upsert a row in a table. It will insert, and if that fails, do an update with a match on a primary key. -
table
Create a query and specify which table it operates on. -
startTransaction
Start a database transaction. Pass the transaction object to each query or command that should be part of the transaction using the .transaction() method. Then call transaction.commit() or .rollback() to complete the process. No need to close the transaction.- Returns:
- a transaction object
-
transaction
Create a query that uses this transaction object. -
close
public void close() -
setDataSourceClassName
-
setDriverClassName
-
setJdbcUrl
-
setServerName
-
setDatabaseName
-
setUser
-
setPassword
-
getMaxPoolSize
public int getMaxPoolSize() -
setMaxPoolSize
public void setMaxPoolSize(int maxPoolSize) -
getMaxLatencyMillis
public long getMaxLatencyMillis() -
setMaxLatency
public void setMaxLatency(long millis) - Parameters:
millis- the maximum latency that allQueryorTransaction.commit()calls should tolerate. By default millis is set to-1which turns off all latency alerting. Note that setting maxLatency to0is an easy way to log all SQL Statements. This value can also be set using environment variablenorm.maxLatency
-
addLatencyAlerter
Adds the providedLatencyAlerterinstance to the instances that are called in-order, when aQueryorTransaction.commit()call to the database exceeds the maximum latency (either the global maximum set viasetMaxLatency(long), orQuery.maxLatency(long)orTransaction.maxLatency(long)- Parameters:
alerter- , the alerter to add
-
alertLatency
-