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

Backport revert backtick support to v1.10.x #692

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
branches:
- master
- 2201.[0-9]+.x
- v1.10.x

jobs:
ubuntu-build:
Expand Down
45 changes: 42 additions & 3 deletions build-config/spotbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,52 @@
<Bug pattern="SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING"/>
</Match>

<!-- Here the insertions in the ParameterisedQuery cannot be copied since they are linked to ballerina objects -->
<Match>
<Class name="io.ballerina.stdlib.sql.ParameterizedQuery"/>
<Class name="io.ballerina.stdlib.sql.datasource.PoolKey"/>
<Bug pattern="EI_EXPOSE_REP2"/>
</Match>
<Match>
<Class name="io.ballerina.stdlib.sql.datasource.SQLDatasource$SQLDatasourceParams"/>
<Method name="setConnectionPool"/>
<Bug pattern="EI_EXPOSE_REP2"/>
</Match>
<Match>
<Class name="io.ballerina.stdlib.sql.datasource.SQLDatasource$SQLDatasourceParams"/>
<Method name="setOptions"/>
<Bug pattern="EI_EXPOSE_REP2"/>
</Match>
<Match>
<Class name="io.ballerina.stdlib.sql.datasource.SQLDatasource$SQLDatasourceParams"/>
<Method name="setPoolProperties"/>
<Bug pattern="EI_EXPOSE_REP2"/>
</Match>
<Match>
<Class name="io.ballerina.stdlib.sql.parameterprocessor.DefaultStatementParameterProcessor"/>
<Method name="getInstance"/>
<Bug pattern="MS_EXPOSE_REP"/>
</Match>
<Match>
<Class name="io.ballerina.stdlib.sql.transaction.SQLTransactionContext"/>
<Method name="getConnection"/>
<Bug pattern="EI_EXPOSE_REP"/>
</Match>
<Match>
<Class name="io.ballerina.stdlib.sql.transaction.SQLTransactionContext"/>
<Method name="getXAResource"/>
<Bug pattern="EI_EXPOSE_REP"/>
</Match>
<Match>
<Class name="io.ballerina.stdlib.sql.ParameterizedQuery"/>
<Class name="io.ballerina.stdlib.sql.transaction.SQLTransactionContext"/>
<Bug pattern="EI_EXPOSE_REP2"/>
</Match>
<Match>
<Class name="io.ballerina.stdlib.sql.utils.ColumnDefinition"/>
<Method name="getBallerinaType"/>
<Bug pattern="EI_EXPOSE_REP"/>
</Match>
<Match>
<Class name="io.ballerina.stdlib.sql.utils.RecordColumnDefinition"/>
<Method name="getInnerFields"/>
<Bug pattern="EI_EXPOSE_REP"/>
</Match>
</FindBugsFilter>
13 changes: 13 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

