Search in sources :

Example 11 with ArrayImpl

use of org.teiid.core.types.ArrayImpl in project teiid by teiid.

the class TempTableDataManager method handleSystemProcedures.

private TupleSource handleSystemProcedures(final CommandContext context, StoredProcedure proc) throws TeiidComponentException, QueryMetadataException, QueryProcessingException, QueryResolverException, QueryValidatorException, TeiidProcessingException, ExpressionEvaluationException {
    final QueryMetadataInterface metadata = context.getMetadata();
    if (StringUtil.endsWithIgnoreCase(proc.getProcedureCallableName(), REFRESHMATVIEW)) {
        Object groupID = validateMatView(metadata, (String) ((Constant) proc.getParameter(2).getExpression()).getValue());
        TempMetadataID matTableId = context.getGlobalTableStore().getGlobalTempTableMetadataId(groupID);
        final GlobalTableStore globalStore = getGlobalStore(context, matTableId);
        String matViewName = metadata.getFullName(groupID);
        String matTableName = metadata.getFullName(matTableId);
        // $NON-NLS-1$
        LogManager.logDetail(LogConstants.CTX_MATVIEWS, "processing refreshmatview for", matViewName);
        boolean invalidate = Boolean.TRUE.equals(((Constant) proc.getParameter(3).getExpression()).getValue());
        boolean needsLoading = globalStore.getMatTableInfo(matTableName).getAndClearAsynch();
        if (!needsLoading) {
            needsLoading = globalStore.needsLoading(matTableName, globalStore.getAddress(), true, true, invalidate);
            if (needsLoading) {
                needsLoading = globalStore.needsLoading(matTableName, globalStore.getAddress(), false, false, invalidate);
            }
        }
        if (!needsLoading) {
            return CollectionTupleSource.createUpdateCountTupleSource(-1);
        }
        GroupSymbol matTable = new GroupSymbol(matTableName);
        matTable.setMetadataID(matTableId);
        return loadGlobalTable(context, matTable, matTableName, globalStore);
    } else if (StringUtil.endsWithIgnoreCase(proc.getProcedureCallableName(), REFRESHMATVIEWROWS)) {
        final Object groupID = validateMatView(metadata, (String) ((Constant) proc.getParameter(2).getExpression()).getValue());
        TempMetadataID matTableId = context.getGlobalTableStore().getGlobalTempTableMetadataId(groupID);
        final GlobalTableStore globalStore = getGlobalStore(context, matTableId);
        Object pk = metadata.getPrimaryKey(groupID);
        String matViewName = metadata.getFullName(groupID);
        if (pk == null) {
            throw new QueryProcessingException(QueryPlugin.Event.TEIID30230, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30230, matViewName));
        }
        List<?> ids = metadata.getElementIDsInKey(pk);
        Object[][] params = (Object[][]) ((ArrayImpl) ((Constant) proc.getParameter(3).getExpression()).getValue()).getValues();
        return updateMatviewRows(context, metadata, groupID, globalStore, matViewName, ids, params);
    } else if (StringUtil.endsWithIgnoreCase(proc.getProcedureCallableName(), REFRESHMATVIEWROW)) {
        final Object groupID = validateMatView(metadata, (String) ((Constant) proc.getParameter(2).getExpression()).getValue());
        TempMetadataID matTableId = context.getGlobalTableStore().getGlobalTempTableMetadataId(groupID);
        final GlobalTableStore globalStore = getGlobalStore(context, matTableId);
        Object pk = metadata.getPrimaryKey(groupID);
        final String matViewName = metadata.getFullName(groupID);
        if (pk == null) {
            throw new QueryProcessingException(QueryPlugin.Event.TEIID30230, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30230, matViewName));
        }
        List<?> ids = metadata.getElementIDsInKey(pk);
        Constant key = (Constant) proc.getParameter(3).getExpression();
        Object initialValue = key.getValue();
        SPParameter keyOther = proc.getParameter(4);
        Object[] param = null;
        if (keyOther != null) {
            Object[] otherCols = ((ArrayImpl) ((Constant) keyOther.getExpression()).getValue()).getValues();
            if (otherCols != null) {
                param = new Object[1 + otherCols.length];
                param[0] = initialValue;
                for (int i = 0; i < otherCols.length; i++) {
                    param[i + 1] = otherCols[i];
                }
            }
        }
        if (param == null) {
            param = new Object[] { initialValue };
        }
        Object[][] params = new Object[][] { param };
        return updateMatviewRows(context, metadata, groupID, globalStore, matViewName, ids, params);
    }
    return null;
}
Also used : Constant(org.teiid.query.sql.symbol.Constant) ArrayImpl(org.teiid.core.types.ArrayImpl) TempMetadataID(org.teiid.query.metadata.TempMetadataID) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) QueryProcessingException(org.teiid.api.exception.query.QueryProcessingException)

