org.ujorm.orm
Class SqlDialect

java.lang.Object
  extended by org.ujorm.orm.SqlDialect
Direct Known Subclasses:
DerbyDialect, FirebirdDialect, H2Dialect, MSSqlDialect, MySqlDialect, PostgreSqlDialect

public abstract class SqlDialect
extends Object

SQL dialect abstract class. Methods of this class print a SQL statement(s) along a metamodel usually. You may create a subclass of any implementation to create another SQL statement, however just I can't exclude some small changes of this API in the next release.


Field Summary
static String COMMON_SEQ_TABLE_KEY
          The table key for a common sequence emulator.
static String DEFAULT_SCHEMA_SYMBOL
          The default schema symbol
protected  OrmHandler ormHandler
          The ORM handler
 
Constructor Summary
SqlDialect()
           
 
Method Summary
 Connection createConnection(MetaDatabase db)
          Create a new database connection
 InitialContext createJndiInitialContext(MetaDatabase db)
          Get or create an Initial Context for the JNDI lookup.
protected  Query createSubQuery(CriterionDecoder decoder)
          Create a subquery for the DELETE/UPDATE statement
protected  void escape(CharSequence text, Appendable out)
          Escape the special character: "'"
protected  String getAliasColumnName(ColumnWrapper column)
          Returns quoted column name including the alias table
protected  String getColumnType(MetaColumn column)
          Returns a database column type
 String getCriterionTemplate(ValueCriterion crit)
          Returns an SQL criterion template.
 SqlDialectEx getExtentedDialect()
          Get an Extended dialect
abstract  String getJdbcDriver()
          Returns a JDBC driver class name.
abstract  String getJdbcUrl()
          Returns a default JDBC URL
 Set<String> getKeywordSet(Connection conn)
          Return database SQL keyword set in the upper case.
 SqlNameProvider getNameProvider()
          Returns a name provider
protected  String getQuotedName(CharSequence name)
          Prints quoted name (identifier) to SQL - always.
 SeqTableModel getSeqTableModel()
          The table model for an internal sequence table support
 boolean isCatalog()
          Does the database support a catalog? The feature supports: MySqlDialect and MSSqlDialect.
protected  boolean isColumnLengthAllowed(MetaColumn column)
          Is allowed a column length in a SQL phrase for creating column? The length example can be: NAME CHAR(10)
protected  boolean isFilled(CharSequence text)
          Returns true, if the argument text is not null and not empty.
 boolean isMultiRowInsertSupported()
          Is supported the Multirow inserts ? Default value is true
 Appendable printAlterTableAddColumn(MetaColumn column, Appendable out)
          Print a SQL script to add a new column to the table
 Appendable printAlterTableColumn(MetaColumn column, boolean add, Appendable out)
          Print a SQL script to add a new column to the table
 Appendable printCall(MetaProcedure procedure, Appendable out)
          Print the call of a stored procedure by template:
{? = call procedure_when(?,?)}
 Appendable printColumnAlias(ColumnWrapper column, Appendable out)
          Print a full SQL column alias name by sample: "TABLE_ALIAS"."ORIG_COLUMN"
 Appendable printColumnDeclaration(MetaColumn column, String aName, Appendable out)
          Print a SQL to create column
 Appendable printComment(MetaColumn column, Appendable out)
          Print a Comment to a database Column
 Appendable printComment(MetaTable table, Appendable out)
          Print a Comment to a database Table
 Appendable printCommit(Appendable out)
          Print SQL 'COMMIT'
 Appendable printCreateSchema(String schema, Appendable out)
          Print SQL 'CREATE SCHEMA'
 ValueCriterion printCriterion(ValueCriterion crit, Appendable out)
          Print a condition phrase from the criterion.
