Package org.sql2o

Class Query

java.lang.Object
org.sql2o.Query
All Implemented Interfaces:
AutoCloseable

public class Query extends Object implements AutoCloseable
Represents a sql2o statement. With sql2o, all statements are instances of the Query class.
  • Constructor Details Link icon

  • Method Details Link icon

    • toString Link icon

      public String toString()
      Overrides:
      toString in class Object
    • isCaseSensitive Link icon

      public boolean isCaseSensitive()
    • setCaseSensitive Link icon

      public Query setCaseSensitive(boolean caseSensitive)
    • isAutoDeriveColumnNames Link icon

      public boolean isAutoDeriveColumnNames()
    • setAutoDeriveColumnNames Link icon

      public Query setAutoDeriveColumnNames(boolean autoDeriveColumnNames)
    • throwOnMappingFailure Link icon

      public Query throwOnMappingFailure(boolean throwOnMappingFailure)
    • isThrowOnMappingFailure Link icon

      public boolean isThrowOnMappingFailure()
    • getConnection Link icon

      public Connection getConnection()
    • getName Link icon

      public String getName()
    • setName Link icon

      public Query setName(String name)
    • getResultSetHandlerFactoryBuilder Link icon

      public ResultSetHandlerFactoryBuilder getResultSetHandlerFactoryBuilder()
    • setResultSetHandlerFactoryBuilder Link icon

      public void setResultSetHandlerFactoryBuilder(ResultSetHandlerFactoryBuilder resultSetHandlerFactoryBuilder)
    • getParamNameToIdxMap Link icon

      public Map<String,List<Integer>> getParamNameToIdxMap()
    • addParameter Link icon

      public <T> Query addParameter(String name, Class<T> parameterClass, T value)
    • withParams Link icon

      public Query withParams(Object... paramValues)
    • addParameter Link icon

      public Query addParameter(String name, Object value)
    • addParameter Link icon

      public Query addParameter(String name, InputStream value)
    • addParameter Link icon

      public Query addParameter(String name, int value)
    • addParameter Link icon

      public Query addParameter(String name, Integer value)
    • addParameter Link icon

      public Query addParameter(String name, long value)
    • addParameter Link icon

      public Query addParameter(String name, Long value)
    • addParameter Link icon

      public Query addParameter(String name, String value)
    • addParameter Link icon

      public Query addParameter(String name, Timestamp value)
    • addParameter Link icon

      public Query addParameter(String name, Time value)
    • addParameter Link icon

      public Query addParameter(String name, boolean value)
    • addParameter Link icon

      public Query addParameter(String name, Boolean value)
    • addParameter Link icon

      public Query addParameter(String name, UUID value)
    • addParameter Link icon

      public Query addParameter(String name, Object... values)
      Set an array parameter.
      For example:
           createQuery("SELECT * FROM user WHERE id IN(:ids)")
            .addParameter("ids", 4, 5, 6)
            .executeAndFetch(...)
       
      will generate the query : SELECT * FROM user WHERE id IN(4,5,6)

      It is not possible to use array parameters with a batch PreparedStatement: since the text query passed to the PreparedStatement depends on the number of parameters in the array, array parameters are incompatible with batch mode.

      If the values array is empty, null will be set to the array parameter: SELECT * FROM user WHERE id IN(NULL)
      Throws:
      NullPointerException - if values parameter is null
    • addParameter Link icon

      public Query addParameter(String name, Collection<?> values)
      Set an array parameter.
      See addParameter(String, Object...) for details
    • bind Link icon

      public Query bind(Object pojo)
    • close Link icon

      public void close()
      Specified by:
      close in interface AutoCloseable
    • executeAndFetchLazy Link icon

      public <T> ResultSetIterable<T> executeAndFetchLazy(Class<T> returnType)
      Read a collection lazily. Generally speaking, this should only be used if you are reading MANY results and keeping them all in a Collection would cause memory issues. You MUST call ResultSetIterable.close() when you are done iterating.
      Parameters:
      returnType - type of each row
      Returns:
      iterable results
    • executeAndFetchLazy Link icon

      public <T> ResultSetIterable<T> executeAndFetchLazy(ResultSetHandlerFactory<T> resultSetHandlerFactory)
      Read a collection lazily. Generally speaking, this should only be used if you are reading MANY results and keeping them all in a Collection would cause memory issues. You MUST call ResultSetIterable.close() when you are done iterating.
      Parameters:
      resultSetHandlerFactory - factory to provide ResultSetHandler
      Returns:
      iterable results
    • executeAndFetchLazy Link icon

      public <T> ResultSetIterable<T> executeAndFetchLazy(ResultSetHandler<T> resultSetHandler)
      Read a collection lazily. Generally speaking, this should only be used if you are reading MANY results and keeping them all in a Collection would cause memory issues. You MUST call ResultSetIterable.close() when you are done iterating.
      Parameters:
      resultSetHandler - ResultSetHandler
      Returns:
      iterable results
    • executeAndFetchUnique Link icon

      public <T> T executeAndFetchUnique(Class<T> returnType)
    • executeAndFetchUnique Link icon

      public <T> T executeAndFetchUnique(ResultSetHandler<T> resultSetHandler)
    • executeAndFetchUnique Link icon

      public <T> T executeAndFetchUnique(ResultSetHandlerFactory<T> factory)
    • executeAndFetch Link icon

      public <T> List<T> executeAndFetch(Class<T> returnType)
    • executeAndFetch Link icon

      public <T> List<T> executeAndFetch(ResultSetHandler<T> resultSetHandler)
    • executeAndFetch Link icon

      public <T> List<T> executeAndFetch(ResultSetHandlerFactory<T> factory)
    • executeAndFetchFirst Link icon

      public <T> T executeAndFetchFirst(Class<T> returnType)
    • executeAndFetchFirst Link icon

      public <T> T executeAndFetchFirst(ResultSetHandler<T> resultSetHandler)
    • executeAndFetchFirst Link icon

      public <T> T executeAndFetchFirst(ResultSetHandlerFactory<T> resultSetHandlerFactory)
    • executeAndFetchTableLazy Link icon

      public LazyTable executeAndFetchTableLazy()
    • executeAndFetchTable Link icon

      public Table executeAndFetchTable()
    • executeUpdate Link icon

      public Connection executeUpdate()
    • executeScalar Link icon

      public Object executeScalar()
    • executeScalar Link icon

      public <V> V executeScalar(Class<V> returnType)
    • executeScalar Link icon

      public <V> V executeScalar(Converter<V> converter)
    • executeScalarList Link icon

      public <T> List<T> executeScalarList(Class<T> returnType)
    • setMaxBatchRecords Link icon

      public Query setMaxBatchRecords(int maxBatchRecords)
      Sets the number of batched commands this Query allows to be added before implicitly calling executeBatch() from addToBatch().
      When set to 0, executeBatch is not called implicitly. This is the default behaviour.
      When using this, please take care about calling executeBatch() after finished adding all commands to the batch because commands may remain unexecuted after the last addToBatch() call. Additionally, if fetchGeneratedKeys is set, then previously generated keys will be lost after a batch is executed.
      Throws:
      IllegalArgumentException - Thrown if the value is negative.
    • getMaxBatchRecords Link icon

      public int getMaxBatchRecords()
    • getCurrentBatchRecords Link icon

      public int getCurrentBatchRecords()
      Returns:
      The current number of unexecuted batched statements
    • isExplicitExecuteBatchRequired Link icon

      public boolean isExplicitExecuteBatchRequired()
      Returns:
      True if maxBatchRecords is set and there are unexecuted batched commands or maxBatchRecords is not set
    • addToBatch Link icon

      public Query addToBatch()
      Adds a set of parameters to this Query object's batch of commands.
      If maxBatchRecords is more than 0, executeBatch is called upon adding that many commands to the batch.
      The current number of batched commands is accessible via the getCurrentBatchRecords() method.
    • addToBatchGetKeys Link icon

      public <A> List<A> addToBatchGetKeys(Class<A> klass)
      Adds a set of parameters to this Query object's batch of commands and returns any generated keys.
      If maxBatchRecords is more than 0, executeBatch is called upon adding that many commands to the batch. This method will return any generated keys if fetchGeneratedKeys is set.
      The current number of batched commands is accessible via the getCurrentBatchRecords() method.
    • executeBatch Link icon

      public Connection executeBatch() throws Sql2oException
      Throws:
      Sql2oException
    • getColumnMappings Link icon

      public Map<String,String> getColumnMappings()
      column mapping
    • setColumnMappings Link icon

      public Query setColumnMappings(Map<String,String> mappings)
    • addColumnMapping Link icon

      public Query addColumnMapping(String columnName, String propertyName)