Search in sources :

Example 61 with Table

use of org.teiid.metadata.Table in project teiid by teiid.

the class InfinispanQueryExecution method useModifiedGroups.

static void useModifiedGroups(InfinispanConnection connection, ExecutionContext context, RuntimeMetadata metadata, Command command) throws TranslatorException {
    BasicCache<String, String> aliasCache = InfinispanDirectQueryExecution.getAliasCache(connection);
    CollectorVisitor.collectGroups(command).forEach(namedTable -> {
        try {
            Table table = InfinispanDirectQueryExecution.getAliasTable(context, metadata, aliasCache, namedTable.getMetadataObject());
            Collection<ColumnReference> columns = CollectorVisitor.collectElements(command);
            columns.forEach(reference -> {
                if (reference.getTable().getMetadataObject().equals(namedTable.getMetadataObject())) {
                    Column column = table.getColumnByName(reference.getMetadataObject().getName());
                    reference.getTable().setMetadataObject(table);
                    reference.setMetadataObject(column);
                }
            });
            namedTable.setMetadataObject(table);
        } catch (TranslatorException e) {
            LogManager.logError(LogConstants.CTX_CONNECTOR, e, e.getMessage());
        }
    });
}
Also used : Table(org.teiid.metadata.Table) Column(org.teiid.metadata.Column) TranslatorException(org.teiid.translator.TranslatorException) ColumnReference(org.teiid.language.ColumnReference)

Example 62 with Table

use of org.teiid.metadata.Table in project teiid by teiid.

the class InfinispanUpdateExecution method execute.

@Override
public void execute() throws TranslatorException {
    if (useAliasCache) {
        if (useAliasCache) {
            InfinispanQueryExecution.useModifiedGroups(this.connection, this.executionContext, this.metadata, this.command);
        }
    }
    final InfinispanUpdateVisitor visitor = new InfinispanUpdateVisitor(this.metadata);
    visitor.append(this.command);
    if (!visitor.exceptions.isEmpty()) {
        throw visitor.exceptions.get(0);
    }
    TeiidTableMarsheller marshaller = null;
    try {
        Table table = visitor.getParentTable();
        Column pkColumn = visitor.getPrimaryKey();
        if (pkColumn == null) {
            throw new TranslatorException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25013, table.getName()));
        }
        final String PK = MarshallerBuilder.getDocumentAttributeName(pkColumn, false, this.metadata);
        DocumentFilter docFilter = null;
        if (visitor.isNestedOperation() && visitor.getWhereClause() != null) {
            Action action = Action.ALWAYSADD;
            if (command instanceof Delete) {
                action = Action.REMOVE;
            }
            SQLStringVisitor ssv = new SQLStringVisitor() {

                @Override
                public void visit(ColumnReference obj) {
                    String groupName = null;
                    NamedTable group = obj.getTable();
                    if (group.getCorrelationName() != null) {
                        groupName = group.getCorrelationName();
                    } else {
                        Table groupID = group.getMetadataObject();
                        if (groupID.getFullName().equals(visitor.getParentTable().getFullName())) {
                            groupName = visitor.getParentNamedTable().getCorrelationName();
                        } else {
                            groupName = visitor.getQueryNamedTable().getCorrelationName();
                        }
                    }
                    buffer.append(groupName).append(Tokens.DOT).append(getName(obj.getMetadataObject()));
                }

                @Override
                public String getName(AbstractMetadataRecord object) {
                    return object.getName();
                }
            };
            ssv.append(visitor.getWhereClause());
            docFilter = new ComplexDocumentFilter(visitor.getParentNamedTable(), visitor.getQueryNamedTable(), this.metadata, ssv.toString(), action);
        }
        marshaller = MarshallerBuilder.getMarshaller(table, this.metadata, docFilter);
        this.connection.registerMarshaller(marshaller);
        // if the message in defined in different cache than the default, switch it out now.
        final RemoteCache<Object, Object> cache = InfinispanQueryExecution.getCache(table, connection);
        if (visitor.getOperationType() == OperationType.DELETE) {
            paginateDeleteResults(cache, visitor.getDeleteQuery(), new Task() {

                @Override
                public void run(Object row) throws TranslatorException {
                    if (visitor.isNestedOperation()) {
                        String childName = ProtobufMetadataProcessor.getMessageName(visitor.getQueryTable());
                        InfinispanDocument document = (InfinispanDocument) row;
                        cache.replace(document.getProperties().get(PK), document);
                        // false below means count that not matched, i.e. deleted count
                        updateCount = updateCount + document.getUpdateCount(childName, false);
                    } else {
                        Object key = ((Object[]) row)[0];
                        cache.remove(key);
                        updateCount++;
                    }
                }
            }, this.executionContext.getBatchSize());
        } else if (visitor.getOperationType() == OperationType.UPDATE) {
            paginateUpdateResults(cache, visitor.getUpdateQuery(), new Task() {

                @Override
                public void run(Object row) throws TranslatorException {
                    InfinispanDocument previous = (InfinispanDocument) row;
                    Object key = previous.getProperties().get(PK);
                    int count = previous.merge(visitor.getInsertPayload());
                    cache.replace(key, previous);
                    updateCount = updateCount + count;
                }
            }, this.executionContext.getBatchSize());
        } else if (visitor.getOperationType() == OperationType.INSERT) {
            performInsert(visitor, table, cache, false, marshaller);
        } else if (visitor.getOperationType() == OperationType.UPSERT) {
            performInsert(visitor, table, cache, true, marshaller);
        }
    } finally {
        if (marshaller != null) {
            this.connection.unRegisterMarshaller(marshaller);
        }
    }
}
Also used : Delete(org.teiid.language.Delete) NamedTable(org.teiid.language.NamedTable) Action(org.teiid.infinispan.api.DocumentFilter.Action) NamedTable(org.teiid.language.NamedTable) Table(org.teiid.metadata.Table) SQLStringVisitor(org.teiid.language.visitor.SQLStringVisitor) TeiidTableMarsheller(org.teiid.infinispan.api.TeiidTableMarsheller) InfinispanDocument(org.teiid.infinispan.api.InfinispanDocument) DocumentFilter(org.teiid.infinispan.api.DocumentFilter) AbstractMetadataRecord(org.teiid.metadata.AbstractMetadataRecord) Column(org.teiid.metadata.Column) TranslatorException(org.teiid.translator.TranslatorException) ColumnReference(org.teiid.language.ColumnReference)

