diff options
author | James Moger <james.moger@gitblit.com> | 2014-11-09 12:22:12 -0500 |
---|---|---|
committer | James Moger <james.moger@gitblit.com> | 2014-11-09 12:22:12 -0500 |
commit | 97358035987e26ea801abac461bf5b5c9a406aa7 (patch) | |
tree | e6a34fc922a9ca56092518db233526985c780630 /src | |
parent | 96d0aca9ff3b29be62bc6558af80fe115b646b88 (diff) | |
download | iciql-97358035987e26ea801abac461bf5b5c9a406aa7.tar.gz iciql-97358035987e26ea801abac461bf5b5c9a406aa7.zip |
Moved data type adapters into separate package, added gson and xstream
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/com/iciql/SQLDialectPostgreSQL.java | 103 | ||||
-rw-r--r-- | src/main/java/com/iciql/adapter/GsonTypeAdapter.java | 58 | ||||
-rw-r--r-- | src/main/java/com/iciql/adapter/JavaSerializationTypeAdapter.java (renamed from src/main/java/com/iciql/JavaSerializationTypeAdapter.java) | 4 | ||||
-rw-r--r-- | src/main/java/com/iciql/adapter/XStreamTypeAdapter.java | 58 | ||||
-rw-r--r-- | src/main/java/com/iciql/adapter/postgres/GsonBTypeAdapter.java | 51 | ||||
-rw-r--r-- | src/main/java/com/iciql/adapter/postgres/GsonTypeAdapter.java | 49 | ||||
-rw-r--r-- | src/main/java/com/iciql/adapter/postgres/JsonStringAdapter.java | 55 | ||||
-rw-r--r-- | src/main/java/com/iciql/adapter/postgres/JsonbStringAdapter.java | 55 | ||||
-rw-r--r-- | src/main/java/com/iciql/adapter/postgres/XStreamTypeAdapter.java | 35 | ||||
-rw-r--r-- | src/main/java/com/iciql/adapter/postgres/XmlStringAdapter.java | 55 | ||||
-rw-r--r-- | src/site/dta.mkd | 18 | ||||
-rw-r--r-- | src/test/java/com/iciql/test/DataTypeAdapterTest.java | 2 |
12 files changed, 434 insertions, 109 deletions
diff --git a/src/main/java/com/iciql/SQLDialectPostgreSQL.java b/src/main/java/com/iciql/SQLDialectPostgreSQL.java index f10017c..6998c24 100644 --- a/src/main/java/com/iciql/SQLDialectPostgreSQL.java +++ b/src/main/java/com/iciql/SQLDialectPostgreSQL.java @@ -16,11 +16,6 @@ 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;
@@ -106,102 +101,4 @@ 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 JSONB data type.
- */
- public class JsonbStringAdapter implements DataTypeAdapter<String> {
-
- @Override
- public String getDataType() {
- return "jsonb";
- }
-
- @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 diff --git a/src/main/java/com/iciql/adapter/GsonTypeAdapter.java b/src/main/java/com/iciql/adapter/GsonTypeAdapter.java new file mode 100644 index 0000000..a85f15e --- /dev/null +++ b/src/main/java/com/iciql/adapter/GsonTypeAdapter.java @@ -0,0 +1,58 @@ +/* + * Copyright 2014 James Moger. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.iciql.adapter; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.iciql.Iciql.DataTypeAdapter; + +/** + * Base class for inserting/retrieving a Java Object (de)serialized as JSON using Google GSON. + * <p>You use this by creating a subclass which defines your object class.</p> + * <pre> + * public class CustomObjectAdapter extends GsonTypeAdapter<CustomObject> { + * + * public Class<CustomObject> getJavaType() { + * return CustomObject.class; + * } + * } + * </pre> + * @param <T> + */ +public abstract class GsonTypeAdapter<T> implements DataTypeAdapter<T> { + + protected Gson gson() { + return new GsonBuilder().create(); + } + + @Override + public String getDataType() { + return "TEXT"; + } + + @Override + public Object serialize(T value) { + return gson().toJson(value); + } + + @Override + public T deserialize(Object value) { + String json = value.toString(); + Gson gson = gson(); + T t = gson.fromJson(json, getJavaType()); + return t; + } +} diff --git a/src/main/java/com/iciql/JavaSerializationTypeAdapter.java b/src/main/java/com/iciql/adapter/JavaSerializationTypeAdapter.java index e38e0f8..c475817 100644 --- a/src/main/java/com/iciql/JavaSerializationTypeAdapter.java +++ b/src/main/java/com/iciql/adapter/JavaSerializationTypeAdapter.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.iciql; +package com.iciql.adapter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -25,6 +25,8 @@ import java.io.ObjectOutputStream; import java.sql.Blob; import java.sql.SQLException; +import com.iciql.Iciql; +import com.iciql.IciqlException; import com.iciql.Iciql.DataTypeAdapter; /** diff --git a/src/main/java/com/iciql/adapter/XStreamTypeAdapter.java b/src/main/java/com/iciql/adapter/XStreamTypeAdapter.java new file mode 100644 index 0000000..3d1a9ce --- /dev/null +++ b/src/main/java/com/iciql/adapter/XStreamTypeAdapter.java @@ -0,0 +1,58 @@ +/* + * Copyright 2014 James Moger. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.iciql.adapter; + +import com.iciql.Iciql.DataTypeAdapter; +import com.thoughtworks.xstream.XStream; + +/** + * Base class for inserting/retrieving a Java Object (de)serialized as XML using XStream. + * <p>You use this by creating a subclass which defines your object class.</p> + * <pre> + * public class CustomObjectAdapter extends XStreamTypeAdapter<CustomObject> { + * + * public Class<CustomObject> getJavaType() { + * return CustomObject.class; + * } + * } + * </pre> + * @param <T> + */ +public abstract class XStreamTypeAdapter<T> implements DataTypeAdapter<T> { + + protected XStream xstream() { + return new XStream(); + } + + @Override + public String getDataType() { + return "TEXT"; + } + + @Override + public Object serialize(T value) { + return xstream().toXML(value); + } + + @Override + public T deserialize(Object value) { + String xml = value.toString(); + XStream xstream = xstream(); + T t = (T) xstream.fromXML(xml); + return t; + } +} diff --git a/src/main/java/com/iciql/adapter/postgres/GsonBTypeAdapter.java b/src/main/java/com/iciql/adapter/postgres/GsonBTypeAdapter.java new file mode 100644 index 0000000..6332742 --- /dev/null +++ b/src/main/java/com/iciql/adapter/postgres/GsonBTypeAdapter.java @@ -0,0 +1,51 @@ +/* + * Copyright 2014 James Moger. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.iciql.adapter.postgres; + +import java.sql.SQLException; + +import org.postgresql.util.PGobject; + +import com.iciql.adapter.GsonTypeAdapter; + +/** + * Postgres JSONB data type adapter maps a JSONB column to a domain object using Google GSON. + * + * @author James Moger + * + * @param <T> + */ +public abstract class GsonBTypeAdapter<T> extends GsonTypeAdapter<T> { + + @Override + public String getDataType() { + return "jsonb"; + } + + @Override + public Object serialize(T value) { + + String json = gson().toJson(value); + PGobject pg = new PGobject(); + pg.setType(getDataType()); + try { + pg.setValue(json); + } catch (SQLException e) { + // not thrown on base PGobject + } + return pg; + } +} diff --git a/src/main/java/com/iciql/adapter/postgres/GsonTypeAdapter.java b/src/main/java/com/iciql/adapter/postgres/GsonTypeAdapter.java new file mode 100644 index 0000000..64481bd --- /dev/null +++ b/src/main/java/com/iciql/adapter/postgres/GsonTypeAdapter.java @@ -0,0 +1,49 @@ +/* + * Copyright 2014 James Moger. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.iciql.adapter.postgres; + +import java.sql.SQLException; + +import org.postgresql.util.PGobject; + +/** + * Postgres JSON data type adapter maps a JSON column to a domain object using Google GSON. + * + * @author James Moger + * + * @param <T> + */ +public abstract class GsonTypeAdapter<T> extends com.iciql.adapter.GsonTypeAdapter<T> { + + @Override + public String getDataType() { + return "json"; + } + + @Override + public Object serialize(T value) { + + String json = gson().toJson(value); + PGobject pg = new PGobject(); + pg.setType(getDataType()); + try { + pg.setValue(json); + } catch (SQLException e) { + // not thrown on base PGobject + } + return pg; + } +} diff --git a/src/main/java/com/iciql/adapter/postgres/JsonStringAdapter.java b/src/main/java/com/iciql/adapter/postgres/JsonStringAdapter.java new file mode 100644 index 0000000..64ad073 --- /dev/null +++ b/src/main/java/com/iciql/adapter/postgres/JsonStringAdapter.java @@ -0,0 +1,55 @@ +/* + * Copyright 2014 James Moger. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.iciql.adapter.postgres; + +import java.sql.SQLException; + +import org.postgresql.util.PGobject; + +import com.iciql.Iciql.DataTypeAdapter; + +/** + * 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(); + } + }
\ No newline at end of file diff --git a/src/main/java/com/iciql/adapter/postgres/JsonbStringAdapter.java b/src/main/java/com/iciql/adapter/postgres/JsonbStringAdapter.java new file mode 100644 index 0000000..1619811 --- /dev/null +++ b/src/main/java/com/iciql/adapter/postgres/JsonbStringAdapter.java @@ -0,0 +1,55 @@ +/* + * Copyright 2014 James Moger. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.iciql.adapter.postgres; + +import java.sql.SQLException; + +import org.postgresql.util.PGobject; + +import com.iciql.Iciql.DataTypeAdapter; + +/** + * Handles transforming raw strings to/from the Postgres JSONB data type. + */ +public class JsonbStringAdapter implements DataTypeAdapter<String> { + + @Override + public String getDataType() { + return "jsonb"; + } + + @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 diff --git a/src/main/java/com/iciql/adapter/postgres/XStreamTypeAdapter.java b/src/main/java/com/iciql/adapter/postgres/XStreamTypeAdapter.java new file mode 100644 index 0000000..1bf6fc2 --- /dev/null +++ b/src/main/java/com/iciql/adapter/postgres/XStreamTypeAdapter.java @@ -0,0 +1,35 @@ +/* + * Copyright 2014 James Moger. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.iciql.adapter.postgres; + + + +/** + * Postgres XML data type adapter maps an XML column to a domain object using XStream. + * + * @author James Moger + * + * @param <T> + */ +public abstract class XStreamTypeAdapter<T> extends com.iciql.adapter.XStreamTypeAdapter<T> { + + @Override + public String getDataType() { + return "xml"; + } + +} diff --git a/src/main/java/com/iciql/adapter/postgres/XmlStringAdapter.java b/src/main/java/com/iciql/adapter/postgres/XmlStringAdapter.java new file mode 100644 index 0000000..ded517e --- /dev/null +++ b/src/main/java/com/iciql/adapter/postgres/XmlStringAdapter.java @@ -0,0 +1,55 @@ +/* + * Copyright 2014 James Moger. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.iciql.adapter.postgres; + +import java.sql.SQLException; + +import org.postgresql.util.PGobject; + +import com.iciql.Iciql.DataTypeAdapter; + +/** + * 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 diff --git a/src/site/dta.mkd b/src/site/dta.mkd index 61c5f62..6795a0f 100644 --- a/src/site/dta.mkd +++ b/src/site/dta.mkd @@ -93,12 +93,22 @@ public @interface InvoiceAdapter { } Not every DataTypeAdapter has to be a custom implementation.
The following adapters are included in Iciql for general purpose use.
-- `com.iciql.JavaSerializationTypeAdapter`
+- `com.iciql.adapter.JavaSerializationTypeAdapter`
Uses Java serialization to store/retrieve your objects as BLOBs.
-- `com.iciql.SQLDialectPostgreSQL.JsonStringAdapter`
+- `com.iciql.adapter.GsonTypeAdapter`
+Uses Google Gson to store/retrieve your objects as TEXT.
+- `com.iciql.adapter.XmlTypeAdapter`
+Uses XStream to store/retrieve your objects as TEXT.
+- `com.iciql.adapter.postgres.GsonTypeAdapter`
+Uses Google Gson to store/retrieve your objects as JSON for Postgres.
+- `com.iciql.adapter.postgres.GsonBTypeAdapter`
+Uses Google Gson to store/retrieve your objects as JSONB for Postgres.
+- `com.iciql.adapter.postgres.XStreamTypeAdapter`
+Uses XStream to store/retrieve your objects as XML for Postgres.
+- `com.iciql.adapter.postgres.JsonStringAdapter`
Allows you to use the Postgres JSON data type with a standard String.
-- `com.iciql.SQLDialectPostgreSQL.JsonbStringAdapter`
+- `com.iciql.adapter.postgres.JsonbStringAdapter`
Allows you to use the Postgres JSONB data type with a standard String.
-- `com.iciql.SQLDialectPostgreSQL.XmlStringAdapter`
+- `com.iciql.adapter.postgres.XmlStringAdapter`
Allows you to use the Postgres XML data type with a standard String.
diff --git a/src/test/java/com/iciql/test/DataTypeAdapterTest.java b/src/test/java/com/iciql/test/DataTypeAdapterTest.java index d1ccb53..1f0e771 100644 --- a/src/test/java/com/iciql/test/DataTypeAdapterTest.java +++ b/src/test/java/com/iciql/test/DataTypeAdapterTest.java @@ -31,7 +31,7 @@ import com.iciql.Db; import com.iciql.Iciql.IQColumn; import com.iciql.Iciql.IQTable; import com.iciql.Iciql.TypeAdapter; -import com.iciql.JavaSerializationTypeAdapter; +import com.iciql.adapter.JavaSerializationTypeAdapter; import com.iciql.test.models.SupportedTypes; /** |