Search in sources :

Example 36 with BRefValueArray

use of org.ballerinalang.model.values.BRefValueArray in project ballerina by ballerina-lang.

the class SystemTest method testFormatBooleanFalse.

@Test
public void testFormatBooleanFalse() {
    BRefValueArray fArgs = new BRefValueArray();
    fArgs.add(0, new BBoolean(false));
    BValue[] args = { new BString("%b"), fArgs };
    BValue[] returns = BRunUtil.invoke(compileResult, "testSprintf", args);
    Assert.assertEquals(returns[0].stringValue(), "false");
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BBoolean(org.ballerinalang.model.values.BBoolean) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) Test(org.testng.annotations.Test)

Example 37 with BRefValueArray

use of org.ballerinalang.model.values.BRefValueArray in project ballerina by ballerina-lang.

the class SystemTest method testFormatIntArray.

@Test
public void testFormatIntArray() {
    BRefValueArray fArgs = new BRefValueArray();
    BIntArray arr = new BIntArray();
    arr.add(0, 111);
    arr.add(1, 222);
    arr.add(2, 333);
    fArgs.add(0, arr);
    BValue[] args = { new BString("%a"), fArgs };
    BValue[] returns = BRunUtil.invoke(compileResult, "testSprintf", args);
    Assert.assertEquals(returns[0].stringValue(), "[111, 222, 333]");
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) BIntArray(org.ballerinalang.model.values.BIntArray) Test(org.testng.annotations.Test)

Example 38 with BRefValueArray

use of org.ballerinalang.model.values.BRefValueArray in project ballerina by ballerina-lang.

the class SystemTest method testFormatDecimal.

@Test
public void testFormatDecimal() {
    BRefValueArray fArgs = new BRefValueArray();
    fArgs.add(0, new BInteger(65));
    BValue[] args = { new BString("%d"), fArgs };
    BValue[] returns = BRunUtil.invoke(compileResult, "testSprintf", args);
    Assert.assertEquals(returns[0].stringValue(), "65");
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) Test(org.testng.annotations.Test)

Example 39 with BRefValueArray

use of org.ballerinalang.model.values.BRefValueArray in project ballerina by ballerina-lang.

the class NativeConversionTest method testStructToMapWithRefTypeArray.

// Todo - Fix casting issue
@Test(enabled = false)
public void testStructToMapWithRefTypeArray() {
    BValue[] returns = BRunUtil.invoke(compileResult, "testStructToMapWithRefTypeArray");
    Assert.assertTrue(returns[0] instanceof BMap<?, ?>);
    BMap<String, ?> map = (BMap<String, ?>) returns[0];
    BValue name = map.get("title");
    Assert.assertTrue(name instanceof BString);
    Assert.assertEquals(name.stringValue(), "The Revenant");
    BValue age = map.get("year");
    Assert.assertTrue(age instanceof BInteger);
    Assert.assertEquals(((BInteger) age).intValue(), 2015);
    BValue marks = map.get("genre");
    Assert.assertTrue(marks instanceof BStringArray);
    BStringArray genreArray = (BStringArray) marks;
    Assert.assertEquals(genreArray.get(0), "Adventure");
    Assert.assertEquals(genreArray.get(1), "Drama");
    Assert.assertEquals(genreArray.get(2), "Thriller");
    BValue actors = map.get("actors");
    Assert.assertTrue(actors instanceof BRefValueArray);
    BRefValueArray actorsArray = (BRefValueArray) actors;
    Assert.assertTrue(actorsArray.get(0) instanceof BStruct);
    Assert.assertEquals(actorsArray.get(0).stringValue(), "{fname:\"Leonardo\", lname:\"DiCaprio\", age:35}");
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BMap(org.ballerinalang.model.values.BMap) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) BString(org.ballerinalang.model.values.BString) BStringArray(org.ballerinalang.model.values.BStringArray) Test(org.testng.annotations.Test)

Example 40 with BRefValueArray

use of org.ballerinalang.model.values.BRefValueArray in project ballerina by ballerina-lang.

the class AbstractSQLAction method executeUpdateWithKeys.

protected void executeUpdateWithKeys(Context context, SQLDatasource datasource, String query, BStringArray keyColumns, BRefValueArray parameters) {
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    boolean isInTransaction = context.isInTransaction();
    try {
        conn = getDatabaseConnection(context, datasource, isInTransaction);
        String processedQuery = createProcessedQueryString(query, parameters);
        int keyColumnCount = 0;
        if (keyColumns != null) {
            keyColumnCount = (int) keyColumns.size();
        }
        if (keyColumnCount > 0) {
            String[] columnArray = new String[keyColumnCount];
            for (int i = 0; i < keyColumnCount; i++) {
                columnArray[i] = keyColumns.get(i);
            }
            stmt = conn.prepareStatement(processedQuery, columnArray);
        } else {
            stmt = conn.prepareStatement(processedQuery, Statement.RETURN_GENERATED_KEYS);
        }
        createProcessedStatement(conn, stmt, parameters);
        int count = stmt.executeUpdate();
        BInteger updatedCount = new BInteger(count);
        rs = stmt.getGeneratedKeys();
        /*The result set contains the auto generated keys. There can be multiple auto generated columns
            in a table.*/
        BStringArray generatedKeys = null;
        if (rs.next()) {
            generatedKeys = getGeneratedKeys(rs);
        }
        BRefValueArray tuple = new BRefValueArray(executeUpdateWithKeysTupleType);
        tuple.add(0, updatedCount);
        tuple.add(1, generatedKeys);
        context.setReturnValues(tuple);
    } catch (SQLException e) {
        throw new BallerinaException("execute update with generated keys failed: " + e.getMessage(), e);
    } finally {
        SQLDatasourceUtils.cleanupConnection(rs, stmt, conn, isInTransaction);
    }
}
Also used : SQLException(java.sql.SQLException) BInteger(org.ballerinalang.model.values.BInteger) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) ResultSet(java.sql.ResultSet) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) PreparedStatement(java.sql.PreparedStatement) BString(org.ballerinalang.model.values.BString) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BStringArray(org.ballerinalang.model.values.BStringArray)

Aggregations

BRefValueArray (org.ballerinalang.model.values.BRefValueArray)83 BValue (org.ballerinalang.model.values.BValue)45 Test (org.testng.annotations.Test)40 BStruct (org.ballerinalang.model.values.BStruct)37 BString (org.ballerinalang.model.values.BString)22 BInteger (org.ballerinalang.model.values.BInteger)18 BXMLSequence (org.ballerinalang.model.values.BXMLSequence)9 BeforeTest (org.testng.annotations.BeforeTest)9 BIntArray (org.ballerinalang.model.values.BIntArray)7 BMap (org.ballerinalang.model.values.BMap)7 BStringArray (org.ballerinalang.model.values.BStringArray)7 BXML (org.ballerinalang.model.values.BXML)7 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)7 HashMap (java.util.HashMap)6 BStructType (org.ballerinalang.model.types.BStructType)6 BRefType (org.ballerinalang.model.values.BRefType)5 SQLDatasource (org.ballerinalang.nativeimpl.actions.data.sql.SQLDatasource)5 BBooleanArray (org.ballerinalang.model.values.BBooleanArray)4 BFloatArray (org.ballerinalang.model.values.BFloatArray)4 IOException (java.io.IOException)3