protected  ValueCriterion printCriterionValue(String template, ColumnWrapper column, ValueCriterion crit, Appendable out)
          Write a right value form criterion
 Appendable printDefaultConstraint(MetaColumn column, StringBuilder out)
          Print Default Constraint
 Appendable printDefaultSchema(String schema, Appendable out)
          Deprecated. 
 Appendable printDefaultValue(MetaColumn column, Appendable out)
          Print a SQL phrase for the DEFAULT VALUE, for example: DEFAULT 777
 Appendable printDelete(CriterionDecoder decoder, Appendable out)
          Print an SQL DELETE statement.
 Appendable printFKColumnsDeclaration(MetaColumn column, Appendable out)
          Print a SQL to create foreign keys.
 Appendable printForeignKey(MetaColumn column, Appendable out)
          Print foreign key for the parameter column
 void printForeignKey(ValueCriterion crit, ColumnWrapper column, String template, Appendable out)
          Print all items of the foreign key
 Appendable printFullTableName(MetaTable table, Appendable out)
          Print a full SQL table name by sample: SCHEMA.TABLE
 Appendable printFullTableName(MetaTable table, boolean printSymbolSchema, Appendable out)
          Print a extended SQL table name by sample: SCHEMA.TABLE
 Appendable printIndex(MetaIndex index, Appendable out)
          Print an INDEX for the parameter column.
 Appendable printInsert(List<? extends OrmUjo> bos, int idxFrom, int idxTo, Appendable out)
          Print an SQL INSERT statement.
 Appendable printInsert(OrmUjo bo, Appendable out)
          Print an SQL INSERT statement.
 Appendable printInsertBySelect(List<? extends OrmUjo> bos, int idxFrom, int idxTo, String fromPhrase, Appendable out)
          Print an batch SQL INSERT statement using SELECT UNION statement.
 void println(Appendable out)
          Print the new line.
protected  Appendable printLockForSelect(Query query, Appendable out)
          Print a 'lock clausule' to the end of SQL SELECT statement to use a pessimistic lock.
 Appendable printNextSequence(String sequenceName, MetaTable table, Appendable out)
          Create a SQL script for the NEXT SEQUENCE from a native database sequencer
 void printOffset(Query query, Appendable out)
          Print an OFFSET of the statement SELECT.
 Appendable printQuotedName(CharSequence name, Appendable sql)
          Prints quoted name (identifier) to SQL according the parameter MetaParams.QUOTE_SQL_NAMES.
protected  Appendable printQuotedNameAlways(CharSequence name, Appendable sql)
          Prints quoted name (identifier) to SQL always.
 Appendable printSelect(TableWrapper table, Query query, boolean count, Appendable out)
          Print a SQL SELECT by table model and query
protected  void printSelectOrder(Query query, Appendable out)
          Print SQL ORDER BY
protected  Appendable printSelectTable(Query query, boolean count, Appendable out)
          Print SQL database SELECT
protected  void printSelectTableBase(Query query, boolean count, Appendable out)
          Print SQL database SELECT
protected  Appendable printSelectView(TableWrapper table, Query query, boolean count, Appendable out)
          Print SQL view SELECT
 Appendable printSequenceCurrentValue(UjoSequencer sequence, Appendable out)
          Print SQL CURRENT SEQUENCE VALUE.
 Appendable printSequenceDeleteById(UjoSequencer sequence, String id, Appendable out)
          Print SQL DELETE SEQUENCE BY ID.
 Appendable printSequenceInit(UjoSequencer sequence, Appendable out)
          Print SQL CREATE SEQUENCE (insert sequence row).
 Appendable printSequenceInitWithValues(UjoSequencer sequence, long seq, int cache, Appendable out)
           
 Appendable printSequenceNextValue(UjoSequencer sequence, Appendable out)
          Print SQL UPDATE NEXT SEQUENCE value.
 Appendable printSequenceTable(MetaDatabase db, Appendable out)
          Print SQL CREATE SEQUENCE.
