com.j256.ormlite.db
Class BaseDatabaseType

java.lang.Object
  extended by com.j256.ormlite.db.BaseDatabaseType
All Implemented Interfaces:
DatabaseType
Direct Known Subclasses:
Db2DatabaseType, DerbyEmbeddedDatabaseType, H2DatabaseType, HsqldbDatabaseType, MysqlDatabaseType, OracleDatabaseType, PostgresDatabaseType, SqliteDatabaseType, SqlServerDatabaseType

public abstract class BaseDatabaseType
extends Object
implements DatabaseType

Base class for all of the DatabaseType classes that provide the per-database type functionality to create tables and build queries.

Here's a good page which shows some of the differences between SQL versions.

Author:
graywatson

Nested Class Summary
protected static class BaseDatabaseType.BooleanNumberFieldConverter
          Conversion to/from the Boolean Java field as a number because some databases like the true/false.
 
Field Summary
protected static FieldConverter booleanConverter
           
 
Constructor Summary
BaseDatabaseType()
           
 
Method Summary
protected  void appendBooleanType(StringBuilder sb)
          Output the SQL type for a Java boolean.
protected  void appendByteType(StringBuilder sb)
          Output the SQL type for a Java byte.
protected  void appendCanBeNull(StringBuilder sb, FieldType fieldType)
          If the field can be nullable, do we need to add some sort of NULL SQL for the create table.
 void appendColumnArg(StringBuilder sb, FieldType fieldType, List<String> additionalArgs, List<String> statementsBefore, List<String> statementsAfter, List<String> queriesAfter)
          Takes a FieldType and appends the SQL necessary to create the field to the string builder.
 void appendCreateTableSuffix(StringBuilder sb)
          Append the SQL necessary to properly finish a CREATE TABLE line.
protected  void appendDateType(StringBuilder sb)
          Output the SQL type for a Java Date.
protected  void appendDefaultValue(StringBuilder sb, FieldType fieldType, Object defaultValue)
          Output the SQL type for a Java boolean default value.
protected  void appendDoubleType(StringBuilder sb)
          Output the SQL type for a Java double.
protected  void appendEnumIntType(StringBuilder sb, FieldType fieldType)
          Output the SQL type for a Enum object stored as intg.
protected  void appendEnumStringType(StringBuilder sb, FieldType fieldType)
          Output the SQL type for a Enum object stored as String.
 void appendEscapedEntityName(StringBuilder sb, String word)
          Add a entity-name word to the string builder wrapped in the proper characters to escape it.
 void appendEscapedWord(StringBuilder sb, String word)
          Add the word to the string builder wrapped in the proper characters to escape it.
protected  void appendFloatType(StringBuilder sb)
          Output the SQL type for a Java float.
protected  void appendIntegerType(StringBuilder sb)
          Output the SQL type for a Java integer.
 void appendLimitValue(StringBuilder sb, int limit)
          Append to the string builder the necessary SQL to limit the results to a certain number.
protected  void appendLongType(StringBuilder sb)
          Output the SQL type for a Java long.
protected  void appendObjectType(StringBuilder sb)
          Output the SQL type for a Java object.
 void appendSelectNextValFromSequence(StringBuilder sb, String sequenceName)
          Append the SQL necessary to get the next-value from a sequence.
protected  void appendShortType(StringBuilder sb)
          Output the SQL type for a Java short.
protected  void appendStringType(StringBuilder sb, FieldType fieldType, int fieldWidth)
          Output the SQL type for a Java String.
 DatabaseAccess buildDatabaseAccess(DataSource dataSource)
          Return a newly built database access object which possibly is attached to the provided data source.
protected  void configureGeneratedId(StringBuilder sb, FieldType fieldType, List<String> statementsBefore, List<String> additionalArgs, List<String> queriesAfter)
          Output the SQL necessary to configure a generated-id column.
