1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.efaps.db.databases;
22
23 import java.sql.Connection;
24 import java.sql.ResultSet;
25 import java.sql.SQLException;
26 import java.sql.Statement;
27 import java.util.Map;
28
29 import org.apache.commons.dbutils.BasicRowProcessor;
30 import org.apache.commons.dbutils.RowProcessor;
31 import org.efaps.db.databases.information.TableInformation;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
36
37
38
39
40
41
42
43 public class DerbyDatabase
44 extends AbstractDatabase<DerbyDatabase>
45 {
46
47
48
49 private static final Logger LOG = LoggerFactory.getLogger(DerbyDatabase.class);
50
51
52
53
54
55
56 private static final String SELECT_ALL_KEYS
57 = "select t.TABLENAME, c.CONSTRAINTNAME "
58 + "from SYS.SYSSCHEMAS s, SYS.SYSTABLES t, SYS.SYSCONSTRAINTS c "
59 + "where s.AUTHORIZATIONID<>'DBA' "
60 + "and s.SCHEMAID=t.SCHEMAID "
61 + "and t.TABLEID=c.TABLEID "
62 + "and c.TYPE='F'";
63
64
65
66
67
68
69 private static final String SELECT_ALL_VIEWS
70 = "select t.TABLENAME "
71 + "from SYS.SYSSCHEMAS s, SYS.SYSTABLES t "
72 + "where s.AUTHORIZATIONID<>'DBA' "
73 + "and s.SCHEMAID=t.SCHEMAID "
74 + "and t.TABLETYPE='V'";
75
76
77
78
79
80
81 private static final String SELECT_ALL_TABLES
82 = "select t.TABLENAME "
83 + "from SYS.SYSSCHEMAS s, SYS.SYSTABLES t "
84 + "where s.AUTHORIZATIONID<>'DBA' "
85 + "and s.SCHEMAID=t.SCHEMAID "
86 + "and t.TABLETYPE='T'";
87
88
89
90
91
92
93
94 private static final RowProcessor ROWPROCESSOR = new BasicRowProcessor();
95
96
97
98
99
100 public DerbyDatabase()
101 {
102 addMapping(ColumnType.INTEGER, "bigint", "cast(null as bigint)", "bigint");
103
104 addMapping(ColumnType.STRING_SHORT, "char", "cast(null as char)", "char");
105 addMapping(ColumnType.STRING_LONG, "varchar", "cast(null as varchar)", "varchar");
106 addMapping(ColumnType.DATETIME, "timestamp", "cast(null as timestamp)", "timestamp");
107 addMapping(ColumnType.BLOB, "blob(2G)", "cast(null as blob)", "blob");
108 addMapping(ColumnType.CLOB, "clob(2G)", "cast(null as clob)", "clob");
109 addMapping(ColumnType.BOOLEAN, "smallint", "cast(null as smallint)", "smallint");
110 }
111
112
113
114
115 @Override
116 public boolean isConnected(final Connection _connection)
117 {
118
119 return false;
120 }
121
122
123
124
125 @Override
126 public String getCurrentTimeStamp()
127 {
128 return "current_timestamp";
129 }
130
131
132
133
134 @Override
135 public String getTimestampValue(final String _isoDateTime)
136 {
137 return "timestamp '" + _isoDateTime + "'";
138 }
139
140 @Override
141 public Object getBooleanValue(final Boolean _value)
142 {
143
144 return _value;
145 }
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161 @Override
162 @SuppressFBWarnings("SQL_NONCONSTANT_STRING_PASSED_TO_EXECUTE")
163 public void deleteAll(final Connection _con)
164 throws SQLException
165 {
166 final Statement stmtSel = _con.createStatement();
167 final Statement stmtExec = _con.createStatement();
168
169 try {
170
171 if (DerbyDatabase.LOG.isInfoEnabled()) {
172 DerbyDatabase.LOG.info("Remove all Foreign Keys");
173 }
174 ResultSet rs = stmtSel.executeQuery(DerbyDatabase.SELECT_ALL_KEYS);
175 while (rs.next()) {
176 final String tableName = rs.getString(1);
177 final String constrName = rs.getString(2);
178 if (DerbyDatabase.LOG.isDebugEnabled()) {
179 DerbyDatabase.LOG.debug(" - Table '" + tableName + "' Constraint '" + constrName + "'");
180 }
181 stmtExec.execute("alter table " + tableName + " drop constraint " + constrName);
182 }
183 rs.close();
184
185
186 if (DerbyDatabase.LOG.isInfoEnabled()) {
187 DerbyDatabase.LOG.info("Remove all Views");
188 }
189 rs = stmtSel.executeQuery(DerbyDatabase.SELECT_ALL_VIEWS);
190 while (rs.next()) {
191 final String viewName = rs.getString(1);
192 if (DerbyDatabase.LOG.isDebugEnabled()) {
193 DerbyDatabase.LOG.debug(" - View '" + viewName + "'");
194 }
195 stmtExec.execute("drop view " + viewName);
196 }
197 rs.close();
198
199
200 if (DerbyDatabase.LOG.isInfoEnabled()) {
201 DerbyDatabase.LOG.info("Remove all Tables");
202 }
203 rs = stmtSel.executeQuery(DerbyDatabase.SELECT_ALL_TABLES);
204 while (rs.next()) {
205 final String tableName = rs.getString(1);
206 if (DerbyDatabase.LOG.isDebugEnabled()) {
207 DerbyDatabase.LOG.debug(" - Table '" + tableName + "'");
208 }
209 stmtExec.execute("drop table " + tableName);
210 }
211 rs.close();
212 } finally {
213 stmtSel.close();
214 stmtExec.close();
215 }
216 }
217
218
219
220
221 @Override
222 public DerbyDatabase deleteView(final Connection _con,
223 final String _name)
224 throws SQLException
225 {
226 throw new Error("not implemented");
227 }
228
229
230
231
232 @Override
233 public DerbyDatabase createTable(final Connection _con,
234 final String _table
235 )
236 throws SQLException
237 {
238 final Statement stmt = _con.createStatement();
239
240 try {
241
242
243 final StringBuilder cmd = new StringBuilder()
244 .append("create table ").append(_table).append(" (")
245 .append(" ID bigint not null");
246
247
248
249
250
251
252 cmd.append(",")
253 .append(" constraint ").append(_table).append("_UK_ID unique(ID)");
254
255
256
257
258
259
260
261
262
263 cmd.append(")");
264 stmt.executeUpdate(cmd.toString());
265 } finally {
266 stmt.close();
267 }
268
269 return this;
270 }
271
272
273
274
275
276 @Override
277 public DerbyDatabase defineTableAutoIncrement(final Connection _con,
278 final String _table)
279 throws SQLException
280 {
281 throw new Error("not implemented");
282 }
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303 @Override
304
305 public DerbyDatabase addTableColumn(final Connection _con,
306 final String _tableName,
307 final String _columnName,
308 final ColumnType _columnType,
309 final String _defaultValue,
310 final int _length,
311 final int _scale)
312 throws SQLException
313 {
314
315 String defaultValue = _defaultValue;
316
317 if (defaultValue == null) {
318 switch (_columnType) {
319 case INTEGER:
320 case REAL:
321 defaultValue = "0";
322 break;
323 case DATETIME:
324 case STRING_LONG:
325 case STRING_SHORT:
326 defaultValue = "''";
327 break;
328 default:
329 break;
330 }
331 }
332 return super.addTableColumn(_con, _tableName, _columnName, _columnType,
333 defaultValue, _length, _scale);
334 }
335
336
337
338
339 @Override
340 public boolean supportsGetGeneratedKeys()
341 {
342 return true;
343 }
344
345
346
347
348 @Override
349 public boolean supportsBinaryInputStream()
350 {
351 return false;
352 }
353
354
355
356
357
358 @Override
359 public boolean supportsBigTransactions()
360 {
361 return false;
362 }
363
364
365
366
367 @Override
368 public DerbyDatabase createSequence(final Connection _con,
369 final String _name,
370 final long _startValue)
371 {
372 throw new Error("not implemented");
373 }
374
375
376
377
378 @Override
379 public DerbyDatabase deleteSequence(final Connection _con,
380 final String _name)
381 {
382 throw new Error("not implemented");
383 }
384
385
386
387
388 @Override
389 public boolean existsSequence(final Connection _con,
390 final String _name)
391 {
392 throw new Error("not implemented");
393 }
394
395
396
397
398 @Override
399 public long nextSequence(final Connection _con,
400 final String _name)
401 throws SQLException
402 {
403 throw new Error("not implemented");
404 }
405
406
407
408
409 @Override
410 public DerbyDatabase setSequence(final Connection _con,
411 final String _name,
412 final long _value)
413 throws SQLException
414 {
415
416 throw new Error("not implemented");
417 }
418
419
420
421
422 @Override
423 public String getHibernateDialect()
424 {
425 return "org.hibernate.dialect.DerbyTenSevenDialect";
426 }
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452 @Override
453 protected void initTableInfoUniqueKeys(final Connection _con,
454 final String _sql,
455 final Map<String, TableInformation> _cache4Name)
456 throws SQLException
457 {
458 final String sqlStmt = new StringBuilder()
459 .append("select t.tablename as TABLE_NAME, c.CONSTRAINTNAME as INDEX_NAME, g.DESCRIPTOR as COLUMN_NAME")
460 .append(" from SYS.SYSTABLES t, SYS.SYSCONSTRAINTS c, SYS.SYSKEYS k, SYS.SYSCONGLOMERATES g ")
461 .append(" where t.TABLEID=c.TABLEID")
462 .append(" AND c.TYPE='U'")
463 .append(" AND c.CONSTRAINTID = k.CONSTRAINTID")
464 .append(" AND k.CONGLOMERATEID = g.CONGLOMERATEID")
465 .toString();
466 super.initTableInfoUniqueKeys(_con, sqlStmt, _cache4Name);
467 }
468
469
470
471
472 @Override
473 protected StringBuilder getAlterColumn(final String _columnName,
474 final org.efaps.db.databases.AbstractDatabase.ColumnType _columnType)
475 {
476 final StringBuilder ret = new StringBuilder()
477 .append(" alter ").append(getColumnQuote()).append(_columnName).append(getColumnQuote())
478 .append(" type ")
479 .append(getWriteSQLTypeName(_columnType));
480 return ret;
481 }
482
483
484
485
486 @Override
487 protected StringBuilder getAlterColumnIsNotNull(final String _columnName,
488 final boolean _isNotNull)
489 {
490 final StringBuilder ret = new StringBuilder()
491 .append(" alter column ").append(getColumnQuote()).append(_columnName).append(getColumnQuote());
492 if (_isNotNull) {
493 ret.append(" set ");
494 } else {
495 ret.append(" drop ");
496 }
497 ret.append(" not null");
498 return ret;
499 }
500
501
502
503
504 @Override
505 protected boolean check4NullValues(final Connection _con,
506 final String _tableName,
507 final String _columnName)
508 throws SQLException
509 {
510 boolean ret = true;
511 final StringBuilder cmd = new StringBuilder();
512 cmd.append("select count(*) from ").append(getTableQuote()).append(_tableName).append(getTableQuote())
513 .append(" where ").append(getColumnQuote()).append(_columnName).append(getColumnQuote())
514 .append(" is null");
515
516 DerbyDatabase.LOG.debug(" ..SQL> {}", cmd);
517
518 final Statement stmt = _con.createStatement();
519 ResultSet rs = null;
520 try {
521 rs = stmt.executeQuery(cmd.toString());
522 rs.next();
523 ret = rs.getInt(1) > 0;
524 } finally {
525 if (rs != null) {
526 rs.close();
527 }
528 stmt.close();
529 }
530 return ret;
531 }
532
533
534
535
536 @Override
537 public RowProcessor getRowProcessor()
538 {
539 return DerbyDatabase.ROWPROCESSOR;
540 }
541 }