Search in sources :

Example 91 with TeiidRuntimeException

use of org.teiid.core.TeiidRuntimeException in project teiid by teiid.

the class N1QLUpdateVisitor method appendBulkValues.

private void appendBulkValues(Map<Integer, Parameter> preparedValues, List<CBColumnData> rowCache, Insert command) {
    BatchedCommand batchCommand = (BatchedCommand) command;
    Iterator<? extends List<?>> vi = batchCommand.getParameterValues();
    int maxBulkSize = ef.getMaxBulkInsertSize();
    int cursor = 0;
    List<String> n1qlList = new ArrayList<>();
    StringBuilder sb = new StringBuilder();
    boolean comma = false;
    while (vi != null && vi.hasNext()) {
        if (cursor == 0) {
            sb.append(buffer);
        }
        cursor++;
        List<?> row = vi.next();
        String documentID = null;
        JsonObject json = JsonObject.create();
        for (int i = 0; i < rowCache.size(); i++) {
            CBColumnData columnData = rowCache.get(i);
            Parameter p = preparedValues.get(i);
            Object value = null;
            if (p != null) {
                value = row.get(p.getValueIndex());
            } else {
                value = columnData.getValue();
            }
            if (columnData.getCBColumn().isPK()) {
                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(), value);
            }
        }
        if (null == documentID) {
            throw new TeiidRuntimeException(CouchbasePlugin.Event.TEIID29006, CouchbasePlugin.Util.gs(CouchbasePlugin.Event.TEIID29006, command));
        }
        if (comma) {
            sb.append(COMMA);
        } else {
            comma = true;
        }
        sb.append(SPACE).append(VALUES).append(SPACE).append(LPAREN).append(SQLConstants.Tokens.QUOTE).append(escapeString(documentID, SQLConstants.Tokens.QUOTE)).append(SQLConstants.Tokens.QUOTE);
        sb.append(COMMA).append(SPACE).append(json).append(RPAREN);
        if (cursor == maxBulkSize) {
            sb.append(SPACE).append(RETURNING).append(SPACE).append(buildMeta(this.keyspace)).append(SPACE);
            sb.append(AS).append(SPACE).append(PK);
            n1qlList.add(sb.toString());
            cursor = 0;
            sb.delete(0, sb.length());
            comma = false;
        }
    }
    if (cursor > 0 && cursor < maxBulkSize) {
        sb.append(SPACE).append(RETURNING).append(SPACE).append(buildMeta(this.keyspace)).append(SPACE);
        sb.append(AS).append(SPACE).append(PK);
        n1qlList.add(sb.toString());
    }
    this.bulkCommands = n1qlList.toArray(new String[n1qlList.size()]);
}
Also used : ArrayList(java.util.ArrayList) JsonObject(com.couchbase.client.java.document.json.JsonObject) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) JsonObject(com.couchbase.client.java.document.json.JsonObject)

Example 92 with TeiidRuntimeException

use of org.teiid.core.TeiidRuntimeException 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 93 with TeiidRuntimeException

use of org.teiid.core.TeiidRuntimeException in project teiid by teiid.

the class ODataTypeManager method convertToODataURIValue.

public static String convertToODataURIValue(Object val, String odataType) throws EdmPrimitiveTypeException {
    if (val == null) {
        // is this correct? //$NON-NLS-1$
        return "null";
    }
    if (odataType.startsWith("Edm.")) {
        // $NON-NLS-1$
        odataType = odataType.substring(4);
    }
    if (val instanceof GeometryType) {
        Geometry g;
        try {
            g = GeometryUtils.getGeometry((GeometryType) val);
        } catch (FunctionExecutionException e1) {
            throw new EdmPrimitiveTypeException(e1.getMessage(), e1);
        }
        StringWriter sw = new StringWriter();
        // $NON-NLS-1$
        sw.write("geometry'SRID=");
        sw.write(String.valueOf(g.getSRID()));
        // $NON-NLS-1$
        sw.write(";");
        ODataWKTWriter writer = new ODataWKTWriter();
        try {
            writer.write(g, sw);
        } catch (IOException e) {
            throw new TeiidRuntimeException(e);
        }
        // $NON-NLS-1$
        sw.write("'");
        return sw.toString();
    }
    EdmPrimitiveTypeKind kind = EdmPrimitiveTypeKind.valueOf(odataType);
    String value = EdmPrimitiveTypeFactory.getInstance(kind).valueToString(val, true, null, null, Integer.MAX_VALUE, true);
    if (kind == EdmPrimitiveTypeKind.String) {
        return EdmString.getInstance().toUriLiteral(value);
    }
    return value;
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) StringWriter(java.io.StringWriter) EdmPrimitiveTypeException(org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException) IOException(java.io.IOException) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) EdmPrimitiveTypeKind(org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind) EdmString(org.apache.olingo.commons.core.edm.primitivetype.EdmString)

