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.

SQLDialect.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright 2004-2011 H2 Group.
  3. * Copyright 2011 James Moger.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.iciql;
  18. import java.sql.Connection;
  19. import java.sql.DatabaseMetaData;
  20. import java.sql.SQLException;
  21. import com.iciql.TableDefinition.IndexDefinition;
  22. import com.iciql.util.StatementBuilder;
  23. import com.iciql.util.StringUtils;
  24. /**
  25. * This interface defines points where iciql can build different statements
  26. * depending on the database used.
  27. */
  28. public interface SQLDialect {
  29. /**
  30. * Configure the dialect from the database connection.
  31. *
  32. * @param conn
  33. */
  34. void configureDialect(Connection conn);
  35. /**
  36. * Returns a properly formatted table name for the dialect.
  37. *
  38. * @param schema
  39. * the schema name, or null for no schema
  40. * @param table
  41. * the properly formatted table name
  42. * @return the SQL snippet
  43. */
  44. String prepareTableName(String schema, String table);
  45. /**
  46. * Returns a properly formatted column name for the dialect.
  47. *
  48. * @param name
  49. * the column name
  50. * @return the properly formatted column name
  51. */
  52. String prepareColumnName(String name);
  53. /**
  54. * Get the CREATE INDEX statement.
  55. *
  56. * @param schema
  57. * the schema name
  58. * @param table
  59. * the table name
  60. * @param index
  61. * the index definition
  62. * @return the SQL statement
  63. */
  64. String prepareCreateIndex(String schema, String table, IndexDefinition index);
  65. /**
  66. * Append "LIMIT limit" to the SQL statement.
  67. *
  68. * @param stat
  69. * the statement
  70. * @param limit
  71. * the limit
  72. */
  73. void appendLimit(SQLStatement stat, long limit);
  74. /**
  75. * Append "OFFSET offset" to the SQL statement.
  76. *
  77. * @param stat
  78. * the statement
  79. * @param offset
  80. * the offset
  81. */
  82. void appendOffset(SQLStatement stat, long offset);
  83. /**
  84. * Whether memory tables are supported.
  85. *
  86. * @return true if they are
  87. */
  88. boolean supportsMemoryTables();
  89. /**
  90. * Whether merge is a supported function.
  91. *
  92. * @return true if they are
  93. */
  94. boolean supportsMerge();
  95. /**
  96. * Whether LIMIT/OFFSET notation is supported.
  97. *
  98. * @return true if they are
  99. */
  100. boolean supportsLimitOffset();
  101. /**
  102. * Default implementation of an SQL dialect.
  103. * Does not support merge nor index creation.
  104. */
  105. public static class DefaultSQLDialect implements SQLDialect {
  106. float databaseVersion;
  107. String productName;
  108. String productVersion;
  109. @Override
  110. public String toString() {
  111. return getClass().getName() + ": " + productName + " " + productVersion;
  112. }
  113. @Override
  114. public void configureDialect(Connection conn) {
  115. loadIdentity(conn);
  116. }
  117. protected void loadIdentity(Connection conn) {
  118. try {
  119. DatabaseMetaData data = conn.getMetaData();
  120. databaseVersion = Float.parseFloat(data.getDatabaseMajorVersion() + "."
  121. + data.getDatabaseMinorVersion());
  122. productName = data.getDatabaseProductName();
  123. productVersion = data.getDatabaseProductVersion();
  124. } catch (SQLException e) {
  125. throw new IciqlException(e);
  126. }
  127. }
  128. @Override
  129. public boolean supportsMemoryTables() {
  130. return false;
  131. }
  132. @Override
  133. public boolean supportsMerge() {
  134. return false;
  135. }
  136. @Override
  137. public boolean supportsLimitOffset() {
  138. return true;
  139. }
  140. @Override
  141. public String prepareTableName(String schema, String table) {
  142. if (StringUtils.isNullOrEmpty(schema)) {
  143. return table;
  144. }
  145. return schema + "." + table;
  146. }
  147. @Override
  148. public String prepareColumnName(String name) {
  149. return name;
  150. }
  151. @Override
  152. public String prepareCreateIndex(String schema, String table, IndexDefinition index) {
  153. throw new IciqlException("Dialect does not support index creation!");
  154. }
  155. @Override
  156. public void appendLimit(SQLStatement stat, long limit) {
  157. stat.appendSQL(" LIMIT " + limit);
  158. }
  159. @Override
  160. public void appendOffset(SQLStatement stat, long offset) {
  161. stat.appendSQL(" OFFSET " + offset);
  162. }
  163. }
  164. /**
  165. * H2 database dialect.
  166. */
  167. public static class H2Dialect extends DefaultSQLDialect {
  168. @Override
  169. public boolean supportsMemoryTables() {
  170. return true;
  171. }
  172. @Override
  173. public boolean supportsMerge() {
  174. return true;
  175. }
  176. @Override
  177. public String prepareCreateIndex(String schema, String table, IndexDefinition index) {
  178. StatementBuilder buff = new StatementBuilder();
  179. buff.append("CREATE ");
  180. switch (index.type) {
  181. case STANDARD:
  182. break;
  183. case UNIQUE:
  184. buff.append("UNIQUE ");
  185. break;
  186. case HASH:
  187. buff.append("HASH ");
  188. break;
  189. case UNIQUE_HASH:
  190. buff.append("UNIQUE HASH ");
  191. break;
  192. }
  193. buff.append("INDEX IF NOT EXISTS ");
  194. buff.append(index.indexName);
  195. buff.append(" ON ");
  196. buff.append(table);
  197. buff.append("(");
  198. for (String col : index.columnNames) {
  199. buff.appendExceptFirst(", ");
  200. buff.append(col);
  201. }
  202. buff.append(")");
  203. return buff.toString();
  204. }
  205. }
  206. }