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);
}
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);
}
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);
}
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);
}
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++;
}
}
}
Aggregations