|
|
|
@ -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;
|
|
|
|
@ -51,8 +55,23 @@ 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,16 +80,25 @@ public class SqlExecutor {
|
|
|
|
|
@Cleanup
|
|
|
|
|
PreparedStatement stmt = conn.prepareStatement(sql);
|
|
|
|
|
|
|
|
|
|
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 || object instanceof Long) {
|
|
|
|
|
stmt.setLong(parameterIndex++, (Long) object);
|
|
|
|
|
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++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Cleanup
|
|
|
|
|