org.sqlproc.engine
Class SqlCrudEngine

java.lang.Object
  extended by org.sqlproc.engine.SqlEngine
      extended by org.sqlproc.engine.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 queries.properties there's the next definition:

 CRUD_UPDATE_PERSON= \
  update PERSON \
  {= set name = :name} \
  {= where {& id = :id^long^notnull}}
 

In the case of SQL Processor initialization

 SqlPropertiesLoader loader = new SqlPropertiesLoader("queries.properties", this.getClass());
 SqlEngineLoader sqlLoader = new SqlEngineLoader(loader.getProperties());
 SqlCrudEngine sqlEngine = sqlLoader.getCrudEngine("UPDATE_PERSON");
 
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 Reference Guide or tutorials.

Author:
Vladimir Hudec

Field Summary
 
Fields inherited from class org.sqlproc.engine.SqlEngine
features, logger, mapping, monitor, name, statement
 
Constructor Summary
SqlCrudEngine(java.lang.String name, SqlMetaStatement statement)
          Creates a new instance of SqlCrudEngine from one META SQL statement instance.
SqlCrudEngine(java.lang.String name, SqlMetaStatement statement, SqlMonitor monitor, java.util.Map<java.lang.String,java.lang.Object> features)
          Creates a new instance of SqlCrudEngine from one META SQL statement instance.
SqlCrudEngine(java.lang.String name, java.lang.String statement)
          Creates a new instance of SqlCrudEngine from one META SQL statement string.
SqlCrudEngine(java.lang.String name, java.lang.String statement, SqlMonitor monitor, java.util.Map<java.lang.String,java.lang.Object> features)
          Creates a new instance of SqlCrudEngine from one META SQL statement string.
 
Method Summary
<E> int
delete(org.hibernate.Session session, java.lang.Object dynamicInputValues)
          Runs a META SQL delete statement to delete a database row.
<E> int
delete(org.hibernate.Session session, java.lang.Object dynamicInputValues, java.lang.Object staticInputValues)
          Runs a META SQL delete statement to delete a database row.
<E> int
delete(org.hibernate.Session session, java.lang.Object dynamicInputValues, java.lang.Object staticInputValues, int maxTimeout)
          Runs a META SQL delete statement to delete a database row.
<E> E
get(org.hibernate.Session session, java.lang.Class<E> resultClass, java.lang.Object dynamicInputValues)
          Runs a META SQL query to obtain a unique database row.
<E> E
get(org.hibernate.Session session, java.lang.Class<E> resultClass, java.lang.Object dynamicInputValues, java.lang.Object staticInputValues)
          Runs a META SQL query to obtain a unique database row.
<E> E
get(org.hibernate.Session session, java.lang.Class<E> resultClass, java.lang.Object dynamicInputValues, java.lang.Object staticInputValues, int maxTimeout)
          Runs a META SQL query to obtain a unique database row.
 java.lang.String getDeleteSql(java.lang.Object dynamicInputValues, java.lang.Object staticInputValues)
          Returns the delete statement derived from the META SQL statement.
 java.lang.String getGetSql(java.lang.Object dynamicInputValues, java.lang.Object staticInputValues)
          Returns the query select statement derived from the META SQL statement.
 java.lang.String getInsertSql(java.lang.Object dynamicInputValues, java.lang.Object staticInputValues)
          Returns the insert statement derived from the META SQL statement.
 SqlMonitor getMonitor()
          Returns the SQL Monitor instance for the runtime statistics gathering.
 java.lang.String getName()
          Returns the name of this META SQL, which uniquely identifies the instance.
 java.lang.String getSql(java.lang.Object dynamicInputValues, java.lang.Object staticInputValues, SqlMetaStatement.Type statementType)
          Because SQL Processor is Data Driven Query engine, every input parameters can produce in fact different SQL statement command.
 java.lang.String getUpdateSql(java.lang.Object dynamicInputValues, java.lang.Object staticInputValues)
          Returns the update statement derived from the META SQL statement.
