Search in sources :

Example 6 with BIntArray

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

the class TypeCastExprTest method testStructToAnyExplicit.

@Test(description = "Test explicit casting struct to any")
public void testStructToAnyExplicit() {
    BValue[] returns = BRunUtil.invoke(result, "testStructToAnyExplicit");
    Assert.assertTrue(returns[0] instanceof BStruct);
    BStruct student = (BStruct) returns[0];
    Assert.assertEquals(student.getStringField(0), "Supun");
    Assert.assertEquals(student.getIntField(0), 25);
    BValue address = student.getRefField(0);
    Assert.assertTrue(address instanceof BMap<?, ?>);
    BMap<String, BString> addressMap = (BMap<String, BString>) address;
    Assert.assertEquals(addressMap.get("city").stringValue(), "Kandy");
    Assert.assertEquals(addressMap.get("country").stringValue(), "SriLanka");
    BIntArray marksArray = (BIntArray) student.getRefField(1);
    Assert.assertTrue(marksArray instanceof BIntArray);
    Assert.assertEquals(marksArray.size(), 2);
    Assert.assertEquals(marksArray.get(0), 24);
    Assert.assertEquals(marksArray.get(1), 81);
    Assert.assertEquals(student.getFloatField(0), 0.0);
}
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) BString(org.ballerinalang.model.values.BString) BIntArray(org.ballerinalang.model.values.BIntArray) Test(org.testng.annotations.Test)

Example 7 with BIntArray

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

the class NativeConversionTest method testMapToStruct.

@Test(enabled = false)
public void testMapToStruct() {
    BValue[] returns = BRunUtil.invoke(compileResult, "testMapToStruct");
    Assert.assertTrue(returns[0] instanceof BStruct);
    BStruct struct = (BStruct) returns[0];
    String name = struct.getStringField(0);
    Assert.assertEquals(name, "Child");
    long age = struct.getIntField(0);
    Assert.assertEquals(age, 25);
    BValue parent = struct.getRefField(0);
    Assert.assertTrue(parent instanceof BStruct);
    BStruct parentStruct = (BStruct) parent;
    Assert.assertEquals(parentStruct.getStringField(0), "Parent");
    Assert.assertEquals(parentStruct.getIntField(0), 50);
    Assert.assertEquals(parentStruct.getRefField(0), null);
    Assert.assertEquals(parentStruct.getRefField(1), null);
    Assert.assertEquals(parentStruct.getRefField(2), null);
    Assert.assertEquals(parentStruct.getRefField(3), null);
    BValue info = struct.getRefField(1);
    Assert.assertTrue(info instanceof BJSON);
    Assert.assertEquals(info.stringValue(), "{\"status\":\"single\"}");
    BValue address = struct.getRefField(2);
    Assert.assertTrue(address instanceof BMap<?, ?>);
    BMap<String, ?> addressMap = (BMap<String, ?>) address;
    Assert.assertEquals(addressMap.get("city").stringValue(), "Colombo");
    Assert.assertEquals(addressMap.get("country").stringValue(), "SriLanka");
    BValue marks = struct.getRefField(3);
    Assert.assertTrue(marks instanceof BIntArray);
    BIntArray marksArray = (BIntArray) marks;
    Assert.assertEquals(marksArray.get(0), 87);
    Assert.assertEquals(marksArray.get(1), 94);
    Assert.assertEquals(marksArray.get(2), 72);
}
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) BJSON(org.ballerinalang.model.values.BJSON) BIntArray(org.ballerinalang.model.values.BIntArray) Test(org.testng.annotations.Test)

Example 8 with BIntArray

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

the class NativeConversionTest method testJsonToStruct.

@Test
public void testJsonToStruct() {
    BValue[] returns = BRunUtil.invoke(compileResult, "testJsonToStruct");
    Assert.assertTrue(returns[0] instanceof BStruct);
    BStruct struct = (BStruct) returns[0];
    String name = struct.getStringField(0);
    Assert.assertEquals(name, "Child");
    long age = struct.getIntField(0);
    Assert.assertEquals(age, 25);
    BValue parent = struct.getRefField(0);
    Assert.assertTrue(parent instanceof BStruct);
    BStruct parentStruct = (BStruct) parent;
    Assert.assertEquals(parentStruct.getStringField(0), "Parent");
    Assert.assertEquals(parentStruct.getIntField(0), 50);
    Assert.assertEquals(parentStruct.getRefField(0), null);
    Assert.assertEquals(parentStruct.getRefField(1), null);
    Assert.assertEquals(parentStruct.getRefField(2), null);
    Assert.assertEquals(parentStruct.getRefField(3), null);
    BValue info = struct.getRefField(1);
    Assert.assertTrue(info instanceof BJSON);
    Assert.assertEquals(info.stringValue(), "{\"status\":\"single\"}");
    BValue address = struct.getRefField(2);
    Assert.assertTrue(address instanceof BMap<?, ?>);
    BMap<String, ?> addressMap = (BMap<String, ?>) address;
    Assert.assertEquals(addressMap.get("city").stringValue(), "Colombo");
    Assert.assertEquals(addressMap.get("country").stringValue(), "SriLanka");
    BValue marks = struct.getRefField(3);
    Assert.assertTrue(marks instanceof BIntArray);
    BIntArray marksArray = (BIntArray) marks;
    Assert.assertEquals(marksArray.size(), 2);
    Assert.assertEquals(marksArray.get(0), 56);
    Assert.assertEquals(marksArray.get(1), 79);
}
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) BJSON(org.ballerinalang.model.values.BJSON) BIntArray(org.ballerinalang.model.values.BIntArray) Test(org.testng.annotations.Test)