Example 63 with Table

use of org.teiid.metadata.Table in project teiid by teiid.

the class TestHiveExecutionFactory method testQuoting.

@Test
public void testQuoting() throws Exception {
    HiveMetadataProcessor hmp = new HiveMetadataProcessor();
    Connection c = Mockito.mock(Connection.class);
    MetadataFactory mf = Mockito.mock(MetadataFactory.class);
    Table table = new Table();
    Mockito.stub(mf.addTable("x")).toReturn(table);
    Column col = new Column();
    col.setName("y");
    Mockito.stub(mf.addColumn("y", "string", table)).toReturn(col);
    Statement stmt = Mockito.mock(Statement.class);
    Mockito.stub(c.createStatement()).toReturn(stmt);
    ResultSet rs = Mockito.mock(ResultSet.class);
    Mockito.stub(stmt.executeQuery("SHOW TABLES")).toReturn(rs);
    Mockito.stub(rs.next()).toReturn(true).toReturn(false);
    Mockito.stub(rs.getString(1)).toReturn("x");
    ResultSet rs1 = Mockito.mock(ResultSet.class);
    Mockito.stub(stmt.executeQuery("DESCRIBE x")).toReturn(rs1);
    Mockito.stub(rs1.next()).toReturn(true).toReturn(false);
    Mockito.stub(rs1.getString(1)).toReturn("y");
    Mockito.stub(rs1.getString(2)).toReturn("string");
    hmp.process(mf, c);
    assertEquals("`x`", table.getNameInSource());
    assertEquals("`y`", col.getNameInSource());
}
Also used : Table(org.teiid.metadata.Table) RealMetadataFactory(org.teiid.query.unittest.RealMetadataFactory) MetadataFactory(org.teiid.metadata.MetadataFactory) Column(org.teiid.metadata.Column) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 64 with Table

use of org.teiid.metadata.Table in project teiid by teiid.

the class MySQLExecutionFactory method getMetadataProcessor.

