Class Database

java.lang.Object
com.dieselpoint.norm.Database

public class Database extends Object
Provides methods to access a database.
  • Field Details

    • sqlMaker

      protected SqlMaker sqlMaker
    • ds

      protected DataSource ds
    • dataSourceClassName

      protected String dataSourceClassName
    • driverClassName

      protected String driverClassName
    • jdbcUrl

      protected String jdbcUrl
    • serverName

      protected String serverName
    • databaseName

      protected String databaseName
    • user

      protected String user
    • password

      protected String password
    • maxPoolSize

      protected int maxPoolSize
    • maxLatency

      protected long maxLatency
    • latencyAlerters

      protected ArrayList<LatencyAlerter> latencyAlerters
    • dataSourceProperties

      protected Map<String,String> dataSourceProperties
  • Constructor Details

    • Database

      public Database()
  • Method Details

    • setSqlMaker

      public void setSqlMaker(SqlMaker sqlMaker)
      Set the maker object for the particular flavor of sql.
    • getSqlMaker

      public SqlMaker getSqlMaker()
    • getDataSource

      protected DataSource getDataSource() throws SQLException
      Provides the DataSource used by this database. Override this method to change how the DataSource is created or configured.
      Throws:
      SQLException
    • addDataSourceProperty

      public void addDataSourceProperty(String name, String value)
    • sql

      public Query sql(String sql, Object... args)
      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

      public Query where(String where, Object... args)
      Create a query with the given where clause.
      Parameters:
      where - Example: "name=?"
      args - The parameter values to use in the where, example: "Bob"
    • orderBy

      public Query orderBy(String orderBy)
      Create a query with the given "order by" clause.
    • getConnection

      public Connection 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

      public Query createTable(Class<?> clazz)
      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

      public Query insert(Object row)
      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

      public Query generatedKeyReceiver(Object generatedKeyReceiver, String... generatedKeyNames)
    • delete

      public Query delete(Object row)
      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

      public <T> List<T> results(Class<T> clazz)
      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

      public <T> T first(Class<T> clazz)
      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

      public Query update(Object row)
      Update a row in a table. It will match an existing row based on the primary key.
    • upsert

      public Query upsert(Object row)
      Upsert a row in a table. It will insert, and if that fails, do an update with a match on a primary key.
    • table

      public Query table(String table)
      Create a query and specify which table it operates on.
    • startTransaction

      public Transaction 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

      public Query transaction(Transaction trans)
      Create a query that uses this transaction object.
    • close

      public void close()
    • setDataSourceClassName

      public void setDataSourceClassName(String dataSourceClassName)
    • setDriverClassName

      public void setDriverClassName(String driverClassName)
    • setJdbcUrl

      public void setJdbcUrl(String jdbcUrl)
    • setServerName

      public void setServerName(String serverName)
    • setDatabaseName

      public void setDatabaseName(String databaseName)
    • setUser

      public void setUser(String user)
    • setPassword

      public void setPassword(String password)
    • 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 all Query or Transaction.commit() calls should tolerate. By default millis is set to -1 which turns off all latency alerting. Note that setting maxLatency to 0 is an easy way to log all SQL Statements. This value can also be set using environment variable norm.maxLatency
    • addLatencyAlerter

      public void addLatencyAlerter(LatencyAlerter alerter)
      Adds the provided LatencyAlerter instance to the instances that are called in-order, when a Query or Transaction.commit() call to the database exceeds the maximum latency (either the global maximum set via setMaxLatency(long), or Query.maxLatency(long) or Transaction.maxLatency(long)
      Parameters:
      alerter - , the alerter to add
    • alertLatency

      public void alertLatency(DbLatencyWarning latencyWarning)