Skip to content

Commit

Permalink
Merge pull request #91 from warunalakshitha/syncMaster
Browse files Browse the repository at this point in the history
Migrate runtime APIs for Java 21
  • Loading branch information
warunalakshitha authored Nov 16, 2024
2 parents 5aeac65 + fd229a5 commit b1b2218
Show file tree
Hide file tree
Showing 28 changed files with 1,511 additions and 187 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build-timestamped-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Set up JDK 21
uses: actions/setup-java@v2
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: 21.0.3
Expand All @@ -44,9 +44,9 @@ jobs:
run: |
./gradlew clean build publishAllPublicationsToGitHubPackagesRepository --scan --no-daemon
- name: Generate CodeCov Report
uses: codecov/codecov-action@v2
uses: codecov/codecov-action@v3
- name: Upload Artifact
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: ballerina-runtime
path: target/ballerina-runtime/
Expand Down
16 changes: 8 additions & 8 deletions .github/workflows/publish-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ jobs:
runs-on: ubuntu-latest
if: github.repository_owner == 'ballerina-platform'
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Set up JDK 21
uses: actions/setup-java@v2
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: 21.0.3
Expand All @@ -27,13 +27,13 @@ jobs:
- name: Create lib directory if not exists
run: mkdir -p ballerina/lib
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
uses: aquasecurity/trivy-action@0.23.0
with:
scan-type: 'rootfs'
scan-ref: '/github/workspace/ballerina/lib'
format: 'table'
timeout: '10m0s'
exit-code: '1'
scan-type: "rootfs"
scan-ref: "/github/workspace/ballerina/lib"
format: "table"
timeout: "10m0s"
exit-code: "1"
- name: Set version env variable
run: echo "VERSION=$((grep -w 'version' | cut -d= -f2) < gradle.properties | rev | cut --complement -d- -f1 | rev)" >> $GITHUB_ENV
- name: Pre release dependency version update
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish-snapshot-nexus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ jobs:
runs-on: ubuntu-latest
if: github.repository_owner == 'ballerina-platform'
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Set up JDK 21
uses: actions/setup-java@v2
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: 21.0.3
Expand Down
6 changes: 3 additions & 3 deletions ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
org = "ballerinax"
name = "persist.sql"
version = "1.4.0"
version = "1.4.1"
authors = ["Ballerina"]
keywords = ["persist", "sql", "mysql", "mssql", "sql-server"]
repository = "https://github.com/ballerina-platform/module-ballerinax-persist.sql"
Expand All @@ -15,8 +15,8 @@ graalvmCompatible = true
[[platform.java21.dependency]]
groupId = "io.ballerina.stdlib"
artifactId = "persist.sql-native"
version = "1.4.0"
path = "../native/build/libs/persist.sql-native-1.4.0.jar"
version = "1.4.1"
path = "../native/build/libs/persist.sql-native-1.4.1.jar"

[[platform.java21.dependency]]
groupId = "io.ballerina.stdlib"
Expand Down
2 changes: 1 addition & 1 deletion ballerina/CompilerPlugin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ id = "persist.sql-compiler-plugin"
class = "io.ballerina.stdlib.persist.sql.compiler.PersistSqlCompilerPlugin"

[[dependency]]
path = "../compiler-plugin/build/libs/persist.sql-compiler-plugin-1.4.0.jar"
path = "../compiler-plugin/build/libs/persist.sql-compiler-plugin-1.4.1.jar"

[[dependency]]
path = "./lib/persist-native-1.4.0.jar"
Expand Down
2 changes: 1 addition & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ modules = [
[[package]]
org = "ballerinax"
name = "persist.sql"
version = "1.4.0"
version = "1.4.1"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "log"},
Expand Down
56 changes: 56 additions & 0 deletions ballerina/sql_client.bal
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,62 @@ public isolated client class SQLClient {
}
}

# Check whether associated entries exist for the given record.
#
# + 'object - The record to which the retrieved records should be appended
# + fields - The fields to be retrieved
# + include - The relations to be retrieved (SQL `JOINs` to be performed)
# + return - `()` if the operation is performed successfully or a `persist:Error` if the operation fails
public isolated function verifyEntityAssociation(anydata 'object, string[] fields, string[] include) returns persist:Error? {
if 'object !is record {} {
return error persist:Error("The 'object' parameter should be a record");
}

do {
// check the values of included entities are ()
foreach string joinKey in self.getJoinFields(include) {
JoinMetadata joinMetadata = self.joinMetadata.get(joinKey);
anydata associatedEntity = 'object.get(joinMetadata.fieldName);
if associatedEntity is record {} {
// check if the fields are empty in the associated record.
map<anydata> nonEmptyAssocEntity = associatedEntity.filter(value => value != ());
// If the associated entity has non-empty fields, then the association is already verified.
if nonEmptyAssocEntity.length() > 0 {
continue;
}

// check if the associated record values contain the foreign fields, if so, we can skip the query.
boolean hasKeys = true;
foreach string refColumn in joinMetadata.refColumns {
if !associatedEntity.hasKey(refColumn) {
hasKeys = false;
break;
}
}
if hasKeys {
'object[joinMetadata.fieldName] = ();
continue;
}
// construct the query to check whether the associated entries are exists
sql:ParameterizedQuery query = ``;
map<string> whereFilter = check self.getManyRelationWhereFilter('object, joinMetadata);
query = sql:queryConcat(
` SELECT COUNT(*) AS count`,
` FROM `, stringToParameterizedQuery(self.escape(joinMetadata.refTable)),
` WHERE`, check self.getWhereClauses(whereFilter, true)
);
// execute the query and check the count of the associated entries
int count = check self.dbClient->queryRow(query);
if count == 0 {
'object[joinMetadata.fieldName] = ();
}
}
}
} on fail error err {
return error persist:Error(err.message(), err);
}
}

