Search in sources :

Example 6 with KeyRecord

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

the class SAPMetadataProcessor method addEntitySetAsTable.

@Override
protected Table addEntitySetAsTable(MetadataFactory mf, EdmEntitySet entitySet) throws TranslatorException {
    Table table = super.addEntitySetAsTable(mf, entitySet);
    KeyRecord accessPattern = this.accessPatterns.get(table);
    if (accessPattern != null) {
        table.getAccessPatterns().add(accessPattern);
    }
    return table;
}
Also used : KeyRecord(org.teiid.metadata.KeyRecord) Table(org.teiid.metadata.Table)

Example 7 with KeyRecord

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

the class IckleConversionVisitor method visit.

@Override
public void visit(NamedTable obj) {
    this.queriedTable = obj;
    if (obj.getCorrelationName() == null) {
        obj.setCorrelationName(obj.getMetadataObject().getName().toLowerCase() + "_" + aliasCounter.getAndIncrement());
    }
    if (this.rootNode == null) {
        String messageName = null;
        String aliasName = null;
        String mergedTableName = ProtobufMetadataProcessor.getMerge(obj.getMetadataObject());
        if (mergedTableName == null) {
            aliasName = obj.getCorrelationName();
            messageName = getMessageName(obj.getMetadataObject());
            this.parentTable = obj;
            this.rootNode = new DocumentNode(obj.getMetadataObject(), true);
            this.joinedNode = this.rootNode;
            // check to see if there is one-2-one rows
            Set<String> tags = new HashSet<>();
            for (Column column : obj.getMetadataObject().getColumns()) {
                if (ProtobufMetadataProcessor.getParentTag(column) != -1) {
                    String childMessageName = ProtobufMetadataProcessor.getMessageName(column);
                    if (!tags.contains(childMessageName)) {
                        tags.add(childMessageName);
                        // TODO: DocumentNode needs to be refactored to just take name, not table
                        Table t = new Table();
                        t.setName(childMessageName);
                        this.joinedNode = this.rootNode.joinWith(JoinType.INNER_JOIN, new DocumentNode(t, false));
                    }
                }
            }
        } else {
            try {
                Table mergedTable = this.metadata.getTable(mergedTableName);
                messageName = getMessageName(mergedTable);
                aliasName = mergedTable.getName().toLowerCase() + "_" + aliasCounter.getAndIncrement();
                this.parentTable = new NamedTable(mergedTable.getName(), aliasName, mergedTable);
                this.rootNode = new DocumentNode(mergedTable, true);
                this.joinedNode = this.rootNode.joinWith(JoinType.INNER_JOIN, new DocumentNode(obj.getMetadataObject(), true));
                this.nested = true;
            } catch (TranslatorException e) {
                this.exceptions.add(e);
            }
        }
        buffer.append(messageName);
        if (aliasName != null) {
            buffer.append(Tokens.SPACE);
            buffer.append(aliasName);
        }
        if (this.includePK) {
            KeyRecord pk = this.parentTable.getMetadataObject().getPrimaryKey();
            if (pk != null) {
                for (Column column : pk.getColumns()) {
                    projectedExpressions.add(new ColumnReference(obj, column.getName(), column, column.getJavaType()));
                }
            }
        }
    }
}
Also used : KeyRecord(org.teiid.metadata.KeyRecord) Table(org.teiid.metadata.Table) DocumentNode(org.teiid.translator.document.DocumentNode) Column(org.teiid.metadata.Column) TranslatorException(org.teiid.translator.TranslatorException) HashSet(java.util.HashSet)

Example 8 with KeyRecord

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

the class JDBCUpdateExecution method executeTranslatedCommand.

/**
 * @param translatedComm
 * @throws TranslatorException
 * @since 4.3
 */
