diff --git a/changelog.md b/changelog.md index e851f7b50..48de2d661 100644 --- a/changelog.md +++ b/changelog.md @@ -7,9 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased ### Changed -- Fixes an issue where client API is still generated even if all entities contain unsupported field(s) -- Fixes an issue where unique indexes are declared twice in script.sql in one-to-one associations -- Fixes an issue where the cardinality of the first association is taken as the cardinality of all the other associations between same entities +- Fix an issue where client API is still generated even if all entities contain unsupported field(s) +- Fix an issue where unique indexes are declared twice in script.sql in one-to-one associations +- Fix an issue where the cardinality of the first association is taken as the cardinality of all the other associations between same entities +- [Fix an issue where table name of the entity becomes empty when doc comments are above the entity definition](https://github.com/ballerina-platform/ballerina-library/issues/6497) ### Added - [Add introspection support for PostgreSQL databases](https://github.com/ballerina-platform/ballerina-library/issues/6333) diff --git a/persist-cli-tests/src/test/resources/test-src/input/tool_test_generate_73/persist/model.bal b/persist-cli-tests/src/test/resources/test-src/input/tool_test_generate_73/persist/model.bal index f218b2f59..6403946fe 100644 --- a/persist-cli-tests/src/test/resources/test-src/input/tool_test_generate_73/persist/model.bal +++ b/persist-cli-tests/src/test/resources/test-src/input/tool_test_generate_73/persist/model.bal @@ -42,3 +42,30 @@ public type Car record {| @sql:Relation {keys: ["ownerId"]} User owner; |}; + +# Description. +# +# + name - field description +# + age - field description +# + nic - field description +# + salary - field description +public type Person record {| + readonly string name; + int age; + string nic; + decimal salary; +|}; + +# Description. +# +# + name - field description +# + age - field description +# + nic - field description +# + salary - field description +@sql:Name {value: "people2"} +public type Person2 record {| + readonly string name; + int age; + string nic; + decimal salary; +|}; diff --git a/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/entities/persist_client.bal b/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/entities/persist_client.bal index 184897053..667f2e4ae 100644 --- a/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/entities/persist_client.bal +++ b/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/entities/persist_client.bal @@ -1,6 +1,8 @@ // AUTO-GENERATED FILE. DO NOT MODIFY. + // This file is an auto-generated file by Ballerina persistence layer for model. // It should not be modified by hand. + import ballerina/jballerina.java; import ballerina/persist; import ballerina/sql; @@ -10,6 +12,8 @@ import ballerinax/persist.sql as psql; const USER = "users"; const CAR = "cars"; +const PERSON = "people"; +const PERSON2 = "person2s"; public isolated client class Client { *persist:AbstractPersistClient; @@ -52,6 +56,28 @@ public isolated client class Client { }, keyFields: ["id"], joinMetadata: {owner: {entity: User, fieldName: "owner", refTable: "User", refColumns: ["id"], joinColumns: ["ownerId"], 'type: psql:ONE_TO_MANY}} + }, + [PERSON]: { + entityName: "Person", + tableName: "Person", + fieldMetadata: { + name: {columnName: "name"}, + age: {columnName: "age"}, + nic: {columnName: "nic"}, + salary: {columnName: "salary"} + }, + keyFields: ["name"] + }, + [PERSON2]: { + entityName: "Person2", + tableName: "people2", + fieldMetadata: { + name: {columnName: "name"}, + age: {columnName: "age"}, + nic: {columnName: "nic"}, + salary: {columnName: "salary"} + }, + keyFields: ["name"] } }; @@ -63,7 +89,9 @@ public isolated client class Client { self.dbClient = dbClient; self.persistClients = { [USER]: check new (dbClient, self.metadata.get(USER), psql:MYSQL_SPECIFICS), - [CAR]: check new (dbClient, self.metadata.get(CAR), psql:MYSQL_SPECIFICS) + [CAR]: check new (dbClient, self.metadata.get(CAR), psql:MYSQL_SPECIFICS), + [PERSON]: check new (dbClient, self.metadata.get(PERSON), psql:MYSQL_SPECIFICS), + [PERSON2]: check new (dbClient, self.metadata.get(PERSON2), psql:MYSQL_SPECIFICS) }; } @@ -145,6 +173,84 @@ public isolated client class Client { return result; } + isolated resource function get people(PersonTargetType targetType = <>, sql:ParameterizedQuery whereClause = ``, sql:ParameterizedQuery orderByClause = ``, sql:ParameterizedQuery limitClause = ``, sql:ParameterizedQuery groupByClause = ``) returns stream = @java:Method { + 'class: "io.ballerina.stdlib.persist.sql.datastore.MySQLProcessor", + name: "query" + } external; + + isolated resource function get people/[string name](PersonTargetType targetType = <>) returns targetType|persist:Error = @java:Method { + 'class: "io.ballerina.stdlib.persist.sql.datastore.MySQLProcessor", + name: "queryOne" + } external; + + isolated resource function post people(PersonInsert[] data) returns string[]|persist:Error { + psql:SQLClient sqlClient; + lock { + sqlClient = self.persistClients.get(PERSON); + } + _ = check sqlClient.runBatchInsertQuery(data); + return from PersonInsert inserted in data + select inserted.name; + } + + isolated resource function put people/[string name](PersonUpdate value) returns Person|persist:Error { + psql:SQLClient sqlClient; + lock { + sqlClient = self.persistClients.get(PERSON); + } + _ = check sqlClient.runUpdateQuery(name, value); + return self->/people/[name].get(); + } + + isolated resource function delete people/[string name]() returns Person|persist:Error { + Person result = check self->/people/[name].get(); + psql:SQLClient sqlClient; + lock { + sqlClient = self.persistClients.get(PERSON); + } + _ = check sqlClient.runDeleteQuery(name); + return result; + } + + isolated resource function get person2s(Person2TargetType targetType = <>, sql:ParameterizedQuery whereClause = ``, sql:ParameterizedQuery orderByClause = ``, sql:ParameterizedQuery limitClause = ``, sql:ParameterizedQuery groupByClause = ``) returns stream = @java:Method { + 'class: "io.ballerina.stdlib.persist.sql.datastore.MySQLProcessor", + name: "query" + } external; + + isolated resource function get person2s/[string name](Person2TargetType targetType = <>) returns targetType|persist:Error = @java:Method { + 'class: "io.ballerina.stdlib.persist.sql.datastore.MySQLProcessor", + name: "queryOne" + } external; + + isolated resource function post person2s(Person2Insert[] data) returns string[]|persist:Error { + psql:SQLClient sqlClient; + lock { + sqlClient = self.persistClients.get(PERSON2); + } + _ = check sqlClient.runBatchInsertQuery(data); + return from Person2Insert inserted in data + select inserted.name; + } + + isolated resource function put person2s/[string name](Person2Update value) returns Person2|persist:Error { + psql:SQLClient sqlClient; + lock { + sqlClient = self.persistClients.get(PERSON2); + } + _ = check sqlClient.runUpdateQuery(name, value); + return self->/person2s/[name].get(); + } + + isolated resource function delete person2s/[string name]() returns Person2|persist:Error { + Person2 result = check self->/person2s/[name].get(); + psql:SQLClient sqlClient; + lock { + sqlClient = self.persistClients.get(PERSON2); + } + _ = check sqlClient.runDeleteQuery(name); + return result; + } + remote isolated function queryNativeSQL(sql:ParameterizedQuery sqlQuery, typedesc rowType = <>) returns stream = @java:Method { 'class: "io.ballerina.stdlib.persist.sql.datastore.MySQLProcessor" } external; diff --git a/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/entities/persist_types.bal b/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/entities/persist_types.bal index 30b1fd89e..b847a8a68 100644 --- a/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/entities/persist_types.bal +++ b/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/entities/persist_types.bal @@ -14,6 +14,7 @@ public type User record {| UserGender gender; string nic; decimal? salary; + |}; public type UserOptionalized record {| @@ -69,3 +70,51 @@ public type CarUpdate record {| int ownerId?; |}; +public type Person record {| + readonly string name; + int age; + string nic; + decimal salary; +|}; + +public type PersonOptionalized record {| + string name?; + int age?; + string nic?; + decimal salary?; +|}; + +public type PersonTargetType typedesc; + +public type PersonInsert Person; + +public type PersonUpdate record {| + int age?; + string nic?; + decimal salary?; +|}; + +public type Person2 record {| + readonly string name; + int age; + string nic; + decimal salary; +|}; + +public type Person2Optionalized record {| + string name?; + int age?; + string nic?; + decimal salary?; +|}; + +public type Person2TargetType typedesc; + +public type Person2Insert Person2; + +public type Person2Update record {| + int age?; + string nic?; + decimal salary?; +|}; + diff --git a/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/entities/script.sql b/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/entities/script.sql index 6268c8bf1..abdb956e8 100644 --- a/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/entities/script.sql +++ b/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/entities/script.sql @@ -5,6 +5,24 @@ DROP TABLE IF EXISTS `cars`; DROP TABLE IF EXISTS `User`; +DROP TABLE IF EXISTS `people2`; +DROP TABLE IF EXISTS `Person`; + +CREATE TABLE `Person` ( + `name` VARCHAR(191) NOT NULL, + `age` INT NOT NULL, + `nic` VARCHAR(191) NOT NULL, + `salary` DECIMAL(65,30) NOT NULL, + PRIMARY KEY(`name`) +); + +CREATE TABLE `people2` ( + `name` VARCHAR(191) NOT NULL, + `age` INT NOT NULL, + `nic` VARCHAR(191) NOT NULL, + `salary` DECIMAL(65,30) NOT NULL, + PRIMARY KEY(`name`) +); CREATE TABLE `User` ( `id` INT NOT NULL, diff --git a/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/mssql_entities/persist_client.bal b/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/mssql_entities/persist_client.bal index 8c9f5498c..8f971b8d8 100644 --- a/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/mssql_entities/persist_client.bal +++ b/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/mssql_entities/persist_client.bal @@ -1,6 +1,8 @@ // AUTO-GENERATED FILE. DO NOT MODIFY. + // This file is an auto-generated file by Ballerina persistence layer for model. // It should not be modified by hand. + import ballerina/jballerina.java; import ballerina/persist; import ballerina/sql; @@ -10,6 +12,8 @@ import ballerinax/persist.sql as psql; const USER = "users"; const CAR = "cars"; +const PERSON = "people"; +const PERSON2 = "person2s"; public isolated client class Client { *persist:AbstractPersistClient; @@ -52,6 +56,28 @@ public isolated client class Client { }, keyFields: ["id"], joinMetadata: {owner: {entity: User, fieldName: "owner", refTable: "User", refColumns: ["id"], joinColumns: ["ownerId"], 'type: psql:ONE_TO_MANY}} + }, + [PERSON]: { + entityName: "Person", + tableName: "Person", + fieldMetadata: { + name: {columnName: "name"}, + age: {columnName: "age"}, + nic: {columnName: "nic"}, + salary: {columnName: "salary"} + }, + keyFields: ["name"] + }, + [PERSON2]: { + entityName: "Person2", + tableName: "people2", + fieldMetadata: { + name: {columnName: "name"}, + age: {columnName: "age"}, + nic: {columnName: "nic"}, + salary: {columnName: "salary"} + }, + keyFields: ["name"] } }; @@ -63,7 +89,9 @@ public isolated client class Client { self.dbClient = dbClient; self.persistClients = { [USER]: check new (dbClient, self.metadata.get(USER), psql:MSSQL_SPECIFICS), - [CAR]: check new (dbClient, self.metadata.get(CAR), psql:MSSQL_SPECIFICS) + [CAR]: check new (dbClient, self.metadata.get(CAR), psql:MSSQL_SPECIFICS), + [PERSON]: check new (dbClient, self.metadata.get(PERSON), psql:MSSQL_SPECIFICS), + [PERSON2]: check new (dbClient, self.metadata.get(PERSON2), psql:MSSQL_SPECIFICS) }; } @@ -145,6 +173,84 @@ public isolated client class Client { return result; } + isolated resource function get people(PersonTargetType targetType = <>, sql:ParameterizedQuery whereClause = ``, sql:ParameterizedQuery orderByClause = ``, sql:ParameterizedQuery limitClause = ``, sql:ParameterizedQuery groupByClause = ``) returns stream = @java:Method { + 'class: "io.ballerina.stdlib.persist.sql.datastore.MSSQLProcessor", + name: "query" + } external; + + isolated resource function get people/[string name](PersonTargetType targetType = <>) returns targetType|persist:Error = @java:Method { + 'class: "io.ballerina.stdlib.persist.sql.datastore.MSSQLProcessor", + name: "queryOne" + } external; + + isolated resource function post people(PersonInsert[] data) returns string[]|persist:Error { + psql:SQLClient sqlClient; + lock { + sqlClient = self.persistClients.get(PERSON); + } + _ = check sqlClient.runBatchInsertQuery(data); + return from PersonInsert inserted in data + select inserted.name; + } + + isolated resource function put people/[string name](PersonUpdate value) returns Person|persist:Error { + psql:SQLClient sqlClient; + lock { + sqlClient = self.persistClients.get(PERSON); + } + _ = check sqlClient.runUpdateQuery(name, value); + return self->/people/[name].get(); + } + + isolated resource function delete people/[string name]() returns Person|persist:Error { + Person result = check self->/people/[name].get(); + psql:SQLClient sqlClient; + lock { + sqlClient = self.persistClients.get(PERSON); + } + _ = check sqlClient.runDeleteQuery(name); + return result; + } + + isolated resource function get person2s(Person2TargetType targetType = <>, sql:ParameterizedQuery whereClause = ``, sql:ParameterizedQuery orderByClause = ``, sql:ParameterizedQuery limitClause = ``, sql:ParameterizedQuery groupByClause = ``) returns stream = @java:Method { + 'class: "io.ballerina.stdlib.persist.sql.datastore.MSSQLProcessor", + name: "query" + } external; + + isolated resource function get person2s/[string name](Person2TargetType targetType = <>) returns targetType|persist:Error = @java:Method { + 'class: "io.ballerina.stdlib.persist.sql.datastore.MSSQLProcessor", + name: "queryOne" + } external; + + isolated resource function post person2s(Person2Insert[] data) returns string[]|persist:Error { + psql:SQLClient sqlClient; + lock { + sqlClient = self.persistClients.get(PERSON2); + } + _ = check sqlClient.runBatchInsertQuery(data); + return from Person2Insert inserted in data + select inserted.name; + } + + isolated resource function put person2s/[string name](Person2Update value) returns Person2|persist:Error { + psql:SQLClient sqlClient; + lock { + sqlClient = self.persistClients.get(PERSON2); + } + _ = check sqlClient.runUpdateQuery(name, value); + return self->/person2s/[name].get(); + } + + isolated resource function delete person2s/[string name]() returns Person2|persist:Error { + Person2 result = check self->/person2s/[name].get(); + psql:SQLClient sqlClient; + lock { + sqlClient = self.persistClients.get(PERSON2); + } + _ = check sqlClient.runDeleteQuery(name); + return result; + } + remote isolated function queryNativeSQL(sql:ParameterizedQuery sqlQuery, typedesc rowType = <>) returns stream = @java:Method { 'class: "io.ballerina.stdlib.persist.sql.datastore.MSSQLProcessor" } external; diff --git a/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/mssql_entities/persist_types.bal b/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/mssql_entities/persist_types.bal index a84d6e36c..b847a8a68 100644 --- a/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/mssql_entities/persist_types.bal +++ b/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/mssql_entities/persist_types.bal @@ -70,3 +70,51 @@ public type CarUpdate record {| int ownerId?; |}; +public type Person record {| + readonly string name; + int age; + string nic; + decimal salary; +|}; + +public type PersonOptionalized record {| + string name?; + int age?; + string nic?; + decimal salary?; +|}; + +public type PersonTargetType typedesc; + +public type PersonInsert Person; + +public type PersonUpdate record {| + int age?; + string nic?; + decimal salary?; +|}; + +public type Person2 record {| + readonly string name; + int age; + string nic; + decimal salary; +|}; + +public type Person2Optionalized record {| + string name?; + int age?; + string nic?; + decimal salary?; +|}; + +public type Person2TargetType typedesc; + +public type Person2Insert Person2; + +public type Person2Update record {| + int age?; + string nic?; + decimal salary?; +|}; + diff --git a/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/mssql_entities/script.sql b/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/mssql_entities/script.sql index 2065d6a77..1abc6a335 100644 --- a/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/mssql_entities/script.sql +++ b/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/mssql_entities/script.sql @@ -5,6 +5,24 @@ DROP TABLE IF EXISTS [cars]; DROP TABLE IF EXISTS [User]; +DROP TABLE IF EXISTS [people2]; +DROP TABLE IF EXISTS [Person]; + +CREATE TABLE [Person] ( + [name] VARCHAR(191) NOT NULL, + [age] INT NOT NULL, + [nic] VARCHAR(191) NOT NULL, + [salary] DECIMAL(38,30) NOT NULL, + PRIMARY KEY([name]) +); + +CREATE TABLE [people2] ( + [name] VARCHAR(191) NOT NULL, + [age] INT NOT NULL, + [nic] VARCHAR(191) NOT NULL, + [salary] DECIMAL(38,30) NOT NULL, + PRIMARY KEY([name]) +); CREATE TABLE [User] ( [id] INT NOT NULL, diff --git a/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/postgresql_entities/persist_client.bal b/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/postgresql_entities/persist_client.bal index cd9fc65b9..8412b7d47 100644 --- a/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/postgresql_entities/persist_client.bal +++ b/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/postgresql_entities/persist_client.bal @@ -1,6 +1,8 @@ // AUTO-GENERATED FILE. DO NOT MODIFY. + // This file is an auto-generated file by Ballerina persistence layer for model. // It should not be modified by hand. + import ballerina/jballerina.java; import ballerina/persist; import ballerina/sql; @@ -10,6 +12,8 @@ import ballerinax/postgresql.driver as _; const USER = "users"; const CAR = "cars"; +const PERSON = "people"; +const PERSON2 = "person2s"; public isolated client class Client { *persist:AbstractPersistClient; @@ -52,6 +56,28 @@ public isolated client class Client { }, keyFields: ["id"], joinMetadata: {owner: {entity: User, fieldName: "owner", refTable: "User", refColumns: ["id"], joinColumns: ["ownerId"], 'type: psql:ONE_TO_MANY}} + }, + [PERSON]: { + entityName: "Person", + tableName: "Person", + fieldMetadata: { + name: {columnName: "name"}, + age: {columnName: "age"}, + nic: {columnName: "nic"}, + salary: {columnName: "salary"} + }, + keyFields: ["name"] + }, + [PERSON2]: { + entityName: "Person2", + tableName: "people2", + fieldMetadata: { + name: {columnName: "name"}, + age: {columnName: "age"}, + nic: {columnName: "nic"}, + salary: {columnName: "salary"} + }, + keyFields: ["name"] } }; @@ -63,7 +89,9 @@ public isolated client class Client { self.dbClient = dbClient; self.persistClients = { [USER]: check new (dbClient, self.metadata.get(USER), psql:POSTGRESQL_SPECIFICS), - [CAR]: check new (dbClient, self.metadata.get(CAR), psql:POSTGRESQL_SPECIFICS) + [CAR]: check new (dbClient, self.metadata.get(CAR), psql:POSTGRESQL_SPECIFICS), + [PERSON]: check new (dbClient, self.metadata.get(PERSON), psql:POSTGRESQL_SPECIFICS), + [PERSON2]: check new (dbClient, self.metadata.get(PERSON2), psql:POSTGRESQL_SPECIFICS) }; } @@ -145,6 +173,84 @@ public isolated client class Client { return result; } + isolated resource function get people(PersonTargetType targetType = <>, sql:ParameterizedQuery whereClause = ``, sql:ParameterizedQuery orderByClause = ``, sql:ParameterizedQuery limitClause = ``, sql:ParameterizedQuery groupByClause = ``) returns stream = @java:Method { + 'class: "io.ballerina.stdlib.persist.sql.datastore.PostgreSQLProcessor", + name: "query" + } external; + + isolated resource function get people/[string name](PersonTargetType targetType = <>) returns targetType|persist:Error = @java:Method { + 'class: "io.ballerina.stdlib.persist.sql.datastore.PostgreSQLProcessor", + name: "queryOne" + } external; + + isolated resource function post people(PersonInsert[] data) returns string[]|persist:Error { + psql:SQLClient sqlClient; + lock { + sqlClient = self.persistClients.get(PERSON); + } + _ = check sqlClient.runBatchInsertQuery(data); + return from PersonInsert inserted in data + select inserted.name; + } + + isolated resource function put people/[string name](PersonUpdate value) returns Person|persist:Error { + psql:SQLClient sqlClient; + lock { + sqlClient = self.persistClients.get(PERSON); + } + _ = check sqlClient.runUpdateQuery(name, value); + return self->/people/[name].get(); + } + + isolated resource function delete people/[string name]() returns Person|persist:Error { + Person result = check self->/people/[name].get(); + psql:SQLClient sqlClient; + lock { + sqlClient = self.persistClients.get(PERSON); + } + _ = check sqlClient.runDeleteQuery(name); + return result; + } + + isolated resource function get person2s(Person2TargetType targetType = <>, sql:ParameterizedQuery whereClause = ``, sql:ParameterizedQuery orderByClause = ``, sql:ParameterizedQuery limitClause = ``, sql:ParameterizedQuery groupByClause = ``) returns stream = @java:Method { + 'class: "io.ballerina.stdlib.persist.sql.datastore.PostgreSQLProcessor", + name: "query" + } external; + + isolated resource function get person2s/[string name](Person2TargetType targetType = <>) returns targetType|persist:Error = @java:Method { + 'class: "io.ballerina.stdlib.persist.sql.datastore.PostgreSQLProcessor", + name: "queryOne" + } external; + + isolated resource function post person2s(Person2Insert[] data) returns string[]|persist:Error { + psql:SQLClient sqlClient; + lock { + sqlClient = self.persistClients.get(PERSON2); + } + _ = check sqlClient.runBatchInsertQuery(data); + return from Person2Insert inserted in data + select inserted.name; + } + + isolated resource function put person2s/[string name](Person2Update value) returns Person2|persist:Error { + psql:SQLClient sqlClient; + lock { + sqlClient = self.persistClients.get(PERSON2); + } + _ = check sqlClient.runUpdateQuery(name, value); + return self->/person2s/[name].get(); + } + + isolated resource function delete person2s/[string name]() returns Person2|persist:Error { + Person2 result = check self->/person2s/[name].get(); + psql:SQLClient sqlClient; + lock { + sqlClient = self.persistClients.get(PERSON2); + } + _ = check sqlClient.runDeleteQuery(name); + return result; + } + remote isolated function queryNativeSQL(sql:ParameterizedQuery sqlQuery, typedesc rowType = <>) returns stream = @java:Method { 'class: "io.ballerina.stdlib.persist.sql.datastore.PostgreSQLProcessor" } external; diff --git a/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/postgresql_entities/persist_types.bal b/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/postgresql_entities/persist_types.bal index a84d6e36c..b847a8a68 100644 --- a/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/postgresql_entities/persist_types.bal +++ b/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/postgresql_entities/persist_types.bal @@ -70,3 +70,51 @@ public type CarUpdate record {| int ownerId?; |}; +public type Person record {| + readonly string name; + int age; + string nic; + decimal salary; +|}; + +public type PersonOptionalized record {| + string name?; + int age?; + string nic?; + decimal salary?; +|}; + +public type PersonTargetType typedesc; + +public type PersonInsert Person; + +public type PersonUpdate record {| + int age?; + string nic?; + decimal salary?; +|}; + +public type Person2 record {| + readonly string name; + int age; + string nic; + decimal salary; +|}; + +public type Person2Optionalized record {| + string name?; + int age?; + string nic?; + decimal salary?; +|}; + +public type Person2TargetType typedesc; + +public type Person2Insert Person2; + +public type Person2Update record {| + int age?; + string nic?; + decimal salary?; +|}; + diff --git a/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/postgresql_entities/script.sql b/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/postgresql_entities/script.sql index 884e3a5fd..c5401dca0 100644 --- a/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/postgresql_entities/script.sql +++ b/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/modules/postgresql_entities/script.sql @@ -5,6 +5,24 @@ DROP TABLE IF EXISTS "cars"; DROP TABLE IF EXISTS "User"; +DROP TABLE IF EXISTS "people2"; +DROP TABLE IF EXISTS "Person"; + +CREATE TABLE "Person" ( + "name" VARCHAR(191) NOT NULL, + "age" INT NOT NULL, + "nic" VARCHAR(191) NOT NULL, + "salary" DECIMAL(65,30) NOT NULL, + PRIMARY KEY("name") +); + +CREATE TABLE "people2" ( + "name" VARCHAR(191) NOT NULL, + "age" INT NOT NULL, + "nic" VARCHAR(191) NOT NULL, + "salary" DECIMAL(65,30) NOT NULL, + PRIMARY KEY("name") +); CREATE TABLE "User" ( "id" INT NOT NULL, diff --git a/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/persist/model.bal b/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/persist/model.bal index f218b2f59..6403946fe 100644 --- a/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/persist/model.bal +++ b/persist-cli-tests/src/test/resources/test-src/output/tool_test_generate_73/persist/model.bal @@ -42,3 +42,30 @@ public type Car record {| @sql:Relation {keys: ["ownerId"]} User owner; |}; + +# Description. +# +# + name - field description +# + age - field description +# + nic - field description +# + salary - field description +public type Person record {| + readonly string name; + int age; + string nic; + decimal salary; +|}; + +# Description. +# +# + name - field description +# + age - field description +# + nic - field description +# + salary - field description +@sql:Name {value: "people2"} +public type Person2 record {| + readonly string name; + int age; + string nic; + decimal salary; +|}; diff --git a/persist-cli/src/main/java/io/ballerina/persist/utils/BalProjectUtils.java b/persist-cli/src/main/java/io/ballerina/persist/utils/BalProjectUtils.java index 3e2e22e74..ab4d6be22 100644 --- a/persist-cli/src/main/java/io/ballerina/persist/utils/BalProjectUtils.java +++ b/persist-cli/src/main/java/io/ballerina/persist/utils/BalProjectUtils.java @@ -226,13 +226,13 @@ public static void populateEntities(Module.Builder moduleBuilder, SyntaxTree bal .typeDescriptor(); Optional entityMetadataNode = typeDefinitionNode.metadata(); entityBuilder.setTableName(typeDefinitionNode.typeName().text().trim()); - entityMetadataNode.ifPresent(value -> entityBuilder.setTableName( - readStringValueFromAnnotation( - new BalSyntaxUtils.AnnotationUtilRecord(value.annotations().stream().toList(), + String annotatedTableName = entityMetadataNode.map(metaData -> readStringValueFromAnnotation( + new BalSyntaxUtils.AnnotationUtilRecord(metaData.annotations().stream().toList(), BalSyntaxConstants.SQL_DB_NAME_ANNOTATION_NAME, - BalSyntaxConstants.ANNOTATION_VALUE_FIELD) - ) - )); + BalSyntaxConstants.ANNOTATION_VALUE_FIELD))).orElse(""); + if (!annotatedTableName.isEmpty()) { + entityBuilder.setTableName(annotatedTableName); + } if (recordDesc.toSourceCode().contains(UNSUPPORTED_TYPE_COMMENT_START)) { errStream.println("WARNING the entity '" + entityBuilder.getEntityName() + "' contains " + "unsupported data types. client api for this entity will not be generated.");