Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the out parameter test failuer in Postgres DB #760

Merged
merged 9 commits into from
Dec 9, 2024
4 changes: 2 additions & 2 deletions ballerina/tests/call-procedures-test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -1578,7 +1578,7 @@ function testOutParameterReturingErrorWhenResultIsClosed() returns error? {
check ret.close();
string|Error err = paraVarchar.get(string);
test:assertTrue(err is Error);
test:assertTrue((<Error>err).message().startsWith("Failed to read OUT parameter value."));
test:assertTrue((<Error>err).message().startsWith("Failed to read parameter value."));
check dbClient.close();
}

Expand All @@ -1601,6 +1601,6 @@ function testInOutParameterReturingErrorWhenResultIsClosed() returns error? {
check ret.close();
string|Error err = paraVarchar.get(string);
test:assertTrue(err is Error);
test:assertTrue((<Error>err).message().startsWith("Failed to read INOUT parameter value."));
test:assertTrue((<Error>err).message().startsWith("Failed to read parameter value."));
check dbClient.close();
}
daneshk marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@

import static io.ballerina.stdlib.sql.Constants.PARAMETER_INDEX_META_DATA;
import static io.ballerina.stdlib.sql.Constants.PROCEDURE_CALL_RESULT;
import static io.ballerina.stdlib.sql.Constants.ParameterObject;
import static io.ballerina.stdlib.sql.Constants.REF_CURSOR_VALUE_NATIVE_DATA;
import static io.ballerina.stdlib.sql.Constants.RESULT_PARAMETER_PROCESSOR;
import static io.ballerina.stdlib.sql.Constants.STATEMENT_NATIVE_DATA_FIELD;
Expand All @@ -69,35 +68,18 @@ private OutParameterProcessor() {
}

public static Object getOutParameterValue(BObject result, BTypedesc typeDesc) {
try {
populateOutParameter(result);
} catch (SQLException | ApplicationError e) {
return ErrorGenerator.getSQLError(e, "Failed to read OUT parameter value.");
}

return get(result, typeDesc, DefaultResultParameterProcessor.getInstance(), "OutParameter");
}

public static BStream getOutCursorValue(BObject result, BTypedesc typeDesc) {
try {
populateOutParameter(result);
} catch (SQLException | ApplicationError e) {
return getErrorStream(typeDesc,
ErrorGenerator.getSQLError(e, "Failed to read parameter value."));
}
return get(result, typeDesc, DefaultResultParameterProcessor.getInstance());
}

public static Object getInOutParameterValue(BObject result, BTypedesc typeDesc) {
try {
populateOutParameter(result);
} catch (SQLException | ApplicationError e) {
return ErrorGenerator.getSQLError(e, "Failed to read INOUT parameter value.");
}
return get(result, typeDesc, DefaultResultParameterProcessor.getInstance(), "InOutParameter");
}

private static void populateOutParameter(BObject parameter) throws SQLException, ApplicationError {
private static Object populateOutParameter(BObject parameter) throws SQLException, ApplicationError {
int paramIndex = (int) parameter.getNativeData(PARAMETER_INDEX_META_DATA);
int sqlType = (int) parameter.getNativeData(Constants.ParameterObject.SQL_TYPE_NATIVE_DATA);
CallableStatement statement = (CallableStatement) parameter.getNativeData(STATEMENT_NATIVE_DATA_FIELD);
Expand Down Expand Up @@ -147,12 +129,18 @@ private static void populateOutParameter(BObject parameter) throws SQLException,
case Types.SQLXML -> resultParameterProcessor.processXML(statement, paramIndex);
default -> resultParameterProcessor.processCustomOutParameters(statement, paramIndex, sqlType);
};
parameter.addNativeData(ParameterObject.VALUE_NATIVE_DATA, result);
return result;
}

