Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #1 from turtlebender/feature/process-json-arrays-i…

…n-mysql

don't swallow exceptions and convert arrays to<->from json (if asJSON is...
  • Loading branch information...
commit e1cbe95a2a936744b383b9e0bf3963cacb6dc7d7 2 parents f821e7f + 6a416b8
@timf authored
View
4 pom.xml
@@ -3,7 +3,7 @@
<groupId>org.dasein</groupId>
<artifactId>dasein-persist</artifactId>
- <version>x2013.02.2</version>
+ <version>x2013.02.3</version>
<packaging>jar</packaging>
<name>dasein-persist</name>
@@ -77,7 +77,7 @@
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
- <version>5.1.10</version>
+ <version>5.1.29</version>
<scope>test</scope>
</dependency>
<dependency>
View
2  src/main/java/org/dasein/persist/Execution.java
@@ -376,7 +376,7 @@ else if( res instanceof HashMap ) {
}
catch( SQLException e ) {
logger.debug("Error executing event: " + e.getMessage(), e);
- throw new PersistenceException(e.getMessage());
+ throw new PersistenceException(e);
}
}
finally {
View
3  src/main/java/org/dasein/persist/PersistenceException.java
@@ -60,13 +60,14 @@ public PersistenceException(String msg) {
super(msg);
}
+
/**
* Constructs a new persistence exception that results from the
* specified data store exception.
* @param cse the cause for this persistence exception
*/
public PersistenceException(Exception cse) {
- super(cse.getMessage());
+ super(cse.getMessage(), cse);
cause = cse;
}
View
31 src/main/java/org/dasein/persist/jdbc/AutomatedSql.java
@@ -20,9 +20,10 @@
/* Copyright (c) 2006 Valtira Corporation, All Rights Reserved */
package org.dasein.persist.jdbc;
-import java.lang.reflect.Field;
-import java.lang.reflect.Modifier;
-import java.lang.reflect.ParameterizedType;
+import java.io.StringBufferInputStream;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.lang.reflect.*;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
@@ -45,6 +46,8 @@
import org.dasein.util.CachedItem;
import org.dasein.util.Translator;
import org.dasein.util.uom.Measured;
+import org.json.JSONArray;
+import org.json.JSONException;
public class AutomatedSql extends Execution {
static public enum Operator {
@@ -420,6 +423,28 @@ else if( t.equals(UUID.class) ) {
statement.setString(i, ob.toString());
}
}
+ else if( t.isArray() ){
+ try {
+ JSONArray serialized = new JSONArray();
+ Class<?> componentType = t.getComponentType();
+ Method toJson = componentType.getDeclaredMethod("toJSON");
+ int length = Array.getLength(ob);
+ for(int counter = 0 ; counter < length ; counter++){
+ serialized.put(toJson.invoke(Array.get(ob, counter)));
+ }
+ StringWriter writer = new StringWriter();
+ serialized.write(writer);
+ statement.setString(i, writer.toString());
+ } catch (NoSuchMethodException e) {
+ throw new SQLException("No toJSON method: " + t.getName(), e);
+ } catch (InvocationTargetException e) {
+ throw new SQLException("Error invoking toJSON method: " + t.getName(), e);
+ } catch (IllegalAccessException e) {
+ throw new SQLException("Error invoking toJSON method: " + t.getName(), e);
+ } catch (JSONException e) {
+ throw new SQLException("Error serializing to JSON: " + t.getName(), e);
+ }
+ }
else {
if( ob == null ) {
statement.setNull(i, Types.VARCHAR);
View
39 src/main/java/org/dasein/persist/jdbc/Loader.java
@@ -20,9 +20,7 @@
/* Copyright (c) 2006 Valtira Corporation, All Rights Reserved */
package org.dasein.persist.jdbc;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.Method;
-import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
@@ -40,6 +38,9 @@
import org.dasein.util.CachedItem;
import org.dasein.util.uom.Measured;
import org.dasein.util.uom.UnitOfMeasure;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
public class Loader extends AutomatedSql {
static public final Logger logger = Logger.getLogger(Loader.class);
@@ -349,6 +350,34 @@ else if( type.getName().startsWith("java.") ){
if( rs.wasNull() ) {
ob = null;
}
+ }else if(type.isArray()){
+ try {
+ JSONArray object = new JSONArray(rs.getString(i));
+ Class<?> componentType = type.getComponentType();
+ Method m = componentType.getDeclaredMethod("valueOf", JSONObject.class);
+ Object newArray = Array.newInstance(componentType, object.length());
+ for(int counter = 0 ; counter < object.length() ; counter++){
+ Array.set(newArray, counter, m.invoke(null, object.get(counter)));
+ }
+ ob = newArray;
+ } catch (JSONException e) {
+ throw new SQLException("Error parsing json: " + type.getName(), e);
+ } catch (NoSuchMethodException e) {
+ throw new SQLException(
+ "No valueOf function for loading JSON: " + type.getName(),
+ e
+ );
+ } catch (InvocationTargetException e) {
+ throw new SQLException(
+ "Failed to invoke valueOf function for loading JSON: " + type.getName(),
+ e
+ );
+ } catch (IllegalAccessException e) {
+ throw new SQLException(
+ "Failed to invoke valueOf function for loading JSON: " + type.getName(),
+ e
+ );
+ }
}
else {
String str = rs.getString(i);
@@ -359,11 +388,11 @@ else if( type.getName().startsWith("java.") ){
else {
try {
Method m = type.getDeclaredMethod("valueOf", String.class);
-
+
ob = m.invoke(null, str);
}
catch( Exception e ) {
- throw new SQLException("I have no idea how to map to " + type.getName());
+ throw new SQLException("I have no idea how to map to " + type.getName(), e);
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.