org.ujoframework.orm
Class SqlDialect

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

public abstract class SqlDialect
extends java.lang.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 java.lang.String COMMON_SEQ_TABLE_KEY
          The table key for a common sequence emulator.
static java.lang.String COMMON_SEQ_TABLE_NAME
          The table key for a common sequence emulator.
protected  OrmHandler ormHandler
          The ORM handler
 
Constructor Summary
SqlDialect()
           
 
Method Summary
 java.sql.Connection createConnection(MetaDatabase db)
          Create a new database connection
 javax.naming.InitialContext createJndiInitialContext(MetaDatabase db)
          Get or create an Initial Context for the JNDI lookup.
protected  java.lang.String getColumnType(MetaColumn column)
          Returns a database column type
 java.lang.String getCriterionTemplate(ValueCriterion crit)
          Returns an SQL criterion template.
abstract  java.lang.String getJdbcDriver()
          Returns a JDBC driver class name.
abstract  java.lang.String getJdbcUrl()
          Returns a default JDBC URL
 java.util.Set<java.lang.String> getKeywordSet(java.sql.Connection conn)
          Return database SQL keyword set in the upper case.
protected  boolean isUsable(java.lang.CharSequence text)
          Returns true, if the argument text is not null and not empty.
 java.lang.Appendable printAlterTable(MetaColumn column, java.lang.Appendable out)
          Print a SQL sript to add a new column to the table
 java.lang.Appendable printCall(MetaProcedure procedure, java.lang.Appendable out)
          Print the call of a stored procedure by template:
{?
 java.lang.Appendable printColumnAlias(MetaColumn column, java.lang.Appendable out)
          Print a full SQL column alias name by sample: TABLE_ALIAS.COLUMN
 java.lang.Appendable printColumnDeclaration(MetaColumn column, java.lang.String aName, java.lang.Appendable out)
          Print a SQL to create column
 java.lang.Appendable printCommit(java.lang.Appendable out)
          Print SQL 'COMMIT'
 java.lang.Appendable printCreateSchema(java.lang.String schema, java.lang.Appendable out)
          Print SQL 'CREATE SCHEMA'
 ValueCriterion printCriterion(ValueCriterion crit, java.lang.Appendable out)
          Print a conditon phrase by the criterion.
 java.lang.Appendable printDefaultSchema(java.lang.String schema, java.lang.Appendable out)
          Deprecated. 
 java.lang.Appendable printDefaultValue(MetaColumn column, java.lang.Appendable out)
          Print a SQL phrase for the DEFAULT VALUE, for example: DEFAULT 777
 java.lang.Appendable printDelete(MetaTable table, CriterionDecoder decoder, java.lang.Appendable out)
          Print an SQL DELETE statement.
 java.lang.Appendable printFKColumnsDeclaration(MetaColumn column, java.lang.Appendable out)
          Print a SQL to create foreign keys.
 java.lang.Appendable printForeignKey(MetaColumn column, MetaTable table, java.lang.Appendable out)
          Print foreign key for the parameter column
 void printForeignKey(ValueCriterion crit, MetaColumn column, java.lang.String template, java.lang.Appendable out)
          Print all items of the foreign key
 java.lang.Appendable printFullTableName(MetaTable table, java.lang.Appendable out)
          Print a full SQL table name by sample: SCHEMA.TABLE
 java.lang.Appendable printIndex(MetaIndex index, java.lang.Appendable out)
          Print an INDEX for the parameter column.
 java.lang.Appendable printInsert(OrmUjo bo, java.lang.Appendable out)
          Print an SQL INSERT statement.
 void println(java.lang.Appendable out)
          Print the new line.
protected  java.lang.Appendable printLockForSelect(Query query, java.lang.Appendable out)
          Print a 'lock clausule' to the end of SQL SELECT statement to use a pessimistic lock.
 void printOffset(Query query, java.lang.Appendable out)
          Print an OFFSET of the statement SELECT.
 java.lang.Appendable printSelect(MetaTable table, Query query, boolean count, java.lang.Appendable out)
          Print a SQL SELECT by table model and query
 void printSelectOrder(Query query, java.lang.Appendable out)
          Print SQL ORDER BY
protected  java.lang.Appendable printSelectTable(Query query, boolean count, java.lang.Appendable out)
          Print SQL database SELECT
protected  java.lang.Appendable printSelectView(MetaTable table, Query query, boolean count, java.lang.Appendable out)
          Print SQL view SELECT
 java.lang.Appendable printSequenceCurrentValue(UjoSequencer sequence, java.lang.Appendable out)
          Print SQL CURRENT SEQUENCE VALUE.
 java.lang.Appendable printSequenceInit(UjoSequencer sequence, java.lang.Appendable out)
          Print SQL CREATE SEQUENCE (insert sequence row).
 java.lang.Appendable printSequenceNextValue(UjoSequencer sequence, java.lang.Appendable out)
          Print SQL UPDATE NEXT SEQUENCE value.
 java.lang.Appendable printSequenceTable(MetaDatabase db, java.lang.Appendable out)
          Print SQL CREATE SEQUENCE.
protected  java.lang.Appendable printSequenceTableName(UjoSequencer sequence, java.lang.Appendable out)
          Prinnt the full sequence table
 java.lang.Appendable printSetMaxSequence(UjoSequencer sequence, java.lang.Appendable out)
          Set sequence to the max value.
 java.lang.Appendable printTable(MetaTable table, java.lang.Appendable out)
          Print a SQL sript to create table
 void printTableAliasDefinition(MetaTable table, java.lang.Appendable out)
          Print a SQL database and table name and an alias definition - by sample: SCHEMA.TABLE ALIAS
 void printTableColumns(java.util.List<MetaColumn> columns, java.lang.Appendable values, java.lang.Appendable out)
          Print table columns
 java.lang.Appendable printUpdate(MetaTable table, java.util.List<MetaColumn> changedColumns, CriterionDecoder decoder, java.lang.Appendable out)
          Print an SQL UPDATE statement.
 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_NAME

public static final java.lang.String COMMON_SEQ_TABLE_NAME
The table key for a common sequence emulator.
The SQL script for migration to the Ujorm 0.93:
ALTER TABLE ormujo_pk_support RENAME TO ujorm_pk_support;

See Also:
Constant Field Values

COMMON_SEQ_TABLE_KEY

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

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 java.lang.String getJdbcUrl()
Returns a default JDBC URL


getJdbcDriver

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


createConnection

public java.sql.Connection createConnection(MetaDatabase db)
                                     throws java.lang.Exception
Create a new database connection

Throws:
java.lang.Exception

createJndiInitialContext

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

Throws:
javax.naming.NamingException

printCreateSchema

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

Throws:
java.io.IOException

printDefaultSchema

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

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

Throws:
java.io.IOException

printFullTableName

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

Throws:
java.io.IOException

printTableAliasDefinition

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

Throws:
java.io.IOException

printColumnAlias

public java.lang.Appendable printColumnAlias(MetaColumn column,
                                             java.lang.Appendable out)
                                      throws java.io.IOException
Print a full SQL column alias name by sample: TABLE_ALIAS.COLUMN

Throws:
java.io.IOException

printTable

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

Throws:
java.io.IOException

printAlterTable

public java.lang.Appendable printAlterTable(MetaColumn column,
                                            java.lang.Appendable out)
                                     throws java.io.IOException
Print a SQL sript to add a new column to the table

Throws:
java.io.IOException

printDefaultValue

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

Throws:
java.io.IOException

printForeignKey

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

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

printIndex

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

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

printColumnDeclaration

public java.lang.Appendable printColumnDeclaration(MetaColumn column,
                                                   java.lang.String aName,
                                                   java.lang.Appendable out)
                                            throws java.io.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:
java.io.IOException

getColumnType

protected java.lang.String getColumnType(MetaColumn column)
Returns a database column type


printFKColumnsDeclaration

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

Throws:
java.io.IOException

printInsert

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

Throws:
java.io.IOException

printUpdate

public java.lang.Appendable printUpdate(MetaTable table,
                                        java.util.List<MetaColumn> changedColumns,
                                        CriterionDecoder decoder,
                                        java.lang.Appendable out)
                                 throws java.io.IOException
Print an SQL UPDATE statement.

Throws:
java.io.IOException

printDelete

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

Throws:
java.io.IOException

getCriterionTemplate

public java.lang.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(java.util.List<MetaColumn> columns,
                              java.lang.Appendable values,
                              java.lang.Appendable out)
                       throws java.io.IOException
Print table columns

Parameters:
columns - List of tablel columns
values - Print columns include alias.
out - Table columns output.
Throws:
java.io.IOException

printCriterion

public ValueCriterion printCriterion(ValueCriterion crit,
                                     java.lang.Appendable out)
                              throws java.io.IOException
Print a conditon phrase by the criterion.

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

printForeignKey

public void printForeignKey(ValueCriterion crit,
                            MetaColumn column,
                            java.lang.String template,
                            java.lang.Appendable out)
                     throws java.io.IOException
Print all items of the foreign key

Throws:
java.io.IOException

printSelect

public java.lang.Appendable printSelect(MetaTable table,
                                        Query query,
                                        boolean count,
                                        java.lang.Appendable out)
                                 throws java.io.IOException
Print a SQL SELECT by table model and query

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

printSelectView

protected java.lang.Appendable printSelectView(MetaTable table,
                                               Query query,
                                               boolean count,
                                               java.lang.Appendable out)
                                        throws java.io.IOException
Print SQL view SELECT

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

printSelectTable

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

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

printLockForSelect

protected java.lang.Appendable printLockForSelect(Query query,
                                                  java.lang.Appendable out)
                                           throws java.io.IOException,
                                                  java.lang.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:
java.io.IOException
java.lang.UnsupportedOperationException

printSelectOrder

public void printSelectOrder(Query query,
                             java.lang.Appendable out)
                      throws java.io.IOException
Print SQL ORDER BY

Throws:
java.io.IOException

printCall

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

Throws:
java.io.IOException

printOffset

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

Throws:
java.io.IOException

printSequenceTableName

protected java.lang.Appendable printSequenceTableName(UjoSequencer sequence,
                                                      java.lang.Appendable out)
                                               throws java.io.IOException
Prinnt the full sequence table

Throws:
java.io.IOException

printSequenceTable

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

Throws:
java.io.IOException

printSequenceInit

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

Throws:
java.io.IOException

printSequenceNextValue

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

Throws:
java.io.IOException

printSetMaxSequence

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

Throws:
java.io.IOException

printSequenceCurrentValue

public java.lang.Appendable printSequenceCurrentValue(UjoSequencer sequence,
                                                      java.lang.Appendable out)
                                               throws java.io.IOException
Print SQL CURRENT SEQUENCE VALUE. Returns a new sequence limit and the current cache.

Throws:
java.io.IOException

isUsable

protected boolean isUsable(java.lang.CharSequence text)
Returns true, if the argument text is not null and not empty.


println

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

Throws:
java.io.IOException

printCommit

public java.lang.Appendable printCommit(java.lang.Appendable out)
                                 throws java.io.IOException
Print SQL 'COMMIT'

Throws:
java.io.IOException

getKeywordSet

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



Copyright © 2010. All Rights Reserved.