Class SqlCrudEngine


  • public class SqlCrudEngine
    extends SqlEngine
    The primary SQL Processor class for the META SQL CRUD statement execution.

    Instance of this class holds one META SQL statement.

    For example there's a table PERSON with two columns - ID and NAME.
    In the meta statements file statements.qry there's the next definition:

     UPDATE_PERSON(CRUD)=
       update PERSON
       {= set name = :name}
       {= where {& id = :id^long^notnull}}
     ;
     

    In the case of the SQL Processor initialization

     JdbcEngineFactory sqlFactory = new JdbcEngineFactory();
     sqlFactory.setMetaFilesNames("statements.qry"); // the meta statements file
     SqlCrudEngine sqlEngine = sqlFactory.getCrudEngine("UPDATE_PERSON");
     
     // for the case it runs on the top of the JDBC stack
     Connection connection = DriverManager.getConnection("jdbc:hsqldb:mem:sqlproc", "sa", "");
     SqlSession session = new JdbcSimpleSession(connection);
     
    there's created an instance of SqlCrudEngine with the name UPDATE_PERSON.

    Next the query can be executed with one of the updateXXX methods. For example there's a Java bean class Person with attributes id and name. The invocation

     Person person = new Person();
     person.setId(1);
     person.setName("Bozena");
     
     int count = sqlEngine.update(session, person);
     
    produces the next SQL execution
     update PERSON SET name = ? WHERE id = ?
     

    and returns the number of updated rows.

    For more info please see the Tutorials.

    Author:
    Vladimir Hudec
    • Constructor Detail

      • SqlCrudEngine

        public SqlCrudEngine​(String name,
                             String statement,
                             SqlTypeFactory typeFactory,
                             SqlPluginFactory pluginFactory)
                      throws SqlEngineException
        Creates a new instance of the SqlCrudEngine from one META SQL statement string. Constructor will call the internal ANTLR parser for the CRUD statement construction. This constructor is devoted to manual META SQL statements construction. More obvious is to put the META SQL statements definitions into the meta statements file and engage the SqlProcessorLoader for the SqlCrudEngine instances construction.
        Parameters:
        name - the name of this SQL Engine instance
        statement - the META SQL CRUD statement
        typeFactory - the factory for the META types construction
        pluginFactory - the factory for the SQL Processor plugins
        Throws:
        SqlEngineException - in the case the provided statements are not compliant with the ANTLR grammar
      • SqlCrudEngine

        public SqlCrudEngine​(String name,
                             String statement,
                             SqlMonitor monitor,
                             Map<String,​Object> features,
                             SqlTypeFactory typeFactory,
                             SqlPluginFactory pluginFactory)
                      throws SqlEngineException
        Creates a new instance of the SqlCrudEngine from one META SQL statement string. Constructor will call the internal ANTLR parser for the CRUD statement instances construction. Compared to the previous constructor, an external SQL Monitor for the runtime sSqlValidationExceptiontatistics gathering is engaged and the optional features can be involved. This constructor is devoted to manual META SQL statements construction. More obvious is to put the META SQL statements definitions into the meta statements file and engage the SqlProcessorLoader for the SqlCrudEngine instances construction.
        Parameters:
        name - the name of this SQL Engine instance
        statement - the META SQL CRUD statement
        monitor - the SQL Monitor for the runtime statistics gathering
        features - the optional SQL Processor features
        typeFactory - the factory for the META types construction
        pluginFactory - the factory for the SQL Processor plugins
        Throws:
        SqlEngineException - in the case the provided statements are not compliant with the ANTLR grammar
      • SqlCrudEngine

        public SqlCrudEngine​(String name,
                             SqlMetaStatement statement,
                             SqlTypeFactory typeFactory,
                             SqlPluginFactory pluginFactory)
        Creates a new instance of the SqlCrudEngine from one META SQL statement instance. This instance is already pre-compiled using the ANTLR parser. This is the recommended usage for the runtime performance optimization. This constructor is devoted to be used from the custom loader, which is able to read all statements definitions from an external meta statements file and create the named SqlCrudEngine instances.
        Parameters:
        name - the name of this SQL Engine instance
        statement - the pre-compiled META SQL CRUD statement
        typeFactory - the factory for the META types construction
        pluginFactory - the factory for the SQL Processor plugins
      • SqlCrudEngine

        public SqlCrudEngine​(String name,
                             SqlMetaStatement statement,
                             SqlMonitor monitor,
                             Map<String,​Object> features,
                             SqlTypeFactory typeFactory,
                             SqlPluginFactory pluginFactory)
        Creates a new instance of the SqlCrudEngine from one META SQL statement instance. This instance is already pre-compiled using the ANTLR parsers. This is the recommended usage for the runtime performance optimization. This constructor is devoted to be used from the custom loader, which is able to read all statements definitions from an external meta statements file and create the named SqlCrudEngine instances. Compared to the previous constructor, an external SQL Monitor for the runtime statistics gathering is engaged and the optional features can be involved.
        Parameters:
        name - the name of this SQL Engine instance
        statement - the pre-compiled META SQL CRUD statement
        monitor - the SQL Monitor for the runtime statistics gathering
        features - the optional SQL Processor features
        typeFactory - the factory for the META types construction
        pluginFactory - the factory for the SQL Processor plugins
      • SqlCrudEngine

        public SqlCrudEngine​(String name,
                             SqlMetaStatement statement,
                             SqlMappingRule mapping,
                             SqlMonitor monitor,
                             Map<String,​Object> features,
                             SqlTypeFactory typeFactory,
                             SqlPluginFactory pluginFactory)
        Creates a new instance of the SqlCrudEngine from one META SQL statement instance. This instance is already pre-compiled using the ANTLR parsers. This is the recommended usage for the runtime performance optimization. This constructor is devoted to be used from the custom loader, which is able to read all statements definitions from an external meta statements file and create the named SqlCrudEngine instances. Compared to the previous constructor, an external SQL Monitor for the runtime statistics gathering is engaged and the optional features can be involved.
        Parameters:
        name - the name of this SQL Engine instance
        statement - the pre-compiled META SQL CRUD statement
        mapping - the pre-compiled SQL mapping rule
        monitor - the SQL Monitor for the runtime statistics gathering
        features - the optional SQL Processor features
        typeFactory - the factory for the META types construction
        pluginFactory - the factory for the SQL Processor plugins
      • SqlCrudEngine

        public SqlCrudEngine​(String name,
                             SqlMetaStatement statement,
                             SqlMappingRule mapping,
                             SqlMonitor monitor,
                             Map<String,​Object> features,
                             SqlTypeFactory typeFactory,
                             SqlPluginFactory pluginFactory,
                             SqlEngineConfiguration configuration)
        Creates a new instance of the SqlCrudEngine from one META SQL statement instance. This instance is already pre-compiled using the ANTLR parsers. This is the recommended usage for the runtime performance optimization. This constructor is devoted to be used from the custom loader, which is able to read all statements definitions from an external meta statements file and create the named SqlCrudEngine instances. Compared to the previous constructor, an external SQL Monitor for the runtime statistics gathering is engaged and the optional features can be involved.
        Parameters:
        name - the name of this SQL Engine instance
        statement - the pre-compiled META SQL CRUD statement
        mapping - the pre-compiled SQL mapping rule
        monitor - the SQL Monitor for the runtime statistics gathering
        features - the optional SQL Processor features
        typeFactory - the factory for the META types construction
        pluginFactory - the factory for the SQL Processor plugins
        configuration - the overall configuration, which can be persisted
    • Method Detail

      • insert

        public int insert​(SqlSession session,
                          Object dynamicInputValues,
                          Object staticInputValues,
                          int maxTimeout)
                   throws SqlProcessorException,
                          SqlRuntimeException
        Runs the META SQL insert statement to persist a database row. This is the primary and the most complex SQL Processor execution method to persist an instance of input values.
        Parameters:
        session - The SQL Engine session. It can work as a first level cache and the SQL query execution context. The implementation depends on the stack, on top of which the SQL Processor works. For example it can be an Hibernate session.
        dynamicInputValues - The object used for the SQL statement dynamic input values. The class of this object is also named as the input class or the dynamic parameters class. The exact class type isn't important, all the parameters settled into the SQL prepared statement are picked up using the reflection API.
        staticInputValues - The object used for the SQL statement static input values. The class of this object is also named as the input class or the static parameters class. The exact class type isn't important, all the parameters injected into the SQL query command are picked up using the reflection API. Compared to dynamicInputValues input parameters, parameters in this class should't be produced by an end user to prevent SQL injection threat!
        maxTimeout - The max SQL execution time. This parameter can help to protect production system against ineffective SQL query commands. The value is in milliseconds.
        Returns:
        The number of persisted database rows.
        Throws:
        SqlProcessorException - in the case of any problem with ORM or JDBC stack
        SqlRuntimeException - in the case of any problem with the input/output values handling
      • insert

        public int insert​(SqlSession session,
                          Object dynamicInputValues,
                          SqlControl sqlControl)
                   throws SqlProcessorException,
                          SqlRuntimeException,
                          SqlValidationException
        Runs the META SQL insert statement to persist a database row. This is the primary and the most complex SQL Processor execution method to persist an instance of input values.
        Parameters:
        session - The SQL Engine session. It can work as a first level cache and the SQL query execution context. The implementation depends on the stack, on top of which the SQL Processor works. For example it can be an Hibernate session.
        dynamicInputValues - The object used for the SQL statement dynamic input values. The class of this object is also named as the input class or the dynamic parameters class. The exact class type isn't important, all the parameters settled into the SQL prepared statement are picked up using the reflection API.
        sqlControl - The compound parameters controlling the META SQL execution
        Returns:
        The number of persisted database rows.
        Throws:
        SqlProcessorException - in the case of any problem with ORM or JDBC stack
        SqlRuntimeException - in the case of any problem with the input/output values handling
        SqlValidationException - in the case the validation of the input values isn't successfull
      • insert

        private Integer insert​(SqlQuery query,
                               SqlProcessResult processResult)
        Internal insert implementation
        Parameters:
        query - query
        processResult - processResult
        Returns:
        the result
      • get

        public <E> E get​(SqlSession session,
                         Class<E> resultClass,
                         Object dynamicInputValues,
                         Object staticInputValues,
                         int maxTimeout,
                         Map<String,​Class<?>> moreResultClasses)
                  throws SqlProcessorException,
                         SqlRuntimeException
        Runs the META SQL query to obtain a unique database row. This is the primary and the most complex SQL Processor execution method to obtain an unique instance of the result class. Criteria to pickup the correct database row are taken from input values.
        Parameters:
        session - The SQL Engine session. It can work as a first level cache and the SQL query execution context. The implementation depends on the stack, on top of which the SQL Processor works. For example it can be an Hibernate session.
        resultClass - The class used for the return values, the SQL query execution output. This class is also named as the output class or the transport class, In fact it's a standard POJO class, which must include all the attributes described in the mapping rule statement. This class itself and all its subclasses must have public constructors without any parameters. All the attributes used in the mapping rule statement must be accessible using public getters and setters. The instances of this class are created on the fly in the process of query execution using the reflection API.
        dynamicInputValues - The object used for the SQL statement dynamic input values. The class of this object is also named as the input class or the dynamic parameters class. The exact class type isn't important, all the parameters settled into the SQL prepared statement are picked up using the reflection API.
        staticInputValues - The object used for the SQL statement static input values. The class of this object is also named as the input class or the static parameters class. The exact class type isn't important, all the parameters injected into the SQL query command are picked up using the reflection API. Compared to dynamicInputValues input parameters, parameters in this class should't be produced by an end user to prevent SQL injection threat!
        maxTimeout - The max SQL execution time. This parameter can help to protect production system against ineffective SQL query commands. The value is in milliseconds.
        moreResultClasses - More result classes used for the return values, like the collections classes or the collections items. They are used mainly for the one-to-one, one-to-many and many-to-many associations.
        Returns:
        The instance of the resultClass.
        Throws:
        SqlProcessorException - in the case of any problem with ORM or JDBC stack
        SqlRuntimeException - in the case of any problem with the input/output values handling
      • get

        public <E> E get​(SqlSession session,
                         Class<E> resultClass,
                         Object dynamicInputValues,
                         SqlControl sqlControl)
                  throws SqlProcessorException,
                         SqlRuntimeException
        Runs the META SQL query to obtain a unique database row. This is the primary and the most complex SQL Processor execution method to obtain an unique instance of the result class. Criteria to pickup the correct database row are taken from input values.
        Parameters:
        session - The SQL Engine session. It can work as a first level cache and the SQL query execution context. The implementation depends on the stack, on top of which the SQL Processor works. For example it can be an Hibernate session.
        resultClass - The class used for the return values, the SQL query execution output. This class is also named as the output class or the transport class, In fact it's a standard POJO class, which must include all the attributes described in the mapping rule statement. This class itself and all its subclasses must have public constructors without any parameters. All the attributes used in the mapping rule statement must be accessible using public getters and setters. The instances of this class are created on the fly in the process of query execution using the reflection API.
        dynamicInputValues - The object used for the SQL statement dynamic input values. The class of this object is also named as the input class or the dynamic parameters class. The exact class type isn't important, all the parameters settled into the SQL prepared statement are picked up using the reflection API.
        sqlControl - The compound parameters controlling the META SQL execution
        Returns:
        The instance of the resultClass.
        Throws:
        SqlProcessorException - in the case of any problem with ORM or JDBC stack
        SqlRuntimeException - in the case of any problem with the input/output values handling
      • get

        private <E> E get​(SqlQuery query,
                          SqlMappingResult mappingResult,
                          Class<E> resultClass,
                          SqlControl sqlControl)
        Internal get implementation
        Parameters:
        query - query
        mappingResult - mappingResult
        resultClass - resultClass
        sqlControl - sqlCOntrol
        Returns:
        the result
      • update

        public int update​(SqlSession session,
                          Object dynamicInputValues,
                          Object staticInputValues,
                          int maxTimeout)
                   throws SqlProcessorException,
                          SqlRuntimeException
        Runs the META SQL update statement to persist a database row. This is the primary and the most complex SQL Processor execution method to persist an instance of input values. Changed values are taken from the input values. At the same time criteria to pickup the correct database rows(s) are taken from the input values too.
        Parameters:
        session - The SQL Engine session. It can work as a first level cache and the SQL query execution context. The implementation depends on the stack, on top of which the SQL Processor works. For example it can be an Hibernate session.
        dynamicInputValues - The object used for the SQL statement dynamic input values. The class of this object is also named as the input class or the dynamic parameters class. The exact class type isn't important, all the parameters settled into the SQL prepared statement are picked up using the reflection API.
        staticInputValues - The object used for the SQL statement static input values. The class of this object is also named as the input class or the static parameters class. The exact class type isn't important, all the parameters injected into the SQL query command are picked up using the reflection API. Compared to dynamicInputValues input parameters, parameters in this class should't be produced by an end user to prevent SQL injection threat!
        maxTimeout - The max SQL execution time. This parameter can help to protect production system against ineffective SQL query commands. The value is in milliseconds.
        Returns:
        The number of updated database rows.
        Throws:
        SqlProcessorException - in the case of any problem with ORM or JDBC stack
        SqlRuntimeException - in the case of any problem with the input/output values handling
      • update

        public int update​(SqlSession session,
                          Object dynamicInputValues,
                          SqlControl sqlControl)
                   throws SqlProcessorException,
                          SqlRuntimeException,
                          SqlValidationException
        Runs the META SQL update statement to persist a database row. This is the primary and the most complex SQL Processor execution method to persist an instance of input values. Changed values are taken from the input values. At the same time criteria to pickup the correct database rows(s) are taken from the input values too.
        Parameters:
        session - The SQL Engine session. It can work as a first level cache and the SQL query execution context. The implementation depends on the stack, on top of which the SQL Processor works. For example it can be an Hibernate session.
        dynamicInputValues - The object used for the SQL statement dynamic input values. The class of this object is also named as the input class or the dynamic parameters class. The exact class type isn't important, all the parameters settled into the SQL prepared statement are picked up using the reflection API.
        sqlControl - The compound parameters controlling the META SQL execution
        Returns:
        The number of updated database rows.
        Throws:
        SqlProcessorException - in the case of any problem with ORM or JDBC stack
        SqlRuntimeException - in the case of any problem with the input/output values handling
        SqlValidationException - in the case the validation of the input values isn't successfull
      • update

        private Integer update​(SqlQuery query,
                               SqlProcessResult processResult)
        Internal update implementation
        Parameters:
        query - query
        Returns:
        the result
      • delete

        public int delete​(SqlSession session,
                          Object dynamicInputValues,
                          Object staticInputValues,
                          int maxTimeout)
                   throws SqlProcessorException,
                          SqlRuntimeException
        Runs the META SQL delete statement to delete a database row. This is the primary and the most complex SQL Processor execution method to delete the database row(s) based on criteria from the input values.
        Parameters:
        session - The SQL Engine session. It can work as a first level cache and the SQL query execution context. The implementation depends on the stack, on top of which the SQL Processor works. For example it can be an Hibernate session.
        dynamicInputValues - The object used for the SQL statement dynamic input values. The class of this object is also named as the input class or the dynamic parameters class. The exact class type isn't important, all the parameters settled into the SQL prepared statement are picked up using the reflection API.
        staticInputValues - The object used for the SQL statement static input values. The class of this object is also named as the input class or the static parameters class. The exact class type isn't important, all the parameters injected into the SQL query command are picked up using the reflection API. Compared to dynamicInputValues input parameters, parameters in this class should't be produced by an end user to prevent SQL injection threat!
        maxTimeout - The max SQL execution time. This parameter can help to protect production system against ineffective SQL query commands. The value is in milliseconds.
        Returns:
        The number of updated database rows.
        Throws:
        SqlProcessorException - in the case of any problem with ORM or JDBC stack
        SqlRuntimeException - in the case of any problem with the input/output values handling
      • delete

        public int delete​(SqlSession session,
                          Object dynamicInputValues,
                          SqlControl sqlControl)
                   throws SqlProcessorException,
                          SqlRuntimeException
        Runs the META SQL delete statement to delete a database row. This is the primary and the most complex SQL Processor execution method to delete the database row(s) based on criteria from the input values.
        Parameters:
        session - The SQL Engine session. It can work as a first level cache and the SQL query execution context. The implementation depends on the stack, on top of which the SQL Processor works. For example it can be an Hibernate session.
        dynamicInputValues - The object used for the SQL statement dynamic input values. The class of this object is also named as the input class or the dynamic parameters class. The exact class type isn't important, all the parameters settled into the SQL prepared statement are picked up using the reflection API.
        sqlControl - The compound parameters controlling the META SQL execution
        Returns:
        The number of updated database rows.
        Throws:
        SqlProcessorException - in the case of any problem with ORM or JDBC stack
        SqlRuntimeException - in the case of any problem with the input/output values handling
      • delete

        private Integer delete​(SqlQuery query,
                               SqlProcessResult processResult)
        Internal delete implementation
        Parameters:
        query - query
        Returns:
        the result
      • getSql

        public String getSql​(Object dynamicInputValues,
                             Object staticInputValues,
                             SqlMetaStatement.Type statementType)
                      throws SqlProcessorException,
                             SqlRuntimeException
        Because SQL Processor is Data Driven Query engine, every input parameters can produce in fact different SQL statement command. This method can help to identify the exact SQL statement command, which is produced in the background of the SQL Processor execution. The statement is derived from the META SQL statement.
        Parameters:
        dynamicInputValues - The object used for the SQL statement dynamic input values. The class of this object is also named as the input class or the dynamic parameters class. The exact class type isn't important, all the parameters settled into the SQL prepared statement are picked up using the reflection API.
        staticInputValues - The object used for the SQL statement static input values. The class of this object is also named as the input class or the static parameters class. The exact class type isn't important, all the parameters injected into the SQL query command are picked up using the reflection API. Compared to dynamicInputValues input parameters, parameters in this class should't be produced by an end user to prevent SQL injection threat!
        statementType - The type of the statement under consideration. It can be CREATE, RETRIEVE, UPDATE or DELETE.
        Returns:
        The SQL statement command derived from the META SQL statement based on the input parameters.
        Throws:
        SqlProcessorException - in the case of any problem with ORM or JDBC stack
        SqlRuntimeException - in the case of any problem with the input/output values handling
      • getSql

        public String getSql​(Object dynamicInputValues,
                             SqlControl sqlControl,
                             SqlMetaStatement.Type statementType)
                      throws SqlProcessorException,
                             SqlRuntimeException
        Because SQL Processor is Data Driven Query engine, every input parameters can produce in fact different SQL statement command. This method can help to identify the exact SQL statement command, which is produced in the background of the SQL Processor execution. The statement is derived from the META SQL statement.
        Parameters:
        dynamicInputValues - The object used for the SQL statement dynamic input values. The class of this object is also named as the input class or the dynamic parameters class. The exact class type isn't important, all the parameters settled into the SQL prepared statement are picked up using the reflection API.
        sqlControl - The compound parameters controlling the META SQL execution
        statementType - The type of the statement under consideration. It can be CREATE, RETRIEVE, UPDATE or DELETE.
        Returns:
        The SQL statement command derived from the META SQL statement based on the input parameters.
        Throws:
        SqlProcessorException - in the case of any problem with ORM or JDBC stack
        SqlRuntimeException - in the case of any problem with the input/output values handling
      • getName

        public String getName()
        Returns the name of this META SQL, which uniquely identifies the instance. In the case the META SQL statement is located in the meta statements file, this name is the unique part of the keys in this file. For example for the name ALL in the meta statements file there's the META SQL statement with the name ALL(CRUD).
        Returns:
        The name of the SQL engine instance.
      • getMonitor

        public SqlMonitor getMonitor()
        Returns the SQL Monitor instance for the runtime statistics gathering. By default no runtime statistics gathering is active. So this SQL Monitor is implied in SQL engine constructor in the case the statistics gathering should be engaged.
        Returns:
        The SQL Monitor instance, which is active for this SQL engine instance.