Search in sources :

Example 26 with BStructType

use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.

the class ErrorTableEntry method getMatch.

public static ErrorTableEntry getMatch(PackageInfo packageInfo, int currentIP, final BStruct error) {
    ErrorTableAttributeInfo errorTable = (ErrorTableAttributeInfo) packageInfo.getAttributeInfo(AttributeInfo.Kind.ERROR_TABLE);
    List<ErrorTableEntry> errorTableEntries = errorTable != null ? errorTable.getErrorTableEntriesList() : new ArrayList<>();
    List<MatchedEntry> rangeMatched = new ArrayList<>();
    errorTableEntries.stream().filter(errorTableEntry -> errorTableEntry.matchRange(currentIP)).forEach(errorTableEntry -> {
        MatchedEntry entry = new MatchedEntry();
        entry.errorTableEntry = errorTableEntry;
        entry.ipSize = errorTableEntry.ipTo - errorTableEntry.ipFrom;
        if (errorTableEntry.getErrorStructCPIndex() == -1) {
            // match any.
            entry.status = 2;
            rangeMatched.add(entry);
        } else if (errorTableEntry.getError().getType().equals(error.getType())) {
            // exact match.
            entry.status = 0;
            rangeMatched.add(entry);
        } else if (CPU.checkStructEquivalency((BStructType) error.getType(), (BStructType) errorTableEntry.getError().getType())) {
            entry.status = 1;
            rangeMatched.add(entry);
        }
    });
    if (rangeMatched.size() == 0) {
        return null;
    }
    if (rangeMatched.size() == 1) {
        return rangeMatched.get(0).errorTableEntry;
    }
    MatchedEntry[] matchedEntries = rangeMatched.stream().sorted(Comparator.comparingInt(o -> o.ipSize)).toArray(MatchedEntry[]::new);
    int currentSize = 0;
    ErrorTableEntry errorTableEntry = null;
    for (int i = 0; i < matchedEntries.length; i++) {
        MatchedEntry entry = matchedEntries[i];
        if (currentSize < entry.ipSize) {
            if (errorTableEntry == null) {
                // Expand scope.
                currentSize = entry.ipSize;
            } else {
                // Return best match.
                return errorTableEntry;
            }
        }
        if (entry.status == 0) {
            // Best case.
            return entry.errorTableEntry;
        } else {
            if (errorTableEntry == null) {
                errorTableEntry = entry.errorTableEntry;
            } else {
                if (errorTableEntry.priority > entry.errorTableEntry.priority) {
                    // found a high order entry
                    errorTableEntry = entry.errorTableEntry;
                }
            }
        }
    }
    return errorTableEntry;
}
Also used : CPU(org.ballerinalang.bre.bvm.CPU) List(java.util.List) ErrorTableAttributeInfo(org.ballerinalang.util.codegen.attributes.ErrorTableAttributeInfo) BStructType(org.ballerinalang.model.types.BStructType) BStruct(org.ballerinalang.model.values.BStruct) AttributeInfo(org.ballerinalang.util.codegen.attributes.AttributeInfo) Comparator(java.util.Comparator) ArrayList(java.util.ArrayList) ErrorTableAttributeInfo(org.ballerinalang.util.codegen.attributes.ErrorTableAttributeInfo) ArrayList(java.util.ArrayList)

Example 27 with BStructType

use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.

the class SQLDataIterator method generateNext.