protected  Appendable printSequenceTableName(UjoSequencer sequence, Appendable out)
          Prinnt the full sequence table
 Appendable printSetMaxSequence(UjoSequencer sequence, Appendable out)
          Set sequence to the max value.
 Appendable printTable(MetaTable table, Appendable out)
          Print a SQL script to create table
 void printTableAliasDefinition(TableWrapper table, Appendable out)
          Print a SQL database and table name and an alias definition - by sample: SCHEMA.TABLE ALIAS
 void printTableColumns(Collection<? extends ColumnWrapper> columns, Appendable values, Appendable out)
          Print table columns
 Appendable printUpdate(List<MetaColumn> changedColumns, CriterionDecoder decoder, Appendable out)
          Print an SQL UPDATE statement.
 void releaseSavepoint(Connection conn, Savepoint savepoint, boolean afterRollback)
          Perform the method: {@link Connection#releaseSavepoint(java.sql.Savepoint) ?
 void setHandler(OrmHandler ormHandler)
          Set the OrmHandler - the method is for internal call only.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COMMON_SEQ_TABLE_KEY

public static final String COMMON_SEQ_TABLE_KEY
The table key for a common sequence emulator.

See Also:
Constant Field Values

DEFAULT_SCHEMA_SYMBOL

public static final String DEFAULT_SCHEMA_SYMBOL
The default schema symbol

See Also:
Constant Field Values

ormHandler

protected OrmHandler ormHandler
The ORM handler

Constructor Detail

SqlDialect

public SqlDialect()
Method Detail

setHandler

public void setHandler(OrmHandler ormHandler)
Set the OrmHandler - the method is for internal call only.


getJdbcUrl

public abstract String getJdbcUrl()
Returns a default JDBC URL


getJdbcDriver

public abstract String getJdbcDriver()
Returns a JDBC driver class name.


createConnection

public Connection createConnection(MetaDatabase db)
                            throws Exception
Create a new database connection

Throws:
Exception

createJndiInitialContext

public InitialContext createJndiInitialContext(MetaDatabase db)
                                        throws NamingException
Get or create an Initial Context for the JNDI lookup.

Throws:
NamingException

isCatalog

public boolean isCatalog()
Does the database support a catalog? The feature supports: MySqlDialect and MSSqlDialect.

Returns:
The default value is false.

printCreateSchema

public Appendable printCreateSchema(String schema,
                                    Appendable out)
                             throws IOException
Print SQL 'CREATE SCHEMA'

Throws:
IOException

printDefaultSchema

@Deprecated
public Appendable printDefaultSchema(String schema,
                                                Appendable out)
                              throws IOException
Deprecated. 

Print SQL 'SET SCHEMA'. The method is not used yet.

Throws:
IOException

printFullTableName

public final Appendable printFullTableName(MetaTable table,
                                           Appendable out)
                                    throws IOException
Print a full SQL table name by sample: SCHEMA.TABLE

Throws:
IOException

printFullTableName

public Appendable printFullTableName(MetaTable table,
                                     boolean printSymbolSchema,
                                     Appendable out)
                              throws IOException
Print a extended SQL table name by sample: SCHEMA.TABLE

Parameters:
printSymbolSchema - True parameter replaces a default schema name for the symbol "~" by the example: ~.TABLE
Throws:
IOException

printTableAliasDefinition

public void printTableAliasDefinition(TableWrapper table,
                                      Appendable out)
                               throws IOException
Print a SQL database and table name and an alias definition - by sample: SCHEMA.TABLE ALIAS

Throws:
IOException

printColumnAlias

public Appendable printColumnAlias(ColumnWrapper column,
                                   Appendable out)
                            throws IOException
Print a full SQL column alias name by sample: "TABLE_ALIAS"."ORIG_COLUMN"

Throws:
IOException

printTable

public Appendable printTable(MetaTable table,
                             Appendable out)
                      throws IOException
Print a SQL script to create table

Throws:
IOException

printAlterTableAddColumn

public Appendable printAlterTableAddColumn(MetaColumn column,
                                           Appendable out)
                                    throws IOException
Print a SQL script to add a new column to the table

Throws:
IOException

printAlterTableColumn

public Appendable printAlterTableColumn(MetaColumn column,
                                        boolean add,
                                        Appendable out)
                                 throws IOException
Print a SQL script to add a new column to the table

Throws:
IOException

printDefaultValue

public Appendable printDefaultValue(MetaColumn column,
                                    Appendable out)
                             throws IOException
Print a SQL phrase for the DEFAULT VALUE, for example: DEFAULT 777

Throws:
IOException

printDefaultConstraint

public Appendable printDefaultConstraint(MetaColumn column,
                                         StringBuilder out)
                                  throws IOException
Print Default Constraint

Throws:
IOException

printForeignKey

public Appendable printForeignKey(MetaColumn column,
                                  Appendable out)
                           throws IOException
Print foreign key for the parameter column

Returns:
More statements separated by the ';' characters are enabled
Throws:
IOException

printIndex

public Appendable printIndex(MetaIndex index,
                             Appendable out)
                      throws IOException
Print an INDEX for the parameter column.

Returns:
More statements separated by the ';' characters are enabled
Throws:
IOException

isColumnLengthAllowed

protected boolean isColumnLengthAllowed(MetaColumn column)
Is allowed a column length in a SQL phrase for creating column? The length example can be: NAME CHAR(10)


printColumnDeclaration

public Appendable printColumnDeclaration(MetaColumn column,
                                         String aName,
                                         Appendable out)
                                  throws IOException
Print a SQL to create column

Parameters:
column - Database Column
aName - The name parameter is not mandatory, the not null value means a foreign key.
Throws:
IOException

getColumnType

protected String getColumnType(MetaColumn column)
Returns a database column type


printFKColumnsDeclaration

public Appendable printFKColumnsDeclaration(MetaColumn column,
                                            Appendable out)
                                     throws IOException
Print a SQL to create foreign keys.

Throws:
IOException

printInsert

public Appendable printInsert(OrmUjo bo,
                              Appendable out)
                       throws IOException
Print an SQL INSERT statement.

Throws:
IOException

printInsert

public Appendable printInsert(List<? extends OrmUjo> bos,
                              int idxFrom,
                              int idxTo,
                              Appendable out)
                       throws IOException
Print an SQL INSERT statement.

Parameters:
bos - Business object list
idxFrom - Start index from list
idxTo - Finished index from list (excluded)
Throws:
IOException
See Also:
isMultiRowInsertSupported()

printInsertBySelect

public Appendable printInsertBySelect(List<? extends OrmUjo> bos,
                                      int idxFrom,
                                      int idxTo,
                                      String fromPhrase,
                                      Appendable out)
                               throws IOException
Print an batch SQL INSERT statement using SELECT UNION statement.

Parameters:
bos - Business object list
idxFrom - Start index from list
idxTo - Finished index from list (excluded)
fromPhrase - For example the Oracle syntax: SELECT 1,2,3 FROM DUAL;
Throws:
IOException
See Also:
isMultiRowInsertSupported()

isMultiRowInsertSupported

public boolean isMultiRowInsertSupported()
Is supported the Multirow inserts ? Default value is true

See Also:
Multi row insert

printUpdate

public Appendable printUpdate(List<MetaColumn> changedColumns,
                              CriterionDecoder decoder,
                              Appendable out)
                       throws IOException
Print an SQL UPDATE statement.

Throws:
IOException

printDelete

public Appendable printDelete(CriterionDecoder decoder,
                              Appendable out)
                       throws IOException
Print an SQL DELETE statement.

Throws:
IOException

createSubQuery

protected Query createSubQuery(CriterionDecoder decoder)
Create a subquery for the DELETE/UPDATE statement


getCriterionTemplate

public String getCriterionTemplate(ValueCriterion crit)
Returns an SQL criterion template. The result is a tempate by the next sample: "{0}={1}" .
See an example of the implementation:
 switch (crit.getOperator()) {
     case EQ:
         return "{0}={1}";
     case NOT_EQ:
         return "{0}<>{1}";
     case GT:
         return "{0}>{1}";
     ...
 


printTableColumns

public void printTableColumns(Collection<? extends ColumnWrapper> columns,
                              Appendable values,
                              Appendable out)
                       throws IOException
Print table columns

Parameters:
columns - List of tablel columns
values - Print columns including its aliases.
out - Table columns output.
Throws:
IOException

printCriterion

public ValueCriterion printCriterion(ValueCriterion crit,
                                     Appendable out)
                              throws IOException
Print a condition phrase from the criterion.

Returns:
A value criterion to assign into the SQL query.
Throws:
IOException

printCriterionValue

protected ValueCriterion printCriterionValue(String template,
                                             ColumnWrapper column,
                                             ValueCriterion crit,
                                             Appendable out)
                                      throws IOException
Write a right value form criterion

Returns:
A value criterion to assign into the SQL query.
Throws:
IOException

getAliasColumnName

protected String getAliasColumnName(ColumnWrapper column)
                             throws IOException
Returns quoted column name including the alias table

Throws:
IOException

printForeignKey

public void printForeignKey(ValueCriterion crit,
                            ColumnWrapper column,
                            String template,
                            Appendable out)
                     throws IOException
Print all items of the foreign key

Throws:
IOException

printSelect

public final Appendable printSelect(TableWrapper table,
                                    Query query,
                                    boolean count,
                                    Appendable out)
                             throws IOException
Print a SQL SELECT by table model and query

Parameters:
query - The UJO query
count - only count of items is required;
Throws:
IOException

printSelectView

protected Appendable printSelectView(TableWrapper table,
                                     Query query,
                                     boolean count,
                                     Appendable out)
                              throws IOException
Print SQL view SELECT

Parameters:
query - The UJO query
count - only count of items is required;
Throws:
IOException

printSelectTable

protected Appendable printSelectTable(Query query,
                                      boolean count,
                                      Appendable out)
                               throws IOException
Print SQL database SELECT

Parameters:
query - The UJO query
count - only count of items is required;
Throws:
IOException

printSelectTableBase

protected void printSelectTableBase(Query query,
                                    boolean count,
                                    Appendable out)
                             throws IOException
Print SQL database SELECT

Parameters:
query - The UJO query
count - only count of items is required;
Throws:
IOException

printLockForSelect

protected Appendable printLockForSelect(Query query,
                                        Appendable out)
                                 throws IOException,
                                        UnsupportedOperationException
Print a 'lock clausule' to the end of SQL SELECT statement to use a pessimistic lock. The current database does not support the feature, throw an exception UnsupportedOperationException.
The method prints a text "FOR UPDATE".

Parameters:
query - The UJO query
Throws:
IOException
UnsupportedOperationException

printSelectOrder

protected void printSelectOrder(Query query,
                                Appendable out)
                         throws IOException
Print SQL ORDER BY

Throws:
IOException

printCall

public Appendable printCall(MetaProcedure procedure,
                            Appendable out)
                     throws IOException
Print the call of a stored procedure by template:
{? = call procedure_when(?,?)}

Throws:
IOException

printOffset

public void printOffset(Query query,
                        Appendable out)
                 throws IOException
Print an OFFSET of the statement SELECT.

Throws:
IOException

printSequenceTableName

protected Appendable printSequenceTableName(UjoSequencer sequence,
                                            Appendable out)
                                     throws IOException
Prinnt the full sequence table

Throws:
IOException

printSequenceTable

public Appendable printSequenceTable(MetaDatabase db,
                                     Appendable out)
                              throws IOException
Print SQL CREATE SEQUENCE. No JDBC parameters.

Throws:
IOException

printSequenceInit

public Appendable printSequenceInit(UjoSequencer sequence,
                                    Appendable out)
                             throws IOException
Print SQL CREATE SEQUENCE (insert sequence row). No JDBC parameters.

Throws:
IOException

printSequenceInitWithValues

public Appendable printSequenceInitWithValues(UjoSequencer sequence,
                                              long seq,
                                              int cache,
                                              Appendable out)
                                       throws IOException
Throws:
IOException

printSequenceNextValue

public Appendable printSequenceNextValue(UjoSequencer sequence,
                                         Appendable out)
                                  throws IOException
Print SQL UPDATE NEXT SEQUENCE value.

Throws:
IOException

printSetMaxSequence

public Appendable printSetMaxSequence(UjoSequencer sequence,
                                      Appendable out)
                               throws IOException
Set sequence to the max value.

Throws:
IOException

printSequenceCurrentValue

public Appendable printSequenceCurrentValue(UjoSequencer sequence,
                                            Appendable out)
                                     throws IOException
Print SQL CURRENT SEQUENCE VALUE. Returns a new sequence limit and the. The SQL columns are always selected in the order: sequence, cache, maxValue. current cache.

Throws:
IOException

printSequenceDeleteById

public Appendable printSequenceDeleteById(UjoSequencer sequence,
                                          String id,
                                          Appendable out)
                                   throws IOException
Print SQL DELETE SEQUENCE BY ID.

Throws:
IOException

isFilled

protected final boolean isFilled(CharSequence text)
Returns true, if the argument text is not null and not empty.


println

public final void println(Appendable out)
                   throws IOException
Print the new line.

Throws:
IOException

printCommit

public Appendable printCommit(Appendable out)
                       throws IOException
Print SQL 'COMMIT'

Throws:
IOException

printComment

public Appendable printComment(MetaTable table,
                               Appendable out)
                        throws IOException
Print a Comment to a database Table

Throws:
IOException

printComment

public Appendable printComment(MetaColumn column,
                               Appendable out)
                        throws IOException
Print a Comment to a database Column

Throws:
IOException

getKeywordSet

public Set<String> getKeywordSet(Connection conn)
Return database SQL keyword set in the upper case.


escape

protected final void escape(CharSequence text,
                            Appendable out)
                     throws IOException
Escape the special character: "'"

Throws:
IOException

getSeqTableModel

public SeqTableModel getSeqTableModel()
The table model for an internal sequence table support


releaseSavepoint

public void releaseSavepoint(Connection conn,
                             Savepoint savepoint,
                             boolean afterRollback)
                      throws SQLException
Perform the method: {@link Connection#releaseSavepoint(java.sql.Savepoint) ?

Parameters:
conn - Database Connection
savepoint - Required Savepoint
afterRollback - release is called after a rollback ?
Throws:
SQLException
See Also:
http://technet.microsoft.com/en-us/library/ms378791%28v=sql.110%29.aspx, Connection.releaseSavepoint(java.sql.Savepoint)

printQuotedName

public final Appendable printQuotedName(CharSequence name,
                                        Appendable sql)
                                 throws IOException
Prints quoted name (identifier) to SQL according the parameter MetaParams.QUOTE_SQL_NAMES.

Parameters:
name - Name (identifier) for quoting
sql - Target SQL for printing new quoted name
Returns:
SQL with printed quoted name
Throws:
IOException

printQuotedNameAlways

protected Appendable printQuotedNameAlways(CharSequence name,
                                           Appendable sql)
                                    throws IOException
Prints quoted name (identifier) to SQL always. The default quated character is '"', however different SQL dialects must have got a different character-pairs.

Parameters:
name - Name (identifier) for quoting
sql - Target SQL for printing new quoted name
Returns:
SQL with printed quoted name
Throws:
IOException

getQuotedName

protected final String getQuotedName(CharSequence name)
                              throws IOException
Prints quoted name (identifier) to SQL - always.

Throws:
IOException

getNameProvider

public SqlNameProvider getNameProvider()
                                throws IllegalStateException
Returns a name provider

Returns:
Current SQL name provider
Throws:
IllegalStateException - A problem during creating an instance.

getExtentedDialect

public SqlDialectEx getExtentedDialect()
Get an Extended dialect


printNextSequence

public Appendable printNextSequence(String sequenceName,
                                    MetaTable table,
                                    Appendable out)
                             throws IOException
Create a SQL script for the NEXT SEQUENCE from a native database sequencer

Throws:
IOException


Copyright 2013, Pavel Ponec