Search in sources :

Example 31 with BStructType

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);
    }
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BStruct(org.ballerinalang.model.values.BStruct) HashMap(java.util.HashMap) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) SQLDatasource(org.ballerinalang.nativeimpl.actions.data.sql.SQLDatasource)

Example 32 with BStructType

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);
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo)

Example 33 with BStructType

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;
}
Also used : BRefType(org.ballerinalang.model.values.BRefType) BStructType(org.ballerinalang.model.types.BStructType) BNewArray(org.ballerinalang.model.values.BNewArray) JsonNode(org.ballerinalang.model.util.JsonNode) BJSON(org.ballerinalang.model.values.BJSON)

Example 34 with BStructType

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);
            }
        }
    });
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BArrayType(org.ballerinalang.model.types.BArrayType) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) BStructType(org.ballerinalang.model.types.BStructType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BType(org.ballerinalang.model.types.BType) Event(org.ballerinalang.siddhi.core.event.Event) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 35 with BStructType

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;
}
Also used : SQLException(java.sql.SQLException) JsonNode(org.ballerinalang.model.util.JsonNode) BigDecimal(java.math.BigDecimal) Struct(java.sql.Struct) BStructType(org.ballerinalang.model.types.BStructType) BType(org.ballerinalang.model.types.BType) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Aggregations

BStructType (org.ballerinalang.model.types.BStructType)40 BStruct (org.ballerinalang.model.values.BStruct)24 BType (org.ballerinalang.model.types.BType)13 BString (org.ballerinalang.model.values.BString)11 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)11 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)7 BValue (org.ballerinalang.model.values.BValue)7 StructInfo (org.ballerinalang.util.codegen.StructInfo)7 BRefType (org.ballerinalang.model.values.BRefType)6 BBoolean (org.ballerinalang.model.values.BBoolean)5 BJSON (org.ballerinalang.model.values.BJSON)5 BTypeDescValue (org.ballerinalang.model.values.BTypeDescValue)5 Struct (java.sql.Struct)4 BFloat (org.ballerinalang.model.values.BFloat)4 BInteger (org.ballerinalang.model.values.BInteger)4 PackageInfo (org.ballerinalang.util.codegen.PackageInfo)4 BigDecimal (java.math.BigDecimal)3 SQLException (java.sql.SQLException)3 BMapType (org.ballerinalang.model.types.BMapType)3 BMap (org.ballerinalang.model.values.BMap)3