Search in sources :

Example 96 with Table

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

the class PrestoDBMetadataProcessor method addTable.

private void addTable(String tableName, Connection conn, String catalog, String schema, MetadataFactory metadataFactory) throws SQLException {
    Table table = addTable(metadataFactory, null, null, tableName, null, tableName);
    if (table == null) {
        return;
    }
    // $NON-NLS-1$ //$NON-NLS-2$
    String nis = catalog + "." + schema + "." + tableName;
    table.setNameInSource(nis);
    Statement stmt = conn.createStatement();
    // $NON-NLS-1$
    ResultSet rs = stmt.executeQuery("SHOW COLUMNS FROM " + nis);
    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);
        NullType nt = Boolean.valueOf(rs.getString(3)) ? NullType.Nullable : NullType.No_Nulls;
        Column column = metadataFactory.addColumn(name, runtimeType, table);
        column.setNameInSource(name);
        column.setUpdatable(true);
        column.setNullType(nt);
    }
    rs.close();
}
Also used : Table(org.teiid.metadata.Table) Column(org.teiid.metadata.Column) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) NullType(org.teiid.metadata.BaseColumn.NullType)

Example 97 with Table

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

the class JPAMetadataProcessor method addSingularAttributes.

private void addSingularAttributes(MetadataFactory mf, Metamodel model, ManagedType<?> entity, Table entityTable) throws TranslatorException {
    for (Attribute<?, ?> attr : entity.getAttributes()) {
        if (!attr.isCollection()) {
            boolean simpleType = isSimpleType(attr.getJavaType());
            if (simpleType) {
                Column column = addColumn(mf, attr.getName(), TypeFacility.getDataTypeName(getJavaDataType(attr.getJavaType())), entityTable);
                if (((SingularAttribute) attr).isOptional()) {
                    column.setDefaultValue(null);
                }
            } else {
                boolean classFound = false;
                // this tables columns
                for (EmbeddableType<?> embeddable : model.getEmbeddables()) {
                    if (embeddable.getJavaType().equals(attr.getJavaType())) {
                        addSingularAttributes(mf, model, embeddable, entityTable);
                        classFound = true;
                        break;
                    }
                }
                if (!classFound) {
                    // table, then add that column as FK
                    for (EntityType et : model.getEntities()) {
                        if (et.getJavaType().equals(attr.getJavaType())) {
                            Table attributeTable = addEntity(mf, model, et);
                            KeyRecord pk = attributeTable.getPrimaryKey();
                            if (pk != null) {
                                // TODO: entities must have PK, so this check is not needed.
                                ArrayList<String> keys = new ArrayList<String>();
                                for (Column column : pk.getColumns()) {
                                    addColumn(mf, column.getName(), column.getDatatype().getRuntimeTypeName(), entityTable);
                                    keys.add(column.getName());
                                }
                                if (!foreignKeyExists(keys, entityTable)) {
                                    addForeignKey(mf, attr.getName(), keys, attributeTable.getName(), entityTable);
                                }
                            } else {
                                throw new TranslatorException(JPAPlugin.Util.gs(JPAPlugin.Event.TEIID14001, attributeTable.getName()));
                            }
                            classFound = true;
                            break;
                        }
                    }
                }
                if (!classFound) {
                    throw new TranslatorException(JPAPlugin.Util.gs(JPAPlugin.Event.TEIID14002, attr.getName()));
                }
            }
        }
    }
}
Also used : EntityType(javax.persistence.metamodel.EntityType) KeyRecord(org.teiid.metadata.KeyRecord) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Table(org.teiid.metadata.Table) Column(org.teiid.metadata.Column) ArrayList(java.util.ArrayList) TranslatorException(org.teiid.translator.TranslatorException)

Example 98 with Table

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

the class JPAMetadataProcessor method addEntity.

private Table addEntity(MetadataFactory mf, Metamodel model, EntityType<?> entity) throws TranslatorException {
    Table table = mf.getSchema().getTable(entity.getName());
    if (table == null) {
        table = mf.addTable(entity.getName());
        table.setSupportsUpdate(true);
        table.setProperty(ENTITYCLASS, entity.getJavaType().getCanonicalName());
        addPrimaryKey(mf, model, entity, table);
        addSingularAttributes(mf, model, entity, table);
    }
    return table;
}
Also used : Table(org.teiid.metadata.Table)

Example 99 with Table

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

the class TestCreateDrop method testForeignTemp.

@Test
public void testForeignTemp() {
    Create create = new Create();
    // $NON-NLS-1$
    create.setTable(new GroupSymbol("tempTable"));
    create.setOn("source");
    Table t = new Table();
    t.setName("tempTable");
    t.setUUID("tid:0");
    Column c = new Column();
    c.setName("x");
    c.setUUID("tid:0");
    Datatype string = SystemMetadata.getInstance().getRuntimeTypeMap().get("string");
    c.setDatatype(string, true, 0);
    t.addColumn(c);
    c = new Column();
    c.setName("y");
    c.setUUID("tid:0");
    Datatype decimal = SystemMetadata.getInstance().getRuntimeTypeMap().get("decimal");
    c.setDatatype(decimal, true, 0);
    t.addColumn(c);
    t.setCardinality(10000);
    create.setTableMetadata(t);
    // $NON-NLS-1$ //$NON-NLS-2$
    helpTest("create foreign temporary table tempTable (x string, y decimal) options (cardinality 10000) on source", "CREATE FOREIGN TEMPORARY TABLE tempTable (\n	x string,\n	y bigdecimal\n) OPTIONS (CARDINALITY 10000) ON 'source'", create);
}
Also used : Table(org.teiid.metadata.Table) Column(org.teiid.metadata.Column) Create(org.teiid.query.sql.lang.Create) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) Datatype(org.teiid.metadata.Datatype) Test(org.junit.Test)

Example 100 with Table

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

the class CoherenceUpdateExecution method executeUpdate.

// Private method to actually do an update operation.
private void executeUpdate() throws TranslatorException {
    Update ucommand = (Update) command;
    Table t = metadata.getTable(ucommand.getTable().getMetadataObject().getFullName());
    // if the table has a foreign key, its must be a child (contained) object in the root
    if (t.getForeignKeys() != null && t.getForeignKeys().size() > 0) {
        updateChildObject(t);
        return;
    }
}
Also used : Table(org.teiid.metadata.Table) Update(org.teiid.language.Update)

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