From 166692c7d747c2c0fcd0d01076282ab1d96a6acc Mon Sep 17 00:00:00 2001 From: Haritha Hasathcharu Date: Thu, 22 Feb 2024 10:41:48 +0530 Subject: [PATCH] add test cases for annotations --- ballerina/tests/init-tests.bal | 52 +-- compiler-plugin-test/build.gradle | 2 + .../sql/compiler/CodeModifierTest.java | 10 +- .../sql/compiler/CompilerPluginTest.java | 338 ++++++++++++++++++ .../persist/sql/compiler/TestUtils.java | 34 ++ .../test/resources/project_1/Ballerina.toml | 7 + .../test/resources/project_1/persist/char.bal | 37 ++ .../resources/project_1/persist/decimal.bal | 12 + .../project_1/persist/entity_name_mapping.bal | 56 +++ .../resources/project_1/persist/index.bal | 29 ++ .../resources/project_1/persist/relation1.bal | 22 ++ .../resources/project_1/persist/relation2.bal | 21 ++ .../resources/project_1/persist/relation3.bal | 21 ++ .../resources/project_1/persist/relation4.bal | 21 ++ .../resources/project_1/persist/relation5.bal | 21 ++ .../resources/project_1/persist/relation6.bal | 22 ++ .../resources/project_1/persist/relation7.bal | 24 ++ .../project_1/persist/table_name_mapping.bal | 48 +++ .../project_1/persist/unique_index.bal | 29 ++ .../resources/project_1/persist/varchar.bal | 37 ++ .../src/test/resources/testng.xml | 1 + compiler-plugin/build.gradle | 3 +- 22 files changed, 821 insertions(+), 26 deletions(-) create mode 100644 compiler-plugin-test/src/test/java/io/ballerina/stdlib/persist/sql/compiler/CompilerPluginTest.java create mode 100644 compiler-plugin-test/src/test/java/io/ballerina/stdlib/persist/sql/compiler/TestUtils.java create mode 100644 compiler-plugin-test/src/test/resources/project_1/Ballerina.toml create mode 100644 compiler-plugin-test/src/test/resources/project_1/persist/char.bal create mode 100644 compiler-plugin-test/src/test/resources/project_1/persist/decimal.bal create mode 100644 compiler-plugin-test/src/test/resources/project_1/persist/entity_name_mapping.bal create mode 100644 compiler-plugin-test/src/test/resources/project_1/persist/index.bal create mode 100644 compiler-plugin-test/src/test/resources/project_1/persist/relation1.bal create mode 100644 compiler-plugin-test/src/test/resources/project_1/persist/relation2.bal create mode 100644 compiler-plugin-test/src/test/resources/project_1/persist/relation3.bal create mode 100644 compiler-plugin-test/src/test/resources/project_1/persist/relation4.bal create mode 100644 compiler-plugin-test/src/test/resources/project_1/persist/relation5.bal create mode 100644 compiler-plugin-test/src/test/resources/project_1/persist/relation6.bal create mode 100644 compiler-plugin-test/src/test/resources/project_1/persist/relation7.bal create mode 100644 compiler-plugin-test/src/test/resources/project_1/persist/table_name_mapping.bal create mode 100644 compiler-plugin-test/src/test/resources/project_1/persist/unique_index.bal create mode 100644 compiler-plugin-test/src/test/resources/project_1/persist/varchar.bal diff --git a/ballerina/tests/init-tests.bal b/ballerina/tests/init-tests.bal index 0f61446..55458e2 100644 --- a/ballerina/tests/init-tests.bal +++ b/ballerina/tests/init-tests.bal @@ -53,26 +53,37 @@ configurable record {| @test:BeforeSuite function initTests() returns error? { // MySQL - mysql:Client mysqlDbClient = check new (host = mysql.host, user = mysql.user, password = mysql.password, database = mysql.database, port = mysql.port); - _ = check mysqlDbClient->execute(`SET FOREIGN_KEY_CHECKS = 0`); - _ = check mysqlDbClient->execute(`TRUNCATE Employee`); - _ = check mysqlDbClient->execute(`TRUNCATE Workspace`); - _ = check mysqlDbClient->execute(`TRUNCATE Building`); - _ = check mysqlDbClient->execute(`TRUNCATE Department`); - _ = check mysqlDbClient->execute(`TRUNCATE OrderItem`); - _ = check mysqlDbClient->execute(`TRUNCATE AllTypes`); - _ = check mysqlDbClient->execute(`TRUNCATE FloatIdRecord`); - _ = check mysqlDbClient->execute(`TRUNCATE StringIdRecord`); - _ = check mysqlDbClient->execute(`TRUNCATE DecimalIdRecord`); - _ = check mysqlDbClient->execute(`TRUNCATE BooleanIdRecord`); - _ = check mysqlDbClient->execute(`TRUNCATE IntIdRecord`); - _ = check mysqlDbClient->execute(`TRUNCATE AllTypesIdRecord`); - _ = check mysqlDbClient->execute(`TRUNCATE CompositeAssociationRecord`); - _ = check mysqlDbClient->execute(`SET FOREIGN_KEY_CHECKS = 1`); - check mysqlDbClient.close(); + check MySQLTests(); //MSSQL - mssql:Client mssqlDbClient = check new (host = mssql.host, user = mssql.user, password = mssql.password, port = mssql.port); + check MSSQLTests(); + + // PostgreSQL + check PostgreSQLTests(); +} + +function MySQLTests() returns error? { + mysql:Client mysqlDbClient = check new (host = mysql.host, user = mysql.user, password = mysql.password, database = mysql.database, port = mysql.port); + _ = check mysqlDbClient->execute(`SET FOREIGN_KEY_CHECKS = 0`); + _ = check mysqlDbClient->execute(`TRUNCATE Employee`); + _ = check mysqlDbClient->execute(`TRUNCATE Workspace`); + _ = check mysqlDbClient->execute(`TRUNCATE Building`); + _ = check mysqlDbClient->execute(`TRUNCATE Department`); + _ = check mysqlDbClient->execute(`TRUNCATE OrderItem`); + _ = check mysqlDbClient->execute(`TRUNCATE AllTypes`); + _ = check mysqlDbClient->execute(`TRUNCATE FloatIdRecord`); + _ = check mysqlDbClient->execute(`TRUNCATE StringIdRecord`); + _ = check mysqlDbClient->execute(`TRUNCATE DecimalIdRecord`); + _ = check mysqlDbClient->execute(`TRUNCATE BooleanIdRecord`); + _ = check mysqlDbClient->execute(`TRUNCATE IntIdRecord`); + _ = check mysqlDbClient->execute(`TRUNCATE AllTypesIdRecord`); + _ = check mysqlDbClient->execute(`TRUNCATE CompositeAssociationRecord`); + _ = check mysqlDbClient->execute(`SET FOREIGN_KEY_CHECKS = 1`); + check mysqlDbClient.close(); +} + +function MSSQLTests() returns error? { + mssql:Client mssqlDbClient = check new (host = mssql.host, user = mssql.user, password = mssql.password, port = mssql.port); _ = check mssqlDbClient->execute(`DROP DATABASE IF EXISTS test;`); _ = check mssqlDbClient->execute(`CREATE DATABASE test`); check mssqlDbClient.close(); @@ -221,9 +232,10 @@ function initTests() returns error? { PRIMARY KEY(id) ); `); +} - // PostgreSQL - postgresql:Client postgresqlDbClient = check new (host = postgresql.host, username = postgresql.user, password = postgresql.password, database = postgresql.database, port = postgresql.port); +function PostgreSQLTests() returns error? { + postgresql:Client postgresqlDbClient = check new (host = postgresql.host, username = postgresql.user, password = postgresql.password, database = postgresql.database, port = postgresql.port); _ = check postgresqlDbClient->execute(`TRUNCATE "Employee" CASCADE`); _ = check postgresqlDbClient->execute(`TRUNCATE "Workspace" CASCADE`); _ = check postgresqlDbClient->execute(`TRUNCATE "Building" CASCADE`); diff --git a/compiler-plugin-test/build.gradle b/compiler-plugin-test/build.gradle index ed4850a..0e69652 100644 --- a/compiler-plugin-test/build.gradle +++ b/compiler-plugin-test/build.gradle @@ -42,6 +42,8 @@ dependencies { implementation group: 'org.testng', name: 'testng', version: "${testngVersion}" testImplementation 'org.testng:testng' testImplementation group: 'com.google.code.gson', name: 'gson', version: "${gsonVersion}" + implementation group: 'io.ballerina.stdlib', name: 'persist-native', version: "1.2.1-20240222-125000-4a9d764" + implementation group: 'io.ballerina.stdlib', name: 'persist-compiler-plugin', version: "1.2.1-20240222-125000-4a9d764" } tasks.withType(JavaCompile) { diff --git a/compiler-plugin-test/src/test/java/io/ballerina/stdlib/persist/sql/compiler/CodeModifierTest.java b/compiler-plugin-test/src/test/java/io/ballerina/stdlib/persist/sql/compiler/CodeModifierTest.java index acbef1c..d73f6cd 100644 --- a/compiler-plugin-test/src/test/java/io/ballerina/stdlib/persist/sql/compiler/CodeModifierTest.java +++ b/compiler-plugin-test/src/test/java/io/ballerina/stdlib/persist/sql/compiler/CodeModifierTest.java @@ -56,7 +56,7 @@ private Package loadPackage(String path) { return project.currentPackage(); } - @Test + @Test(enabled = true) public void testCodeModifier() { Package newPackage = getModifiedPackage("project_1"); @@ -130,7 +130,7 @@ public void testCodeModifier() { } } - @Test + @Test(enabled = true) public void testCodeModifierWithRelationTables() { Package newPackage = getModifiedPackage("project_3"); @@ -155,7 +155,7 @@ public void testCodeModifierWithRelationTables() { } } - @Test + @Test(enabled = true) public void testCodeModifierWhenNameHasEscapeCharacter() { Package newPackage = getModifiedPackage("project_4"); @@ -200,7 +200,7 @@ public void testCodeModifierWhenNameHasEscapeCharacter() { } - @Test + @Test(enabled = true) public void testCodeModifierWhenFieldIsInTypeDescriptor() { Package newPackage = getModifiedPackage("project_5"); @@ -241,7 +241,7 @@ public void testCodeModifierWhenFieldIsInTypeDescriptor() { } } - @Test + @Test(enabled = true) public void testCodeModifierWithDiagnostic() { Package currentPackage = loadPackage("project_2"); DiagnosticResult diagnosticResult = currentPackage.getCompilation().diagnosticResult(); diff --git a/compiler-plugin-test/src/test/java/io/ballerina/stdlib/persist/sql/compiler/CompilerPluginTest.java b/compiler-plugin-test/src/test/java/io/ballerina/stdlib/persist/sql/compiler/CompilerPluginTest.java new file mode 100644 index 0000000..e44916a --- /dev/null +++ b/compiler-plugin-test/src/test/java/io/ballerina/stdlib/persist/sql/compiler/CompilerPluginTest.java @@ -0,0 +1,338 @@ +/* + * Copyright (c) 2022, 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. + */ + +package io.ballerina.stdlib.persist.sql.compiler; + +import io.ballerina.projects.DiagnosticResult; +import io.ballerina.projects.Package; +import io.ballerina.projects.directory.SingleFileProject; +import io.ballerina.tools.diagnostics.Diagnostic; +import io.ballerina.tools.diagnostics.DiagnosticInfo; +import io.ballerina.tools.diagnostics.DiagnosticSeverity; +import org.testng.Assert; +import org.testng.annotations.Test; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.stream.Collectors; + +import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_423; +import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_424; +import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_426; +import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_427; +import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_428; +import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_429; +import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_430; +import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_600; +import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_601; +import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_604; +import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_605; +import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_606; +import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_607; +import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_608; +import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_609; +import static io.ballerina.stdlib.persist.compiler.DiagnosticsCodes.PERSIST_610; +import static io.ballerina.stdlib.persist.sql.compiler.TestUtils.getEnvironmentBuilder; + +/** + * Tests persist compiler plugin. + */ +public class CompilerPluginTest { + + + private Package loadPersistModelFile(String directory, String name) { + Path projectDirPath = Paths.get("src", "test", "resources", directory, "persist"). + toAbsolutePath().resolve(name); + SingleFileProject project = SingleFileProject.load(getEnvironmentBuilder(), projectDirPath); + return project.currentPackage(); + } + @Test(enabled = true) + public void validateCharAnnotations() { + List diagnostics = getErrorDiagnostics("project_1", "char.bal", 3); + testDiagnostic( + diagnostics, + new String[]{ + PERSIST_605.getCode(), + PERSIST_607.getCode(), + PERSIST_604.getCode() + }, + new String[]{ + "invalid use of VarChar and Char annotations. " + + "only one of either Char or Varchar annotations can be used at a time.", + "invalid use of 'Char' annotation. length cannot be 0.", + "invalid use of 'Char' annotation. 'Char' annotation can only be used for string data type." + }, + new String[]{ + "(17:4,19:18)", + "(28:4,29:16)", + "(30:4,31:12)" + } + ); + } + + @Test(enabled = true) + public void validateVarCharAnnotations() { + List diagnostics = getErrorDiagnostics("project_1", + "varchar.bal", 3); + testDiagnostic( + diagnostics, + new String[]{ + PERSIST_605.getCode(), + PERSIST_607.getCode(), + PERSIST_604.getCode() + }, + new String[]{ + "invalid use of VarChar and Char annotations. " + + "only one of either Char or Varchar annotations can be used at a time.", + "invalid use of 'VarChar' annotation. length cannot be 0.", + "invalid use of 'VarChar' annotation. 'VarChar' annotation can only be " + + "used for string data type." + }, + new String[]{ + "(17:4,19:18)", + "(28:4,29:16)", + "(30:4,31:12)" + } + ); + } + + @Test(enabled = true) + public void validateDecimalAnnotations() { + List diagnostics = getErrorDiagnostics("project_1", + "decimal.bal", 3); + testDiagnostic( + diagnostics, + new String[]{ + PERSIST_606.getCode(), + PERSIST_608.getCode(), + PERSIST_609.getCode() + }, + new String[]{ + "invalid use of Decimal annotation. Decimal annotation can only be used for decimal data type.", + "invalid use of Decimal annotation. precision cannot be 0.", + "invalid use of Decimal annotation. precision cannot be less than scale." + }, + new String[]{ + "(6:4,7:12)", + "(8:4,9:19)", + "(8:4,9:19)" + } + ); + } + + @Test(enabled = true) + public void validateEntityNameMappingAnnotations() { + List diagnostics = getErrorDiagnostics("project_1", + "entity_name_mapping.bal", 3); + testDiagnostic( + diagnostics, + new String[]{ + PERSIST_600.getCode(), + PERSIST_601.getCode(), + PERSIST_610.getCode() + }, + new String[]{ + "invalid use of Mapping annotation. mapping name cannot be empty.", + "mapping name is same as model definition.", + "invalid use of Mapping annotation. duplicate mapping name found." + }, + new String[]{ + "(16:12,16:23)", + "(26:12,26:19)", + "(44:12,44:17)" + } + ); + } + + @Test(enabled = true) + public void validateFieldNameMappingAnnotations() { + List diagnostics = getErrorDiagnostics("project_1", + "table_name_mapping.bal", 3); + testDiagnostic( + diagnostics, + new String[]{ + PERSIST_600.getCode(), + PERSIST_610.getCode(), + PERSIST_601.getCode() + }, + new String[]{ + "invalid use of Mapping annotation. mapping name cannot be empty.", + "invalid use of Mapping annotation. duplicate mapping name found.", + "mapping name is same as model definition." + }, + new String[]{ + "(20:4,21:29)", + "(32:4,33:24)", + "(43:4,44:23)" + } + ); + } + + @Test(enabled = true) + public void validateRelationAnnotations1() { + List diagnostics = getErrorDiagnostics("project_1", + "relation1.bal", 1); + testDiagnostic( + diagnostics, + new String[]{ + PERSIST_423.getCode() + }, + new String[]{ + "invalid use of Relation annotation. mismatched number of reference " + + "keys for entity 'Car' for relation 'Person'. expected 2 but found 1." + }, + new String[]{ + "(19:4,20:17)" + } + ); + } + + @Test(enabled = true) + public void validateRelationAnnotations2() { + List diagnostics = getErrorDiagnostics("project_1", + "relation2.bal", 1); + testDiagnostic( + diagnostics, + new String[]{ + PERSIST_424.getCode() + }, + new String[]{ + "invalid use of Relation annotation. mismatched key types for entity " + + "'Person' for its relationship." + }, + new String[]{ + "(18:4,19:17)" + } + ); + } + + @Test(enabled = true) + public void validateRelationAnnotations3() { + List diagnostics = getErrorDiagnostics("project_1", + "relation3.bal", 1); + testDiagnostic( + diagnostics, + new String[]{ + PERSIST_426.getCode() + }, + new String[]{ + "invalid use of Relation annotation. the field 'cars' is an array type in a 1-n " + + "relationship. therefore, it cannot have foreign keys." + }, + new String[]{ + "(9:4,10:15)" + } + ); + } + @Test(enabled = true) + public void validateRelationAnnotations4() { + List diagnostics = getErrorDiagnostics("project_1", + "relation4.bal", 1); + testDiagnostic( + diagnostics, + new String[]{ + PERSIST_427.getCode() + }, + new String[]{ + "invalid use of Relation annotation. the field 'car' is an optional type in a 1-1 " + + "relationship. therefore, it cannot have foreign keys." + }, + new String[]{ + "(9:4,10:13)" + } + ); + } + @Test(enabled = true) + public void validateRelationAnnotations5() { + List diagnostics = getErrorDiagnostics("project_1", + "relation5.bal", 1); + testDiagnostic( + diagnostics, + new String[]{ + PERSIST_428.getCode() + }, + new String[]{ + "invalid use of Relation annotation. the field 'ownerNic' is not found in the entity 'Car'." + }, + new String[]{ + "(18:4,19:17)" + } + ); + } + @Test(enabled = true) + public void validateRelationAnnotations6() { + List diagnostics = getErrorDiagnostics("project_1", + "relation6.bal", 1); + testDiagnostic( + diagnostics, + new String[]{ + PERSIST_429.getCode() + }, + new String[]{ + "invalid use of Relation annotation. refs cannot contain duplicates." + }, + new String[]{ + "(19:4,20:17)" + } + ); + } + @Test(enabled = true) + public void validateRelationAnnotations7() { + List diagnostics = getErrorDiagnostics("project_1", + "relation7.bal", 1); + testDiagnostic( + diagnostics, + new String[]{ + PERSIST_430.getCode() + }, + new String[]{ + "invalid use of Relation annotation. duplicated reference field." + }, + new String[]{ + "(21:4,22:20)" + } + ); + } + + + private List getErrorDiagnostics(String modelDirectory, String modelFileName, int count) { + DiagnosticResult diagnosticResult = loadPersistModelFile(modelDirectory, modelFileName).getCompilation() + .diagnosticResult(); + List errorDiagnosticsList = diagnosticResult.diagnostics().stream().filter + (r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR) + || r.diagnosticInfo().severity().equals(DiagnosticSeverity.WARNING)) + .collect(Collectors.toList()); + Assert.assertEquals(errorDiagnosticsList.size(), count); + return errorDiagnosticsList; + } + + + private void testDiagnostic(List errorDiagnosticsList, String[] codes, String[] messages, + String[] locations) { + for (int index = 0; index < errorDiagnosticsList.size(); index++) { + Diagnostic diagnostic = errorDiagnosticsList.get(index); + DiagnosticInfo diagnosticInfo = diagnostic.diagnosticInfo(); + Assert.assertEquals(diagnosticInfo.code(), codes[index]); + Assert.assertTrue(diagnosticInfo.messageFormat().startsWith(messages[index])); + String location = diagnostic.location().lineRange().toString(); + Assert.assertEquals(location, locations[index]); + } + } +} + diff --git a/compiler-plugin-test/src/test/java/io/ballerina/stdlib/persist/sql/compiler/TestUtils.java b/compiler-plugin-test/src/test/java/io/ballerina/stdlib/persist/sql/compiler/TestUtils.java new file mode 100644 index 0000000..f415b9a --- /dev/null +++ b/compiler-plugin-test/src/test/java/io/ballerina/stdlib/persist/sql/compiler/TestUtils.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2022, 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. + */ + +package io.ballerina.stdlib.persist.sql.compiler; + +import io.ballerina.projects.ProjectEnvironmentBuilder; +import io.ballerina.projects.environment.Environment; +import io.ballerina.projects.environment.EnvironmentBuilder; + +import java.nio.file.Path; +import java.nio.file.Paths; + +public class TestUtils { + public static ProjectEnvironmentBuilder getEnvironmentBuilder() { + Path distributionPath = Paths.get("../", "target", "ballerina-runtime").toAbsolutePath(); + Environment environment = EnvironmentBuilder.getBuilder().setBallerinaHome(distributionPath).build(); + return ProjectEnvironmentBuilder.getBuilder(environment); + } +} diff --git a/compiler-plugin-test/src/test/resources/project_1/Ballerina.toml b/compiler-plugin-test/src/test/resources/project_1/Ballerina.toml new file mode 100644 index 0000000..b748e48 --- /dev/null +++ b/compiler-plugin-test/src/test/resources/project_1/Ballerina.toml @@ -0,0 +1,7 @@ +[package] +org = "root" +name = "project_1" +version = "0.1.0" + +[persist] +datastore = "mysql" diff --git a/compiler-plugin-test/src/test/resources/project_1/persist/char.bal b/compiler-plugin-test/src/test/resources/project_1/persist/char.bal new file mode 100644 index 0000000..029f30c --- /dev/null +++ b/compiler-plugin-test/src/test/resources/project_1/persist/char.bal @@ -0,0 +1,37 @@ +import ballerina/persist as _; +import ballerina/time; +import ballerinax/persist.sql; + +public enum AppointmentStatus { + SCHEDULED = "SCHEDULED", + STARTED = "STARTED", + ENDED = "ENDED" +} + +public enum PatientGender { + MALE = "MALE", + FEMALE = "FEMALE" +} + +public type Appointment record {| + readonly int id; + @sql:Char {length: 100} + @sql:VarChar {length: 20} + string reason; + time:Civil appointmentTime; + AppointmentStatus status; + int _patientId; + int doctorId; +|}; + +public type Patient record {| + readonly int id; + @sql:Char {length: 0} + string name; + @sql:Char {length: 10} + int age; + string address; + string phoneNumber; + PatientGender gender; +|}; + diff --git a/compiler-plugin-test/src/test/resources/project_1/persist/decimal.bal b/compiler-plugin-test/src/test/resources/project_1/persist/decimal.bal new file mode 100644 index 0000000..d8cad75 --- /dev/null +++ b/compiler-plugin-test/src/test/resources/project_1/persist/decimal.bal @@ -0,0 +1,12 @@ +import ballerina/persist as _; +import ballerinax/persist.sql; + +public type Person record {| + readonly int id; + string name; + @sql:Decimal {precision: [10,2]} + int age; + @sql:Decimal {precision: [0,2]} + decimal salary; + decimal height; +|}; \ No newline at end of file diff --git a/compiler-plugin-test/src/test/resources/project_1/persist/entity_name_mapping.bal b/compiler-plugin-test/src/test/resources/project_1/persist/entity_name_mapping.bal new file mode 100644 index 0000000..15c7243 --- /dev/null +++ b/compiler-plugin-test/src/test/resources/project_1/persist/entity_name_mapping.bal @@ -0,0 +1,56 @@ +import ballerina/persist as _; +import ballerina/time; +import ballerinax/persist.sql; + +public enum AppointmentStatus { + SCHEDULED = "SCHEDULED", + STARTED = "STARTED", + ENDED = "ENDED" +} + +public enum PatientGender { + MALE = "MALE", + FEMALE = "FEMALE" +} + +@sql:Mapping {name: ""} +public type Appointment record {| + readonly int id; + string reason; + time:Civil appointmentTime; + AppointmentStatus status; + int _patientId; + int doctorId; +|}; + +@sql:Mapping {name: "Patient"} +public type Patient record {| + readonly int id; + string name; + int age; + string address; + string phoneNumber; + PatientGender gender; +|}; + +@sql:Mapping {name: "staff"} +public type Doctor record {| + readonly int id; + string name; + string specialty; + string phoneNumber; +|}; + +@sql:Mapping {name: "staff"} +public type Nurse record {| + readonly int id; + string name; + string phoneNumber; +|}; + +@sql:Mapping {name: "receptioninst"} +public type Receptionist record {| + readonly int id; + string name; + string phoneNumber; +|}; diff --git a/compiler-plugin-test/src/test/resources/project_1/persist/index.bal b/compiler-plugin-test/src/test/resources/project_1/persist/index.bal new file mode 100644 index 0000000..02b1c58 --- /dev/null +++ b/compiler-plugin-test/src/test/resources/project_1/persist/index.bal @@ -0,0 +1,29 @@ +import ballerina/persist as _; +import ballerinax/persist.sql; + +public type Person record {| + readonly int id; + string name; + @sql:Index {names: ["address"]} + Address address; + @sql:Index {names: ["favs"]} + string favColor; + @sql:Index {names: ["favs"]} + string favCar; + @sql:Index {names: ["email", "email"]} + string email; + @sql:Index {names: ["gender_idx", " "]} + string gender; + @sql:Index {names: [""]} + string nic; + int numOfChildren; + +|}; + +public type Address record {| + readonly int id; + string street; + string city; + string country; + Person? user; +|}; \ No newline at end of file diff --git a/compiler-plugin-test/src/test/resources/project_1/persist/relation1.bal b/compiler-plugin-test/src/test/resources/project_1/persist/relation1.bal new file mode 100644 index 0000000..10519fe --- /dev/null +++ b/compiler-plugin-test/src/test/resources/project_1/persist/relation1.bal @@ -0,0 +1,22 @@ +import ballerina/persist as _; +import ballerinax/persist.sql; + +public type Person record {| + readonly int nic; + readonly string email; + string name; + int age; + string city; + Car? car; +|}; + +public type Car record {| + readonly string plateNo; + string make; + string model; + int year; + string color; + int ownerNic; + @sql:Relation {refs: ["ownerNic"]} + Person owner; +|}; \ No newline at end of file diff --git a/compiler-plugin-test/src/test/resources/project_1/persist/relation2.bal b/compiler-plugin-test/src/test/resources/project_1/persist/relation2.bal new file mode 100644 index 0000000..a1d646e --- /dev/null +++ b/compiler-plugin-test/src/test/resources/project_1/persist/relation2.bal @@ -0,0 +1,21 @@ +import ballerina/persist as _; +import ballerinax/persist.sql; + +public type Person record {| + readonly int nic; + string name; + int age; + string city; + Car? car; +|}; + +public type Car record {| + readonly string plateNo; + string make; + string model; + int year; + string color; + string ownerNic; + @sql:Relation {refs: ["ownerNic"]} + Person owner; +|}; \ No newline at end of file diff --git a/compiler-plugin-test/src/test/resources/project_1/persist/relation3.bal b/compiler-plugin-test/src/test/resources/project_1/persist/relation3.bal new file mode 100644 index 0000000..74772f5 --- /dev/null +++ b/compiler-plugin-test/src/test/resources/project_1/persist/relation3.bal @@ -0,0 +1,21 @@ +import ballerina/persist as _; +import ballerinax/persist.sql; + +public type Person record {| + readonly int nic; + string name; + int age; + string city; + string carPlateNo; + @sql:Relation {refs: ["carPlateNo"]} + Car[] cars; +|}; + +public type Car record {| + readonly string plateNo; + string make; + string model; + int year; + string color; + Person owner; +|}; \ No newline at end of file diff --git a/compiler-plugin-test/src/test/resources/project_1/persist/relation4.bal b/compiler-plugin-test/src/test/resources/project_1/persist/relation4.bal new file mode 100644 index 0000000..e142fa7 --- /dev/null +++ b/compiler-plugin-test/src/test/resources/project_1/persist/relation4.bal @@ -0,0 +1,21 @@ +import ballerina/persist as _; +import ballerinax/persist.sql; + +public type Person record {| + readonly int nic; + string name; + int age; + string city; + string carPlateNo; + @sql:Relation {refs: ["carPlateNo"]} + Car? car; +|}; + +public type Car record {| + readonly string plateNo; + string make; + string model; + int year; + string color; + Person owner; +|}; \ No newline at end of file diff --git a/compiler-plugin-test/src/test/resources/project_1/persist/relation5.bal b/compiler-plugin-test/src/test/resources/project_1/persist/relation5.bal new file mode 100644 index 0000000..1b1993e --- /dev/null +++ b/compiler-plugin-test/src/test/resources/project_1/persist/relation5.bal @@ -0,0 +1,21 @@ +import ballerina/persist as _; +import ballerinax/persist.sql; + +public type Person record {| + readonly int nic; + string name; + int age; + string city; + Car? car; +|}; + +public type Car record {| + readonly string plateNo; + string make; + string model; + int year; + string color; + int ownNic; + @sql:Relation {refs: ["ownerNic"]} + Person owner; +|}; \ No newline at end of file diff --git a/compiler-plugin-test/src/test/resources/project_1/persist/relation6.bal b/compiler-plugin-test/src/test/resources/project_1/persist/relation6.bal new file mode 100644 index 0000000..5e752c9 --- /dev/null +++ b/compiler-plugin-test/src/test/resources/project_1/persist/relation6.bal @@ -0,0 +1,22 @@ +import ballerina/persist as _; +import ballerinax/persist.sql; + +public type Person record {| + readonly int nic; + readonly string email; + string name; + int age; + string city; + Car? car; +|}; + +public type Car record {| + readonly string plateNo; + string make; + string model; + int year; + string color; + int ownerNic; + @sql:Relation {refs: ["ownerNic", "ownerNic"]} + Person owner; +|}; \ No newline at end of file diff --git a/compiler-plugin-test/src/test/resources/project_1/persist/relation7.bal b/compiler-plugin-test/src/test/resources/project_1/persist/relation7.bal new file mode 100644 index 0000000..15f8e0c --- /dev/null +++ b/compiler-plugin-test/src/test/resources/project_1/persist/relation7.bal @@ -0,0 +1,24 @@ +import ballerina/persist as _; +import ballerinax/persist.sql; + +public type Person record {| + readonly int nic; + string name; + int age; + string city; + Car? car; + Car? driver; +|}; + +public type Car record {| + readonly string plateNo; + string make; + string model; + int year; + string color; + int ownerNic; + @sql:Relation {refs: ["ownerNic"]} + Person owner; + @sql:Relation {refs: ["ownerNic"]} + Person drivenBy; +|}; \ No newline at end of file diff --git a/compiler-plugin-test/src/test/resources/project_1/persist/table_name_mapping.bal b/compiler-plugin-test/src/test/resources/project_1/persist/table_name_mapping.bal new file mode 100644 index 0000000..85b52de --- /dev/null +++ b/compiler-plugin-test/src/test/resources/project_1/persist/table_name_mapping.bal @@ -0,0 +1,48 @@ +import ballerina/persist as _; +import ballerina/time; +import ballerinax/persist.sql; + +public enum AppointmentStatus { + SCHEDULED = "SCHEDULED", + STARTED = "STARTED", + ENDED = "ENDED" +} + +public enum PatientGender { + MALE = "MALE", + FEMALE = "FEMALE" +} + +public type Appointment record {| + readonly int id; + @sql:Mapping {name: "REASON"} + string reason; + time:Civil appointmentTime; + @sql:Mapping {name: ""} + AppointmentStatus status; +|}; + +public type Patient record {| + readonly int id; + string name; + int age; + @sql:Mapping {name: "Address"} + string address; + @sql:Mapping {name: "phoneNumber1"} + string phoneNumber; + @sql:Mapping {name: "phoneNumber1"} + string phoneNumber2; + PatientGender gender; +|}; + +public type Doctor record {| + readonly int id; + string name; + string specialty; + @sql:Mapping {name: "Address"} + string address; + @sql:Mapping {name: "phoneNumber"} + string phoneNumber; +|}; + + diff --git a/compiler-plugin-test/src/test/resources/project_1/persist/unique_index.bal b/compiler-plugin-test/src/test/resources/project_1/persist/unique_index.bal new file mode 100644 index 0000000..ed56c76 --- /dev/null +++ b/compiler-plugin-test/src/test/resources/project_1/persist/unique_index.bal @@ -0,0 +1,29 @@ +import ballerina/persist as _; +import ballerinax/persist.sql; + +public type Person record {| + readonly int id; + string name; + @sql:UniqueIndex {names: ["address"]} + Address address; + @sql:UniqueIndex {names: ["favs"]} + string favColor; + @sql:UniqueIndex {names: ["favs"]} + string favCar; + @sql:UniqueIndex {names: ["email", "email"]} + string email; + @sql:UniqueIndex {names: ["gender_idx", " "]} + string gender; + @sql:UniqueIndex {names: [""]} + string nic; + int numOfChildren; + +|}; + +public type Address record {| + readonly int id; + string street; + string city; + string country; + Person? user; +|}; \ No newline at end of file diff --git a/compiler-plugin-test/src/test/resources/project_1/persist/varchar.bal b/compiler-plugin-test/src/test/resources/project_1/persist/varchar.bal new file mode 100644 index 0000000..a3b7723 --- /dev/null +++ b/compiler-plugin-test/src/test/resources/project_1/persist/varchar.bal @@ -0,0 +1,37 @@ +import ballerina/persist as _; +import ballerina/time; +import ballerinax/persist.sql; + +public enum AppointmentStatus { + SCHEDULED = "SCHEDULED", + STARTED = "STARTED", + ENDED = "ENDED" +} + +public enum PatientGender { + MALE = "MALE", + FEMALE = "FEMALE" +} + +public type Appointment record {| + readonly int id; + @sql:VarChar {length: 20} + @sql:Char {length: 100} + string reason; + time:Civil appointmentTime; + AppointmentStatus status; + int _patientId; + int doctorId; +|}; + +public type Patient record {| + readonly int id; + @sql:VarChar {length: 0} + string name; + @sql:VarChar {length: 10} + int age; + string address; + string phoneNumber; + PatientGender gender; +|}; + diff --git a/compiler-plugin-test/src/test/resources/testng.xml b/compiler-plugin-test/src/test/resources/testng.xml index 6c5b030..f0a52f6 100644 --- a/compiler-plugin-test/src/test/resources/testng.xml +++ b/compiler-plugin-test/src/test/resources/testng.xml @@ -22,6 +22,7 @@ + diff --git a/compiler-plugin/build.gradle b/compiler-plugin/build.gradle index 5e6c343..5904a78 100644 --- a/compiler-plugin/build.gradle +++ b/compiler-plugin/build.gradle @@ -33,7 +33,8 @@ dependencies { implementation group: 'org.ballerinalang', name: 'ballerina-parser', version: "${ballerinaLangVersion}" implementation group: 'org.ballerinalang', name: 'toml-parser', version: "${ballerinaLangVersion}" implementation group: 'org.ballerinalang', name: 'formatter-core', version: "${ballerinaLangVersion}" - implementation group: 'io.ballerina.stdlib', name: 'persist-native', version: "${stdlibPersistVersion}" + implementation group: 'io.ballerina.stdlib', name: 'persist-native', version: "1.2.1-20240222-125000-4a9d764" + implementation group: 'io.ballerina.stdlib', name: 'persist-compiler-plugin', version: "1.2.1-20240222-125000-4a9d764" } def excludePattern = '**/module-info.java'