<E> int
insert(org.hibernate.Session session, java.lang.Object dynamicInputValues)
          Runs a META SQL insert statement to persist a database row.
<E> int
insert(org.hibernate.Session session, java.lang.Object dynamicInputValues, java.lang.Object staticInputValues)
          Runs a META SQL insert statement to persist a database row.
<E> int
insert(org.hibernate.Session session, java.lang.Object dynamicInputValues, java.lang.Object staticInputValues, int maxTimeout)
          Runs a META SQL insert statement to persist a database row.
<E> int
update(org.hibernate.Session session, java.lang.Object dynamicInputValues)
          Runs a META SQL update statement to persist a database row.
<E> int
update(org.hibernate.Session session, java.lang.Object dynamicInputValues, java.lang.Object staticInputValues)
          Runs a META SQL update statement to persist a database row.
<E> int
update(org.hibernate.Session session, java.lang.Object dynamicInputValues, java.lang.Object staticInputValues, int maxTimeout)
          Runs a META SQL update statement to persist a database row.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SqlCrudEngine

public SqlCrudEngine(java.lang.String name,
                     java.lang.String statement)
              throws SqlEngineException
Creates a new instance of 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 these definitions into queries.properties file and engage SqlEngineLoader for the SqlCrudEngine instances construction.

Parameters:
name - the name if this SQL Engine instance
statement - the META SQL CRUD statement, extension of ANSI SQL
Throws:
SqlEngineException - mainly in the case the provided statements are not compliant with the ANTLR grammar

SqlCrudEngine

public SqlCrudEngine(java.lang.String name,
                     java.lang.String statement,
                     SqlMonitor monitor,
                     java.util.Map<java.lang.String,java.lang.Object> features)
              throws SqlEngineException
Creates a new instance of 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, external SQL Monitor for runtime statistics gathering is engaged. This constructor is devoted to manual META SQL statements construction. More obvious is to put these statements into queries.properties file and engage SqlEngineLoader for SqlEngine instances construction.

Parameters:
name - the name if this SQL Engine instance
statement - the META SQL CRUD statement, extension of ANSI SQL
monitor - the SQL Monitor for the runtime statistics gathering
features - the optional SQL Processor features
Throws:
SqlEngineException - mainly in the case the provided statements are not compliant with the ANTLR grammar

SqlCrudEngine

public SqlCrudEngine(java.lang.String name,
                     SqlMetaStatement statement)
Creates a new instance of SqlCrudEngine from one META SQL statement instance. This instance is already precompiled using the ANTLR parser. This is the recommended usage for the runtime performance optimization. This constructor is devoted to be used from the SqlEngineLoader, which is able to read all definitions from an external queries.properties and create the named SqlCrudEngine instances.

Parameters:
name - the name if this SQL Engine instance
statement - the precompiled META SQL CRUD statement, extension of ANSI SQL

SqlCrudEngine

public SqlCrudEngine(java.lang.String name,
                     SqlMetaStatement statement,
                     SqlMonitor monitor,
                     java.util.Map<java.lang.String,java.lang.Object> features)
Creates a new instance of SqlCrudEngine from one META SQL statement instance. This instance is already precompiled using the ANTLR parsers. This is the recommended usage for the runtime performance optimization. This constructor is devoted to be used from the SqlEngineLoader, which is able to read all definitions from an external queries.properties and create the named SqlCrudEngine instances. Compared to the previous constructor, external SQL Monitor for the runtime statistics gathering is engaged.

Parameters:
name - the name if this SQL Engine instance
statement - the precompiled META SQL statement, extension of ANSI SQL
monitor - the SQL Monitor for the runtime statistics gathering
features - the optional SQL Processor features
Method Detail

insert

public <E> int insert(org.hibernate.Session session,
                      java.lang.Object dynamicInputValues)
           throws org.hibernate.HibernateException
