use of org.ballerinalang.model.values.BRefValueArray in project ballerina by ballerina-lang.
the class XMLUtils method traverseXMLSequence.
/**
* Converts given xml sequence to the corresponding json.
*
* @param xmlSequence XML sequence to traverse
* @param attributePrefix Prefix to use in attributes
* @param preserveNamespaces preserve the namespaces when converting
* @return JsonNode Json node corresponding to the given xml sequence
*/
private static JsonNode traverseXMLSequence(BXMLSequence xmlSequence, String attributePrefix, boolean preserveNamespaces) {
JsonNode jsonNode = null;
BRefValueArray sequence = xmlSequence.value();
long count = sequence.size();
ArrayList<OMElement> childArray = new ArrayList<>();
ArrayList<OMText> textArray = new ArrayList<>();
for (long i = 0; i < count; ++i) {
BXMLItem xmlItem = (BXMLItem) sequence.get(i);
OMNode omNode = xmlItem.value();
if (OMNode.ELEMENT_NODE == omNode.getType()) {
childArray.add((OMElement) omNode);
} else if (OMNode.TEXT_NODE == omNode.getType()) {
textArray.add((OMText) omNode);
}
}
JsonNode textArrayNode = null;
if (textArray.size() > 0) {
// Text nodes are converted into json array
textArrayNode = processTextArray(textArray);
}
if (childArray.size() > 0) {
jsonNode = new JsonNode(Type.OBJECT);
processChildelements(jsonNode, childArray, attributePrefix, preserveNamespaces);
if (textArrayNode != null) {
// When text nodes and elements are mixed, they will set into an array
textArrayNode.add(jsonNode);
}
}
if (textArrayNode != null) {
jsonNode = textArrayNode;
}
return jsonNode;
}
use of org.ballerinalang.model.values.BRefValueArray in project ballerina by ballerina-lang.
the class AbstractSQLAction method executeBatchUpdate.
protected void executeBatchUpdate(Context context, SQLDatasource datasource, String query, BRefValueArray parameters) {
Connection conn = null;
PreparedStatement stmt = null;
int[] updatedCount;
int paramArrayCount = 0;
try {
conn = datasource.getSQLConnection();
stmt = conn.prepareStatement(query);
setConnectionAutoCommit(conn, false);
if (parameters != null) {
paramArrayCount = (int) parameters.size();
for (int index = 0; index < paramArrayCount; index++) {
BRefValueArray params = (BRefValueArray) parameters.get(index);
createProcessedStatement(conn, stmt, params);
stmt.addBatch();
}
} else {
stmt.addBatch();
}
updatedCount = stmt.executeBatch();
conn.commit();
} catch (BatchUpdateException e) {
updatedCount = e.getUpdateCounts();
} catch (SQLException e) {
throw new BallerinaException("execute batch update failed: " + e.getMessage(), e);
} finally {
setConnectionAutoCommit(conn, true);
SQLDatasourceUtils.cleanupConnection(null, stmt, conn, false);
}
// After a command in a batch update fails to execute properly and a BatchUpdateException is thrown, the driver
// may or may not continue to process the remaining commands in the batch. If the driver does not continue
// processing after a failure, the array returned by the method will have -3 (EXECUTE_FAILED) for those updates.
long[] returnedCount = new long[paramArrayCount];
Arrays.fill(returnedCount, Statement.EXECUTE_FAILED);
BIntArray countArray = new BIntArray(returnedCount);
if (updatedCount != null) {
int iSize = updatedCount.length;
for (int i = 0; i < iSize; ++i) {
countArray.add(i, updatedCount[i]);
}
}
context.setReturnValues(countArray);
}
use of org.ballerinalang.model.values.BRefValueArray in project ballerina by ballerina-lang.
the class Call method execute.
@Override
public void execute(Context context) {
try {
BStruct bConnector = (BStruct) context.getRefArgument(0);
String query = context.getStringArgument(0);
BRefValueArray parameters = (BRefValueArray) context.getNullableRefArgument(1);
BStructType structType = getStructType(context);
SQLDatasource datasource = (SQLDatasource) bConnector.getNativeData(Constants.CLIENT_CONNECTOR);
Map<String, String> tags = new HashMap<>();
tags.put(TAG_KEY_DB_STATEMENT, query);
tags.put(TAG_KEY_DB_TYPE, TAG_DB_TYPE_SQL);
TraceUtil.getTracer(context.getParentWorkerExecutionContext()).addTags(tags);
executeProcedure(context, datasource, query, parameters, structType);
} catch (Throwable e) {
context.setReturnValues(SQLDatasourceUtils.getSQLConnectorError(context, e));
SQLDatasourceUtils.handleErrorOnTransaction(context);
}
}
use of org.ballerinalang.model.values.BRefValueArray in project ballerina by ballerina-lang.
the class Select method execute.
@Override
public void execute(Context context) {
try {
BStruct bConnector = (BStruct) context.getRefArgument(0);
String query = context.getStringArgument(0);
BRefValueArray parameters = (BRefValueArray) context.getNullableRefArgument(1);
BStructType structType = getStructType(context);
SQLDatasource datasource = (SQLDatasource) bConnector.getNativeData(Constants.CLIENT_CONNECTOR);
Map<String, String> tags = new HashMap<>();
tags.put(TAG_KEY_DB_STATEMENT, query);
tags.put(TAG_KEY_DB_TYPE, TAG_DB_TYPE_SQL);
TraceUtil.getTracer(context.getParentWorkerExecutionContext()).addTags(tags);
executeQuery(context, datasource, query, parameters, structType);
} catch (Throwable e) {
context.setReturnValues(SQLDatasourceUtils.getSQLConnectorError(context, e));
SQLDatasourceUtils.handleErrorOnTransaction(context);
}
}
use of org.ballerinalang.model.values.BRefValueArray in project ballerina by ballerina-lang.
the class UpdateWithGeneratedKeys method execute.
@Override
public void execute(Context context) {
try {
BStruct bConnector = (BStruct) context.getRefArgument(0);
String query = context.getStringArgument(0);
BRefValueArray parameters = (BRefValueArray) context.getNullableRefArgument(1);
BStringArray keyColumns = (BStringArray) context.getNullableRefArgument(2);
SQLDatasource datasource = (SQLDatasource) bConnector.getNativeData(Constants.CLIENT_CONNECTOR);
Map<String, String> tags = new HashMap<>();
tags.put(TAG_KEY_DB_STATEMENT, query);
tags.put(TAG_KEY_DB_TYPE, TAG_DB_TYPE_SQL);
TraceUtil.getTracer(context.getParentWorkerExecutionContext()).addTags(tags);
executeUpdateWithKeys(context, datasource, query, keyColumns, parameters);
} catch (Throwable e) {
context.setReturnValues(SQLDatasourceUtils.getSQLConnectorError(context, e));
SQLDatasourceUtils.handleErrorOnTransaction(context);
}
}
Aggregations