From 0b09f58608635702c215d7798f3f4b274a248892 Mon Sep 17 00:00:00 2001 From: Akustik Date: Thu, 23 Jul 2020 03:20:27 +0300 Subject: [PATCH 1/3] Travis CI; Java 11; Junit 5; Oracle 18c; Oracle - package instead of standalone SP; --- .travis.yml | 13 + build/setup_mysql.sh | 4 + build/setup_oracle.sh | 40 +++ build/setup_postgres.sh | 4 + pom.xml | 40 ++- readme-oracle-tests.md | 11 +- readme.md | 72 +++-- sql/mysql/create-test-user.sql | 4 + sql/mysql/init/create-test-user.sql | 9 - sql/mysql/schema-mysql-1.sql | 18 +- sql/oracle/create-test-user.sql | 2 + sql/oracle/init/create-test-user.sql | 16 - sql/oracle/schema-oracle-1.sql | 208 ++++++------- sql/postgres/create-test-user.sql | 3 + sql/postgres/init/create-test-user.sql | 3 - sql/postgres/schema-postgres-1.sql | 46 +-- src/main/java/org/morejdbc/DBUtils.java | 15 +- src/main/java/org/morejdbc/JdbcCall.java | 3 +- .../java/org/morejdbc/H2JdbcCallTest.java | 35 ++- .../java/org/morejdbc/MockitoCallTest.java | 102 +++---- .../java/org/morejdbc/MysqlJdbcCallTest.java | 27 +- .../java/org/morejdbc/OracleJdbcCallTest.java | 127 ++++---- .../org/morejdbc/OracleNamedJdbcCallTest.java | 283 +++++++++--------- .../org/morejdbc/PostgresJdbcCallTest.java | 62 ++-- src/test/java/org/morejdbc/TestUtils.java | 27 +- src/test/resources/mysql_test.properties | 2 +- src/test/resources/oracle_test.properties | 2 +- 27 files changed, 589 insertions(+), 589 deletions(-) create mode 100644 .travis.yml create mode 100644 build/setup_mysql.sh create mode 100644 build/setup_oracle.sh create mode 100644 build/setup_postgres.sh create mode 100644 sql/mysql/create-test-user.sql delete mode 100644 sql/mysql/init/create-test-user.sql create mode 100644 sql/oracle/create-test-user.sql delete mode 100644 sql/oracle/init/create-test-user.sql create mode 100644 sql/postgres/create-test-user.sql delete mode 100644 sql/postgres/init/create-test-user.sql diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..296e0c0 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,13 @@ +sudo: required +language: java +services: + - docker + - postgresql + - mysql +before_install: + - ./build/setup_postgres.sh + - ./build/setup_mysql.sh + - ./build/setup_oracle.sh +cache: + directories: + - $HOME/.m2 \ No newline at end of file diff --git a/build/setup_mysql.sh b/build/setup_mysql.sh new file mode 100644 index 0000000..1fbacd9 --- /dev/null +++ b/build/setup_mysql.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +mysql < sql/mysql/create-test-user.sql +mysql -u test --password=test test < sql/mysql/schema-mysql-1.sql \ No newline at end of file diff --git a/build/setup_oracle.sh b/build/setup_oracle.sh new file mode 100644 index 0000000..620262e --- /dev/null +++ b/build/setup_oracle.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash + +CONTAINER_NAME="oracle18" + +# pull from private repo +echo "$DOCKER_PASSWORD" | docker login --username "$DOCKER_USERNAME" --password-stdin +docker pull morejdbc/morejdbc-oracle-18:latest + +# run +echo "Running $CONTAINER_NAME container" +docker run -d --name "$CONTAINER_NAME" -p 1521:1521 --expose=1521 morejdbc/morejdbc-oracle-18:latest + +# wait database to be ready +DB_IS_READY="0" +while test "$DB_IS_READY" = "0"; +do + sleep 5; + + if docker logs "$CONTAINER_NAME" | grep -q 'DATABASE IS READY TO USE'; + then + DB_IS_READY="1" + fi + + if docker logs "$CONTAINER_NAME" | grep -q 'DATABASE SETUP WAS NOT SUCCESSFUL'; + then + exit 1 + fi + echo "It's Oracle. Please wait..." +done; +### + +docker exec -i "$CONTAINER_NAME" sqlplus / as sysdba << EOF +$(cat sql/oracle/create-test-user.sql) +exit; +EOF + +docker exec -i "$CONTAINER_NAME" sqlplus test/test << EOF +$(cat sql/oracle/schema-oracle-1.sql) +exit; +EOF diff --git a/build/setup_postgres.sh b/build/setup_postgres.sh new file mode 100644 index 0000000..6861833 --- /dev/null +++ b/build/setup_postgres.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +psql -U postgres -f sql/postgres/create-test-user.sql +psql -U test -f sql/postgres/schema-postgres-1.sql \ No newline at end of file diff --git a/pom.xml b/pom.xml index 1cfb474..dd7ac16 100644 --- a/pom.xml +++ b/pom.xml @@ -16,11 +16,11 @@ UTF-8 - 1.8 - 1.8 + 1.11 + 1.11 - 1.7.25 - 4.3.26.RELEASE + 1.7.30 + 5.2.7.RELEASE @@ -67,15 +67,21 @@ - junit - junit - 4.12 + org.junit.jupiter + junit-jupiter + 5.6.2 + test + + + org.junit.jupiter + junit-jupiter-engine + 5.6.2 test org.mockito mockito-core - 1.10.19 + 3.4.4 test @@ -95,7 +101,7 @@ mysql mysql-connector-java - 8.0.16 + 8.0.11 test @@ -107,8 +113,8 @@ com.oracle.database.jdbc - ojdbc6 - 11.2.0.4 + ojdbc8 + 19.7.0.0 test @@ -149,8 +155,13 @@ + org.apache.maven.plugins maven-compiler-plugin - 3.6.2 + 3.8.1 + + 11 + 11 + org.apache.maven.plugins @@ -165,6 +176,11 @@ + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M3 + maven-jar-plugin 3.2.0 diff --git a/readme-oracle-tests.md b/readme-oracle-tests.md index 0bf534f..849549d 100644 --- a/readme-oracle-tests.md +++ b/readme-oracle-tests.md @@ -3,20 +3,18 @@ https://download.liquibase.org/download/ Set environment variable LIQUIBASE_HOME Copy driver to $LIQUIBASE_HOME/lib -#### Run docker with Oracle XE 11 release 2 +#### Run docker with Oracle XE 18c (you need to build it first) ``` cd sql/oracle -rm -f init/.cache -docker run --rm -it -p 1521:1521 -v $PWD/init:/docker-entrypoint-initdb.d --name morejdbc-oracle wnameless/oracle-xe-11g-r2 +docker run --name morejdbc-oracle -d -p 1521:1521 oracle/database:18.4.0-xe ``` You can pass the tablespace volume with extra parameter `-v $HOME/oracle_data:/u01/app/oracle` #### Create Oracle schema as test user ``` - # Workaround only for XE and Russian locale (ORA-12705: Cannot access NLS data files or invalid environment specified) # export JAVA_OPTS="-Duser.country=en -Duser.language=en" -$LIQUIBASE_HOME/liquibase --url=jdbc:oracle:thin:@127.0.0.1:1521:XE --username=test --password=test --changeLogFile=changelog.xml --logLevel=info update +$LIQUIBASE_HOME/liquibase --url=jdbc:oracle:thin:@127.0.0.1:1521:XEPDB1 --username=test --password=test --changeLogFile=changelog.xml --logLevel=info update ``` #### Run tests @@ -31,5 +29,4 @@ docker stop morejdbc-oracle ``` For more information: -https://github.com/wnameless/docker-oracle-xe-11g -https://hub.docker.com/r/wnameless/oracle-xe-11g-r2 +https://blogs.oracle.com/oraclemagazine/deliver-oracle-database-18c-express-edition-in-containers diff --git a/readme.md b/readme.md index 2633d7d..2efdbec 100644 --- a/readme.md +++ b/readme.md @@ -1,5 +1,10 @@ +[![license](https://img.shields.io/badge/License-Apache%202.0-green)](https://www.apache.org/licenses/LICENSE-2.0) +[![build](https://travis-ci.com/dmvolodin/morejdbc.svg?branch=master)](https://travis-ci.com/github/dmvolodin/morejdbc) + + + This project contains helper classes to call stored procedures and functions. The base framework is spring (spring-jdbc). -It is fair-typesafe (no unsafe casts inside). Most compatible database is Oracle. +It is fair-typesafe (no unsafe casts inside). The most compatible database is Oracle. To add the library in Maven: ```xml @@ -18,26 +23,27 @@ For Oracle procedure/function calls you can use NamedJdbcCall. The parameters ar * support default value * support overloading -Consider you have a procedure and a function: +Consider you have a procedure and a function in a package: ```sql -CREATE PROCEDURE test_math( - val1 IN number, - val2 number, - out_sum OUT number, - out_mlt OUT number -) IS - BEGIN - out_sum := val1 + val2; - out_mlt := val1 * val2; - END; - -CREATE FUNCTION get_concat(s1 varchar2, s2 varchar2) - RETURN VARCHAR2 -IS - BEGIN - RETURN s1 || s2; - END; +create or replace package body test_more_jdbc_pkg +as + procedure calc_sum_and_multiply_of_two_numbers(p_number1 number, + p_number2 number, + po_sum out number, + po_mlt out number) + is + begin + po_sum := p_number1 + p_number2; + po_mlt := p_number1 * p_number2; + end; + + function get_concat_of_two_strings(p_string1 varchar2, p_string2 varchar2) + return varchar2 is + begin + return p_string1 || p_string2; + end; +end; ``` You can call it: @@ -52,11 +58,11 @@ private JdbcTemplate jdbcTemplate; ... Out sum = Out.of(INTEGER); Out mlt = Out.of(BIGINT); -jdbcTemplate.execute(call("test_math") - .in("val1", 1) - .in("val2", 2L) - .out("out_sum", sum) - .out("out_mlt", mlt) +jdbcTemplate.execute(call("test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers") + .in("p_number1", 1) + .in("p_number2", 2L) + .out("po_sum", sum) + .out("po_mlt", mlt) ); // sum.get() is 3 (Integer) // mlt.get() is 2L (Long) @@ -68,11 +74,11 @@ or in value-consumer style with lambdas ```java AtomicReference sum = new AtomicReference<>(); AtomicReference mlt = new AtomicReference<>(); -jdbcTemplate.execute(call("test_math") - .in("val1", 1) - .in("val2", 2L) - .out("out_sum", INTEGER, sum::set) - .out("out_mlt", BIGINT, mlt::set) +jdbcTemplate.execute(call("test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers") + .in("p_number1", 1) + .in("p_number2", 2L) + .out("po_sum", INTEGER, sum::set) + .out("po_mlt", BIGINT, mlt::set) ); // sum.get() is 3 (Integer) // mlt.get() is 2L (Long) @@ -81,10 +87,10 @@ jdbcTemplate.execute(call("test_math") For functions: ```java -String result = jdbcTemplate.execute(call("get_concat", VARCHAR) - .in("s2", "def") - .in("s1", "abc") // note: reordered s1, s2 +String result = jdbcTemplate.execute(call("test_more_jdbc_pkg.get_concat_of_two_strings", VARCHAR) + .in("p_string2", "def") + .in("p_string1", "abc") // note: reordered p_string1, p_string2 ); -// result is "abcdef" (s1 || s2) +// result is "abcdef" (p_string1 + p_string2) ``` diff --git a/sql/mysql/create-test-user.sql b/sql/mysql/create-test-user.sql new file mode 100644 index 0000000..d513573 --- /dev/null +++ b/sql/mysql/create-test-user.sql @@ -0,0 +1,4 @@ +create database test; +create user 'test'@'%' identified by 'test'; +grant all privileges on *.* to 'test'@'%'; +flush privileges; \ No newline at end of file diff --git a/sql/mysql/init/create-test-user.sql b/sql/mysql/init/create-test-user.sql deleted file mode 100644 index 7fde26b..0000000 --- a/sql/mysql/init/create-test-user.sql +++ /dev/null @@ -1,9 +0,0 @@ -CREATE DATABASE test; - -CREATE USER 'test'@'%' - IDENTIFIED BY 'test'; - -GRANT ALL PRIVILEGES ON *.* TO 'test'@'%'; - -FLUSH PRIVILEGES; - diff --git a/sql/mysql/schema-mysql-1.sql b/sql/mysql/schema-mysql-1.sql index 606543c..a0d2823 100644 --- a/sql/mysql/schema-mysql-1.sql +++ b/sql/mysql/schema-mysql-1.sql @@ -1,19 +1,15 @@ -- liquibase formatted sql -- changeset seregamorph:FEA-1-create-schema-1 splitStatements:false -CREATE PROCEDURE test_math( - IN val1 int, - IN val2 int, - OUT out_sum int, - OUT out_mlt int -) - BEGIN +delimiter // +create procedure test_math(IN val1 int, IN val2 int, OUT out_sum int, OUT out_mlt int) +begin set out_sum = val1 + val2; set out_mlt = val1 * val2; - END; +end; +// -- changeset seregamorph:FEA-1-create-schema-2 splitStatements:false -CREATE FUNCTION get_concat(s1 varchar(50), s2 varchar(50)) - RETURNS VARCHAR(100) DETERMINISTIC - RETURN concat(s1, s2); +create function get_concat(s1 varchar(50), s2 varchar(50)) returns varchar(100) + return concat(s1, s2); diff --git a/sql/oracle/create-test-user.sql b/sql/oracle/create-test-user.sql new file mode 100644 index 0000000..675912f --- /dev/null +++ b/sql/oracle/create-test-user.sql @@ -0,0 +1,2 @@ +create user test identified by test default tablespace users quota 100m on users; +grant connect, resource to test; \ No newline at end of file diff --git a/sql/oracle/init/create-test-user.sql b/sql/oracle/init/create-test-user.sql deleted file mode 100644 index c37c241..0000000 --- a/sql/oracle/init/create-test-user.sql +++ /dev/null @@ -1,16 +0,0 @@ - -CREATE USER test -IDENTIFIED BY test - DEFAULT TABLESPACE users; - -ALTER USER test -quota 100M on users; - -GRANT -CREATE SESSION, -CREATE SEQUENCE, -CREATE TABLE, -CREATE TRIGGER, -CREATE PROCEDURE -TO test; - diff --git a/sql/oracle/schema-oracle-1.sql b/sql/oracle/schema-oracle-1.sql index f6d6933..753631e 100644 --- a/sql/oracle/schema-oracle-1.sql +++ b/sql/oracle/schema-oracle-1.sql @@ -1,108 +1,110 @@ --liquibase formatted sql --changeset seregamorph:FEA-1-create-schema-1 -create table TEMP_PK_TRIGGER ( - ID NUMBER not null primary key, - VALUE VARCHAR2(400 char) -); - -create sequence TEMP_PK_TRIGGER_SEQ; - ---changeset seregamorph:FEA-1-create-schema-2 splitStatements:false -create or replace trigger TEMP_PK_TRIGGER_BI - before insert - on TEMP_PK_TRIGGER - for each row - begin - :new.id := temp_pk_trigger_seq.nextval; - end; - ---changeset seregamorph:FEA-1-create-schema-3 splitStatements:false -CREATE PROCEDURE test_math( - val1 IN number, - val2 number, - out_sum OUT number, - out_mlt OUT number -) IS - BEGIN - out_sum := val1 + val2; - out_mlt := val1 * val2; - END; - ---changeset seregamorph:FEA-1-create-schema-4 splitStatements:false -CREATE FUNCTION get_concat(s1 varchar2, s2 varchar2) - RETURN VARCHAR2 -IS - BEGIN - RETURN s1 || s2; - END; - ---changeset seregamorph:FEA-1-create-schema-5 splitStatements:false -CREATE FUNCTION simple_decode(p_str varchar2) - return varchar2 +create table table_with_identity_pk +( + id number generated always as identity (start with 1 increment by 1) primary key, + value varchar2(400 char) +) +/ + +--changeset seregamorph:FEA-1-create-schema-2 +create or replace package test_more_jdbc_pkg +as + procedure calc_sum_and_multiply_of_two_numbers(p_number1 number, + p_number2 number, + po_sum out number, + po_mlt out number); + + function get_concat_of_two_strings(p_string1 varchar2, p_string2 varchar2) + return varchar2; + + function get_simple_decoded_string(p_string varchar2) + return varchar2; + + function get_cursor_from_key_value_as_string(p_key_value_string varchar2) + return sys_refcursor; + + procedure get_cursor_from_key_value_as_string(p_key_value_string varchar2, po_cursor out sys_refcursor); + + function get_two_blobs_concatenated(p_blob1 blob, p_blob2 blob) + return blob; + + procedure calc_sum_of_two_numbers_with_in_out_parameter(p_number1 number, + p_number2 number, + pio_sum in out number); +end; +/ + +--changeset seregamorph:FEA-1-create-schema-3 +create or replace package body test_more_jdbc_pkg as - v_str varchar2(4000 char); - begin - v_str := replace(p_str, '%3D', '='); - v_str := replace(v_str, '%0A', chr(10)); - v_str := replace(v_str, '%0D', chr(13)); - v_str := replace(v_str, '%3B', ';'); - v_str := replace(v_str, '%7C', '|'); - v_str := replace(v_str, '%25', '%'); - - return v_str; - end simple_decode; - ---changeset seregamorph:FEA-1-create-schema-6 splitStatements:false -CREATE FUNCTION get_extras_tab(extra_string varchar2) - return sys_refcursor -is - v_cur sys_refcursor; - begin - open v_cur for - select - simple_decode(regexp_replace(pair, '([^=]+)(=)(.+)', '\1')) id, - simple_decode(regexp_replace(pair, '([^=]+)(=)(.+)', '\3')) value - from ( - select regexp_substr(extra_string, '[^;]+', 1, level) - as pair - from dual - connect by instr(extra_string, ';', 1, level) > 0 - ); - - return v_cur; - end; - ---changeset seregamorph:FEA-1-create-schema-7 splitStatements:false -CREATE FUNCTION blobs_concat(b1 blob, b2 blob) - return blob -is - b1_copy blob := b1; - begin - dbms_lob.append(b1_copy, b2); - return b1_copy; - end; - ---changeset seregamorph:FEA-1-create-schema-8 splitStatements:false -CREATE PROCEDURE test_in_out(x in number, y number, io_sum in out number) -is - begin - io_sum := x + y + io_sum; - end; - ---changeset seregamorph:FEA-1-create-schema-9 splitStatements:false -CREATE PROCEDURE proc_extras_tab(extra_string varchar2, out_extra_string OUT varchar2, v_cur OUT sys_refcursor) - is -begin - out_extra_string := extra_string; - open v_cur for - select - simple_decode(regexp_replace(pair, '([^=]+)(=)(.+)', '\1')) id, - simple_decode(regexp_replace(pair, '([^=]+)(=)(.+)', '\3')) value - from ( - select regexp_substr(extra_string, '[^;]+', 1, level) - as pair - from dual - connect by instr(extra_string, ';', 1, level) > 0 - ); + procedure calc_sum_and_multiply_of_two_numbers(p_number1 number, + p_number2 number, + po_sum out number, + po_mlt out number) + is + begin + po_sum := p_number1 + p_number2; + po_mlt := p_number1 * p_number2; + end; + + function get_concat_of_two_strings(p_string1 varchar2, p_string2 varchar2) + return varchar2 is + begin + return p_string1 || p_string2; + end; + + function get_simple_decoded_string(p_string varchar2) + return varchar2 is + v_result varchar2(4000 char); + begin + v_result := replace(p_string, '%3D', '='); + v_result := replace(v_result, '%0A', chr(10)); + v_result := replace(v_result, '%0D', chr(13)); + v_result := replace(v_result, '%3B', ';'); + v_result := replace(v_result, '%7C', '|'); + v_result := replace(v_result, '%25', '%'); + + return v_result; + end; + + function get_cursor_from_key_value_as_string(p_key_value_string varchar2) + return sys_refcursor is + v_result sys_refcursor; + begin + open v_result for + select get_simple_decoded_string(regexp_replace(pair, '([^=]+)(=)(.+)', '\1')) id, + get_simple_decoded_string(regexp_replace(pair, '([^=]+)(=)(.+)', '\3')) value + from ( + select regexp_substr(p_key_value_string, '[^;]+', 1, level) as pair + from dual + connect by instr(p_key_value_string, ';', 1, level) > 0 + ); + + return v_result; + end; + + procedure get_cursor_from_key_value_as_string(p_key_value_string varchar2, po_cursor out sys_refcursor) + is + begin + po_cursor := get_cursor_from_key_value_as_string(p_key_value_string); + end; + + function get_two_blobs_concatenated(p_blob1 blob, p_blob2 blob) + return blob is + v_result blob := p_blob1; + begin + dbms_lob.append(v_result, p_blob2); + return v_result; + end; + + procedure calc_sum_of_two_numbers_with_in_out_parameter(p_number1 number, + p_number2 number, + pio_sum in out number) + is + begin + pio_sum := p_number1 + p_number2 + pio_sum; + end; end; +/ \ No newline at end of file diff --git a/sql/postgres/create-test-user.sql b/sql/postgres/create-test-user.sql new file mode 100644 index 0000000..2daba82 --- /dev/null +++ b/sql/postgres/create-test-user.sql @@ -0,0 +1,3 @@ +create user test with encrypted password 'test'; +create database test; +grant all privileges on database test to test; diff --git a/sql/postgres/init/create-test-user.sql b/sql/postgres/init/create-test-user.sql deleted file mode 100644 index d884868..0000000 --- a/sql/postgres/init/create-test-user.sql +++ /dev/null @@ -1,3 +0,0 @@ -CREATE USER test WITH ENCRYPTED PASSWORD 'test'; -CREATE DATABASE test; -GRANT ALL PRIVILEGES ON DATABASE test TO test; diff --git a/sql/postgres/schema-postgres-1.sql b/sql/postgres/schema-postgres-1.sql index 4a40fb9..9d53b94 100644 --- a/sql/postgres/schema-postgres-1.sql +++ b/sql/postgres/schema-postgres-1.sql @@ -1,28 +1,28 @@ --liquibase formatted sql ---changeset seregamorph:FEA-1-create-schema-1 splitStatements:false -CREATE OR REPLACE FUNCTION hi_lo( - a NUMERIC, - b NUMERIC, - c NUMERIC, - OUT hi NUMERIC, - OUT lo NUMERIC) -AS $$ -BEGIN - hi := GREATEST(a, b, c); - lo := LEAST(a, b, c); -END; $$ -LANGUAGE plpgsql; +--changeset seregamorph:fea-1-create-schema-1 splitstatements:false +create or replace function hi_lo( + a numeric, + b numeric, + c numeric, + out hi numeric, + out lo numeric) +as $$ +begin + hi := greatest(a, b, c); + lo := least(a, b, c); +end; $$ +language plpgsql; ---changeset seregamorph:FEA-1-create-schema-2 splitStatements:false -CREATE OR REPLACE FUNCTION refcursorfunc() - RETURNS refcursor AS $$ -DECLARE +--changeset seregamorph:fea-1-create-schema-2 splitstatements:false +create or replace function refcursorfunc() + returns refcursor as $$ +declare mycurs refcursor; -BEGIN - OPEN mycurs FOR SELECT 1 - UNION SELECT 2; - RETURN mycurs; -END; $$ -LANGUAGE plpgsql; +begin + open mycurs for select 1 + union select 2; + return mycurs; +end; $$ +language plpgsql; diff --git a/src/main/java/org/morejdbc/DBUtils.java b/src/main/java/org/morejdbc/DBUtils.java index 7f345db..8fe5358 100644 --- a/src/main/java/org/morejdbc/DBUtils.java +++ b/src/main/java/org/morejdbc/DBUtils.java @@ -2,7 +2,6 @@ import org.jetbrains.annotations.Nullable; -import java.sql.Blob; import java.sql.CallableStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -11,7 +10,7 @@ public class DBUtils { @Nullable public static Integer getIntOrNull(ResultSet rs, String columnName) throws SQLException { - int value = rs.getInt(columnName); + var value = rs.getInt(columnName); if (rs.wasNull()) { return null; } @@ -20,7 +19,7 @@ public static Integer getIntOrNull(ResultSet rs, String columnName) throws SQLEx @Nullable public static Integer getIntOrNull(CallableStatement cs, int idx) throws SQLException { - int value = cs.getInt(idx); + var value = cs.getInt(idx); if (cs.wasNull()) { return null; } @@ -29,7 +28,7 @@ public static Integer getIntOrNull(CallableStatement cs, int idx) throws SQLExce @Nullable public static Long getLongOrNull(ResultSet rs, String columnName) throws SQLException { - long value = rs.getLong(columnName); + var value = rs.getLong(columnName); if (rs.wasNull()) { return null; } @@ -38,7 +37,7 @@ public static Long getLongOrNull(ResultSet rs, String columnName) throws SQLExce @Nullable public static Long getLongOrNull(CallableStatement cs, int idx) throws SQLException { - long value = cs.getLong(idx); + var value = cs.getLong(idx); if (cs.wasNull()) { return null; } @@ -47,7 +46,7 @@ public static Long getLongOrNull(CallableStatement cs, int idx) throws SQLExcept @Nullable public static Double getDoubleOrNull(ResultSet rs, String columnName) throws SQLException { - double value = rs.getDouble(columnName); + var value = rs.getDouble(columnName); if (rs.wasNull()) { return null; } @@ -56,7 +55,7 @@ public static Double getDoubleOrNull(ResultSet rs, String columnName) throws SQL @Nullable public static byte[] getBlobBytes(ResultSet rs, String columnName) throws SQLException { - Blob blob = rs.getBlob(columnName); + var blob = rs.getBlob(columnName); try { return blob == null || blob.length() == 0 ? null : blob.getBytes(1, (int) blob.length()); } finally { @@ -68,7 +67,7 @@ public static byte[] getBlobBytes(ResultSet rs, String columnName) throws SQLExc @Nullable public static byte[] getBlobBytes(CallableStatement cs, int idx) throws SQLException { - Blob blob = cs.getBlob(idx); + var blob = cs.getBlob(idx); try { return blob == null || blob.length() == 0 ? null : blob.getBytes(1, (int) blob.length()); } finally { diff --git a/src/main/java/org/morejdbc/JdbcCall.java b/src/main/java/org/morejdbc/JdbcCall.java index 8a05d13..7ddd711 100644 --- a/src/main/java/org/morejdbc/JdbcCall.java +++ b/src/main/java/org/morejdbc/JdbcCall.java @@ -7,7 +7,6 @@ import org.springframework.jdbc.core.SqlProvider; import java.math.BigDecimal; -import java.sql.CallableStatement; import java.sql.Connection; import java.sql.SQLException; import java.sql.Timestamp; @@ -93,7 +92,7 @@ public Out out(SqlType type) { public Void doInConnection(Connection conn) throws SQLException, DataAccessException { InOut[] parameters = getParameters(); - try (CallableStatement cs = conn.prepareCall(sql)) { + try (var cs = conn.prepareCall(sql)) { for (int i = 0; i < parameters.length; i++) { InOut parameter = parameters[i]; parameter.beforeExecute(cs, i + 1); diff --git a/src/test/java/org/morejdbc/H2JdbcCallTest.java b/src/test/java/org/morejdbc/H2JdbcCallTest.java index b7fe191..b409c71 100644 --- a/src/test/java/org/morejdbc/H2JdbcCallTest.java +++ b/src/test/java/org/morejdbc/H2JdbcCallTest.java @@ -1,39 +1,37 @@ package org.morejdbc; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.jdbc.core.JdbcTemplate; import javax.sql.DataSource; -import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Types; -import java.util.Properties; import java.util.concurrent.atomic.AtomicInteger; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.morejdbc.JdbcCall.callSql; import static org.morejdbc.SqlTypes.INTEGER; public class H2JdbcCallTest { private Connection connection; + private JdbcTemplate jdbc; - @Before + @BeforeEach public void before() throws SQLException { - Properties props = TestUtils.propertiesFromString(TestUtils.readString("h2_test.properties")); + var props = TestUtils.propertiesFromString(TestUtils.readString("h2_test.properties")); this.connection = DriverManager.getConnection(props.getProperty("url"), props); DataSource dataSource = TestUtils.smartDataSource(this.connection); this.jdbc = new JdbcTemplate(dataSource); - jdbc.execute("CREATE ALIAS mult FOR \"org.morejdbc.H2Functions.mult\""); } - @After + @AfterEach public void after() throws SQLException { if (connection != null) { connection.close(); @@ -42,29 +40,34 @@ public void after() throws SQLException { @Test public void testPureJdbc() throws SQLException { - CallableStatement call = connection.prepareCall("{? = call mult(?, ?)}"); + var call = connection.prepareCall("{? = call mult(?, ?)}"); call.registerOutParameter(1, Types.INTEGER); call.setInt(2, 2); call.setInt(3, 3); call.execute(); - assertEquals(6, call.getInt(1)); } @Test public void testMultOut() { - Out out = Out.of(INTEGER); + var out = Out.of(INTEGER); + jdbc.execute(callSql("{? = call mult(?, ?)}") - .out(out).in(2).in(3)); + .out(out) + .in(2) + .in(3)); assertEquals(Integer.valueOf(6), out.get()); } @Test public void testMultConsumer() { - AtomicInteger out = new AtomicInteger(); + var out = new AtomicInteger(); + jdbc.execute(callSql("{? = call mult(?, ?)}") - .out(INTEGER, out::set).in(2).in(3)); + .out(INTEGER, out::set) + .in(2) + .in(3)); assertEquals(6, out.get()); } diff --git a/src/test/java/org/morejdbc/MockitoCallTest.java b/src/test/java/org/morejdbc/MockitoCallTest.java index f82bf01..ae4a40d 100644 --- a/src/test/java/org/morejdbc/MockitoCallTest.java +++ b/src/test/java/org/morejdbc/MockitoCallTest.java @@ -1,6 +1,6 @@ package org.morejdbc; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.jdbc.core.JdbcTemplate; import java.util.Arrays; @@ -8,124 +8,96 @@ import java.util.Map; import java.util.concurrent.atomic.AtomicReference; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.morejdbc.JdbcCall.callSql; import static org.morejdbc.NamedJdbcCall.call; import static org.morejdbc.OracleSqlTypes.cursor; -import static org.morejdbc.SqlTypes.BIGINT; -import static org.morejdbc.SqlTypes.INTEGER; -import static org.morejdbc.SqlTypes.VARCHAR; +import static org.morejdbc.SqlTypes.*; import static org.morejdbc.TestUtils.immutableEntry; public class MockitoCallTest { @Test public void testCallSqlMock() { - JdbcTemplate jdbc = mock(JdbcTemplate.class); - MockOut sum = MockOut.of(INTEGER); - MockOut mlt = MockOut.of(INTEGER); - when(jdbc.execute(callSql("{call test_math(?, ?, ?, ?)}") - .in(10).in(20).out(sum).out(mlt))).then(invocation -> { + var jdbc = mock(JdbcTemplate.class); + var sum = MockOut.of(INTEGER); + var mlt = MockOut.of(INTEGER); + when(jdbc.execute(callSql("{call test_math(?, ?, ?, ?)}").in(10).in(20).out(sum).out(mlt))).then(invocation -> { sum.setTo(invocation.getArguments()[0], 30); mlt.setTo(invocation.getArguments()[0], 200); return null; }); - - Result result = serviceCallSql(jdbc, 10, 20); - + var result = serviceCallSql(jdbc, 10, 20); assertEquals(30, result.sum); assertEquals(200, result.mlt); } @Test public void testCallNamedMock() { - JdbcTemplate jdbc = mock(JdbcTemplate.class); - MockOut sum = MockOut.of(INTEGER); - MockOut mlt = MockOut.of(BIGINT); - when(jdbc.execute(call("test_math") - .in("val1", 10) - .in("val2", 20) - .out("out_sum", sum) - .out("out_mlt", mlt))).then(invocation -> { + var jdbc = mock(JdbcTemplate.class); + var sum = MockOut.of(INTEGER); + var mlt = MockOut.of(BIGINT); + when(jdbc.execute(call("test_math").in("val1", 10).in("val2", 20).out("out_sum", sum).out("out_mlt", mlt))).then(invocation -> { sum.setTo(invocation.getArguments()[0], 30); mlt.setTo(invocation.getArguments()[0], 200L); return null; }); - - Result result = serviceCallNamed(jdbc, 10, 20); - + var result = serviceCallNamed(jdbc, 10, 20); assertEquals(30, result.sum); assertEquals(200, result.mlt); } @Test public void testCallNamedFunctionMock() { - JdbcTemplate jdbc = mock(JdbcTemplate.class); - when(jdbc.execute(call("get_concat", VARCHAR) - .in("s2", "def") - .in("s1", 4))) - .thenReturn("4def"); - - String result = serviceCallNamedFunction(jdbc, 4, "def"); - + var jdbc = mock(JdbcTemplate.class); + when(jdbc.execute(call("get_concat", VARCHAR).in("s2", "def").in("s1", 4))).thenReturn("4def"); + var result = serviceCallNamedFunction(jdbc, 4, "def"); assertEquals("4def", result); } @Test public void testRefCursorOutParam() { - JdbcTemplate jdbc = mock(JdbcTemplate.class); - MockOut>> out = MockOut.of(cursor((rs, rowNum) -> { - return immutableEntry("key", "value"); - })); - when(jdbc.execute(call("proc_extras_tab") - .in("extra_string", "1=value1;2=value2;6=value6;") - .out("v_cur", out))).then(invocation -> { - out.setTo(invocation.getArguments()[0], Arrays.asList( - immutableEntry("1", "value1"), - immutableEntry("2", "value2"), - immutableEntry("6", "value6") - )); + var jdbc = mock(JdbcTemplate.class); + var out = MockOut.of(cursor((rs, rowNum) -> immutableEntry("key", "value"))); + when(jdbc.execute(call("proc_extras_tab").in("extra_string", "1=value1;2=value2;6=value6;").out("v_cur", out))).then(invocation -> { + out.setTo(invocation.getArguments()[0], Arrays.asList(immutableEntry("1", "value1"), immutableEntry("2", "value2"), immutableEntry("6", "value6"))); return null; }); - - List> extras = - serviceTestRefCursorOutParam(jdbc, "1=value1;2=value2;6=value6;"); - assertEquals(extras, Arrays.asList( - immutableEntry("1", "value1"), - immutableEntry("2", "value2"), - immutableEntry("6", "value6") - )); + var extras = serviceTestRefCursorOutParam(jdbc, "1=value1;2=value2;6=value6;"); + assertEquals(Arrays.asList(immutableEntry("1", "value1"), immutableEntry("2", "value2"), immutableEntry("6", "value6")), extras); } - private static List> serviceTestRefCursorOutParam( - JdbcTemplate jdbc, String extra) { - Out>> outExtras = Out.of(cursor((rs, rowNum) -> { - return immutableEntry(rs.getString("id"), rs.getString("value")); - })); - jdbc.execute(call("proc_extras_tab") - .in("extra_string", extra) - .out("v_cur", outExtras)); + private static List> serviceTestRefCursorOutParam(JdbcTemplate jdbc, String extra) { + var outExtras = Out.of(cursor((rs, rowNum) -> immutableEntry(rs.getString("id"), rs.getString("value")))); + jdbc.execute(call("proc_extras_tab").in("extra_string", extra).out("v_cur", outExtras)); return outExtras.get(); } private static Result serviceCallSql(JdbcTemplate jdbc, int val1, int val2) { - Out sum = Out.of(INTEGER); - Out mlt = Out.of(INTEGER); + var sum = Out.of(INTEGER); + var mlt = Out.of(INTEGER); + jdbc.execute(callSql("{call test_math(?, ?, ?, ?)}") - .in(val1).in(val2).out(sum).out(mlt)); + .in(val1) + .in(val2) + .out(sum) + .out(mlt)); + return new Result(sum.get(), mlt.get()); } private static Result serviceCallNamed(JdbcTemplate jdbc, int val1, int val2) { - AtomicReference sum = new AtomicReference<>(); - AtomicReference mlt = new AtomicReference<>(); + var sum = new AtomicReference(); + var mlt = new AtomicReference(); + jdbc.execute(call("test_math") .in("val1", val1) .in("val2", val2) .out("out_sum", INTEGER, sum::set) .out("out_mlt", BIGINT, mlt::set)); + return new Result(sum.get(), mlt.get()); } diff --git a/src/test/java/org/morejdbc/MysqlJdbcCallTest.java b/src/test/java/org/morejdbc/MysqlJdbcCallTest.java index 9a86e99..1620bdf 100644 --- a/src/test/java/org/morejdbc/MysqlJdbcCallTest.java +++ b/src/test/java/org/morejdbc/MysqlJdbcCallTest.java @@ -1,17 +1,16 @@ package org.morejdbc; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.jdbc.core.JdbcTemplate; import javax.sql.DataSource; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; -import java.util.Properties; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.morejdbc.JdbcCall.callSql; import static org.morejdbc.SqlTypes.INTEGER; @@ -23,15 +22,15 @@ public class MysqlJdbcCallTest { private Connection connection; private JdbcTemplate jdbc; - @Before + @BeforeEach public void before() throws SQLException { - Properties props = TestUtils.propertiesFromString(TestUtils.readString("mysql_test.properties")); + var props = TestUtils.propertiesFromString(TestUtils.readString("mysql_test.properties")); this.connection = DriverManager.getConnection(props.getProperty("url"), props); DataSource dataSource = TestUtils.smartDataSource(this.connection); this.jdbc = new JdbcTemplate(dataSource); } - @After + @AfterEach public void after() throws SQLException { if (connection != null) { connection.close(); @@ -40,11 +39,15 @@ public void after() throws SQLException { @Test public void testMath() { - Out sum = Out.of(INTEGER); - Out mlt = Out.of(INTEGER); + var sum = Out.of(INTEGER); + var mlt = Out.of(INTEGER); + jdbc.execute(callSql("{call test_math(?, ?, ?, ?)}") - .in(10).in(20).out(sum).out(mlt) - ); + .in(10) + .in(20) + .out(sum) + .out(mlt)); + assertEquals(30, sum.get().intValue()); assertEquals(200, mlt.get().intValue()); } diff --git a/src/test/java/org/morejdbc/OracleJdbcCallTest.java b/src/test/java/org/morejdbc/OracleJdbcCallTest.java index 96abce4..8e1c5f7 100644 --- a/src/test/java/org/morejdbc/OracleJdbcCallTest.java +++ b/src/test/java/org/morejdbc/OracleJdbcCallTest.java @@ -1,8 +1,8 @@ package org.morejdbc; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.jdbc.BadSqlGrammarException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.SqlTypeValue; @@ -13,23 +13,14 @@ import java.sql.DriverManager; import java.sql.SQLException; import java.util.Arrays; -import java.util.List; import java.util.Locale; -import java.util.Map; -import java.util.Properties; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.atomic.AtomicReference; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.*; import static org.morejdbc.JdbcCall.callSql; import static org.morejdbc.OracleSqlTypes.cursor; -import static org.morejdbc.SqlTypes.BIGINT; -import static org.morejdbc.SqlTypes.BLOB; -import static org.morejdbc.SqlTypes.INTEGER; -import static org.morejdbc.SqlTypes.VARCHAR; +import static org.morejdbc.SqlTypes.*; import static org.morejdbc.TestUtils.immutableEntry; import static org.morejdbc.TestUtils.jdbc; @@ -38,15 +29,19 @@ */ public class OracleJdbcCallTest { + /** + * the default datatype for null variables is defined as varchar2(32) + */ + private static final SqlType UNKNOWN = SqlType.of("unknown", SqlTypeValue.TYPE_UNKNOWN, StatementCreatorUtils::setParameterValue, CallableStatement::getObject); private Connection connection; private JdbcTemplate jdbc; - @Before + @BeforeEach public void before() throws SQLException { - Properties props = TestUtils.propertiesFromString(TestUtils.readString("oracle_test.properties")); - Locale def = Locale.getDefault(); + var props = TestUtils.propertiesFromString(TestUtils.readString("oracle_test.properties")); + var def = Locale.getDefault(); try { - // workarond for XE with russian locale + // workaround for XE with russian locale Locale.setDefault(Locale.ENGLISH); this.connection = DriverManager.getConnection(props.getProperty("url"), props); } finally { @@ -55,7 +50,7 @@ public void before() throws SQLException { this.jdbc = jdbc(this.connection); } - @After + @AfterEach public void after() throws SQLException { if (connection != null) { connection.close(); @@ -64,87 +59,83 @@ public void after() throws SQLException { @Test public void testCall1() { - Out sum = Out.of(INTEGER); - Out mlt = Out.of(INTEGER); - jdbc.execute(callSql("{call test_math(?, ?, ?, ?)}") - .in(1).in(2).out(sum).out(mlt) - ); - assertEquals(sum.get(), Integer.valueOf(3)); - assertEquals(mlt.get(), Integer.valueOf(2)); - } + var sum = Out.of(INTEGER); + var mlt = Out.of(INTEGER); - /** - * the default datatype for null variables is defined as varchar2(32) - */ - private static final SqlType UNKNOWN = SqlType.of("unknown", SqlTypeValue.TYPE_UNKNOWN, - StatementCreatorUtils::setParameterValue, CallableStatement::getObject); + jdbc.execute(callSql("{call test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers(?, ?, ?, ?)}") + .in(8) + .in(3) + .out(sum) + .out(mlt)); + + assertEquals(Integer.valueOf(11), sum.get()); + assertEquals(Integer.valueOf(24), mlt.get()); + } @Test public void testCallBadArgs() { - Out sum = Out.of(INTEGER); - Out mlt = Out.of(INTEGER); + var sum = Out.of(INTEGER); + var mlt = Out.of(INTEGER); try { - jdbc.execute(callSql("{call test_math(?, ?, ?, ?, ?)}") - .in(1).in(2L).out(sum).out(mlt).in(null, UNKNOWN) - ); + jdbc.execute(callSql("{call test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers(?, ?, ?, ?, ?)}") + .in(0) + .in(1L) + .out(sum) + .out(mlt) + .in(null, UNKNOWN)); + fail(); } catch (BadSqlGrammarException e) { // sql via SqlProvider - assertTrue(e.getMessage() - .contains("bad SQL grammar [{call test_math(?, ?, ?, ?, ?)}];")); + assertTrue(e.getMessage().contains("bad SQL grammar [{call test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers(?, ?, ?, ?, ?)}];")); } } @Test public void testInsertReturning() { - // temp_pk_trigger (id number(9), value varchar2(20 char)); + // table_with_identity_pk (id number(9) identity primary key, value varchar2(20 char)); // checks charset - String valueIn = "тест" + System.currentTimeMillis(); - Out idOut = Out.of(BIGINT); - AtomicReference valueOut = new AtomicReference<>(); - jdbc.execute(callSql( - "BEGIN INSERT INTO temp_pk_trigger(value) VALUES (?) " + - "RETURNING id, value INTO ?, ?; END;") + var valueIn = "тест" + System.currentTimeMillis(); + var idOut = Out.of(BIGINT); + var valueOut = new AtomicReference(); + + jdbc.execute(callSql("begin insert into table_with_identity_pk (value) values (?) " + "returning id, value into ?, ?; end;") .in(valueIn) - .out(idOut).out(VARCHAR, valueOut::set) - ); + .out(idOut) + .out(VARCHAR, valueOut::set)); + System.out.println(idOut.get()); System.out.println(valueOut.get()); assertTrue(idOut.get() > 0); - assertEquals(valueOut.get(), valueIn); + assertEquals(valueIn, valueOut.get()); } @Test public void testCallFuncResultSet() { - Out>> extras = Out.of(cursor((row, rowNum) -> { - return immutableEntry(row.getString("id"), row.getString("value")); - })); - jdbc.execute(callSql("{? = call get_extras_tab(?)}") - .out(extras).in("1=value1;2=value2;6=value6;") - ); - assertEquals(extras.get(), Arrays.asList( - immutableEntry("1", "value1"), - immutableEntry("2", "value2"), - immutableEntry("6", "value6") - )); + var extras = Out.of(cursor((row, rowNum) -> immutableEntry(row.getString("id"), row.getString("value")))); + + jdbc.execute(callSql("{? = call test_more_jdbc_pkg.get_cursor_from_key_value_as_string(?)}") + .out(extras) + .in("1=value1;2=value2;6=value6;")); + + assertEquals(Arrays.asList(immutableEntry("1", "value1"), immutableEntry("2", "value2"), immutableEntry("6", "value6")), extras.get()); } @Test public void testLongBlobConcat() { // > 4000 bytes - byte[] blob1 = new byte[4096]; - byte[] blob2 = new byte[4096]; + var blob1 = new byte[4096]; + var blob2 = new byte[4096]; ThreadLocalRandom.current().nextBytes(blob1); ThreadLocalRandom.current().nextBytes(blob2); + var result = Out.of(BLOB); - Out result = Out.of(BLOB); - - jdbc.execute(callSql("{? = call blobs_concat(?, ?)}") + jdbc.execute(callSql("{? = call test_more_jdbc_pkg.get_two_blobs_concatenated(?, ?)}") .out(result) - .in(blob1).in(blob2) - ); + .in(blob1) + .in(blob2)); - byte[] expected = TestUtils.concat(blob1, blob2); + var expected = TestUtils.concat(blob1, blob2); assertArrayEquals(expected, result.get()); } } diff --git a/src/test/java/org/morejdbc/OracleNamedJdbcCallTest.java b/src/test/java/org/morejdbc/OracleNamedJdbcCallTest.java index 451976f..ce19c57 100644 --- a/src/test/java/org/morejdbc/OracleNamedJdbcCallTest.java +++ b/src/test/java/org/morejdbc/OracleNamedJdbcCallTest.java @@ -1,8 +1,8 @@ package org.morejdbc; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.SqlTypeValue; @@ -14,23 +14,16 @@ import java.sql.DriverManager; import java.sql.SQLException; import java.util.Arrays; -import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.Properties; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.atomic.AtomicReference; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.morejdbc.NamedJdbcCall.call; import static org.morejdbc.OracleSqlTypes.cursor; -import static org.morejdbc.SqlTypes.BIGINT; -import static org.morejdbc.SqlTypes.BINARY; -import static org.morejdbc.SqlTypes.BLOB; -import static org.morejdbc.SqlTypes.DECIMAL; -import static org.morejdbc.SqlTypes.INTEGER; -import static org.morejdbc.SqlTypes.VARCHAR; +import static org.morejdbc.SqlTypes.*; import static org.morejdbc.TestUtils.immutableEntry; import static org.morejdbc.TestUtils.jdbc; @@ -39,13 +32,19 @@ */ public class OracleNamedJdbcCallTest { + /** + * the default datatype for null variables is defined as varchar2(32) + */ + private static final SqlType UNKNOWN = SqlType.of("unknown", SqlTypeValue.TYPE_UNKNOWN, StatementCreatorUtils::setParameterValue, CallableStatement::getObject); + private Connection connection; + private JdbcTemplate jdbc; - @Before + @BeforeEach public void before() throws SQLException { - Properties props = TestUtils.propertiesFromString(TestUtils.readString("oracle_test.properties")); - Locale def = Locale.getDefault(); + var props = TestUtils.propertiesFromString(TestUtils.readString("oracle_test.properties")); + var def = Locale.getDefault(); try { // workaround for XE with russian locale Locale.setDefault(Locale.ENGLISH); @@ -56,7 +55,7 @@ public void before() throws SQLException { this.jdbc = jdbc(this.connection); } - @After + @AfterEach public void after() throws SQLException { if (connection != null) { connection.close(); @@ -65,229 +64,215 @@ public void after() throws SQLException { @Test public void testNamedCall1() { - Out sum = Out.of(INTEGER); - Out mlt = Out.of(BIGINT); + var sum = Out.of(INTEGER); + var mlt = Out.of(BIGINT); - jdbc.execute(call("test_math") - .in("val1", 1) - .in("val2", 2L) - .out("out_sum", sum) - .out("out_mlt", mlt)); + jdbc.execute(call("test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers") + .in("p_number1", 1) + .in("p_number2", 2L) + .out("po_sum", sum) + .out("po_mlt", mlt)); - assertEquals(sum.get(), Integer.valueOf(3)); - assertEquals(mlt.get(), Long.valueOf(2L)); + assertEquals(Integer.valueOf(3), sum.get()); + assertEquals(Long.valueOf(2L), mlt.get()); } @Test public void testNamedCall1Consumer() { - AtomicReference sum = new AtomicReference<>(); - AtomicReference mlt = new AtomicReference<>(); + var sum = new AtomicReference(); + var mlt = new AtomicReference(); - jdbc.execute(call("test_math") - .in("val1", 1) - .in("val2", 2L) - .out("out_sum", INTEGER, sum::set) - .out("out_mlt", BIGINT, mlt::set)); + jdbc.execute(call("test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers") + .in("p_number1", 1) + .in("p_number2", 2L) + .out("po_sum", INTEGER, sum::set) + .out("po_mlt", BIGINT, mlt::set)); - assertEquals(sum.get(), Integer.valueOf(3)); - assertEquals(mlt.get(), Long.valueOf(2L)); + assertEquals(Integer.valueOf(3), sum.get()); + assertEquals(Long.valueOf(2L), mlt.get()); } @Test public void testNamedCall2() { - Out sum = Out.of(INTEGER); - Out mlt = Out.of(INTEGER); + var sum = Out.of(INTEGER); + var mlt = Out.of(INTEGER); - jdbc.execute(call("test_math") - .out("out_mlt", mlt) - .out("out_sum", sum) - .in("val1", "1") - .in("val2", new BigDecimal(2))); + jdbc.execute(call("test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers") + .out("po_mlt", mlt) + .out("po_sum", sum) + .in("p_number1", "1") + .in("p_number2", new BigDecimal(2))); - assertEquals(sum.get(), Integer.valueOf(3)); - assertEquals(mlt.get(), Integer.valueOf(2)); + assertEquals(Integer.valueOf(3), sum.get()); + assertEquals(Integer.valueOf(2), mlt.get()); } @Test public void testNamedCall3() { - NamedJdbcCall call = call("test_math") - .in("val1", 1) - .in("val2", 2); - Out sum = call.out("out_sum", INTEGER); - Out mlt = call.out("out_mlt", INTEGER); - + var call = call("test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers") + .in("p_number1", 1) + .in("p_number2", 2); + var sum = call.out("po_sum", INTEGER); + var mlt = call.out("po_mlt", INTEGER); jdbc.execute(call); - assertEquals(sum.get(), Integer.valueOf(3)); - assertEquals(mlt.get(), Integer.valueOf(2)); + assertEquals(Integer.valueOf(3), sum.get()); + assertEquals(Integer.valueOf(2), mlt.get()); } @Test public void testNamedCallFunc1() { - String result = jdbc.execute(call("get_concat", VARCHAR) - .in("s1", "abc") - .in("s2", (String) null)); + var result = jdbc.execute(call("test_more_jdbc_pkg.get_concat_of_two_strings", VARCHAR) + .in("p_string1", "abc") + .in("p_string2", (String) null)); - assertEquals(result, "abc"); + assertEquals("abc", result); } @Test public void testNamedCallFunc2() { - // reorder s1, s2 - String result = jdbc.execute(call("get_concat", VARCHAR) - .in("s2", new StringBuilder("WL-1")) - .in("s1", "abc")); + // reordered p_string1, p_string2 + var result = jdbc.execute(call("test_more_jdbc_pkg.get_concat_of_two_strings", VARCHAR) + .in("p_string2", new StringBuilder("XYZ")) + .in("p_string1", "abc")); - assertEquals(result, "abcWL-1"); + assertEquals("abcXYZ", result); } @Test public void testNamedCallFunc3() { - String result = jdbc.execute(call("get_concat", VARCHAR) - .in("s2", "def") - .in("s1", 4)); + var result = jdbc.execute(call("test_more_jdbc_pkg.get_concat_of_two_strings", VARCHAR) + .in("p_string2", "def") + .in("p_string1", 4)); - assertEquals(result, "4def"); + assertEquals("4def", result); } - /** - * the default datatype for null variables is defined as varchar2(32) - */ - private static final SqlType UNKNOWN = SqlType.of("unknown", SqlTypeValue.TYPE_UNKNOWN, - StatementCreatorUtils::setParameterValue, CallableStatement::getObject); - @Test public void testNamedCallFunc4() { // pass null-string with unknown type - String result = jdbc.execute(call("get_concat", VARCHAR) - .in("s1", "abc") - .in("s2", null, UNKNOWN)); + var result = jdbc.execute(call("test_more_jdbc_pkg.get_concat_of_two_strings", VARCHAR) + .in("p_string1", "abc") + .in("p_string2", null, UNKNOWN)); - assertEquals(result, "abc"); + assertEquals("abc", result); } @Test public void testNamedCallFuncResultSet() { - RowMapper> mapper = (rs, rowNum) -> { - return immutableEntry(rs.getString("id"), rs.getString("value")); - }; - - List> extras = jdbc.execute(call("get_extras_tab", cursor(mapper)) - .in("extra_string", "1=value1;2=value2;6=value6;")); - - assertEquals(extras, Arrays.asList( - immutableEntry("1", "value1"), - immutableEntry("2", "value2"), - immutableEntry("6", "value6") - )); + RowMapper> mapper = (rs, rowNum) -> immutableEntry(rs.getString("id"), rs.getString("value")); + + var extras = jdbc.execute(call("test_more_jdbc_pkg.get_cursor_from_key_value_as_string", cursor(mapper)) + .in("p_key_value_string", "1=value1;2=value2;6=value6;")); + + assertEquals(Arrays.asList(immutableEntry("1", "value1"), immutableEntry("2", "value2"), immutableEntry("6", "value6")), extras); } @Test public void testRefCursorOutParam() { - RowMapper> mapper = (rs, rowNum) -> { - return immutableEntry(rs.getString("id"), rs.getString("value")); - }; - Out outExtraString = Out.of(VARCHAR); - Out>> outExtras = Out.of(cursor(mapper)); - - jdbc.execute(call("proc_extras_tab") - .in("extra_string", "1=value1;2=value2;6=value6;") - .out("out_extra_string", outExtraString) - .out("v_cur", outExtras)); - - assertEquals(outExtras.get(), Arrays.asList( - immutableEntry("1", "value1"), - immutableEntry("2", "value2"), - immutableEntry("6", "value6") - )); + RowMapper> mapper = (rs, rowNum) -> immutableEntry(rs.getString("id"), rs.getString("value")); + var outExtras = Out.of(cursor(mapper)); + + jdbc.execute(call("test_more_jdbc_pkg.get_cursor_from_key_value_as_string") + .in("p_key_value_string", "1=value1;2=value2;6=value6;") + .out("po_cursor", outExtras)); + + assertEquals(Arrays.asList(immutableEntry("1", "value1"), immutableEntry("2", "value2"), immutableEntry("6", "value6")), outExtras.get()); } @Test public void testNamedCallInOut1() { - Out sum = Out.of(DECIMAL); - jdbc.execute(call("test_in_out") - .in("x", 1) - .inOut("io_sum", new BigDecimal(5), sum) - .in("y", 2)); + var sum = Out.of(DECIMAL); - assertEquals(sum.get(), new BigDecimal(8)); + jdbc.execute(call("test_more_jdbc_pkg.calc_sum_of_two_numbers_with_in_out_parameter") + .in("p_number1", 1) + .inOut("pio_sum", new BigDecimal(5), sum) + .in("p_number2", 2)); + + assertEquals(new BigDecimal(8), sum.get()); } @Test public void testNamedCallInOut2() { - Out sum = Out.of(INTEGER); - jdbc.execute(call("test_in_out") - .in("x", 1) - .inOut("io_sum", 5, sum) - .in("y", 2)); + var sum = Out.of(INTEGER); + + jdbc.execute(call("test_more_jdbc_pkg.calc_sum_of_two_numbers_with_in_out_parameter") + .in("p_number1", 1) + .inOut("pio_sum", 5, sum) + .in("p_number2", 2)); - assertEquals(sum.get(), Integer.valueOf(8)); + assertEquals(Integer.valueOf(8), sum.get()); } @Test public void testNamedCallInOut2Consumer() { - AtomicReference sum = new AtomicReference<>(); - jdbc.execute(call("test_in_out") - .in("x", 1) - .inOut("io_sum", 5, sum::set) - .in("y", 2)); + var sum = new AtomicReference(); - assertEquals(sum.get(), Integer.valueOf(8)); + jdbc.execute(call("test_more_jdbc_pkg.calc_sum_of_two_numbers_with_in_out_parameter") + .in("p_number1", 1) + .inOut("pio_sum", 5, sum::set) + .in("p_number2", 2)); + + assertEquals(Integer.valueOf(8), sum.get()); } @Test public void testPackageCallable4arg() { - Out sum = Out.of(BIGINT); - Out mlt = Out.of(INTEGER); - jdbc.execute(call("test_math") - .out("out_sum", sum) - .in("val1", 1) - .in("val2", 2) - .out("out_mlt", mlt)); - assertEquals(sum.get(), Long.valueOf(3L)); - assertEquals(mlt.get(), Integer.valueOf(2)); + var sum = Out.of(BIGINT); + var mlt = Out.of(INTEGER); + + jdbc.execute(call("test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers") + .out("po_sum", sum) + .in("p_number1", 1) + .in("p_number2", 2) + .out("po_mlt", mlt)); + + assertEquals(Long.valueOf(3L), sum.get()); + assertEquals(Integer.valueOf(2), mlt.get()); } @Test public void testPackageCallable3arg() { - Out sum = Out.of(INTEGER); - jdbc.execute(call("test_math") - .in("val1", 1) - .in("val2", 2) - .out("out_sum", sum) - .out("out_mlt", Out.of(INTEGER))); - assertEquals(sum.get(), Integer.valueOf(3)); + var sum = Out.of(INTEGER); + + jdbc.execute(call("test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers") + .in("p_number1", 1) + .in("p_number2", 2) + .out("po_sum", sum) + .out("po_mlt", Out.of(INTEGER))); + + assertEquals(Integer.valueOf(3), sum.get()); } @Test public void testLongBinaryConcat() { // > 4000 bytes - byte[] blob1 = new byte[4096]; - byte[] blob2 = new byte[4096]; + var blob1 = new byte[4096]; + var blob2 = new byte[4096]; ThreadLocalRandom.current().nextBytes(blob1); ThreadLocalRandom.current().nextBytes(blob2); - byte[] result = jdbc.execute(call("blobs_concat", BINARY) - .in("b1", blob1, BINARY) - .in("b2", blob2, BINARY)); + var result = jdbc.execute(call("test_more_jdbc_pkg.get_two_blobs_concatenated", BINARY) + .in("p_blob1", blob1, BINARY) + .in("p_blob2", blob2, BINARY)); - byte[] expected = TestUtils.concat(blob1, blob2); + var expected = TestUtils.concat(blob1, blob2); assertArrayEquals(expected, result); } @Test public void testLongBlobConcat() { // > 4000 bytes - byte[] blob1 = new byte[4096]; - byte[] blob2 = new byte[4096]; + var blob1 = new byte[4096]; + var blob2 = new byte[4096]; ThreadLocalRandom.current().nextBytes(blob1); ThreadLocalRandom.current().nextBytes(blob2); - byte[] result = jdbc.execute(call("blobs_concat", BLOB) - .in("b1", blob1) - .in("b2", blob2)); + var result = jdbc.execute(call("test_more_jdbc_pkg.get_two_blobs_concatenated", BLOB) + .in("p_blob1", blob1) + .in("p_blob2", blob2)); - byte[] expected = TestUtils.concat(blob1, blob2); + var expected = TestUtils.concat(blob1, blob2); assertArrayEquals(expected, result); } } diff --git a/src/test/java/org/morejdbc/PostgresJdbcCallTest.java b/src/test/java/org/morejdbc/PostgresJdbcCallTest.java index 1857796..70bd196 100644 --- a/src/test/java/org/morejdbc/PostgresJdbcCallTest.java +++ b/src/test/java/org/morejdbc/PostgresJdbcCallTest.java @@ -1,8 +1,8 @@ package org.morejdbc; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.jdbc.core.CallableStatementCallback; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DataSourceTransactionManager; @@ -11,16 +11,13 @@ import javax.sql.DataSource; import java.math.BigDecimal; -import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Arrays; -import java.util.List; -import java.util.Properties; import java.util.concurrent.atomic.AtomicReference; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.morejdbc.JdbcCall.callSql; import static org.morejdbc.PostgresSqlTypes.cursor; import static org.morejdbc.SqlTypes.NUMERIC; @@ -31,12 +28,14 @@ public class PostgresJdbcCallTest { private Connection connection; + private JdbcTemplate jdbc; + private TransactionTemplate transactionTemplate; - @Before + @BeforeEach public void before() throws SQLException { - Properties props = TestUtils.propertiesFromString(TestUtils.readString("psql_test.properties")); + var props = TestUtils.propertiesFromString(TestUtils.readString("psql_test.properties")); this.connection = DriverManager.getConnection(props.getProperty("url"), props); DataSource dataSource = TestUtils.smartDataSource(this.connection); PlatformTransactionManager transactionManager = new DataSourceTransactionManager(dataSource); @@ -44,7 +43,7 @@ public void before() throws SQLException { this.jdbc = new JdbcTemplate(dataSource); } - @After + @AfterEach public void after() throws SQLException { if (connection != null) { connection.close(); @@ -53,18 +52,14 @@ public void after() throws SQLException { @Test public void testSelect() { - List list = jdbc.query( - "SELECT hi, lo FROM hi_lo(?, ?, ?)", - (row, rowNum) -> { - HiLo hl = new HiLo(); - hl.hi = row.getInt("hi"); - hl.lo = row.getInt("lo"); - return hl; - }, - 10, 20, 30 - ); + var list = jdbc.query("SELECT hi, lo FROM hi_lo(?, ?, ?)", (row, rowNum) -> { + var hl = new HiLo(); + hl.hi = row.getInt("hi"); + hl.lo = row.getInt("lo"); + return hl; + }, 10, 20, 30); assertEquals(1, list.size()); - HiLo hl = list.get(0); + var hl = list.get(0); assertEquals(30, hl.hi); assertEquals(10, hl.lo); } @@ -72,7 +67,7 @@ public void testSelect() { @Test public void testCallExecute() { jdbc.execute(con -> { - CallableStatement cs = con.prepareCall("{call hi_lo(?, ?, ?, ?, ?)}"); + var cs = con.prepareCall("{call hi_lo(?, ?, ?, ?, ?)}"); cs.setInt(1, 10); cs.setInt(2, 20); cs.setInt(3, 30); @@ -81,7 +76,6 @@ public void testCallExecute() { return cs; }, (CallableStatementCallback) cs -> { cs.execute(); - assertEquals(new BigDecimal(30), cs.getBigDecimal(4)); assertEquals(new BigDecimal(10), cs.getBigDecimal(5)); return null; @@ -90,16 +84,16 @@ public void testCallExecute() { @Test public void testCall() { - AtomicReference hi = new AtomicReference<>(); - AtomicReference lo = new AtomicReference<>(); - jdbc.execute(callSql( - "{call hi_lo(?, ?, ?, ?, ?)}") + var hi = new AtomicReference(); + var lo = new AtomicReference(); + + jdbc.execute(callSql("{call hi_lo(?, ?, ?, ?, ?)}") .in(10) .in(20) .in(30) .out(NUMERIC, hi::set) - .out(NUMERIC, lo::set) - ); + .out(NUMERIC, lo::set)); + assertEquals(30, hi.get().intValue()); assertEquals(10, lo.get().intValue()); } @@ -107,11 +101,11 @@ public void testCall() { @Test public void testRefcursor() { // refcursor out works only in transaction - List values = transactionTemplate.execute(transaction -> { - Out> outValues = Out.of(cursor((row, rowNum) -> row.getInt(1))); - jdbc.execute(callSql("{ ? = call refcursorfunc() }") - .out(outValues) - ); + var values = transactionTemplate.execute(transaction -> { + var outValues = Out.of(cursor((row, rowNum) -> row.getInt(1))); + + jdbc.execute(callSql("{ ? = call refcursorfunc() }").out(outValues)); + return outValues.get(); }); assertEquals(Arrays.asList(1, 2), values); diff --git a/src/test/java/org/morejdbc/TestUtils.java b/src/test/java/org/morejdbc/TestUtils.java index 1341404..0f28b5b 100644 --- a/src/test/java/org/morejdbc/TestUtils.java +++ b/src/test/java/org/morejdbc/TestUtils.java @@ -4,12 +4,7 @@ import org.springframework.jdbc.datasource.SingleConnectionDataSource; import org.springframework.jdbc.datasource.SmartDataSource; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.StringReader; -import java.io.UncheckedIOException; -import java.net.URL; +import java.io.*; import java.sql.Connection; import java.util.AbstractMap; import java.util.Map; @@ -20,7 +15,7 @@ class TestUtils { static Properties propertiesFromString(String str) { - Properties properties = new Properties(); + var properties = new Properties(); if (str != null && !str.isEmpty()) { try { properties.load(new StringReader(str)); @@ -45,13 +40,13 @@ static void closeQuietly(InputStream in) { } static byte[] concat(byte[]... arrays) { - int length = 0; - for (byte[] array : arrays) { + var length = 0; + for (var array : arrays) { length += array.length; } - byte[] result = new byte[length]; - int pos = 0; - for (byte[] array : arrays) { + var result = new byte[length]; + var pos = 0; + for (var array : arrays) { System.arraycopy(array, 0, result, pos, array.length); pos += array.length; } @@ -59,7 +54,7 @@ static byte[] concat(byte[]... arrays) { } static byte[] readBytes(ClassLoader classLoader, String resource) { - URL url = classLoader.getResource(resource); + var url = classLoader.getResource(resource); if (url == null) { throw new IllegalStateException("Missing resource [" + resource + "]"); } @@ -82,8 +77,8 @@ static String readString(String resource) { } private static byte[] toByteArray(InputStream input) throws IOException { - ByteArrayOutputStream output = new ByteArrayOutputStream(); - byte[] buffer = new byte[8192]; + var output = new ByteArrayOutputStream(); + var buffer = new byte[8192]; int n; while ((n = input.read(buffer)) != -1) { output.write(buffer, 0, n); @@ -92,7 +87,7 @@ private static byte[] toByteArray(InputStream input) throws IOException { } static JdbcTemplate jdbc(Connection connection) { - SmartDataSource ds = smartDataSource(connection); + var ds = smartDataSource(connection); return new JdbcTemplate(ds); } diff --git a/src/test/resources/mysql_test.properties b/src/test/resources/mysql_test.properties index 0648e8c..4cfb4bf 100644 --- a/src/test/resources/mysql_test.properties +++ b/src/test/resources/mysql_test.properties @@ -1,4 +1,4 @@ -url=jdbc:mysql://127.0.0.1:3306/test +url=jdbc:mysql://127.0.0.1:3306/test?useSSL=false user=test password=test serverTimezone=UTC diff --git a/src/test/resources/oracle_test.properties b/src/test/resources/oracle_test.properties index 8c904f6..048d3c8 100644 --- a/src/test/resources/oracle_test.properties +++ b/src/test/resources/oracle_test.properties @@ -1,3 +1,3 @@ -url=jdbc:oracle:thin:@127.0.0.1:1521:XE +url=jdbc:oracle:thin:@//127.0.0.1:1521/XEPDB1 user=test password=test From a5278195f465cc7a3513229761931d6d0b73613d Mon Sep 17 00:00:00 2001 From: Akustik Date: Thu, 23 Jul 2020 03:33:37 +0300 Subject: [PATCH 2/3] +executable sh --- build/setup_mysql.sh | 0 build/setup_oracle.sh | 0 build/setup_postgres.sh | 0 3 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 build/setup_mysql.sh mode change 100644 => 100755 build/setup_oracle.sh mode change 100644 => 100755 build/setup_postgres.sh diff --git a/build/setup_mysql.sh b/build/setup_mysql.sh old mode 100644 new mode 100755 diff --git a/build/setup_oracle.sh b/build/setup_oracle.sh old mode 100644 new mode 100755 diff --git a/build/setup_postgres.sh b/build/setup_postgres.sh old mode 100644 new mode 100755 From c12a677fbd84dcb9db9204f775efe762970fecac Mon Sep 17 00:00:00 2001 From: Akustik Date: Wed, 29 Jul 2020 23:42:03 +0300 Subject: [PATCH 3/3] Reverted to java 8, spring 4.3, slf4j 1.7.25 --- .gitignore | 1 - .travis.yml | 3 +- build/setup_mysql.sh | 2 +- build/setup_postgres.sh | 2 +- pom.xml | 12 ++-- sql/mysql/create-test-user.sql | 2 +- sql/mysql/schema-mysql-1.sql | 1 - sql/oracle/create-test-user.sql | 2 +- sql/oracle/schema-oracle-1.sql | 2 +- sql/postgres/schema-postgres-1.sql | 1 - src/main/java/org/morejdbc/DBUtils.java | 15 +++-- src/main/java/org/morejdbc/JdbcCall.java | 3 +- .../java/org/morejdbc/H2JdbcCallTest.java | 10 +-- .../java/org/morejdbc/MockitoCallTest.java | 36 +++++------ .../java/org/morejdbc/MysqlJdbcCallTest.java | 7 +- .../java/org/morejdbc/OracleJdbcCallTest.java | 31 +++++---- .../org/morejdbc/OracleNamedJdbcCallTest.java | 64 ++++++++++--------- .../org/morejdbc/PostgresJdbcCallTest.java | 21 +++--- src/test/java/org/morejdbc/TestUtils.java | 21 +++--- 19 files changed, 124 insertions(+), 112 deletions(-) diff --git a/.gitignore b/.gitignore index 7ebade6..5580fea 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,3 @@ sql/oracle/init/.cache pom.xml.releaseBackup pom.xml.versionsBackup release.properties - diff --git a/.travis.yml b/.travis.yml index 296e0c0..4ab956d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,6 @@ sudo: required language: java +jdk: openjdk8 services: - docker - postgresql @@ -10,4 +11,4 @@ before_install: - ./build/setup_oracle.sh cache: directories: - - $HOME/.m2 \ No newline at end of file + - $HOME/.m2 diff --git a/build/setup_mysql.sh b/build/setup_mysql.sh index 1fbacd9..314c2e0 100755 --- a/build/setup_mysql.sh +++ b/build/setup_mysql.sh @@ -1,4 +1,4 @@ #!/usr/bin/env bash mysql < sql/mysql/create-test-user.sql -mysql -u test --password=test test < sql/mysql/schema-mysql-1.sql \ No newline at end of file +mysql -u test --password=test test < sql/mysql/schema-mysql-1.sql diff --git a/build/setup_postgres.sh b/build/setup_postgres.sh index 6861833..3c3bd51 100755 --- a/build/setup_postgres.sh +++ b/build/setup_postgres.sh @@ -1,4 +1,4 @@ #!/usr/bin/env bash psql -U postgres -f sql/postgres/create-test-user.sql -psql -U test -f sql/postgres/schema-postgres-1.sql \ No newline at end of file +psql -U test -f sql/postgres/schema-postgres-1.sql diff --git a/pom.xml b/pom.xml index dd7ac16..8c9b6b5 100644 --- a/pom.xml +++ b/pom.xml @@ -16,11 +16,11 @@ UTF-8 - 1.11 - 1.11 + 1.8 + 1.8 - 1.7.30 - 5.2.7.RELEASE + 1.7.25 + 4.3.26.RELEASE @@ -159,8 +159,8 @@ maven-compiler-plugin 3.8.1 - 11 - 11 + 8 + 8 diff --git a/sql/mysql/create-test-user.sql b/sql/mysql/create-test-user.sql index d513573..094ab38 100644 --- a/sql/mysql/create-test-user.sql +++ b/sql/mysql/create-test-user.sql @@ -1,4 +1,4 @@ create database test; create user 'test'@'%' identified by 'test'; grant all privileges on *.* to 'test'@'%'; -flush privileges; \ No newline at end of file +flush privileges; diff --git a/sql/mysql/schema-mysql-1.sql b/sql/mysql/schema-mysql-1.sql index a0d2823..6f5b8eb 100644 --- a/sql/mysql/schema-mysql-1.sql +++ b/sql/mysql/schema-mysql-1.sql @@ -12,4 +12,3 @@ end; -- changeset seregamorph:FEA-1-create-schema-2 splitStatements:false create function get_concat(s1 varchar(50), s2 varchar(50)) returns varchar(100) return concat(s1, s2); - diff --git a/sql/oracle/create-test-user.sql b/sql/oracle/create-test-user.sql index 675912f..7f2ce5e 100644 --- a/sql/oracle/create-test-user.sql +++ b/sql/oracle/create-test-user.sql @@ -1,2 +1,2 @@ create user test identified by test default tablespace users quota 100m on users; -grant connect, resource to test; \ No newline at end of file +grant connect, resource to test; diff --git a/sql/oracle/schema-oracle-1.sql b/sql/oracle/schema-oracle-1.sql index 753631e..8ce8067 100644 --- a/sql/oracle/schema-oracle-1.sql +++ b/sql/oracle/schema-oracle-1.sql @@ -107,4 +107,4 @@ as pio_sum := p_number1 + p_number2 + pio_sum; end; end; -/ \ No newline at end of file +/ diff --git a/sql/postgres/schema-postgres-1.sql b/sql/postgres/schema-postgres-1.sql index 9d53b94..93a7061 100644 --- a/sql/postgres/schema-postgres-1.sql +++ b/sql/postgres/schema-postgres-1.sql @@ -25,4 +25,3 @@ begin return mycurs; end; $$ language plpgsql; - diff --git a/src/main/java/org/morejdbc/DBUtils.java b/src/main/java/org/morejdbc/DBUtils.java index 8fe5358..7f345db 100644 --- a/src/main/java/org/morejdbc/DBUtils.java +++ b/src/main/java/org/morejdbc/DBUtils.java @@ -2,6 +2,7 @@ import org.jetbrains.annotations.Nullable; +import java.sql.Blob; import java.sql.CallableStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -10,7 +11,7 @@ public class DBUtils { @Nullable public static Integer getIntOrNull(ResultSet rs, String columnName) throws SQLException { - var value = rs.getInt(columnName); + int value = rs.getInt(columnName); if (rs.wasNull()) { return null; } @@ -19,7 +20,7 @@ public static Integer getIntOrNull(ResultSet rs, String columnName) throws SQLEx @Nullable public static Integer getIntOrNull(CallableStatement cs, int idx) throws SQLException { - var value = cs.getInt(idx); + int value = cs.getInt(idx); if (cs.wasNull()) { return null; } @@ -28,7 +29,7 @@ public static Integer getIntOrNull(CallableStatement cs, int idx) throws SQLExce @Nullable public static Long getLongOrNull(ResultSet rs, String columnName) throws SQLException { - var value = rs.getLong(columnName); + long value = rs.getLong(columnName); if (rs.wasNull()) { return null; } @@ -37,7 +38,7 @@ public static Long getLongOrNull(ResultSet rs, String columnName) throws SQLExce @Nullable public static Long getLongOrNull(CallableStatement cs, int idx) throws SQLException { - var value = cs.getLong(idx); + long value = cs.getLong(idx); if (cs.wasNull()) { return null; } @@ -46,7 +47,7 @@ public static Long getLongOrNull(CallableStatement cs, int idx) throws SQLExcept @Nullable public static Double getDoubleOrNull(ResultSet rs, String columnName) throws SQLException { - var value = rs.getDouble(columnName); + double value = rs.getDouble(columnName); if (rs.wasNull()) { return null; } @@ -55,7 +56,7 @@ public static Double getDoubleOrNull(ResultSet rs, String columnName) throws SQL @Nullable public static byte[] getBlobBytes(ResultSet rs, String columnName) throws SQLException { - var blob = rs.getBlob(columnName); + Blob blob = rs.getBlob(columnName); try { return blob == null || blob.length() == 0 ? null : blob.getBytes(1, (int) blob.length()); } finally { @@ -67,7 +68,7 @@ public static byte[] getBlobBytes(ResultSet rs, String columnName) throws SQLExc @Nullable public static byte[] getBlobBytes(CallableStatement cs, int idx) throws SQLException { - var blob = cs.getBlob(idx); + Blob blob = cs.getBlob(idx); try { return blob == null || blob.length() == 0 ? null : blob.getBytes(1, (int) blob.length()); } finally { diff --git a/src/main/java/org/morejdbc/JdbcCall.java b/src/main/java/org/morejdbc/JdbcCall.java index 7ddd711..8a05d13 100644 --- a/src/main/java/org/morejdbc/JdbcCall.java +++ b/src/main/java/org/morejdbc/JdbcCall.java @@ -7,6 +7,7 @@ import org.springframework.jdbc.core.SqlProvider; import java.math.BigDecimal; +import java.sql.CallableStatement; import java.sql.Connection; import java.sql.SQLException; import java.sql.Timestamp; @@ -92,7 +93,7 @@ public Out out(SqlType type) { public Void doInConnection(Connection conn) throws SQLException, DataAccessException { InOut[] parameters = getParameters(); - try (var cs = conn.prepareCall(sql)) { + try (CallableStatement cs = conn.prepareCall(sql)) { for (int i = 0; i < parameters.length; i++) { InOut parameter = parameters[i]; parameter.beforeExecute(cs, i + 1); diff --git a/src/test/java/org/morejdbc/H2JdbcCallTest.java b/src/test/java/org/morejdbc/H2JdbcCallTest.java index b409c71..9ec9c34 100644 --- a/src/test/java/org/morejdbc/H2JdbcCallTest.java +++ b/src/test/java/org/morejdbc/H2JdbcCallTest.java @@ -6,10 +6,12 @@ import org.springframework.jdbc.core.JdbcTemplate; import javax.sql.DataSource; +import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Types; +import java.util.Properties; import java.util.concurrent.atomic.AtomicInteger; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -24,7 +26,7 @@ public class H2JdbcCallTest { @BeforeEach public void before() throws SQLException { - var props = TestUtils.propertiesFromString(TestUtils.readString("h2_test.properties")); + Properties props = TestUtils.propertiesFromString(TestUtils.readString("h2_test.properties")); this.connection = DriverManager.getConnection(props.getProperty("url"), props); DataSource dataSource = TestUtils.smartDataSource(this.connection); this.jdbc = new JdbcTemplate(dataSource); @@ -40,7 +42,7 @@ public void after() throws SQLException { @Test public void testPureJdbc() throws SQLException { - var call = connection.prepareCall("{? = call mult(?, ?)}"); + CallableStatement call = connection.prepareCall("{? = call mult(?, ?)}"); call.registerOutParameter(1, Types.INTEGER); call.setInt(2, 2); call.setInt(3, 3); @@ -50,7 +52,7 @@ public void testPureJdbc() throws SQLException { @Test public void testMultOut() { - var out = Out.of(INTEGER); + Out out = Out.of(INTEGER); jdbc.execute(callSql("{? = call mult(?, ?)}") .out(out) @@ -62,7 +64,7 @@ public void testMultOut() { @Test public void testMultConsumer() { - var out = new AtomicInteger(); + AtomicInteger out = new AtomicInteger(); jdbc.execute(callSql("{? = call mult(?, ?)}") .out(INTEGER, out::set) diff --git a/src/test/java/org/morejdbc/MockitoCallTest.java b/src/test/java/org/morejdbc/MockitoCallTest.java index ae4a40d..8c6379d 100644 --- a/src/test/java/org/morejdbc/MockitoCallTest.java +++ b/src/test/java/org/morejdbc/MockitoCallTest.java @@ -21,63 +21,63 @@ public class MockitoCallTest { @Test public void testCallSqlMock() { - var jdbc = mock(JdbcTemplate.class); - var sum = MockOut.of(INTEGER); - var mlt = MockOut.of(INTEGER); + JdbcTemplate jdbc = mock(JdbcTemplate.class); + MockOut sum = MockOut.of(INTEGER); + MockOut mlt = MockOut.of(INTEGER); when(jdbc.execute(callSql("{call test_math(?, ?, ?, ?)}").in(10).in(20).out(sum).out(mlt))).then(invocation -> { sum.setTo(invocation.getArguments()[0], 30); mlt.setTo(invocation.getArguments()[0], 200); return null; }); - var result = serviceCallSql(jdbc, 10, 20); + Result result = serviceCallSql(jdbc, 10, 20); assertEquals(30, result.sum); assertEquals(200, result.mlt); } @Test public void testCallNamedMock() { - var jdbc = mock(JdbcTemplate.class); - var sum = MockOut.of(INTEGER); - var mlt = MockOut.of(BIGINT); + JdbcTemplate jdbc = mock(JdbcTemplate.class); + MockOut sum = MockOut.of(INTEGER); + MockOut mlt = MockOut.of(BIGINT); when(jdbc.execute(call("test_math").in("val1", 10).in("val2", 20).out("out_sum", sum).out("out_mlt", mlt))).then(invocation -> { sum.setTo(invocation.getArguments()[0], 30); mlt.setTo(invocation.getArguments()[0], 200L); return null; }); - var result = serviceCallNamed(jdbc, 10, 20); + Result result = serviceCallNamed(jdbc, 10, 20); assertEquals(30, result.sum); assertEquals(200, result.mlt); } @Test public void testCallNamedFunctionMock() { - var jdbc = mock(JdbcTemplate.class); + JdbcTemplate jdbc = mock(JdbcTemplate.class); when(jdbc.execute(call("get_concat", VARCHAR).in("s2", "def").in("s1", 4))).thenReturn("4def"); - var result = serviceCallNamedFunction(jdbc, 4, "def"); + String result = serviceCallNamedFunction(jdbc, 4, "def"); assertEquals("4def", result); } @Test public void testRefCursorOutParam() { - var jdbc = mock(JdbcTemplate.class); - var out = MockOut.of(cursor((rs, rowNum) -> immutableEntry("key", "value"))); + JdbcTemplate jdbc = mock(JdbcTemplate.class); + MockOut>> out = MockOut.of(cursor((rs, rowNum) -> immutableEntry("key", "value"))); when(jdbc.execute(call("proc_extras_tab").in("extra_string", "1=value1;2=value2;6=value6;").out("v_cur", out))).then(invocation -> { out.setTo(invocation.getArguments()[0], Arrays.asList(immutableEntry("1", "value1"), immutableEntry("2", "value2"), immutableEntry("6", "value6"))); return null; }); - var extras = serviceTestRefCursorOutParam(jdbc, "1=value1;2=value2;6=value6;"); + List> extras = serviceTestRefCursorOutParam(jdbc, "1=value1;2=value2;6=value6;"); assertEquals(Arrays.asList(immutableEntry("1", "value1"), immutableEntry("2", "value2"), immutableEntry("6", "value6")), extras); } private static List> serviceTestRefCursorOutParam(JdbcTemplate jdbc, String extra) { - var outExtras = Out.of(cursor((rs, rowNum) -> immutableEntry(rs.getString("id"), rs.getString("value")))); + Out>> outExtras = Out.of(cursor((rs, rowNum) -> immutableEntry(rs.getString("id"), rs.getString("value")))); jdbc.execute(call("proc_extras_tab").in("extra_string", extra).out("v_cur", outExtras)); return outExtras.get(); } private static Result serviceCallSql(JdbcTemplate jdbc, int val1, int val2) { - var sum = Out.of(INTEGER); - var mlt = Out.of(INTEGER); + Out sum = Out.of(INTEGER); + Out mlt = Out.of(INTEGER); jdbc.execute(callSql("{call test_math(?, ?, ?, ?)}") .in(val1) @@ -89,8 +89,8 @@ private static Result serviceCallSql(JdbcTemplate jdbc, int val1, int val2) { } private static Result serviceCallNamed(JdbcTemplate jdbc, int val1, int val2) { - var sum = new AtomicReference(); - var mlt = new AtomicReference(); + AtomicReference sum = new AtomicReference<>(); + AtomicReference mlt = new AtomicReference<>(); jdbc.execute(call("test_math") .in("val1", val1) diff --git a/src/test/java/org/morejdbc/MysqlJdbcCallTest.java b/src/test/java/org/morejdbc/MysqlJdbcCallTest.java index 1620bdf..d00770d 100644 --- a/src/test/java/org/morejdbc/MysqlJdbcCallTest.java +++ b/src/test/java/org/morejdbc/MysqlJdbcCallTest.java @@ -9,6 +9,7 @@ import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; +import java.util.Properties; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.morejdbc.JdbcCall.callSql; @@ -24,7 +25,7 @@ public class MysqlJdbcCallTest { @BeforeEach public void before() throws SQLException { - var props = TestUtils.propertiesFromString(TestUtils.readString("mysql_test.properties")); + Properties props = TestUtils.propertiesFromString(TestUtils.readString("mysql_test.properties")); this.connection = DriverManager.getConnection(props.getProperty("url"), props); DataSource dataSource = TestUtils.smartDataSource(this.connection); this.jdbc = new JdbcTemplate(dataSource); @@ -39,8 +40,8 @@ public void after() throws SQLException { @Test public void testMath() { - var sum = Out.of(INTEGER); - var mlt = Out.of(INTEGER); + Out sum = Out.of(INTEGER); + Out mlt = Out.of(INTEGER); jdbc.execute(callSql("{call test_math(?, ?, ?, ?)}") .in(10) diff --git a/src/test/java/org/morejdbc/OracleJdbcCallTest.java b/src/test/java/org/morejdbc/OracleJdbcCallTest.java index 8e1c5f7..50026be 100644 --- a/src/test/java/org/morejdbc/OracleJdbcCallTest.java +++ b/src/test/java/org/morejdbc/OracleJdbcCallTest.java @@ -13,7 +13,10 @@ import java.sql.DriverManager; import java.sql.SQLException; import java.util.Arrays; +import java.util.List; import java.util.Locale; +import java.util.Map; +import java.util.Properties; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.atomic.AtomicReference; @@ -38,8 +41,8 @@ public class OracleJdbcCallTest { @BeforeEach public void before() throws SQLException { - var props = TestUtils.propertiesFromString(TestUtils.readString("oracle_test.properties")); - var def = Locale.getDefault(); + Properties props = TestUtils.propertiesFromString(TestUtils.readString("oracle_test.properties")); + Locale def = Locale.getDefault(); try { // workaround for XE with russian locale Locale.setDefault(Locale.ENGLISH); @@ -59,8 +62,8 @@ public void after() throws SQLException { @Test public void testCall1() { - var sum = Out.of(INTEGER); - var mlt = Out.of(INTEGER); + Out sum = Out.of(INTEGER); + Out mlt = Out.of(INTEGER); jdbc.execute(callSql("{call test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers(?, ?, ?, ?)}") .in(8) @@ -74,8 +77,8 @@ public void testCall1() { @Test public void testCallBadArgs() { - var sum = Out.of(INTEGER); - var mlt = Out.of(INTEGER); + Out sum = Out.of(INTEGER); + Out mlt = Out.of(INTEGER); try { jdbc.execute(callSql("{call test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers(?, ?, ?, ?, ?)}") .in(0) @@ -95,9 +98,9 @@ public void testCallBadArgs() { public void testInsertReturning() { // table_with_identity_pk (id number(9) identity primary key, value varchar2(20 char)); // checks charset - var valueIn = "тест" + System.currentTimeMillis(); - var idOut = Out.of(BIGINT); - var valueOut = new AtomicReference(); + String valueIn = "тест" + System.currentTimeMillis(); + Out idOut = Out.of(BIGINT); + AtomicReference valueOut = new AtomicReference<>(); jdbc.execute(callSql("begin insert into table_with_identity_pk (value) values (?) " + "returning id, value into ?, ?; end;") .in(valueIn) @@ -112,7 +115,7 @@ public void testInsertReturning() { @Test public void testCallFuncResultSet() { - var extras = Out.of(cursor((row, rowNum) -> immutableEntry(row.getString("id"), row.getString("value")))); + Out>> extras = Out.of(cursor((row, rowNum) -> immutableEntry(row.getString("id"), row.getString("value")))); jdbc.execute(callSql("{? = call test_more_jdbc_pkg.get_cursor_from_key_value_as_string(?)}") .out(extras) @@ -124,18 +127,18 @@ public void testCallFuncResultSet() { @Test public void testLongBlobConcat() { // > 4000 bytes - var blob1 = new byte[4096]; - var blob2 = new byte[4096]; + byte[] blob1 = new byte[4096]; + byte[] blob2 = new byte[4096]; ThreadLocalRandom.current().nextBytes(blob1); ThreadLocalRandom.current().nextBytes(blob2); - var result = Out.of(BLOB); + Out result = Out.of(BLOB); jdbc.execute(callSql("{? = call test_more_jdbc_pkg.get_two_blobs_concatenated(?, ?)}") .out(result) .in(blob1) .in(blob2)); - var expected = TestUtils.concat(blob1, blob2); + byte[] expected = TestUtils.concat(blob1, blob2); assertArrayEquals(expected, result.get()); } } diff --git a/src/test/java/org/morejdbc/OracleNamedJdbcCallTest.java b/src/test/java/org/morejdbc/OracleNamedJdbcCallTest.java index ce19c57..9decba9 100644 --- a/src/test/java/org/morejdbc/OracleNamedJdbcCallTest.java +++ b/src/test/java/org/morejdbc/OracleNamedJdbcCallTest.java @@ -14,8 +14,10 @@ import java.sql.DriverManager; import java.sql.SQLException; import java.util.Arrays; +import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Properties; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.atomic.AtomicReference; @@ -43,8 +45,8 @@ public class OracleNamedJdbcCallTest { @BeforeEach public void before() throws SQLException { - var props = TestUtils.propertiesFromString(TestUtils.readString("oracle_test.properties")); - var def = Locale.getDefault(); + Properties props = TestUtils.propertiesFromString(TestUtils.readString("oracle_test.properties")); + Locale def = Locale.getDefault(); try { // workaround for XE with russian locale Locale.setDefault(Locale.ENGLISH); @@ -64,8 +66,8 @@ public void after() throws SQLException { @Test public void testNamedCall1() { - var sum = Out.of(INTEGER); - var mlt = Out.of(BIGINT); + Out sum = Out.of(INTEGER); + Out mlt = Out.of(BIGINT); jdbc.execute(call("test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers") .in("p_number1", 1) @@ -79,8 +81,8 @@ public void testNamedCall1() { @Test public void testNamedCall1Consumer() { - var sum = new AtomicReference(); - var mlt = new AtomicReference(); + AtomicReference sum = new AtomicReference<>(); + AtomicReference mlt = new AtomicReference<>(); jdbc.execute(call("test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers") .in("p_number1", 1) @@ -94,8 +96,8 @@ public void testNamedCall1Consumer() { @Test public void testNamedCall2() { - var sum = Out.of(INTEGER); - var mlt = Out.of(INTEGER); + Out sum = Out.of(INTEGER); + Out mlt = Out.of(INTEGER); jdbc.execute(call("test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers") .out("po_mlt", mlt) @@ -109,11 +111,11 @@ public void testNamedCall2() { @Test public void testNamedCall3() { - var call = call("test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers") + NamedJdbcCall call = call("test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers") .in("p_number1", 1) .in("p_number2", 2); - var sum = call.out("po_sum", INTEGER); - var mlt = call.out("po_mlt", INTEGER); + Out sum = call.out("po_sum", INTEGER); + Out mlt = call.out("po_mlt", INTEGER); jdbc.execute(call); assertEquals(Integer.valueOf(3), sum.get()); @@ -122,7 +124,7 @@ public void testNamedCall3() { @Test public void testNamedCallFunc1() { - var result = jdbc.execute(call("test_more_jdbc_pkg.get_concat_of_two_strings", VARCHAR) + String result = jdbc.execute(call("test_more_jdbc_pkg.get_concat_of_two_strings", VARCHAR) .in("p_string1", "abc") .in("p_string2", (String) null)); @@ -132,7 +134,7 @@ public void testNamedCallFunc1() { @Test public void testNamedCallFunc2() { // reordered p_string1, p_string2 - var result = jdbc.execute(call("test_more_jdbc_pkg.get_concat_of_two_strings", VARCHAR) + String result = jdbc.execute(call("test_more_jdbc_pkg.get_concat_of_two_strings", VARCHAR) .in("p_string2", new StringBuilder("XYZ")) .in("p_string1", "abc")); @@ -141,7 +143,7 @@ public void testNamedCallFunc2() { @Test public void testNamedCallFunc3() { - var result = jdbc.execute(call("test_more_jdbc_pkg.get_concat_of_two_strings", VARCHAR) + String result = jdbc.execute(call("test_more_jdbc_pkg.get_concat_of_two_strings", VARCHAR) .in("p_string2", "def") .in("p_string1", 4)); @@ -151,7 +153,7 @@ public void testNamedCallFunc3() { @Test public void testNamedCallFunc4() { // pass null-string with unknown type - var result = jdbc.execute(call("test_more_jdbc_pkg.get_concat_of_two_strings", VARCHAR) + String result = jdbc.execute(call("test_more_jdbc_pkg.get_concat_of_two_strings", VARCHAR) .in("p_string1", "abc") .in("p_string2", null, UNKNOWN)); @@ -162,7 +164,7 @@ public void testNamedCallFunc4() { public void testNamedCallFuncResultSet() { RowMapper> mapper = (rs, rowNum) -> immutableEntry(rs.getString("id"), rs.getString("value")); - var extras = jdbc.execute(call("test_more_jdbc_pkg.get_cursor_from_key_value_as_string", cursor(mapper)) + List> extras = jdbc.execute(call("test_more_jdbc_pkg.get_cursor_from_key_value_as_string", cursor(mapper)) .in("p_key_value_string", "1=value1;2=value2;6=value6;")); assertEquals(Arrays.asList(immutableEntry("1", "value1"), immutableEntry("2", "value2"), immutableEntry("6", "value6")), extras); @@ -171,7 +173,7 @@ public void testNamedCallFuncResultSet() { @Test public void testRefCursorOutParam() { RowMapper> mapper = (rs, rowNum) -> immutableEntry(rs.getString("id"), rs.getString("value")); - var outExtras = Out.of(cursor(mapper)); + Out>> outExtras = Out.of(cursor(mapper)); jdbc.execute(call("test_more_jdbc_pkg.get_cursor_from_key_value_as_string") .in("p_key_value_string", "1=value1;2=value2;6=value6;") @@ -182,7 +184,7 @@ public void testRefCursorOutParam() { @Test public void testNamedCallInOut1() { - var sum = Out.of(DECIMAL); + Out sum = Out.of(DECIMAL); jdbc.execute(call("test_more_jdbc_pkg.calc_sum_of_two_numbers_with_in_out_parameter") .in("p_number1", 1) @@ -194,7 +196,7 @@ public void testNamedCallInOut1() { @Test public void testNamedCallInOut2() { - var sum = Out.of(INTEGER); + Out sum = Out.of(INTEGER); jdbc.execute(call("test_more_jdbc_pkg.calc_sum_of_two_numbers_with_in_out_parameter") .in("p_number1", 1) @@ -206,7 +208,7 @@ public void testNamedCallInOut2() { @Test public void testNamedCallInOut2Consumer() { - var sum = new AtomicReference(); + AtomicReference sum = new AtomicReference<>(); jdbc.execute(call("test_more_jdbc_pkg.calc_sum_of_two_numbers_with_in_out_parameter") .in("p_number1", 1) @@ -218,8 +220,8 @@ public void testNamedCallInOut2Consumer() { @Test public void testPackageCallable4arg() { - var sum = Out.of(BIGINT); - var mlt = Out.of(INTEGER); + Out sum = Out.of(BIGINT); + Out mlt = Out.of(INTEGER); jdbc.execute(call("test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers") .out("po_sum", sum) @@ -233,7 +235,7 @@ public void testPackageCallable4arg() { @Test public void testPackageCallable3arg() { - var sum = Out.of(INTEGER); + Out sum = Out.of(INTEGER); jdbc.execute(call("test_more_jdbc_pkg.calc_sum_and_multiply_of_two_numbers") .in("p_number1", 1) @@ -247,32 +249,32 @@ public void testPackageCallable3arg() { @Test public void testLongBinaryConcat() { // > 4000 bytes - var blob1 = new byte[4096]; - var blob2 = new byte[4096]; + byte[] blob1 = new byte[4096]; + byte[] blob2 = new byte[4096]; ThreadLocalRandom.current().nextBytes(blob1); ThreadLocalRandom.current().nextBytes(blob2); - var result = jdbc.execute(call("test_more_jdbc_pkg.get_two_blobs_concatenated", BINARY) + byte[] result = jdbc.execute(call("test_more_jdbc_pkg.get_two_blobs_concatenated", BINARY) .in("p_blob1", blob1, BINARY) .in("p_blob2", blob2, BINARY)); - var expected = TestUtils.concat(blob1, blob2); + byte[] expected = TestUtils.concat(blob1, blob2); assertArrayEquals(expected, result); } @Test public void testLongBlobConcat() { // > 4000 bytes - var blob1 = new byte[4096]; - var blob2 = new byte[4096]; + byte[] blob1 = new byte[4096]; + byte[] blob2 = new byte[4096]; ThreadLocalRandom.current().nextBytes(blob1); ThreadLocalRandom.current().nextBytes(blob2); - var result = jdbc.execute(call("test_more_jdbc_pkg.get_two_blobs_concatenated", BLOB) + byte[] result = jdbc.execute(call("test_more_jdbc_pkg.get_two_blobs_concatenated", BLOB) .in("p_blob1", blob1) .in("p_blob2", blob2)); - var expected = TestUtils.concat(blob1, blob2); + byte[] expected = TestUtils.concat(blob1, blob2); assertArrayEquals(expected, result); } } diff --git a/src/test/java/org/morejdbc/PostgresJdbcCallTest.java b/src/test/java/org/morejdbc/PostgresJdbcCallTest.java index 70bd196..e3fb236 100644 --- a/src/test/java/org/morejdbc/PostgresJdbcCallTest.java +++ b/src/test/java/org/morejdbc/PostgresJdbcCallTest.java @@ -11,10 +11,13 @@ import javax.sql.DataSource; import java.math.BigDecimal; +import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Arrays; +import java.util.List; +import java.util.Properties; import java.util.concurrent.atomic.AtomicReference; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -35,7 +38,7 @@ public class PostgresJdbcCallTest { @BeforeEach public void before() throws SQLException { - var props = TestUtils.propertiesFromString(TestUtils.readString("psql_test.properties")); + Properties props = TestUtils.propertiesFromString(TestUtils.readString("psql_test.properties")); this.connection = DriverManager.getConnection(props.getProperty("url"), props); DataSource dataSource = TestUtils.smartDataSource(this.connection); PlatformTransactionManager transactionManager = new DataSourceTransactionManager(dataSource); @@ -52,14 +55,14 @@ public void after() throws SQLException { @Test public void testSelect() { - var list = jdbc.query("SELECT hi, lo FROM hi_lo(?, ?, ?)", (row, rowNum) -> { - var hl = new HiLo(); + List list = jdbc.query("SELECT hi, lo FROM hi_lo(?, ?, ?)", (row, rowNum) -> { + HiLo hl = new HiLo(); hl.hi = row.getInt("hi"); hl.lo = row.getInt("lo"); return hl; }, 10, 20, 30); assertEquals(1, list.size()); - var hl = list.get(0); + HiLo hl = list.get(0); assertEquals(30, hl.hi); assertEquals(10, hl.lo); } @@ -67,7 +70,7 @@ public void testSelect() { @Test public void testCallExecute() { jdbc.execute(con -> { - var cs = con.prepareCall("{call hi_lo(?, ?, ?, ?, ?)}"); + CallableStatement cs = con.prepareCall("{call hi_lo(?, ?, ?, ?, ?)}"); cs.setInt(1, 10); cs.setInt(2, 20); cs.setInt(3, 30); @@ -84,8 +87,8 @@ public void testCallExecute() { @Test public void testCall() { - var hi = new AtomicReference(); - var lo = new AtomicReference(); + AtomicReference hi = new AtomicReference<>(); + AtomicReference lo = new AtomicReference<>(); jdbc.execute(callSql("{call hi_lo(?, ?, ?, ?, ?)}") .in(10) @@ -101,8 +104,8 @@ public void testCall() { @Test public void testRefcursor() { // refcursor out works only in transaction - var values = transactionTemplate.execute(transaction -> { - var outValues = Out.of(cursor((row, rowNum) -> row.getInt(1))); + List values = transactionTemplate.execute(transaction -> { + Out> outValues = Out.of(cursor((row, rowNum) -> row.getInt(1))); jdbc.execute(callSql("{ ? = call refcursorfunc() }").out(outValues)); diff --git a/src/test/java/org/morejdbc/TestUtils.java b/src/test/java/org/morejdbc/TestUtils.java index 0f28b5b..465ba98 100644 --- a/src/test/java/org/morejdbc/TestUtils.java +++ b/src/test/java/org/morejdbc/TestUtils.java @@ -5,6 +5,7 @@ import org.springframework.jdbc.datasource.SmartDataSource; import java.io.*; +import java.net.URL; import java.sql.Connection; import java.util.AbstractMap; import java.util.Map; @@ -15,7 +16,7 @@ class TestUtils { static Properties propertiesFromString(String str) { - var properties = new Properties(); + Properties properties = new Properties(); if (str != null && !str.isEmpty()) { try { properties.load(new StringReader(str)); @@ -40,13 +41,13 @@ static void closeQuietly(InputStream in) { } static byte[] concat(byte[]... arrays) { - var length = 0; - for (var array : arrays) { + int length = 0; + for (byte[] array : arrays) { length += array.length; } - var result = new byte[length]; - var pos = 0; - for (var array : arrays) { + byte[] result = new byte[length]; + int pos = 0; + for (byte[] array : arrays) { System.arraycopy(array, 0, result, pos, array.length); pos += array.length; } @@ -54,7 +55,7 @@ static byte[] concat(byte[]... arrays) { } static byte[] readBytes(ClassLoader classLoader, String resource) { - var url = classLoader.getResource(resource); + URL url = classLoader.getResource(resource); if (url == null) { throw new IllegalStateException("Missing resource [" + resource + "]"); } @@ -77,8 +78,8 @@ static String readString(String resource) { } private static byte[] toByteArray(InputStream input) throws IOException { - var output = new ByteArrayOutputStream(); - var buffer = new byte[8192]; + ByteArrayOutputStream output = new ByteArrayOutputStream(); + byte[] buffer = new byte[8192]; int n; while ((n = input.read(buffer)) != -1) { output.write(buffer, 0, n); @@ -87,7 +88,7 @@ private static byte[] toByteArray(InputStream input) throws IOException { } static JdbcTemplate jdbc(Connection connection) { - var ds = smartDataSource(connection); + SmartDataSource ds = smartDataSource(connection); return new JdbcTemplate(ds); }