Added updates
This commit is contained in:
@@ -179,6 +179,56 @@ public class SqlExecutor {
|
|||||||
|
|
||||||
return queryResultBuilder.rows(rows).build();
|
return queryResultBuilder.rows(rows).build();
|
||||||
}
|
}
|
||||||
|
public static QueryResult executeUpdate(Connection conn, String sql, Object... parameters) throws SQLException {
|
||||||
|
log.trace("SQL: " + sql.trim());
|
||||||
|
String updateQuery = sql.replaceAll(";$", "").trim();
|
||||||
|
|
||||||
|
@Cleanup
|
||||||
|
PreparedStatement stmt = conn.prepareStatement(updateQuery);
|
||||||
|
|
||||||
|
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 {
|
||||||
|
stmt.setString(parameterIndex, (String) object);
|
||||||
|
String message = String.format("Unhandled paramter type <%s>. Defaulting to String", object.getClass().getName());
|
||||||
|
log.warn(message);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.warn("parameter is null");
|
||||||
|
}
|
||||||
|
parameterIndex++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
long affectedRows = 0L;
|
||||||
|
QueryResultBuilder queryResultBuilder = QueryResult.builder();
|
||||||
|
try {
|
||||||
|
affectedRows = stmt.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
return queryResultBuilder //
|
||||||
|
.exception(e.getClass().getName()) //
|
||||||
|
.exceptionMessage(e.getMessage()) //
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
handleUserErrors(conn, sql, stmt);
|
||||||
|
} catch (SqlExecutionException e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
|
||||||
|
return queryResultBuilder.userErrors(e.getUserErrors()).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
return queryResultBuilder.affectedRows(affectedRows).build();
|
||||||
|
}
|
||||||
|
|
||||||
private static Vector<ColumnDefinition> getColumnDefinitions(ResultSetMetaData metaData) throws SQLException {
|
private static Vector<ColumnDefinition> getColumnDefinitions(ResultSetMetaData metaData) throws SQLException {
|
||||||
Vector<ColumnDefinition> columnDefinitions = new Vector<ColumnDefinition>();
|
Vector<ColumnDefinition> columnDefinitions = new Vector<ColumnDefinition>();
|
||||||
|
@@ -152,6 +152,8 @@ public class Main {
|
|||||||
String result;
|
String result;
|
||||||
if (query.toLowerCase().startsWith("select")) {
|
if (query.toLowerCase().startsWith("select")) {
|
||||||
result = formatJsonObject(SqlExecutor.executeQuery(conn, query, null));
|
result = formatJsonObject(SqlExecutor.executeQuery(conn, query, null));
|
||||||
|
} else if (query.toLowerCase().startsWith("update") || query.toLowerCase().startsWith("insert")) {
|
||||||
|
result = formatJsonObject(SqlExecutor.executeUpdate(conn, query));
|
||||||
} else {
|
} else {
|
||||||
result = formatJsonObject(SqlExecutor.execute(conn, query));
|
result = formatJsonObject(SqlExecutor.execute(conn, query));
|
||||||
}
|
}
|
||||||
|
@@ -44,4 +44,5 @@ public class QueryResult {
|
|||||||
private List<UserError> userErrors;
|
private List<UserError> userErrors;
|
||||||
private String exception;
|
private String exception;
|
||||||
private String exceptionMessage;
|
private String exceptionMessage;
|
||||||
|
private long affectedRows;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user