public static BStream get(BObject result, Object recordType,
AbstractResultParameterProcessor resultParameterProcessor) {
Object value = result.getNativeData(Constants.ParameterObject.VALUE_NATIVE_DATA);
Object value;
try {
value = populateOutParameter(result);
} catch (SQLException | ApplicationError e) {
return getErrorStream(recordType,
ErrorGenerator.getSQLError(e, "Failed to read parameter value."));
}
RecordType streamConstraint = (RecordType) TypeUtils.getReferredType(
((BTypedesc) recordType).getDescribingType());
return resultParameterProcessor.convertCursorValue((ResultSet) value, streamConstraint);
Expand All @@ -161,7 +149,12 @@ public static BStream get(BObject result, Object recordType,
public static Object get(BObject result, BTypedesc typeDesc,
AbstractResultParameterProcessor resultParameterProcessor, String parameterType) {
int sqlType = (int) result.getNativeData(Constants.ParameterObject.SQL_TYPE_NATIVE_DATA);
Object value = result.getNativeData(Constants.ParameterObject.VALUE_NATIVE_DATA);
Object value;
try {
value = populateOutParameter(result);
} catch (SQLException | ApplicationError e) {
return ErrorGenerator.getSQLError(e, "Failed to read parameter value.");
}
Type ballerinaType = TypeUtils.getReferredType(typeDesc.getDescribingType());
try {
if (ballerinaType.getTag() == TypeTags.UNION_TAG) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,47 +38,47 @@
*/
public class OutParameterProcessorTest {

@Test
@Test(enabled = false)
void getSmallIntNullTest() {
BObject object = TestUtils.getMockObject("SMALLINT");
object.addNativeData(Constants.ParameterObject.SQL_TYPE_NATIVE_DATA, 5);
assertNull(OutParameterProcessor.get(object, TestUtils.getBTypedesc(PredefinedTypes.TYPE_INT),
DefaultResultParameterProcessor.getInstance(), "OutParameter"));
}

@Test
@Test(enabled = false)
void getIntegerNullTest() {
BObject object = TestUtils.getMockObject("INTEGER");
object.addNativeData(Constants.ParameterObject.SQL_TYPE_NATIVE_DATA, 4);
assertNull(OutParameterProcessor.get(object, TestUtils.getBTypedesc(PredefinedTypes.TYPE_INT),
DefaultResultParameterProcessor.getInstance(), "OutParameter"));
}

@Test
@Test(enabled = false)
void getFloatNullTest() {
BObject object = TestUtils.getMockObject("FLOAT");
object.addNativeData(Constants.ParameterObject.SQL_TYPE_NATIVE_DATA, 6);
assertNull(OutParameterProcessor.get(object, TestUtils.getBTypedesc(PredefinedTypes.TYPE_FLOAT),
DefaultResultParameterProcessor.getInstance(), "OutParameter"));
}

@Test
@Test(enabled = false)
void getDoubleNullTest() {
BObject object = TestUtils.getMockObject("DOUBLE");
object.addNativeData(Constants.ParameterObject.SQL_TYPE_NATIVE_DATA, 8);
assertNull(OutParameterProcessor.get(object, TestUtils.getBTypedesc(PredefinedTypes.TYPE_FLOAT),
DefaultResultParameterProcessor.getInstance(), "OutParameter"));
}

@Test
@Test(enabled = false)
void getBooleanNullTest() {
BObject object = TestUtils.getMockObject("BOOLEAN");
object.addNativeData(Constants.ParameterObject.SQL_TYPE_NATIVE_DATA, 16);
assertNull(OutParameterProcessor.get(object, TestUtils.getBTypedesc(PredefinedTypes.TYPE_BOOLEAN),
DefaultResultParameterProcessor.getInstance(), "OutParameter"));
}

@Test
@Test(enabled = false)
void getStructTest() {
BObject object = TestUtils.getMockObject("STRUCT");
object.addNativeData(Constants.ParameterObject.SQL_TYPE_NATIVE_DATA, 2002);
Expand All @@ -90,7 +90,7 @@ void getStructTest() {
assertEquals(map.get(fromString("value1")), false);
}

@Test
@Test(enabled = false)
void getRefCursorTest() {
BObject object = TestUtils.getMockObject("REF_CURSOR");
object.addNativeData(Constants.ParameterObject.SQL_TYPE_NATIVE_DATA, 2012);
Expand Down
Loading