Class JdbcQuery

  • All Implemented Interfaces:
    SqlQuery

    public class JdbcQuery
    extends Object
    implements SqlQuery
    The JDBC stack implementation of the SQL Engine query contract. In fact it's an adapter the internal JDBC stuff.

    For more info please see the Tutorials.

    Author:
    Vladimir Hudec
    • Field Detail

      • logger

        final org.slf4j.Logger logger
        The internal slf4j logger.
      • connection

        Connection connection
        The connection to the database. It should be opened.
      • queryString

        String queryString
        The SQL query/statement command.
      • scalars

        List<String> scalars
        The collection of all scalars (output values declarations).
      • scalarTypes

        Map<String,​Object> scalarTypes
        The collection of all scalars types.
      • parameters

        List<String> parameters
        The collection of all parameters (input value declarations).
      • parameterValues

        Map<String,​Object> parameterValues
        The collection of all parameters values.
      • parameterTypes

        Map<String,​Object> parameterTypes
        The collection of all parameters types.
      • parameterOutValueTypes

        Map<String,​Object> parameterOutValueTypes
        The collection of all parameters types for output values.
      • parameterOutValueSetters

        Map<String,​OutValueSetter> parameterOutValueSetters
        The collection of all parameters output value setters.
      • parameterOutValuesToPickup

        Map<Integer,​Integer> parameterOutValuesToPickup
        The collection of all parameters, which have to be picked-up.
      • identities

        List<String> identities
        The collection of all (auto-generated) identities.
      • identityTypes

        Map<String,​Object> identityTypes
        The collection of all identities types.
      • sqlControl

        SqlControl sqlControl
        The compound parameters controlling the META SQL execution.
      • timeout

        Integer timeout
        A timeout for the underlying query.
      • firstResult

        Integer firstResult
        The first row to retrieve. -
      • maxResults

        Integer maxResults
        - * The maximum number of rows to retrieve. -
      • fetchSize

        Integer fetchSize
        - * The fetch size of rows to retrieve in one SQL. -
      • ordered

        boolean ordered
        The SQL output is sorted.
      • logError

        boolean logError
        The failed SQL command should be logged.
      • NO_MORE_DATA

        private static final Map<String,​Object> NO_MORE_DATA
        The indicator there are no more data in ResultSet.
    • Constructor Detail

      • JdbcQuery

        public JdbcQuery​(Connection connection,
                         String queryString)
        Creates a new instance of this adapter.
        Parameters:
        connection - the connection to the database
        queryString - the SQL query/statement command
    • Method Detail

      • getQuery

        public Object getQuery()
        Returns the internal representation of this query. This implementation depends on the stack on top of which the SQL Processor works. It can be for example the Hibernate SQLQuery instance.
        Specified by:
        getQuery in interface SqlQuery
        Returns:
        the internal representation of this query
      • setSqlControl

        public SqlQuery setSqlControl​(SqlControl sqlControl)
        Sets the compound parameters controlling the META SQL execution.
        Specified by:
        setSqlControl in interface SqlQuery
        Parameters:
        sqlControl - the compound parameters controlling the META SQL execution
        Returns:
        this instance to enable the methods chaining
      • setOrdered

        public SqlQuery setOrdered​(boolean ordered)
        Sets the indicator the SQL output is sorted.
        Specified by:
        setOrdered in interface SqlQuery
        Parameters:
        ordered - the indicator the SQL output is sorted
        Returns:
        this instance to enable the methods chaining
      • isSetJDBCIdentity

        private boolean isSetJDBCIdentity()
      • getGeneratedKeys

        protected void getGeneratedKeys​(String identityName,
                                        Statement statement)
        Retrieves the value of auto-generated identity from executed prepared statement.
        Parameters:
        identityName - the identity name from the META SQL statement
        statement - statement to retrieve auto-generated keys from
      • doIdentitySelect

        protected void doIdentitySelect​(String identityName)
        Runs the select to obtain the value of auto-generated identity.
        Parameters:
        identityName - the identity name from the META SQL statement
      • callList

        public List<Map<String,​Object>> callList​(SqlRuntimeContext runtimeCtx)
                                                throws SqlProcessorException
        Returns the stored procedure execution results as a List. If the query contains multiple results per row, the results are returned in an instance of Object[]. It's based on the CallableStatement invocation.
        Specified by:
        callList in interface SqlQuery
        Parameters:
        runtimeCtx - the public runtime context
        Returns:
        the result list
        Throws:
        SqlProcessorException - in the case of any problem in ORM or JDBC stack
      • callUnique

        public Map<String,​Object> callUnique​(SqlRuntimeContext runtimeCtx)
                                            throws SqlProcessorException
        Convenience method to return a single instance that matches the stored procedure execution, or null if the stored procedure execution returns no results. It's based on the CallableStatement invocation.
        Specified by:
        callUnique in interface SqlQuery
        Parameters:
        runtimeCtx - the public runtime context
        Returns:
        the single result or null
        Throws:
        SqlProcessorException - in the case of any problem in ORM or JDBC stack
      • callUpdate

        public int callUpdate​(SqlRuntimeContext runtimeCtx)
                       throws SqlProcessorException
        Executes the update, delete, insert statement or other statements in the stored procedure, which don't return the result set. It's based on the CallableStatement invocation.
        Specified by:
        callUpdate in interface SqlQuery
        Parameters:
        runtimeCtx - the public runtime context
        Returns:
        the number of affected rows
        Throws:
        SqlProcessorException - in the case of any problem in ORM or JDBC stack
      • callFunction

        public Map<String,​Object> callFunction()
                                              throws SqlProcessorException
        Executes the statements in the stored function, which return value of any type but the result set. It's based on the CallableStatement invocation.
        Specified by:
        callFunction in interface SqlQuery
        Returns:
        the result of the stored function invocation
        Throws:
        SqlProcessorException - in the case of any problem in ORM or JDBC stack
      • addScalar

        public SqlQuery addScalar​(String columnAlias)
        Declares a scalar query result, which is an SQL query execution output value.
        Specified by:
        addScalar in interface SqlQuery
        Parameters:
        columnAlias - the name of the database column or the alias in the result set
        Returns:
        this instance to enable the methods chaining
      • addScalar

        public SqlQuery addScalar​(String columnAlias,
                                  Object type,
                                  Class<?>... moreTypes)
        Declares a scalar query result, which is an SQL query execution output value.
        Specified by:
        addScalar in interface SqlQuery
        Parameters:
        columnAlias - the name of the database column or the alias in the result set
        type - the (META) type of the parameter
        Returns:
        this instance to enable the methods chaining
      • setParameter

        public SqlQuery setParameter​(String name,
                                     Object val)
                              throws SqlProcessorException
        Binds a value to a named query parameter.
        Specified by:
        setParameter in interface SqlQuery
        Parameters:
        name - the name of the parameter
        val - the not-null parameter value
        Returns:
        this instance to enable the methods chaining
        Throws:
        SqlProcessorException - in the case of any problem in ORM or JDBC stack
      • setParameter

        public SqlQuery setParameter​(String name,
                                     Object val,
                                     Object type,
                                     Class<?>... moreTypes)
                              throws SqlProcessorException
        Binds a value to a named query parameter.
        Specified by:
        setParameter in interface SqlQuery
        Parameters:
        name - the name of the parameter
        val - the possibly-null parameter value
        type - the (META) type of the parameter
        Returns:
        this instance to enable the methods chaining
        Throws:
        SqlProcessorException - in the case of any problem in ORM or JDBC stack
      • setParameterList

        public SqlQuery setParameterList​(String name,
                                         Object[] vals)
                                  throws SqlProcessorException
        Binds multiple values to a named query parameter. This is useful for binding a list of values to the query fragment in (:value_list).
        Specified by:
        setParameterList in interface SqlQuery
        Parameters:
        name - the name of the parameter
        vals - a collection of values
        Returns:
        this instance to enable the methods chaining
        Throws:
        SqlProcessorException - in the case of any problem in ORM or JDBC stack
      • setParameterList

        public SqlQuery setParameterList​(String name,
                                         Object[] vals,
                                         Object type,
                                         Class<?>... moreTypes)
                                  throws SqlProcessorException
        Binds multiple values to a named query parameter. This is useful for binding a list of values to the query fragment in (:value_list).
        Specified by:
        setParameterList in interface SqlQuery
        Parameters:
        name - the name of the parameter
        vals - a collection of values
        type - the (META) type of the parameter
        Returns:
        this instance to enable the methods chaining
        Throws:
        SqlProcessorException - in the case of any problem in ORM or JDBC stack
      • setParameters

        protected void setParameters​(PreparedStatement ps,
                                     SqlFromToPlugin.LimitType limitType,
                                     int start)
                              throws SQLException,
                                     SqlProcessorException
        Sets the value of the designated parameters.
        Parameters:
        ps - an instance of PreparedStatement
        limitType - the limit type to restrict the number of rows in the result set
        start - the index of the first parameter to bind to prepared statement
        Throws:
        SQLException - if a database access error occurs or this method is called on a closed PreparedStatement
        SqlProcessorException
      • setLimits

        protected int setLimits​(PreparedStatement ps,
                                SqlFromToPlugin.LimitType limitType,
                                int ix,
                                boolean afterSql)
                         throws SQLException
        Sets the limit related parameters.
        Parameters:
        ps - an instance of PreparedStatement
        limitType - the limit type to restrict the number of rows in the result set
        ix - a column index
        afterSql - an indicator it's done after the main SQL statement execution
        Returns:
        the updated column index
        Throws:
        SQLException - if a database access error occurs or this method is called on a closed PreparedStatement
      • getParameters

        protected Map<String,​Object> getParameters​(CallableStatement cs,
                                                         boolean isFunction)
                                                  throws SQLException
        Gets the value of the designated OUT parameters.
        Parameters:
        cs - an instance of CallableStatement
        Throws:
        SQLException - if a database access error occurs or this method is called on a closed CallableStatement
      • getResults

        protected List<Map<String,​Object>> getResults​(ResultSet rs)
                                                     throws SQLException
        Gets the value of the designated columns as the objects in the Java programming language.
        Parameters:
        rs - an instance of ResultSet
        Returns:
        the result list
        Throws:
        SQLException - if a database access error occurs or this method is called on a closed ResultSet
      • getOneResult

        protected Map<String,​Object> getOneResult​(ResultSet rs)
                                                 throws SQLException
        Gets the value of the designated columns for one database row as the object in the Java programming language.
        Parameters:
        rs - an instance of ResultSet
        Returns:
        the result object for one row
        Throws:
        SQLException - if a database access error occurs or this method is called on a closed ResultSet
      • executeBatch

        public int[] executeBatch​(String[] statements)
                           throws SqlProcessorException
        Submits a batch of commands to the database for execution and if all commands execute successfully, returns an array of update counts.
        Specified by:
        executeBatch in interface SqlQuery
        Parameters:
        statements - SQL statements to be executed in batch
        Returns:
        an array of update counts containing one element for each command in the batch
        Throws:
        SqlProcessorException - in the case of any problem in ORM or JDBC stack
      • setLogError

        public void setLogError​(boolean logError)
        Sets an indicator the failed SQL command should be logged
        Specified by:
        setLogError in interface SqlQuery
        Parameters:
        logError - an indicator the failed SQL command should be logged