@Override
public MetadataProcessor<Connection> getMetadataProcessor() {
    return new JDBCMetadataProcessor() {

        @Override
        protected String getRuntimeType(int type, String typeName, int precision) {
            // mysql will otherwise report a 0/null type for geometry
            if ("geometry".equalsIgnoreCase(typeName)) {
                // $NON-NLS-1$
                return TypeFacility.RUNTIME_NAMES.GEOMETRY;
            }
            return super.getRuntimeType(type, typeName, precision);
        }

        @Override
        protected Column addColumn(ResultSet columns, Table table, MetadataFactory metadataFactory, int rsColumns) throws SQLException {
            Column c = super.addColumn(columns, table, metadataFactory, rsColumns);
            if (c.getPrecision() == 0 && "bit".equalsIgnoreCase(c.getNativeType())) {
                // $NON-NLS-1$
                c.setNativeType(TINYINT);
            }
            return c;
        }

        @Override
        protected void getTableStatistics(Connection conn, String catalog, String schema, String name, Table table) throws SQLException {
            PreparedStatement stmt = null;
            ResultSet rs = null;
            try {
                // $NON-NLS-1$
                stmt = conn.prepareStatement("SELECT cardinality FROM INFORMATION_SCHEMA.STATISTICS WHERE table_schema = ? AND table_name = ?");
                if (catalog != null && schema == null) {
                    // mysql jdbc reports the schema as the catalog
                    stmt.setString(1, catalog);
                } else {
                    stmt.setString(1, schema);
                }
                stmt.setString(2, name);
                rs = stmt.executeQuery();
                if (rs.next()) {
                    int cardinality = rs.getInt(1);
                    if (!rs.wasNull()) {
                        table.setCardinality(cardinality);
                    }
                }
            } finally {
                if (rs != null) {
                    rs.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            }
        }

        @Override
        protected boolean isUnsignedTypeName(String name) {
            if (!name.contains("UNSIGNED")) {
                // $NON-NLS-1$
                return false;
            }
            return super.isUnsignedTypeName(name);
        }
    };
}
Also used : Table(org.teiid.metadata.Table) MetadataFactory(org.teiid.metadata.MetadataFactory) JDBCMetadataProcessor(org.teiid.translator.jdbc.JDBCMetadataProcessor) Column(org.teiid.metadata.Column) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 65 with Table

use of org.teiid.metadata.Table in project teiid by teiid.

the class HiveMetadataProcessor method addTable.

private void addTable(String tableName, Connection conn, MetadataFactory metadataFactory) throws SQLException {
    Table table = addTable(metadataFactory, null, null, tableName, null, tableName);
    if (table == null) {
        return;
    }
    Statement stmt = conn.createStatement();
    // $NON-NLS-1$
    ResultSet rs = stmt.executeQuery("DESCRIBE " + tableName);
    while (rs.next()) {
        String name = rs.getString(1);
        if (this.trimColumnNames) {
            name = name.trim();
        }
        String type = rs.getString(2);
        if (type != null) {
            type = type.trim();
        }
        String runtimeType = getRuntimeType(type);
        Column column = metadataFactory.addColumn(name, runtimeType, table);
        column.setNameInSource(quoteName(name));
        column.setUpdatable(true);
    }
    rs.close();
}
Also used : Table(org.teiid.metadata.Table) Column(org.teiid.metadata.Column) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet)

Aggregations

Table (org.teiid.metadata.Table)239 Test (org.junit.Test)82 Column (org.teiid.metadata.Column)72 MetadataFactory (org.teiid.metadata.MetadataFactory)59 Properties (java.util.Properties)45 MetadataStore (org.teiid.metadata.MetadataStore)37 Schema (org.teiid.metadata.Schema)35 TranslatorException (org.teiid.translator.TranslatorException)30 ArrayList (java.util.ArrayList)27 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)27 RealMetadataFactory (org.teiid.query.unittest.RealMetadataFactory)23 List (java.util.List)22 ForeignKey (org.teiid.metadata.ForeignKey)22 Connection (java.sql.Connection)15 QueryNode (org.teiid.query.mapping.relational.QueryNode)15 BasicSourceCapabilities (org.teiid.query.optimizer.capabilities.BasicSourceCapabilities)15 KeyRecord (org.teiid.metadata.KeyRecord)14 Dimension (org.teiid.translator.couchbase.CouchbaseMetadataProcessor.Dimension)14 CouchbaseMetadataProcessor (org.teiid.translator.couchbase.CouchbaseMetadataProcessor)13 CouchbaseProperties (org.teiid.translator.couchbase.CouchbaseProperties)13