Search in sources :

Example 1 with JsonArray

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

the class TestCouchbase method testSetValue.

@Test(expected = IllegalArgumentException.class)
public void testSetValue() {
    CouchbaseExecutionFactory cef = new CouchbaseExecutionFactory();
    JsonArray array = JsonArray.create();
    cef.setValue(array, Float.class, 1.0f);
    assertEquals(1.0f, array.get(0));
}
Also used : JsonArray(com.couchbase.client.java.document.json.JsonArray) Test(org.junit.Test)

Example 2 with JsonArray

use of com.couchbase.client.java.document.json.JsonArray in project jnosql-diana-driver by eclipse.

the class EntityConverter method convertIterable.

private static void convertIterable(JsonObject jsonObject, Document document, Object value) {
    JsonObject map = JsonObject.create();
    JsonArray array = JsonArray.create();
    Iterable.class.cast(value).forEach(element -> {
        if (Document.class.isInstance(element)) {
            Document subdocument = Document.class.cast(element);
            map.put(subdocument.getName(), subdocument.get());
        } else if (isSudDocument(element)) {
            JsonObject subJson = JsonObject.create();
            stream(Iterable.class.cast(element).spliterator(), false).forEach(getSubdocument(subJson));
            array.add(subJson);
        } else {
            array.add(element);
        }
    });
    if (array.isEmpty()) {
        jsonObject.put(document.getName(), map);
    } else {
        jsonObject.put(document.getName(), array);
    }
}
Also used : JsonArray(com.couchbase.client.java.document.json.JsonArray) JsonObject(com.couchbase.client.java.document.json.JsonObject) Document(org.jnosql.diana.api.document.Document)

Example 3 with JsonArray

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

the class N1QLUpdateVisitor method visit.

@Override
public void visit(Insert obj) {
    visit(obj.getTable());
    List<CBColumnData> rowCache = new ArrayList<N1QLUpdateVisitor.CBColumnData>();
    Integer typeIndex = null;
    for (int i = 0; i < obj.getColumns().size(); i++) {
        ColumnReference col = obj.getColumns().get(i);
        CBColumn column = formCBColumn(col);
        CBColumnData cacheData = new CBColumnData(col.getType(), column);
        rowCache.add(cacheData);
        if (typedName != null && typedName.equals(nameInSource(cacheData.getCBColumn().getLeafName()))) {
            typeIndex = i;
        }
    }
    Map<Integer, Parameter> preparedValues = new HashMap<Integer, Parameter>();
    ExpressionValueSource evs = (ExpressionValueSource) obj.getValueSource();
    for (int i = 0; i < evs.getValues().size(); i++) {
        Expression exp = evs.getValues().get(i);
        if (exp instanceof Literal) {
            Literal l = (Literal) exp;
            rowCache.get(i).setValue(l.getValue());
            if (typeIndex != null && typeIndex == i && !typedValue.equals(getValueString(l.getType(), l.getValue()))) {
                throw new TeiidRuntimeException(CouchbasePlugin.Event.TEIID29022, CouchbasePlugin.Util.gs(CouchbasePlugin.Event.TEIID29022, typedValue));
            }
        } else if (exp instanceof Parameter) {
            Parameter p = (Parameter) exp;
            preparedValues.put(i, p);
            if (typeIndex != null && typeIndex == i) {
                throw new TeiidRuntimeException(CouchbasePlugin.Event.TEIID29022, CouchbasePlugin.Util.gs(CouchbasePlugin.Event.TEIID29022, typedValue));
            }
        } else {
            throw new TeiidRuntimeException(CouchbasePlugin.Event.TEIID29024, CouchbasePlugin.Util.gs(CouchbasePlugin.Event.TEIID29024));
        }
    }
    if (typedName != null && typeIndex == null) {
        // add type
        boolean added = false;
        for (Column c : obj.getTable().getMetadataObject().getColumns()) {
            if (!nameInSource(c.getSourceName()).endsWith(typedName)) {
                continue;
            }
            ColumnReference cr = new ColumnReference(obj.getTable(), c.getName(), c, c.getJavaType());
            CBColumn column = formCBColumn(cr);
            CBColumnData cacheData = new CBColumnData(TypeFacility.RUNTIME_TYPES.STRING, column);
            cacheData.setValue(getRawValue(typedValue));
            rowCache.add(cacheData);
            added = true;
            break;
        }
        if (!added) {
            // TODO: could support this without requiring a type column
            throw new TeiidRuntimeException(CouchbasePlugin.Event.TEIID29023, CouchbasePlugin.Util.gs(CouchbasePlugin.Event.TEIID29023, typedName));
        }
    }
    if (isArrayTable) {
        if (preparedValues.size() > 0) {
            throw new TeiidRuntimeException(CouchbasePlugin.Event.TEIID29017, CouchbasePlugin.Util.gs(CouchbasePlugin.Event.TEIID29017, obj));
        }
        buffer.append(UPDATE).append(SPACE).append(this.keyspace).append(SPACE);
        appendDocumentID(obj, rowCache);
        String arrayIDX = buildNestedArrayIdx(obj, rowCache);
        JsonArray array = JsonArray.create();
        for (int i = 0; i < rowCache.size(); i++) {
            CBColumnData columnData = rowCache.get(i);
            if (!columnData.getCBColumn().isPK() && !columnData.getCBColumn().isIdx()) {
                if (columnData.getCBColumn().hasLeaf()) {
                    String attr = columnData.getCBColumn().getLeafName();
                    String path = columnData.getCBColumn().getNameInSource();
                    JsonObject nestedObj = findObject(array, path);
                    if (nestedObj == null) {
                        nestedObj = JsonObject.create();
                        array.add(nestedObj);
                    }
                    ef.setValue(nestedObj, attr, columnData.getColumnType(), columnData.getValue());
                } else {
                    ef.setValue(array, columnData.getColumnType(), columnData.getValue());
                }
            }
        }
        StringBuilder left = new StringBuilder();
        left.append("IFMISSINGORNULL").append(LPAREN).append(arrayIDX).append(COMMA).append(SPACE).append(SQUARE_BRACKETS).append(RPAREN);
        appendConcat(arrayIDX, left, array);
    } else {
        if (obj.isUpsert()) {
            buffer.append(getUpsertKeyword());
        } else {
            buffer.append(getInsertKeyword());
            ;
        }
        buffer.append(SPACE).append(INTO).append(SPACE).append(keyspace).append(SPACE);
        buffer.append(LPAREN).append(KEY).append(COMMA).append(SPACE).append(VALUE).append(RPAREN);
        if (preparedValues.size() > 0) {
            appendBulkValues(preparedValues, rowCache, obj);
            return;
        }
        String documentID = null;
        JsonObject json = JsonObject.create();
        for (int i = 0; i < rowCache.size(); i++) {
            CBColumnData columnData = rowCache.get(i);
            if (columnData.getCBColumn().isPK()) {
                Object value = columnData.getValue();
                if (value != null) {
                    documentID = value.toString();
                }
            } else {
                String attr = columnData.getCBColumn().getLeafName();
                String path = columnData.getCBColumn().getNameInSource();
                JsonObject nestedObj = findObject(json, path);
                ef.setValue(nestedObj, attr, columnData.getColumnType(), columnData.getValue());
            }
        }
        if (null == documentID) {
            throw new TeiidRuntimeException(CouchbasePlugin.Event.TEIID29006, CouchbasePlugin.Util.gs(CouchbasePlugin.Event.TEIID29006, obj));
        }
        buffer.append(SPACE).append(VALUES).append(SPACE).append(LPAREN).append(SQLConstants.Tokens.QUOTE).append(escapeString(documentID, SQLConstants.Tokens.QUOTE)).append(SQLConstants.Tokens.QUOTE);
        buffer.append(COMMA).append(SPACE).append(json).append(RPAREN);
    }
    appendRetuning();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonObject(com.couchbase.client.java.document.json.JsonObject) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) JsonArray(com.couchbase.client.java.document.json.JsonArray) Column(org.teiid.metadata.Column) JsonObject(com.couchbase.client.java.document.json.JsonObject)

