diff --git a/ballerina/Ballerina.toml b/ballerina/Ballerina.toml index 4b8c0d13..7666c1e6 100644 --- a/ballerina/Ballerina.toml +++ b/ballerina/Ballerina.toml @@ -12,7 +12,7 @@ distribution = "2201.0.0" path = "../native/build/libs/postgresql-native-1.3.0-SNAPSHOT.jar" [[platform.java11.dependency]] -path = "./lib/sql-native-1.3.0-20211221-080400-54b27f2.jar" +path = "./lib/sql-native-1.3.0-20211222-201000-1819d5f.jar" [[platform.java11.dependency]] path = "./lib/postgresql-42.2.20.jar" diff --git a/changelog.md b/changelog.md index c77ba9fd..c98aff73 100644 --- a/changelog.md +++ b/changelog.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added ### Changed -- Release PostgreSQL module on Ballerina 2201.0.0 distribution +- [Fix Compiler plugin crash when variable is passed for `sql:ConnectionPool` and `postgresql:Options`](https://github.com/ballerina-platform/ballerina-standard-library/issues/2536) ## [1.2.0] - 2021-12-13 diff --git a/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/postgresql/compiler/CompilerPluginTest.java b/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/postgresql/compiler/CompilerPluginTest.java index 44f81911..101417c6 100644 --- a/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/postgresql/compiler/CompilerPluginTest.java +++ b/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/postgresql/compiler/CompilerPluginTest.java @@ -204,4 +204,17 @@ public void testOutParameterHint() { }); } + + @Test + public void testOptionsWithVariables() { + Package currentPackage = loadPackage("sample6"); + PackageCompilation compilation = currentPackage.getCompilation(); + DiagnosticResult diagnosticResult = compilation.diagnosticResult(); + List diagnosticErrorStream = diagnosticResult.diagnostics().stream() + .filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)) + .collect(Collectors.toList()); + long availableErrors = diagnosticErrorStream.size(); + + Assert.assertEquals(availableErrors, 0); + } } diff --git a/compiler-plugin-tests/src/test/resources/diagnostics/sample6/Ballerina.toml b/compiler-plugin-tests/src/test/resources/diagnostics/sample6/Ballerina.toml new file mode 100644 index 00000000..a07fddec --- /dev/null +++ b/compiler-plugin-tests/src/test/resources/diagnostics/sample6/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "postgresql_test" +name = "sample6" +version = "0.1.0" diff --git a/compiler-plugin-tests/src/test/resources/diagnostics/sample6/main.bal b/compiler-plugin-tests/src/test/resources/diagnostics/sample6/main.bal new file mode 100644 index 00000000..ddfe1a9a --- /dev/null +++ b/compiler-plugin-tests/src/test/resources/diagnostics/sample6/main.bal @@ -0,0 +1,32 @@ +// 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/postgresql; + +public function main() returns error? { + + decimal loginTimeout = 5; + decimal loginTimeoutInvalid = -5; + + postgresql:Options options1 = { + loginTimeout: loginTimeout + }; + + postgresql:Options options2 = { + loginTimeout: loginTimeoutInvalid + }; + +} diff --git a/compiler-plugin/src/main/java/io/ballerina/stdlib/postgresql/compiler/Utils.java b/compiler-plugin/src/main/java/io/ballerina/stdlib/postgresql/compiler/Utils.java index fc15fc81..d37e33d5 100644 --- a/compiler-plugin/src/main/java/io/ballerina/stdlib/postgresql/compiler/Utils.java +++ b/compiler-plugin/src/main/java/io/ballerina/stdlib/postgresql/compiler/Utils.java @@ -99,7 +99,7 @@ public static void validateOptions(SyntaxNodeAnalysisContext ctx, MappingConstru case Constants.Options.LOGIN_TIMEOUT: case Constants.Options.SOCKET_TIMEOUT: case Constants.Options.CANCEL_SIGNAL_TIMEOUT: - float timeoutVal = Float.parseFloat(getTerminalNodeValue(valueNode)); + float timeoutVal = Float.parseFloat(getTerminalNodeValue(valueNode, "0")); if (timeoutVal < 0) { DiagnosticInfo diagnosticInfo = new DiagnosticInfo(POSTGRESQL_101.getCode(), POSTGRESQL_101.getMessage(), POSTGRESQL_101.getSeverity()); @@ -113,7 +113,7 @@ public static void validateOptions(SyntaxNodeAnalysisContext ctx, MappingConstru case Constants.Options.PREPARED_STATEMENT_THRESHOLD: case Constants.Options.PREPARED_STATEMENT_CACHE_QUERIES: case Constants.Options.PREPARED_STATEMENT_CACHE_SIZE_MIB: - int sizeVal = Integer.parseInt(getTerminalNodeValue(valueNode)); + int sizeVal = Integer.parseInt(getTerminalNodeValue(valueNode, "1")); if (sizeVal <= 0) { DiagnosticInfo diagnosticInfo = new DiagnosticInfo(POSTGRESQL_102.getCode(), POSTGRESQL_102.getMessage(), POSTGRESQL_102.getSeverity()); @@ -128,8 +128,8 @@ public static void validateOptions(SyntaxNodeAnalysisContext ctx, MappingConstru } } - public static String getTerminalNodeValue(Node valueNode) { - String value = ""; + public static String getTerminalNodeValue(Node valueNode, String defaultValue) { + String value = defaultValue; if (valueNode instanceof BasicLiteralNode) { value = ((BasicLiteralNode) valueNode).literalToken().text(); } else if (valueNode instanceof UnaryExpressionNode) { @@ -137,6 +137,7 @@ public static String getTerminalNodeValue(Node valueNode) { value = unaryExpressionNode.unaryOperator() + ((BasicLiteralNode) unaryExpressionNode.expression()).literalToken().text(); } + // Currently, we cannot process values from variables, this needs code flow analysis return value.replaceAll(UNNECESSARY_CHARS_REGEX, ""); } diff --git a/compiler-plugin/src/main/java/io/ballerina/stdlib/postgresql/compiler/analyzer/InitializerParamAnalyzer.java b/compiler-plugin/src/main/java/io/ballerina/stdlib/postgresql/compiler/analyzer/InitializerParamAnalyzer.java index 357814a1..8e689270 100644 --- a/compiler-plugin/src/main/java/io/ballerina/stdlib/postgresql/compiler/analyzer/InitializerParamAnalyzer.java +++ b/compiler-plugin/src/main/java/io/ballerina/stdlib/postgresql/compiler/analyzer/InitializerParamAnalyzer.java @@ -115,7 +115,7 @@ private void validateConnectionPool(SyntaxNodeAnalysisContext ctx, MappingConstr ExpressionNode valueNode = ((SpecificFieldNode) field).valueExpr().get(); switch (name) { case Constants.ConnectionPool.MAX_OPEN_CONNECTIONS: - int maxOpenConnections = Integer.parseInt(getTerminalNodeValue(valueNode)); + int maxOpenConnections = Integer.parseInt(getTerminalNodeValue(valueNode, "1")); if (maxOpenConnections < 1) { DiagnosticInfo diagnosticInfo = new DiagnosticInfo(SQL_101.getCode(), SQL_101.getMessage(), SQL_101.getSeverity()); @@ -126,7 +126,7 @@ private void validateConnectionPool(SyntaxNodeAnalysisContext ctx, MappingConstr } break; case Constants.ConnectionPool.MIN_IDLE_CONNECTIONS: - int minIdleConnection = Integer.parseInt(getTerminalNodeValue(valueNode)); + int minIdleConnection = Integer.parseInt(getTerminalNodeValue(valueNode, "0")); if (minIdleConnection < 0) { DiagnosticInfo diagnosticInfo = new DiagnosticInfo(SQL_102.getCode(), SQL_102.getMessage(), SQL_102.getSeverity()); @@ -136,7 +136,7 @@ private void validateConnectionPool(SyntaxNodeAnalysisContext ctx, MappingConstr } break; case Constants.ConnectionPool.MAX_CONNECTION_LIFE_TIME: - float maxConnectionTime = Float.parseFloat(getTerminalNodeValue(valueNode)); + float maxConnectionTime = Float.parseFloat(getTerminalNodeValue(valueNode, "30")); if (maxConnectionTime < 30) { DiagnosticInfo diagnosticInfo = new DiagnosticInfo(SQL_103.getCode(), SQL_103.getMessage(), SQL_103.getSeverity());