Add JSON parsing for queries
This commit is contained in:
		@@ -1,5 +1,6 @@
 | 
			
		||||
package at.compax.tools.sql;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.sql.Connection;
 | 
			
		||||
import java.sql.PreparedStatement;
 | 
			
		||||
import java.sql.ResultSet;
 | 
			
		||||
@@ -11,7 +12,10 @@ import java.sql.Types;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Vector;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.core.JsonParseException;
 | 
			
		||||
import com.fasterxml.jackson.core.JsonProcessingException;
 | 
			
		||||
import com.fasterxml.jackson.databind.JsonMappingException;
 | 
			
		||||
import com.fasterxml.jackson.databind.ObjectMapper;
 | 
			
		||||
 | 
			
		||||
import at.compax.tools.sql.model.QueryParameter;
 | 
			
		||||
import at.compax.tools.sql.model.QueryResult;
 | 
			
		||||
@@ -50,9 +54,24 @@ public class SqlExecutor {
 | 
			
		||||
 | 
			
		||||
		return resultBuilder.build();
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
	public static QueryResult executeJsonQuery(Connection conn, String json)
 | 
			
		||||
			throws SQLException, JsonParseException, JsonMappingException, IOException {
 | 
			
		||||
		log.debug("Parsing JSON: " + json);
 | 
			
		||||
 | 
			
		||||
		ObjectMapper mapper = new ObjectMapper();
 | 
			
		||||
		QueryParameter queryParameter = mapper.readValue(json, QueryParameter.class);
 | 
			
		||||
		log.debug("Parsed QueryParamter: " + queryParameter.toString());
 | 
			
		||||
 | 
			
		||||
		return executeQuery(conn, queryParameter);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public static QueryResult executeQuery(Connection conn, QueryParameter parameters) throws SQLException {
 | 
			
		||||
		return executeQuery(conn, parameters.getSql(), parameters.getParameters());
 | 
			
		||||
		if (parameters == null || parameters.getParameters() == null) {
 | 
			
		||||
			return executeQuery(conn, parameters.getSql());
 | 
			
		||||
		} else {
 | 
			
		||||
			return executeQuery(conn, parameters.getSql(), parameters.getParameters().toArray());
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public static QueryResult executeQuery(Connection conn, String sql, Object... parameters) throws SQLException {
 | 
			
		||||
@@ -61,15 +80,24 @@ public class SqlExecutor {
 | 
			
		||||
		@Cleanup
 | 
			
		||||
		PreparedStatement stmt = conn.prepareStatement(sql);
 | 
			
		||||
 | 
			
		||||
		int parameterIndex = 1;
 | 
			
		||||
		for (Object object : parameters) {
 | 
			
		||||
			if (object instanceof String) {
 | 
			
		||||
				stmt.setString(parameterIndex++, (String) object);
 | 
			
		||||
			} else if (object instanceof Integer || object instanceof Long) {
 | 
			
		||||
				stmt.setLong(parameterIndex++, (Long) object);
 | 
			
		||||
			} else {
 | 
			
		||||
				String message = "Unhandled paramter type: " + object.getClass().getName();
 | 
			
		||||
				throw new IllegalArgumentException(message);
 | 
			
		||||
		if (parameters != null) {
 | 
			
		||||
			int parameterIndex = 1;
 | 
			
		||||
			for (Object object : parameters) {
 | 
			
		||||
				if (object != null) {
 | 
			
		||||
					if (object instanceof String) {
 | 
			
		||||
						stmt.setString(parameterIndex, (String) object);
 | 
			
		||||
					} else if (object instanceof Integer) {
 | 
			
		||||
						stmt.setInt(parameterIndex, (Integer) object);
 | 
			
		||||
					} else if (object instanceof Long) {
 | 
			
		||||
						stmt.setLong(parameterIndex, (Long) object);
 | 
			
		||||
					} else {
 | 
			
		||||
						String message = "Unhandled paramter type: " + object.getClass().getName();
 | 
			
		||||
						throw new IllegalArgumentException(message);
 | 
			
		||||
					}
 | 
			
		||||
				} else {
 | 
			
		||||
					log.warn("parameter is null");
 | 
			
		||||
				}
 | 
			
		||||
				parameterIndex++;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -17,6 +17,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 | 
			
		||||
 | 
			
		||||
import at.compax.tools.sql.SqlExecutionException;
 | 
			
		||||
import at.compax.tools.sql.SqlExecutor;
 | 
			
		||||
import at.compax.tools.sql.model.QueryParameter;
 | 
			
		||||
import at.compax.tools.sql.model.QueryResult;
 | 
			
		||||
import at.compax.tools.sql.model.Result;
 | 
			
		||||
import at.compax.tools.sql.model.UserError;
 | 
			
		||||
@@ -119,7 +120,7 @@ public class Main {
 | 
			
		||||
		LogManager.getLogger("at.compax.tools.sql.main").setLevel(Level.ALL);
 | 
			
		||||
		LogManager.getLogger("at.compax.tools.sql").setLevel(Level.ALL);
 | 
			
		||||
		ObjectMapper mapper = new ObjectMapper();
 | 
			
		||||
		
 | 
			
		||||
 | 
			
		||||
		if (args.length == 0) {
 | 
			
		||||
			runTests();
 | 
			
		||||
		} else {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user