Skip to content

Commit

Permalink
Merge pull request #26 from kaneeldias/improve
Browse files Browse the repository at this point in the history
  • Loading branch information
kaneeldias authored Jul 19, 2023
2 parents 0d6a9b6 + f926040 commit 2b2c5b7
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 29 deletions.
10 changes: 5 additions & 5 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.1.0"
version = "1.1.1"
authors = ["Ballerina"]
keywords = ["persist", "sql", "mysql", "mssql", "sql-server"]
repository = "https://github.com/ballerina-platform/module-ballerinas-persist.sql"
Expand All @@ -15,11 +15,11 @@ graalvmCompatible = true
[[platform.java11.dependency]]
groupId = "io.ballerina.stdlib"
artifactId = "persist.sql-native"
version = "1.1.0"
path = "../native/build/libs/persist.sql-native-1.1.0.jar"
version = "1.1.1"
path = "../native/build/libs/persist.sql-native-1.1.1-SNAPSHOT.jar"

[[platform.java11.dependency]]
groupId = "io.ballerina.stdlib"
artifactId = "persist-native"
version = "1.1.0"
path = "./lib/persist-native-1.1.0.jar"
version = "1.1.1"
path = "./lib/persist-native-1.1.1-20230718-104500-b6791bc.jar"
4 changes: 2 additions & 2 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "persist"
version = "1.1.0"
version = "1.1.1"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand Down Expand Up @@ -433,7 +433,7 @@ modules = [
[[package]]
org = "ballerinax"
name = "persist.sql"
version = "1.1.0"
version = "1.1.1"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "persist"},
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ stdlibHttpVersion=2.9.1-20230712-161400-d16a019
stdlibSqlVersion=1.10.1-20230718-140700-bdc5c17

# Level 09
stdlibPersistVersion=1.1.0
stdlibPersistVersion=1.1.1-20230718-104500-b6791bc

# Ballerina external dependency
stdlibMysqlDriverVersion=1.4.1
Expand Down
63 changes: 63 additions & 0 deletions native/src/main/java/io/ballerina/stdlib/persist/sql/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* 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.
*/

package io.ballerina.stdlib.persist.sql;

import io.ballerina.runtime.api.PredefinedTypes;
import io.ballerina.runtime.api.creators.TypeCreator;
import io.ballerina.runtime.api.creators.ValueCreator;
import io.ballerina.runtime.api.types.RecordType;
import io.ballerina.runtime.api.utils.TypeUtils;
import io.ballerina.runtime.api.values.BArray;
import io.ballerina.runtime.api.values.BError;
import io.ballerina.runtime.api.values.BObject;
import io.ballerina.runtime.api.values.BStream;
import io.ballerina.runtime.api.values.BTypedesc;

import static io.ballerina.stdlib.persist.sql.Constants.PERSIST_SQL_STREAM;
import static io.ballerina.stdlib.persist.sql.ModuleUtils.getModule;

