Add JSON parsing for queries
This commit is contained in:
		@@ -1,5 +1,6 @@
 | 
				
			|||||||
package at.compax.tools.sql;
 | 
					package at.compax.tools.sql;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.io.IOException;
 | 
				
			||||||
import java.sql.Connection;
 | 
					import java.sql.Connection;
 | 
				
			||||||
import java.sql.PreparedStatement;
 | 
					import java.sql.PreparedStatement;
 | 
				
			||||||
import java.sql.ResultSet;
 | 
					import java.sql.ResultSet;
 | 
				
			||||||
@@ -11,7 +12,10 @@ import java.sql.Types;
 | 
				
			|||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
import java.util.Vector;
 | 
					import java.util.Vector;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.fasterxml.jackson.core.JsonParseException;
 | 
				
			||||||
import com.fasterxml.jackson.core.JsonProcessingException;
 | 
					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.QueryParameter;
 | 
				
			||||||
import at.compax.tools.sql.model.QueryResult;
 | 
					import at.compax.tools.sql.model.QueryResult;
 | 
				
			||||||
@@ -51,8 +55,23 @@ public class SqlExecutor {
 | 
				
			|||||||
		return resultBuilder.build();
 | 
							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 {
 | 
						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 {
 | 
						public static QueryResult executeQuery(Connection conn, String sql, Object... parameters) throws SQLException {
 | 
				
			||||||
@@ -61,16 +80,25 @@ public class SqlExecutor {
 | 
				
			|||||||
		@Cleanup
 | 
							@Cleanup
 | 
				
			||||||
		PreparedStatement stmt = conn.prepareStatement(sql);
 | 
							PreparedStatement stmt = conn.prepareStatement(sql);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (parameters != null) {
 | 
				
			||||||
			int parameterIndex = 1;
 | 
								int parameterIndex = 1;
 | 
				
			||||||
			for (Object object : parameters) {
 | 
								for (Object object : parameters) {
 | 
				
			||||||
 | 
									if (object != null) {
 | 
				
			||||||
					if (object instanceof String) {
 | 
										if (object instanceof String) {
 | 
				
			||||||
				stmt.setString(parameterIndex++, (String) object);
 | 
											stmt.setString(parameterIndex, (String) object);
 | 
				
			||||||
			} else if (object instanceof Integer || object instanceof Long) {
 | 
										} else if (object instanceof Integer) {
 | 
				
			||||||
				stmt.setLong(parameterIndex++, (Long) object);
 | 
											stmt.setInt(parameterIndex, (Integer) object);
 | 
				
			||||||
 | 
										} else if (object instanceof Long) {
 | 
				
			||||||
 | 
											stmt.setLong(parameterIndex, (Long) object);
 | 
				
			||||||
					} else {
 | 
										} else {
 | 
				
			||||||
						String message = "Unhandled paramter type: " + object.getClass().getName();
 | 
											String message = "Unhandled paramter type: " + object.getClass().getName();
 | 
				
			||||||
						throw new IllegalArgumentException(message);
 | 
											throw new IllegalArgumentException(message);
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
 | 
									} else {
 | 
				
			||||||
 | 
										log.warn("parameter is null");
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									parameterIndex++;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		@Cleanup
 | 
							@Cleanup
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -17,6 +17,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import at.compax.tools.sql.SqlExecutionException;
 | 
					import at.compax.tools.sql.SqlExecutionException;
 | 
				
			||||||
import at.compax.tools.sql.SqlExecutor;
 | 
					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.QueryResult;
 | 
				
			||||||
import at.compax.tools.sql.model.Result;
 | 
					import at.compax.tools.sql.model.Result;
 | 
				
			||||||
import at.compax.tools.sql.model.UserError;
 | 
					import at.compax.tools.sql.model.UserError;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user