use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.
the class Select method execute.
@Override
public void execute(Context context) {
try {
BStruct bConnector = (BStruct) context.getRefArgument(0);
String query = context.getStringArgument(0);
BRefValueArray parameters = (BRefValueArray) context.getNullableRefArgument(1);
BStructType structType = getStructType(context);
SQLDatasource datasource = (SQLDatasource) bConnector.getNativeData(Constants.CLIENT_CONNECTOR);
Map<String, String> tags = new HashMap<>();
tags.put(TAG_KEY_DB_STATEMENT, query);
tags.put(TAG_KEY_DB_TYPE, TAG_DB_TYPE_SQL);
TraceUtil.getTracer(context.getParentWorkerExecutionContext()).addTags(tags);
executeQuery(context, datasource, query, parameters, structType);
} catch (Throwable e) {
context.setReturnValues(SQLDatasourceUtils.getSQLConnectorError(context, e));
SQLDatasourceUtils.handleErrorOnTransaction(context);
}
}
use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.
the class BTestUtils method createAndGetStruct.
public static BStruct createAndGetStruct(ProgramFile programFile, String packagePath, String structName) {
PackageInfo structPackageInfo = programFile.getPackageInfo(packagePath);
StructInfo structInfo = structPackageInfo.getStructInfo(structName);
BStructType structType = structInfo.getType();
return new BStruct(structType);
}
use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.
the class Equals method isEqual.
/**
* Deep equal or strict equal implementation for any value type.
*
* @param lhsValue The value on the left side.
* @param rhsValue The value on the right side.
* @return True if values are equal, else false.
*/
@SuppressWarnings("rawtypes")
private boolean isEqual(BValue lhsValue, BValue rhsValue) {
if (null == lhsValue && null == rhsValue) {
return true;
}
if (null == lhsValue || null == rhsValue) {
return false;
}
// Required for any == any.
if (lhsValue.getType().getTag() != rhsValue.getType().getTag()) {
return false;
}
if (null != lhsValue.getType().getPackagePath() && null != rhsValue.getType().getPackagePath() && !lhsValue.getType().getPackagePath().equals(rhsValue.getType().getPackagePath())) {
return false;
}
if (null != lhsValue.getType().getName() && null != rhsValue.getType().getName() && !lhsValue.getType().getName().equals(rhsValue.getType().getName())) {
return false;
}
// Handling JSON.
if (lhsValue instanceof BJSON && rhsValue instanceof BJSON) {
JsonNode lhsJSON = ((BJSON) lhsValue).value();
JsonNode rhsJSON = ((BJSON) rhsValue).value();
return isEqual(lhsJSON, rhsJSON);
}
switch(lhsValue.getType().getTag()) {
case TypeTags.STRING_TAG:
case TypeTags.INT_TAG:
case TypeTags.FLOAT_TAG:
case TypeTags.BOOLEAN_TAG:
case TypeTags.TYPEDESC_TAG:
BRefType lhsRef = (BRefType) lhsValue;
BRefType rhsRef = (BRefType) rhsValue;
if (null == lhsRef.value() && null == rhsRef.value()) {
return true;
}
if (null == lhsRef.value() || null == rhsRef.value()) {
return false;
}
return lhsRef.value().equals(rhsRef.value());
case TypeTags.STRUCT_TAG:
BStructType lhsStructType = (BStructType) lhsValue.getType();
BStructType rhsStructType = (BStructType) rhsValue.getType();
if (!Arrays.equals(lhsStructType.getFieldTypeCount(), rhsStructType.getFieldTypeCount())) {
return false;
}
return isEqual((BStruct) lhsValue, (BStruct) rhsValue, lhsStructType);
case TypeTags.MAP_TAG:
return isEqual((BMap) lhsValue, (BMap) rhsValue);
case TypeTags.ARRAY_TAG:
case TypeTags.ANY_TAG:
// TODO: This block should ideally be in the ARRAY_TAG case only. Not ANY_TAG. #4505.
if (lhsValue instanceof BNewArray) {
BNewArray lhsArray = (BNewArray) lhsValue;
BNewArray rhsArray = (BNewArray) rhsValue;
if (lhsArray.size() != rhsArray.size()) {
return false;
}
for (int i = 0; i < lhsArray.size(); i++) {
if (!isEqual(lhsArray.getBValue(i), rhsArray.getBValue(i))) {
return false;
}
}
}
break;
case TypeTags.XML_TAG:
// TODO: https://www.pixelstech.net/article/1453272563-Canonicalize-XML-in-Java
break;
default:
return false;
}
return true;
}
use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.
the class StreamingRuntimeManager method addCallback.
public void addCallback(String streamId, BFunctionPointer functionPointer, SiddhiAppRuntime siddhiAppRuntime) {
BType[] parameters = functionPointer.value().getFunctionInfo().getParamTypes();
BStructType structType = (BStructType) ((BArrayType) parameters[0]).getElementType();
if (!(parameters[0] instanceof BArrayType)) {
throw new BallerinaException("incompatible function: inline function needs to be a function accepting" + " a struct array");
}
siddhiAppRuntime.addCallback(streamId, new StreamCallback() {
@Override
public void receive(Event[] events) {
for (Event event : events) {
AtomicInteger intVarIndex = new AtomicInteger(-1);
AtomicInteger floatVarIndex = new AtomicInteger(-1);
AtomicInteger boolVarIndex = new AtomicInteger(-1);
AtomicInteger stringVarIndex = new AtomicInteger(-1);
BStruct output = new BStruct(structType);
for (Object field : event.getData()) {
if (field instanceof Long) {
output.setIntField(intVarIndex.incrementAndGet(), (Long) field);
} else if (field instanceof Double) {
output.setFloatField(floatVarIndex.incrementAndGet(), (Double) field);
} else if (field instanceof Boolean) {
output.setBooleanField(boolVarIndex.incrementAndGet(), (Integer) field);
} else if (field instanceof String) {
output.setStringField(stringVarIndex.incrementAndGet(), (String) field);
}
}
BValue[] args = { output };
BLangFunctions.invokeCallable(functionPointer.value().getFunctionInfo(), args);
}
}
});
}
use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.
the class TableJSONDataSource method getStructData.
private static JsonNode getStructData(Object[] data, BStructType.StructField[] structFields, int index) {
JsonNode jsonData = null;
try {
if (structFields == null) {
jsonData = new JsonNode(Type.ARRAY);
if (data != null) {
for (Object value : data) {
if (value instanceof String) {
jsonData.add((String) value);
} else if (value instanceof Boolean) {
jsonData.add((Boolean) value);
} else if (value instanceof Long) {
jsonData.add((long) value);
} else if (value instanceof Double) {
jsonData.add((double) value);
} else if (value instanceof Integer) {
jsonData.add((int) value);
} else if (value instanceof Float) {
jsonData.add((float) value);
} else if (value instanceof BigDecimal) {
jsonData.add(((BigDecimal) value).doubleValue());
}
}
}
} else {
jsonData = new JsonNode(Type.OBJECT);
boolean structError = true;
if (data != null) {
int i = 0;
for (Object value : data) {
BType internaltType = structFields[index - 1].fieldType;
if (internaltType.getTag() == TypeTags.STRUCT_TAG) {
BStructType.StructField[] interanlStructFields = ((BStructType) internaltType).getStructFields();
if (interanlStructFields != null) {
if (value instanceof String) {
jsonData.set(interanlStructFields[i].fieldName, (String) value);
} else if (value instanceof Boolean) {
jsonData.set(interanlStructFields[i].fieldName, (Boolean) value);
} else if (value instanceof Long) {
jsonData.set(interanlStructFields[i].fieldName, (long) value);
} else if (value instanceof Double) {
jsonData.set(interanlStructFields[i].fieldName, (double) value);
} else if (value instanceof Integer) {
jsonData.set(interanlStructFields[i].fieldName, (int) value);
} else if (value instanceof Float) {
jsonData.set(interanlStructFields[i].fieldName, (float) value);
} else if (value instanceof BigDecimal) {
jsonData.set(interanlStructFields[i].fieldName, ((BigDecimal) value).doubleValue());
} else if (value instanceof Struct) {
jsonData.set(interanlStructFields[i].fieldName, getStructData(((Struct) value).getAttributes(), interanlStructFields, i + 1));
}
structError = false;
}
}
++i;
}
}
if (structError) {
throw new BallerinaException("error in constructing the json object from struct type data");
}
}
} catch (SQLException e) {
throw new BallerinaException("error in retrieving struct data to construct the inner json object:" + e.getMessage());
}
return jsonData;
}
Aggregations