Runs a META SQL insert statement to persist a database row. This is one of the overriden methods. For the parameters description please see the most complex execution method insert(Session, Object, Object, int) .

Throws:
org.hibernate.HibernateException

insert

public <E> int insert(org.hibernate.Session session,
                      java.lang.Object dynamicInputValues,
                      java.lang.Object staticInputValues)
           throws org.hibernate.HibernateException
Runs a META SQL insert statement to persist a database row. This is one of the overriden methods. For the parameters description please see the most complex execution method insert(Session, Object, Object, int) .

Throws:
org.hibernate.HibernateException

insert

public <E> int insert(org.hibernate.Session session,
                      java.lang.Object dynamicInputValues,
                      java.lang.Object staticInputValues,
                      int maxTimeout)
           throws org.hibernate.HibernateException
Runs a 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. Inserted values are taken from input values.

Parameters:
session - Hibernate session, first level cache and the SQL query execution context
dynamicInputValues - The object used for the SQL statement dynamic parameters. 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 substituted into the SQL prepared statement are picked up using the reflection API.
staticInputValues - The object used for the SQL statement static parameters. 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 the 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:
org.hibernate.HibernateException

get

public <E> E get(org.hibernate.Session session,
                 java.lang.Class<E> resultClass,
                 java.lang.Object dynamicInputValues)
      throws org.hibernate.HibernateException
Runs a META SQL query to obtain a unique database row. This is one of the overriden methods. For the parameters description please see the most complex execution method get(Session, Class, Object, Object, int) .

Throws:
org.hibernate.HibernateException

get

public <E> E get(org.hibernate.Session session,
                 java.lang.Class<E> resultClass,
                 java.lang.Object dynamicInputValues,
                 java.lang.Object staticInputValues)
      throws org.hibernate.HibernateException
Runs a META SQL query to obtain a unique database row. This is one of the overriden methods. For the parameters description please see the most complex execution method get(Session, Class, Object, Object, int) .

Throws:
org.hibernate.HibernateException

get

public <E> E get(org.hibernate.Session session,
                 java.lang.Class<E> resultClass,
                 java.lang.Object dynamicInputValues,
                 java.lang.Object staticInputValues,
                 int maxTimeout)
      throws org.hibernate.HibernateException
Runs a META SQL query to obtain a unique database row. This is the primary and the most complex SQL Processor execution method to obtain a unique instance of result class. Criteria to pickup the correct database row are taken from input values.

Parameters:
session - Hibernate session, first level cache and the SQL query execution context
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 instance of this class are created on the fly in the query execution using the reflection API.
dynamicInputValues - The object used for the SQL statement dynamic parameters. 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 substituted into the SQL prepared statement are picked up using the reflection API.
staticInputValues - The object used for the SQL statement static parameters. 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 the 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 instance of the resultClass.
Throws:
org.hibernate.HibernateException

update

public <E> int update(org.hibernate.Session session,
                      java.lang.Object dynamicInputValues)
           throws org.hibernate.HibernateException
Runs a META SQL update statement to persist a database row. This is one of the overriden methods. For the parameters description please see the most complex execution method update(Session, Object, Object, int) .

Throws:
org.hibernate.HibernateException

update

public <E> int update(org.hibernate.Session session,
                      java.lang.Object dynamicInputValues,
                      java.lang.Object staticInputValues)
           throws org.hibernate.HibernateException
Runs a META SQL update statement to persist a database row. This is one of the overriden methods. For the parameters description please see the most complex execution method update(Session, Object, Object, int) .

Throws:
org.hibernate.HibernateException

update

public <E> int update(org.hibernate.Session session,
                      java.lang.Object dynamicInputValues,
                      java.lang.Object staticInputValues,
                      int maxTimeout)
           throws org.hibernate.HibernateException
Runs a 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 input values. At the same time criteria to pickup the correct database rows(s) are taken from input values.