Example 9 with BIntArray

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

the class NativeConversionTest method testStructToMap.

@Test
public void testStructToMap() {
    BValue[] returns = BRunUtil.invoke(compileResult, "testStructToMap");
    Assert.assertTrue(returns[0] instanceof BMap<?, ?>);
    BMap<String, ?> map = (BMap<String, ?>) returns[0];
    BValue name = map.get("name");
    Assert.assertTrue(name instanceof BString);
    Assert.assertEquals(name.stringValue(), "Child");
    BValue age = map.get("age");
    Assert.assertTrue(age instanceof BInteger);
    Assert.assertEquals(((BInteger) age).intValue(), 25);
    BValue parent = map.get("parent");
    Assert.assertTrue(parent instanceof BStruct);
    BStruct parentStruct = (BStruct) parent;
    Assert.assertEquals(parentStruct.getStringField(0), "Parent");
    Assert.assertEquals(parentStruct.getIntField(0), 50);
    BValue address = map.get("address");
    Assert.assertTrue(address instanceof BMap<?, ?>);
    BMap<String, ?> addressMap = (BMap<String, ?>) address;
    Assert.assertEquals(addressMap.get("city").stringValue(), "Colombo");
    Assert.assertEquals(addressMap.get("country").stringValue(), "SriLanka");
    BValue info = map.get("info");
    Assert.assertTrue(info instanceof BJSON);
    Assert.assertEquals(info.stringValue(), "{\"status\":\"single\"}");
    BValue marks = map.get("marks");
    Assert.assertTrue(marks instanceof BIntArray);
    BIntArray marksArray = (BIntArray) marks;
    Assert.assertEquals(marksArray.get(0), 67);
    Assert.assertEquals(marksArray.get(1), 38);
    Assert.assertEquals(marksArray.get(2), 91);
}
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) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON) BIntArray(org.ballerinalang.model.values.BIntArray) Test(org.testng.annotations.Test)

Example 10 with BIntArray

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

the class AbstractSQLAction method createProcessedStatement.

private void createProcessedStatement(Connection conn, PreparedStatement stmt, BRefValueArray params, String dataSourceName) {
    if (params == null) {
        return;
    }
    int paramCount = (int) params.size();
    int currentOrdinal = 0;
    for (int index = 0; index < paramCount; index++) {
        BStruct paramStruct = (BStruct) params.get(index);
        if (paramStruct != null) {
            String sqlType = getSQLType(paramStruct);
            BValue value = paramStruct.getRefField(1);
            int direction = getParameterDirection(paramStruct);
            // If the parameter is an array and sql type is not "array" then treat it as an array of parameters
            if (value != null && value.getType().getTag() == TypeTags.ARRAY_TAG && !Constants.SQLDataTypes.ARRAY.equalsIgnoreCase(sqlType)) {
                int arrayLength = (int) ((BNewArray) value).size();
                int typeTag = ((BArrayType) value.getType()).getElementType().getTag();
                for (int i = 0; i < arrayLength; i++) {
                    BValue paramValue;
                    switch(typeTag) {
                        case TypeTags.INT_TAG:
                            paramValue = new BInteger(((BIntArray) value).get(i));
                            break;
                        case TypeTags.FLOAT_TAG:
                            paramValue = new BFloat(((BFloatArray) value).get(i));
                            break;
                        case TypeTags.STRING_TAG:
                            paramValue = new BString(((BStringArray) value).get(i));
                            break;
                        case TypeTags.BOOLEAN_TAG:
                            paramValue = new BBoolean(((BBooleanArray) value).get(i) > 0);
                            break;
                        case TypeTags.BLOB_TAG:
                            paramValue = new BBlob(((BBlobArray) value).get(i));
                            break;
                        default:
                            throw new BallerinaException("unsupported array type for parameter index " + index);
                    }
                    if (Constants.SQLDataTypes.REFCURSOR.equals(sqlType)) {
                        setParameter(conn, stmt, sqlType, paramValue, direction, currentOrdinal, dataSourceName);
                    } else {
                        setParameter(conn, stmt, sqlType, paramValue, direction, currentOrdinal);
                    }
                    currentOrdinal++;
                }
            } else {
                if (Constants.SQLDataTypes.REFCURSOR.equals(sqlType)) {
                    setParameter(conn, stmt, sqlType, value, direction, currentOrdinal, dataSourceName);
                } else {
                    setParameter(conn, stmt, sqlType, value, direction, currentOrdinal);
                }
                currentOrdinal++;
            }
        } else {
            SQLDatasourceUtils.setNullObject(stmt, index);
            currentOrdinal++;
        }
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) BBoolean(org.ballerinalang.model.values.BBoolean) BString(org.ballerinalang.model.values.BString) BStringArray(org.ballerinalang.model.values.BStringArray) BIntArray(org.ballerinalang.model.values.BIntArray) BBlobArray(org.ballerinalang.model.values.BBlobArray) BFloat(org.ballerinalang.model.values.BFloat) BFloatArray(org.ballerinalang.model.values.BFloatArray) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BBlob(org.ballerinalang.model.values.BBlob)

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