Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce JDBC Tests for MSSQL #1124

Merged
merged 1 commit into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions test/connector/tcp/mssql/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,16 @@ RUN rm -rf /etc/apt/sources.list.d/mssql-release.list && \
# Add python 3 and pyodbc
RUN apt-get install -y python3 python3-pip
RUN pip3 install pyodbc

# Add java8 and add to $PATH
# Fix cert issues
RUN apt-get update && \
apt-get install -y ant \
openjdk-8-jdk \
ca-certificates-java && \
apt-get clean && \
update-ca-certificates -f


# Setup JAVA_HOME -- useful for docker commandline
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
6 changes: 5 additions & 1 deletion test/connector/tcp/mssql/mssql_connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ func TestMSSQLConnector(t *testing.T) {
RunConnectivityTests(t, pythonODBCExec)
})

t.Run("java-JDBC", func(t *testing.T) {
RunConnectivityTests(t, javaJDBCExec)
})

t.Run("go-mssql", func(t *testing.T) {
RunConnectivityTests(t, gomssqlExec)
})
Expand All @@ -35,7 +39,7 @@ func RunConnectivityTests(t *testing.T, queryExec dbQueryExecutor) {
// Execute Query
out, err := queryExec(
defaultSecretlessDbConfig(),
fmt.Sprintf("select %s, '%s'", testInt, testString),
fmt.Sprintf("SELECT %s AS sum, '%s' AS str", testInt, testString),
)

// Test the returned values
Expand Down
42 changes: 42 additions & 0 deletions test/connector/tcp/mssql/mssql_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type dbConfig struct {

type dbQueryExecutor func(cfg dbConfig, query string) (string, error)

const jdbcJARPath = "/secretless/test/util/jdbc/jdbc.jar"

func defaultSecretlessDbConfig() dbConfig {
return dbConfig{
Host: testutil.SecretlessHost,
Expand Down Expand Up @@ -108,6 +110,46 @@ func pythonODBCExec(
return string(out), nil
}

// runs queries using Java JDBC
// Jar modified from this source: http://jdbcsql.sourceforge.net/
func javaJDBCExec(
cfg dbConfig,
query string,
) (string, error) {

args := []string{
"-jar", jdbcJARPath,
"-m", "mssql",
"-h", fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
"-U", cfg.Username,
"-P", cfg.Password,
}

// For JDBC, database is not optional. If empty, add the default MsSQL database
if db := cfg.Database; db == "" {
args = append(args, "-d", "tempdb")
} else {
args = append(args, "-d", db)
}

args = append(args, query)

out, err := exec.Command(
"java",
args...,
).Output()

if err != nil {
if exitErrr, ok := err.(*exec.ExitError); ok {
return "", errors.New(string(exitErrr.Stderr))
}

return "", err
}

return string(out), nil
}

// runs queries using go-mssqldb
func gomssqlExec(
cfg dbConfig,
Expand Down
105 changes: 105 additions & 0 deletions test/util/jdbc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Documentation for jdbcsql-1.0.zip
> Modified from original documentation found [here](http://jdbcsql.sourceforge.net/)

To quote the creator:

> jdbcsql is a small command-line tool written in JAVA and can be used on all platforms,
for which JRE 8 is available. To connect to a specific DBMS the tool uses its JDBC driver.
The current version for download supports the following DBMS: mysql, oracle and postgresql.
Other systems can easily be added by the user. The result of the executed 'select' query
is displayed in CSV format (by default complying to rfc4180, but other standards are
supported too). When there is an error the tool stops with exit code 1 and the error
message is output on stderr. jdbcsql is created with a main purpose to be used in
shell-scripts.

> Relatively easy to configurate, this tool is suitable for queries ‘select’, ‘update’
and ‘delete’. I think that is not suitable for a large number of requests, like 'insert'.

## Usage
```
$ java -jar jdbcsql.zip
jdbcsql execute queries in diferent databases such as mysql, oracle, postgresql and etc.
Query with resultset output over stdout in CSV format.

usage: jdbcsql [OPTION]... SQL
-?,--help show this help, then exit
-d,--dbname database name to connect
-f,--csv-format Output CSV format (EXCEL, MYSQL,
RFC-4180 and TDF). Default is RFC-4180
-h,--host database server host
-H,--hide-headers hide headers on output
-m,--management-system database management system (mysql,
oracle, postgresql ...)
-p,--port database server port
-P,--password database password
-s,--separator column separator (default: "\t")
-U,--usernme database user name
```


## Adding DBMS
Example: Adding support for Microsoft SQL Server. For this purpose we need JDBC driver
sqljdbc4.jar, which can be found [here](https://docs.microsoft.com/en-us/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-ver15).

- Add sqljdbc4.jar file in the root directory of the archive jdbcsql.zip

- Add the name of the driver sqljdbc4.jar in the file jdbcsql.zip/META-INF/MANIFEST
.MF
in the field Rsrc-Class-Path: ./ commons-cli-1.2.jar commons-csv-1.1.jar
postgresql-9.3-1102-jdbc4.jar ...

- Add the following lines in the file Jdbcsql.zip/JDBCConfig.properties:

# sqlserver settings
sqlserver_driver = com.microsoft.sqlserver.jdbc.SQLServerDriver
sqlserver_url = jdbc:sqlserver://host:port;databaseName=dbnam

- The project currently exists with an Exclipse dependency. As such, you need to export it
from eclipse after making these changes to bundle it into a standalone jar file.

From Eclipse
Click: File > Export > Java - Jar > Next

Select all resources for export
Select :
[] Export generated class files and resources
[] Export Java source files and resources
Export directory:
<project dir>/bin/com/mssql-jdbc.jar
Select
[] Compress the contents of the JAR File

Click: Next > Next

Select
[] Use existing manifest file from workplace

Click: Finish

The prefix sqlserver (randomly choosen for this example) becomes an argument of the option
`-m`.

To construct a correct url for jdbc the tool will automatically replace 'host', 'port' and
'dbname' in the string `jdbc:sqlserver://host:port;databaseName=dbname` respectively
with the arguments of the options -h, -p and -d.

The command to query Microsoft SQL Server will look like this:

`java -jar jdbcsql.zip -m sqlserver -h 127.0.0.1 -d dbtest -U sqluser -P ***** 'select *
from table'`

In this manner you can add support in jdbcsql for any DBMS as long as it has a JDBC
driver.

## Examples:

Postgres:
`java -jar jdbcsql.zip -m postgresql -h 'host:port' -d dbtest -U postgres -P
***** 'select * from table'`

`java -jar jdbcsql.zip -m postgresql -h pgsql.host.com -d dbtest -U postgres -P ***** -s
';' 'select * from table'`

> Note:
For DBMS Oracle (and Sybase, for example) the port is mandatory i.e. the -p option is
required. This may be true in in other DBMS as well.
Binary file added test/util/jdbc/jdbc.jar
Binary file not shown.