Search in sources :

Example 91 with Table

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

the class TestTriggerActions method testUpdateWithChanging.

@Test
public void testUpdateWithChanging() throws Exception {
    TransformationMetadata metadata = TestUpdateValidator.example1();
    TestUpdateValidator.createView("select 1 as x, 2 as y", metadata, GX);
    Table t = metadata.getMetadataStore().getSchemas().get(VM1).getTables().get(GX);
    t.setDeletePlan("");
    t.setUpdatePlan("FOR EACH ROW BEGIN update pm1.g1 set e2 = case when changing.y then new.y end where e2 = old.y; END");
    t.setInsertPlan("");
    String sql = "update gx set y = 5";
    FakeDataManager dm = new FakeDataManager();
    FakeDataStore.addTable("pm1.g1", dm, metadata);
    CommandContext context = createCommandContext();
    BasicSourceCapabilities caps = TestOptimizer.getTypicalCapabilities();
    ProcessorPlan plan = TestProcessor.helpGetPlan(TestResolver.helpResolve(sql, metadata), metadata, new DefaultCapabilitiesFinder(caps), context);
    List<?>[] expected = new List[] { Arrays.asList(1) };
    helpProcess(plan, context, dm, expected);
    assertEquals("UPDATE pm1.g1 SET e2 = 5 WHERE e2 = 2", dm.getQueries().get(0));
}
Also used : TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) Table(org.teiid.metadata.Table) CommandContext(org.teiid.query.util.CommandContext) BasicSourceCapabilities(org.teiid.query.optimizer.capabilities.BasicSourceCapabilities) List(java.util.List) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) Test(org.junit.Test)

Example 92 with Table

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

the class MongoDBMetadataProcessor method addColumn.

