|
|
@ -1,6 +1,8 @@
|
|
|
|
package at.compax.tools.sql;
|
|
|
|
package at.compax.tools.sql;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import java.sql.CallableStatement;
|
|
|
|
|
|
|
|
import java.sql.Clob;
|
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.PreparedStatement;
|
|
|
|
import java.sql.PreparedStatement;
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
import java.sql.ResultSet;
|
|
|
@ -62,6 +64,8 @@ public class SqlExecutor {
|
|
|
|
resultBuilder.userErrors(e.getUserErrors());
|
|
|
|
resultBuilder.userErrors(e.getUserErrors());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
resultBuilder.dbOutput(parseDbOutput(conn));
|
|
|
|
|
|
|
|
|
|
|
|
return resultBuilder.build();
|
|
|
|
return resultBuilder.build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -162,7 +166,12 @@ public class SqlExecutor {
|
|
|
|
} else if (columnType == Types.TIMESTAMP) {
|
|
|
|
} else if (columnType == Types.TIMESTAMP) {
|
|
|
|
columnBuilder.value(rs.getTimestamp(columnIndex));
|
|
|
|
columnBuilder.value(rs.getTimestamp(columnIndex));
|
|
|
|
} else if (columnType == Types.CLOB) {
|
|
|
|
} else if (columnType == Types.CLOB) {
|
|
|
|
columnBuilder.value(rs.getClob(columnIndex).getSubString(1, 100));
|
|
|
|
Clob clob = rs.getClob(columnIndex);
|
|
|
|
|
|
|
|
if (clob != null) {
|
|
|
|
|
|
|
|
columnBuilder.value(clob.getSubString(1, 100));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
columnBuilder.value(null);
|
|
|
|
|
|
|
|
}
|
|
|
|
} else if (columnType == Types.BLOB) {
|
|
|
|
} else if (columnType == Types.BLOB) {
|
|
|
|
columnBuilder.value("[BLOB]");
|
|
|
|
columnBuilder.value("[BLOB]");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -179,6 +188,7 @@ 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 {
|
|
|
|
public static QueryResult executeUpdate(Connection conn, String sql, Object... parameters) throws SQLException {
|
|
|
|
log.trace("SQL: " + sql.trim());
|
|
|
|
log.trace("SQL: " + sql.trim());
|
|
|
|
String updateQuery = sql.replaceAll(";$", "").trim();
|
|
|
|
String updateQuery = sql.replaceAll(";$", "").trim();
|
|
|
@ -208,27 +218,27 @@ public class SqlExecutor {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
long affectedRows = 0L;
|
|
|
|
long affectedRows = 0L;
|
|
|
|
QueryResultBuilder queryResultBuilder = QueryResult.builder();
|
|
|
|
QueryResultBuilder queryResultBuilder = QueryResult.builder();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
affectedRows = stmt.executeUpdate();
|
|
|
|
affectedRows = stmt.executeUpdate();
|
|
|
|
} catch (SQLException e) {
|
|
|
|
} catch (SQLException e) {
|
|
|
|
return queryResultBuilder //
|
|
|
|
return queryResultBuilder //
|
|
|
|
.exception(e.getClass().getName()) //
|
|
|
|
.exception(e.getClass().getName()) //
|
|
|
|
.exceptionMessage(e.getMessage()) //
|
|
|
|
.exceptionMessage(e.getMessage()) //
|
|
|
|
.build();
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
handleUserErrors(conn, sql, stmt);
|
|
|
|
handleUserErrors(conn, sql, stmt);
|
|
|
|
} catch (SqlExecutionException e) {
|
|
|
|
} catch (SqlExecutionException e) {
|
|
|
|
log.error(e.getMessage(), e);
|
|
|
|
log.error(e.getMessage(), e);
|
|
|
|
|
|
|
|
|
|
|
|
return queryResultBuilder.userErrors(e.getUserErrors()).build();
|
|
|
|
return queryResultBuilder.userErrors(e.getUserErrors()).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return queryResultBuilder.affectedRows(affectedRows).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>();
|
|
|
@ -301,4 +311,37 @@ public class SqlExecutor {
|
|
|
|
|
|
|
|
|
|
|
|
return userErrors;
|
|
|
|
return userErrors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static List<String> parseDbOutput(Connection conn) throws SQLException {
|
|
|
|
|
|
|
|
enableDbOutput(conn);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<String> result = new Vector<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Cleanup
|
|
|
|
|
|
|
|
CallableStatement call = conn.prepareCall("{call DBMS_OUTPUT.GET_LINE(?, ?)}");
|
|
|
|
|
|
|
|
call.registerOutParameter(1, Types.VARCHAR);
|
|
|
|
|
|
|
|
call.registerOutParameter(2, Types.NUMERIC);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
long status = 0L;
|
|
|
|
|
|
|
|
while (status == 0L) {
|
|
|
|
|
|
|
|
call.execute();
|
|
|
|
|
|
|
|
status = call.getLong(2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String line = call.getString(1);
|
|
|
|
|
|
|
|
log.trace("DBMS_OUTPUT: " + line);
|
|
|
|
|
|
|
|
log.trace("DBMS_OUTPUT Status: " + status);
|
|
|
|
|
|
|
|
if (line != null && status == 0L) {
|
|
|
|
|
|
|
|
result.add(line);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void enableDbOutput(Connection conn) throws SQLException {
|
|
|
|
|
|
|
|
conn.createStatement() //
|
|
|
|
|
|
|
|
.execute("BEGIN DBMS_OUTPUT.ENABLE; END;");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|