Example 12 with ArrayImpl

use of org.teiid.core.types.ArrayImpl in project teiid by teiid.

the class SQLStringVisitor method outputLiteral.

private void outputLiteral(Class<?> type, boolean multiValued, Object value) throws AssertionError {
    String[] constantParts = null;
    if (multiValued) {
        // $NON-NLS-1$
        constantParts = new String[] { "?" };
    } else if (value == null) {
        if (type.equals(DataTypeManager.DefaultDataClasses.BOOLEAN)) {
            constantParts = new String[] { UNKNOWN };
        } else {
            // $NON-NLS-1$
            constantParts = new String[] { "null" };
        }
    } else {
        if (value.getClass() == ArrayImpl.class) {
            ArrayImpl av = (ArrayImpl) value;
            append(Tokens.LPAREN);
            for (int i = 0; i < av.getValues().length; i++) {
                if (i > 0) {
                    append(Tokens.COMMA);
                    append(SPACE);
                }
                Object value2 = av.getValues()[i];
                outputLiteral(value2 != null ? value2.getClass() : av.getValues().getClass().getComponentType(), multiValued, value2);
            }
            if (av.getValues().length == 1) {
                append(Tokens.COMMA);
            }
            append(Tokens.RPAREN);
            return;
        } else if (type.isArray()) {
            append(Tokens.LPAREN);
            int length = java.lang.reflect.Array.getLength(value);
            for (int i = 0; i < length; i++) {
                if (i > 0) {
                    append(Tokens.COMMA);
                    append(SPACE);
                }
                Object value2 = java.lang.reflect.Array.get(value, i);
                outputLiteral(type.getComponentType(), multiValued, value2);
            }
            if (length == 1) {
                append(Tokens.COMMA);
            }
            append(Tokens.RPAREN);
            return;
        }
        if (Number.class.isAssignableFrom(type)) {
            constantParts = new String[] { value.toString() };
        } else if (type.equals(DataTypeManager.DefaultDataClasses.BOOLEAN)) {
            constantParts = new String[] { value.equals(Boolean.TRUE) ? TRUE : FALSE };
        } else if (type.equals(DataTypeManager.DefaultDataClasses.TIMESTAMP)) {
            // $NON-NLS-1$ //$NON-NLS-2$
            constantParts = new String[] { "{ts'", value.toString(), "'}" };
        } else if (type.equals(DataTypeManager.DefaultDataClasses.TIME)) {
            // $NON-NLS-1$ //$NON-NLS-2$
            constantParts = new String[] { "{t'", value.toString(), "'}" };
        } else if (type.equals(DataTypeManager.DefaultDataClasses.DATE)) {
            // $NON-NLS-1$ //$NON-NLS-2$
            constantParts = new String[] { "{d'", value.toString(), "'}" };
        } else if (type.equals(DataTypeManager.DefaultDataClasses.VARBINARY)) {
            // $NON-NLS-1$ //$NON-NLS-2$
            constantParts = new String[] { "X'", value.toString(), "'" };
        }
        if (constantParts == null) {
            if (DataTypeManager.isLOB(type)) {
                // $NON-NLS-1$
                constantParts = new String[] { "?" };
            } else {
                append('\'');
                String strValue = value.toString();
                for (int i = 0; i < strValue.length(); i++) {
                    char c = strValue.charAt(i);
                    if (c == '\'') {
                        parts.append('\'');
                    } else if (Character.isISOControl(c)) {
                        parts.append(// $NON-NLS-1$
                        "\\u" + PropertiesUtils.toHex((c >> 12) & 0xF) + PropertiesUtils.toHex((c >> 8) & 0xF) + PropertiesUtils.toHex((c >> 4) & 0xF) + PropertiesUtils.toHex(c & 0xF));
                        continue;
                    }
                    parts.append(c);
                }
                parts.append('\'');
                return;
            }
        }
    }
    for (String string : constantParts) {
        append(string);
    }
}
Also used : ArrayImpl(org.teiid.core.types.ArrayImpl) LanguageObject(org.teiid.query.sql.LanguageObject) SpecificHint(org.teiid.query.sql.lang.SourceHint.SpecificHint) SubqueryHint(org.teiid.query.sql.lang.ExistsCriteria.SubqueryHint)

Example 13 with ArrayImpl

use of org.teiid.core.types.ArrayImpl in project teiid by teiid.