private void executeTranslatedCommand(TranslatedCommand translatedComm) throws TranslatorException {
    // create statement or PreparedStatement and execute
    String sql = translatedComm.getSql();
    boolean commitType = false;
    boolean succeeded = false;
    try {
        int updateCount = 0;
        Class<?>[] keyColumnDataTypes = null;
        String[] keyColumnNames = null;
        if (command instanceof Insert && context.getCommandContext().isReturnAutoGeneratedKeys() && executionFactory.supportsGeneratedKeys(context, command)) {
            Insert insert = (Insert) command;
            NamedTable nt = insert.getTable();
            if (nt.getMetadataObject() != null) {
                KeyRecord key = nt.getMetadataObject().getPrimaryKey();
                if (key != null) {
                    List<Column> cols = key.getColumns();
                    keyColumnDataTypes = new Class<?>[cols.size()];
                    keyColumnNames = new String[cols.size()];
                    for (int i = 0; i < cols.size(); i++) {
                        Column c = cols.get(i);
                        keyColumnDataTypes[i] = c.getJavaType();
                        // won't work in scenarios where the teiid name is changed or contains a .
                        keyColumnNames[i] = c.getName();
                    }
                }
            }
        }
        if (!translatedComm.isPrepared()) {
            statement = getStatement();
            // handle autoGeneratedKeys
            if (keyColumnDataTypes != null) {
                if (executionFactory.useColumnNamesForGeneratedKeys()) {
                    updateCount = statement.executeUpdate(sql, keyColumnNames);
                } else {
                    updateCount = statement.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
                }
            } else {
                updateCount = statement.executeUpdate(sql);
            }
            result = new int[] { updateCount };
            addStatementWarnings();
        } else {
            PreparedStatement pstatement = null;
            if (statement != null) {
                statement.close();
            }
            if (keyColumnDataTypes != null) {
                if (executionFactory.useColumnNamesForGeneratedKeys()) {
                    pstatement = connection.prepareStatement(sql, keyColumnNames);
                } else {
                    pstatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                }
            } else {
                pstatement = getPreparedStatement(sql);
            }
            statement = pstatement;
            Iterator<? extends List<?>> vi = null;
            if (command instanceof BulkCommand) {
                BulkCommand batchCommand = (BulkCommand) command;
                vi = batchCommand.getParameterValues();
            }
            int k = 0;
            int batchStart = 0;
            if (vi != null) {
                try {
                    commitType = getAutoCommit(translatedComm);
                    if (commitType) {
                        connection.setAutoCommit(false);
                    }
                    int maxBatchSize = (command instanceof Insert) ? maxPreparedInsertBatchSize : Integer.MAX_VALUE;
                    boolean done = false;
                    outer: while (!done) {
                        for (int i = 0; i < maxBatchSize; i++) {
                            if (vi.hasNext()) {
                                List<?> values = vi.next();
                                bind(pstatement, translatedComm.getPreparedValues(), values);
                                k++;
                            } else {
                                if (i == 0) {
                                    break outer;
                                }
                                done = true;
                                break;
                            }
                        }
                        int[] results = pstatement.executeBatch();
                        batchStart = k;
                        if (result == null) {
                            result = results;
                        } else {
                            int len = result.length;
                            result = Arrays.copyOf(result, len + results.length);
                            System.arraycopy(results, 0, result, len, results.length);
                        }
                    }
                } catch (SQLException e) {
                    int size = k + 1;
                    if (result == null) {
                        result = new int[size];
                    } else {
                        result = Arrays.copyOf(result, size);
                    }
                    // if there is a BatchUpdateException, there are more update counts to accumulate
                    if (e instanceof BatchUpdateException) {
                        BatchUpdateException bue = (BatchUpdateException) e;
                        int[] batchResults = bue.getUpdateCounts();
                        for (int j = 0; j < batchResults.length; j++) {
                            result[batchStart + j] = batchResults[j];
                        }
                        size = batchStart + batchResults.length;
                    } else {
                        size = batchStart;
                    }
                    // resize the result and throw exception
                    throw new TranslatorBatchException(e, Arrays.copyOf(result, size));
                }
            } else {
                bind(pstatement, translatedComm.getPreparedValues(), null);
                updateCount = pstatement.executeUpdate();
                result = new int[] { updateCount };
            }
            addStatementWarnings();
            succeeded = true;
        }
        if (keyColumnDataTypes != null) {
            try {
                ResultSet keys = statement.getGeneratedKeys();
                GeneratedKeys generatedKeys = context.getCommandContext().returnGeneratedKeys(keyColumnNames, keyColumnDataTypes);
                // many databases only support returning a single generated value, but we'll still attempt to gather all
                outer: while (keys.next()) {
                    List<Object> vals = new ArrayList<Object>(keyColumnDataTypes.length);
                    for (int i = 0; i < keyColumnDataTypes.length; i++) {
                        Object value = this.executionFactory.retrieveValue(keys, i + 1, keyColumnDataTypes[i]);
                        if (value != null && TypeFacility.getRuntimeType(value.getClass()) != keyColumnDataTypes[i]) {
                            // TODO we may need to let the engine to the final conversion
                            LogManager.logDetail(LogConstants.CTX_CONNECTOR, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID11023, keyColumnDataTypes[i], keyColumnNames[i], value.getClass()));
                            continue outer;
                        }
                        vals.add(value);
                    }
                    generatedKeys.addKey(vals);
                }
            } catch (SQLException e) {
                context.addWarning(e);
                // $NON-NLS-1$
                LogManager.logDetail(LogConstants.CTX_CONNECTOR, e, "Exception determining generated keys, no keys will be returned");
            }
        }
    } catch (SQLException err) {
        throw new JDBCExecutionException(JDBCPlugin.Event.TEIID11013, err, translatedComm);
    } finally {
        if (commitType) {
            restoreAutoCommit(!succeeded, translatedComm);
        }
    }
}
Also used : NamedTable(org.teiid.language.NamedTable) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) GeneratedKeys(org.teiid.GeneratedKeys) Insert(org.teiid.language.Insert) TranslatorBatchException(org.teiid.translator.TranslatorBatchException) KeyRecord(org.teiid.metadata.KeyRecord) Column(org.teiid.metadata.Column) BulkCommand(org.teiid.language.BulkCommand) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) BatchUpdateException(java.sql.BatchUpdateException)

