use of net.sf.jsqlparser.schema.Table in project dbeaver by serge-rider.
the class SQLSemanticProcessor method patchSelectQuery.
private static boolean patchSelectQuery(DBPDataSource dataSource, PlainSelect select, DBDDataFilter filter) throws JSQLParserException {
// WHERE
if (filter.hasConditions()) {
for (DBDAttributeConstraint co : filter.getConstraints()) {
if (co.hasCondition()) {
Table table = getConstraintTable(select, co);
if (table != null) {
if (table.getAlias() != null) {
co.setEntityAlias(table.getAlias().getName());
} else {
co.setEntityAlias(table.getName());
}
} else {
co.setEntityAlias(null);
}
}
}
StringBuilder whereString = new StringBuilder();
SQLUtils.appendConditionString(filter, dataSource, null, whereString, true);
String condString = whereString.toString();
addWhereToSelect(select, condString);
}
// ORDER
if (filter.hasOrdering()) {
List<OrderByElement> orderByElements = select.getOrderByElements();
if (orderByElements == null) {
orderByElements = new ArrayList<>();
select.setOrderByElements(orderByElements);
}
for (DBDAttributeConstraint co : filter.getOrderConstraints()) {
Expression orderExpr = getConstraintExpression(select, co);
OrderByElement element = new OrderByElement();
element.setExpression(orderExpr);
if (co.isOrderDescending()) {
element.setAsc(false);
element.setAscDescPresent(true);
}
orderByElements.add(element);
}
}
return true;
}
use of net.sf.jsqlparser.schema.Table in project dbeaver by dbeaver.
the class SQLSemanticProcessor method patchSelectQuery.
private static boolean patchSelectQuery(DBPDataSource dataSource, PlainSelect select, DBDDataFilter filter) throws JSQLParserException {
// WHERE
if (filter.hasConditions()) {
for (DBDAttributeConstraint co : filter.getConstraints()) {
if (co.hasCondition()) {
Table table = getConstraintTable(select, co);
if (table != null) {
if (table.getAlias() != null) {
co.setEntityAlias(table.getAlias().getName());
} else {
co.setEntityAlias(table.getName());
}
} else {
co.setEntityAlias(null);
}
}
}
StringBuilder whereString = new StringBuilder();
SQLUtils.appendConditionString(filter, dataSource, null, whereString, true);
String condString = whereString.toString();
addWhereToSelect(select, condString);
}
// ORDER
if (filter.hasOrdering()) {
List<OrderByElement> orderByElements = select.getOrderByElements();
if (orderByElements == null) {
orderByElements = new ArrayList<>();
select.setOrderByElements(orderByElements);
}
for (DBDAttributeConstraint co : filter.getOrderConstraints()) {
Expression orderExpr = getConstraintExpression(dataSource, select, co);
OrderByElement element = new OrderByElement();
element.setExpression(orderExpr);
if (co.isOrderDescending()) {
element.setAsc(false);
element.setAscDescPresent(true);
}
orderByElements.add(element);
}
}
return true;
}
use of net.sf.jsqlparser.schema.Table in project JSqlParser by JSQLParser.
the class SelectUtilsTest method testAddJoin.
@Test
public void testAddJoin() throws JSQLParserException {
Select select = (Select) CCJSqlParserUtil.parse("select a from mytable");
final EqualsTo equalsTo = new EqualsTo();
equalsTo.setLeftExpression(new Column("a"));
equalsTo.setRightExpression(new Column("b"));
Join addJoin = SelectUtils.addJoin(select, new Table("mytable2"), equalsTo);
addJoin.setLeft(true);
assertEquals("SELECT a FROM mytable LEFT JOIN mytable2 ON a = b", select.toString());
}
use of net.sf.jsqlparser.schema.Table in project JSqlParser by JSQLParser.
the class SelectUtilsTest method testBuildSelectFromTableWithGroupBy.
@Test
public void testBuildSelectFromTableWithGroupBy() {
Select select = SelectUtils.buildSelectFromTable(new Table("mytable"));
SelectUtils.addGroupBy(select, new Column("b"));
assertEquals("SELECT * FROM mytable GROUP BY b", select.toString());
}
use of net.sf.jsqlparser.schema.Table in project JSqlParser by JSQLParser.
the class SelectUtilsTest method testTableAliasIssue311.
@Test
public void testTableAliasIssue311() {
Table table1 = new Table("mytable1");
table1.setAlias(new Alias("tab1"));
Table table2 = new Table("mytable2");
table2.setAlias(new Alias("tab2"));
List<? extends Expression> colunas = Arrays.asList(new Column(table1, "col1"), new Column(table1, "col2"), new Column(table1, "col3"), new Column(table2, "b1"), new Column(table2, "b2"));
Select select = SelectUtils.buildSelectFromTableAndExpressions(table1, colunas.toArray(new Expression[colunas.size()]));
final EqualsTo equalsTo = new EqualsTo();
equalsTo.setLeftExpression(new Column(table1, "col1"));
equalsTo.setRightExpression(new Column(table2, "b1"));
Join addJoin = SelectUtils.addJoin(select, table2, equalsTo);
addJoin.setLeft(true);
assertEquals("SELECT tab1.col1, tab1.col2, tab1.col3, tab2.b1, tab2.b2 FROM mytable1 AS tab1 LEFT JOIN mytable2 AS tab2 ON tab1.col1 = tab2.b1", select.toString());
}
Aggregations