use of org.teiid.query.sql.lang.Query in project teiid by teiid.
the class TestSetQueryParsing method testThreeUnions.
/**
* select c1 from g1 union select c2 from g2 union all select c3 from g3 union select c4 from g4
*/
@Test
public void testThreeUnions() {
SetQuery setQuery = new SetQuery(Operation.UNION);
setQuery.setAll(false);
// $NON-NLS-1$
GroupSymbol g = new GroupSymbol("g1");
From from = new From();
from.addGroup(g);
Select select = new Select();
// $NON-NLS-1$
select.addSymbol(new ElementSymbol("c1"));
Query query = new Query();
query.setSelect(select);
query.setFrom(from);
setQuery.setLeftQuery(query);
// $NON-NLS-1$
g = new GroupSymbol("g2");
from = new From();
from.addGroup(g);
select = new Select();
// $NON-NLS-1$
select.addSymbol(new ElementSymbol("c2"));
query = new Query();
query.setSelect(select);
query.setFrom(from);
setQuery.setRightQuery(query);
// $NON-NLS-1$
g = new GroupSymbol("g3");
from = new From();
from.addGroup(g);
select = new Select();
// $NON-NLS-1$
select.addSymbol(new ElementSymbol("c3"));
query = new Query();
query.setSelect(select);
query.setFrom(from);
setQuery = new SetQuery(SetQuery.Operation.UNION, true, setQuery, query);
// $NON-NLS-1$
g = new GroupSymbol("g4");
from = new From();
from.addGroup(g);
select = new Select();
// $NON-NLS-1$
select.addSymbol(new ElementSymbol("c4"));
query = new Query();
query.setSelect(select);
query.setFrom(from);
setQuery = new SetQuery(SetQuery.Operation.UNION, false, setQuery, query);
// $NON-NLS-1$
TestParser.helpTest(// $NON-NLS-1$
"select c1 from g1 union select c2 from g2 union all select c3 from g3 union select c4 from g4", // $NON-NLS-1$
"SELECT c1 FROM g1 UNION SELECT c2 FROM g2 UNION ALL SELECT c3 FROM g3 UNION SELECT c4 FROM g4", setQuery);
}
use of org.teiid.query.sql.lang.Query in project teiid by teiid.
the class TestAliasGenerator method testViewAliasing.
/**
* Ensures that views are named with v_ even without metadata
*/
@Test
public void testViewAliasing() throws Exception {
// $NON-NLS-1$
String sql = "select y.e1 from (select pm1.g1.e1 from pm1.g1) y";
Query command = (Query) QueryParser.getQueryParser().parseCommand(sql);
// $NON-NLS-1$
((ElementSymbol) command.getSelect().getSymbol(0)).setGroupSymbol(new GroupSymbol("y"));
command.acceptVisitor(new AliasGenerator(true));
// $NON-NLS-1$
assertEquals("SELECT v_0.c_0 FROM (SELECT g_0.e1 AS c_0 FROM pm1.g1 AS g_0) AS v_0", command.toString());
}
use of org.teiid.query.sql.lang.Query in project teiid by teiid.
the class TestAliasGenerator method testNestedViewAliasing.
@Test
public void testNestedViewAliasing() throws Exception {
// $NON-NLS-1$
String sql = "select e1, e2 from (select y.e1, y.e2 from (select pm1.g1.e1, 1 as e2 from pm1.g1) y) z";
Query command = (Query) QueryParser.getQueryParser().parseCommand(sql);
QueryResolver.resolveCommand(command, RealMetadataFactory.example1Cached());
command = (Query) command.clone();
command.acceptVisitor(new AliasGenerator(true));
// $NON-NLS-1$
assertEquals("SELECT v_1.c_0, v_1.c_1 FROM (SELECT v_0.c_0, v_0.c_1 FROM (SELECT g_0.e1 AS c_0, 1 AS c_1 FROM pm1.g1 AS g_0) AS v_0) AS v_1", command.toString());
}
use of org.teiid.query.sql.lang.Query in project teiid by teiid.
the class UpdateValidator method internalValidate.
private void internalValidate(Command command, List<ElementSymbol> viewSymbols) throws QueryMetadataException, TeiidComponentException {
if (!(command instanceof Query)) {
// $NON-NLS-1$
handleValidationError(QueryPlugin.Util.getString("ERR.015.012.0001"), true, true, true);
return;
}
Query query = (Query) command;
if (query.getFrom() == null || query.getInto() != null) {
// $NON-NLS-1$
handleValidationError(QueryPlugin.Util.getString("ERR.015.012.0001"), true, true, true);
return;
}
if (query.getWith() != null) {
// $NON-NLS-1$
String warning = QueryPlugin.Util.getString("ERR.015.012.0002");
updateReport.handleValidationWarning(warning);
deleteReport.handleValidationWarning(warning);
updateInfo.isSimple = false;
}
if (query.hasAggregates()) {
// $NON-NLS-1$
handleValidationError(QueryPlugin.Util.getString("ERR.015.012.0006"), true, true, true);
return;
}
if (query.getLimit() != null) {
// $NON-NLS-1$
handleValidationError(QueryPlugin.Util.getString("ERR.015.012.0013"), true, true, true);
return;
}
if (query.getSelect().isDistinct()) {
// $NON-NLS-1$
handleValidationError(QueryPlugin.Util.getString("ERR.015.012.0008"), true, true, true);
return;
}
updateInfo.view = query;
List<Expression> projectedSymbols = query.getSelect().getProjectedSymbols();
for (int i = 0; i < projectedSymbols.size(); i++) {
Expression symbol = projectedSymbols.get(i);
Expression ex = SymbolMap.getExpression(symbol);
if (!metadata.elementSupports(viewSymbols.get(i).getMetadataID(), SupportConstants.Element.UPDATE)) {
continue;
}
if (ex instanceof ElementSymbol) {
ElementSymbol es = (ElementSymbol) ex;
String groupName = es.getGroupSymbol().getName();
UpdateMapping info = updateInfo.updatableGroups.get(groupName);
if (es.getGroupSymbol().getDefinition() != null) {
ElementSymbol clone = es.clone();
clone.setOutputName(null);
clone.getGroupSymbol().setName(clone.getGroupSymbol().getNonCorrelationName());
clone.getGroupSymbol().setDefinition(null);
es = clone;
}
if (info == null) {
info = new UpdateMapping();
info.group = es.getGroupSymbol();
info.correlatedName = ((ElementSymbol) ex).getGroupSymbol();
updateInfo.updatableGroups.put(groupName, info);
}
// TODO: warn if mapped twice
info.updatableViewSymbols.put(viewSymbols.get(i), es);
} else {
// TODO: look for reversable widening conversions
// $NON-NLS-1$
report.handleValidationWarning(QueryPlugin.Util.getString("ERR.015.012.0007", viewSymbols.get(i), symbol));
}
}
if (query.getFrom().getClauses().size() > 1 || (!(query.getFrom().getClauses().get(0) instanceof UnaryFromClause))) {
// $NON-NLS-1$
String warning = QueryPlugin.Util.getString("ERR.015.012.0009", query.getFrom());
updateReport.handleValidationWarning(warning);
deleteReport.handleValidationWarning(warning);
updateInfo.isSimple = false;
}
List<GroupSymbol> allGroups = query.getFrom().getGroups();
HashSet<GroupSymbol> keyPreservingGroups = new HashSet<GroupSymbol>();
ResolverUtil.findKeyPreserved(query, keyPreservingGroups, metadata);
for (GroupSymbol groupSymbol : keyPreservingGroups) {
setUpdateFlags(groupSymbol);
}
allGroups.removeAll(keyPreservingGroups);
if (updateInfo.isSimple) {
if (!allGroups.isEmpty()) {
setUpdateFlags(allGroups.iterator().next());
}
} else {
for (GroupSymbol groupSymbol : allGroups) {
UpdateMapping info = updateInfo.updatableGroups.get(groupSymbol.getName());
if (info == null) {
// not projected
continue;
}
// $NON-NLS-1$
String warning = QueryPlugin.Util.getString("ERR.015.012.0004", info.correlatedName);
report.handleValidationWarning(warning);
}
}
boolean updatable = false;
boolean insertable = false;
for (UpdateMapping info : updateInfo.updatableGroups.values()) {
if (info.updateAllowed) {
if (!updatable) {
this.updateInfo.deleteTarget = info;
} else if (!info.getGroup().equals(this.updateInfo.deleteTarget.getGroup())) {
// TODO: warning about multiple
this.updateInfo.deleteTarget = null;
}
}
updatable |= info.updateAllowed;
insertable |= info.insertAllowed;
}
if ((this.updateInfo.insertType == UpdateType.INHERENT && !insertable)) {
// $NON-NLS-1$
handleValidationError(QueryPlugin.Util.getString("ERR.015.012.0015"), false, true, false);
}
if (this.updateInfo.updateType == UpdateType.INHERENT && !updatable) {
// $NON-NLS-1$
handleValidationError(QueryPlugin.Util.getString("ERR.015.012.0005"), true, false, false);
}
if (this.updateInfo.deleteType == UpdateType.INHERENT) {
if (this.updateInfo.deleteTarget == null) {
if (this.updateInfo.isSimple && updatable) {
this.updateInfo.deleteTarget = this.updateInfo.updatableGroups.values().iterator().next();
} else {
// $NON-NLS-1$
handleValidationError(QueryPlugin.Util.getString("ERR.015.012.0014"), false, false, true);
}
}
if (this.updateInfo.deleteTarget != null) {
GroupSymbol group = this.updateInfo.deleteTarget.group;
if (!this.updateInfo.isSimple && metadata.getPrimaryKey(group.getMetadataID()) == null && metadata.getUniqueKeysInGroup(group.getMetadataID()).isEmpty()) {
handleValidationError(QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31267, viewSymbols.iterator().next().getGroupSymbol(), group), false, false, true);
}
}
}
}
use of org.teiid.query.sql.lang.Query in project teiid by teiid.
the class CommandBuilder method expandAllSymbol.
/**
* Convert the "*" in "select * from..." to the list of column names for the data source.
*/
protected void expandAllSymbol(Command command) {
if (command instanceof Query) {
Query query = (Query) command;
Select select = query.getSelect();
List<Expression> originalSymbols = select.getSymbols();
List<Expression> expandedSymbols = new ArrayList<Expression>();
for (Iterator<Expression> i = originalSymbols.iterator(); i.hasNext(); ) {
Expression next = i.next();
if (next instanceof MultipleElementSymbol) {
MultipleElementSymbol allSymbol = (MultipleElementSymbol) next;
expandedSymbols.addAll(allSymbol.getElementSymbols());
} else {
expandedSymbols.add(next);
}
}
select.setSymbols(expandedSymbols);
}
}
Aggregations