Search in sources :

Example 1 with TeiidRuntimeException

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

the class N1QLUpdateVisitor method appendClauses.

private void appendClauses(List<CBColumnData> rowCache, List<Condition> otherConditions, List<SetClause> setClauses) {
    String useKey = this.getUsesKeyString(rowCache);
    boolean isTyped = false;
    if (useKey != null) {
        buffer.append(SPACE).append(useKey);
    }
    if (setClauses != null) {
        buffer.append(SPACE).append(SET).append(SPACE);
        for (SetClause clause : setClauses) {
            ColumnReference col = clause.getSymbol();
            if (isPKColumn(col)) {
                throw new TeiidRuntimeException(CouchbasePlugin.Event.TEIID29025, CouchbasePlugin.Util.gs(CouchbasePlugin.Event.TEIID29025, typedValue));
            }
            CBColumn column = formCBColumn(col);
            CBColumnData cacheData = new CBColumnData(col.getType(), column);
            Object value = clause.getValue();
            if (clause.getValue() instanceof Literal) {
                value = ((Literal) clause.getValue()).getValue();
            }
            if (typedName != null && typedName.equals(nameInSource(cacheData.getCBColumn().getLeafName())) && (value == null || !typedValue.equals(getValueString(cacheData.getColumnType(), value)))) {
                throw new TeiidRuntimeException(CouchbasePlugin.Event.TEIID29022, CouchbasePlugin.Util.gs(CouchbasePlugin.Event.TEIID29022, typedValue));
            }
        }
        append(setClauses);
    }
    boolean hasPredicate = false;
    for (CBColumnData columnData : rowCache) {
        if (typedName != null && typedName.equals(nameInSource(columnData.getCBColumn().getLeafName()))) {
            if (typedValue.equals(getValueString(columnData.getColumnType(), columnData.getValue()))) {
                isTyped = true;
            }
        // else no rows affected
        }
        if (columnData.getCBColumn().isPK()) {
            continue;
        }
        if (!hasPredicate) {
            buffer.append(SPACE).append(WHERE);
            hasPredicate = true;
        } else {
            buffer.append(SPACE).append(AND);
        }
        buffer.append(SPACE).append(columnData.getCBColumn().getNameInSource()).append(SPACE);
        buffer.append(Tokens.EQ).append(SPACE);
        buffer.append(getValueString(columnData.getColumnType(), columnData.getValue()));
    }
    boolean hasTypedValue = !isTyped && typedName != null && typedValue != null;
    if (!otherConditions.isEmpty()) {
        if (!hasPredicate) {
            buffer.append(SPACE).append(WHERE);
            hasPredicate = true;
        } else {
            buffer.append(SPACE).append(AND);
        }
        buffer.append(SPACE);
        if (hasTypedValue) {
            buffer.append(LPAREN);
            append(LanguageUtil.combineCriteria(otherConditions));
            buffer.append(RPAREN);
        } else {
            append(LanguageUtil.combineCriteria(otherConditions));
        }
    }
    if (hasTypedValue) {
        if (hasPredicate) {
            buffer.append(SPACE).append(AND);
        } else {
            buffer.append(SPACE).append(WHERE);
        }
        buffer.append(SPACE).append(keyspace).append(SOURCE_SEPARATOR).append(typedName).append(SPACE).append(EQ).append(SPACE).append(typedValue);
    }
}
Also used : JsonObject(com.couchbase.client.java.document.json.JsonObject) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException)

Example 2 with TeiidRuntimeException

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

the class N1QLUpdateVisitor method visit.