@Override
public BStruct generateNext() {
    if (this.type == null) {
        throw new BallerinaException("the expected struct type is not specified in action");
    }
    BStruct bStruct = new BStruct(this.type);
    int longRegIndex = -1;
    int doubleRegIndex = -1;
    int stringRegIndex = -1;
    int booleanRegIndex = -1;
    int blobRegIndex = -1;
    int refRegIndex = -1;
    int index = 0;
    String columnName = null;
    try {
        for (ColumnDefinition columnDef : columnDefs) {
            if (columnDef instanceof SQLColumnDefinition) {
                SQLColumnDefinition def = (SQLColumnDefinition) columnDef;
                columnName = def.getName();
                int sqlType = def.getSqlType();
                ++index;
                switch(sqlType) {
                    case Types.ARRAY:
                        Array dataArray = rs.getArray(index);
                        bStruct.setRefField(++refRegIndex, getDataArray(dataArray));
                        break;
                    case Types.CHAR:
                    case Types.VARCHAR:
                    case Types.LONGVARCHAR:
                    case Types.NCHAR:
                    case Types.NVARCHAR:
                    case Types.LONGNVARCHAR:
                        String sValue = rs.getString(index);
                        bStruct.setStringField(++stringRegIndex, sValue);
                        break;
                    case Types.BLOB:
                    case Types.BINARY:
                    case Types.VARBINARY:
                    case Types.LONGVARBINARY:
                        Blob value = rs.getBlob(index);
                        if (value != null) {
                            bStruct.setBlobField(++blobRegIndex, value.getBytes(1L, (int) value.length()));
                        } else {
                            bStruct.setBlobField(++blobRegIndex, new byte[0]);
                        }
                        break;
                    case Types.CLOB:
                        String clobValue = SQLDatasourceUtils.getString((rs.getClob(index)));
                        bStruct.setStringField(++stringRegIndex, clobValue);
                        break;
                    case Types.NCLOB:
                        String nClobValue = SQLDatasourceUtils.getString(rs.getNClob(index));
                        bStruct.setStringField(++stringRegIndex, nClobValue);
                        break;
                    case Types.DATE:
                        Date date = rs.getDate(index);
                        int fieldType = this.type.getStructFields()[index - 1].getFieldType().getTag();
                        if (fieldType == TypeTags.STRING_TAG) {
                            String dateValue = SQLDatasourceUtils.getString(date);
                            bStruct.setStringField(++stringRegIndex, dateValue);
                        } else if (fieldType == TypeTags.STRUCT_TAG) {
                            bStruct.setRefField(++refRegIndex, createTimeStruct(date.getTime()));
                        } else if (fieldType == TypeTags.INT_TAG) {
                            bStruct.setIntField(++longRegIndex, date.getTime());
                        }
                        break;
                    case Types.TIME:
                    case Types.TIME_WITH_TIMEZONE:
                        Time time = rs.getTime(index, utcCalendar);
                        fieldType = this.type.getStructFields()[index - 1].getFieldType().getTag();
                        if (fieldType == TypeTags.STRING_TAG) {
                            String timeValue = SQLDatasourceUtils.getString(time);
                            bStruct.setStringField(++stringRegIndex, timeValue);
                        } else if (fieldType == TypeTags.STRUCT_TAG) {
                            bStruct.setRefField(++refRegIndex, createTimeStruct(time.getTime()));
                        } else if (fieldType == TypeTags.INT_TAG) {
                            bStruct.setIntField(++longRegIndex, time.getTime());
                        }
                        break;
                    case Types.TIMESTAMP:
                    case Types.TIMESTAMP_WITH_TIMEZONE:
                        Timestamp timestamp = rs.getTimestamp(index, utcCalendar);
                        fieldType = this.type.getStructFields()[index - 1].getFieldType().getTag();
                        if (fieldType == TypeTags.STRING_TAG) {
                            String timestmpValue = SQLDatasourceUtils.getString(timestamp);
                            bStruct.setStringField(++stringRegIndex, timestmpValue);
                        } else if (fieldType == TypeTags.STRUCT_TAG) {
                            bStruct.setRefField(++refRegIndex, createTimeStruct(timestamp.getTime()));
                        } else if (fieldType == TypeTags.INT_TAG) {
                            bStruct.setIntField(++longRegIndex, timestamp.getTime());
                        }
                        break;
                    case Types.ROWID:
                        BValue strValue = new BString(new String(rs.getRowId(index).getBytes(), "UTF-8"));
                        bStruct.setStringField(++stringRegIndex, strValue.stringValue());
                        break;
                    case Types.TINYINT:
                    case Types.SMALLINT:
                        long iValue = rs.getInt(index);
                        bStruct.setIntField(++longRegIndex, iValue);
                        break;
                    case Types.INTEGER:
                    case Types.BIGINT:
                        long lValue = rs.getLong(index);
                        bStruct.setIntField(++longRegIndex, lValue);
                        break;
                    case Types.REAL:
                    case Types.FLOAT:
                        double fValue = rs.getFloat(index);
                        bStruct.setFloatField(++doubleRegIndex, fValue);
                        break;
                    case Types.DOUBLE:
                        double dValue = rs.getDouble(index);
                        bStruct.setFloatField(++doubleRegIndex, dValue);
                        break;
                    case Types.NUMERIC:
                    case Types.DECIMAL:
                        double decimalValue = 0;
                        BigDecimal bigDecimalValue = rs.getBigDecimal(index);
                        if (bigDecimalValue != null) {
                            decimalValue = bigDecimalValue.doubleValue();
                        }
                        bStruct.setFloatField(++doubleRegIndex, decimalValue);
                        break;
                    case Types.BIT:
                    case Types.BOOLEAN:
                        boolean boolValue = rs.getBoolean(index);
                        bStruct.setBooleanField(++booleanRegIndex, boolValue ? 1 : 0);
                        break;
                    case Types.STRUCT:
                        Struct structdata = (Struct) rs.getObject(index);
                        BType structFieldType = this.type.getStructFields()[index - 1].getFieldType();
                        fieldType = structFieldType.getTag();
                        if (fieldType == TypeTags.STRUCT_TAG) {
                            bStruct.setRefField(++refRegIndex, createUserDefinedType(structdata, (BStructType) structFieldType));
                        }
                        break;
                    default:
                        throw new BallerinaException("unsupported sql type " + sqlType + " found for the column " + columnName + " index:" + index);
                }
            }
        }
    } catch (Throwable e) {
        throw new BallerinaException("error in retrieving next value for column: " + columnName + ": at index:" + index + ":" + e.getMessage());
    }
    return bStruct;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) Blob(java.sql.Blob) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) Time(java.sql.Time) BString(org.ballerinalang.model.values.BString) Timestamp(java.sql.Timestamp) Date(java.sql.Date) BigDecimal(java.math.BigDecimal) ColumnDefinition(org.ballerinalang.model.ColumnDefinition) Struct(java.sql.Struct) BStruct(org.ballerinalang.model.values.BStruct) Array(java.sql.Array) BStructType(org.ballerinalang.model.types.BStructType) BType(org.ballerinalang.model.types.BType) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 28 with BStructType

