Search in sources :

Example 71 with Column

use of org.teiid.metadata.Column 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 72 with Column

use of org.teiid.metadata.Column 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)

Example 73 with Column

use of org.teiid.metadata.Column 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 74 with Column

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

the class IQueryToLdapSearchParser method getLiteralQueryString.

static String getLiteralQueryString(Expression lhs, Expression rhs) {
    Column mdIDElement = ((ColumnReference) lhs).getMetadataObject();
    String expressionName = getLiteralString(mdIDElement, (Literal) rhs);
    expressionName = escapeReservedChars(expressionName);
    return expressionName;
}
Also used : Column(org.teiid.metadata.Column)

Example 75 with Column

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

the class IQueryToLdapSearchParser method translateSQLQueryToLDAPSearch.

/**
 * Public entry point to the parser.
 *  Parses the IQuery object, and constructs an equivalent LDAP search filter,
 *  keeping track of the attributes of interest.
 *  Here are some example SQL queries, and the equivalent LDAP search info:
 *  SQL: select cn, managerName from people_table where managerName LIKE "John%" and cn!="Mar()"
 *  Context name: [people_table's NameInSource, e.g. (ou=people,dc=company,dc=com)]
 *  LDAP attributes: (cn, String), (managerName, String)
 *  LDAP search filter: (&(managerName="John*")(!(cn="Mar\(\)")))
 *
 *  @param query the query
 *  @return the LDAPSearchDetails object
 */
// GHH 20080326 - added ability to restrict queries to only values where
// objectClass = table name.  This is done by adding a third parameter,
// RESTRICT, to the NameInSource property in the model:
// ou=people,dc=company,dc=com?SUBTREE_SCOPE?RESTRICT
// TODO - change method for calling RESTRICT to also specify
// object class name (RESTRICT=inetOrgPerson)
public LDAPSearchDetails translateSQLQueryToLDAPSearch(Select query) throws TranslatorException {
    // Parse SELECT symbols.
    // The columns will be translated into LDAP attributes of interest.
    ArrayList<Column> elementList = getElementsFromSelectSymbols(query);
    // Parse FROM table.
    // Only one table is expected here.
    List<TableReference> fromList = query.getFrom();
    Iterator<TableReference> itr = fromList.iterator();
    if (!itr.hasNext()) {
        // $NON-NLS-1$
        final String msg = LDAPPlugin.Util.getString("IQueryToLdapSearchParser.noTablesInFromError");
        throw new TranslatorException(msg);
    }
    TableReference fItm = itr.next();
    if (itr.hasNext()) {
        // $NON-NLS-1$
        final String msg = LDAPPlugin.Util.getString("IQueryToLdapSearchParser.multiItemsInFromError");
        throw new TranslatorException(msg);
    }
    LDAPSearchDetails sd = null;
    NamedTable tbl = null;
    NamedTable tblRight = null;
    if (fItm instanceof NamedTable) {
        tbl = (NamedTable) fItm;
    } else if (fItm instanceof Join) {
        Join join = (Join) fItm;
        if (!(join.getLeftItem() instanceof NamedTable) || !(join.getRightItem() instanceof NamedTable)) {
            // should not happen
            // $NON-NLS-1$
            final String msg = LDAPPlugin.Util.getString("IQueryToLdapSearchParser.groupCountExceededError");
            throw new TranslatorException(msg);
        }
        tbl = (NamedTable) join.getLeftItem();
        tblRight = (NamedTable) join.getRightItem();
    } else {
        // $NON-NLS-1$
        throw new AssertionError("Unsupported construct");
    }
    String contextName = getContextNameFromFromItem(tbl);
    int searchScope = getSearchScopeFromFromItem(tbl);
    // GHH 20080326 - added check for RESTRICT parameter in
    // NameInSource of from item
    String classRestriction = getRestrictToNamedClass(tbl);
    if (tblRight != null) {
        String contextName1 = getContextNameFromFromItem(tblRight);
        int searchScope1 = getSearchScopeFromFromItem(tblRight);
        String classRestriction1 = getRestrictToNamedClass(tblRight);
        if (!EquivalenceUtil.areEqual(contextName, contextName1) || searchScope != searchScope1 || !EquivalenceUtil.areEqual(classRestriction, classRestriction1)) {
            // $NON-NLS-1$
            final String msg = LDAPPlugin.Util.getString("IQueryToLdapSearchParser.not_same", tbl.getMetadataObject().getFullName(), tblRight.getMetadataObject().getFullName());
            throw new TranslatorException(msg);
        }
    }
    // Parse the WHERE clause.
    // Create an equivalent LDAP search filter.
    List<String> searchStringList = new LinkedList<String>();
    searchStringList = getSearchFilterFromWhereClause(query.getWhere(), searchStringList);
    StringBuilder filterBuilder = new StringBuilder();
    for (String string : searchStringList) {
        filterBuilder.append(string);
    }
    // add it to the search filter
    if (classRestriction != null && classRestriction.trim().length() > 0) {
        // $NON-NLS-1$  //$NON-NLS-2$  //$NON-NLS-3$
        filterBuilder.insert(0, "(&").append("(objectClass=").append(classRestriction).append("))");
    }
    // Parse the ORDER BY clause.
    // Create an ordered sort list.
    OrderBy orderBy = query.getOrderBy();
    // Referenced the JNDI standard...arguably, this should not be done inside this
    // class, and we should make our own key class. In practice, this makes things simpler.
    SortKey[] sortKeys = getSortKeysFromOrderByClause(orderBy);
    // Parse LIMIT clause.
    // Note that offsets are not supported.
    Limit limit = query.getLimit();
    long countLimit = -1;
    if (limit != null) {
        countLimit = limit.getRowLimit();
    }
    // Create Search Details
    sd = new LDAPSearchDetails(contextName, searchScope, filterBuilder.toString(), sortKeys, countLimit, elementList, 0);
    // Search Details logging
    sd.printDetailsToLog();
    return sd;
}
Also used : SortKey(javax.naming.ldap.SortKey) LinkedList(java.util.LinkedList) Column(org.teiid.metadata.Column) TranslatorException(org.teiid.translator.TranslatorException)

Aggregations

Column (org.teiid.metadata.Column)210 Table (org.teiid.metadata.Table)72 ArrayList (java.util.ArrayList)47 TranslatorException (org.teiid.translator.TranslatorException)47 Test (org.junit.Test)39 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)21 MetadataFactory (org.teiid.metadata.MetadataFactory)20 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)19 KeyRecord (org.teiid.metadata.KeyRecord)18 Schema (org.teiid.metadata.Schema)18 MetadataStore (org.teiid.metadata.MetadataStore)17 Procedure (org.teiid.metadata.Procedure)14 RealMetadataFactory (org.teiid.query.unittest.RealMetadataFactory)14 ColumnReference (org.teiid.language.ColumnReference)12 DerivedColumn (org.teiid.language.DerivedColumn)12 Expression (org.teiid.language.Expression)12 Literal (org.teiid.language.Literal)10 QueryNode (org.teiid.query.mapping.relational.QueryNode)9 Connection (java.sql.Connection)7 ResultSet (java.sql.ResultSet)7