Search in sources :

Example 1 with ColumnDefinition

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

the class AbstractSQLAction method getColumnDefinitions.

private List<ColumnDefinition> getColumnDefinitions(ResultSet rs) throws SQLException {
    List<ColumnDefinition> columnDefs = new ArrayList<>();
    Set<String> columnNames = new HashSet<>();
    ResultSetMetaData rsMetaData = rs.getMetaData();
    int cols = rsMetaData.getColumnCount();
    for (int i = 1; i <= cols; i++) {
        String colName = rsMetaData.getColumnLabel(i);
        if (columnNames.contains(colName)) {
            String tableName = rsMetaData.getTableName(i).toUpperCase();
            colName = tableName + "." + colName;
        }
        int colType = rsMetaData.getColumnType(i);
        TypeKind mappedType = SQLDatasourceUtils.getColumnType(colType);
        columnDefs.add(new SQLDataIterator.SQLColumnDefinition(colName, mappedType, colType));
        columnNames.add(colName);
    }
    return columnDefs;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ArrayList(java.util.ArrayList) TypeKind(org.ballerinalang.model.types.TypeKind) BString(org.ballerinalang.model.values.BString) ColumnDefinition(org.ballerinalang.model.ColumnDefinition) HashSet(java.util.HashSet) SQLDataIterator(org.ballerinalang.nativeimpl.actions.data.sql.SQLDataIterator)

Example 2 with ColumnDefinition

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

the class TableIterator method generateColumnDefinitions.

private void generateColumnDefinitions() {
    BStructType.StructField[] structFields = this.type.getStructFields();
    columnDefs = new ArrayList<>(structFields.length);
    for (BStructType.StructField sf : structFields) {
        BType type = sf.getFieldType();
        TypeKind typeKind = TypeKind.ANY;
        switch(type.getTag()) {
            case TypeTags.INT_TAG:
                typeKind = TypeKind.INT;
                break;
            case TypeTags.STRING_TAG:
                typeKind = TypeKind.STRING;
                break;
            case TypeTags.FLOAT_TAG:
                typeKind = TypeKind.FLOAT;
                break;
            case TypeTags.BOOLEAN_TAG:
                typeKind = TypeKind.BOOLEAN;
                break;
            case TypeTags.JSON_TAG:
                typeKind = TypeKind.JSON;
                break;
            case TypeTags.XML_TAG:
                typeKind = TypeKind.XML;
                break;
            case TypeTags.BLOB_TAG:
                typeKind = TypeKind.BLOB;
                break;
            case TypeTags.ARRAY_TAG:
                typeKind = TypeKind.ARRAY;
                break;
        }
        ColumnDefinition def = new ColumnDefinition(sf.fieldName, typeKind);
        columnDefs.add(def);
    }
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BType(org.ballerinalang.model.types.BType) TypeKind(org.ballerinalang.model.types.TypeKind) ColumnDefinition(org.ballerinalang.model.ColumnDefinition)

Example 3 with ColumnDefinition

use of org.ballerinalang.model.ColumnDefinition 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)

Aggregations

ColumnDefinition (org.ballerinalang.model.ColumnDefinition)3 BStructType (org.ballerinalang.model.types.BStructType)2 BType (org.ballerinalang.model.types.BType)2 TypeKind (org.ballerinalang.model.types.TypeKind)2 BString (org.ballerinalang.model.values.BString)2 BigDecimal (java.math.BigDecimal)1 Array (java.sql.Array)1 Blob (java.sql.Blob)1 Date (java.sql.Date)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 Struct (java.sql.Struct)1 Time (java.sql.Time)1 Timestamp (java.sql.Timestamp)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 BStruct (org.ballerinalang.model.values.BStruct)1 BValue (org.ballerinalang.model.values.BValue)1 SQLDataIterator (org.ballerinalang.nativeimpl.actions.data.sql.SQLDataIterator)1 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)1