use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.

the class AbstractSQLAction method getStructType.

protected BStructType getStructType(Context context) {
    BStructType structType = null;
    BTypeDescValue type = (BTypeDescValue) context.getNullableRefArgument(2);
    if (type != null) {
        structType = (BStructType) type.value();
    }
    return structType;
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BTypeDescValue(org.ballerinalang.model.values.BTypeDescValue)

Example 29 with BStructType

use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.

the class Call 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);
        executeProcedure(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 30 with BStructType

use of org.ballerinalang.model.types.BStructType in project ballerina by ballerina-lang.

the class SQLDatasourceUtils method getStructData.

private static Object[] getStructData(BValue value, Connection conn) throws SQLException {
    if (value == null || value.getType().getTag() != TypeTags.STRUCT_TAG) {
        return new Object[] { null, null };
    }
    String structuredSQLType = value.getType().getName().toUpperCase(Locale.getDefault());
    BStructType.StructField[] structFields = ((BStructType) value.getType()).getStructFields();
    int fieldCount = structFields.length;
    Object[] structData = new Object[fieldCount];
    int intFieldIndex = 0;
    int floatFieldIndex = 0;
    int stringFieldIndex = 0;
    int booleanFieldIndex = 0;
    int blobFieldIndex = 0;
    int refFieldIndex = 0;
    for (int i = 0; i < fieldCount; ++i) {
        BStructType.StructField field = structFields[i];
        int typeTag = field.getFieldType().getTag();
        switch(typeTag) {
            case TypeTags.INT_TAG:
                structData[i] = ((BStruct) value).getIntField(intFieldIndex);
                ++intFieldIndex;
                break;
            case TypeTags.FLOAT_TAG:
                structData[i] = ((BStruct) value).getFloatField(floatFieldIndex);
                ++floatFieldIndex;
                break;
            case TypeTags.STRING_TAG:
                structData[i] = ((BStruct) value).getStringField(stringFieldIndex);
                ++stringFieldIndex;
                break;
            case TypeTags.BOOLEAN_TAG:
                structData[i] = ((BStruct) value).getBooleanField(booleanFieldIndex) > 0;
                ++booleanFieldIndex;
                break;
            case TypeTags.BLOB_TAG:
                structData[i] = ((BStruct) value).getBlobField(blobFieldIndex);
                ++blobFieldIndex;
                break;
            case TypeTags.STRUCT_TAG:
                Object structValue = ((BStruct) value).getRefField(refFieldIndex);
                if (structValue instanceof BStruct) {
                    Object[] internalStructData = getStructData((BStruct) structValue, conn);
                    Object[] dataArray = (Object[]) internalStructData[0];
                    String internalStructType = (String) internalStructData[1];
                    structValue = conn.createStruct(internalStructType, dataArray);
                }
                structData[i] = structValue;
                ++refFieldIndex;
                break;
            default:
                throw new BallerinaException("unsupported data type for struct parameter: " + structuredSQLType);
        }
    }
    return new Object[] { structData, structuredSQLType };
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BStruct(org.ballerinalang.model.values.BStruct) BString(org.ballerinalang.model.values.BString) 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