public class SQLQueryFactory
extends com.streamscape.sef.dispatcher.AbstractFabricContextFactory
implements com.streamscape.omf.SerializerSupport
Title: SQL Object Mediation: SQL Query Factory
Description: A factory for working with the SQL Query Object
and its associate SQL Query Batch scripts.
This factory allows users to work with SQL Objects. The factory allows users to construct sql query objects from parametrized SQL, store the object as a repository artifact or perform other operations. The SQL Object is considered a standard entity (not a system type). It is commonly stored as an XML artifact in the entity repository. The SQL query parameters are of the following format:
${ [ARRAY] DATA_TYPE [(SCALE, PRECISION)] @VARIABLE_NAME [DEFAULT = VALUE] [(IN|OUT|INOUT)] }where elements of the syntax mean:
SQLTypeToken
.NULL
@ReturnCode
is a special holder parameter that is part of every result Row Set
that
The examples below illustrate usage of the script:
SELECT * FROM [StreamScapeTest].[dbo].[TestTable] WHERE [itemId] = ${FLOAT @VALUE DEFAULT = 0}; INSERT INTO [StreamScapeTest].[dbo].[TestTable] ([char]) VALUES ( ${CHAR(10) @VALUE DEFAULT = NULL} ); SELECT * FROM [StreamScapeTest].[dbo].[TestTable] WHERE [name] = ${VARCHAR(10) @VALUE DEFAULT = 'Jim'}; UPDATE [StreamScapeTest].[dbo].[TestTable] SET VALUE = ${VARCHAR(10) @VALUE DEFAULT = 'Unassigned'} WHERE ID = ${INTEGER @ID}; INSERT INTO [StreamScapeTest].[dbo].[TestTable] VALUES ( ${ ARRAY MY_ARRAY_TYPE @ARRAY_VALUE DEFAULT = ARRAY[1,2,3] } );Oracle specific:
UPDATE [SCHEMA].[TestTable] SET COLUMN1 = ${VARCHAR(10) @COLUMN1 DEFAULT = 'Unassigned'}, COLUMN2 = ${ARRAY MY_ARRAY_TYPE @COLUMN2 DEFAULT = NULL}, WHERE ID = ${INTEGER @ID}; INSERT INTO [SCHEMA].[TestTable] VALUES ( ${ ARRAY MY_ARRAY_TYPE @ARRAY_VALUE DEFAULT = ARRAY[1,2,3] } );The SQL parameters are set by parsing input SQL scripts. Each SQL statement, terminated by
;
is resolved
into an instance of an SQLQuery
object. Multiple statements constitute a script of sequentially
executable commands and are resolved to an instance of SQLQueryBatch
object.
Query objects are persistent like all other Structured Data Objects
and may be serialized to disk,
as configuration objects or encapsulated in Event Datagrams
for transport. The factory allows
developers to decompose SQL into the syntax tree held in an SQLQueryBatch
object or to render a full
SQL query that may be used with any JDBC
compliant driver. Query objects may be combined with
JDBC Factory
and JDBC Connection
objects to execute ad-hoc database queries or work
with the Open Service Framework
.
The factory allows query objects to be rendered as resolved SQL queries or as SQL queries used by prepared
or callable statements. However, note that the factory functions independent of any JDBC Drivers
and does not prepare or pre-compile any of the queries. SQL script syntax may be extended by using a specific
implementation of ISQLQuerySyntax
. It also supports default SELECT, INSERT, UPDATE and CALL script
generation and allows users to abstract SQL query execution thru the usage of SQLQuery
and RowSet
.
These objects may also be cross-mapped, allowing users to transform results into queries and vice versa.
Copyright: Copyright (c) 2009
Company: StreamScape Technologies
SQLQuery
,
SQLQueryBatch
,
SQLQueryMapper
,
Serialized FormDEFAULT_SERIAL_VERSION_ID
Constructor and Description |
---|
SQLQueryFactory() |
Modifier and Type | Method and Description |
---|---|
static SQLQueryBatch |
createBatch(java.lang.String sqlScript)
Constructs SQL query batch object by parsing a given script which holds a set of
parameterized SQL queries as a batch.
|
static CallableStatement |
createCallableStatement(Connection connection,
SQLQuery object)
Creates callable statement based on given connection and sql query object.
|
static SQLColumnSet |
createColumnSet()
Creates empty
SQLColumnSet object. |
static SQLColumnSet |
createColumnSet(ResultSet resultSet)
Creates
SQLColumnSet from provided ResultSet . |
static SQLColumnSet |
createColumnSet(RowSet rowSet)
Creates
SQLColumnSet from provided RowSet . |
static SQLQuery |
createForCall(Connection connection,
java.lang.String procedure)
Generates SQL script with parameters which then can be used to create query object and
call given stored procedure.
|
static SQLQuery |
createForDelete(Connection connection,
java.lang.String table)
Generates an SQLQuery DELETE script for specified
table table. |
static SQLQuery |
createForDelete(Connection connection,
java.lang.String table,
java.lang.String[] whereColumns)
Generates an SQLQuery DELETE script for specified
table table. |
static SQLQuery |
createForDynamicSQL(Connection connection,
java.lang.String query)
Returns SQLQuery object based on provided
query SQL prepared query. |
static SQLQuery |
createForInsert(Connection connection,
java.lang.String table)
Generates SQL script with parameters which then can be used to create an SQL query object and
perform inserts into a given table.
|
static SQLQuery |
createForSelect(Connection connection,
java.lang.String table)
Generates an SQLQuery SELECT script that selects all columns from given table.
|
static SQLQuery |
createForSelect(Connection connection,
java.lang.String table,
java.lang.String[] selectColumns)
Generates an SQLQuery SELECT script that selects
selectColumns columns from given table. |
static SQLQuery |
createForSelect(Connection connection,
java.lang.String table,
java.lang.String[] selectColumns,
java.lang.String[] whereColumns)
Generates an SQLQuery SELECT script that selects
selectColumns columns from given table. |
static SQLQuery |
createForUpdate(Connection connection,
java.lang.String table)
Generates an SQLQuery UPDTAE script with parameters which then can be used to create an SQL query object and
perform an update on given table.
|
static SQLQuery |
createForUpdate(Connection connection,
java.lang.String table,
java.lang.String[] updateColumns)
Generates an SQLQuery UPDTAE script for specified
updateColumns columns. |
static SQLQuery |
createForUpdate(Connection connection,
java.lang.String table,
java.lang.String[] updateColumns,
java.lang.String[] whereColumns)
Generates an SQLQuery UPDATE script for specified
updateColumns columns. |
static SQLQuery |
createForUpdate(java.lang.String table,
SQLColumnSet columnSet)
Generates an SQLQuery UPDTAE script for specified
columnSet columns. |
static PreparedStatement |
createPreparedStatement(Connection connection,
SQLQuery object)
Creates prepared statement based on given connection and sql query object.
|
static SQLQuery |
createQuery(java.lang.String sqlScript)
Constructs SQL query object by parsing given script which holds a parameterized SQL query
|
static int |
execute(SQLQueryBatch batch,
int queryTimeout,
Connection connection)
Execute the batch query contained in the SQL script object.
|
static java.util.List |
execute(SQLQuery query,
int queryTimeout,
Connection connection)
Execute the query contained in the SQL script object.
|
static SQLQueryFactory |
getInstance() |
static SemanticType |
getSQLParameterType()
Returns an SQL Parameter semantic type.
|
static SemanticType |
getSQLQueryBatchType()
Returns an SQL Query Batch semantic type.
|
static SemanticType |
getSQLQueryType()
Returns an SQL Query semantic type.
|
static java.util.List |
getSupportedTypes()
Returns a lit of all supported semantic types managed by this factory.
|
void |
initSerializer(SerializerFactory factory,
java.lang.String serializerName)
Allows users to initialize a serializer that will be used by this factory.
|
static SQLQuery |
loadQueryObject(java.lang.String workingDir,
java.lang.String queryName)
Loads an SQL Query object of a given name from a directory specified by the user.
|
static java.lang.String |
renderBatch(SQLQueryBatch batch)
Renders the SQL Batch object as a dynamic SQL script (text).
|
static java.lang.String |
renderQuery(SQLQuery object)
Generates SQL query string with resolved parameters that is ready to be executed.
|
static void |
saveQueryObject(SQLQuery sqo)
Deprecated.
|
static void |
saveQueryObject(java.lang.String workingDir,
SQLQuery query)
Saves an SQL Query object as a data object into the directory specified by the user.
|
static java.lang.String |
toSQL(SQLQuery object)
Generates SQL query statement used by Prepared or Callable statements where all parameters
are substituted by place-holders >.
|
getContext, getContextType, hasContext
clone, getSerialVersionUID
public static SQLQueryFactory getInstance()
public void initSerializer(SerializerFactory factory, java.lang.String serializerName) throws com.streamscape.omf.SerializerSupportException
initSerializer
in interface com.streamscape.omf.SerializerSupport
factory
- serializerName
- com.streamscape.omf.SerializerSupportException
public static SemanticType getSQLQueryType() throws UtilitiesException
UtilitiesException
public static SemanticType getSQLParameterType() throws UtilitiesException
UtilitiesException
public static SemanticType getSQLQueryBatchType() throws UtilitiesException
UtilitiesException
public static SQLColumnSet createColumnSet()
SQLColumnSet
object.public static SQLColumnSet createColumnSet(ResultSet resultSet) throws SQLException, SQLQueryValidationException, MetaDataException
SQLColumnSet
from provided ResultSet
.resultSet
- SQLException
SQLQueryValidationException
MetaDataException
public static SQLColumnSet createColumnSet(RowSet rowSet) throws MetaDataException, SQLQueryValidationException
SQLColumnSet
from provided RowSet
.rowSet
- SQLQueryValidationException
MetaDataException
public static SQLQuery createQuery(java.lang.String sqlScript) throws SQLQueryParseException, SQLQueryValidationException
sqlScript
- SQL script holds parameterized SQL querySQLQueryParseException
- if any parse errors occurSQLQueryValidationException
- if any query validation errors occurpublic static SQLQueryBatch createBatch(java.lang.String sqlScript) throws SQLQueryParseException, SQLQueryValidationException
SQLQuery
objects.sqlScript
- SQL script holds a set of parameterized SQL queriesSQLQueryParseException
- if any parse errors occurSQLQueryValidationException
- if any query validation errors occurpublic static java.lang.String toSQL(SQLQuery object) throws SQLQueryParseException, SQLQueryValidationException
object
- SQL query object that holds the querySQLQueryParseException
- if any parse errors occurSQLQueryValidationException
- if any query validation errors occurpublic static java.lang.String renderQuery(SQLQuery object) throws SQLQueryParseException, SQLQueryValidationException
object
- SQL query object holds the querySQLQueryParseException
- if any parse errors occurSQLQueryValidationException
- if any query validation errors occurpublic static java.lang.String renderBatch(SQLQueryBatch batch) throws SQLQueryParseException, SQLQueryValidationException
batch
- SQLQueryParseException
SQLQueryValidationException
public static CallableStatement createCallableStatement(Connection connection, SQLQuery object) throws SQLQueryParseException, SQLQueryValidationException, DatabaseSQLException
connection
- database connection on which callable statement will be createdobject
- SQL query object holds script and query parametersSQLQueryParseException
- if any parse errors occurSQLQueryValidationException
- if any query validation errors occur or given query object
doesn't hold SQL query to call stored procedureDatabaseSQLException
- if any database errors occurpublic static PreparedStatement createPreparedStatement(Connection connection, SQLQuery object) throws SQLQueryParseException, SQLQueryValidationException, DatabaseSQLException
connection
- database connection on which prepared statement will be createdobject
- SQL query object holds script and query parametersSQLQueryParseException
- if any parse errors occurSQLQueryValidationException
- if any query validation errors occur or given query object
holds SQL query to call stored procedureDatabaseSQLException
- if any database errors occurpublic static SQLQuery createForCall(Connection connection, java.lang.String procedure) throws SQLQueryValidationException, DatabaseSQLException, SQLQueryParseException
connection
- database connection is used to retrieve stored procedure meta-dataprocedure
- stored procedure nameSQLQueryException
- if any engine based errors occurDatabaseSQLException
- if any database errors occurSQLQueryValidationException
SQLQueryParseException
public static SQLQuery createForSelect(Connection connection, java.lang.String table) throws SQLQueryParseException, SQLQueryValidationException, DatabaseSQLException
connection
- database connectiontable
- database table name, syntax: [CATALOG].[SCHEMA].[TABLE]SQLQueryParseException
SQLQueryValidationException
DatabaseSQLException
public static SQLQuery createForSelect(Connection connection, java.lang.String table, java.lang.String[] selectColumns) throws SQLQueryParseException, SQLQueryValidationException, DatabaseSQLException
selectColumns
columns from given table.
WHERE clause not generated.connection
- database connectiontable
- database table name, syntax: [CATALOG].[SCHEMA].[TABLE]selectColumns
- column names to select, if null all columns will be selectedSQLQueryParseException
SQLQueryValidationException
DatabaseSQLException
public static SQLQuery createForSelect(Connection connection, java.lang.String table, java.lang.String[] selectColumns, java.lang.String[] whereColumns) throws SQLQueryParseException, SQLQueryValidationException, DatabaseSQLException
selectColumns
columns from given table.
WHERE clause is based on whereColumns
column names and concatenated with "AND".connection
- database connectiontable
- database table name, syntax: [CATALOG].[SCHEMA].[TABLE]selectColumns
- column names to select, if null all columns will be selectedwhereColumns
- columns names in where clause, if null WHERE clause will not be generatedSQLQueryParseException
SQLQueryValidationException
DatabaseSQLException
public static SQLQuery createForUpdate(Connection connection, java.lang.String table) throws SQLQueryValidationException, SQLQueryParseException, DatabaseSQLException
NULL
may be specified.
WHERE clause not generated.connection
- database connection is used to retrieve table meta-datatable
- database table name, syntax: [CATALOG].[SCHEMA].[TABLE]SQLQueryValidationException
SQLQueryParseException
DatabaseSQLException
public static SQLQuery createForUpdate(Connection connection, java.lang.String table, java.lang.String[] updateColumns) throws SQLQueryValidationException, SQLQueryParseException, DatabaseSQLException
updateColumns
columns.
WHERE clause not generated.connection
- database connection is used to retrieve table meta-datatable
- database table name, syntax: [CATALOG].[SCHEMA].[TABLE]updateColumns
- columns to update, if null update for all columns will be generatedSQLQueryValidationException
SQLQueryParseException
DatabaseSQLException
public static SQLQuery createForUpdate(Connection connection, java.lang.String table, java.lang.String[] updateColumns, java.lang.String[] whereColumns) throws SQLQueryValidationException, SQLQueryParseException, DatabaseSQLException
updateColumns
columns.
WHERE clause is based on whereColumns
column names and concatenated with "AND".connection
- database connection is used to retrieve table meta-datatable
- database table name, syntax: [CATALOG].[SCHEMA].[TABLE]updateColumns
- columns to update, if null update for all columns will be generatedwhereColumns
- columns in where clause, if null no where clause will be generatedSQLQueryException
- if any engine based errors occurSQLQueryParseException
DatabaseSQLException
SQLQueryValidationException
public static SQLQuery createForUpdate(java.lang.String table, SQLColumnSet columnSet) throws SQLQueryParseException, SQLQueryValidationException
columnSet
columns.
WHERE clause is not generated.table
- database table name, syntax: [CATALOG].[SCHEMA].[TABLE]columnSet
- column to updateSQLQueryParseException
SQLQueryValidationException
public static SQLQuery createForDelete(Connection connection, java.lang.String table) throws SQLQueryValidationException, SQLQueryParseException, DatabaseSQLException
table
table.
WHERE clause not generated.connection
- database connectiontable
- database table name, syntax: [CATALOG].[SCHEMA].[TABLE]SQLQueryValidationException
SQLQueryParseException
DatabaseSQLException
public static SQLQuery createForDelete(Connection connection, java.lang.String table, java.lang.String[] whereColumns) throws SQLQueryValidationException, SQLQueryParseException, DatabaseSQLException
table
table.
WHERE clause is based on whereColumns
column names and concatenated with "AND".connection
- database connectiontable
- database table name, syntax: [CATALOG].[SCHEMA].[TABLE]whereColumns
- column names for where clause, if null where clause not generatedSQLQueryValidationException
SQLQueryParseException
DatabaseSQLException
public static SQLQuery createForInsert(Connection connection, java.lang.String table) throws SQLQueryValidationException, DatabaseSQLException, SQLQueryParseException
NULL
may be specified.connection
- The database connection that is used to retrieve table meta-datatable
- database table name, syntax: [CATALOG].[SCHEMA].[TABLE]SQLQueryException
- if any engine based errors occurDatabaseSQLException
- if any database errors occurSQLQueryValidationException
SQLQueryParseException
public static SQLQuery createForDynamicSQL(Connection connection, java.lang.String query) throws SQLQueryParseException, SQLQueryValidationException, DatabaseSQLException
query
SQL prepared query.
Place-holders > symbols in SQL query will be resolved and replaced with parameters in the following format:
${ [ARRAY] DATA_TYPE [(SCALE, PRECISION)] @VARIABLE_NAME (DEFAULT = VALUE) (PARAMETER_TYPE) }INSERT,UPDATE,SELECT and DELETE queries types are supported. Joins, function values, aliases not supported.
connection
- database connectionquery
- sql querySQLQueryParseException
SQLQueryValidationException
DatabaseSQLException
@Deprecated public static void saveQueryObject(SQLQuery sqo) throws SQLQueryException
sqo
- SQLQueryException
public static void saveQueryObject(java.lang.String workingDir, SQLQuery query) throws SQLQueryException
workingDir
- query
- SQL querySQLQueryException
public static SQLQuery loadQueryObject(java.lang.String workingDir, java.lang.String queryName) throws SQLQueryException
SQLQuery.{ Query Name }.xdo
.workingDir
- queryName
- SQLQueryException
public static java.util.List execute(SQLQuery query, int queryTimeout, Connection connection) throws DatabaseSQLException, SQLQueryParseException, SQLQueryValidationException, SQLQueryException
query
- The SQL query object that contains a fully resolved query to execute.queryTimeout
- The number of seconds that this query waits before the driver times out.connection
- The database connection to use.RowSet
objects as a list. Various databases may return more then one result set.DatabaseSQLException
SQLQueryParseException
SQLQueryValidationException
SQLQueryException
public static int execute(SQLQueryBatch batch, int queryTimeout, Connection connection) throws DatabaseSQLException, SQLQueryParseException, SQLQueryValidationException, SQLQueryException
batch
- The SQL batch query script object that holds the SQL batch.connection
- The database connection.DatabaseSQLException
SQLQueryParseException
SQLQueryValidationException
SQLQueryException
public static java.util.List getSupportedTypes()
Copyright © 2015-2024 StreamScape Technologies. All rights reserved.