/**
* This class provides the SQL util methods for persistence.
*
* @since 1.1.0
*/
public class Utils {

private static BObject createPersistSQLStream(BStream sqlStream, BTypedesc targetType, BArray fields,
BArray includes, BArray typeDescriptions, BObject persistClient,
BError persistError) {
return ValueCreator.createObjectValue(getModule(), PERSIST_SQL_STREAM,
sqlStream, targetType, fields, includes, typeDescriptions, persistClient, persistError);
}

private static BStream createPersistSQLStreamValue(BTypedesc targetType, BObject persistSQLStream) {
RecordType streamConstraint =
(RecordType) TypeUtils.getReferredType(targetType.getDescribingType());
return ValueCreator.createStreamValue(
TypeCreator.createStreamType(streamConstraint, PredefinedTypes.TYPE_NULL), persistSQLStream);
}

public static BStream createPersistSQLStreamValue(BStream sqlStream, BTypedesc targetType, BArray fields,
BArray includes, BArray typeDescriptions, BObject persistClient,
BError persistError) {
BObject persistSQLStream = createPersistSQLStream(sqlStream, targetType, fields, includes, typeDescriptions,
persistClient, persistError);
return createPersistSQLStreamValue(targetType, persistSQLStream);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import io.ballerina.runtime.api.types.RecordType;
import io.ballerina.runtime.api.types.StreamType;
import io.ballerina.runtime.api.types.Type;
import io.ballerina.runtime.api.utils.TypeUtils;
import io.ballerina.runtime.api.values.BArray;
import io.ballerina.runtime.api.values.BError;
import io.ballerina.runtime.api.values.BObject;
Expand All @@ -37,19 +36,19 @@
import io.ballerina.runtime.api.values.BTypedesc;
import io.ballerina.stdlib.persist.Constants;
import io.ballerina.stdlib.persist.ModuleUtils;
import io.ballerina.stdlib.persist.sql.Utils;

import java.util.Map;

import static io.ballerina.stdlib.persist.Constants.ERROR;
import static io.ballerina.stdlib.persist.Constants.KEY_FIELDS;
import static io.ballerina.stdlib.persist.ErrorGenerator.wrapError;
import static io.ballerina.stdlib.persist.Utils.getEntity;
import static io.ballerina.stdlib.persist.Utils.getKey;
import static io.ballerina.stdlib.persist.Utils.getMetadata;
import static io.ballerina.stdlib.persist.Utils.getPersistClient;
import static io.ballerina.stdlib.persist.Utils.getRecordTypeWithKeyFields;
import static io.ballerina.stdlib.persist.Utils.getTransactionContextProperties;
import static io.ballerina.stdlib.persist.sql.Constants.PERSIST_SQL_STREAM;
import static io.ballerina.stdlib.persist.sql.ModuleUtils.getModule;

/**
* This class provides the SQL query processing implementations for persistence.
Expand All @@ -59,6 +58,8 @@
class SQLProcessor {

static BStream query(Environment env, BObject client, BTypedesc targetType) {
// This method will return `stream<targetType, persist:Error?>`

BString entity = getEntity(env);
BObject persistClient = getPersistClient(client, entity);
BArray keyFields = (BArray) persistClient.get(KEY_FIELDS);
Expand All @@ -70,6 +71,7 @@ static BStream query(Environment env, BObject client, BTypedesc targetType) {
PredefinedTypes.TYPE_NULL);

Map<String, Object> trxContextProperties = getTransactionContextProperties();
String strandName = env.getStrandName().isPresent() ? env.getStrandName().get() : null;

BArray[] metadata = getMetadata(recordType);
BArray fields = metadata[0];
Expand All @@ -78,27 +80,28 @@ static BStream query(Environment env, BObject client, BTypedesc targetType) {

Future balFuture = env.markAsync();
env.getRuntime().invokeMethodAsyncSequentially(
persistClient, Constants.RUN_READ_QUERY_METHOD,
null, null, new Callback() {
// Call `SQLClient.runReadQuery(
// typedesc<record {}> rowType, string[] fields = [], string[] include = []
// )`
// which returns `stream<record {}, sql:Error?>|persist:Error`

persistClient, Constants.RUN_READ_QUERY_METHOD, strandName, env.getStrandMetadata(), new Callback() {
@Override
public void notifySuccess(Object o) {
BStream sqlStream = (BStream) o;
BObject persistStream = ValueCreator.createObjectValue(
getModule(), PERSIST_SQL_STREAM, sqlStream, targetType,
fields, includes, typeDescriptions, persistClient, null
);

RecordType streamConstraint =
(RecordType) TypeUtils.getReferredType(targetType.getDescribingType());
balFuture.complete(
ValueCreator.createStreamValue(TypeCreator.createStreamType(streamConstraint,
PredefinedTypes.TYPE_NULL), persistStream)
);
if (o instanceof BStream) { // stream<record {}, sql:Error?>
BStream sqlStream = (BStream) o;
balFuture.complete(Utils.createPersistSQLStreamValue(sqlStream, targetType, fields,
includes, typeDescriptions, persistClient, null));
} else { // persist:Error
balFuture.complete(Utils.createPersistSQLStreamValue(null, targetType, fields, includes,
typeDescriptions, persistClient, (BError) o));
}
}

@Override
public void notifyFailure(BError bError) {
balFuture.complete(bError);
balFuture.complete(Utils.createPersistSQLStreamValue(null, targetType, fields, includes,
typeDescriptions, persistClient, wrapError(bError)));
}
}, trxContextProperties, streamTypeWithIdFields,
targetTypeWithIdFields, true, fields, true, includes, true
Expand All @@ -108,6 +111,7 @@ public void notifyFailure(BError bError) {
}

static Object queryOne(Environment env, BObject client, BArray path, BTypedesc targetType) {
// This method will return `targetType|persist:Error`

BString entity = getEntity(env);
BObject persistClient = getPersistClient(client, entity);
Expand All @@ -116,6 +120,7 @@ static Object queryOne(Environment env, BObject client, BArray path, BTypedesc t
RecordType recordType = (RecordType) targetType.getDescribingType();

Map<String, Object> trxContextProperties = getTransactionContextProperties();
String strandName = env.getStrandName().isPresent() ? env.getStrandName().get() : null;

RecordType recordTypeWithIdFields = getRecordTypeWithKeyFields(keyFields, recordType);
BTypedesc targetTypeWithIdFields = ValueCreator.createTypedescValue(recordTypeWithIdFields);
Expand All @@ -131,16 +136,22 @@ static Object queryOne(Environment env, BObject client, BArray path, BTypedesc t

Future balFuture = env.markAsync();
env.getRuntime().invokeMethodAsyncSequentially(
getPersistClient(client, entity), Constants.RUN_READ_BY_KEY_QUERY_METHOD,
null, null, new Callback() {
// Call `SQLClient.runReadByKeyQuery(
// typedesc<record {}> rowType, typedesc<record {}> rowTypeWithIdFields, anydata key,
// string[] fields = [], string[] include = [], typedesc<record {}>[] typeDescriptions = []
// )`
// which returns `record {}|persist:Error`

getPersistClient(client, entity), Constants.RUN_READ_BY_KEY_QUERY_METHOD, strandName,
env.getStrandMetadata(), new Callback() {
@Override
public void notifySuccess(Object o) {
balFuture.complete(o);
}

@Override
public void notifyFailure(BError bError) {
balFuture.complete(bError);
balFuture.complete(wrapError(bError));
}
}, trxContextProperties, unionType,
targetType, true, targetTypeWithIdFields, true, key, true, fields, true, includes, true,
Expand Down

0 comments on commit 2b2c5b7

Please sign in to comment.