Search in sources :

Example 71 with TranslatorException

use of org.teiid.translator.TranslatorException in project teiid by teiid.

the class MongoDBSelectVisitor method visit.

@Override
public void visit(IsNull obj) {
    append(obj.getExpression());
    Object expr = this.onGoingExpression.pop();
    ColumnDetail detail = this.expressionMap.get(expr);
    QueryBuilder query = QueryBuilder.start();
    if (detail == null) {
        this.exceptions.add(new TranslatorException(MongoDBPlugin.Event.TEIID18032, MongoDBPlugin.Util.gs(MongoDBPlugin.Event.TEIID18032)));
    } else {
        query = detail.getQueryBuilder();
        this.onGoingExpression.push(buildIsNullQuery(obj, query).get());
    }
}
Also used : DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) TranslatorException(org.teiid.translator.TranslatorException) QueryBuilder(com.mongodb.QueryBuilder)

Example 72 with TranslatorException

use of org.teiid.translator.TranslatorException 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)

Example 73 with TranslatorException

use of org.teiid.translator.TranslatorException in project teiid by teiid.

the class IQueryToLdapSearchParser method buildRequest.

public LDAPSearchDetails buildRequest(String query) throws TranslatorException {
    ArrayList<String> attributes = new ArrayList<String>();
    ArrayList<Column> columns = new ArrayList<Column>();
    String contextName = null;
    // $NON-NLS-1$
    String criteria = "";
    String searchScope = this.executionFactory.getSearchDefaultScope().name();
    int timeLimit = 0;
    long countLimit = -1;
    List<String> parts = StringUtil.tokenize(query, ';');
    for (String var : parts) {
        int index = var.indexOf('=');
        if (index == -1) {
            throw new TranslatorException(LDAPPlugin.Util.gs(LDAPPlugin.Event.TEIID12013, var));
        }
        String key = var.substring(0, index).trim();
        String value = var.substring(index + 1).trim();
        if (key.equalsIgnoreCase(CONTEXT_NAME)) {
            contextName = value;
        } else if (key.equalsIgnoreCase(CRITERIA)) {
            criteria = value;
        } else if (key.equalsIgnoreCase(SEARCH_SCOPE)) {
            searchScope = value;
        } else if (key.equalsIgnoreCase(TIMEOUT)) {
            timeLimit = Integer.parseInt(value);
        } else if (key.equalsIgnoreCase(COUNT_LIMIT)) {
            countLimit = Long.parseLong(value);
        } else if (key.equalsIgnoreCase(ATTRIBUTES)) {
            // $NON-NLS-1$
            StringTokenizer attrTokens = new StringTokenizer(value, ",");
            while (attrTokens.hasMoreElements()) {
                String name = attrTokens.nextToken().trim();
                attributes.add(name);
                Column column = new Column();
                column.setName(name);
                Datatype type = new Datatype();
                type.setName(TypeFacility.RUNTIME_NAMES.OBJECT);
                type.setJavaClassName(Object.class.getCanonicalName());
                column.setDatatype(type, true);
                columns.add(column);
            }
        } else {
            throw new TranslatorException(LDAPPlugin.Util.gs(LDAPPlugin.Event.TEIID12013, var));
        }
    }
    int searchScopeInt = buildSearchScope(searchScope);
    return new LDAPSearchDetails(contextName, searchScopeInt, criteria, null, countLimit, columns, timeLimit);
}
Also used : StringTokenizer(java.util.StringTokenizer) Column(org.teiid.metadata.Column) ArrayList(java.util.ArrayList) TranslatorException(org.teiid.translator.TranslatorException) Datatype(org.teiid.metadata.Datatype)

Example 74 with TranslatorException

use of org.teiid.translator.TranslatorException in project teiid by teiid.

the class LDAPDirectCreateUpdateDeleteQueryExecution method getAttributes.

private ArrayList<BasicAttribute> getAttributes(StringTokenizer st) throws TranslatorException {
    if (!st.hasMoreTokens()) {
        throw new TranslatorException(LDAPPlugin.Util.gs(LDAPPlugin.Event.TEIID12011));
    }
    ArrayList<BasicAttribute> attributes = new ArrayList<BasicAttribute>();
    if (st.hasMoreElements()) {
        String var = st.nextToken();
        int index = var.indexOf('=');
        if (index == -1) {
            throw new TranslatorException(LDAPPlugin.Util.gs(LDAPPlugin.Event.TEIID12011));
        }
        String key = var.substring(0, index).trim();
        String value = var.substring(index + 1).trim();
        if (key.equalsIgnoreCase(ATTRIBUTES)) {
            // $NON-NLS-1$
            StringTokenizer attrTokens = new StringTokenizer(value, ",");
            int attrCount = 0;
            while (attrTokens.hasMoreElements()) {
                String name = attrTokens.nextToken().trim();
                if (arguments.size() <= attrCount) {
                    throw new TranslatorException(LDAPPlugin.Util.gs(LDAPPlugin.Event.TEIID12012, name));
                }
                Argument argument = arguments.get(attrCount++);
                Object anObj = null;
                if (argument.getArgumentValue().getValue() != null) {
                    anObj = IQueryToLdapSearchParser.getLiteralString(argument.getArgumentValue());
                }
                attributes.add(new BasicAttribute(name, anObj));
            }
        } else {
            throw new TranslatorException(LDAPPlugin.Util.gs(LDAPPlugin.Event.TEIID12011));
        }
    }
    return attributes;
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) StringTokenizer(java.util.StringTokenizer) Argument(org.teiid.language.Argument) ArrayList(java.util.ArrayList) TranslatorException(org.teiid.translator.TranslatorException)

