Skip to content

Commit

Permalink
Refactor java code
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedSabthar committed Oct 13, 2024
1 parent d204863 commit 1b6c29c
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,124 +253,6 @@ private static void populateOutParametersMetaData(CallableStatement statement, B
}
}

public static void 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);
BObject procedureCallResult = (BObject) parameter.getNativeData(PROCEDURE_CALL_RESULT);
AbstractResultParameterProcessor resultParameterProcessor = (AbstractResultParameterProcessor) parameter
.getNativeData(RESULT_PARAMETER_PROCESSOR);
Object result;
switch (sqlType) {
case Types.CHAR:
result = resultParameterProcessor.processChar(statement, paramIndex);
break;
case Types.VARCHAR:
result = resultParameterProcessor.processVarchar(statement, paramIndex);
break;
case Types.LONGVARCHAR:
result = resultParameterProcessor.processLongVarchar(statement, paramIndex);
break;
case Types.NCHAR:
result = resultParameterProcessor.processNChar(statement, paramIndex);
break;
case Types.NVARCHAR:
result = resultParameterProcessor.processNVarchar(statement, paramIndex);
break;
case Types.LONGNVARCHAR:
result = resultParameterProcessor.processLongNVarchar(statement, paramIndex);
break;
case Types.BINARY:
result = resultParameterProcessor.processBinary(statement, paramIndex);
break;
case Types.VARBINARY:
result = resultParameterProcessor.processVarBinary(statement, paramIndex);
break;
case Types.LONGVARBINARY:
result = resultParameterProcessor.processLongVarBinary(statement, paramIndex);
break;
case Types.BLOB:
result = resultParameterProcessor.processBlob(statement, paramIndex);
break;
case Types.CLOB:
result = resultParameterProcessor.processClob(statement, paramIndex);
break;
case Types.NCLOB:
result = resultParameterProcessor.processNClob(statement, paramIndex);
break;
case Types.DATE:
result = resultParameterProcessor.processDate(statement, paramIndex);
break;
case Types.TIME:
result = resultParameterProcessor.processTime(statement, paramIndex);
break;
case Types.TIME_WITH_TIMEZONE:
result = resultParameterProcessor.processTimeWithTimeZone(statement, paramIndex);
break;
case Types.TIMESTAMP:
result = resultParameterProcessor.processTimestamp(statement, paramIndex);
break;
case Types.TIMESTAMP_WITH_TIMEZONE:
result = resultParameterProcessor.processTimestampWithTimeZone(statement, paramIndex);
break;
case Types.ARRAY:
result = resultParameterProcessor.processArray(statement, paramIndex);
break;
case Types.ROWID:
result = resultParameterProcessor.processRowID(statement, paramIndex);
break;
case Types.TINYINT:
result = resultParameterProcessor.processTinyInt(statement, paramIndex);
break;
case Types.SMALLINT:
result = resultParameterProcessor.processSmallInt(statement, paramIndex);
break;
case Types.INTEGER:
result = resultParameterProcessor.processInteger(statement, paramIndex);
break;
case Types.BIGINT:
result = resultParameterProcessor.processBigInt(statement, paramIndex);
break;
case Types.REAL:
result = resultParameterProcessor.processReal(statement, paramIndex);
break;
case Types.FLOAT:
result = resultParameterProcessor.processFloat(statement, paramIndex);
break;
case Types.DOUBLE:
result = resultParameterProcessor.processDouble(statement, paramIndex);
break;
case Types.NUMERIC:
result = resultParameterProcessor.processNumeric(statement, paramIndex);
break;
case Types.DECIMAL:
result = resultParameterProcessor.processDecimal(statement, paramIndex);
break;
case Types.BIT:
result = resultParameterProcessor.processBit(statement, paramIndex);
break;
case Types.BOOLEAN:
result = resultParameterProcessor.processBoolean(statement, paramIndex);
break;
case Types.REF:
case Types.REF_CURSOR:
result = resultParameterProcessor.processRef(statement, paramIndex);
// This is to clean up the result set attached to the ref cursor out parameter
// when procedure call result is closed.
procedureCallResult.addNativeData(Constants.REF_CURSOR_VALUE_NATIVE_DATA, result);
break;
case Types.STRUCT:
result = resultParameterProcessor.processStruct(statement, paramIndex);
break;
case Types.SQLXML:
result = resultParameterProcessor.processXML(statement, paramIndex);
break;
default:
result = resultParameterProcessor.processCustomOutParameters(statement, paramIndex, sqlType);
}
parameter.addNativeData(Constants.ParameterObject.VALUE_NATIVE_DATA, result);
}

