diff --git a/README.md b/README.md index 8148570..848a07e 100644 --- a/README.md +++ b/README.md @@ -1,32 +1,39 @@ -# TODO: this content needs to be updated and finished +# Remal SQL-Runner command line tool -[release note](release.md) +_keywords: java, sql, query, insert, select, update, oracle, database, bash script, shell script, command line, tool, execute, automate, docker_ -The tool can be used from script files. -A possible Docker use case to check whether the database server is ready to receiver incoming connections or not: +_[Release Note](release.md)_ + +## 1) Overview +The SQL-Runner is a flexible command line Java tool can be used to run any SQL commands using bash script and command line. It can be used very effectively from Windows/Linux script files. + +The latest version of the tool only supports Oracle Database server, but it can support different SQL dialects as well. + +## 2) Use cases +#### 2.1) Database status check +A possible use case in Docker environment is to check whether the database server is ready to receive incoming connections or not: ~~~ #!/bin/bash -until java -jar sql-runner-1.0-with-dependencies.jar -j jdbc:oracle:thin:@//oracle-db:1521/ORCLPDB1.localdomain -U "SYS as SYSDBA" -P Oradoc_db1 "select 1 from dual" +until java -jar sql-runner-0.2.0-with-dependencies.jar -j jdbc:oracle:thin:@//oracle-db:1521/ORCLPDB1.localdomain -U "SYS as SYSDBA" -P Oradoc_db1 "select 1 from dual" do echo "The database server in not up and running. Waiting..." - # sleep 0.5 + sleep 0.5 done - -echo ok +echo "Database server is up and running" ~~~ -**Usage** +## 3) Usage ~~~~ -Usage: SqlRunner [-?sv] [-t=] -U= (-P= | -I) (-j= | (-h= - -p= -d=)) +Usage: SqlRunner [-?sv] [-c=] -U= (-P= | -I) (-j= | (-h= + -p= -d=)) SQL command line tool. It executes the given SQL and show the result on the standard output. General options: SQL to be executed. Example: 'select 1 from dual' -?, --help Display this help and exit. -v, --verbose It provides additional details as to what the tool is doing. - -t, --dialect= SQL dialect used during the execution of the SQL statement. Supported + -c, --dialect= SQL dialect used during the execution of the SQL statement. Supported SQL dialects: ORACLE. Default: ORACLE -s, --showHeader Shows the name of the fields from the SQL result set. @@ -39,18 +46,22 @@ Specify a password for the connecting user: Custom configuration: -h, --host= Name of the database server. -p, --port= Number of the port where the server listens for requests. - -d, --database= Name of the particular database on the server. Also known as the SID in + -d, --database= Name of the particular database on the server. Also known as the SID in Oracle terminology. Provide a JDBC URL: -j, --jdbcUrl= JDBC URL, example: jdbc:oracle::@//:/. Exit codes: - 1 Successful program execution. - 2 An unexpected error appeared while executing the SQL statement. - 3 Usage error. User input for the command was incorrect. + 0 Successful program execution. + 1 An unexpected error appeared while executing the SQL statement. + 2 Usage error. User input for the command was incorrect. + +Please report issues at arnold.somogyi@gmail.com. +Documentation, source code: https://github.com/zappee/sql-runner.git ~~~~ -**Examples** -* `java -jar target/sql-runner-1.0-with-dependencies.jar -j jdbc:oracle:thin:@//localhost:1521/ORCLPDB1.localdomain -U "SYS as SYSDBA" -P "Oradoc_db1" "select * from SLM.APPLICATION"` -* `java -jar target/sql-runner-1.0-with-dependencies.jar -h localhost -p 1521 -d ORCLPDB1.localdomain -U "SYS as SYSDBA" -P "Oradoc_db1" "select * from SLM.APPLICATION"` +## 4) Licence +BSD (2-clause) licensed. + + diff --git a/pom.xml b/pom.xml index 1e97b51..21b47f0 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.remal.sqlrunner sql-runner - 0.1.0 + 0.2.0 diff --git a/release.md b/release.md index 81dabc5..90bfc2c 100644 --- a/release.md +++ b/release.md @@ -7,7 +7,6 @@ All notable changes to this project will be documented in this file. #### Added - Initial version based on the requirements -### [x.y.z] - yyyy-mm-dd +### [0.2.0] - yyyy-mm-dd #### Added -#### Changed -#### removed \ No newline at end of file +- set timeout to 60 sec whiile trying to connect to the database \ No newline at end of file diff --git a/src/main/java/com/remal/sqlrunner/SqlRunner.java b/src/main/java/com/remal/sqlrunner/SqlRunner.java index 7aad763..2a4ffac 100644 --- a/src/main/java/com/remal/sqlrunner/SqlRunner.java +++ b/src/main/java/com/remal/sqlrunner/SqlRunner.java @@ -4,6 +4,7 @@ import java.util.concurrent.Callable; import com.remal.sqlrunner.domain.Dialect; +import com.remal.sqlrunner.domain.ExitCode; import picocli.CommandLine; import picocli.CommandLine.ArgGroup; import picocli.CommandLine.Command; @@ -26,11 +27,11 @@ description = "SQL command line tool. It executes the given SQL and show the result on the standard output.%n", parameterListHeading = "General options:%n", exitCodeListHeading = "%nExit codes:%n", - exitCodeOnUsageHelp = 3, + exitCodeOnUsageHelp = ExitCode.CLI_ERROR_EXIT_CODE, exitCodeList = { - "1:Successful program execution.", - "2:An unexpected error appeared while executing the SQL statement.", - "3:Usage error. User input for the command was incorrect." }, + "0:Successful program execution.", + "1:An unexpected error appeared while executing the SQL statement.", + "2:Usage error. User input for the command was incorrect." }, footerHeading = "%nPlease report issues at arnold.somogyi@gmail.com.", footer = "%nDocumentation, source code: https://github.com/zappee/sql-runner.git") public class SqlRunner implements Callable { @@ -47,7 +48,7 @@ public class SqlRunner implements Callable { description = "It provides additional details as to what the tool is doing.") private boolean verbose; - @Option(names = {"-t", "--dialect"}, + @Option(names = {"-c", "--dialect"}, defaultValue = Dialect.ORACLE_VALUE, showDefaultValue = CommandLine.Help.Visibility.ALWAYS, description = "SQL dialect used during the execution of the SQL statement. " @@ -139,7 +140,7 @@ static class CustomConfigurationGroup { required = true, description = "Name of the particular database on the server. Also known as the SID in Oracle " + "terminology.") - private String sid; + private String database; } /** @@ -180,7 +181,7 @@ public Integer call() { dialect, mainArgGroup.customConfigurationGroup.host, mainArgGroup.customConfigurationGroup.port, - mainArgGroup.customConfigurationGroup.sid, + mainArgGroup.customConfigurationGroup.database, sql) .getExitCode(); } diff --git a/src/main/java/com/remal/sqlrunner/SqlStatementExecutor.java b/src/main/java/com/remal/sqlrunner/SqlStatementExecutor.java index 99e8a73..bb04fec 100644 --- a/src/main/java/com/remal/sqlrunner/SqlStatementExecutor.java +++ b/src/main/java/com/remal/sqlrunner/SqlStatementExecutor.java @@ -6,9 +6,12 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.util.Properties; +import java.util.concurrent.Executors; import com.remal.sqlrunner.domain.Dialect; import com.remal.sqlrunner.domain.ExitCode; +import oracle.jdbc.driver.OracleConnection; /** * This class connects to the database and executes the provided SQL statement. @@ -21,6 +24,8 @@ */ public class SqlStatementExecutor { + private static final int ONE_SECOND_IN_MILLISEC = 1000; + private PrintStream out = System.out; private boolean verbose = false; private boolean showHeader = false; @@ -117,6 +122,28 @@ private Connection getConnection(String jdbcUrl) throws SQLException { if (verbose) { out.println("getting connection to " + jdbcUrl + "..."); } - return DriverManager.getConnection(jdbcUrl, user, password); + + DriverManager.setLoginTimeout(ONE_SECOND_IN_MILLISEC / 1000); + + Properties properties = getConnectionArguments(user, password); + Connection connection = DriverManager.getConnection(jdbcUrl, properties); + connection.setNetworkTimeout(Executors.newSingleThreadExecutor(), ONE_SECOND_IN_MILLISEC); + + return connection; + } + + /** + * Build a list of arbitrary string tag/value pairs as connection arguments. + * Normally at least a "user" and "password" property should be included. + * + * @param user username + * @param password password + * @return connection argument list + */ + private Properties getConnectionArguments(String user, String password) { + Properties properties = new Properties(); + properties.put(OracleConnection.CONNECTION_PROPERTY_USER_NAME, user); + properties.put(OracleConnection.CONNECTION_PROPERTY_PASSWORD, password); + return properties; } } diff --git a/src/main/java/com/remal/sqlrunner/domain/Dialect.java b/src/main/java/com/remal/sqlrunner/domain/Dialect.java index 5aca0fe..4014cde 100644 --- a/src/main/java/com/remal/sqlrunner/domain/Dialect.java +++ b/src/main/java/com/remal/sqlrunner/domain/Dialect.java @@ -48,4 +48,13 @@ public String getJdbcUrl(String host, int port, String database) { .replace("port", String.valueOf(port)) .replace("database", database); } + + /** + * Getter. + * + * @return the string representation of the enum value + */ + public String getValue() { + return value; + } } diff --git a/src/main/java/com/remal/sqlrunner/domain/ExitCode.java b/src/main/java/com/remal/sqlrunner/domain/ExitCode.java index 97da23b..43124e1 100644 --- a/src/main/java/com/remal/sqlrunner/domain/ExitCode.java +++ b/src/main/java/com/remal/sqlrunner/domain/ExitCode.java @@ -10,15 +10,30 @@ * @author arnold.somogyi@gmail.com */ public enum ExitCode { + /** * Used in case of the successful program execution. */ - OK(1), + OK(ExitCode.OK_EXIT_CODE), + + /** + * Command Line Interface error. + * This exit code is used in case of missing command line parameter(s). + */ + CLI_ERROR(ExitCode.CLI_ERROR_EXIT_CODE), /** * Used if an unexpected error appeared while executing the SQL statement. */ - SQL_ERROR(2); + SQL_ERROR(ExitCode.SQL_ERROR_EXIT_CODE); + + /** + * Exit codes constants. + */ + public static final int OK_EXIT_CODE = 0; + public static final int CLI_ERROR_EXIT_CODE = 1; + public static final int SQL_ERROR_EXIT_CODE = 2; + private int exitCodeRepresentation;