Example 4 with JsonArray

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

the class TestN1QLUpdateVisitor method testNestedJsonArrayType.

@Test
public void testNestedJsonArrayType() {
    JsonObject order = formOder();
    JsonArray jsonArray = order.getArray("Items");
    List<Object> items = jsonArray.toList();
    for (int i = 0; i < items.size(); i++) {
        Object item = items.get(i);
        assertEquals(item.getClass(), HashMap.class);
    }
    for (int i = 0; i < jsonArray.size(); i++) {
        Object item = jsonArray.get(i);
        assertEquals(item.getClass(), JsonObject.class);
    }
}
Also used : JsonArray(com.couchbase.client.java.document.json.JsonArray) JsonObject(com.couchbase.client.java.document.json.JsonObject) JsonObject(com.couchbase.client.java.document.json.JsonObject) Test(org.junit.Test)

Example 5 with JsonArray

use of com.couchbase.client.java.document.json.JsonArray in project tutorials by eugenp.

the class StudentGradeService method countStudentsByCourse.

public Map<String, Long> countStudentsByCourse() {
    ViewQuery query = ViewQuery.from("studentGrades", "countStudentsByCourse").reduce().groupLevel(1);
    ViewResult result = bucket.query(query);
    Map<String, Long> numStudentsByCourse = new HashMap<>();
    for (ViewRow row : result.allRows()) {
        JsonArray keyArray = (JsonArray) row.key();
        String course = keyArray.getString(0);
        long count = Long.valueOf(row.value().toString());
        numStudentsByCourse.put(course, count);
    }
    return numStudentsByCourse;
}
Also used : JsonArray(com.couchbase.client.java.document.json.JsonArray) HashMap(java.util.HashMap) ViewQuery(com.couchbase.client.java.view.ViewQuery) ViewResult(com.couchbase.client.java.view.ViewResult) ViewRow(com.couchbase.client.java.view.ViewRow)

Aggregations

JsonArray (com.couchbase.client.java.document.json.JsonArray)6 JsonObject (com.couchbase.client.java.document.json.JsonObject)3 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 ParameterizedN1qlQuery (com.couchbase.client.java.query.ParameterizedN1qlQuery)1 ViewQuery (com.couchbase.client.java.view.ViewQuery)1 ViewResult (com.couchbase.client.java.view.ViewResult)1 ViewRow (com.couchbase.client.java.view.ViewRow)1 ArrayList (java.util.ArrayList)1 Document (org.jnosql.diana.api.document.Document)1 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)1 Column (org.teiid.metadata.Column)1