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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.IndexType;
  20. /**
  21. * This class provides utility methods to define primary keys, indexes, and set
  22. * the name of the table.
  23. */
  24. public class Define {
  25. private static TableDefinition<?> currentTableDefinition;
  26. private static Iciql currentTable;
  27. public static void skipCreate() {
  28. checkInDefine();
  29. currentTableDefinition.defineSkipCreate();
  30. }
  31. public static void index(IndexType type, Object... columns) {
  32. checkInDefine();
  33. currentTableDefinition.defineIndex(null, type, columns);
  34. }
  35. public static void index(String name, IndexType type, Object... columns) {
  36. checkInDefine();
  37. currentTableDefinition.defineIndex(name, type, columns);
  38. }
  39. public static void constraintUnique(String name, Object... columns) {
  40. checkInDefine();
  41. currentTableDefinition.defineConstraintUnique(name, columns);
  42. }
  43. /*
  44. * The variable argument type Object can't be used twice :-)
  45. */
  46. // public static void constraintForeignKey(String name, String refTableName,
  47. // ConstraintDeleteType deleteType, ConstraintUpdateType updateType,
  48. // ConstraintDeferrabilityType deferrabilityType, Object... columns, Object... refColumns) {
  49. // checkInDefine();
  50. // currentTableDefinition.defineForeignKey(name, columns, refTableName, Columns, deleteType, updateType, deferrabilityType);
  51. // }
  52. public static void primaryKey(Object... columns) {
  53. checkInDefine();
  54. currentTableDefinition.definePrimaryKey(columns);
  55. }
  56. public static void schemaName(String schemaName) {
  57. checkInDefine();
  58. currentTableDefinition.defineSchemaName(schemaName);
  59. }
  60. public static void tableName(String tableName) {
  61. checkInDefine();
  62. currentTableDefinition.defineTableName(tableName);
  63. }
  64. public static void viewTableName(String viewTableName) {
  65. checkInDefine();
  66. currentTableDefinition.defineViewTableName(viewTableName);
  67. }
  68. public static void memoryTable() {
  69. checkInDefine();
  70. currentTableDefinition.defineMemoryTable();
  71. }
  72. public static void columnName(Object column, String columnName) {
  73. checkInDefine();
  74. currentTableDefinition.defineColumnName(column, columnName);
  75. }
  76. public static void autoIncrement(Object column) {
  77. checkInDefine();
  78. currentTableDefinition.defineAutoIncrement(column);
  79. }
  80. public static void length(Object column, int length) {
  81. checkInDefine();
  82. currentTableDefinition.defineLength(column, length);
  83. }
  84. public static void scale(Object column, int scale) {
  85. checkInDefine();
  86. currentTableDefinition.defineScale(column, scale);
  87. }
  88. public static void trim(Object column) {
  89. checkInDefine();
  90. currentTableDefinition.defineTrim(column);
  91. }
  92. public static void nullable(Object column, boolean isNullable) {
  93. checkInDefine();
  94. currentTableDefinition.defineNullable(column, isNullable);
  95. }
  96. public static void defaultValue(Object column, String defaultValue) {
  97. checkInDefine();
  98. currentTableDefinition.defineDefaultValue(column, defaultValue);
  99. }
  100. public static void constraint(Object column, String constraint) {
  101. checkInDefine();
  102. currentTableDefinition.defineConstraint(column, constraint);
  103. }
  104. static synchronized <T> void define(TableDefinition<T> tableDefinition, Iciql table) {
  105. currentTableDefinition = tableDefinition;
  106. currentTable = table;
  107. tableDefinition.mapObject(table);
  108. table.defineIQ();
  109. currentTable = null;
  110. currentTableDefinition = null;
  111. }
  112. private static void checkInDefine() {
  113. if (currentTable == null) {
  114. throw new IciqlException("This method may only be called "
  115. + "from within the define() method, and the define() method "
  116. + "is called by the framework.");
  117. }
  118. }
  119. }