Class Query

java.lang.Object
com.dieselpoint.norm.Query

public class Query extends Object
Holds all of the information in a query. Create a query using Database.someQueryCreationMethod(), populate it using a builder pattern, and execute it using either .execute() (to update the database) or .results() (to get the results of a query.)
  • Constructor Details

  • Method Details

    • where

      public Query where(String where, Object... args)
      Add a where clause and some parameters to a query. Has no effect if the .sql() method is used.
      Parameters:
      where - Example: "name=?"
      args - The parameter values to use in the where, example: "Bob"
    • 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.
    • sql

      public Query sql(String sql, List<?> 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.
    • orderBy

      public Query orderBy(String orderBy)
      Add an "orderBy" clause to a query.
    • first

      public <T> T first(Class<T> clazz)
      Returns the first row in a query in a pojo, or null if the query returns no results. Will return it in a Map if a class that implements Map is specified.
    • results

      public <T> List<T> results(Class<T> clazz)
      Execute a "select" query and return a list of results where each row is an instance of clazz. Returns an empty list if there are no results.
    • 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.
    • upsert

      public Query upsert(Object row)
      Upsert a row into a table. See http://en.wikipedia.org/wiki/Merge_%28SQL%29
    • update

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

      public Query execute()
      Execute a sql command that does not return a result set. The sql should previously have been set with the sql(String) method. Returns this Query object. To see how the command did, call .rowsAffected().
    • generatedKeyReceiver

      public Query generatedKeyReceiver(Object generatedKeyReceiver, String... generatedKeyNames)
      Specify the object and its fields that should receive any column values that the database server generates during an insert or update. If a column is marked as "AUTO INCREMENT" (MySql) or has datatype "SERIAL" (Postgres) then the database will generate a value for that column. Databases can also generate a last-updated timestamp for a column, or just fill in a column with a default value. To get those values, you can specify generatedKeyReceiver as a Map or as a pojo. It can be the pojo you just inserted or updated. Specify also the field/column names that should be filled in.
    • createTable

      public Query createTable(Class<?> clazz)
      Deprecated.
      Simple, primitive method for creating a table based on a pojo.
    • 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.
    • delete

      public Query delete()
      Delete multiple rows in a table. Be sure to specify the table with the .table() method and limit the rows to delete using the .where() method.
    • table

      public Query table(String table)
      Specify the table to operate on.
    • getRowsAffected

      public int getRowsAffected()
      For queries that affect the database in some way, this method returns the number of rows affected. Call it after you call .execute(), .update(), .delete(), etc.: .table("foo").where("bar=bah").delete().rowsAffected();
    • transaction

      public Query transaction(Transaction trans)
      Specify that this query should be a part of the specified transaction.
    • getOrderBy

      public String getOrderBy()
    • getWhere

      public String getWhere()
    • getTable

      public String getTable()
    • getResultSetMetaData

      public ResultSetMetaData getResultSetMetaData()
    • getMaxLatencyMillis

      public long getMaxLatencyMillis()
    • maxLatency

      public Query maxLatency(long millis)
      sets the maximum acceptable latency for this query. Must be called before terminal operators such as execute() or first(Class) .
      If latency of the query exceeds the threshold then the LatencyAlerter that have been added to the Database will be called in order
      Parameters:
      millis - maximum number of milliseconds that a query can take to execute before an alert will be generated
      Returns:
      this, to enable maxLatency to be chained, a la db.sql( "select count(*) from Thing" ).maxLatency(50).first( Long.class )
    • getDatabase

      public Database getDatabase()