### Changed
- [Revert Accept escaped backtick as insertions in parameterised query](https://github.com/ballerina-platform/ballerina-standard-library/issues/2056)

## [1.10.0] - 2023-06-30

### Added
- [Support retrieval of enum types](https://github.com/ballerina-platform/ballerina-standard-library/issues/4588)

### Changed
- [Fix failure for unsupported time type](https://github.com/ballerina-platform/ballerina-standard-library/issues/4531)

## [1.9.0] - 2023-06-01

### Changed
- Add GraalVM support

## [1.8.0] - 2023-04-10

### Changed
- [Improve documentation regard `sql:Column` annotation](https://github.com/ballerina-platform/ballerina-standard-library/issues/4134)
- [Handle null error messages from underlying drivers](https://github.com/ballerina-platform/ballerina-standard-library/issues/4200)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ private Constants() {
public static final String PASSWORD = "password";

public static final String ANNON_RECORD_TYPE_NAME = "record {";
public static final String BACKTICK = "`";
public static final String DEFAULT_STREAM_CONSTRAINT_NAME = "$stream$anon$constraint$";
public static final String INHERENT_TYPE_VIOLATION = "{ballerina/lang.map}InherentTypeViolation";
/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import io.ballerina.runtime.api.values.BTypedesc;
import io.ballerina.runtime.transactions.TransactionResourceManager;
import io.ballerina.stdlib.sql.Constants;
import io.ballerina.stdlib.sql.ParameterizedQuery;
import io.ballerina.stdlib.sql.datasource.SQLDatasource;
import io.ballerina.stdlib.sql.exception.ApplicationError;
import io.ballerina.stdlib.sql.parameterprocessor.AbstractResultParameterProcessor;
Expand Down Expand Up @@ -62,6 +61,7 @@
import static io.ballerina.stdlib.sql.datasource.SQLWorkerThreadPool.SQL_EXECUTOR_SERVICE;
import static io.ballerina.stdlib.sql.utils.Utils.getColumnDefinitions;
import static io.ballerina.stdlib.sql.utils.Utils.getDefaultStreamConstraint;
import static io.ballerina.stdlib.sql.utils.Utils.getSqlQuery;
import static io.ballerina.stdlib.sql.utils.Utils.updateProcedureCallExecutionResult;

/**
Expand Down Expand Up @@ -121,13 +121,12 @@ private static Object nativeCallExecutable(BObject client, BObject paramSQLStrin
ResultSet resultSet;
String sqlQuery = null;
try {
ParameterizedQuery parameterizedQuery = Utils.getParameterizedSQLQuery(paramSQLString);
sqlQuery = parameterizedQuery.getSqlQuery();
sqlQuery = getSqlQuery(paramSQLString);
connection = SQLDatasource.getConnection(isWithinTrxBlock, trxResourceManager, client, sqlDatasource);
statement = connection.prepareCall(sqlQuery);

HashMap<Integer, Integer> outputParamTypes = new HashMap<>();
setCallParameters(connection, statement, parameterizedQuery.getInsertions(), outputParamTypes,
setCallParameters(connection, statement, paramSQLString, outputParamTypes,
statementParameterProcessor);

boolean resultType = statement.execute();
Expand Down Expand Up @@ -159,7 +158,7 @@ private static Object nativeCallExecutable(BObject client, BObject paramSQLStrin
updateProcedureCallExecutionResult(statement, procedureCallResult);
}

populateOutParameters(statement, parameterizedQuery.getInsertions(), outputParamTypes,
populateOutParameters(statement, paramSQLString, outputParamTypes,
resultParameterProcessor);

procedureCallResult.addNativeData(STATEMENT_NATIVE_DATA_FIELD, statement);
Expand All @@ -182,11 +181,12 @@ private static Object nativeCallExecutable(BObject client, BObject paramSQLStrin
}

private static void setCallParameters(Connection connection, CallableStatement statement,
Object[] insertions, HashMap<Integer, Integer> outputParamTypes,
BObject paramString, HashMap<Integer, Integer> outputParamTypes,
AbstractStatementParameterProcessor statementParameterProcessor)
throws SQLException, ApplicationError {
for (int i = 0; i < insertions.length; i++) {
Object object = insertions[i];
BArray arrayValue = paramString.getArrayValue(Constants.ParameterizedQueryFields.INSERTIONS);
for (int i = 0; i < arrayValue.size(); i++) {
Object object = arrayValue.get(i);
int index = i + 1;
if (object instanceof BObject) {
BObject objectValue = (BObject) object;
Expand Down Expand Up @@ -225,19 +225,20 @@ private static void setCallParameters(Connection connection, CallableStatement s
}
}

private static void populateOutParameters(CallableStatement statement, Object[] insertions,
private static void populateOutParameters(CallableStatement statement, BObject paramSQLString,
HashMap<Integer, Integer> outputParamTypes,
AbstractResultParameterProcessor resultParameterProcessor)
throws SQLException, ApplicationError {
if (outputParamTypes.size() == 0) {
return;
}
BArray arrayValue = paramSQLString.getArrayValue(Constants.ParameterizedQueryFields.INSERTIONS);

for (Map.Entry<Integer, Integer> entry : outputParamTypes.entrySet()) {
int paramIndex = entry.getKey();
int sqlType = entry.getValue();

BObject parameter = (BObject) insertions[paramIndex - 1];
BObject parameter = (BObject) arrayValue.get(paramIndex - 1);
parameter.addNativeData(Constants.ParameterObject.SQL_TYPE_NATIVE_DATA, sqlType);

Object result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import io.ballerina.runtime.api.values.BString;
import io.ballerina.runtime.transactions.TransactionResourceManager;
import io.ballerina.stdlib.sql.Constants;
import io.ballerina.stdlib.sql.ParameterizedQuery;
import io.ballerina.stdlib.sql.datasource.SQLDatasource;
import io.ballerina.stdlib.sql.exception.ApplicationError;
import io.ballerina.stdlib.sql.parameterprocessor.AbstractStatementParameterProcessor;
Expand All @@ -52,6 +51,7 @@
import static io.ballerina.stdlib.sql.datasource.SQLWorkerThreadPool.SQL_EXECUTOR_SERVICE;
import static io.ballerina.stdlib.sql.utils.Utils.closeResources;
import static io.ballerina.stdlib.sql.utils.Utils.getGeneratedKeys;
import static io.ballerina.stdlib.sql.utils.Utils.getSqlQuery;


/**
Expand Down Expand Up @@ -104,8 +104,7 @@ private static Object nativeExecuteExecutable(BObject client, BObject paramSQLSt
ResultSet resultSet = null;
String sqlQuery = null;
try {
ParameterizedQuery parameterizedQuery = Utils.getParameterizedSQLQuery(paramSQLString);
sqlQuery = parameterizedQuery.getSqlQuery();
sqlQuery = getSqlQuery(paramSQLString);
connection = SQLDatasource.getConnection(isWithInTrxBlock, trxResourceManager, client, sqlDatasource);

if (sqlDatasource.getExecuteGKFlag()) {
Expand All @@ -114,7 +113,7 @@ private static Object nativeExecuteExecutable(BObject client, BObject paramSQLSt
statement = connection.prepareStatement(sqlQuery);
}

statementParameterProcessor.setParams(connection, statement, parameterizedQuery.getInsertions());
statementParameterProcessor.setParams(connection, statement, paramSQLString);

int count = statement.executeUpdate();
Object lastInsertedId = null;
Expand Down Expand Up @@ -184,19 +183,20 @@ private static Object nativeBatchExecuteExecutable(BObject client, BArray paramS
Connection connection = null;
PreparedStatement statement = null;
String sqlQuery = null;
List<Object[]> parameters = new ArrayList<>();
List<BObject> parameters = new ArrayList<>();
List<BMap<BString, Object>> executionResults = new ArrayList<>();
boolean processResultSet = false;
int batchSize = 1000;
try {
Object[] paramSQLObjects = paramSQLStrings.getValues();
ParameterizedQuery parameterizedQuery = Utils.getParameterizedSQLQuery(((BObject) paramSQLObjects[0]));
sqlQuery = parameterizedQuery.getSqlQuery();
parameters.add(parameterizedQuery.getInsertions());
BObject parameterizedQuery = (BObject) paramSQLObjects[0];
sqlQuery = getSqlQuery(parameterizedQuery);
parameters.add(parameterizedQuery);
for (int paramIndex = 1; paramIndex < paramSQLStrings.size(); paramIndex++) {
parameterizedQuery = Utils.getParameterizedSQLQuery(((BObject) paramSQLObjects[paramIndex]));
if (sqlQuery.equals(parameterizedQuery.getSqlQuery())) {
parameters.add(parameterizedQuery.getInsertions());
parameterizedQuery = (BObject) paramSQLObjects[paramIndex];
String paramSQLQuery = getSqlQuery(parameterizedQuery);
if (sqlQuery.equals(paramSQLQuery)) {
parameters.add(parameterizedQuery);
} else {
return ErrorGenerator.getSQLApplicationError("Batch Execute cannot contain different SQL " +
"commands. These has to be executed in different function calls");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import io.ballerina.runtime.api.values.BTypedesc;
import io.ballerina.runtime.transactions.TransactionResourceManager;
import io.ballerina.stdlib.sql.Constants;
import io.ballerina.stdlib.sql.ParameterizedQuery;
import io.ballerina.stdlib.sql.datasource.SQLDatasource;
import io.ballerina.stdlib.sql.exception.ApplicationError;
import io.ballerina.stdlib.sql.exception.DataError;
Expand Down Expand Up @@ -115,11 +114,10 @@ private static BStream nativeQueryExecutable(
ResultSet resultSet = null;
String sqlQuery = null;
try {
ParameterizedQuery parameterizedQuery = Utils.getParameterizedSQLQuery(paramSQLString);
sqlQuery = parameterizedQuery.getSqlQuery();
sqlQuery = Utils.getSqlQuery(paramSQLString);
connection = SQLDatasource.getConnection(isWithInTrxBlock, trxResourceManager, client, sqlDatasource);
statement = connection.prepareStatement(sqlQuery);
statementParameterProcessor.setParams(connection, statement, parameterizedQuery.getInsertions());
statementParameterProcessor.setParams(connection, statement, paramSQLString);
resultSet = statement.executeQuery();
RecordType streamConstraint = (RecordType) TypeUtils.getReferredType(
((BTypedesc) recordType).getDescribingType());
Expand Down Expand Up @@ -172,7 +170,8 @@ public static Object nativeQueryRow(Environment env, BObject client, BObject par
}

private static Object nativeQueryRowExecutable(
BObject client, BObject paramSQLString, BTypedesc ballerinaType,
BObject client, BObject paramSQLString,
BTypedesc ballerinaType,
AbstractStatementParameterProcessor statementParameterProcessor,
AbstractResultParameterProcessor resultParameterProcessor, boolean isWithInTrxBlock,
TransactionResourceManager trxResourceManager) {
Expand All @@ -189,11 +188,10 @@ private static Object nativeQueryRowExecutable(
ResultSet resultSet = null;
String sqlQuery = null;
try {
ParameterizedQuery parameterizedQuery = Utils.getParameterizedSQLQuery(paramSQLString);
sqlQuery = parameterizedQuery.getSqlQuery();
sqlQuery = Utils.getSqlQuery(paramSQLString);
connection = SQLDatasource.getConnection(isWithInTrxBlock, trxResourceManager, client, sqlDatasource);
statement = connection.prepareStatement(sqlQuery);
statementParameterProcessor.setParams(connection, statement, parameterizedQuery.getInsertions());
statementParameterProcessor.setParams(connection, statement, paramSQLString);
resultSet = statement.executeQuery();
if (!resultSet.next()) {
return ErrorGenerator.getNoRowsError("Query did not retrieve any rows.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,11 @@ protected abstract void setXml(Connection connection, PreparedStatement prepared
protected abstract int setCustomBOpenRecord(Connection connection, PreparedStatement preparedStatement, int index,
Object value, boolean returnType) throws DataError, SQLException;

public void setParams(Connection connection, PreparedStatement preparedStatement,
Object[] insertions) throws DataError, SQLException {
for (int i = 0; i < insertions.length; i++) {
Object object = insertions[i];
public void setParams(Connection connection, PreparedStatement preparedStatement, BObject paramString)
throws DataError, SQLException {
BArray arrayValue = paramString.getArrayValue(Constants.ParameterizedQueryFields.INSERTIONS);
for (int i = 0; i < arrayValue.size(); i++) {
Object object = arrayValue.get(i);
int index = i + 1;
setSQLValueParam(connection, preparedStatement, index, object, false);
}
Expand Down
23 changes: 7 additions & 16 deletions native/src/main/java/io/ballerina/stdlib/sql/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import io.ballerina.runtime.api.values.BValue;
import io.ballerina.runtime.transactions.TransactionResourceManager;
import io.ballerina.stdlib.sql.Constants;
import io.ballerina.stdlib.sql.ParameterizedQuery;
import io.ballerina.stdlib.sql.exception.ApplicationError;
import io.ballerina.stdlib.sql.exception.ConversionError;
import io.ballerina.stdlib.sql.exception.DataError;
Expand Down Expand Up @@ -87,7 +86,6 @@
import static io.ballerina.stdlib.sql.Constants.AFFECTED_ROW_COUNT_FIELD;
import static io.ballerina.stdlib.sql.Constants.ANNON_RECORD_TYPE_NAME;
import static io.ballerina.stdlib.sql.Constants.ANN_COLUMN_NAME_FIELD;
import static io.ballerina.stdlib.sql.Constants.BACKTICK;
import static io.ballerina.stdlib.sql.Constants.COLUMN_ANN_NAME;
import static io.ballerina.stdlib.sql.Constants.DEFAULT_STREAM_CONSTRAINT_NAME;
import static io.ballerina.stdlib.sql.Constants.EXECUTION_RESULT_FIELD;
Expand Down Expand Up @@ -158,23 +156,16 @@ public static void closeResources(boolean isWithinTrxBlock, ResultSet resultSet,
}
}

public static ParameterizedQuery getParameterizedSQLQuery(BObject paramString) {
public static String getSqlQuery(BObject paramString) {
BArray stringsArray = paramString.getArrayValue(Constants.ParameterizedQueryFields.STRINGS);
StringBuilder sqlQuery = new StringBuilder();
List<Object> insertions = new ArrayList<>();

BArray bStringsArray = paramString.getArrayValue(Constants.ParameterizedQueryFields.STRINGS);
BArray bInsertions = paramString.getArrayValue(Constants.ParameterizedQueryFields.INSERTIONS);
for (int i = 0; i < bInsertions.size(); i++) {
if (bInsertions.get(i) instanceof BString && bInsertions.getBString(i).getValue().equals(BACKTICK)) {
sqlQuery.append(bStringsArray.getBString(i).getValue()).append(BACKTICK);
} else {
insertions.add(bInsertions.get(i));
sqlQuery.append(bStringsArray.getBString(i).getValue()).append(" ? ");
for (int i = 0; i < stringsArray.size(); i++) {
if (i > 0) {
sqlQuery.append(" ? ");
}
sqlQuery.append(stringsArray.get(i).toString());
}
sqlQuery.append(bStringsArray.getBString(bInsertions.size()));

return new ParameterizedQuery(sqlQuery.toString(), insertions.toArray());
return sqlQuery.toString();
}

public static DataError throwInvalidParameterError(Object value, String sqlType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public BObject getObjectValue(BString bString) {

@Override
public BArray getArrayValue(BString bString) {
return ((BArray) nativeData.get(bString.getValue()));
return null;
}

@Override
Expand Down
Loading
Loading