Skip to content

Commit

Permalink
Merge pull request #370 from hasathcharu/annotation-bug
Browse files Browse the repository at this point in the history
Fix the issue where table name is not mapped correctly
  • Loading branch information
daneshk authored May 7, 2024
2 parents c6df8c0 + 2bef5a4 commit 65bb326
Show file tree
Hide file tree
Showing 13 changed files with 584 additions and 12 deletions.
7 changes: 4 additions & 3 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
|};
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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"]
}
};

Expand All @@ -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)
};
}

Expand Down Expand Up @@ -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<targetType, persist:Error?> = @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<targetType, persist:Error?> = @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<record {}> rowType = <>) returns stream<rowType, persist:Error?> = @java:Method {
'class: "io.ballerina.stdlib.persist.sql.datastore.MySQLProcessor"
} external;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public type User record {|
UserGender gender;
string nic;
decimal? salary;

|};

public type UserOptionalized record {|
Expand Down Expand Up @@ -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<PersonOptionalized>;

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<Person2Optionalized>;

public type Person2Insert Person2;

public type Person2Update record {|
int age?;
string nic?;
decimal salary?;
|};

Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading

0 comments on commit 65bb326

Please sign in to comment.