From 4b9a61d0ef0fc2a9230a53a0ade45a20889aa9e3 Mon Sep 17 00:00:00 2001 From: James Moger Date: Sun, 9 Nov 2014 13:33:27 -0500 Subject: [PATCH] Revise built-in type adapter class names again, add Yaml type adapter --- .classpath | 1 + build.moxie | 1 + releases.moxie | 3 +- .../com/iciql/adapter/GsonTypeAdapter.java | 15 ++-- .../iciql/adapter/SnakeYamlTypeAdapter.java | 55 +++++++++++++++ .../com/iciql/adapter/XStreamTypeAdapter.java | 24 +++---- .../JsonObjectAdapter.java} | 9 ++- .../JsonStringAdapter.java | 62 ++++++++-------- .../JsonbObjectAdapter.java} | 7 +- .../JsonbStringAdapter.java | 2 +- .../XmlObjectAdapter.java} | 10 +-- .../XmlStringAdapter.java | 2 +- src/site/dta.mkd | 70 ++++++++++++------- src/site/index.mkd | 14 +++- 14 files changed, 183 insertions(+), 92 deletions(-) create mode 100644 src/main/java/com/iciql/adapter/SnakeYamlTypeAdapter.java rename src/main/java/com/iciql/adapter/{postgres/GsonTypeAdapter.java => postgresql/JsonObjectAdapter.java} (85%) rename src/main/java/com/iciql/adapter/{postgres => postgresql}/JsonStringAdapter.java (52%) rename src/main/java/com/iciql/adapter/{postgres/GsonBTypeAdapter.java => postgresql/JsonbObjectAdapter.java} (88%) rename src/main/java/com/iciql/adapter/{postgres => postgresql}/JsonbStringAdapter.java (97%) rename src/main/java/com/iciql/adapter/{postgres/XStreamTypeAdapter.java => postgresql/XmlObjectAdapter.java} (81%) rename src/main/java/com/iciql/adapter/{postgres => postgresql}/XmlStringAdapter.java (97%) diff --git a/.classpath b/.classpath index db0a904..8f40afa 100644 --- a/.classpath +++ b/.classpath @@ -18,6 +18,7 @@ + diff --git a/build.moxie b/build.moxie index 0595b2d..98fd63e 100644 --- a/build.moxie +++ b/build.moxie @@ -103,5 +103,6 @@ dependencies: - provided 'commons-dbcp:commons-dbcp:1.4' - provided 'com.google.code.gson:gson:2.3' - provided 'com.thoughtworks.xstream:xstream:1.4.7' +- provided 'org.yaml:snakeyaml:1.14' - test 'junit' - build 'jacoco' diff --git a/releases.moxie b/releases.moxie index 7f829f5..6e271c7 100644 --- a/releases.moxie +++ b/releases.moxie @@ -14,7 +14,8 @@ r23: { - Improved automatic date conversions - Revised data type adapters to be specified separately with the @TypeAdapter annotation additions: - - Add a Dao feature similar to JDBI + - Add a DAO feature similar to JDBI + - Added Gson, XStream, and SnakeYaml type adapters dependencyChanges: ~ contributors: - James Moger diff --git a/src/main/java/com/iciql/adapter/GsonTypeAdapter.java b/src/main/java/com/iciql/adapter/GsonTypeAdapter.java index a85f15e..b07b7b5 100644 --- a/src/main/java/com/iciql/adapter/GsonTypeAdapter.java +++ b/src/main/java/com/iciql/adapter/GsonTypeAdapter.java @@ -20,16 +20,21 @@ 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. - *

You use this by creating a subclass which defines your object class.

+ * Base class for inserting/retrieving a Java Object (de)serialized as JSON + * using Google GSON. + *

+ * You use this by creating a subclass which defines your object class. + *

+ * *
  * public class CustomObjectAdapter extends GsonTypeAdapter<CustomObject> {
  *
- *    public Class<CustomObject> getJavaType() {
- *        return CustomObject.class;
- *    }
+ * 	public Class<CustomObject> getJavaType() {
+ * 		return CustomObject.class;
+ * 	}
  * }
  * 
+ * * @param */ public abstract class GsonTypeAdapter implements DataTypeAdapter { diff --git a/src/main/java/com/iciql/adapter/SnakeYamlTypeAdapter.java b/src/main/java/com/iciql/adapter/SnakeYamlTypeAdapter.java new file mode 100644 index 0000000..fe4541c --- /dev/null +++ b/src/main/java/com/iciql/adapter/SnakeYamlTypeAdapter.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; + +import org.yaml.snakeyaml.DumperOptions.FlowStyle; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.nodes.Tag; + +import com.iciql.Iciql.DataTypeAdapter; + +/** + * Base class for inserting/retrieving a Java Object (de)serialized as YAML using SnakeYaml. + */ +public abstract class SnakeYamlTypeAdapter implements DataTypeAdapter { + + protected Yaml yaml() { + return new Yaml(); + } + + @Override + public String getDataType() { + return "TEXT"; + } + + @Override + public abstract Class getJavaType(); + + @Override + public Object serialize(Object value) { + return yaml().dumpAs(value, Tag.MAP, FlowStyle.BLOCK); + } + + @Override + public T deserialize(Object value) { + String yaml = value.toString(); + Yaml processor = yaml(); + T t = processor.loadAs(yaml, getJavaType()); + return t; + } + +} diff --git a/src/main/java/com/iciql/adapter/XStreamTypeAdapter.java b/src/main/java/com/iciql/adapter/XStreamTypeAdapter.java index 3d1a9ce..5e152df 100644 --- a/src/main/java/com/iciql/adapter/XStreamTypeAdapter.java +++ b/src/main/java/com/iciql/adapter/XStreamTypeAdapter.java @@ -21,18 +21,8 @@ import com.thoughtworks.xstream.XStream; /** * Base class for inserting/retrieving a Java Object (de)serialized as XML using XStream. - *

You use this by creating a subclass which defines your object class.

- *
- * public class CustomObjectAdapter extends XStreamTypeAdapter<CustomObject> {
- *
- *    public Class<CustomObject> getJavaType() {
- *        return CustomObject.class;
- *    }
- * }
- * 
- * @param */ -public abstract class XStreamTypeAdapter implements DataTypeAdapter { +public class XStreamTypeAdapter implements DataTypeAdapter { protected XStream xstream() { return new XStream(); @@ -44,15 +34,21 @@ public abstract class XStreamTypeAdapter implements DataTypeAdapter { } @Override - public Object serialize(T value) { + public Class getJavaType() { + return Object.class; + } + + @Override + public Object serialize(Object value) { return xstream().toXML(value); } @Override - public T deserialize(Object value) { + public Object deserialize(Object value) { String xml = value.toString(); XStream xstream = xstream(); - T t = (T) xstream.fromXML(xml); + Object t = xstream.fromXML(xml); return t; } + } diff --git a/src/main/java/com/iciql/adapter/postgres/GsonTypeAdapter.java b/src/main/java/com/iciql/adapter/postgresql/JsonObjectAdapter.java similarity index 85% rename from src/main/java/com/iciql/adapter/postgres/GsonTypeAdapter.java rename to src/main/java/com/iciql/adapter/postgresql/JsonObjectAdapter.java index 64481bd..118cfc1 100644 --- a/src/main/java/com/iciql/adapter/postgres/GsonTypeAdapter.java +++ b/src/main/java/com/iciql/adapter/postgresql/JsonObjectAdapter.java @@ -13,20 +13,23 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.iciql.adapter.postgres; +package com.iciql.adapter.postgresql; import java.sql.SQLException; import org.postgresql.util.PGobject; +import com.iciql.adapter.GsonTypeAdapter; + /** - * Postgres JSON data type adapter maps a JSON column to a domain object using Google GSON. + * Postgres JSON data type adapter maps a JSON column to a domain object using + * Google GSON. * * @author James Moger * * @param */ -public abstract class GsonTypeAdapter extends com.iciql.adapter.GsonTypeAdapter { +public abstract class JsonObjectAdapter extends GsonTypeAdapter { @Override public String getDataType() { diff --git a/src/main/java/com/iciql/adapter/postgres/JsonStringAdapter.java b/src/main/java/com/iciql/adapter/postgresql/JsonStringAdapter.java similarity index 52% rename from src/main/java/com/iciql/adapter/postgres/JsonStringAdapter.java rename to src/main/java/com/iciql/adapter/postgresql/JsonStringAdapter.java index 64ad073..c4fbd6a 100644 --- a/src/main/java/com/iciql/adapter/postgres/JsonStringAdapter.java +++ b/src/main/java/com/iciql/adapter/postgresql/JsonStringAdapter.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.iciql.adapter.postgres; +package com.iciql.adapter.postgresql; import java.sql.SQLException; @@ -22,34 +22,34 @@ 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 { - - @Override - public String getDataType() { - return "json"; - } - - @Override - public Class 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 JSON data type. + */ +public class JsonStringAdapter implements DataTypeAdapter { + + @Override + public String getDataType() { + return "json"; + } + + @Override + public Class 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 } - } \ No newline at end of file + 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/GsonBTypeAdapter.java b/src/main/java/com/iciql/adapter/postgresql/JsonbObjectAdapter.java similarity index 88% rename from src/main/java/com/iciql/adapter/postgres/GsonBTypeAdapter.java rename to src/main/java/com/iciql/adapter/postgresql/JsonbObjectAdapter.java index 6332742..6d8ad2c 100644 --- a/src/main/java/com/iciql/adapter/postgres/GsonBTypeAdapter.java +++ b/src/main/java/com/iciql/adapter/postgresql/JsonbObjectAdapter.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.iciql.adapter.postgres; +package com.iciql.adapter.postgresql; import java.sql.SQLException; @@ -22,13 +22,14 @@ 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. + * Postgres JSONB data type adapter maps a JSONB column to a domain object using + * Google GSON. * * @author James Moger * * @param */ -public abstract class GsonBTypeAdapter extends GsonTypeAdapter { +public abstract class JsonbObjectAdapter extends GsonTypeAdapter { @Override public String getDataType() { diff --git a/src/main/java/com/iciql/adapter/postgres/JsonbStringAdapter.java b/src/main/java/com/iciql/adapter/postgresql/JsonbStringAdapter.java similarity index 97% rename from src/main/java/com/iciql/adapter/postgres/JsonbStringAdapter.java rename to src/main/java/com/iciql/adapter/postgresql/JsonbStringAdapter.java index 1619811..cc2d9c4 100644 --- a/src/main/java/com/iciql/adapter/postgres/JsonbStringAdapter.java +++ b/src/main/java/com/iciql/adapter/postgresql/JsonbStringAdapter.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.iciql.adapter.postgres; +package com.iciql.adapter.postgresql; import java.sql.SQLException; diff --git a/src/main/java/com/iciql/adapter/postgres/XStreamTypeAdapter.java b/src/main/java/com/iciql/adapter/postgresql/XmlObjectAdapter.java similarity index 81% rename from src/main/java/com/iciql/adapter/postgres/XStreamTypeAdapter.java rename to src/main/java/com/iciql/adapter/postgresql/XmlObjectAdapter.java index 1bf6fc2..eba7c06 100644 --- a/src/main/java/com/iciql/adapter/postgres/XStreamTypeAdapter.java +++ b/src/main/java/com/iciql/adapter/postgresql/XmlObjectAdapter.java @@ -14,18 +14,18 @@ * limitations under the License. */ -package com.iciql.adapter.postgres; - +package com.iciql.adapter.postgresql; +import com.iciql.adapter.XStreamTypeAdapter; /** - * Postgres XML data type adapter maps an XML column to a domain object using XStream. + * Postgres XML data type adapter maps an XML column to a domain object using + * XStream. * * @author James Moger * - * @param */ -public abstract class XStreamTypeAdapter extends com.iciql.adapter.XStreamTypeAdapter { +public abstract class XmlObjectAdapter extends XStreamTypeAdapter { @Override public String getDataType() { diff --git a/src/main/java/com/iciql/adapter/postgres/XmlStringAdapter.java b/src/main/java/com/iciql/adapter/postgresql/XmlStringAdapter.java similarity index 97% rename from src/main/java/com/iciql/adapter/postgres/XmlStringAdapter.java rename to src/main/java/com/iciql/adapter/postgresql/XmlStringAdapter.java index ded517e..defe9f9 100644 --- a/src/main/java/com/iciql/adapter/postgres/XmlStringAdapter.java +++ b/src/main/java/com/iciql/adapter/postgresql/XmlStringAdapter.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.iciql.adapter.postgres; +package com.iciql.adapter.postgresql; import java.sql.SQLException; diff --git a/src/site/dta.mkd b/src/site/dta.mkd index 6795a0f..70fc25e 100644 --- a/src/site/dta.mkd +++ b/src/site/dta.mkd @@ -50,7 +50,7 @@ public class InvoiceAdapterImpl implements DataTypeAdapter { } @Override - public Object serialize(DcKey value) { + public Object serialize(Invoice value) { String json = gson().toJson(value); PGobject pg = new PGobject(); pg.setType(getDataType()); @@ -70,7 +70,8 @@ public class InvoiceAdapterImpl implements DataTypeAdapter { final Invoice invoice = gson().fromJson(json, getJavaType()); return invoice; - } + } +} ---JAVA--- Here you can see how the *InvoiceTypeAdapter* defines a [Postgres JSONB data type](http://www.postgresql.org/docs/9.4/static/datatype-json.html) and automatically handles JSON (de)serialization with [Google Gson](https://code.google.com/p/google-gson) so that the database gets the content in a form that it requires but we can continue to work with objects in Java. @@ -88,27 +89,46 @@ To simplify this, you can implement your own annotation which specifies your typ public @interface InvoiceAdapter { } ---JAVA--- -### Included DataTypeAdapters - -Not every DataTypeAdapter has to be a custom implementation. -The following adapters are included in Iciql for general purpose use. - -- `com.iciql.adapter.JavaSerializationTypeAdapter` -Uses Java serialization to store/retrieve your objects as BLOBs. -- `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.adapter.postgres.JsonbStringAdapter` -Allows you to use the Postgres JSONB data type with a standard String. -- `com.iciql.adapter.postgres.XmlStringAdapter` -Allows you to use the Postgres XML data type with a standard String. +### Included Data Type Adapters + +The following adapters are included in Iciql. They may require an optional dependency such as Gson, XStream, or SnakeYaml. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Common Type Adapters
AdapterJavaSQLDescription
com.iciql.adapter.JavaSerializationTypeAdapterObjectBLOBUses Java serialization to (de)serialize your object
com.iciql.adapter.GsonTypeAdapter<T><T>TEXT +Uses Google Gson to (de)serialize your object as JSON
com.iciql.adapter.XStreamTypeAdapterObjectTEXT +Uses XStream to (de)serialize your object as XML
com.iciql.adapter.SnakeYamlTypeAdapter<T><T>TEXT +Uses SnakeYaml to (de)serialize your object as YAML
PostgreSQL Type Adapters
Object AdaptersJavaSQLDescription
com.iciql.adapter.postgresql.JsonObjectAdapter<T><T>JSON +Uses Google Gson to (de)serialize your object as JSON
com.iciql.adapter.postgresql.JsonbObjectAdapter<T><T>JSONB +Uses Google Gson to (de)serialize your object as JSONB
com.iciql.adapter.postgresql.XmlObjectAdapterObjectXML +Uses XStream to (de)serialize your object as XML
String Adapters
com.iciql.adapter.postgresql.JsonStringAdapterStringJSON +Maps the JSON data type to a java.lang.String
com.iciql.adapter.postgresql.JsonbStringAdapterStringJSONB +Maps the JSONB data type to a java.lang.String
com.iciql.adapter.postgresql.XmlStringAdapterStringXML +Maps the XML data type to a java.lang.String
diff --git a/src/site/index.mkd b/src/site/index.mkd index a79233c..b9fc549 100644 --- a/src/site/index.mkd +++ b/src/site/index.mkd @@ -17,7 +17,7 @@ iciql **is not**... ### fluent, type-safe SQL DSL with rich object mapping -Born from the unfinished [JaQu][jaqu] subproject of H2 in August 2011, Iciql has advanced the codebase & DSL greatly. It supports more SQL syntax, more SQL data types, and all standard JDBC object types. +Born from the unfinished [JaQu][jaqu] subproject of H2 in August 2011, Iciql has [advanced the codebase](jaqu_comparison.html) & DSL greatly. It supports more SQL syntax, more SQL data types, and all standard JDBC object types. ---JAVA--- try (Db db = Db.open("jdbc:h2:mem:iciql")) { @@ -32,7 +32,7 @@ try (Db db = Db.open("jdbc:h2:mem:iciql")) { ### dynamic, annotated DAO with standard crud operations -Inspired by [JDBI](http://jdbi.org), Iciql offers a similar Dao feature. There are some clear benefits to using SQL directly rather than SQL-through-a-DSL so use them both where it makes the most sense for your need. +Inspired by JDBI, Iciql offers a similar [DAO feature](dao.html). There are some clear benefits to using SQL directly rather than SQL-through-a-DSL so use each one where it makes the mose sense. ---JAVA--- // Define your DAO with SQL annotations and optional type adapters @@ -83,6 +83,14 @@ try (Db db = Db.open("jdbc:h2:mem:iciql")) { } ---JAVA--- +### flexible field data types + +The [Data Type Adapter feature](dta.html) allows you to customize how your SQL column data types map to/from Java objects. + +This is very useful for mapping your field domain models to SQL without having to flatten them out to additional columns within your table. In other words, you can use your database as an object store at the column level by implementing a `@TypeAdapter` (de)serialization step. + +You might use this to take advantage of the underlying database's type system. For example, PostgreSQL ships with the compelling JSON/JSONB/XML data types. Iciql provides String and Object adapters to facilitate use of those data types. + ### Supported Databases (Unit-Tested) - [H2](http://h2database.com) ${h2.version} - [HSQLDB](http://hsqldb.org) ${hsqldb.version} @@ -103,7 +111,7 @@ iciql is distributed under the terms of the [Apache Software Foundation license, [jaqu]: http://h2database.com/html/jaqu.html "H2 JaQu project" [orm]: http://en.wikipedia.org/wiki/Object-relational_mapping "Object Relational Mapping" [jooq]: http://jooq.sourceforge.net "jOOQ" -[querydsl]: http://source.mysema.com/display/querydsl/Querydsl "Querydsl" +[querydsl]: http://source.mysema.com/display/querydsl/Querydsl "QueryDSL" [hibernate]: http://www.hibernate.org "Hibernate" [mybatis]: http://www.mybatis.org "mybatis" [github]: http://github.com/gitblit/iciql "iciql git repository" -- 2.39.5