public isolated function getKeyFields() returns string[] {
return self.keyFields;
}
Expand Down
2 changes: 2 additions & 0 deletions ballerina/stream_types.bal
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public class PersistSQLStream {
return <persist:Error>error(value.message());
}
check (<SQLClient>self.persistClient).getManyRelations(value, self.fields, self.include, self.typeDescriptions);
// verifies the entity association whether associated entity is available in the database.
check (<SQLClient>self.persistClient).verifyEntityAssociation(value, self.fields, self.include);

string[] keyFields = (<SQLClient>self.persistClient).getKeyFields();
foreach string keyField in keyFields {
Expand Down
73 changes: 73 additions & 0 deletions ballerina/tests/api_subscription_types.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) 2024 WSO2 LLC. (http://www.wso2.org).
//
// WSO2 LLC. 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.

public type Subscription record {|
readonly string subscriptionId;
string userName;
string apimetadataApiId;
string apimetadataOrgId;
|};

public type SubscriptionOptionalized record {|
string subscriptionId?;
string userName?;
string apimetadataApiId?;
string apimetadataOrgId?;
|};

public type SubscriptionWithRelations record {|
*SubscriptionOptionalized;
ApiMetadataOptionalized apimetadata?;
|};

public type SubscriptionTargetType typedesc<SubscriptionWithRelations>;

public type SubscriptionInsert Subscription;

public type SubscriptionUpdate record {|
string userName?;
string apimetadataApiId?;
string apimetadataOrgId?;
|};

public type ApiMetadata record {|
readonly string apiId;
readonly string orgId;
string apiName;
string metadata;

|};

public type ApiMetadataOptionalized record {|
string apiId?;
string orgId?;
string apiName?;
string metadata?;
|};

public type ApiMetadataWithRelations record {|
*ApiMetadataOptionalized;
SubscriptionOptionalized subscription?;
|};

public type ApiMetadataTargetType typedesc<ApiMetadataWithRelations>;

public type ApiMetadataInsert ApiMetadata;

public type ApiMetadataUpdate record {|
string apiName?;
string metadata?;
|};
118 changes: 118 additions & 0 deletions ballerina/tests/h2-optional-assoc-tests.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright (c) 2024 WSO2 LLC. (http://www.wso2.org).
//
// WSO2 LLC. 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/test;
import ballerina/persist;

@test:Config {
groups: ["assoc", "h2"]
}
function h2APMNoRelationsTest() returns error? {
H2ApimClient apimClient = check new ();

[string, string][] metaId = check apimClient->/apimetadata.post([{
apiId: "123457",
orgId: "wso2",
apiName: "abc",
metadata: "metadata"
}]);
test:assertEquals(metaId, [["123457", "wso2"]]);

stream<H2ApimWithSubscriptions, persist:Error?> streamResult = apimClient->/apimetadata();
H2ApimWithSubscriptions[] apimResults = check from H2ApimWithSubscriptions apiMetadata in streamResult
select apiMetadata;

test:assertEquals(apimResults.length(), 1);
test:assertEquals(apimResults[0].subscription, ());
check apimClient.close();
}

@test:Config {
groups: ["assoc", "h2"],
dependsOn: [mssqlAPMNoRelationsTest, mssqlAPMWithRelationsTest]
}
function h2APMWithoutRelationsTest() returns error? {
H2ApimClient apimClient = check new ();

stream<H2ApimWithoutSubscriptions, persist:Error?> streamResult = apimClient->/apimetadata();
H2ApimWithoutSubscriptions[] apimResults = check from H2ApimWithoutSubscriptions apiMetadata in streamResult
select apiMetadata;

test:assertEquals(apimResults.length(), 2);
test:assertEquals(apimResults[0], {
apiId: "123457",
orgId: "wso2",
apiName: "abc",
metadata: "metadata"
});
check apimClient.close();
}

@test:Config {
groups: ["assoc", "h2"],
dependsOn: [h2APMNoRelationsTest]
}
function h2APMWithRelationsTest() returns error? {
H2ApimClient apimClient = check new ();

[string, string][] metaId = check apimClient->/apimetadata.post([{
apiId: "123458",
orgId: "wso2",
apiName: "abc",
metadata: "metadata"
}]);
test:assertEquals(metaId, [["123458", "wso2"]]);

string[] subId = check apimClient->/subscriptions.post([{
subscriptionId: "123",
userName: "ballerina",
apimetadataApiId: "123458",
apimetadataOrgId: "wso2"
}]);
test:assertEquals(subId, ["123"]);

stream<H2ApimWithSubscriptions, persist:Error?> streamResult = apimClient->/apimetadata();
H2ApimWithSubscriptions[] apimResults = check from H2ApimWithSubscriptions apiMetadata in streamResult
select apiMetadata;

test:assertEquals(apimResults.length(), 2);

test:assertEquals(apimResults[0].subscription, ());
test:assertEquals(apimResults[1].subscription, {
subscriptionId: "123",
apimetadataApiId: "123458"
});
check apimClient.close();
}

type H2ApimWithSubscriptions record {|
string apiId;
string orgId;
string apiName;
string metadata;
record {|
string subscriptionId;
string apimetadataApiId;
|} subscription?;
|};


type H2ApimWithoutSubscriptions record {|
string apiId;
string orgId;
string apiName;
string metadata;
|};
Loading

0 comments on commit b1b2218

Please sign in to comment.