Parameters:
session - Hibernate session, first level cache and the SQL query execution context
dynamicInputValues - The object used for the SQL statement dynamic parameters. 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 substituted into the SQL prepared statement are picked up using the reflection API.
staticInputValues - The object used for the SQL statement static parameters. 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 the 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:
org.hibernate.HibernateException

delete

public <E> int delete(org.hibernate.Session session,
                      java.lang.Object dynamicInputValues)
           throws org.hibernate.HibernateException
Runs a META SQL delete statement to delete a database row. This is one of the overriden methods. For the parameters description please see the most complex execution method delete(Session, Object, Object, int) .

Throws:
org.hibernate.HibernateException

delete

public <E> int delete(org.hibernate.Session session,
                      java.lang.Object dynamicInputValues,
                      java.lang.Object staticInputValues)
           throws org.hibernate.HibernateException
Runs a META SQL delete statement to delete a database row. This is one of the overriden methods. For the parameters description please see the most complex execution method delete(Session, Object, Object, int) .

Throws:
org.hibernate.HibernateException

delete

public <E> int delete(org.hibernate.Session session,
                      java.lang.Object dynamicInputValues,
                      java.lang.Object staticInputValues,
                      int maxTimeout)
           throws org.hibernate.HibernateException
Runs a 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 input values.

Parameters:
session - Hibernate session, first level cache and the SQL query execution context
dynamicInputValues - The object used for the SQL statement dynamic parameters. 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 substituted into the SQL prepared statement are picked up using the reflection API.
staticInputValues - The object used for the SQL statement static parameters. 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 the 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:
org.hibernate.HibernateException

getInsertSql

public java.lang.String getInsertSql(java.lang.Object dynamicInputValues,
                                     java.lang.Object staticInputValues)
                              throws org.hibernate.HibernateException
Returns the insert statement derived from the META SQL statement. For the parameters description please see the most complex execution method getSql(Object, Object, org.sqlproc.engine.impl.SqlMetaStatement.Type) .

Throws:
org.hibernate.HibernateException

getGetSql

public java.lang.String getGetSql(java.lang.Object dynamicInputValues,
                                  java.lang.Object staticInputValues)
                           throws org.hibernate.HibernateException
Returns the query select statement derived from the META SQL statement. For the parameters description please see the most complex execution method getSql(Object, Object, org.sqlproc.engine.impl.SqlMetaStatement.Type) .

Throws:
org.hibernate.HibernateException

getUpdateSql

public java.lang.String getUpdateSql(java.lang.Object dynamicInputValues,
                                     java.lang.Object staticInputValues)
                              throws org.hibernate.HibernateException
Returns the update statement derived from the META SQL statement. For the parameters description please see the most complex execution method getSql(Object, Object, org.sqlproc.engine.impl.SqlMetaStatement.Type) .

Throws:
org.hibernate.HibernateException

getDeleteSql

public java.lang.String getDeleteSql(java.lang.Object dynamicInputValues,
                                     java.lang.Object staticInputValues)
                              throws org.hibernate.HibernateException
Returns the delete statement derived from the META SQL statement. For the parameters description please see the most complex execution method getSql(Object, Object, org.sqlproc.engine.impl.SqlMetaStatement.Type) .

Throws:
org.hibernate.HibernateException

getSql

public java.lang.String getSql(java.lang.Object dynamicInputValues,
                               java.lang.Object staticInputValues,
                               SqlMetaStatement.Type statementType)
                        throws org.hibernate.HibernateException
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 parameters. 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 substituted into the SQL prepared statement are picked up using the reflection API.
staticInputValues - The object used for the SQL statement static parameters. 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 the 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 in the input parameters.
Throws:
org.hibernate.HibernateException

getName

public java.lang.String getName()
Returns the name of this META SQL, which uniquely identifies the instance. In the case the META SQL statement and Mapping rule are located in queries.properties, this name is the unique part of the keys in this file. For example for the name ALL in queries.properties there's META SQL statement with the name QRY_ALL and Mapping rule with the name OUT_ALL.

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.


Copyright © 2011. All Rights Reserved.