private Column addColumn(MetadataFactory metadataFactory, Table table, String columnKey, Object value) {
    Column column = null;
    if (columnKey.equals(ID)) {
        if (value instanceof BasicDBObject) {
            BasicDBObject compositeKey = (BasicDBObject) value;
            for (String key : compositeKey.keySet()) {
                column = addColumn(metadataFactory, table, key, compositeKey.get(key));
                column.setUpdatable(true);
            }
        }
    }
    if (!columnKey.equals(ID) && value instanceof BasicDBObject) {
        // embedded doc - one to one
        Table childTable = addTable(metadataFactory, columnKey, (BasicDBObject) value, table);
        if (childTable != null) {
            childTable.setProperty(MERGE, table.getName());
            childTable.setProperty(ASSOSIATION, MergeDetails.Association.ONE.name());
        }
    } else if (value instanceof BasicDBList) {
        // embedded doc, list one to many
        if (((BasicDBList) value).get(0) instanceof BasicDBObject) {
            Table childTable = addTable(metadataFactory, columnKey, (BasicDBObject) ((BasicDBList) value).get(0), table);
            if (childTable != null) {
                childTable.setProperty(MERGE, table.getName());
                childTable.setProperty(ASSOSIATION, MergeDetails.Association.MANY.name());
            }
        } else {
            column = table.getColumnByName(columnKey);
            if (column == null) {
                // $NON-NLS-1$
                column = metadataFactory.addColumn(columnKey, TypeFacility.RUNTIME_NAMES.OBJECT + "[]", table);
            } else if (!column.getRuntimeType().equals(TypeFacility.RUNTIME_NAMES.OBJECT + "[]")) {
                // $NON-NLS-1$
                // type conflict
                MetadataFactory.setDataType(TypeFacility.RUNTIME_NAMES.OBJECT, column, metadataFactory.getDataTypes(), false);
                column.setNativeType(null);
            }
            column.setSearchType(SearchType.Unsearchable);
        }
    } else if (value instanceof DBRef) {
        Object obj = ((DBRef) value).getId();
        column = addColumn(metadataFactory, table, columnKey, obj);
        String ref = ((DBRef) value).getCollectionName();
        // $NON-NLS-1$
        metadataFactory.addForeignKey("FK_" + columnKey, Arrays.asList(columnKey), ref, table);
    } else {
        column = table.getColumnByName(columnKey);
        String dataType = getDataType(value);
        if (column == null) {
            column = metadataFactory.addColumn(columnKey, dataType, table);
            setNativeType(column, value);
        } else if (!column.getRuntimeType().equals(getDataType(value))) {
            // type conflict
            if (STRING_COMPATIBLE_TYPES.contains(column.getRuntimeType()) && STRING_COMPATIBLE_TYPES.contains(dataType)) {
                MetadataFactory.setDataType(TypeFacility.RUNTIME_NAMES.STRING, column, metadataFactory.getDataTypes(), false);
            } else {
                MetadataFactory.setDataType(TypeFacility.RUNTIME_NAMES.OBJECT, column, metadataFactory.getDataTypes(), false);
            }
            column.setNativeType(null);
            column.setSearchType(SearchType.Unsearchable);
        }
    }
    // create a PK out of _id
    if (columnKey.equals(ID)) {
        if (value instanceof BasicDBObject) {
            BasicDBObject compositeKey = (BasicDBObject) value;
            ArrayList<String> columns = new ArrayList<String>();
            for (String key : compositeKey.keySet()) {
                columns.add(key);
            }
            // $NON-NLS-1$
            metadataFactory.addPrimaryKey("PK0", columns, table);
        } else {
            // $NON-NLS-1$
            metadataFactory.addPrimaryKey("PK0", Arrays.asList(ID), table);
        }
    }
    return column;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicDBList(com.mongodb.BasicDBList) Table(org.teiid.metadata.Table) Column(org.teiid.metadata.Column) DBRef(com.mongodb.DBRef) ArrayList(java.util.ArrayList) BasicDBObject(com.mongodb.BasicDBObject)

Example 93 with Table

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

the class MongoDBMetadataProcessor method addTable.

private Table addTable(MetadataFactory metadataFactory, String tableName, BasicDBObject row, Table parent) {
    Table table = null;
    if (metadataFactory.getSchema().getTable(tableName) != null) {
        table = metadataFactory.getSchema().getTable(tableName);
    }
    Set<String> keys = row.keySet();
    if (keys != null && !keys.isEmpty()) {
        if (table == null) {
            table = metadataFactory.addTable(tableName);
            table.setSupportsUpdate(true);
            if (parent != null) {
                // $NON-NLS-1$
                FullyQualifiedName rn = new FullyQualifiedName("embedded", tableName);
                String parentfqn = parent.getProperty(FQN, false);
                table.setProperty(FQN, parentfqn + FullyQualifiedName.SEPARATOR + rn.toString());
            } else {
                // $NON-NLS-1$
                FullyQualifiedName fqn = new FullyQualifiedName("collection", tableName);
                table.setProperty(FQN, fqn.toString());
            }
        }
        for (String columnKey : keys) {
            Object value = row.get(columnKey);
            Column column = addColumn(metadataFactory, table, columnKey, value);
            if (column != null) {
                column.setUpdatable(true);
            }
        }
        return table;
    }
    return null;
}
Also used : Table(org.teiid.metadata.Table) Column(org.teiid.metadata.Column) FullyQualifiedName(org.teiid.util.FullyQualifiedName) BasicDBObject(com.mongodb.BasicDBObject)

Example 94 with Table

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

the class MongoDBUpdateExecution method removeParentKey.

private void removeParentKey(MongoDocument document, BasicDBObject row) throws TranslatorException {
    Table source = document.getTable();
    Table target = document.getMergeTable();
    for (ForeignKey fk : source.getForeignKeys()) {
        if (fk.getReferenceTableName().equals(target.getName())) {
            for (int i = 0; i < fk.getColumns().size(); i++) {
                if (row != null) {
                    row.remove(fk.getColumns().get(i).getName());
                }
            }
        }
    }
}
Also used : Table(org.teiid.metadata.Table) ForeignKey(org.teiid.metadata.ForeignKey)

Example 95 with Table

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

the class IQueryToLdapSearchParser method getSearchScopeFromFromItem.

private int getSearchScopeFromFromItem(NamedTable fromItem) {
    // $NON-NLS-1$
    String searchScopeString = "";
    int searchScope = LDAPConnectorConstants.ldapDefaultSearchScope;
    // TODO: Re-use the getExpressionString method if possible, rather than
    // rewriting the same code twice.
    Table group = fromItem.getMetadataObject();
    String nameInSource = group.getNameInSource();
    // string instead so we can safely call split on it
    if (nameInSource == null) {
        // $NON-NLS-1$
        nameInSource = "";
    }
    // now split it on ? to find the part of it that specifies search scope
    // $NON-NLS-1$
    String[] nameInSourceArray = nameInSource.split("\\?");
    if (nameInSourceArray.length >= 2) {
        searchScopeString = nameInSourceArray[1];
    }
    // try the default in the connector properties
    if (searchScopeString.equals("")) {
        // $NON-NLS-1$
        SearchDefaultScope searchDefaultScope = this.executionFactory.getSearchDefaultScope();
        if (searchDefaultScope != null) {
            searchScopeString = searchDefaultScope.name();
        }
        // protect against getting null back from the property
        if (searchScopeString == null) {
            // $NON-NLS-1$
            searchScopeString = "";
        }
    }
    if (searchScopeString.equals("SUBTREE_SCOPE")) {
        // $NON-NLS-1$
        searchScope = SearchControls.SUBTREE_SCOPE;
    } else if (searchScopeString.equals("ONELEVEL_SCOPE")) {
        // $NON-NLS-1$
        searchScope = SearchControls.ONELEVEL_SCOPE;
    } else if (searchScopeString.equals("OBJECT_SCOPE")) {
        // $NON-NLS-1$
        searchScope = SearchControls.OBJECT_SCOPE;
    }
    return searchScope;
}
Also used : Table(org.teiid.metadata.Table) SearchDefaultScope(org.teiid.translator.ldap.LDAPExecutionFactory.SearchDefaultScope)

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