@Override
public void visit(Update obj) {
    visit(obj.getTable());
    buffer.append(UPDATE).append(SPACE).append(this.keyspace);
    Condition where = obj.getWhere();
    List<CBColumnData> whereRowCache = new ArrayList<N1QLUpdateVisitor.CBColumnData>();
    List<Condition> conditions = findEqualityPredicates(where, whereRowCache);
    if (isArrayTable) {
        if (whereRowCache.size() < (dimension + 1)) {
            throw new TeiidRuntimeException(CouchbasePlugin.Event.TEIID29019, CouchbasePlugin.Util.gs(CouchbasePlugin.Event.TEIID29019, obj));
        }
        List<CBColumnData> rowCache = new ArrayList<N1QLUpdateVisitor.CBColumnData>();
        for (SetClause clause : obj.getChanges()) {
            ColumnReference col = clause.getSymbol();
            if (isPKColumn(col)) {
                throw new TeiidRuntimeException(CouchbasePlugin.Event.TEIID29025, CouchbasePlugin.Util.gs(CouchbasePlugin.Event.TEIID29025, typedValue));
            }
            CBColumn column = formCBColumn(col);
            CBColumnData cacheData = new CBColumnData(col.getType(), column);
            Object value = clause.getValue();
            if (value instanceof Literal) {
                value = ((Literal) value).getValue();
            }
            if (typedName != null && typedName.equals(nameInSource(cacheData.getCBColumn().getLeafName()))) {
                if (!typedValue.equals(getValueString(cacheData.getColumnType(), value))) {
                    throw new TeiidRuntimeException(CouchbasePlugin.Event.TEIID29022, CouchbasePlugin.Util.gs(CouchbasePlugin.Event.TEIID29022, typedValue));
                }
                continue;
            }
            cacheData.setValue(value);
            rowCache.add(cacheData);
        }
        List<CBColumnData> setList = new ArrayList<>(rowCache.size());
        for (int i = 0; i < rowCache.size(); i++) {
            if (rowCache.get(i).getCBColumn().isPK() || rowCache.get(i).getCBColumn().isIdx()) {
                throw new TeiidRuntimeException(CouchbasePlugin.Event.TEIID29018, CouchbasePlugin.Util.gs(CouchbasePlugin.Event.TEIID29018, obj));
            }
            setList.add(rowCache.get(i));
        }
        buffer.append(SPACE);
        appendDocumentID(obj, whereRowCache);
        buffer.append(SPACE);
        List<CBColumnData> idxList = new ArrayList<>(dimension);
        List<CBColumnData> equalityWhereList = new ArrayList<>(whereRowCache.size());
        for (CBColumnData columnData : whereRowCache) {
            if (columnData.getCBColumn().isIdx()) {
                idxList.add(columnData);
            } else if (columnData.getCBColumn().isPK()) {
                continue;
            } else {
                equalityWhereList.add(columnData);
            }
        }
        this.setAttrArray = buildNestedArrayIdx(setAttr, dimension, idxList, obj) + LSBRACE + idxList.get(idxList.size() - 1).getValue() + RSBRACE;
        buffer.append(SET);
        boolean comma = false;
        for (CBColumnData columnData : setList) {
            if (comma) {
                buffer.append(COMMA).append(SPACE);
            } else {
                buffer.append(SPACE);
                comma = true;
            }
            String setRef = buildNestedAttrRef(setAttrArray, columnData.getCBColumn());
            buffer.append(setRef).append(SPACE).append(EQ).append(SPACE).append(getValueString(columnData.getColumnType(), columnData.getValue()));
        }
        boolean hasPredicate = false;
        for (CBColumnData columnData : equalityWhereList) {
            if (!hasPredicate) {
                buffer.append(SPACE).append(WHERE);
                hasPredicate = true;
            } else {
                buffer.append(SPACE).append(AND);
            }
            String whereRef = buildNestedAttrRef(setAttrArray, columnData.getCBColumn());
            buffer.append(SPACE).append(whereRef).append(SPACE).append(EQ).append(SPACE).append(getValueString(columnData.getColumnType(), columnData.getValue()));
        }
        for (Condition condition : conditions) {
            if (!hasPredicate) {
                buffer.append(SPACE).append(WHERE).append(SPACE);
                hasPredicate = true;
            } else {
                buffer.append(SPACE).append(AND).append(SPACE);
            }
            append(condition);
        }
    } else {
        appendClauses(whereRowCache, conditions, obj.getChanges());
    }
    appendRetuning();
}
Also used : ArrayList(java.util.ArrayList) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) JsonObject(com.couchbase.client.java.document.json.JsonObject)

Example 3 with TeiidRuntimeException

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

the class AccumuloDataTypeManager method deserialize.

