📄 dialect.java
字号:
//$Id: Dialect.java,v 1.36 2005/04/11 12:39:50 maxcsaucdk Exp $package org.hibernate.dialect;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.hibernate.Hibernate;import org.hibernate.HibernateException;import org.hibernate.LockMode;import org.hibernate.MappingException;import org.hibernate.QueryException;import org.hibernate.cfg.Environment;import org.hibernate.dialect.function.SQLFunction;import org.hibernate.dialect.function.SQLFunctionTemplate;import org.hibernate.dialect.function.StandardSQLFunction;import org.hibernate.engine.Mapping;import org.hibernate.exception.SQLExceptionConverter;import org.hibernate.exception.SQLStateConverter;import org.hibernate.exception.ViolatedConstraintNameExtracter;import org.hibernate.id.IdentityGenerator;import org.hibernate.id.SequenceGenerator;import org.hibernate.id.TableHiLoGenerator;import org.hibernate.sql.ANSICaseFragment;import org.hibernate.sql.ANSIJoinFragment;import org.hibernate.sql.CaseFragment;import org.hibernate.sql.JoinFragment;import org.hibernate.type.Type;import org.hibernate.util.ReflectHelper;import org.hibernate.util.StringHelper;import java.sql.CallableStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Types;import java.util.HashMap;import java.util.Map;import java.util.Properties;/** * Represents a dialect of SQL implemented by a particular RDBMS. * Subclasses implement Hibernate compatibility with different systems.<br> * <br> * Subclasses should provide a public default constructor that <tt>register()</tt> * a set of type mappings and default Hibernate properties.<br> * <br> * Subclasses should be immutable. * * @author Gavin King, David Channon */public abstract class Dialect { private static final Log log = LogFactory.getLog( Dialect.class ); static final String DEFAULT_BATCH_SIZE = "15"; static final String NO_BATCH = "0"; private static final Map STANDARD_AGGREGATE_FUNCTIONS = new HashMap(); static { STANDARD_AGGREGATE_FUNCTIONS.put( "count", new StandardSQLFunction("count") { public Type getReturnType(Type columnType, Mapping mapping) { return Hibernate.INTEGER; } } ); STANDARD_AGGREGATE_FUNCTIONS.put( "avg", new StandardSQLFunction("avg") { public Type getReturnType(Type columnType, Mapping mapping) throws QueryException { int[] sqlTypes; try { sqlTypes = columnType.sqlTypes( mapping ); } catch ( MappingException me ) { throw new QueryException( me ); } if ( sqlTypes.length != 1 ) throw new QueryException( "multi-column type in avg()" ); int sqlType = sqlTypes[0]; if ( sqlType == Types.INTEGER || sqlType == Types.BIGINT || sqlType == Types.TINYINT ) { return Hibernate.FLOAT; } else { return columnType; } } } ); STANDARD_AGGREGATE_FUNCTIONS.put( "max", new StandardSQLFunction("max") ); STANDARD_AGGREGATE_FUNCTIONS.put( "min", new StandardSQLFunction("min") ); STANDARD_AGGREGATE_FUNCTIONS.put( "sum", new StandardSQLFunction("sum") ); } protected Dialect() { log.info( "Using dialect: " + this ); sqlFunctions.putAll( STANDARD_AGGREGATE_FUNCTIONS ); // standard sql92 functions registerFunction( "substring", new SQLFunctionTemplate( Hibernate.STRING, "substring(?1, ?2, ?3)" ) ); registerFunction( "length", new SQLFunctionTemplate( Hibernate.INTEGER, "length(?1)" ) ); registerFunction( "locate", new SQLFunctionTemplate( Hibernate.INTEGER, "locate(?1, ?2, ?3)" ) ); registerFunction( "abs", new SQLFunctionTemplate( Hibernate.DOUBLE, "abs(?1)" ) ); registerFunction( "sqrt", new SQLFunctionTemplate( Hibernate.DOUBLE, "sqrt(?1)" ) ); registerFunction( "trim", new SQLFunctionTemplate( Hibernate.STRING, "trim(?1 ?2 ?3 ?4)" ) ); registerFunction( "bit_length", new SQLFunctionTemplate( Hibernate.INTEGER, "BIT_LENGTH(?1)" ) ); } public String toString() { return getClass().getName(); } private final TypeNames typeNames = new TypeNames(); private final Properties properties = new Properties(); private final Map sqlFunctions = new HashMap(); /** * Characters used for quoting SQL identifiers */ public static final String QUOTE = "`\"["; public static final String CLOSED_QUOTE = "`\"]"; /** * Get the name of the database type associated with the given * <tt>java.sql.Types</tt> typecode. * * @param code <tt>java.sql.Types</tt> typecode * @return the database type name * @throws HibernateException */ public String getTypeName(int code) throws HibernateException { String result = typeNames.get( code ); if ( result == null ) { throw new HibernateException( "No default type mapping for (java.sql.Types) " + code ); } return result; } /** * Get the name of the database type associated with the given * <tt>java.sql.Types</tt> typecode. * @param code <tt>java.sql.Types</tt> typecode * @param length the length or precision of the column * @param precision the precision of the column * @param scale the scale of the column * * @return the database type name * @throws HibernateException */ public String getTypeName(int code, int length, int precision, int scale) throws HibernateException { String result = typeNames.get( code, length, precision, scale ); if ( result == null ) { throw new HibernateException( "No type mapping for java.sql.Types code: " + code + ", length: " + length ); } return result; } protected void registerFunction(String name, SQLFunction function) { sqlFunctions.put( name, function ); } /** * Subclasses register a typename for the given type code and maximum * column length. <tt>$l</tt> in the type name with be replaced by the * column length (if appropriate). * * @param code <tt>java.sql.Types</tt> typecode * @param capacity maximum length of database type * @param name the database type name */ protected void registerColumnType(int code, int capacity, String name) { typeNames.put( code, capacity, name ); } /** * Subclasses register a typename for the given type code. <tt>$l</tt> in * the type name with be replaced by the column length (if appropriate). * * @param code <tt>java.sql.Types</tt> typecode * @param name the database type name */ protected void registerColumnType(int code, String name) { typeNames.put( code, name ); } /** * Does this dialect support the <tt>ALTER TABLE</tt> syntax? * * @return boolean */ public boolean hasAlterTable() { return true; } /** * Do we need to drop constraints before dropping tables in this dialect? * * @return boolean */ public boolean dropConstraints() { return true; } /** * Do we need to qualify index names with the schema name? * * @return boolean */ public boolean qualifyIndexName() { return true; } /** * Does the <tt>FOR UPDATE OF</tt> syntax specify particular * columns? */ public boolean forUpdateOfColumns() { return false; } /** * Does this dialect support the <tt>FOR UPDATE OF</tt> syntax? * * @return boolean */ public String getForUpdateString(String aliases) { return getForUpdateString(); } /** * Does this dialect support the Oracle-style <tt>FOR UPDATE OF ... NOWAIT</tt> * syntax? * * @return boolean */ public String getForUpdateNowaitString(String aliases) { return getForUpdateString( aliases ); } /** * Does this dialect support the <tt>FOR UPDATE</tt> syntax? * * @return boolean */ public String getForUpdateString() { return " for update"; } /** * Does this dialect support the Oracle-style <tt>FOR UPDATE NOWAIT</tt> syntax? * * @return boolean */ public String getForUpdateNowaitString() { return getForUpdateString(); } /** * Does this dialect support the <tt>UNIQUE</tt> column syntax? * * @return boolean */ public boolean supportsUnique() { return true; } /** * Does this dialect support adding Unique constraints via create and alter table ? * @return boolean */ public boolean supportsUniqueConstraintInCreateAlterTable() { return true; } /** * The syntax used to add a column to a table (optional). */ public String getAddColumnString() { throw new UnsupportedOperationException( "No add column syntax supported by Dialect" ); } public String getDropForeignKeyString() { return " drop constraint "; } public String getTableTypeString() { return ""; } /** * The syntax used to add a foreign key constraint to a table. * * @return String */ public String getAddForeignKeyConstraintString(String constraintName, String[] foreignKey, String referencedTable, String[] primaryKey) { return new StringBuffer( 30 ) .append( " add constraint " ) .append( constraintName ) .append( " foreign key (" ) .append( StringHelper.join( ", ", foreignKey ) ) .append( ") references " ) .append( referencedTable ) .toString(); } /** * The syntax used to add a primary key constraint to a table. * * @return String */ public String getAddPrimaryKeyConstraintString(String constraintName) { return " add constraint " + constraintName + " primary key "; } /** * The keyword used to specify a nullable column. * * @return String */ public String getNullColumnString() { return ""; } /** * Does this dialect support identity column key generation? * * @return boolean */ public boolean supportsIdentityColumns() { return false; } /** * Does this dialect support sequences? * * @return boolean */ public boolean supportsSequences() { return false; } public boolean supportsInsertSelectIdentity() { return false; } /** * Append a clause to retrieve the generated identity value for the * given <tt>INSERT</tt> statement. */ public String appendIdentitySelectToInsert(String insertString) { return insertString; } protected String getIdentitySelectString() throws MappingException { throw new MappingException( "Dialect does not support identity key generation" ); } /** * The syntax that returns the identity value of the last insert, if * identity column key generation is supported. * * @param type TODO * @throws MappingException if no native key generation */ public String getIdentitySelectString(String table, String column, int type) throws MappingException { return getIdentitySelectString(); } protected String getIdentityColumnString() throws MappingException { throw new MappingException( "Dialect does not support identity key generation" ); } /** * The keyword used to specify an identity column, if identity * column key generation is supported. * * @param type the SQL column type, as defined by <tt>java.sql.Types</tt> * @throws MappingException if no native key generation */ public String getIdentityColumnString(int type) throws MappingException { return getIdentityColumnString(); } /** * The keyword used to insert a generated value into an identity column (or null) * * @return String */ public String getIdentityInsertString() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -