Skip to content

Commit

Permalink
Merge pull request #6 from zappee/set-timeout
Browse files Browse the repository at this point in the history
Set timeout
  • Loading branch information
zappee authored Aug 4, 2020
2 parents 907f6ad + 1c15c14 commit e8947c1
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 33 deletions.
49 changes: 30 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -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=<dialect>] -U=<user> (-P=<password> | -I) (-j=<jdbcUrl> | (-h=<host>
-p=<port> -d=<sid>)) <sql>
Usage: SqlRunner [-?sv] [-c=<dialect>] -U=<user> (-P=<password> | -I) (-j=<jdbcUrl> | (-h=<host>
-p=<port> -d=<database>)) <sql>
SQL command line tool. It executes the given SQL and show the result on the standard output.
General options:
<sql> 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=<dialect> SQL dialect used during the execution of the SQL statement. Supported
-c, --dialect=<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.
Expand All @@ -39,18 +46,22 @@ Specify a password for the connecting user:
Custom configuration:
-h, --host=<host> Name of the database server.
-p, --port=<port> Number of the port where the server listens for requests.
-d, --database=<sid> Name of the particular database on the server. Also known as the SID in
-d, --database=<database> Name of the particular database on the server. Also known as the SID in
Oracle terminology.
Provide a JDBC URL:
-j, --jdbcUrl=<jdbcUrl> JDBC URL, example: jdbc:oracle:<drivertype>:@//<host>:<port>/<database>.
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 [email protected].
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.


2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>com.remal.sqlrunner</groupId>
<artifactId>sql-runner</artifactId>
<version>0.1.0</version>
<version>0.2.0</version>

<licenses>
<license>
Expand Down
5 changes: 2 additions & 3 deletions release.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
- set timeout to 60 sec whiile trying to connect to the database
15 changes: 8 additions & 7 deletions src/main/java/com/remal/sqlrunner/SqlRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 [email protected].",
footer = "%nDocumentation, source code: https://github.com/zappee/sql-runner.git")
public class SqlRunner implements Callable<Integer> {
Expand All @@ -47,7 +48,7 @@ public class SqlRunner implements Callable<Integer> {
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. "
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -180,7 +181,7 @@ public Integer call() {
dialect,
mainArgGroup.customConfigurationGroup.host,
mainArgGroup.customConfigurationGroup.port,
mainArgGroup.customConfigurationGroup.sid,
mainArgGroup.customConfigurationGroup.database,
sql)
.getExitCode();
}
Expand Down
29 changes: 28 additions & 1 deletion src/main/java/com/remal/sqlrunner/SqlStatementExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/remal/sqlrunner/domain/Dialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
19 changes: 17 additions & 2 deletions src/main/java/com/remal/sqlrunner/domain/ExitCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,30 @@
* @author [email protected]
*/
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;

Expand Down

0 comments on commit e8947c1

Please sign in to comment.