@ -15,6 +15,8 @@ import org.apache.log4j.LogManager;
import com.fasterxml.jackson.core.JsonProcessingException ;
import com.fasterxml.jackson.databind.ObjectMapper ;
import at.compax.tools.sql.ConnectionParameters ;
import at.compax.tools.sql.FrogConnection ;
import at.compax.tools.sql.SqlExecutionException ;
import at.compax.tools.sql.SqlExecutor ;
import at.compax.tools.sql.model.QueryResult ;
@ -38,20 +40,38 @@ public class Main {
private static ExecutionMode executionMode = ExecutionMode . PLUGIN_BACKEND ;
private static Connection getConnection ( String dbIp , long port , String sid , String username , String password ) throws SQLException {
private static FrogConnection getConnection ( ConnectionParameters connectionParameters ) throws SQLException {
log . debug ( "Establishing conneciton..." ) ;
Connection conn = null ;
String jdbcConnection = String . format ( "jdbc:oracle:thin:@%s:%d:%s" , dbIp, port , sid ) ;
String jdbcConnection = String . format ( "jdbc:oracle:thin:@%s:%d:%s" , connectionParameters. getDbIp ( ) , connectionParameters . getPort ( ) , connectionParameters . getSid ( ) ) ;
conn = DriverManager . getConnection ( jdbcConnection , username , password ) ;
conn . setAutoCommit ( false ) ;
Connection sqlConn = DriverManager . getConnection ( jdbcConnection , //
connectionParameters . getUsername ( ) , //
connectionParameters . getPassword ( ) ) ;
sqlConn . setAutoCommit ( false ) ;
FrogConnection conn = FrogConnection . builder ( ) //
. conn ( sqlConn ) //
. build ( ) ;
log . debug ( "Established conneciton!" ) ;
if ( connectionParameters . isEnableDbOutput ( ) ) {
log . info ( "Enabling db output..." ) ;
try {
SqlExecutor . enableDbOutput ( conn ) ;
} catch ( SQLException e ) {
log . error ( "Could not enable db output: " + e . getMessage ( ) , e ) ;
conn . setDbOutputEnabled ( false ) ;
}
} else {
conn . setDbOutputEnabled ( false ) ;
}
return conn ;
}
private static void closeConnection ( Connection conn ) {
private static void closeConnection ( Frog Connection conn ) {
if ( conn ! = null ) {
try {
conn . close ( ) ;
@ -61,28 +81,28 @@ public class Main {
}
}
private static QueryResult executeExampleQueryWithException ( Connection conn ) throws SQLException , JsonProcessingException {
private static QueryResult executeExampleQueryWithException ( Frog Connection conn ) throws SQLException , JsonProcessingException {
String sql = "SELECT id, version, workspac FROM s_settings" ;
return SqlExecutor . executeQuery ( conn , sql , 10L ) ;
}
private static QueryResult executeExampleQueryWithoutParameter ( Connection conn ) throws SQLException , JsonProcessingException {
private static QueryResult executeExampleQueryWithoutParameter ( Frog Connection conn ) throws SQLException , JsonProcessingException {
String sql = "SELECT id, version, workspace FROM s_settings" ;
return SqlExecutor . executeQuery ( conn , sql , 10L ) ;
}
private static QueryResult executeExampleQueryWithParameter ( Connection conn ) throws SQLException , JsonProcessingException {
private static QueryResult executeExampleQueryWithParameter ( Frog Connection conn ) throws SQLException , JsonProcessingException {
String sql = "select client from k_clients where id = ?" ;
return SqlExecutor . executeQuery ( conn , sql , 1L ) ;
}
private static Result compileExampleSpec ( Connection conn ) throws SQLException , JsonProcessingException {
private static Result compileExampleSpec ( Frog 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 ) ;
}
private static Result compileExampleSpecWithErrors ( Connection conn ) throws SQLException , JsonProcessingException {
private static Result compileExampleSpecWithErrors ( Frog 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;" ;
return SqlExecutor . execute ( conn , specSql ) ;
@ -111,7 +131,13 @@ public class Main {
// String stdInSql = readFromStdIn();
@Cleanup
Connection conn = getConnection ( "172.19.13.66" , 1521L , "aax2qc" , "aax2qc" , "aax2qc" ) ;
FrogConnection conn = getConnection ( ConnectionParameters . builder ( ) //
. dbIp ( "172.19.13.66" ) //
. port ( 1521L ) //
. sid ( "aax2qc" ) //
. username ( "aax2qc" ) //
. password ( "aax2qc" ) //
. build ( ) ) ;
System . out . println ( formatJsonObject ( SqlExecutor . executeJsonQuery ( conn , 10L , "{ \"sql\": \"select customer_id from d_customers\"}" ) ) ) ;
System . out . println ( formatJsonObject ( compileExampleSpec ( conn ) ) ) ;
@ -119,6 +145,10 @@ public class Main {
System . out . println ( formatJsonObject ( executeExampleQueryWithException ( conn ) ) ) ;
System . out . println ( formatJsonObject ( executeExampleQueryWithoutParameter ( conn ) ) ) ;
System . out . println ( formatJsonObject ( executeExampleQueryWithParameter ( conn ) ) ) ;
System . out . println ( formatJsonObject ( SqlExecutor . executeJson ( conn , "{ \"sql\": \"begin dbms_output.put_line('should not be seen'); end;\"}" ) ) ) ;
System . out . println ( formatJsonObject ( SqlExecutor . executeJson ( conn , "{ \"enableDbOutput\": true, \"sql\": \"begin dbms_output.put_line('should be seen'); end;\"}" ) ) ) ;
System . out . println ( formatJsonObject ( SqlExecutor . executeJson ( conn , "{ \"sql\": \"begin dbms_output.put_line('should not be seen'); end;\"}" ) ) ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
@ -133,16 +163,17 @@ public class Main {
}
}
private static void serveForever ( String host , long port , String sid , String username , String password ) {
Connection conn = null ;
private static void serveForever ( ConnectionParameters connectionParameters ) {
Frog Connection conn = null ;
try {
try {
conn = getConnection ( host, port , sid , username , password ) ;
conn = getConnection ( connectionParameters ) ;
} catch ( SQLException e ) {
log . error ( e . getMessage ( ) , e ) ;
System . err . println ( "Could not connect to Database: " + e . getMessage ( ) ) ;
System . exit ( 1 ) ;
}
log . info ( "Waiting for input..." ) ;
BufferedReader br = new BufferedReader ( new InputStreamReader ( System . in , Charset . forName ( "UTF-8" ) ) ) ;
@ -201,8 +232,14 @@ public class Main {
String sid = args [ 2 ] ;
String username = args [ 3 ] ;
String password = args [ 4 ] ;
serveForever ( ConnectionParameters . builder ( ) //
. dbIp ( host ) //
. port ( port ) //
. sid ( sid ) //
. username ( username ) //
. password ( password ) //
. build ( ) ) ;
serveForever ( host , port , sid , username , password ) ;
}
}