Search in sources :

Example 1 with TypeKind

use of org.ballerinalang.model.types.TypeKind 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 TypeKind

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

Aggregations

ColumnDefinition (org.ballerinalang.model.ColumnDefinition)2 TypeKind (org.ballerinalang.model.types.TypeKind)2 ResultSetMetaData (java.sql.ResultSetMetaData)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 BStructType (org.ballerinalang.model.types.BStructType)1 BType (org.ballerinalang.model.types.BType)1 BString (org.ballerinalang.model.values.BString)1 SQLDataIterator (org.ballerinalang.nativeimpl.actions.data.sql.SQLDataIterator)1