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

  • Method Details

    • toString

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

      public boolean isCaseSensitive()
    • setCaseSensitive

      public Query setCaseSensitive(boolean caseSensitive)
    • isAutoDeriveColumnNames

      public boolean isAutoDeriveColumnNames()
    • setAutoDeriveColumnNames

      public Query setAutoDeriveColumnNames(boolean autoDeriveColumnNames)
    • throwOnMappingFailure

      public Query throwOnMappingFailure(boolean throwOnMappingFailure)
    • isThrowOnMappingFailure

      public boolean isThrowOnMappingFailure()
    • getConnection

      public Connection getConnection()
    • getName

      public String getName()
    • setName

      public Query setName(String name)
    • getResultSetHandlerFactoryBuilder

      public ResultSetHandlerFactoryBuilder getResultSetHandlerFactoryBuilder()
    • setResultSetHandlerFactoryBuilder

      public void setResultSetHandlerFactoryBuilder(ResultSetHandlerFactoryBuilder resultSetHandlerFactoryBuilder)
    • getParamNameToIdxMap

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      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

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

      public Query bind(Object pojo)
    • close

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

      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

      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

      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

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

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

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

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

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

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

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

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

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

      public LazyTable executeAndFetchTableLazy()
    • executeAndFetchTable

      public Table executeAndFetchTable()
    • executeUpdate

      public Connection executeUpdate()
    • executeScalar

      public Object executeScalar()
    • executeScalar

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

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

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

      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

      public int getMaxBatchRecords()
    • getCurrentBatchRecords

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

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

      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

      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

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

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

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

      public Query addColumnMapping(String columnName, String propertyName)