Search in sources :

Example 1 with JsonValue

use of com.couchbase.client.java.document.json.JsonValue in project teiid by teiid.

the class CouchbaseMetadataProcessor method scanObjectRow.

private void scanObjectRow(String key, String keyInSource, JsonObject value, MetadataFactory mf, Table table, String referenceTableName, boolean isNestedType, Dimension dimension) {
    Set<String> names = new TreeSet<String>(value.getNames());
    for (String name : names) {
        String columnName = name;
        Object columnValue = value.get(columnName);
        String columnType = getDataType(columnValue);
        if (columnType.equals(OBJECT)) {
            JsonValue jsonValue = (JsonValue) columnValue;
            String newKey = key + UNDERSCORE + columnName;
            String newKeyInSource = keyInSource + SOURCE_SEPARATOR + nameInSource(columnName);
            if (isObjectJsonType(columnValue)) {
                scanRow(newKey, newKeyInSource, jsonValue, mf, table, referenceTableName, true, dimension);
            } else if (isArrayJsonType(columnValue)) {
                String tableName = repleaceTypedName(table.getName(), newKey);
                String tableNameInSource = newKeyInSource + SQUARE_BRACKETS;
                Dimension d = new Dimension();
                Table subTable = addTable(tableName, tableNameInSource, true, referenceTableName, d, mf);
                scanRow(newKey, newKeyInSource, jsonValue, mf, subTable, referenceTableName, true, d);
            }
        } else {
            if (isNestedType) {
                columnName = key + UNDERSCORE + columnName;
            }
            String columnNameInSource = keyInSource + SOURCE_SEPARATOR + nameInSource(name);
            addColumn(columnName, columnType, columnValue, true, columnNameInSource, table, mf);
        }
    }
}
Also used : Table(org.teiid.metadata.Table) JsonValue(com.couchbase.client.java.document.json.JsonValue) JsonObject(com.couchbase.client.java.document.json.JsonObject)

Example 2 with JsonValue

use of com.couchbase.client.java.document.json.JsonValue in project teiid by teiid.

the class CouchbaseMetadataProcessor method scanArrayRow.

private void scanArrayRow(String keyspace, String keyInSource, JsonArray array, MetadataFactory mf, Table table, String referenceTableName, boolean isNestedType, Dimension dimension) {
    if (array.size() > 0) {
        for (int i = 0; i < array.size(); i++) {
            Object element = array.get(i);
            if (isObjectJsonType(element)) {
                JsonObject json = (JsonObject) element;
                for (String name : new TreeSet<String>(json.getNames())) {
                    Object columnValue = json.get(name);
                    String columnType = this.getDataType(columnValue);
                    if (columnType.equals(OBJECT)) {
                        JsonValue jsonValue = (JsonValue) columnValue;
                        if (isObjectJsonType(jsonValue)) {
                            scanRow(keyspace, keyInSource, jsonValue, mf, table, referenceTableName, true, dimension);
                        } else if (isArrayJsonType(jsonValue)) {
                            String tableName = table.getName() + UNDERSCORE + name + UNDERSCORE + dimension.get();
                            String tableNameInSrc = table.getNameInSource() + SOURCE_SEPARATOR + nameInSource(name) + SQUARE_BRACKETS;
                            Dimension d = new Dimension();
                            Table subTable = addTable(tableName, tableNameInSrc, true, referenceTableName, d, mf);
                            scanRow(keyspace, keyInSource, jsonValue, mf, subTable, referenceTableName, true, d);
                        }
                    } else {
                        String columnName = table.getName() + UNDERSCORE + name;
                        String columnNameInSource = table.getNameInSource() + SOURCE_SEPARATOR + nameInSource(name);
                        addColumn(columnName, columnType, columnValue, true, columnNameInSource, table, mf);
                    }
                }
            } else if (isArrayJsonType(element)) {
                String tableName = table.getName() + UNDERSCORE + dimension.get();
                String tableNameInSrc = table.getNameInSource() + SQUARE_BRACKETS;
                Dimension d = dimension.copy();
                Table subTable = addTable(tableName, tableNameInSrc, true, referenceTableName, d, mf);
                scanRow(keyspace, keyInSource, (JsonValue) element, mf, subTable, referenceTableName, true, d);
            } else {
                String elementType = getDataType(element);
                String columnName = table.getName();
                String columnNameInSource = table.getNameInSource();
                addColumn(columnName, elementType, element, true, columnNameInSource, table, mf);
            }
        }
    } else {
        String columnName = table.getName();
        addColumn(columnName, null, null, true, null, table, mf);
    }
}
Also used : Table(org.teiid.metadata.Table) JsonValue(com.couchbase.client.java.document.json.JsonValue) JsonObject(com.couchbase.client.java.document.json.JsonObject) JsonObject(com.couchbase.client.java.document.json.JsonObject)

Example 3 with JsonValue

use of com.couchbase.client.java.document.json.JsonValue in project teiid by teiid.

the class CouchbaseExecutionFactory method retrieveValue.

public Object retrieveValue(Class<?> columnType, Object value) throws TranslatorException {
    if (value == null) {
        return null;
    }
    if (value.getClass().equals(columnType)) {
        return value;
    }
    if (columnType.equals(ClobType.class)) {
        boolean json = false;
        if (value instanceof JsonValue) {
            json = true;
        }
        ClobImpl clob = new ClobImpl(value.toString());
        ClobType result = new ClobType(clob);
        result.setType(json ? Type.JSON : Type.TEXT);
        return result;
    }
    if (columnType.equals(BigInteger.class)) {
        if (value instanceof BigDecimal) {
            return ((BigDecimal) value).toBigInteger();
        }
        return BigInteger.valueOf(((Number) value).longValue());
    }
    if (columnType.equals(BigDecimal.class)) {
        if (value instanceof BigInteger) {
            value = new BigDecimal((BigInteger) value);
        } else {
            value = BigDecimal.valueOf(((Number) value).doubleValue());
        }
    }
    return value;
}
Also used : ClobType(org.teiid.core.types.ClobType) JsonValue(com.couchbase.client.java.document.json.JsonValue) BigInteger(java.math.BigInteger) ClobImpl(org.teiid.core.types.ClobImpl) BigDecimal(java.math.BigDecimal)

Aggregations

JsonValue (com.couchbase.client.java.document.json.JsonValue)3 JsonObject (com.couchbase.client.java.document.json.JsonObject)2 Table (org.teiid.metadata.Table)2 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 ClobImpl (org.teiid.core.types.ClobImpl)1 ClobType (org.teiid.core.types.ClobType)1