You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SQLDialectDerby.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2011 James Moger.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.iciql;
  17. import com.iciql.util.StatementBuilder;
  18. import com.iciql.util.StringUtils;
  19. /**
  20. * Derby database dialect.
  21. */
  22. public class SQLDialectDerby extends SQLDialectDefault {
  23. @Override
  24. public Class<? extends java.util.Date> getDateTimeClass() {
  25. return java.sql.Timestamp.class;
  26. }
  27. @Override
  28. public String convertSqlType(String sqlType) {
  29. if ("TINYINT".equals(sqlType)) {
  30. // Derby does not have a TINYINT/BYTE type
  31. return "SMALLINT";
  32. }
  33. return sqlType;
  34. }
  35. @Override
  36. public void appendLimitOffset(SQLStatement stat, long limit, long offset) {
  37. // FETCH/OFFSET added in 10.5
  38. if (databaseMajorVersion >= 10 && databaseMinorVersion >= 5) {
  39. if (offset > 0) {
  40. stat.appendSQL(" OFFSET " + offset + (offset == 1 ? " ROW" : " ROWS"));
  41. }
  42. if (limit > 0) {
  43. stat.appendSQL(" FETCH NEXT " + limit + (limit == 1 ? " ROW" : " ROWS") + " ONLY");
  44. }
  45. }
  46. }
  47. @Override
  48. protected boolean prepareColumnDefinition(StatementBuilder buff, String dataType,
  49. boolean isAutoIncrement, boolean isPrimaryKey) {
  50. String convertedType = convertSqlType(dataType);
  51. buff.append(convertedType);
  52. if (isIntegerType(dataType) && isAutoIncrement) {
  53. buff.append(" GENERATED BY DEFAULT AS IDENTITY");
  54. }
  55. return false;
  56. }
  57. @Override
  58. public String prepareColumnConstraint(boolean isAutoIncrement, boolean isPrimaryKey, boolean nullable, Class<?> fieldType, String dataType, String defaultValue) {
  59. StringBuilder sb = new StringBuilder();
  60. if (!isAutoIncrement && !isPrimaryKey) {
  61. if (nullable && (defaultValue == null || ((dataType.equals("TIMESTAMP") || dataType.equals("DATETIME")) && StringUtils.isNullOrEmpty(defaultValue)))) {
  62. sb.append(" DEFAULT NULL");
  63. } else if (!StringUtils.isNullOrEmpty(defaultValue)) {
  64. if (ModelUtils.isProperlyFormattedDefaultValue(defaultValue)
  65. && ModelUtils.isValidDefaultValue(fieldType, defaultValue)) {
  66. sb.append(" DEFAULT ").append(defaultValue);
  67. }
  68. }
  69. }
  70. if (!nullable) {
  71. sb.append(" NOT NULL");
  72. }
  73. return sb.toString();
  74. }
  75. @Override
  76. public <T> void prepareDropTable(SQLStatement stat, TableDefinition<T> def) {
  77. StatementBuilder buff = new StatementBuilder("DROP TABLE "
  78. + prepareTableName(def.schemaName, def.tableName));
  79. stat.setSQL(buff.toString());
  80. return;
  81. }
  82. @Override
  83. public <T, A> void prepareBitwiseAnd(SQLStatement stat, Query<T> query, A x, A y) {
  84. throw new IciqlException("{0} does not support bitwise operator AND", databaseName);
  85. }
  86. @Override
  87. public <T, A> void prepareBitwiseXor(SQLStatement stat, Query<T> query, A x, A y) {
  88. throw new IciqlException("{0} does not support bitwise operator XOR", databaseName);
  89. }
  90. }