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.

Author:
Pavel Ponec

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  void escape(CharSequence text, Appendable out)
          Escape the special character: "'"
protected  String getColumnType(MetaColumn column)
          Returns a database column type
 String getCriterionTemplate(ValueCriterion crit)
          Returns an SQL criterion template.
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.
protected  String getQuotedName(CharSequence name)
          Prints quoted name (identifier) to SQL.
 SeqTableModel getSeqTableModel()
          The table model for an internal sequence table support
protected  boolean isColumnLengthAllowed(MetaColumn column)
          Is allowed a column length in a SQL phrase for creating column?
protected  boolean isFilled(CharSequence text)
          Returns true, if the argument text is not null and not empty.
 boolean isMultiRowInsertSupported()
          Is supported the Multirow inserts ?
 Appendable printAlterTableAddColumn(MetaColumn column, Appendable out)
          Print a SQL sript to add a new column to the table
 Appendable printCall(MetaProcedure procedure, Appendable out)
          Print the call of a stored procedure by template:
{?
 Appendable printColumnAlias(ColumnWrapper column, Appendable out)
          Print a full SQL column alias name by sample: TABLE_ALIAS.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'
protected  void printConstraintName(MetaTable table, MetaColumn column, Appendable out)
          Print a constraint name
 Appendable printCreateSchema(String schema, Appendable out)
          Print SQL 'CREATE SCHEMA'
 ValueCriterion printCriterion(ValueCriterion crit, Appendable out)
          Print a conditon phrase from the criterion.
 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(MetaTable table, 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, MetaTable table, 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 unsing SELECT UNION statejemnt.
 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.
 void printOffset(Query query, Appendable out)
          Print an OFFSET of the statement SELECT.
protected  Appendable printQuotedName(CharSequence name, Appendable sql)
          Prints quoted name (identifier) to SQL.
 Appendable printSelect(TableWrapper table, Query query, boolean count, Appendable out)
          Print a SQL SELECT by table model and query
 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 printSequenceInit(UjoSequencer sequence, long seq, int cache, Appendable out)
          Print SQL CREATE SEQUENCE (insert sequence row).
 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 printTable(MetaTable table, Appendable out)
          Print a SQL sript 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(List<? extends ColumnWrapper> columns, Appendable values, Appendable out)
          Print table columns
 Appendable printUpdate(MetaTable table, 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

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.COLUMN

Throws:
IOException

printTable

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

Throws:
IOException

printAlterTableAddColumn

public Appendable printAlterTableAddColumn(MetaColumn column,
                                           Appendable out)
                                    throws IOException
Print a SQL sript 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

printForeignKey

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

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

printConstraintName

protected void printConstraintName(MetaTable table,
                                   MetaColumn column,
                                   Appendable out)
                            throws IOException
Print a constraint name

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 ';' charactes 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 unsing SELECT UNION statejemnt.

Parameters:
bos - Business object list
idxFrom - Start index from list
idxTo - Finished index from list (excluded)
fromPhrase - For example the Oracla 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(MetaTable table,
                              List<MetaColumn> changedColumns,
                              CriterionDecoder decoder,
                              Appendable out)
                       throws IOException
Print an SQL UPDATE statement.

Throws:
IOException

printDelete

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

Throws:
IOException

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(List<? 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 conditon phrase from the criterion.

Returns:
A value criterion to assign into the SQL query.
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

public 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,
                                    long seq,
                                    int cache,
                                    Appendable out)
                             throws IOException
Print SQL CREATE SEQUENCE (insert sequence row). No JDBC parameters.

Throws:
IOException

printSequenceNextValue

public Appendable printSequenceNextValue(UjoSequencer sequence,
                                         Appendable out)
                                  throws IOException
Print SQL UPDATE NEXT SEQUENCE 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 current cache.

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

protected Appendable printQuotedName(CharSequence name,
                                     Appendable sql)
                              throws IOException
Prints quoted name (identifier) to SQL. Method is prepared for overriding based on SQL dialect.

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.

Throws:
IOException


Copyright © 2012. All Rights Reserved.