http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4546628e/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridSqlSelect.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridSqlSelect.java b/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridSqlSelect.java new file mode 100644 index 0000000..535c3d1 --- /dev/null +++ b/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridSqlSelect.java @@ -0,0 +1,241 @@ +/* @java.file.header */ + +/* _________ _____ __________________ _____ + * __ ____/___________(_)______ /__ ____/______ ____(_)_______ + * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ + * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / + * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ + */ + +package org.gridgain.grid.kernal.processors.query.h2.sql; + +import org.h2.result.*; +import org.h2.util.*; + +import java.util.*; + +/** + * Select query. + */ +public class GridSqlSelect implements Cloneable { + /** */ + private boolean distinct; + + /** */ + private List<GridSqlElement> select = new ArrayList<>(); + + /** */ + private List<GridSqlElement> groups = new ArrayList<>(); + + /** */ + private GridSqlElement from; + + /** */ + private GridSqlElement where; + + /** */ + private GridSqlElement having; + + /** */ + private Map<GridSqlElement, Integer> sort = new LinkedHashMap<>(); + + /** + * @return Distinct. + */ + public boolean distinct() { + return distinct; + } + + /** + * @param distinct New distinct. + */ + public void distinct(boolean distinct) { + this.distinct = distinct; + } + + /** + * @return Generate sql. + */ + public String getSQL() { + StatementBuilder buff = new StatementBuilder("SELECT"); + + if (distinct) + buff.append(" DISTINCT"); + + for (GridSqlElement expression : select) { + buff.appendExceptFirst(","); + buff.append('\n'); + buff.append(StringUtils.indent(expression.getSQL(), 4, false)); + } + + buff.append("\nFROM ").append(from.getSQL()); + + if (where != null) + buff.append("\nWHERE ").append(StringUtils.unEnclose(where.getSQL())); + + if (!groups.isEmpty()) { + buff.append("\nGROUP BY "); + + buff.resetCount(); + + for (GridSqlElement expression : groups) { + buff.appendExceptFirst(", "); + + if (expression instanceof GridSqlAlias) + buff.append(StringUtils.unEnclose((expression.child().getSQL()))); + else + buff.append(StringUtils.unEnclose(expression.getSQL())); + } + } + + if (having != null) + buff.append("\nHAVING ").append(StringUtils.unEnclose(having.getSQL())); + + if (!sort.isEmpty()) { + buff.append("\nORDER BY "); + + buff.resetCount(); + + for (Map.Entry<GridSqlElement, Integer> entry : sort.entrySet()) { + buff.appendExceptFirst(", "); + + GridSqlElement expression = entry.getKey(); + + int idx = select.indexOf(expression); + + if (idx >= 0) + buff.append(idx + 1); + else + buff.append('=').append(StringUtils.unEnclose(expression.getSQL())); + + int type = entry.getValue(); + + if ((type & SortOrder.DESCENDING) != 0) + buff.append(" DESC"); + + if ((type & SortOrder.NULLS_FIRST) != 0) + buff.append(" NULLS FIRST"); + else if ((type & SortOrder.NULLS_LAST) != 0) + buff.append(" NULLS LAST"); + } + } + + return buff.toString(); + } + + /** + * @return Expressions. + */ + public List<GridSqlElement> select() { + return select; + } + + /** + * @param expression Expression. + */ + public void addSelectExpression(GridSqlElement expression) { + select.add(expression); + } + + /** + * @return Expressions. + */ + public List<GridSqlElement> groups() { + return groups; + } + + /** + * + */ + public void clearGroups() { + groups.clear(); + } + + /** + * @param expression Expression. + */ + public void addGroupExpression(GridSqlElement expression) { + groups.add(expression); + } + + /** + * @return Tables. + */ + public GridSqlElement from() { + return from; + } + + /** + * @param from From element. + */ + public void from(GridSqlElement from) { + this.from = from; + } + + /** + * @return Where. + */ + public GridSqlElement where() { + return where; + } + + /** + * @param where New where. + */ + public void where(GridSqlElement where) { + this.where = where; + } + + /** + * @return Having. + */ + public GridSqlElement having() { + return having; + } + + /** + * @param having New having. + */ + public void having(GridSqlElement having) { + this.having = having; + } + + /** + * @return Sort. + */ + public Map<GridSqlElement, Integer> sort() { + return sort; + } + + /** + * + */ + public void clearSort() { + sort.clear(); + } + + /** + * @param expression Expression. + * @param sortType The sort type bit mask (SortOrder.DESCENDING, SortOrder.NULLS_FIRST, SortOrder.NULLS_LAST). + */ + public void addSort(GridSqlElement expression, int sortType) { + sort.put(expression, sortType); + } + + /** {@inheritDoc} */ + @SuppressWarnings({"CloneCallsConstructors", "CloneDoesntDeclareCloneNotSupportedException"}) + @Override public GridSqlSelect clone() { + try { + GridSqlSelect res = (GridSqlSelect)super.clone(); + + res.select = new ArrayList<>(select); + res.groups = new ArrayList<>(groups); + res.sort = new LinkedHashMap<>(sort); + + return res; + } + catch (CloneNotSupportedException e) { + throw new RuntimeException(e); // Never thrown. + } + } +}
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4546628e/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridSqlSubquery.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridSqlSubquery.java b/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridSqlSubquery.java new file mode 100644 index 0000000..834f1f3 --- /dev/null +++ b/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridSqlSubquery.java @@ -0,0 +1,44 @@ +/* @java.file.header */ + +/* _________ _____ __________________ _____ + * __ ____/___________(_)______ /__ ____/______ ____(_)_______ + * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ + * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / + * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ + */ + +package org.gridgain.grid.kernal.processors.query.h2.sql; + +/** + * Subquery. + */ +public class GridSqlSubquery extends GridSqlElement { + /** */ + private GridSqlSelect select; + + /** + * @param select Select. + */ + public GridSqlSubquery(GridSqlSelect select) { + this.select = select; + } + + /** {@inheritDoc} */ + @Override public String getSQL() { + return "(" + select.getSQL() + ")"; + } + + /** + * @return Select. + */ + public GridSqlSelect select() { + return select; + } + + /** + * @param select New select. + */ + public void select(GridSqlSelect select) { + this.select = select; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4546628e/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridSqlTable.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridSqlTable.java b/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridSqlTable.java new file mode 100644 index 0000000..497fbc9 --- /dev/null +++ b/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridSqlTable.java @@ -0,0 +1,55 @@ +/* @java.file.header */ + +/* _________ _____ __________________ _____ + * __ ____/___________(_)______ /__ ____/______ ____(_)_______ + * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ + * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / + * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ + */ + +package org.gridgain.grid.kernal.processors.query.h2.sql; + +import org.h2.command.*; +import org.jetbrains.annotations.*; + +/** + * Table with optional schema. + */ +public class GridSqlTable extends GridSqlElement { + /** */ + private final String schema; + + /** */ + private final String tblName; + + /** + * @param schema Schema. + * @param tblName Table name. + */ + public GridSqlTable(@Nullable String schema, String tblName) { + this.schema = schema; + this.tblName = tblName; + } + + /** {@inheritDoc} */ + @Override public String getSQL() { + if (schema == null) + return Parser.quoteIdentifier(tblName); + + return Parser.quoteIdentifier(schema) + '.' + Parser.quoteIdentifier(tblName); + } + + /** + * @return Schema. + */ + public String schema() { + return schema; + } + + /** + * @return Table name. + */ + public String tableName() { + return tblName; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4546628e/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridSubquery.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridSubquery.java b/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridSubquery.java deleted file mode 100644 index 79dc7c0..0000000 --- a/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridSubquery.java +++ /dev/null @@ -1,44 +0,0 @@ -/* @java.file.header */ - -/* _________ _____ __________________ _____ - * __ ____/___________(_)______ /__ ____/______ ____(_)_______ - * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ - * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / - * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ - */ - -package org.gridgain.grid.kernal.processors.query.h2.sql; - -/** - * Subquery. - */ -public class GridSubquery extends GridSqlElement { - /** */ - private GridSelect select; - - /** - * @param select Select. - */ - public GridSubquery(GridSelect select) { - this.select = select; - } - - /** {@inheritDoc} */ - @Override public String getSQL() { - return "(" + select.getSQL() + ")"; - } - - /** - * @return Select. - */ - public GridSelect select() { - return select; - } - - /** - * @param select New select. - */ - public void select(GridSelect select) { - this.select = select; - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4546628e/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridTable.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridTable.java b/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridTable.java deleted file mode 100644 index 53b3b3f..0000000 --- a/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridTable.java +++ /dev/null @@ -1,55 +0,0 @@ -/* @java.file.header */ - -/* _________ _____ __________________ _____ - * __ ____/___________(_)______ /__ ____/______ ____(_)_______ - * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ - * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / - * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ - */ - -package org.gridgain.grid.kernal.processors.query.h2.sql; - -import org.h2.command.*; -import org.jetbrains.annotations.*; - -/** - * Table with optional schema. - */ -public class GridTable extends GridSqlElement { - /** */ - private final String schema; - - /** */ - private final String tblName; - - /** - * @param schema Schema. - * @param tblName Table name. - */ - public GridTable(@Nullable String schema, String tblName) { - this.schema = schema; - this.tblName = tblName; - } - - /** {@inheritDoc} */ - @Override public String getSQL() { - if (schema == null) - return Parser.quoteIdentifier(tblName); - - return Parser.quoteIdentifier(schema) + '.' + Parser.quoteIdentifier(tblName); - } - - /** - * @return Schema. - */ - public String schema() { - return schema; - } - - /** - * @return Table name. - */ - public String tableName() { - return tblName; - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4546628e/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridValueExpression.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridValueExpression.java b/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridValueExpression.java deleted file mode 100644 index 05c5c6c..0000000 --- a/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridValueExpression.java +++ /dev/null @@ -1,39 +0,0 @@ -/* @java.file.header */ - -/* _________ _____ __________________ _____ - * __ ____/___________(_)______ /__ ____/______ ____(_)_______ - * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ - * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / - * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ - */ - -package org.gridgain.grid.kernal.processors.query.h2.sql; - -import org.h2.value.*; - -/** - * Constant value. - */ -public class GridValueExpression extends GridSqlElement implements GridSqlValue { - /** */ - private final Value val; - - /** - * @param val Value. - */ - public GridValueExpression(Value val) { - this.val = val; - } - - /** - * @return Value. - */ - public Value value() { - return val; - } - - /** {@inheritDoc} */ - @Override public String getSQL() { - return val.getSQL(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4546628e/modules/indexing/src/test/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridQueryTest.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridQueryTest.java b/modules/indexing/src/test/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridQueryTest.java index f4f3c71..409ac13 100644 --- a/modules/indexing/src/test/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridQueryTest.java +++ b/modules/indexing/src/test/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridQueryTest.java @@ -135,9 +135,9 @@ public class GridQueryTest extends GridCacheAbstractQuerySelfTest { public void testExample1() throws Exception { Select select = parse("select p.name n, max(p.old) maxOld, min(p.old) minOld from Person p group by p.name having maxOld > 10 and min(p.old) < 1"); - GridQueryParser ses = new GridQueryParser(); + GridSqlQueryParser ses = new GridSqlQueryParser(); - GridSelect gridSelect = ses.toGridSelect(select); + GridSqlSelect gridSelect = ses.toGridSelect(select); //System.out.println(select.getPlanSQL()); System.out.println(gridSelect.getSQL()); @@ -159,12 +159,12 @@ public class GridQueryTest extends GridCacheAbstractQuerySelfTest { /** * @param sql Sql. */ - private GridSelect toGridSelect(String sql) throws Exception { + private GridSqlSelect toGridSelect(String sql) throws Exception { Session ses = (Session)connection().getSession(); Select select = (Select)ses.prepare(sql); - return new GridQueryParser().toGridSelect(select); + return new GridSqlQueryParser().toGridSelect(select); } /** @@ -203,7 +203,7 @@ public class GridQueryTest extends GridCacheAbstractQuerySelfTest { private void checkQuery(String qry) throws Exception { Prepared prepared = parse(qry); - GridQueryParser ses = new GridQueryParser(); + GridSqlQueryParser ses = new GridSqlQueryParser(); String res;