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);
}
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);
}
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;
}
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;
}
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;
}
Aggregations