Add execution modes and format
This commit is contained in:
		@@ -17,15 +17,26 @@ 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
 | 
				
			||||||
 | 
					  @RequiredArgsConstructor
 | 
				
			||||||
 | 
					  public static enum ExecutionMode {
 | 
				
			||||||
 | 
					    PLUGIN_BACKEND(Level.OFF),
 | 
				
			||||||
 | 
					    TERMINAL(Level.ALL);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private final Level loggingLevel;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private static ExecutionMode executionMode = ExecutionMode.PLUGIN_BACKEND;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private static Connection getConnection(String dbIp, long port, String sid, String username, String password) {
 | 
					  private static Connection getConnection(String dbIp, long port, String sid, String username, String password) {
 | 
				
			||||||
    log.debug("Establishing conneciton...");
 | 
					    log.debug("Establishing conneciton...");
 | 
				
			||||||
@@ -44,36 +55,29 @@ public class Main {
 | 
				
			|||||||
    return conn;
 | 
					    return conn;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private static QueryResult executeExampleQueryWithException(Connection conn)
 | 
					  private static QueryResult executeExampleQueryWithException(Connection conn) throws SQLException, JsonProcessingException {
 | 
				
			||||||
			throws SQLException, JsonProcessingException {
 | 
					 | 
				
			||||||
    String sql = "SELECT id, version, workspac FROM s_settings";
 | 
					    String sql = "SELECT id, version, workspac FROM s_settings";
 | 
				
			||||||
    return SqlExecutor.executeQuery(conn, sql);
 | 
					    return SqlExecutor.executeQuery(conn, sql);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private static QueryResult executeExampleQueryWithoutParameter(Connection conn)
 | 
					  private static QueryResult executeExampleQueryWithoutParameter(Connection conn) throws SQLException, JsonProcessingException {
 | 
				
			||||||
			throws SQLException, JsonProcessingException {
 | 
					 | 
				
			||||||
    String sql = "SELECT id, version, workspace FROM s_settings";
 | 
					    String sql = "SELECT id, version, workspace FROM s_settings";
 | 
				
			||||||
    return SqlExecutor.executeQuery(conn, sql);
 | 
					    return SqlExecutor.executeQuery(conn, sql);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private static QueryResult executeExampleQueryWithParameter(Connection conn)
 | 
					  private static QueryResult executeExampleQueryWithParameter(Connection conn) throws SQLException, JsonProcessingException {
 | 
				
			||||||
			throws SQLException, JsonProcessingException {
 | 
					 | 
				
			||||||
    String sql = "select client from k_clients where id = ?";
 | 
					    String sql = "select client from k_clients where id = ?";
 | 
				
			||||||
    return SqlExecutor.executeQuery(conn, sql, 1L);
 | 
					    return SqlExecutor.executeQuery(conn, sql, 1L);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private static Result compileExampleSpec(Connection conn) throws SQLException, JsonProcessingException {
 | 
					  private static Result compileExampleSpec(Connection conn) throws SQLException, JsonProcessingException {
 | 
				
			||||||
		String specSql = "CREATE OR REPLACE PACKAGE aax2_wedel\n" + "AS\n"
 | 
					    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;";
 | 
				
			||||||
				+ "   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 Result compileExampleSpecWithErrors(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"
 | 
					    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;";
 | 
				
			||||||
				+ "   PROCEDURE create_cli_migration_workflow(p_customer d_customers.id%typ,  p_migration_date date);\n"
 | 
					 | 
				
			||||||
				+ "END aax2_wedel;";
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return SqlExecutor.execute(conn, specSql);
 | 
					    return SqlExecutor.execute(conn, specSql);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@@ -117,8 +121,8 @@ public class Main {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  public static void main(String[] args) throws Exception {
 | 
					  public static void main(String[] args) throws Exception {
 | 
				
			||||||
    BasicConfigurator.configure();
 | 
					    BasicConfigurator.configure();
 | 
				
			||||||
		LogManager.getLogger("at.compax.tools.sql.main").setLevel(Level.OFF);
 | 
					    LogManager.getLogger("at.compax.tools.sql.main").setLevel(executionMode.getLoggingLevel());
 | 
				
			||||||
		LogManager.getLogger("at.compax.tools.sql").setLevel(Level.OFF);
 | 
					    LogManager.getLogger("at.compax.tools.sql").setLevel(executionMode.getLoggingLevel());
 | 
				
			||||||
    ObjectMapper mapper = new ObjectMapper();
 | 
					    ObjectMapper mapper = new ObjectMapper();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (args.length == 0) {
 | 
					    if (args.length == 0) {
 | 
				
			||||||
@@ -147,7 +151,15 @@ public class Main {
 | 
				
			|||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
          String query = commandBuffer.toString().trim();
 | 
					          String query = commandBuffer.toString().trim();
 | 
				
			||||||
          if (query.toLowerCase().startsWith("select")) {
 | 
					          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())));
 | 
					              System.out.println(mapper.writeValueAsString(SqlExecutor.executeQuery(conn, query.replaceAll(";$", "").trim())));
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
          } else {
 | 
					          } else {
 | 
				
			||||||
            System.out.println(mapper.writeValueAsString(SqlExecutor.execute(conn, query)));
 | 
					            System.out.println(mapper.writeValueAsString(SqlExecutor.execute(conn, query)));
 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user