Example 9 with KeyRecord

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

the class TestUpdateValidator method example1.

public static TransformationMetadata example1(boolean allUpdatable) {
    MetadataStore metadataStore = new MetadataStore();
    // Create models
    // $NON-NLS-1$
    Schema pm1 = RealMetadataFactory.createPhysicalModel("pm1", metadataStore);
    // $NON-NLS-1$
    Schema vm1 = RealMetadataFactory.createVirtualModel("vm1", metadataStore);
    // Create physical groups
    // $NON-NLS-1$
    Table pm1g1 = RealMetadataFactory.createPhysicalGroup("g1", pm1);
    // $NON-NLS-1$
    Table pm1g2 = RealMetadataFactory.createPhysicalGroup("g2", pm1);
    // $NON-NLS-1$
    Table pm1g3 = RealMetadataFactory.createPhysicalGroup("g3", pm1);
    // Create physical elements
    List<Column> pm1g1e = RealMetadataFactory.createElements(pm1g1, // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    new String[] { "e1", "e2", "e3", "e4" }, new String[] { DataTypeManager.DefaultDataTypes.STRING, DataTypeManager.DefaultDataTypes.INTEGER, DataTypeManager.DefaultDataTypes.BOOLEAN, DataTypeManager.DefaultDataTypes.DOUBLE });
    if (!allUpdatable) {
        pm1g1e.get(0).setUpdatable(false);
    }
    KeyRecord pk = RealMetadataFactory.createKey(Type.Primary, "pk", pm1g1, pm1g1e.subList(0, 1));
    List<Column> pm1g2e = RealMetadataFactory.createElements(pm1g2, // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    new String[] { "e1", "e2", "e3", "e4" }, new String[] { DataTypeManager.DefaultDataTypes.STRING, DataTypeManager.DefaultDataTypes.INTEGER, DataTypeManager.DefaultDataTypes.BOOLEAN, DataTypeManager.DefaultDataTypes.DOUBLE });
    RealMetadataFactory.createKey(Type.Primary, "pk", pm1g2, pm1g1e.subList(1, 2));
    RealMetadataFactory.createForeignKey("fk", pm1g2, pm1g2e.subList(0, 1), pk);
    List<Column> pm1g3e = RealMetadataFactory.createElements(pm1g3, // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    new String[] { "e1", "e2", "e3", "e4" }, new String[] { DataTypeManager.DefaultDataTypes.STRING, DataTypeManager.DefaultDataTypes.INTEGER, DataTypeManager.DefaultDataTypes.BOOLEAN, DataTypeManager.DefaultDataTypes.DOUBLE });
    pm1g3e.get(0).setNullType(NullType.No_Nulls);
    pm1g3e.get(0).setDefaultValue(null);
    pm1g3e.get(1).setNullType(NullType.No_Nulls);
    pm1g3e.get(1).setAutoIncremented(true);
    pm1g3e.get(1).setDefaultValue(null);
    pm1g3e.get(2).setNullType(NullType.No_Nulls);
    // $NON-NLS-1$
    pm1g3e.get(2).setDefaultValue("xyz");
    RealMetadataFactory.createKey(Type.Primary, "pk", pm1g3, pm1g3e.subList(0, 1));
    // Create virtual groups
    // $NON-NLS-1$ //$NON-NLS-2$
    QueryNode vm1g1n1 = new QueryNode("SELECT e1 as a, e2 FROM pm1.g1 WHERE e3 > 5");
    // $NON-NLS-1$
    Table vm1g1 = RealMetadataFactory.createUpdatableVirtualGroup("g1", vm1, vm1g1n1);
    // $NON-NLS-1$ //$NON-NLS-2$
    QueryNode vm1g2n1 = new QueryNode("SELECT e1, e2, e3, e4 FROM pm1.g2 WHERE e3 > 5");
    // $NON-NLS-1$
    Table vm1g2 = RealMetadataFactory.createUpdatableVirtualGroup("g2", vm1, vm1g2n1);
    // $NON-NLS-1$ //$NON-NLS-2$
    QueryNode vm1g3n1 = new QueryNode("SELECT e1, e3 FROM pm1.g3");
    // $NON-NLS-1$
    Table vm1g3 = RealMetadataFactory.createUpdatableVirtualGroup("g3", vm1, vm1g3n1);
    // $NON-NLS-1$ //$NON-NLS-2$
    QueryNode vm1g4n1 = new QueryNode("SELECT e1, e2 FROM pm1.g3");
    // $NON-NLS-1$
    Table vm1g4 = RealMetadataFactory.createUpdatableVirtualGroup("g4", vm1, vm1g4n1);
    // $NON-NLS-1$ //$NON-NLS-2$
    QueryNode vm1g5n1 = new QueryNode("SELECT e2, e3 FROM pm1.g3");
    // $NON-NLS-1$
    Table vm1g5 = RealMetadataFactory.createVirtualGroup("g5", vm1, vm1g5n1);
    // Create virtual elements
    RealMetadataFactory.createElements(vm1g1, // $NON-NLS-1$ //$NON-NLS-2$
    new String[] { "a", "e2" }, new String[] { DataTypeManager.DefaultDataTypes.STRING, DataTypeManager.DefaultDataTypes.INTEGER });
    RealMetadataFactory.createElements(vm1g2, // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    new String[] { "e1", "e2", "e3", "e4" }, new String[] { DataTypeManager.DefaultDataTypes.STRING, DataTypeManager.DefaultDataTypes.INTEGER, DataTypeManager.DefaultDataTypes.BOOLEAN, DataTypeManager.DefaultDataTypes.DOUBLE });
    RealMetadataFactory.createElements(vm1g3, // $NON-NLS-1$ //$NON-NLS-2$
    new String[] { "e1", "e2" }, new String[] { DataTypeManager.DefaultDataTypes.STRING, DataTypeManager.DefaultDataTypes.INTEGER });
    RealMetadataFactory.createElements(vm1g4, // $NON-NLS-1$ //$NON-NLS-2$
    new String[] { "e1", "e3" }, new String[] { DataTypeManager.DefaultDataTypes.STRING, DataTypeManager.DefaultDataTypes.BOOLEAN });
    RealMetadataFactory.createElements(vm1g5, // $NON-NLS-1$ //$NON-NLS-2$
    new String[] { "e2", "e3" }, new String[] { DataTypeManager.DefaultDataTypes.INTEGER, DataTypeManager.DefaultDataTypes.BOOLEAN });
    // Stored queries
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    ColumnSet<Procedure> rs1 = RealMetadataFactory.createResultSet("rs1", new String[] { "e1", "e2" }, new String[] { DataTypeManager.DefaultDataTypes.STRING, DataTypeManager.DefaultDataTypes.INTEGER });
    // $NON-NLS-1$ //$NON-NLS-2$
    QueryNode sq1n1 = new QueryNode("CREATE VIRTUAL PROCEDURE BEGIN SELECT e1, e2 FROM pm1.g1; END");
    // $NON-NLS-1$
    Procedure sq1 = RealMetadataFactory.createVirtualProcedure("sq1", pm1, Collections.EMPTY_LIST, sq1n1);
    sq1.setResultSet(rs1);
    // Create the facade from the store
    return RealMetadataFactory.createTransformationMetadata(metadataStore, "example");
}
Also used : MetadataStore(org.teiid.metadata.MetadataStore) KeyRecord(org.teiid.metadata.KeyRecord) Table(org.teiid.metadata.Table) Column(org.teiid.metadata.Column) QueryNode(org.teiid.query.mapping.relational.QueryNode) Schema(org.teiid.metadata.Schema) Procedure(org.teiid.metadata.Procedure)

Example 10 with KeyRecord

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

the class MongoDBMetadataProcessor method addForeignKey.

private void addForeignKey(MetadataFactory metadataFactory, Table childTable, Table table) {
    MergeDetails.Association association = MergeDetails.Association.valueOf(childTable.getProperty(ASSOSIATION, false));
    childTable.setProperty(ASSOSIATION, null);
    if (association == MergeDetails.Association.ONE) {
        KeyRecord record = table.getPrimaryKey();
        if (record != null) {
            ArrayList<String> pkColumns = new ArrayList<String>();
            for (Column column : record.getColumns()) {
                Column c = metadataFactory.getSchema().getTable(childTable.getName()).getColumnByName(column.getName());
                if (c == null) {
                    c = metadataFactory.addColumn(column.getName(), column.getRuntimeType(), childTable);
                }
                pkColumns.add(c.getName());
            }
            // $NON-NLS-1$
            metadataFactory.addPrimaryKey("PK0", pkColumns, childTable);
            // $NON-NLS-1$
            metadataFactory.addForeignKey("FK0", pkColumns, table.getName(), childTable);
        }
    } else {
        KeyRecord record = table.getPrimaryKey();
        if (record != null) {
            ArrayList<String> pkColumns = new ArrayList<String>();
            for (Column column : record.getColumns()) {
                // $NON-NLS-1$
                Column c = metadataFactory.getSchema().getTable(childTable.getName()).getColumnByName(table.getName() + "_" + column.getName());
                if (c == null) {
                    // $NON-NLS-1$
                    c = metadataFactory.addColumn(table.getName() + "_" + column.getName(), column.getRuntimeType(), childTable);
                }
                pkColumns.add(c.getName());
            }
            // $NON-NLS-1$
            metadataFactory.addForeignKey("FK0", pkColumns, table.getName(), childTable);
        }
    }
}
Also used : KeyRecord(org.teiid.metadata.KeyRecord) Column(org.teiid.metadata.Column) ArrayList(java.util.ArrayList)

Aggregations

KeyRecord (org.teiid.metadata.KeyRecord)22 Column (org.teiid.metadata.Column)18 Table (org.teiid.metadata.Table)14 ArrayList (java.util.ArrayList)11 LinkedHashMap (java.util.LinkedHashMap)3 Test (org.junit.Test)3 ForeignKey (org.teiid.metadata.ForeignKey)3 MetadataStore (org.teiid.metadata.MetadataStore)3 Schema (org.teiid.metadata.Schema)3 QueryNode (org.teiid.query.mapping.relational.QueryNode)3 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)3 TranslatorException (org.teiid.translator.TranslatorException)3 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 Properties (java.util.Properties)2 EntityType (javax.persistence.metamodel.EntityType)2 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)2 ChildRelationship (com.sforce.soap.partner.ChildRelationship)1 BatchUpdateException (java.sql.BatchUpdateException)1