diff options
author | James Moger <james.moger@gitblit.com> | 2014-10-29 17:12:14 -0400 |
---|---|---|
committer | James Moger <james.moger@gitblit.com> | 2014-10-29 17:12:14 -0400 |
commit | 8d28bc740c9bcb76186e7572f74a144397e780ce (patch) | |
tree | 1815e1d21df77e352ba2e8106557f71cb5561a8e /src/main/java/com/iciql/SQLDialectPostgreSQL.java | |
parent | bdb2899da4cbb27016d85c5e4fe268ddbccef546 (diff) | |
download | iciql-8d28bc740c9bcb76186e7572f74a144397e780ce.tar.gz iciql-8d28bc740c9bcb76186e7572f74a144397e780ce.zip |
Support data type adapters
This allows custom types to be (de)serialized into a standard JDBC type or
to support db-specific data types, like the Postgres json and xml types.
Diffstat (limited to 'src/main/java/com/iciql/SQLDialectPostgreSQL.java')
-rw-r--r-- | src/main/java/com/iciql/SQLDialectPostgreSQL.java | 75 |
1 files changed, 73 insertions, 2 deletions
diff --git a/src/main/java/com/iciql/SQLDialectPostgreSQL.java b/src/main/java/com/iciql/SQLDialectPostgreSQL.java index fc115ab..b5ac5c3 100644 --- a/src/main/java/com/iciql/SQLDialectPostgreSQL.java +++ b/src/main/java/com/iciql/SQLDialectPostgreSQL.java @@ -16,6 +16,11 @@ package com.iciql;
+import java.sql.SQLException;
+
+import org.postgresql.util.PGobject;
+
+import com.iciql.Iciql.DataTypeAdapter;
import com.iciql.TableDefinition.IndexDefinition;
import com.iciql.util.StatementBuilder;
@@ -46,7 +51,7 @@ public class SQLDialectPostgreSQL extends SQLDialectDefault { @Override
protected boolean prepareColumnDefinition(StatementBuilder buff, String dataType,
- boolean isAutoIncrement, boolean isPrimaryKey) {
+ boolean isAutoIncrement, boolean isPrimaryKey) {
String convertedType = convertSqlType(dataType);
if (isIntegerType(dataType)) {
if (isAutoIncrement) {
@@ -63,7 +68,7 @@ public class SQLDialectPostgreSQL extends SQLDialectDefault { }
return false;
}
-
+
@Override
public void prepareCreateIndex(SQLStatement stat, String schemaName, String tableName,
IndexDefinition index) {
@@ -100,4 +105,70 @@ public class SQLDialectPostgreSQL extends SQLDialectDefault { stat.setSQL(buff.toString().trim());
}
+
+ /**
+ * Handles transforming raw strings to/from the Postgres JSON data type.
+ */
+ public class JsonStringAdapter implements DataTypeAdapter<String> {
+
+ @Override
+ public String getDataType() {
+ return "json";
+ }
+
+ @Override
+ public Class<String> getJavaType() {
+ return String.class;
+ }
+
+ @Override
+ public Object serialize(String value) {
+ PGobject pg = new PGobject();
+ pg.setType(getDataType());
+ try {
+ pg.setValue(value);
+ } catch (SQLException e) {
+ // not thrown on base PGobject
+ }
+ return pg;
+ }
+
+ @Override
+ public String deserialize(Object value) {
+ return value.toString();
+ }
+ }
+
+ /**
+ * Handles transforming raw strings to/from the Postgres XML data type.
+ */
+ public class XmlStringAdapter implements DataTypeAdapter<String> {
+
+ @Override
+ public String getDataType() {
+ return "xml";
+ }
+
+ @Override
+ public Class<String> getJavaType() {
+ return String.class;
+ }
+
+ @Override
+ public Object serialize(String value) {
+ PGobject pg = new PGobject();
+ pg.setType(getDataType());
+ try {
+ pg.setValue(value);
+ } catch (SQLException e) {
+ // not thrown on base PGobject
+ }
+ return pg;
+ }
+
+ @Override
+ public String deserialize(Object value) {
+ return value.toString();
+ }
+ }
}
\ No newline at end of file |