|
|
@ -17,144 +17,156 @@ 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;
|
|
|
|
import lombok.Cleanup;
|
|
|
|
import lombok.Cleanup;
|
|
|
|
|
|
|
|
import lombok.Getter;
|
|
|
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import lombok.extern.log4j.Log4j;
|
|
|
|
import lombok.extern.log4j.Log4j;
|
|
|
|
|
|
|
|
|
|
|
|
@Log4j
|
|
|
|
@Log4j
|
|
|
|
public class Main {
|
|
|
|
public class Main {
|
|
|
|
|
|
|
|
@Getter
|
|
|
|
private static Connection getConnection(String dbIp, long port, String sid, String username, String password) {
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
log.debug("Establishing conneciton...");
|
|
|
|
public static enum ExecutionMode {
|
|
|
|
Connection conn = null;
|
|
|
|
PLUGIN_BACKEND(Level.OFF),
|
|
|
|
|
|
|
|
TERMINAL(Level.ALL);
|
|
|
|
String jdbcConnection = String.format("jdbc:oracle:thin:@%s:%d:%s", dbIp, port, sid);
|
|
|
|
|
|
|
|
|
|
|
|
private final Level loggingLevel;
|
|
|
|
try {
|
|
|
|
}
|
|
|
|
conn = DriverManager.getConnection(jdbcConnection, username, password);
|
|
|
|
|
|
|
|
conn.setAutoCommit(false);
|
|
|
|
private static ExecutionMode executionMode = ExecutionMode.PLUGIN_BACKEND;
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
|
|
|
throw new RuntimeException("Couldn't connect to database", e);
|
|
|
|
private static Connection getConnection(String dbIp, long port, String sid, String username, String password) {
|
|
|
|
}
|
|
|
|
log.debug("Establishing conneciton...");
|
|
|
|
|
|
|
|
Connection conn = null;
|
|
|
|
log.debug("Established conneciton!");
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
String jdbcConnection = String.format("jdbc:oracle:thin:@%s:%d:%s", dbIp, port, sid);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
private static QueryResult executeExampleQueryWithException(Connection conn)
|
|
|
|
conn = DriverManager.getConnection(jdbcConnection, username, password);
|
|
|
|
throws SQLException, JsonProcessingException {
|
|
|
|
conn.setAutoCommit(false);
|
|
|
|
String sql = "SELECT id, version, workspac FROM s_settings";
|
|
|
|
} catch (SQLException e) {
|
|
|
|
return SqlExecutor.executeQuery(conn, sql);
|
|
|
|
throw new RuntimeException("Couldn't connect to database", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static QueryResult executeExampleQueryWithoutParameter(Connection conn)
|
|
|
|
log.debug("Established conneciton!");
|
|
|
|
throws SQLException, JsonProcessingException {
|
|
|
|
return conn;
|
|
|
|
String sql = "SELECT id, version, workspace FROM s_settings";
|
|
|
|
}
|
|
|
|
return SqlExecutor.executeQuery(conn, sql);
|
|
|
|
|
|
|
|
}
|
|
|
|
private static QueryResult executeExampleQueryWithException(Connection conn) throws SQLException, JsonProcessingException {
|
|
|
|
|
|
|
|
String sql = "SELECT id, version, workspac FROM s_settings";
|
|
|
|
private static QueryResult executeExampleQueryWithParameter(Connection conn)
|
|
|
|
return SqlExecutor.executeQuery(conn, sql);
|
|
|
|
throws SQLException, JsonProcessingException {
|
|
|
|
}
|
|
|
|
String sql = "select client from k_clients where id = ?";
|
|
|
|
|
|
|
|
return SqlExecutor.executeQuery(conn, sql, 1L);
|
|
|
|
private static QueryResult executeExampleQueryWithoutParameter(Connection conn) throws SQLException, JsonProcessingException {
|
|
|
|
}
|
|
|
|
String sql = "SELECT id, version, workspace FROM s_settings";
|
|
|
|
|
|
|
|
return SqlExecutor.executeQuery(conn, sql);
|
|
|
|
private static Result compileExampleSpec(Connection conn) throws SQLException, JsonProcessingException {
|
|
|
|
}
|
|
|
|
String specSql = "CREATE OR REPLACE PACKAGE aax2_wedel\n" + "AS\n"
|
|
|
|
|
|
|
|
+ " PROCEDURE create_cli_migration_workflow(p_customer d_customers.id%type, p_migration_date date);\n"
|
|
|
|
private static QueryResult executeExampleQueryWithParameter(Connection conn) throws SQLException, JsonProcessingException {
|
|
|
|
+ "END aax2_wedel;";
|
|
|
|
String sql = "select client from k_clients where id = ?";
|
|
|
|
|
|
|
|
return SqlExecutor.executeQuery(conn, sql, 1L);
|
|
|
|
return SqlExecutor.execute(conn, specSql);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static Result compileExampleSpec(Connection conn) throws SQLException, JsonProcessingException {
|
|
|
|
private static Result compileExampleSpecWithErrors(Connection conn) throws SQLException, JsonProcessingException {
|
|
|
|
String specSql = "CREATE OR REPLACE PACKAGE aax2_wedel\n" + "AS\n" + " PROCEDURE create_cli_migration_workflow(p_customer d_customers.id%type, p_migration_date date);\n" + "END aax2_wedel;";
|
|
|
|
String specSql = "CREATE OR REPLACE PACKAGE aax2_wedel\n" + "AS\n"
|
|
|
|
|
|
|
|
+ " PROCEDURE create_cli_migration_workflow(p_customer d_customers.id%typ, p_migration_date date);\n"
|
|
|
|
return SqlExecutor.execute(conn, specSql);
|
|
|
|
+ "END aax2_wedel;";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return SqlExecutor.execute(conn, specSql);
|
|
|
|
private static Result compileExampleSpecWithErrors(Connection conn) throws SQLException, JsonProcessingException {
|
|
|
|
}
|
|
|
|
String specSql = "CREATE OR REPLACE PACKAGE aax2_wedel\n" + "AS\n" + " PROCEDURE create_cli_migration_workflow(p_customer d_customers.id%typ, p_migration_date date);\n" + "END aax2_wedel;";
|
|
|
|
|
|
|
|
|
|
|
|
private static void handleSqlExecutionException(SqlExecutionException e) throws JsonProcessingException {
|
|
|
|
return SqlExecutor.execute(conn, specSql);
|
|
|
|
for (UserError userError : e.getUserErrors()) {
|
|
|
|
}
|
|
|
|
log.debug(userError.toString());
|
|
|
|
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
private static void handleSqlExecutionException(SqlExecutionException e) throws JsonProcessingException {
|
|
|
|
System.out.println(mapper.writeValueAsString(userError));
|
|
|
|
for (UserError userError : e.getUserErrors()) {
|
|
|
|
}
|
|
|
|
log.debug(userError.toString());
|
|
|
|
}
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
|
|
|
System.out.println(mapper.writeValueAsString(userError));
|
|
|
|
private static String readFromStdIn() throws IOException {
|
|
|
|
}
|
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
|
|
|
}
|
|
|
|
String line;
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
private static String readFromStdIn() throws IOException {
|
|
|
|
while ((line = br.readLine()) != null) {
|
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
|
|
|
sb.append(line);
|
|
|
|
String line;
|
|
|
|
}
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
return sb.toString();
|
|
|
|
while ((line = br.readLine()) != null) {
|
|
|
|
}
|
|
|
|
sb.append(line);
|
|
|
|
|
|
|
|
}
|
|
|
|
public static void runTests() {
|
|
|
|
return sb.toString();
|
|
|
|
try {
|
|
|
|
}
|
|
|
|
// String stdInSql = readFromStdIn();
|
|
|
|
|
|
|
|
|
|
|
|
public static void runTests() {
|
|
|
|
@Cleanup
|
|
|
|
try {
|
|
|
|
Connection conn = getConnection("172.19.13.66", 1521L, "aax2qc", "aax2qc", "aax2qc");
|
|
|
|
// String stdInSql = readFromStdIn();
|
|
|
|
|
|
|
|
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
@Cleanup
|
|
|
|
|
|
|
|
Connection conn = getConnection("172.19.13.66", 1521L, "aax2qc", "aax2qc", "aax2qc");
|
|
|
|
System.out.println(mapper.writeValueAsString(compileExampleSpec(conn)));
|
|
|
|
|
|
|
|
System.out.println(mapper.writeValueAsString(compileExampleSpecWithErrors(conn)));
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
System.out.println(mapper.writeValueAsString(executeExampleQueryWithException(conn)));
|
|
|
|
|
|
|
|
System.out.println(mapper.writeValueAsString(executeExampleQueryWithoutParameter(conn)));
|
|
|
|
System.out.println(mapper.writeValueAsString(compileExampleSpec(conn)));
|
|
|
|
System.out.println(mapper.writeValueAsString(executeExampleQueryWithParameter(conn)));
|
|
|
|
System.out.println(mapper.writeValueAsString(compileExampleSpecWithErrors(conn)));
|
|
|
|
} catch (Exception e) {
|
|
|
|
System.out.println(mapper.writeValueAsString(executeExampleQueryWithException(conn)));
|
|
|
|
e.printStackTrace();
|
|
|
|
System.out.println(mapper.writeValueAsString(executeExampleQueryWithoutParameter(conn)));
|
|
|
|
}
|
|
|
|
System.out.println(mapper.writeValueAsString(executeExampleQueryWithParameter(conn)));
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
}
|
|
|
|
BasicConfigurator.configure();
|
|
|
|
}
|
|
|
|
LogManager.getLogger("at.compax.tools.sql.main").setLevel(Level.OFF);
|
|
|
|
|
|
|
|
LogManager.getLogger("at.compax.tools.sql").setLevel(Level.OFF);
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
BasicConfigurator.configure();
|
|
|
|
|
|
|
|
LogManager.getLogger("at.compax.tools.sql.main").setLevel(executionMode.getLoggingLevel());
|
|
|
|
if (args.length == 0) {
|
|
|
|
LogManager.getLogger("at.compax.tools.sql").setLevel(executionMode.getLoggingLevel());
|
|
|
|
runTests();
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (args.length < 4)
|
|
|
|
if (args.length == 0) {
|
|
|
|
throw new IllegalArgumentException("Arguments: Host Port sid username password");
|
|
|
|
runTests();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
String host = args[0];
|
|
|
|
if (args.length < 4)
|
|
|
|
long port = Long.parseLong(args[1]);
|
|
|
|
throw new IllegalArgumentException("Arguments: Host Port sid username password");
|
|
|
|
String sid = args[2];
|
|
|
|
|
|
|
|
String username = args[3];
|
|
|
|
String host = args[0];
|
|
|
|
String password = args[4];
|
|
|
|
long port = Long.parseLong(args[1]);
|
|
|
|
|
|
|
|
String sid = args[2];
|
|
|
|
@Cleanup
|
|
|
|
String username = args[3];
|
|
|
|
Connection conn = getConnection(host, port, sid, username, password);
|
|
|
|
String password = args[4];
|
|
|
|
log.info("Waiting for input...");
|
|
|
|
|
|
|
|
|
|
|
|
@Cleanup
|
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(System.in, Charset.forName("UTF-8")));
|
|
|
|
Connection conn = getConnection(host, port, sid, username, password);
|
|
|
|
String line;
|
|
|
|
log.info("Waiting for input...");
|
|
|
|
StringBuffer commandBuffer = new StringBuffer();
|
|
|
|
|
|
|
|
|
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(System.in, Charset.forName("UTF-8")));
|
|
|
|
while ((line = br.readLine()) != null) {
|
|
|
|
String line;
|
|
|
|
if (!"--- END ---".equals(line)) {
|
|
|
|
StringBuffer commandBuffer = new StringBuffer();
|
|
|
|
commandBuffer.append(line);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
while ((line = br.readLine()) != null) {
|
|
|
|
String query = commandBuffer.toString().trim();
|
|
|
|
if (!"--- END ---".equals(line)) {
|
|
|
|
if (query.toLowerCase().startsWith("select")) {
|
|
|
|
commandBuffer.append(line);
|
|
|
|
System.out.println(mapper.writeValueAsString(SqlExecutor.executeQuery(conn, query.replaceAll(";$", "").trim())));
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
String query = commandBuffer.toString().trim();
|
|
|
|
System.out.println(mapper.writeValueAsString(SqlExecutor.execute(conn, query)));
|
|
|
|
if (query.toLowerCase().startsWith("select")) {
|
|
|
|
}
|
|
|
|
if (executionMode == ExecutionMode.TERMINAL) {
|
|
|
|
commandBuffer.setLength(0);
|
|
|
|
System.out.println(mapper //
|
|
|
|
}
|
|
|
|
.writerWithDefaultPrettyPrinter() //
|
|
|
|
}
|
|
|
|
.writeValueAsString(SqlExecutor.executeQuery(conn, //
|
|
|
|
|
|
|
|
query.replaceAll(";$", "").trim())) //
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
System.out.println(mapper.writeValueAsString(SqlExecutor.executeQuery(conn, query.replaceAll(";$", "").trim())));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
System.out.println(mapper.writeValueAsString(SqlExecutor.execute(conn, query)));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
commandBuffer.setLength(0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|