Example 75 with TranslatorException

use of org.teiid.translator.TranslatorException in project teiid by teiid.

the class LDAPDirectCreateUpdateDeleteQueryExecution method execute.

@Override
public void execute() throws TranslatorException {
    String firstToken = null;
    // $NON-NLS-1$
    StringTokenizer st = new StringTokenizer(query, ";");
    if (st.hasMoreTokens()) {
        firstToken = st.nextToken();
    }
    if (firstToken == null || (!firstToken.equalsIgnoreCase("create") && !firstToken.equalsIgnoreCase("update") && !firstToken.equalsIgnoreCase("delete"))) {
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        throw new TranslatorException(LDAPPlugin.Util.gs(LDAPPlugin.Event.TEIID12009));
    }
    LdapContext ldapCtx = null;
    try {
        // $NON-NLS-1$
        ldapCtx = (LdapContext) this.ldapConnection.lookup("");
    } catch (NamingException ne) {
        // $NON-NLS-1$
        throw new TranslatorException(ne, LDAPPlugin.Util.getString("LDAPUpdateExecution.createContextError", ne.getExplanation()));
    }
    if (firstToken.equalsIgnoreCase("delete")) {
        // //$NON-NLS-1$
        // the token after the marker is always DN
        String theDN = getDN(st);
        if (st.hasMoreTokens()) {
            throw new TranslatorException(LDAPPlugin.Util.gs(LDAPPlugin.Event.TEIID12013, st.nextToken()));
        }
        try {
            ldapCtx.destroySubcontext(theDN);
            this.updateCount = 1;
        } catch (NamingException ne) {
            // $NON-NLS-1$
            throw new TranslatorException(ne, LDAPPlugin.Util.getString("LDAPUpdateExecution.deleteFailed", theDN, ne.getExplanation()));
        } catch (Exception e) {
            // $NON-NLS-1$
            throw new TranslatorException(e, LDAPPlugin.Util.getString("LDAPUpdateExecution.deleteFailedUnexpected", theDN));
        }
    } else if (firstToken.equalsIgnoreCase("create")) {
        // $NON-NLS-1$
        // the token after the marker is always DN
        String theDN = getDN(st);
        ArrayList<BasicAttribute> attributes = getAttributes(st);
        if (st.hasMoreTokens()) {
            throw new TranslatorException(LDAPPlugin.Util.gs(LDAPPlugin.Event.TEIID12013, st.nextToken()));
        }
        BasicAttributes attrs = new BasicAttributes();
        for (BasicAttribute ba : attributes) {
            attrs.put(ba);
        }
        try {
            ldapCtx.createSubcontext(theDN, attrs);
            this.updateCount = 1;
        } catch (NamingException ne) {
            // $NON-NLS-1$
            throw new TranslatorException(ne, LDAPPlugin.Util.getString("LDAPUpdateExecution.insertFailed", theDN, ne.getExplanation()));
        } catch (Exception e) {
            // $NON-NLS-1$
            throw new TranslatorException(e, LDAPPlugin.Util.getString("LDAPUpdateExecution.insertFailedUnexpected", theDN));
        }
    } else if (firstToken.equalsIgnoreCase("update")) {
        // $NON-NLS-1$
        // the token after the marker is always DN
        String theDN = getDN(st);
        ArrayList<BasicAttribute> attributes = getAttributes(st);
        if (st.hasMoreTokens()) {
            throw new TranslatorException(LDAPPlugin.Util.gs(LDAPPlugin.Event.TEIID12013, st.nextToken()));
        }
        ModificationItem[] updateMods = new ModificationItem[attributes.size()];
        int i = 0;
        for (BasicAttribute ba : attributes) {
            updateMods[i++] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, ba);
        }
        try {
            ldapCtx.modifyAttributes(theDN, updateMods);
            this.updateCount = 1;
        } catch (NamingException ne) {
            // $NON-NLS-1$
            throw new TranslatorException(ne, LDAPPlugin.Util.getString("LDAPUpdateExecution.updateFailed", theDN, ne.getExplanation()));
        } catch (Exception e) {
            // $NON-NLS-1$
            throw new TranslatorException(e, LDAPPlugin.Util.getString("LDAPUpdateExecution.updateFailedUnexpected", theDN));
        }
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) ArrayList(java.util.ArrayList) NamingException(javax.naming.NamingException) TranslatorException(org.teiid.translator.TranslatorException) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) ModificationItem(javax.naming.directory.ModificationItem) StringTokenizer(java.util.StringTokenizer) TranslatorException(org.teiid.translator.TranslatorException) NamingException(javax.naming.NamingException) LdapContext(javax.naming.ldap.LdapContext)

Aggregations

TranslatorException (org.teiid.translator.TranslatorException)227 ArrayList (java.util.ArrayList)51 Column (org.teiid.metadata.Column)47 List (java.util.List)32 Table (org.teiid.metadata.Table)30 IOException (java.io.IOException)26 SQLException (java.sql.SQLException)26 ResourceException (javax.resource.ResourceException)26 Test (org.junit.Test)16 Expression (org.teiid.language.Expression)16 Literal (org.teiid.language.Literal)16 DataNotAvailableException (org.teiid.translator.DataNotAvailableException)16 Blob (java.sql.Blob)15 Argument (org.teiid.language.Argument)13 DBObject (com.mongodb.DBObject)11 HashMap (java.util.HashMap)11 ColumnReference (org.teiid.language.ColumnReference)11 ExecutionContext (org.teiid.translator.ExecutionContext)11 BasicDBObject (com.mongodb.BasicDBObject)10 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)10