001: //$Id: SAPDBDialect.java 8749 2005-12-04 17:32:04Z oneovthafew $
002: // contributed by Brad Clow
003: package org.hibernate.dialect;
004:
005: import java.sql.Types;
006:
007: import org.hibernate.Hibernate;
008: import org.hibernate.cfg.Environment;
009: import org.hibernate.dialect.function.NoArgSQLFunction;
010: import org.hibernate.dialect.function.StandardSQLFunction;
011: import org.hibernate.dialect.function.VarArgsSQLFunction;
012: import org.hibernate.sql.CaseFragment;
013: import org.hibernate.sql.DecodeCaseFragment;
014: import org.hibernate.sql.OracleJoinFragment;
015: import org.hibernate.sql.JoinFragment;
016: import org.hibernate.util.StringHelper;
017:
018: /**
019: * An SQL dialect compatible with SAP DB.
020: * @author Brad Clow
021: */
022: public class SAPDBDialect extends Dialect {
023:
024: public SAPDBDialect() {
025: super ();
026: registerColumnType(Types.BIT, "boolean");
027: registerColumnType(Types.BIGINT, "fixed(19,0)");
028: registerColumnType(Types.SMALLINT, "smallint");
029: registerColumnType(Types.TINYINT, "fixed(3,0)");
030: registerColumnType(Types.INTEGER, "int");
031: registerColumnType(Types.CHAR, "char(1)");
032: registerColumnType(Types.VARCHAR, "varchar($l)");
033: registerColumnType(Types.FLOAT, "float");
034: registerColumnType(Types.DOUBLE, "double precision");
035: registerColumnType(Types.DATE, "date");
036: registerColumnType(Types.TIME, "time");
037: registerColumnType(Types.TIMESTAMP, "timestamp");
038: registerColumnType(Types.VARBINARY, "long byte");
039: registerColumnType(Types.NUMERIC, "fixed($p,$s)");
040: registerColumnType(Types.CLOB, "long varchar");
041: registerColumnType(Types.BLOB, "long byte");
042:
043: registerFunction("abs", new StandardSQLFunction("abs"));
044: registerFunction("sign", new StandardSQLFunction("sign",
045: Hibernate.INTEGER));
046:
047: registerFunction("exp", new StandardSQLFunction("exp",
048: Hibernate.DOUBLE));
049: registerFunction("ln", new StandardSQLFunction("ln",
050: Hibernate.DOUBLE));
051: registerFunction("log", new StandardSQLFunction("ln",
052: Hibernate.DOUBLE));
053: registerFunction("pi", new NoArgSQLFunction("pi",
054: Hibernate.DOUBLE));
055: registerFunction("power", new StandardSQLFunction("power"));
056: registerFunction("acos", new StandardSQLFunction("acos",
057: Hibernate.DOUBLE));
058: registerFunction("asin", new StandardSQLFunction("asin",
059: Hibernate.DOUBLE));
060: registerFunction("atan", new StandardSQLFunction("atan",
061: Hibernate.DOUBLE));
062: registerFunction("cos", new StandardSQLFunction("cos",
063: Hibernate.DOUBLE));
064: registerFunction("cosh", new StandardSQLFunction("cosh",
065: Hibernate.DOUBLE));
066: registerFunction("cot", new StandardSQLFunction("cos",
067: Hibernate.DOUBLE));
068: registerFunction("sin", new StandardSQLFunction("sin",
069: Hibernate.DOUBLE));
070: registerFunction("sinh", new StandardSQLFunction("sinh",
071: Hibernate.DOUBLE));
072: registerFunction("tan", new StandardSQLFunction("tan",
073: Hibernate.DOUBLE));
074: registerFunction("tanh", new StandardSQLFunction("tanh",
075: Hibernate.DOUBLE));
076: registerFunction("radians", new StandardSQLFunction("radians",
077: Hibernate.DOUBLE));
078: registerFunction("degrees", new StandardSQLFunction("degrees",
079: Hibernate.DOUBLE));
080: registerFunction("atan2", new StandardSQLFunction("atan2",
081: Hibernate.DOUBLE));
082:
083: registerFunction("round", new StandardSQLFunction("round"));
084: registerFunction("trunc", new StandardSQLFunction("trunc"));
085: registerFunction("ceil", new StandardSQLFunction("ceil"));
086: registerFunction("floor", new StandardSQLFunction("floor"));
087: registerFunction("greatest",
088: new StandardSQLFunction("greatest"));
089: registerFunction("least", new StandardSQLFunction("least"));
090:
091: registerFunction("time", new StandardSQLFunction("time",
092: Hibernate.TIME));
093: registerFunction("timestamp", new StandardSQLFunction(
094: "timestamp", Hibernate.TIMESTAMP));
095: registerFunction("date", new StandardSQLFunction("date",
096: Hibernate.DATE));
097: registerFunction("microsecond", new StandardSQLFunction(
098: "microsecond", Hibernate.INTEGER));
099:
100: registerFunction("dayname", new StandardSQLFunction("dayname",
101: Hibernate.STRING));
102: registerFunction("monthname", new StandardSQLFunction(
103: "monthname", Hibernate.STRING));
104: registerFunction("dayofmonth", new StandardSQLFunction(
105: "dayofmonth", Hibernate.INTEGER));
106: registerFunction("dayofweek", new StandardSQLFunction(
107: "dayofweek", Hibernate.INTEGER));
108: registerFunction("dayofyear", new StandardSQLFunction(
109: "dayofyear", Hibernate.INTEGER));
110: registerFunction("weekofyear", new StandardSQLFunction(
111: "weekofyear", Hibernate.INTEGER));
112:
113: registerFunction("replace", new StandardSQLFunction("replace",
114: Hibernate.STRING));
115: registerFunction("translate", new StandardSQLFunction(
116: "translate", Hibernate.STRING));
117: registerFunction("lpad", new StandardSQLFunction("lpad",
118: Hibernate.STRING));
119: registerFunction("rpad", new StandardSQLFunction("rpad",
120: Hibernate.STRING));
121: registerFunction("substr", new StandardSQLFunction("substr",
122: Hibernate.STRING));
123: registerFunction("initcap", new StandardSQLFunction("initcap",
124: Hibernate.STRING));
125: registerFunction("lower", new StandardSQLFunction("lower",
126: Hibernate.STRING));
127: registerFunction("ltrim", new StandardSQLFunction("ltrim",
128: Hibernate.STRING));
129: registerFunction("rtrim", new StandardSQLFunction("rtrim",
130: Hibernate.STRING));
131: registerFunction("lfill", new StandardSQLFunction("ltrim",
132: Hibernate.STRING));
133: registerFunction("rfill", new StandardSQLFunction("rtrim",
134: Hibernate.STRING));
135: registerFunction("soundex", new StandardSQLFunction("soundex",
136: Hibernate.STRING));
137: registerFunction("upper", new StandardSQLFunction("upper",
138: Hibernate.STRING));
139: registerFunction("ascii", new StandardSQLFunction("ascii",
140: Hibernate.STRING));
141: registerFunction("index", new StandardSQLFunction("index",
142: Hibernate.INTEGER));
143:
144: registerFunction("value", new StandardSQLFunction("value"));
145:
146: registerFunction("concat", new VarArgsSQLFunction(
147: Hibernate.STRING, "(", "||", ")"));
148: registerFunction("substring", new StandardSQLFunction("substr",
149: Hibernate.STRING));
150: registerFunction("locate", new StandardSQLFunction("index",
151: Hibernate.INTEGER));
152: registerFunction("coalesce", new StandardSQLFunction("value"));
153:
154: getDefaultProperties().setProperty(
155: Environment.STATEMENT_BATCH_SIZE, DEFAULT_BATCH_SIZE);
156:
157: }
158:
159: public boolean dropConstraints() {
160: return false;
161: }
162:
163: public String getAddColumnString() {
164: return "add";
165: }
166:
167: public String getAddForeignKeyConstraintString(
168: String constraintName, String[] foreignKey,
169: String referencedTable, String[] primaryKey,
170: boolean referencesPrimaryKey) {
171: StringBuffer res = new StringBuffer(30).append(" foreign key ")
172: .append(constraintName).append(" (").append(
173: StringHelper.join(", ", foreignKey)).append(
174: ") references ").append(referencedTable);
175:
176: if (!referencesPrimaryKey) {
177: res.append(" (")
178: .append(StringHelper.join(", ", primaryKey))
179: .append(')');
180: }
181:
182: return res.toString();
183: }
184:
185: public String getAddPrimaryKeyConstraintString(String constraintName) {
186: return " primary key ";
187: }
188:
189: public String getNullColumnString() {
190: return " null";
191: }
192:
193: public String getSequenceNextValString(String sequenceName) {
194: return "select " + getSelectSequenceNextValString(sequenceName)
195: + " from dual";
196: }
197:
198: public String getSelectSequenceNextValString(String sequenceName) {
199: return sequenceName + ".nextval";
200: }
201:
202: public String getCreateSequenceString(String sequenceName) {
203: return "create sequence " + sequenceName;
204: }
205:
206: public String getDropSequenceString(String sequenceName) {
207: return "drop sequence " + sequenceName;
208: }
209:
210: public String getQuerySequencesString() {
211: return "select sequence_name from domain.sequences";
212: }
213:
214: public JoinFragment createOuterJoinFragment() {
215: return new OracleJoinFragment();
216: }
217:
218: public boolean supportsSequences() {
219: return true;
220: }
221:
222: public CaseFragment createCaseFragment() {
223: return new DecodeCaseFragment();
224: }
225:
226: public boolean supportsTemporaryTables() {
227: return true;
228: }
229:
230: public String getCreateTemporaryTablePostfix() {
231: return "ignore rollback";
232: }
233:
234: public String generateTemporaryTableName(String baseTableName) {
235: return "temp."
236: + super.generateTemporaryTableName(baseTableName);
237: }
238:
239: }
|