protected  void configureGeneratedIdSequence(StringBuilder sb, FieldType fieldType, List<String> statementsBefore, List<String> additionalArgs, List<String> queriesAfter)
          Output the SQL necessary to configure a generated-id column.
protected  void configureId(StringBuilder sb, FieldType fieldType, List<String> statementsBefore, List<String> additionalArgs, List<String> queriesAfter)
          Output the SQL necessary to configure an id column.
 String convertColumnName(String columnName)
          Convert and return the column name for table and sequence creation.
 void dropColumnArg(FieldType fieldType, List<String> statementsBefore, List<String> statementsAfter)
          Takes a FieldType and adds the necessary statements to the before and after lists necessary so that the dropping of the table will succeed and will clear other associated sequences or other database artifacts
 String generateIdSequenceName(String tableName, FieldType idFieldType)
          Return the name of an ID sequence based on the tabelName and the fieldType of the id.
 String getCommentLinePrefix()
          Return the prefix to put at the front of a SQL line to mark it as a comment.
protected  int getDefaultVarcharWidth()
          Return the default varchar width if not specified by DatabaseField.width().
 FieldConverter getFieldConverter(FieldType fieldType)
          Return the FieldConverter to associate with the FieldType.
 boolean isCreateTableReturnsZero()
          Returns true if a 'CREATE TABLE' statement should return 0.
 boolean isEntityNamesMustBeUpCase()
          Returns true if table and field names should be made uppercase.
 boolean isIdSequenceNeeded()
          Return true if the database needs a sequence when you use generated IDs.
 boolean isLimitAfterSelect()
          Return true if the LIMIT should be called after SELECT otherwise at the end of the WHERE (the default).
 boolean isLimitSqlSupported()
          Return true if the database supports the LIMIT SQL command.
 boolean isVarcharFieldWidthSupported()
          Return true if the database supports the width parameter on VARCHAR fields.
 void loadDriver()
          Load the driver class associated with this database so it can wire itself into JDBC.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.j256.ormlite.db.DatabaseType
getDriverClassName, getDriverUrlPart
 

Field Detail

booleanConverter

protected static final FieldConverter booleanConverter
Constructor Detail

BaseDatabaseType

public BaseDatabaseType()
Method Detail

buildDatabaseAccess

public DatabaseAccess buildDatabaseAccess(DataSource dataSource)
Description copied from interface: DatabaseType
Return a newly built database access object which possibly is attached to the provided data source. Returns null if the dataSource was not set and it is required.

Specified by:
buildDatabaseAccess in interface DatabaseType

loadDriver

public void loadDriver()
                throws ClassNotFoundException
Description copied from interface: DatabaseType
Load the driver class associated with this database so it can wire itself into JDBC.

Specified by:
loadDriver in interface DatabaseType
Throws:
ClassNotFoundException - If the driver class is not available in the classpath.

appendColumnArg

public void appendColumnArg(StringBuilder sb,
                            FieldType fieldType,
                            List<String> additionalArgs,
                            List<String> statementsBefore,
                            List<String> statementsAfter,
                            List<String> queriesAfter)
Description copied from interface: DatabaseType
Takes a FieldType and appends the SQL necessary to create the field to the string builder. The field may also generate additional arguments which go at the end of the insert statement or additional statements to be executed before or afterwards depending on the configurations. The database can also add to the list of queries that will be performed afterward to test portions of the config.

Specified by:
appendColumnArg in interface DatabaseType

convertColumnName

public String convertColumnName(String columnName)
Description copied from interface: DatabaseType
Convert and return the column name for table and sequence creation. Often this is necessary to fix case issues.

Specified by:
convertColumnName in interface DatabaseType

appendStringType

protected void appendStringType(StringBuilder sb,
                                FieldType fieldType,
                                int fieldWidth)
Output the SQL type for a Java String.


appendDateType

protected void appendDateType(StringBuilder sb)
Output the SQL type for a Java Date.


appendBooleanType

protected void appendBooleanType(StringBuilder sb)
Output the SQL type for a Java boolean.


appendByteType

protected void appendByteType(StringBuilder sb)
Output the SQL type for a Java byte.


appendShortType

protected void appendShortType(StringBuilder sb)
Output the SQL type for a Java short.


appendIntegerType

protected void appendIntegerType(StringBuilder sb)
Output the SQL type for a Java integer.


appendLongType

protected void appendLongType(StringBuilder sb)
Output the SQL type for a Java long.


appendFloatType

protected void appendFloatType(StringBuilder sb)
Output the SQL type for a Java float.


appendDoubleType

protected void appendDoubleType(StringBuilder sb)
Output the SQL type for a Java double.


appendObjectType

protected void appendObjectType(StringBuilder sb)
Output the SQL type for a Java object.


appendEnumStringType

protected void appendEnumStringType(StringBuilder sb,
                                    FieldType fieldType)
Output the SQL type for a Enum object stored as String.


appendEnumIntType

protected void appendEnumIntType(StringBuilder sb,
                                 FieldType fieldType)
Output the SQL type for a Enum object stored as intg.


appendDefaultValue

protected void appendDefaultValue(StringBuilder sb,
                                  FieldType fieldType,
                                  Object defaultValue)
Output the SQL type for a Java boolean default value.


configureGeneratedIdSequence

protected void configureGeneratedIdSequence(StringBuilder sb,
                                            FieldType fieldType,
                                            List<String> statementsBefore,
                                            List<String> additionalArgs,
                                            List<String> queriesAfter)
Output the SQL necessary to configure a generated-id column. This may add to the before statements list or additional arguments later. NOTE: Only one of configureGeneratedIdSequence, configureGeneratedId, or configureId will be called.


configureGeneratedId

protected void configureGeneratedId(StringBuilder sb,
                                    FieldType fieldType,
                                    List<String> statementsBefore,
                                    List<String> additionalArgs,
                                    List<String> queriesAfter)
Output the SQL necessary to configure a generated-id column. This may add to the before statements list or additional arguments later. NOTE: Only one of configureGeneratedIdSequence, configureGeneratedId, or configureId will be called.


configureId

protected void configureId(StringBuilder sb,
                           FieldType fieldType,
                           List<String> statementsBefore,
                           List<String> additionalArgs,
                           List<String> queriesAfter)
Output the SQL necessary to configure an id column. This may add to the before statements list or additional arguments later. NOTE: Only one of configureGeneratedIdSequence, configureGeneratedId, or configureId will be called.


dropColumnArg

public void dropColumnArg(FieldType fieldType,
                          List<String> statementsBefore,
                          List<String> statementsAfter)
Description copied from interface: DatabaseType
Takes a FieldType and adds the necessary statements to the before and after lists necessary so that the dropping of the table will succeed and will clear other associated sequences or other database artifacts

Specified by:
dropColumnArg in interface DatabaseType

appendEscapedWord

public void appendEscapedWord(StringBuilder sb,
                              String word)
Description copied from interface: DatabaseType
Add the word to the string builder wrapped in the proper characters to escape it. This avoids problems with data values being reserved words.

Specified by:
appendEscapedWord in interface DatabaseType

appendEscapedEntityName

public void appendEscapedEntityName(StringBuilder sb,
                                    String word)
Description copied from interface: DatabaseType
Add a entity-name word to the string builder wrapped in the proper characters to escape it. This avoids problems with table, column, and sequence-names being reserved words.

Specified by:
appendEscapedEntityName in interface DatabaseType

generateIdSequenceName

public String generateIdSequenceName(String tableName,
                                     FieldType idFieldType)
Description copied from interface: DatabaseType
Return the name of an ID sequence based on the tabelName and the fieldType of the id.

Specified by:
generateIdSequenceName in interface DatabaseType

getCommentLinePrefix

public String getCommentLinePrefix()
Description copied from interface: DatabaseType
Return the prefix to put at the front of a SQL line to mark it as a comment.

Specified by:
getCommentLinePrefix in interface DatabaseType

