Skip to content

Commit

Permalink
Merge pull request #71 from daneshk/mssql-bulk
Browse files Browse the repository at this point in the history
Fix the bulk insert failure in sql modules
  • Loading branch information
daneshk authored Jul 5, 2024
2 parents e95b8cd + 334190a commit b162813
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 40 deletions.
7 changes: 1 addition & 6 deletions ballerina/sql_client.bal
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,7 @@ public isolated client class SQLClient {
if columnCount > 0 {
params = sql:queryConcat(params, `,`);
}

if 'object[key] is () {
params = sql:queryConcat(params, `NULL`);
} else {
params = sql:queryConcat(params, `${<sql:Value>'object[key]}`);
}
params = sql:queryConcat(params, `${<sql:Value>'object[key]}`);
columnCount = columnCount + 1;
}
params = sql:queryConcat(params, `)`);
Expand Down
56 changes: 52 additions & 4 deletions ballerina/tests/init-tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,14 @@ AllTypes allTypes3 = {
civilType: {year: 1999, month: 11, day: 3, hour: 12, minute: 32, second: 34},
booleanTypeOptional: (),
intTypeOptional: (),
floatTypeOptional: (),
// Passing () for float, date, time, civil types are not supported in MSSQL connector.
// Issue: https://github.com/ballerina-platform/ballerina-library/issues/6562
floatTypeOptional: 66.0,
decimalTypeOptional: (),
stringTypeOptional: (),
dateTypeOptional: (),
timeOfDayTypeOptional: (),
civilTypeOptional: (),
dateTypeOptional: {year: 1293, month: 11, day: 3},
timeOfDayTypeOptional: {hour: 19, minute: 32, second: 34},
civilTypeOptional: {year: 1989, month: 11, day: 3, hour: 12, minute: 32, second: 34},
enumType: "TYPE_1",
enumTypeOptional: ()
};
Expand Down Expand Up @@ -427,6 +429,52 @@ AllTypes allTypes3Expected = {
enumTypeOptional: allTypes3.enumTypeOptional
};

AllTypes allTypes4 = {
id: 4,
booleanType: true,
intType: 35,
floatType: 63.0,
decimalType: 233.44,
stringType: "test2",
byteArrayType: base16 `55 EE 66 AF 77 AB`,
dateType: {year: 1996, month: 11, day: 3},
timeOfDayType: {hour: 17, minute: 32, second: 34},
civilType: {year: 1999, month: 11, day: 3, hour: 12, minute: 32, second: 34},
booleanTypeOptional: true,
intTypeOptional: 6,
floatTypeOptional: 66.0,
decimalTypeOptional: 233.44,
stringTypeOptional: "test2",
dateTypeOptional: {year: 1293, month: 11, day: 3},
timeOfDayTypeOptional: {hour: 19, minute: 32, second: 34},
civilTypeOptional: {year: 1989, month: 11, day: 3, hour: 12, minute: 32, second: 34},
enumType: "TYPE_1",
enumTypeOptional: "TYPE_3"
};

AllTypes allTypes4Expected = {
id: allTypes4.id,
booleanType: allTypes4.booleanType,
intType: allTypes4.intType,
floatType: allTypes4.floatType,
decimalType: allTypes4.decimalType,
stringType: allTypes4.stringType,
byteArrayType: allTypes4.byteArrayType,
dateType: allTypes4.dateType,
timeOfDayType: allTypes4.timeOfDayType,
civilType: allTypes4.civilType,
booleanTypeOptional: allTypes4.booleanTypeOptional,
intTypeOptional: allTypes4.intTypeOptional,
floatTypeOptional: allTypes4.floatTypeOptional,
decimalTypeOptional: allTypes4.decimalTypeOptional,
stringTypeOptional: allTypes4.stringTypeOptional,
dateTypeOptional: allTypes4.dateTypeOptional,
timeOfDayTypeOptional: allTypes4.timeOfDayTypeOptional,
civilTypeOptional: allTypes4.civilTypeOptional,
enumType: allTypes4.enumType,
enumTypeOptional: allTypes4.enumTypeOptional
};