public static Object deserialize(final byte[] value, final Class<?> expectedType) {
    if (value == null || Arrays.equals(value, EMPTY_BYTES)) {
        return null;
    }
    try {
        if (expectedType.isAssignableFrom(Clob.class)) {
            return new ClobImpl(new InputStreamFactory() {

                @Override
                public InputStream getInputStream() throws IOException {
                    return ObjectConverterUtil.convertToInputStream(value);
                }
            }, -1);
        } else if (expectedType.isAssignableFrom(Blob.class)) {
            return new BlobType(new BlobImpl(new InputStreamFactory() {

                @Override
                public InputStream getInputStream() throws IOException {
                    return ObjectConverterUtil.convertToInputStream(value);
                }
            }));
        } else if (expectedType.isAssignableFrom(SQLXML.class)) {
            return new SQLXMLImpl(new InputStreamFactory() {

                @Override
                public InputStream getInputStream() throws IOException {
                    return ObjectConverterUtil.convertToInputStream(value);
                }
            });
        } else if (expectedType.isAssignableFrom(BinaryType.class)) {
            return new BinaryType(value);
        } else if (expectedType.isAssignableFrom(GeometryType.class)) {
            GeometryType result = new GeometryType(Arrays.copyOf(value, value.length - 4));
            int srid = (((value[value.length - 4] & 0xff) << 24) + ((value[value.length - 3] & 0xff) << 16) + ((value[value.length - 2] & 0xff) << 8) + ((value[value.length - 1] & 0xff) << 0));
            result.setSrid(srid);
            return result;
        } else if (expectedType.isAssignableFrom(byte[].class)) {
            return value;
        } else if (expectedType.isAssignableFrom(String.class) || expectedType.isAssignableFrom(Boolean.class) || expectedType.isAssignableFrom(Boolean.class) || expectedType.isAssignableFrom(Byte.class) || expectedType.isAssignableFrom(Short.class) || expectedType.isAssignableFrom(Character.class) || expectedType.isAssignableFrom(Integer.class) || expectedType.isAssignableFrom(Long.class) || expectedType.isAssignableFrom(BigInteger.class) || expectedType.isAssignableFrom(BigDecimal.class) || expectedType.isAssignableFrom(Float.class) || expectedType.isAssignableFrom(Double.class) || expectedType.isAssignableFrom(Date.class) || expectedType.isAssignableFrom(Time.class) || expectedType.isAssignableFrom(Timestamp.class)) {
            return DataTypeManager.transformValue(new String(value, UTF_8), expectedType);
        } else {
            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(value));
            Object obj = ois.readObject();
            ois.close();
            return obj;
        }
    } catch (ClassNotFoundException e) {
        throw new TeiidRuntimeException(e);
    } catch (IOException e) {
        throw new TeiidRuntimeException(e);
    } catch (TransformationException e) {
        throw new TeiidRuntimeException(e);
    }
}
Also used : TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) InputStreamFactory(org.teiid.core.types.InputStreamFactory) Timestamp(java.sql.Timestamp) GeometryType(org.teiid.core.types.GeometryType) ClobImpl(org.teiid.core.types.ClobImpl) BlobImpl(org.teiid.core.types.BlobImpl) Blob(java.sql.Blob) SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) TransformationException(org.teiid.core.types.TransformationException) BinaryType(org.teiid.core.types.BinaryType) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Date(java.sql.Date) BigInteger(java.math.BigInteger) BlobType(org.teiid.core.types.BlobType) ByteArrayInputStream(java.io.ByteArrayInputStream) BigInteger(java.math.BigInteger) ObjectInputStream(java.io.ObjectInputStream)

Example 4 with TeiidRuntimeException

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

the class LocalClient method getVDBInternal.

private VDBMetaData getVDBInternal() throws SQLException, TeiidProcessingException {
    if (this.vdb == null) {
        LocalServerConnection lsc = (LocalServerConnection) getConnection().getServerConnection();
        vdb = lsc.getWorkContext().getVDB();
        if (vdb == null) {
            throw new TeiidRuntimeException(ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16001, this.vdbName, this.vdbVersion));
        }
        this.vdb = vdb;
    }
    if (vdb.getStatus() != Status.ACTIVE) {
        throw new TeiidProcessingException(QueryPlugin.Event.TEIID31099, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31099, vdb, vdb.getStatus()));
    }
    return this.vdb;
}
Also used : TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) LocalServerConnection(org.teiid.transport.LocalServerConnection) TeiidProcessingException(org.teiid.core.TeiidProcessingException)

Example 5 with TeiidRuntimeException

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

the class ODataExpressionToSQLVisitor method visit.

@Override
public void visit(Literal expr) {
    try {
        Object value = null;
        if (expr.getText() != null && !expr.getText().equalsIgnoreCase("null")) {
            String type = expr.getType().getFullQualifiedName().getFullQualifiedNameAsString();
            value = ODataTypeManager.parseLiteral(type, expr.getText());
        }
        if (this.prepared) {
            if (value == null) {
                this.stack.add(new Constant(value));
            } else {
                Function ref = new Function(CONVERT, new org.teiid.query.sql.symbol.Expression[] { new Reference(this.params.size()), new Constant(DataTypeManager.getDataTypeName(value.getClass())) });
                stack.add(ref);
                this.params.add(new SQLParameter(value, JDBCSQLTypeInfo.getSQLTypeFromClass(value.getClass().getName())));
            }
        } else {
            this.stack.add(new Constant(value));
        }
    } catch (TeiidException e) {
        throw new TeiidRuntimeException(e);
    }
}
Also used : Function(org.teiid.query.sql.symbol.Function) Constant(org.teiid.query.sql.symbol.Constant) Reference(org.teiid.query.sql.symbol.Reference) ScalarSubquery(org.teiid.query.sql.symbol.ScalarSubquery) SQLParameter(org.teiid.odata.api.SQLParameter) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) TeiidException(org.teiid.core.TeiidException)

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