Search in sources :

Example 46 with Select

use of org.teiid.query.sql.lang.Select in project teiid by teiid.

the class TestSubqueryFromClause method example3.

public static SubqueryFromClause example3() {
    Query query = new Query();
    Select select = new Select();
    // $NON-NLS-1$
    select.addSymbol(new ElementSymbol("a"));
    // $NON-NLS-1$
    select.addSymbol(new ElementSymbol("b"));
    query.setSelect(select);
    From from = new From();
    // $NON-NLS-1$
    from.addGroup(new GroupSymbol("m.g"));
    query.setFrom(from);
    CompareCriteria crit = new CompareCriteria();
    // $NON-NLS-1$
    crit.setLeftExpression(new ElementSymbol("a"));
    crit.setRightExpression(new Constant(new Integer(5)));
    crit.setOperator(CompareCriteria.EQ);
    query.setCriteria(crit);
    // $NON-NLS-1$
    return new SubqueryFromClause("temp2", query);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Query(org.teiid.query.sql.lang.Query) Constant(org.teiid.query.sql.symbol.Constant) Select(org.teiid.query.sql.lang.Select) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) From(org.teiid.query.sql.lang.From) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) SubqueryFromClause(org.teiid.query.sql.lang.SubqueryFromClause)

Example 47 with Select

use of org.teiid.query.sql.lang.Select in project teiid by teiid.

the class TestAssignmentStatement method sample2.

public static final AssignmentStatement sample2() {
    Query query = new Query();
    // $NON-NLS-1$
    query.setSelect(new Select(Arrays.asList(new ElementSymbol("x"))));
    // $NON-NLS-1$
    query.setFrom(new From(Arrays.asList(new UnaryFromClause(new GroupSymbol("y")))));
    // $NON-NLS-1$
    return new AssignmentStatement(new ElementSymbol("b"), query);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Query(org.teiid.query.sql.lang.Query) UnaryFromClause(org.teiid.query.sql.lang.UnaryFromClause) AssignmentStatement(org.teiid.query.sql.proc.AssignmentStatement) Select(org.teiid.query.sql.lang.Select) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) From(org.teiid.query.sql.lang.From)

Example 48 with Select

use of org.teiid.query.sql.lang.Select in project teiid by teiid.

the class XMLQueryVisitationStrategy method consumeMsg.

/**
 * Consume an XML message and update the specified Select instance.
 * <br>
 * @param object the instance that is to be updated with the XML message data.
 * @param selectElement the XML element that contains the data
 * @return the updated instance.
 * @exception JDOMException if there is an error consuming the message.
 */