AllTypes allTypes1Updated = {
id: 1,
booleanType: true,
Expand Down
45 changes: 35 additions & 10 deletions ballerina/tests/mssql-all-types-tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,24 @@ function mssqlAllTypesCreateTest() returns error? {
@test:Config {
groups: ["all-types", "mssql"]
}
function mssqlAllTypesCreateOptionalTest() returns error? {
function mssqlAllTypesCreateMixedTest() returns error? {
MSSQLTestEntitiesClient testEntitiesClient = check new ();

int[] ids = check testEntitiesClient->/alltypes.post([allTypes3]);
test:assertEquals(ids, [allTypes3.id]);
int[] ids = check testEntitiesClient->/alltypes.post([allTypes3, allTypes4]);
test:assertEquals(ids, [allTypes3.id, allTypes4.id]);

AllTypes allTypesRetrieved = check testEntitiesClient->/alltypes/[allTypes3.id].get();
test:assertEquals(allTypesRetrieved, allTypes3Expected);

allTypesRetrieved = check testEntitiesClient->/alltypes/[allTypes4.id].get();
test:assertEquals(allTypesRetrieved, allTypes4Expected);

check testEntitiesClient.close();
}

@test:Config {
groups: ["all-types", "mssql"],
dependsOn: [mssqlAllTypesCreateTest, mssqlAllTypesCreateOptionalTest]
dependsOn: [mssqlAllTypesCreateTest, mssqlAllTypesCreateMixedTest]
}
function mssqlAllTypesReadTest() returns error? {
MSSQLTestEntitiesClient testEntitiesClient = check new ();
Expand All @@ -61,13 +64,13 @@ function mssqlAllTypesReadTest() returns error? {
AllTypes[] allTypes = check from AllTypes allTypesRecord in allTypesStream
select allTypesRecord;

test:assertEquals(allTypes, [allTypes1Expected, allTypes2Expected, allTypes3Expected]);
test:assertEquals(allTypes, [allTypes1Expected, allTypes2Expected, allTypes3Expected, allTypes4Expected]);
check testEntitiesClient.close();
}

@test:Config {
groups: ["all-types", "mssql", "dependent"],
dependsOn: [mssqlAllTypesCreateTest, mssqlAllTypesCreateOptionalTest]
dependsOn: [mssqlAllTypesCreateTest, mssqlAllTypesCreateMixedTest]
}
function mssqlAllTypesReadDependentTest() returns error? {
MSSQLTestEntitiesClient testEntitiesClient = check new ();
Expand Down Expand Up @@ -133,14 +136,33 @@ function mssqlAllTypesReadDependentTest() returns error? {
dateTypeOptional: allTypes3Expected.dateTypeOptional,
timeOfDayTypeOptional: allTypes3Expected.timeOfDayTypeOptional,
civilTypeOptional: allTypes3Expected.civilTypeOptional
},
{
booleanType: allTypes4Expected.booleanType,
intType: allTypes4Expected.intType,
floatType: allTypes4Expected.floatType,
decimalType: allTypes4Expected.decimalType,
stringType: allTypes4Expected.stringType,
byteArrayType: allTypes4Expected.byteArrayType,
dateType: allTypes4Expected.dateType,
timeOfDayType: allTypes4Expected.timeOfDayType,
civilType: allTypes4Expected.civilType,
booleanTypeOptional: allTypes4Expected.booleanTypeOptional,
intTypeOptional: allTypes4Expected.intTypeOptional,
floatTypeOptional: allTypes4Expected.floatTypeOptional,
decimalTypeOptional: allTypes4Expected.decimalTypeOptional,
stringTypeOptional: allTypes4Expected.stringTypeOptional,
dateTypeOptional: allTypes4Expected.dateTypeOptional,
timeOfDayTypeOptional: allTypes4Expected.timeOfDayTypeOptional,
civilTypeOptional: allTypes4Expected.civilTypeOptional
}
]);
check testEntitiesClient.close();
}

@test:Config {
groups: ["all-types", "mssql"],
dependsOn: [mssqlAllTypesCreateTest, mssqlAllTypesCreateOptionalTest]
dependsOn: [mssqlAllTypesCreateTest, mssqlAllTypesCreateMixedTest]
}
function mssqlAllTypesReadOneTest() returns error? {
MSSQLTestEntitiesClient testEntitiesClient = check new ();
Expand All @@ -154,6 +176,9 @@ function mssqlAllTypesReadOneTest() returns error? {
allTypesRetrieved = check testEntitiesClient->/alltypes/[allTypes3.id].get();
test:assertEquals(allTypesRetrieved, allTypes3Expected);

allTypesRetrieved = check testEntitiesClient->/alltypes/[allTypes4.id].get();
test:assertEquals(allTypesRetrieved, allTypes4Expected);

check testEntitiesClient.close();
}

Expand All @@ -163,9 +188,9 @@ function mssqlAllTypesReadOneTest() returns error? {
function mssqlAllTypesReadOneTestNegative() returns error? {
MSSQLTestEntitiesClient testEntitiesClient = check new ();

AllTypes|persist:Error allTypesRetrieved = testEntitiesClient->/alltypes/[4].get();
AllTypes|persist:Error allTypesRetrieved = testEntitiesClient->/alltypes/[5].get();
if allTypesRetrieved is persist:NotFoundError {
test:assertEquals(allTypesRetrieved.message(), "A record with the key '4' does not exist for the entity 'AllTypes'.");
test:assertEquals(allTypesRetrieved.message(), "A record with the key '5' does not exist for the entity 'AllTypes'.");
}
else {
test:assertFail("persist:NotFoundError expected.");
Expand Down Expand Up @@ -223,6 +248,6 @@ function mssqlAllTypesDeleteTest() returns error? {
AllTypes[] allTypesCollection = check from AllTypes allTypesRecord in allTypesStream
select allTypesRecord;

test:assertEquals(allTypesCollection, [allTypes1UpdatedExpected, allTypes3Expected]);
test:assertEquals(allTypesCollection, [allTypes1UpdatedExpected, allTypes3Expected, allTypes4Expected]);
check testEntitiesClient.close();
}
45 changes: 35 additions & 10 deletions ballerina/tests/mysql-all-types-tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,24 @@ function mysqlAllTypesCreateTest() returns error? {
@test:Config {
groups: ["all-types", "mysql"]
}
function mysqlAllTypesCreateOptionalTest() returns error? {
function mysqlAllTypesCreateMixedTest() returns error? {
MySQLTestEntitiesClient testEntitiesClient = check new ();

int[] ids = check testEntitiesClient->/alltypes.post([allTypes3]);
test:assertEquals(ids, [allTypes3.id]);
int[] ids = check testEntitiesClient->/alltypes.post([allTypes3, allTypes4]);
test:assertEquals(ids, [allTypes3.id, allTypes4.id]);

AllTypes allTypesRetrieved = check testEntitiesClient->/alltypes/[allTypes3.id].get();
test:assertEquals(allTypesRetrieved, allTypes3Expected);

allTypesRetrieved = check testEntitiesClient->/alltypes/[allTypes4.id].get();
test:assertEquals(allTypesRetrieved, allTypes4Expected);

check testEntitiesClient.close();
}

@test:Config {
groups: ["all-types", "mysql"],
dependsOn: [mysqlAllTypesCreateTest, mysqlAllTypesCreateOptionalTest]
dependsOn: [mysqlAllTypesCreateTest, mysqlAllTypesCreateMixedTest]
}
function mysqlAllTypesReadTest() returns error? {
MySQLTestEntitiesClient testEntitiesClient = check new ();
Expand All @@ -61,13 +64,13 @@ function mysqlAllTypesReadTest() returns error? {
AllTypes[] allTypes = check from AllTypes allTypesRecord in allTypesStream
select allTypesRecord;

test:assertEquals(allTypes, [allTypes1Expected, allTypes2Expected, allTypes3Expected]);
test:assertEquals(allTypes, [allTypes1Expected, allTypes2Expected, allTypes3Expected, allTypes4Expected]);
check testEntitiesClient.close();
}

@test:Config {
groups: ["all-types", "mysql", "dependent"],
dependsOn: [mysqlAllTypesCreateTest, mysqlAllTypesCreateOptionalTest]
dependsOn: [mysqlAllTypesCreateTest, mysqlAllTypesCreateMixedTest]
}
function mysqlAllTypesReadDependentTest() returns error? {
MySQLTestEntitiesClient testEntitiesClient = check new ();
Expand Down Expand Up @@ -133,14 +136,33 @@ function mysqlAllTypesReadDependentTest() returns error? {
dateTypeOptional: allTypes3Expected.dateTypeOptional,
timeOfDayTypeOptional: allTypes3Expected.timeOfDayTypeOptional,
civilTypeOptional: allTypes3Expected.civilTypeOptional
},
{
booleanType: allTypes4Expected.booleanType,
intType: allTypes4Expected.intType,
floatType: allTypes4Expected.floatType,
decimalType: allTypes4Expected.decimalType,
stringType: allTypes4Expected.stringType,
byteArrayType: allTypes4Expected.byteArrayType,
dateType: allTypes4Expected.dateType,
timeOfDayType: allTypes4Expected.timeOfDayType,
civilType: allTypes4Expected.civilType,
booleanTypeOptional: allTypes4Expected.booleanTypeOptional,
intTypeOptional: allTypes4Expected.intTypeOptional,
floatTypeOptional: allTypes4Expected.floatTypeOptional,
decimalTypeOptional: allTypes4Expected.decimalTypeOptional,
stringTypeOptional: allTypes4Expected.stringTypeOptional,
dateTypeOptional: allTypes4Expected.dateTypeOptional,
timeOfDayTypeOptional: allTypes4Expected.timeOfDayTypeOptional,
civilTypeOptional: allTypes4Expected.civilTypeOptional
}
]);
check testEntitiesClient.close();
}

@test:Config {
groups: ["all-types", "mysql"],
dependsOn: [mysqlAllTypesCreateTest, mysqlAllTypesCreateOptionalTest]
dependsOn: [mysqlAllTypesCreateTest, mysqlAllTypesCreateMixedTest]
}
function mysqlAllTypesReadOneTest() returns error? {
MySQLTestEntitiesClient testEntitiesClient = check new ();
Expand All @@ -154,6 +176,9 @@ function mysqlAllTypesReadOneTest() returns error? {
allTypesRetrieved = check testEntitiesClient->/alltypes/[allTypes3.id].get();
test:assertEquals(allTypesRetrieved, allTypes3Expected);

allTypesRetrieved = check testEntitiesClient->/alltypes/[allTypes4.id].get();
test:assertEquals(allTypesRetrieved, allTypes4Expected);

check testEntitiesClient.close();
}

Expand All @@ -163,9 +188,9 @@ function mysqlAllTypesReadOneTest() returns error? {
function mysqlAllTypesReadOneTestNegative() returns error? {
MySQLTestEntitiesClient testEntitiesClient = check new ();

AllTypes|persist:Error allTypesRetrieved = testEntitiesClient->/alltypes/[4].get();
AllTypes|persist:Error allTypesRetrieved = testEntitiesClient->/alltypes/[5].get();
if allTypesRetrieved is persist:NotFoundError {
test:assertEquals(allTypesRetrieved.message(), "A record with the key '4' does not exist for the entity 'AllTypes'.");
test:assertEquals(allTypesRetrieved.message(), "A record with the key '5' does not exist for the entity 'AllTypes'.");
}
else {
test:assertFail("persist:NotFoundError expected.");
Expand Down Expand Up @@ -223,6 +248,6 @@ function mysqlAllTypesDeleteTest() returns error? {
AllTypes[] allTypesCollection = check from AllTypes allTypesRecord in allTypesStream
select allTypesRecord;

test:assertEquals(allTypesCollection, [allTypes1UpdatedExpected, allTypes3Expected]);
test:assertEquals(allTypesCollection, [allTypes1UpdatedExpected, allTypes3Expected, allTypes4Expected]);
check testEntitiesClient.close();
}
Loading

0 comments on commit b162813

Please sign in to comment.