public static String generateDeleteDataStatment(String tableName, BMap<?, ?> constrainedType) { StringBuilder sbSql = new StringBuilder(); sbSql.append(TableConstants.SQL_DELETE_FROM).append(tableName).append(TableConstants.SQL_WHERE); Collection<BField> structFields = ((BStructureType) constrainedType.getType()).getFields().values(); String sep = ""; for (BField sf : structFields) { String name = sf.getFieldName(); sbSql.append(sep).append(name).append(" = ? "); sep = TableConstants.SQL_AND; } return sbSql.toString(); }
!rhsType.getPackagePath().equals(lhsType.getPackagePath())) { return false; lhsType.getAttachedFunctions().length - 1 : lhsType.getAttachedFunctions().length; if (lhsType.getFields().size() > rhsType.getFields().size() || lhsAttachedFunctionCount > rhsType.getAttachedFunctions().length) { return false; rhsType.getPackagePath().equals(lhsType.getPackagePath()) ? checkPrivateObjectsEquivalency(lhsType, rhsType, unresolvedTypes) : checkPublicObjectsEquivalency(lhsType, rhsType, unresolvedTypes);
/** * Creates a GlobalMemoryBlock. * * @param structType type of the struct */ public GlobalMemoryBlock(BStructureType structType) { this.structType = structType; int[] fieldCount = this.structType.getFieldTypeCount(); longFields = new long[fieldCount[0]]; doubleFields = new double[fieldCount[1]]; stringFields = new String[fieldCount[2]]; intFields = new int[fieldCount[3]]; refFields = new BRefType[fieldCount[4]]; Arrays.fill(stringFields, BLangConstants.STRING_EMPTY_VALUE); }
private static boolean checkPrivateObjectsEquivalency(BStructureType lhsType, BStructureType rhsType, List<TypePair> unresolvedTypes) { Map<String, BField> rhsFields = rhsType.getFields(); for (Map.Entry<String, BField> lhsFieldEntry : lhsType.getFields().entrySet()) { BField rhsField = rhsFields.get(lhsFieldEntry.getKey()); if (rhsField == null || !isSameType(rhsField.fieldType, lhsFieldEntry.getValue().fieldType)) { return false; } } BAttachedFunction[] lhsFuncs = lhsType.getAttachedFunctions(); BAttachedFunction[] rhsFuncs = rhsType.getAttachedFunctions(); for (BAttachedFunction lhsFunc : lhsFuncs) { if (lhsFunc == lhsType.initializer || lhsFunc == lhsType.defaultsValuesInitFunc) { continue; } BAttachedFunction rhsFunc = getMatchingInvokableType(rhsFuncs, lhsFunc, unresolvedTypes); if (rhsFunc == null) { return false; } } return true; }
private void setAttachedFunctions(PackageInfo packageInfo) { TypeDefInfo[] structInfoEntries = packageInfo.getTypeDefInfoEntries(); for (TypeDefInfo structInfo : structInfoEntries) { if (structInfo.typeTag == TypeTags.FINITE_TYPE_TAG) { continue; } StructureTypeInfo structureTypeInfo = (StructureTypeInfo) structInfo.typeInfo; BStructureType structType = structureTypeInfo.getType(); // Resolve attached function signature if (structType.getTag() != TypeTags.OBJECT_TYPE_TAG && structType.getTag() != TypeTags.RECORD_TYPE_TAG) { continue; } int attachedFuncCount = structureTypeInfo.funcInfoEntries.size(); BAttachedFunction[] attachedFunctions = new BAttachedFunction[attachedFuncCount]; int count = 0; for (FunctionInfo attachedFuncInfo : structureTypeInfo.funcInfoEntries.values()) { BAttachedFunction attachedFunction = new BAttachedFunction(attachedFuncInfo.name, attachedFuncInfo.funcType, attachedFuncInfo.flags); attachedFunctions[count++] = attachedFunction; if (structureTypeInfo.initializer == attachedFuncInfo) { structureTypeInfo.getType().initializer = attachedFunction; } } structureTypeInfo.getType().setAttachedFunctions(attachedFunctions); } }
if (structType.getTag() == TypeTags.RECORD_TYPE_TAG && !((BRecordType) structType).sealed) { RecordTypeInfo recTypeInfo = (RecordTypeInfo) structureTypeInfo; String restTypeDesc = recTypeInfo.getRestFieldTypeSignature(); structType.setFieldTypeCount(attributeInfo.getVarTypeCount()); structType.setFields(structFields);
private void updateAttachFunctionInfo(PackageInfo packageInfo, BType attachedType, String funcName, FunctionInfo functionInfo) throws IOException { // Append the receiver type to the parameter types. This is done because in the VM, // first parameter will always be the attached type. These param types will be used // to allocate worker local data. // This is the only place where we append the receiver to the params. BType[] paramTypes = Stream.concat(Stream.of(functionInfo.attachedToType), Stream.of(functionInfo.getParamTypes())) .toArray(BType[]::new); functionInfo.setParamTypes(paramTypes); if (attachedType.getTag() != TypeTags.OBJECT_TYPE_TAG && attachedType.getTag() != TypeTags.RECORD_TYPE_TAG) { return; } //Update the attachedFunctionInfo String objectInit; BStructureType structType = (BStructureType) attachedType; if (attachedType.getTag() == TypeTags.OBJECT_TYPE_TAG) { objectInit = CONSTRUCTOR_FUNCTION_SUFFIX; } else { objectInit = structType.getName() + INIT_FUNCTION_SUFFIX; } StructureTypeInfo typeInfo = (StructureTypeInfo) structType.getTypeInfo(); typeInfo.funcInfoEntries.put(funcName, functionInfo); // Setting the object initializer if (objectInit.equals(funcName)) { typeInfo.initializer = functionInfo; } }
@Override public String getName() { return type.getName(); }
private static Strand invokeVirtualFunction(Strand ctx, StackFrame sf, int receiver, FunctionInfo virtualFuncInfo, int[] argRegs, int retReg, int flags) { BMap<String, BValue> structVal = (BMap<String, BValue>) sf.refRegs[receiver]; // TODO use ObjectTypeInfo once record init function is removed StructureTypeInfo structInfo = (StructureTypeInfo) ((BStructureType) structVal.getType()).getTypeInfo(); FunctionInfo attachedFuncInfo = structInfo.funcInfoEntries.get(virtualFuncInfo.getName()); return invokeCallable(ctx, attachedFuncInfo, argRegs, retReg, flags); }
private static StructureTypeInfo getStructureTypeInfo(ProgramFile programFile) { PackageInfo runtimePackage = programFile.getPackageInfo(BALLERINA_RUNTIME_PKG); StructureTypeInfo typeInfo = runtimePackage.getStructInfo(STRUCT_CALL_STACK_ELEMENT); if (typeInfo == null || typeInfo.getType().getTag() != TypeTags.RECORD_TYPE_TAG) { throw new BallerinaConnectorException("record - " + STRUCT_CALL_STACK_ELEMENT + " does not exist"); } return typeInfo; } }
@Override public void addAttributeInfo(AttributeInfo.Kind attributeKind, AttributeInfo attributeInfo) { attributeInfoMap.put(attributeKind, attributeInfo); if (attributeKind == AttributeInfo.Kind.VARIABLE_TYPE_COUNT_ATTRIBUTE) { // TODO Move this out of the program file to a program context.. Runtime representation of a program. // TODO ProgramFile is the static program data. VarTypeCountAttributeInfo varTypeCountAttribInfo = (VarTypeCountAttributeInfo) attributeInfo; int[] globalVarCount = varTypeCountAttribInfo.getVarTypeCount(); // TODO Introduce an abstraction for memory blocks // Initialize global memory block BStructureType dummyType = new BRecordType(null, "", "", 0); dummyType.setFieldTypeCount(globalVarCount); this.globalMemoryBlock = new GlobalMemoryBlock(dummyType); } }
@Override public String getPackage() { return type.getPackagePath(); }
private static boolean checkPublicObjectsEquivalency(BStructureType lhsType, BStructureType rhsType, List<TypePair> unresolvedTypes) { if (rhsType.getFields().values().stream().anyMatch(field -> !Flags.isFlagOn(field.flags, Flags.PUBLIC))) { return false; Map<String, BField> rhsFields = rhsType.getFields(); for (Map.Entry<String, BField> lhsFieldEntry : lhsType.getFields().entrySet()) { BField rhsField = rhsFields.get(lhsFieldEntry.getKey()); if (rhsField == null || !Flags.isFlagOn(lhsFieldEntry.getValue().flags, Flags.PUBLIC) || BAttachedFunction[] lhsFuncs = lhsType.getAttachedFunctions(); BAttachedFunction[] rhsFuncs = rhsType.getAttachedFunctions(); for (BAttachedFunction lhsFunc : lhsFuncs) { if (lhsFunc == lhsType.initializer || lhsFunc == lhsType.defaultsValuesInitFunc) {
public String createTable(String fromTableName, String joinTableName, String query, BStructureType tableType, BValueArray params) { String newTableName = TableConstants.TABLE_PREFIX + tableType.getName().toUpperCase() + "_" + getTableID(); String sqlStmt = query.replaceFirst(TableConstants.TABLE_NAME_REGEX, fromTableName); if (joinTableName != null && !joinTableName.isEmpty()) { sqlStmt = sqlStmt.replaceFirst(TableConstants.TABLE_NAME_REGEX, joinTableName); } sqlStmt = generateCreateTableStatment(sqlStmt, newTableName); prepareAndExecuteStatement(sqlStmt, params); return newTableName; }
StructureTypeInfo structInfo = (StructureTypeInfo) structType.getTypeInfo(); for (StructFieldInfo fieldInfo : structInfo.getFieldInfoEntries()) { BType fieldType = fieldInfo.getFieldType();
public static BMap<String, BValue> createObject(ProgramFile programFile, String pkgPath, String objectName, BValue... values) { PackageInfo packageInfo = programFile.getPackageInfo(pkgPath); if (packageInfo == null) { throw new BallerinaConnectorException("module - " + pkgPath + " does not exist"); } StructureTypeInfo typeInfo = packageInfo.getStructInfo(objectName); if (typeInfo == null || typeInfo.getType().getTag() != TypeTags.OBJECT_TYPE_TAG) { throw new BallerinaConnectorException("object - " + objectName + " does not exist"); } return BLangVMStructs.createObject((ObjectTypeInfo) typeInfo, values); }
private void initGlobalMemBlock(PackageInfo[] packageInfoArray) { for (PackageInfo packageInfo : packageInfoArray) { // Get the package-level variable count from the attribute. AttributeInfo attributeInfo = packageInfo.getAttributeInfo( AttributeInfo.Kind.VARIABLE_TYPE_COUNT_ATTRIBUTE); VarTypeCountAttributeInfo varTypeCountAttribInfo = (VarTypeCountAttributeInfo) attributeInfo; int[] globalVarCount = varTypeCountAttribInfo.getVarTypeCount(); // We are using the struct value to hold package-level variable values for the moment. BStructureType dummyType = new BRecordType(null, "", "", 0); dummyType.setFieldTypeCount(globalVarCount); globalMemBlock[packageInfo.pkgIndex] = new GlobalMemoryBlock(dummyType); } } }
public static String generateInsertDataStatment(String tableName, BMap<?, ?> constrainedType) { StringBuilder sbSql = new StringBuilder(); StringBuilder sbValues = new StringBuilder(); sbSql.append(TableConstants.SQL_INSERT_INTO).append(tableName).append(" ("); Collection<BField> structFields = ((BStructureType) constrainedType.getType()).getFields().values(); String sep = ""; for (BField sf : structFields) { String name = sf.getFieldName(); sbSql.append(sep).append(name).append(" "); sbValues.append(sep).append("?"); sep = ","; } sbSql.append(") values (").append(sbValues).append(")"); return sbSql.toString(); }
public void addData(BMap<String, BValue> data, Context context) { if (data.getType() != this.constraintType) { throw new BallerinaException("incompatible types: record of type:" + data.getType().getName() + " cannot be added to a table with type:" + this.constraintType.getName()); } tableProvider.insertData(tableName, data); reset(); }
private static BMap<String, BValue> convertMapToStruct(Strand ctx, BMap<String, BValue> bMap, BStructureType structType) { BMap<String, BValue> bStruct = new BMap<>(structType); StructureTypeInfo structInfo = (StructureTypeInfo) structType.getTypeInfo();