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.

Define.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright 2004-2011 H2 Group.
  3. * Copyright 2011 James Moger.
  4. * Copyright 2012 Frédéric Gaillard.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package com.iciql;
  19. import com.iciql.Iciql.DataTypeAdapter;
  20. import com.iciql.Iciql.IndexType;
  21. /**
  22. * This class provides utility methods to define primary keys, indexes, and set
  23. * the name of the table.
  24. */
  25. public class Define {
  26. private static TableDefinition<?> currentTableDefinition;
  27. private static Iciql currentTable;
  28. public static void skipCreate() {
  29. checkInDefine();
  30. currentTableDefinition.defineSkipCreate();
  31. }
  32. public static void index(IndexType type, Object... columns) {
  33. checkInDefine();
  34. currentTableDefinition.defineIndex(null, type, columns);
  35. }
  36. public static void index(String name, IndexType type, Object... columns) {
  37. checkInDefine();
  38. currentTableDefinition.defineIndex(name, type, columns);
  39. }
  40. public static void constraintUnique(String name, Object... columns) {
  41. checkInDefine();
  42. currentTableDefinition.defineConstraintUnique(name, columns);
  43. }
  44. /*
  45. * The variable argument type Object can't be used twice :-)
  46. */
  47. // public static void constraintForeignKey(String name, String refTableName,
  48. // ConstraintDeleteType deleteType, ConstraintUpdateType updateType,
  49. // ConstraintDeferrabilityType deferrabilityType, Object... columns, Object... refColumns) {
  50. // checkInDefine();
  51. // currentTableDefinition.defineForeignKey(name, columns, refTableName, Columns, deleteType, updateType, deferrabilityType);
  52. // }
  53. public static void primaryKey(Object... columns) {
  54. checkInDefine();
  55. currentTableDefinition.definePrimaryKey(columns);
  56. }
  57. public static void schemaName(String schemaName) {
  58. checkInDefine();
  59. currentTableDefinition.defineSchemaName(schemaName);
  60. }
  61. public static void tableName(String tableName) {
  62. checkInDefine();
  63. currentTableDefinition.defineTableName(tableName);
  64. }
  65. public static void viewTableName(String viewTableName) {
  66. checkInDefine();
  67. currentTableDefinition.defineViewTableName(viewTableName);
  68. }
  69. public static void memoryTable() {
  70. checkInDefine();
  71. currentTableDefinition.defineMemoryTable();
  72. }
  73. public static void columnName(Object column, String columnName) {
  74. checkInDefine();
  75. currentTableDefinition.defineColumnName(column, columnName);
  76. }
  77. public static void autoIncrement(Object column) {
  78. checkInDefine();
  79. currentTableDefinition.defineAutoIncrement(column);
  80. }
  81. public static void length(Object column, int length) {
  82. checkInDefine();
  83. currentTableDefinition.defineLength(column, length);
  84. }
  85. public static void scale(Object column, int scale) {
  86. checkInDefine();
  87. currentTableDefinition.defineScale(column, scale);
  88. }
  89. public static void trim(Object column) {
  90. checkInDefine();
  91. currentTableDefinition.defineTrim(column);
  92. }
  93. public static void nullable(Object column, boolean isNullable) {
  94. checkInDefine();
  95. currentTableDefinition.defineNullable(column, isNullable);
  96. }
  97. public static void defaultValue(Object column, String defaultValue) {
  98. checkInDefine();
  99. currentTableDefinition.defineDefaultValue(column, defaultValue);
  100. }
  101. public static void constraint(Object column, String constraint) {
  102. checkInDefine();
  103. currentTableDefinition.defineConstraint(column, constraint);
  104. }
  105. public static void typeAdapter(Object column, Class<? extends DataTypeAdapter<?>> typeAdapter) {
  106. checkInDefine();
  107. currentTableDefinition.defineTypeAdapter(column, typeAdapter);
  108. }
  109. static synchronized <T> void define(TableDefinition<T> tableDefinition, Iciql table) {
  110. currentTableDefinition = tableDefinition;
  111. currentTable = table;
  112. tableDefinition.mapObject(table);
  113. table.defineIQ();
  114. currentTable = null;
  115. currentTableDefinition = null;
  116. }
  117. private static void checkInDefine() {
  118. if (currentTable == null) {
  119. throw new IciqlException("This method may only be called "
  120. + "from within the define() method, and the define() method "
  121. + "is called by the framework.");
  122. }
  123. }
  124. }