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.

SQLDialectPostgreSQL.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.TableDefinition.FieldDefinition;
  18. import com.iciql.TableDefinition.IndexDefinition;
  19. import com.iciql.util.StatementBuilder;
  20. /**
  21. * PostgreSQL database dialect.
  22. */
  23. public class SQLDialectPostgreSQL extends SQLDialectDefault {
  24. @Override
  25. public Class<? extends java.util.Date> getDateTimeClass() {
  26. return java.sql.Timestamp.class;
  27. }
  28. @Override
  29. public String convertSqlType(String sqlType) {
  30. if ("DOUBLE".equals(sqlType)) {
  31. return "DOUBLE PRECISION";
  32. } else if ("TINYINT".equals(sqlType)) {
  33. // PostgreSQL does not have a byte type
  34. return "SMALLINT";
  35. } else if ("CLOB".equals(sqlType)) {
  36. return "TEXT";
  37. } else if ("BLOB".equals(sqlType)) {
  38. return "BYTEA";
  39. }
  40. return sqlType;
  41. }
  42. @Override
  43. protected boolean prepareColumnDefinition(StatementBuilder buff, String dataType,
  44. boolean isAutoIncrement, boolean isPrimaryKey) {
  45. String convertedType = convertSqlType(dataType);
  46. if (isIntegerType(dataType)) {
  47. if (isAutoIncrement) {
  48. if ("BIGINT".equals(dataType)) {
  49. buff.append("BIGSERIAL");
  50. } else {
  51. buff.append("SERIAL");
  52. }
  53. } else {
  54. buff.append(convertedType);
  55. }
  56. } else {
  57. buff.append(convertedType);
  58. }
  59. return false;
  60. }
  61. @Override
  62. public void prepareCreateIndex(SQLStatement stat, String schemaName, String tableName,
  63. IndexDefinition index) {
  64. StatementBuilder buff = new StatementBuilder();
  65. buff.append("CREATE ");
  66. switch (index.type) {
  67. case UNIQUE:
  68. buff.append("UNIQUE ");
  69. break;
  70. case UNIQUE_HASH:
  71. buff.append("UNIQUE ");
  72. break;
  73. }
  74. buff.append("INDEX ");
  75. buff.append(index.indexName);
  76. buff.append(" ON ");
  77. buff.append(tableName);
  78. switch (index.type) {
  79. case HASH:
  80. buff.append(" USING HASH");
  81. break;
  82. case UNIQUE_HASH:
  83. buff.append(" USING HASH");
  84. break;
  85. }
  86. buff.append(" (");
  87. for (String col : index.columnNames) {
  88. buff.appendExceptFirst(", ");
  89. buff.append(prepareColumnName(col));
  90. }
  91. buff.append(") ");
  92. stat.setSQL(buff.toString().trim());
  93. }
  94. @Override
  95. public <T> void prepareMerge(SQLStatement stat, String schemaName, String tableName,
  96. TableDefinition<T> def, Object obj) {
  97. FieldDefinition primaryKey = null;
  98. for (FieldDefinition field : def.fields) {
  99. if (field.isPrimaryKey) {
  100. primaryKey = field;
  101. }
  102. }
  103. if (primaryKey == null || databaseVersion < 9.5f) {
  104. // simulated UPSERT for <= 9.4 release
  105. super.prepareMerge(stat, schemaName, tableName, def, obj);
  106. return;
  107. }
  108. // official UPSERT added in 9.5 release
  109. StatementBuilder buff = new StatementBuilder("INSERT INTO ");
  110. buff.append(prepareTableName(schemaName, tableName)).append(" (");
  111. buff.resetCount();
  112. for (FieldDefinition field : def.fields) {
  113. buff.appendExceptFirst(", ");
  114. buff.append(field.columnName);
  115. }
  116. buff.resetCount();
  117. buff.append(") VALUES (");
  118. for (FieldDefinition field : def.fields) {
  119. buff.appendExceptFirst(", ");
  120. buff.append('?');
  121. Object value = def.getValue(obj, field);
  122. Object parameter = serialize(value, field.typeAdapter);
  123. stat.addParameter(parameter);
  124. }
  125. buff.append(") ON CONFLICT (");
  126. buff.resetCount();
  127. for (FieldDefinition field : def.fields) {
  128. if (field.isPrimaryKey) {
  129. buff.appendExceptFirst(", ");
  130. buff.append(field.columnName);
  131. }
  132. }
  133. buff.append(") DO UPDATE SET ");
  134. buff.resetCount();
  135. for (FieldDefinition field : def.fields) {
  136. buff.appendExceptFirst(", ");
  137. buff.append(field.columnName);
  138. buff.append("=?");
  139. Object value = def.getValue(obj, field);
  140. Object parameter = serialize(value, field.typeAdapter);
  141. stat.addParameter(parameter);
  142. }
  143. stat.setSQL(buff.toString());
  144. }
  145. @Override
  146. public <T, A> void prepareBitwiseXor(SQLStatement stat, Query<T> query, A x, A y) {
  147. query.appendSQL(stat, null, x);
  148. stat.appendSQL(" # ");
  149. query.appendSQL(stat, x, y);
  150. }
  151. }