private static int getOutParameterType(BObject typedValue,
AbstractStatementParameterProcessor statementParameterProcessor)
throws ApplicationError, SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.math.BigDecimal;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Date;
import java.sql.NClob;
Expand All @@ -48,6 +49,12 @@
import java.time.OffsetDateTime;
import java.time.OffsetTime;

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;
import static io.ballerina.stdlib.sql.utils.Utils.getErrorStream;
import static io.ballerina.stdlib.sql.utils.Utils.getString;

Expand All @@ -63,7 +70,7 @@ private OutParameterProcessor() {

public static Object getOutParameterValue(BObject result, BTypedesc typeDesc) {
try {
CallProcessor.populateOutParameter(result);
populateOutParameter(result);
} catch (SQLException | ApplicationError e) {
return ErrorGenerator.getSQLError(e, "Failed to read OUT parameter value.");
}
Expand All @@ -73,7 +80,7 @@ public static Object getOutParameterValue(BObject result, BTypedesc typeDesc) {

public static BStream getOutCursorValue(BObject result, BTypedesc typeDesc) {
try {
CallProcessor.populateOutParameter(result);
populateOutParameter(result);
} catch (SQLException | ApplicationError e) {
return getErrorStream(typeDesc,
ErrorGenerator.getSQLError(e, "Failed to read parameter value."));
Expand All @@ -83,13 +90,131 @@ public static BStream getOutCursorValue(BObject result, BTypedesc typeDesc) {

public static Object getInOutParameterValue(BObject result, BTypedesc typeDesc) {
try {
CallProcessor.populateOutParameter(result);
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 {
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);
BObject procedureCallResult = (BObject) parameter.getNativeData(PROCEDURE_CALL_RESULT);
AbstractResultParameterProcessor resultParameterProcessor = (AbstractResultParameterProcessor) parameter
.getNativeData(RESULT_PARAMETER_PROCESSOR);
Object result;
switch (sqlType) {
case Types.CHAR:
result = resultParameterProcessor.processChar(statement, paramIndex);
break;
case Types.VARCHAR:
result = resultParameterProcessor.processVarchar(statement, paramIndex);
break;
case Types.LONGVARCHAR:
result = resultParameterProcessor.processLongVarchar(statement, paramIndex);
break;
case Types.NCHAR:
result = resultParameterProcessor.processNChar(statement, paramIndex);
break;

Check warning on line 120 in native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java

View check run for this annotation

Codecov / codecov/patch

native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java#L119-L120

Added lines #L119 - L120 were not covered by tests
case Types.NVARCHAR:
result = resultParameterProcessor.processNVarchar(statement, paramIndex);
break;
case Types.LONGNVARCHAR:
result = resultParameterProcessor.processLongNVarchar(statement, paramIndex);
break;

Check warning on line 126 in native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java

View check run for this annotation

Codecov / codecov/patch

native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java#L125-L126

Added lines #L125 - L126 were not covered by tests
case Types.BINARY:
result = resultParameterProcessor.processBinary(statement, paramIndex);
break;
case Types.VARBINARY:
result = resultParameterProcessor.processVarBinary(statement, paramIndex);
break;
case Types.LONGVARBINARY:
result = resultParameterProcessor.processLongVarBinary(statement, paramIndex);
break;

Check warning on line 135 in native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java

View check run for this annotation

Codecov / codecov/patch

native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java#L134-L135

Added lines #L134 - L135 were not covered by tests
case Types.BLOB:
result = resultParameterProcessor.processBlob(statement, paramIndex);
break;

Check warning on line 138 in native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java

View check run for this annotation

Codecov / codecov/patch

native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java#L137-L138

Added lines #L137 - L138 were not covered by tests
case Types.CLOB:
result = resultParameterProcessor.processClob(statement, paramIndex);
break;

Check warning on line 141 in native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java

View check run for this annotation

Codecov / codecov/patch

native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java#L140-L141

Added lines #L140 - L141 were not covered by tests
case Types.NCLOB:
result = resultParameterProcessor.processNClob(statement, paramIndex);
break;

Check warning on line 144 in native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java

View check run for this annotation

Codecov / codecov/patch

native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java#L143-L144

Added lines #L143 - L144 were not covered by tests
case Types.DATE:
result = resultParameterProcessor.processDate(statement, paramIndex);
break;
case Types.TIME:
result = resultParameterProcessor.processTime(statement, paramIndex);
break;
case Types.TIME_WITH_TIMEZONE:
result = resultParameterProcessor.processTimeWithTimeZone(statement, paramIndex);
break;
case Types.TIMESTAMP:
result = resultParameterProcessor.processTimestamp(statement, paramIndex);
break;
case Types.TIMESTAMP_WITH_TIMEZONE:
result = resultParameterProcessor.processTimestampWithTimeZone(statement, paramIndex);
break;
case Types.ARRAY:
result = resultParameterProcessor.processArray(statement, paramIndex);
break;
case Types.ROWID:
result = resultParameterProcessor.processRowID(statement, paramIndex);
break;

Check warning on line 165 in native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java

View check run for this annotation

Codecov / codecov/patch

native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java#L164-L165

Added lines #L164 - L165 were not covered by tests
case Types.TINYINT:
result = resultParameterProcessor.processTinyInt(statement, paramIndex);
break;

Check warning on line 168 in native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java

View check run for this annotation

Codecov / codecov/patch

native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java#L167-L168

Added lines #L167 - L168 were not covered by tests
case Types.SMALLINT:
result = resultParameterProcessor.processSmallInt(statement, paramIndex);
break;
case Types.INTEGER:
result = resultParameterProcessor.processInteger(statement, paramIndex);
break;
case Types.BIGINT:
result = resultParameterProcessor.processBigInt(statement, paramIndex);
break;
case Types.REAL:
result = resultParameterProcessor.processReal(statement, paramIndex);
break;
case Types.FLOAT:
result = resultParameterProcessor.processFloat(statement, paramIndex);
break;
case Types.DOUBLE:
result = resultParameterProcessor.processDouble(statement, paramIndex);
break;
case Types.NUMERIC:
result = resultParameterProcessor.processNumeric(statement, paramIndex);
break;
case Types.DECIMAL:
result = resultParameterProcessor.processDecimal(statement, paramIndex);
break;
case Types.BIT:
result = resultParameterProcessor.processBit(statement, paramIndex);
break;
case Types.BOOLEAN:
result = resultParameterProcessor.processBoolean(statement, paramIndex);
break;
case Types.REF:
case Types.REF_CURSOR:
result = resultParameterProcessor.processRef(statement, paramIndex);

Check warning on line 201 in native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java

View check run for this annotation

Codecov / codecov/patch

native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java#L201

Added line #L201 was not covered by tests
// This is to clean up the result set attached to the ref cursor out parameter
// when procedure call result is closed.
procedureCallResult.addNativeData(REF_CURSOR_VALUE_NATIVE_DATA, result);
break;

Check warning on line 205 in native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java

View check run for this annotation

Codecov / codecov/patch

native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java#L204-L205

Added lines #L204 - L205 were not covered by tests
case Types.STRUCT:
result = resultParameterProcessor.processStruct(statement, paramIndex);
break;

Check warning on line 208 in native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java

View check run for this annotation

Codecov / codecov/patch

native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java#L207-L208

Added lines #L207 - L208 were not covered by tests
case Types.SQLXML:
result = resultParameterProcessor.processXML(statement, paramIndex);
break;

Check warning on line 211 in native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java

View check run for this annotation

Codecov / codecov/patch

native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java#L210-L211

Added lines #L210 - L211 were not covered by tests
default:
result = resultParameterProcessor.processCustomOutParameters(statement, paramIndex, sqlType);

Check warning on line 213 in native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java

View check run for this annotation

Codecov / codecov/patch

native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/OutParameterProcessor.java#L213

Added line #L213 was not covered by tests
}
parameter.addNativeData(ParameterObject.VALUE_NATIVE_DATA, result);
}

public static BStream get(BObject result, Object recordType,
AbstractResultParameterProcessor resultParameterProcessor) {
Object value = result.getNativeData(Constants.ParameterObject.VALUE_NATIVE_DATA);
Expand Down

0 comments on commit 1b6c29c

Please sign in to comment.