private Select consumeMsg(Select object, Element selectElement) throws JDOMException {
    Select select = (object != null) ? (Select) object : new Select();
    // --------------------------------
    // Read the DISTINCT attribute
    // --------------------------------
    String distinct = selectElement.getAttributeValue(TagNames.Attributes.DISTINCT);
    if (distinct != null) {
        if (distinct.equalsIgnoreCase("true")) {
            // $NON-NLS-1$
            select.setDistinct(true);
        }
    }
    // --------------------------------
    // Read the STAR attribute
    // --------------------------------
    String star = selectElement.getAttributeValue(TagNames.Attributes.STAR);
    if (star != null) {
        if (star.equalsIgnoreCase("true")) {
            // $NON-NLS-1$
            if (selectElement.getChildren() != null) {
                // $NON-NLS-1$
                throw new JDOMException("No children expected when star is chosen.");
            }
            return select;
        }
    }
    // --------------------------------
    // Read the IDENTIFIER elements ...
    // --------------------------------
    List idents = selectElement.getChildren();
    Iterator identIter = idents.iterator();
    while (identIter.hasNext()) {
        Element dataElement = (Element) identIter.next();
        Attribute dataType = dataElement.getAttribute(TagNames.Attributes.TYPE);
        // add the dataType of the element to the list containing dataTypes
        ElementSymbol nodeID = new ElementSymbol(dataElement.getText());
        Class nodeType = (Class) TagNames.TYPE_MAP.get(dataType.getValue());
        if (nodeType == null) {
            // $NON-NLS-1$ //$NON-NLS-2$
            throw new JDOMException("Unknown class for type \"" + dataType.getValue() + "\".");
        }
        nodeID.setType(nodeType);
        select.addSymbol(nodeID);
    }
    return select;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Attribute(org.jdom.Attribute) Element(org.jdom.Element) Select(org.teiid.query.sql.lang.Select) JDOMException(org.jdom.JDOMException)

Example 49 with Select

use of org.teiid.query.sql.lang.Select in project teiid by teiid.

the class XMLQueryVisitationStrategy method produceMsg.

/**
 * Produce a JDOM Element for an instance of a JDBC ResultSet object.
 * <br>
 * @param object for which the JDOM Element is to be produced.
 * @param endRow The row until which the results are to be converted to XML.
 * @return the JDOM element of the results object that was converted to XML.
 * @exception JDOMException if there is an error producing XML.
 * @exception SQLException if there is an error walking through the ResultSet object.
 */
private Element produceMsg(ResultSet object, int endRow) throws JDOMException, SQLException {
    // -----------------------------------
    // Create the QueryResults element ...
    // -----------------------------------
    Element resultsElement = new Element(TagNames.Elements.QUERY_RESULTS);
    // -----------------------------------
    try {
        ResultSetMetaData rmdata = object.getMetaData();
        List identList = new ArrayList(rmdata.getColumnCount());
        for (int i = 1; i <= rmdata.getColumnCount(); i++) {
            identList.add(new ElementSymbol(rmdata.getColumnName(i)));
        }
        Select select = new Select(identList);
        resultsElement = produceMsg(select, rmdata, resultsElement);
        // -------------------------
        // Add the Table element ...
        // -------------------------
        resultsElement.addContent(new Element(TagNames.Elements.TABLE));
        Element tableElement = resultsElement.getChild(TagNames.Elements.TABLE);
        int rowCount = 0;
        int colCount = rmdata.getColumnCount();
        while (object.next() && (object.getRow() <= endRow)) {
            // -------------------------
            // Add the ROW element ...
            // -------------------------
            Element rowElement = new Element(TagNames.Elements.TABLE_ROW);
            for (int i = 1; i <= colCount; i++) {
                // -------------------------
                // Add the Cell element ...
                // -------------------------
                Element cellElement = new Element(TagNames.Elements.TABLE_CELL);
                Object cellValue = object.getObject(i);
                if (cellValue != null) {
                    cellElement = produceMsg(cellValue, cellElement);
                } else {
                    cellElement = cellElement.addContent(TagNames.Elements.NULL);
                }
                rowElement.addContent(cellElement);
            }
            tableElement.addContent(rowElement);
            rowCount++;
        }
        Attribute rowCountAttribute = new Attribute(TagNames.Attributes.TABLE_ROW_COUNT, Integer.toString(rowCount));
        Attribute columnCountAttribute = new Attribute(TagNames.Attributes.TABLE_COLUMN_COUNT, Integer.toString(colCount));
        tableElement.setAttribute(rowCountAttribute);
        tableElement.setAttribute(columnCountAttribute);
    } catch (SQLException e) {
        // error while reading results
        throw (e);
    }
    return resultsElement;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Attribute(org.jdom.Attribute) Element(org.jdom.Element) Select(org.teiid.query.sql.lang.Select)

Example 50 with Select

use of org.teiid.query.sql.lang.Select in project teiid by teiid.

the class XMLQueryVisitationStrategy method consumeMsg.

/**
 * Consume an XML message and update the specified QueryResults instance.
 * <br>
 * @param object the instance that is to be updated with the XML message data.
 * @param resultsElement the XML element that contains the data
 * @return the updated instance.
 */
private QueryResults consumeMsg(QueryResults object, Element resultsElement) throws JDOMException {
    // -----------------------
    // Process the element ...
    // -----------------------
    QueryResults results = object;
    if (results == null) {
        results = new QueryResults();
    }
    if (resultsElement.getChild(TagNames.Elements.SELECT) == null) {
        return results;
    }
    // -------------------------------
    // Read the SELECT elements
    // -------------------------------
    Element selectElement = resultsElement.getChild(TagNames.Elements.SELECT);
    Select select = new Select();
    select = consumeMsg(select, selectElement);
    List listOfElementSymbols = select.getSymbols();
    Iterator elementSymbolItr = listOfElementSymbols.iterator();
    Collection collectionOfColumnInfos = new ArrayList();
    while (elementSymbolItr.hasNext()) {
        ElementSymbol elementSymbol = (ElementSymbol) elementSymbolItr.next();
        Class elementType = elementSymbol.getType();
        String dataType = DataTypeManager.getDataTypeName(elementType);
        ColumnInfo columnInfo = new ColumnInfo(elementSymbol.getName(), dataType, elementType);
        collectionOfColumnInfos.add(columnInfo);
    }
    // Save column info
    results.addFields(collectionOfColumnInfos);
    // -------------------------------
    // Read the TABLE of data
    // -------------------------------
    Element tableElement = resultsElement.getChild(TagNames.Elements.TABLE);
    List tableRows = tableElement.getChildren(TagNames.Elements.TABLE_ROW);
    if (tableRows.size() > 0) {
        Iterator rowIter = tableRows.iterator();
        while (rowIter.hasNext()) {
            Element rowElement = (Element) rowIter.next();
            List cellElements = rowElement.getChildren(TagNames.Elements.TABLE_CELL);
            Iterator cellIter = cellElements.iterator();
            // Read cells of the table
            ArrayList row = new ArrayList();
            Object evalue = null;
            while (cellIter.hasNext()) {
                Element cellElement = (Element) cellIter.next();
                if (cellElement.getTextTrim().equalsIgnoreCase(TagNames.Elements.NULL)) {
                    row.add(null);
                } else {
                    Element cellChildElement = (Element) cellElement.getChildren().get(0);
                    evalue = consumeMsg(cellChildElement);
                    row.add(evalue);
                }
            }
            // Save row
            results.addRecord(row);
        }
    }
    return results;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Element(org.jdom.Element) Select(org.teiid.query.sql.lang.Select) ColumnInfo(org.teiid.test.client.ctc.QueryResults.ColumnInfo)

Aggregations

Select (org.teiid.query.sql.lang.Select)50 Query (org.teiid.query.sql.lang.Query)30 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)26 MultipleElementSymbol (org.teiid.query.sql.symbol.MultipleElementSymbol)26 From (org.teiid.query.sql.lang.From)25 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)24 Test (org.junit.Test)17 SetQuery (org.teiid.query.sql.lang.SetQuery)17 Constant (org.teiid.query.sql.symbol.Constant)15 UnaryFromClause (org.teiid.query.sql.lang.UnaryFromClause)11 Limit (org.teiid.query.sql.lang.Limit)10 ArrayList (java.util.ArrayList)5 CompareCriteria (org.teiid.query.sql.lang.CompareCriteria)5 Reference (org.teiid.query.sql.symbol.Reference)5 Element (org.jdom.Element)4 Expression (org.teiid.query.sql.symbol.Expression)4 List (java.util.List)3 Attribute (org.jdom.Attribute)3 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)3 LanguageObject (org.teiid.query.sql.LanguageObject)3