<classpathentry kind="lib" path="ext/slf4j-api-1.6.1.jar" sourcepath="ext/src/slf4j-api-1.6.1.jar" />
<classpathentry kind="lib" path="ext/commons-pool-1.5.6.jar" sourcepath="ext/src/commons-pool-1.5.6.jar" />
<classpathentry kind="lib" path="ext/commons-dbcp-1.4.jar" sourcepath="ext/src/commons-dbcp-1.4.jar" />
+ <classpathentry kind="lib" path="ext/gson-2.3.jar" sourcepath="ext/src/gson-2.3.jar" />
+ <classpathentry kind="lib" path="ext/xstream-1.4.7.jar" sourcepath="ext/src/xstream-1.4.7.jar" />
+ <classpathentry kind="lib" path="ext/xmlpull-1.1.3.1.jar" />
+ <classpathentry kind="lib" path="ext/xpp3_min-1.1.4c.jar" />
<classpathentry kind="lib" path="ext/junit-4.11.jar" sourcepath="ext/src/junit-4.11.jar" />
+ <classpathentry kind="lib" path="ext/jmock-1.0.1.jar" sourcepath="ext/src/jmock-1.0.1.jar" />
<classpathentry kind="lib" path="ext/hamcrest-core-1.3.jar" sourcepath="ext/src/hamcrest-core-1.3.jar" />
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER" />
<classpathentry kind="output" path="bin/classes" />
- provided 'org.slf4j:slf4j-api:1.6.1'
- provided 'commons-pool:commons-pool:1.5.6'
- provided 'commons-dbcp:commons-dbcp:1.4'
+- provided 'com.google.code.gson:gson:2.3'
+- provided 'com.thoughtworks.xstream:xstream:1.4.7'
- test 'junit'
- build 'jacoco'
+++ /dev/null
-/*
- * 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;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.sql.Blob;
-import java.sql.SQLException;
-
-import com.iciql.Iciql.DataTypeAdapter;
-
-/**
- * Base class for inserting/retrieving a Java Object as a BLOB field using Java Serialization.
- * <p>You use this by creating a subclass which defines your object class.</p>
- * <pre>
- * public class CustomObjectAdapter extends JavaSerializationTypeAdapter<CustomObject> {
- *
- * public Class<CustomObject> getJavaType() {
- * return CustomObject.class;
- * }
- * }
- * </pre>
- * @param <T>
- */
-public abstract class JavaSerializationTypeAdapter<T> implements DataTypeAdapter<T> {
-
- @Override
- public final String getDataType() {
- return "BLOB";
- }
-
- @Override
- public final Object serialize(T value) {
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- try {
- new ObjectOutputStream(os).writeObject(value);
- return os.toByteArray();
- } catch (IOException e) {
- throw new IciqlException(e);
- } finally {
- try {
- os.close();
- } catch (IOException e) {
- throw new IciqlException (e);
- }
- }
- }
-
- @SuppressWarnings("unchecked")
- @Override
- public final T deserialize(Object value) {
- InputStream is = null;
- if (value instanceof Blob) {
- Blob blob = (Blob) value;
- try {
- is = blob.getBinaryStream();
- } catch (SQLException e) {
- throw new IciqlException(e);
- }
- } else if (value instanceof byte[]) {
- byte [] bytes = (byte []) value;
- is = new ByteArrayInputStream(bytes);
- }
-
- try {
- T object = (T) new ObjectInputStream(is).readObject();
- return object;
- } catch (Exception e) {
- throw new IciqlException(e);
- } finally {
- if (is != null) {
- try {
- is.close();
- } catch (IOException e) {
- throw new IciqlException (e);
- }
- }
- }
- }
-}
\r
package com.iciql;\r
\r
-import java.sql.SQLException;\r
-\r
-import org.postgresql.util.PGobject;\r
-\r
-import com.iciql.Iciql.DataTypeAdapter;\r
import com.iciql.TableDefinition.IndexDefinition;\r
import com.iciql.util.StatementBuilder;\r
\r
stat.setSQL(buff.toString().trim());\r
}\r
\r
- /**\r
- * Handles transforming raw strings to/from the Postgres JSON data type.\r
- */\r
- public class JsonStringAdapter implements DataTypeAdapter<String> {\r
-\r
- @Override\r
- public String getDataType() {\r
- return "json";\r
- }\r
-\r
- @Override\r
- public Class<String> getJavaType() {\r
- return String.class;\r
- }\r
-\r
- @Override\r
- public Object serialize(String value) {\r
- PGobject pg = new PGobject();\r
- pg.setType(getDataType());\r
- try {\r
- pg.setValue(value);\r
- } catch (SQLException e) {\r
- // not thrown on base PGobject\r
- }\r
- return pg;\r
- }\r
-\r
- @Override\r
- public String deserialize(Object value) {\r
- return value.toString();\r
- }\r
- }\r
-\r
- /**\r
- * Handles transforming raw strings to/from the Postgres JSONB data type.\r
- */\r
- public class JsonbStringAdapter implements DataTypeAdapter<String> {\r
-\r
- @Override\r
- public String getDataType() {\r
- return "jsonb";\r
- }\r
-\r
- @Override\r
- public Class<String> getJavaType() {\r
- return String.class;\r
- }\r
-\r
- @Override\r
- public Object serialize(String value) {\r
- PGobject pg = new PGobject();\r
- pg.setType(getDataType());\r
- try {\r
- pg.setValue(value);\r
- } catch (SQLException e) {\r
- // not thrown on base PGobject\r
- }\r
- return pg;\r
- }\r
-\r
- @Override\r
- public String deserialize(Object value) {\r
- return value.toString();\r
- }\r
- }\r
-\r
- /**\r
- * Handles transforming raw strings to/from the Postgres XML data type.\r
- */\r
- public class XmlStringAdapter implements DataTypeAdapter<String> {\r
-\r
- @Override\r
- public String getDataType() {\r
- return "xml";\r
- }\r
-\r
- @Override\r
- public Class<String> getJavaType() {\r
- return String.class;\r
- }\r
-\r
- @Override\r
- public Object serialize(String value) {\r
- PGobject pg = new PGobject();\r
- pg.setType(getDataType());\r
- try {\r
- pg.setValue(value);\r
- } catch (SQLException e) {\r
- // not thrown on base PGobject\r
- }\r
- return pg;\r
- }\r
-\r
- @Override\r
- public String deserialize(Object value) {\r
- return value.toString();\r
- }\r
- }\r
}
\ No newline at end of file
--- /dev/null
+/*
+ * 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;
+ }
+}
--- /dev/null
+/*
+ * 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 java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+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;
+
+/**
+ * Base class for inserting/retrieving a Java Object as a BLOB field using Java Serialization.
+ * <p>You use this by creating a subclass which defines your object class.</p>
+ * <pre>
+ * public class CustomObjectAdapter extends JavaSerializationTypeAdapter<CustomObject> {
+ *
+ * public Class<CustomObject> getJavaType() {
+ * return CustomObject.class;
+ * }
+ * }
+ * </pre>
+ * @param <T>
+ */
+public abstract class JavaSerializationTypeAdapter<T> implements DataTypeAdapter<T> {
+
+ @Override
+ public final String getDataType() {
+ return "BLOB";
+ }
+
+ @Override
+ public final Object serialize(T value) {
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ try {
+ new ObjectOutputStream(os).writeObject(value);
+ return os.toByteArray();
+ } catch (IOException e) {
+ throw new IciqlException(e);
+ } finally {
+ try {
+ os.close();
+ } catch (IOException e) {
+ throw new IciqlException (e);
+ }
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public final T deserialize(Object value) {
+ InputStream is = null;
+ if (value instanceof Blob) {
+ Blob blob = (Blob) value;
+ try {
+ is = blob.getBinaryStream();
+ } catch (SQLException e) {
+ throw new IciqlException(e);
+ }
+ } else if (value instanceof byte[]) {
+ byte [] bytes = (byte []) value;
+ is = new ByteArrayInputStream(bytes);
+ }
+
+ try {
+ T object = (T) new ObjectInputStream(is).readObject();
+ return object;
+ } catch (Exception e) {
+ throw new IciqlException(e);
+ } finally {
+ if (is != null) {
+ try {
+ is.close();
+ } catch (IOException e) {
+ throw new IciqlException (e);
+ }
+ }
+ }
+ }
+}
--- /dev/null
+/*
+ * 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;
+ }
+}
--- /dev/null
+/*
+ * 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;
+ }
+}
--- /dev/null
+/*
+ * 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;
+ }
+}
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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";
+ }
+
+}
--- /dev/null
+/*
+ * 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
Not every DataTypeAdapter has to be a custom implementation.\r
The following adapters are included in Iciql for general purpose use.\r
\r
-- `com.iciql.JavaSerializationTypeAdapter`\r
+- `com.iciql.adapter.JavaSerializationTypeAdapter`\r
Uses Java serialization to store/retrieve your objects as BLOBs.\r
-- `com.iciql.SQLDialectPostgreSQL.JsonStringAdapter`\r
+- `com.iciql.adapter.GsonTypeAdapter`\r
+Uses Google Gson to store/retrieve your objects as TEXT.\r
+- `com.iciql.adapter.XmlTypeAdapter`\r
+Uses XStream to store/retrieve your objects as TEXT.\r
+- `com.iciql.adapter.postgres.GsonTypeAdapter`\r
+Uses Google Gson to store/retrieve your objects as JSON for Postgres.\r
+- `com.iciql.adapter.postgres.GsonBTypeAdapter`\r
+Uses Google Gson to store/retrieve your objects as JSONB for Postgres.\r
+- `com.iciql.adapter.postgres.XStreamTypeAdapter`\r
+Uses XStream to store/retrieve your objects as XML for Postgres.\r
+- `com.iciql.adapter.postgres.JsonStringAdapter`\r
Allows you to use the Postgres JSON data type with a standard String.\r
-- `com.iciql.SQLDialectPostgreSQL.JsonbStringAdapter`\r
+- `com.iciql.adapter.postgres.JsonbStringAdapter`\r
Allows you to use the Postgres JSONB data type with a standard String.\r
-- `com.iciql.SQLDialectPostgreSQL.XmlStringAdapter`\r
+- `com.iciql.adapter.postgres.XmlStringAdapter`\r
Allows you to use the Postgres XML data type with a standard String.\r
\r
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;
/**