1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 package org.melati.poem.dbms;
46
47 import java.sql.Connection;
48 import java.sql.Statement;
49 import java.sql.SQLException;
50
51 import org.melati.poem.BinaryPoemType;
52 import org.melati.poem.Column;
53 import org.melati.poem.PoemType;
54 import org.melati.poem.StringPoemType;
55 import org.melati.poem.Table;
56 import org.melati.poem.util.StringUtils;
57
58
59
60
61
62
63
64
65 public class Hsqldb extends AnsiStandard {
66
67
68
69
70
71
72
73
74
75
76 public static int hsqldbTextHack = 266;
77
78 public static int hsqldbTextSize1 = 1048576;
79 public static int hsqldbTextSize2 = 16777216;
80
81
82
83
84 public Hsqldb() {
85 setDriverClassName("org.hsqldb.jdbcDriver");
86 }
87
88
89
90
91 @Override
92 public void shutdown(Connection connection) throws SQLException {
93 if (!connection.isClosed()) {
94 Statement st = connection.createStatement();
95 try {
96 st.execute("SHUTDOWN SCRIPT");
97 } catch (SQLException e) {
98
99 if (!e.getMessage().equals("Access is denied: Session is closed"))
100 throw e;
101 } finally {
102 st.close();
103 }
104 }
105 }
106
107
108
109
110
111
112 @Override
113 public String createTableTypeQualifierSql(Table<?> table) {
114 String tableType;
115 if (table == null || table.getDbmsTableType() == null)
116 tableType = "CACHED ";
117 else
118 tableType = table.getDbmsTableType() + " ";
119
120 return tableType;
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141 @Override
142 public String getStringSqlDefinition(int size) {
143 if (size < 0)
144 return "LONGVARCHAR";
145
146 return "VARCHAR(" + size + ")";
147 }
148
149
150
151
152
153 @Override
154 public String getLongSqlDefinition() {
155 return "BIGINT";
156 }
157
158
159
160
161
162 @Override
163 public String getBinarySqlDefinition(int size) throws SQLException {
164 return "LONGVARBINARY";
165 }
166
167
168
169
170
171
172 @Override
173 public <S,O>PoemType<O> canRepresent(PoemType<S> storage, PoemType<O> type) {
174 if (storage instanceof StringPoemType && type instanceof StringPoemType) {
175 if (
176 (((StringPoemType)storage).getSize() == hsqldbTextHack
177 ||
178 ((StringPoemType)storage).getSize() == hsqldbTextSize1
179 ||
180 ((StringPoemType)storage).getSize() == hsqldbTextSize2
181 )
182 && ((StringPoemType)type).getSize() == -1
183 && !(!storage.getNullable() && type.getNullable())
184 ) {
185 return type;
186 } else {
187 return storage.canRepresent(type);
188 }
189 } else if (storage instanceof BinaryPoemType && type instanceof BinaryPoemType) {
190 if (
191 (
192 (((BinaryPoemType)storage).getSize() == 0)
193 ||
194 (((BinaryPoemType)storage).getSize() > ((BinaryPoemType)type).getSize())
195 )
196 &&
197 !(!storage.getNullable() && type.getNullable())
198 ) {
199 return type;
200 } else {
201 return storage.canRepresent(type);
202 }
203 } else {
204 return super.canRepresent(storage, type);
205 }
206 }
207
208
209
210
211
212 @Override
213 public String unreservedName(String name) {
214 if(name.equalsIgnoreCase("UNIQUE")) name = "MELATI_" + name.toUpperCase();
215 if(name.equalsIgnoreCase("CONSTRAINT")) name = "MELATI_" + name.toUpperCase();
216
217 return name.toUpperCase();
218 }
219
220
221
222
223
224 @Override
225 public String melatiName(String name) {
226 if (name == null) return name;
227 if(name.equalsIgnoreCase("MELATI_UNIQUE")) name = "UNIQUE";
228 if(name.equalsIgnoreCase("MELATI_CONSTRAINT")) name = "CONSTRAINT";
229
230 return name.toLowerCase();
231 }
232
233
234
235
236
237
238
239
240 @Override
241 public String caseInsensitiveRegExpSQL(String term1, String term2) {
242 if (StringUtils.isQuoted(term2)) {
243 term2 = term2.substring(1, term2.length() - 1);
244 }
245 term2 = StringUtils.quoted(StringUtils.quoted(term2, '%'), '\'');
246
247 return term1 + " LIKE " + term2;
248 }
249
250
251
252
253
254 @Override
255 public String getForeignKeyDefinition(String tableName, String fieldName,
256 String targetTableName,
257 String targetTableFieldName,
258 String fixName) {
259 StringBuffer sb = new StringBuffer();
260 sb.append(" ADD FOREIGN KEY (" + getQuotedName(fieldName) + ") REFERENCES " +
261 getQuotedName(targetTableName) +
262 "(" + getQuotedName(targetTableFieldName) + ")");
263
264
265
266
267
268
269 if (fixName.equals("delete"))
270 sb.append(" ON DELETE CASCADE");
271 if (fixName.equals("clear"))
272 sb.append(" ON DELETE SET NULL");
273
274 return sb.toString();
275 }
276
277
278
279
280
281
282 @Override
283 public String getJdbcMetadataName(String name) {
284 return name.toUpperCase();
285 }
286
287
288
289 @Override
290 public String alterColumnAddCommentSQL(Column<?> column, String comment) {
291 return null;
292 }
293
294 }