Search in sources :

Example 16 with BIntArray

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;
}
Also used : BIntArray(org.ballerinalang.model.values.BIntArray)

Example 17 with BIntArray

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;
    }
}
Also used : BStringArray(org.ballerinalang.model.values.BStringArray) BIntArray(org.ballerinalang.model.values.BIntArray) BBooleanArray(org.ballerinalang.model.values.BBooleanArray) BFloatArray(org.ballerinalang.model.values.BFloatArray)

Example 18 with BIntArray

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);
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) PreparedStatement(java.sql.PreparedStatement) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BatchUpdateException(java.sql.BatchUpdateException) BIntArray(org.ballerinalang.model.values.BIntArray)

Example 19 with BIntArray

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);
}
Also used : BBooleanArray(org.ballerinalang.model.values.BBooleanArray) BValue(org.ballerinalang.model.values.BValue) BFloatArray(org.ballerinalang.model.values.BFloatArray) BStringArray(org.ballerinalang.model.values.BStringArray) BIntArray(org.ballerinalang.model.values.BIntArray) Test(org.testng.annotations.Test)

Example 20 with BIntArray

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));
}
Also used : BValue(org.ballerinalang.model.values.BValue) BInteger(org.ballerinalang.model.values.BInteger) BIntArray(org.ballerinalang.model.values.BIntArray) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

BIntArray (org.ballerinalang.model.values.BIntArray)33 BValue (org.ballerinalang.model.values.BValue)25 Test (org.testng.annotations.Test)25 BString (org.ballerinalang.model.values.BString)14 BInteger (org.ballerinalang.model.values.BInteger)9 BStruct (org.ballerinalang.model.values.BStruct)9 BFloatArray (org.ballerinalang.model.values.BFloatArray)8 BStringArray (org.ballerinalang.model.values.BStringArray)8 BBooleanArray (org.ballerinalang.model.values.BBooleanArray)7 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)7 BJSON (org.ballerinalang.model.values.BJSON)6 BMap (org.ballerinalang.model.values.BMap)6 BBlobArray (org.ballerinalang.model.values.BBlobArray)4 BFloat (org.ballerinalang.model.values.BFloat)4 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)4 BRefType (org.ballerinalang.model.values.BRefType)3 CompileResult (org.ballerinalang.launcher.util.CompileResult)2 BMapType (org.ballerinalang.model.types.BMapType)2 BBoolean (org.ballerinalang.model.values.BBoolean)2 StructureType (org.ballerinalang.model.values.StructureType)2