use of org.ballerinalang.model.values.BIntArray in project ballerina by ballerina-lang.
the class JSONUtils method jsonNodeToBIntArray.
private static BIntArray jsonNodeToBIntArray(JsonNode arrayNode) {
BIntArray intArray = new BIntArray();
for (int i = 0; i < arrayNode.size(); i++) {
JsonNode jsonValue = arrayNode.get(i);
intArray.add(i, jsonNodeToInt(jsonValue));
}
return intArray;
}
use of org.ballerinalang.model.values.BIntArray in project ballerina by ballerina-lang.
the class TableIterator method getDataArray.
protected BNewArray getDataArray(Array array) throws SQLException {
Object[] dataArray = generateArrayDataResult(array);
if (dataArray == null || dataArray.length == 0) {
return null;
}
Object obj = dataArray[0];
int length = dataArray.length;
if (obj instanceof String) {
BStringArray stringDataArray = new BStringArray();
for (int i = 0; i < length; i++) {
stringDataArray.add(i, (String) dataArray[i]);
}
return stringDataArray;
} else if (obj instanceof Boolean) {
BBooleanArray boolDataArray = new BBooleanArray();
for (int i = 0; i < length; i++) {
boolDataArray.add(i, ((Boolean) dataArray[i]) ? 1 : 0);
}
return boolDataArray;
} else if (obj instanceof Integer) {
BIntArray intDataArray = new BIntArray();
for (int i = 0; i < length; i++) {
intDataArray.add(i, ((Integer) dataArray[i]));
}
return intDataArray;
} else if (obj instanceof Long) {
BIntArray longDataArray = new BIntArray();
for (int i = 0; i < length; i++) {
longDataArray.add(i, (Long) dataArray[i]);
}
return longDataArray;
} else if (obj instanceof Float) {
BFloatArray floatDataArray = new BFloatArray();
for (int i = 0; i < length; i++) {
floatDataArray.add(i, (Float) dataArray[i]);
}
return floatDataArray;
} else if (obj instanceof Double) {
BFloatArray doubleDataArray = new BFloatArray();
for (int i = 0; i < dataArray.length; i++) {
doubleDataArray.add(i, (Double) dataArray[i]);
}
return doubleDataArray;
} else {
return null;
}
}
use of org.ballerinalang.model.values.BIntArray 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.BIntArray in project ballerina by ballerina-lang.
the class TableTest method testArrayData.
@Test(groups = "TableTest", description = "Check array data types.")
public void testArrayData() {
BValue[] returns = BRunUtil.invoke(result, "testArrayData");
Assert.assertEquals(returns.length, 5);
Assert.assertTrue(returns[0] instanceof BIntArray);
BIntArray intArray = (BIntArray) returns[0];
Assert.assertEquals(intArray.get(0), 1);
Assert.assertEquals(intArray.get(1), 2);
Assert.assertEquals(intArray.get(2), 3);
Assert.assertTrue(returns[1] instanceof BIntArray);
BIntArray longArray = (BIntArray) returns[1];
Assert.assertEquals(longArray.get(0), 100000000);
Assert.assertEquals(longArray.get(1), 200000000);
Assert.assertEquals(longArray.get(2), 300000000);
Assert.assertTrue(returns[2] instanceof BFloatArray);
BFloatArray doubleArray = (BFloatArray) returns[2];
Assert.assertEquals(doubleArray.get(0), 245.23);
Assert.assertEquals(doubleArray.get(1), 5559.49);
Assert.assertEquals(doubleArray.get(2), 8796.123);
Assert.assertTrue(returns[3] instanceof BStringArray);
BStringArray stringArray = (BStringArray) returns[3];
Assert.assertEquals(stringArray.get(0), "Hello");
Assert.assertEquals(stringArray.get(1), "Ballerina");
Assert.assertTrue(returns[4] instanceof BBooleanArray);
BBooleanArray booleanArray = (BBooleanArray) returns[4];
Assert.assertEquals(booleanArray.get(0), 1);
Assert.assertEquals(booleanArray.get(1), 0);
Assert.assertEquals(booleanArray.get(2), 1);
}
use of org.ballerinalang.model.values.BIntArray in project ballerina by ballerina-lang.
the class VectorTest method testSize.
@Test(description = "Test case for testing size() function")
public void testSize() {
long[] addElems = new long[] { 11, 12, 13, 14, 15 };
long[] insertElems = new long[] { 21, 23, 25 };
long[] replaceElems = new long[] { 32, 34, 36, 38 };
long nRemoveElems = 9;
long vectorSize = 10;
BValue[] returns = BRunUtil.invoke(compileResult, "testSize", new BValue[] { buildIntArray(addElems), buildIntArray(insertElems), buildIntArray(replaceElems), new BInteger(nRemoveElems), new BInteger(vectorSize) });
Assert.assertNotNull(returns);
BIntArray vecSizes = (BIntArray) returns[0];
Assert.assertEquals(vecSizes.get(0), (vectorSize += addElems.length));
Assert.assertEquals(vecSizes.get(1), (vectorSize += insertElems.length));
Assert.assertEquals(vecSizes.get(2), vectorSize);
Assert.assertEquals(vecSizes.get(3), (vectorSize -= nRemoveElems));
}
Aggregations