the class TestJDBCSocketTransport method testArray.

@Test
public void testArray() throws Exception {
    Statement s = conn.createStatement();
    ResultSet rs = s.executeQuery("SELECT (1, (1,2))");
    rs.next();
    assertEquals(new ArrayImpl(new Object[] { 1, new Object[] { 1, 2 } }), rs.getArray(1));
    assertEquals("java.sql.Array", rs.getMetaData().getColumnClassName(1));
    assertEquals(Types.ARRAY, rs.getMetaData().getColumnType(1));
    assertEquals("object[]", rs.getMetaData().getColumnTypeName(1));
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ArrayImpl(org.teiid.core.types.ArrayImpl) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 14 with ArrayImpl

use of org.teiid.core.types.ArrayImpl in project teiid by teiid.

the class LDAPQueryExecution method getArray.

private ArrayImpl getArray(Class<?> componentType, Attribute resultAttr, Column modelElement, String modelAttrName) throws NamingException, TranslatorException {
    ArrayList<Object> multivalList = new ArrayList<Object>();
    NamingEnumeration<?> attrNE = resultAttr.getAll();
    int length = 0;
    while (attrNE.hasMore()) {
        try {
            multivalList.add(convertSingleValue(modelElement, modelAttrName, componentType, attrNE.next()));
            length++;
        } catch (InvalidNameException e) {
        // just ignore
        }
    }
    Object[] values = (Object[]) Array.newInstance(componentType, length);
    ArrayImpl value = new ArrayImpl(multivalList.toArray(values));
    return value;
}
Also used : InvalidNameException(javax.naming.InvalidNameException) ArrayImpl(org.teiid.core.types.ArrayImpl) ArrayList(java.util.ArrayList)

Example 15 with ArrayImpl

use of org.teiid.core.types.ArrayImpl in project teiid by teiid.

the class LanguageBridgeFactory method translate.

org.teiid.language.Expression translate(Constant constant) {
    if (constant.isMultiValued()) {
        Parameter result = new Parameter();
        result.setType(constant.getType());
        final List<?> values = (List<?>) constant.getValue();
        allValues.add(values);
        result.setValueIndex(valueIndex++);
        return result;
    }
    if (constant.getValue() instanceof ArrayImpl) {
        // TODO: we could check if there is a common base type (also needs to be in the dependent logic)
        // and expand binding options in the translators
        // we currently support the notion of a mixed type array, since we consider object a common base type
        // that will not work for all sources, so instead of treating this as a single array (as commented out below),
        // we just turn it into an array of parameters
        // Literal result = new Literal(av.getValues(), org.teiid.language.Array.class);
        // result.setBindEligible(constant.isBindEligible());
        // return result;
        ArrayImpl av = (ArrayImpl) constant.getValue();
        List<Constant> vals = new ArrayList<Constant>();
        Class<?> baseType = null;
        for (Object o : av.getValues()) {
            Constant c = new Constant(o);
            c.setBindEligible(constant.isBindEligible());
            vals.add(c);
            if (baseType == null) {
                baseType = c.getType();
            } else if (!baseType.equals(c.getType())) {
                baseType = DataTypeManager.DefaultDataClasses.OBJECT;
            }
        }
        return new org.teiid.language.Array(baseType, translateExpressionList(vals));
    }
    Literal result = new Literal(constant.getValue(), constant.getType());
    result.setBindEligible(constant.isBindEligible());
    return result;
}
Also used : Array(org.teiid.query.sql.symbol.Array) Constant(org.teiid.query.sql.symbol.Constant) ArrayImpl(org.teiid.core.types.ArrayImpl) ProcedureParameter(org.teiid.metadata.ProcedureParameter)

Aggregations

ArrayImpl (org.teiid.core.types.ArrayImpl)31 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)7 List (java.util.List)7 SQLException (java.sql.SQLException)3 Array (org.teiid.query.sql.symbol.Array)3 Constant (org.teiid.query.sql.symbol.Constant)3 IOException (java.io.IOException)2 ObjectOutputStream (java.io.ObjectOutputStream)2 Array (java.sql.Array)2 TeiidProcessingException (org.teiid.core.TeiidProcessingException)2 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)2 TransformationException (org.teiid.core.types.TransformationException)2 XMLType (org.teiid.core.types.XMLType)2 ProcedureParameter (org.teiid.metadata.ProcedureParameter)2 LanguageObject (org.teiid.query.sql.LanguageObject)2 Command (org.teiid.query.sql.lang.Command)2 Expression (org.teiid.query.sql.symbol.Expression)2 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1