Example 94 with TeiidRuntimeException

use of org.teiid.core.TeiidRuntimeException in project teiid by teiid.

the class IndexMetadataRepository method loadAll.

// there are multiple threads trying to load this, since the initial index lading is not
// optimized for multi-thread loading this locking to sync will work
private synchronized void loadAll(Collection<Datatype> systemDatatypes, Map<String, ? extends VDBResource> resources) throws IOException {
    if (this.loaded) {
        return;
    }
    ArrayList<Index> tmp = new ArrayList<Index>();
    for (VDBResource f : resources.values()) {
        if (f.getName().endsWith(VDBResources.INDEX_EXT)) {
            Index index = new Index(f);
            index.setDoCache(true);
            tmp.add(index);
        }
    }
    for (Index index : tmp) {
        try {
            IEntryResult[] results = SimpleIndexUtil.queryIndex(new Index[] { index }, new char[0], true, true, false);
            recordFactory.getMetadataRecord(results);
        } catch (TeiidException e) {
            throw new TeiidRuntimeException(RuntimeMetadataPlugin.Event.TEIID80000, e);
        }
    }
    // force close, since we cached the index files
    for (Index index : tmp) {
        index.close();
    }
    Map<String, AbstractMetadataRecord> uuidToRecord = getByType(MetadataConstants.RECORD_TYPE.DATATYPE);
    if (systemDatatypes != null) {
        for (Datatype datatype : systemDatatypes) {
            uuidToRecord.put(datatype.getUUID(), datatype);
        }
    }
    this.loaded = true;
    // associate the annotation/extension metadata
    for (Map<String, AbstractMetadataRecord> map : allRecords.values()) {
        for (AbstractMetadataRecord metadataRecord : map.values()) {
            String uuid = metadataRecord.getUUID();
            metadataRecord.setAnnotation(this.annotationCache.get(uuid));
            metadataRecord.setProperties(this.extensionCache.get(uuid));
        }
    }
}
Also used : IEntryResult(org.teiid.core.index.IEntryResult) ArrayList(java.util.ArrayList) Index(org.teiid.internal.core.index.Index) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) TeiidException(org.teiid.core.TeiidException)

Example 95 with TeiidRuntimeException

use of org.teiid.core.TeiidRuntimeException in project teiid by teiid.

the class ODataSchemaBuilder method buildMetadata.

public static CsdlSchema buildMetadata(String namespace, org.teiid.metadata.Schema teiidSchema) {
    try {
        CsdlSchema csdlSchema = new CsdlSchema();
        String fullSchemaName = namespace + "." + teiidSchema.getName();
        csdlSchema.setNamespace(fullSchemaName).setAlias(teiidSchema.getName());
        buildEntityTypes(namespace, teiidSchema, csdlSchema);
        buildProcedures(teiidSchema, csdlSchema);
        return csdlSchema;
    } catch (Exception e) {
        throw new TeiidRuntimeException(e);
    }
}
Also used : TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException)

Aggregations

TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)103 IOException (java.io.IOException)27 TeiidComponentException (org.teiid.core.TeiidComponentException)22 TeiidException (org.teiid.core.TeiidException)22 ArrayList (java.util.ArrayList)20 TeiidProcessingException (org.teiid.core.TeiidProcessingException)17 SQLException (java.sql.SQLException)11 ObjectInputStream (java.io.ObjectInputStream)9 HashMap (java.util.HashMap)9 InputStream (java.io.InputStream)7 Map (java.util.Map)7 Test (org.junit.Test)7 QueryMetadataException (org.teiid.api.exception.query.QueryMetadataException)7 ObjectOutputStream (java.io.ObjectOutputStream)6 List (java.util.List)6 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)6 XMLStreamException (javax.xml.stream.XMLStreamException)5 QueryPlannerException (org.teiid.api.exception.query.QueryPlannerException)5 JsonObject (com.couchbase.client.java.document.json.JsonObject)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4