From 4016c0d4619e479b0e18758642b622b3c89b9405 Mon Sep 17 00:00:00 2001 From: kaneeldias Date: Thu, 19 Aug 2021 12:01:41 +0530 Subject: [PATCH 01/22] Add workflows --- .github/workflows/ci.yml | 27 +++++++++++++++++++++++++++ .github/workflows/release.yml | 29 +++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..25a854b --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,27 @@ +name: CI + +on: + push: + branches: + - main + workflow_dispatch: + pull_request: + branches: + - main + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Ballerina Build + uses: ballerina-platform/ballerina-action/@slbeta1 + with: + args: + build -c + env: + DB_HOST: ${{ secrets.DB_HOST }} + DB_USER: ${{ secrets.DB_USER }} + DB_PASSWORD: ${{ secrets.DB_PASSWORD }} \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..a5a9c3e --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,29 @@ +name: Deployment + +on: + release: + types: [published] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Ballerina Build + uses: ballerina-platform/ballerina-action/@slbeta1 + with: + args: + build -c --skip-tests + env: + DB_HOST: ${{ secrets.DB_HOST }} + DB_USER: ${{ secrets.DB_USER }} + DB_PASSWORD: ${{ secrets.DB_PASSWORD }} + - name: Ballerina Push + uses: ballerina-platform/ballerina-action/@slbeta1 + with: + args: + push + env: + BALLERINA_CENTRAL_ACCESS_TOKEN: ${{ secrets.BALLERINA_CENTRAL_ACCESS_TOKEN }} \ No newline at end of file From a131ed1504cc6c5170ce5c5199792bc3149ce923 Mon Sep 17 00:00:00 2001 From: kaneeldias Date: Thu, 19 Aug 2021 12:02:50 +0530 Subject: [PATCH 02/22] Update gitignore --- .gitignore | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/.gitignore b/.gitignore index a1c2a23..179819f 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,25 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + +# Target folder of ballerina project +/target/* + +# resources folder +/resources/* + +# idea files +*.idea +*.iml + +# .ballerina files +*.ballerina + +# Ignore Gradle project-specific cache directory +.gradle + +# Ignore Gradle build output directory +build + +# +*.log From f7a573c5925dbadc4c00eb1ae2bc97d5ee4f899b Mon Sep 17 00:00:00 2001 From: kaneeldias Date: Thu, 19 Aug 2021 12:08:22 +0530 Subject: [PATCH 03/22] Add Package.md --- Package.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Package.md diff --git a/Package.md b/Package.md new file mode 100644 index 0000000..54a3865 --- /dev/null +++ b/Package.md @@ -0,0 +1,18 @@ +## Package overview + +This Package bundles the latest MSSQL driver so that mssql connector can be used in ballerina projects easily. + +## Compatibility + +Ballerina Language Version **Swan Lake Beta 2** +MSSQL Driver Version **9.4.0** + +## Usage + +To add the MySQL driver dependency the project simply import the module as below, + +```ballerina +import ballerina/sql; +import ballerinax/mssql; +import ballerinax/mssql.driver as _; +``` \ No newline at end of file From b5c7966935bc98ab1dc22dd0e000990cfb3328db Mon Sep 17 00:00:00 2001 From: kaneeldias Date: Thu, 19 Aug 2021 12:08:57 +0530 Subject: [PATCH 04/22] Add client.bal --- client.bal | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 client.bal diff --git a/client.bal b/client.bal new file mode 100644 index 0000000..68a42f4 --- /dev/null +++ b/client.bal @@ -0,0 +1,15 @@ +// Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +// +// WSO2 Inc. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. From f5d8fae902451f32bb3e1b0f9722c70c62da032a Mon Sep 17 00:00:00 2001 From: kaneeldias Date: Thu, 19 Aug 2021 12:12:16 +0530 Subject: [PATCH 05/22] Add Ballerina.toml --- Ballerina.toml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Ballerina.toml diff --git a/Ballerina.toml b/Ballerina.toml new file mode 100644 index 0000000..270a074 --- /dev/null +++ b/Ballerina.toml @@ -0,0 +1,16 @@ +[package] +org="ballerinax" +name="mysql.driver" +version="1.0.0" +authors=["Ballerina"] +keywords=["MSSQL", "Sql Server"] +repository="https://github.com/ballerina-platform/module-ballerinax-mssql.driver" +license=["Apache-2.0"] + +[[platform.java11.dependency]] +groupId = "com.microsoft.sqlserver" +artifactId = "mssql-jdbc" +version = "9.4.0.jre11" + +[build-options] +observabilityIncluded = true From 143f1f766be832deb05d7d3f1687e16e3a90b54d Mon Sep 17 00:00:00 2001 From: kaneeldias Date: Thu, 19 Aug 2021 12:18:14 +0530 Subject: [PATCH 06/22] Update MSSQL driver version --- Package.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.md b/Package.md index 54a3865..aecaaf1 100644 --- a/Package.md +++ b/Package.md @@ -5,7 +5,7 @@ This Package bundles the latest MSSQL driver so that mssql connector can be used ## Compatibility Ballerina Language Version **Swan Lake Beta 2** -MSSQL Driver Version **9.4.0** +MSSQL Driver Version **9.4.0.jre11** ## Usage From c574b2464f6a6df81640f0d492d958c291a0cd02 Mon Sep 17 00:00:00 2001 From: kaneeldias Date: Thu, 19 Aug 2021 12:42:51 +0530 Subject: [PATCH 07/22] Update Ballerina.toml --- Ballerina.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Ballerina.toml b/Ballerina.toml index 270a074..76df54a 100644 --- a/Ballerina.toml +++ b/Ballerina.toml @@ -1,9 +1,9 @@ [package] org="ballerinax" -name="mysql.driver" +name="mssql.driver" version="1.0.0" authors=["Ballerina"] -keywords=["MSSQL", "Sql Server"] +keywords=["MSSQL", "SQL Server"] repository="https://github.com/ballerina-platform/module-ballerinax-mssql.driver" license=["Apache-2.0"] From 86a2a3157cad66bba4a8c961f99e7e6f752f8e97 Mon Sep 17 00:00:00 2001 From: kaneeldias Date: Fri, 20 Aug 2021 12:27:09 +0530 Subject: [PATCH 08/22] Add connection tests --- tests/connection-init-test.bal | 71 ++++++++++++++++++++++++++++++++++ tests/utils.bal | 71 ++++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 tests/connection-init-test.bal create mode 100644 tests/utils.bal diff --git a/tests/connection-init-test.bal b/tests/connection-init-test.bal new file mode 100644 index 0000000..50295b1 --- /dev/null +++ b/tests/connection-init-test.bal @@ -0,0 +1,71 @@ +// Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + +// WSO2 Inc. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 + +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import ballerina/sql; +import ballerinax/java.jdbc; +import ballerina/test; + +string connectDB = "CONNECT_DB"; + +@test:BeforeGroups { + value: ["connection-init"] +} +function initConnectionTests() returns error? { + _ = check executeQueryMssqlClient(`DROP DATABASE IF EXISTS CONNECT_DB`); + _ = check executeQueryMssqlClient(`CREATE DATABASE CONNECT_DB`); +} + +@test:Config { + groups: ["connection", "connection-init"] +} +function testWithURLParams() returns error? { + jdbc:Client dbClient = check new (url, user, password); + sql:Error? closeResult = dbClient.close(); + test:assertExactEquals(closeResult, (), "Initialising connection with params fails."); +} + +@test:Config { + groups: ["connection", "connection-init"] +} +function testWithConnectionPool() returns error? { + sql:ConnectionPool connectionPool = { + maxOpenConnections: 25, + maxConnectionLifeTime : 15, + minIdleConnections : 15 + }; + jdbc:Client dbClient = check new (url = url, user = user, password = password, connectionPool = connectionPool); + sql:Error? closeResult = dbClient.close(); + test:assertExactEquals(closeResult, (), "Initialising connection with option max connection pool fails."); + test:assertEquals(connectionPool.maxOpenConnections, 25, "Configured max connection config is wrong."); + test:assertEquals(connectionPool.maxConnectionLifeTime, 15, "Configured max connection life time second is wrong."); + test:assertEquals(connectionPool.minIdleConnections, 15, "Configured min idle connection is wrong."); +} + +@test:Config { + groups: ["connection", "connection-init"] +} +function testWithClosedClient1() returns error? { + jdbc:Client dbClient = check new (url = url, user = user, password = password); + sql:Error? closeResult = dbClient.close(); + test:assertExactEquals(closeResult, (), "Initialising connection with connection params fails."); + sql:ExecutionResult|sql:Error result = dbClient->execute(`CREATE TABLE test (id int)`); + if (result is sql:Error) { + string expectedErrorMessage = "SQL Client is already closed, hence further operations are not allowed"; + test:assertTrue(result.message().startsWith(expectedErrorMessage), + "Error message does not match, actual :\n'" + result.message() + "'\nExpected : \n" + expectedErrorMessage); + } else { + test:assertFail("Error expected"); + } +} diff --git a/tests/utils.bal b/tests/utils.bal new file mode 100644 index 0000000..2f3aada --- /dev/null +++ b/tests/utils.bal @@ -0,0 +1,71 @@ +// Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +// +// WSO2 Inc. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import ballerinax/java.jdbc; +import ballerina/sql; + +string url = "jdbc:sqlserver://localhost:1433;"; +string user = "sa"; +string password = "Test123#"; + +function getMssqlClient(string? database = ()) returns jdbc:Client|error { + jdbc:Client dbClient; + if database is () { + dbClient = check new (url, user, password); + } else { + dbClient = check new (url + "/" + database, user, password); + } + return dbClient; +} + +function queryMssqlClient(string|sql:ParameterizedQuery sqlQuery, typedesc? resultType = (), string? database = ()) +returns record {}|error? { + jdbc:Client dbClient = check getMssqlClient(database); + stream streamData; + if resultType is () { + streamData = dbClient->query(sqlQuery); + } else { + streamData = dbClient->query(sqlQuery, resultType); + } + record {|record {} value;|}? data = check streamData.next(); + check streamData.close(); + record {}? value = data?.value; + check dbClient.close(); + return value; +} + +function executeQueryMssqlClient(string|sql:ParameterizedQuery sqlQuery, string? database = ()) +returns sql:ExecutionResult|error { + jdbc:Client dbClient = check getMssqlClient(database); + sql:ExecutionResult result = check dbClient->execute(sqlQuery); + check dbClient.close(); + return result; +} + +function batchExecuteQueryMssqlClient(sql:ParameterizedQuery[] sqlQueries, string? database = ()) returns sql:ExecutionResult[] | error { + jdbc:Client dbClient = check getMssqlClient(database); + sql:ExecutionResult[] result = check dbClient->batchExecute(sqlQueries); + check dbClient.close(); + return result; +} + +function callProcedureMssqlClient(sql:ParameterizedCallQuery sqlQuery, string database, typedesc[] rowTypes = []) +returns sql:ProcedureCallResult | error { + jdbc:Client dbClient = check getMssqlClient(database); + sql:ProcedureCallResult result = check dbClient->call(sqlQuery, rowTypes); + check dbClient.close(); + return result; +} From 784b78286ebe4ddd46d2881c681e68204b02e84c Mon Sep 17 00:00:00 2001 From: kaneeldias Date: Fri, 20 Aug 2021 13:06:22 +0530 Subject: [PATCH 09/22] Add query, execute and procedure call tests --- tests/call-procedures-test.bal | 416 +++++++++++++++++++++++++++++++++ tests/execute-basic-test.bal | 325 ++++++++++++++++++++++++++ tests/query-params-test.bal | 368 +++++++++++++++++++++++++++++ 3 files changed, 1109 insertions(+) create mode 100644 tests/call-procedures-test.bal create mode 100644 tests/execute-basic-test.bal create mode 100644 tests/query-params-test.bal diff --git a/tests/call-procedures-test.bal b/tests/call-procedures-test.bal new file mode 100644 index 0000000..3587ccc --- /dev/null +++ b/tests/call-procedures-test.bal @@ -0,0 +1,416 @@ +// Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +// +// WSO2 Inc. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import ballerinax/java.jdbc; +import ballerina/sql; +import ballerina/test; + +string proceduresDb = "procedures_db"; + +@test:BeforeGroups { + value: ["procedures"] +} +function initCallProceduresTests() returns error? { + _ = check executeQueryMssqlClient(`DROP DATABASE IF EXISTS PROCEDURES_DB`); + _ = check executeQueryMssqlClient(`CREATE DATABASE PROCEDURES_DB`); + + sql:ParameterizedQuery query = ` + + DROP TABLE IF EXISTS ExactNumeric; + + CREATE TABLE ExactNumeric( + row_id INT PRIMARY KEY, + bigint_type bigint, + numeric_type numeric(10,5), + smallint_type smallint, + decimal_type decimal(5,2), + int_type INT, + tinyint_type tinyint, + ); + + INSERT INTO ExactNumeric (row_id, bigint_type, numeric_type, smallint_type, decimal_type, int_type, tinyint_type) + VALUES(1, 9223372036854775807, 12.12000, 32767, 123.41, 2147483647, 255); + + INSERT INTO ExactNumeric (row_id) + VALUES(2); + + DROP TABLE IF EXISTS ApproximateNumeric; + + CREATE TABLE ApproximateNumeric( + row_id INT PRIMARY KEY, + float_type float, + real_type real + ); + + INSERT INTO ApproximateNumeric (row_id, float_type, real_type) VALUES (1, 1.79E+308, -1.18E-38); + + DROP TABLE IF EXISTS DateandTime; + + CREATE TABLE DateandTime( + row_id INT PRIMARY KEY, + date_type date, + dateTimeOffset_type datetimeoffset, + dateTime2_type datetime2, + smallDateTime_type smalldatetime, + dateTime_type datetime, + time_type time + ); + + INSERT INTO DateandTime (row_id, date_type, dateTimeOffset_type, dateTime2_type, smallDateTime_type , dateTime_type, time_type) + VALUES (1, '2017-06-26', '2020-01-01 19:14:51 +05:30', '1900-01-01 00:25:00.0021425', '2007-05-10 10:00:20', '2017-06-26 09:54:21.325', '09:46:22'); + + DROP TABLE IF EXISTS StringTypes; + + CREATE TABLE StringTypes ( + row_id INT PRIMARY KEY, + varchar_type VARCHAR(255), + char_type CHAR(14), + text_type TEXT + ); + + INSERT INTO StringTypes (row_id, varchar_type, char_type, text_type) VALUES (1,'This is a varchar','This is a char','This is a long text'); + + INSERT INTO StringTypes (row_id) VALUES (3); + + DROP TABLE IF EXISTS MoneyTypes; + + CREATE TABLE MoneyTypes ( + row_id INT PRIMARY KEY, + money_type money, + smallmoney_type smallmoney + ); + `; + + sql:ParameterizedQuery query1 = ` + CREATE PROCEDURE StringProcedure + @row_id_in int, + @char_in char(14), + @varchar_in varchar(255), + @text_in text + AS + SET NOCOUNT ON + INSERT INTO StringTypes ([row_id], [char_type], [varchar_type], [text_type]) + VALUES(@row_id_in, @char_in, @varchar_in, @text_in) + `; + + sql:ParameterizedQuery query2 = ` + CREATE PROCEDURE ExactNumericProcedure + @row_id_in int, + @smallint_in smallint, + @int_in int, + @bigint_in bigint, + @decimal_in decimal(5,2), + @numeric_in numeric(10,5), + @tinyint_in tinyint + AS + SET NOCOUNT ON + INSERT INTO + ExactNumeric ([row_id], [smallint_type], [int_type], [bigint_type], [decimal_type], [numeric_type], [tinyint_type]) + VALUES(@row_id_in, @smallint_in, @int_in, @bigint_in, @decimal_in, @numeric_in, @tinyint_in) + `; + + sql:ParameterizedQuery query3 = ` + CREATE PROCEDURE DateTimeProcedure + @row_id_in int, + @date_type_in date, + @dateTimeOffset_type_in datetimeoffset, + @dateTime2_type_in datetime2, + @smallDateTime_type_in smalldatetime, + @dateTime_type_in datetime, + @time_type_in time + AS + SET NOCOUNT ON + INSERT INTO + DateandTime ([row_id], [date_type], [dateTimeOffset_type], [dateTime2_type], [smallDateTime_type], [dateTime_type], [time_type]) + VALUES( + @row_id_in, @date_type_in, @dateTimeOffset_type_in, @dateTime2_type_in, @smallDateTime_type_in, + @dateTime_type_in, @time_type_in + ) + `; + + sql:ParameterizedQuery query4 = ` + CREATE PROCEDURE ApproximateNumericProcedure + @row_id_in int, + @float_type_in float, + @real_type_in real + AS + SET NOCOUNT ON + INSERT INTO ApproximateNumeric ([row_id], [float_type], [real_type]) + VALUES (@row_id_in, @float_type_in, @real_type_in) + `; + + sql:ParameterizedQuery query5 = ` + CREATE PROCEDURE MoneyProcedure + @row_id_in int, + @money_type_in money, + @smallmoney_type_in smallmoney + AS + SET NOCOUNT ON + INSERT INTO + MoneyTypes ([row_id], [money_type], [smallMoney_type]) + VALUES (@row_id_in, @money_type_in, @smallmoney_type_in) + `; + + sql:ParameterizedQuery query6 = ` + CREATE PROCEDURE ExactNumericOutProcedure ( + @row_id_in int, + @smallint_out smallint OUTPUT, + @int_out int OUTPUT, + @bigint_out bigint OUTPUT, + @tinyint_out tinyint OUTPUT, + @decimal_out decimal(5,2) OUTPUT, + @numeric_out numeric(10,5) OUTPUT + ) + AS + SET NOCOUNT ON + SELECT + @smallint_out, + @int_out, + @bigint_out, + @tinyint_out, + @decimal_out, + @numeric_out + FROM + ExactNumeric + WHERE + ExactNumeric.row_id = @row_id_in + `; + + sql:ParameterizedQuery query7 = ` + CREATE PROCEDURE DateTimeOutProcedure + @row_id_in int, + @dateTimeOffset_type_out datetimeoffset OUTPUT + AS + SET NOCOUNT OFF + SELECT @dateTimeOffset_type_out = dateTimeOffset_type + FROM DateandTime + WHERE row_id = @row_id_in; + `; + + // Data for timestamp retrieval test + sql:ParameterizedQuery query8 = `INSERT INTO DateandTime (row_id, dateTimeOffset_type) VALUES (2, '2021-07-21 19:14:51.00 +01:30')`; + + sql:ParameterizedQuery query9 = ` + CREATE PROCEDURE SelectStringTypesMultiple AS + BEGIN + SELECT row_id, varchar_type, char_type, text_type FROM StringTypes WHERE row_id = 1; + SELECT varchar_type FROM StringTypes WHERE row_id = 1; + END + `; + + _ = check executeQueryMssqlClient(query, proceduresDb); + _ = check executeQueryMssqlClient(query1, proceduresDb); + _ = check executeQueryMssqlClient(query2, proceduresDb); + _ = check executeQueryMssqlClient(query3, proceduresDb); + _ = check executeQueryMssqlClient(query4, proceduresDb); + _ = check executeQueryMssqlClient(query5, proceduresDb); + _ = check executeQueryMssqlClient(query6, proceduresDb); + _ = check executeQueryMssqlClient(query7, proceduresDb); + _ = check executeQueryMssqlClient(query8, proceduresDb); + _ = check executeQueryMssqlClient(query9, proceduresDb); +} + +public type StringProcedureRecord record { + int row_id; + string char_type; + string varchar_type; + string text_type; +}; + +@test:Config { + groups: ["procedures"] +} +function testStringProcedureCall() returns error? { + int rowId = 35; + sql:CharValue charValue = new("This is a char"); + sql:VarcharValue varcharValue = new("This is a varchar3"); + string textValue = "This is a text3"; + + sql:ParameterizedCallQuery sqlQuery = `exec StringProcedure ${rowId}, ${charValue}, ${varcharValue}, ${textValue};`; + sql:ProcedureCallResult result = check callProcedureMssqlClient(sqlQuery, proceduresDb, [StringProcedureRecord]); + + sql:ParameterizedQuery query = `SELECT row_id, char_type, varchar_type, text_type FROM StringTypes WHERE row_id = ${rowId}`; + + StringProcedureRecord expectedDataRow = { + row_id: rowId, + char_type: "This is a char", + varchar_type: "This is a varchar3", + text_type: "This is a text3" + }; + test:assertEquals(check queryMssqlClient(query, StringProcedureRecord, proceduresDb), expectedDataRow, + "Character Call procedure insert and query did not match."); +} + +public type ExactNumericProcedureRecord record { + int row_id; + int smallint_type; + int int_type; + int bigint_type; + decimal decimal_type; + decimal numeric_type; + int tinyint_type; +}; + +@test:Config { + groups: ["procedures"] +} +function testExactNumericProcedureCall() returns error? { + int rowId = 35; + sql:SmallIntValue smallintType = new (1); + sql:IntegerValue intType = new (1); + int bigintType = 123456; + sql:DecimalValue decimalType = new (123.56); + decimal numericType = 12.12000; + int tinyintType = 255; + + sql:ParameterizedCallQuery sqlQuery = + `exec ExactNumericProcedure ${rowId}, ${smallintType}, ${intType}, ${bigintType}, ${decimalType}, + ${numericType}, ${tinyintType};`; + sql:ProcedureCallResult result = check callProcedureMssqlClient(sqlQuery, proceduresDb, [ExactNumericProcedureRecord]); + + sql:ParameterizedQuery query = + `SELECT row_id, smallint_type, int_type, bigint_type, decimal_type, numeric_type, tinyint_type + FROM ExactNumeric WHERE row_id = ${rowId}`; + + ExactNumericProcedureRecord expectedDataRow = { + row_id: rowId, + smallint_type: 1, + int_type: 1, + bigint_type: 123456, + decimal_type: 123.56, + numeric_type: 12.12000, + tinyint_type: 255 + }; + test:assertEquals(check queryMssqlClient(query, ExactNumericProcedureRecord, proceduresDb), expectedDataRow, + "Numeric Call procedure insert and query did not match."); +} + +public type ApproximateNumericProcedureRecord record { + int row_id; + float float_type; + float real_type; +}; + +@test:Config { + groups: ["procedures"] +} +function testApproximateNumericProcedureCall() returns error? { + int rowId = 35; + float floatType = 1.79E+308; + float realType = -1.179999945774631E-38; + sql:ParameterizedCallQuery sqlQuery = `exec ApproximateNumericProcedure ${rowId}, ${floatType}, ${realType};`; + sql:ProcedureCallResult result = check callProcedureMssqlClient(sqlQuery, proceduresDb, [ApproximateNumericProcedureRecord]); + + sql:ParameterizedQuery query = `SELECT * FROM ApproximateNumeric WHERE row_id = ${rowId}`; + + ApproximateNumericProcedureRecord expectedDataRow = { + row_id: rowId, + float_type: 1.79E+308, + real_type:-1.179999945774631E-38 + }; + test:assertEquals(check queryMssqlClient(query, ApproximateNumericProcedureRecord, proceduresDb), expectedDataRow, + "ApproximateNumeric Call procedure insert and query did not match."); +} + +public type DatetimeProcedureRecord record { + int row_id; + string date_type; + string time_type; + string dateTime_type; + string dateTime2_type; + string smallDateTime_type; + string dateTimeOffset_type; +}; + +@test:Config { + groups: ["procedures"] +} +function testDatetimeProcedureCall() returns error? { + int rowId = 35; + sql:DateValue date = new ("2017-06-26"); + sql:DateTimeValue date_time_offset = new("2020-01-01 19:14:51.0021425 +05:30"); + sql:DateTimeValue date_time2 = new ("1900-01-01 00:25:00.0021425"); + sql:DateTimeValue small_date_time = new ("2007-05-10 10:00:20"); + sql:DateTimeValue date_time = new ("2017-06-26 09:54:21.325"); + sql:TimeValue time = new ("09:46:22"); + sql:ParameterizedCallQuery sqlQuery = + `exec DatetimeProcedure ${rowId}, ${date}, ${date_time_offset}, ${date_time2}, ${small_date_time}, ${date_time}, ${time};`; + sql:ProcedureCallResult result = check callProcedureMssqlClient(sqlQuery, proceduresDb, [DatetimeProcedureRecord]); + + sql:ParameterizedQuery query = `SELECT * FROM DateandTime WHERE row_id = ${rowId}`; + + DatetimeProcedureRecord expectedDataRow = { + row_id: rowId, + date_type: "2017-06-26", + time_type: "09:46:22", + dateTime_type: "2017-06-26 09:54:21.327", + dateTime2_type: "1900-01-01 00:25:00.0021425", + smallDateTime_type: "2007-05-10 10:00:00.0", + dateTimeOffset_type: "2020-01-01 19:14:51.0021425 +05:30" + }; + test:assertEquals(check queryMssqlClient(query, DatetimeProcedureRecord, proceduresDb), expectedDataRow, + "Datetime Call procedure insert and query did not match."); +} + +public type MoneyProcedureRecord record { + int row_id; + string money_type; + string smallMoney_type; +}; + +@test:Config { + groups: ["procedures"] +} +function testMultipleSelectProcedureCall() returns error? { + sql:ParameterizedCallQuery sqlQuery = `{call SelectStringTypesMultiple}`; + jdbc:Client dbClient = check getMssqlClient(proceduresDb); + sql:ProcedureCallResult result = check dbClient->call(sqlQuery, [StringProcedureRecord, StringProcedureRecord]); + + stream? qResult = result.queryResult; + if qResult is () { + test:assertFail("First result set is empty."); + } else { + record {|record {} value;|}? data = check qResult.next(); + record {}? result1 = data?.value; + StringProcedureRecord expectedDataRow = { + row_id: 1, + char_type: "This is a char", + varchar_type: "This is a varchar", + text_type: "This is a long text" + }; + test:assertEquals(result1, expectedDataRow, "Call procedure first select did not match."); + } + + boolean nextResult = check result.getNextQueryResult(); + if !nextResult { + test:assertFail("Only one result set returned."); + } + + qResult = result.queryResult; + if qResult is () { + test:assertFail("Second result set is empty."); + } else { + record {|record {} value;|}? data = check qResult.next(); + record {}? result1 = data?.value; + record {} expectedDataRow = { + "varchar_type": "This is a varchar" + }; + test:assertEquals(result1, expectedDataRow, "Call procedure second select did not match."); + } + + check result.close(); + check dbClient.close(); +} diff --git a/tests/execute-basic-test.bal b/tests/execute-basic-test.bal new file mode 100644 index 0000000..6d5ce4c --- /dev/null +++ b/tests/execute-basic-test.bal @@ -0,0 +1,325 @@ +// Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + +// WSO2 Inc. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 + +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import ballerinax/java.jdbc; +import ballerina/sql; +import ballerina/test; + +string executeDb = "EXECUTE_DB"; + +@test:BeforeGroups { + value: ["execute-basic"] +} +function initExecuteBasicTests() returns error? { + _ = check executeQueryMssqlClient(`DROP DATABASE IF EXISTS EXECUTE_DB`); + _ = check executeQueryMssqlClient(`CREATE DATABASE EXECUTE_DB`); + + sql:ParameterizedQuery query = ` + DROP TABLE IF EXISTS ExactNumericTypes; + + CREATE TABLE ExactNumericTypes ( + id INT NOT NULL IDENTITY PRIMARY KEY, + smallint_type SMALLINT, + int_type INT, + tinyint_type TINYINT, + bigint_type BIGINT, + decimal_type DECIMAL, + numeric_type NUMERIC, + ); + + DROP TABLE IF EXISTS StringTypes; + + CREATE TABLE StringTypes ( + id INT PRIMARY KEY, + varchar_type VARCHAR(255), + char_type CHAR(4), + text_type TEXT, + nchar_type NCHAR(4), + nvarchar_type NVARCHAR(10) + ); + + INSERT INTO StringTypes (id, varchar_type) VALUES (1, 'test data'); + + DROP TABLE IF EXISTS GeometricTypes; + + CREATE TABLE GeometricTypes ( + row_id INT PRIMARY KEY, + point_type geometry, + lineString_type geometry, + geometry_type geometry, + circularstring_type geometry, + compoundcurve_type geometry, + polygon_type geometry, + curvepolygon_type geometry, + multipolygon_type geometry, + multilinestring_type geometry, + multipoint_type geometry + ); + + DROP TABLE IF EXISTS MoneyTypes; + + CREATE TABLE MoneyTypes ( + row_id INT PRIMARY KEY, + money_type money, + smallmoney_type smallmoney + ); + + `; + _ = check executeQueryMssqlClient(query, executeDb); +} + +@test:Config { + groups: ["execute", "execute-basic"] +} +function testCreateTable() returns error? { + sql:ExecutionResult result = check executeQueryMssqlClient( + "CREATE TABLE Student(studentID int, LastName varchar(255))", executeDb); + test:assertExactEquals(result.affectedRowCount, 0, "Affected row count is different."); + test:assertExactEquals(result.lastInsertId, (), "Last Insert Id is not nil."); +} + +@test:Config { + groups: ["execute", "execute-basic"], + dependsOn: [testCreateTable] +} +function testInsertTable() returns error? { + sql:ExecutionResult result = check executeQueryMssqlClient( + "INSERT INTO ExactNumericTypes (int_type) VALUES (20)", executeDb); + test:assertExactEquals(result.affectedRowCount, 1, "Affected row count is different."); + string|int? insertId = result.lastInsertId; + if (insertId is string) { + int|error id = int:fromString(insertId); + if (id is int) { + test:assertTrue(id == 1, "Last Insert Id is nil."); + } else { + test:assertFail("Insert Id should be an integer."); + } + } else { + test:assertFail("Insert Id is not string"); + } +} + +@test:Config { + groups: ["execute", "execute-basic"], + dependsOn: [testInsertTable] +} +function testInsertTableWithoutGeneratedKeys() returns error? { + sql:ExecutionResult result = check executeQueryMssqlClient( + "INSERT INTO StringTypes (id, varchar_type) VALUES (5, 'test data')", executeDb); + test:assertExactEquals(result.affectedRowCount, 1, "Affected row count is different."); + test:assertEquals(result.lastInsertId, (), "Last Insert Id is nil."); +} + +@test:Config { + groups: ["execute", "execute-basic"], + dependsOn: [testInsertTableWithoutGeneratedKeys] +} +function testInsertTableWithGeneratedKeys() returns error? { + sql:ExecutionResult result = check executeQueryMssqlClient( + "INSERT INTO ExactNumericTypes (int_type) VALUES (21)", executeDb); + test:assertExactEquals(result.affectedRowCount, 1, "Affected row count is different."); + string|int? insertId = result.lastInsertId; + if (insertId is string) { + int|error id = int:fromString(insertId); + if (id is int) { + test:assertTrue(id > 1, "Last Insert Id is nil."); + } else { + test:assertFail("Insert Id should be an integer."); + } + } else { + test:assertFail("Insert Id is not string"); + } +} + +type ExactNumericType record { + int id; + int? int_type; + int? bigint_type; + int? smallint_type; + int? tinyint_type; + decimal? decimal_type; + decimal? numeric_type; +}; + +@test:Config { + groups: ["execute", "execute-basic"], + dependsOn: [testInsertTableWithGeneratedKeys] +} +function testInsertAndSelectTableWithGeneratedKeys() returns error? { + sql:ExecutionResult result = check executeQueryMssqlClient( + "INSERT INTO ExactNumericTypes (int_type) VALUES (31)", executeDb); + test:assertExactEquals(result.affectedRowCount, 1, "Affected row count is different."); + + string|int? insertId = result.lastInsertId; + if (insertId is string) { + int|error id = int:fromString(insertId); + if (id is int) { + string query = string `SELECT * from ExactNumericTypes where id = ${id}`; + record {}? queryResult = check queryMssqlClient(query, database = executeDb); + test:assertNotExactEquals(queryResult, (), "Incorrect InsertId returned."); + } else { + test:assertFail("Insert Id should be an integer."); + } + } else { + test:assertFail("Insert Id is not string"); + } +} + +@test:Config { + groups: ["execute", "execute-basic"], + dependsOn: [testInsertAndSelectTableWithGeneratedKeys] +} +function testInsertWithAllNilAndSelectTableWithGeneratedKeys() returns error? { + sql:ExecutionResult result = check executeQueryMssqlClient( + "INSERT INTO ExactNumericTypes (smallint_type, int_type, bigint_type, decimal_type, numeric_type) " + + "VALUES (null, null, null, null, null)", executeDb); + test:assertExactEquals(result.affectedRowCount, 1, "Affected row count is different."); + + string|int? insertedId = result.lastInsertId; + if (insertedId is string) { + int|error id = int:fromString(insertedId); + if (id is int) { + string query = string `SELECT * FROM ExactNumericTypes WHERE id = ${id}`; + record {}? queryResult = check queryMssqlClient(query, database = executeDb); + test:assertNotExactEquals(queryResult, (), "Incorrect InsertId returned."); + } else { + test:assertFail("Insert Id should be an integer."); + } + } else { + test:assertFail("Insert Id is not string"); + } +} + +type StringData record { + int id; + string varchar_type; + string char_type; + string text_type; + string nchar_type; + string nvarchar_type; +}; + +@test:Config { + groups: ["execute", "execute-basic"], + dependsOn: [testInsertAndSelectTableWithGeneratedKeys] +} +function testInsertWithStringAndSelectTable() returns error? { + string intIDVal = "25"; + string insertQuery = "INSERT INTO StringTypes (id, varchar_type, char_type, text_type" + + ", nchar_type, nvarchar_type) VALUES (" + intIDVal + ",'str1','str2','str3','str4','str5')"; + sql:ExecutionResult result = check executeQueryMssqlClient(insertQuery, executeDb); + test:assertExactEquals(result.affectedRowCount, 1, "Affected row count is different."); + + string query = string `SELECT * FROM StringTypes WHERE id = ${intIDVal}`; + record {}? queryResult = check queryMssqlClient(query, StringData, executeDb); + + StringData expectedInsertRow = { + id: 25, + varchar_type: "str1", + char_type: "str2", + text_type: "str3", + nchar_type: "str4", + nvarchar_type: "str5" + }; + test:assertEquals(queryResult, expectedInsertRow, "Incorrect InsetId returned."); +} + +type ResultCount record { + int countVal; +}; + +@test:Config { + groups: ["execute", "execute-basic"], + dependsOn: [testInsertWithStringAndSelectTable] +} +function testUpdateNumericData() returns error? { + sql:ExecutionResult result = check executeQueryMssqlClient( + "UPDATE ExactNumericTypes SET int_type = 11 WHERE int_type = 20", executeDb); + test:assertExactEquals(result.affectedRowCount, 1, "Affected row count is different."); + + jdbc:Client dbClient = check getMssqlClient(executeDb); + stream queryResult = dbClient->query( + "SELECT COUNT(*) as countval FROM ExactNumericTypes WHERE int_type = 11", ResultCount); + stream streamData = >queryResult; + record {|ResultCount value;|}? data = check streamData.next(); + check streamData.close(); + test:assertEquals(data?.value?.countVal, 1, "Update command was not successful."); + + check dbClient.close(); + } + +@test:Config { + groups: ["execute", "execute-basic"], + dependsOn: [testUpdateNumericData] +} +function testUpdateStringData() returns error? { + sql:ExecutionResult result = check executeQueryMssqlClient( + "UPDATE StringTypes SET varchar_type = 'updatedstring' WHERE varchar_type = 'str1'", executeDb); + test:assertExactEquals(result.affectedRowCount, 1, "Affected row count is different."); + + jdbc:Client dbClient = check getMssqlClient(executeDb); + stream queryResult = dbClient->query( + "SELECT COUNT(*) as countval FROM StringTypes WHERE varchar_type = 'updatedstring'", ResultCount); + stream streamData = >queryResult; + record {|ResultCount value;|}? data = check streamData.next(); + check streamData.close(); + test:assertEquals(data?.value?.countVal, 1, "String table Update command was not successful."); + + check dbClient.close(); +} + +@test:Config { + groups: ["execute", "execute-basic"], + dependsOn: [testUpdateStringData] +} +function testDeleteNumericData() returns error? { + sql:ExecutionResult result = check executeQueryMssqlClient( + "INSERT INTO ExactNumericTypes (int_type) VALUES (1451)", executeDb); + result = check executeQueryMssqlClient( + "DELETE FROM ExactNumericTypes WHERE int_type = 1451", executeDb); + test:assertExactEquals(result.affectedRowCount, 1, "Affected row count is different."); + + jdbc:Client dbClient = check getMssqlClient(executeDb); + stream queryResult = dbClient->query( + "SELECT COUNT(*) as countval FROM ExactNumericTypes WHERE int_type = 1451", ResultCount); + stream streamData = >queryResult; + record {|ResultCount value;|}? data = check streamData.next(); + check streamData.close(); + test:assertEquals(data?.value?.countVal, 0, "Numeric table Delete command was not successful."); + + check dbClient.close(); +} + +@test:Config { + groups: ["execute", "execute-basic"], + dependsOn: [testDeleteNumericData] +} +function testDeleteStringData() returns error? { + string intId = "28"; + sql:ExecutionResult result = check executeQueryMssqlClient( + "INSERT INTO StringTypes (id, varchar_type) VALUES (" + intId +", 'deletestr')", executeDb); + result = check executeQueryMssqlClient("DELETE FROM StringTypes WHERE varchar_type = 'deletestr'", executeDb); + test:assertExactEquals(result.affectedRowCount, 1, "Affected row count is different."); + + jdbc:Client dbClient = check getMssqlClient(executeDb); + stream queryResult = dbClient->query( + "SELECT COUNT(*) as countval FROM StringTypes WHERE varchar_type = 'deletestr'", ResultCount); + stream streamData = >queryResult; + record {|ResultCount value;|}? data = check streamData.next(); + check streamData.close(); + test:assertEquals(data?.value?.countVal, 0, "String table Delete command was not successful."); + + check dbClient.close(); +} diff --git a/tests/query-params-test.bal b/tests/query-params-test.bal new file mode 100644 index 0000000..32e01e9 --- /dev/null +++ b/tests/query-params-test.bal @@ -0,0 +1,368 @@ +// Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +// +// WSO2 Inc. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import ballerina/sql; +import ballerina/test; +import ballerina/time; + +string simpleParamsDb = "simple_params_query_db"; + +@test:BeforeGroups { + value: ["query-simple-params"] +} +function initQueryParamsTests() returns error? { + _ = check executeQueryMssqlClient(`DROP DATABASE IF EXISTS SIMPLE_PARAMS_QUERY_DB`); + _ = check executeQueryMssqlClient(`CREATE DATABASE SIMPLE_PARAMS_QUERY_DB`); + + sql:ParameterizedQuery query = ` + + DROP TABLE IF EXISTS ExactNumeric; + + CREATE TABLE ExactNumeric( + row_id INT PRIMARY KEY, + bigint_type bigint, + numeric_type numeric(10,5), + bit_type bit, + smallint_type smallint, + decimal_type decimal(5,2), + smallmoney_type smallmoney, + int_type INT, + tinyint_type tinyint, + money_type money + ); + + + INSERT INTO ExactNumeric (row_id, bigint_type, numeric_type, bit_type, smallint_type, decimal_type, smallmoney_type, int_type, tinyint_type, money_type) + VALUES(1, 9223372036854775807, 12.12000, 1, 32767, 123.41, 214748.3647, 2147483647, 255, 922337203685477.2807); + + DROP TABLE IF EXISTS ApproximateNumeric; + + CREATE TABLE ApproximateNumeric( + row_id INT PRIMARY KEY, + float_type float, + real_type real + ); + + INSERT INTO ApproximateNumeric (row_id, float_type, real_type) VALUES (1, 1.79E+308, -1.18E-38); + + DROP TABLE IF EXISTS DateandTime; + + CREATE TABLE DateandTime( + row_id INT PRIMARY KEY, + date_type date, + dateTimeOffset_type datetimeoffset, + dateTime2_type datetime2, + smallDateTime_type smalldatetime, + dateTime_type datetime, + time_type time + ); + + INSERT INTO DateandTime (row_id, date_type, dateTimeOffset_type, dateTime2_type, smallDateTime_type , dateTime_type, time_type) + VALUES (1, '2017-06-26', '2020-01-01 19:14:51 +05:30', '1900-01-01 00:25:00.0021425', '2007-05-10 10:00:20', '2017-06-26 09:54:21.325', '09:46:22'); + + DROP TABLE IF EXISTS StringTypes; + + CREATE TABLE StringTypes ( + row_id INT PRIMARY KEY, + varchar_type VARCHAR(255), + char_type CHAR(14), + text_type TEXT, + nchar_type NCHAR(4), + nvarchar_type NVARCHAR(10) + ); + + INSERT INTO StringTypes (row_id, varchar_type, char_type, text_type, nchar_type, nvarchar_type) VALUES (1,'This is a varchar','This is a char','This is a long text','str4','str5'); + + DROP TABLE IF EXISTS GeometricTypes; + + CREATE TABLE GeometricTypes ( + row_id INT PRIMARY KEY, + point_type geometry, + pointCol AS point_type.STAsText(), + lineString_type geometry, + lineCol AS lineString_type.STAsText(), + ); + + INSERT INTO GeometricTypes (row_id, point_type) VALUES (1,'POINT (4 6)'); + `; + _ = check executeQueryMssqlClient(query, simpleParamsDb); +} + +@test:Config { + groups: ["query","query-simple-params"] +} +function querySingleIntParam() returns error? { + int rowId = 1; + sql:ParameterizedQuery sqlQuery = `SELECT * FROM ExactNumeric WHERE row_id = ${rowId}`; + validateExactNumericTableResult(check queryMssqlClient(sqlQuery, database = simpleParamsDb)); +} + +@test:Config { + groups: ["query","query-simple-params"] +} +function queryDoubleIntParam() returns error? { + int rowId = 1; + int intValue1 = 2147483647; + sql:IntegerValue intValue2 = new(2147483647); + sql:ParameterizedQuery sqlQuery1 = `SELECT * FROM ExactNumeric WHERE row_id = ${rowId} AND int_type = ${intValue1}`; + sql:ParameterizedQuery sqlQuery2 = `SELECT * FROM ExactNumeric WHERE row_id = ${rowId} AND int_type = ${intValue2}`; + validateExactNumericTableResult(check queryMssqlClient(sqlQuery1, database = simpleParamsDb)); + validateExactNumericTableResult(check queryMssqlClient(sqlQuery2, database = simpleParamsDb)); +} + +@test:Config { + groups: ["query","query-simple-params"] +} +function queryBigintParam() returns error? { + int rowId = 1; + int bigInt1 = 9223372036854775807; + sql:BigIntValue bigInt2 = new(9223372036854775807); + sql:ParameterizedQuery sqlQuery1 = `SELECT * FROM ExactNumeric WHERE row_id = ${rowId} AND bigint_type = ${bigInt1}`; + sql:ParameterizedQuery sqlQuery2 = `SELECT * FROM ExactNumeric WHERE row_id = ${rowId} AND bigint_type = ${bigInt2}`; + validateExactNumericTableResult(check queryMssqlClient(sqlQuery1, database = simpleParamsDb)); + validateExactNumericTableResult(check queryMssqlClient(sqlQuery2, database = simpleParamsDb)); +} + +@test:Config { + groups: ["query","query-simple-params"] +} +function querySmallintParam() returns error? { + int rowId = 1; + int smallInt1 = 32767; + sql:SmallIntValue smallInt2 = new(32767); + sql:ParameterizedQuery sqlQuery1 = `SELECT * FROM ExactNumeric WHERE row_id = ${rowId} AND smallint_type = ${smallInt1}`; + sql:ParameterizedQuery sqlQuery2 = `SELECT * FROM ExactNumeric WHERE row_id = ${rowId} AND smallint_type = ${smallInt2}`; + + validateExactNumericTableResult(check queryMssqlClient(sqlQuery1, database = simpleParamsDb)); + validateExactNumericTableResult(check queryMssqlClient(sqlQuery2, database = simpleParamsDb)); +} + +@test:Config { + groups: ["query","query-simple-params"] +} +function queryTinyintParam() returns error? { + int rowId = 1; + int tinyInt = 255; + sql:ParameterizedQuery sqlQuery = `SELECT * FROM ExactNumeric WHERE row_id = ${rowId} AND tinyint_type = ${tinyInt}`; + validateExactNumericTableResult(check queryMssqlClient(sqlQuery, database = simpleParamsDb)); +} + +@test:Config { + groups: ["query","query-simple-params"] +} +function queryNumericParam() returns error? { + int rowId = 1; + decimal numericVal1 = 12.12000; + sql:NumericValue numericVal2 = new(12.12000); + sql:ParameterizedQuery sqlQuery1 = `SELECT * FROM ExactNumeric WHERE row_id = ${rowId} AND numeric_type = ${numericVal1}`; + sql:ParameterizedQuery sqlQuery2 = `SELECT * FROM ExactNumeric WHERE row_id = ${rowId} AND numeric_type = ${numericVal2}`; + validateExactNumericTableResult(check queryMssqlClient(sqlQuery1, database = simpleParamsDb)); + validateExactNumericTableResult(check queryMssqlClient(sqlQuery2, database = simpleParamsDb)); +} + +@test:Config { + groups: ["query","query-simple-params"] +} +function queryDecimalParam() returns error? { + int rowId = 1; + decimal decimalVal1 = 123.41; + sql:DecimalValue decimalVal2 = new(123.41); + sql:ParameterizedQuery sqlQuery1 = `SELECT * FROM ExactNumeric WHERE row_id = ${rowId} AND decimal_type = ${decimalVal1}`; + sql:ParameterizedQuery sqlQuery2 = `SELECT * FROM ExactNumeric WHERE row_id = ${rowId} AND decimal_type = ${decimalVal2}`; + validateExactNumericTableResult(check queryMssqlClient(sqlQuery1, database = simpleParamsDb)); + validateExactNumericTableResult(check queryMssqlClient(sqlQuery2, database = simpleParamsDb)); +} + +@test:Config { + groups: ["query","query-simple-params"] +} +function queryVarcharParam() returns error? { + string varcharValue1 = "This is a varchar"; + sql:VarcharValue varcharValue2 = new("This is a varchar"); + sql:ParameterizedQuery sqlQuery1 = `SELECT * FROM StringTypes WHERE varchar_type = ${varcharValue1}`; + sql:ParameterizedQuery sqlQuery2 = `SELECT * FROM StringTypes WHERE varchar_type = ${varcharValue2}`; + validateStringTypeTableResult(check queryMssqlClient(sqlQuery1, database = simpleParamsDb)); + validateStringTypeTableResult(check queryMssqlClient(sqlQuery2, database = simpleParamsDb)); +} + +@test:Config { + groups: ["query","query-simple-params"] +} +function queryCharParam() returns error? { + string charValue1 = "This is a char"; + sql:CharValue charValue2 = new("This is a char"); + sql:ParameterizedQuery sqlQuery1 = `SELECT * FROM StringTypes WHERE char_type = ${charValue1}`; + sql:ParameterizedQuery sqlQuery2 = `SELECT * FROM StringTypes WHERE char_type = ${charValue2}`; + validateStringTypeTableResult(check queryMssqlClient(sqlQuery1, database = simpleParamsDb)); + validateStringTypeTableResult(check queryMssqlClient(sqlQuery2, database = simpleParamsDb)); +} + +@test:Config { + groups: ["query","query-simple-params"] +} +function queryIntAndStringParam() returns error? { + string charVal = "This is a char"; + int rowId =1; + sql:ParameterizedQuery sqlQuery = `SELECT * FROM StringTypes WHERE char_type = ${charVal} AND row_id = ${rowId}`; + validateStringTypeTableResult(check queryMssqlClient(sqlQuery, database = simpleParamsDb)); +} + +@test:Config { + groups: ["query","query-simple-params"] +} +function queryFloatParam() returns error? { + int rowId = 1; + sql:ParameterizedQuery sqlQuery = `SELECT * FROM ApproximateNumeric WHERE row_id = ${rowId}`; + validateApproximateNumericTableResult(check queryMssqlClient(sqlQuery, database = simpleParamsDb)); +} + +@test:Config { + groups: ["query","query-simple-params"] +} +function queryBitStringParam() returns error? { + sql:BitValue typeVal = new(true); + sql:ParameterizedQuery sqlQuery = `SELECT * FROM ExactNumeric WHERE bit_type = ${typeVal}`; + validateExactNumericTableResult(check queryMssqlClient(sqlQuery, database = simpleParamsDb)); +} + +@test:Config { + groups: ["query","query-simple-params"] +} +function queryBitInvalidIntParam() returns error? { + sql:BitValue typeVal = new (12); + sql:ParameterizedQuery sqlQuery = `SELECT * FROM ExactNumeric WHERE bit_type = ${typeVal}`; + record{}|error? returnVal = trap queryMssqlClient(sqlQuery, database = simpleParamsDb); + test:assertTrue(returnVal is error); + error dbError = returnVal; + test:assertEquals(dbError.message(), "Only 1 or 0 can be passed for BitValue SQL Type, but found :12"); +} + +@test:Config { + groups: ["query","query-simple-params"] +} +function queryDateValueParam() returns error? { + int rowId = 1; + time:Date dateValue = {year: 2017, month: 6, day: 26}; + sql:DateValue dateValue1 = new (dateValue); + sql:DateValue dateValue2 = new ("2017-06-26"); + sql:ParameterizedQuery sqlQuery1 = `SELECT * FROM DateandTime WHERE date_type = ${dateValue1} AND row_id = ${rowId}`; + sql:ParameterizedQuery sqlQuery2 = `SELECT * FROM DateandTime WHERE date_type = ${dateValue2} AND row_id = ${rowId}`; + validateDateTimeTableResult(check queryMssqlClient(sqlQuery1, database = simpleParamsDb)); + validateDateTimeTableResult(check queryMssqlClient(sqlQuery2, database = simpleParamsDb)); +} + +@test:Config { + groups: ["query","query-simple-params"] +} +function queryDateTimeOffsetValueParam() returns error? { + int rowId = 1; + time:Civil dateTimeOffsetValue = {year: 2020, month:1, day: 1, hour: 19, minute: 14, second:51, "utcOffset": {hours: 5, minutes: 30}}; + sql:DateTimeValue dateTimeOffsetValue1 = new(dateTimeOffsetValue); + sql:DateTimeValue dateTimeOffsetValue2 = new("2020-01-01 19:14:51.0000000 +05:30"); + sql:ParameterizedQuery sqlQuery1 = `SELECT * FROM DateandTime WHERE dateTimeOffset_type = ${dateTimeOffsetValue1} AND row_id = ${rowId}`; + sql:ParameterizedQuery sqlQuery2 = `SELECT * FROM DateandTime WHERE dateTimeOffset_type = ${dateTimeOffsetValue2} AND row_id = ${rowId}`; + validateDateTimeTableResult(check queryMssqlClient(sqlQuery1, database = simpleParamsDb)); + validateDateTimeTableResult(check queryMssqlClient(sqlQuery2, database = simpleParamsDb)); +} + +@test:Config { + groups: ["query","query-simple-params"] +} +function queryDateTime2ValueParam() returns error? { + int rowId = 1; + time:Civil dateTimeValue = {year: 1900, month:1, day: 1, hour: 0, minute: 25, second:0.0021425}; + sql:DateTimeValue dateTimeValue1 = new (dateTimeValue); + sql:DateTimeValue dateTimeValue2 = new ("1900-01-01 00:25:00.0021425"); + sql:ParameterizedQuery sqlQuery1 = `SELECT * FROM DateandTime WHERE dateTime2_type = ${dateTimeValue1} AND row_id = ${rowId}`; + sql:ParameterizedQuery sqlQuery2 = `SELECT * FROM DateandTime WHERE dateTime2_type = ${dateTimeValue2} AND row_id = ${rowId}`; + validateDateTimeTableResult(check queryMssqlClient(sqlQuery1, database = simpleParamsDb)); + validateDateTimeTableResult(check queryMssqlClient(sqlQuery2, database = simpleParamsDb)); +} + +@test:Config { + groups: ["query","query-simple-params"] +} +function querySmallDateTimeValueParam() returns error? { + int rowId = 1; + time:Civil smallDateTimeValue = {year: 2007, month: 5, day: 10, hour: 10, minute: 0, second:0.0}; + sql:DateTimeValue smallDateTimeValue1 = new (smallDateTimeValue); + sql:DateTimeValue smallDateTimeValue2 = new ("2007-05-10 10:00:00.0"); + sql:ParameterizedQuery sqlQuery1 = `SELECT * FROM DateandTime WHERE smallDateTime_type = ${smallDateTimeValue1} AND row_id = ${rowId}`; + sql:ParameterizedQuery sqlQuery2 = `SELECT * FROM DateandTime WHERE smallDateTime_type = ${smallDateTimeValue2} AND row_id = ${rowId}`; + validateDateTimeTableResult(check queryMssqlClient(sqlQuery1, database = simpleParamsDb)); + validateDateTimeTableResult(check queryMssqlClient(sqlQuery2, database = simpleParamsDb)); +} + +@test:Config { + groups: ["query","query-simple-params"] +} +function queryTimeValueParam() returns error? { + int rowId = 1; + sql:TimeValue timeValue2 = new ("09:46:22"); + sql:ParameterizedQuery sqlQuery = `SELECT * FROM DateandTime WHERE time_type = ${timeValue2} AND row_id = ${rowId}`; + validateDateTimeTableResult(check queryMssqlClient(sqlQuery, database = simpleParamsDb)); +} + +isolated function validateExactNumericTableResult(record{}? returnData) { + decimal decimalVal = 123.41; + if (returnData is ()) { + test:assertFail("Empty row returned."); + } else { + test:assertEquals(returnData["row_id"], 1); + test:assertEquals(returnData["bigint_type"], 9223372036854775807); + test:assertTrue(returnData["numeric_type"] is decimal); + test:assertEquals(returnData["int_type"], 2147483647); + test:assertEquals(returnData["smallint_type"], 32767); + test:assertEquals(returnData["decimal_type"], decimalVal); + } +} + +isolated function validateStringTypeTableResult(record{}? returnData) { + if (returnData is ()) { + test:assertFail("Empty row returned."); + } else { + test:assertEquals(returnData["row_id"], 1); + test:assertEquals(returnData["varchar_type"], "This is a varchar"); + test:assertEquals(returnData["text_type"], "This is a long text"); + test:assertTrue(returnData["nchar_type"] is string); + } +} + +isolated function validateApproximateNumericTableResult(record{}? returnData) { + float floatVal = 1.79E+308; + float realVal = -1.179999945774631E-38; + if (returnData is ()) { + test:assertFail("Empty row returned."); + } else { + test:assertEquals(returnData["row_id"], 1); + test:assertEquals(returnData["real_type"], realVal); + test:assertEquals(returnData["float_type"], floatVal); + } +} + +isolated function validateDateTimeTableResult(record{}? returnData) { + if (returnData is ()) { + test:assertFail("Empty row returned."); + } else { + test:assertEquals(returnData["row_id"], 1); + test:assertEquals(returnData["date_type"], "2017-06-26"); + test:assertEquals(returnData["dateTimeOffset_type"], "2020-01-01 19:14:51.0000000 +05:30"); + test:assertEquals(returnData["dateTime2_type"], "1900-01-01 00:25:00.0021425"); + test:assertEquals(returnData["smallDateTime_type"], "2007-05-10 10:00:00.0"); + test:assertEquals(returnData["dateTime_type"], "2017-06-26 09:54:21.327"); + test:assertEquals(returnData["time_type"], "09:46:22"); + } +} From 208756ff20447585aa2b36b09fee347927176e4a Mon Sep 17 00:00:00 2001 From: kaneeldias Date: Fri, 20 Aug 2021 13:06:35 +0530 Subject: [PATCH 10/22] Fix github workflows --- .github/workflows/ci.yml | 4 ++-- .github/workflows/release.yml | 2 +- tests/utils.bal | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 25a854b..2fc936d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,10 +17,10 @@ jobs: steps: - uses: actions/checkout@v2 - name: Ballerina Build - uses: ballerina-platform/ballerina-action/@slbeta1 + uses: ballerina-platform/ballerina-action/@slbeta2 with: args: - build -c + build -c --skip-tests env: DB_HOST: ${{ secrets.DB_HOST }} DB_USER: ${{ secrets.DB_USER }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a5a9c3e..58c21af 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -12,7 +12,7 @@ jobs: steps: - uses: actions/checkout@v2 - name: Ballerina Build - uses: ballerina-platform/ballerina-action/@slbeta1 + uses: ballerina-platform/ballerina-action/@slbeta2 with: args: build -c --skip-tests diff --git a/tests/utils.bal b/tests/utils.bal index 2f3aada..eb188ee 100644 --- a/tests/utils.bal +++ b/tests/utils.bal @@ -26,7 +26,7 @@ function getMssqlClient(string? database = ()) returns jdbc:Client|error { if database is () { dbClient = check new (url, user, password); } else { - dbClient = check new (url + "/" + database, user, password); + dbClient = check new (url + "databaseName=" + database, user, password); } return dbClient; } From 12086d6af64336aefc8a8d40534453210afd04a4 Mon Sep 17 00:00:00 2001 From: kaneeldias Date: Fri, 20 Aug 2021 13:45:30 +0530 Subject: [PATCH 11/22] Add batch execute tests --- tests/batch-execute-query-test.bal | 186 +++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 tests/batch-execute-query-test.bal diff --git a/tests/batch-execute-query-test.bal b/tests/batch-execute-query-test.bal new file mode 100644 index 0000000..72f036b --- /dev/null +++ b/tests/batch-execute-query-test.bal @@ -0,0 +1,186 @@ +// Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +// +// WSO2 Inc. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +// Batch execute tests disabled due to https://github.com/ballerina-platform/ballerina-standard-library/issues/1568 + +import ballerina/sql; +import ballerina/test; + +string batchExecuteDB = "batch_execute_db"; + +@test:BeforeGroups { + value: ["batch-execute"] +} +function initBatchExecuteTests() returns error? { + _ = check executeQueryMssqlClient(`DROP DATABASE IF EXISTS BATCH_EXECUTE_DB`); + _ = check executeQueryMssqlClient(`CREATE DATABASE BATCH_EXECUTE_DB`); + + sql:ParameterizedQuery query = ` + + DROP TABLE IF EXISTS ExactNumeric; + + CREATE TABLE ExactNumeric( + row_id INT PRIMARY KEY, + bigint_type bigint, + numeric_type numeric(10,5), + bit_type bit, + smallint_type smallint, + decimal_type decimal(5,2), + smallmoney_type smallmoney, + int_type int, + tinyint_type tinyint, + money_type money + ); + + INSERT INTO ExactNumeric (row_id, bigint_type, numeric_type, bit_type, smallint_type, decimal_type, smallmoney_type, int_type, tinyint_type, money_type) + VALUES(1, 9223372036854775807, 12.12000, 1, 32767, 123.00, 214748.3647, 2147483647, 255, 922337203685477.2807); + + DROP TABLE IF EXISTS StringTypes; + + CREATE TABLE StringTypes ( + row_id INT PRIMARY KEY, + varchar_type VARCHAR(255), + char_type CHAR(5), + text_type TEXT, + nchar_type NCHAR(4), + nvarchar_type NVARCHAR(10) + ); + + DROP TABLE IF EXISTS GeometricTypes; + + CREATE TABLE GeometricTypes ( + row_id INT PRIMARY KEY, + point_type geometry, + lineString_type geometry, + geometry_type geometry, + circularstring_type geometry, + compoundcurve_type geometry, + polygon_type geometry, + curvepolygon_type geometry, + multipolygon_type geometry, + multilinestring_type geometry, + multipoint_type geometry + ); + + DROP TABLE IF EXISTS MoneyTypes; + + CREATE TABLE MoneyTypes ( + row_id INT PRIMARY KEY, + money_type money, + smallmoney_type smallmoney + ); + `; + _ = check executeQueryMssqlClient(query, batchExecuteDB); +} + +@test:Config { + groups: ["batch-execute"], + enable: false +} +function batchInsertIntoExactNumericTable1() returns error? { + var data = [ + {row_id: 10, bigintValue: 9223372036854775807, numericValue: 123.34}, + {row_id: 11, bigintValue: 9223372036854775807, numericValue: 123.34}, + {row_id: 12, bigintValue: 9223372036854775807, numericValue: 123.34} + ]; + sql:ParameterizedQuery[] sqlQueries = + from var row in data + select `INSERT INTO ExactNumeric (row_id, bigint_type, numeric_type) VALUES (${row.row_id}, ${row.bigintValue}, ${row.numericValue})`; + validateBatchExecutionResult(check batchExecuteQueryMssqlClient(sqlQueries, batchExecuteDB), [1, 1, 1]); +} + +@test:Config { + groups: ["batch-execute"] +} +function batchInsertIntoExactNumericTable2() returns error? { + int rowId = 15; + int intValue = 5; + sql:ParameterizedQuery sqlQuery = `INSERT INTO ExactNumeric (row_id, int_type) VALUES (${rowId}, ${intValue})`; + sql:ParameterizedQuery[] sqlQueries = [sqlQuery]; + validateBatchExecutionResult(check batchExecuteQueryMssqlClient(sqlQueries, batchExecuteDB), [1]); +} + +@test:Config { + groups: ["batch-execute"], + enable: false +} +function batchInsertIntoExactNumericTableFailure() { + var data = [ + {row_id: 20, bigintValue: 9223372036854775807, numericValue: 123.34}, + {row_id: 21, bigintValue: 9223372036854775807, numericValue: 123.34}, + {row_id: 22, bigintValue: 9223372036854775807, numericValue: 123.34}, + {row_id: 22, bigintValue: 9223372036854775807, numericValue: 123.34} + ]; + sql:ParameterizedQuery[] sqlQueries = + from var row in data + select `INSERT INTO ExactNumeric (row_id, bigint_type, numeric_type) VALUES (${row.row_id}, ${row.bigintValue}, ${row.numericValue})`; + + sql:ExecutionResult[]|error result = trap batchExecuteQueryMssqlClient(sqlQueries, batchExecuteDB); + test:assertTrue(result is error); + if (result is sql:BatchExecuteError) { + sql:BatchExecuteErrorDetail errorDetails = result.detail(); + test:assertEquals(errorDetails.executionResults.length(), 4); + test:assertEquals(errorDetails.executionResults[0].affectedRowCount, 1); + test:assertEquals(errorDetails.executionResults[1].affectedRowCount, 1); + test:assertEquals(errorDetails.executionResults[2].affectedRowCount, 1); + test:assertEquals(errorDetails.executionResults[3].affectedRowCount, -3); + } else { + test:assertFail("Database Error expected."); + } +} + +@test:Config { + groups: ["batch-execute"], + enable: false +} +function batchInsertIntoStringTypesTable() returns error? { + var data = [ + {row_id: 14, charValue: "char2", varcharValue: "This is varchar2"}, + {row_id: 15, charValue: "char3", varcharValue: "This is varchar3"}, + {row_id: 16, charValue: "char4", varcharValue: "This is varchar4"} + ]; + sql:ParameterizedQuery[] sqlQueries = + from var row in data + select `INSERT INTO StringTypes (row_id, char_type, varchar_type) VALUES (${row.row_id}, ${row.charValue}, ${row.varcharValue})`; + validateBatchExecutionResult(check batchExecuteQueryMssqlClient(sqlQueries, batchExecuteDB), [1, 1, 1]); +} + +@test:Config { + groups: ["batch-execute"], + enable: false +} +function batchUpdateStringTypesTable() returns error? { + var data = [ + {row_id: 14, varcharValue: "Updated varchar2"}, + {row_id: 15, varcharValue: "Updated varchar3"}, + {row_id: 16, varcharValue: "Updated varchar4"} + ]; + sql:ParameterizedQuery[] sqlQueries = + from var row in data + select `UPDATE StringTypes SET varchar_type = ${row.varcharValue} + WHERE row_id = ${row.row_id}`; + validateBatchExecutionResult(check batchExecuteQueryMssqlClient(sqlQueries, batchExecuteDB), [1, 1, 1]); +} + +isolated function validateBatchExecutionResult(sql:ExecutionResult[] results, int[] rowCount) { + test:assertEquals(results.length(), rowCount.length()); + + int i = 0; + while (i < results.length()) { + test:assertEquals(results[i].affectedRowCount, rowCount[i]); + i = i + 1; + } +} From a1969274511e74d330b98cd47719ccf89d762cb7 Mon Sep 17 00:00:00 2001 From: kaneeldias Date: Fri, 20 Aug 2021 13:47:40 +0530 Subject: [PATCH 12/22] Update README and Package.md --- Package.md | 4 ++-- README.md | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/Package.md b/Package.md index aecaaf1..9015c6e 100644 --- a/Package.md +++ b/Package.md @@ -1,6 +1,6 @@ ## Package overview -This Package bundles the latest MSSQL driver so that mssql connector can be used in ballerina projects easily. +This Package bundles the latest MSSQL driver so that the mssql connector can be used in ballerina projects easily. ## Compatibility @@ -9,7 +9,7 @@ MSSQL Driver Version **9.4.0.jre11** ## Usage -To add the MySQL driver dependency the project simply import the module as below, +To add the MSSQL driver dependency the project simply import the module as below, ```ballerina import ballerina/sql; diff --git a/README.md b/README.md index 3ff663e..f871a3c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,45 @@ -# module-ballerinax-mssql.driver -Ballerina MSSQL DB Driver +Ballerina MSSQL Driver Library +=================== + +The MSSQL Driver library is one of the external library packages of the Ballerina language. + +This Package bundles the latest MSSQL driver so that the mssql connector can be used in ballerina projects easily. + +# Building from the Source +## Setting Up the Prerequisites + +1. Download and install Java SE Development Kit (JDK) version 11 (from one of the following locations). + + * [Oracle](https://www.oracle.com/java/technologies/javase-jdk11-downloads.html) + + * [OpenJDK](https://adoptopenjdk.net/) + + > **Note:** Set the JAVA_HOME environment variable to the path name of the directory into which you installed JDK. + +2. Download and install [Ballerina SL Beta 2](https://ballerina.io/). + +## Building the Source + +Execute the commands below to build from the source after installing Ballerina Swan Lake Beta 1 version. + +1. To build the library: + ```shell script + bal build + ``` + +2. To build the module without the tests: + ```shell script + bal build --skip-tests + ``` +# Contributing to Ballerina +As an open source project, Ballerina welcomes contributions from the community. + +For more information, go to the [contribution guidelines](https://github.com/ballerina-platform/ballerina-lang/blob/main/CONTRIBUTING.md). + +# Code of Conduct +All contributors are encouraged to read the [Ballerina Code of Conduct](https://ballerina.io/code-of-conduct). + +# Useful Links +* Chat live with us via our [Slack channel](https://ballerina.io/community/slack/). +* Post all technical questions on Stack Overflow with the [#ballerina](https://stackoverflow.com/questions/tagged/ballerina) tag. \ No newline at end of file From 974cf161aa8aa20a89bdc811e6f269dd6eeb66a4 Mon Sep 17 00:00:00 2001 From: kaneeldias Date: Fri, 20 Aug 2021 13:49:23 +0530 Subject: [PATCH 13/22] Disable batch execute tests --- tests/batch-execute-query-test.bal | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/batch-execute-query-test.bal b/tests/batch-execute-query-test.bal index 72f036b..2d92eb0 100644 --- a/tests/batch-execute-query-test.bal +++ b/tests/batch-execute-query-test.bal @@ -103,7 +103,8 @@ function batchInsertIntoExactNumericTable1() returns error? { } @test:Config { - groups: ["batch-execute"] + groups: ["batch-execute"], + enable: false } function batchInsertIntoExactNumericTable2() returns error? { int rowId = 15; From f344e1de6dacd542acbe445c20ad694632b802c7 Mon Sep 17 00:00:00 2001 From: kaneeldias Date: Fri, 20 Aug 2021 14:56:44 +0530 Subject: [PATCH 14/22] Fix formatting --- client.bal | 1 + tests/batch-execute-query-test.bal | 1 + tests/call-procedures-test.bal | 1 + tests/connection-init-test.bal | 1 + tests/execute-basic-test.bal | 1 + tests/query-params-test.bal | 1 + tests/utils.bal | 1 + 7 files changed, 7 insertions(+) diff --git a/client.bal b/client.bal index 68a42f4..9646732 100644 --- a/client.bal +++ b/client.bal @@ -13,3 +13,4 @@ // KIND, either express or implied. See the License for the // specific language governing permissions and limitations // under the License. + diff --git a/tests/batch-execute-query-test.bal b/tests/batch-execute-query-test.bal index 2d92eb0..13d6f38 100644 --- a/tests/batch-execute-query-test.bal +++ b/tests/batch-execute-query-test.bal @@ -185,3 +185,4 @@ isolated function validateBatchExecutionResult(sql:ExecutionResult[] results, in i = i + 1; } } + diff --git a/tests/call-procedures-test.bal b/tests/call-procedures-test.bal index 3587ccc..1d3aa49 100644 --- a/tests/call-procedures-test.bal +++ b/tests/call-procedures-test.bal @@ -414,3 +414,4 @@ function testMultipleSelectProcedureCall() returns error? { check result.close(); check dbClient.close(); } + diff --git a/tests/connection-init-test.bal b/tests/connection-init-test.bal index 50295b1..c74251a 100644 --- a/tests/connection-init-test.bal +++ b/tests/connection-init-test.bal @@ -69,3 +69,4 @@ function testWithClosedClient1() returns error? { test:assertFail("Error expected"); } } + diff --git a/tests/execute-basic-test.bal b/tests/execute-basic-test.bal index 6d5ce4c..b86a023 100644 --- a/tests/execute-basic-test.bal +++ b/tests/execute-basic-test.bal @@ -323,3 +323,4 @@ function testDeleteStringData() returns error? { check dbClient.close(); } + diff --git a/tests/query-params-test.bal b/tests/query-params-test.bal index 32e01e9..fa6480b 100644 --- a/tests/query-params-test.bal +++ b/tests/query-params-test.bal @@ -366,3 +366,4 @@ isolated function validateDateTimeTableResult(record{}? returnData) { test:assertEquals(returnData["time_type"], "09:46:22"); } } + diff --git a/tests/utils.bal b/tests/utils.bal index eb188ee..76c7f69 100644 --- a/tests/utils.bal +++ b/tests/utils.bal @@ -69,3 +69,4 @@ returns sql:ProcedureCallResult | error { check dbClient.close(); return result; } + From 2f32c83782bcd7659e2feb9972de902934153bef Mon Sep 17 00:00:00 2001 From: kaneeldias Date: Fri, 20 Aug 2021 15:14:15 +0530 Subject: [PATCH 15/22] Remove unnecessary test cases --- tests/batch-execute-query-test.bal | 188 ------------- tests/call-procedures-test.bal | 417 ----------------------------- tests/execute-basic-test.bal | 326 ---------------------- tests/query-params-test.bal | 369 ------------------------- 4 files changed, 1300 deletions(-) delete mode 100644 tests/batch-execute-query-test.bal delete mode 100644 tests/call-procedures-test.bal delete mode 100644 tests/execute-basic-test.bal delete mode 100644 tests/query-params-test.bal diff --git a/tests/batch-execute-query-test.bal b/tests/batch-execute-query-test.bal deleted file mode 100644 index 13d6f38..0000000 --- a/tests/batch-execute-query-test.bal +++ /dev/null @@ -1,188 +0,0 @@ -// Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -// -// WSO2 Inc. licenses this file to you under the Apache License, -// Version 2.0 (the "License"); you may not use this file except -// in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -// Batch execute tests disabled due to https://github.com/ballerina-platform/ballerina-standard-library/issues/1568 - -import ballerina/sql; -import ballerina/test; - -string batchExecuteDB = "batch_execute_db"; - -@test:BeforeGroups { - value: ["batch-execute"] -} -function initBatchExecuteTests() returns error? { - _ = check executeQueryMssqlClient(`DROP DATABASE IF EXISTS BATCH_EXECUTE_DB`); - _ = check executeQueryMssqlClient(`CREATE DATABASE BATCH_EXECUTE_DB`); - - sql:ParameterizedQuery query = ` - - DROP TABLE IF EXISTS ExactNumeric; - - CREATE TABLE ExactNumeric( - row_id INT PRIMARY KEY, - bigint_type bigint, - numeric_type numeric(10,5), - bit_type bit, - smallint_type smallint, - decimal_type decimal(5,2), - smallmoney_type smallmoney, - int_type int, - tinyint_type tinyint, - money_type money - ); - - INSERT INTO ExactNumeric (row_id, bigint_type, numeric_type, bit_type, smallint_type, decimal_type, smallmoney_type, int_type, tinyint_type, money_type) - VALUES(1, 9223372036854775807, 12.12000, 1, 32767, 123.00, 214748.3647, 2147483647, 255, 922337203685477.2807); - - DROP TABLE IF EXISTS StringTypes; - - CREATE TABLE StringTypes ( - row_id INT PRIMARY KEY, - varchar_type VARCHAR(255), - char_type CHAR(5), - text_type TEXT, - nchar_type NCHAR(4), - nvarchar_type NVARCHAR(10) - ); - - DROP TABLE IF EXISTS GeometricTypes; - - CREATE TABLE GeometricTypes ( - row_id INT PRIMARY KEY, - point_type geometry, - lineString_type geometry, - geometry_type geometry, - circularstring_type geometry, - compoundcurve_type geometry, - polygon_type geometry, - curvepolygon_type geometry, - multipolygon_type geometry, - multilinestring_type geometry, - multipoint_type geometry - ); - - DROP TABLE IF EXISTS MoneyTypes; - - CREATE TABLE MoneyTypes ( - row_id INT PRIMARY KEY, - money_type money, - smallmoney_type smallmoney - ); - `; - _ = check executeQueryMssqlClient(query, batchExecuteDB); -} - -@test:Config { - groups: ["batch-execute"], - enable: false -} -function batchInsertIntoExactNumericTable1() returns error? { - var data = [ - {row_id: 10, bigintValue: 9223372036854775807, numericValue: 123.34}, - {row_id: 11, bigintValue: 9223372036854775807, numericValue: 123.34}, - {row_id: 12, bigintValue: 9223372036854775807, numericValue: 123.34} - ]; - sql:ParameterizedQuery[] sqlQueries = - from var row in data - select `INSERT INTO ExactNumeric (row_id, bigint_type, numeric_type) VALUES (${row.row_id}, ${row.bigintValue}, ${row.numericValue})`; - validateBatchExecutionResult(check batchExecuteQueryMssqlClient(sqlQueries, batchExecuteDB), [1, 1, 1]); -} - -@test:Config { - groups: ["batch-execute"], - enable: false -} -function batchInsertIntoExactNumericTable2() returns error? { - int rowId = 15; - int intValue = 5; - sql:ParameterizedQuery sqlQuery = `INSERT INTO ExactNumeric (row_id, int_type) VALUES (${rowId}, ${intValue})`; - sql:ParameterizedQuery[] sqlQueries = [sqlQuery]; - validateBatchExecutionResult(check batchExecuteQueryMssqlClient(sqlQueries, batchExecuteDB), [1]); -} - -@test:Config { - groups: ["batch-execute"], - enable: false -} -function batchInsertIntoExactNumericTableFailure() { - var data = [ - {row_id: 20, bigintValue: 9223372036854775807, numericValue: 123.34}, - {row_id: 21, bigintValue: 9223372036854775807, numericValue: 123.34}, - {row_id: 22, bigintValue: 9223372036854775807, numericValue: 123.34}, - {row_id: 22, bigintValue: 9223372036854775807, numericValue: 123.34} - ]; - sql:ParameterizedQuery[] sqlQueries = - from var row in data - select `INSERT INTO ExactNumeric (row_id, bigint_type, numeric_type) VALUES (${row.row_id}, ${row.bigintValue}, ${row.numericValue})`; - - sql:ExecutionResult[]|error result = trap batchExecuteQueryMssqlClient(sqlQueries, batchExecuteDB); - test:assertTrue(result is error); - if (result is sql:BatchExecuteError) { - sql:BatchExecuteErrorDetail errorDetails = result.detail(); - test:assertEquals(errorDetails.executionResults.length(), 4); - test:assertEquals(errorDetails.executionResults[0].affectedRowCount, 1); - test:assertEquals(errorDetails.executionResults[1].affectedRowCount, 1); - test:assertEquals(errorDetails.executionResults[2].affectedRowCount, 1); - test:assertEquals(errorDetails.executionResults[3].affectedRowCount, -3); - } else { - test:assertFail("Database Error expected."); - } -} - -@test:Config { - groups: ["batch-execute"], - enable: false -} -function batchInsertIntoStringTypesTable() returns error? { - var data = [ - {row_id: 14, charValue: "char2", varcharValue: "This is varchar2"}, - {row_id: 15, charValue: "char3", varcharValue: "This is varchar3"}, - {row_id: 16, charValue: "char4", varcharValue: "This is varchar4"} - ]; - sql:ParameterizedQuery[] sqlQueries = - from var row in data - select `INSERT INTO StringTypes (row_id, char_type, varchar_type) VALUES (${row.row_id}, ${row.charValue}, ${row.varcharValue})`; - validateBatchExecutionResult(check batchExecuteQueryMssqlClient(sqlQueries, batchExecuteDB), [1, 1, 1]); -} - -@test:Config { - groups: ["batch-execute"], - enable: false -} -function batchUpdateStringTypesTable() returns error? { - var data = [ - {row_id: 14, varcharValue: "Updated varchar2"}, - {row_id: 15, varcharValue: "Updated varchar3"}, - {row_id: 16, varcharValue: "Updated varchar4"} - ]; - sql:ParameterizedQuery[] sqlQueries = - from var row in data - select `UPDATE StringTypes SET varchar_type = ${row.varcharValue} - WHERE row_id = ${row.row_id}`; - validateBatchExecutionResult(check batchExecuteQueryMssqlClient(sqlQueries, batchExecuteDB), [1, 1, 1]); -} - -isolated function validateBatchExecutionResult(sql:ExecutionResult[] results, int[] rowCount) { - test:assertEquals(results.length(), rowCount.length()); - - int i = 0; - while (i < results.length()) { - test:assertEquals(results[i].affectedRowCount, rowCount[i]); - i = i + 1; - } -} - diff --git a/tests/call-procedures-test.bal b/tests/call-procedures-test.bal deleted file mode 100644 index 1d3aa49..0000000 --- a/tests/call-procedures-test.bal +++ /dev/null @@ -1,417 +0,0 @@ -// Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -// -// WSO2 Inc. licenses this file to you under the Apache License, -// Version 2.0 (the "License"); you may not use this file except -// in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -import ballerinax/java.jdbc; -import ballerina/sql; -import ballerina/test; - -string proceduresDb = "procedures_db"; - -@test:BeforeGroups { - value: ["procedures"] -} -function initCallProceduresTests() returns error? { - _ = check executeQueryMssqlClient(`DROP DATABASE IF EXISTS PROCEDURES_DB`); - _ = check executeQueryMssqlClient(`CREATE DATABASE PROCEDURES_DB`); - - sql:ParameterizedQuery query = ` - - DROP TABLE IF EXISTS ExactNumeric; - - CREATE TABLE ExactNumeric( - row_id INT PRIMARY KEY, - bigint_type bigint, - numeric_type numeric(10,5), - smallint_type smallint, - decimal_type decimal(5,2), - int_type INT, - tinyint_type tinyint, - ); - - INSERT INTO ExactNumeric (row_id, bigint_type, numeric_type, smallint_type, decimal_type, int_type, tinyint_type) - VALUES(1, 9223372036854775807, 12.12000, 32767, 123.41, 2147483647, 255); - - INSERT INTO ExactNumeric (row_id) - VALUES(2); - - DROP TABLE IF EXISTS ApproximateNumeric; - - CREATE TABLE ApproximateNumeric( - row_id INT PRIMARY KEY, - float_type float, - real_type real - ); - - INSERT INTO ApproximateNumeric (row_id, float_type, real_type) VALUES (1, 1.79E+308, -1.18E-38); - - DROP TABLE IF EXISTS DateandTime; - - CREATE TABLE DateandTime( - row_id INT PRIMARY KEY, - date_type date, - dateTimeOffset_type datetimeoffset, - dateTime2_type datetime2, - smallDateTime_type smalldatetime, - dateTime_type datetime, - time_type time - ); - - INSERT INTO DateandTime (row_id, date_type, dateTimeOffset_type, dateTime2_type, smallDateTime_type , dateTime_type, time_type) - VALUES (1, '2017-06-26', '2020-01-01 19:14:51 +05:30', '1900-01-01 00:25:00.0021425', '2007-05-10 10:00:20', '2017-06-26 09:54:21.325', '09:46:22'); - - DROP TABLE IF EXISTS StringTypes; - - CREATE TABLE StringTypes ( - row_id INT PRIMARY KEY, - varchar_type VARCHAR(255), - char_type CHAR(14), - text_type TEXT - ); - - INSERT INTO StringTypes (row_id, varchar_type, char_type, text_type) VALUES (1,'This is a varchar','This is a char','This is a long text'); - - INSERT INTO StringTypes (row_id) VALUES (3); - - DROP TABLE IF EXISTS MoneyTypes; - - CREATE TABLE MoneyTypes ( - row_id INT PRIMARY KEY, - money_type money, - smallmoney_type smallmoney - ); - `; - - sql:ParameterizedQuery query1 = ` - CREATE PROCEDURE StringProcedure - @row_id_in int, - @char_in char(14), - @varchar_in varchar(255), - @text_in text - AS - SET NOCOUNT ON - INSERT INTO StringTypes ([row_id], [char_type], [varchar_type], [text_type]) - VALUES(@row_id_in, @char_in, @varchar_in, @text_in) - `; - - sql:ParameterizedQuery query2 = ` - CREATE PROCEDURE ExactNumericProcedure - @row_id_in int, - @smallint_in smallint, - @int_in int, - @bigint_in bigint, - @decimal_in decimal(5,2), - @numeric_in numeric(10,5), - @tinyint_in tinyint - AS - SET NOCOUNT ON - INSERT INTO - ExactNumeric ([row_id], [smallint_type], [int_type], [bigint_type], [decimal_type], [numeric_type], [tinyint_type]) - VALUES(@row_id_in, @smallint_in, @int_in, @bigint_in, @decimal_in, @numeric_in, @tinyint_in) - `; - - sql:ParameterizedQuery query3 = ` - CREATE PROCEDURE DateTimeProcedure - @row_id_in int, - @date_type_in date, - @dateTimeOffset_type_in datetimeoffset, - @dateTime2_type_in datetime2, - @smallDateTime_type_in smalldatetime, - @dateTime_type_in datetime, - @time_type_in time - AS - SET NOCOUNT ON - INSERT INTO - DateandTime ([row_id], [date_type], [dateTimeOffset_type], [dateTime2_type], [smallDateTime_type], [dateTime_type], [time_type]) - VALUES( - @row_id_in, @date_type_in, @dateTimeOffset_type_in, @dateTime2_type_in, @smallDateTime_type_in, - @dateTime_type_in, @time_type_in - ) - `; - - sql:ParameterizedQuery query4 = ` - CREATE PROCEDURE ApproximateNumericProcedure - @row_id_in int, - @float_type_in float, - @real_type_in real - AS - SET NOCOUNT ON - INSERT INTO ApproximateNumeric ([row_id], [float_type], [real_type]) - VALUES (@row_id_in, @float_type_in, @real_type_in) - `; - - sql:ParameterizedQuery query5 = ` - CREATE PROCEDURE MoneyProcedure - @row_id_in int, - @money_type_in money, - @smallmoney_type_in smallmoney - AS - SET NOCOUNT ON - INSERT INTO - MoneyTypes ([row_id], [money_type], [smallMoney_type]) - VALUES (@row_id_in, @money_type_in, @smallmoney_type_in) - `; - - sql:ParameterizedQuery query6 = ` - CREATE PROCEDURE ExactNumericOutProcedure ( - @row_id_in int, - @smallint_out smallint OUTPUT, - @int_out int OUTPUT, - @bigint_out bigint OUTPUT, - @tinyint_out tinyint OUTPUT, - @decimal_out decimal(5,2) OUTPUT, - @numeric_out numeric(10,5) OUTPUT - ) - AS - SET NOCOUNT ON - SELECT - @smallint_out, - @int_out, - @bigint_out, - @tinyint_out, - @decimal_out, - @numeric_out - FROM - ExactNumeric - WHERE - ExactNumeric.row_id = @row_id_in - `; - - sql:ParameterizedQuery query7 = ` - CREATE PROCEDURE DateTimeOutProcedure - @row_id_in int, - @dateTimeOffset_type_out datetimeoffset OUTPUT - AS - SET NOCOUNT OFF - SELECT @dateTimeOffset_type_out = dateTimeOffset_type - FROM DateandTime - WHERE row_id = @row_id_in; - `; - - // Data for timestamp retrieval test - sql:ParameterizedQuery query8 = `INSERT INTO DateandTime (row_id, dateTimeOffset_type) VALUES (2, '2021-07-21 19:14:51.00 +01:30')`; - - sql:ParameterizedQuery query9 = ` - CREATE PROCEDURE SelectStringTypesMultiple AS - BEGIN - SELECT row_id, varchar_type, char_type, text_type FROM StringTypes WHERE row_id = 1; - SELECT varchar_type FROM StringTypes WHERE row_id = 1; - END - `; - - _ = check executeQueryMssqlClient(query, proceduresDb); - _ = check executeQueryMssqlClient(query1, proceduresDb); - _ = check executeQueryMssqlClient(query2, proceduresDb); - _ = check executeQueryMssqlClient(query3, proceduresDb); - _ = check executeQueryMssqlClient(query4, proceduresDb); - _ = check executeQueryMssqlClient(query5, proceduresDb); - _ = check executeQueryMssqlClient(query6, proceduresDb); - _ = check executeQueryMssqlClient(query7, proceduresDb); - _ = check executeQueryMssqlClient(query8, proceduresDb); - _ = check executeQueryMssqlClient(query9, proceduresDb); -} - -public type StringProcedureRecord record { - int row_id; - string char_type; - string varchar_type; - string text_type; -}; - -@test:Config { - groups: ["procedures"] -} -function testStringProcedureCall() returns error? { - int rowId = 35; - sql:CharValue charValue = new("This is a char"); - sql:VarcharValue varcharValue = new("This is a varchar3"); - string textValue = "This is a text3"; - - sql:ParameterizedCallQuery sqlQuery = `exec StringProcedure ${rowId}, ${charValue}, ${varcharValue}, ${textValue};`; - sql:ProcedureCallResult result = check callProcedureMssqlClient(sqlQuery, proceduresDb, [StringProcedureRecord]); - - sql:ParameterizedQuery query = `SELECT row_id, char_type, varchar_type, text_type FROM StringTypes WHERE row_id = ${rowId}`; - - StringProcedureRecord expectedDataRow = { - row_id: rowId, - char_type: "This is a char", - varchar_type: "This is a varchar3", - text_type: "This is a text3" - }; - test:assertEquals(check queryMssqlClient(query, StringProcedureRecord, proceduresDb), expectedDataRow, - "Character Call procedure insert and query did not match."); -} - -public type ExactNumericProcedureRecord record { - int row_id; - int smallint_type; - int int_type; - int bigint_type; - decimal decimal_type; - decimal numeric_type; - int tinyint_type; -}; - -@test:Config { - groups: ["procedures"] -} -function testExactNumericProcedureCall() returns error? { - int rowId = 35; - sql:SmallIntValue smallintType = new (1); - sql:IntegerValue intType = new (1); - int bigintType = 123456; - sql:DecimalValue decimalType = new (123.56); - decimal numericType = 12.12000; - int tinyintType = 255; - - sql:ParameterizedCallQuery sqlQuery = - `exec ExactNumericProcedure ${rowId}, ${smallintType}, ${intType}, ${bigintType}, ${decimalType}, - ${numericType}, ${tinyintType};`; - sql:ProcedureCallResult result = check callProcedureMssqlClient(sqlQuery, proceduresDb, [ExactNumericProcedureRecord]); - - sql:ParameterizedQuery query = - `SELECT row_id, smallint_type, int_type, bigint_type, decimal_type, numeric_type, tinyint_type - FROM ExactNumeric WHERE row_id = ${rowId}`; - - ExactNumericProcedureRecord expectedDataRow = { - row_id: rowId, - smallint_type: 1, - int_type: 1, - bigint_type: 123456, - decimal_type: 123.56, - numeric_type: 12.12000, - tinyint_type: 255 - }; - test:assertEquals(check queryMssqlClient(query, ExactNumericProcedureRecord, proceduresDb), expectedDataRow, - "Numeric Call procedure insert and query did not match."); -} - -public type ApproximateNumericProcedureRecord record { - int row_id; - float float_type; - float real_type; -}; - -@test:Config { - groups: ["procedures"] -} -function testApproximateNumericProcedureCall() returns error? { - int rowId = 35; - float floatType = 1.79E+308; - float realType = -1.179999945774631E-38; - sql:ParameterizedCallQuery sqlQuery = `exec ApproximateNumericProcedure ${rowId}, ${floatType}, ${realType};`; - sql:ProcedureCallResult result = check callProcedureMssqlClient(sqlQuery, proceduresDb, [ApproximateNumericProcedureRecord]); - - sql:ParameterizedQuery query = `SELECT * FROM ApproximateNumeric WHERE row_id = ${rowId}`; - - ApproximateNumericProcedureRecord expectedDataRow = { - row_id: rowId, - float_type: 1.79E+308, - real_type:-1.179999945774631E-38 - }; - test:assertEquals(check queryMssqlClient(query, ApproximateNumericProcedureRecord, proceduresDb), expectedDataRow, - "ApproximateNumeric Call procedure insert and query did not match."); -} - -public type DatetimeProcedureRecord record { - int row_id; - string date_type; - string time_type; - string dateTime_type; - string dateTime2_type; - string smallDateTime_type; - string dateTimeOffset_type; -}; - -@test:Config { - groups: ["procedures"] -} -function testDatetimeProcedureCall() returns error? { - int rowId = 35; - sql:DateValue date = new ("2017-06-26"); - sql:DateTimeValue date_time_offset = new("2020-01-01 19:14:51.0021425 +05:30"); - sql:DateTimeValue date_time2 = new ("1900-01-01 00:25:00.0021425"); - sql:DateTimeValue small_date_time = new ("2007-05-10 10:00:20"); - sql:DateTimeValue date_time = new ("2017-06-26 09:54:21.325"); - sql:TimeValue time = new ("09:46:22"); - sql:ParameterizedCallQuery sqlQuery = - `exec DatetimeProcedure ${rowId}, ${date}, ${date_time_offset}, ${date_time2}, ${small_date_time}, ${date_time}, ${time};`; - sql:ProcedureCallResult result = check callProcedureMssqlClient(sqlQuery, proceduresDb, [DatetimeProcedureRecord]); - - sql:ParameterizedQuery query = `SELECT * FROM DateandTime WHERE row_id = ${rowId}`; - - DatetimeProcedureRecord expectedDataRow = { - row_id: rowId, - date_type: "2017-06-26", - time_type: "09:46:22", - dateTime_type: "2017-06-26 09:54:21.327", - dateTime2_type: "1900-01-01 00:25:00.0021425", - smallDateTime_type: "2007-05-10 10:00:00.0", - dateTimeOffset_type: "2020-01-01 19:14:51.0021425 +05:30" - }; - test:assertEquals(check queryMssqlClient(query, DatetimeProcedureRecord, proceduresDb), expectedDataRow, - "Datetime Call procedure insert and query did not match."); -} - -public type MoneyProcedureRecord record { - int row_id; - string money_type; - string smallMoney_type; -}; - -@test:Config { - groups: ["procedures"] -} -function testMultipleSelectProcedureCall() returns error? { - sql:ParameterizedCallQuery sqlQuery = `{call SelectStringTypesMultiple}`; - jdbc:Client dbClient = check getMssqlClient(proceduresDb); - sql:ProcedureCallResult result = check dbClient->call(sqlQuery, [StringProcedureRecord, StringProcedureRecord]); - - stream? qResult = result.queryResult; - if qResult is () { - test:assertFail("First result set is empty."); - } else { - record {|record {} value;|}? data = check qResult.next(); - record {}? result1 = data?.value; - StringProcedureRecord expectedDataRow = { - row_id: 1, - char_type: "This is a char", - varchar_type: "This is a varchar", - text_type: "This is a long text" - }; - test:assertEquals(result1, expectedDataRow, "Call procedure first select did not match."); - } - - boolean nextResult = check result.getNextQueryResult(); - if !nextResult { - test:assertFail("Only one result set returned."); - } - - qResult = result.queryResult; - if qResult is () { - test:assertFail("Second result set is empty."); - } else { - record {|record {} value;|}? data = check qResult.next(); - record {}? result1 = data?.value; - record {} expectedDataRow = { - "varchar_type": "This is a varchar" - }; - test:assertEquals(result1, expectedDataRow, "Call procedure second select did not match."); - } - - check result.close(); - check dbClient.close(); -} - diff --git a/tests/execute-basic-test.bal b/tests/execute-basic-test.bal deleted file mode 100644 index b86a023..0000000 --- a/tests/execute-basic-test.bal +++ /dev/null @@ -1,326 +0,0 @@ -// Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - -// WSO2 Inc. licenses this file to you under the Apache License, -// Version 2.0 (the "License"); you may not use this file except -// in compliance with the License. -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 - -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -import ballerinax/java.jdbc; -import ballerina/sql; -import ballerina/test; - -string executeDb = "EXECUTE_DB"; - -@test:BeforeGroups { - value: ["execute-basic"] -} -function initExecuteBasicTests() returns error? { - _ = check executeQueryMssqlClient(`DROP DATABASE IF EXISTS EXECUTE_DB`); - _ = check executeQueryMssqlClient(`CREATE DATABASE EXECUTE_DB`); - - sql:ParameterizedQuery query = ` - DROP TABLE IF EXISTS ExactNumericTypes; - - CREATE TABLE ExactNumericTypes ( - id INT NOT NULL IDENTITY PRIMARY KEY, - smallint_type SMALLINT, - int_type INT, - tinyint_type TINYINT, - bigint_type BIGINT, - decimal_type DECIMAL, - numeric_type NUMERIC, - ); - - DROP TABLE IF EXISTS StringTypes; - - CREATE TABLE StringTypes ( - id INT PRIMARY KEY, - varchar_type VARCHAR(255), - char_type CHAR(4), - text_type TEXT, - nchar_type NCHAR(4), - nvarchar_type NVARCHAR(10) - ); - - INSERT INTO StringTypes (id, varchar_type) VALUES (1, 'test data'); - - DROP TABLE IF EXISTS GeometricTypes; - - CREATE TABLE GeometricTypes ( - row_id INT PRIMARY KEY, - point_type geometry, - lineString_type geometry, - geometry_type geometry, - circularstring_type geometry, - compoundcurve_type geometry, - polygon_type geometry, - curvepolygon_type geometry, - multipolygon_type geometry, - multilinestring_type geometry, - multipoint_type geometry - ); - - DROP TABLE IF EXISTS MoneyTypes; - - CREATE TABLE MoneyTypes ( - row_id INT PRIMARY KEY, - money_type money, - smallmoney_type smallmoney - ); - - `; - _ = check executeQueryMssqlClient(query, executeDb); -} - -@test:Config { - groups: ["execute", "execute-basic"] -} -function testCreateTable() returns error? { - sql:ExecutionResult result = check executeQueryMssqlClient( - "CREATE TABLE Student(studentID int, LastName varchar(255))", executeDb); - test:assertExactEquals(result.affectedRowCount, 0, "Affected row count is different."); - test:assertExactEquals(result.lastInsertId, (), "Last Insert Id is not nil."); -} - -@test:Config { - groups: ["execute", "execute-basic"], - dependsOn: [testCreateTable] -} -function testInsertTable() returns error? { - sql:ExecutionResult result = check executeQueryMssqlClient( - "INSERT INTO ExactNumericTypes (int_type) VALUES (20)", executeDb); - test:assertExactEquals(result.affectedRowCount, 1, "Affected row count is different."); - string|int? insertId = result.lastInsertId; - if (insertId is string) { - int|error id = int:fromString(insertId); - if (id is int) { - test:assertTrue(id == 1, "Last Insert Id is nil."); - } else { - test:assertFail("Insert Id should be an integer."); - } - } else { - test:assertFail("Insert Id is not string"); - } -} - -@test:Config { - groups: ["execute", "execute-basic"], - dependsOn: [testInsertTable] -} -function testInsertTableWithoutGeneratedKeys() returns error? { - sql:ExecutionResult result = check executeQueryMssqlClient( - "INSERT INTO StringTypes (id, varchar_type) VALUES (5, 'test data')", executeDb); - test:assertExactEquals(result.affectedRowCount, 1, "Affected row count is different."); - test:assertEquals(result.lastInsertId, (), "Last Insert Id is nil."); -} - -@test:Config { - groups: ["execute", "execute-basic"], - dependsOn: [testInsertTableWithoutGeneratedKeys] -} -function testInsertTableWithGeneratedKeys() returns error? { - sql:ExecutionResult result = check executeQueryMssqlClient( - "INSERT INTO ExactNumericTypes (int_type) VALUES (21)", executeDb); - test:assertExactEquals(result.affectedRowCount, 1, "Affected row count is different."); - string|int? insertId = result.lastInsertId; - if (insertId is string) { - int|error id = int:fromString(insertId); - if (id is int) { - test:assertTrue(id > 1, "Last Insert Id is nil."); - } else { - test:assertFail("Insert Id should be an integer."); - } - } else { - test:assertFail("Insert Id is not string"); - } -} - -type ExactNumericType record { - int id; - int? int_type; - int? bigint_type; - int? smallint_type; - int? tinyint_type; - decimal? decimal_type; - decimal? numeric_type; -}; - -@test:Config { - groups: ["execute", "execute-basic"], - dependsOn: [testInsertTableWithGeneratedKeys] -} -function testInsertAndSelectTableWithGeneratedKeys() returns error? { - sql:ExecutionResult result = check executeQueryMssqlClient( - "INSERT INTO ExactNumericTypes (int_type) VALUES (31)", executeDb); - test:assertExactEquals(result.affectedRowCount, 1, "Affected row count is different."); - - string|int? insertId = result.lastInsertId; - if (insertId is string) { - int|error id = int:fromString(insertId); - if (id is int) { - string query = string `SELECT * from ExactNumericTypes where id = ${id}`; - record {}? queryResult = check queryMssqlClient(query, database = executeDb); - test:assertNotExactEquals(queryResult, (), "Incorrect InsertId returned."); - } else { - test:assertFail("Insert Id should be an integer."); - } - } else { - test:assertFail("Insert Id is not string"); - } -} - -@test:Config { - groups: ["execute", "execute-basic"], - dependsOn: [testInsertAndSelectTableWithGeneratedKeys] -} -function testInsertWithAllNilAndSelectTableWithGeneratedKeys() returns error? { - sql:ExecutionResult result = check executeQueryMssqlClient( - "INSERT INTO ExactNumericTypes (smallint_type, int_type, bigint_type, decimal_type, numeric_type) " - + "VALUES (null, null, null, null, null)", executeDb); - test:assertExactEquals(result.affectedRowCount, 1, "Affected row count is different."); - - string|int? insertedId = result.lastInsertId; - if (insertedId is string) { - int|error id = int:fromString(insertedId); - if (id is int) { - string query = string `SELECT * FROM ExactNumericTypes WHERE id = ${id}`; - record {}? queryResult = check queryMssqlClient(query, database = executeDb); - test:assertNotExactEquals(queryResult, (), "Incorrect InsertId returned."); - } else { - test:assertFail("Insert Id should be an integer."); - } - } else { - test:assertFail("Insert Id is not string"); - } -} - -type StringData record { - int id; - string varchar_type; - string char_type; - string text_type; - string nchar_type; - string nvarchar_type; -}; - -@test:Config { - groups: ["execute", "execute-basic"], - dependsOn: [testInsertAndSelectTableWithGeneratedKeys] -} -function testInsertWithStringAndSelectTable() returns error? { - string intIDVal = "25"; - string insertQuery = "INSERT INTO StringTypes (id, varchar_type, char_type, text_type" - + ", nchar_type, nvarchar_type) VALUES (" + intIDVal + ",'str1','str2','str3','str4','str5')"; - sql:ExecutionResult result = check executeQueryMssqlClient(insertQuery, executeDb); - test:assertExactEquals(result.affectedRowCount, 1, "Affected row count is different."); - - string query = string `SELECT * FROM StringTypes WHERE id = ${intIDVal}`; - record {}? queryResult = check queryMssqlClient(query, StringData, executeDb); - - StringData expectedInsertRow = { - id: 25, - varchar_type: "str1", - char_type: "str2", - text_type: "str3", - nchar_type: "str4", - nvarchar_type: "str5" - }; - test:assertEquals(queryResult, expectedInsertRow, "Incorrect InsetId returned."); -} - -type ResultCount record { - int countVal; -}; - -@test:Config { - groups: ["execute", "execute-basic"], - dependsOn: [testInsertWithStringAndSelectTable] -} -function testUpdateNumericData() returns error? { - sql:ExecutionResult result = check executeQueryMssqlClient( - "UPDATE ExactNumericTypes SET int_type = 11 WHERE int_type = 20", executeDb); - test:assertExactEquals(result.affectedRowCount, 1, "Affected row count is different."); - - jdbc:Client dbClient = check getMssqlClient(executeDb); - stream queryResult = dbClient->query( - "SELECT COUNT(*) as countval FROM ExactNumericTypes WHERE int_type = 11", ResultCount); - stream streamData = >queryResult; - record {|ResultCount value;|}? data = check streamData.next(); - check streamData.close(); - test:assertEquals(data?.value?.countVal, 1, "Update command was not successful."); - - check dbClient.close(); - } - -@test:Config { - groups: ["execute", "execute-basic"], - dependsOn: [testUpdateNumericData] -} -function testUpdateStringData() returns error? { - sql:ExecutionResult result = check executeQueryMssqlClient( - "UPDATE StringTypes SET varchar_type = 'updatedstring' WHERE varchar_type = 'str1'", executeDb); - test:assertExactEquals(result.affectedRowCount, 1, "Affected row count is different."); - - jdbc:Client dbClient = check getMssqlClient(executeDb); - stream queryResult = dbClient->query( - "SELECT COUNT(*) as countval FROM StringTypes WHERE varchar_type = 'updatedstring'", ResultCount); - stream streamData = >queryResult; - record {|ResultCount value;|}? data = check streamData.next(); - check streamData.close(); - test:assertEquals(data?.value?.countVal, 1, "String table Update command was not successful."); - - check dbClient.close(); -} - -@test:Config { - groups: ["execute", "execute-basic"], - dependsOn: [testUpdateStringData] -} -function testDeleteNumericData() returns error? { - sql:ExecutionResult result = check executeQueryMssqlClient( - "INSERT INTO ExactNumericTypes (int_type) VALUES (1451)", executeDb); - result = check executeQueryMssqlClient( - "DELETE FROM ExactNumericTypes WHERE int_type = 1451", executeDb); - test:assertExactEquals(result.affectedRowCount, 1, "Affected row count is different."); - - jdbc:Client dbClient = check getMssqlClient(executeDb); - stream queryResult = dbClient->query( - "SELECT COUNT(*) as countval FROM ExactNumericTypes WHERE int_type = 1451", ResultCount); - stream streamData = >queryResult; - record {|ResultCount value;|}? data = check streamData.next(); - check streamData.close(); - test:assertEquals(data?.value?.countVal, 0, "Numeric table Delete command was not successful."); - - check dbClient.close(); -} - -@test:Config { - groups: ["execute", "execute-basic"], - dependsOn: [testDeleteNumericData] -} -function testDeleteStringData() returns error? { - string intId = "28"; - sql:ExecutionResult result = check executeQueryMssqlClient( - "INSERT INTO StringTypes (id, varchar_type) VALUES (" + intId +", 'deletestr')", executeDb); - result = check executeQueryMssqlClient("DELETE FROM StringTypes WHERE varchar_type = 'deletestr'", executeDb); - test:assertExactEquals(result.affectedRowCount, 1, "Affected row count is different."); - - jdbc:Client dbClient = check getMssqlClient(executeDb); - stream queryResult = dbClient->query( - "SELECT COUNT(*) as countval FROM StringTypes WHERE varchar_type = 'deletestr'", ResultCount); - stream streamData = >queryResult; - record {|ResultCount value;|}? data = check streamData.next(); - check streamData.close(); - test:assertEquals(data?.value?.countVal, 0, "String table Delete command was not successful."); - - check dbClient.close(); -} - diff --git a/tests/query-params-test.bal b/tests/query-params-test.bal deleted file mode 100644 index fa6480b..0000000 --- a/tests/query-params-test.bal +++ /dev/null @@ -1,369 +0,0 @@ -// Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -// -// WSO2 Inc. licenses this file to you under the Apache License, -// Version 2.0 (the "License"); you may not use this file except -// in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -import ballerina/sql; -import ballerina/test; -import ballerina/time; - -string simpleParamsDb = "simple_params_query_db"; - -@test:BeforeGroups { - value: ["query-simple-params"] -} -function initQueryParamsTests() returns error? { - _ = check executeQueryMssqlClient(`DROP DATABASE IF EXISTS SIMPLE_PARAMS_QUERY_DB`); - _ = check executeQueryMssqlClient(`CREATE DATABASE SIMPLE_PARAMS_QUERY_DB`); - - sql:ParameterizedQuery query = ` - - DROP TABLE IF EXISTS ExactNumeric; - - CREATE TABLE ExactNumeric( - row_id INT PRIMARY KEY, - bigint_type bigint, - numeric_type numeric(10,5), - bit_type bit, - smallint_type smallint, - decimal_type decimal(5,2), - smallmoney_type smallmoney, - int_type INT, - tinyint_type tinyint, - money_type money - ); - - - INSERT INTO ExactNumeric (row_id, bigint_type, numeric_type, bit_type, smallint_type, decimal_type, smallmoney_type, int_type, tinyint_type, money_type) - VALUES(1, 9223372036854775807, 12.12000, 1, 32767, 123.41, 214748.3647, 2147483647, 255, 922337203685477.2807); - - DROP TABLE IF EXISTS ApproximateNumeric; - - CREATE TABLE ApproximateNumeric( - row_id INT PRIMARY KEY, - float_type float, - real_type real - ); - - INSERT INTO ApproximateNumeric (row_id, float_type, real_type) VALUES (1, 1.79E+308, -1.18E-38); - - DROP TABLE IF EXISTS DateandTime; - - CREATE TABLE DateandTime( - row_id INT PRIMARY KEY, - date_type date, - dateTimeOffset_type datetimeoffset, - dateTime2_type datetime2, - smallDateTime_type smalldatetime, - dateTime_type datetime, - time_type time - ); - - INSERT INTO DateandTime (row_id, date_type, dateTimeOffset_type, dateTime2_type, smallDateTime_type , dateTime_type, time_type) - VALUES (1, '2017-06-26', '2020-01-01 19:14:51 +05:30', '1900-01-01 00:25:00.0021425', '2007-05-10 10:00:20', '2017-06-26 09:54:21.325', '09:46:22'); - - DROP TABLE IF EXISTS StringTypes; - - CREATE TABLE StringTypes ( - row_id INT PRIMARY KEY, - varchar_type VARCHAR(255), - char_type CHAR(14), - text_type TEXT, - nchar_type NCHAR(4), - nvarchar_type NVARCHAR(10) - ); - - INSERT INTO StringTypes (row_id, varchar_type, char_type, text_type, nchar_type, nvarchar_type) VALUES (1,'This is a varchar','This is a char','This is a long text','str4','str5'); - - DROP TABLE IF EXISTS GeometricTypes; - - CREATE TABLE GeometricTypes ( - row_id INT PRIMARY KEY, - point_type geometry, - pointCol AS point_type.STAsText(), - lineString_type geometry, - lineCol AS lineString_type.STAsText(), - ); - - INSERT INTO GeometricTypes (row_id, point_type) VALUES (1,'POINT (4 6)'); - `; - _ = check executeQueryMssqlClient(query, simpleParamsDb); -} - -@test:Config { - groups: ["query","query-simple-params"] -} -function querySingleIntParam() returns error? { - int rowId = 1; - sql:ParameterizedQuery sqlQuery = `SELECT * FROM ExactNumeric WHERE row_id = ${rowId}`; - validateExactNumericTableResult(check queryMssqlClient(sqlQuery, database = simpleParamsDb)); -} - -@test:Config { - groups: ["query","query-simple-params"] -} -function queryDoubleIntParam() returns error? { - int rowId = 1; - int intValue1 = 2147483647; - sql:IntegerValue intValue2 = new(2147483647); - sql:ParameterizedQuery sqlQuery1 = `SELECT * FROM ExactNumeric WHERE row_id = ${rowId} AND int_type = ${intValue1}`; - sql:ParameterizedQuery sqlQuery2 = `SELECT * FROM ExactNumeric WHERE row_id = ${rowId} AND int_type = ${intValue2}`; - validateExactNumericTableResult(check queryMssqlClient(sqlQuery1, database = simpleParamsDb)); - validateExactNumericTableResult(check queryMssqlClient(sqlQuery2, database = simpleParamsDb)); -} - -@test:Config { - groups: ["query","query-simple-params"] -} -function queryBigintParam() returns error? { - int rowId = 1; - int bigInt1 = 9223372036854775807; - sql:BigIntValue bigInt2 = new(9223372036854775807); - sql:ParameterizedQuery sqlQuery1 = `SELECT * FROM ExactNumeric WHERE row_id = ${rowId} AND bigint_type = ${bigInt1}`; - sql:ParameterizedQuery sqlQuery2 = `SELECT * FROM ExactNumeric WHERE row_id = ${rowId} AND bigint_type = ${bigInt2}`; - validateExactNumericTableResult(check queryMssqlClient(sqlQuery1, database = simpleParamsDb)); - validateExactNumericTableResult(check queryMssqlClient(sqlQuery2, database = simpleParamsDb)); -} - -@test:Config { - groups: ["query","query-simple-params"] -} -function querySmallintParam() returns error? { - int rowId = 1; - int smallInt1 = 32767; - sql:SmallIntValue smallInt2 = new(32767); - sql:ParameterizedQuery sqlQuery1 = `SELECT * FROM ExactNumeric WHERE row_id = ${rowId} AND smallint_type = ${smallInt1}`; - sql:ParameterizedQuery sqlQuery2 = `SELECT * FROM ExactNumeric WHERE row_id = ${rowId} AND smallint_type = ${smallInt2}`; - - validateExactNumericTableResult(check queryMssqlClient(sqlQuery1, database = simpleParamsDb)); - validateExactNumericTableResult(check queryMssqlClient(sqlQuery2, database = simpleParamsDb)); -} - -@test:Config { - groups: ["query","query-simple-params"] -} -function queryTinyintParam() returns error? { - int rowId = 1; - int tinyInt = 255; - sql:ParameterizedQuery sqlQuery = `SELECT * FROM ExactNumeric WHERE row_id = ${rowId} AND tinyint_type = ${tinyInt}`; - validateExactNumericTableResult(check queryMssqlClient(sqlQuery, database = simpleParamsDb)); -} - -@test:Config { - groups: ["query","query-simple-params"] -} -function queryNumericParam() returns error? { - int rowId = 1; - decimal numericVal1 = 12.12000; - sql:NumericValue numericVal2 = new(12.12000); - sql:ParameterizedQuery sqlQuery1 = `SELECT * FROM ExactNumeric WHERE row_id = ${rowId} AND numeric_type = ${numericVal1}`; - sql:ParameterizedQuery sqlQuery2 = `SELECT * FROM ExactNumeric WHERE row_id = ${rowId} AND numeric_type = ${numericVal2}`; - validateExactNumericTableResult(check queryMssqlClient(sqlQuery1, database = simpleParamsDb)); - validateExactNumericTableResult(check queryMssqlClient(sqlQuery2, database = simpleParamsDb)); -} - -@test:Config { - groups: ["query","query-simple-params"] -} -function queryDecimalParam() returns error? { - int rowId = 1; - decimal decimalVal1 = 123.41; - sql:DecimalValue decimalVal2 = new(123.41); - sql:ParameterizedQuery sqlQuery1 = `SELECT * FROM ExactNumeric WHERE row_id = ${rowId} AND decimal_type = ${decimalVal1}`; - sql:ParameterizedQuery sqlQuery2 = `SELECT * FROM ExactNumeric WHERE row_id = ${rowId} AND decimal_type = ${decimalVal2}`; - validateExactNumericTableResult(check queryMssqlClient(sqlQuery1, database = simpleParamsDb)); - validateExactNumericTableResult(check queryMssqlClient(sqlQuery2, database = simpleParamsDb)); -} - -@test:Config { - groups: ["query","query-simple-params"] -} -function queryVarcharParam() returns error? { - string varcharValue1 = "This is a varchar"; - sql:VarcharValue varcharValue2 = new("This is a varchar"); - sql:ParameterizedQuery sqlQuery1 = `SELECT * FROM StringTypes WHERE varchar_type = ${varcharValue1}`; - sql:ParameterizedQuery sqlQuery2 = `SELECT * FROM StringTypes WHERE varchar_type = ${varcharValue2}`; - validateStringTypeTableResult(check queryMssqlClient(sqlQuery1, database = simpleParamsDb)); - validateStringTypeTableResult(check queryMssqlClient(sqlQuery2, database = simpleParamsDb)); -} - -@test:Config { - groups: ["query","query-simple-params"] -} -function queryCharParam() returns error? { - string charValue1 = "This is a char"; - sql:CharValue charValue2 = new("This is a char"); - sql:ParameterizedQuery sqlQuery1 = `SELECT * FROM StringTypes WHERE char_type = ${charValue1}`; - sql:ParameterizedQuery sqlQuery2 = `SELECT * FROM StringTypes WHERE char_type = ${charValue2}`; - validateStringTypeTableResult(check queryMssqlClient(sqlQuery1, database = simpleParamsDb)); - validateStringTypeTableResult(check queryMssqlClient(sqlQuery2, database = simpleParamsDb)); -} - -@test:Config { - groups: ["query","query-simple-params"] -} -function queryIntAndStringParam() returns error? { - string charVal = "This is a char"; - int rowId =1; - sql:ParameterizedQuery sqlQuery = `SELECT * FROM StringTypes WHERE char_type = ${charVal} AND row_id = ${rowId}`; - validateStringTypeTableResult(check queryMssqlClient(sqlQuery, database = simpleParamsDb)); -} - -@test:Config { - groups: ["query","query-simple-params"] -} -function queryFloatParam() returns error? { - int rowId = 1; - sql:ParameterizedQuery sqlQuery = `SELECT * FROM ApproximateNumeric WHERE row_id = ${rowId}`; - validateApproximateNumericTableResult(check queryMssqlClient(sqlQuery, database = simpleParamsDb)); -} - -@test:Config { - groups: ["query","query-simple-params"] -} -function queryBitStringParam() returns error? { - sql:BitValue typeVal = new(true); - sql:ParameterizedQuery sqlQuery = `SELECT * FROM ExactNumeric WHERE bit_type = ${typeVal}`; - validateExactNumericTableResult(check queryMssqlClient(sqlQuery, database = simpleParamsDb)); -} - -@test:Config { - groups: ["query","query-simple-params"] -} -function queryBitInvalidIntParam() returns error? { - sql:BitValue typeVal = new (12); - sql:ParameterizedQuery sqlQuery = `SELECT * FROM ExactNumeric WHERE bit_type = ${typeVal}`; - record{}|error? returnVal = trap queryMssqlClient(sqlQuery, database = simpleParamsDb); - test:assertTrue(returnVal is error); - error dbError = returnVal; - test:assertEquals(dbError.message(), "Only 1 or 0 can be passed for BitValue SQL Type, but found :12"); -} - -@test:Config { - groups: ["query","query-simple-params"] -} -function queryDateValueParam() returns error? { - int rowId = 1; - time:Date dateValue = {year: 2017, month: 6, day: 26}; - sql:DateValue dateValue1 = new (dateValue); - sql:DateValue dateValue2 = new ("2017-06-26"); - sql:ParameterizedQuery sqlQuery1 = `SELECT * FROM DateandTime WHERE date_type = ${dateValue1} AND row_id = ${rowId}`; - sql:ParameterizedQuery sqlQuery2 = `SELECT * FROM DateandTime WHERE date_type = ${dateValue2} AND row_id = ${rowId}`; - validateDateTimeTableResult(check queryMssqlClient(sqlQuery1, database = simpleParamsDb)); - validateDateTimeTableResult(check queryMssqlClient(sqlQuery2, database = simpleParamsDb)); -} - -@test:Config { - groups: ["query","query-simple-params"] -} -function queryDateTimeOffsetValueParam() returns error? { - int rowId = 1; - time:Civil dateTimeOffsetValue = {year: 2020, month:1, day: 1, hour: 19, minute: 14, second:51, "utcOffset": {hours: 5, minutes: 30}}; - sql:DateTimeValue dateTimeOffsetValue1 = new(dateTimeOffsetValue); - sql:DateTimeValue dateTimeOffsetValue2 = new("2020-01-01 19:14:51.0000000 +05:30"); - sql:ParameterizedQuery sqlQuery1 = `SELECT * FROM DateandTime WHERE dateTimeOffset_type = ${dateTimeOffsetValue1} AND row_id = ${rowId}`; - sql:ParameterizedQuery sqlQuery2 = `SELECT * FROM DateandTime WHERE dateTimeOffset_type = ${dateTimeOffsetValue2} AND row_id = ${rowId}`; - validateDateTimeTableResult(check queryMssqlClient(sqlQuery1, database = simpleParamsDb)); - validateDateTimeTableResult(check queryMssqlClient(sqlQuery2, database = simpleParamsDb)); -} - -@test:Config { - groups: ["query","query-simple-params"] -} -function queryDateTime2ValueParam() returns error? { - int rowId = 1; - time:Civil dateTimeValue = {year: 1900, month:1, day: 1, hour: 0, minute: 25, second:0.0021425}; - sql:DateTimeValue dateTimeValue1 = new (dateTimeValue); - sql:DateTimeValue dateTimeValue2 = new ("1900-01-01 00:25:00.0021425"); - sql:ParameterizedQuery sqlQuery1 = `SELECT * FROM DateandTime WHERE dateTime2_type = ${dateTimeValue1} AND row_id = ${rowId}`; - sql:ParameterizedQuery sqlQuery2 = `SELECT * FROM DateandTime WHERE dateTime2_type = ${dateTimeValue2} AND row_id = ${rowId}`; - validateDateTimeTableResult(check queryMssqlClient(sqlQuery1, database = simpleParamsDb)); - validateDateTimeTableResult(check queryMssqlClient(sqlQuery2, database = simpleParamsDb)); -} - -@test:Config { - groups: ["query","query-simple-params"] -} -function querySmallDateTimeValueParam() returns error? { - int rowId = 1; - time:Civil smallDateTimeValue = {year: 2007, month: 5, day: 10, hour: 10, minute: 0, second:0.0}; - sql:DateTimeValue smallDateTimeValue1 = new (smallDateTimeValue); - sql:DateTimeValue smallDateTimeValue2 = new ("2007-05-10 10:00:00.0"); - sql:ParameterizedQuery sqlQuery1 = `SELECT * FROM DateandTime WHERE smallDateTime_type = ${smallDateTimeValue1} AND row_id = ${rowId}`; - sql:ParameterizedQuery sqlQuery2 = `SELECT * FROM DateandTime WHERE smallDateTime_type = ${smallDateTimeValue2} AND row_id = ${rowId}`; - validateDateTimeTableResult(check queryMssqlClient(sqlQuery1, database = simpleParamsDb)); - validateDateTimeTableResult(check queryMssqlClient(sqlQuery2, database = simpleParamsDb)); -} - -@test:Config { - groups: ["query","query-simple-params"] -} -function queryTimeValueParam() returns error? { - int rowId = 1; - sql:TimeValue timeValue2 = new ("09:46:22"); - sql:ParameterizedQuery sqlQuery = `SELECT * FROM DateandTime WHERE time_type = ${timeValue2} AND row_id = ${rowId}`; - validateDateTimeTableResult(check queryMssqlClient(sqlQuery, database = simpleParamsDb)); -} - -isolated function validateExactNumericTableResult(record{}? returnData) { - decimal decimalVal = 123.41; - if (returnData is ()) { - test:assertFail("Empty row returned."); - } else { - test:assertEquals(returnData["row_id"], 1); - test:assertEquals(returnData["bigint_type"], 9223372036854775807); - test:assertTrue(returnData["numeric_type"] is decimal); - test:assertEquals(returnData["int_type"], 2147483647); - test:assertEquals(returnData["smallint_type"], 32767); - test:assertEquals(returnData["decimal_type"], decimalVal); - } -} - -isolated function validateStringTypeTableResult(record{}? returnData) { - if (returnData is ()) { - test:assertFail("Empty row returned."); - } else { - test:assertEquals(returnData["row_id"], 1); - test:assertEquals(returnData["varchar_type"], "This is a varchar"); - test:assertEquals(returnData["text_type"], "This is a long text"); - test:assertTrue(returnData["nchar_type"] is string); - } -} - -isolated function validateApproximateNumericTableResult(record{}? returnData) { - float floatVal = 1.79E+308; - float realVal = -1.179999945774631E-38; - if (returnData is ()) { - test:assertFail("Empty row returned."); - } else { - test:assertEquals(returnData["row_id"], 1); - test:assertEquals(returnData["real_type"], realVal); - test:assertEquals(returnData["float_type"], floatVal); - } -} - -isolated function validateDateTimeTableResult(record{}? returnData) { - if (returnData is ()) { - test:assertFail("Empty row returned."); - } else { - test:assertEquals(returnData["row_id"], 1); - test:assertEquals(returnData["date_type"], "2017-06-26"); - test:assertEquals(returnData["dateTimeOffset_type"], "2020-01-01 19:14:51.0000000 +05:30"); - test:assertEquals(returnData["dateTime2_type"], "1900-01-01 00:25:00.0021425"); - test:assertEquals(returnData["smallDateTime_type"], "2007-05-10 10:00:00.0"); - test:assertEquals(returnData["dateTime_type"], "2017-06-26 09:54:21.327"); - test:assertEquals(returnData["time_type"], "09:46:22"); - } -} - From cf71558aba7ecd3bd056bb10e1c2434fd278d3f9 Mon Sep 17 00:00:00 2001 From: kaneeldias Date: Fri, 20 Aug 2021 15:25:27 +0530 Subject: [PATCH 16/22] Remove utils file --- tests/connection-init-test.bal | 12 ++---- tests/utils.bal | 72 ---------------------------------- 2 files changed, 3 insertions(+), 81 deletions(-) delete mode 100644 tests/utils.bal diff --git a/tests/connection-init-test.bal b/tests/connection-init-test.bal index c74251a..30399ec 100644 --- a/tests/connection-init-test.bal +++ b/tests/connection-init-test.bal @@ -17,15 +17,9 @@ import ballerina/sql; import ballerinax/java.jdbc; import ballerina/test; -string connectDB = "CONNECT_DB"; - -@test:BeforeGroups { - value: ["connection-init"] -} -function initConnectionTests() returns error? { - _ = check executeQueryMssqlClient(`DROP DATABASE IF EXISTS CONNECT_DB`); - _ = check executeQueryMssqlClient(`CREATE DATABASE CONNECT_DB`); -} +string url = "jdbc:sqlserver://localhost:1433;"; +string user = "sa"; +string password = "Test123#"; @test:Config { groups: ["connection", "connection-init"] diff --git a/tests/utils.bal b/tests/utils.bal deleted file mode 100644 index 76c7f69..0000000 --- a/tests/utils.bal +++ /dev/null @@ -1,72 +0,0 @@ -// Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -// -// WSO2 Inc. licenses this file to you under the Apache License, -// Version 2.0 (the "License"); you may not use this file except -// in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -import ballerinax/java.jdbc; -import ballerina/sql; - -string url = "jdbc:sqlserver://localhost:1433;"; -string user = "sa"; -string password = "Test123#"; - -function getMssqlClient(string? database = ()) returns jdbc:Client|error { - jdbc:Client dbClient; - if database is () { - dbClient = check new (url, user, password); - } else { - dbClient = check new (url + "databaseName=" + database, user, password); - } - return dbClient; -} - -function queryMssqlClient(string|sql:ParameterizedQuery sqlQuery, typedesc? resultType = (), string? database = ()) -returns record {}|error? { - jdbc:Client dbClient = check getMssqlClient(database); - stream streamData; - if resultType is () { - streamData = dbClient->query(sqlQuery); - } else { - streamData = dbClient->query(sqlQuery, resultType); - } - record {|record {} value;|}? data = check streamData.next(); - check streamData.close(); - record {}? value = data?.value; - check dbClient.close(); - return value; -} - -function executeQueryMssqlClient(string|sql:ParameterizedQuery sqlQuery, string? database = ()) -returns sql:ExecutionResult|error { - jdbc:Client dbClient = check getMssqlClient(database); - sql:ExecutionResult result = check dbClient->execute(sqlQuery); - check dbClient.close(); - return result; -} - -function batchExecuteQueryMssqlClient(sql:ParameterizedQuery[] sqlQueries, string? database = ()) returns sql:ExecutionResult[] | error { - jdbc:Client dbClient = check getMssqlClient(database); - sql:ExecutionResult[] result = check dbClient->batchExecute(sqlQueries); - check dbClient.close(); - return result; -} - -function callProcedureMssqlClient(sql:ParameterizedCallQuery sqlQuery, string database, typedesc[] rowTypes = []) -returns sql:ProcedureCallResult | error { - jdbc:Client dbClient = check getMssqlClient(database); - sql:ProcedureCallResult result = check dbClient->call(sqlQuery, rowTypes); - check dbClient.close(); - return result; -} - From ec9cb48830965d9636168c04b021db234f8b23c2 Mon Sep 17 00:00:00 2001 From: kaneeldias Date: Fri, 20 Aug 2021 16:16:37 +0530 Subject: [PATCH 17/22] Remove unused environment variables & add newline at EOF --- .github/workflows/ci.yml | 5 +---- .github/workflows/release.yml | 6 +----- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2fc936d..e51bc40 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,7 +21,4 @@ jobs: with: args: build -c --skip-tests - env: - DB_HOST: ${{ secrets.DB_HOST }} - DB_USER: ${{ secrets.DB_USER }} - DB_PASSWORD: ${{ secrets.DB_PASSWORD }} \ No newline at end of file + diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 58c21af..94008a5 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -16,14 +16,10 @@ jobs: with: args: build -c --skip-tests - env: - DB_HOST: ${{ secrets.DB_HOST }} - DB_USER: ${{ secrets.DB_USER }} - DB_PASSWORD: ${{ secrets.DB_PASSWORD }} - name: Ballerina Push uses: ballerina-platform/ballerina-action/@slbeta1 with: args: push env: - BALLERINA_CENTRAL_ACCESS_TOKEN: ${{ secrets.BALLERINA_CENTRAL_ACCESS_TOKEN }} \ No newline at end of file + BALLERINA_CENTRAL_ACCESS_TOKEN: ${{ secrets.BALLERINA_CENTRAL_ACCESS_TOKEN }} From 24b8154929abac876e4181b368c702b3ac98f9c4 Mon Sep 17 00:00:00 2001 From: kaneeldias Date: Fri, 20 Aug 2021 16:18:22 +0530 Subject: [PATCH 18/22] Remove unused environment variables & add newline at EOF --- .github/workflows/release.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 94008a5..26452a7 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -23,3 +23,4 @@ jobs: push env: BALLERINA_CENTRAL_ACCESS_TOKEN: ${{ secrets.BALLERINA_CENTRAL_ACCESS_TOKEN }} + From b4a98b586fd69ebbb60a104f60fee7f82e4a3c05 Mon Sep 17 00:00:00 2001 From: kaneeldias Date: Fri, 20 Aug 2021 16:27:36 +0530 Subject: [PATCH 19/22] Update version to 1.1.0 --- Ballerina.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Ballerina.toml b/Ballerina.toml index 76df54a..f1e9d78 100644 --- a/Ballerina.toml +++ b/Ballerina.toml @@ -1,7 +1,7 @@ [package] org="ballerinax" name="mssql.driver" -version="1.0.0" +version="1.1.0" authors=["Ballerina"] keywords=["MSSQL", "SQL Server"] repository="https://github.com/ballerina-platform/module-ballerinax-mssql.driver" From ff65b8b299fd187793d5afd6e273c192c67d8e9c Mon Sep 17 00:00:00 2001 From: kaneeldias Date: Fri, 20 Aug 2021 16:30:20 +0530 Subject: [PATCH 20/22] Update ballerina version used in Package.md to SL Beta 1 --- Package.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.md b/Package.md index 9015c6e..cbc6932 100644 --- a/Package.md +++ b/Package.md @@ -4,7 +4,7 @@ This Package bundles the latest MSSQL driver so that the mssql connector can be ## Compatibility -Ballerina Language Version **Swan Lake Beta 2** +Ballerina Language Version **Swan Lake Beta 1** MSSQL Driver Version **9.4.0.jre11** ## Usage From a078857747da3210cfdd8aa450e6713b99728581 Mon Sep 17 00:00:00 2001 From: kaneeldias Date: Fri, 20 Aug 2021 17:15:12 +0530 Subject: [PATCH 21/22] Change Ballerina version in SL Beta 1 --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e51bc40..52bf041 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,7 +17,7 @@ jobs: steps: - uses: actions/checkout@v2 - name: Ballerina Build - uses: ballerina-platform/ballerina-action/@slbeta2 + uses: ballerina-platform/ballerina-action/@slbeta1 with: args: build -c --skip-tests From 5456a7202d272c31544a1092dd050f2d10879b5e Mon Sep 17 00:00:00 2001 From: kaneeldias Date: Fri, 20 Aug 2021 17:16:06 +0530 Subject: [PATCH 22/22] Change Ballerina version in SL Beta 1 --- .github/workflows/release.yml | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 26452a7..a13945b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -12,7 +12,7 @@ jobs: steps: - uses: actions/checkout@v2 - name: Ballerina Build - uses: ballerina-platform/ballerina-action/@slbeta2 + uses: ballerina-platform/ballerina-action/@slbeta1 with: args: build -c --skip-tests diff --git a/README.md b/README.md index f871a3c..a73de44 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ This Package bundles the latest MSSQL driver so that the mssql connector can be > **Note:** Set the JAVA_HOME environment variable to the path name of the directory into which you installed JDK. -2. Download and install [Ballerina SL Beta 2](https://ballerina.io/). +2. Download and install [Ballerina SL Beta 1](https://ballerina.io/). ## Building the Source