KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > repository > MySQL5Dialect


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * Created Nov 14, 2005
14  * @author mbatchel
15  */

16 package org.pentaho.repository;
17
18 import java.sql.Types JavaDoc;
19 import org.hibernate.Hibernate;
20 import org.hibernate.cfg.Environment;
21 import org.hibernate.dialect.Dialect;
22 import org.hibernate.dialect.function.NoArgSQLFunction;
23 import org.hibernate.dialect.function.StandardSQLFunction;
24 import org.hibernate.util.StringHelper;
25
26 public class MySQL5Dialect extends Dialect {
27
28     static final String JavaDoc DEFAULT_BATCH_SIZE = "15"; //$NON-NLS-1$
29

30     public MySQL5Dialect() {
31         super();
32         registerColumnType(Types.BIT, "bit"); //$NON-NLS-1$
33
registerColumnType(Types.BIGINT, "bigint"); //$NON-NLS-1$
34
registerColumnType(Types.SMALLINT, "smallint"); //$NON-NLS-1$
35
registerColumnType(Types.TINYINT, "tinyint"); //$NON-NLS-1$
36
registerColumnType(Types.INTEGER, "integer"); //$NON-NLS-1$
37
registerColumnType(Types.CHAR, "char(1)"); //$NON-NLS-1$
38
registerColumnType(Types.VARCHAR, "longtext"); //$NON-NLS-1$
39
registerColumnType(Types.VARCHAR, 16777215, "mediumtext"); //$NON-NLS-1$
40
registerColumnType(Types.VARCHAR, 65535, "varchar($l)"); //$NON-NLS-1$
41
registerColumnType(Types.FLOAT, "float"); //$NON-NLS-1$
42
registerColumnType(Types.DOUBLE, "double precision"); //$NON-NLS-1$
43
registerColumnType(Types.DATE, "date"); //$NON-NLS-1$
44
registerColumnType(Types.TIME, "time"); //$NON-NLS-1$
45
registerColumnType(Types.TIMESTAMP, "datetime"); //$NON-NLS-1$
46
registerColumnType(Types.VARBINARY, "longblob"); //$NON-NLS-1$
47
registerColumnType(Types.VARBINARY, 16777215, "mediumblob"); //$NON-NLS-1$
48
registerColumnType(Types.VARBINARY, 65535, "blob"); //$NON-NLS-1$
49
registerColumnType(Types.VARBINARY, 255, "tinyblob"); //$NON-NLS-1$
50
registerColumnType(Types.NUMERIC, "numeric($p,$s)"); //$NON-NLS-1$
51
registerColumnType(Types.BLOB, "longblob"); //$NON-NLS-1$
52
registerColumnType(Types.BLOB, 16777215, "mediumblob"); //$NON-NLS-1$
53
registerColumnType(Types.BLOB, 65535, "blob"); //$NON-NLS-1$
54
registerColumnType(Types.CLOB, "longtext"); //$NON-NLS-1$
55
registerColumnType(Types.CLOB, 16777215, "mediumtext"); //$NON-NLS-1$
56
registerColumnType(Types.CLOB, 65535, "text"); //$NON-NLS-1$
57

58         registerFunction("ascii", new StandardSQLFunction("ascii", Hibernate.INTEGER)); //$NON-NLS-1$ //$NON-NLS-2$
59
registerFunction("bin", new StandardSQLFunction("bin", Hibernate.STRING)); //$NON-NLS-1$ //$NON-NLS-2$
60
registerFunction("char_length", new StandardSQLFunction("char_length", Hibernate.LONG)); //$NON-NLS-1$ //$NON-NLS-2$
61
registerFunction("character_length", new StandardSQLFunction("character_length", Hibernate.LONG)); //$NON-NLS-1$ //$NON-NLS-2$
62
registerFunction("lcase", new StandardSQLFunction("lcase")); //$NON-NLS-1$ //$NON-NLS-2$
63
registerFunction("lower", new StandardSQLFunction("lower")); //$NON-NLS-1$ //$NON-NLS-2$
64
registerFunction("length", new StandardSQLFunction("length", Hibernate.LONG)); //$NON-NLS-1$ //$NON-NLS-2$
65
registerFunction("ltrim", new StandardSQLFunction("ltrim")); //$NON-NLS-1$ //$NON-NLS-2$
66
registerFunction("ord", new StandardSQLFunction("ord", Hibernate.INTEGER)); //$NON-NLS-1$ //$NON-NLS-2$
67
registerFunction("quote", new StandardSQLFunction("quote")); //$NON-NLS-1$ //$NON-NLS-2$
68
registerFunction("reverse", new StandardSQLFunction("reverse")); //$NON-NLS-1$ //$NON-NLS-2$
69
registerFunction("rtrim", new StandardSQLFunction("rtrim")); //$NON-NLS-1$ //$NON-NLS-2$
70
registerFunction("soundex", new StandardSQLFunction("soundex")); //$NON-NLS-1$ //$NON-NLS-2$
71
registerFunction("space", new StandardSQLFunction("space", Hibernate.STRING)); //$NON-NLS-1$ //$NON-NLS-2$
72
registerFunction("ucase", new StandardSQLFunction("ucase")); //$NON-NLS-1$ //$NON-NLS-2$
73
registerFunction("upper", new StandardSQLFunction("upper")); //$NON-NLS-1$ //$NON-NLS-2$
74
registerFunction("unhex", new StandardSQLFunction("unhex", Hibernate.STRING)); //$NON-NLS-1$ //$NON-NLS-2$
75

76         registerFunction("abs", new StandardSQLFunction("abs")); //$NON-NLS-1$ //$NON-NLS-2$
77
registerFunction("sign", new StandardSQLFunction("sign", Hibernate.INTEGER)); //$NON-NLS-1$ //$NON-NLS-2$
78

79         registerFunction("acos", new StandardSQLFunction("acos", Hibernate.DOUBLE)); //$NON-NLS-1$ //$NON-NLS-2$
80
registerFunction("asin", new StandardSQLFunction("asin", Hibernate.DOUBLE)); //$NON-NLS-1$ //$NON-NLS-2$
81
registerFunction("atan", new StandardSQLFunction("atan", Hibernate.DOUBLE)); //$NON-NLS-1$ //$NON-NLS-2$
82
registerFunction("cos", new StandardSQLFunction("cos", Hibernate.DOUBLE)); //$NON-NLS-1$ //$NON-NLS-2$
83
registerFunction("cot", new StandardSQLFunction("cot", Hibernate.DOUBLE)); //$NON-NLS-1$ //$NON-NLS-2$
84
registerFunction("crc32", new StandardSQLFunction("crc32", Hibernate.LONG)); //$NON-NLS-1$ //$NON-NLS-2$
85
registerFunction("exp", new StandardSQLFunction("exp", Hibernate.DOUBLE)); //$NON-NLS-1$ //$NON-NLS-2$
86
registerFunction("ln", new StandardSQLFunction("ln", Hibernate.DOUBLE)); //$NON-NLS-1$ //$NON-NLS-2$
87
registerFunction("log", new StandardSQLFunction("log", Hibernate.DOUBLE)); //$NON-NLS-1$ //$NON-NLS-2$
88
registerFunction("log2", new StandardSQLFunction("log2", Hibernate.DOUBLE)); //$NON-NLS-1$ //$NON-NLS-2$
89
registerFunction("log10", new StandardSQLFunction("log10", Hibernate.DOUBLE)); //$NON-NLS-1$ //$NON-NLS-2$
90
registerFunction("pi", new NoArgSQLFunction("pi", Hibernate.DOUBLE)); //$NON-NLS-1$ //$NON-NLS-2$
91
registerFunction("rand", new NoArgSQLFunction("rand", Hibernate.DOUBLE)); //$NON-NLS-1$ //$NON-NLS-2$
92
registerFunction("sin", new StandardSQLFunction("sin", Hibernate.DOUBLE)); //$NON-NLS-1$ //$NON-NLS-2$
93
registerFunction("sqrt", new StandardSQLFunction("sqrt", Hibernate.DOUBLE)); //$NON-NLS-1$ //$NON-NLS-2$
94
registerFunction("tan", new StandardSQLFunction("tan", Hibernate.DOUBLE)); //$NON-NLS-1$ //$NON-NLS-2$
95

96         registerFunction("radians", new StandardSQLFunction("radians", Hibernate.DOUBLE)); //$NON-NLS-1$ //$NON-NLS-2$
97
registerFunction("degrees", new StandardSQLFunction("degrees", Hibernate.DOUBLE)); //$NON-NLS-1$ //$NON-NLS-2$
98

99         registerFunction("ceiling", new StandardSQLFunction("ceiling", Hibernate.INTEGER)); //$NON-NLS-1$ //$NON-NLS-2$
100
registerFunction("ceil", new StandardSQLFunction("ceil", Hibernate.INTEGER)); //$NON-NLS-1$ //$NON-NLS-2$
101
registerFunction("floor", new StandardSQLFunction("floor", Hibernate.INTEGER)); //$NON-NLS-1$ //$NON-NLS-2$
102
registerFunction("round", new StandardSQLFunction("round", Hibernate.INTEGER)); //$NON-NLS-1$ //$NON-NLS-2$
103

104         registerFunction("curdate", new NoArgSQLFunction("curdate", Hibernate.DATE)); //$NON-NLS-1$ //$NON-NLS-2$
105
registerFunction("curtime", new NoArgSQLFunction("curtime", Hibernate.TIME)); //$NON-NLS-1$ //$NON-NLS-2$
106
registerFunction("current_date", new NoArgSQLFunction("current_date", Hibernate.DATE, false)); //$NON-NLS-1$ //$NON-NLS-2$
107
registerFunction("current_time", new NoArgSQLFunction("current_time", Hibernate.TIME, false)); //$NON-NLS-1$ //$NON-NLS-2$
108
registerFunction("current_timestamp", new NoArgSQLFunction("current_timestamp", Hibernate.TIMESTAMP, false)); //$NON-NLS-1$ //$NON-NLS-2$
109
registerFunction("date", new StandardSQLFunction("date", Hibernate.DATE)); //$NON-NLS-1$ //$NON-NLS-2$
110
registerFunction("day", new StandardSQLFunction("day", Hibernate.INTEGER)); //$NON-NLS-1$ //$NON-NLS-2$
111
registerFunction("dayofmonth", new StandardSQLFunction("dayofmonth", Hibernate.INTEGER)); //$NON-NLS-1$ //$NON-NLS-2$
112
registerFunction("dayname", new StandardSQLFunction("dayname", Hibernate.STRING)); //$NON-NLS-1$ //$NON-NLS-2$
113
registerFunction("dayofweek", new StandardSQLFunction("dayofweek", Hibernate.INTEGER)); //$NON-NLS-1$ //$NON-NLS-2$
114
registerFunction("dayofyear", new StandardSQLFunction("dayofyear", Hibernate.INTEGER)); //$NON-NLS-1$ //$NON-NLS-2$
115
registerFunction("from_days", new StandardSQLFunction("from_days", Hibernate.DATE)); //$NON-NLS-1$ //$NON-NLS-2$
116
registerFunction("from_unixtime", new StandardSQLFunction("from_unixtime", Hibernate.TIMESTAMP)); //$NON-NLS-1$ //$NON-NLS-2$
117
registerFunction("hour", new StandardSQLFunction("hour", Hibernate.INTEGER)); //$NON-NLS-1$ //$NON-NLS-2$
118
registerFunction("last_day", new StandardSQLFunction("last_day", Hibernate.DATE)); //$NON-NLS-1$ //$NON-NLS-2$
119
registerFunction("localtime", new NoArgSQLFunction("localtime", Hibernate.TIMESTAMP)); //$NON-NLS-1$ //$NON-NLS-2$
120
registerFunction("localtimestamp", new NoArgSQLFunction("localtimestamp", Hibernate.TIMESTAMP)); //$NON-NLS-1$ //$NON-NLS-2$
121
registerFunction("microseconds", new StandardSQLFunction("microseconds", Hibernate.INTEGER)); //$NON-NLS-1$ //$NON-NLS-2$
122
registerFunction("minute", new StandardSQLFunction("minute", Hibernate.INTEGER)); //$NON-NLS-1$ //$NON-NLS-2$
123
registerFunction("month", new StandardSQLFunction("month", Hibernate.INTEGER)); //$NON-NLS-1$ //$NON-NLS-2$
124
registerFunction("monthname", new StandardSQLFunction("monthname", Hibernate.STRING)); //$NON-NLS-1$ //$NON-NLS-2$
125
registerFunction("now", new NoArgSQLFunction("now", Hibernate.TIMESTAMP)); //$NON-NLS-1$ //$NON-NLS-2$
126
registerFunction("quarter", new StandardSQLFunction("quarter", Hibernate.INTEGER)); //$NON-NLS-1$ //$NON-NLS-2$
127
registerFunction("second", new StandardSQLFunction("second", Hibernate.INTEGER)); //$NON-NLS-1$ //$NON-NLS-2$
128
registerFunction("sec_to_time", new StandardSQLFunction("sec_to_time", Hibernate.TIME)); //$NON-NLS-1$ //$NON-NLS-2$
129
registerFunction("sysdate", new NoArgSQLFunction("sysdate", Hibernate.TIMESTAMP)); //$NON-NLS-1$ //$NON-NLS-2$
130
registerFunction("time", new StandardSQLFunction("time", Hibernate.TIME)); //$NON-NLS-1$ //$NON-NLS-2$
131
registerFunction("timestamp", new StandardSQLFunction("timestamp", Hibernate.TIMESTAMP)); //$NON-NLS-1$ //$NON-NLS-2$
132
registerFunction("time_to_sec", new StandardSQLFunction("time_to_sec", Hibernate.INTEGER)); //$NON-NLS-1$ //$NON-NLS-2$
133
registerFunction("to_days", new StandardSQLFunction("to_days", Hibernate.LONG)); //$NON-NLS-1$ //$NON-NLS-2$
134
registerFunction("unix_timestamp", new StandardSQLFunction("unix_timestamp", Hibernate.LONG)); //$NON-NLS-1$ //$NON-NLS-2$
135
registerFunction("utc_date", new NoArgSQLFunction("utc_date", Hibernate.STRING)); //$NON-NLS-1$ //$NON-NLS-2$
136
registerFunction("utc_time", new NoArgSQLFunction("utc_time", Hibernate.STRING)); //$NON-NLS-1$ //$NON-NLS-2$
137
registerFunction("utc_timestamp", new NoArgSQLFunction("utc_timestamp", Hibernate.STRING)); //$NON-NLS-1$ //$NON-NLS-2$
138
registerFunction("week", new StandardSQLFunction("week", Hibernate.INTEGER)); //$NON-NLS-1$ //$NON-NLS-2$
139
registerFunction("weekday", new StandardSQLFunction("weekday", Hibernate.INTEGER)); //$NON-NLS-1$ //$NON-NLS-2$
140
registerFunction("weekofyear", new StandardSQLFunction("weekofyear", Hibernate.INTEGER)); //$NON-NLS-1$ //$NON-NLS-2$
141
registerFunction("year", new StandardSQLFunction("year", Hibernate.INTEGER)); //$NON-NLS-1$ //$NON-NLS-2$
142
registerFunction("yearweek", new StandardSQLFunction("yearweek", Hibernate.INTEGER)); //$NON-NLS-1$ //$NON-NLS-2$
143

144         registerFunction("hex", new StandardSQLFunction("hex", Hibernate.STRING)); //$NON-NLS-1$ //$NON-NLS-2$
145
registerFunction("oct", new StandardSQLFunction("oct", Hibernate.STRING)); //$NON-NLS-1$ //$NON-NLS-2$
146

147         registerFunction("octet_length", new StandardSQLFunction("octet_length", Hibernate.LONG)); //$NON-NLS-1$ //$NON-NLS-2$
148
registerFunction("bit_length", new StandardSQLFunction("bit_length", Hibernate.LONG)); //$NON-NLS-1$ //$NON-NLS-2$
149

150         registerFunction("bit_count", new StandardSQLFunction("bit_count", Hibernate.LONG)); //$NON-NLS-1$ //$NON-NLS-2$
151
registerFunction("encrypt", new StandardSQLFunction("encrypt", Hibernate.STRING)); //$NON-NLS-1$ //$NON-NLS-2$
152
registerFunction("md5", new StandardSQLFunction("md5", Hibernate.STRING)); //$NON-NLS-1$ //$NON-NLS-2$
153
registerFunction("sha1", new StandardSQLFunction("sha1", Hibernate.STRING)); //$NON-NLS-1$ //$NON-NLS-2$
154
registerFunction("sha", new StandardSQLFunction("sha", Hibernate.STRING)); //$NON-NLS-1$ //$NON-NLS-2$
155

156         registerFunction("concat", new StandardSQLFunction("concat", Hibernate.STRING)); //$NON-NLS-1$ //$NON-NLS-2$
157

158         getDefaultProperties().setProperty(Environment.MAX_FETCH_DEPTH, "2"); //$NON-NLS-1$
159
getDefaultProperties().setProperty(Environment.STATEMENT_BATCH_SIZE, DEFAULT_BATCH_SIZE);
160     }
161
162     public String JavaDoc getAddColumnString() {
163         return "add column"; //$NON-NLS-1$
164
}
165
166     public boolean qualifyIndexName() {
167         return false;
168     }
169
170     public boolean supportsIdentityColumns() {
171         return true;
172     }
173
174     public String JavaDoc getIdentitySelectString() {
175         return "select last_insert_id()"; //$NON-NLS-1$
176
}
177
178     public String JavaDoc getIdentityColumnString() {
179         return "not null auto_increment"; //$NON-NLS-1$
180
}
181
182     public String JavaDoc getAddForeignKeyConstraintString(String JavaDoc constraintName, String JavaDoc foreignKey[], String JavaDoc referencedTable, String JavaDoc primaryKey[], boolean referencesPrimaryKey) {
183         String JavaDoc cols = StringHelper.join(", ", foreignKey); //$NON-NLS-1$
184
return (new StringBuffer JavaDoc(30))
185                 .append(" add index ").append(constraintName).append(" (").append(cols).append("), add constraint ").append(constraintName).append(" foreign key (").append(cols).append(") references ").append(referencedTable).append(" (").append(StringHelper.join(", ", primaryKey)).append(')').toString(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$
186
}
187
188     public boolean supportsLimit() {
189         return true;
190     }
191
192     public String JavaDoc getDropForeignKeyString() {
193         return " drop foreign key "; //$NON-NLS-1$
194
}
195
196     public String JavaDoc getLimitString(String JavaDoc sql, boolean hasOffset) {
197         return (new StringBuffer JavaDoc(sql.length() + 20)).append(sql).append(hasOffset ? " limit ?, ?" : " limit ?").toString(); //$NON-NLS-1$ //$NON-NLS-2$
198
}
199
200     public char closeQuote() {
201         return '`';
202     }
203
204     public char openQuote() {
205         return '`';
206     }
207
208     public boolean supportsIfExistsBeforeTableName() {
209         return true;
210     }
211
212     public char getSchemaSeparator() {
213         return '_';
214     }
215
216     public String JavaDoc getSelectGUIDString() {
217         return "select uuid()"; //$NON-NLS-1$
218
}
219
220     public boolean supportsCascadeDelete() {
221         return false;
222     }
223
224     public String JavaDoc getTableComment(String JavaDoc comment) {
225         return " comment='" + comment + "'"; //$NON-NLS-1$ //$NON-NLS-2$
226
}
227
228     public String JavaDoc getColumnComment(String JavaDoc comment) {
229         return " comment '" + comment + "'"; //$NON-NLS-1$ //$NON-NLS-2$
230
}
231
232     public boolean supportsTemporaryTables() {
233         return true;
234     }
235
236     public String JavaDoc getTemporaryTableCreationCommand() {
237         return "create temporary table if not exists"; //$NON-NLS-1$
238
}
239
240     public String JavaDoc getCastTypeName(int code) {
241         if (code == 4)
242             return "signed"; //$NON-NLS-1$
243
else
244             return super.getCastTypeName(code);
245     }
246
247     public boolean supportsCurrentTimestampSelection() {
248         return true;
249     }
250
251     public boolean isCurrentTimestampSelectStringCallable() {
252         return false;
253     }
254
255     public String JavaDoc getCurrentTimestampSelectString() {
256         return "select now()"; //$NON-NLS-1$
257
}
258 }
259
Popular Tags