|
|
|
@ -27,6 +27,8 @@ import lombok.extern.log4j.Log4j;
|
|
|
|
|
|
|
|
|
|
@Log4j
|
|
|
|
|
public class Main {
|
|
|
|
|
private static final long MAX_ROWNUMS = 10L;
|
|
|
|
|
|
|
|
|
|
@Getter
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
public static enum ExecutionMode {
|
|
|
|
@ -107,13 +109,12 @@ public class Main {
|
|
|
|
|
@Cleanup
|
|
|
|
|
Connection conn = getConnection("172.19.13.66", 1521L, "aax2qc", "aax2qc", "aax2qc");
|
|
|
|
|
|
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
|
|
|
|
|
System.out.println(mapper.writeValueAsString(compileExampleSpec(conn)));
|
|
|
|
|
System.out.println(mapper.writeValueAsString(compileExampleSpecWithErrors(conn)));
|
|
|
|
|
System.out.println(mapper.writeValueAsString(executeExampleQueryWithException(conn)));
|
|
|
|
|
System.out.println(mapper.writeValueAsString(executeExampleQueryWithoutParameter(conn)));
|
|
|
|
|
System.out.println(mapper.writeValueAsString(executeExampleQueryWithParameter(conn)));
|
|
|
|
|
System.out.println(formatJsonObject(SqlExecutor.executeJsonQuery(conn, "{ \"sql\": \"select customer_id from d_customers\"}")));
|
|
|
|
|
System.out.println(formatJsonObject(compileExampleSpec(conn)));
|
|
|
|
|
System.out.println(formatJsonObject(compileExampleSpecWithErrors(conn)));
|
|
|
|
|
System.out.println(formatJsonObject(executeExampleQueryWithException(conn)));
|
|
|
|
|
System.out.println(formatJsonObject(executeExampleQueryWithoutParameter(conn)));
|
|
|
|
|
System.out.println(formatJsonObject(executeExampleQueryWithParameter(conn)));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
@ -121,21 +122,22 @@ public class Main {
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
|
BasicConfigurator.configure();
|
|
|
|
|
LogManager.getRootLogger().setLevel(Level.OFF);
|
|
|
|
|
LogManager.getLogger("at.compax.tools.sql.main").setLevel(executionMode.getLoggingLevel());
|
|
|
|
|
LogManager.getLogger("at.compax.tools.sql").setLevel(executionMode.getLoggingLevel());
|
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
|
|
|
|
|
if (args.length == 0) {
|
|
|
|
|
runTests();
|
|
|
|
|
} else {
|
|
|
|
|
if (args.length < 4)
|
|
|
|
|
throw new IllegalArgumentException("Arguments: Host Port sid username password");
|
|
|
|
|
throw new IllegalArgumentException("Arguments: Host Port sid username password [max rownums]");
|
|
|
|
|
|
|
|
|
|
String host = args[0];
|
|
|
|
|
long port = Long.parseLong(args[1]);
|
|
|
|
|
String sid = args[2];
|
|
|
|
|
String username = args[3];
|
|
|
|
|
String password = args[4];
|
|
|
|
|
long maxRownums = (args.length >= 6) ? Long.parseLong(args[5]) : MAX_ROWNUMS;
|
|
|
|
|
|
|
|
|
|
@Cleanup
|
|
|
|
|
Connection conn = getConnection(host, port, sid, username, password);
|
|
|
|
@ -150,23 +152,27 @@ public class Main {
|
|
|
|
|
commandBuffer.append(line);
|
|
|
|
|
} else {
|
|
|
|
|
String query = commandBuffer.toString().trim();
|
|
|
|
|
String result;
|
|
|
|
|
if (query.toLowerCase().startsWith("select")) {
|
|
|
|
|
if (executionMode == ExecutionMode.TERMINAL) {
|
|
|
|
|
System.out.println(mapper //
|
|
|
|
|
.writerWithDefaultPrettyPrinter() //
|
|
|
|
|
.writeValueAsString(SqlExecutor.executeQuery(conn, //
|
|
|
|
|
query.replaceAll(";$", "").trim())) //
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println(mapper.writeValueAsString(SqlExecutor.executeQuery(conn, query.replaceAll(";$", "").trim())));
|
|
|
|
|
}
|
|
|
|
|
String selectQuery = String.format("SELECT * FROM (%s) WHERE rownum <= %d", query.replaceAll(";$", "").trim(), maxRownums);
|
|
|
|
|
|
|
|
|
|
result = formatJsonObject(SqlExecutor.executeQuery(conn, selectQuery));
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println(mapper.writeValueAsString(SqlExecutor.execute(conn, query)));
|
|
|
|
|
result = formatJsonObject(SqlExecutor.execute(conn, query));
|
|
|
|
|
}
|
|
|
|
|
System.out.println(result);
|
|
|
|
|
commandBuffer.setLength(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String formatJsonObject(Object object) throws JsonProcessingException {
|
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
if (executionMode == ExecutionMode.TERMINAL) {
|
|
|
|
|
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
|
|
|
|
|
} else {
|
|
|
|
|
return mapper.writeValueAsString(object);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|