getFieldConverter

public FieldConverter getFieldConverter(FieldType fieldType)
Description copied from interface: DatabaseType
Return the FieldConverter to associate with the FieldType. This allows the database instance to convert a field as necessary before it goes to the database.

Specified by:
getFieldConverter in interface DatabaseType

isIdSequenceNeeded

public boolean isIdSequenceNeeded()
Description copied from interface: DatabaseType
Return true if the database needs a sequence when you use generated IDs. Some databases (H2, MySQL) create them auto-magically. This also means that the database needs to query for a sequence value before the object is inserted. For old[er] versions of Postgres, for example, the JDBC call-back stuff to get the just-inserted id value does not work so we have to get the next sequence value by hand, assign it into the object, and then insert the object -- yes two SQL statements.

Specified by:
isIdSequenceNeeded in interface DatabaseType

isVarcharFieldWidthSupported

public boolean isVarcharFieldWidthSupported()
Description copied from interface: DatabaseType
Return true if the database supports the width parameter on VARCHAR fields.

Specified by:
isVarcharFieldWidthSupported in interface DatabaseType

isLimitSqlSupported

public boolean isLimitSqlSupported()
Description copied from interface: DatabaseType
Return true if the database supports the LIMIT SQL command. Otherwise we have to use the Statement.setMaxRows(int) instead. See prepareSqlStatement in MappedPreparedQuery.

Specified by:
isLimitSqlSupported in interface DatabaseType

isLimitAfterSelect

public boolean isLimitAfterSelect()
Description copied from interface: DatabaseType
Return true if the LIMIT should be called after SELECT otherwise at the end of the WHERE (the default).

Specified by:
isLimitAfterSelect in interface DatabaseType

appendLimitValue

public void appendLimitValue(StringBuilder sb,
                             int limit)
Description copied from interface: DatabaseType
Append to the string builder the necessary SQL to limit the results to a certain number.

Specified by:
appendLimitValue in interface DatabaseType

getDefaultVarcharWidth

protected int getDefaultVarcharWidth()
Return the default varchar width if not specified by DatabaseField.width().


appendSelectNextValFromSequence

public void appendSelectNextValFromSequence(StringBuilder sb,
                                            String sequenceName)
Description copied from interface: DatabaseType
Append the SQL necessary to get the next-value from a sequence. This is only necessary if DatabaseType.isIdSequenceNeeded() is true.

Specified by:
appendSelectNextValFromSequence in interface DatabaseType

appendCreateTableSuffix

public void appendCreateTableSuffix(StringBuilder sb)
Description copied from interface: DatabaseType
Append the SQL necessary to properly finish a CREATE TABLE line.

Specified by:
appendCreateTableSuffix in interface DatabaseType

isCreateTableReturnsZero

public boolean isCreateTableReturnsZero()
Description copied from interface: DatabaseType
Returns true if a 'CREATE TABLE' statement should return 0. False if > 0.

Specified by:
isCreateTableReturnsZero in interface DatabaseType

isEntityNamesMustBeUpCase

public boolean isEntityNamesMustBeUpCase()
Description copied from interface: DatabaseType
Returns true if table and field names should be made uppercase.

Turns out that Derby and Hsqldb are doing something wrong (IMO) with entity names. If you create a table with the name "footable" (with the quotes) then it will be created as lowercase footable, case sensitive. However, if you then issue the query 'select * from footable' it won't find the table because it gets promoted to be FOOTABLE and is searched in a case sensitive manner. So for these databases, entity names have to be forced to be uppercase so external queries will also work.

Specified by:
isEntityNamesMustBeUpCase in interface DatabaseType

appendCanBeNull

protected void appendCanBeNull(StringBuilder sb,
                               FieldType fieldType)
If the field can be nullable, do we need to add some sort of NULL SQL for the create table. By default it is a noop. This is necessary because MySQL has a auto default value for the TIMESTAMP type.



Copyright © 2010. All Rights Reserved.