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