From e5e17048931c9adf4541aab40f7afdfd8ff8c77c Mon Sep 17 00:00:00 2001 From: omershelef Date: Thu, 9 Apr 2015 12:45:43 +0300 Subject: [PATCH 001/559] new release 1.3.2 --- README.md | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ee69c377..a81b2a3d 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Install as plugin: ###Elasticsearch 1.4.X ```` -./bin/plugin -u https://github.com/NLPchina/elasticsearch-sql/releases/download/1.3.1/elasticsearch-sql-1.3.1.zip --install sql +./bin/plugin -u https://github.com/NLPchina/elasticsearch-sql/releases/download/1.3.2/elasticsearch-sql-1.3.2.zip --install sql ```` ## Basic Usage diff --git a/pom.xml b/pom.xml index 68fbc6dd..ea07065d 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.nlpcn elasticsearch-sql - 1.3.1 + 1.3.2 jar Query elasticsearch using SQL elasticsearch-sql From 442af5cf68162c52cd66a67b1f4caa4bdd0e1f28 Mon Sep 17 00:00:00 2001 From: omershelef Date: Thu, 9 Apr 2015 13:03:39 +0300 Subject: [PATCH 002/559] Add BUILDING.md --- BUILDING.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 BUILDING.md diff --git a/BUILDING.md b/BUILDING.md new file mode 100644 index 00000000..60bff575 --- /dev/null +++ b/BUILDING.md @@ -0,0 +1,26 @@ +## Building + +To build zip release of the plugin, run this command: + + +```` +mvn clean package assembly:single -DskipTests +```` + +Running this command will create zip release named elasticsearch-sql-{version}.zip under 'target' directory. which can be installed from the file system using elasticsearch plugin bash script: + +```` +./bin/plugin -u file:///home/omershelef/IdeaProjects/elasticsearch-sql/target/elasticsearch-sql-1.3.2.zip --install sql + +```` + + +## Tests + +To run the tests, you will need elasticsearch instance running on your local machine. Alternatively you can set the environment variables ES_TEST_HOST and ES_TEST_PORT to point the tests to some other elasticsearch instance instead local machine on default port 9200. +To run the test all you need is running: + +```` +mvn test + +```` \ No newline at end of file From a310ffe4e05a4e7c23af23aa18e8969c578dafe2 Mon Sep 17 00:00:00 2001 From: Jake Heimbouch Date: Tue, 12 May 2015 15:00:52 -0500 Subject: [PATCH 003/559] Updated ExplanCond to provide support for NOT operator in front of a condition. This gives you the ability to say 'where not column_name operator value'. --- .../org/nlpcn/es4sql/parse/SqlParser.java | 44 +++++++++---------- .../java/org/nlpcn/es4sql/MainTestSuite.java | 23 +++++++++- src/test/resources/phrases.json | 4 +- 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index e23055cb..e7559a68 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.List; +import org.durid.sql.ast.expr.*; import org.durid.sql.ast.statement.*; import org.nlpcn.es4sql.domain.*; import org.nlpcn.es4sql.domain.Where.CONN; @@ -10,16 +11,6 @@ import org.durid.sql.ast.SQLExpr; import org.durid.sql.ast.SQLOrderBy; import org.durid.sql.ast.SQLOrderingSpecification; -import org.durid.sql.ast.expr.SQLBetweenExpr; -import org.durid.sql.ast.expr.SQLBinaryOpExpr; -import org.durid.sql.ast.expr.SQLCharExpr; -import org.durid.sql.ast.expr.SQLIdentifierExpr; -import org.durid.sql.ast.expr.SQLInListExpr; -import org.durid.sql.ast.expr.SQLMethodInvokeExpr; -import org.durid.sql.ast.expr.SQLNullExpr; -import org.durid.sql.ast.expr.SQLNumericLiteralExpr; -import org.durid.sql.ast.expr.SQLPropertyExpr; -import org.durid.sql.ast.expr.SQLQueryExpr; import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit; @@ -115,20 +106,25 @@ private void parseWhere(SQLBinaryOpExpr expr, SQLBinaryOpExpr sub, Where where) } private void explanCond(String opear, SQLExpr expr, Where where) throws SqlParseException { - if (expr instanceof SQLBinaryOpExpr) { - SQLBinaryOpExpr soExpr = (SQLBinaryOpExpr) expr; - Condition condition = new Condition(CONN.valueOf(opear), soExpr.getLeft().toString(), soExpr.getOperator().name, parseValue(soExpr.getRight())); - where.addWhere(condition); - } else if (expr instanceof SQLInListExpr) { - SQLInListExpr siExpr = (SQLInListExpr) expr; - Condition condition = new Condition(CONN.valueOf(opear), siExpr.getExpr().toString(), siExpr.isNot() ? "NOT IN" : "IN", parseValue(siExpr.getTargetList())); - where.addWhere(condition); - } else if (expr instanceof SQLBetweenExpr) { - SQLBetweenExpr between = ((SQLBetweenExpr) expr); - Condition condition = new Condition(CONN.valueOf(opear), between.getTestExpr().toString(), between.isNot() ? "NOT BETWEEN" : "BETWEEN", new Object[] { parseValue(between.beginExpr), - parseValue(between.endExpr) }); - where.addWhere(condition); - } else { + if (expr instanceof SQLBinaryOpExpr) { + SQLBinaryOpExpr soExpr = (SQLBinaryOpExpr) expr; + Condition condition = new Condition(CONN.valueOf(opear), soExpr.getLeft().toString(), soExpr.getOperator().name, parseValue(soExpr.getRight())); + where.addWhere(condition); + } else if (expr instanceof SQLInListExpr) { + SQLInListExpr siExpr = (SQLInListExpr) expr; + Condition condition = new Condition(CONN.valueOf(opear), siExpr.getExpr().toString(), siExpr.isNot() ? "NOT IN" : "IN", parseValue(siExpr.getTargetList())); + where.addWhere(condition); + } else if (expr instanceof SQLBetweenExpr) { + SQLBetweenExpr between = ((SQLBetweenExpr) expr); + Condition condition = new Condition(CONN.valueOf(opear), between.getTestExpr().toString(), between.isNot() ? "NOT BETWEEN" : "BETWEEN", new Object[]{parseValue(between.beginExpr), + parseValue(between.endExpr)}); + where.addWhere(condition); + } else if (expr instanceof SQLNotExpr){ + String left = ((SQLBinaryOpExpr) ((SQLNotExpr) expr).getExpr()).getLeft().toString(); + SQLExpr right = ((SQLBinaryOpExpr) ((SQLNotExpr) expr).getExpr()).getRight(); + Condition condition = new Condition(CONN.valueOf(opear),left, Condition.OPEAR.N, parseValue(right)); + where.addWhere(condition); + } else { throw new SqlParseException("err find condition " + expr.getClass()); } } diff --git a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java index c2861864..e57e4fa9 100644 --- a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java +++ b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java @@ -50,12 +50,12 @@ public static void setUp() throws Exception { // Load test data. deleteQuery(TEST_INDEX); loadBulk("src/test/resources/accounts.json"); - loadBulk("src/test/resources/phrases.json"); loadBulk("src/test/resources/online.json"); prepareOdbcIndex(); loadBulk("src/test/resources/odbc-date-formats.json"); - + preparePhraseIndex(); + loadBulk("src/test/resources/phrases.json"); searchDao = new SearchDao(client); @@ -137,6 +137,25 @@ public static void prepareOdbcIndex(){ client.admin().indices().preparePutMapping(TEST_INDEX).setType("odbc").setSource(dataMapping).execute().actionGet(); } + public static void preparePhraseIndex(){ + String dataMapping = "{\n" + + "\t\"phrase\" :{\n" + + "\t\t\"properties\":{\n" + + "\t\t\t\"insert_time\":{\n" + + "\t\t\t\t\"type\":\"date\",\n" + + "\t\t\t\t\"format\": \"{'ts' ''yyyy-MM-dd HH:mm:ss.SSS''}\"\n" + + "\t\t\t},\n" + + "\t\"phrase\":{\n" + + "\t\"type\":\"string\",\n" + + "\t\"index\":\"not_analyzed\"\n" + + "\t}\n" + + "\t}\n" + + "\t}\n" + + "}"; + + client.admin().indices().preparePutMapping(TEST_INDEX).setType("phrase").setSource(dataMapping).execute().actionGet(); + } + public static SearchDao getSearchDao() { return searchDao; } diff --git a/src/test/resources/phrases.json b/src/test/resources/phrases.json index df69205c..f4ed23bc 100644 --- a/src/test/resources/phrases.json +++ b/src/test/resources/phrases.json @@ -1,8 +1,8 @@ {"index":{"_type": "phrase"}} {"phrase": "quick fox"} {"index":{"_type": "phrase"}} -{"phrase": "quick fox here", "insert_time":"2014-08-19T07:09:13.434Z" } +{"phrase": "quick fox here"} {"index":{"_type": "phrase"}} {"phrase": "brown fox"} {"index":{"_type": "phrase"}} -{"phrase": "fox brown", "insert_time":"2014-08-19T07:09:13.434Z"} +{"phrase": "fox brown"} From 5221b097fc5b65959cdcd6a5ce88506c2c470067 Mon Sep 17 00:00:00 2001 From: Jake Heimbouch Date: Tue, 12 May 2015 15:12:58 -0500 Subject: [PATCH 004/559] New Release 1.3.3 --- README.md | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a81b2a3d..558afc6b 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Install as plugin: ###Elasticsearch 1.4.X ```` -./bin/plugin -u https://github.com/NLPchina/elasticsearch-sql/releases/download/1.3.2/elasticsearch-sql-1.3.2.zip --install sql +./bin/plugin -u https://github.com/NLPchina/elasticsearch-sql/releases/download/1.3.2/elasticsearch-sql-1.3.3.zip --install sql ```` ## Basic Usage diff --git a/pom.xml b/pom.xml index ea07065d..2ab496d4 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.nlpcn elasticsearch-sql - 1.3.2 + 1.3.3 jar Query elasticsearch using SQL elasticsearch-sql From d3cc8bd0c95a8b8fd9ba8fbb84ff80aee5dca4b0 Mon Sep 17 00:00:00 2001 From: Jake Heimbouch Date: Tue, 12 May 2015 15:20:05 -0500 Subject: [PATCH 005/559] Fixed 1.3.2 > 1.3.3 in plugin install URL --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 558afc6b..c8c927d5 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Install as plugin: ###Elasticsearch 1.4.X ```` -./bin/plugin -u https://github.com/NLPchina/elasticsearch-sql/releases/download/1.3.2/elasticsearch-sql-1.3.3.zip --install sql +./bin/plugin -u https://github.com/NLPchina/elasticsearch-sql/releases/download/1.3.3/elasticsearch-sql-1.3.3.zip --install sql ```` ## Basic Usage From dc028125c3150657fe50c267269f3a6b3f27ca72 Mon Sep 17 00:00:00 2001 From: Jake Heimbouch Date: Wed, 20 May 2015 13:24:04 -0500 Subject: [PATCH 006/559] Fixed tests that were failing by reverting the phrase index back to standard analyzed instead of not_analyzed. Also returned the input_time field back to phrases.json --- .../java/org/nlpcn/es4sql/MainTestSuite.java | 23 +------------------ src/test/resources/phrases.json | 4 ++-- 2 files changed, 3 insertions(+), 24 deletions(-) diff --git a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java index e57e4fa9..9ca4f45d 100644 --- a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java +++ b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java @@ -51,12 +51,10 @@ public static void setUp() throws Exception { deleteQuery(TEST_INDEX); loadBulk("src/test/resources/accounts.json"); loadBulk("src/test/resources/online.json"); + loadBulk("src/test/resources/phrases.json"); prepareOdbcIndex(); loadBulk("src/test/resources/odbc-date-formats.json"); - preparePhraseIndex(); - loadBulk("src/test/resources/phrases.json"); - searchDao = new SearchDao(client); System.out.println("Finished the setup process..."); @@ -137,25 +135,6 @@ public static void prepareOdbcIndex(){ client.admin().indices().preparePutMapping(TEST_INDEX).setType("odbc").setSource(dataMapping).execute().actionGet(); } - public static void preparePhraseIndex(){ - String dataMapping = "{\n" + - "\t\"phrase\" :{\n" + - "\t\t\"properties\":{\n" + - "\t\t\t\"insert_time\":{\n" + - "\t\t\t\t\"type\":\"date\",\n" + - "\t\t\t\t\"format\": \"{'ts' ''yyyy-MM-dd HH:mm:ss.SSS''}\"\n" + - "\t\t\t},\n" + - "\t\"phrase\":{\n" + - "\t\"type\":\"string\",\n" + - "\t\"index\":\"not_analyzed\"\n" + - "\t}\n" + - "\t}\n" + - "\t}\n" + - "}"; - - client.admin().indices().preparePutMapping(TEST_INDEX).setType("phrase").setSource(dataMapping).execute().actionGet(); - } - public static SearchDao getSearchDao() { return searchDao; } diff --git a/src/test/resources/phrases.json b/src/test/resources/phrases.json index f4ed23bc..df69205c 100644 --- a/src/test/resources/phrases.json +++ b/src/test/resources/phrases.json @@ -1,8 +1,8 @@ {"index":{"_type": "phrase"}} {"phrase": "quick fox"} {"index":{"_type": "phrase"}} -{"phrase": "quick fox here"} +{"phrase": "quick fox here", "insert_time":"2014-08-19T07:09:13.434Z" } {"index":{"_type": "phrase"}} {"phrase": "brown fox"} {"index":{"_type": "phrase"}} -{"phrase": "fox brown"} +{"phrase": "fox brown", "insert_time":"2014-08-19T07:09:13.434Z"} From 17e6232791c59882ec8cc7923ee641ce4f0337f1 Mon Sep 17 00:00:00 2001 From: omershelef Date: Fri, 29 May 2015 15:41:45 +0300 Subject: [PATCH 007/559] fix bug in order by field with spaces. issue #48 --- .../java/org/nlpcn/es4sql/parse/SqlParser.java | 1 + src/test/java/org/nlpcn/es4sql/QueryTest.java | 16 ++++++++++++++++ src/test/resources/phrases.json | 4 ++++ 3 files changed, 21 insertions(+) diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index e7559a68..46971f8e 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -189,6 +189,7 @@ private void findOrderBy(MySqlSelectQueryBlock query, Select select) throws SqlP } String type = sqlSelectOrderByItem.getType().toString(); for (String name : lists) { + name = name.replace("`", ""); select.addOrderBy(name, type); } lists.clear(); diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java index 6c3803ac..389285d3 100644 --- a/src/test/java/org/nlpcn/es4sql/QueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java @@ -404,6 +404,22 @@ public void orderByDescTest() throws IOException, SqlParseException, SQLFeatureN Assert.assertTrue("The list is not ordered descending", sortedAges.equals(ages)); } + + @Test + public void orderByAscFieldWithSpaceTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { + SearchHits response = query(String.format("SELECT * FROM %s/phrase_2 ORDER BY `test field` ASC LIMIT 1000", TEST_INDEX)); + SearchHit[] hits = response.getHits(); + + ArrayList testFields = new ArrayList(); + for(SearchHit hit : hits) { + testFields.add((int)hit.getSource().get("test field")); + } + + ArrayList sortedTestFields = (ArrayList)testFields.clone(); + Collections.sort(sortedTestFields); + Assert.assertTrue("The list is not ordered ascending", sortedTestFields.equals(testFields)); + } + @Test public void testMultipartWhere() throws IOException, SqlParseException, SQLFeatureNotSupportedException{ SearchHits response = query(String.format("SELECT * FROM %s/account WHERE (firstname LIKE 'opal' OR firstname like 'rodriquez') AND (state like 'oh' OR state like 'hi')", TEST_INDEX)); diff --git a/src/test/resources/phrases.json b/src/test/resources/phrases.json index df69205c..494b6f80 100644 --- a/src/test/resources/phrases.json +++ b/src/test/resources/phrases.json @@ -6,3 +6,7 @@ {"phrase": "brown fox"} {"index":{"_type": "phrase"}} {"phrase": "fox brown", "insert_time":"2014-08-19T07:09:13.434Z"} +{"index":{"_type": "phrase_2"}} +{"phrase": "my test", "test field": 5} +{"index":{"_type": "phrase_2"}} +{"phrase": "my test 2", "test field": 7} \ No newline at end of file From 88f3a79db4af9b9c9340d43d83fd1628cccab330 Mon Sep 17 00:00:00 2001 From: omershelef Date: Fri, 29 May 2015 15:48:02 +0300 Subject: [PATCH 008/559] bugfix. selecting fields with backticks (when they contains space for example) is possible now. --- src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java | 3 ++- src/test/java/org/nlpcn/es4sql/QueryTest.java | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java b/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java index 6696cfa1..5681a003 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java +++ b/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java @@ -27,7 +27,8 @@ public class FieldMaker { public static Field makeField(SQLExpr expr, String alias) throws SqlParseException { if (expr instanceof SQLIdentifierExpr || expr instanceof SQLPropertyExpr) { - return new Field(expr.toString(), alias); + String name = expr.toString().replace("`", ""); + return new Field(name, alias); } else if (expr instanceof SQLQueryExpr) { throw new SqlParseException("unknow field name : " + expr); } else if (expr instanceof SQLAllColumnExpr) { diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java index 389285d3..b8b142c3 100644 --- a/src/test/java/org/nlpcn/es4sql/QueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java @@ -54,6 +54,18 @@ public void selectSpecificFields() throws IOException, SqlParseException, SQLFea } } + @Test + public void selectFieldWithSpace() throws IOException, SqlParseException, SQLFeatureNotSupportedException { + String[] arr = new String[] {"test field"}; + Set expectedSource = new HashSet(Arrays.asList(arr)); + + SearchHits response = query(String.format("SELECT `test field` FROM %s/phrase_2", TEST_INDEX)); + SearchHit[] hits = response.getHits(); + for(SearchHit hit : hits) { + Assert.assertEquals(expectedSource, hit.getSource().keySet()); + } + } + // TODO field aliases is not supported currently. it might be possible to change field names after the query already executed. /* From bc70defdb8d2bc7a5ed76edf3c78a666b87456ce Mon Sep 17 00:00:00 2001 From: Jake Heimbouch Date: Fri, 19 Jun 2015 09:59:00 -0500 Subject: [PATCH 009/559] * Can specify aggregation fields through GET using "&aggs=comma,delimited,field,names" * Can specify aggregation fields through POST by adding "comma,delimited,field,names" * You can also add sub aggregates for an aggregate by using a colon - e.g. "Firstname:age" --- .../plugin/nlpcn/RestSqlAction.java | 56 ++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java b/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java index 3b00dfe5..9a13d737 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java @@ -13,6 +13,10 @@ import org.elasticsearch.rest.*; import org.elasticsearch.rest.action.support.RestBuilderListener; import org.elasticsearch.rest.action.support.RestStatusToXContentListener; +import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; +import org.elasticsearch.search.aggregations.AggregationBuilders; +import org.elasticsearch.search.aggregations.bucket.terms.Terms; +import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder; import org.nlpcn.es4sql.SearchDao; import org.nlpcn.es4sql.query.explain.ExplainManager; @@ -31,17 +35,37 @@ public RestSqlAction(Settings settings, Client client, RestController restContro @Override protected void handleRequest(RestRequest request, RestChannel channel, final Client client) throws Exception { + final String aggsStart = ""; + final String aggsEnd = ""; + String sql = request.param("sql"); + String aggs = request.param("aggs"); if (sql == null) { sql = request.content().toUtf8(); + int startIndex = sql.indexOf(aggsStart); + int endIndex = sql.indexOf(aggsEnd); + + // If is properly included in the Post Body + if (startIndex >= 0 && endIndex >= 0) { + // Grab the the aggs element + String aggElement = sql.substring(startIndex, endIndex + aggsEnd.length()); + // Remove it from the sql param (body) + sql = sql.replaceAll(aggElement, ""); + // Assign aggs to the value of the element with the the tags + aggs = aggElement.substring(aggsStart.length(), aggElement.length() - aggsEnd.length()); + } } SearchDao searchDao = new SearchDao(client); ActionRequestBuilder actionRequestBuilder = searchDao.explain(sql); + ActionRequest actionRequest = actionRequestBuilder.request(); + // Add our aggregations to the action request builder if necessary + handleAggs(aggs, actionRequestBuilder); + // TODO add unittests to explain. (rest level?) if (request.path().endsWith("/_explain")) { String jsonExplanation = ExplainManager.explain(actionRequestBuilder); @@ -51,4 +75,34 @@ protected void handleRequest(RestRequest request, RestChannel channel, final Cli new ActionRequestExecuter(actionRequest, channel, client).execute(); } } -} + + private void handleAggs(String aggs, ActionRequestBuilder actionRequestBuilder) { + // If the aggs parameter isn't null or empty + if (aggs != null && aggs.trim().length() > 0) { + // Get a reference to Action Request Builder + SearchRequestBuilder b = (SearchRequestBuilder) actionRequestBuilder; + + // Split the comma delimited field names + String[] arAggs = aggs.split(","); + for (String strAgg : arAggs) { + strAgg = strAgg.trim(); + + // Split again on colons for sub aggregates + String[] arSubAggs = strAgg.split(":"); + TermsBuilder objOriginalTerms = null; + for (String strSubAgg: arSubAggs) { + if (objOriginalTerms == null) { + objOriginalTerms = AggregationBuilders.terms(strSubAgg).field(strSubAgg).size(0).order(Terms.Order.term(true)); + // Add our aggregation to the actionRequestBuilder + b.addAggregation(objOriginalTerms); + } + else { + TermsBuilder objSubTerms = AggregationBuilders.terms(strSubAgg).field(strSubAgg).size(0).order(Terms.Order.term(true)); + objOriginalTerms.subAggregation(objSubTerms); + objOriginalTerms = objSubTerms; + } + } + } + } + } +} \ No newline at end of file From ed9e9c98e0aba5646f2fff33b34fa971f59dfc69 Mon Sep 17 00:00:00 2001 From: omershelef Date: Sat, 20 Jun 2015 22:23:39 +0300 Subject: [PATCH 010/559] elasticsearch 1.6 support. issue #62 --- .travis.yml | 2 +- README.md | 4 ++-- pom.xml | 4 ++-- src/test/java/org/nlpcn/es4sql/MainTestSuite.java | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3ada6688..85fa0ca2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,7 @@ jdk: before_install: - - wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.4.1.deb && sudo dpkg -i --force-confnew elasticsearch-1.4.1.deb + - wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.6.0.deb && sudo dpkg -i --force-confnew elasticsearch-1.6.0.deb services: - elasticsearch diff --git a/README.md b/README.md index c8c927d5..07fca8e7 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,10 @@ You can also use ES functions in SQL. Install as plugin: -###Elasticsearch 1.4.X +###Elasticsearch 1.6.X ```` -./bin/plugin -u https://github.com/NLPchina/elasticsearch-sql/releases/download/1.3.3/elasticsearch-sql-1.3.3.zip --install sql +./bin/plugin -u https://github.com/NLPchina/elasticsearch-sql/releases/download/1.3.4/elasticsearch-sql-1.3.4.zip --install sql ```` ## Basic Usage diff --git a/pom.xml b/pom.xml index 2ab496d4..6b06f186 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.nlpcn elasticsearch-sql - 1.3.3 + 1.3.4 jar Query elasticsearch using SQL elasticsearch-sql @@ -55,7 +55,7 @@ org.elasticsearch elasticsearch - 1.4.1 + 1.6.0 provided diff --git a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java index 9ca4f45d..337924d3 100644 --- a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java +++ b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java @@ -109,7 +109,7 @@ public static void loadBulk(String jsonPath) throws Exception { BulkRequestBuilder bulkBuilder = new BulkRequestBuilder(client); byte[] buffer = ByteStreams.toByteArray(new FileInputStream(jsonPath)); - bulkBuilder.add(buffer, 0, buffer.length, true, TEST_INDEX, null); + bulkBuilder.add(buffer, 0, buffer.length, TEST_INDEX, null); BulkResponse response = bulkBuilder.get(); if(response.hasFailures()) { From 1b0117737055703c4f653bea3ff79bf3919a785c Mon Sep 17 00:00:00 2001 From: omershelef Date: Sat, 20 Jun 2015 23:08:34 +0300 Subject: [PATCH 011/559] revert travis ci elasticsearch version to 1.4.1 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 85fa0ca2..3ada6688 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,7 @@ jdk: before_install: - - wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.6.0.deb && sudo dpkg -i --force-confnew elasticsearch-1.6.0.deb + - wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.4.1.deb && sudo dpkg -i --force-confnew elasticsearch-1.4.1.deb services: - elasticsearch From f841a100ba5275f2b98600e9c456c185dd4735d4 Mon Sep 17 00:00:00 2001 From: Jake Heimbouch Date: Tue, 21 Jul 2015 13:20:17 -0500 Subject: [PATCH 012/559] - Refactored sub aggregations work. - Syntax now looks more like SQL per Omer's recommendation. - Added unit test. All existing unit tests still pass. - Removed old implementation of sub aggregations --- .../plugin/nlpcn/RestSqlAction.java | 51 -------------- src/main/java/org/nlpcn/es4sql/SearchDao.java | 66 ++++++++++++++++++- .../org/nlpcn/es4sql/AggregationTest.java | 53 ++++++++++----- 3 files changed, 101 insertions(+), 69 deletions(-) diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java b/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java index 9a13d737..55c79ca0 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java @@ -35,37 +35,16 @@ public RestSqlAction(Settings settings, Client client, RestController restContro @Override protected void handleRequest(RestRequest request, RestChannel channel, final Client client) throws Exception { - final String aggsStart = ""; - final String aggsEnd = ""; - - String sql = request.param("sql"); - String aggs = request.param("aggs"); if (sql == null) { sql = request.content().toUtf8(); - int startIndex = sql.indexOf(aggsStart); - int endIndex = sql.indexOf(aggsEnd); - - // If is properly included in the Post Body - if (startIndex >= 0 && endIndex >= 0) { - // Grab the the aggs element - String aggElement = sql.substring(startIndex, endIndex + aggsEnd.length()); - // Remove it from the sql param (body) - sql = sql.replaceAll(aggElement, ""); - // Assign aggs to the value of the element with the the tags - aggs = aggElement.substring(aggsStart.length(), aggElement.length() - aggsEnd.length()); - } } SearchDao searchDao = new SearchDao(client); ActionRequestBuilder actionRequestBuilder = searchDao.explain(sql); - ActionRequest actionRequest = actionRequestBuilder.request(); - // Add our aggregations to the action request builder if necessary - handleAggs(aggs, actionRequestBuilder); - // TODO add unittests to explain. (rest level?) if (request.path().endsWith("/_explain")) { String jsonExplanation = ExplainManager.explain(actionRequestBuilder); @@ -75,34 +54,4 @@ protected void handleRequest(RestRequest request, RestChannel channel, final Cli new ActionRequestExecuter(actionRequest, channel, client).execute(); } } - - private void handleAggs(String aggs, ActionRequestBuilder actionRequestBuilder) { - // If the aggs parameter isn't null or empty - if (aggs != null && aggs.trim().length() > 0) { - // Get a reference to Action Request Builder - SearchRequestBuilder b = (SearchRequestBuilder) actionRequestBuilder; - - // Split the comma delimited field names - String[] arAggs = aggs.split(","); - for (String strAgg : arAggs) { - strAgg = strAgg.trim(); - - // Split again on colons for sub aggregates - String[] arSubAggs = strAgg.split(":"); - TermsBuilder objOriginalTerms = null; - for (String strSubAgg: arSubAggs) { - if (objOriginalTerms == null) { - objOriginalTerms = AggregationBuilders.terms(strSubAgg).field(strSubAgg).size(0).order(Terms.Order.term(true)); - // Add our aggregation to the actionRequestBuilder - b.addAggregation(objOriginalTerms); - } - else { - TermsBuilder objSubTerms = AggregationBuilders.terms(strSubAgg).field(strSubAgg).size(0).order(Terms.Order.term(true)); - objOriginalTerms.subAggregation(objSubTerms); - objOriginalTerms = objSubTerms; - } - } - } - } - } } \ No newline at end of file diff --git a/src/main/java/org/nlpcn/es4sql/SearchDao.java b/src/main/java/org/nlpcn/es4sql/SearchDao.java index aa6e7f06..c1de6103 100644 --- a/src/main/java/org/nlpcn/es4sql/SearchDao.java +++ b/src/main/java/org/nlpcn/es4sql/SearchDao.java @@ -3,6 +3,8 @@ import java.sql.SQLFeatureNotSupportedException; import java.util.HashSet; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.durid.sql.SQLUtils; import org.durid.sql.ast.expr.SQLQueryExpr; @@ -13,7 +15,11 @@ import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; +import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.client.Client; +import org.elasticsearch.search.aggregations.AggregationBuilders; +import org.elasticsearch.search.aggregations.bucket.terms.Terms; +import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder; import org.nlpcn.es4sql.exception.SqlParseException; import org.nlpcn.es4sql.query.ESActionFactory; import org.nlpcn.es4sql.query.QueryAction; @@ -47,9 +53,67 @@ public SearchDao(Client client) { * @throws SqlParseException */ public ActionRequestBuilder explain(String sql) throws SqlParseException, SQLFeatureNotSupportedException { + final String pattern = ".*((group\\sby[\\s\\w,]*)(\\(.\\S*\\))([\\s\\w]*.*))"; + Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE); + Matcher m = p.matcher(sql); + String subAggregate = ""; + if(m.find()) { + subAggregate = m.group(3); + // If we find a sub aggregate possibility + if(m.group(2).trim().equalsIgnoreCase("group by")) { + sql = sql.replace(m.group(1), ""); + // Is there more at the end of the query? + if(m.group(4).trim().length() > 0) { + // Add it back on + sql += " " + m.group(4); + } + } + } QueryAction query = ESActionFactory.create(client, sql); - return query.explain(); + ActionRequestBuilder ret = query.explain(); + + // If there were aggregations, lets add them now + if(subAggregate.trim().length() > 0) { + addAggregations(subAggregate, ret); + } + + return ret; + } + + private void addAggregations(String aggs, ActionRequestBuilder actionRequestBuilder) { + // If the aggs parameter isn't null or empty + if (aggs != null && aggs.trim().length() > 0) { + // Get a reference to Action Request Builder + SearchRequestBuilder b = (SearchRequestBuilder) actionRequestBuilder; + + // Split the comma delimited field names + String[] arAggs = aggs.split(","); + for (String strAgg : arAggs) { + strAgg = strAgg.trim(); + + if(strAgg.startsWith("(")) { + strAgg = strAgg.substring(1, strAgg.length()); + } + + // Split again on left parenthesis for sub aggregates + String[] arSubAggs = strAgg.split("\\("); + TermsBuilder objOriginalTerms = null; + for (String strSubAgg: arSubAggs) { + strSubAgg = strSubAgg.replace(")", ""); + if (objOriginalTerms == null) { + objOriginalTerms = AggregationBuilders.terms(strSubAgg).field(strSubAgg).size(0).order(Terms.Order.term(true)); + // Add our aggregation to the actionRequestBuilder + b.addAggregation(objOriginalTerms); + } + else { + TermsBuilder objSubTerms = AggregationBuilders.terms(strSubAgg).field(strSubAgg).size(0).order(Terms.Order.term(true)); + objOriginalTerms.subAggregation(objSubTerms); + objOriginalTerms = objSubTerms; + } + } + } + } } } diff --git a/src/test/java/org/nlpcn/es4sql/AggregationTest.java b/src/test/java/org/nlpcn/es4sql/AggregationTest.java index 225bb975..6acac375 100644 --- a/src/test/java/org/nlpcn/es4sql/AggregationTest.java +++ b/src/test/java/org/nlpcn/es4sql/AggregationTest.java @@ -16,21 +16,16 @@ import org.junit.Assert; import org.junit.Test; import org.nlpcn.es4sql.exception.SqlParseException; - import java.io.IOException; import java.sql.SQLFeatureNotSupportedException; import java.util.*; - import static org.elasticsearch.search.aggregations.bucket.range.Range.Bucket; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; -import static org.hamcrest.Matchers.lessThanOrEqualTo; import static org.nlpcn.es4sql.TestsConstants.TEST_INDEX; - public class AggregationTest { - @Test public void countTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account", TEST_INDEX)); @@ -38,7 +33,6 @@ public void countTest() throws IOException, SqlParseException, SQLFeatureNotSupp Assert.assertEquals(1000, count.getValue()); } - @Test public void sumTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { Aggregations result = query(String.format("SELECT SUM(balance) FROM %s/account", TEST_INDEX)); @@ -78,7 +72,6 @@ public void statsTest() throws IOException, SqlParseException, SQLFeatureNotSupp assertThat(stats.getAvg(), equalTo(30.171)); } - @Test public void aliasTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { Aggregations result = query(String.format("SELECT COUNT(*) AS mycount FROM %s/account", TEST_INDEX)); @@ -160,8 +153,6 @@ public void orderByDescTest() throws IOException, SqlParseException, SQLFeatureN Assert.assertTrue("The list is not ordered descending", agesCount.equals(agesCount)); } - - @Test public void limitTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY age ORDER BY COUNT(*) LIMIT 5", TEST_INDEX)); @@ -170,8 +161,6 @@ public void limitTest() throws IOException, SqlParseException, SQLFeatureNotSupp assertThat(age.getBuckets().size(), equalTo(5)); } - - @Test public void countGroupByRange() throws IOException, SqlParseException, SQLFeatureNotSupportedException { Aggregations result = query(String.format("SELECT COUNT(age) FROM %s/account GROUP BY range(age, 20,25,30,35,40) ", TEST_INDEX)); @@ -188,9 +177,9 @@ public void countGroupByRange() throws IOException, SqlParseException, SQLFeatur /** * 时间 聚合 , 每天按照天聚合 参数说明: - * + * * http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html - * + * * @throws IOException * @throws SqlParseException */ @@ -202,9 +191,9 @@ public void countGroupByDateTest() throws IOException, SqlParseException, SQLFea /** * 时间范围聚合 - * + * * http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-daterange-aggregation.html - * + * * @throws IOException * @throws SqlParseException */ @@ -217,9 +206,9 @@ public void countDateRangeTest() throws IOException, SqlParseException, SQLFeatu /** * tophits 查询 - * + * * http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html - * + * * @throws IOException * @throws SqlParseException */ @@ -235,4 +224,34 @@ private Aggregations query(String query) throws SqlParseException, SQLFeatureNot return select.get().getAggregations(); } + @Test + public void testSubAggregations() throws Exception { + Set expectedAges = new HashSet(ContiguousSet.create(Range.closed(20, 40), DiscreteDomain.integers())); + + Map> buckets = new HashMap<>(); + + Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY (gender(age),state)", TEST_INDEX)); + Terms gender = result.get("gender"); + for(Terms.Bucket genderBucket : gender.getBuckets()) { + String genderKey = genderBucket.getKey(); + buckets.put(genderKey, new HashSet()); + Terms ageBuckets = (Terms) genderBucket.getAggregations().get("age"); + for(Terms.Bucket ageBucket : ageBuckets.getBuckets()) { + buckets.get(genderKey).add(Integer.parseInt(ageBucket.getKey())); + } + } + + Assert.assertEquals(2, buckets.keySet().size()); + Assert.assertEquals(expectedAges, buckets.get("m")); + Assert.assertEquals(expectedAges, buckets.get("f")); + + Terms state = result.get("state"); + for(Terms.Bucket stateBucket : state.getBuckets()) { + if(stateBucket.getKey().equalsIgnoreCase("ak")) { + Assert.assertTrue("There are 22 entries for state ak", stateBucket.getDocCount() == 22); + } + + } + } + } From e95724c2d64dbfcb5d3d14c7fb3c35062cd09853 Mon Sep 17 00:00:00 2001 From: Caleb Ott Date: Thu, 30 Jul 2015 16:15:47 -0500 Subject: [PATCH 013/559] Added the initial logic to make the group by more flexible. NOT WORKING currently. Need to work on fixing errors just wanted to get these changes into source control --- .../durid/sql/ast/expr/SQLIdentifierExpr.java | 9 +++ .../mysql/parser/MySqlSelectParser.java | 9 ++- src/main/java/org/nlpcn/es4sql/SearchDao.java | 74 +------------------ .../java/org/nlpcn/es4sql/domain/Select.java | 13 +++- .../org/nlpcn/es4sql/parse/SqlParser.java | 33 ++++++++- .../es4sql/query/AggregationQueryAction.java | 33 +++++---- .../org/nlpcn/es4sql/AggregationTest.java | 2 +- 7 files changed, 77 insertions(+), 96 deletions(-) diff --git a/src/main/java/org/durid/sql/ast/expr/SQLIdentifierExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLIdentifierExpr.java index 3bccc0ad..81b85bf7 100644 --- a/src/main/java/org/durid/sql/ast/expr/SQLIdentifierExpr.java +++ b/src/main/java/org/durid/sql/ast/expr/SQLIdentifierExpr.java @@ -24,6 +24,7 @@ public class SQLIdentifierExpr extends SQLExprImpl implements SQLName { private static final long serialVersionUID = -4101240977289682659L; private String name; + private boolean wrappedInParens; public SQLIdentifierExpr(){ @@ -46,6 +47,14 @@ public void setName(String name) { this.name = name; } + public boolean isWrappedInParens() { + return this.wrappedInParens; + } + + public void setWrappedInParens(boolean wrappedInParens) { + this.wrappedInParens = wrappedInParens; + } + public void output(StringBuffer buf) { buf.append(this.name); } diff --git a/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlSelectParser.java b/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlSelectParser.java index 6f34731a..d6f36e09 100644 --- a/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlSelectParser.java +++ b/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlSelectParser.java @@ -17,6 +17,7 @@ import org.durid.sql.ast.SQLExpr; import org.durid.sql.ast.SQLSetQuantifier; +import org.durid.sql.ast.expr.SQLIdentifierExpr; import org.durid.sql.ast.expr.SQLLiteralExpr; import org.durid.sql.ast.statement.SQLSelectGroupByClause; import org.durid.sql.ast.statement.SQLSelectQuery; @@ -227,7 +228,13 @@ protected void parseGroupBy(SQLSelectQueryBlock queryBlock) { SQLSelectGroupByClause groupBy = new SQLSelectGroupByClause(); while (true) { - groupBy.getItems().add(this.exprParser.expr()); + boolean parens = lexer.token() == Token.LPAREN; + SQLExpr expr = this.exprParser.expr(); + // if it is an identifier, keep track of if it is in parens or not + if (expr instanceof SQLIdentifierExpr) { + ((SQLIdentifierExpr) expr).setWrappedInParens(parens); + } + groupBy.getItems().add(expr); if (!(lexer.token() == (Token.COMMA))) { break; } diff --git a/src/main/java/org/nlpcn/es4sql/SearchDao.java b/src/main/java/org/nlpcn/es4sql/SearchDao.java index c1de6103..a225a1b9 100644 --- a/src/main/java/org/nlpcn/es4sql/SearchDao.java +++ b/src/main/java/org/nlpcn/es4sql/SearchDao.java @@ -3,23 +3,9 @@ import java.sql.SQLFeatureNotSupportedException; import java.util.HashSet; import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import org.durid.sql.SQLUtils; -import org.durid.sql.ast.expr.SQLQueryExpr; -import org.durid.sql.ast.statement.SQLTableSource; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; -import org.elasticsearch.action.ActionFuture; import org.elasticsearch.action.ActionRequestBuilder; -import org.elasticsearch.action.ActionResponse; -import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; -import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; -import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.client.Client; -import org.elasticsearch.search.aggregations.AggregationBuilders; -import org.elasticsearch.search.aggregations.bucket.terms.Terms; -import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder; import org.nlpcn.es4sql.exception.SqlParseException; import org.nlpcn.es4sql.query.ESActionFactory; import org.nlpcn.es4sql.query.QueryAction; @@ -53,67 +39,9 @@ public SearchDao(Client client) { * @throws SqlParseException */ public ActionRequestBuilder explain(String sql) throws SqlParseException, SQLFeatureNotSupportedException { - final String pattern = ".*((group\\sby[\\s\\w,]*)(\\(.\\S*\\))([\\s\\w]*.*))"; - Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE); - Matcher m = p.matcher(sql); - String subAggregate = ""; - if(m.find()) { - subAggregate = m.group(3); - // If we find a sub aggregate possibility - if(m.group(2).trim().equalsIgnoreCase("group by")) { - sql = sql.replace(m.group(1), ""); - // Is there more at the end of the query? - if(m.group(4).trim().length() > 0) { - // Add it back on - sql += " " + m.group(4); - } - } - } QueryAction query = ESActionFactory.create(client, sql); - ActionRequestBuilder ret = query.explain(); - - // If there were aggregations, lets add them now - if(subAggregate.trim().length() > 0) { - addAggregations(subAggregate, ret); - } - - return ret; - } - - private void addAggregations(String aggs, ActionRequestBuilder actionRequestBuilder) { - // If the aggs parameter isn't null or empty - if (aggs != null && aggs.trim().length() > 0) { - // Get a reference to Action Request Builder - SearchRequestBuilder b = (SearchRequestBuilder) actionRequestBuilder; - - // Split the comma delimited field names - String[] arAggs = aggs.split(","); - for (String strAgg : arAggs) { - strAgg = strAgg.trim(); - - if(strAgg.startsWith("(")) { - strAgg = strAgg.substring(1, strAgg.length()); - } - - // Split again on left parenthesis for sub aggregates - String[] arSubAggs = strAgg.split("\\("); - TermsBuilder objOriginalTerms = null; - for (String strSubAgg: arSubAggs) { - strSubAgg = strSubAgg.replace(")", ""); - if (objOriginalTerms == null) { - objOriginalTerms = AggregationBuilders.terms(strSubAgg).field(strSubAgg).size(0).order(Terms.Order.term(true)); - // Add our aggregation to the actionRequestBuilder - b.addAggregation(objOriginalTerms); - } - else { - TermsBuilder objSubTerms = AggregationBuilders.terms(strSubAgg).field(strSubAgg).size(0).order(Terms.Order.term(true)); - objOriginalTerms.subAggregation(objSubTerms); - objOriginalTerms = objSubTerms; - } - } - } - } + return query.explain(); } } diff --git a/src/main/java/org/nlpcn/es4sql/domain/Select.java b/src/main/java/org/nlpcn/es4sql/domain/Select.java index 80fb884c..10c14b2d 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/Select.java +++ b/src/main/java/org/nlpcn/es4sql/domain/Select.java @@ -15,7 +15,7 @@ public class Select extends Query { private final List aggsFunctions = Arrays.asList("SUM", "MAX", "MIN", "AVG", "TOPHITS", "COUNT", "STATS"); private List fields = new ArrayList<>(); - private List groupBys = new ArrayList<>(); + private List> groupBys = new ArrayList<>(); private List orderBys = new ArrayList<>(); private int offset; private int rowCount = 200; @@ -39,13 +39,18 @@ public void setRowCount(int rowCount) { this.rowCount = rowCount; } - public void addGroupBy(Field field) { + List wrapper = new ArrayList<>(); + wrapper.add(field); + addGroupBy(wrapper); + } + + public void addGroupBy(List fields) { isAgg = true; - this.groupBys.add(field); + this.groupBys.add(fields); } - public List getGroupBys() { + public List> getGroupBys() { return groupBys; } diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index 46971f8e..4939b96a 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -168,11 +168,42 @@ private void findGroupBy(MySqlSelectQueryBlock query, Select select) throws SqlP return; } List items = groupBy.getItems(); + + List standardGroupBys = new ArrayList<>(); for (SQLExpr sqlExpr : items) { - select.addGroupBy(FieldMaker.makeField(sqlExpr, null)); + if ((!(sqlExpr instanceof SQLIdentifierExpr) || ((SQLIdentifierExpr) sqlExpr).isWrappedInParens()) && !standardGroupBys.isEmpty()) { + // flush the standard group bys + select.addGroupBy(convertExprsToFields(standardGroupBys)); + standardGroupBys = new ArrayList<>(); + } + + if (sqlExpr instanceof SQLIdentifierExpr) { + SQLIdentifierExpr identifierExpr = (SQLIdentifierExpr) sqlExpr; + if (identifierExpr.isWrappedInParens()) { + // single item with parens (should be its own agg) + select.addGroupBy(FieldMaker.makeField(identifierExpr, null)); + } else { + // single item without parens (should latch to before or after list) + standardGroupBys.add(identifierExpr); + } + } else if (sqlExpr instanceof SQLListExpr) { + // multiple items in their own list + SQLListExpr listExpr = (SQLListExpr) sqlExpr; + select.addGroupBy(convertExprsToFields(listExpr.getItems())); + } } } + private List convertExprsToFields(List exprs) throws SqlParseException { + List fields = new ArrayList<>(exprs.size()); + for (SQLExpr expr : exprs) { + fields.add(FieldMaker.makeField(expr, null)); + } + return fields; + } + + + private void findOrderBy(MySqlSelectQueryBlock query, Select select) throws SqlParseException { SQLOrderBy orderBy = query.getOrderBy(); diff --git a/src/main/java/org/nlpcn/es4sql/query/AggregationQueryAction.java b/src/main/java/org/nlpcn/es4sql/query/AggregationQueryAction.java index f09d3e15..5a4ab8aa 100644 --- a/src/main/java/org/nlpcn/es4sql/query/AggregationQueryAction.java +++ b/src/main/java/org/nlpcn/es4sql/query/AggregationQueryAction.java @@ -45,26 +45,27 @@ public SearchRequestBuilder explain() throws SqlParseException { setWhere(select.getWhere()); AggregationBuilder lastAgg = null; - if (select.getGroupBys().size() > 0) { - Field field = select.getGroupBys().get(0); - lastAgg = aggMaker.makeGroupAgg(field); - + for (List groupBy : select.getGroupBys()) { + if (!groupBy.isEmpty()) { + Field field = groupBy.get(0); + lastAgg = aggMaker.makeGroupAgg(field); - if (lastAgg != null && lastAgg instanceof TermsBuilder) { - ((TermsBuilder) lastAgg).size(select.getRowCount()); - } + if (lastAgg != null && lastAgg instanceof TermsBuilder) { + ((TermsBuilder) lastAgg).size(select.getRowCount()); + } + + request.addAggregation(lastAgg); - request.addAggregation(lastAgg); + for (int i = 1; i < groupBy.size(); i++) { + field = groupBy.get(i); + AggregationBuilder subAgg = aggMaker.makeGroupAgg(field); + if (subAgg instanceof TermsBuilder) { + ((TermsBuilder) subAgg).size(0); + } - for (int i = 1; i < select.getGroupBys().size(); i++) { - field = select.getGroupBys().get(i); - AggregationBuilder subAgg = aggMaker.makeGroupAgg(field); - if(subAgg instanceof TermsBuilder){ - ((TermsBuilder)subAgg).size(0) ; + lastAgg.subAggregation(subAgg); + lastAgg = subAgg; } - - lastAgg.subAggregation(subAgg); - lastAgg = subAgg; } } diff --git a/src/test/java/org/nlpcn/es4sql/AggregationTest.java b/src/test/java/org/nlpcn/es4sql/AggregationTest.java index 6acac375..4a726714 100644 --- a/src/test/java/org/nlpcn/es4sql/AggregationTest.java +++ b/src/test/java/org/nlpcn/es4sql/AggregationTest.java @@ -230,7 +230,7 @@ public void testSubAggregations() throws Exception { Map> buckets = new HashMap<>(); - Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY (gender(age),state)", TEST_INDEX)); + Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY (gender, age), (state)", TEST_INDEX)); Terms gender = result.get("gender"); for(Terms.Bucket genderBucket : gender.getBuckets()) { String genderKey = genderBucket.getKey(); From f0666ad7aaa3ae6b5c2e8f6a0b493aec13a23e5d Mon Sep 17 00:00:00 2001 From: Caleb Ott Date: Thu, 30 Jul 2015 16:26:43 -0500 Subject: [PATCH 014/559] Added the last catch to add the group bys in the list at the end of the loop --- src/main/java/org/nlpcn/es4sql/parse/SqlParser.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index 4939b96a..90c84a61 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -192,6 +192,9 @@ private void findGroupBy(MySqlSelectQueryBlock query, Select select) throws SqlP select.addGroupBy(convertExprsToFields(listExpr.getItems())); } } + if (!standardGroupBys.isEmpty()) { + select.addGroupBy(convertExprsToFields(standardGroupBys)); + } } private List convertExprsToFields(List exprs) throws SqlParseException { From 89f199cbd4fc7f932dd5b2456df5a750a0b17332 Mon Sep 17 00:00:00 2001 From: Caleb Ott Date: Fri, 31 Jul 2015 07:48:36 -0500 Subject: [PATCH 015/559] Fixed a bug where expr that were not Identifier or List would be left out. Simply added an extra else to "catch" the rest of the expressions --- src/main/java/org/nlpcn/es4sql/parse/SqlParser.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index 90c84a61..ff986642 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -190,6 +190,9 @@ private void findGroupBy(MySqlSelectQueryBlock query, Select select) throws SqlP // multiple items in their own list SQLListExpr listExpr = (SQLListExpr) sqlExpr; select.addGroupBy(convertExprsToFields(listExpr.getItems())); + } else { + // something else + standardGroupBys.add(sqlExpr); } } if (!standardGroupBys.isEmpty()) { From 8e8c490d110c8d50d30e5e34ee2038f98c5e6281 Mon Sep 17 00:00:00 2001 From: Caleb Ott Date: Fri, 31 Jul 2015 07:57:47 -0500 Subject: [PATCH 016/559] Moved the setting "in parens" logic to a more logical location. now identifiers can be tracked as if they are in parens or not (since now that is important) --- .../sql/dialect/mysql/parser/MySqlSelectParser.java | 9 +-------- src/main/java/org/durid/sql/parser/SQLExprParser.java | 3 +++ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlSelectParser.java b/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlSelectParser.java index d6f36e09..6f34731a 100644 --- a/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlSelectParser.java +++ b/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlSelectParser.java @@ -17,7 +17,6 @@ import org.durid.sql.ast.SQLExpr; import org.durid.sql.ast.SQLSetQuantifier; -import org.durid.sql.ast.expr.SQLIdentifierExpr; import org.durid.sql.ast.expr.SQLLiteralExpr; import org.durid.sql.ast.statement.SQLSelectGroupByClause; import org.durid.sql.ast.statement.SQLSelectQuery; @@ -228,13 +227,7 @@ protected void parseGroupBy(SQLSelectQueryBlock queryBlock) { SQLSelectGroupByClause groupBy = new SQLSelectGroupByClause(); while (true) { - boolean parens = lexer.token() == Token.LPAREN; - SQLExpr expr = this.exprParser.expr(); - // if it is an identifier, keep track of if it is in parens or not - if (expr instanceof SQLIdentifierExpr) { - ((SQLIdentifierExpr) expr).setWrappedInParens(parens); - } - groupBy.getItems().add(expr); + groupBy.getItems().add(this.exprParser.expr()); if (!(lexer.token() == (Token.COMMA))) { break; } diff --git a/src/main/java/org/durid/sql/parser/SQLExprParser.java b/src/main/java/org/durid/sql/parser/SQLExprParser.java index 536664e0..01e1006b 100644 --- a/src/main/java/org/durid/sql/parser/SQLExprParser.java +++ b/src/main/java/org/durid/sql/parser/SQLExprParser.java @@ -140,6 +140,9 @@ public SQLExpr primary() { sqlExpr = listExpr; } accept(Token.RPAREN); + if (sqlExpr instanceof SQLIdentifierExpr) { + ((SQLIdentifierExpr) sqlExpr).setWrappedInParens(true); + } break; case LBRACE: lexer.nextToken(); From 3aaac10c1592ccb30dff1b03fc12b7485a3feb0e Mon Sep 17 00:00:00 2001 From: Caleb Ott Date: Fri, 31 Jul 2015 08:30:34 -0500 Subject: [PATCH 017/559] Removed some unnecessary imports --- .../elasticsearch/plugin/nlpcn/RestSqlAction.java | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java b/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java index 55c79ca0..42b0e163 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java @@ -2,26 +2,13 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestBuilder; -import org.elasticsearch.action.search.SearchRequest; -import org.elasticsearch.action.search.SearchRequestBuilder; -import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.common.io.stream.BytesStreamOutput; -import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.rest.*; -import org.elasticsearch.rest.action.support.RestBuilderListener; -import org.elasticsearch.rest.action.support.RestStatusToXContentListener; -import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; -import org.elasticsearch.search.aggregations.AggregationBuilders; -import org.elasticsearch.search.aggregations.bucket.terms.Terms; -import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder; import org.nlpcn.es4sql.SearchDao; import org.nlpcn.es4sql.query.explain.ExplainManager; -import java.io.FileOutputStream; - public class RestSqlAction extends BaseRestHandler { @Inject From 356e2e67c9ec8f06cf11273330ed05fe07dbd58d Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sat, 1 Aug 2015 00:31:47 +0300 Subject: [PATCH 018/559] geoshape filter support all you need to do is select * from indexName where GEO_INTERSECTS(fieldName,'WKT') --- pom.xml | 11 +++- .../org/nlpcn/es4sql/domain/Condition.java | 5 +- .../org/nlpcn/es4sql/parse/SqlParser.java | 15 +++++- .../org/nlpcn/es4sql/query/maker/Maker.java | 50 +++++++++++++++++-- .../java/org/nlpcn/es4sql/ExplainTest.java | 10 +++- .../java/org/nlpcn/es4sql/MainTestSuite.java | 34 ++++++++++--- src/test/java/org/nlpcn/es4sql/QueryTest.java | 8 +++ .../search_spatial_explain.json | 24 +++++++++ src/test/resources/locations.json | 4 ++ 9 files changed, 146 insertions(+), 15 deletions(-) create mode 100644 src/test/resources/expectedOutput/search_spatial_explain.json create mode 100644 src/test/resources/locations.json diff --git a/pom.xml b/pom.xml index 6b06f186..41546696 100644 --- a/pom.xml +++ b/pom.xml @@ -58,7 +58,11 @@ 1.6.0 provided - + + com.esri.geometry + esri-geometry-api + 1.2 + log4j log4j @@ -93,6 +97,11 @@ 18.0 + + com.vividsolutions + jts + 1.13 + diff --git a/src/main/java/org/nlpcn/es4sql/domain/Condition.java b/src/main/java/org/nlpcn/es4sql/domain/Condition.java index 7318b76d..e68eb2e8 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/Condition.java +++ b/src/main/java/org/nlpcn/es4sql/domain/Condition.java @@ -12,7 +12,7 @@ public class Condition extends Where { public static enum OPEAR { - EQ, GT, LT, GTE, LTE, N, LIKE, NLIKE, IS, ISN, IN, NIN , BETWEEN ,NBETWEEN + EQ, GT, LT, GTE, LTE, N, LIKE, NLIKE, IS, ISN, IN, NIN , BETWEEN ,NBETWEEN , GEO_INTERSECTS }; private String name; @@ -88,6 +88,9 @@ public Condition(CONN conn, String name, String oper, Object value) throws SqlPa case "NOT BETWEEN": this.opear = OPEAR.NBETWEEN; break; + case "GEO_INTERSECTS": + this.opear = OPEAR.GEO_INTERSECTS; + break; default: throw new SqlParseException(oper + " is err!"); } diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index ff986642..166c5b7d 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -124,7 +124,20 @@ private void explanCond(String opear, SQLExpr expr, Where where) throws SqlParse SQLExpr right = ((SQLBinaryOpExpr) ((SQLNotExpr) expr).getExpr()).getRight(); Condition condition = new Condition(CONN.valueOf(opear),left, Condition.OPEAR.N, parseValue(right)); where.addWhere(condition); - } else { + } + else if (expr instanceof SQLMethodInvokeExpr) { + SQLMethodInvokeExpr methodExpr = (SQLMethodInvokeExpr) expr; + List methodParameters = methodExpr.getParameters(); + if (methodParameters.size() > 2) { + throw new SqlParseException("only support 2 parameters methods as conditions " + expr.getClass()); + } + String firstVar = methodParameters.get(0).toString(); + String secondVar = methodParameters.get(1).toString(); + String methodName = methodExpr.getMethodName(); + + Condition condition = new Condition(CONN.valueOf(opear), firstVar, methodName, secondVar); + where.addWhere(condition); + }else { throw new SqlParseException("err find condition " + expr.getClass()); } } diff --git a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java index 4b379528..e2c058b2 100644 --- a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java +++ b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java @@ -1,9 +1,17 @@ package org.nlpcn.es4sql.query.maker; +import java.io.IOException; import java.util.Set; +import com.esri.core.geometry.Geometry; +import com.esri.core.geometry.GeometryEngine; +import com.esri.core.geometry.WktImportFlags; import org.elasticsearch.common.collect.Sets; +import org.elasticsearch.common.geo.ShapeRelation; +import org.elasticsearch.common.geo.builders.ShapeBuilder; import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.query.*; import org.nlpcn.es4sql.domain.Condition; import org.nlpcn.es4sql.domain.Condition.OPEAR; @@ -26,9 +34,7 @@ protected Maker(Boolean isQuery) { /** * 构建过滤条件 * - * @param boolFilter - * @param expr - * @param expr + * @param cond * @return * @throws SqlParseException */ @@ -198,6 +204,21 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar else x = FilterBuilders.rangeFilter(name).gte(((Object[]) value)[0]).lte(((Object[]) value)[1]); break; + case GEO_INTERSECTS: + String wkt = cond.getValue().toString(); + try { + ShapeBuilder shapeBuilder = getShapeBuilderFromWkt(wkt); + if(isQuery) + x = QueryBuilders.geoShapeQuery(cond.getName(), shapeBuilder); + else + x = FilterBuilders.geoShapeFilter(cond.getName(), shapeBuilder, ShapeRelation.INTERSECTS); + + } catch (IOException e) { + e.printStackTrace(); + throw new SqlParseException("couldn't create shapeBuilder from wkt: " + wkt); + } + + break; default: throw new SqlParseException("not define type " + cond.getName()); } @@ -206,7 +227,28 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar return x; } - private ToXContent fixNot(Condition cond, ToXContent bqb) { + private ShapeBuilder getShapeBuilderFromWkt(String wkt) throws IOException { + String json = getGeoJsonFromWkt(wkt); + return getShapeBuilderFromJson(json); + } + + private ShapeBuilder getShapeBuilderFromJson(String json) throws IOException { + XContentParser parser = null; + parser = JsonXContent.jsonXContent.createParser(json); + parser.nextToken(); + return ShapeBuilder.parse(parser); + } + + private String getGeoJsonFromWkt(String wkt) { + //trims the '' from sql string rep + wkt = wkt.substring(1, wkt.length()-1); + //using esri to validate that it is parsable geometry and create geoJson from it + + com.esri.core.geometry.Geometry geometry = GeometryEngine.geometryFromWkt(wkt, WktImportFlags.wktImportDefaults, Geometry.Type.Unknown); + return GeometryEngine.geometryToGeoJson(geometry); + } + + private ToXContent fixNot(Condition cond, ToXContent bqb) { if (NOT_OPEAR_SET.contains(cond.getOpear())) { if (isQuery) { bqb = QueryBuilders.boolQuery().mustNot((QueryBuilder) bqb); diff --git a/src/test/java/org/nlpcn/es4sql/ExplainTest.java b/src/test/java/org/nlpcn/es4sql/ExplainTest.java index 14f22712..7854181c 100644 --- a/src/test/java/org/nlpcn/es4sql/ExplainTest.java +++ b/src/test/java/org/nlpcn/es4sql/ExplainTest.java @@ -36,8 +36,14 @@ public void deleteSanity() throws IOException, SqlParseException, NoSuchMethodEx assertThat(result, equalTo(expectedOutput)); } - - private String explain(String sql) throws SQLFeatureNotSupportedException, SqlParseException, InvocationTargetException, NoSuchMethodException, IllegalAccessException, IOException { + @Test + public void spatialFilterExplainTest() throws IOException, SqlParseException, NoSuchMethodException, IllegalAccessException, SQLFeatureNotSupportedException, InvocationTargetException { + String expectedOutput = Files.toString(new File("src/test/resources/expectedOutput/search_spatial_explain.json"), StandardCharsets.UTF_8); + String result = explain(String.format("SELECT * FROM %s WHERE GEO_INTERSECTS(place,'POLYGON ((102 2, 103 2, 103 3, 102 3, 102 2))')", TEST_INDEX)); + assertThat(result, equalTo(expectedOutput)); + } + + private String explain(String sql) throws SQLFeatureNotSupportedException, SqlParseException, InvocationTargetException, NoSuchMethodException, IllegalAccessException, IOException { SearchDao searchDao = MainTestSuite.getSearchDao(); ActionRequestBuilder requestBuilder = searchDao.explain(sql); return ExplainManager.explain(requestBuilder); diff --git a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java index 337924d3..0e0c68b6 100644 --- a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java +++ b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java @@ -3,14 +3,9 @@ import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse; -import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequestBuilder; -import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; -import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; import org.elasticsearch.action.bulk.BulkRequestBuilder; import org.elasticsearch.action.bulk.BulkResponse; -import org.elasticsearch.action.delete.DeleteRequestBuilder; import org.elasticsearch.action.deletebyquery.DeleteByQueryRequestBuilder; -import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.io.ByteStreams; import org.elasticsearch.common.transport.InetSocketTransportAddress; @@ -56,7 +51,14 @@ public static void setUp() throws Exception { prepareOdbcIndex(); loadBulk("src/test/resources/odbc-date-formats.json"); - searchDao = new SearchDao(client); + prepareSpatialIndex(); + loadBulk("src/test/resources/locations.json"); + + searchDao = new SearchDao(client); + + //refresh to make sure all the docs will return on queries + client.admin().indices().prepareRefresh(TEST_INDEX).execute().actionGet(); + System.out.println("Finished the setup process..."); } @@ -116,7 +118,27 @@ public static void loadBulk(String jsonPath) throws Exception { throw new Exception(String.format("Failed during bulk load of file %s. failure message: %s", jsonPath, response.buildFailureMessage())); } } + public static void prepareSpatialIndex(){ + String dataMapping = "{\n" + + "\t\"location\" :{\n" + + "\t\t\"properties\":{\n" + + "\t\t\t\"place\":{\n" + + "\t\t\t\t\"type\":\"geo_shape\",\n" + + "\t\t\t\t\"tree\": \"quadtree\",\n" + + "\t\t\t\t\"precision\": \"10km\"\n" + + "\t\t\t},\n" + + "\t\t\t\"center\":{\n" + + "\t\t\t\t\"type\":\"geo_point\"\n" + + "\t\t\t},\n" + + "\t\t\t\"description\":{\n" + + "\t\t\t\t\"type\":\"string\"\n" + + "\t\t\t}\n" + + "\t\t}\n" + + "\t}\n" + + "}"; + client.admin().indices().preparePutMapping(TEST_INDEX).setType("location").setSource(dataMapping).execute().actionGet(); + } public static void prepareOdbcIndex(){ String dataMapping = "{\n" + "\t\"odbc\" :{\n" + diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java index b8b142c3..22313a2f 100644 --- a/src/test/java/org/nlpcn/es4sql/QueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java @@ -450,6 +450,14 @@ public void testMultipartWhere3() throws IOException, SqlParseException, SQLFeat Assert.assertEquals(7, response.getTotalHits()); } + @Test + public void filterPolygonTest() throws SQLFeatureNotSupportedException, SqlParseException, InterruptedException { + SearchHits results = query(String.format("SELECT * FROM %s WHERE GEO_INTERSECTS(place,'POLYGON ((102 2, 103 2, 103 3, 102 3, 102 2))')", TEST_INDEX)); + org.junit.Assert.assertEquals(1,results.getTotalHits()); + SearchHit result = results.getAt(0); + Assert.assertEquals("bigSquare",result.getSource().get("description")); + } + private SearchHits query(String query) throws SqlParseException, SQLFeatureNotSupportedException, SQLFeatureNotSupportedException { SearchDao searchDao = MainTestSuite.getSearchDao(); SearchRequestBuilder select = (SearchRequestBuilder)searchDao.explain(query); diff --git a/src/test/resources/expectedOutput/search_spatial_explain.json b/src/test/resources/expectedOutput/search_spatial_explain.json new file mode 100644 index 00000000..ab1a8c1e --- /dev/null +++ b/src/test/resources/expectedOutput/search_spatial_explain.json @@ -0,0 +1,24 @@ +{ + "from" : 0, + "size" : 200, + "query" : { + "filtered" : { + "filter" : { + "bool" : { + "must" : { + "geo_shape" : { + "place" : { + "shape" : { + "type" : "polygon", + "coordinates" : [ [ [ 102.0, 2.0 ], [ 102.0, 3.0 ], [ 103.0, 3.0 ], [ 103.0, 2.0 ], [ 102.0, 2.0 ] ] ] + }, + "relation" : "intersects" + }, + "_name" : null + } + } + } + } + } + } +} \ No newline at end of file diff --git a/src/test/resources/locations.json b/src/test/resources/locations.json new file mode 100644 index 00000000..ebe2d777 --- /dev/null +++ b/src/test/resources/locations.json @@ -0,0 +1,4 @@ +{"index":{"_type": "location", "_id":"1"}} +{"description":"square","place":{"type": "Polygon","coordinates": [[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],[100.0, 1.0], [100.0, 0.0]]]},"center":{"lat": 0.5, "lon": 100.5 }} +{"index":{"_type": "location", "_id":"2"}} +{"description":"bigSquare","place":{"type": "Polygon","coordinates": [[ [100.0, 0.0], [110.0, 0.0], [110.0, 10.0],[100.0, 10.0], [100.0, 0.0]]]},"center":{"lat": 5.0, "lon": 105.0 }} From 729105201bf80765cb6c386b1da7afcfa910d992 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sat, 1 Aug 2015 12:05:10 +0300 Subject: [PATCH 019/559] add all dependencies to zip file (needed this to work after adding dependencies which weren't on elasticseaech lib) --- pom.xml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pom.xml b/pom.xml index 41546696..470b7144 100644 --- a/pom.xml +++ b/pom.xml @@ -129,6 +129,21 @@ + + org.apache.maven.plugins + maven-dependency-plugin + + + package + + copy-dependencies + + + ${project.build.directory} + + + + maven-assembly-plugin From 2e0b0a85e4de58bd5497c47b99a48ab3aa4feef8 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sat, 1 Aug 2015 15:50:13 +0300 Subject: [PATCH 020/559] supports boundingBox filter --- .../org/nlpcn/es4sql/domain/Condition.java | 5 ++- .../org/nlpcn/es4sql/parse/SqlParser.java | 19 ++++++------ .../org/nlpcn/es4sql/query/maker/Maker.java | 11 ++++++- .../spatial/BoundingBoxFilterParams.java | 22 +++++++++++++ .../java/org/nlpcn/es4sql/spatial/Point.java | 22 +++++++++++++ .../es4sql/spatial/SpatialParamsFactory.java | 31 +++++++++++++++++++ src/test/java/org/nlpcn/es4sql/QueryTest.java | 8 +++++ 7 files changed, 106 insertions(+), 12 deletions(-) create mode 100644 src/main/java/org/nlpcn/es4sql/spatial/BoundingBoxFilterParams.java create mode 100644 src/main/java/org/nlpcn/es4sql/spatial/Point.java create mode 100644 src/main/java/org/nlpcn/es4sql/spatial/SpatialParamsFactory.java diff --git a/src/main/java/org/nlpcn/es4sql/domain/Condition.java b/src/main/java/org/nlpcn/es4sql/domain/Condition.java index e68eb2e8..65830348 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/Condition.java +++ b/src/main/java/org/nlpcn/es4sql/domain/Condition.java @@ -12,7 +12,7 @@ public class Condition extends Where { public static enum OPEAR { - EQ, GT, LT, GTE, LTE, N, LIKE, NLIKE, IS, ISN, IN, NIN , BETWEEN ,NBETWEEN , GEO_INTERSECTS + EQ, GT, LT, GTE, LTE, N, LIKE, NLIKE, IS, ISN, IN, NIN , BETWEEN ,NBETWEEN , GEO_INTERSECTS , GEO_BOUNDING_BOX }; private String name; @@ -90,6 +90,9 @@ public Condition(CONN conn, String name, String oper, Object value) throws SqlPa break; case "GEO_INTERSECTS": this.opear = OPEAR.GEO_INTERSECTS; + break; + case "GEO_BOUNDING_BOX": + this.opear = OPEAR.GEO_BOUNDING_BOX; break; default: throw new SqlParseException(oper + " is err!"); diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index 166c5b7d..b53c7631 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -13,6 +13,7 @@ import org.durid.sql.ast.SQLOrderingSpecification; import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit; +import org.nlpcn.es4sql.spatial.SpatialParamsFactory; /** * es sql support @@ -126,17 +127,15 @@ private void explanCond(String opear, SQLExpr expr, Where where) throws SqlParse where.addWhere(condition); } else if (expr instanceof SQLMethodInvokeExpr) { - SQLMethodInvokeExpr methodExpr = (SQLMethodInvokeExpr) expr; - List methodParameters = methodExpr.getParameters(); - if (methodParameters.size() > 2) { - throw new SqlParseException("only support 2 parameters methods as conditions " + expr.getClass()); - } - String firstVar = methodParameters.get(0).toString(); - String secondVar = methodParameters.get(1).toString(); - String methodName = methodExpr.getMethodName(); + SQLMethodInvokeExpr methodExpr = (SQLMethodInvokeExpr) expr; + List methodParameters = methodExpr.getParameters(); + + String methodName = methodExpr.getMethodName(); + String fieldName = methodParameters.get(0).toString(); + Object spatialParamsObject = SpatialParamsFactory.generateSpatialParamsObject(methodName, methodParameters); - Condition condition = new Condition(CONN.valueOf(opear), firstVar, methodName, secondVar); - where.addWhere(condition); + Condition condition = new Condition(CONN.valueOf(opear), fieldName, methodName, spatialParamsObject); + where.addWhere(condition); }else { throw new SqlParseException("err find condition " + expr.getClass()); } diff --git a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java index e2c058b2..89cc07ac 100644 --- a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java +++ b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java @@ -20,6 +20,8 @@ import org.durid.sql.ast.expr.SQLIdentifierExpr; import org.durid.sql.ast.expr.SQLMethodInvokeExpr; +import org.nlpcn.es4sql.spatial.BoundingBoxFilterParams; +import org.nlpcn.es4sql.spatial.Point; public abstract class Maker { @@ -217,7 +219,14 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar e.printStackTrace(); throw new SqlParseException("couldn't create shapeBuilder from wkt: " + wkt); } - + break; + case GEO_BOUNDING_BOX: + if(isQuery) + throw new SqlParseException("Bounding box is only a filter"); + BoundingBoxFilterParams boxFilterParams = (BoundingBoxFilterParams) cond.getValue(); + Point topLeft = boxFilterParams.getTopLeft(); + Point bottomRight = boxFilterParams.getBottomRight(); + x = FilterBuilders.geoBoundingBoxFilter(cond.getName()).topLeft(topLeft.getLat(),topLeft.getLon()).bottomRight(bottomRight.getLat(),bottomRight.getLon()); break; default: throw new SqlParseException("not define type " + cond.getName()); diff --git a/src/main/java/org/nlpcn/es4sql/spatial/BoundingBoxFilterParams.java b/src/main/java/org/nlpcn/es4sql/spatial/BoundingBoxFilterParams.java new file mode 100644 index 00000000..3323c497 --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/spatial/BoundingBoxFilterParams.java @@ -0,0 +1,22 @@ +package org.nlpcn.es4sql.spatial; + +/** + * Created by Eliran on 1/8/2015. + */ +public class BoundingBoxFilterParams { + private Point topLeft; + private Point bottomRight; + + public BoundingBoxFilterParams(Point topLeft, Point bottomRight) { + this.topLeft = topLeft; + this.bottomRight = bottomRight; + } + + public Point getTopLeft() { + return topLeft; + } + + public Point getBottomRight() { + return bottomRight; + } +} diff --git a/src/main/java/org/nlpcn/es4sql/spatial/Point.java b/src/main/java/org/nlpcn/es4sql/spatial/Point.java new file mode 100644 index 00000000..3d84e010 --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/spatial/Point.java @@ -0,0 +1,22 @@ +package org.nlpcn.es4sql.spatial; + +/** + * Created by Eliran on 1/8/2015. + */ +public class Point { + private double lon; + private double lat; + + public Point(double lon, double lat) { + this.lon = lon; + this.lat = lat; + } + + public double getLon() { + return lon; + } + + public double getLat() { + return lat; + } +} diff --git a/src/main/java/org/nlpcn/es4sql/spatial/SpatialParamsFactory.java b/src/main/java/org/nlpcn/es4sql/spatial/SpatialParamsFactory.java new file mode 100644 index 00000000..9efdb2fa --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/spatial/SpatialParamsFactory.java @@ -0,0 +1,31 @@ +package org.nlpcn.es4sql.spatial; + +import org.durid.sql.ast.SQLExpr; +import org.durid.sql.parser.SQLParseException; +import org.nlpcn.es4sql.exception.SqlParseException; + +import java.util.List; + +/** + * Created by Eliran on 1/8/2015. + */ +public class SpatialParamsFactory { + public static Object generateSpatialParamsObject(String methodName,List params){ + switch(methodName){ + case "GEO_INTERSECTS": + if(params.size() != 2) + throw new SQLParseException("GEO_INTERSECTS should have exactly 2 parameters : (fieldName,'WKT') "); + return params.get(1).toString(); + case "GEO_BOUNDING_BOX": + if(params.size() != 5) + throw new SQLParseException("GEO_BOUNDING_BOX should have exactly 5 parameters : (fieldName,topLeftLon,topLeftLan,bottomRightLon,bottomRightLan) "); + double topLeftLon = Double.parseDouble(params.get(1).toString()); + double topLeftLat = Double.parseDouble(params.get(2).toString()); + double bottomRightLon = Double.parseDouble(params.get(3).toString()); + double bottomRightLat = Double.parseDouble(params.get(4).toString()); + return new BoundingBoxFilterParams(new Point(topLeftLon,topLeftLat),new Point(bottomRightLon,bottomRightLat)); + default: + throw new SQLParseException(String.format("Unknown method name: %s", methodName)); + } + } +} diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java index 22313a2f..f899f5f1 100644 --- a/src/test/java/org/nlpcn/es4sql/QueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java @@ -458,6 +458,14 @@ public void filterPolygonTest() throws SQLFeatureNotSupportedException, SqlParse Assert.assertEquals("bigSquare",result.getSource().get("description")); } + @Test + public void boundingBox() throws SQLFeatureNotSupportedException, SqlParseException, InterruptedException { + SearchHits results = query(String.format("SELECT * FROM %s WHERE GEO_BOUNDING_BOX(center,100.0,1.0,101,0.0)", TEST_INDEX)); + org.junit.Assert.assertEquals(1,results.getTotalHits()); + SearchHit result = results.getAt(0); + Assert.assertEquals("square",result.getSource().get("description")); + } + private SearchHits query(String query) throws SqlParseException, SQLFeatureNotSupportedException, SQLFeatureNotSupportedException { SearchDao searchDao = MainTestSuite.getSearchDao(); SearchRequestBuilder select = (SearchRequestBuilder)searchDao.explain(query); From 3bf2750cd73182a6be39add9fe50d3fb0640ca54 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sat, 1 Aug 2015 19:51:57 +0300 Subject: [PATCH 021/559] supports distance filter --- .../org/nlpcn/es4sql/domain/Condition.java | 5 ++++- .../org/nlpcn/es4sql/query/maker/Maker.java | 19 ++++++++++++---- .../es4sql/spatial/DistanceFilterParams.java | 22 +++++++++++++++++++ .../es4sql/spatial/SpatialParamsFactory.java | 8 ++++++- src/test/java/org/nlpcn/es4sql/QueryTest.java | 7 ++++++ 5 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 src/main/java/org/nlpcn/es4sql/spatial/DistanceFilterParams.java diff --git a/src/main/java/org/nlpcn/es4sql/domain/Condition.java b/src/main/java/org/nlpcn/es4sql/domain/Condition.java index 65830348..5536c5a5 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/Condition.java +++ b/src/main/java/org/nlpcn/es4sql/domain/Condition.java @@ -12,7 +12,7 @@ public class Condition extends Where { public static enum OPEAR { - EQ, GT, LT, GTE, LTE, N, LIKE, NLIKE, IS, ISN, IN, NIN , BETWEEN ,NBETWEEN , GEO_INTERSECTS , GEO_BOUNDING_BOX + EQ, GT, LT, GTE, LTE, N, LIKE, NLIKE, IS, ISN, IN, NIN , BETWEEN ,NBETWEEN , GEO_INTERSECTS , GEO_BOUNDING_BOX , GEO_DISTANCE }; private String name; @@ -94,6 +94,9 @@ public Condition(CONN conn, String name, String oper, Object value) throws SqlPa case "GEO_BOUNDING_BOX": this.opear = OPEAR.GEO_BOUNDING_BOX; break; + case "GEO_DISTANCE": + this.opear = OPEAR.GEO_DISTANCE; + break; default: throw new SqlParseException(oper + " is err!"); } diff --git a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java index 89cc07ac..ee6cf5a4 100644 --- a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java +++ b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java @@ -21,6 +21,7 @@ import org.durid.sql.ast.expr.SQLIdentifierExpr; import org.durid.sql.ast.expr.SQLMethodInvokeExpr; import org.nlpcn.es4sql.spatial.BoundingBoxFilterParams; +import org.nlpcn.es4sql.spatial.DistanceFilterParams; import org.nlpcn.es4sql.spatial.Point; public abstract class Maker { @@ -222,11 +223,19 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar break; case GEO_BOUNDING_BOX: if(isQuery) - throw new SqlParseException("Bounding box is only a filter"); + throw new SqlParseException("Bounding box is only for filter"); BoundingBoxFilterParams boxFilterParams = (BoundingBoxFilterParams) cond.getValue(); Point topLeft = boxFilterParams.getTopLeft(); Point bottomRight = boxFilterParams.getBottomRight(); x = FilterBuilders.geoBoundingBoxFilter(cond.getName()).topLeft(topLeft.getLat(),topLeft.getLon()).bottomRight(bottomRight.getLat(),bottomRight.getLon()); + break; + case GEO_DISTANCE: + if(isQuery) + throw new SqlParseException("Distance is only for filter"); + DistanceFilterParams distanceFilterParams = (DistanceFilterParams) cond.getValue(); + Point fromPoint = distanceFilterParams.getFrom(); + String distance = trimApostrophes(distanceFilterParams.getDistance()); + x = FilterBuilders.geoDistanceFilter(cond.getName()).distance(distance).lon(fromPoint.getLon()).lat(fromPoint.getLat()); break; default: throw new SqlParseException("not define type " + cond.getName()); @@ -249,14 +258,16 @@ private ShapeBuilder getShapeBuilderFromJson(String json) throws IOException { } private String getGeoJsonFromWkt(String wkt) { - //trims the '' from sql string rep - wkt = wkt.substring(1, wkt.length()-1); + wkt = trimApostrophes(wkt); //using esri to validate that it is parsable geometry and create geoJson from it - com.esri.core.geometry.Geometry geometry = GeometryEngine.geometryFromWkt(wkt, WktImportFlags.wktImportDefaults, Geometry.Type.Unknown); return GeometryEngine.geometryToGeoJson(geometry); } + private String trimApostrophes(String str) { + return str.substring(1, str.length()-1); + } + private ToXContent fixNot(Condition cond, ToXContent bqb) { if (NOT_OPEAR_SET.contains(cond.getOpear())) { if (isQuery) { diff --git a/src/main/java/org/nlpcn/es4sql/spatial/DistanceFilterParams.java b/src/main/java/org/nlpcn/es4sql/spatial/DistanceFilterParams.java new file mode 100644 index 00000000..177acc30 --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/spatial/DistanceFilterParams.java @@ -0,0 +1,22 @@ +package org.nlpcn.es4sql.spatial; + +/** + * Created by Eliran on 1/8/2015. + */ +public class DistanceFilterParams { + private String distance; + private Point from; + + public DistanceFilterParams(String distance, Point from) { + this.distance = distance; + this.from = from; + } + + public String getDistance() { + return distance; + } + + public Point getFrom() { + return from; + } +} diff --git a/src/main/java/org/nlpcn/es4sql/spatial/SpatialParamsFactory.java b/src/main/java/org/nlpcn/es4sql/spatial/SpatialParamsFactory.java index 9efdb2fa..44604e1c 100644 --- a/src/main/java/org/nlpcn/es4sql/spatial/SpatialParamsFactory.java +++ b/src/main/java/org/nlpcn/es4sql/spatial/SpatialParamsFactory.java @@ -2,7 +2,6 @@ import org.durid.sql.ast.SQLExpr; import org.durid.sql.parser.SQLParseException; -import org.nlpcn.es4sql.exception.SqlParseException; import java.util.List; @@ -24,6 +23,13 @@ public static Object generateSpatialParamsObject(String methodName,List double bottomRightLon = Double.parseDouble(params.get(3).toString()); double bottomRightLat = Double.parseDouble(params.get(4).toString()); return new BoundingBoxFilterParams(new Point(topLeftLon,topLeftLat),new Point(bottomRightLon,bottomRightLat)); + case "GEO_DISTANCE": + if(params.size()!=4) + throw new SQLParseException("GEO_DISTANCE should have exactly 5 parameters : (fieldName,distance,fromLon,fromLat) "); + String distance = params.get(1).toString(); + double lon = Double.parseDouble(params.get(2).toString()); + double lat = Double.parseDouble(params.get(3).toString()); + return new DistanceFilterParams(distance ,new Point(lon,lat)); default: throw new SQLParseException(String.format("Unknown method name: %s", methodName)); } diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java index f899f5f1..302b9800 100644 --- a/src/test/java/org/nlpcn/es4sql/QueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java @@ -465,6 +465,13 @@ public void boundingBox() throws SQLFeatureNotSupportedException, SqlParseExcept SearchHit result = results.getAt(0); Assert.assertEquals("square",result.getSource().get("description")); } + @Test + public void geoDistance() throws SQLFeatureNotSupportedException, SqlParseException, InterruptedException { + SearchHits results = query(String.format("SELECT * FROM %s WHERE GEO_DISTANCE(center,'1km',100.5,0.500001)", TEST_INDEX)); + org.junit.Assert.assertEquals(1,results.getTotalHits()); + SearchHit result = results.getAt(0); + Assert.assertEquals("square",result.getSource().get("description")); + } private SearchHits query(String query) throws SqlParseException, SQLFeatureNotSupportedException, SQLFeatureNotSupportedException { SearchDao searchDao = MainTestSuite.getSearchDao(); From c6df29305276b64e65a41a6f68595c530d9bb08b Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sat, 1 Aug 2015 23:49:09 +0300 Subject: [PATCH 022/559] geohash_grid agg support --- .../nlpcn/es4sql/query/maker/AggMaker.java | 32 +++++++++++++++++-- .../org/nlpcn/es4sql/AggregationTest.java | 14 +++++++- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java b/src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java index 8e4fddc4..7d5891d1 100644 --- a/src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java +++ b/src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java @@ -10,6 +10,7 @@ import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.ValuesSourceAggregationBuilder; +import org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGridBuilder; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogram; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramBuilder; import org.elasticsearch.search.aggregations.bucket.histogram.Histogram; @@ -77,7 +78,7 @@ public AbstractAggregationBuilder makeFieldAgg(MethodField field, AbstractAggreg } } - private ValuesSourceAggregationBuilder makeRangeGroup(MethodField field) throws SqlParseException { + private AggregationBuilder makeRangeGroup(MethodField field) throws SqlParseException { switch (field.getName().toLowerCase()) { case "range": return rangeBuilder(field); @@ -89,13 +90,40 @@ private ValuesSourceAggregationBuilder makeRangeGroup(MethodField field) thro return dateRange(field); case "histogram": return histogram(field); + case "geohash_grid": + return geohashGrid(field); default: throw new SqlParseException("can define this method " + field); } } - private static final String TIME_FARMAT = "yyyy-MM-dd HH:mm:ss"; + private AggregationBuilder geohashGrid(MethodField field) throws SqlParseException { + GeoHashGridBuilder geoHashGrid = AggregationBuilders.geohashGrid(field.getAlias()); + String value = null; + for (KVValue kv : field.getParams()) { + value = kv.value.toString(); + switch (kv.key.toLowerCase()) { + case "precision": + geoHashGrid.precision(Integer.parseInt(value)); + break; + case "field": + geoHashGrid.field(value); + break; + case "size": + geoHashGrid.size(Integer.parseInt(value)); + break; + case "shard_size": + geoHashGrid.shardSize(Integer.parseInt(value)); + break; + default: + throw new SqlParseException("geohash grid err or not define field " + kv.toString()); + } + } + return geoHashGrid; + } + + private static final String TIME_FARMAT = "yyyy-MM-dd HH:mm:ss"; private ValuesSourceAggregationBuilder dateRange(MethodField field) { DateRangeBuilder dateRange = AggregationBuilders.dateRange(field.getAlias()).format(TIME_FARMAT); diff --git a/src/test/java/org/nlpcn/es4sql/AggregationTest.java b/src/test/java/org/nlpcn/es4sql/AggregationTest.java index 4a726714..d5d621c1 100644 --- a/src/test/java/org/nlpcn/es4sql/AggregationTest.java +++ b/src/test/java/org/nlpcn/es4sql/AggregationTest.java @@ -5,7 +5,10 @@ import com.google.common.collect.DiscreteDomain; import com.google.common.collect.Range; import org.elasticsearch.action.search.SearchRequestBuilder; +import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.Aggregations; +import org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid; +import org.elasticsearch.search.aggregations.bucket.geogrid.InternalGeoHashGrid; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.metrics.avg.Avg; import org.elasticsearch.search.aggregations.metrics.max.Max; @@ -136,7 +139,16 @@ public void orderByAscTest() throws IOException, SqlParseException, SQLFeatureNo Assert.assertTrue("The list is not ordered ascending", agesCount.equals(agesCount)); } - + @Test + public void geoHashGrid() throws SQLFeatureNotSupportedException, SqlParseException { + Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/location GROUP BY geohash_grid(field='center',precision=5) ", TEST_INDEX)); + InternalGeoHashGrid grid = result.get("geohash_grid(field=center,precision=5)"); + Collection buckets = grid.getBuckets(); + for (GeoHashGrid.Bucket bucket : buckets) { + Assert.assertTrue(bucket.getKey().equals("w2fsm") || bucket.getKey().equals("w0p6y") ); + Assert.assertEquals(1,bucket.getDocCount()); + } + } @Test public void orderByDescTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { ArrayList agesCount = new ArrayList<>(); From d7f9f92b13aaa72e3b6821262bc16b4e85343b79 Mon Sep 17 00:00:00 2001 From: Caleb Ott Date: Mon, 3 Aug 2015 09:18:01 -0500 Subject: [PATCH 023/559] Added support for group by (aggregate) queries to return actual results as well. This way you can run a query and add on aggregations as well to fully utilize the power of elastic through this plugin. --- .../es4sql/query/AggregationQueryAction.java | 62 ++++++++++++++----- .../expectedOutput/search_explain.json | 3 +- 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/nlpcn/es4sql/query/AggregationQueryAction.java b/src/main/java/org/nlpcn/es4sql/query/AggregationQueryAction.java index 5a4ab8aa..bdbc2dc8 100644 --- a/src/main/java/org/nlpcn/es4sql/query/AggregationQueryAction.java +++ b/src/main/java/org/nlpcn/es4sql/query/AggregationQueryAction.java @@ -1,5 +1,6 @@ package org.nlpcn.es4sql.query; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -12,6 +13,7 @@ import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder; +import org.elasticsearch.search.sort.SortOrder; import org.nlpcn.es4sql.domain.Field; import org.nlpcn.es4sql.domain.KVValue; import org.nlpcn.es4sql.domain.MethodField; @@ -72,32 +74,36 @@ public SearchRequestBuilder explain() throws SqlParseException { Map groupMap = aggMaker.getGroupMap(); // add field if (select.getFields().size() > 0) { + setFields(select.getFields()); explanFields(request, select.getFields(), lastAgg); } // add order if (lastAgg != null && select.getOrderBys().size() > 0) { - KVValue temp = null; - TermsBuilder termsBuilder = null; for (Order order : select.getOrderBys()) { - temp = groupMap.get(order.getName()); - termsBuilder = (TermsBuilder) temp.value; - switch (temp.key) { - case "COUNT": - termsBuilder.order(Terms.Order.count(isASC(order))); - break; - case "KEY": - termsBuilder.order(Terms.Order.term(isASC(order))); - break; - case "FIELD": - termsBuilder.order(Terms.Order.aggregation(order.getName(), isASC(order))); - break; - default: - throw new SqlParseException(order.getName() + " can not to order"); + KVValue temp = groupMap.get(order.getName()); + if (temp != null) { + TermsBuilder termsBuilder = (TermsBuilder) temp.value; + switch (temp.key) { + case "COUNT": + termsBuilder.order(Terms.Order.count(isASC(order))); + break; + case "KEY": + termsBuilder.order(Terms.Order.term(isASC(order))); + break; + case "FIELD": + termsBuilder.order(Terms.Order.aggregation(order.getName(), isASC(order))); + break; + default: + throw new SqlParseException(order.getName() + " can not to order"); + } + } else { + request.addSort(order.getName(), SortOrder.valueOf(order.getType())); } } } - request.setSize(0); + setLimit(select.getOffset(), select.getRowCount()); + request.setSearchType(SearchType.DEFAULT); return request; } @@ -106,6 +112,20 @@ private boolean isASC(Order order) { return "ASC".equals(order.getType()); } + private void setFields(List fields) { + if (select.getFields().size() > 0) { + ArrayList includeFields = new ArrayList<>(); + + for (Field field : fields) { + if (field != null) { + includeFields.add(field.getName()); + } + } + + request.setFetchSource(includeFields.toArray(new String[includeFields.size()]), null); + } + } + private void explanFields(SearchRequestBuilder request, List fields, AggregationBuilder groupByAgg) throws SqlParseException { for (Field field : fields) { if (field instanceof MethodField) { @@ -149,4 +169,12 @@ private void setIndicesAndTypes() { request.setTypes(typeArr); } } + + private void setLimit(int from, int size) { + request.setFrom(from); + + if (size > -1) { + request.setSize(size); + } + } } diff --git a/src/test/resources/expectedOutput/search_explain.json b/src/test/resources/expectedOutput/search_explain.json index 1d69a40b..189b4176 100644 --- a/src/test/resources/expectedOutput/search_explain.json +++ b/src/test/resources/expectedOutput/search_explain.json @@ -1,5 +1,6 @@ { - "size" : 0, + "from" : 0, + "size" : 200, "query" : { "filtered" : { "filter" : { From 9870efd7ebe951395f15e2a42e55365533572460 Mon Sep 17 00:00:00 2001 From: Caleb Ott Date: Mon, 3 Aug 2015 09:24:59 -0500 Subject: [PATCH 024/559] Expanded the sub aggregations test to ensure results are returned. --- .../java/org/nlpcn/es4sql/AggregationTest.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/nlpcn/es4sql/AggregationTest.java b/src/test/java/org/nlpcn/es4sql/AggregationTest.java index 4a726714..52f721fa 100644 --- a/src/test/java/org/nlpcn/es4sql/AggregationTest.java +++ b/src/test/java/org/nlpcn/es4sql/AggregationTest.java @@ -5,6 +5,7 @@ import com.google.common.collect.DiscreteDomain; import com.google.common.collect.Range; import org.elasticsearch.action.search.SearchRequestBuilder; +import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.metrics.avg.Avg; @@ -226,11 +227,16 @@ private Aggregations query(String query) throws SqlParseException, SQLFeatureNot @Test public void testSubAggregations() throws Exception { - Set expectedAges = new HashSet(ContiguousSet.create(Range.closed(20, 40), DiscreteDomain.integers())); + Set expectedAges = new HashSet<>(ContiguousSet.create(Range.closed(20, 40), DiscreteDomain.integers())); + final String query = String.format("SELECT * FROM %s/account GROUP BY (gender, age), (state) LIMIT 0,10", TEST_INDEX); Map> buckets = new HashMap<>(); - Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY (gender, age), (state)", TEST_INDEX)); + SearchDao searchDao = MainTestSuite.getSearchDao(); + SearchRequestBuilder select = (SearchRequestBuilder) searchDao.explain(query); + SearchResponse response = select.get(); + Aggregations result = response.getAggregations(); + Terms gender = result.get("gender"); for(Terms.Bucket genderBucket : gender.getBuckets()) { String genderKey = genderBucket.getKey(); @@ -250,8 +256,10 @@ public void testSubAggregations() throws Exception { if(stateBucket.getKey().equalsIgnoreCase("ak")) { Assert.assertTrue("There are 22 entries for state ak", stateBucket.getDocCount() == 22); } - } + + Assert.assertEquals(response.getHits().totalHits(), 1000); + Assert.assertEquals(response.getHits().hits().length, 10); } } From 405b463d2895d438c7dd530ca7abded2f49449c9 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Thu, 6 Aug 2015 01:07:07 +0300 Subject: [PATCH 025/559] remove esri dependency and write wktConverter (only point and polygon supported now) --- pom.xml | 5 -- .../org/nlpcn/es4sql/query/maker/Maker.java | 13 +-- .../es4sql/spatial/WktToGeoJsonConverter.java | 82 +++++++++++++++++++ .../java/org/nlpcn/es4sql/MainTestSuite.java | 3 +- .../es4sql/WktToGeoJsonConverterTests.java | 70 ++++++++++++++++ .../search_spatial_explain.json | 2 +- 6 files changed, 157 insertions(+), 18 deletions(-) create mode 100644 src/main/java/org/nlpcn/es4sql/spatial/WktToGeoJsonConverter.java create mode 100644 src/test/java/org/nlpcn/es4sql/WktToGeoJsonConverterTests.java diff --git a/pom.xml b/pom.xml index 470b7144..75b86871 100644 --- a/pom.xml +++ b/pom.xml @@ -58,11 +58,6 @@ 1.6.0 provided - - com.esri.geometry - esri-geometry-api - 1.2 - log4j log4j diff --git a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java index ee6cf5a4..6546f82f 100644 --- a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java +++ b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java @@ -3,9 +3,6 @@ import java.io.IOException; import java.util.Set; -import com.esri.core.geometry.Geometry; -import com.esri.core.geometry.GeometryEngine; -import com.esri.core.geometry.WktImportFlags; import org.elasticsearch.common.collect.Sets; import org.elasticsearch.common.geo.ShapeRelation; import org.elasticsearch.common.geo.builders.ShapeBuilder; @@ -23,6 +20,7 @@ import org.nlpcn.es4sql.spatial.BoundingBoxFilterParams; import org.nlpcn.es4sql.spatial.DistanceFilterParams; import org.nlpcn.es4sql.spatial.Point; +import org.nlpcn.es4sql.spatial.WktToGeoJsonConverter; public abstract class Maker { @@ -246,7 +244,7 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar } private ShapeBuilder getShapeBuilderFromWkt(String wkt) throws IOException { - String json = getGeoJsonFromWkt(wkt); + String json = WktToGeoJsonConverter.toGeoJson(trimApostrophes(wkt)); return getShapeBuilderFromJson(json); } @@ -257,13 +255,6 @@ private ShapeBuilder getShapeBuilderFromJson(String json) throws IOException { return ShapeBuilder.parse(parser); } - private String getGeoJsonFromWkt(String wkt) { - wkt = trimApostrophes(wkt); - //using esri to validate that it is parsable geometry and create geoJson from it - com.esri.core.geometry.Geometry geometry = GeometryEngine.geometryFromWkt(wkt, WktImportFlags.wktImportDefaults, Geometry.Type.Unknown); - return GeometryEngine.geometryToGeoJson(geometry); - } - private String trimApostrophes(String str) { return str.substring(1, str.length()-1); } diff --git a/src/main/java/org/nlpcn/es4sql/spatial/WktToGeoJsonConverter.java b/src/main/java/org/nlpcn/es4sql/spatial/WktToGeoJsonConverter.java new file mode 100644 index 00000000..31ba6128 --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/spatial/WktToGeoJsonConverter.java @@ -0,0 +1,82 @@ +package org.nlpcn.es4sql.spatial; + +import com.sun.javaws.exceptions.InvalidArgumentException; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Eliran on 4/8/2015. + */ +public class WktToGeoJsonConverter { + public static String toGeoJson(String wkt){ + wkt = wkt.toLowerCase(); + //need to check if indexof( !=-1 + int afterType = wkt.indexOf("("); + if(afterType == -1) + throw new IllegalArgumentException("not valid wkt"); + + String wktType = wkt.substring(0, afterType).trim(); + wkt = wkt.substring(afterType); + + String type=""; + String coordinates=""; + switch (wktType){ + case("point"): + type = "Point"; + coordinates = pointCoordinatesFromWkt(wkt); + break; + case("polygon"): + type = "Polygon"; + coordinates = polygonCoordinatesFromWkt(wkt); + break; + default: + throw new IllegalArgumentException("not supported wkt type"); + + } + + return buildGeoJson(type,coordinates); + } + + /* currently suppoted this format: + * POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10)) + * need to support this format too: POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10),(20 30, 35 35, 30 20, 20 30)) + */ + private static String polygonCoordinatesFromWkt(String wkt) { + wkt = removeBrackets(wkt,2); + String[] points = wkt.split(","); + List coordinates = new ArrayList<>(); + for(String point : points){ + coordinates.add(extractCoordinateFromPoint(point)); + } + String joinedCoordinates = String.join(",", coordinates); + return String.format("[[%s]]", joinedCoordinates); + } + + private static String buildGeoJson(String type, String coordinates) { + return String.format("{\"type\":\"%s\", \"coordinates\": %s}", type, coordinates); + } + + public static String pointCoordinatesFromWkt(String wkt) { + wkt = removeBrackets(wkt,1); + return extractCoordinateFromPoint(wkt); + } + + private static String extractCoordinateFromPoint(String point) { + String goodPattern = "(\\s*)([0-9\\.]+)(\\s*)([0-9\\.]+)(\\s*)"; + return point.replaceAll(goodPattern,"[$2,$4]"); + } + + private static String removeBrackets(String wkt, int num) { + String result= wkt; + for(int i=0;i Date: Fri, 7 Aug 2015 09:30:57 +0300 Subject: [PATCH 026/559] support polygon with holes --- .../es4sql/spatial/WktToGeoJsonConverter.java | 41 +++++++++++++------ .../es4sql/WktToGeoJsonConverterTests.java | 16 ++++++++ 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/nlpcn/es4sql/spatial/WktToGeoJsonConverter.java b/src/main/java/org/nlpcn/es4sql/spatial/WktToGeoJsonConverter.java index 31ba6128..ed74eb67 100644 --- a/src/main/java/org/nlpcn/es4sql/spatial/WktToGeoJsonConverter.java +++ b/src/main/java/org/nlpcn/es4sql/spatial/WktToGeoJsonConverter.java @@ -11,13 +11,12 @@ public class WktToGeoJsonConverter { public static String toGeoJson(String wkt){ wkt = wkt.toLowerCase(); - //need to check if indexof( !=-1 - int afterType = wkt.indexOf("("); - if(afterType == -1) + int startOfCoordinates = wkt.indexOf("("); + if(startOfCoordinates == -1) throw new IllegalArgumentException("not valid wkt"); - String wktType = wkt.substring(0, afterType).trim(); - wkt = wkt.substring(afterType); + String wktType = wkt.substring(0, startOfCoordinates).trim(); + wkt = wkt.substring(startOfCoordinates); String type=""; String coordinates=""; @@ -38,19 +37,35 @@ public static String toGeoJson(String wkt){ return buildGeoJson(type,coordinates); } - /* currently suppoted this format: - * POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10)) - * need to support this format too: POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10),(20 30, 35 35, 30 20, 20 30)) - */ + private static String polygonCoordinatesFromWkt(String wkt) { wkt = removeBrackets(wkt,2); - String[] points = wkt.split(","); + String coordinates; + //if polygon contains inner hole + boolean polygonContainsInnerHoles = wkt.contains("("); + if(polygonContainsInnerHoles) { + String[] polygons = wkt.split("\\s*\\)\\s*,\\s*\\(\\s*"); + String[] coordinatesOfPolygons = new String[polygons.length]; + for (int i = 0; i < polygons.length; i++) { + String polygonCoordinates = getJsonArrayFromListOfPoints(polygons[i]); + coordinatesOfPolygons[i] = polygonCoordinates; + } + coordinates = String.join(",",coordinatesOfPolygons); + } + else { + coordinates = getJsonArrayFromListOfPoints(wkt); + } + return String.format("[%s]", coordinates); + } + + private static String getJsonArrayFromListOfPoints(String pointsInWkt) { + String[] points = pointsInWkt.split(","); List coordinates = new ArrayList<>(); for(String point : points){ coordinates.add(extractCoordinateFromPoint(point)); } String joinedCoordinates = String.join(",", coordinates); - return String.format("[[%s]]", joinedCoordinates); + return String.format("[%s]", joinedCoordinates); } private static String buildGeoJson(String type, String coordinates) { @@ -63,8 +78,8 @@ public static String pointCoordinatesFromWkt(String wkt) { } private static String extractCoordinateFromPoint(String point) { - String goodPattern = "(\\s*)([0-9\\.]+)(\\s*)([0-9\\.]+)(\\s*)"; - return point.replaceAll(goodPattern,"[$2,$4]"); + String pointPattern = "(\\s*)([0-9\\.]+)(\\s*)([0-9\\.]+)(\\s*)"; + return point.replaceAll(pointPattern,"[$2,$4]"); } private static String removeBrackets(String wkt, int num) { diff --git a/src/test/java/org/nlpcn/es4sql/WktToGeoJsonConverterTests.java b/src/test/java/org/nlpcn/es4sql/WktToGeoJsonConverterTests.java index de24e025..f7db063f 100644 --- a/src/test/java/org/nlpcn/es4sql/WktToGeoJsonConverterTests.java +++ b/src/test/java/org/nlpcn/es4sql/WktToGeoJsonConverterTests.java @@ -66,5 +66,21 @@ public void convertPolygon_WithRedundantSpaces_ShouldConvert(){ Assert.assertEquals(expectedGeoJson,geoJson); } + @Test + public void convertPolygonWithHole_NoRedundantSpaces_ShouldConvert(){ + String wkt = "POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10),(20 30, 35 35, 30 20, 20 30))"; + String geoJson = WktToGeoJsonConverter.toGeoJson(wkt); + String expectedGeoJson = "{\"type\":\"Polygon\", \"coordinates\": [[[35,10],[45,45],[15,40],[10,20],[35,10]],[[20,30],[35,35],[30,20],[20,30]]]}"; + Assert.assertEquals(expectedGeoJson,geoJson); + } + + @Test + public void convertPolygonWithHole_WithRedundantSpaces_ShouldConvert(){ + String wkt = "POLYGON ( (35 10, 45 45, 15 40, 10 20, 35 10 ), (20 30 , 35 35, 30 20, 20 30 ) ) "; + String geoJson = WktToGeoJsonConverter.toGeoJson(wkt); + String expectedGeoJson = "{\"type\":\"Polygon\", \"coordinates\": [[[35,10],[45,45],[15,40],[10,20],[35,10]],[[20,30],[35,35],[30,20],[20,30]]]}"; + Assert.assertEquals(expectedGeoJson,geoJson); + } + } From 71c52a86d499642eb5ccb7d839b1eb45ee89f30b Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Fri, 7 Aug 2015 10:00:03 +0300 Subject: [PATCH 027/559] wktConverters lineString and multipolygon support --- .../es4sql/spatial/WktToGeoJsonConverter.java | 31 ++++++++++++++-- .../es4sql/WktToGeoJsonConverterTests.java | 36 +++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/nlpcn/es4sql/spatial/WktToGeoJsonConverter.java b/src/main/java/org/nlpcn/es4sql/spatial/WktToGeoJsonConverter.java index ed74eb67..5304eef3 100644 --- a/src/main/java/org/nlpcn/es4sql/spatial/WktToGeoJsonConverter.java +++ b/src/main/java/org/nlpcn/es4sql/spatial/WktToGeoJsonConverter.java @@ -29,6 +29,14 @@ public static String toGeoJson(String wkt){ type = "Polygon"; coordinates = polygonCoordinatesFromWkt(wkt); break; + case("linestring"): + type = "LineString"; + coordinates = lineStringCoordinatesFromWkt(wkt); + break; + case("multipolygon"): + type = "MultiPolygon"; + coordinates = multiPolygonCoordinatesFromWkt(wkt); + break; default: throw new IllegalArgumentException("not supported wkt type"); @@ -37,11 +45,30 @@ public static String toGeoJson(String wkt){ return buildGeoJson(type,coordinates); } + //input (((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5))) + private static String multiPolygonCoordinatesFromWkt(String wkt) { + wkt = removeBrackets(wkt,1); + String polygonsWithPipeSeparator = wkt.replaceAll("\\s*\\)\\s*\\)\\s*,\\s*\\(\\s*\\(\\s*","))|(("); + String[] polygons = polygonsWithPipeSeparator.split("\\|"); + String[] polygonsCoordinates = new String[polygons.length]; + for (int i=0;i Date: Fri, 7 Aug 2015 10:36:46 +0300 Subject: [PATCH 028/559] multiPoint and multiLineString support --- .../es4sql/spatial/WktToGeoJsonConverter.java | 35 +++++++++++++++++++ .../es4sql/WktToGeoJsonConverterTests.java | 32 +++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/src/main/java/org/nlpcn/es4sql/spatial/WktToGeoJsonConverter.java b/src/main/java/org/nlpcn/es4sql/spatial/WktToGeoJsonConverter.java index 5304eef3..6b99342c 100644 --- a/src/main/java/org/nlpcn/es4sql/spatial/WktToGeoJsonConverter.java +++ b/src/main/java/org/nlpcn/es4sql/spatial/WktToGeoJsonConverter.java @@ -37,6 +37,14 @@ public static String toGeoJson(String wkt){ type = "MultiPolygon"; coordinates = multiPolygonCoordinatesFromWkt(wkt); break; + case("multipoint"): + type = "MultiPoint"; + coordinates = multiPointCoordinatesFromWkt(wkt); + break; + case("multilinestring"): + type = "MultiLineString"; + coordinates = multiLineStringCoordinatesFromWkt(wkt); + break; default: throw new IllegalArgumentException("not supported wkt type"); @@ -44,6 +52,33 @@ public static String toGeoJson(String wkt){ return buildGeoJson(type,coordinates); } + //input: ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10)) + private static String multiLineStringCoordinatesFromWkt(String wkt) { + wkt = removeBrackets(wkt,1); + String lineStringsWithPipeSeparator = wkt.replaceAll("\\s*\\)\\s*,\\s*\\(",")|("); + String[] lineStrings = lineStringsWithPipeSeparator.split("\\|"); + String[] coordinates = new String[lineStrings.length]; + for (int i=0;i 10 40, 40 30, 20 20 + wkt = wkt.replaceAll("\\(|\\)" ,""); + } + coordinates = getJsonArrayFromListOfPoints(wkt); + return coordinates; + } //input (((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5))) private static String multiPolygonCoordinatesFromWkt(String wkt) { diff --git a/src/test/java/org/nlpcn/es4sql/WktToGeoJsonConverterTests.java b/src/test/java/org/nlpcn/es4sql/WktToGeoJsonConverterTests.java index b6980194..63a2b39a 100644 --- a/src/test/java/org/nlpcn/es4sql/WktToGeoJsonConverterTests.java +++ b/src/test/java/org/nlpcn/es4sql/WktToGeoJsonConverterTests.java @@ -89,6 +89,7 @@ public void convertLineString_NoRedundantSpaces_ShouldConvert(){ String expectedGeoJson = "{\"type\":\"LineString\", \"coordinates\": [[30,10],[10,30],[40,40]]}"; Assert.assertEquals(expectedGeoJson,geoJson); } + @Test public void convertLineString_WithRedundantSpaces_ShouldConvert(){ String wkt = "LINESTRING ( 30 10, 10 30 , 40 40 )"; @@ -118,5 +119,36 @@ public void convertMultiPolygon_OnePolygonHaveHoles_ShouldConvert(){ String expectedGeoJson = "{\"type\":\"MultiPolygon\", \"coordinates\": [[[[30,20],[45,40],[10,40],[30,20]],[[20,30],[35,35],[30,20],[20,30]]],[[[15,5],[40,10],[10,20],[5,10],[15,5]]]]}"; Assert.assertEquals(expectedGeoJson,geoJson); } + + @Test + public void convertMultiPoint_V1_ShouldConvert(){ + String wkt = "MULTIPOINT (10 40, 40 30, 20 20, 30 10)"; + String geoJson = WktToGeoJsonConverter.toGeoJson(wkt); + String expectedGeoJson = "{\"type\":\"MultiPoint\", \"coordinates\": [[10,40],[40,30],[20,20],[30,10]]}"; + Assert.assertEquals(expectedGeoJson,geoJson); + } + + @Test + public void convertMultiPoint_V2_ShouldConvert(){ + String wkt = "MULTIPOINT ((10 40), (40 30), (20 20), (30 10))"; + String geoJson = WktToGeoJsonConverter.toGeoJson(wkt); + String expectedGeoJson = "{\"type\":\"MultiPoint\", \"coordinates\": [[10,40],[40,30],[20,20],[30,10]]}"; + Assert.assertEquals(expectedGeoJson,geoJson); + } + + @Test + public void convertMultiLineString_NoRedundantSpaces_ShouldConvert(){ + String wkt = "MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))"; + String geoJson = WktToGeoJsonConverter.toGeoJson(wkt); + String expectedGeoJson = "{\"type\":\"MultiLineString\", \"coordinates\": [[[10,10],[20,20],[10,40]],[[40,40],[30,30],[40,20],[30,10]]]}"; + Assert.assertEquals(expectedGeoJson,geoJson); + } + @Test + public void convertMultiLineString_WithRedundantSpaces_ShouldConvert(){ + String wkt = "MULTILINESTRING ( (10 10, 20 20, 10 40 ) , (40 40, 30 30, 40 20, 30 10))"; + String geoJson = WktToGeoJsonConverter.toGeoJson(wkt); + String expectedGeoJson = "{\"type\":\"MultiLineString\", \"coordinates\": [[[10,10],[20,20],[10,40]],[[40,40],[30,30],[40,20],[30,10]]]}"; + Assert.assertEquals(expectedGeoJson,geoJson); + } } From 890fcea11f003b72b97d28af3174a850cc5fe549 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Fri, 7 Aug 2015 13:42:58 +0300 Subject: [PATCH 029/559] remove unused dependencies --- pom.xml | 227 ++++++++---------- .../es4sql/spatial/WktToGeoJsonConverter.java | 3 - 2 files changed, 104 insertions(+), 126 deletions(-) diff --git a/pom.xml b/pom.xml index 75b86871..8117989c 100644 --- a/pom.xml +++ b/pom.xml @@ -1,76 +1,77 @@ - 4.0.0 - org.nlpcn - elasticsearch-sql - 1.3.4 - jar - Query elasticsearch using SQL - elasticsearch-sql - https://github.com/NLPchina/elasticsearch-sql/ - - - The Apache Software License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt - repo - - - - - - ansj - ansj - ansj-sun@163.com - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + org.nlpcn + elasticsearch-sql + 1.3.4 + jar + Query elasticsearch using SQL + elasticsearch-sql + https://github.com/NLPchina/elasticsearch-sql/ + + + The Apache Software License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + + + ansj + ansj + ansj-sun@163.com + omershelef Omer shelef shlaflaf@gmail.com - + - - - mvn-repo - scpexe://ansj/home/mvn-repo/ROOT - - + + + mvn-repo + scpexe://ansj/home/mvn-repo/ROOT + + - - - mvn-repo - http://maven.nlpcn.org/ - - + + + mvn-repo + http://maven.nlpcn.org/ + + - - UTF-8 + + UTF-8 **/MainTestSuite.class - - - - - org.elasticsearch - elasticsearch - 1.6.0 - provided - - - log4j - log4j - 1.2.16 - provided - - - - com.alibaba - fastjson - 1.1.41 - test - + + + + + org.elasticsearch + elasticsearch + 1.6.0 + provided + + + + log4j + log4j + 1.2.16 + provided + + + + com.alibaba + fastjson + 1.1.41 + test + org.hamcrest @@ -79,12 +80,12 @@ test - - junit - junit - 4.11 - test - + + junit + junit + 4.11 + test + com.google.guava @@ -92,64 +93,44 @@ 18.0 - - com.vividsolutions - jts - 1.13 - - - - - - - - maven-compiler-plugin - 2.3.2 - - 1.7 - 1.7 - UTF-8 - - - - - maven-jar-plugin - - - - jar - - package - - - + + + + - org.apache.maven.plugins - maven-dependency-plugin + maven-compiler-plugin + 2.3.2 + + 1.7 + 1.7 + UTF-8 + + + + + maven-jar-plugin - package - copy-dependencies + jar - - ${project.build.directory} - + package - - maven-assembly-plugin - 2.4.1 - - all - - jar-with-dependencies - - - + + + maven-assembly-plugin + 2.4.1 + + all + + jar-with-dependencies + + + @@ -173,14 +154,14 @@ - - - org.apache.maven.wagon - wagon-ssh-external - 1.0-beta-6 - - + + + org.apache.maven.wagon + wagon-ssh-external + 1.0-beta-6 + + - + - + \ No newline at end of file diff --git a/src/main/java/org/nlpcn/es4sql/spatial/WktToGeoJsonConverter.java b/src/main/java/org/nlpcn/es4sql/spatial/WktToGeoJsonConverter.java index 6b99342c..d0267a44 100644 --- a/src/main/java/org/nlpcn/es4sql/spatial/WktToGeoJsonConverter.java +++ b/src/main/java/org/nlpcn/es4sql/spatial/WktToGeoJsonConverter.java @@ -1,7 +1,4 @@ package org.nlpcn.es4sql.spatial; - -import com.sun.javaws.exceptions.InvalidArgumentException; - import java.util.ArrayList; import java.util.List; From 496a53c02e0a55ccd734a40e479104f89cc74fd5 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Fri, 7 Aug 2015 19:49:05 +0300 Subject: [PATCH 030/559] needed for ShapeBuilder --- pom.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pom.xml b/pom.xml index 8117989c..f9bdad3b 100644 --- a/pom.xml +++ b/pom.xml @@ -93,6 +93,11 @@ 18.0 + + com.vividsolutions + jts + 1.13 + From f52c23a3d2073d9e7eaf5abfe5e10b98b29f2be5 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sat, 8 Aug 2015 18:47:38 +0300 Subject: [PATCH 031/559] add back geohash test --- .../java/org/nlpcn/es4sql/AggregationTest.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/test/java/org/nlpcn/es4sql/AggregationTest.java b/src/test/java/org/nlpcn/es4sql/AggregationTest.java index 52f721fa..88f046b8 100644 --- a/src/test/java/org/nlpcn/es4sql/AggregationTest.java +++ b/src/test/java/org/nlpcn/es4sql/AggregationTest.java @@ -7,6 +7,8 @@ import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.search.aggregations.Aggregations; +import org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid; +import org.elasticsearch.search.aggregations.bucket.geogrid.InternalGeoHashGrid; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.metrics.avg.Avg; import org.elasticsearch.search.aggregations.metrics.max.Max; @@ -262,4 +264,16 @@ public void testSubAggregations() throws Exception { Assert.assertEquals(response.getHits().hits().length, 10); } + + @Test + public void geoHashGrid() throws SQLFeatureNotSupportedException, SqlParseException { + Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/location GROUP BY geohash_grid(field='center',precision=5) ", TEST_INDEX)); + InternalGeoHashGrid grid = result.get("geohash_grid(field=center,precision=5)"); + Collection buckets = grid.getBuckets(); + for (GeoHashGrid.Bucket bucket : buckets) { + Assert.assertTrue(bucket.getKey().equals("w2fsm") || bucket.getKey().equals("w0p6y") ); + Assert.assertEquals(1,bucket.getDocCount()); + } + } + } From b367fc52043efd9f383ce4359a303d8d09006524 Mon Sep 17 00:00:00 2001 From: omershelef Date: Sat, 8 Aug 2015 20:32:44 +0300 Subject: [PATCH 032/559] new release, update readme to include geographic support --- README.md | 30 ++++++++++++++++++++---------- pom.xml | 2 +- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 07fca8e7..0d64b34c 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Install as plugin: ###Elasticsearch 1.6.X ```` -./bin/plugin -u https://github.com/NLPchina/elasticsearch-sql/releases/download/1.3.4/elasticsearch-sql-1.3.4.zip --install sql +./bin/plugin -u https://github.com/NLPchina/elasticsearch-sql/releases/download/1.3.5/elasticsearch-sql-1.3.5.zip --install sql ```` ## Basic Usage @@ -64,7 +64,7 @@ http://localhost:9200/_sql/_explain?sql=select * from indexName limit 10 SELECT address FROM bank WHERE address = matchQuery('880 Holmes Lane') ORDER BY _score DESC LIMIT 3 -* Group by aggregation +* Aggregations + range age group 20-25,25-30,30-35,35-40 @@ -78,23 +78,25 @@ http://localhost:9200/_sql/_explain?sql=select * from indexName limit 10 SELECT online FROM online GROUP BY date_range(field='insert_time','format'='yyyy-MM-dd' ,'2014-08-18','2014-08-17','now-8d','now-7d','now-6d','now') +* ES Geographic + + SELECT * FROM locations WHERE GEO_BOUNDING_BOX(fieldname,100.0,1.0,101,0.0) + * Select type SELECT * FROM indexName/type -## Features +## SQL Features * SQL Select * SQL Delete -* ES TopHits -* ES MISSING -* ES STATS -* SQL COUNT distinct -* SQL where -* SQL AND & OR +* SQL Where * SQL Order By +* SQL Group By +* SQL AND & OR * SQL Like +* SQL COUNT distinct * SQL In * SQL Between * SQL Aliases @@ -108,9 +110,17 @@ http://localhost:9200/_sql/_explain?sql=select * from indexName limit 10 * SQL sum() * SQL Nulls * SQL isnull() -* SQL Group By * SQL now() +## Beyond sql features + +* ES TopHits +* ES MISSING +* ES STATS +* ES GEO_INTERSECTS +* ES GEO_BOUNDING_BOX +* ES GEO_DISTANCE +* ES GEOHASH_GRID aggregation diff --git a/pom.xml b/pom.xml index f9bdad3b..89cdaa7d 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.nlpcn elasticsearch-sql - 1.3.4 + 1.3.5 jar Query elasticsearch using SQL elasticsearch-sql From dbec906fd0bfd24580fb753ac98eb0e87722a25b Mon Sep 17 00:00:00 2001 From: omershelef Date: Sat, 8 Aug 2015 20:46:38 +0300 Subject: [PATCH 033/559] String.join does not exist in java 7. use Joiner instead --- .../nlpcn/es4sql/spatial/WktToGeoJsonConverter.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/nlpcn/es4sql/spatial/WktToGeoJsonConverter.java b/src/main/java/org/nlpcn/es4sql/spatial/WktToGeoJsonConverter.java index d0267a44..48866d8b 100644 --- a/src/main/java/org/nlpcn/es4sql/spatial/WktToGeoJsonConverter.java +++ b/src/main/java/org/nlpcn/es4sql/spatial/WktToGeoJsonConverter.java @@ -1,4 +1,6 @@ package org.nlpcn.es4sql.spatial; +import org.elasticsearch.common.base.Joiner; + import java.util.ArrayList; import java.util.List; @@ -58,7 +60,7 @@ private static String multiLineStringCoordinatesFromWkt(String wkt) { for (int i=0;i Date: Sat, 8 Aug 2015 21:34:52 +0300 Subject: [PATCH 034/559] fix dateSearchBraces test --- src/test/java/org/nlpcn/es4sql/QueryTest.java | 10 +++++++--- src/test/java/org/nlpcn/es4sql/TestsConstants.java | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java index 302b9800..13f5879c 100644 --- a/src/test/java/org/nlpcn/es4sql/QueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java @@ -19,6 +19,7 @@ import static org.hamcrest.Matchers.*; import static org.nlpcn.es4sql.TestsConstants.DATE_FORMAT; import static org.nlpcn.es4sql.TestsConstants.TEST_INDEX; +import static org.nlpcn.es4sql.TestsConstants.TS_DATE_FORMAT; public class QueryTest { @@ -304,14 +305,17 @@ public void dateSearch() throws IOException, SqlParseException, SQLFeatureNotSup @Test public void dateSearchBraces() throws IOException, SqlParseException, SQLFeatureNotSupportedException, ParseException { - DateTimeFormatter formatter = DateTimeFormat.forPattern(DATE_FORMAT); - DateTime dateToCompare = new DateTime(2015, 1, 15, 0, 0, 0); + DateTimeFormatter formatter = DateTimeFormat.forPattern(TS_DATE_FORMAT); + DateTime dateToCompare = new DateTime(2015, 3, 15, 0, 0, 0); SearchHits response = query(String.format("SELECT insert_time FROM %s/odbc WHERE insert_time < {ts '2015-03-15 00:00:00.000'}", TEST_INDEX)); SearchHit[] hits = response.getHits(); for(SearchHit hit : hits) { Map source = hit.getSource(); - DateTime insertTime = formatter.parseDateTime((String) source.get("insert_time")); + String insertTimeStr = (String) source.get("insert_time"); + insertTimeStr = insertTimeStr.replace("{ts '", "").replace("'}", ""); + + DateTime insertTime = formatter.parseDateTime(insertTimeStr); String errorMessage = String.format("insert_time must be smaller then 2015-03-15. found: %s", insertTime); Assert.assertTrue(errorMessage, insertTime.isBefore(dateToCompare)); diff --git a/src/test/java/org/nlpcn/es4sql/TestsConstants.java b/src/test/java/org/nlpcn/es4sql/TestsConstants.java index 976b5107..d908a188 100644 --- a/src/test/java/org/nlpcn/es4sql/TestsConstants.java +++ b/src/test/java/org/nlpcn/es4sql/TestsConstants.java @@ -7,5 +7,5 @@ public class TestsConstants { public final static String TEST_INDEX = "elasticsearch-sql_test_index"; public final static String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; - + public final static String TS_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; } From 12eb3ce13d76f79edf2757c277689f2e8f8b99ba Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sat, 15 Aug 2015 12:10:15 +0300 Subject: [PATCH 035/559] support on rest of point Filters: Cell , Polygon and RangeDistance --- .../org/nlpcn/es4sql/domain/Condition.java | 13 +++++-- .../org/nlpcn/es4sql/query/maker/Maker.java | 32 ++++++++++++++--- .../es4sql/spatial/CellFilterParams.java | 33 ++++++++++++++++++ .../es4sql/spatial/PolygonFilterParams.java | 18 ++++++++++ .../spatial/RangeDistanceFilterParams.java | 21 ++++++++++++ .../es4sql/spatial/SpatialParamsFactory.java | 34 ++++++++++++++++++- .../java/org/nlpcn/es4sql/MainTestSuite.java | 5 ++- src/test/java/org/nlpcn/es4sql/QueryTest.java | 27 ++++++++++++++- 8 files changed, 173 insertions(+), 10 deletions(-) create mode 100644 src/main/java/org/nlpcn/es4sql/spatial/CellFilterParams.java create mode 100644 src/main/java/org/nlpcn/es4sql/spatial/PolygonFilterParams.java create mode 100644 src/main/java/org/nlpcn/es4sql/spatial/RangeDistanceFilterParams.java diff --git a/src/main/java/org/nlpcn/es4sql/domain/Condition.java b/src/main/java/org/nlpcn/es4sql/domain/Condition.java index 5536c5a5..431ea23a 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/Condition.java +++ b/src/main/java/org/nlpcn/es4sql/domain/Condition.java @@ -12,7 +12,7 @@ public class Condition extends Where { public static enum OPEAR { - EQ, GT, LT, GTE, LTE, N, LIKE, NLIKE, IS, ISN, IN, NIN , BETWEEN ,NBETWEEN , GEO_INTERSECTS , GEO_BOUNDING_BOX , GEO_DISTANCE + EQ, GT, LT, GTE, LTE, N, LIKE, NLIKE, IS, ISN, IN, NIN , BETWEEN ,NBETWEEN , GEO_INTERSECTS , GEO_BOUNDING_BOX , GEO_DISTANCE , GEO_DISTANCE_RANGE, GEO_POLYGON , GEO_CELL }; private String name; @@ -97,7 +97,16 @@ public Condition(CONN conn, String name, String oper, Object value) throws SqlPa case "GEO_DISTANCE": this.opear = OPEAR.GEO_DISTANCE; break; - default: + case "GEO_DISTANCE_RANGE": + this.opear = OPEAR.GEO_DISTANCE_RANGE; + break; + case "GEO_POLYGON": + this.opear = OPEAR.GEO_POLYGON; + break; + case "GEO_CELL": + this.opear = OPEAR.GEO_CELL; + break; + default: throw new SqlParseException(oper + " is err!"); } } diff --git a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java index 6546f82f..de9a0c26 100644 --- a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java +++ b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java @@ -17,10 +17,7 @@ import org.durid.sql.ast.expr.SQLIdentifierExpr; import org.durid.sql.ast.expr.SQLMethodInvokeExpr; -import org.nlpcn.es4sql.spatial.BoundingBoxFilterParams; -import org.nlpcn.es4sql.spatial.DistanceFilterParams; -import org.nlpcn.es4sql.spatial.Point; -import org.nlpcn.es4sql.spatial.WktToGeoJsonConverter; +import org.nlpcn.es4sql.spatial.*; public abstract class Maker { @@ -235,7 +232,32 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar String distance = trimApostrophes(distanceFilterParams.getDistance()); x = FilterBuilders.geoDistanceFilter(cond.getName()).distance(distance).lon(fromPoint.getLon()).lat(fromPoint.getLat()); break; - default: + case GEO_DISTANCE_RANGE: + if(isQuery) + throw new SqlParseException("RangeDistance is only for filter"); + RangeDistanceFilterParams rangeDistanceFilterParams = (RangeDistanceFilterParams) cond.getValue(); + fromPoint = rangeDistanceFilterParams.getFrom(); + String distanceFrom = trimApostrophes(rangeDistanceFilterParams.getDistanceFrom()); + String distanceTo = trimApostrophes(rangeDistanceFilterParams.getDistanceTo()); + x = FilterBuilders.geoDistanceRangeFilter(cond.getName()).from(distanceFrom).to(distanceTo).lon(fromPoint.getLon()).lat(fromPoint.getLat()); + break; + case GEO_POLYGON: + if(isQuery) + throw new SqlParseException("Polygon is only for filter"); + PolygonFilterParams polygonFilterParams = (PolygonFilterParams) cond.getValue(); + GeoPolygonFilterBuilder polygonFilterBuilder = FilterBuilders.geoPolygonFilter(cond.getName()); + for(Point p : polygonFilterParams.getPolygon()) + polygonFilterBuilder.addPoint(p.getLat(),p.getLon()); + x = polygonFilterBuilder; + break; + case GEO_CELL: + if(isQuery) + throw new SqlParseException("geocell is only for filter"); + CellFilterParams cellFilterParams = (CellFilterParams) cond.getValue(); + Point geoHashPoint = cellFilterParams.getGeohashPoint(); + x = FilterBuilders.geoHashCellFilter(cond.getName()).point(geoHashPoint.getLat(),geoHashPoint.getLon()).precision(cellFilterParams.getPrecision()).neighbors(cellFilterParams.isNeighbors()); + break; + default: throw new SqlParseException("not define type " + cond.getName()); } diff --git a/src/main/java/org/nlpcn/es4sql/spatial/CellFilterParams.java b/src/main/java/org/nlpcn/es4sql/spatial/CellFilterParams.java new file mode 100644 index 00000000..8a7a9331 --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/spatial/CellFilterParams.java @@ -0,0 +1,33 @@ +package org.nlpcn.es4sql.spatial; + +/** + * Created by Eliran on 15/8/2015. + */ +public class CellFilterParams { + private Point geohashPoint; + private int precision; + private boolean neighbors; + + public CellFilterParams(Point geohashPoint, int precision, boolean neighbors) { + this.geohashPoint = geohashPoint; + this.precision = precision; + this.neighbors = neighbors; + } + + public CellFilterParams(Point geohashPoint, int precision) { + this(geohashPoint, precision,false); + } + + public Point getGeohashPoint() { + return geohashPoint; + } + + public int getPrecision() { + return precision; + } + + public boolean isNeighbors() { + return neighbors; + } +} + diff --git a/src/main/java/org/nlpcn/es4sql/spatial/PolygonFilterParams.java b/src/main/java/org/nlpcn/es4sql/spatial/PolygonFilterParams.java new file mode 100644 index 00000000..d20cfdab --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/spatial/PolygonFilterParams.java @@ -0,0 +1,18 @@ +package org.nlpcn.es4sql.spatial; + +import java.util.List; + +/** + * Created by Eliran on 15/8/2015. + */ +public class PolygonFilterParams { + private List polygon; + + public PolygonFilterParams( List polygon) { + this.polygon = polygon; + } + + public List getPolygon() { + return polygon; + } +} diff --git a/src/main/java/org/nlpcn/es4sql/spatial/RangeDistanceFilterParams.java b/src/main/java/org/nlpcn/es4sql/spatial/RangeDistanceFilterParams.java new file mode 100644 index 00000000..c19e1a60 --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/spatial/RangeDistanceFilterParams.java @@ -0,0 +1,21 @@ +package org.nlpcn.es4sql.spatial; + +/** + * Created by Eliran on 15/8/2015. + */ +public class RangeDistanceFilterParams extends DistanceFilterParams { + private String distanceTo; + + public RangeDistanceFilterParams(String distanceFrom,String distanceTo, Point from) { + super(distanceFrom, from); + this.distanceTo = distanceTo; + } + + public String getDistanceTo() { + return distanceTo; + } + + public String getDistanceFrom() { + return this.getDistance(); + } +} diff --git a/src/main/java/org/nlpcn/es4sql/spatial/SpatialParamsFactory.java b/src/main/java/org/nlpcn/es4sql/spatial/SpatialParamsFactory.java index 44604e1c..dee176df 100644 --- a/src/main/java/org/nlpcn/es4sql/spatial/SpatialParamsFactory.java +++ b/src/main/java/org/nlpcn/es4sql/spatial/SpatialParamsFactory.java @@ -3,6 +3,7 @@ import org.durid.sql.ast.SQLExpr; import org.durid.sql.parser.SQLParseException; +import java.util.LinkedList; import java.util.List; /** @@ -25,11 +26,42 @@ public static Object generateSpatialParamsObject(String methodName,List return new BoundingBoxFilterParams(new Point(topLeftLon,topLeftLat),new Point(bottomRightLon,bottomRightLat)); case "GEO_DISTANCE": if(params.size()!=4) - throw new SQLParseException("GEO_DISTANCE should have exactly 5 parameters : (fieldName,distance,fromLon,fromLat) "); + throw new SQLParseException("GEO_DISTANCE should have exactly 4 parameters : (fieldName,distance,fromLon,fromLat) "); String distance = params.get(1).toString(); double lon = Double.parseDouble(params.get(2).toString()); double lat = Double.parseDouble(params.get(3).toString()); return new DistanceFilterParams(distance ,new Point(lon,lat)); + case "GEO_DISTANCE_RANGE": + if(params.size()!=5) + throw new SQLParseException("GEO_DISTANCE should have exactly 5 parameters : (fieldName,distanceFrom,distanceTo,fromLon,fromLat) "); + String distanceFrom = params.get(1).toString(); + String distanceTo = params.get(2).toString(); + lon = Double.parseDouble(params.get(3).toString()); + lat = Double.parseDouble(params.get(4).toString()); + return new RangeDistanceFilterParams(distanceFrom,distanceTo ,new Point(lon,lat)); + case "GEO_POLYGON": + if(params.size()%2 == 0 || params.size() <= 5) + throw new SQLParseException("GEO_POLYGON should have odd num of parameters and > 5 : (fieldName,lon1,lat1,lon2,lat2,lon3,lat3,...) "); + int numberOfPoints = (params.size()-1)/2; + List points = new LinkedList<>(); + for(int i =0 ;i< numberOfPoints ;i ++){ + int currentPointLocation = 1 + i * 2; + lon = Double.parseDouble(params.get(currentPointLocation).toString()); + lat = Double.parseDouble(params.get(currentPointLocation + 1).toString()); + points.add(new Point(lon,lat)); + } + return new PolygonFilterParams(points); + case "GEO_CELL": + if(params.size()< 4 || params.size() > 5) + throw new SQLParseException("GEO_CELL should have 4 or 5 params (fieldName,lon,lat,precision,neighbors(optional)) "); + lon = Double.parseDouble(params.get(1).toString()); + lat = Double.parseDouble(params.get(2).toString()); + Point geoHashPoint = new Point(lon,lat); + int precision = Integer.parseInt(params.get(3).toString()); + if(params.size()==4) + return new CellFilterParams(geoHashPoint,precision); + boolean neighbors = Boolean.parseBoolean(params.get(4).toString()); + return new CellFilterParams(geoHashPoint,precision,neighbors); default: throw new SQLParseException(String.format("Unknown method name: %s", methodName)); } diff --git a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java index 55b56a71..9cac1586 100644 --- a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java +++ b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java @@ -129,7 +129,10 @@ public static void prepareSpatialIndex(){ "\t\t\t\t\"precision\": \"10km\"\n" + "\t\t\t},\n" + "\t\t\t\"center\":{\n" + - "\t\t\t\t\"type\":\"geo_point\"\n" + + "\t\t\t\t\"type\":\"geo_point\",\n" + + "\t\t\t\t\"geohash\":true,\n" + + "\t\t\t\t\"geohash_prefix\":true,\n" + + "\t\t\t\t\"geohash_precision\":17\n" + "\t\t\t},\n" + "\t\t\t\"description\":{\n" + "\t\t\t\t\"type\":\"string\"\n" + diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java index 13f5879c..6a15225c 100644 --- a/src/test/java/org/nlpcn/es4sql/QueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java @@ -477,7 +477,32 @@ public void geoDistance() throws SQLFeatureNotSupportedException, SqlParseExcept Assert.assertEquals("square",result.getSource().get("description")); } - private SearchHits query(String query) throws SqlParseException, SQLFeatureNotSupportedException, SQLFeatureNotSupportedException { + @Test + public void geoDistanceRange() throws SQLFeatureNotSupportedException, SqlParseException, InterruptedException { + SearchHits results = query(String.format("SELECT * FROM %s WHERE GEO_DISTANCE_RANGE(center,'1m','1km',100.5,0.50001)", TEST_INDEX)); + org.junit.Assert.assertEquals(1,results.getTotalHits()); + SearchHit result = results.getAt(0); + Assert.assertEquals("square",result.getSource().get("description")); + } + + @Test + public void geoCell() throws SQLFeatureNotSupportedException, SqlParseException, InterruptedException { + SearchHits results = query(String.format("SELECT * FROM %s WHERE GEO_CELL(center,100.5,0.50001,7)", TEST_INDEX)); + org.junit.Assert.assertEquals(1,results.getTotalHits()); + SearchHit result = results.getAt(0); + Assert.assertEquals("square",result.getSource().get("description")); + } + + @Test + public void geoPolygon() throws SQLFeatureNotSupportedException, SqlParseException, InterruptedException { + SearchHits results = query(String.format("SELECT * FROM %s WHERE GEO_POLYGON(center,100,0,100.5,2,101.0,0)", TEST_INDEX)); + org.junit.Assert.assertEquals(1,results.getTotalHits()); + SearchHit result = results.getAt(0); + Assert.assertEquals("square",result.getSource().get("description")); + } + + + private SearchHits query(String query) throws SqlParseException, SQLFeatureNotSupportedException, SQLFeatureNotSupportedException { SearchDao searchDao = MainTestSuite.getSearchDao(); SearchRequestBuilder select = (SearchRequestBuilder)searchDao.explain(query); return select.get().getHits(); From b6af598c0262c0ef257c5e244e666caeda50be22 Mon Sep 17 00:00:00 2001 From: Per Lundberg Date: Thu, 20 Aug 2015 15:22:25 +0300 Subject: [PATCH 036/559] Added note about the required restart This has been discussed a bit in #42. --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0d64b34c..50ede710 100644 --- a/README.md +++ b/README.md @@ -15,12 +15,13 @@ You can also use ES functions in SQL. Install as plugin: -###Elasticsearch 1.6.X +### Elasticsearch 1.6.X ```` - ./bin/plugin -u https://github.com/NLPchina/elasticsearch-sql/releases/download/1.3.5/elasticsearch-sql-1.3.5.zip --install sql - ```` + +After doing this, you need to restart the Elasticsearch server. Otherwise you may get errors like `Invalid index name [sql], must not start with '']; ","status":400}`. + ## Basic Usage * Visit The elasticsearch-sql web front end: From bfe81e8648fb97529f4f40b1dd246c1876027661 Mon Sep 17 00:00:00 2001 From: Caleb Ott Date: Thu, 27 Aug 2015 08:02:57 -0500 Subject: [PATCH 037/559] Fixed a bug with scanning strings when there is more than one escaped single quote. For example this would break, Where fullName LIKE 'Clark ''Superman'' Kent'. The issue in the code was that it wasn't checking to see if it was already parsing special characters. So it was re-initializing the buff and trashing all previous special characters that were handled. --- .../org/durid/sql/dialect/mysql/parser/MySqlLexer.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlLexer.java b/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlLexer.java index bced7ee8..edf4dafa 100644 --- a/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlLexer.java +++ b/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlLexer.java @@ -264,9 +264,11 @@ protected void scanString() { token = LITERAL_CHARS; break; } else { - initBuff(bufPos); - arraycopy(mark + 1, buf, 0, bufPos); - hasSpecial = true; + if (!hasSpecial) { + initBuff(bufPos); + arraycopy(mark + 1, buf, 0, bufPos); + hasSpecial = true; + } putChar('\''); continue; } From ac5bdd3df1fc97bdce2678b1972d2670128fcbe5 Mon Sep 17 00:00:00 2001 From: Caleb Ott Date: Mon, 31 Aug 2015 09:19:57 -0500 Subject: [PATCH 038/559] Fixed "NOT LIKE" so it works properly now. --- src/main/java/org/nlpcn/es4sql/query/maker/Maker.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java index de9a0c26..e29b723b 100644 --- a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java +++ b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java @@ -21,7 +21,7 @@ public abstract class Maker { - private static final Set NOT_OPEAR_SET = Sets.newHashSet(OPEAR.N, OPEAR.NIN, OPEAR.ISN, OPEAR.NBETWEEN); + private static final Set NOT_OPEAR_SET = Sets.newHashSet(OPEAR.N, OPEAR.NIN, OPEAR.ISN, OPEAR.NBETWEEN, OPEAR.NLIKE); private boolean isQuery = false; @@ -142,6 +142,7 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar break; } case LIKE: + case NLIKE: String queryStr = ((String) value).replace('%', '*').replace('_', '?'); WildcardQueryBuilder wildcardQuery = QueryBuilders.wildcardQuery(name, queryStr); x = isQuery ? wildcardQuery : FilterBuilders.queryFilter(wildcardQuery); From 07da40712130b5a969a55553e823fb47dbff16f0 Mon Sep 17 00:00:00 2001 From: Caleb Ott Date: Mon, 31 Aug 2015 10:10:28 -0500 Subject: [PATCH 039/559] Fixed "NOT field LIKE 'value'" so it works properly now. When encountering a NOT it no longer assumes NOT EQ. Now it checks if it was LIKE to set the opear to NLIKE --- src/main/java/org/nlpcn/es4sql/parse/SqlParser.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index b53c7631..88f0e4c2 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -121,10 +121,13 @@ private void explanCond(String opear, SQLExpr expr, Where where) throws SqlParse parseValue(between.endExpr)}); where.addWhere(condition); } else if (expr instanceof SQLNotExpr){ - String left = ((SQLBinaryOpExpr) ((SQLNotExpr) expr).getExpr()).getLeft().toString(); - SQLExpr right = ((SQLBinaryOpExpr) ((SQLNotExpr) expr).getExpr()).getRight(); - Condition condition = new Condition(CONN.valueOf(opear),left, Condition.OPEAR.N, parseValue(right)); - where.addWhere(condition); + SQLBinaryOpExpr notExpr = (SQLBinaryOpExpr) ((SQLNotExpr) expr).getExpr(); + String left = notExpr.getLeft().toString(); + SQLExpr right = notExpr.getRight(); + // add a check here to see if the not'd value is a 'like' operator + Condition.OPEAR notOpear = notExpr.getOperator() == SQLBinaryOperator.Like ? Condition.OPEAR.NLIKE : Condition.OPEAR.N; + Condition condition = new Condition(CONN.valueOf(opear), left, notOpear, parseValue(right)); + where.addWhere(condition); } else if (expr instanceof SQLMethodInvokeExpr) { SQLMethodInvokeExpr methodExpr = (SQLMethodInvokeExpr) expr; From 417ed448392a69afb689f08dea37d9b7f265278b Mon Sep 17 00:00:00 2001 From: Caleb Ott Date: Tue, 15 Sep 2015 08:31:14 -0500 Subject: [PATCH 040/559] Removed the exprRest from the NOT with parens since it should not expr the rest --- src/main/java/org/durid/sql/parser/SQLExprParser.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/org/durid/sql/parser/SQLExprParser.java b/src/main/java/org/durid/sql/parser/SQLExprParser.java index 01e1006b..18632041 100644 --- a/src/main/java/org/durid/sql/parser/SQLExprParser.java +++ b/src/main/java/org/durid/sql/parser/SQLExprParser.java @@ -275,7 +275,6 @@ public SQLExpr primary() { SQLExpr notTarget = expr(); accept(Token.RPAREN); - notTarget = exprRest(notTarget); sqlExpr = new SQLNotExpr(notTarget); From d4cb48de4c684948d7ec5fc6ef90d5d963abe959 Mon Sep 17 00:00:00 2001 From: Caleb Ott Date: Tue, 15 Sep 2015 10:37:57 -0500 Subject: [PATCH 041/559] Changed how NOTs are handled so that they can properly handle parenthesis. Added a complex NOT test to ensure it is NOTing correctly. --- .../org/nlpcn/es4sql/domain/Condition.java | 30 +++++++++-- .../java/org/nlpcn/es4sql/domain/Where.java | 14 +++-- .../org/nlpcn/es4sql/parse/SqlParser.java | 54 ++++++++++--------- src/test/java/org/nlpcn/es4sql/QueryTest.java | 29 ++++++++++ 4 files changed, 96 insertions(+), 31 deletions(-) diff --git a/src/main/java/org/nlpcn/es4sql/domain/Condition.java b/src/main/java/org/nlpcn/es4sql/domain/Condition.java index 431ea23a..d26d75f8 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/Condition.java +++ b/src/main/java/org/nlpcn/es4sql/domain/Condition.java @@ -2,6 +2,8 @@ import java.util.Arrays; +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; import org.nlpcn.es4sql.exception.SqlParseException; /** @@ -11,9 +13,31 @@ */ public class Condition extends Where { - public static enum OPEAR { - EQ, GT, LT, GTE, LTE, N, LIKE, NLIKE, IS, ISN, IN, NIN , BETWEEN ,NBETWEEN , GEO_INTERSECTS , GEO_BOUNDING_BOX , GEO_DISTANCE , GEO_DISTANCE_RANGE, GEO_POLYGON , GEO_CELL - }; + public enum OPEAR { + EQ, GT, LT, GTE, LTE, N, LIKE, NLIKE, IS, ISN, IN, NIN , BETWEEN ,NBETWEEN , GEO_INTERSECTS , GEO_BOUNDING_BOX , GEO_DISTANCE , GEO_DISTANCE_RANGE, GEO_POLYGON , GEO_CELL; + + private static BiMap negatives; + + static { + negatives = HashBiMap.create(7); + negatives.put(EQ, N); + negatives.put(GT, LTE); + negatives.put(LT, GTE); + negatives.put(LIKE, NLIKE); + negatives.put(IS, ISN); + negatives.put(IN, NIN); + negatives.put(BETWEEN, NBETWEEN); + } + + public OPEAR negative() throws SqlParseException { + OPEAR negative = negatives.get(this); + negative = negative != null ? negative : negatives.inverse().get(this); + if (negative == null) { + throw new SqlParseException("OPEAR negative not supported: " + this); + } + return negative; + } + } private String name; diff --git a/src/main/java/org/nlpcn/es4sql/domain/Where.java b/src/main/java/org/nlpcn/es4sql/domain/Where.java index 21f6e284..f933b9f0 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/Where.java +++ b/src/main/java/org/nlpcn/es4sql/domain/Where.java @@ -4,9 +4,13 @@ public class Where { - public static enum CONN { - AND, OR - }; + public enum CONN { + AND, OR; + + public CONN negative() { + return this == AND ? OR : AND; + } + } public static Where newInstance() { return new Where(CONN.AND); @@ -31,6 +35,10 @@ public void addWhere(Where where) { public CONN getConn() { return this.conn; } + + public void setConn(CONN conn) { + this.conn = conn; + } public LinkedList getWheres() { return wheres; diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index 88f0e4c2..66a3cb48 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -24,7 +24,7 @@ public class SqlParser { public SqlParser() { - }; + } public Select parseSelect(SQLQueryExpr mySqlExpr) throws SqlParseException { @@ -78,32 +78,32 @@ private void parseWhere(SQLExpr expr, Where where) throws SqlParseException { SQLBinaryOpExpr bExpr = (SQLBinaryOpExpr) expr; routeCond(bExpr, bExpr.left, where); routeCond(bExpr, bExpr.right, where); + } else if (expr instanceof SQLNotExpr) { + parseWhere(((SQLNotExpr) expr).getExpr(), where); + negateWhere(where); } else { explanCond("AND", expr, where); } } private void routeCond(SQLBinaryOpExpr bExpr, SQLExpr sub, Where where) throws SqlParseException { - if (sub instanceof SQLBinaryOpExpr) { - parseWhere(bExpr, (SQLBinaryOpExpr) sub, where); - } else { - explanCond(bExpr.operator.name, sub, where); - } - } - - private void parseWhere(SQLBinaryOpExpr expr, SQLBinaryOpExpr sub, Where where) throws SqlParseException { - if (isCond(sub)) { - explanCond(expr.operator.name, sub, where); - } else { - if (sub.operator.priority != expr.operator.priority) { - Where subWhere = new Where(expr.getOperator().name); + if (sub instanceof SQLBinaryOpExpr && !isCond((SQLBinaryOpExpr) sub)) { + SQLBinaryOpExpr binarySub = (SQLBinaryOpExpr) sub; + if (binarySub.operator.priority != bExpr.operator.priority) { + Where subWhere = new Where(bExpr.getOperator().name); where.addWhere(subWhere); - parseWhere(sub, subWhere); + parseWhere(binarySub, subWhere); } else { - parseWhere(sub, where); + parseWhere(binarySub, where); } + } else if (sub instanceof SQLNotExpr) { + Where subWhere = new Where(bExpr.getOperator().name); + where.addWhere(subWhere); + parseWhere(((SQLNotExpr) sub).getExpr(), subWhere); + negateWhere(subWhere); + } else { + explanCond(bExpr.operator.name, sub, where); } - } private void explanCond(String opear, SQLExpr expr, Where where) throws SqlParseException { @@ -120,14 +120,6 @@ private void explanCond(String opear, SQLExpr expr, Where where) throws SqlParse Condition condition = new Condition(CONN.valueOf(opear), between.getTestExpr().toString(), between.isNot() ? "NOT BETWEEN" : "BETWEEN", new Object[]{parseValue(between.beginExpr), parseValue(between.endExpr)}); where.addWhere(condition); - } else if (expr instanceof SQLNotExpr){ - SQLBinaryOpExpr notExpr = (SQLBinaryOpExpr) ((SQLNotExpr) expr).getExpr(); - String left = notExpr.getLeft().toString(); - SQLExpr right = notExpr.getRight(); - // add a check here to see if the not'd value is a 'like' operator - Condition.OPEAR notOpear = notExpr.getOperator() == SQLBinaryOperator.Like ? Condition.OPEAR.NLIKE : Condition.OPEAR.N; - Condition condition = new Condition(CONN.valueOf(opear), left, notOpear, parseValue(right)); - where.addWhere(condition); } else if (expr instanceof SQLMethodInvokeExpr) { SQLMethodInvokeExpr methodExpr = (SQLMethodInvokeExpr) expr; @@ -278,4 +270,16 @@ private List findFrom(SQLTableSource from) { return fromList; } + private void negateWhere(Where where) throws SqlParseException { + for (Where sub : where.getWheres()) { + if (sub instanceof Condition) { + Condition cond = (Condition) sub; + cond.setOpear(cond.getOpear().negative()); + } else { + negateWhere(sub); + } + sub.setConn(sub.getConn().negative()); + } + } + } diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java index 6a15225c..e7691800 100644 --- a/src/test/java/org/nlpcn/es4sql/QueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java @@ -199,6 +199,17 @@ public void likeTest() throws IOException, SqlParseException, SQLFeatureNotSuppo Assert.assertEquals("Amber", hits[0].getSource().get("firstname")); } + @Test + public void notLikeTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { + SearchHits response = query(String.format("SELECT * FROM %s WHERE firstname NOT LIKE 'amb%%'", TEST_INDEX)); + SearchHit[] hits = response.getHits(); + + // assert we got hits + Assert.assertNotEquals(0, response.getTotalHits()); + for (SearchHit hit : hits) { + Assert.assertFalse(hit.getSource().get("firstname").toString().toLowerCase().startsWith("amb")); + } + } @Test public void limitTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { @@ -388,6 +399,24 @@ public void complexConditionQuery() throws IOException, SqlParseException, SQLFe } } + @Test + public void complexNotConditionQuery() throws IOException, SqlParseException, SQLFeatureNotSupportedException { + String errorMessage = "Result does not exist to the condition NOT (gender='m' AND NOT (age > 25 OR account_number > 5)) OR (NOT gender='f' AND NOT (age > 30 OR account_number < 8))"; + + SearchHits response = query(String.format("SELECT * FROM %s/account WHERE NOT (gender='m' AND NOT (age > 25 OR account_number > 5)) OR (NOT gender='f' AND NOT (age > 30 OR account_number < 8))", TEST_INDEX)); + SearchHit[] hits = response.getHits(); + + Assert.assertNotEquals(hits.length, 0); + + for (SearchHit hit : hits) { + Map source = hit.getSource(); + String gender = ((String) source.get("gender")).toLowerCase(); + int age = (int) source.get("age"); + int account_number = (int) source.get("account_number"); + + Assert.assertTrue(errorMessage, !(gender.equals("m") && !(age > 25 || account_number > 5)) || (!gender.equals("f") && !(age > 30 || account_number < 8))); + } + } @Test public void orderByAscTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { From b3a95f22485ca4a1a017bda0b862fbb976714ecd Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Tue, 18 Aug 2015 21:05:16 +0300 Subject: [PATCH 042/559] removed old druid and update to newer alibaba version --- pom.xml | 7 + .../java/org/durid/DruidRuntimeException.java | 38 - src/main/java/org/durid/sql/SQLUtils.java | 135 - .../org/durid/sql/ast/SQLCommentHint.java | 47 - .../java/org/durid/sql/ast/SQLDataType.java | 27 - .../org/durid/sql/ast/SQLDataTypeImpl.java | 75 - src/main/java/org/durid/sql/ast/SQLExpr.java | 20 - .../java/org/durid/sql/ast/SQLExprImpl.java | 29 - src/main/java/org/durid/sql/ast/SQLHint.java | 21 - src/main/java/org/durid/sql/ast/SQLName.java | 20 - .../java/org/durid/sql/ast/SQLObject.java | 39 - .../java/org/durid/sql/ast/SQLObjectImpl.java | 109 - .../java/org/durid/sql/ast/SQLOrderBy.java | 57 - .../sql/ast/SQLOrderingSpecification.java | 20 - src/main/java/org/durid/sql/ast/SQLOver.java | 89 - .../durid/sql/ast/SQLPartitioningClause.java | 22 - .../org/durid/sql/ast/SQLSetQuantifier.java | 28 - .../java/org/durid/sql/ast/SQLStatement.java | 20 - .../org/durid/sql/ast/SQLStatementImpl.java | 36 - .../durid/sql/ast/expr/SQLAggregateExpr.java | 136 - .../durid/sql/ast/expr/SQLAllColumnExpr.java | 45 - .../org/durid/sql/ast/expr/SQLAllExpr.java | 87 - .../org/durid/sql/ast/expr/SQLAnyExpr.java | 86 - .../durid/sql/ast/expr/SQLBetweenExpr.java | 153 - .../durid/sql/ast/expr/SQLBinaryOpExpr.java | 132 - .../durid/sql/ast/expr/SQLBinaryOperator.java | 118 - .../sql/ast/expr/SQLBitStringLiteralExpr.java | 94 - .../org/durid/sql/ast/expr/SQLCaseExpr.java | 184 -- .../org/durid/sql/ast/expr/SQLCastExpr.java | 104 - .../org/durid/sql/ast/expr/SQLCharExpr.java | 47 - .../sql/ast/expr/SQLCurrentOfCursorExpr.java | 88 - .../sql/ast/expr/SQLDateLiteralExpr.java | 82 - .../sql/ast/expr/SQLDateLiteralValue.java | 56 - .../durid/sql/ast/expr/SQLDefaultExpr.java | 44 - .../org/durid/sql/ast/expr/SQLExistsExpr.java | 111 - .../org/durid/sql/ast/expr/SQLHexExpr.java | 85 - .../sql/ast/expr/SQLHexStringLiteralExpr.java | 81 - .../durid/sql/ast/expr/SQLIdentifierExpr.java | 98 - .../org/durid/sql/ast/expr/SQLInListExpr.java | 142 - .../durid/sql/ast/expr/SQLInSubQueryExpr.java | 122 - .../durid/sql/ast/expr/SQLIntegerExpr.java | 82 - .../sql/ast/expr/SQLIntervalLiteralExpr.java | 90 - .../org/durid/sql/ast/expr/SQLListExpr.java | 73 - .../durid/sql/ast/expr/SQLLiteralExpr.java | 23 - .../sql/ast/expr/SQLMethodInvokeExpr.java | 141 - .../org/durid/sql/ast/expr/SQLNCharExpr.java | 47 - .../org/durid/sql/ast/expr/SQLNotExpr.java | 89 - .../org/durid/sql/ast/expr/SQLNullExpr.java | 46 - .../org/durid/sql/ast/expr/SQLNumberExpr.java | 81 - .../sql/ast/expr/SQLNumericLiteralExpr.java | 31 - .../org/durid/sql/ast/expr/SQLOdbcExpr.java | 60 - .../durid/sql/ast/expr/SQLPropertyExpr.java | 112 - .../org/durid/sql/ast/expr/SQLQueryExpr.java | 88 - .../org/durid/sql/ast/expr/SQLSomeExpr.java | 86 - .../sql/ast/expr/SQLTextLiteralExpr.java | 72 - .../org/durid/sql/ast/expr/SQLUnaryExpr.java | 102 - .../durid/sql/ast/expr/SQLUnaryOperator.java | 26 - .../durid/sql/ast/expr/SQLVariantRefExpr.java | 109 - .../sql/ast/statement/NotNullConstraint.java | 33 - .../ast/statement/SQLAlterTableAddColumn.java | 46 - .../ast/statement/SQLAlterTableAddIndex.java | 64 - .../statement/SQLAlterTableAddPrimaryKey.java | 42 - .../SQLAlterTableDropColumnItem.java | 43 - .../ast/statement/SQLAlterTableDropIndex.java | 44 - .../sql/ast/statement/SQLAlterTableItem.java | 22 - .../ast/statement/SQLAlterTableStatement.java | 57 - .../sql/ast/statement/SQLAssignItem.java | 68 - .../sql/ast/statement/SQLCallStatement.java | 53 - .../ast/statement/SQLCharactorDataType.java | 46 - .../ast/statement/SQLColumnConstraint.java | 20 - .../ast/statement/SQLColumnDefinition.java | 108 - .../ast/statement/SQLCommentStatement.java | 68 - .../durid/sql/ast/statement/SQLConstaint.java | 26 - .../sql/ast/statement/SQLConstaintImpl.java | 38 - .../statement/SQLCreateDatabaseStatement.java | 66 - .../statement/SQLCreateIndexStatement.java | 71 - .../statement/SQLCreateTableStatement.java | 103 - .../ast/statement/SQLCreateViewStatement.java | 105 - .../sql/ast/statement/SQLDDLStatement.java | 23 - .../sql/ast/statement/SQLDeleteStatement.java | 81 - .../ast/statement/SQLDropIndexStatement.java | 53 - .../ast/statement/SQLDropTableStatement.java | 70 - .../ast/statement/SQLDropViewStatement.java | 70 - .../sql/ast/statement/SQLExprTableSource.java | 66 - .../statement/SQLForeignKeyConstraint.java | 31 - .../sql/ast/statement/SQLIndexDefinition.java | 21 - .../sql/ast/statement/SQLInsertInto.java | 86 - .../sql/ast/statement/SQLInsertStatement.java | 75 - .../sql/ast/statement/SQLJoinTableSource.java | 119 - .../sql/ast/statement/SQLPrimaryKey.java | 20 - .../SQLReleaseSavePointStatement.java | 42 - .../ast/statement/SQLRollbackStatement.java | 44 - .../ast/statement/SQLSavePointStatement.java | 42 - .../durid/sql/ast/statement/SQLSelect.java | 67 - .../ast/statement/SQLSelectGroupByClause.java | 56 - .../sql/ast/statement/SQLSelectItem.java | 73 - .../ast/statement/SQLSelectOrderByItem.java | 66 - .../sql/ast/statement/SQLSelectQuery.java | 27 - .../ast/statement/SQLSelectQueryBlock.java | 136 - .../sql/ast/statement/SQLSelectStatement.java | 54 - .../sql/ast/statement/SQLSelectUnionType.java | 22 - .../sql/ast/statement/SQLSetStatement.java | 66 - .../ast/statement/SQLSubqueryTableSource.java | 70 - .../sql/ast/statement/SQLTableConstaint.java | 20 - .../ast/statement/SQLTableConstaintImpl.java | 32 - .../sql/ast/statement/SQLTableElement.java | 22 - .../sql/ast/statement/SQLTableSource.java | 32 - .../sql/ast/statement/SQLTableSourceImpl.java | 57 - .../ast/statement/SQLTruncateStatement.java | 49 - .../sql/ast/statement/SQLUnionOperator.java | 30 - .../sql/ast/statement/SQLUnionQuery.java | 76 - .../ast/statement/SQLUniqueConstraint.java | 26 - .../statement/SQLUniqueConstraintImpl.java | 46 - .../sql/ast/statement/SQLUpdateSetItem.java | 65 - .../sql/ast/statement/SQLUpdateStatement.java | 100 - .../sql/ast/statement/SQLUseStatement.java | 44 - .../mysql/ast/MySqlForceIndexHint.java | 32 - .../sql/dialect/mysql/ast/MySqlHint.java | 23 - .../mysql/ast/MySqlIgnoreIndexHint.java | 32 - .../sql/dialect/mysql/ast/MySqlIndexHint.java | 31 - .../dialect/mysql/ast/MySqlIndexHintImpl.java | 51 - .../durid/sql/dialect/mysql/ast/MySqlKey.java | 66 - .../sql/dialect/mysql/ast/MySqlObject.java | 24 - .../dialect/mysql/ast/MySqlObjectImpl.java | 36 - .../dialect/mysql/ast/MySqlPrimaryKey.java | 35 - .../dialect/mysql/ast/MySqlUseIndexHint.java | 32 - .../mysql/ast/expr/MySqlBinaryExpr.java | 92 - .../mysql/ast/expr/MySqlBooleanExpr.java | 86 - .../dialect/mysql/ast/expr/MySqlCharExpr.java | 81 - .../sql/dialect/mysql/ast/expr/MySqlExpr.java | 22 - .../dialect/mysql/ast/expr/MySqlExprImpl.java | 25 - .../mysql/ast/expr/MySqlExtractExpr.java | 96 - .../mysql/ast/expr/MySqlHexadecimalExpr.java | 31 - .../mysql/ast/expr/MySqlIntervalExpr.java | 98 - .../mysql/ast/expr/MySqlIntervalUnit.java | 32 - .../mysql/ast/expr/MySqlMatchAgainstExpr.java | 128 - .../mysql/ast/expr/MySqlOutFileExpr.java | 126 - .../dialect/mysql/ast/expr/MySqlUserName.java | 56 - .../mysql/ast/statement/CobarShowStatus.java | 29 - .../statement/MySqlAlterTableAddColumn.java | 75 - .../statement/MySqlAlterTableAddIndex.java | 55 - .../statement/MySqlAlterTableAddUnique.java | 55 - .../MySqlAlterTableChangeColumn.java | 89 - .../statement/MySqlAlterTableCharacter.java | 55 - .../ast/statement/MySqlAlterTableOption.java | 59 - .../statement/MySqlAlterTableStatement.java | 53 - .../ast/statement/MySqlBinlogStatement.java | 39 - .../ast/statement/MySqlCommitStatement.java | 58 - .../MySqlCreateDatabaseStatement.java | 21 - .../statement/MySqlCreateIndexStatement.java | 52 - .../statement/MySqlCreateTableStatement.java | 133 - .../statement/MySqlCreateUserStatement.java | 88 - .../ast/statement/MySqlDeleteStatement.java | 120 - .../ast/statement/MySqlDescribeStatement.java | 42 - .../statement/MySqlDropTableStatement.java | 71 - .../mysql/ast/statement/MySqlDropUser.java | 43 - .../ast/statement/MySqlDropViewStatement.java | 62 - .../ast/statement/MySqlExecuteStatement.java | 51 - .../ast/statement/MySqlHelpStatement.java | 41 - .../ast/statement/MySqlInsertStatement.java | 119 - .../ast/statement/MySqlKillStatement.java | 51 - .../MySqlLoadDataInFileStatement.java | 192 -- .../ast/statement/MySqlLoadXmlStatement.java | 148 - .../statement/MySqlLockTableStatement.java | 66 - .../ast/statement/MySqlPartitionByKey.java | 60 - .../ast/statement/MySqlPrepareStatement.java | 59 - .../statement/MySqlRenameTableStatement.java | 78 - .../ast/statement/MySqlReplaceStatement.java | 89 - .../ast/statement/MySqlResetStatement.java | 39 - .../ast/statement/MySqlRollbackStatement.java | 79 - .../statement/MySqlSQLColumnDefinition.java | 38 - .../ast/statement/MySqlSelectGroupBy.java | 58 - .../ast/statement/MySqlSelectQueryBlock.java | 249 -- .../statement/MySqlSetCharSetStatement.java | 59 - .../ast/statement/MySqlSetNamesStatement.java | 59 - ...SetTransactionIsolationLevelStatement.java | 49 - .../statement/MySqlShowAuthorsStatement.java | 29 - .../MySqlShowBinLogEventsStatement.java | 63 - .../MySqlShowBinaryLogsStatement.java | 29 - .../MySqlShowCharacterSetStatement.java | 52 - .../MySqlShowCollationStatement.java | 52 - .../statement/MySqlShowColumnsStatement.java | 90 - .../MySqlShowContributorsStatement.java | 29 - .../MySqlShowCreateDatabaseStatement.java | 42 - .../MySqlShowCreateEventStatement.java | 42 - .../MySqlShowCreateFunctionStatement.java | 42 - .../MySqlShowCreateProcedureStatement.java | 42 - .../MySqlShowCreateTableStatement.java | 42 - .../MySqlShowCreateTriggerStatement.java | 42 - .../MySqlShowCreateViewStatement.java | 42 - .../MySqlShowDatabasesStatement.java | 62 - .../statement/MySqlShowEngineStatement.java | 54 - .../statement/MySqlShowEnginesStatement.java | 38 - .../statement/MySqlShowErrorsStatement.java | 50 - .../statement/MySqlShowEventsStatement.java | 61 - .../MySqlShowFunctionCodeStatement.java | 42 - .../MySqlShowFunctionStatusStatement.java | 51 - .../statement/MySqlShowGrantsStatement.java | 42 - .../statement/MySqlShowIndexesStatement.java | 59 - .../ast/statement/MySqlShowKeysStatement.java | 59 - .../MySqlShowMasterLogsStatement.java | 29 - .../MySqlShowMasterStatusStatement.java | 29 - .../MySqlShowOpenTablesStatement.java | 62 - .../statement/MySqlShowPluginsStatement.java | 29 - .../MySqlShowPrivilegesStatement.java | 29 - .../MySqlShowProcedureCodeStatement.java | 42 - .../MySqlShowProcedureStatusStatement.java | 51 - .../MySqlShowProcessListStatement.java | 39 - .../statement/MySqlShowProfileStatement.java | 75 - .../statement/MySqlShowProfilesStatement.java | 28 - .../MySqlShowRelayLogEventsStatement.java | 62 - .../MySqlShowSlaveHostsStatement.java | 28 - .../MySqlShowSlaveStatusStatement.java | 28 - .../statement/MySqlShowStatusStatement.java | 70 - .../MySqlShowTableStatusStatement.java | 61 - .../statement/MySqlShowTablesStatement.java | 72 - .../statement/MySqlShowTriggersStatement.java | 62 - .../statement/MySqlShowVariantsStatement.java | 70 - .../statement/MySqlShowWarningsStatement.java | 50 - .../MySqlStartTransactionStatement.java | 58 - .../mysql/ast/statement/MySqlStatement.java | 23 - .../ast/statement/MySqlStatementImpl.java | 38 - .../mysql/ast/statement/MySqlTableIndex.java | 80 - .../mysql/ast/statement/MySqlUnionQuery.java | 56 - .../statement/MySqlUnlockTablesStatement.java | 29 - .../ast/statement/MySqlUpdateStatement.java | 85 - .../mysql/parser/MySqlCreateTableParser.java | 227 -- .../dialect/mysql/parser/MySqlExprParser.java | 644 ---- .../mysql/parser/MySqlSelectParser.java | 331 -- .../mysql/parser/MySqlStatementParser.java | 2364 --------------- .../visitor/MySql2OracleOutputVisitor.java | 33 - .../mysql/visitor/MySqlASTVisitor.java | 518 ---- .../mysql/visitor/MySqlASTVisitorAdapter.java | 1141 ------- .../mysql/visitor/MySqlEvalVisitorImpl.java | 132 - .../visitor/MySqlExportParameterVisitor.java | 93 - .../mysql/visitor/MySqlOutputVisitor.java | 2679 ----------------- .../MySqlParameterizedOutputVisitor.java | 150 - .../mysql/visitor/MySqlSchemaStatVisitor.java | 1211 -------- .../transact/ast/TransactSQLObject.java | 22 - .../transact/ast/TransactSQLObjectImpl.java | 36 - .../transact/ast/TransactSQLSelect.java | 20 - .../java/org/durid/sql/parser/CharTypes.java | 98 - .../java/org/durid/sql/parser/Keywords.java | 137 - .../durid/sql/parser/LayoutCharacters.java | 54 - src/main/java/org/durid/sql/parser/Lexer.java | 978 ------ .../sql/parser/NotAllowCommentException.java | 34 - .../org/durid/sql/parser/ParserException.java | 42 - .../sql/parser/SQLCreateTableParser.java | 107 - .../org/durid/sql/parser/SQLDDLParser.java | 49 - .../org/durid/sql/parser/SQLExprParser.java | 1274 -------- .../durid/sql/parser/SQLParseException.java | 38 - .../java/org/durid/sql/parser/SQLParser.java | 141 - .../org/durid/sql/parser/SQLParserUtils.java | 45 - .../org/durid/sql/parser/SQLSelectParser.java | 348 --- .../durid/sql/parser/SQLStatementParser.java | 652 ---- src/main/java/org/durid/sql/parser/Token.java | 242 -- .../sql/visitor/ExportParameterVisitor.java | 23 - .../visitor/ExportParameterVisitorUtils.java | 102 - .../visitor/ParameterizedOutputVisitor.java | 85 - .../ParameterizedOutputVisitorUtils.java | 173 -- .../sql/visitor/SQLASTOutputVisitor.java | 1178 -------- .../org/durid/sql/visitor/SQLASTVisitor.java | 371 --- .../sql/visitor/SQLASTVisitorAdapter.java | 691 ----- .../org/durid/sql/visitor/SQLEvalVisitor.java | 33 - .../durid/sql/visitor/SQLEvalVisitorImpl.java | 117 - .../sql/visitor/SQLEvalVisitorUtils.java | 1429 --------- .../durid/sql/visitor/SQLShardingContext.java | 21 - .../durid/sql/visitor/SQLShardingVisitor.java | 32 - .../durid/sql/visitor/SchemaStatVisitor.java | 985 ------ src/main/java/org/durid/stat/TableStat.java | 454 --- .../support/logging/Jdk14LoggingImpl.java | 105 - .../java/org/durid/support/logging/Log.java | 47 - .../org/durid/support/logging/Log4jImpl.java | 101 - .../org/durid/support/logging/LogFactory.java | 87 - .../durid/support/logging/NoLoggingImpl.java | 88 - .../org/durid/support/logging/Resources.java | 336 --- src/main/java/org/durid/util/HexBin.java | 120 - .../java/org/durid/util/JdbcConstants.java | 56 - src/main/java/org/durid/util/JdbcUtils.java | 685 ----- src/main/java/org/nlpcn/es4sql/Util.java | 9 +- .../java/org/nlpcn/es4sql/domain/From.java | 1 + .../java/org/nlpcn/es4sql/domain/Paramer.java | 9 +- .../org/nlpcn/es4sql/parse/ElasticLexer.java | 94 + .../es4sql/parse/ElasticSqlExprParser.java | 19 + .../org/nlpcn/es4sql/parse/FieldMaker.java | 15 +- .../org/nlpcn/es4sql/parse/SqlParser.java | 68 +- .../nlpcn/es4sql/query/ESActionFactory.java | 28 +- .../org/nlpcn/es4sql/query/maker/Maker.java | 5 +- .../es4sql/spatial/SpatialParamsFactory.java | 19 +- .../java/org/nlpcn/es4sql/MainTestSuite.java | 1 + 290 files changed, 208 insertions(+), 35172 deletions(-) delete mode 100644 src/main/java/org/durid/DruidRuntimeException.java delete mode 100644 src/main/java/org/durid/sql/SQLUtils.java delete mode 100644 src/main/java/org/durid/sql/ast/SQLCommentHint.java delete mode 100644 src/main/java/org/durid/sql/ast/SQLDataType.java delete mode 100644 src/main/java/org/durid/sql/ast/SQLDataTypeImpl.java delete mode 100644 src/main/java/org/durid/sql/ast/SQLExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/SQLExprImpl.java delete mode 100644 src/main/java/org/durid/sql/ast/SQLHint.java delete mode 100644 src/main/java/org/durid/sql/ast/SQLName.java delete mode 100644 src/main/java/org/durid/sql/ast/SQLObject.java delete mode 100644 src/main/java/org/durid/sql/ast/SQLObjectImpl.java delete mode 100644 src/main/java/org/durid/sql/ast/SQLOrderBy.java delete mode 100644 src/main/java/org/durid/sql/ast/SQLOrderingSpecification.java delete mode 100644 src/main/java/org/durid/sql/ast/SQLOver.java delete mode 100644 src/main/java/org/durid/sql/ast/SQLPartitioningClause.java delete mode 100644 src/main/java/org/durid/sql/ast/SQLSetQuantifier.java delete mode 100644 src/main/java/org/durid/sql/ast/SQLStatement.java delete mode 100644 src/main/java/org/durid/sql/ast/SQLStatementImpl.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLAggregateExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLAllColumnExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLAllExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLAnyExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLBetweenExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLBinaryOpExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLBinaryOperator.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLBitStringLiteralExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLCaseExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLCastExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLCharExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLCurrentOfCursorExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLDateLiteralExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLDateLiteralValue.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLDefaultExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLExistsExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLHexExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLHexStringLiteralExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLIdentifierExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLInListExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLInSubQueryExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLIntegerExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLIntervalLiteralExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLListExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLLiteralExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLMethodInvokeExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLNCharExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLNotExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLNullExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLNumberExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLNumericLiteralExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLOdbcExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLPropertyExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLQueryExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLSomeExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLTextLiteralExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLUnaryExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLUnaryOperator.java delete mode 100644 src/main/java/org/durid/sql/ast/expr/SQLVariantRefExpr.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/NotNullConstraint.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLAlterTableAddColumn.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLAlterTableAddIndex.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLAlterTableAddPrimaryKey.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLAlterTableDropColumnItem.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLAlterTableDropIndex.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLAlterTableItem.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLAlterTableStatement.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLAssignItem.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLCallStatement.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLCharactorDataType.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLColumnConstraint.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLColumnDefinition.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLCommentStatement.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLConstaint.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLConstaintImpl.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLCreateDatabaseStatement.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLCreateIndexStatement.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLCreateTableStatement.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLCreateViewStatement.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLDDLStatement.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLDeleteStatement.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLDropIndexStatement.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLDropTableStatement.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLDropViewStatement.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLExprTableSource.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLForeignKeyConstraint.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLIndexDefinition.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLInsertInto.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLInsertStatement.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLJoinTableSource.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLPrimaryKey.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLReleaseSavePointStatement.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLRollbackStatement.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLSavePointStatement.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLSelect.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLSelectGroupByClause.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLSelectItem.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLSelectOrderByItem.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLSelectQuery.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLSelectQueryBlock.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLSelectStatement.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLSelectUnionType.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLSetStatement.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLSubqueryTableSource.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLTableConstaint.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLTableConstaintImpl.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLTableElement.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLTableSource.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLTableSourceImpl.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLTruncateStatement.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLUnionOperator.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLUnionQuery.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLUniqueConstraint.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLUniqueConstraintImpl.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLUpdateSetItem.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLUpdateStatement.java delete mode 100644 src/main/java/org/durid/sql/ast/statement/SQLUseStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/MySqlForceIndexHint.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/MySqlHint.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/MySqlIgnoreIndexHint.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/MySqlIndexHint.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/MySqlIndexHintImpl.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/MySqlKey.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/MySqlObject.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/MySqlObjectImpl.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/MySqlPrimaryKey.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/MySqlUseIndexHint.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlBinaryExpr.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlBooleanExpr.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlCharExpr.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlExpr.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlExprImpl.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlExtractExpr.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlHexadecimalExpr.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlIntervalExpr.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlIntervalUnit.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlMatchAgainstExpr.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlOutFileExpr.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlUserName.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/CobarShowStatus.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableAddColumn.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableAddIndex.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableAddUnique.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableChangeColumn.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableCharacter.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableOption.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlBinlogStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlCommitStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlCreateDatabaseStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlCreateIndexStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlCreateTableStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlCreateUserStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlDeleteStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlDescribeStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlDropTableStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlDropUser.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlDropViewStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlExecuteStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlHelpStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlInsertStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlKillStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlLoadDataInFileStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlLoadXmlStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlLockTableStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlPartitionByKey.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlPrepareStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlRenameTableStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlReplaceStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlResetStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlRollbackStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlSQLColumnDefinition.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlSelectGroupBy.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlSelectQueryBlock.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlSetCharSetStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlSetNamesStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlSetTransactionIsolationLevelStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowAuthorsStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowBinLogEventsStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowBinaryLogsStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCharacterSetStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCollationStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowColumnsStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowContributorsStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateDatabaseStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateEventStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateFunctionStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateProcedureStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateTableStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateTriggerStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateViewStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowDatabasesStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowEngineStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowEnginesStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowErrorsStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowEventsStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowFunctionCodeStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowFunctionStatusStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowGrantsStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowIndexesStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowKeysStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowMasterLogsStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowMasterStatusStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowOpenTablesStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowPluginsStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowPrivilegesStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowProcedureCodeStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowProcedureStatusStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowProcessListStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowProfileStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowProfilesStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowRelayLogEventsStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowSlaveHostsStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowSlaveStatusStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowStatusStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowTableStatusStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowTablesStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowTriggersStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowVariantsStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowWarningsStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlStartTransactionStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlStatementImpl.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlTableIndex.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlUnionQuery.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlUnlockTablesStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlUpdateStatement.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/parser/MySqlCreateTableParser.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/parser/MySqlExprParser.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/parser/MySqlSelectParser.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/parser/MySqlStatementParser.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/visitor/MySql2OracleOutputVisitor.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlASTVisitor.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlASTVisitorAdapter.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlEvalVisitorImpl.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlExportParameterVisitor.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlParameterizedOutputVisitor.java delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlSchemaStatVisitor.java delete mode 100644 src/main/java/org/durid/sql/dialect/transact/ast/TransactSQLObject.java delete mode 100644 src/main/java/org/durid/sql/dialect/transact/ast/TransactSQLObjectImpl.java delete mode 100644 src/main/java/org/durid/sql/dialect/transact/ast/TransactSQLSelect.java delete mode 100644 src/main/java/org/durid/sql/parser/CharTypes.java delete mode 100644 src/main/java/org/durid/sql/parser/Keywords.java delete mode 100644 src/main/java/org/durid/sql/parser/LayoutCharacters.java delete mode 100644 src/main/java/org/durid/sql/parser/Lexer.java delete mode 100644 src/main/java/org/durid/sql/parser/NotAllowCommentException.java delete mode 100644 src/main/java/org/durid/sql/parser/ParserException.java delete mode 100644 src/main/java/org/durid/sql/parser/SQLCreateTableParser.java delete mode 100644 src/main/java/org/durid/sql/parser/SQLDDLParser.java delete mode 100644 src/main/java/org/durid/sql/parser/SQLExprParser.java delete mode 100644 src/main/java/org/durid/sql/parser/SQLParseException.java delete mode 100644 src/main/java/org/durid/sql/parser/SQLParser.java delete mode 100644 src/main/java/org/durid/sql/parser/SQLParserUtils.java delete mode 100644 src/main/java/org/durid/sql/parser/SQLSelectParser.java delete mode 100644 src/main/java/org/durid/sql/parser/SQLStatementParser.java delete mode 100644 src/main/java/org/durid/sql/parser/Token.java delete mode 100644 src/main/java/org/durid/sql/visitor/ExportParameterVisitor.java delete mode 100644 src/main/java/org/durid/sql/visitor/ExportParameterVisitorUtils.java delete mode 100644 src/main/java/org/durid/sql/visitor/ParameterizedOutputVisitor.java delete mode 100644 src/main/java/org/durid/sql/visitor/ParameterizedOutputVisitorUtils.java delete mode 100644 src/main/java/org/durid/sql/visitor/SQLASTOutputVisitor.java delete mode 100644 src/main/java/org/durid/sql/visitor/SQLASTVisitor.java delete mode 100644 src/main/java/org/durid/sql/visitor/SQLASTVisitorAdapter.java delete mode 100644 src/main/java/org/durid/sql/visitor/SQLEvalVisitor.java delete mode 100644 src/main/java/org/durid/sql/visitor/SQLEvalVisitorImpl.java delete mode 100644 src/main/java/org/durid/sql/visitor/SQLEvalVisitorUtils.java delete mode 100644 src/main/java/org/durid/sql/visitor/SQLShardingContext.java delete mode 100644 src/main/java/org/durid/sql/visitor/SQLShardingVisitor.java delete mode 100644 src/main/java/org/durid/sql/visitor/SchemaStatVisitor.java delete mode 100644 src/main/java/org/durid/stat/TableStat.java delete mode 100644 src/main/java/org/durid/support/logging/Jdk14LoggingImpl.java delete mode 100644 src/main/java/org/durid/support/logging/Log.java delete mode 100644 src/main/java/org/durid/support/logging/Log4jImpl.java delete mode 100644 src/main/java/org/durid/support/logging/LogFactory.java delete mode 100644 src/main/java/org/durid/support/logging/NoLoggingImpl.java delete mode 100644 src/main/java/org/durid/support/logging/Resources.java delete mode 100644 src/main/java/org/durid/util/HexBin.java delete mode 100644 src/main/java/org/durid/util/JdbcConstants.java delete mode 100644 src/main/java/org/durid/util/JdbcUtils.java create mode 100644 src/main/java/org/nlpcn/es4sql/parse/ElasticLexer.java create mode 100644 src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java diff --git a/pom.xml b/pom.xml index 89cdaa7d..a03ce0ab 100644 --- a/pom.xml +++ b/pom.xml @@ -87,6 +87,13 @@ test + + com.alibaba + druid + 1.0.15 + + + com.google.guava guava diff --git a/src/main/java/org/durid/DruidRuntimeException.java b/src/main/java/org/durid/DruidRuntimeException.java deleted file mode 100644 index 3c4f1a21..00000000 --- a/src/main/java/org/durid/DruidRuntimeException.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid; - -public class DruidRuntimeException extends RuntimeException { - - private static final long serialVersionUID = 1L; - - public DruidRuntimeException(){ - super(); - } - - public DruidRuntimeException(String message, Throwable cause){ - super(message, cause); - } - - public DruidRuntimeException(String message){ - super(message); - } - - public DruidRuntimeException(Throwable cause){ - super(cause); - } - -} diff --git a/src/main/java/org/durid/sql/SQLUtils.java b/src/main/java/org/durid/sql/SQLUtils.java deleted file mode 100644 index eb3e201f..00000000 --- a/src/main/java/org/durid/sql/SQLUtils.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql; - -import java.util.List; - -import org.durid.DruidRuntimeException; -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLObject; -import org.durid.sql.ast.SQLStatement; -import org.durid.sql.dialect.mysql.visitor.MySqlOutputVisitor; -import org.durid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; -import org.durid.sql.parser.ParserException; -import org.durid.sql.parser.SQLExprParser; -import org.durid.sql.parser.SQLParseException; -import org.durid.sql.parser.SQLParserUtils; -import org.durid.sql.parser.SQLStatementParser; -import org.durid.sql.parser.Token; -import org.durid.sql.visitor.SQLASTOutputVisitor; -import org.durid.sql.visitor.SchemaStatVisitor; -import org.durid.support.logging.Log; -import org.durid.support.logging.LogFactory; -import org.durid.util.JdbcUtils; - -public class SQLUtils { - - private final static Log LOG = LogFactory.getLog(SQLUtils.class); - - public static String toSQLString(SQLObject sqlObject, String dbType) { - return toMySqlString(sqlObject); - } - - public static String toSQLString(SQLObject sqlObject) { - StringBuilder out = new StringBuilder(); - sqlObject.accept(new SQLASTOutputVisitor(out)); - - String sql = out.toString(); - return sql; - } - - public static String toMySqlString(SQLObject sqlObject) { - StringBuilder out = new StringBuilder(); - sqlObject.accept(new MySqlOutputVisitor(out)); - - String sql = out.toString(); - return sql; - } - - public static SQLExpr toMySqlExpr(String sql) { - return toSQLExpr(sql, JdbcUtils.MYSQL); - } - - public static String formatMySql(String sql) { - return format(sql, JdbcUtils.MYSQL); - } - - public static String formatOracle(String sql) { - return format(sql, JdbcUtils.ORACLE); - } - - public static String formatPGSql(String sql) { - return format(sql, JdbcUtils.POSTGRESQL); - } - - public static SQLExpr toSQLExpr(String sql, String dbType) { - SQLExprParser parser = SQLParserUtils.createExprParser(sql, dbType); - SQLExpr expr = parser.expr(); - - if (parser.getLexer().token() != Token.EOF) { - throw new ParserException("illegal sql expr : " + sql); - } - - return expr; - } - - public static List toStatementList(String sql, String dbType) { - SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType); - return parser.parseStatementList(); - } - - public static SQLExpr toSQLExpr(String sql) { - return toSQLExpr(sql, null); - } - - public static String format(String sql, String dbType) { - try { - List statementList = toStatementList(sql, dbType); - - StringBuilder out = new StringBuilder(); - SQLASTOutputVisitor visitor = createFormatOutputVisitor(out, statementList, dbType); - - for (SQLStatement stmt : statementList) { - stmt.accept(visitor); - } - - return out.toString(); - } catch (SQLParseException ex) { - LOG.warn("format error", ex); - return sql; - } catch (ParserException ex) { - LOG.warn("format error", ex); - return sql; - } - } - - public static SQLASTOutputVisitor createFormatOutputVisitor(Appendable out, List statementList, String dbType) { - return new MySqlOutputVisitor(out); - } - - public static SchemaStatVisitor createSchemaStatVisitor(List statementList, String dbType) { - return new MySqlSchemaStatVisitor(); - } - - public static List parseStatements(String sql, String dbType) { - SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType); - List stmtList = parser.parseStatementList(); - if (parser.getLexer().token() != Token.EOF) { - throw new DruidRuntimeException("syntax error : " + sql); - } - return stmtList; - } -} diff --git a/src/main/java/org/durid/sql/ast/SQLCommentHint.java b/src/main/java/org/durid/sql/ast/SQLCommentHint.java deleted file mode 100644 index 691b75f6..00000000 --- a/src/main/java/org/durid/sql/ast/SQLCommentHint.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast; - -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLCommentHint extends SQLObjectImpl implements SQLHint { - - private static final long serialVersionUID = 1L; - - private String text; - - public SQLCommentHint(){ - - } - - public SQLCommentHint(String text){ - - this.text = text; - } - - public String getText() { - return this.text; - } - - public void setText(String text) { - this.text = text; - } - - protected void accept0(SQLASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/ast/SQLDataType.java b/src/main/java/org/durid/sql/ast/SQLDataType.java deleted file mode 100644 index 8567b1c6..00000000 --- a/src/main/java/org/durid/sql/ast/SQLDataType.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast; - -import java.util.List; - -public interface SQLDataType extends SQLObject { - - String getName(); - - void setName(String name); - - List getArguments(); -} diff --git a/src/main/java/org/durid/sql/ast/SQLDataTypeImpl.java b/src/main/java/org/durid/sql/ast/SQLDataTypeImpl.java deleted file mode 100644 index ad171560..00000000 --- a/src/main/java/org/durid/sql/ast/SQLDataTypeImpl.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLDataTypeImpl extends SQLObjectImpl implements SQLDataType { - - private static final long serialVersionUID = -2783296007802532452L; - - protected String name; - protected final List arguments = new ArrayList(); - - public SQLDataTypeImpl(){ - - } - - public SQLDataTypeImpl(String name){ - - this.name = name; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.arguments); - } - - visitor.endVisit(this); - } - - - public String getName() { - return this.name; - } - - public void setName(String name) { - this.name = name; - } - - public List getArguments() { - return this.arguments; - } - - public void output(StringBuffer buf) { - buf.append(this.name); - if (this.arguments.size() > 0) { - buf.append("("); - int i = 0; - for (int size = this.arguments.size(); i < size; ++i) { - if (i != 0) { - buf.append(", "); - } - ((SQLExpr) this.arguments.get(i)).output(buf); - } - buf.append(")"); - } - } -} diff --git a/src/main/java/org/durid/sql/ast/SQLExpr.java b/src/main/java/org/durid/sql/ast/SQLExpr.java deleted file mode 100644 index 86984887..00000000 --- a/src/main/java/org/durid/sql/ast/SQLExpr.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast; - -public interface SQLExpr extends SQLObject { - -} diff --git a/src/main/java/org/durid/sql/ast/SQLExprImpl.java b/src/main/java/org/durid/sql/ast/SQLExprImpl.java deleted file mode 100644 index 66cb301d..00000000 --- a/src/main/java/org/durid/sql/ast/SQLExprImpl.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast; - -public abstract class SQLExprImpl extends SQLObjectImpl implements SQLExpr { - - private static final long serialVersionUID = 1278977287415092601L; - - public SQLExprImpl(){ - - } - - public abstract boolean equals(Object o); - - public abstract int hashCode(); -} diff --git a/src/main/java/org/durid/sql/ast/SQLHint.java b/src/main/java/org/durid/sql/ast/SQLHint.java deleted file mode 100644 index 963ce670..00000000 --- a/src/main/java/org/durid/sql/ast/SQLHint.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast; - - -public interface SQLHint extends SQLObject { - -} diff --git a/src/main/java/org/durid/sql/ast/SQLName.java b/src/main/java/org/durid/sql/ast/SQLName.java deleted file mode 100644 index b188f524..00000000 --- a/src/main/java/org/durid/sql/ast/SQLName.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast; - -public interface SQLName extends SQLExpr { - String getSimleName(); -} diff --git a/src/main/java/org/durid/sql/ast/SQLObject.java b/src/main/java/org/durid/sql/ast/SQLObject.java deleted file mode 100644 index 2cbdb31b..00000000 --- a/src/main/java/org/durid/sql/ast/SQLObject.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast; - -import java.util.Map; - -import org.durid.sql.visitor.SQLASTVisitor; - -public interface SQLObject { - - void accept(SQLASTVisitor visitor); - - SQLObject getParent(); - - void setParent(SQLObject parent); - - Map getAttributes(); - - Object getAttribute(String name); - - void putAttribute(String name, Object value); - - Map getAttributesDirect(); - - void output(StringBuffer buf); -} diff --git a/src/main/java/org/durid/sql/ast/SQLObjectImpl.java b/src/main/java/org/durid/sql/ast/SQLObjectImpl.java deleted file mode 100644 index 4371dd2a..00000000 --- a/src/main/java/org/durid/sql/ast/SQLObjectImpl.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast; - -import java.io.Serializable; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.durid.sql.visitor.SQLASTVisitor; - -public abstract class SQLObjectImpl implements SQLObject, Serializable { - - private static final long serialVersionUID = 5569722716326763762L; - - private SQLObject parent; - - private Map attributes; - - public SQLObjectImpl(){ - } - - public final void accept(SQLASTVisitor visitor) { - if (visitor == null) { - throw new IllegalArgumentException(); - } - - visitor.preVisit(this); - - accept0(visitor); - - visitor.postVisit(this); - } - - protected abstract void accept0(SQLASTVisitor visitor); - - protected final void acceptChild(SQLASTVisitor visitor, List children) { - for (SQLObject child : children) { - acceptChild(visitor, child); - } - } - - protected final void acceptChild(SQLASTVisitor visitor, SQLObject child) { - if (child == null) { - return; - } - - child.accept(visitor); - } - - public void output(StringBuffer buf) { - buf.append(super.toString()); - } - - public String toString() { - StringBuffer buf = new StringBuffer(); - output(buf); - return buf.toString(); - } - - public SQLObject getParent() { - return parent; - } - - public void setParent(SQLObject parent) { - this.parent = parent; - } - - public Map getAttributes() { - if (attributes == null) { - attributes = new HashMap(1); - } - - return attributes; - } - - public Object getAttribute(String name) { - if (attributes == null) { - return null; - } - - return attributes.get(name); - } - - public void putAttribute(String name, Object value) { - if (attributes == null) { - attributes = new HashMap(1); - } - - attributes.put(name, value); - } - - public Map getAttributesDirect() { - return attributes; - } -} diff --git a/src/main/java/org/durid/sql/ast/SQLOrderBy.java b/src/main/java/org/durid/sql/ast/SQLOrderBy.java deleted file mode 100644 index 549ca008..00000000 --- a/src/main/java/org/durid/sql/ast/SQLOrderBy.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.statement.SQLSelectOrderByItem; -import org.durid.sql.visitor.SQLASTVisitor; - -@SuppressWarnings("serial") -public class SQLOrderBy extends SQLObjectImpl { - - protected final List items = new ArrayList(); - - public SQLOrderBy(){ - - } - - public List getItems() { - return this.items; - } - - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.items); - } - - visitor.endVisit(this); - } - - public void output(StringBuffer buf) { - buf.append("ORDER "); - buf.append("BY "); - - int i = 0; - for (int size = this.items.size(); i < size; ++i) { - if (i != 0) { - buf.append(", "); - } - this.items.get(i).output(buf); - } - } -} diff --git a/src/main/java/org/durid/sql/ast/SQLOrderingSpecification.java b/src/main/java/org/durid/sql/ast/SQLOrderingSpecification.java deleted file mode 100644 index 8cea9b77..00000000 --- a/src/main/java/org/durid/sql/ast/SQLOrderingSpecification.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast; - -public enum SQLOrderingSpecification { - ASC, DESC; -} diff --git a/src/main/java/org/durid/sql/ast/SQLOver.java b/src/main/java/org/durid/sql/ast/SQLOver.java deleted file mode 100644 index 1d08a325..00000000 --- a/src/main/java/org/durid/sql/ast/SQLOver.java +++ /dev/null @@ -1,89 +0,0 @@ -package org.durid.sql.ast; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.visitor.SQLASTVisitor; - -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -public class SQLOver extends SQLObjectImpl { - - private static final long serialVersionUID = 1L; - - protected final List partitionBy = new ArrayList(); - protected SQLOrderBy orderBy; - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.partitionBy); - acceptChild(visitor, this.orderBy); - } - visitor.endVisit(this); - } - - public SQLOrderBy getOrderBy() { - return orderBy; - } - - public void setOrderBy(SQLOrderBy orderBy) { - this.orderBy = orderBy; - } - - public List getPartitionBy() { - return partitionBy; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((orderBy == null) ? 0 : orderBy.hashCode()); - result = prime * result + ((partitionBy == null) ? 0 : partitionBy.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - SQLOver other = (SQLOver) obj; - if (orderBy == null) { - if (other.orderBy != null) { - return false; - } - } else if (!orderBy.equals(other.orderBy)) { - return false; - } - if (partitionBy == null) { - if (other.partitionBy != null) { - return false; - } - } else if (!partitionBy.equals(other.partitionBy)) { - return false; - } - return true; - } - -} diff --git a/src/main/java/org/durid/sql/ast/SQLPartitioningClause.java b/src/main/java/org/durid/sql/ast/SQLPartitioningClause.java deleted file mode 100644 index e433e4a2..00000000 --- a/src/main/java/org/durid/sql/ast/SQLPartitioningClause.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast; - - - -public interface SQLPartitioningClause extends SQLObject { - -} diff --git a/src/main/java/org/durid/sql/ast/SQLSetQuantifier.java b/src/main/java/org/durid/sql/ast/SQLSetQuantifier.java deleted file mode 100644 index 8fa0de95..00000000 --- a/src/main/java/org/durid/sql/ast/SQLSetQuantifier.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast; - -public interface SQLSetQuantifier { - - // SQL 92 - public final static int ALL = 1; - public final static int DISTINCT = 2; - - public final static int UNIQUE = 3; - public final static int DISTINCTROW = 4; - - // -} diff --git a/src/main/java/org/durid/sql/ast/SQLStatement.java b/src/main/java/org/durid/sql/ast/SQLStatement.java deleted file mode 100644 index 9e2ee6d7..00000000 --- a/src/main/java/org/durid/sql/ast/SQLStatement.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast; - -public interface SQLStatement extends SQLObject { - -} diff --git a/src/main/java/org/durid/sql/ast/SQLStatementImpl.java b/src/main/java/org/durid/sql/ast/SQLStatementImpl.java deleted file mode 100644 index 05f3455b..00000000 --- a/src/main/java/org/durid/sql/ast/SQLStatementImpl.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast; - -import org.durid.sql.visitor.SQLASTVisitor; - -public abstract class SQLStatementImpl extends SQLObjectImpl implements SQLStatement { - - private static final long serialVersionUID = 1L; - - public SQLStatementImpl(){ - - } - - public void output(StringBuffer buf) { - buf.append(super.toString()); - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - throw new UnsupportedOperationException(this.getClass().getName()); - } -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLAggregateExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLAggregateExpr.java deleted file mode 100644 index 3a812f16..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLAggregateExpr.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.ast.SQLOver; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLAggregateExpr extends SQLExprImpl implements Serializable { - - private static final long serialVersionUID = 1L; - protected String methodName; - protected Option option; - protected final List arguments = new ArrayList(); - protected SQLOver over; - - public SQLAggregateExpr(String methodName){ - - this.methodName = methodName; - } - - public SQLAggregateExpr(String methodName, Option option){ - this.methodName = methodName; - this.option = option; - } - - public String getMethodName() { - return this.methodName; - } - - public void setMethodName(String methodName) { - this.methodName = methodName; - } - - public Option getOption() { - return this.option; - } - - public void setOption(Option option) { - this.option = option; - } - - public List getArguments() { - return this.arguments; - } - - public SQLOver getOver() { - return over; - } - - public void setOver(SQLOver over) { - this.over = over; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.arguments); - acceptChild(visitor, this.over); - } - - visitor.endVisit(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((arguments == null) ? 0 : arguments.hashCode()); - result = prime * result + ((methodName == null) ? 0 : methodName.hashCode()); - result = prime * result + ((option == null) ? 0 : option.hashCode()); - result = prime * result + ((over == null) ? 0 : over.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - SQLAggregateExpr other = (SQLAggregateExpr) obj; - if (arguments == null) { - if (other.arguments != null) { - return false; - } - } else if (!arguments.equals(other.arguments)) { - return false; - } - if (methodName == null) { - if (other.methodName != null) { - return false; - } - } else if (!methodName.equals(other.methodName)) { - return false; - } - if (over == null) { - if (other.over != null) { - return false; - } - } else if (!over.equals(other.over)) { - return false; - } - if (option != other.option) { - return false; - } - return true; - } - - public static enum Option { - DISTINCT, ALL, UNIQUE - } -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLAllColumnExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLAllColumnExpr.java deleted file mode 100644 index 4b28c702..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLAllColumnExpr.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLAllColumnExpr extends SQLExprImpl { - - private static final long serialVersionUID = 1L; - - public SQLAllColumnExpr(){ - - } - - public void output(StringBuffer buf) { - buf.append("*"); - } - - protected void accept0(SQLASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } - - public int hashCode() { - return 0; - } - - public boolean equals(Object o) { - return o instanceof SQLAllColumnExpr; - } -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLAllExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLAllExpr.java deleted file mode 100644 index 95a42ac3..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLAllExpr.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.ast.statement.SQLSelect; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLAllExpr extends SQLExprImpl { - - private static final long serialVersionUID = 1L; - public SQLSelect subQuery; - - public SQLAllExpr(){ - - } - - public SQLAllExpr(SQLSelect select){ - - this.subQuery = select; - } - - public SQLSelect getSubQuery() { - return this.subQuery; - } - - public void setSubQuery(SQLSelect subQuery) { - this.subQuery = subQuery; - } - - public void output(StringBuffer buf) { - this.subQuery.output(buf); - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.subQuery); - } - - visitor.endVisit(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((subQuery == null) ? 0 : subQuery.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - SQLAllExpr other = (SQLAllExpr) obj; - if (subQuery == null) { - if (other.subQuery != null) { - return false; - } - } else if (!subQuery.equals(other.subQuery)) { - return false; - } - return true; - } - -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLAnyExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLAnyExpr.java deleted file mode 100644 index e887d773..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLAnyExpr.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.ast.statement.SQLSelect; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLAnyExpr extends SQLExprImpl { - - private static final long serialVersionUID = 1L; - public SQLSelect subQuery; - - public SQLAnyExpr(){ - - } - - public SQLAnyExpr(SQLSelect select){ - - this.subQuery = select; - } - - public SQLSelect getSubQuery() { - return this.subQuery; - } - - public void setSubQuery(SQLSelect subQuery) { - this.subQuery = subQuery; - } - - public void output(StringBuffer buf) { - this.subQuery.output(buf); - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.subQuery); - } - - visitor.endVisit(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((subQuery == null) ? 0 : subQuery.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - SQLAnyExpr other = (SQLAnyExpr) obj; - if (subQuery == null) { - if (other.subQuery != null) { - return false; - } - } else if (!subQuery.equals(other.subQuery)) { - return false; - } - return true; - } -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLBetweenExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLBetweenExpr.java deleted file mode 100644 index b446757e..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLBetweenExpr.java +++ /dev/null @@ -1,153 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import java.io.Serializable; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLBetweenExpr extends SQLExprImpl implements Serializable { - - private static final long serialVersionUID = 1L; - public SQLExpr testExpr; - private boolean not; - public SQLExpr beginExpr; - public SQLExpr endExpr; - - public SQLBetweenExpr(){ - - } - - public SQLBetweenExpr(SQLExpr testExpr, SQLExpr beginExpr, SQLExpr endExpr){ - - this.testExpr = testExpr; - this.beginExpr = beginExpr; - this.endExpr = endExpr; - } - - public SQLBetweenExpr(SQLExpr testExpr, boolean not, SQLExpr beginExpr, SQLExpr endExpr){ - - this.testExpr = testExpr; - this.not = not; - this.beginExpr = beginExpr; - this.endExpr = endExpr; - } - - public void output(StringBuffer buf) { - this.testExpr.output(buf); - if (this.not) { - buf.append(" NOT BETWEEN "); - } else { - buf.append(" BETWEEN "); - } - this.beginExpr.output(buf); - buf.append(" AND "); - this.endExpr.output(buf); - } - - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.testExpr); - acceptChild(visitor, this.beginExpr); - acceptChild(visitor, this.endExpr); - } - visitor.endVisit(this); - } - - public SQLExpr getTestExpr() { - return this.testExpr; - } - - public void setTestExpr(SQLExpr testExpr) { - this.testExpr = testExpr; - } - - public boolean isNot() { - return this.not; - } - - public void setNot(boolean not) { - this.not = not; - } - - public SQLExpr getBeginExpr() { - return this.beginExpr; - } - - public void setBeginExpr(SQLExpr beginExpr) { - this.beginExpr = beginExpr; - } - - public SQLExpr getEndExpr() { - return this.endExpr; - } - - public void setEndExpr(SQLExpr endExpr) { - this.endExpr = endExpr; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((beginExpr == null) ? 0 : beginExpr.hashCode()); - result = prime * result + ((endExpr == null) ? 0 : endExpr.hashCode()); - result = prime * result + (not ? 1231 : 1237); - result = prime * result + ((testExpr == null) ? 0 : testExpr.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - SQLBetweenExpr other = (SQLBetweenExpr) obj; - if (beginExpr == null) { - if (other.beginExpr != null) { - return false; - } - } else if (!beginExpr.equals(other.beginExpr)) { - return false; - } - if (endExpr == null) { - if (other.endExpr != null) { - return false; - } - } else if (!endExpr.equals(other.endExpr)) { - return false; - } - if (not != other.not) { - return false; - } - if (testExpr == null) { - if (other.testExpr != null) { - return false; - } - } else if (!testExpr.equals(other.testExpr)) { - return false; - } - return true; - } -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLBinaryOpExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLBinaryOpExpr.java deleted file mode 100644 index 063ffd3e..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLBinaryOpExpr.java +++ /dev/null @@ -1,132 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import java.io.Serializable; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLBinaryOpExpr extends SQLExprImpl implements Serializable { - - private static final long serialVersionUID = 1L; - public SQLExpr left; - public SQLExpr right; - public SQLBinaryOperator operator; - - public SQLBinaryOpExpr(){ - - } - - public SQLBinaryOpExpr(SQLExpr left, SQLBinaryOperator operator, SQLExpr right){ - - this.left = left; - this.right = right; - this.operator = operator; - } - - public SQLBinaryOpExpr(SQLExpr left, SQLExpr right, SQLBinaryOperator operator){ - - this.left = left; - this.right = right; - this.operator = operator; - } - - public SQLExpr getLeft() { - return this.left; - } - - public void setLeft(SQLExpr left) { - this.left = left; - } - - public SQLExpr getRight() { - return this.right; - } - - public void setRight(SQLExpr right) { - this.right = right; - } - - public SQLBinaryOperator getOperator() { - return this.operator; - } - - public void setOperator(SQLBinaryOperator operator) { - this.operator = operator; - } - - public void output(StringBuffer buf) { - this.left.output(buf); - buf.append(" "); - buf.append(this.operator.name); - buf.append(" "); - this.right.output(buf); - } - - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.left); - acceptChild(visitor, this.right); - } - - visitor.endVisit(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((left == null) ? 0 : left.hashCode()); - result = prime * result + ((operator == null) ? 0 : operator.hashCode()); - result = prime * result + ((right == null) ? 0 : right.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (!(obj instanceof SQLBinaryOpExpr)) { - return false; - } - SQLBinaryOpExpr other = (SQLBinaryOpExpr) obj; - if (left == null) { - if (other.left != null) { - return false; - } - } else if (!left.equals(other.left)) { - return false; - } - if (operator != other.operator) { - return false; - } - if (right == null) { - if (other.right != null) { - return false; - } - } else if (!right.equals(other.right)) { - return false; - } - return true; - } - -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLBinaryOperator.java b/src/main/java/org/durid/sql/ast/expr/SQLBinaryOperator.java deleted file mode 100644 index 7b5ee3be..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLBinaryOperator.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -/** - * - * 二元操作符 - * @author wenshao 2011-5-20 下午12:32:02 - * @formatter:off - */ -public enum SQLBinaryOperator { - Union("UNION", 0), - COLLATE("COLLATE", 20), - BitwiseXor("^", 50), - - Multiply("*", 60), - Divide("/", 60), - Modulus("%", 60), - - Add("+", 70), - Subtract("-", 70), - - LeftShift("<<", 80), - RightShift(">>", 80), - - BitwiseAnd("&", 90), - BitwiseOr("|", 100), - InvertBits("~", 100), - - GreaterThan(">", 110), - GreaterThanOrEqual(">=", 110), - Is("IS", 110), - LessThan("<", 110), - LessThanOrEqual("<=", 110), - LessThanOrEqualOrGreaterThan("<=>",110), - LessThanOrGreater("<>", 110), - - Like("LIKE", 110), - NotLike("NOT LIKE", 110), - - RLike("RLIKE", 110), - NotRLike("NOT RLIKE", 110), - - NotEqual("!=", 110), - NotLessThan("!<", 110), - NotGreaterThan("!>", 110), - IsNot("IS NOT", 110), - Escape("ESCAPE", 110), - RegExp("REGEXP", 110), - NotRegExp("NOT REGEXP", 110), - Equality("=", 110), - - BitwiseNot("!", 130), - Concat("||", 140), - - BooleanAnd("AND", 140), - BooleanXor("XOR", 150), - BooleanOr("OR", 160), - Assignment(":=", 169), - - ; - - - - - - public static int getPriority(SQLBinaryOperator operator) { - return 0; - } - - public final String name; - public final int priority; - - SQLBinaryOperator(){ - this(null, 0); - } - - SQLBinaryOperator(String name, int priority){ - this.name = name; - this.priority = priority; - } - - public boolean isRelational() { - switch (this) { - case Equality: - case Like: - case NotEqual: - case GreaterThan: - case GreaterThanOrEqual: - case LessThan: - case LessThanOrEqual: - case LessThanOrGreater: - case NotLike: - case NotLessThan: - case NotGreaterThan: - case RLike: - case NotRLike: - case RegExp: - case NotRegExp: - return true; - default: - return false; - } - } -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLBitStringLiteralExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLBitStringLiteralExpr.java deleted file mode 100644 index 8fa308cf..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLBitStringLiteralExpr.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import java.util.BitSet; - -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -/** - * SQL-92 - *

- * <bit string literal> ::= B <quote> [ <bit> ... ] <quote> [ { <separator> ... <quote> [ <bit> ... ] - * <quote> }... ] - *

- * - * @author WENSHAO - */ -public class SQLBitStringLiteralExpr extends SQLExprImpl implements SQLLiteralExpr { - - private static final long serialVersionUID = 1L; - - private BitSet value; - - public SQLBitStringLiteralExpr(){ - - } - - public BitSet getValue() { - return value; - } - - public void setValue(BitSet value) { - this.value = value; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - visitor.visit(this); - - visitor.endVisit(this); - } - - public void output(StringBuffer buf) { - buf.append("b'"); - for (int i = 0; i < value.length(); ++i) { - buf.append(value); - } - buf.append("'"); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((value == null) ? 0 : value.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - SQLBitStringLiteralExpr other = (SQLBitStringLiteralExpr) obj; - if (value == null) { - if (other.value != null) { - return false; - } - } else if (!value.equals(other.value)) { - return false; - } - return true; - } -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLCaseExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLCaseExpr.java deleted file mode 100644 index ba781dcf..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLCaseExpr.java +++ /dev/null @@ -1,184 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.ast.SQLObjectImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLCaseExpr extends SQLExprImpl implements Serializable { - - private static final long serialVersionUID = 1L; - private final List items = new ArrayList(); - private SQLExpr valueExpr; - private SQLExpr elseExpr; - - public SQLCaseExpr(){ - - } - - public void output(StringBuffer buf) { - buf.append("CASE "); - if (this.valueExpr != null) { - this.valueExpr.output(buf); - buf.append(" "); - } - - int i = 0; - for (int size = this.items.size(); i < size; ++i) { - if (i != 0) { - buf.append(" "); - } - ((Item) this.items.get(i)).output(buf); - } - - if (this.elseExpr != null) { - buf.append(" ELSE "); - this.elseExpr.output(buf); - } - - buf.append(" END"); - } - - public SQLExpr getValueExpr() { - return this.valueExpr; - } - - public void setValueExpr(SQLExpr valueExpr) { - this.valueExpr = valueExpr; - } - - public SQLExpr getElseExpr() { - return this.elseExpr; - } - - public void setElseExpr(SQLExpr elseExpr) { - this.elseExpr = elseExpr; - } - - public List getItems() { - return this.items; - } - - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.valueExpr); - acceptChild(visitor, this.items); - acceptChild(visitor, this.elseExpr); - } - visitor.endVisit(this); - } - - public static class Item extends SQLObjectImpl implements Serializable { - - private static final long serialVersionUID = 1L; - private SQLExpr conditionExpr; - private SQLExpr valueExpr; - - public Item(){ - - } - - public Item(SQLExpr conditionExpr, SQLExpr valueExpr){ - - this.conditionExpr = conditionExpr; - this.valueExpr = valueExpr; - } - - public SQLExpr getConditionExpr() { - return this.conditionExpr; - } - - public void setConditionExpr(SQLExpr conditionExpr) { - this.conditionExpr = conditionExpr; - } - - public SQLExpr getValueExpr() { - return this.valueExpr; - } - - public void setValueExpr(SQLExpr valueExpr) { - this.valueExpr = valueExpr; - } - - public void output(StringBuffer buf) { - buf.append("WHEN "); - this.conditionExpr.output(buf); - buf.append(" THEN "); - this.valueExpr.output(buf); - } - - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.conditionExpr); - acceptChild(visitor, this.valueExpr); - } - visitor.endVisit(this); - } - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((elseExpr == null) ? 0 : elseExpr.hashCode()); - result = prime * result + ((items == null) ? 0 : items.hashCode()); - result = prime * result + ((valueExpr == null) ? 0 : valueExpr.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - SQLCaseExpr other = (SQLCaseExpr) obj; - if (elseExpr == null) { - if (other.elseExpr != null) { - return false; - } - } else if (!elseExpr.equals(other.elseExpr)) { - return false; - } - if (items == null) { - if (other.items != null) { - return false; - } - } else if (!items.equals(other.items)) { - return false; - } - if (valueExpr == null) { - if (other.valueExpr != null) { - return false; - } - } else if (!valueExpr.equals(other.valueExpr)) { - return false; - } - return true; - } - -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLCastExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLCastExpr.java deleted file mode 100644 index cb4a4484..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLCastExpr.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import org.durid.sql.ast.SQLDataType; -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLCastExpr extends SQLExprImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr expr; - private SQLDataType dataType; - - public SQLCastExpr(){ - - } - - public SQLExpr getExpr() { - return this.expr; - } - - public void setExpr(SQLExpr expr) { - this.expr = expr; - } - - public SQLDataType getDataType() { - return this.dataType; - } - - public void setDataType(SQLDataType dataType) { - this.dataType = dataType; - } - - public void output(StringBuffer buf) { - buf.append("CAST("); - this.expr.output(buf); - buf.append(" AS "); - this.dataType.output(buf); - buf.append(")"); - } - - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.expr); - acceptChild(visitor, this.dataType); - } - visitor.endVisit(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((dataType == null) ? 0 : dataType.hashCode()); - result = prime * result + ((expr == null) ? 0 : expr.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - SQLCastExpr other = (SQLCastExpr) obj; - if (dataType == null) { - if (other.dataType != null) { - return false; - } - } else if (!dataType.equals(other.dataType)) { - return false; - } - if (expr == null) { - if (other.expr != null) { - return false; - } - } else if (!expr.equals(other.expr)) { - return false; - } - return true; - } - -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLCharExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLCharExpr.java deleted file mode 100644 index b65ff695..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLCharExpr.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLCharExpr extends SQLTextLiteralExpr { - - private static final long serialVersionUID = 1L; - - public SQLCharExpr(){ - - } - - public SQLCharExpr(String text){ - super(text); - } - - @Override - public void output(StringBuffer buf) { - if ((this.text == null) || (this.text.length() == 0)) { - buf.append("NULL"); - } else { - buf.append("'"); - buf.append(this.text.replaceAll("'", "''")); - buf.append("'"); - } - } - - protected void accept0(SQLASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLCurrentOfCursorExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLCurrentOfCursorExpr.java deleted file mode 100644 index cd6916f3..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLCurrentOfCursorExpr.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.ast.SQLName; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLCurrentOfCursorExpr extends SQLExprImpl { - - private static final long serialVersionUID = 1L; - - private SQLName cursorName; - - public SQLCurrentOfCursorExpr(){ - - } - - public SQLCurrentOfCursorExpr(SQLName cursorName){ - this.cursorName = cursorName; - } - - public SQLName getCursorName() { - return cursorName; - } - - public void setCursorName(SQLName cursorName) { - this.cursorName = cursorName; - } - - @Override - public void output(StringBuffer buf) { - buf.append("CURRENT OF "); - cursorName.output(buf); - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.cursorName); - } - visitor.endVisit(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((cursorName == null) ? 0 : cursorName.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - SQLCurrentOfCursorExpr other = (SQLCurrentOfCursorExpr) obj; - if (cursorName == null) { - if (other.cursorName != null) { - return false; - } - } else if (!cursorName.equals(other.cursorName)) { - return false; - } - return true; - } - -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLDateLiteralExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLDateLiteralExpr.java deleted file mode 100644 index c8cb32f4..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLDateLiteralExpr.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLDateLiteralExpr extends SQLExprImpl implements SQLLiteralExpr { - - private static final long serialVersionUID = 1L; - - private SQLDateLiteralValue value = new SQLDateLiteralValue(); - - public SQLDateLiteralExpr(){ - - } - - public SQLDateLiteralValue getValue() { - return value; - } - - public void setValue(SQLDateLiteralValue value) { - this.value = value; - } - - public void output(StringBuffer buf) { - buf.append("DATE'"); - this.value.output(buf); - buf.append("'"); - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - visitor.visit(this); - - visitor.endVisit(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((value == null) ? 0 : value.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - SQLDateLiteralExpr other = (SQLDateLiteralExpr) obj; - if (value == null) { - if (other.value != null) { - return false; - } - } else if (!value.equals(other.value)) { - return false; - } - return true; - } - -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLDateLiteralValue.java b/src/main/java/org/durid/sql/ast/expr/SQLDateLiteralValue.java deleted file mode 100644 index f1c20118..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLDateLiteralValue.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -public class SQLDateLiteralValue { - - private int years; - private int months; - private int days; - - public int getYears() { - return years; - } - - public void setYears(int years) { - this.years = years; - } - - public int getMonths() { - return months; - } - - public void setMonths(int months) { - this.months = months; - } - - public int getDays() { - return days; - } - - public void setDays(int days) { - this.days = days; - } - - public void output(StringBuffer buf) { - buf.append(years); - buf.append("-"); - buf.append(months); - buf.append("-"); - buf.append(days); - } - -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLDefaultExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLDefaultExpr.java deleted file mode 100644 index 0a6e1c1b..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLDefaultExpr.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLDefaultExpr extends SQLExprImpl implements SQLLiteralExpr { - - private static final long serialVersionUID = 1L; - - @Override - public boolean equals(Object o) { - return o instanceof SQLDefaultExpr; - } - - @Override - public int hashCode() { - return 0; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } - - public String toString() { - return "DEFAULT"; - } -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLExistsExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLExistsExpr.java deleted file mode 100644 index a873b049..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLExistsExpr.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import java.io.Serializable; - -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.ast.statement.SQLSelect; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLExistsExpr extends SQLExprImpl implements Serializable { - - private static final long serialVersionUID = 1L; - public boolean not = false; - public SQLSelect subQuery; - - public SQLExistsExpr(){ - - } - - public SQLExistsExpr(SQLSelect subQuery){ - - this.subQuery = subQuery; - } - - public SQLExistsExpr(SQLSelect subQuery, boolean not){ - - this.subQuery = subQuery; - this.not = not; - } - - public boolean isNot() { - return this.not; - } - - public void setNot(boolean not) { - this.not = not; - } - - public SQLSelect getSubQuery() { - return this.subQuery; - } - - public void setSubQuery(SQLSelect subQuery) { - this.subQuery = subQuery; - } - - public void output(StringBuffer buf) { - if (this.not) { - buf.append("NOT "); - } - buf.append("EXISTS ("); - this.subQuery.output(buf); - buf.append(")"); - } - - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.subQuery); - } - - visitor.endVisit(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + (not ? 1231 : 1237); - result = prime * result + ((subQuery == null) ? 0 : subQuery.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - SQLExistsExpr other = (SQLExistsExpr) obj; - if (not != other.not) { - return false; - } - if (subQuery == null) { - if (other.subQuery != null) { - return false; - } - } else if (!subQuery.equals(other.subQuery)) { - return false; - } - return true; - } -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLHexExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLHexExpr.java deleted file mode 100644 index fcebc6f6..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLHexExpr.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.visitor.SQLASTVisitor; -import org.durid.util.HexBin; - -public class SQLHexExpr extends SQLExprImpl implements SQLLiteralExpr { - - private static final long serialVersionUID = 1L; - - private final String hex; - - public SQLHexExpr(String hex){ - this.hex = hex; - } - - public String getHex() { - return hex; - } - - public void output(StringBuffer buf) { - buf.append("0x"); - buf.append(this.hex); - - String charset = (String) getAttribute("USING"); - if (charset != null) { - buf.append(" USING "); - buf.append(charset); - } - } - - protected void accept0(SQLASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((hex == null) ? 0 : hex.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - SQLHexExpr other = (SQLHexExpr) obj; - if (hex == null) { - if (other.hex != null) { - return false; - } - } else if (!hex.equals(other.hex)) { - return false; - } - return true; - } - - public byte[] toBytes() { - return HexBin.decode(this.hex); - } -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLHexStringLiteralExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLHexStringLiteralExpr.java deleted file mode 100644 index 6c948881..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLHexStringLiteralExpr.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLHexStringLiteralExpr extends SQLExprImpl implements SQLLiteralExpr { - - private static final long serialVersionUID = 1L; - - private String value; - - public SQLHexStringLiteralExpr(){ - - } - - public String getValue() { - return value; - } - - public void setValue(String value) { - this.value = value; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - visitor.visit(this); - - visitor.endVisit(this); - } - - public void output(StringBuffer buf) { - buf.append("x'"); - buf.append(value); - buf.append("'"); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((value == null) ? 0 : value.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - SQLHexStringLiteralExpr other = (SQLHexStringLiteralExpr) obj; - if (value == null) { - if (other.value != null) { - return false; - } - } else if (!value.equals(other.value)) { - return false; - } - return true; - } -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLIdentifierExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLIdentifierExpr.java deleted file mode 100644 index 81b85bf7..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLIdentifierExpr.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.ast.SQLName; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLIdentifierExpr extends SQLExprImpl implements SQLName { - - private static final long serialVersionUID = -4101240977289682659L; - - private String name; - private boolean wrappedInParens; - - public SQLIdentifierExpr(){ - - } - - public SQLIdentifierExpr(String name){ - - this.name = name; - } - - public String getSimleName() { - return name; - } - - public String getName() { - return this.name; - } - - public void setName(String name) { - this.name = name; - } - - public boolean isWrappedInParens() { - return this.wrappedInParens; - } - - public void setWrappedInParens(boolean wrappedInParens) { - this.wrappedInParens = wrappedInParens; - } - - public void output(StringBuffer buf) { - buf.append(this.name); - } - - protected void accept0(SQLASTVisitor visitor) { - visitor.visit(this); - - visitor.endVisit(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((name == null) ? 0 : name.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (!(obj instanceof SQLIdentifierExpr)) { - return false; - } - SQLIdentifierExpr other = (SQLIdentifierExpr) obj; - if (name == null) { - if (other.name != null) { - return false; - } - } else if (!name.equals(other.name)) { - return false; - } - return true; - } - -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLInListExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLInListExpr.java deleted file mode 100644 index fc3a11eb..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLInListExpr.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLInListExpr extends SQLExprImpl implements Serializable { - - private static final long serialVersionUID = 1L; - private boolean not = false; - private SQLExpr expr; - private List targetList = new ArrayList(); - - public SQLInListExpr(){ - - } - - public SQLInListExpr(SQLExpr expr){ - - this.expr = expr; - } - - public SQLInListExpr(SQLExpr expr, boolean not){ - - this.expr = expr; - this.not = not; - } - - public boolean isNot() { - return this.not; - } - - public void setNot(boolean not) { - this.not = not; - } - - public SQLExpr getExpr() { - return this.expr; - } - - public void setExpr(SQLExpr expr) { - this.expr = expr; - } - - public List getTargetList() { - return this.targetList; - } - - public void setTargetList(List targetList) { - this.targetList = targetList; - } - - public void output(StringBuffer buf) { - this.expr.output(buf); - - if (this.not) { - buf.append("NOT IN "); - } else { - buf.append("IN "); - } - - buf.append("("); - int i = 0; - for (int size = this.targetList.size(); i < size; ++i) { - if (i != 0) { - buf.append(", "); - } - ((SQLExpr) this.targetList.get(i)).output(buf); - } - buf.append(")"); - } - - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.expr); - acceptChild(visitor, this.targetList); - } - - visitor.endVisit(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((expr == null) ? 0 : expr.hashCode()); - result = prime * result + (not ? 1231 : 1237); - result = prime * result + ((targetList == null) ? 0 : targetList.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - SQLInListExpr other = (SQLInListExpr) obj; - if (expr == null) { - if (other.expr != null) { - return false; - } - } else if (!expr.equals(other.expr)) { - return false; - } - if (not != other.not) { - return false; - } - if (targetList == null) { - if (other.targetList != null) { - return false; - } - } else if (!targetList.equals(other.targetList)) { - return false; - } - return true; - } -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLInSubQueryExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLInSubQueryExpr.java deleted file mode 100644 index 9f304614..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLInSubQueryExpr.java +++ /dev/null @@ -1,122 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import java.io.Serializable; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.ast.statement.SQLSelect; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLInSubQueryExpr extends SQLExprImpl implements Serializable { - - private static final long serialVersionUID = 1L; - private boolean not = false; - private SQLExpr expr; - - public SQLSelect subQuery; - - public SQLInSubQueryExpr(){ - - } - - public boolean isNot() { - return not; - } - - public void setNot(boolean not) { - this.not = not; - } - - public SQLExpr getExpr() { - return expr; - } - - public void setExpr(SQLExpr expr) { - this.expr = expr; - } - - public SQLInSubQueryExpr(SQLSelect select){ - - this.subQuery = select; - } - - public SQLSelect getSubQuery() { - return this.subQuery; - } - - public void setSubQuery(SQLSelect subQuery) { - this.subQuery = subQuery; - } - - public void output(StringBuffer buf) { - this.subQuery.output(buf); - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor,this.expr); - acceptChild(visitor, this.subQuery); - } - - visitor.endVisit(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((expr == null) ? 0 : expr.hashCode()); - result = prime * result + (not ? 1231 : 1237); - result = prime * result + ((subQuery == null) ? 0 : subQuery.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - SQLInSubQueryExpr other = (SQLInSubQueryExpr) obj; - if (expr == null) { - if (other.expr != null) { - return false; - } - } else if (!expr.equals(other.expr)) { - return false; - } - if (not != other.not) { - return false; - } - if (subQuery == null) { - if (other.subQuery != null) { - return false; - } - } else if (!subQuery.equals(other.subQuery)) { - return false; - } - return true; - } - -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLIntegerExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLIntegerExpr.java deleted file mode 100644 index 081f8bb9..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLIntegerExpr.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLIntegerExpr extends SQLNumericLiteralExpr { - - private static final long serialVersionUID = 1L; - - private Number number; - - public SQLIntegerExpr(Number number){ - - this.number = number; - } - - public SQLIntegerExpr(){ - - } - - public Number getNumber() { - return this.number; - } - - public void setNumber(Number number) { - this.number = number; - } - - public void output(StringBuffer buf) { - buf.append(this.number); - } - - protected void accept0(SQLASTVisitor visitor) { - visitor.visit(this); - - visitor.endVisit(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((number == null) ? 0 : number.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - SQLIntegerExpr other = (SQLIntegerExpr) obj; - if (number == null) { - if (other.number != null) { - return false; - } - } else if (!number.equals(other.number)) { - return false; - } - return true; - } -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLIntervalLiteralExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLIntervalLiteralExpr.java deleted file mode 100644 index 85895b24..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLIntervalLiteralExpr.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -/** - * TODO - * - * @author WENSHAO - */ -public class SQLIntervalLiteralExpr extends SQLExprImpl implements SQLLiteralExpr { - - private static final long serialVersionUID = 1L; - - private Character sign = null; - - public Character getSign() { - return sign; - } - - public void setSign(Character sign) { - this.sign = sign; - } - - public SQLIntervalLiteralExpr(){ - - } - - @Override - public void output(StringBuffer buf) { - buf.append("INTERVAL"); - if (sign != null) { - buf.append(sign.charValue()); - } - throw new RuntimeException("TODO"); - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - visitor.visit(this); - - visitor.endVisit(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((sign == null) ? 0 : sign.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - SQLIntervalLiteralExpr other = (SQLIntervalLiteralExpr) obj; - if (sign == null) { - if (other.sign != null) { - return false; - } - } else if (!sign.equals(other.sign)) { - return false; - } - return true; - } - -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLListExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLListExpr.java deleted file mode 100644 index 3bd11154..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLListExpr.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLListExpr extends SQLExprImpl { - - private static final long serialVersionUID = 1L; - - private final List items = new ArrayList(); - - public List getItems() { - return items; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, items); - } - visitor.endVisit(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((items == null) ? 0 : items.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - SQLListExpr other = (SQLListExpr) obj; - if (items == null) { - if (other.items != null) { - return false; - } - } else if (!items.equals(other.items)) { - return false; - } - return true; - } - -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLLiteralExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLLiteralExpr.java deleted file mode 100644 index c015f37e..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLLiteralExpr.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import org.durid.sql.ast.SQLExpr; - -public interface SQLLiteralExpr extends SQLExpr { - - -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLMethodInvokeExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLMethodInvokeExpr.java deleted file mode 100644 index 15074bee..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLMethodInvokeExpr.java +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLMethodInvokeExpr extends SQLExprImpl implements Serializable { - - private static final long serialVersionUID = 1L; - private String methodName; - private SQLExpr owner; - private final List parameters = new ArrayList(); - - public SQLMethodInvokeExpr(){ - - } - - public SQLMethodInvokeExpr(String methodName){ - this.methodName = methodName; - } - - public SQLMethodInvokeExpr(String methodName, SQLExpr owner){ - - this.methodName = methodName; - this.owner = owner; - } - - public String getMethodName() { - return this.methodName; - } - - public void setMethodName(String methodName) { - this.methodName = methodName; - } - - public SQLExpr getOwner() { - return this.owner; - } - - public void setOwner(SQLExpr owner) { - this.owner = owner; - } - - public List getParameters() { - return this.parameters; - } - - public void output(StringBuffer buf) { - if (this.owner != null) { - this.owner.output(buf); - buf.append("."); - } - - buf.append(this.methodName); - buf.append("("); - for (int i = 0, size = this.parameters.size(); i < size; ++i) { - if (i != 0) { - buf.append(", "); - } - - this.parameters.get(i).output(buf); - } - buf.append(")"); - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.owner); - acceptChild(visitor, this.parameters); - } - - visitor.endVisit(this); - } - - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((methodName == null) ? 0 : methodName.hashCode()); - result = prime * result + ((owner == null) ? 0 : owner.hashCode()); - result = prime * result + ((parameters == null) ? 0 : parameters.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - SQLMethodInvokeExpr other = (SQLMethodInvokeExpr) obj; - if (methodName == null) { - if (other.methodName != null) { - return false; - } - } else if (!methodName.equals(other.methodName)) { - return false; - } - if (owner == null) { - if (other.owner != null) { - return false; - } - } else if (!owner.equals(other.owner)) { - return false; - } - if (parameters == null) { - if (other.parameters != null) { - return false; - } - } else if (!parameters.equals(other.parameters)) { - return false; - } - return true; - } -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLNCharExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLNCharExpr.java deleted file mode 100644 index e6aeeef9..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLNCharExpr.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLNCharExpr extends SQLTextLiteralExpr { - - private static final long serialVersionUID = 1L; - - public SQLNCharExpr(){ - - } - - public SQLNCharExpr(String text){ - super(text); - } - - public void output(StringBuffer buf) { - if ((this.text == null) || (this.text.length() == 0)) { - buf.append("NULL"); - return; - } - - buf.append("N'"); - buf.append(this.text.replaceAll("'", "''")); - buf.append("'"); - } - - protected void accept0(SQLASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLNotExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLNotExpr.java deleted file mode 100644 index 6e9e18fb..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLNotExpr.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import java.io.Serializable; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLNotExpr extends SQLExprImpl implements Serializable { - - private static final long serialVersionUID = 1L; - public SQLExpr expr; - - public SQLNotExpr(){ - - } - - public SQLNotExpr(SQLExpr expr){ - - this.expr = expr; - } - - public SQLExpr getExpr() { - return this.expr; - } - - public void setExpr(SQLExpr expr) { - this.expr = expr; - } - - @Override - public void output(StringBuffer buf) { - buf.append(" NOT "); - this.expr.output(buf); - } - - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.expr); - } - - visitor.endVisit(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((expr == null) ? 0 : expr.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - SQLNotExpr other = (SQLNotExpr) obj; - if (expr == null) { - if (other.expr != null) { - return false; - } - } else if (!expr.equals(other.expr)) { - return false; - } - return true; - } -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLNullExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLNullExpr.java deleted file mode 100644 index 6e1de90e..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLNullExpr.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLNullExpr extends SQLExprImpl implements SQLLiteralExpr { - - private static final long serialVersionUID = 1L; - - public SQLNullExpr(){ - - } - - public void output(StringBuffer buf) { - buf.append("NULL"); - } - - protected void accept0(SQLASTVisitor visitor) { - visitor.visit(this); - - visitor.endVisit(this); - } - - public int hashCode() { - return 0; - } - - public boolean equals(Object o) { - return o instanceof SQLNullExpr; - } -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLNumberExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLNumberExpr.java deleted file mode 100644 index bf80c4c8..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLNumberExpr.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLNumberExpr extends SQLNumericLiteralExpr { - - private static final long serialVersionUID = 1L; - - private Number number; - - public SQLNumberExpr(){ - - } - - public SQLNumberExpr(Number number){ - - this.number = number; - } - - public Number getNumber() { - return this.number; - } - - public void setNumber(Number number) { - this.number = number; - } - - public void output(StringBuffer buf) { - buf.append(this.number.toString()); - } - - protected void accept0(SQLASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((number == null) ? 0 : number.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - SQLNumberExpr other = (SQLNumberExpr) obj; - if (number == null) { - if (other.number != null) { - return false; - } - } else if (!number.equals(other.number)) { - return false; - } - return true; - } -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLNumericLiteralExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLNumericLiteralExpr.java deleted file mode 100644 index 247d78b2..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLNumericLiteralExpr.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import org.durid.sql.ast.SQLExprImpl; - -public abstract class SQLNumericLiteralExpr extends SQLExprImpl implements SQLLiteralExpr { - - private static final long serialVersionUID = 1L; - - public SQLNumericLiteralExpr(){ - - } - - public abstract Number getNumber(); - - public abstract void setNumber(Number number); -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLOdbcExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLOdbcExpr.java deleted file mode 100644 index bae09195..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLOdbcExpr.java +++ /dev/null @@ -1,60 +0,0 @@ -package org.durid.sql.ast.expr; - -/** - * Created by jheimbouch on 3/17/15. - */ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLOdbcExpr extends SQLCharExpr { - - private static final long serialVersionUID = 1L; - - public SQLOdbcExpr(){ - - } - - public SQLOdbcExpr(String text){ - super(text); - } - - @Override - public void output(StringBuffer buf) { - if ((this.text == null) || (this.text.length() == 0)) { - buf.append("NULL"); - } else { - buf.append("{ts '"); - buf.append(this.text.replaceAll("'", "''")); - buf.append("'}"); - } - } - - @Override - public String getText() { - StringBuilder sb = new StringBuilder(); - sb.append("{ts '"); - sb.append(this.text); - sb.append("'}"); - return sb.toString(); - } - - protected void accept0(SQLASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLPropertyExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLPropertyExpr.java deleted file mode 100644 index c5453947..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLPropertyExpr.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.ast.SQLName; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLPropertyExpr extends SQLExprImpl implements SQLName { - - private static final long serialVersionUID = 1L; - - private SQLExpr owner; - private String name; - - public SQLPropertyExpr(SQLExpr owner, String name){ - - this.owner = owner; - this.name = name; - } - - public SQLPropertyExpr(){ - - } - - public String getSimleName() { - return name; - } - - public SQLExpr getOwner() { - return this.owner; - } - - public void setOwner(SQLExpr owner) { - this.owner = owner; - } - - public String getName() { - return this.name; - } - - public void setName(String name) { - this.name = name; - } - - public void output(StringBuffer buf) { - this.owner.output(buf); - buf.append("."); - buf.append(this.name); - } - - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.owner); - } - - visitor.endVisit(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((name == null) ? 0 : name.hashCode()); - result = prime * result + ((owner == null) ? 0 : owner.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (!(obj instanceof SQLPropertyExpr)) { - return false; - } - SQLPropertyExpr other = (SQLPropertyExpr) obj; - if (name == null) { - if (other.name != null) { - return false; - } - } else if (!name.equals(other.name)) { - return false; - } - if (owner == null) { - if (other.owner != null) { - return false; - } - } else if (!owner.equals(other.owner)) { - return false; - } - return true; - } - -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLQueryExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLQueryExpr.java deleted file mode 100644 index 57d71e55..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLQueryExpr.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import java.io.Serializable; - -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.ast.statement.SQLSelect; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLQueryExpr extends SQLExprImpl implements Serializable { - - private static final long serialVersionUID = 1L; - public SQLSelect subQuery; - - public SQLQueryExpr(){ - - } - - public SQLQueryExpr(SQLSelect select){ - - this.subQuery = select; - } - - public SQLSelect getSubQuery() { - return this.subQuery; - } - - public void setSubQuery(SQLSelect subQuery) { - this.subQuery = subQuery; - } - - public void output(StringBuffer buf) { - this.subQuery.output(buf); - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.subQuery); - } - - visitor.endVisit(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((subQuery == null) ? 0 : subQuery.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - SQLQueryExpr other = (SQLQueryExpr) obj; - if (subQuery == null) { - if (other.subQuery != null) { - return false; - } - } else if (!subQuery.equals(other.subQuery)) { - return false; - } - return true; - } -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLSomeExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLSomeExpr.java deleted file mode 100644 index 668cacfa..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLSomeExpr.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.ast.statement.SQLSelect; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLSomeExpr extends SQLExprImpl { - - private static final long serialVersionUID = 1L; - public SQLSelect subQuery; - - public SQLSomeExpr(){ - - } - - public SQLSomeExpr(SQLSelect select){ - - this.subQuery = select; - } - - public SQLSelect getSubQuery() { - return this.subQuery; - } - - public void setSubQuery(SQLSelect subQuery) { - this.subQuery = subQuery; - } - - public void output(StringBuffer buf) { - this.subQuery.output(buf); - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.subQuery); - } - - visitor.endVisit(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((subQuery == null) ? 0 : subQuery.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - SQLSomeExpr other = (SQLSomeExpr) obj; - if (subQuery == null) { - if (other.subQuery != null) { - return false; - } - } else if (!subQuery.equals(other.subQuery)) { - return false; - } - return true; - } -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLTextLiteralExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLTextLiteralExpr.java deleted file mode 100644 index abeb6bc9..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLTextLiteralExpr.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import org.durid.sql.ast.SQLExprImpl; - -public abstract class SQLTextLiteralExpr extends SQLExprImpl implements SQLLiteralExpr { - - private static final long serialVersionUID = 1L; - - protected String text; - - public SQLTextLiteralExpr(){ - - } - - public SQLTextLiteralExpr(String text){ - - this.text = text; - } - - public String getText() { - return this.text; - } - - public void setText(String text) { - this.text = text; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((text == null) ? 0 : text.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - SQLTextLiteralExpr other = (SQLTextLiteralExpr) obj; - if (text == null) { - if (other.text != null) { - return false; - } - } else if (!text.equals(other.text)) { - return false; - } - return true; - } -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLUnaryExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLUnaryExpr.java deleted file mode 100644 index 61856ac0..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLUnaryExpr.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import java.io.Serializable; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLUnaryExpr extends SQLExprImpl implements Serializable { - - private static final long serialVersionUID = 1L; - private SQLExpr expr; - private SQLUnaryOperator operator; - - public SQLUnaryExpr(){ - - } - - public SQLUnaryExpr(SQLUnaryOperator operator, SQLExpr expr){ - this.operator = operator; - this.expr = expr; - } - - public SQLUnaryOperator getOperator() { - return operator; - } - - public void setOperator(SQLUnaryOperator operator) { - this.operator = operator; - } - - public SQLExpr getExpr() { - return this.expr; - } - - public void setExpr(SQLExpr expr) { - this.expr = expr; - } - - @Override - public void output(StringBuffer buf) { - buf.append(" NOT "); - this.expr.output(buf); - } - - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.expr); - } - - visitor.endVisit(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((expr == null) ? 0 : expr.hashCode()); - result = prime * result + ((operator == null) ? 0 : operator.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - SQLUnaryExpr other = (SQLUnaryExpr) obj; - if (expr == null) { - if (other.expr != null) { - return false; - } - } else if (!expr.equals(other.expr)) { - return false; - } - if (operator != other.operator) { - return false; - } - return true; - } -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLUnaryOperator.java b/src/main/java/org/durid/sql/ast/expr/SQLUnaryOperator.java deleted file mode 100644 index cde83ca0..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLUnaryOperator.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -public enum SQLUnaryOperator { - Plus("+"), Negative("-"), Not("!"), Compl("~"), Prior("PRIOR"), ConnectByRoot("CONNECT BY"), NOT("NOT"); - - public final String name; - - SQLUnaryOperator(String name){ - this.name = name; - } -} diff --git a/src/main/java/org/durid/sql/ast/expr/SQLVariantRefExpr.java b/src/main/java/org/durid/sql/ast/expr/SQLVariantRefExpr.java deleted file mode 100644 index 3dea4652..00000000 --- a/src/main/java/org/durid/sql/ast/expr/SQLVariantRefExpr.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.expr; - -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLVariantRefExpr extends SQLExprImpl { - - private static final long serialVersionUID = 1L; - - private String name; - - private boolean global = false; - - private int index = -1; - - public SQLVariantRefExpr(String name){ - this.name = name; - } - - public SQLVariantRefExpr(String name, boolean global){ - this.name = name; - this.global = global; - } - - public SQLVariantRefExpr(){ - - } - - public int getIndex() { - return index; - } - - public void setIndex(int index) { - this.index = index; - } - - public String getName() { - return this.name; - } - - public void setName(String name) { - this.name = name; - } - - public void output(StringBuffer buf) { - buf.append(this.name); - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - visitor.visit(this); - - visitor.endVisit(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((name == null) ? 0 : name.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (!(obj instanceof SQLVariantRefExpr)) { - return false; - } - SQLVariantRefExpr other = (SQLVariantRefExpr) obj; - if (name == null) { - if (other.name != null) { - return false; - } - } else if (!name.equals(other.name)) { - return false; - } - return true; - } - - public boolean isGlobal() { - return global; - } - - public void setGlobal(boolean global) { - this.global = global; - } - -} diff --git a/src/main/java/org/durid/sql/ast/statement/NotNullConstraint.java b/src/main/java/org/durid/sql/ast/statement/NotNullConstraint.java deleted file mode 100644 index e45aafea..00000000 --- a/src/main/java/org/durid/sql/ast/statement/NotNullConstraint.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.visitor.SQLASTVisitor; - -@SuppressWarnings("serial") -public class NotNullConstraint extends SQLConstaintImpl implements SQLColumnConstraint { - - public NotNullConstraint(){ - - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLAlterTableAddColumn.java b/src/main/java/org/durid/sql/ast/statement/SQLAlterTableAddColumn.java deleted file mode 100644 index b893df28..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLAlterTableAddColumn.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLObjectImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLAlterTableAddColumn extends SQLObjectImpl implements SQLAlterTableItem { - - private static final long serialVersionUID = 1L; - - private List columns = new ArrayList(); - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, columns); - } - visitor.endVisit(this); - } - - public List getColumns() { - return columns; - } - - public void setColumns(List columns) { - this.columns = columns; - } - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLAlterTableAddIndex.java b/src/main/java/org/durid/sql/ast/statement/SQLAlterTableAddIndex.java deleted file mode 100644 index 70236931..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLAlterTableAddIndex.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLObjectImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLAlterTableAddIndex extends SQLObjectImpl implements SQLAlterTableItem { - - private static final long serialVersionUID = 1L; - - private SQLName name; - - private List items = new ArrayList(); - - private String type; - - @Override - protected void accept0(SQLASTVisitor visitor) { - - } - - public List getItems() { - return items; - } - - public void setItems(List items) { - this.items = items; - } - - public SQLName getName() { - return name; - } - - public void setName(SQLName name) { - this.name = name; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLAlterTableAddPrimaryKey.java b/src/main/java/org/durid/sql/ast/statement/SQLAlterTableAddPrimaryKey.java deleted file mode 100644 index b9421cad..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLAlterTableAddPrimaryKey.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLObjectImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLAlterTableAddPrimaryKey extends SQLObjectImpl implements SQLAlterTableItem { - - private static final long serialVersionUID = 1L; - private SQLPrimaryKey primaryKey; - - public SQLPrimaryKey getPrimaryKey() { - return primaryKey; - } - - public void setPrimaryKey(SQLPrimaryKey primaryKey) { - this.primaryKey = primaryKey; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, primaryKey); - } - visitor.endVisit(this); - } - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLAlterTableDropColumnItem.java b/src/main/java/org/durid/sql/ast/statement/SQLAlterTableDropColumnItem.java deleted file mode 100644 index a0f207fd..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLAlterTableDropColumnItem.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLObjectImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLAlterTableDropColumnItem extends SQLObjectImpl implements SQLAlterTableItem { - - private static final long serialVersionUID = 1L; - private SQLName columnName; - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, columnName); - } - visitor.endVisit(this); - } - - public SQLName getColumnName() { - return columnName; - } - - public void setColumnName(SQLName columnName) { - this.columnName = columnName; - } - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLAlterTableDropIndex.java b/src/main/java/org/durid/sql/ast/statement/SQLAlterTableDropIndex.java deleted file mode 100644 index b36d6896..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLAlterTableDropIndex.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLObjectImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLAlterTableDropIndex extends SQLObjectImpl implements SQLAlterTableItem { - - private static final long serialVersionUID = 1L; - - private SQLName indexName; - - public SQLName getIndexName() { - return indexName; - } - - public void setIndexName(SQLName indexName) { - this.indexName = indexName; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, indexName); - } - visitor.endVisit(this); - } - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLAlterTableItem.java b/src/main/java/org/durid/sql/ast/statement/SQLAlterTableItem.java deleted file mode 100644 index b026b21f..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLAlterTableItem.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLObject; - -public interface SQLAlterTableItem extends SQLObject { - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLAlterTableStatement.java b/src/main/java/org/durid/sql/ast/statement/SQLAlterTableStatement.java deleted file mode 100644 index eeaa44bc..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLAlterTableStatement.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLStatementImpl; - -public class SQLAlterTableStatement extends SQLStatementImpl implements SQLDDLStatement { - - private static final long serialVersionUID = 1L; - - private SQLExprTableSource tableSource; - private List items = new ArrayList(); - - public List getItems() { - return items; - } - - public void setItems(List items) { - this.items = items; - } - - public SQLExprTableSource getTableSource() { - return tableSource; - } - - public void setTableSource(SQLExprTableSource tableSource) { - this.tableSource = tableSource; - } - - public SQLName getName() { - if (getTableSource() == null) { - return null; - } - return (SQLName) getTableSource().getExpr(); - } - - public void setName(SQLName name) { - this.setTableSource(new SQLExprTableSource(name)); - } -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLAssignItem.java b/src/main/java/org/durid/sql/ast/statement/SQLAssignItem.java deleted file mode 100644 index a904b96a..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLAssignItem.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLObjectImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLAssignItem extends SQLObjectImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr target; - private SQLExpr value; - - public SQLAssignItem(){ - } - - public SQLAssignItem(SQLExpr target, SQLExpr value){ - this.target = target; - this.value = value; - } - - public SQLExpr getTarget() { - return target; - } - - public void setTarget(SQLExpr target) { - this.target = target; - } - - public SQLExpr getValue() { - return value; - } - - public void setValue(SQLExpr value) { - this.value = value; - } - - public void output(StringBuffer buf) { - target.output(buf); - buf.append(" = "); - value.output(buf); - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.target); - acceptChild(visitor, this.value); - } - visitor.endVisit(this); - } - -} \ No newline at end of file diff --git a/src/main/java/org/durid/sql/ast/statement/SQLCallStatement.java b/src/main/java/org/durid/sql/ast/statement/SQLCallStatement.java deleted file mode 100644 index affcbbe7..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLCallStatement.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLStatementImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLCallStatement extends SQLStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLName procedureName; - - private final List parameters = new ArrayList(); - - public SQLName getProcedureName() { - return procedureName; - } - - public void setProcedureName(SQLName procedureName) { - this.procedureName = procedureName; - } - - public List getParameters() { - return parameters; - } - - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.procedureName); - acceptChild(visitor, this.parameters); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLCharactorDataType.java b/src/main/java/org/durid/sql/ast/statement/SQLCharactorDataType.java deleted file mode 100644 index bd925ce2..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLCharactorDataType.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLDataTypeImpl; - -@SuppressWarnings("serial") -public class SQLCharactorDataType extends SQLDataTypeImpl { - - private String charSetName; - private String collate; - - public SQLCharactorDataType(String name){ - super(name); - } - - public String getCharSetName() { - return charSetName; - } - - public void setCharSetName(String charSetName) { - this.charSetName = charSetName; - } - - public String getCollate() { - return collate; - } - - public void setCollate(String collate) { - this.collate = collate; - } - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLColumnConstraint.java b/src/main/java/org/durid/sql/ast/statement/SQLColumnConstraint.java deleted file mode 100644 index 5c720574..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLColumnConstraint.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -public interface SQLColumnConstraint extends SQLConstaint { - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLColumnDefinition.java b/src/main/java/org/durid/sql/ast/statement/SQLColumnDefinition.java deleted file mode 100644 index 1d8116e4..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLColumnDefinition.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLDataType; -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLObjectImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -@SuppressWarnings("serial") -public class SQLColumnDefinition extends SQLObjectImpl implements SQLTableElement { - - private SQLName name; - private SQLDataType dataType; - private SQLExpr defaultExpr; - private final List constaints = new ArrayList(0); - private String comment; - - private Boolean enable; - - public SQLColumnDefinition(){ - - } - - public Boolean getEnable() { - return enable; - } - - public void setEnable(Boolean enable) { - this.enable = enable; - } - - public SQLName getName() { - return name; - } - - public void setName(SQLName name) { - this.name = name; - } - - public SQLDataType getDataType() { - return dataType; - } - - public void setDataType(SQLDataType dataType) { - this.dataType = dataType; - } - - public SQLExpr getDefaultExpr() { - return defaultExpr; - } - - public void setDefaultExpr(SQLExpr defaultExpr) { - this.defaultExpr = defaultExpr; - } - - public List getConstaints() { - return constaints; - } - - @Override - public void output(StringBuffer buf) { - name.output(buf); - buf.append(' '); - this.dataType.output(buf); - if (defaultExpr != null) { - buf.append(" DEFAULT "); - this.defaultExpr.output(buf); - } - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - this.acceptChild(visitor, name); - this.acceptChild(visitor, dataType); - this.acceptChild(visitor, defaultExpr); - this.acceptChild(visitor, constaints); - } - visitor.endVisit(this); - } - - public String getComment() { - return comment; - } - - public void setComment(String comment) { - this.comment = comment; - } - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLCommentStatement.java b/src/main/java/org/durid/sql/ast/statement/SQLCommentStatement.java deleted file mode 100644 index ea1d9109..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLCommentStatement.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLObjectImpl; -import org.durid.sql.ast.SQLStatement; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLCommentStatement extends SQLObjectImpl implements SQLStatement { - - private static final long serialVersionUID = 1L; - - public static enum Type { - TABLE, COLUMN - } - - private SQLExpr on; - private Type type; - private SQLExpr comment; - - public SQLExpr getComment() { - return comment; - } - - public void setComment(SQLExpr comment) { - this.comment = comment; - } - - public Type getType() { - return type; - } - - public void setType(Type type) { - this.type = type; - } - - public SQLExpr getOn() { - return on; - } - - public void setOn(SQLExpr on) { - this.on = on; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, on); - acceptChild(visitor, comment); - } - visitor.endVisit(this); - } - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLConstaint.java b/src/main/java/org/durid/sql/ast/statement/SQLConstaint.java deleted file mode 100644 index 09d12d20..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLConstaint.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLObject; - -public interface SQLConstaint extends SQLObject { - - SQLName getName(); - - void setName(SQLName value); -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLConstaintImpl.java b/src/main/java/org/durid/sql/ast/statement/SQLConstaintImpl.java deleted file mode 100644 index a8d8f84e..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLConstaintImpl.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLObjectImpl; - -@SuppressWarnings("serial") -public abstract class SQLConstaintImpl extends SQLObjectImpl implements SQLConstaint { - - private SQLName name; - - public SQLConstaintImpl(){ - - } - - public SQLName getName() { - return name; - } - - public void setName(SQLName name) { - this.name = name; - } - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLCreateDatabaseStatement.java b/src/main/java/org/durid/sql/ast/statement/SQLCreateDatabaseStatement.java deleted file mode 100644 index fa415e76..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLCreateDatabaseStatement.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLStatementImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLCreateDatabaseStatement extends SQLStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLName name; - - private String characterSet; - private String collate; - - public SQLCreateDatabaseStatement(){ - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, name); - } - visitor.endVisit(this); - } - - public SQLName getName() { - return name; - } - - public void setName(SQLName name) { - this.name = name; - } - - public String getCharacterSet() { - return characterSet; - } - - public void setCharacterSet(String characterSet) { - this.characterSet = characterSet; - } - - public String getCollate() { - return collate; - } - - public void setCollate(String collate) { - this.collate = collate; - } - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLCreateIndexStatement.java b/src/main/java/org/durid/sql/ast/statement/SQLCreateIndexStatement.java deleted file mode 100644 index 47fe03cb..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLCreateIndexStatement.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLStatementImpl; - -public class SQLCreateIndexStatement extends SQLStatementImpl implements SQLDDLStatement { - - /** - * - */ - private static final long serialVersionUID = 1L; - - private SQLName name; - - private SQLName table; - - private List items = new ArrayList(); - - private String type; - - public SQLName getTable() { - return table; - } - - public void setTable(SQLName table) { - this.table = table; - } - - public List getItems() { - return items; - } - - public void setItems(List items) { - this.items = items; - } - - public SQLName getName() { - return name; - } - - public void setName(SQLName name) { - this.name = name; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLCreateTableStatement.java b/src/main/java/org/durid/sql/ast/statement/SQLCreateTableStatement.java deleted file mode 100644 index 13713bbe..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLCreateTableStatement.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLStatementImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -@SuppressWarnings("serial") -public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLStatement { - - protected Type type; - protected SQLExprTableSource tableSource; - - protected List tableElementList = new ArrayList(); - - public SQLCreateTableStatement(){ - - } - - public SQLName getName() { - if (tableSource == null) { - return null; - } - - return (SQLName) tableSource.getExpr(); - } - - public void setName(SQLName name) { - this.setTableSource(new SQLExprTableSource(name)); - } - - public SQLExprTableSource getTableSource() { - return tableSource; - } - - public void setTableSource(SQLExprTableSource tableSource) { - this.tableSource = tableSource; - } - - public Type getType() { - return type; - } - - public void setType(Type type) { - this.type = type; - } - - public static enum Type { - GLOBAL_TEMPORARY, LOCAL_TEMPORARY - } - - public List getTableElementList() { - return tableElementList; - } - - @Override - public void output(StringBuffer buf) { - buf.append("CREATE TABLE "); - if (Type.GLOBAL_TEMPORARY.equals(this.type)) { - buf.append("GLOBAL TEMPORARY "); - } else if (Type.LOCAL_TEMPORARY.equals(this.type)) { - buf.append("LOCAL TEMPORARY "); - } - - this.tableSource.output(buf); - buf.append(" "); - - buf.append("("); - for (int i = 0, size = tableElementList.size(); i < size; ++i) { - if (i != 0) { - buf.append(", "); - } - tableElementList.get(i).output(buf); - } - buf.append(")"); - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - this.acceptChild(visitor, tableSource); - this.acceptChild(visitor, tableElementList); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLCreateViewStatement.java b/src/main/java/org/durid/sql/ast/statement/SQLCreateViewStatement.java deleted file mode 100644 index 93147c67..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLCreateViewStatement.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLStatementImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLCreateViewStatement extends SQLStatementImpl implements SQLDDLStatement { - - private static final long serialVersionUID = 1L; - protected SQLName name; - protected SQLSelect subQuery; - - protected final List columns = new ArrayList(); - - private Level with; - - public SQLCreateViewStatement(){ - - } - - public SQLName getName() { - return name; - } - - public void setName(SQLName name) { - this.name = name; - } - - public Level getWith() { - return with; - } - - public void setWith(Level with) { - this.with = with; - } - - public SQLSelect getSubQuery() { - return subQuery; - } - - public void setSubQuery(SQLSelect subQuery) { - this.subQuery = subQuery; - } - - public List getColumns() { - return columns; - } - - public void output(StringBuffer buf) { - buf.append("CREATE VIEW "); - this.name.output(buf); - - if (this.columns.size() > 0) { - buf.append(" ("); - for (int i = 0, size = this.columns.size(); i < size; ++i) { - if (i != 0) { - buf.append(", "); - } - this.columns.get(i).output(buf); - } - buf.append(")"); - } - - buf.append(" AS "); - this.subQuery.output(buf); - - if (this.with != null) { - buf.append(" WITH "); - buf.append(this.with.name()); - } - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.name); - acceptChild(visitor, this.columns); - acceptChild(visitor, this.subQuery); - } - visitor.endVisit(this); - } - - public static enum Level { - CASCADED, LOCAL - } -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLDDLStatement.java b/src/main/java/org/durid/sql/ast/statement/SQLDDLStatement.java deleted file mode 100644 index 228bb7d6..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLDDLStatement.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLStatement; - - -public interface SQLDDLStatement extends SQLStatement { - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLDeleteStatement.java b/src/main/java/org/durid/sql/ast/statement/SQLDeleteStatement.java deleted file mode 100644 index 20128232..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLDeleteStatement.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLStatementImpl; -import org.durid.sql.ast.expr.SQLIdentifierExpr; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLDeleteStatement extends SQLStatementImpl { - - private static final long serialVersionUID = 1L; - protected SQLTableSource tableSource; - protected SQLExpr where; - - public SQLDeleteStatement(){ - - } - - public SQLTableSource getTableSource() { - return tableSource; - } - - public SQLExprTableSource getExprTableSource() { - return (SQLExprTableSource) getTableSource(); - } - - public void setTableSource(SQLExpr expr) { - this.setTableSource(new SQLExprTableSource(expr)); - } - - public void setTableSource(SQLTableSource tableSource) { - this.tableSource = tableSource; - } - - public SQLName getTableName() { - return (SQLName) getExprTableSource().getExpr(); - } - - - public SQLExpr getWhere() { - return where; - } - - public void setWhere(SQLExpr where) { - this.where = where; - } - - public String getAlias() { - return this.tableSource.getAlias(); - } - - public void setAlias(String alias) { - this.tableSource.setAlias(alias); - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, tableSource); - acceptChild(visitor, where); - } - - visitor.endVisit(this); - } - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLDropIndexStatement.java b/src/main/java/org/durid/sql/ast/statement/SQLDropIndexStatement.java deleted file mode 100644 index cd7643fe..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLDropIndexStatement.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLStatementImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLDropIndexStatement extends SQLStatementImpl implements SQLDDLStatement { - - private static final long serialVersionUID = 1L; - - private SQLExpr indexName; - private SQLExpr tableName; - - public SQLExpr getIndexName() { - return indexName; - } - - public void setIndexName(SQLExpr indexName) { - this.indexName = indexName; - } - - public SQLExpr getTableName() { - return tableName; - } - - public void setTableName(SQLExpr tableName) { - this.tableName = tableName; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, indexName); - acceptChild(visitor, tableName); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLDropTableStatement.java b/src/main/java/org/durid/sql/ast/statement/SQLDropTableStatement.java deleted file mode 100644 index dac0ee28..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLDropTableStatement.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLStatementImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLDropTableStatement extends SQLStatementImpl implements SQLDDLStatement { - - private static final long serialVersionUID = 1L; - - protected List tableSources = new ArrayList(); - - public SQLDropTableStatement(){ - - } - - public SQLDropTableStatement(SQLName name){ - this(new SQLExprTableSource(name)); - } - - public SQLDropTableStatement(SQLExprTableSource tableSource){ - this.tableSources.add(tableSource); - } - - public List getTableSources() { - return tableSources; - } - - public void setTableSources(List tableSources) { - this.tableSources = tableSources; - } - - public void setName(SQLName name) { - this.addTableSource(new SQLExprTableSource(name)); - } - - public void addTableSource(SQLName name) { - this.addTableSource(new SQLExprTableSource(name)); - } - - public void addTableSource(SQLExprTableSource tableSource) { - tableSources.add(tableSource); - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - this.acceptChild(visitor, tableSources); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLDropViewStatement.java b/src/main/java/org/durid/sql/ast/statement/SQLDropViewStatement.java deleted file mode 100644 index 869d6f41..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLDropViewStatement.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLStatementImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLDropViewStatement extends SQLStatementImpl implements SQLDDLStatement { - - private static final long serialVersionUID = 1L; - - protected List tableSources = new ArrayList(); - - public SQLDropViewStatement(){ - - } - - public SQLDropViewStatement(SQLName name){ - this(new SQLExprTableSource(name)); - } - - public SQLDropViewStatement(SQLExprTableSource tableSource){ - this.tableSources.add(tableSource); - } - - public List getTableSources() { - return tableSources; - } - - public void setTableSources(List tableSources) { - this.tableSources = tableSources; - } - - public void setName(SQLName name) { - this.addTableSource(new SQLExprTableSource(name)); - } - - public void addTableSource(SQLName name) { - this.addTableSource(new SQLExprTableSource(name)); - } - - public void addTableSource(SQLExprTableSource tableSource) { - tableSources.add(tableSource); - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - this.acceptChild(visitor, tableSources); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLExprTableSource.java b/src/main/java/org/durid/sql/ast/statement/SQLExprTableSource.java deleted file mode 100644 index 22e0008d..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLExprTableSource.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLExprTableSource extends SQLTableSourceImpl { - - private static final long serialVersionUID = 1L; - - protected SQLExpr expr; - private String tablename; - - public SQLExprTableSource(){ - - } - - public SQLExprTableSource(SQLExpr expr){ - this.expr = expr; - this.tablename = expr.toString().replace(" ", ""); - } - - public SQLExprTableSource(String tablename){ - this.tablename = tablename; - } - - public SQLExpr getExpr() { - return this.expr; - } - - public void setExpr(SQLExpr expr) { - this.expr = expr; - this.tablename = expr.toString().replace(" ", ""); - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.expr); - } - visitor.endVisit(this); - } - - public void output(StringBuffer buf) { - this.expr.output(buf); - } - - @Override - public String getTablename() { - return this.tablename; - } -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLForeignKeyConstraint.java b/src/main/java/org/durid/sql/ast/statement/SQLForeignKeyConstraint.java deleted file mode 100644 index 7d33074b..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLForeignKeyConstraint.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import java.util.List; - -import org.durid.sql.ast.SQLName; - -public interface SQLForeignKeyConstraint extends SQLConstaint { - - List getReferencingColumns(); - - SQLName getReferencedTableName(); - - void setReferencedTableName(SQLName value); - - List getReferencedColumns(); -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLIndexDefinition.java b/src/main/java/org/durid/sql/ast/statement/SQLIndexDefinition.java deleted file mode 100644 index 5244f297..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLIndexDefinition.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - - -public class SQLIndexDefinition { - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLInsertInto.java b/src/main/java/org/durid/sql/ast/statement/SQLInsertInto.java deleted file mode 100644 index aa5358a3..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLInsertInto.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLObjectImpl; -import org.durid.sql.ast.statement.SQLInsertStatement.ValuesClause; - -public abstract class SQLInsertInto extends SQLObjectImpl { - - private static final long serialVersionUID = 1L; - protected SQLExprTableSource tableSource; - - protected final List columns = new ArrayList(); - protected ValuesClause values; - protected SQLSelect query; - - public SQLInsertInto(){ - - } - - public String getAlias() { - return tableSource.getAlias(); - } - - public void setAlias(String alias) { - this.tableSource.setAlias(alias); - } - - public SQLExprTableSource getTableSource() { - return tableSource; - } - - public void setTableSource(SQLExprTableSource tableSource) { - this.tableSource = tableSource; - } - - public SQLName getTableName() { - return (SQLName) tableSource.getExpr(); - } - - public void setTableName(SQLName tableName) { - this.setTableSource(new SQLExprTableSource(tableName)); - } - - public void setTableSource(SQLName tableName) { - this.setTableSource(new SQLExprTableSource(tableName)); - } - - public SQLSelect getQuery() { - return query; - } - - public void setQuery(SQLSelect query) { - this.query = query; - } - - public List getColumns() { - return columns; - } - - public ValuesClause getValues() { - return values; - } - - public void setValues(ValuesClause values) { - this.values = values; - } -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLInsertStatement.java b/src/main/java/org/durid/sql/ast/statement/SQLInsertStatement.java deleted file mode 100644 index 9927cd19..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLInsertStatement.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLObjectImpl; -import org.durid.sql.ast.SQLStatement; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLInsertStatement extends SQLInsertInto implements SQLStatement { - - private static final long serialVersionUID = 1L; - - public SQLInsertStatement(){ - - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - this.acceptChild(visitor, tableSource); - this.acceptChild(visitor, columns); - this.acceptChild(visitor, values); - this.acceptChild(visitor, query); - } - - visitor.endVisit(this); - } - - public static class ValuesClause extends SQLObjectImpl { - - private static final long serialVersionUID = 1L; - private final List values = new ArrayList(); - - public List getValues() { - return values; - } - - public void output(StringBuffer buf) { - buf.append(" VALUES ("); - for (int i = 0, size = values.size(); i < size; ++i) { - if (i != 0) { - buf.append(", "); - } - values.get(i).output(buf); - } - buf.append(")"); - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - this.acceptChild(visitor, values); - } - - visitor.endVisit(this); - } - } -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLJoinTableSource.java b/src/main/java/org/durid/sql/ast/statement/SQLJoinTableSource.java deleted file mode 100644 index d3cb365e..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLJoinTableSource.java +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLJoinTableSource extends SQLTableSourceImpl { - - private static final long serialVersionUID = 1L; - - protected SQLTableSource left; - protected JoinType joinType; - protected SQLTableSource right; - protected SQLExpr condition; - - public SQLJoinTableSource(String alias){ - super(alias); - } - - public SQLJoinTableSource(){ - - } - - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.left); - acceptChild(visitor, this.right); - acceptChild(visitor, this.condition); - } - - visitor.endVisit(this); - } - - public JoinType getJoinType() { - return this.joinType; - } - - public void setJoinType(JoinType joinType) { - this.joinType = joinType; - } - - public SQLTableSource getLeft() { - return this.left; - } - - public void setLeft(SQLTableSource left) { - this.left = left; - } - - public SQLTableSource getRight() { - return this.right; - } - - public void setRight(SQLTableSource right) { - this.right = right; - } - - public SQLExpr getCondition() { - return this.condition; - } - - public void setCondition(SQLExpr condition) { - this.condition = condition; - } - - public void output(StringBuffer buf) { - this.left.output(buf); - buf.append(' '); - buf.append(JoinType.toString(this.joinType)); - buf.append(' '); - this.right.output(buf); - - if (this.condition != null) { - buf.append(" ON "); - this.condition.output(buf); - } - } - - @Override - public String getTablename() { - return this.toString().replace(" ", ""); - } - - public static enum JoinType { - COMMA(","), // - JOIN("JOIN"), // - INNER_JOIN("INNER JOIN"), // - CROSS_JOIN("CROSS JOIN"), // - NATURAL_JOIN("NATURAL JOIN"), // - NATURAL_INNER_JOIN("NATURAL INNER JOIN"), // - LEFT_OUTER_JOIN("LEFT JOIN"), - RIGHT_OUTER_JOIN("RIGHT JOIN"), - FULL_OUTER_JOIN("FULL JOIN"), - STRAIGHT_JOIN("STRAIGHT_JOIN"); - - public final String name; - - JoinType(String name){ - this.name = name; - } - public static String toString(JoinType joinType) { - return joinType.name; - } - } -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLPrimaryKey.java b/src/main/java/org/durid/sql/ast/statement/SQLPrimaryKey.java deleted file mode 100644 index b8920c33..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLPrimaryKey.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -public interface SQLPrimaryKey extends SQLUniqueConstraint, SQLTableElement { - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLReleaseSavePointStatement.java b/src/main/java/org/durid/sql/ast/statement/SQLReleaseSavePointStatement.java deleted file mode 100644 index 3454efa9..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLReleaseSavePointStatement.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLStatementImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLReleaseSavePointStatement extends SQLStatementImpl { - - private static final long serialVersionUID = 1L; - private SQLExpr name; - - public SQLExpr getName() { - return name; - } - - public void setName(SQLExpr name) { - this.name = name; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, name); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLRollbackStatement.java b/src/main/java/org/durid/sql/ast/statement/SQLRollbackStatement.java deleted file mode 100644 index 57e7fe52..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLRollbackStatement.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLStatementImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLRollbackStatement extends SQLStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLName to; - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, to); - } - visitor.endVisit(this); - } - - public SQLName getTo() { - return to; - } - - public void setTo(SQLName to) { - this.to = to; - } - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLSavePointStatement.java b/src/main/java/org/durid/sql/ast/statement/SQLSavePointStatement.java deleted file mode 100644 index afb6c589..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLSavePointStatement.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLStatementImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLSavePointStatement extends SQLStatementImpl { - - private static final long serialVersionUID = 1L; - private SQLExpr name; - - public SQLExpr getName() { - return name; - } - - public void setName(SQLExpr name) { - this.name = name; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, name); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLSelect.java b/src/main/java/org/durid/sql/ast/statement/SQLSelect.java deleted file mode 100644 index 9603c080..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLSelect.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLObjectImpl; -import org.durid.sql.ast.SQLOrderBy; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLSelect extends SQLObjectImpl { - - private static final long serialVersionUID = 1L; - - protected SQLSelectQuery query; - protected SQLOrderBy orderBy; - - public SQLSelect(){ - - } - - public SQLSelectQuery getQuery() { - return this.query; - } - - public void setQuery(SQLSelectQuery query) { - this.query = query; - } - - public SQLOrderBy getOrderBy() { - return this.orderBy; - } - - public void setOrderBy(SQLOrderBy orderBy) { - this.orderBy = orderBy; - } - - public void output(StringBuffer buf) { - this.query.output(buf); - buf.append(" "); - - if (this.orderBy != null) { - this.orderBy.output(buf); - } - } - - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.query); - acceptChild(visitor, this.orderBy); - } - - visitor.endVisit(this); - } - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLSelectGroupByClause.java b/src/main/java/org/durid/sql/ast/statement/SQLSelectGroupByClause.java deleted file mode 100644 index 150bd085..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLSelectGroupByClause.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLObjectImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLSelectGroupByClause extends SQLObjectImpl { - - private static final long serialVersionUID = 1L; - - private final List items = new ArrayList(); - private SQLExpr having; - - public SQLSelectGroupByClause(){ - - } - - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.items); - acceptChild(visitor, this.having); - } - - visitor.endVisit(this); - } - - public SQLExpr getHaving() { - return this.having; - } - - public void setHaving(SQLExpr having) { - this.having = having; - } - - public List getItems() { - return this.items; - } -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLSelectItem.java b/src/main/java/org/durid/sql/ast/statement/SQLSelectItem.java deleted file mode 100644 index d732d2e8..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLSelectItem.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLObjectImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLSelectItem extends SQLObjectImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr expr; - private String alias; - - public SQLSelectItem(){ - - } - - public SQLSelectItem(SQLExpr expr) { - this(expr, null); - } - - public SQLSelectItem(SQLExpr expr, String alias){ - - this.expr = expr; - this.alias = alias; - } - - public SQLExpr getExpr() { - return this.expr; - } - - public void setExpr(SQLExpr expr) { - this.expr = expr; - } - - public String getAlias() { - return this.alias; - } - - public void setAlias(String alias) { - this.alias = alias; - } - - public void output(StringBuffer buf) { - this.expr.output(buf); - if ((this.alias != null) && (this.alias.length() != 0)) { - buf.append(" AS "); - buf.append(this.alias); - } - } - - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.expr); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLSelectOrderByItem.java b/src/main/java/org/durid/sql/ast/statement/SQLSelectOrderByItem.java deleted file mode 100644 index 743c9597..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLSelectOrderByItem.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLObjectImpl; -import org.durid.sql.ast.SQLOrderingSpecification; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLSelectOrderByItem extends SQLObjectImpl { - - private static final long serialVersionUID = 1L; - - protected SQLExpr expr; - protected String collate; - protected SQLOrderingSpecification type; - - public SQLSelectOrderByItem(){ - - } - - public SQLExpr getExpr() { - return this.expr; - } - - public void setExpr(SQLExpr expr) { - this.expr = expr; - } - - public String getCollate() { - return collate; - } - - public void setCollate(String collate) { - this.collate = collate; - } - - public SQLOrderingSpecification getType() { - return this.type; - } - - public void setType(SQLOrderingSpecification type) { - this.type = type; - } - - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.expr); - } - - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLSelectQuery.java b/src/main/java/org/durid/sql/ast/statement/SQLSelectQuery.java deleted file mode 100644 index f5c42e29..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLSelectQuery.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLObjectImpl; - -public abstract class SQLSelectQuery extends SQLObjectImpl { - - private static final long serialVersionUID = 1L; - - public SQLSelectQuery(){ - - } -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLSelectQueryBlock.java b/src/main/java/org/durid/sql/ast/statement/SQLSelectQueryBlock.java deleted file mode 100644 index eb448a8a..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLSelectQueryBlock.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLSetQuantifier; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLSelectQueryBlock extends SQLSelectQuery { - - private static final long serialVersionUID = 1L; - - protected int distionOption; - protected final List selectList = new ArrayList(); - - protected SQLTableSource from; - protected SQLExprTableSource into; - protected SQLExpr where; - protected SQLSelectGroupByClause groupBy; - - public SQLSelectQueryBlock(){ - - } - - public SQLExprTableSource getInto() { - return into; - } - - public void setInto(SQLExpr into) { - this.into = new SQLExprTableSource(into); - } - - public void setInto(SQLExprTableSource into) { - this.into = into; - } - - public SQLSelectGroupByClause getGroupBy() { - return this.groupBy; - } - - public void setGroupBy(SQLSelectGroupByClause groupBy) { - this.groupBy = groupBy; - } - - public SQLExpr getWhere() { - return this.where; - } - - public void setWhere(SQLExpr where) { - this.where = where; - } - - public int getDistionOption() { - return this.distionOption; - } - - public void setDistionOption(int distionOption) { - this.distionOption = distionOption; - } - - public List getSelectList() { - return this.selectList; - } - - public SQLTableSource getFrom() { - return this.from; - } - - public void setFrom(SQLTableSource from) { - this.from = from; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.selectList); - acceptChild(visitor, this.from); - acceptChild(visitor, this.where); - acceptChild(visitor, this.groupBy); - } - visitor.endVisit(this); - } - - public void output(StringBuffer buf) { - buf.append("SELECT "); - - if (SQLSetQuantifier.ALL == this.distionOption) { - buf.append("ALL "); - } else if (SQLSetQuantifier.DISTINCT == this.distionOption) { - buf.append("DISTINCT "); - } else if (SQLSetQuantifier.UNIQUE == this.distionOption) { - buf.append("UNIQUE "); - } - - int i = 0; - for (int size = this.selectList.size(); i < size; ++i) { - if (i != 0) { - buf.append(", "); - } - ((SQLSelectItem) this.selectList.get(i)).output(buf); - } - - buf.append(" FROM "); - if (this.from == null) { - buf.append("DUAL"); - } else { - this.from.output(buf); - } - - if (this.where != null) { - buf.append(" WHERE "); - this.where.output(buf); - } - - if (this.groupBy != null) { - buf.append(" "); - this.groupBy.output(buf); - } - } -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLSelectStatement.java b/src/main/java/org/durid/sql/ast/statement/SQLSelectStatement.java deleted file mode 100644 index 75698e5a..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLSelectStatement.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLStatementImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLSelectStatement extends SQLStatementImpl { - - private static final long serialVersionUID = 1L; - - protected SQLSelect select; - - public SQLSelectStatement(){ - - } - - public SQLSelectStatement(SQLSelect select){ - - this.select = select; - } - - public SQLSelect getSelect() { - return this.select; - } - - public void setSelect(SQLSelect select) { - this.select = select; - } - - public void output(StringBuffer buf) { - this.select.output(buf); - } - - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.select); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLSelectUnionType.java b/src/main/java/org/durid/sql/ast/statement/SQLSelectUnionType.java deleted file mode 100644 index 070b0984..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLSelectUnionType.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -public class SQLSelectUnionType { - - public final static int ALL = 0; - public final static int DISTINCT = 1; -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLSetStatement.java b/src/main/java/org/durid/sql/ast/statement/SQLSetStatement.java deleted file mode 100644 index 6f874ac7..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLSetStatement.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLStatementImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLSetStatement extends SQLStatementImpl { - - private static final long serialVersionUID = 1L; - - private List items = new ArrayList(); - - public SQLSetStatement(){ - } - - public SQLSetStatement(SQLExpr target, SQLExpr value) { - this.items.add(new SQLAssignItem(target, value)); - } - - public List getItems() { - return items; - } - - public void setItems(List items) { - this.items = items; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.items); - } - visitor.endVisit(this); - } - - public void output(StringBuffer buf) { - buf.append("SET "); - - for (int i = 0; i < items.size(); ++i) { - if (i != 0) { - buf.append(", "); - } - - SQLAssignItem item = items.get(i); - item.output(buf); - } - } -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLSubqueryTableSource.java b/src/main/java/org/durid/sql/ast/statement/SQLSubqueryTableSource.java deleted file mode 100644 index 931c55f2..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLSubqueryTableSource.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLSubqueryTableSource extends SQLTableSourceImpl { - - private static final long serialVersionUID = 1L; - - protected SQLSelect select; - - public SQLSubqueryTableSource(){ - - } - - public SQLSubqueryTableSource(String alias){ - super(alias); - } - - public SQLSubqueryTableSource(SQLSelect select, String alias){ - super(alias); - this.select = select; - } - - public SQLSubqueryTableSource(SQLSelect select){ - - this.select = select; - } - - public SQLSelect getSelect() { - return this.select; - } - - public void setSelect(SQLSelect select) { - this.select = select; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, select); - } - visitor.endVisit(this); - } - - public void output(StringBuffer buf) { - buf.append("("); - this.select.output(buf); - buf.append(")"); - } - - @Override - public String getTablename() { - return null; - } -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLTableConstaint.java b/src/main/java/org/durid/sql/ast/statement/SQLTableConstaint.java deleted file mode 100644 index 9d87c99c..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLTableConstaint.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -public interface SQLTableConstaint extends SQLConstaint, SQLTableElement { - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLTableConstaintImpl.java b/src/main/java/org/durid/sql/ast/statement/SQLTableConstaintImpl.java deleted file mode 100644 index d09b2a14..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLTableConstaintImpl.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.visitor.SQLASTVisitor; - -@SuppressWarnings("serial") -public class SQLTableConstaintImpl extends SQLConstaintImpl implements SQLTableConstaint { - - public SQLTableConstaintImpl(){ - - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - - } - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLTableElement.java b/src/main/java/org/durid/sql/ast/statement/SQLTableElement.java deleted file mode 100644 index 8e09612f..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLTableElement.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLObject; - -public interface SQLTableElement extends SQLObject { - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLTableSource.java b/src/main/java/org/durid/sql/ast/statement/SQLTableSource.java deleted file mode 100644 index 85e4a982..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLTableSource.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import java.util.List; - -import org.durid.sql.ast.SQLHint; -import org.durid.sql.ast.SQLObject; - -public interface SQLTableSource extends SQLObject { - - String getAlias(); - - void setAlias(String alias); - - String getTablename(); - - List getHints(); -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLTableSourceImpl.java b/src/main/java/org/durid/sql/ast/statement/SQLTableSourceImpl.java deleted file mode 100644 index 2464f68a..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLTableSourceImpl.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLHint; -import org.durid.sql.ast.SQLObjectImpl; - -public abstract class SQLTableSourceImpl extends SQLObjectImpl implements SQLTableSource { - - private static final long serialVersionUID = 1L; - - protected String alias; - - protected List hints = new ArrayList(2); - - public SQLTableSourceImpl(){ - - } - - public SQLTableSourceImpl(String alias){ - - this.alias = alias; - } - - public String getAlias() { - return this.alias; - } - - public void setAlias(String alias) { - this.alias = alias; - } - - public List getHints() { - return hints; - } - - public void setHints(List hints) { - this.hints = hints; - } - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLTruncateStatement.java b/src/main/java/org/durid/sql/ast/statement/SQLTruncateStatement.java deleted file mode 100644 index 074759ca..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLTruncateStatement.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLStatementImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLTruncateStatement extends SQLStatementImpl { - - private static final long serialVersionUID = 1L; - protected List tableSources = new ArrayList(2); - - public List getTableSources() { - return tableSources; - } - - public void setTableSources(List tableSources) { - this.tableSources = tableSources; - } - - public void addTableSource(SQLName name) { - this.tableSources.add(new SQLExprTableSource(name)); - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, tableSources); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLUnionOperator.java b/src/main/java/org/durid/sql/ast/statement/SQLUnionOperator.java deleted file mode 100644 index 916fc616..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLUnionOperator.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -public enum SQLUnionOperator { - UNION("UNION"), UNION_ALL("UNION ALL"), MINUS("MINUS"), INTERSECT("INTERSECT"), DISTINCT("UNION DISTINCT"); - - public final String name; - - private SQLUnionOperator(String name){ - this.name = name; - } - - public String toString() { - return name; - } -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLUnionQuery.java b/src/main/java/org/durid/sql/ast/statement/SQLUnionQuery.java deleted file mode 100644 index 368fbd31..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLUnionQuery.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLOrderBy; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLUnionQuery extends SQLSelectQuery { - - private static final long serialVersionUID = 1L; - - private SQLSelectQuery left; - private SQLSelectQuery right; - private SQLUnionOperator operator = SQLUnionOperator.UNION; - private SQLOrderBy orderBy; - - public SQLUnionOperator getOperator() { - return operator; - } - - public void setOperator(SQLUnionOperator operator) { - this.operator = operator; - } - - public SQLUnionQuery(){ - - } - - public SQLSelectQuery getLeft() { - return left; - } - - public void setLeft(SQLSelectQuery left) { - this.left = left; - } - - public SQLSelectQuery getRight() { - return right; - } - - public void setRight(SQLSelectQuery right) { - this.right = right; - } - - public SQLOrderBy getOrderBy() { - return orderBy; - } - - public void setOrderBy(SQLOrderBy orderBy) { - this.orderBy = orderBy; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, left); - acceptChild(visitor, right); - acceptChild(visitor, orderBy); - } - visitor.endVisit(this); - } - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLUniqueConstraint.java b/src/main/java/org/durid/sql/ast/statement/SQLUniqueConstraint.java deleted file mode 100644 index dd07d248..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLUniqueConstraint.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import java.util.List; - -import org.durid.sql.ast.SQLExpr; - -public interface SQLUniqueConstraint extends SQLConstaint { - - List getColumns(); - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLUniqueConstraintImpl.java b/src/main/java/org/durid/sql/ast/statement/SQLUniqueConstraintImpl.java deleted file mode 100644 index 4b00bf8f..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLUniqueConstraintImpl.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.visitor.SQLASTVisitor; - -@SuppressWarnings("serial") -public class SQLUniqueConstraintImpl extends SQLConstaintImpl implements SQLUniqueConstraint { - - public SQLUniqueConstraintImpl(){ - - } - - private List columns = new ArrayList(); - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.getName()); - acceptChild(visitor, columns); - } - visitor.endVisit(this); - } - - @Override - public List getColumns() { - return columns; - } -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLUpdateSetItem.java b/src/main/java/org/durid/sql/ast/statement/SQLUpdateSetItem.java deleted file mode 100644 index 60f3d92b..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLUpdateSetItem.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLObjectImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLUpdateSetItem extends SQLObjectImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr column; - private SQLExpr value; - - public SQLUpdateSetItem(){ - - } - - public SQLExpr getColumn() { - return column; - } - - public void setColumn(SQLExpr column) { - this.column = column; - } - - public SQLExpr getValue() { - return value; - } - - public void setValue(SQLExpr value) { - this.value = value; - } - - public void output(StringBuffer buf) { - column.output(buf); - buf.append(" = "); - value.output(buf); - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, column); - acceptChild(visitor, value); - } - - visitor.endVisit(this); - } - -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLUpdateStatement.java b/src/main/java/org/durid/sql/ast/statement/SQLUpdateStatement.java deleted file mode 100644 index e2bbbe20..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLUpdateStatement.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLStatementImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLUpdateStatement extends SQLStatementImpl { - - private static final long serialVersionUID = 1L; - - protected final List items = new ArrayList(); - protected SQLExpr where; - - protected SQLTableSource tableSource; - - public SQLUpdateStatement(){ - - } - - public SQLTableSource getTableSource() { - return tableSource; - } - - public void setTableSource(SQLExpr expr) { - this.setTableSource(new SQLExprTableSource(expr)); - } - - public void setTableSource(SQLTableSource tableSource) { - this.tableSource = tableSource; - } - - public SQLName getTableName() { - if (tableSource instanceof SQLExprTableSource) { - SQLExprTableSource exprTableSource = (SQLExprTableSource) tableSource; - return (SQLName) exprTableSource.getExpr(); - } - return null; - } - - public SQLExpr getWhere() { - return where; - } - - public void setWhere(SQLExpr where) { - this.where = where; - } - - public List getItems() { - return items; - } - - @Override - public void output(StringBuffer buf) { - buf.append("UPDATE "); - - this.tableSource.output(buf); - - buf.append(" SET "); - for (int i = 0, size = items.size(); i < size; ++i) { - if (i != 0) { - buf.append(", "); - } - items.get(i).output(buf); - } - - if (this.where != null) { - buf.append(" WHERE "); - this.where.output(buf); - } - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, tableSource); - acceptChild(visitor, items); - acceptChild(visitor, where); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/ast/statement/SQLUseStatement.java b/src/main/java/org/durid/sql/ast/statement/SQLUseStatement.java deleted file mode 100644 index 927f342c..00000000 --- a/src/main/java/org/durid/sql/ast/statement/SQLUseStatement.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.ast.statement; - -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLStatement; -import org.durid.sql.ast.SQLStatementImpl; -import org.durid.sql.visitor.SQLASTVisitor; - -public class SQLUseStatement extends SQLStatementImpl implements SQLStatement { - - private static final long serialVersionUID = 1L; - private SQLName database; - - public SQLName getDatabase() { - return database; - } - - public void setDatabase(SQLName database) { - this.database = database; - } - - @Override - public void accept0(SQLASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, database); - } - visitor.endVisit(this); - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlForceIndexHint.java b/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlForceIndexHint.java deleted file mode 100644 index 8b5fcbbf..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlForceIndexHint.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast; - -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlForceIndexHint extends MySqlIndexHintImpl { - - private static final long serialVersionUID = 1L; - - @Override - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, getIndexList()); - } - visitor.endVisit(this); - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlHint.java b/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlHint.java deleted file mode 100644 index 63bc0634..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlHint.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast; - -import org.durid.sql.ast.SQLHint; - - -public interface MySqlHint extends SQLHint, MySqlObject { - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlIgnoreIndexHint.java b/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlIgnoreIndexHint.java deleted file mode 100644 index 4e7c1ebd..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlIgnoreIndexHint.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast; - -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlIgnoreIndexHint extends MySqlIndexHintImpl { - - private static final long serialVersionUID = 1L; - - @Override - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, getIndexList()); - } - visitor.endVisit(this); - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlIndexHint.java b/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlIndexHint.java deleted file mode 100644 index 5b9b6150..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlIndexHint.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast; - -public interface MySqlIndexHint extends MySqlHint { - public static enum Option { - JOIN("JOIN"), - ORDER_BY("ORDER BY"), - GROUP_BY("GROUP BY") - ; - - public final String name; - - Option(String name) { - this.name = name; - } - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlIndexHintImpl.java b/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlIndexHintImpl.java deleted file mode 100644 index 2f3fbe03..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlIndexHintImpl.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLName; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public abstract class MySqlIndexHintImpl extends MySqlObjectImpl implements MySqlIndexHint { - - private static final long serialVersionUID = 1L; - - private MySqlIndexHint.Option option; - - private List indexList = new ArrayList(); - - @Override - public abstract void accept0(MySqlASTVisitor visitor); - - public MySqlIndexHint.Option getOption() { - return option; - } - - public void setOption(MySqlIndexHint.Option option) { - this.option = option; - } - - public List getIndexList() { - return indexList; - } - - public void setIndexList(List indexList) { - this.indexList = indexList; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlKey.java b/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlKey.java deleted file mode 100644 index 88433e9e..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlKey.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.statement.SQLConstaintImpl; -import org.durid.sql.ast.statement.SQLTableConstaint; -import org.durid.sql.ast.statement.SQLUniqueConstraint; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import org.durid.sql.visitor.SQLASTVisitor; - -@SuppressWarnings("serial") -public class MySqlKey extends SQLConstaintImpl implements SQLUniqueConstraint, SQLTableConstaint { - - private List columns = new ArrayList(); - private String indexType; - - public MySqlKey(){ - - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor instanceof MySqlASTVisitor) { - accept0((MySqlASTVisitor) visitor); - } - } - - protected void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.getName()); - acceptChild(visitor, this.getColumns()); - } - visitor.endVisit(this); - } - - @Override - public List getColumns() { - return columns; - } - - public String getIndexType() { - return indexType; - } - - public void setIndexType(String indexType) { - this.indexType = indexType; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlObject.java b/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlObject.java deleted file mode 100644 index c7262d9d..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlObject.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast; - -import org.durid.sql.ast.SQLObject; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - - -public interface MySqlObject extends SQLObject { - void accept0(MySqlASTVisitor visitor); -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlObjectImpl.java b/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlObjectImpl.java deleted file mode 100644 index 93be9ae8..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlObjectImpl.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast; - -import org.durid.sql.ast.SQLObjectImpl; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import org.durid.sql.visitor.SQLASTVisitor; - -public abstract class MySqlObjectImpl extends SQLObjectImpl implements MySqlObject { - - private static final long serialVersionUID = 1L; - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor instanceof MySqlASTVisitor) { - accept0((MySqlASTVisitor) visitor); - } else { - throw new IllegalArgumentException("not support visitor type : " + visitor.getClass().getName()); - } - } - - public abstract void accept0(MySqlASTVisitor visitor); -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlPrimaryKey.java b/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlPrimaryKey.java deleted file mode 100644 index 2d77aadd..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlPrimaryKey.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast; - -import org.durid.sql.ast.statement.SQLPrimaryKey; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -@SuppressWarnings("serial") -public class MySqlPrimaryKey extends MySqlKey implements SQLPrimaryKey { - - public MySqlPrimaryKey(){ - - } - - protected void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.getName()); - acceptChild(visitor, this.getColumns()); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlUseIndexHint.java b/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlUseIndexHint.java deleted file mode 100644 index 8d1e62bc..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/MySqlUseIndexHint.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast; - -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlUseIndexHint extends MySqlIndexHintImpl { - - private static final long serialVersionUID = 1L; - - @Override - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, getIndexList()); - } - visitor.endVisit(this); - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlBinaryExpr.java b/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlBinaryExpr.java deleted file mode 100644 index f4e9bc50..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlBinaryExpr.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.expr; - -import org.durid.sql.ast.expr.SQLLiteralExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import org.durid.sql.visitor.SQLASTVisitor; - -public class MySqlBinaryExpr extends MySqlExprImpl implements MySqlExpr, SQLLiteralExpr { - - private static final long serialVersionUID = 1L; - - private String value; - - public MySqlBinaryExpr(){ - - } - - public MySqlBinaryExpr(String value){ - super(); - this.value = value; - } - - public String getValue() { - return value; - } - - public void setValue(String value) { - this.value = value; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - this.accept0((MySqlASTVisitor) visitor); - } - - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - - visitor.endVisit(this); - } - - public void output(StringBuffer buf) { - buf.append("b'"); - buf.append(value); - buf.append('\''); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((value == null) ? 0 : value.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - MySqlBinaryExpr other = (MySqlBinaryExpr) obj; - if (value == null) { - if (other.value != null) { - return false; - } - } else if (!value.equals(other.value)) { - return false; - } - return true; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlBooleanExpr.java b/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlBooleanExpr.java deleted file mode 100644 index 087f2757..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlBooleanExpr.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.expr; - -import org.durid.sql.ast.expr.SQLLiteralExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import org.durid.sql.visitor.SQLASTVisitor; - -public class MySqlBooleanExpr extends MySqlExprImpl implements MySqlExpr, SQLLiteralExpr { - - private static final long serialVersionUID = 1L; - - private boolean value; - - public MySqlBooleanExpr(){ - - } - - public MySqlBooleanExpr(boolean value){ - this.value = value; - } - - public boolean getValue() { - return value; - } - - public void setValue(boolean value) { - this.value = value; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - this.accept0((MySqlASTVisitor) visitor); - } - - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - - visitor.endVisit(this); - } - - public void output(StringBuffer buf) { - buf.append("x"); - buf.append(value ? "TRUE" : "FALSE"); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + (value ? 1231 : 1237); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - MySqlBooleanExpr other = (MySqlBooleanExpr) obj; - if (value != other.value) { - return false; - } - return true; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlCharExpr.java b/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlCharExpr.java deleted file mode 100644 index 9c83a02a..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlCharExpr.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.expr; - -import org.durid.sql.ast.expr.SQLCharExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import org.durid.sql.visitor.SQLASTVisitor; - -public class MySqlCharExpr extends SQLCharExpr implements MySqlExpr { - - private static final long serialVersionUID = 1L; - - private String charset; - private String collate; - - public MySqlCharExpr(){ - - } - - public MySqlCharExpr(String text){ - super(text); - } - - public String getCharset() { - return charset; - } - - public void setCharset(String charset) { - this.charset = charset; - } - - public String getCollate() { - return collate; - } - - public void setCollate(String collate) { - this.collate = collate; - } - - public void output(StringBuffer buf) { - if (charset != null) { - buf.append(charset); - buf.append(' '); - } - - super.output(buf); - - if (collate != null) { - buf.append(" COLLATE "); - buf.append(collate); - } - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor instanceof MySqlASTVisitor) { - accept0((MySqlASTVisitor) visitor); - } else { - visitor.visit(this); - visitor.endVisit(this); - } - } - - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlExpr.java b/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlExpr.java deleted file mode 100644 index ac4e7c6d..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlExpr.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.expr; - -import org.durid.sql.ast.SQLExpr; - -public interface MySqlExpr extends SQLExpr { - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlExprImpl.java b/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlExprImpl.java deleted file mode 100644 index 822adc59..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlExprImpl.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.expr; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.ast.MySqlObjectImpl; - -public abstract class MySqlExprImpl extends MySqlObjectImpl implements SQLExpr { - - private static final long serialVersionUID = 1L; - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlExtractExpr.java b/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlExtractExpr.java deleted file mode 100644 index aed1c81e..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlExtractExpr.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.expr; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import org.durid.sql.visitor.SQLASTVisitor; - -public class MySqlExtractExpr extends SQLExprImpl implements MySqlExpr { - - private static final long serialVersionUID = 1L; - - private SQLExpr value; - private MySqlIntervalUnit unit; - - public MySqlExtractExpr(){ - } - - public SQLExpr getValue() { - return value; - } - - public void setValue(SQLExpr value) { - this.value = value; - } - - public MySqlIntervalUnit getUnit() { - return unit; - } - - public void setUnit(MySqlIntervalUnit unit) { - this.unit = unit; - } - - @Override - public void output(StringBuffer buf) { - value.output(buf); - buf.append(' '); - buf.append(unit.name()); - } - - protected void accept0(SQLASTVisitor visitor) { - MySqlASTVisitor mysqlVisitor = (MySqlASTVisitor) visitor; - mysqlVisitor.visit(this); - mysqlVisitor.endVisit(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((unit == null) ? 0 : unit.hashCode()); - result = prime * result + ((value == null) ? 0 : value.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (!(obj instanceof MySqlExtractExpr)) { - return false; - } - MySqlExtractExpr other = (MySqlExtractExpr) obj; - if (unit != other.unit) { - return false; - } - if (value == null) { - if (other.value != null) { - return false; - } - } else if (!value.equals(other.value)) { - return false; - } - return true; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlHexadecimalExpr.java b/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlHexadecimalExpr.java deleted file mode 100644 index cf5112c7..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlHexadecimalExpr.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.expr; - -import org.durid.sql.ast.expr.SQLLiteralExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - - -public class MySqlHexadecimalExpr extends MySqlExprImpl implements SQLLiteralExpr { - - private static final long serialVersionUID = 1L; - - @Override - public void accept0(MySqlASTVisitor visitor) { - - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlIntervalExpr.java b/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlIntervalExpr.java deleted file mode 100644 index bcab7c4b..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlIntervalExpr.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.expr; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import org.durid.sql.visitor.SQLASTVisitor; - -public class MySqlIntervalExpr extends SQLExprImpl implements MySqlExpr { - - private static final long serialVersionUID = 1L; - - private SQLExpr value; - private MySqlIntervalUnit unit; - - public MySqlIntervalExpr(){ - } - - public SQLExpr getValue() { - return value; - } - - public void setValue(SQLExpr value) { - this.value = value; - } - - public MySqlIntervalUnit getUnit() { - return unit; - } - - public void setUnit(MySqlIntervalUnit unit) { - this.unit = unit; - } - - @Override - public void output(StringBuffer buf) { - value.output(buf); - buf.append(' '); - buf.append(unit.name()); - } - - protected void accept0(SQLASTVisitor visitor) { - MySqlASTVisitor mysqlVisitor = (MySqlASTVisitor) visitor; - if (mysqlVisitor.visit(this)) { - acceptChild(visitor, this.value); - } - mysqlVisitor.endVisit(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((unit == null) ? 0 : unit.hashCode()); - result = prime * result + ((value == null) ? 0 : value.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - MySqlIntervalExpr other = (MySqlIntervalExpr) obj; - if (unit != other.unit) { - return false; - } - if (value == null) { - if (other.value != null) { - return false; - } - } else if (!value.equals(other.value)) { - return false; - } - return true; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlIntervalUnit.java b/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlIntervalUnit.java deleted file mode 100644 index 12b5f842..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlIntervalUnit.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.expr; - -public enum MySqlIntervalUnit { - YEAR, YEAR_MONTH, - - QUARTER, - - MONTH, WEEK, DAY, DAY_HOUR, DAY_MINUTE, DAY_SECOND, DAY_MICROSECOND, - - HOUR, HOUR_MINUTE, HOUR_SECOND, HOUR_MICROSECOND, - - MINUTE, MINUTE_SECOND, MINUTE_MICROSECOND, - - SECOND, SECOND_MICROSECOND, - - MICROSECOND -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlMatchAgainstExpr.java b/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlMatchAgainstExpr.java deleted file mode 100644 index bc7c9805..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlMatchAgainstExpr.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.expr; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLExprImpl; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import org.durid.sql.visitor.SQLASTVisitor; - -public class MySqlMatchAgainstExpr extends SQLExprImpl implements MySqlExpr { - - private static final long serialVersionUID = 1L; - - private List columns = new ArrayList(); - - private SQLExpr against; - - private SearchModifier searchModifier; - - public List getColumns() { - return columns; - } - - public void setColumns(List columns) { - this.columns = columns; - } - - public SQLExpr getAgainst() { - return against; - } - - public void setAgainst(SQLExpr against) { - this.against = against; - } - - public SearchModifier getSearchModifier() { - return searchModifier; - } - - public void setSearchModifier(SearchModifier searchModifier) { - this.searchModifier = searchModifier; - } - - public static enum SearchModifier { - IN_BOOLEAN_MODE("IN BOOLEAN MODE"), IN_NATURAL_LANGUAGE_MODE("IN NATURAL LANGUAGE MODE"), - IN_NATURAL_LANGUAGE_MODE_WITH_QUERY_EXPANSION("IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION"), - WITH_QUERY_EXPANSION("WITH QUERY EXPANSION"), ; - - public final String name; - - SearchModifier(){ - this(null); - } - - SearchModifier(String name){ - this.name = name; - } - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - MySqlASTVisitor mysqlVisitor = (MySqlASTVisitor) visitor; - if (mysqlVisitor.visit(this)) { - acceptChild(visitor, this.columns); - acceptChild(visitor, this.against); - } - mysqlVisitor.endVisit(this); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((against == null) ? 0 : against.hashCode()); - result = prime * result + ((columns == null) ? 0 : columns.hashCode()); - result = prime * result + ((searchModifier == null) ? 0 : searchModifier.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - MySqlMatchAgainstExpr other = (MySqlMatchAgainstExpr) obj; - if (against == null) { - if (other.against != null) { - return false; - } - } else if (!against.equals(other.against)) { - return false; - } - if (columns == null) { - if (other.columns != null) { - return false; - } - } else if (!columns.equals(other.columns)) { - return false; - } - if (searchModifier != other.searchModifier) { - return false; - } - return true; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlOutFileExpr.java b/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlOutFileExpr.java deleted file mode 100644 index 28e0dcd5..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlOutFileExpr.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.expr; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.expr.SQLLiteralExpr; -import org.durid.sql.dialect.mysql.ast.MySqlObjectImpl; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlOutFileExpr extends MySqlObjectImpl implements SQLExpr { - - private static final long serialVersionUID = 1L; - private SQLExpr file; - private String charset; - - private SQLLiteralExpr columnsTerminatedBy; - private boolean columnsEnclosedOptionally = false; - private SQLLiteralExpr columnsEnclosedBy; - private SQLLiteralExpr columnsEscaped; - - private SQLLiteralExpr linesStartingBy; - private SQLLiteralExpr linesTerminatedBy; - - private SQLExpr ignoreLinesNumber; - - public MySqlOutFileExpr(){ - } - - public MySqlOutFileExpr(SQLExpr file){ - this.file = file; - } - - @Override - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, file); - } - visitor.endVisit(this); - } - - public SQLExpr getFile() { - return file; - } - - public void setFile(SQLExpr file) { - this.file = file; - } - - public String getCharset() { - return charset; - } - - public void setCharset(String charset) { - this.charset = charset; - } - - public SQLLiteralExpr getColumnsTerminatedBy() { - return columnsTerminatedBy; - } - - public void setColumnsTerminatedBy(SQLLiteralExpr columnsTerminatedBy) { - this.columnsTerminatedBy = columnsTerminatedBy; - } - - public boolean isColumnsEnclosedOptionally() { - return columnsEnclosedOptionally; - } - - public void setColumnsEnclosedOptionally(boolean columnsEnclosedOptionally) { - this.columnsEnclosedOptionally = columnsEnclosedOptionally; - } - - public SQLLiteralExpr getColumnsEnclosedBy() { - return columnsEnclosedBy; - } - - public void setColumnsEnclosedBy(SQLLiteralExpr columnsEnclosedBy) { - this.columnsEnclosedBy = columnsEnclosedBy; - } - - public SQLLiteralExpr getColumnsEscaped() { - return columnsEscaped; - } - - public void setColumnsEscaped(SQLLiteralExpr columnsEscaped) { - this.columnsEscaped = columnsEscaped; - } - - public SQLLiteralExpr getLinesStartingBy() { - return linesStartingBy; - } - - public void setLinesStartingBy(SQLLiteralExpr linesStartingBy) { - this.linesStartingBy = linesStartingBy; - } - - public SQLLiteralExpr getLinesTerminatedBy() { - return linesTerminatedBy; - } - - public void setLinesTerminatedBy(SQLLiteralExpr linesTerminatedBy) { - this.linesTerminatedBy = linesTerminatedBy; - } - - public SQLExpr getIgnoreLinesNumber() { - return ignoreLinesNumber; - } - - public void setIgnoreLinesNumber(SQLExpr ignoreLinesNumber) { - this.ignoreLinesNumber = ignoreLinesNumber; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlUserName.java b/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlUserName.java deleted file mode 100644 index 075de8ed..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/expr/MySqlUserName.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.expr; - -import org.durid.sql.ast.SQLName; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlUserName extends MySqlExprImpl implements SQLName { - - private static final long serialVersionUID = 1L; - private String userName; - private String host; - - public String getUserName() { - return userName; - } - - public void setUserName(String userName) { - this.userName = userName; - } - - public String getHost() { - return host; - } - - public void setHost(String host) { - this.host = host; - } - - @Override - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } - - public String getSimleName() { - return userName + '@' + host; - } - - public String toString() { - return getSimleName(); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/CobarShowStatus.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/CobarShowStatus.java deleted file mode 100644 index ec2dfabc..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/CobarShowStatus.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class CobarShowStatus extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableAddColumn.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableAddColumn.java deleted file mode 100644 index 3afeb645..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableAddColumn.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.statement.SQLAlterTableAddColumn; -import org.durid.sql.dialect.mysql.ast.MySqlObject; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import org.durid.sql.visitor.SQLASTVisitor; - -public class MySqlAlterTableAddColumn extends SQLAlterTableAddColumn implements MySqlObject { - - private static final long serialVersionUID = 1L; - - private SQLName firstColumn; - private SQLName afterColumn; - - private boolean first; - - public SQLName getFirstColumn() { - return firstColumn; - } - - public void setFirstColumn(SQLName first) { - this.firstColumn = first; - } - - public boolean isFirst() { - return first; - } - - public void setFirst(boolean first) { - this.first = first; - } - - public SQLName getAfterColumn() { - return afterColumn; - } - - public void setAfterColumn(SQLName after) { - this.afterColumn = after; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor instanceof MySqlASTVisitor) { - accept0((MySqlASTVisitor) visitor); - } else { - throw new IllegalArgumentException("not support visitor type : " + visitor.getClass().getName()); - } - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, getColumns()); - - acceptChild(visitor, firstColumn); - acceptChild(visitor, afterColumn); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableAddIndex.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableAddIndex.java deleted file mode 100644 index edf7317e..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableAddIndex.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.statement.SQLAlterTableAddIndex; -import org.durid.sql.ast.statement.SQLAlterTableItem; -import org.durid.sql.dialect.mysql.ast.MySqlObject; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import org.durid.sql.visitor.SQLASTVisitor; - -public class MySqlAlterTableAddIndex extends SQLAlterTableAddIndex implements SQLAlterTableItem, MySqlObject { - - private static final long serialVersionUID = 1L; - - private String using; - - public String getUsing() { - return using; - } - - public void setUsing(String using) { - this.using = using; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor instanceof MySqlASTVisitor) { - accept0((MySqlASTVisitor) visitor); - } else { - throw new IllegalArgumentException("not support visitor type : " + visitor.getClass().getName()); - } - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, getName()); - acceptChild(visitor, getItems()); - } - visitor.endVisit(this); - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableAddUnique.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableAddUnique.java deleted file mode 100644 index 8c89c204..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableAddUnique.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.statement.SQLAlterTableAddIndex; -import org.durid.sql.ast.statement.SQLAlterTableItem; -import org.durid.sql.dialect.mysql.ast.MySqlObject; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import org.durid.sql.visitor.SQLASTVisitor; - -public class MySqlAlterTableAddUnique extends SQLAlterTableAddIndex implements SQLAlterTableItem, MySqlObject { - - private static final long serialVersionUID = 1L; - - private String using; - - public String getUsing() { - return using; - } - - public void setUsing(String using) { - this.using = using; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor instanceof MySqlASTVisitor) { - accept0((MySqlASTVisitor) visitor); - } else { - throw new IllegalArgumentException("not support visitor type : " + visitor.getClass().getName()); - } - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, getName()); - acceptChild(visitor, getItems()); - } - visitor.endVisit(this); - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableChangeColumn.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableChangeColumn.java deleted file mode 100644 index 4a19a49c..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableChangeColumn.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.statement.SQLAlterTableItem; -import org.durid.sql.ast.statement.SQLColumnDefinition; -import org.durid.sql.dialect.mysql.ast.MySqlObjectImpl; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlAlterTableChangeColumn extends MySqlObjectImpl implements SQLAlterTableItem { - - private static final long serialVersionUID = 1L; - - private SQLName columnName; - - private SQLColumnDefinition newColumnDefinition; - - private boolean first; - - private SQLExpr firstColumn; - private SQLExpr afterColumn; - - @Override - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, columnName); - acceptChild(visitor, newColumnDefinition); - - acceptChild(visitor, firstColumn); - acceptChild(visitor, afterColumn); - } - } - - public SQLExpr getFirstColumn() { - return firstColumn; - } - - public void setFirstColumn(SQLExpr firstColumn) { - this.firstColumn = firstColumn; - } - - public SQLExpr getAfterColumn() { - return afterColumn; - } - - public void setAfterColumn(SQLExpr afterColumn) { - this.afterColumn = afterColumn; - } - - public SQLName getColumnName() { - return columnName; - } - - public void setColumnName(SQLName columnName) { - this.columnName = columnName; - } - - public SQLColumnDefinition getNewColumnDefinition() { - return newColumnDefinition; - } - - public void setNewColumnDefinition(SQLColumnDefinition newColumnDefinition) { - this.newColumnDefinition = newColumnDefinition; - } - - public boolean isFirst() { - return first; - } - - public void setFirst(boolean first) { - this.first = first; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableCharacter.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableCharacter.java deleted file mode 100644 index e1706fa3..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableCharacter.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.statement.SQLAlterTableItem; -import org.durid.sql.dialect.mysql.ast.MySqlObjectImpl; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlAlterTableCharacter extends MySqlObjectImpl implements SQLAlterTableItem { - - private static final long serialVersionUID = 1L; - - private SQLExpr characterSet; - private SQLExpr collate; - - @Override - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, characterSet); - acceptChild(visitor, collate); - } - visitor.endVisit(this); - } - - public SQLExpr getCharacterSet() { - return characterSet; - } - - public void setCharacterSet(SQLExpr characterSet) { - this.characterSet = characterSet; - } - - public SQLExpr getCollate() { - return collate; - } - - public void setCollate(SQLExpr collate) { - this.collate = collate; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableOption.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableOption.java deleted file mode 100644 index d267230b..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableOption.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.statement.SQLAlterTableItem; -import org.durid.sql.dialect.mysql.ast.MySqlObjectImpl; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlAlterTableOption extends MySqlObjectImpl implements SQLAlterTableItem { - - private static final long serialVersionUID = 1L; - - private String name; - private String value; - - public MySqlAlterTableOption(String name, String value){ - this.name = name; - this.value = value; - } - - public MySqlAlterTableOption(){ - } - - @Override - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getValue() { - return value; - } - - public void setValue(String value) { - this.value = value; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableStatement.java deleted file mode 100644 index 8ef4934e..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlAlterTableStatement.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.statement.SQLAlterTableStatement; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import org.durid.sql.visitor.SQLASTVisitor; - -public class MySqlAlterTableStatement extends SQLAlterTableStatement { - - private static final long serialVersionUID = 1L; - - private boolean ignore = false; - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor instanceof MySqlASTVisitor) { - accept0((MySqlASTVisitor) visitor); - } else { - throw new IllegalArgumentException("not support visitor type : " + visitor.getClass().getName()); - } - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, getTableSource()); - acceptChild(visitor, getItems()); - } - visitor.endVisit(this); - } - - public boolean isIgnore() { - return ignore; - } - - public void setIgnore(boolean ignore) { - this.ignore = ignore; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlBinlogStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlBinlogStatement.java deleted file mode 100644 index d21b5624..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlBinlogStatement.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlBinlogStatement extends MySqlStatementImpl { - private static final long serialVersionUID = 1L; - - private SQLExpr expr; - - public SQLExpr getExpr() { - return expr; - } - - public void setExpr(SQLExpr expr) { - this.expr = expr; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, expr); - } - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlCommitStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlCommitStatement.java deleted file mode 100644 index be2f7260..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlCommitStatement.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlCommitStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private boolean work = false; - - private Boolean chain; - private Boolean release; - - public Boolean getChain() { - return chain; - } - - public void setChain(Boolean chain) { - this.chain = chain; - } - - public Boolean getRelease() { - return release; - } - - public void setRelease(Boolean release) { - this.release = release; - } - - public boolean isWork() { - return work; - } - - public void setWork(boolean work) { - this.work = work; - } - - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlCreateDatabaseStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlCreateDatabaseStatement.java deleted file mode 100644 index ed4f507e..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlCreateDatabaseStatement.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - - -public class MySqlCreateDatabaseStatement { - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlCreateIndexStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlCreateIndexStatement.java deleted file mode 100644 index 98f3e00e..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlCreateIndexStatement.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.statement.SQLCreateIndexStatement; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import org.durid.sql.visitor.SQLASTVisitor; - -public class MySqlCreateIndexStatement extends SQLCreateIndexStatement implements MySqlStatement { - - /** - * - */ - private static final long serialVersionUID = 1L; - - private String using; - - public String getUsing() { - return using; - } - - public void setUsing(String using) { - this.using = using; - } - - protected void accept0(SQLASTVisitor visitor) { - accept0((MySqlASTVisitor) visitor); - } - - @Override - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, getName()); - acceptChild(visitor, getTable()); - acceptChild(visitor, getItems()); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlCreateTableStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlCreateTableStatement.java deleted file mode 100644 index ef6baa24..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlCreateTableStatement.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import org.durid.sql.ast.SQLCommentHint; -import org.durid.sql.ast.SQLPartitioningClause; -import org.durid.sql.ast.statement.SQLCreateTableStatement; -import org.durid.sql.ast.statement.SQLSelect; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import org.durid.sql.visitor.SQLASTVisitor; - -@SuppressWarnings("serial") -public class MySqlCreateTableStatement extends SQLCreateTableStatement implements MySqlStatement { - - private boolean ifNotExiists = false; - - private Map tableOptions = new LinkedHashMap(); - - protected SQLSelect query; - - private SQLPartitioningClause partitioning; - - public MySqlCreateTableStatement(){ - - } - - private List hints = new ArrayList(); - - public List getHints() { - return hints; - } - - public void setHints(List hints) { - this.hints = hints; - } - - public void setTableOptions(Map tableOptions) { - this.tableOptions = tableOptions; - } - - public SQLPartitioningClause getPartitioning() { - return partitioning; - } - - public void setPartitioning(SQLPartitioningClause partitioning) { - this.partitioning = partitioning; - } - - public Map getTableOptions() { - return tableOptions; - } - - public SQLSelect getQuery() { - return query; - } - - public void setQuery(SQLSelect query) { - this.query = query; - } - - @Override - public void output(StringBuffer buf) { - if (Type.GLOBAL_TEMPORARY.equals(this.type)) { - buf.append("CREATE TEMPORARY TABLE "); - } else { - buf.append("CREATE TABLE "); - } - - if (ifNotExiists) { - buf.append("IF NOT EXISTS "); - } - - this.tableSource.output(buf); - buf.append(" "); - buf.append("("); - for (int i = 0, size = tableElementList.size(); i < size; ++i) { - if (i != 0) { - buf.append(", "); - } - tableElementList.get(i).output(buf); - } - buf.append(")"); - - if (query != null) { - buf.append(" "); - query.output(buf); - } - } - - public boolean isIfNotExiists() { - return ifNotExiists; - } - - public void setIfNotExiists(boolean ifNotExiists) { - this.ifNotExiists = ifNotExiists; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor instanceof MySqlASTVisitor) { - accept0((MySqlASTVisitor) visitor); - } else { - throw new IllegalArgumentException("not support visitor type : " + visitor.getClass().getName()); - } - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - this.acceptChild(visitor, getHints()); - this.acceptChild(visitor, getTableSource()); - this.acceptChild(visitor, getTableElementList()); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlCreateUserStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlCreateUserStatement.java deleted file mode 100644 index 2650d91e..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlCreateUserStatement.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.ast.MySqlObjectImpl; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlCreateUserStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private List users = new ArrayList(2); - - public List getUsers() { - return users; - } - - public void setUsers(List users) { - this.users = users; - } - - @Override - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, users); - } - } - - public static class UserSpecification extends MySqlObjectImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr user; - private SQLExpr password; - private SQLExpr authPlugin; - - public SQLExpr getUser() { - return user; - } - - public void setUser(SQLExpr user) { - this.user = user; - } - - public SQLExpr getPassword() { - return password; - } - - public void setPassword(SQLExpr password) { - this.password = password; - } - - public SQLExpr getAuthPlugin() { - return authPlugin; - } - - public void setAuthPlugin(SQLExpr authPlugin) { - this.authPlugin = authPlugin; - } - - @Override - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, user); - acceptChild(visitor, password); - acceptChild(visitor, authPlugin); - } - } - - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlDeleteStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlDeleteStatement.java deleted file mode 100644 index 2c66a5a4..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlDeleteStatement.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLOrderBy; -import org.durid.sql.ast.statement.SQLDeleteStatement; -import org.durid.sql.ast.statement.SQLTableSource; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import org.durid.sql.dialect.mysql.visitor.MySqlOutputVisitor; -import org.durid.sql.visitor.SQLASTVisitor; - -public class MySqlDeleteStatement extends SQLDeleteStatement { - - private static final long serialVersionUID = 1L; - - private boolean lowPriority = false; - private boolean quick = false; - private boolean ignore = false; - - private SQLTableSource from; - private SQLTableSource using; - private SQLOrderBy orderBy; - private Limit limit; - - public boolean isLowPriority() { - return lowPriority; - } - - public void setLowPriority(boolean lowPriority) { - this.lowPriority = lowPriority; - } - - public boolean isQuick() { - return quick; - } - - public void setQuick(boolean quick) { - this.quick = quick; - } - - public boolean isIgnore() { - return ignore; - } - - public void setIgnore(boolean ignore) { - this.ignore = ignore; - } - - public SQLTableSource getFrom() { - return from; - } - - public SQLTableSource getUsing() { - return using; - } - - public void setUsing(SQLTableSource using) { - this.using = using; - } - - public void setFrom(SQLTableSource from) { - this.from = from; - } - - public SQLOrderBy getOrderBy() { - return orderBy; - } - - public void setOrderBy(SQLOrderBy orderBy) { - this.orderBy = orderBy; - } - - public Limit getLimit() { - return limit; - } - - public void setLimit(Limit limit) { - this.limit = limit; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor instanceof MySqlASTVisitor) { - accept0((MySqlASTVisitor) visitor); - } else { - throw new IllegalArgumentException("not support visitor type : " + visitor.getClass().getName()); - } - } - - public void output(StringBuffer buf) { - new MySqlOutputVisitor(buf).visit(this); - } - - protected void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, getTableSource()); - acceptChild(visitor, getWhere()); - acceptChild(visitor, getFrom()); - acceptChild(visitor, getUsing()); - acceptChild(visitor, orderBy); - acceptChild(visitor, limit); - } - - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlDescribeStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlDescribeStatement.java deleted file mode 100644 index 36344d32..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlDescribeStatement.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLName; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlDescribeStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLName object; - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, object); - } - visitor.endVisit(this); - } - - public SQLName getObject() { - return object; - } - - public void setObject(SQLName object) { - this.object = object; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlDropTableStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlDropTableStatement.java deleted file mode 100644 index 4384eab2..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlDropTableStatement.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.statement.SQLDropTableStatement; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import org.durid.sql.visitor.SQLASTVisitor; - -public class MySqlDropTableStatement extends SQLDropTableStatement implements MySqlStatement { - - private static final long serialVersionUID = 1L; - private boolean temporary = false; - - private boolean ifExists = false; - - private String option; - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor instanceof MySqlASTVisitor) { - accept0((MySqlASTVisitor) visitor); - } else { - throw new IllegalArgumentException("not support visitor type : " + visitor.getClass().getName()); - } - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - this.acceptChild(visitor, tableSources); - } - visitor.endVisit(this); - } - - public boolean isIfExists() { - return ifExists; - } - - public void setIfExists(boolean ifExists) { - this.ifExists = ifExists; - } - - public boolean isTemporary() { - return temporary; - } - - public void setTemporary(boolean temporary) { - this.temporary = temporary; - } - - public String getOption() { - return option; - } - - public void setOption(String option) { - this.option = option; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlDropUser.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlDropUser.java deleted file mode 100644 index fd9b4542..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlDropUser.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlDropUser extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private List users = new ArrayList(2); - - public List getUsers() { - return users; - } - - public void setUsers(List users) { - this.users = users; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, users); - } - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlDropViewStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlDropViewStatement.java deleted file mode 100644 index 1a153096..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlDropViewStatement.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.statement.SQLDropViewStatement; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import org.durid.sql.visitor.SQLASTVisitor; - -public class MySqlDropViewStatement extends SQLDropViewStatement implements MySqlStatement { - - private static final long serialVersionUID = 1L; - - private boolean ifExists = false; - - private String option; - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor instanceof MySqlASTVisitor) { - accept0((MySqlASTVisitor) visitor); - } else { - throw new IllegalArgumentException("not support visitor type : " + visitor.getClass().getName()); - } - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - this.acceptChild(visitor, tableSources); - } - visitor.endVisit(this); - } - - public boolean isIfExists() { - return ifExists; - } - - public void setIfExists(boolean ifExists) { - this.ifExists = ifExists; - } - - public String getOption() { - return option; - } - - public void setOption(String option) { - this.option = option; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlExecuteStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlExecuteStatement.java deleted file mode 100644 index ff0c9168..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlExecuteStatement.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLName; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlExecuteStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLName statementName; - private final List parameters = new ArrayList(); - - public SQLName getStatementName() { - return statementName; - } - - public void setStatementName(SQLName statementName) { - this.statementName = statementName; - } - - public List getParameters() { - return parameters; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, statementName); - acceptChild(visitor, parameters); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlHelpStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlHelpStatement.java deleted file mode 100644 index 016ddc27..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlHelpStatement.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlHelpStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr content; - - public SQLExpr getContent() { - return content; - } - - public void setContent(SQLExpr content) { - this.content = content; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, content); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlInsertStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlInsertStatement.java deleted file mode 100644 index 01917a18..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlInsertStatement.java +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.statement.SQLInsertStatement; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import org.durid.sql.dialect.mysql.visitor.MySqlOutputVisitor; -import org.durid.sql.visitor.SQLASTVisitor; - -public class MySqlInsertStatement extends SQLInsertStatement { - - private static final long serialVersionUID = 1L; - - private boolean lowPriority = false; - private boolean delayed = false; - private boolean highPriority = false; - private boolean ignore = false; - - private List valuesList = new ArrayList(); - - private final List duplicateKeyUpdate = new ArrayList(); - - public List getDuplicateKeyUpdate() { - return duplicateKeyUpdate; - } - - public ValuesClause getValues() { - if (valuesList.size() == 0) { - return null; - } - return valuesList.get(0); - } - - public void setValues(ValuesClause values) { - if (valuesList.size() == 0) { - valuesList.add(values); - } else { - valuesList.set(0, values); - } - } - - public List getValuesList() { - return valuesList; - } - - public boolean isLowPriority() { - return lowPriority; - } - - public void setLowPriority(boolean lowPriority) { - this.lowPriority = lowPriority; - } - - public boolean isDelayed() { - return delayed; - } - - public void setDelayed(boolean delayed) { - this.delayed = delayed; - } - - public boolean isHighPriority() { - return highPriority; - } - - public void setHighPriority(boolean highPriority) { - this.highPriority = highPriority; - } - - public boolean isIgnore() { - return ignore; - } - - public void setIgnore(boolean ignore) { - this.ignore = ignore; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor instanceof MySqlASTVisitor) { - accept0((MySqlASTVisitor) visitor); - } else { - throw new IllegalArgumentException("not support visitor type : " + visitor.getClass().getName()); - } - } - - public void output(StringBuffer buf) { - new MySqlOutputVisitor(buf).visit(this); - } - - protected void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - this.acceptChild(visitor, getTableSource()); - this.acceptChild(visitor, getColumns()); - this.acceptChild(visitor, getValuesList()); - this.acceptChild(visitor, getQuery()); - this.acceptChild(visitor, getDuplicateKeyUpdate()); - } - - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlKillStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlKillStatement.java deleted file mode 100644 index 610f4aa8..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlKillStatement.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlKillStatement extends MySqlStatementImpl { - private static final long serialVersionUID = 1L; - private Type type; - private SQLExpr threadId; - - public static enum Type { - CONNECTION, QUERY - } - - public Type getType() { - return type; - } - - public void setType(Type type) { - this.type = type; - } - - public SQLExpr getThreadId() { - return threadId; - } - - public void setThreadId(SQLExpr threadId) { - this.threadId = threadId; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, threadId); - } - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlLoadDataInFileStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlLoadDataInFileStatement.java deleted file mode 100644 index 32975efe..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlLoadDataInFileStatement.java +++ /dev/null @@ -1,192 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.expr.SQLLiteralExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlLoadDataInFileStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - private boolean lowPriority = false; - private boolean concurrent = false; - private boolean local = false; - - private SQLLiteralExpr fileName; - - private boolean replicate = false; - private boolean ignore = false; - - private SQLName tableName; - - private String charset; - - private SQLLiteralExpr columnsTerminatedBy; - private boolean columnsEnclosedOptionally = false; - private SQLLiteralExpr columnsEnclosedBy; - private SQLLiteralExpr columnsEscaped; - - private SQLLiteralExpr linesStartingBy; - private SQLLiteralExpr linesTerminatedBy; - - private SQLExpr ignoreLinesNumber; - - private final List setList = new ArrayList(); - - public boolean isLowPriority() { - return lowPriority; - } - - public void setLowPriority(boolean lowPriority) { - this.lowPriority = lowPriority; - } - - public boolean isConcurrent() { - return concurrent; - } - - public void setConcurrent(boolean concurrent) { - this.concurrent = concurrent; - } - - public boolean isLocal() { - return local; - } - - public void setLocal(boolean local) { - this.local = local; - } - - public SQLLiteralExpr getFileName() { - return fileName; - } - - public void setFileName(SQLLiteralExpr fileName) { - this.fileName = fileName; - } - - public boolean isReplicate() { - return replicate; - } - - public void setReplicate(boolean replicate) { - this.replicate = replicate; - } - - public boolean isIgnore() { - return ignore; - } - - public void setIgnore(boolean ignore) { - this.ignore = ignore; - } - - public SQLName getTableName() { - return tableName; - } - - public void setTableName(SQLName tableName) { - this.tableName = tableName; - } - - public String getCharset() { - return charset; - } - - public void setCharset(String charset) { - this.charset = charset; - } - - public SQLLiteralExpr getColumnsTerminatedBy() { - return columnsTerminatedBy; - } - - public void setColumnsTerminatedBy(SQLLiteralExpr columnsTerminatedBy) { - this.columnsTerminatedBy = columnsTerminatedBy; - } - - public boolean isColumnsEnclosedOptionally() { - return columnsEnclosedOptionally; - } - - public void setColumnsEnclosedOptionally(boolean columnsEnclosedOptionally) { - this.columnsEnclosedOptionally = columnsEnclosedOptionally; - } - - public SQLLiteralExpr getColumnsEnclosedBy() { - return columnsEnclosedBy; - } - - public void setColumnsEnclosedBy(SQLLiteralExpr columnsEnclosedBy) { - this.columnsEnclosedBy = columnsEnclosedBy; - } - - public SQLLiteralExpr getColumnsEscaped() { - return columnsEscaped; - } - - public void setColumnsEscaped(SQLLiteralExpr columnsEscaped) { - this.columnsEscaped = columnsEscaped; - } - - public SQLLiteralExpr getLinesStartingBy() { - return linesStartingBy; - } - - public void setLinesStartingBy(SQLLiteralExpr linesStartingBy) { - this.linesStartingBy = linesStartingBy; - } - - public SQLLiteralExpr getLinesTerminatedBy() { - return linesTerminatedBy; - } - - public void setLinesTerminatedBy(SQLLiteralExpr linesTerminatedBy) { - this.linesTerminatedBy = linesTerminatedBy; - } - - public SQLExpr getIgnoreLinesNumber() { - return ignoreLinesNumber; - } - - public void setIgnoreLinesNumber(SQLExpr ignoreLinesNumber) { - this.ignoreLinesNumber = ignoreLinesNumber; - } - - public List getSetList() { - return setList; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, fileName); - acceptChild(visitor, tableName); - acceptChild(visitor, columnsTerminatedBy); - acceptChild(visitor, columnsEnclosedBy); - acceptChild(visitor, columnsEscaped); - acceptChild(visitor, linesStartingBy); - acceptChild(visitor, linesTerminatedBy); - acceptChild(visitor, ignoreLinesNumber); - acceptChild(visitor, setList); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlLoadXmlStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlLoadXmlStatement.java deleted file mode 100644 index 800a8a5b..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlLoadXmlStatement.java +++ /dev/null @@ -1,148 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.expr.SQLLiteralExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlLoadXmlStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private boolean lowPriority = false; - private boolean concurrent = false; - private boolean local = false; - - private SQLLiteralExpr fileName; - - private boolean replicate = false; - private boolean ignore = false; - - private SQLName tableName; - - private String charset; - - private SQLExpr rowsIdentifiedBy; - - private SQLExpr ignoreLinesNumber; - - private final List setList = new ArrayList(); - - public SQLExpr getRowsIdentifiedBy() { - return rowsIdentifiedBy; - } - - public void setRowsIdentifiedBy(SQLExpr rowsIdentifiedBy) { - this.rowsIdentifiedBy = rowsIdentifiedBy; - } - - public boolean isLowPriority() { - return lowPriority; - } - - public void setLowPriority(boolean lowPriority) { - this.lowPriority = lowPriority; - } - - public boolean isConcurrent() { - return concurrent; - } - - public void setConcurrent(boolean concurrent) { - this.concurrent = concurrent; - } - - public boolean isLocal() { - return local; - } - - public void setLocal(boolean local) { - this.local = local; - } - - public SQLLiteralExpr getFileName() { - return fileName; - } - - public void setFileName(SQLLiteralExpr fileName) { - this.fileName = fileName; - } - - public boolean isReplicate() { - return replicate; - } - - public void setReplicate(boolean replicate) { - this.replicate = replicate; - } - - public boolean isIgnore() { - return ignore; - } - - public void setIgnore(boolean ignore) { - this.ignore = ignore; - } - - public SQLName getTableName() { - return tableName; - } - - public void setTableName(SQLName tableName) { - this.tableName = tableName; - } - - public String getCharset() { - return charset; - } - - public void setCharset(String charset) { - this.charset = charset; - } - - public SQLExpr getIgnoreLinesNumber() { - return ignoreLinesNumber; - } - - public void setIgnoreLinesNumber(SQLExpr ignoreLinesNumber) { - this.ignoreLinesNumber = ignoreLinesNumber; - } - - public List getSetList() { - return setList; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, fileName); - acceptChild(visitor, tableName); - acceptChild(visitor, rowsIdentifiedBy); - // acceptChild(visitor, columnsTerminatedBy); - // acceptChild(visitor, columnsEnclosedBy); - // acceptChild(visitor, columnsEscaped); - // acceptChild(visitor, linesStartingBy); - // acceptChild(visitor, linesTerminatedBy); - acceptChild(visitor, ignoreLinesNumber); - acceptChild(visitor, setList); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlLockTableStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlLockTableStatement.java deleted file mode 100644 index c4b66f27..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlLockTableStatement.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.statement.SQLExprTableSource; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlLockTableStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLExprTableSource tableSource; - - private LockType lockType; - - public SQLExprTableSource getTableSource() { - return tableSource; - } - - public void setTableSource(SQLExprTableSource tableSource) { - this.tableSource = tableSource; - } - - public void setTableSource(SQLName name) { - this.tableSource = new SQLExprTableSource(name); - } - - public LockType getLockType() { - return lockType; - } - - public void setLockType(LockType lockType) { - this.lockType = lockType; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, tableSource); - } - visitor.endVisit(this); - } - - public static enum LockType { - READ("READ"), READ_LOCAL("READ LOCAL"), WRITE("WRITE"), LOW_PRIORITY_WRITE("LOW_PRIORITY WRITE"); - - public final String name; - - LockType(String name){ - this.name = name; - } - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlPartitionByKey.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlPartitionByKey.java deleted file mode 100644 index 2c5c7039..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlPartitionByKey.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLPartitioningClause; -import org.durid.sql.dialect.mysql.ast.MySqlObjectImpl; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlPartitionByKey extends MySqlObjectImpl implements SQLPartitioningClause { - - private static final long serialVersionUID = 1L; - - private List columns = new ArrayList(); - - private SQLExpr partitionCount; - - @Override - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, columns); - acceptChild(visitor, partitionCount); - } - visitor.endVisit(this); - } - - public SQLExpr getPartitionCount() { - return partitionCount; - } - - public void setPartitionCount(SQLExpr partitionCount) { - this.partitionCount = partitionCount; - } - - public List getColumns() { - return columns; - } - - public void setColumns(List columns) { - this.columns = columns; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlPrepareStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlPrepareStatement.java deleted file mode 100644 index 081c84a4..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlPrepareStatement.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLName; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlPrepareStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - private SQLName name; - private SQLExpr from; - - public MySqlPrepareStatement(){ - } - - public MySqlPrepareStatement(SQLName name, SQLExpr from){ - this.name = name; - this.from = from; - } - - public SQLName getName() { - return name; - } - - public void setName(SQLName name) { - this.name = name; - } - - public SQLExpr getFrom() { - return from; - } - - public void setFrom(SQLExpr from) { - this.from = from; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, name); - acceptChild(visitor, from); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlRenameTableStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlRenameTableStatement.java deleted file mode 100644 index e0006de6..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlRenameTableStatement.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.ast.MySqlObjectImpl; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlRenameTableStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private List items = new ArrayList(2); - - public List getItems() { - return items; - } - - public void setItems(List items) { - this.items = items; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, items); - } - visitor.endVisit(this); - } - - public static class Item extends MySqlObjectImpl { - - private static final long serialVersionUID = 1L; - private SQLExpr name; - private SQLExpr to; - - public SQLExpr getName() { - return name; - } - - public void setName(SQLExpr name) { - this.name = name; - } - - public SQLExpr getTo() { - return to; - } - - public void setTo(SQLExpr to) { - this.to = to; - } - - @Override - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, name); - acceptChild(visitor, to); - } - visitor.endVisit(this); - } - - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlReplaceStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlReplaceStatement.java deleted file mode 100644 index 706726f8..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlReplaceStatement.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.expr.SQLQueryExpr; -import org.durid.sql.ast.statement.SQLInsertStatement.ValuesClause; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlReplaceStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private boolean lowPriority = false; - private boolean delayed = false; - - private SQLName tableName; - private final List columns = new ArrayList(); - private List valuesList = new ArrayList(); - private SQLQueryExpr query; - - public SQLName getTableName() { - return tableName; - } - - public void setTableName(SQLName tableName) { - this.tableName = tableName; - } - - public List getColumns() { - return columns; - } - - public boolean isLowPriority() { - return lowPriority; - } - - public void setLowPriority(boolean lowPriority) { - this.lowPriority = lowPriority; - } - - public boolean isDelayed() { - return delayed; - } - - public void setDelayed(boolean delayed) { - this.delayed = delayed; - } - - public SQLQueryExpr getQuery() { - return query; - } - - public void setQuery(SQLQueryExpr query) { - query.setParent(this); - this.query = query; - } - - public List getValuesList() { - return valuesList; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, tableName); - acceptChild(visitor, columns); - acceptChild(visitor, valuesList); - acceptChild(visitor, query); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlResetStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlResetStatement.java deleted file mode 100644 index 056f710e..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlResetStatement.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlResetStatement extends MySqlStatementImpl { - private static final long serialVersionUID = 1L; - - private List options = new ArrayList(); - - public List getOptions() { - return options; - } - - public void setOptions(List options) { - this.options = options; - } - - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlRollbackStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlRollbackStatement.java deleted file mode 100644 index 1b88f05f..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlRollbackStatement.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.statement.SQLRollbackStatement; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import org.durid.sql.visitor.SQLASTVisitor; - -public class MySqlRollbackStatement extends SQLRollbackStatement implements MySqlStatement { - - private static final long serialVersionUID = 1L; - - private Boolean chain; - private Boolean release; - - private SQLExpr force; - - public MySqlRollbackStatement(){ - - } - - public Boolean getChain() { - return chain; - } - - public void setChain(Boolean chain) { - this.chain = chain; - } - - public Boolean getRelease() { - return release; - } - - public void setRelease(Boolean release) { - this.release = release; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor instanceof MySqlASTVisitor) { - accept0((MySqlASTVisitor) visitor); - } else { - throw new IllegalArgumentException("not support visitor type : " + visitor.getClass().getName()); - } - } - - @Override - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, getTo()); - acceptChild(visitor, getForce()); - } - - visitor.endVisit(this); - } - - public SQLExpr getForce() { - return force; - } - - public void setForce(SQLExpr force) { - this.force = force; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlSQLColumnDefinition.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlSQLColumnDefinition.java deleted file mode 100644 index 54ef84f1..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlSQLColumnDefinition.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.statement.SQLColumnDefinition; - -public class MySqlSQLColumnDefinition extends SQLColumnDefinition { - - private static final long serialVersionUID = 1L; - - private boolean autoIncrement = false; - - public MySqlSQLColumnDefinition(){ - - } - - public boolean isAutoIncrement() { - return autoIncrement; - } - - public void setAutoIncrement(boolean autoIncrement) { - this.autoIncrement = autoIncrement; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlSelectGroupBy.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlSelectGroupBy.java deleted file mode 100644 index a0fa9a87..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlSelectGroupBy.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.statement.SQLSelectGroupByClause; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import org.durid.sql.visitor.SQLASTVisitor; - -public class MySqlSelectGroupBy extends SQLSelectGroupByClause { - - private static final long serialVersionUID = 1L; - - private boolean rollUp = false; - - public boolean isRollUp() { - return rollUp; - } - - public void setRollUp(boolean rollUp) { - this.rollUp = rollUp; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor instanceof MySqlASTVisitor) { - accept0((MySqlASTVisitor) visitor); - } else { - if (visitor.visit(this)) { - acceptChild(visitor, this.getItems()); - acceptChild(visitor, this.getHaving()); - } - - visitor.endVisit(this); - } - } - - protected void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.getItems()); - acceptChild(visitor, this.getHaving()); - } - - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlSelectQueryBlock.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlSelectQueryBlock.java deleted file mode 100644 index f4143e63..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlSelectQueryBlock.java +++ /dev/null @@ -1,249 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLCommentHint; -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLObjectImpl; -import org.durid.sql.ast.SQLOrderBy; -import org.durid.sql.ast.statement.SQLSelectQueryBlock; -import org.durid.sql.dialect.mysql.ast.MySqlObject; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import org.durid.sql.visitor.SQLASTVisitor; - -@SuppressWarnings("serial") -public class MySqlSelectQueryBlock extends SQLSelectQueryBlock implements MySqlObject { - - private boolean hignPriority; - private boolean straightJoin; - - private boolean smallResult; - private boolean bigResult; - private boolean bufferResult; - private Boolean cache; - private boolean calcFoundRows; - - private SQLOrderBy orderBy; - - private Limit limit; - - private SQLName procedureName; - private List procedureArgumentList = new ArrayList(); - - private boolean forUpdate = false; - private boolean lockInShareMode = false; - - private List hints = new ArrayList(); - - public MySqlSelectQueryBlock(){ - - } - - public List getHints() { - return hints; - } - - public void setHints(List hints) { - this.hints = hints; - } - - public boolean isForUpdate() { - return forUpdate; - } - - public void setForUpdate(boolean forUpdate) { - this.forUpdate = forUpdate; - } - - public boolean isLockInShareMode() { - return lockInShareMode; - } - - public void setLockInShareMode(boolean lockInShareMode) { - this.lockInShareMode = lockInShareMode; - } - - public SQLName getProcedureName() { - return procedureName; - } - - public void setProcedureName(SQLName procedureName) { - this.procedureName = procedureName; - } - - public List getProcedureArgumentList() { - return procedureArgumentList; - } - - public void setProcedureArgumentList(List procedureArgumentList) { - this.procedureArgumentList = procedureArgumentList; - } - - public boolean isHignPriority() { - return hignPriority; - } - - public void setHignPriority(boolean hignPriority) { - this.hignPriority = hignPriority; - } - - public boolean isStraightJoin() { - return straightJoin; - } - - public void setStraightJoin(boolean straightJoin) { - this.straightJoin = straightJoin; - } - - public boolean isSmallResult() { - return smallResult; - } - - public void setSmallResult(boolean smallResult) { - this.smallResult = smallResult; - } - - public boolean isBigResult() { - return bigResult; - } - - public void setBigResult(boolean bigResult) { - this.bigResult = bigResult; - } - - public boolean isBufferResult() { - return bufferResult; - } - - public void setBufferResult(boolean bufferResult) { - this.bufferResult = bufferResult; - } - - public Boolean getCache() { - return cache; - } - - public void setCache(Boolean cache) { - this.cache = cache; - } - - public boolean isCalcFoundRows() { - return calcFoundRows; - } - - public void setCalcFoundRows(boolean calcFoundRows) { - this.calcFoundRows = calcFoundRows; - } - - public SQLOrderBy getOrderBy() { - return orderBy; - } - - public void setOrderBy(SQLOrderBy orderBy) { - this.orderBy = orderBy; - } - - public Limit getLimit() { - return limit; - } - - public void setLimit(Limit limit) { - this.limit = limit; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor instanceof MySqlASTVisitor) { - accept0((MySqlASTVisitor) visitor); - return; - } - - if (visitor.visit(this)) { - acceptChild(visitor, this.selectList); - acceptChild(visitor, this.from); - acceptChild(visitor, this.where); - acceptChild(visitor, this.groupBy); - acceptChild(visitor, this.orderBy); - acceptChild(visitor, this.limit); - acceptChild(visitor, this.procedureName); - acceptChild(visitor, this.procedureArgumentList); - acceptChild(visitor, this.into); - } - - visitor.endVisit(this); - } - - @Override - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, this.selectList); - acceptChild(visitor, this.from); - acceptChild(visitor, this.where); - acceptChild(visitor, this.groupBy); - acceptChild(visitor, this.orderBy); - acceptChild(visitor, this.limit); - acceptChild(visitor, this.procedureName); - acceptChild(visitor, this.procedureArgumentList); - acceptChild(visitor, this.into); - } - - visitor.endVisit(this); - } - - public static class Limit extends SQLObjectImpl { - - public Limit(){ - - } - - private SQLExpr rowCount; - private SQLExpr offset; - - public SQLExpr getRowCount() { - return rowCount; - } - - public void setRowCount(SQLExpr rowCount) { - this.rowCount = rowCount; - } - - public SQLExpr getOffset() { - return offset; - } - - public void setOffset(SQLExpr offset) { - this.offset = offset; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor instanceof MySqlASTVisitor) { - MySqlASTVisitor mysqlVisitor = (MySqlASTVisitor) visitor; - - if (mysqlVisitor.visit(this)) { - acceptChild(visitor, offset); - acceptChild(visitor, rowCount); - } - } - } - - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlSetCharSetStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlSetCharSetStatement.java deleted file mode 100644 index 7ff01e73..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlSetCharSetStatement.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlSetCharSetStatement extends MySqlStatementImpl { - - /** - * - */ - private static final long serialVersionUID = 1L; - private boolean isDefault = false; - private String charSet; - private String collate; - - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } - - public String getCharSet() { - return charSet; - } - - public void setCharSet(String charSet) { - this.charSet = charSet; - } - - public String getCollate() { - return collate; - } - - public void setCollate(String collate) { - this.collate = collate; - } - - public boolean isDefault() { - return isDefault; - } - - public void setDefault(boolean isDefault) { - this.isDefault = isDefault; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlSetNamesStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlSetNamesStatement.java deleted file mode 100644 index 23339f5b..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlSetNamesStatement.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlSetNamesStatement extends MySqlStatementImpl { - - /** - * - */ - private static final long serialVersionUID = 1L; - private boolean isDefault = false; - private String charSet; - private String collate; - - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } - - public String getCharSet() { - return charSet; - } - - public void setCharSet(String charSet) { - this.charSet = charSet; - } - - public String getCollate() { - return collate; - } - - public void setCollate(String collate) { - this.collate = collate; - } - - public boolean isDefault() { - return isDefault; - } - - public void setDefault(boolean isDefault) { - this.isDefault = isDefault; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlSetTransactionIsolationLevelStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlSetTransactionIsolationLevelStatement.java deleted file mode 100644 index f640fb36..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlSetTransactionIsolationLevelStatement.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlSetTransactionIsolationLevelStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private Boolean global; - - private String level; - - public String getLevel() { - return level; - } - - public void setLevel(String level) { - this.level = level; - } - - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } - - public Boolean getGlobal() { - return global; - } - - public void setGlobal(Boolean global) { - this.global = global; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowAuthorsStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowAuthorsStatement.java deleted file mode 100644 index 983d8207..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowAuthorsStatement.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - - -public class MySqlShowAuthorsStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowBinLogEventsStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowBinLogEventsStatement.java deleted file mode 100644 index 6157907c..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowBinLogEventsStatement.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowBinLogEventsStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr in; - private SQLExpr from; - private Limit limit; - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, in); - acceptChild(visitor, from); - acceptChild(visitor, limit); - } - visitor.endVisit(this); - } - - public SQLExpr getIn() { - return in; - } - - public void setIn(SQLExpr in) { - this.in = in; - } - - public SQLExpr getFrom() { - return from; - } - - public void setFrom(SQLExpr from) { - this.from = from; - } - - public Limit getLimit() { - return limit; - } - - public void setLimit(Limit limit) { - this.limit = limit; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowBinaryLogsStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowBinaryLogsStatement.java deleted file mode 100644 index 95f86261..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowBinaryLogsStatement.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - - -public class MySqlShowBinaryLogsStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCharacterSetStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCharacterSetStatement.java deleted file mode 100644 index ec642c93..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCharacterSetStatement.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowCharacterSetStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr where; - private SQLExpr pattern; - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, where); - acceptChild(visitor, pattern); - } - visitor.endVisit(this); - } - - public SQLExpr getWhere() { - return where; - } - - public void setWhere(SQLExpr where) { - this.where = where; - } - - public SQLExpr getPattern() { - return pattern; - } - - public void setPattern(SQLExpr pattern) { - this.pattern = pattern; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCollationStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCollationStatement.java deleted file mode 100644 index e333d9ae..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCollationStatement.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowCollationStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr where; - private SQLExpr pattern; - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, where); - acceptChild(visitor, pattern); - } - visitor.endVisit(this); - } - - public SQLExpr getWhere() { - return where; - } - - public void setWhere(SQLExpr where) { - this.where = where; - } - - public SQLExpr getPattern() { - return pattern; - } - - public void setPattern(SQLExpr pattern) { - this.pattern = pattern; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowColumnsStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowColumnsStatement.java deleted file mode 100644 index 4317eb86..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowColumnsStatement.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.expr.SQLIdentifierExpr; -import org.durid.sql.ast.expr.SQLPropertyExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowColumnsStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private boolean full; - - private SQLName table; - private SQLName database; - private SQLExpr like; - private SQLExpr where; - - public boolean isFull() { - return full; - } - - public void setFull(boolean full) { - this.full = full; - } - - public SQLName getTable() { - return table; - } - - public void setTable(SQLName table) { - if (table instanceof SQLPropertyExpr) { - SQLPropertyExpr propExpr = (SQLPropertyExpr) table; - this.setDatabase((SQLName) propExpr.getOwner()); - this.table = new SQLIdentifierExpr(propExpr.getName()); - return; - } - this.table = table; - } - - public SQLName getDatabase() { - return database; - } - - public void setDatabase(SQLName database) { - this.database = database; - } - - public SQLExpr getLike() { - return like; - } - - public void setLike(SQLExpr like) { - this.like = like; - } - - public SQLExpr getWhere() { - return where; - } - - public void setWhere(SQLExpr where) { - this.where = where; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, table); - acceptChild(visitor, database); - acceptChild(visitor, like); - acceptChild(visitor, where); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowContributorsStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowContributorsStatement.java deleted file mode 100644 index 0695d3ce..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowContributorsStatement.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - - -public class MySqlShowContributorsStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateDatabaseStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateDatabaseStatement.java deleted file mode 100644 index 052b6239..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateDatabaseStatement.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowCreateDatabaseStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr database; - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, database); - } - visitor.endVisit(this); - } - - public SQLExpr getDatabase() { - return database; - } - - public void setDatabase(SQLExpr database) { - this.database = database; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateEventStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateEventStatement.java deleted file mode 100644 index 59a1749e..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateEventStatement.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowCreateEventStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr eventName; - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, eventName); - } - visitor.endVisit(this); - } - - public SQLExpr getEventName() { - return eventName; - } - - public void setEventName(SQLExpr eventName) { - this.eventName = eventName; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateFunctionStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateFunctionStatement.java deleted file mode 100644 index 5957f4ac..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateFunctionStatement.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowCreateFunctionStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr name; - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, name); - } - visitor.endVisit(this); - } - - public SQLExpr getName() { - return name; - } - - public void setName(SQLExpr functionName) { - this.name = functionName; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateProcedureStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateProcedureStatement.java deleted file mode 100644 index 6d972424..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateProcedureStatement.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowCreateProcedureStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr name; - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, name); - } - visitor.endVisit(this); - } - - public SQLExpr getName() { - return name; - } - - public void setName(SQLExpr functionName) { - this.name = functionName; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateTableStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateTableStatement.java deleted file mode 100644 index d3b38814..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateTableStatement.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowCreateTableStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr name; - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, name); - } - visitor.endVisit(this); - } - - public SQLExpr getName() { - return name; - } - - public void setName(SQLExpr functionName) { - this.name = functionName; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateTriggerStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateTriggerStatement.java deleted file mode 100644 index 59d318f8..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateTriggerStatement.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowCreateTriggerStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr name; - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, name); - } - visitor.endVisit(this); - } - - public SQLExpr getName() { - return name; - } - - public void setName(SQLExpr functionName) { - this.name = functionName; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateViewStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateViewStatement.java deleted file mode 100644 index d6326bba..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowCreateViewStatement.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowCreateViewStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr name; - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, name); - } - visitor.endVisit(this); - } - - public SQLExpr getName() { - return name; - } - - public void setName(SQLExpr functionName) { - this.name = functionName; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowDatabasesStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowDatabasesStatement.java deleted file mode 100644 index c44afa23..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowDatabasesStatement.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLName; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowDatabasesStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLName database; - private SQLExpr like; - private SQLExpr where; - - public SQLName getDatabase() { - return database; - } - - public void setDatabase(SQLName database) { - this.database = database; - } - - public SQLExpr getLike() { - return like; - } - - public void setLike(SQLExpr like) { - this.like = like; - } - - public SQLExpr getWhere() { - return where; - } - - public void setWhere(SQLExpr where) { - this.where = where; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, database); - acceptChild(visitor, like); - acceptChild(visitor, where); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowEngineStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowEngineStatement.java deleted file mode 100644 index 23f75ae4..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowEngineStatement.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowEngineStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr name; - private Option option; - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, name); - } - visitor.endVisit(this); - } - - public SQLExpr getName() { - return name; - } - - public void setName(SQLExpr name) { - this.name = name; - } - - public Option getOption() { - return option; - } - - public void setOption(Option option) { - this.option = option; - } - - public static enum Option { - STATUS, MUTEX - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowEnginesStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowEnginesStatement.java deleted file mode 100644 index 6d2dad0a..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowEnginesStatement.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowEnginesStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - private boolean storage = false; - - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } - - public boolean isStorage() { - return storage; - } - - public void setStorage(boolean storage) { - this.storage = storage; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowErrorsStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowErrorsStatement.java deleted file mode 100644 index 6cbed938..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowErrorsStatement.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowErrorsStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private boolean count = false; - private Limit limit; - - public boolean isCount() { - return count; - } - - public void setCount(boolean count) { - this.count = count; - } - - public Limit getLimit() { - return limit; - } - - public void setLimit(Limit limit) { - this.limit = limit; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, limit); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowEventsStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowEventsStatement.java deleted file mode 100644 index 1f592f7e..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowEventsStatement.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowEventsStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr schema; - private SQLExpr like; - private SQLExpr where; - - public SQLExpr getSchema() { - return schema; - } - - public void setSchema(SQLExpr schema) { - this.schema = schema; - } - - public SQLExpr getLike() { - return like; - } - - public void setLike(SQLExpr like) { - this.like = like; - } - - public SQLExpr getWhere() { - return where; - } - - public void setWhere(SQLExpr where) { - this.where = where; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, schema); - acceptChild(visitor, like); - acceptChild(visitor, where); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowFunctionCodeStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowFunctionCodeStatement.java deleted file mode 100644 index ab12db64..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowFunctionCodeStatement.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowFunctionCodeStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr name; - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, name); - } - visitor.endVisit(this); - } - - public SQLExpr getName() { - return name; - } - - public void setName(SQLExpr functionName) { - this.name = functionName; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowFunctionStatusStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowFunctionStatusStatement.java deleted file mode 100644 index 5056c086..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowFunctionStatusStatement.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowFunctionStatusStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr like; - private SQLExpr where; - - public SQLExpr getLike() { - return like; - } - - public void setLike(SQLExpr like) { - this.like = like; - } - - public SQLExpr getWhere() { - return where; - } - - public void setWhere(SQLExpr where) { - this.where = where; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, like); - acceptChild(visitor, where); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowGrantsStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowGrantsStatement.java deleted file mode 100644 index 1e5df738..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowGrantsStatement.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowGrantsStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr user; - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, user); - } - visitor.endVisit(this); - } - - public SQLExpr getUser() { - return user; - } - - public void setUser(SQLExpr user) { - this.user = user; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowIndexesStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowIndexesStatement.java deleted file mode 100644 index f04adba2..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowIndexesStatement.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.expr.SQLIdentifierExpr; -import org.durid.sql.ast.expr.SQLPropertyExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowIndexesStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLName table; - private SQLName database; - - public SQLName getTable() { - return table; - } - - public void setTable(SQLName table) { - if (table instanceof SQLPropertyExpr) { - SQLPropertyExpr propExpr = (SQLPropertyExpr) table; - this.setDatabase((SQLName) propExpr.getOwner()); - this.table = new SQLIdentifierExpr(propExpr.getName()); - return; - } - this.table = table; - } - - public SQLName getDatabase() { - return database; - } - - public void setDatabase(SQLName database) { - this.database = database; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, table); - acceptChild(visitor, database); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowKeysStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowKeysStatement.java deleted file mode 100644 index a118907d..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowKeysStatement.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.expr.SQLIdentifierExpr; -import org.durid.sql.ast.expr.SQLPropertyExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowKeysStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLName table; - private SQLName database; - - public SQLName getTable() { - return table; - } - - public void setTable(SQLName table) { - if (table instanceof SQLPropertyExpr) { - SQLPropertyExpr propExpr = (SQLPropertyExpr) table; - this.setDatabase((SQLName) propExpr.getOwner()); - this.table = new SQLIdentifierExpr(propExpr.getName()); - return; - } - this.table = table; - } - - public SQLName getDatabase() { - return database; - } - - public void setDatabase(SQLName database) { - this.database = database; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, table); - acceptChild(visitor, database); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowMasterLogsStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowMasterLogsStatement.java deleted file mode 100644 index a8391d32..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowMasterLogsStatement.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - - -public class MySqlShowMasterLogsStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowMasterStatusStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowMasterStatusStatement.java deleted file mode 100644 index 559ecfa3..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowMasterStatusStatement.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - - -public class MySqlShowMasterStatusStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowOpenTablesStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowOpenTablesStatement.java deleted file mode 100644 index 0593d1ac..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowOpenTablesStatement.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowOpenTablesStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr database; - private SQLExpr like; - private SQLExpr where; - - public SQLExpr getLike() { - return like; - } - - public void setLike(SQLExpr like) { - this.like = like; - } - - public SQLExpr getWhere() { - return where; - } - - public void setWhere(SQLExpr where) { - this.where = where; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, database); - acceptChild(visitor, like); - acceptChild(visitor, where); - } - visitor.endVisit(this); - } - - public SQLExpr getDatabase() { - return database; - } - - public void setDatabase(SQLExpr database) { - this.database = database; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowPluginsStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowPluginsStatement.java deleted file mode 100644 index 4113f962..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowPluginsStatement.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - - -public class MySqlShowPluginsStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowPrivilegesStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowPrivilegesStatement.java deleted file mode 100644 index c0edb11c..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowPrivilegesStatement.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - - -public class MySqlShowPrivilegesStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowProcedureCodeStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowProcedureCodeStatement.java deleted file mode 100644 index c96d8972..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowProcedureCodeStatement.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowProcedureCodeStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr name; - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, name); - } - visitor.endVisit(this); - } - - public SQLExpr getName() { - return name; - } - - public void setName(SQLExpr functionName) { - this.name = functionName; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowProcedureStatusStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowProcedureStatusStatement.java deleted file mode 100644 index a91a4dd1..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowProcedureStatusStatement.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowProcedureStatusStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr like; - private SQLExpr where; - - public SQLExpr getLike() { - return like; - } - - public void setLike(SQLExpr like) { - this.like = like; - } - - public SQLExpr getWhere() { - return where; - } - - public void setWhere(SQLExpr where) { - this.where = where; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, like); - acceptChild(visitor, where); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowProcessListStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowProcessListStatement.java deleted file mode 100644 index 5d35c9dd..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowProcessListStatement.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowProcessListStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private boolean full = false; - - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } - - public boolean isFull() { - return full; - } - - public void setFull(boolean full) { - this.full = full; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowProfileStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowProfileStatement.java deleted file mode 100644 index 3e03b693..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowProfileStatement.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowProfileStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private List types = new ArrayList(); - - private SQLExpr forQuery; - - private Limit limit; - - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } - - public List getTypes() { - return types; - } - - public void setTypes(List types) { - this.types = types; - } - - public SQLExpr getForQuery() { - return forQuery; - } - - public void setForQuery(SQLExpr forQuery) { - this.forQuery = forQuery; - } - - public Limit getLimit() { - return limit; - } - - public void setLimit(Limit limit) { - this.limit = limit; - } - - public static enum Type { - ALL("ALL"), BLOCK_IO("BLOCK IO"), CONTEXT_SWITCHES("CONTEXT SWITCHES"), CPU("CPU"), IPC("IPC"), - MEMORY("MEMORY"), PAGE_FAULTS("PAGE FAULTS"), SOURCE("SOURCE"), SWAPS("SWAPS"); - - public final String name; - - Type(String name){ - this.name = name; - } - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowProfilesStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowProfilesStatement.java deleted file mode 100644 index 17277e93..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowProfilesStatement.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowProfilesStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowRelayLogEventsStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowRelayLogEventsStatement.java deleted file mode 100644 index 521b76b4..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowRelayLogEventsStatement.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowRelayLogEventsStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr logName; - private SQLExpr from; - private Limit limit; - - public SQLExpr getLogName() { - return logName; - } - - public void setLogName(SQLExpr logName) { - this.logName = logName; - } - - public SQLExpr getFrom() { - return from; - } - - public void setFrom(SQLExpr from) { - this.from = from; - } - - public Limit getLimit() { - return limit; - } - - public void setLimit(Limit limit) { - this.limit = limit; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, logName); - acceptChild(visitor, from); - acceptChild(visitor, limit); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowSlaveHostsStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowSlaveHostsStatement.java deleted file mode 100644 index cb2c4327..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowSlaveHostsStatement.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowSlaveHostsStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowSlaveStatusStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowSlaveStatusStatement.java deleted file mode 100644 index e521d1fc..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowSlaveStatusStatement.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowSlaveStatusStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowStatusStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowStatusStatement.java deleted file mode 100644 index a5cb0dca..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowStatusStatement.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowStatusStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private boolean global = false; - private boolean session = false; - - private SQLExpr like; - private SQLExpr where; - - public boolean isGlobal() { - return global; - } - - public void setGlobal(boolean global) { - this.global = global; - } - - public boolean isSession() { - return session; - } - - public void setSession(boolean session) { - this.session = session; - } - - public SQLExpr getLike() { - return like; - } - - public void setLike(SQLExpr like) { - this.like = like; - } - - public SQLExpr getWhere() { - return where; - } - - public void setWhere(SQLExpr where) { - this.where = where; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, like); - acceptChild(visitor, where); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowTableStatusStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowTableStatusStatement.java deleted file mode 100644 index 9ae660e1..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowTableStatusStatement.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowTableStatusStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLExpr database; - private SQLExpr like; - private SQLExpr where; - - public SQLExpr getDatabase() { - return database; - } - - public void setDatabase(SQLExpr database) { - this.database = database; - } - - public SQLExpr getLike() { - return like; - } - - public void setLike(SQLExpr like) { - this.like = like; - } - - public SQLExpr getWhere() { - return where; - } - - public void setWhere(SQLExpr where) { - this.where = where; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, database); - acceptChild(visitor, like); - acceptChild(visitor, where); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowTablesStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowTablesStatement.java deleted file mode 100644 index 08d28cbd..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowTablesStatement.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLName; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowTablesStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private boolean full; - - private SQLName database; - private SQLExpr like; - private SQLExpr where; - - public boolean isFull() { - return full; - } - - public void setFull(boolean full) { - this.full = full; - } - - public SQLName getDatabase() { - return database; - } - - public void setDatabase(SQLName database) { - this.database = database; - } - - public SQLExpr getLike() { - return like; - } - - public void setLike(SQLExpr like) { - this.like = like; - } - - public SQLExpr getWhere() { - return where; - } - - public void setWhere(SQLExpr where) { - this.where = where; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, database); - acceptChild(visitor, like); - acceptChild(visitor, where); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowTriggersStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowTriggersStatement.java deleted file mode 100644 index 0a04dec4..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowTriggersStatement.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLName; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowTriggersStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private SQLName database; - private SQLExpr like; - private SQLExpr where; - - public SQLName getDatabase() { - return database; - } - - public void setDatabase(SQLName database) { - this.database = database; - } - - public SQLExpr getLike() { - return like; - } - - public void setLike(SQLExpr like) { - this.like = like; - } - - public SQLExpr getWhere() { - return where; - } - - public void setWhere(SQLExpr where) { - this.where = where; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, database); - acceptChild(visitor, like); - acceptChild(visitor, where); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowVariantsStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowVariantsStatement.java deleted file mode 100644 index dccc1b56..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowVariantsStatement.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowVariantsStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private boolean global = false; - private boolean session = false; - - private SQLExpr like; - private SQLExpr where; - - public boolean isGlobal() { - return global; - } - - public void setGlobal(boolean global) { - this.global = global; - } - - public boolean isSession() { - return session; - } - - public void setSession(boolean session) { - this.session = session; - } - - public SQLExpr getLike() { - return like; - } - - public void setLike(SQLExpr like) { - this.like = like; - } - - public SQLExpr getWhere() { - return where; - } - - public void setWhere(SQLExpr where) { - this.where = where; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, like); - acceptChild(visitor, where); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowWarningsStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowWarningsStatement.java deleted file mode 100644 index 85ff367e..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlShowWarningsStatement.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlShowWarningsStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private boolean count = false; - private Limit limit; - - public boolean isCount() { - return count; - } - - public void setCount(boolean count) { - this.count = count; - } - - public Limit getLimit() { - return limit; - } - - public void setLimit(Limit limit) { - this.limit = limit; - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, limit); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlStartTransactionStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlStartTransactionStatement.java deleted file mode 100644 index ea9b93df..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlStartTransactionStatement.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - -public class MySqlStartTransactionStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - private boolean consistentSnapshot = false; - - private boolean begin = false; - private boolean work = false; - - public boolean isConsistentSnapshot() { - return consistentSnapshot; - } - - public void setConsistentSnapshot(boolean consistentSnapshot) { - this.consistentSnapshot = consistentSnapshot; - } - - public boolean isBegin() { - return begin; - } - - public void setBegin(boolean begin) { - this.begin = begin; - } - - public boolean isWork() { - return work; - } - - public void setWork(boolean work) { - this.work = work; - } - - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlStatement.java deleted file mode 100644 index da5dc6d6..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlStatement.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLStatement; -import org.durid.sql.dialect.mysql.ast.MySqlObject; - -public interface MySqlStatement extends SQLStatement, MySqlObject { - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlStatementImpl.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlStatementImpl.java deleted file mode 100644 index 931c6518..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlStatementImpl.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLStatementImpl; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import org.durid.sql.visitor.SQLASTVisitor; - -public abstract class MySqlStatementImpl extends SQLStatementImpl implements MySqlStatement { - - private static final long serialVersionUID = 1L; - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor instanceof MySqlASTVisitor) { - accept0((MySqlASTVisitor) visitor); - } else { - throw new IllegalArgumentException("not support visitor type : " + visitor.getClass().getName()); - } - } - - public void accept0(MySqlASTVisitor visitor) { - throw new UnsupportedOperationException(this.getClass().getName()); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlTableIndex.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlTableIndex.java deleted file mode 100644 index 29801c3c..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlTableIndex.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLObjectImpl; -import org.durid.sql.ast.statement.SQLTableElement; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import org.durid.sql.visitor.SQLASTVisitor; - -@SuppressWarnings("serial") -public class MySqlTableIndex extends SQLObjectImpl implements SQLTableElement { - - private SQLName name; - private String indexType; - private List columns = new ArrayList(); - - public MySqlTableIndex(){ - - } - - public SQLName getName() { - return name; - } - - public String getIndexType() { - return indexType; - } - - public void setIndexType(String indexType) { - this.indexType = indexType; - } - - public void setName(SQLName name) { - this.name = name; - } - - public List getColumns() { - return columns; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor instanceof MySqlASTVisitor) { - accept0((MySqlASTVisitor) visitor); - return; - } - - if (visitor.visit(this)) { - acceptChild(visitor, name); - acceptChild(visitor, columns); - } - visitor.endVisit(this); - } - - protected void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, name); - acceptChild(visitor, columns); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlUnionQuery.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlUnionQuery.java deleted file mode 100644 index a38e84db..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlUnionQuery.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.statement.SQLUnionQuery; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import org.durid.sql.visitor.SQLASTVisitor; - -public class MySqlUnionQuery extends SQLUnionQuery implements MySqlStatement { - - private static final long serialVersionUID = 1L; - - private Limit limit; - - public Limit getLimit() { - return limit; - } - - public void setLimit(Limit limit) { - this.limit = limit; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor instanceof MySqlASTVisitor) { - accept0((MySqlASTVisitor) visitor); - } else { - throw new IllegalArgumentException("not support visitor type : " + visitor.getClass().getName()); - } - } - - @Override - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, getLeft()); - acceptChild(visitor, getRight()); - acceptChild(visitor, getOrderBy()); - acceptChild(visitor, getLimit()); - } - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlUnlockTablesStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlUnlockTablesStatement.java deleted file mode 100644 index 9ec17ea8..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlUnlockTablesStatement.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; - - -public class MySqlUnlockTablesStatement extends MySqlStatementImpl { - - private static final long serialVersionUID = 1L; - - public void accept0(MySqlASTVisitor visitor) { - visitor.visit(this); - visitor.endVisit(this); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlUpdateStatement.java b/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlUpdateStatement.java deleted file mode 100644 index ee467ed9..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/ast/statement/MySqlUpdateStatement.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.ast.statement; - -import org.durid.sql.ast.SQLOrderBy; -import org.durid.sql.ast.statement.SQLUpdateStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit; -import org.durid.sql.dialect.mysql.visitor.MySqlASTVisitor; -import org.durid.sql.visitor.SQLASTVisitor; - -public class MySqlUpdateStatement extends SQLUpdateStatement implements MySqlStatement { - - private static final long serialVersionUID = 1L; - private SQLOrderBy orderBy; - private Limit limit; - - private boolean lowPriority = false; - private boolean ignore = false; - - public Limit getLimit() { - return limit; - } - - public void setLimit(Limit limit) { - this.limit = limit; - } - - @Override - protected void accept0(SQLASTVisitor visitor) { - if (visitor instanceof MySqlASTVisitor) { - accept0((MySqlASTVisitor) visitor); - } else { - throw new IllegalArgumentException("not support visitor type : " + visitor.getClass().getName()); - } - } - - public void accept0(MySqlASTVisitor visitor) { - if (visitor.visit(this)) { - acceptChild(visitor, tableSource); - acceptChild(visitor, items); - acceptChild(visitor, where); - acceptChild(visitor, orderBy); - acceptChild(visitor, limit); - } - visitor.endVisit(this); - } - - public boolean isLowPriority() { - return lowPriority; - } - - public void setLowPriority(boolean lowPriority) { - this.lowPriority = lowPriority; - } - - public boolean isIgnore() { - return ignore; - } - - public void setIgnore(boolean ignore) { - this.ignore = ignore; - } - - public SQLOrderBy getOrderBy() { - return orderBy; - } - - public void setOrderBy(SQLOrderBy orderBy) { - this.orderBy = orderBy; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlCreateTableParser.java b/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlCreateTableParser.java deleted file mode 100644 index 2fbfc611..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlCreateTableParser.java +++ /dev/null @@ -1,227 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.parser; - -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.statement.SQLColumnDefinition; -import org.durid.sql.ast.statement.SQLCreateTableStatement; -import org.durid.sql.ast.statement.SQLSelect; -import org.durid.sql.ast.statement.SQLTableConstaint; -import org.durid.sql.dialect.mysql.ast.MySqlKey; -import org.durid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlPartitionByKey; -import org.durid.sql.dialect.mysql.ast.statement.MySqlTableIndex; -import org.durid.sql.parser.ParserException; -import org.durid.sql.parser.SQLCreateTableParser; -import org.durid.sql.parser.SQLExprParser; -import org.durid.sql.parser.Token; - -public class MySqlCreateTableParser extends SQLCreateTableParser { - - public MySqlCreateTableParser(String sql) { - super(new MySqlExprParser(sql)); - } - - public MySqlCreateTableParser(SQLExprParser exprParser){ - super(exprParser); - } - - public SQLCreateTableStatement parseCrateTable() { - return parseCrateTable(true); - } - - public MySqlCreateTableStatement parseCrateTable(boolean acceptCreate) { - if (acceptCreate) { - accept(Token.CREATE); - } - MySqlCreateTableStatement stmt = new MySqlCreateTableStatement(); - - if (identifierEquals("TEMPORARY")) { - lexer.nextToken(); - stmt.setType(SQLCreateTableStatement.Type.GLOBAL_TEMPORARY); - } - - accept(Token.TABLE); - - if (lexer.token() == Token.IF || identifierEquals("IF")) { - lexer.nextToken(); - accept(Token.NOT); - accept(Token.EXISTS); - - stmt.setIfNotExiists(true); - } - - stmt.setName(this.exprParser.name()); - - if (lexer.token() == (Token.LPAREN)) { - lexer.nextToken(); - - for (;;) { - if (lexer.token() == Token.IDENTIFIER) { - SQLColumnDefinition column = this.exprParser.parseColumn(); - stmt.getTableElementList().add(column); - } else if (lexer.token() == (Token.CONSTRAINT)) { - stmt.getTableElementList().add(parseConstraint()); - } else if (lexer.token() == (Token.INDEX)) { - lexer.nextToken(); - - MySqlTableIndex idx = new MySqlTableIndex(); - - if (lexer.token() == Token.IDENTIFIER) { - if (!"USING".equalsIgnoreCase(lexer.stringVal())) { - idx.setName(this.exprParser.name()); - } - } - - if (identifierEquals("USING")) { - lexer.nextToken(); - idx.setIndexType(lexer.stringVal()); - lexer.nextToken(); - } - - accept(Token.LPAREN); - for (;;) { - idx.getColumns().add(this.exprParser.expr()); - if (!(lexer.token() == (Token.COMMA))) { - break; - } else { - lexer.nextToken(); - } - } - accept(Token.RPAREN); - - stmt.getTableElementList().add(idx); - } else if (lexer.token() == (Token.KEY)) { - stmt.getTableElementList().add(parseConstraint()); - } else if (lexer.token() == (Token.PRIMARY)) { - stmt.getTableElementList().add(parseConstraint()); - } - - if (!(lexer.token() == (Token.COMMA))) { - break; - } else { - lexer.nextToken(); - } - } - - accept(Token.RPAREN); - } - - for (;;) { - if (identifierEquals("ENGINE")) { - lexer.nextToken(); - accept(Token.EQ); - stmt.getTableOptions().put("ENGINE", lexer.stringVal()); - lexer.nextToken(); - continue; - } - - if (identifierEquals("TYPE")) { - lexer.nextToken(); - accept(Token.EQ); - stmt.getTableOptions().put("TYPE", lexer.stringVal()); - lexer.nextToken(); - continue; - } - - if (identifierEquals("PARTITION")) { - lexer.nextToken(); - accept(Token.BY); - - if (lexer.token() == Token.KEY) { - MySqlPartitionByKey clause = new MySqlPartitionByKey(); - lexer.nextToken(); - accept(Token.LPAREN); - for (;;) { - clause.getColumns().add(this.exprParser.name()); - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; - } - break; - } - accept(Token.RPAREN); - stmt.setPartitioning(clause); - - if (identifierEquals("PARTITIONS")) { - lexer.nextToken(); - clause.setPartitionCount(this.exprParser.expr()); - } - } else { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } - } - - break; - } - - if (lexer.token() == (Token.ON)) { - throw new ParserException("TODO"); - } - - if (lexer.token() == (Token.SELECT)) { - SQLSelect query = new MySqlSelectParser(this.exprParser).select(); - stmt.setQuery(query); - } - - return stmt; - } - - @SuppressWarnings("unused") - protected SQLTableConstaint parseConstraint() { - SQLName name = null; - if (lexer.token() == (Token.CONSTRAINT)) { - lexer.nextToken(); - } - - if (lexer.token() == Token.IDENTIFIER) { - name = this.exprParser.name(); - } - - if (lexer.token() == (Token.KEY)) { - lexer.nextToken(); - - MySqlKey key = new MySqlKey(); - - if (identifierEquals("USING")) { - lexer.nextToken(); - key.setIndexType(lexer.stringVal()); - lexer.nextToken(); - } - - accept(Token.LPAREN); - for (;;) { - key.getColumns().add(this.exprParser.expr()); - if (!(lexer.token() == (Token.COMMA))) { - break; - } else { - lexer.nextToken(); - } - } - accept(Token.RPAREN); - - return key; - } - - if (lexer.token() == (Token.PRIMARY)) { - return (SQLTableConstaint) this.exprParser.parsePrimaryKey(); - } - - throw new ParserException("TODO"); - } - - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlExprParser.java b/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlExprParser.java deleted file mode 100644 index 72249317..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlExprParser.java +++ /dev/null @@ -1,644 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.parser; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.expr.SQLBinaryOpExpr; -import org.durid.sql.ast.expr.SQLBinaryOperator; -import org.durid.sql.ast.expr.SQLCharExpr; -import org.durid.sql.ast.expr.SQLHexExpr; -import org.durid.sql.ast.expr.SQLIdentifierExpr; -import org.durid.sql.ast.expr.SQLIntegerExpr; -import org.durid.sql.ast.expr.SQLMethodInvokeExpr; -import org.durid.sql.ast.expr.SQLVariantRefExpr; -import org.durid.sql.ast.statement.SQLAssignItem; -import org.durid.sql.ast.statement.SQLColumnDefinition; -import org.durid.sql.ast.statement.SQLPrimaryKey; -import org.durid.sql.dialect.mysql.ast.MySqlPrimaryKey; -import org.durid.sql.dialect.mysql.ast.expr.MySqlBinaryExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlBooleanExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlCharExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlExtractExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlIntervalExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlIntervalUnit; -import org.durid.sql.dialect.mysql.ast.expr.MySqlMatchAgainstExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlMatchAgainstExpr.SearchModifier; -import org.durid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlUserName; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSQLColumnDefinition; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit; -import org.durid.sql.parser.Lexer; -import org.durid.sql.parser.ParserException; -import org.durid.sql.parser.SQLExprParser; -import org.durid.sql.parser.SQLSelectParser; -import org.durid.sql.parser.Token; - -public class MySqlExprParser extends SQLExprParser { - - public MySqlExprParser(Lexer lexer){ - super(lexer); - } - - public MySqlExprParser(String sql) { - this(new MySqlLexer(sql)); - this.lexer.nextToken(); - } - - public SQLExpr relationalRest(SQLExpr expr) { - if (identifierEquals("REGEXP")) { - lexer.nextToken(); - SQLExpr rightExp = equality(); - - rightExp = relationalRest(rightExp); - - return new SQLBinaryOpExpr(expr, SQLBinaryOperator.RegExp, rightExp); - } - - if (identifierEquals("RLIKE")) { - lexer.nextToken(); - SQLExpr rightExp = equality(); - - rightExp = relationalRest(rightExp); - - return new SQLBinaryOpExpr(expr, SQLBinaryOperator.RegExp, rightExp); - } - - return super.relationalRest(expr); - } - - public SQLExpr multiplicativeRest(SQLExpr expr) { - if (lexer.token() == Token.IDENTIFIER && "MOD".equalsIgnoreCase(lexer.stringVal())) { - lexer.nextToken(); - SQLExpr rightExp = primary(); - - rightExp = relationalRest(rightExp); - - return new SQLBinaryOpExpr(expr, SQLBinaryOperator.Modulus, rightExp); - } - - return super.multiplicativeRest(expr); - } - - public SQLExpr notRationalRest(SQLExpr expr) { - if (identifierEquals("REGEXP")) { - lexer.nextToken(); - SQLExpr rightExp = primary(); - - rightExp = relationalRest(rightExp); - - return new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotRegExp, rightExp); - } - - if (identifierEquals("RLIKE")) { - lexer.nextToken(); - SQLExpr rightExp = primary(); - - rightExp = relationalRest(rightExp); - - return new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotRLike, rightExp); - } - - return super.notRationalRest(expr); - } - - public SQLExpr primary() { - final Token tok = lexer.token(); - - if (identifierEquals("outfile")) { - lexer.nextToken(); - SQLExpr file = primary(); - SQLExpr expr = new MySqlOutFileExpr(file); - - return primaryRest(expr); - - } - - switch (tok) { - case TRUE: - lexer.nextToken(); - return primaryRest(new MySqlBooleanExpr(true)); - case FALSE: - lexer.nextToken(); - return primaryRest(new MySqlBooleanExpr(false)); - case LITERAL_ALIAS: - String aliasValue = lexer.stringVal(); - lexer.nextToken(); - return primaryRest(new SQLCharExpr(aliasValue)); - case VARIANT: - SQLVariantRefExpr varRefExpr = new SQLVariantRefExpr(lexer.stringVal()); - lexer.nextToken(); - if (varRefExpr.getName().equalsIgnoreCase("@@global")) { - accept(Token.DOT); - varRefExpr = new SQLVariantRefExpr(lexer.stringVal(), true); - lexer.nextToken(); - } else if (varRefExpr.getName().equals("@") && lexer.token() == Token.LITERAL_CHARS) { - varRefExpr.setName("@'" + lexer.stringVal() + "'"); - lexer.nextToken(); - } else if (varRefExpr.getName().equals("@@") && lexer.token() == Token.LITERAL_CHARS) { - varRefExpr.setName("@@'" + lexer.stringVal() + "'"); - lexer.nextToken(); - } - return primaryRest(varRefExpr); - case VALUES: - lexer.nextToken(); - if (lexer.token() != Token.LPAREN) { - throw new ParserException("syntax error, illegal values clause"); - } - return this.methodRest(new SQLIdentifierExpr("VALUES"), true); - default: - return super.primary(); - } - - } - - public final SQLExpr primaryRest(SQLExpr expr) { - if (expr == null) { - throw new IllegalArgumentException("expr"); - } - - if (lexer.token() == Token.LITERAL_CHARS) { - if (expr instanceof SQLIdentifierExpr) { - SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr; - String ident = identExpr.getName(); - - if (ident.equalsIgnoreCase("x")) { - String charValue = lexer.stringVal(); - lexer.nextToken(); - expr = new SQLHexExpr(charValue); - - return primaryRest(expr); - } else if (ident.equalsIgnoreCase("b")) { - String charValue = lexer.stringVal(); - lexer.nextToken(); - expr = new MySqlBinaryExpr(charValue); - - return primaryRest(expr); - } else if (ident.startsWith("_")) { - String charValue = lexer.stringVal(); - lexer.nextToken(); - - MySqlCharExpr mysqlCharExpr = new MySqlCharExpr(charValue); - mysqlCharExpr.setCharset(identExpr.getName()); - if (identifierEquals("COLLATE")) { - lexer.nextToken(); - - String collate = lexer.stringVal(); - mysqlCharExpr.setCollate(collate); - accept(Token.IDENTIFIER); - } - - expr = mysqlCharExpr; - - return primaryRest(expr); - } else if (ident.equalsIgnoreCase("BINARY")) { - String charValue = lexer.stringVal(); - lexer.nextToken(); - - MySqlCharExpr mysqlCharExpr = new MySqlCharExpr(charValue); - mysqlCharExpr.setCharset("BINARY"); - expr = mysqlCharExpr; - - return primaryRest(expr); - } - } else if (expr instanceof SQLCharExpr) { - SQLMethodInvokeExpr concat = new SQLMethodInvokeExpr("CONCAT"); - concat.getParameters().add(expr); - do { - String chars = lexer.stringVal(); - concat.getParameters().add(new SQLCharExpr(chars)); - lexer.nextToken(); - } while (lexer.token() == Token.LITERAL_CHARS); - expr = concat; - } - } else if (lexer.token() == Token.IDENTIFIER) { - if (expr instanceof SQLHexExpr) { - if ("USING".equalsIgnoreCase(lexer.stringVal())) { - lexer.nextToken(); - if (lexer.token() != Token.IDENTIFIER) { - throw new ParserException("syntax error, illegal hex"); - } - String charSet = lexer.stringVal(); - lexer.nextToken(); - expr.getAttributes().put("USING", charSet); - - return primaryRest(expr); - } - } else if ("COLLATE".equalsIgnoreCase(lexer.stringVal())) { - lexer.nextToken(); - - if (lexer.token() != Token.IDENTIFIER) { - throw new ParserException("syntax error"); - } - - String collate = lexer.stringVal(); - lexer.nextToken(); - - SQLBinaryOpExpr binaryExpr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.COLLATE, - new SQLIdentifierExpr(collate)); - - expr = binaryExpr; - - return primaryRest(expr); - } else if (expr instanceof SQLVariantRefExpr) { - if ("COLLATE".equalsIgnoreCase(lexer.stringVal())) { - lexer.nextToken(); - - if (lexer.token() != Token.IDENTIFIER) { - throw new ParserException("syntax error"); - } - - String collate = lexer.stringVal(); - lexer.nextToken(); - - expr.putAttribute("COLLATE", collate); - - return primaryRest(expr); - } - } else if (expr instanceof SQLIntegerExpr) { - SQLIntegerExpr intExpr = (SQLIntegerExpr) expr; - String binaryString = lexer.stringVal(); - if (intExpr.getNumber().intValue() == 0 && binaryString.startsWith("b")) { - lexer.nextToken(); - expr = new MySqlBinaryExpr(binaryString.substring(1)); - - return primaryRest(expr); - } - } - } - - if (lexer.token() == Token.LPAREN && expr instanceof SQLIdentifierExpr) { - SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr; - String ident = identExpr.getName(); - - if ("EXTRACT".equalsIgnoreCase(ident)) { - lexer.nextToken(); - - if (lexer.token() != Token.IDENTIFIER) { - throw new ParserException("syntax error"); - } - - String unitVal = lexer.stringVal(); - MySqlIntervalUnit unit = MySqlIntervalUnit.valueOf(unitVal); - lexer.nextToken(); - - accept(Token.FROM); - - SQLExpr value = expr(); - - MySqlExtractExpr extract = new MySqlExtractExpr(); - extract.setValue(value); - extract.setUnit(unit); - accept(Token.RPAREN); - - expr = extract; - - return primaryRest(expr); - } else if ("SUBSTRING".equalsIgnoreCase(ident)) { - lexer.nextToken(); - SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr(ident); - for (;;) { - SQLExpr param = expr(); - methodInvokeExpr.getParameters().add(param); - - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; - } else if (lexer.token() == Token.FROM) { - lexer.nextToken(); - SQLExpr from = expr(); - methodInvokeExpr.putAttribute("FROM", from); - - if (lexer.token() == Token.FOR) { - lexer.nextToken(); - SQLExpr forExpr = expr(); - methodInvokeExpr.putAttribute("FOR", forExpr); - } - break; - } else if (lexer.token() == Token.RPAREN) { - break; - } else { - throw new ParserException("syntax error"); - } - } - - accept(Token.RPAREN); - expr = methodInvokeExpr; - - return primaryRest(expr); - } else if ("TRIM".equalsIgnoreCase(ident)) { - lexer.nextToken(); - SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr(ident); - - if (lexer.token() == Token.IDENTIFIER) { - String flagVal = lexer.stringVal(); - if ("LEADING".equalsIgnoreCase(flagVal)) { - lexer.nextToken(); - methodInvokeExpr.getAttributes().put("TRIM_TYPE", "LEADING"); - } else if ("BOTH".equalsIgnoreCase(flagVal)) { - lexer.nextToken(); - methodInvokeExpr.getAttributes().put("TRIM_TYPE", "BOTH"); - } else if ("TRAILING".equalsIgnoreCase(flagVal)) { - lexer.nextToken(); - methodInvokeExpr.putAttribute("TRIM_TYPE", "TRAILING"); - } - } - - SQLExpr param = expr(); - methodInvokeExpr.getParameters().add(param); - - if (lexer.token() == Token.FROM) { - lexer.nextToken(); - SQLExpr from = expr(); - methodInvokeExpr.putAttribute("FROM", from); - } - - accept(Token.RPAREN); - expr = methodInvokeExpr; - - return primaryRest(expr); - } else if ("MATCH".equalsIgnoreCase(ident)) { - lexer.nextToken(); - MySqlMatchAgainstExpr matchAgainstExpr = new MySqlMatchAgainstExpr(); - - if (lexer.token() == Token.RPAREN) { - lexer.nextToken(); - } else { - exprList(matchAgainstExpr.getColumns()); - accept(Token.RPAREN); - } - - acceptIdentifier("AGAINST"); - - accept(Token.LPAREN); - SQLExpr against = primary(); - matchAgainstExpr.setAgainst(against); - - if (lexer.token() == Token.IN) { - lexer.nextToken(); - if (identifierEquals("NATURAL")) { - lexer.nextToken(); - acceptIdentifier("LANGUAGE"); - acceptIdentifier("MODE"); - if (identifierEquals("WITH")) { - lexer.nextToken(); - acceptIdentifier("QUERY"); - acceptIdentifier("EXPANSION"); - matchAgainstExpr.setSearchModifier(SearchModifier.IN_NATURAL_LANGUAGE_MODE_WITH_QUERY_EXPANSION); - } else { - matchAgainstExpr.setSearchModifier(SearchModifier.IN_NATURAL_LANGUAGE_MODE); - } - } else { - throw new ParserException("TODO"); - } - } else if (identifierEquals("WITH")) { - throw new ParserException("TODO"); - } - - accept(Token.RPAREN); - - expr = matchAgainstExpr; - - return primaryRest(expr); - } else if ("CONVERT".equalsIgnoreCase(ident)) { - lexer.nextToken(); - SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr(ident); - - if (lexer.token() != Token.RPAREN) { - exprList(methodInvokeExpr.getParameters()); - } - - if (identifierEquals("USING")) { - lexer.nextToken(); - if (lexer.token() != Token.IDENTIFIER) { - throw new ParserException("syntax error"); - } - String charset = lexer.stringVal(); - lexer.nextToken(); - methodInvokeExpr.putAttribute("USING", charset); - } - - accept(Token.RPAREN); - - expr = methodInvokeExpr; - - return primaryRest(expr); - } - } - - if (lexer.token() == Token.VARIANT && "@".equals(lexer.stringVal())) { - lexer.nextToken(); - MySqlUserName userName = new MySqlUserName(); - if (expr instanceof SQLCharExpr) { - userName.setUserName(((SQLCharExpr)expr).toString()); - } else { - userName.setUserName(((SQLIdentifierExpr)expr).getName()); - } - - if (lexer.token() == Token.LITERAL_CHARS) { - userName.setHost("'" + lexer.stringVal() + "'"); - } else { - userName.setHost(lexer.stringVal()); - } - lexer.nextToken(); - return userName; - } - - return super.primaryRest(expr); - } - - public SQLSelectParser createSelectParser() { - return new MySqlSelectParser(this); - } - - protected SQLExpr parseInterval() { - accept(Token.INTERVAL); - - if (lexer.token() == Token.LPAREN) { - lexer.nextToken(); - - SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr("INTERVAL"); - if (lexer.token() != Token.RPAREN) { - exprList(methodInvokeExpr.getParameters()); - } - - accept(Token.RPAREN); - - return primaryRest(methodInvokeExpr); - } else { - SQLExpr value = expr(); - - if (lexer.token() != Token.IDENTIFIER) { - throw new ParserException("Syntax error"); - } - - String unit = lexer.stringVal(); - lexer.nextToken(); - - MySqlIntervalExpr intervalExpr = new MySqlIntervalExpr(); - intervalExpr.setValue(value); - intervalExpr.setUnit(MySqlIntervalUnit.valueOf(unit.toUpperCase())); - - return intervalExpr; - } - } - - public SQLColumnDefinition parseColumn() { - MySqlSQLColumnDefinition column = new MySqlSQLColumnDefinition(); - column.setName(name()); - column.setDataType(parseDataType()); - - return parseColumnRest(column); - } - - public SQLColumnDefinition parseColumnRest(SQLColumnDefinition column) { - if (identifierEquals("AUTO_INCREMENT")) { - lexer.nextToken(); - if (column instanceof MySqlSQLColumnDefinition) { - ((MySqlSQLColumnDefinition) column).setAutoIncrement(true); - } - return parseColumnRest(column); - } - - if (identifierEquals("PARTITION")) { - throw new ParserException("syntax error " + lexer.token() + " " + lexer.stringVal()); - } - super.parseColumnRest(column); - - return column; - } - - public SQLExpr orRest(SQLExpr expr) { - - for (;;) { - if (lexer.token() == Token.OR || lexer.token() == Token.BARBAR) { - lexer.nextToken(); - SQLExpr rightExp = and(); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BooleanOr, rightExp); - } else if (lexer.token() == Token.XOR) { - lexer.nextToken(); - SQLExpr rightExp = and(); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BooleanXor, rightExp); - } else { - break; - } - } - - return expr; - } - - public SQLAssignItem parseAssignItem() { - SQLAssignItem item = new SQLAssignItem(); - - SQLExpr var = primary(); - - if (var instanceof SQLIdentifierExpr) { - String ident = ((SQLIdentifierExpr) var).getName(); - - if ("GLOBAL".equalsIgnoreCase(ident)) { - ident = lexer.stringVal(); - lexer.nextToken(); - var = new SQLVariantRefExpr(ident, true); - } else if ("SESSION".equalsIgnoreCase(ident)) { - ident = lexer.stringVal(); - lexer.nextToken(); - var = new SQLVariantRefExpr(ident, false); - } else { - var = new SQLVariantRefExpr(ident); - } - } - item.setTarget(var); - if (lexer.token() == Token.COLONEQ) { - lexer.nextToken(); - } else { - accept(Token.EQ); - } - item.setValue(expr()); - - return item; - } - - public SQLName nameRest(SQLName name) { - if (lexer.token() == Token.VARIANT && "@".equals(lexer.stringVal())) { - lexer.nextToken(); - MySqlUserName userName = new MySqlUserName(); - userName.setUserName(((SQLIdentifierExpr)name).getName()); - - if (lexer.token() == Token.LITERAL_CHARS) { - userName.setHost("'" + lexer.stringVal() + "'"); - } else { - userName.setHost(lexer.stringVal()); - } - lexer.nextToken(); - return userName; - } - return super.nameRest(name); - } - - public Limit parseLimit() { - if (lexer.token() == Token.LIMIT) { - lexer.nextToken(); - - MySqlSelectQueryBlock.Limit limit = new MySqlSelectQueryBlock.Limit(); - - SQLExpr temp = this.expr(); - if (lexer.token() == (Token.COMMA)) { - limit.setOffset(temp); - lexer.nextToken(); - limit.setRowCount(this.expr()); - } else if (identifierEquals("OFFSET")) { - limit.setRowCount(temp); - lexer.nextToken(); - limit.setOffset(this.expr()); - } else { - limit.setRowCount(temp); - } - return limit; - } - - return null; - } - - @Override - public SQLPrimaryKey parsePrimaryKey() { - accept(Token.PRIMARY); - accept(Token.KEY); - - MySqlPrimaryKey primaryKey = new MySqlPrimaryKey(); - - if (identifierEquals("USING")) { - lexer.nextToken(); - primaryKey.setIndexType(lexer.stringVal()); - lexer.nextToken(); - } - - accept(Token.LPAREN); - for (;;) { - primaryKey.getColumns().add(this.expr()); - if (!(lexer.token() == (Token.COMMA))) { - break; - } else { - lexer.nextToken(); - } - } - accept(Token.RPAREN); - - return primaryKey; - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlSelectParser.java b/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlSelectParser.java deleted file mode 100644 index 6f34731a..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlSelectParser.java +++ /dev/null @@ -1,331 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.parser; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLSetQuantifier; -import org.durid.sql.ast.expr.SQLLiteralExpr; -import org.durid.sql.ast.statement.SQLSelectGroupByClause; -import org.durid.sql.ast.statement.SQLSelectQuery; -import org.durid.sql.ast.statement.SQLSelectQueryBlock; -import org.durid.sql.ast.statement.SQLTableSource; -import org.durid.sql.ast.statement.SQLUnionQuery; -import org.durid.sql.dialect.mysql.ast.MySqlForceIndexHint; -import org.durid.sql.dialect.mysql.ast.MySqlIgnoreIndexHint; -import org.durid.sql.dialect.mysql.ast.MySqlIndexHint; -import org.durid.sql.dialect.mysql.ast.MySqlIndexHintImpl; -import org.durid.sql.dialect.mysql.ast.MySqlUseIndexHint; -import org.durid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectGroupBy; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit; -import org.durid.sql.dialect.mysql.ast.statement.MySqlUnionQuery; -import org.durid.sql.parser.ParserException; -import org.durid.sql.parser.SQLExprParser; -import org.durid.sql.parser.SQLSelectParser; -import org.durid.sql.parser.Token; - -public class MySqlSelectParser extends SQLSelectParser { - - public MySqlSelectParser(SQLExprParser exprParser){ - super(exprParser); - } - - public MySqlSelectParser(String sql) { - this(new MySqlExprParser(sql)); - } - - @Override - public SQLSelectQuery query() { - if (lexer.token() == (Token.LPAREN)) { - lexer.nextToken(); - - SQLSelectQuery select = query(); - accept(Token.RPAREN); - - return queryRest(select); - } - - MySqlSelectQueryBlock queryBlock = new MySqlSelectQueryBlock(); - - if (lexer.token() == Token.SELECT) { - lexer.nextToken(); - - if (lexer.token() == Token.HINT) { - this.exprParser.parseHints(queryBlock.getHints()); - } - - if (lexer.token() == (Token.DISTINCT)) { - queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT); - lexer.nextToken(); - } else if (identifierEquals("DISTINCTROW")) { - queryBlock.setDistionOption(SQLSetQuantifier.DISTINCTROW); - lexer.nextToken(); - } else if (lexer.token() == (Token.ALL)) { - queryBlock.setDistionOption(SQLSetQuantifier.ALL); - lexer.nextToken(); - } - - if (identifierEquals("HIGH_PRIORITY")) { - queryBlock.setHignPriority(true); - lexer.nextToken(); - } - - if (identifierEquals("STRAIGHT_JOIN")) { - queryBlock.setStraightJoin(true); - lexer.nextToken(); - } - - if (identifierEquals("SQL_SMALL_RESULT")) { - queryBlock.setSmallResult(true); - lexer.nextToken(); - } - - if (identifierEquals("SQL_BIG_RESULT")) { - queryBlock.setBigResult(true); - lexer.nextToken(); - } - - if (identifierEquals("SQL_BUFFER_RESULT")) { - queryBlock.setBufferResult(true); - lexer.nextToken(); - } - - if (identifierEquals("SQL_CACHE")) { - queryBlock.setCache(true); - lexer.nextToken(); - } - - if (identifierEquals("SQL_NO_CACHE")) { - queryBlock.setCache(false); - lexer.nextToken(); - } - - if (identifierEquals("SQL_CALC_FOUND_ROWS")) { - queryBlock.setCalcFoundRows(true); - lexer.nextToken(); - } - - parseSelectList(queryBlock); - - if (lexer.token() == (Token.INTO)) { - lexer.nextToken(); - - if (identifierEquals("OUTFILE")) { - lexer.nextToken(); - - MySqlOutFileExpr outFile = new MySqlOutFileExpr(); - outFile.setFile(expr()); - - queryBlock.setInto(outFile); - - if (identifierEquals("FIELDS") || identifierEquals("COLUMNS")) { - lexer.nextToken(); - - if (identifierEquals("TERMINATED")) { - lexer.nextToken(); - accept(Token.BY); - } - outFile.setColumnsTerminatedBy((SQLLiteralExpr) expr()); - - if (identifierEquals("OPTIONALLY")) { - lexer.nextToken(); - outFile.setColumnsEnclosedOptionally(true); - } - - if (identifierEquals("ENCLOSED")) { - lexer.nextToken(); - accept(Token.BY); - outFile.setColumnsEnclosedBy((SQLLiteralExpr) expr()); - } - - if (identifierEquals("ESCAPED")) { - lexer.nextToken(); - accept(Token.BY); - outFile.setColumnsEscaped((SQLLiteralExpr) expr()); - } - } - - if (identifierEquals("LINES")) { - lexer.nextToken(); - - if (identifierEquals("STARTING")) { - lexer.nextToken(); - accept(Token.BY); - outFile.setLinesStartingBy((SQLLiteralExpr) expr()); - } else { - identifierEquals("TERMINATED"); - lexer.nextToken(); - accept(Token.BY); - outFile.setLinesTerminatedBy((SQLLiteralExpr) expr()); - } - } - } else { - queryBlock.setInto(this.exprParser.name()); - } - } - } - - parseFrom(queryBlock); - - parseWhere(queryBlock); - - parseGroupBy(queryBlock); - - queryBlock.setOrderBy(this.exprParser.parseOrderBy()); - - if (lexer.token() == Token.LIMIT) { - queryBlock.setLimit(parseLimit()); - } - - if (identifierEquals("PROCEDURE")) { - lexer.nextToken(); - throw new ParserException("TODO"); - } - - if (lexer.token() == Token.INTO) { - lexer.nextToken(); - SQLExpr expr = this.exprParser.name(); - queryBlock.setInto(expr); - } - - if (lexer.token() == Token.FOR) { - lexer.nextToken(); - accept(Token.UPDATE); - - queryBlock.setForUpdate(true); - } - - if (lexer.token() == Token.LOCK) { - lexer.nextToken(); - accept(Token.IN); - acceptIdentifier("SHARE"); - acceptIdentifier("MODE"); - queryBlock.setLockInShareMode(true); - } - - return queryRest(queryBlock); - } - - protected void parseGroupBy(SQLSelectQueryBlock queryBlock) { - if (lexer.token() == (Token.GROUP)) { - lexer.nextToken(); - accept(Token.BY); - - SQLSelectGroupByClause groupBy = new SQLSelectGroupByClause(); - while (true) { - groupBy.getItems().add(this.exprParser.expr()); - if (!(lexer.token() == (Token.COMMA))) { - break; - } - lexer.nextToken(); - } - - if (identifierEquals("WITH")) { - lexer.nextToken(); - acceptIdentifier("ROLLUP"); - - MySqlSelectGroupBy mySqlGroupBy = new MySqlSelectGroupBy(); - mySqlGroupBy.getItems().addAll(groupBy.getItems()); - mySqlGroupBy.setRollUp(true); - - groupBy = mySqlGroupBy; - } - - if (lexer.token() == Token.HAVING) { - lexer.nextToken(); - - groupBy.setHaving(this.exprParser.expr()); - } - - queryBlock.setGroupBy(groupBy); - } - } - - protected SQLTableSource parseTableSourceRest(SQLTableSource tableSource) { - if (identifierEquals("USING")) { - return tableSource; - } - - if (identifierEquals("USE")) { - lexer.nextToken(); - MySqlUseIndexHint hint = new MySqlUseIndexHint(); - parseIndexHint(hint); - tableSource.getHints().add(hint); - } - - if (identifierEquals("IGNORE")) { - lexer.nextToken(); - MySqlIgnoreIndexHint hint = new MySqlIgnoreIndexHint(); - parseIndexHint(hint); - tableSource.getHints().add(hint); - } - - if (identifierEquals("FORCE")) { - lexer.nextToken(); - MySqlForceIndexHint hint = new MySqlForceIndexHint(); - parseIndexHint(hint); - tableSource.getHints().add(hint); - } - - return super.parseTableSourceRest(tableSource); - } - - private void parseIndexHint(MySqlIndexHintImpl hint) { - if (lexer.token() == Token.INDEX) { - lexer.nextToken(); - } else { - accept(Token.KEY); - } - - if (lexer.token() == Token.FOR) { - lexer.nextToken(); - - if (lexer.token() == Token.JOIN) { - lexer.nextToken(); - hint.setOption(MySqlIndexHint.Option.JOIN); - } else if (lexer.token() == Token.ORDER) { - lexer.nextToken(); - accept(Token.BY); - hint.setOption(MySqlIndexHint.Option.ORDER_BY); - } else { - accept(Token.GROUP); - accept(Token.BY); - hint.setOption(MySqlIndexHint.Option.GROUP_BY); - } - } - - accept(Token.LPAREN); - this.exprParser.names(hint.getIndexList()); - accept(Token.RPAREN); - } - - protected MySqlUnionQuery createSQLUnionQuery() { - return new MySqlUnionQuery(); - } - - public SQLUnionQuery unionRest(SQLUnionQuery union) { - if (lexer.token() == Token.LIMIT) { - MySqlUnionQuery mysqlUnionQuery = (MySqlUnionQuery) union; - mysqlUnionQuery.setLimit(parseLimit()); - } - return super.unionRest(union); - } - - public Limit parseLimit() { - return ((MySqlExprParser)this.exprParser) .parseLimit(); - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlStatementParser.java b/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlStatementParser.java deleted file mode 100644 index 34363b25..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlStatementParser.java +++ /dev/null @@ -1,2364 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.parser; - -import java.util.List; - -import org.durid.sql.ast.SQLCommentHint; -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLOrderBy; -import org.durid.sql.ast.SQLStatement; -import org.durid.sql.ast.expr.SQLCharExpr; -import org.durid.sql.ast.expr.SQLLiteralExpr; -import org.durid.sql.ast.expr.SQLQueryExpr; -import org.durid.sql.ast.expr.SQLVariantRefExpr; -import org.durid.sql.ast.statement.SQLAlterTableAddPrimaryKey; -import org.durid.sql.ast.statement.SQLAlterTableDropColumnItem; -import org.durid.sql.ast.statement.SQLAlterTableDropIndex; -import org.durid.sql.ast.statement.SQLColumnDefinition; -import org.durid.sql.ast.statement.SQLCreateDatabaseStatement; -import org.durid.sql.ast.statement.SQLCreateTableStatement; -import org.durid.sql.ast.statement.SQLDropTableStatement; -import org.durid.sql.ast.statement.SQLDropViewStatement; -import org.durid.sql.ast.statement.SQLInsertStatement; -import org.durid.sql.ast.statement.SQLPrimaryKey; -import org.durid.sql.ast.statement.SQLSelect; -import org.durid.sql.ast.statement.SQLSelectOrderByItem; -import org.durid.sql.ast.statement.SQLSelectStatement; -import org.durid.sql.ast.statement.SQLSetStatement; -import org.durid.sql.ast.statement.SQLTableSource; -import org.durid.sql.ast.statement.SQLUpdateSetItem; -import org.durid.sql.ast.statement.SQLUpdateStatement; -import org.durid.sql.dialect.mysql.ast.statement.CobarShowStatus; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddColumn; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddIndex; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddUnique; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableCharacter; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableOption; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlBinlogStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlCommitStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlCreateIndexStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlDescribeStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlDropTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlDropUser; -import org.durid.sql.dialect.mysql.ast.statement.MySqlDropViewStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlExecuteStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlHelpStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlInsertStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlKillStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlLoadDataInFileStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlLoadXmlStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement.LockType; -import org.durid.sql.dialect.mysql.ast.statement.MySqlPrepareStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlRenameTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlReplaceStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlResetStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlRollbackStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSetCharSetStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSetNamesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSetTransactionIsolationLevelStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowAuthorsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowBinLogEventsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowBinaryLogsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCharacterSetStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCollationStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowColumnsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowContributorsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateDatabaseStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateEventStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateFunctionStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateProcedureStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateTriggerStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateViewStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowDatabasesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowEngineStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowEnginesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowErrorsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowEventsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowFunctionCodeStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowFunctionStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowGrantsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowIndexesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowKeysStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowMasterStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowOpenTablesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowPluginsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowPrivilegesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowProcedureCodeStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowProcedureStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowProcessListStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowProfileStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowProfilesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowRelayLogEventsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowSlaveHostsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowSlaveStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowTableStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowTriggersStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowVariantsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlStartTransactionStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlUnlockTablesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement; -import org.durid.sql.parser.Lexer; -import org.durid.sql.parser.ParserException; -import org.durid.sql.parser.SQLSelectParser; -import org.durid.sql.parser.SQLStatementParser; -import org.durid.sql.parser.Token; - -public class MySqlStatementParser extends SQLStatementParser { - - private static final String COLLATE2 = "COLLATE"; - private static final String CASCADE = "CASCADE"; - private static final String RESTRICT = "RESTRICT"; - private static final String CHAIN = "CHAIN"; - private static final String ENGINES = "ENGINES"; - private static final String ENGINE = "ENGINE"; - private static final String BINLOG = "BINLOG"; - private static final String EVENTS = "EVENTS"; - private static final String CHARACTER = "CHARACTER"; - private static final String SESSION = "SESSION"; - private static final String GLOBAL = "GLOBAL"; - private static final String VARIABLES = "VARIABLES"; - private static final String ERRORS = "ERRORS"; - private static final String STATUS = "STATUS"; - private static final String IGNORE = "IGNORE"; - private static final String RESET = "RESET"; - private static final String DESCRIBE = "DESCRIBE"; - private static final String DESC = "DESC"; - private static final String WRITE = "WRITE"; - private static final String READ = "READ"; - private static final String LOCAL = "LOCAL"; - private static final String TABLES = "TABLES"; - private static final String TEMPORARY = "TEMPORARY"; - private static final String USER = "USER"; - private static final String SPATIAL = "SPATIAL"; - private static final String FULLTEXT = "FULLTEXT"; - private static final String REPLACE = "REPLACE"; - private static final String DELAYED = "DELAYED"; - private static final String LOW_PRIORITY = "LOW_PRIORITY"; - - public MySqlStatementParser(String sql){ - super(new MySqlExprParser(sql)); - } - - public MySqlStatementParser(Lexer lexer){ - super(new MySqlExprParser(lexer)); - } - - public SQLCreateTableStatement parseCreateTable() { - MySqlCreateTableParser parser = new MySqlCreateTableParser(this.exprParser); - return parser.parseCrateTable(); - } - - public SQLSelectStatement parseSelect() { - return new SQLSelectStatement(new MySqlSelectParser(this.exprParser).select()); - } - - public SQLUpdateStatement parseUpdateStatement() { - MySqlUpdateStatement stmt = createUpdateStatement(); - - if (lexer.token() == Token.UPDATE) { - lexer.nextToken(); - - if (identifierEquals(LOW_PRIORITY)) { - lexer.nextToken(); - stmt.setLowPriority(true); - } - - if (identifierEquals(IGNORE)) { - lexer.nextToken(); - stmt.setIgnore(true); - } - - SQLTableSource tableSource = this.exprParser.createSelectParser().parseTableSource(); - stmt.setTableSource(tableSource); - } - - accept(Token.SET); - - for (;;) { - SQLUpdateSetItem item = new SQLUpdateSetItem(); - item.setColumn(this.exprParser.name()); - if (lexer.token() == Token.EQ) { - lexer.nextToken(); - } else { - accept(Token.COLONEQ); - } - item.setValue(this.exprParser.expr()); - - stmt.getItems().add(item); - - if (lexer.token() == (Token.COMMA)) { - lexer.nextToken(); - continue; - } - - break; - } - - if (lexer.token() == (Token.WHERE)) { - lexer.nextToken(); - stmt.setWhere(this.exprParser.expr()); - } - - stmt.setOrderBy(this.exprParser.parseOrderBy()); - - stmt.setLimit(parseLimit()); - - return stmt; - } - - protected MySqlUpdateStatement createUpdateStatement() { - return new MySqlUpdateStatement(); - } - - public MySqlDeleteStatement parseDeleteStatement() { - MySqlDeleteStatement deleteStatement = new MySqlDeleteStatement(); - - if (lexer.token() == Token.DELETE) { - lexer.nextToken(); - - if (identifierEquals(LOW_PRIORITY)) { - deleteStatement.setLowPriority(true); - lexer.nextToken(); - } - - if (identifierEquals("QUICK")) { - deleteStatement.setQuick(true); - lexer.nextToken(); - } - - if (identifierEquals(IGNORE)) { - deleteStatement.setIgnore(true); - lexer.nextToken(); - } - - if (lexer.token() == Token.IDENTIFIER) { - deleteStatement.setTableSource(createSQLSelectParser().parseTableSource()); - - if (lexer.token() == Token.FROM) { - lexer.nextToken(); - SQLTableSource tableSource = createSQLSelectParser().parseTableSource(); - deleteStatement.setFrom(tableSource); - } - } else { - if (lexer.token() == Token.FROM) { - lexer.nextToken(); - deleteStatement.setTableSource(createSQLSelectParser().parseTableSource()); - } - } - - if (identifierEquals("USING")) { - lexer.nextToken(); - - SQLTableSource tableSource = createSQLSelectParser().parseTableSource(); - deleteStatement.setUsing(tableSource); - } - } - - if (lexer.token() == (Token.WHERE)) { - lexer.nextToken(); - SQLExpr where = this.exprParser.expr(); - deleteStatement.setWhere(where); - } - - if (lexer.token() == (Token.ORDER)) { - SQLOrderBy orderBy = exprParser.parseOrderBy(); - deleteStatement.setOrderBy(orderBy); - } - - deleteStatement.setLimit(parseLimit()); - - return deleteStatement; - } - - public SQLStatement parseCreate() { - accept(Token.CREATE); - - List hints = this.exprParser.parseHints(); - - if (lexer.token() == Token.TABLE || identifierEquals(TEMPORARY)) { - MySqlCreateTableParser parser = new MySqlCreateTableParser(this.exprParser); - MySqlCreateTableStatement stmt = parser.parseCrateTable(false); - stmt.setHints(hints); - return stmt; - } - - if (lexer.token() == Token.DATABASE) { - return parseCreateDatabase(); - } - - if (lexer.token() == Token.UNIQUE || lexer.token() == Token.INDEX || identifierEquals(FULLTEXT) - || identifierEquals(SPATIAL)) { - return parseCreateIndex(); - } - - if (identifierEquals(USER)) { - return parseCreateUser(); - } - - throw new ParserException("TODO " + lexer.token()); - } - - public SQLStatement parseCreateIndex() { - MySqlCreateIndexStatement stmt = new MySqlCreateIndexStatement(); - - if (lexer.token() == Token.UNIQUE) { - stmt.setType("UNIQUE"); - lexer.nextToken(); - } else if (identifierEquals(FULLTEXT)) { - stmt.setType(FULLTEXT); - lexer.nextToken(); - } else if (identifierEquals(SPATIAL)) { - stmt.setType(SPATIAL); - lexer.nextToken(); - } - - accept(Token.INDEX); - - stmt.setName(this.exprParser.name()); - - parseCreateIndexUsing(stmt); - - accept(Token.ON); - - stmt.setTable(this.exprParser.name()); - - accept(Token.LPAREN); - - for (;;) { - SQLSelectOrderByItem item = this.exprParser.parseSelectOrderByItem(); - stmt.getItems().add(item); - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; - } - break; - } - accept(Token.RPAREN); - - parseCreateIndexUsing(stmt); - - return stmt; - } - - private void parseCreateIndexUsing(MySqlCreateIndexStatement stmt) { - if (identifierEquals("USING")) { - lexer.nextToken(); - - if (identifierEquals("BTREE")) { - stmt.setUsing("BTREE"); - lexer.nextToken(); - } else if (identifierEquals("HASH")) { - stmt.setUsing("HASH"); - lexer.nextToken(); - } else { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } - } - } - - public SQLStatement parseCreateUser() { - if (lexer.token() == Token.CREATE) { - lexer.nextToken(); - } - - acceptIdentifier(USER); - - MySqlCreateUserStatement stmt = new MySqlCreateUserStatement(); - - for (;;) { - MySqlCreateUserStatement.UserSpecification userSpec = new MySqlCreateUserStatement.UserSpecification(); - - SQLExpr expr = exprParser.primary(); - userSpec.setUser(expr); - - if (lexer.token() == Token.IDENTIFIED) { - lexer.nextToken(); - if (lexer.token() == Token.BY) { - lexer.nextToken(); - - if (identifierEquals("PASSWORD")) { - lexer.nextToken(); - } - - SQLCharExpr password = (SQLCharExpr) this.exprParser.expr(); - userSpec.setPassword(password); - } else if (lexer.token() == Token.WITH) { - lexer.nextToken(); - - SQLCharExpr text = (SQLCharExpr) this.exprParser.expr(); - userSpec.setAuthPlugin(text); - } - } - - stmt.getUsers().add(userSpec); - - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; - } - - break; - } - - return stmt; - } - - public SQLStatement parseKill() { - accept(Token.KILL); - - MySqlKillStatement stmt = new MySqlKillStatement(); - - if (identifierEquals("CONNECTION")) { - stmt.setType(MySqlKillStatement.Type.CONNECTION); - lexer.nextToken(); - } else if (identifierEquals("QUERY")) { - stmt.setType(MySqlKillStatement.Type.QUERY); - lexer.nextToken(); - } else { - throw new ParserException("not support kill type " + lexer.token()); - } - - SQLExpr threadId = this.exprParser.expr(); - stmt.setThreadId(threadId); - - return stmt; - } - - public SQLStatement parseBinlog() { - acceptIdentifier("binlog"); - - MySqlBinlogStatement stmt = new MySqlBinlogStatement(); - - SQLExpr expr = this.exprParser.expr(); - stmt.setExpr(expr); - - return stmt; - } - - public SQLStatement parseReset() { - acceptIdentifier(RESET); - - MySqlResetStatement stmt = new MySqlResetStatement(); - - for (;;) { - if (lexer.token() == Token.IDENTIFIER) { - if (identifierEquals("QUERY")) { - lexer.nextToken(); - acceptIdentifier("CACHE"); - stmt.getOptions().add("QUERY CACHE"); - } else { - stmt.getOptions().add(lexer.stringVal()); - lexer.nextToken(); - } - - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; - } - } - break; - } - - return stmt; - } - - public boolean parseStatementListDialect(List statementList) { - if (lexer.token() == Token.KILL) { - SQLStatement stmt = parseKill(); - statementList.add(stmt); - return true; - } - - if (identifierEquals("PREPARE")) { - MySqlPrepareStatement stmt = parsePrepare(); - statementList.add(stmt); - return true; - } - - if (identifierEquals("EXECUTE")) { - MySqlExecuteStatement stmt = parseExecute(); - statementList.add(stmt); - return true; - } - - if (identifierEquals("LOAD")) { - SQLStatement stmt = parseLoad(); - statementList.add(stmt); - return true; - } - - if (identifierEquals(REPLACE)) { - MySqlReplaceStatement stmt = parseReplicate(); - statementList.add(stmt); - return true; - } - - if (identifierEquals("START")) { - MySqlStartTransactionStatement stmt = parseStart(); - statementList.add(stmt); - return true; - } - - if (identifierEquals("SHOW")) { - SQLStatement stmt = parseShow(); - statementList.add(stmt); - return true; - } - - if (identifierEquals(BINLOG)) { - SQLStatement stmt = parseBinlog(); - statementList.add(stmt); - return true; - } - - if (identifierEquals(RESET)) { - SQLStatement stmt = parseReset(); - statementList.add(stmt); - return true; - } - - if (identifierEquals("HELP")) { - lexer.nextToken(); - MySqlHelpStatement stmt = new MySqlHelpStatement(); - stmt.setContent(this.exprParser.primary()); - statementList.add(stmt); - return true; - } - - if (identifierEquals(DESC) || identifierEquals(DESCRIBE)) { - SQLStatement stmt = parseDescribe(); - statementList.add(stmt); - return true; - } - - if (lexer.token() == Token.LOCK) { - lexer.nextToken(); - acceptIdentifier(TABLES); - - MySqlLockTableStatement stmt = new MySqlLockTableStatement(); - stmt.setTableSource(this.exprParser.name()); - - if (identifierEquals(READ)) { - lexer.nextToken(); - if (identifierEquals(LOCAL)) { - lexer.nextToken(); - stmt.setLockType(LockType.READ_LOCAL); - } else { - stmt.setLockType(LockType.READ); - } - } else if (identifierEquals(WRITE)) { - stmt.setLockType(LockType.WRITE); - } else if (identifierEquals(LOW_PRIORITY)) { - lexer.nextToken(); - acceptIdentifier(WRITE); - stmt.setLockType(LockType.LOW_PRIORITY_WRITE); - } - - statementList.add(stmt); - return true; - } - - if (identifierEquals("UNLOCK")) { - lexer.nextToken(); - acceptIdentifier(TABLES); - statementList.add(new MySqlUnlockTablesStatement()); - return true; - } - - return false; - } - - public MySqlDescribeStatement parseDescribe() { - if (lexer.token() == Token.DESC || identifierEquals(DESCRIBE)) { - lexer.nextToken(); - } else { - throw new ParserException("expect DESC, actual " + lexer.token()); - } - - MySqlDescribeStatement stmt = new MySqlDescribeStatement(); - stmt.setObject(this.exprParser.name()); - - return stmt; - } - - public SQLStatement parseShow() { - acceptIdentifier("SHOW"); - - if (lexer.token() == Token.FULL) { - lexer.nextToken(); - - if (identifierEquals("PROCESSLIST")) { - lexer.nextToken(); - MySqlShowProcessListStatement stmt = new MySqlShowProcessListStatement(); - stmt.setFull(true); - return stmt; - } - - acceptIdentifier("COLUMNS"); - - MySqlShowColumnsStatement stmt = parseShowColumns(); - stmt.setFull(true); - - return stmt; - } - - if (identifierEquals("COLUMNS")) { - lexer.nextToken(); - - MySqlShowColumnsStatement stmt = parseShowColumns(); - - return stmt; - } - - if (identifierEquals(TABLES)) { - lexer.nextToken(); - - MySqlShowTablesStatement stmt = parseShowTabless(); - - return stmt; - } - - if (identifierEquals("DATABASES")) { - lexer.nextToken(); - - MySqlShowDatabasesStatement stmt = parseShowDatabases(); - - return stmt; - } - - if (identifierEquals("WARNINGS")) { - lexer.nextToken(); - - MySqlShowWarningsStatement stmt = parseShowWarnings(); - - return stmt; - } - - if (identifierEquals("COUNT")) { - lexer.nextToken(); - accept(Token.LPAREN); - accept(Token.STAR); - accept(Token.RPAREN); - - if (identifierEquals(ERRORS)) { - lexer.nextToken(); - - MySqlShowErrorsStatement stmt = new MySqlShowErrorsStatement(); - stmt.setCount(true); - - return stmt; - } else { - acceptIdentifier("WARNINGS"); - - MySqlShowWarningsStatement stmt = new MySqlShowWarningsStatement(); - stmt.setCount(true); - - return stmt; - } - } - - if (identifierEquals(ERRORS)) { - lexer.nextToken(); - - MySqlShowErrorsStatement stmt = new MySqlShowErrorsStatement(); - stmt.setLimit(parseLimit()); - - return stmt; - } - - if (identifierEquals(STATUS)) { - lexer.nextToken(); - - MySqlShowStatusStatement stmt = parseShowStatus(); - - return stmt; - } - - if (identifierEquals(VARIABLES)) { - lexer.nextToken(); - - MySqlShowVariantsStatement stmt = parseShowVariants(); - - return stmt; - } - - if (identifierEquals(GLOBAL)) { - lexer.nextToken(); - - if (identifierEquals(STATUS)) { - lexer.nextToken(); - MySqlShowStatusStatement stmt = parseShowStatus(); - stmt.setGlobal(true); - return stmt; - } - - if (identifierEquals(VARIABLES)) { - lexer.nextToken(); - MySqlShowVariantsStatement stmt = parseShowVariants(); - stmt.setGlobal(true); - return stmt; - } - } - - if (identifierEquals(SESSION)) { - lexer.nextToken(); - - if (identifierEquals(STATUS)) { - lexer.nextToken(); - MySqlShowStatusStatement stmt = parseShowStatus(); - stmt.setSession(true); - return stmt; - } - - if (identifierEquals(VARIABLES)) { - lexer.nextToken(); - MySqlShowVariantsStatement stmt = parseShowVariants(); - stmt.setSession(true); - return stmt; - } - } - - if (identifierEquals("COBAR_STATUS")) { - lexer.nextToken(); - return new CobarShowStatus(); - } - - if (identifierEquals("AUTHORS")) { - lexer.nextToken(); - return new MySqlShowAuthorsStatement(); - } - - if (identifierEquals("BINARY")) { - lexer.nextToken(); - acceptIdentifier("LOGS"); - return new MySqlShowBinaryLogsStatement(); - } - - if (identifierEquals("MASTER")) { - lexer.nextToken(); - if (identifierEquals("LOGS")) { - lexer.nextToken(); - return new MySqlShowMasterLogsStatement(); - } - acceptIdentifier(STATUS); - return new MySqlShowMasterStatusStatement(); - } - - if (identifierEquals(CHARACTER)) { - lexer.nextToken(); - accept(Token.SET); - MySqlShowCharacterSetStatement stmt = new MySqlShowCharacterSetStatement(); - - if (lexer.token() == Token.LIKE) { - lexer.nextToken(); - stmt.setPattern(this.exprParser.expr()); - } - - if (lexer.token() == Token.WHERE) { - lexer.nextToken(); - stmt.setWhere(this.exprParser.expr()); - } - - return stmt; - } - - if (identifierEquals("COLLATION")) { - lexer.nextToken(); - MySqlShowCollationStatement stmt = new MySqlShowCollationStatement(); - - if (lexer.token() == Token.LIKE) { - lexer.nextToken(); - stmt.setPattern(this.exprParser.expr()); - } - - if (lexer.token() == Token.WHERE) { - lexer.nextToken(); - stmt.setWhere(this.exprParser.expr()); - } - - return stmt; - } - - if (identifierEquals(BINLOG)) { - lexer.nextToken(); - acceptIdentifier(EVENTS); - MySqlShowBinLogEventsStatement stmt = new MySqlShowBinLogEventsStatement(); - - if (lexer.token() == Token.IN) { - lexer.nextToken(); - stmt.setIn(this.exprParser.expr()); - } - - if (lexer.token() == Token.FROM) { - lexer.nextToken(); - stmt.setFrom(this.exprParser.expr()); - } - - stmt.setLimit(parseLimit()); - - return stmt; - } - - if (identifierEquals("CONTRIBUTORS")) { - lexer.nextToken(); - return new MySqlShowContributorsStatement(); - } - - if (lexer.token() == Token.CREATE) { - lexer.nextToken(); - - if (lexer.token() == Token.DATABASE) { - lexer.nextToken(); - - MySqlShowCreateDatabaseStatement stmt = new MySqlShowCreateDatabaseStatement(); - stmt.setDatabase(this.exprParser.name()); - return stmt; - } - - if (identifierEquals("EVENT")) { - lexer.nextToken(); - - MySqlShowCreateEventStatement stmt = new MySqlShowCreateEventStatement(); - stmt.setEventName(this.exprParser.name()); - return stmt; - } - - if (identifierEquals("FUNCTION")) { - lexer.nextToken(); - - MySqlShowCreateFunctionStatement stmt = new MySqlShowCreateFunctionStatement(); - stmt.setName(this.exprParser.name()); - return stmt; - } - - if (identifierEquals("PROCEDURE")) { - lexer.nextToken(); - - MySqlShowCreateProcedureStatement stmt = new MySqlShowCreateProcedureStatement(); - stmt.setName(this.exprParser.name()); - return stmt; - } - - if (lexer.token() == Token.TABLE) { - lexer.nextToken(); - - MySqlShowCreateTableStatement stmt = new MySqlShowCreateTableStatement(); - stmt.setName(this.exprParser.name()); - return stmt; - } - - if (lexer.token() == Token.VIEW) { - lexer.nextToken(); - - MySqlShowCreateViewStatement stmt = new MySqlShowCreateViewStatement(); - stmt.setName(this.exprParser.name()); - return stmt; - } - - if (identifierEquals("TRIGGER")) { - lexer.nextToken(); - - MySqlShowCreateTriggerStatement stmt = new MySqlShowCreateTriggerStatement(); - stmt.setName(this.exprParser.name()); - return stmt; - } - - throw new ParserException("TODO " + lexer.stringVal()); - } - - if (identifierEquals(ENGINE)) { - lexer.nextToken(); - MySqlShowEngineStatement stmt = new MySqlShowEngineStatement(); - stmt.setName(this.exprParser.name()); - stmt.setOption(MySqlShowEngineStatement.Option.valueOf(lexer.stringVal().toUpperCase())); - lexer.nextToken(); - return stmt; - } - - if (identifierEquals("STORAGE")) { - lexer.nextToken(); - acceptIdentifier(ENGINES); - MySqlShowEnginesStatement stmt = new MySqlShowEnginesStatement(); - stmt.setStorage(true); - return stmt; - } - - if (identifierEquals(ENGINES)) { - lexer.nextToken(); - MySqlShowEnginesStatement stmt = new MySqlShowEnginesStatement(); - return stmt; - } - - if (identifierEquals(EVENTS)) { - lexer.nextToken(); - MySqlShowEventsStatement stmt = new MySqlShowEventsStatement(); - - if (lexer.token() == Token.FROM || lexer.token() == Token.IN) { - lexer.nextToken(); - stmt.setSchema(this.exprParser.name()); - } - - if (lexer.token() == Token.LIKE) { - lexer.nextToken(); - stmt.setLike(this.exprParser.expr()); - } - - if (lexer.token() == Token.WHERE) { - lexer.nextToken(); - stmt.setWhere(this.exprParser.expr()); - } - return stmt; - } - - if (identifierEquals("FUNCTION")) { - lexer.nextToken(); - - if (identifierEquals("CODE")) { - lexer.nextToken(); - MySqlShowFunctionCodeStatement stmt = new MySqlShowFunctionCodeStatement(); - stmt.setName(this.exprParser.name()); - return stmt; - } - - acceptIdentifier(STATUS); - MySqlShowFunctionStatusStatement stmt = new MySqlShowFunctionStatusStatement(); - - if (lexer.token() == Token.LIKE) { - lexer.nextToken(); - stmt.setLike(this.exprParser.expr()); - } - - if (lexer.token() == Token.WHERE) { - lexer.nextToken(); - stmt.setWhere(this.exprParser.expr()); - } - return stmt; - } - - // MySqlShowFunctionStatusStatement - - if (identifierEquals(ENGINE)) { - lexer.nextToken(); - MySqlShowEngineStatement stmt = new MySqlShowEngineStatement(); - stmt.setName(this.exprParser.name()); - stmt.setOption(MySqlShowEngineStatement.Option.valueOf(lexer.stringVal().toUpperCase())); - lexer.nextToken(); - return stmt; - } - - if (identifierEquals("STORAGE")) { - lexer.nextToken(); - acceptIdentifier(ENGINES); - MySqlShowEnginesStatement stmt = new MySqlShowEnginesStatement(); - stmt.setStorage(true); - return stmt; - } - - if (identifierEquals(ENGINES)) { - lexer.nextToken(); - MySqlShowEnginesStatement stmt = new MySqlShowEnginesStatement(); - return stmt; - } - - if (identifierEquals("GRANTS")) { - lexer.nextToken(); - MySqlShowGrantsStatement stmt = new MySqlShowGrantsStatement(); - - if (lexer.token() == Token.FOR) { - lexer.nextToken(); - stmt.setUser(this.exprParser.expr()); - } - - return stmt; - } - - if (lexer.token() == Token.INDEX || identifierEquals("INDEXES")) { - lexer.nextToken(); - MySqlShowIndexesStatement stmt = new MySqlShowIndexesStatement(); - - if (lexer.token() == Token.FROM || lexer.token() == Token.IN) { - lexer.nextToken(); - SQLName table = exprParser.name(); - stmt.setTable(table); - - if (lexer.token() == Token.FROM || lexer.token() == Token.IN) { - lexer.nextToken(); - SQLName database = exprParser.name(); - stmt.setDatabase(database); - } - } - - return stmt; - } - - if (identifierEquals("KEYS")) { - lexer.nextToken(); - MySqlShowKeysStatement stmt = new MySqlShowKeysStatement(); - - if (lexer.token() == Token.FROM || lexer.token() == Token.IN) { - lexer.nextToken(); - SQLName table = exprParser.name(); - stmt.setTable(table); - - if (lexer.token() == Token.FROM || lexer.token() == Token.IN) { - lexer.nextToken(); - SQLName database = exprParser.name(); - stmt.setDatabase(database); - } - } - - return stmt; - } - - if (identifierEquals("OPEN")) { - lexer.nextToken(); - acceptIdentifier(TABLES); - MySqlShowOpenTablesStatement stmt = new MySqlShowOpenTablesStatement(); - - if (lexer.token() == Token.FROM || lexer.token() == Token.IN) { - lexer.nextToken(); - stmt.setDatabase(this.exprParser.name()); - } - - if (lexer.token() == Token.LIKE) { - lexer.nextToken(); - stmt.setLike(this.exprParser.expr()); - } - - if (lexer.token() == Token.WHERE) { - lexer.nextToken(); - stmt.setWhere(this.exprParser.expr()); - } - return stmt; - } - - if (identifierEquals("PLUGINS")) { - lexer.nextToken(); - MySqlShowPluginsStatement stmt = new MySqlShowPluginsStatement(); - return stmt; - } - - if (identifierEquals("PRIVILEGES")) { - lexer.nextToken(); - MySqlShowPrivilegesStatement stmt = new MySqlShowPrivilegesStatement(); - return stmt; - } - - if (identifierEquals("PROCEDURE")) { - lexer.nextToken(); - - if (identifierEquals("CODE")) { - lexer.nextToken(); - MySqlShowProcedureCodeStatement stmt = new MySqlShowProcedureCodeStatement(); - stmt.setName(this.exprParser.name()); - return stmt; - } - - acceptIdentifier(STATUS); - MySqlShowProcedureStatusStatement stmt = new MySqlShowProcedureStatusStatement(); - - if (lexer.token() == Token.LIKE) { - lexer.nextToken(); - stmt.setLike(this.exprParser.expr()); - } - - if (lexer.token() == Token.WHERE) { - lexer.nextToken(); - stmt.setWhere(this.exprParser.expr()); - } - return stmt; - } - - if (identifierEquals("PROCESSLIST")) { - lexer.nextToken(); - MySqlShowProcessListStatement stmt = new MySqlShowProcessListStatement(); - return stmt; - } - - if (identifierEquals("PROFILES")) { - lexer.nextToken(); - MySqlShowProfilesStatement stmt = new MySqlShowProfilesStatement(); - return stmt; - } - - if (identifierEquals("PROFILE")) { - lexer.nextToken(); - MySqlShowProfileStatement stmt = new MySqlShowProfileStatement(); - - for (;;) { - if (lexer.token() == Token.ALL) { - stmt.getTypes().add(MySqlShowProfileStatement.Type.ALL); - lexer.nextToken(); - } else if (identifierEquals("BLOCK")) { - lexer.nextToken(); - acceptIdentifier("IO"); - stmt.getTypes().add(MySqlShowProfileStatement.Type.BLOCK_IO); - } else if (identifierEquals("CONTEXT")) { - lexer.nextToken(); - acceptIdentifier("SWITCHES"); - stmt.getTypes().add(MySqlShowProfileStatement.Type.CONTEXT_SWITCHES); - } else if (identifierEquals("CPU")) { - lexer.nextToken(); - stmt.getTypes().add(MySqlShowProfileStatement.Type.CPU); - } else if (identifierEquals("IPC")) { - lexer.nextToken(); - stmt.getTypes().add(MySqlShowProfileStatement.Type.IPC); - } else if (identifierEquals("MEMORY")) { - lexer.nextToken(); - stmt.getTypes().add(MySqlShowProfileStatement.Type.MEMORY); - } else if (identifierEquals("PAGE")) { - lexer.nextToken(); - acceptIdentifier("FAULTS"); - stmt.getTypes().add(MySqlShowProfileStatement.Type.PAGE_FAULTS); - } else if (identifierEquals("SOURCE")) { - lexer.nextToken(); - stmt.getTypes().add(MySqlShowProfileStatement.Type.SOURCE); - } else if (identifierEquals("SWAPS")) { - lexer.nextToken(); - stmt.getTypes().add(MySqlShowProfileStatement.Type.SWAPS); - } else { - break; - } - - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; - } - break; - } - - if (lexer.token() == Token.FOR) { - lexer.nextToken(); - acceptIdentifier("QUERY"); - stmt.setForQuery(this.exprParser.primary()); - } - - stmt.setLimit(this.parseLimit()); - - return stmt; - } - - if (identifierEquals("RELAYLOG")) { - lexer.nextToken(); - acceptIdentifier(EVENTS); - MySqlShowRelayLogEventsStatement stmt = new MySqlShowRelayLogEventsStatement(); - - if (lexer.token() == Token.IN) { - lexer.nextToken(); - stmt.setLogName(this.exprParser.primary()); - } - - if (lexer.token() == Token.FROM) { - lexer.nextToken(); - stmt.setFrom(this.exprParser.primary()); - } - - stmt.setLimit(this.parseLimit()); - - return stmt; - } - - if (identifierEquals("RELAYLOG")) { - lexer.nextToken(); - acceptIdentifier(EVENTS); - MySqlShowRelayLogEventsStatement stmt = new MySqlShowRelayLogEventsStatement(); - - if (lexer.token() == Token.IN) { - lexer.nextToken(); - stmt.setLogName(this.exprParser.primary()); - } - - if (lexer.token() == Token.FROM) { - lexer.nextToken(); - stmt.setFrom(this.exprParser.primary()); - } - - stmt.setLimit(this.parseLimit()); - - return stmt; - } - - if (identifierEquals("SLAVE")) { - lexer.nextToken(); - if (identifierEquals(STATUS)) { - lexer.nextToken(); - return new MySqlShowSlaveStatusStatement(); - } else { - acceptIdentifier("HOSTS"); - MySqlShowSlaveHostsStatement stmt = new MySqlShowSlaveHostsStatement(); - return stmt; - } - } - - if (lexer.token() == Token.TABLE) { - lexer.nextToken(); - acceptIdentifier(STATUS); - MySqlShowTableStatusStatement stmt = new MySqlShowTableStatusStatement(); - if (lexer.token() == Token.FROM || lexer.token() == Token.IN) { - lexer.nextToken(); - stmt.setDatabase(this.exprParser.name()); - } - - if (lexer.token() == Token.LIKE) { - lexer.nextToken(); - stmt.setLike(this.exprParser.expr()); - } - - if (lexer.token() == Token.WHERE) { - lexer.nextToken(); - stmt.setWhere(this.exprParser.expr()); - } - - return stmt; - } - - if (identifierEquals("TRIGGERS")) { - lexer.nextToken(); - MySqlShowTriggersStatement stmt = new MySqlShowTriggersStatement(); - - if (lexer.token() == Token.FROM) { - lexer.nextToken(); - SQLName database = exprParser.name(); - stmt.setDatabase(database); - } - - if (lexer.token() == Token.LIKE) { - lexer.nextToken(); - SQLExpr like = exprParser.expr(); - stmt.setLike(like); - } - - if (lexer.token() == Token.WHERE) { - lexer.nextToken(); - SQLExpr where = exprParser.expr(); - stmt.setWhere(where); - } - - return stmt; - } - - // MySqlShowSlaveHostsStatement - throw new ParserException("TODO " + lexer.stringVal()); - } - - private MySqlShowStatusStatement parseShowStatus() { - MySqlShowStatusStatement stmt = new MySqlShowStatusStatement(); - - if (lexer.token() == Token.LIKE) { - lexer.nextToken(); - SQLExpr like = exprParser.expr(); - stmt.setLike(like); - } - - if (lexer.token() == Token.WHERE) { - lexer.nextToken(); - SQLExpr where = exprParser.expr(); - stmt.setWhere(where); - } - - return stmt; - } - - private MySqlShowVariantsStatement parseShowVariants() { - MySqlShowVariantsStatement stmt = new MySqlShowVariantsStatement(); - - if (lexer.token() == Token.LIKE) { - lexer.nextToken(); - SQLExpr like = exprParser.expr(); - stmt.setLike(like); - } - - if (lexer.token() == Token.WHERE) { - lexer.nextToken(); - SQLExpr where = exprParser.expr(); - stmt.setWhere(where); - } - - return stmt; - } - - private MySqlShowWarningsStatement parseShowWarnings() { - MySqlShowWarningsStatement stmt = new MySqlShowWarningsStatement(); - - stmt.setLimit(parseLimit()); - - return stmt; - } - - private MySqlShowDatabasesStatement parseShowDatabases() { - MySqlShowDatabasesStatement stmt = new MySqlShowDatabasesStatement(); - - if (lexer.token() == Token.LIKE) { - lexer.nextToken(); - SQLExpr like = exprParser.expr(); - stmt.setLike(like); - } - - if (lexer.token() == Token.WHERE) { - lexer.nextToken(); - SQLExpr where = exprParser.expr(); - stmt.setWhere(where); - } - - return stmt; - } - - private MySqlShowTablesStatement parseShowTabless() { - MySqlShowTablesStatement stmt = new MySqlShowTablesStatement(); - - if (lexer.token() == Token.FROM) { - lexer.nextToken(); - SQLName database = exprParser.name(); - stmt.setDatabase(database); - } - - if (lexer.token() == Token.LIKE) { - lexer.nextToken(); - SQLExpr like = exprParser.expr(); - stmt.setLike(like); - } - - if (lexer.token() == Token.WHERE) { - lexer.nextToken(); - SQLExpr where = exprParser.expr(); - stmt.setWhere(where); - } - - return stmt; - } - - private MySqlShowColumnsStatement parseShowColumns() { - MySqlShowColumnsStatement stmt = new MySqlShowColumnsStatement(); - - if (lexer.token() == Token.FROM) { - lexer.nextToken(); - SQLName table = exprParser.name(); - stmt.setTable(table); - - if (lexer.token() == Token.FROM) { - lexer.nextToken(); - SQLName database = exprParser.name(); - stmt.setDatabase(database); - } - } - - if (lexer.token() == Token.LIKE) { - lexer.nextToken(); - SQLExpr like = exprParser.expr(); - stmt.setLike(like); - } - - if (lexer.token() == Token.WHERE) { - lexer.nextToken(); - SQLExpr where = exprParser.expr(); - stmt.setWhere(where); - } - - return stmt; - } - - public MySqlStartTransactionStatement parseStart() { - acceptIdentifier("START"); - acceptIdentifier("TRANSACTION"); - - MySqlStartTransactionStatement stmt = new MySqlStartTransactionStatement(); - - if (identifierEquals("WITH")) { - lexer.nextToken(); - acceptIdentifier("CONSISTENT"); - acceptIdentifier("SNAPSHOT"); - stmt.setConsistentSnapshot(true); - } - - if (identifierEquals("BEGIN")) { - lexer.nextToken(); - stmt.setBegin(true); - if (identifierEquals("WORK")) { - lexer.nextToken(); - stmt.setWork(true); - } - } - - return stmt; - } - - @Override - public MySqlRollbackStatement parseRollback() { - acceptIdentifier("ROLLBACK"); - - MySqlRollbackStatement stmt = new MySqlRollbackStatement(); - - if (identifierEquals("WORK")) { - lexer.nextToken(); - } - - if (lexer.token() == Token.AND) { - lexer.nextToken(); - if (lexer.token() == Token.NOT) { - lexer.nextToken(); - acceptIdentifier(CHAIN); - stmt.setChain(Boolean.FALSE); - } else { - acceptIdentifier(CHAIN); - stmt.setChain(Boolean.TRUE); - } - } - - if (identifierEquals("TO")) { - lexer.nextToken(); - - if (identifierEquals("SAVEPOINT")) { - lexer.nextToken(); - } - - stmt.setTo(this.exprParser.name()); - } - - return stmt; - } - - public MySqlCommitStatement parseCommit() { - acceptIdentifier("COMMIT"); - - MySqlCommitStatement stmt = new MySqlCommitStatement(); - - if (identifierEquals("WORK")) { - lexer.nextToken(); - stmt.setWork(true); - } - - if (lexer.token() == Token.AND) { - lexer.nextToken(); - if (lexer.token() == Token.NOT) { - lexer.nextToken(); - acceptIdentifier(CHAIN); - stmt.setChain(Boolean.FALSE); - } else { - acceptIdentifier(CHAIN); - stmt.setChain(Boolean.TRUE); - } - } - - return stmt; - } - - public MySqlReplaceStatement parseReplicate() { - MySqlReplaceStatement stmt = new MySqlReplaceStatement(); - - acceptIdentifier(REPLACE); - - if (identifierEquals(LOW_PRIORITY)) { - stmt.setLowPriority(true); - lexer.nextToken(); - } - - if (identifierEquals(DELAYED)) { - stmt.setDelayed(true); - lexer.nextToken(); - } - - if (lexer.token() == Token.INTO) { - lexer.nextToken(); - } - - SQLName tableName = exprParser.name(); - stmt.setTableName(tableName); - - if (lexer.token() == Token.LPAREN) { - lexer.nextToken(); - if (lexer.token() == Token.SELECT) { - SQLQueryExpr queryExpr = (SQLQueryExpr) this.exprParser.expr(); - stmt.setQuery(queryExpr); - } else { - this.exprParser.exprList(stmt.getColumns()); - } - accept(Token.RPAREN); - } - - if (lexer.token() == Token.VALUES || identifierEquals("VALUE")) { - lexer.nextToken(); - - for (;;) { - accept(Token.LPAREN); - SQLInsertStatement.ValuesClause values = new SQLInsertStatement.ValuesClause(); - this.exprParser.exprList(values.getValues()); - stmt.getValuesList().add(values); - accept(Token.RPAREN); - - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; - } else { - break; - } - } - } else if (lexer.token() == Token.SELECT) { - SQLQueryExpr queryExpr = (SQLQueryExpr) this.exprParser.expr(); - stmt.setQuery(queryExpr); - } else if (lexer.token() == Token.SET) { - lexer.nextToken(); - - SQLInsertStatement.ValuesClause values = new SQLInsertStatement.ValuesClause(); - stmt.getValuesList().add(values); - for (;;) { - stmt.getColumns().add(this.exprParser.name()); - if (lexer.token() == Token.COLONEQ) { - lexer.nextToken(); - } else { - accept(Token.EQ); - } - values.getValues().add(this.exprParser.expr()); - - if (lexer.token() == (Token.COMMA)) { - lexer.nextToken(); - continue; - } - - break; - } - } else if (lexer.token() == Token.LPAREN) { - lexer.nextToken(); - SQLQueryExpr queryExpr = (SQLQueryExpr) this.exprParser.expr(); - stmt.setQuery(queryExpr); - accept(Token.RPAREN); - } - - return stmt; - } - - protected SQLStatement parseLoad() { - acceptIdentifier("LOAD"); - - if (identifierEquals("DATA")) { - SQLStatement stmt = parseLoadDataInFile(); - return stmt; - } - - if (identifierEquals("XML")) { - SQLStatement stmt = parseLoadXml(); - return stmt; - } - - throw new ParserException("TODO"); - } - - protected MySqlLoadXmlStatement parseLoadXml() { - acceptIdentifier("XML"); - - MySqlLoadXmlStatement stmt = new MySqlLoadXmlStatement(); - - if (identifierEquals(LOW_PRIORITY)) { - stmt.setLowPriority(true); - lexer.nextToken(); - } - - if (identifierEquals("CONCURRENT")) { - stmt.setConcurrent(true); - lexer.nextToken(); - } - - if (identifierEquals(LOCAL)) { - stmt.setLocal(true); - lexer.nextToken(); - } - - acceptIdentifier("INFILE"); - - SQLLiteralExpr fileName = (SQLLiteralExpr) exprParser.expr(); - stmt.setFileName(fileName); - - if (identifierEquals(REPLACE)) { - stmt.setReplicate(true); - lexer.nextToken(); - } - - if (identifierEquals(IGNORE)) { - stmt.setIgnore(true); - lexer.nextToken(); - } - - accept(Token.INTO); - accept(Token.TABLE); - - SQLName tableName = exprParser.name(); - stmt.setTableName(tableName); - - if (identifierEquals(CHARACTER)) { - lexer.nextToken(); - accept(Token.SET); - - if (lexer.token() != Token.LITERAL_CHARS) { - throw new ParserException("syntax error, illegal charset"); - } - - String charset = lexer.stringVal(); - lexer.nextToken(); - stmt.setCharset(charset); - } - - if (identifierEquals("ROWS")) { - lexer.nextToken(); - accept(Token.IDENTIFIED); - accept(Token.BY); - SQLExpr rowsIdentifiedBy = exprParser.expr(); - stmt.setRowsIdentifiedBy(rowsIdentifiedBy); - } - - if (identifierEquals(IGNORE)) { - throw new ParserException("TODO"); - } - - if (lexer.token() == Token.SET) { - throw new ParserException("TODO"); - } - - return stmt; - } - - protected MySqlLoadDataInFileStatement parseLoadDataInFile() { - acceptIdentifier("DATA"); - - MySqlLoadDataInFileStatement stmt = new MySqlLoadDataInFileStatement(); - - if (identifierEquals(LOW_PRIORITY)) { - stmt.setLowPriority(true); - lexer.nextToken(); - } - - if (identifierEquals("CONCURRENT")) { - stmt.setConcurrent(true); - lexer.nextToken(); - } - - if (identifierEquals(LOCAL)) { - stmt.setLocal(true); - lexer.nextToken(); - } - - acceptIdentifier("INFILE"); - - SQLLiteralExpr fileName = (SQLLiteralExpr) exprParser.expr(); - stmt.setFileName(fileName); - - if (identifierEquals(REPLACE)) { - stmt.setReplicate(true); - lexer.nextToken(); - } - - if (identifierEquals(IGNORE)) { - stmt.setIgnore(true); - lexer.nextToken(); - } - - accept(Token.INTO); - accept(Token.TABLE); - - SQLName tableName = exprParser.name(); - stmt.setTableName(tableName); - - if (identifierEquals(CHARACTER)) { - lexer.nextToken(); - accept(Token.SET); - - if (lexer.token() != Token.LITERAL_CHARS) { - throw new ParserException("syntax error, illegal charset"); - } - - String charset = lexer.stringVal(); - lexer.nextToken(); - stmt.setCharset(charset); - } - - if (identifierEquals("FIELDS") || identifierEquals("COLUMNS")) { - throw new ParserException("TODO"); - } - - if (identifierEquals("LINES")) { - throw new ParserException("TODO"); - } - - if (identifierEquals(IGNORE)) { - throw new ParserException("TODO"); - } - - if (lexer.token() == Token.SET) { - throw new ParserException("TODO"); - } - - return stmt; - } - - public MySqlPrepareStatement parsePrepare() { - acceptIdentifier("PREPARE"); - - SQLName name = exprParser.name(); - accept(Token.FROM); - SQLExpr from = exprParser.expr(); - - return new MySqlPrepareStatement(name, from); - } - - public MySqlExecuteStatement parseExecute() { - acceptIdentifier("EXECUTE"); - - MySqlExecuteStatement stmt = new MySqlExecuteStatement(); - - SQLName statementName = exprParser.name(); - stmt.setStatementName(statementName); - - if (identifierEquals("USING")) { - lexer.nextToken(); - exprParser.exprList(stmt.getParameters()); - } - - return stmt; - } - - public SQLInsertStatement parseInsert() { - MySqlInsertStatement insertStatement = new MySqlInsertStatement(); - - if (lexer.token() == Token.INSERT) { - lexer.nextToken(); - - if (identifierEquals(LOW_PRIORITY)) { - insertStatement.setLowPriority(true); - lexer.nextToken(); - } - - if (identifierEquals(DELAYED)) { - insertStatement.setDelayed(true); - lexer.nextToken(); - } - - if (identifierEquals("HIGH_PRIORITY")) { - insertStatement.setHighPriority(true); - lexer.nextToken(); - } - - if (identifierEquals(IGNORE)) { - insertStatement.setIgnore(true); - lexer.nextToken(); - } - - if (lexer.token() == Token.INTO) { - lexer.nextToken(); - } - - SQLName tableName = this.exprParser.name(); - insertStatement.setTableName(tableName); - - if (lexer.token() == Token.IDENTIFIER && !identifierEquals("VALUE")) { - insertStatement.setAlias(lexer.stringVal()); - lexer.nextToken(); - } - - } - - if (lexer.token() == (Token.LPAREN)) { - lexer.nextToken(); - if (lexer.token() == (Token.SELECT)) { - SQLSelect select = this.exprParser.createSelectParser().select(); - select.setParent(insertStatement); - insertStatement.setQuery(select); - } else { - this.exprParser.exprList(insertStatement.getColumns()); - } - accept(Token.RPAREN); - } - - if (lexer.token() == Token.VALUES || identifierEquals("VALUE")) { - lexer.nextToken(); - - for (;;) { - accept(Token.LPAREN); - SQLInsertStatement.ValuesClause values = new SQLInsertStatement.ValuesClause(); - this.exprParser.exprList(values.getValues()); - insertStatement.getValuesList().add(values); - accept(Token.RPAREN); - - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; - } else { - break; - } - } - } else if (lexer.token() == Token.SET) { - lexer.nextToken(); - - SQLInsertStatement.ValuesClause values = new SQLInsertStatement.ValuesClause(); - insertStatement.getValuesList().add(values); - - for (;;) { - SQLName name = this.exprParser.name(); - insertStatement.getColumns().add(name); - if (lexer.token() == Token.EQ) { - lexer.nextToken(); - } else { - accept(Token.COLONEQ); - } - values.getValues().add(this.exprParser.expr()); - - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; - } - - break; - } - - } else if (lexer.token() == (Token.SELECT)) { - SQLSelect select = this.exprParser.createSelectParser().select(); - select.setParent(insertStatement); - insertStatement.setQuery(select); - } else if (lexer.token() == (Token.LPAREN)) { - lexer.nextToken(); - SQLSelect select = this.exprParser.createSelectParser().select(); - select.setParent(insertStatement); - insertStatement.setQuery(select); - accept(Token.RPAREN); - } - - if (lexer.token() == Token.ON) { - lexer.nextToken(); - acceptIdentifier("DUPLICATE"); - accept(Token.KEY); - accept(Token.UPDATE); - - exprParser.exprList(insertStatement.getDuplicateKeyUpdate()); - } - - return insertStatement; - } - - public SQLStatement parseDropUser() { - acceptIdentifier(USER); - - MySqlDropUser stmt = new MySqlDropUser(); - for (;;) { - SQLExpr expr = this.exprParser.expr(); - stmt.getUsers().add(expr); - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; - } - break; - } - - return stmt; - } - - protected SQLDropTableStatement parseDropTable(boolean acceptDrop) { - if (acceptDrop) { - accept(Token.DROP); - } - - MySqlDropTableStatement stmt = new MySqlDropTableStatement(); - - if (identifierEquals(TEMPORARY)) { - lexer.nextToken(); - stmt.setTemporary(true); - } - - accept(Token.TABLE); - - if (lexer.token() == Token.IF) { - lexer.nextToken(); - accept(Token.EXISTS); - stmt.setIfExists(true); - } - - for (;;) { - SQLName name = this.exprParser.name(); - stmt.addTableSource(name); - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; - } - break; - } - - if (identifierEquals(RESTRICT)) { - stmt.setOption(RESTRICT); - lexer.nextToken(); - } else if (identifierEquals(CASCADE)) { - stmt.setOption(CASCADE); - lexer.nextToken(); - } - - return stmt; - } - - protected SQLDropViewStatement parseDropView(boolean acceptDrop) { - if (acceptDrop) { - accept(Token.DROP); - } - - MySqlDropViewStatement stmt = new MySqlDropViewStatement(); - - accept(Token.VIEW); - - if (lexer.token() == Token.IF) { - lexer.nextToken(); - accept(Token.EXISTS); - stmt.setIfExists(true); - } - - for (;;) { - SQLName name = this.exprParser.name(); - stmt.addTableSource(name); - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; - } - break; - } - - if (identifierEquals(RESTRICT)) { - stmt.setOption(RESTRICT); - lexer.nextToken(); - } else if (identifierEquals(CASCADE)) { - stmt.setOption(CASCADE); - lexer.nextToken(); - } - - return stmt; - } - - public SQLSelectParser createSQLSelectParser() { - return new MySqlSelectParser(this.exprParser); - } - - public SQLStatement parseSet() { - accept(Token.SET); - - Boolean global = null; - if (identifierEquals(GLOBAL)) { - global = Boolean.TRUE; - lexer.nextToken(); - } else if (identifierEquals(SESSION)) { - global = Boolean.FALSE; - lexer.nextToken(); - } - - if (identifierEquals("TRANSACTION")) { - lexer.nextToken(); - acceptIdentifier("ISOLATION"); - acceptIdentifier("LEVEL"); - - MySqlSetTransactionIsolationLevelStatement stmt = new MySqlSetTransactionIsolationLevelStatement(); - stmt.setGlobal(global); - - if (identifierEquals(READ)) { - lexer.nextToken(); - - if (identifierEquals("UNCOMMITTED")) { - stmt.setLevel("READ UNCOMMITTED"); - lexer.nextToken(); - } else if (identifierEquals(WRITE)) { - stmt.setLevel("READ WRITE"); - lexer.nextToken(); - } else if (identifierEquals("ONLY")) { - stmt.setLevel("READ ONLY"); - lexer.nextToken(); - } else if (identifierEquals("COMMITTED")) { - stmt.setLevel("READ COMMITTED"); - lexer.nextToken(); - } else { - throw new ParserException("UNKOWN TRANSACTION LEVEL : " + lexer.stringVal()); - } - } else if (identifierEquals("SERIALIZABLE")) { - stmt.setLevel("SERIALIZABLE"); - lexer.nextToken(); - } else if (identifierEquals("REPEATABLE")) { - lexer.nextToken(); - if (identifierEquals(READ)) { - stmt.setLevel("REPEATABLE READ"); - lexer.nextToken(); - } else { - throw new ParserException("UNKOWN TRANSACTION LEVEL : " + lexer.stringVal()); - } - } else { - throw new ParserException("UNKOWN TRANSACTION LEVEL : " + lexer.stringVal()); - } - - return stmt; - } else if (identifierEquals("NAMES")) { - lexer.nextToken(); - - MySqlSetNamesStatement stmt = new MySqlSetNamesStatement(); - if (lexer.token() == Token.DEFAULT) { - lexer.nextToken(); - stmt.setDefault(true); - } else { - String charSet = lexer.stringVal(); - stmt.setCharSet(charSet); - lexer.nextToken(); - if (identifierEquals(COLLATE2)) { - lexer.nextToken(); - - String collate = lexer.stringVal(); - stmt.setCollate(collate); - lexer.nextToken(); - } - } - return stmt; - } else if (identifierEquals(CHARACTER)) { - lexer.nextToken(); - - accept(Token.SET); - - MySqlSetCharSetStatement stmt = new MySqlSetCharSetStatement(); - if (lexer.token() == Token.DEFAULT) { - lexer.nextToken(); - stmt.setDefault(true); - } else { - String charSet = lexer.stringVal(); - stmt.setCharSet(charSet); - lexer.nextToken(); - if (identifierEquals(COLLATE2)) { - lexer.nextToken(); - - String collate = lexer.stringVal(); - stmt.setCollate(collate); - lexer.nextToken(); - } - } - return stmt; - } else { - SQLSetStatement stmt = new SQLSetStatement(); - - parseAssignItems(stmt.getItems()); - - if (global != null && global.booleanValue()) { - SQLVariantRefExpr varRef = (SQLVariantRefExpr) stmt.getItems().get(0).getTarget(); - varRef.setGlobal(true); - } - - return stmt; - } - } - - public Limit parseLimit() { - return ((MySqlExprParser) this.exprParser).parseLimit(); - } - - public SQLStatement parseAlter() { - accept(Token.ALTER); - - boolean ignore = false; - - if (identifierEquals(IGNORE)) { - ignore = true; - lexer.nextToken(); - } - - if (lexer.token() == Token.TABLE) { - lexer.nextToken(); - - MySqlAlterTableStatement stmt = new MySqlAlterTableStatement(); - stmt.setIgnore(ignore); - stmt.setName(this.exprParser.name()); - - for (;;) { - if (identifierEquals("ADD")) { - lexer.nextToken(); - - if (identifierEquals("COLUMN")) { - lexer.nextToken(); - MySqlAlterTableAddColumn item = new MySqlAlterTableAddColumn(); - SQLColumnDefinition columnDef = this.exprParser.parseColumn(); - item.getColumns().add(columnDef); - if (identifierEquals("AFTER")) { - lexer.nextToken(); - item.setAfterColumn(this.exprParser.name()); - } else if (identifierEquals("FIRST")) { - lexer.nextToken(); - if (lexer.token() == Token.IDENTIFIER) { - item.setFirstColumn(this.exprParser.name()); - } else { - item.setFirst(true); - } - } - stmt.getItems().add(item); - } else if (lexer.token() == Token.INDEX) { - lexer.nextToken(); - MySqlAlterTableAddIndex item = new MySqlAlterTableAddIndex(); - - if (lexer.token() == Token.LPAREN) { - lexer.nextToken(); - } else { - item.setName(this.exprParser.name()); - accept(Token.LPAREN); - } - - for (;;) { - SQLSelectOrderByItem column = this.exprParser.parseSelectOrderByItem(); - item.getItems().add(column); - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; - } - break; - } - accept(Token.RPAREN); - - stmt.getItems().add(item); - } else if (lexer.token() == Token.UNIQUE) { - lexer.nextToken(); - - if (lexer.token() == Token.INDEX) { - lexer.nextToken(); - } - - MySqlAlterTableAddUnique item = new MySqlAlterTableAddUnique(); - - if (lexer.token() == Token.LPAREN) { - lexer.nextToken(); - } else { - item.setName(this.exprParser.name()); - accept(Token.LPAREN); - } - - for (;;) { - SQLSelectOrderByItem column = this.exprParser.parseSelectOrderByItem(); - item.getItems().add(column); - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; - } - break; - } - accept(Token.RPAREN); - - stmt.getItems().add(item); - } else if (lexer.token() == Token.PRIMARY) { - SQLPrimaryKey primaryKey = ((MySqlExprParser) this.exprParser).parsePrimaryKey(); - SQLAlterTableAddPrimaryKey item = new SQLAlterTableAddPrimaryKey(); - item.setPrimaryKey(primaryKey); - stmt.getItems().add(item); - } else if (lexer.token() == Token.KEY) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } else if (lexer.token() == Token.CONSTRAINT) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } else if (identifierEquals(FULLTEXT)) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } else if (identifierEquals(SPATIAL)) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } else { - MySqlAlterTableAddColumn item = new MySqlAlterTableAddColumn(); - SQLColumnDefinition columnDef = this.exprParser.parseColumn(); - item.getColumns().add(columnDef); - if (identifierEquals("AFTER")) { - lexer.nextToken(); - item.setAfterColumn(this.exprParser.name()); - } - stmt.getItems().add(item); - } - } else if (identifierEquals("ALTER")) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } else if (identifierEquals("CHANGE")) { - lexer.nextToken(); - if (identifierEquals("COLUMN")) { - lexer.nextToken(); - } - MySqlAlterTableChangeColumn item = new MySqlAlterTableChangeColumn(); - item.setColumnName(this.exprParser.name()); - item.setNewColumnDefinition(this.exprParser.parseColumn()); - if (identifierEquals("AFTER")) { - lexer.nextToken(); - item.setAfterColumn(this.exprParser.name()); - } else if (identifierEquals("FIRST")) { - lexer.nextToken(); - if (lexer.token() == Token.IDENTIFIER) { - item.setFirstColumn(this.exprParser.name()); - } else { - item.setFirst(true); - } - } - stmt.getItems().add(item); - } else if (identifierEquals("MODIFY")) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } else if (lexer.token() == Token.DROP) { - lexer.nextToken(); - if (lexer.token() == Token.INDEX) { - lexer.nextToken(); - SQLName indexName = this.exprParser.name(); - SQLAlterTableDropIndex item = new SQLAlterTableDropIndex(); - item.setIndexName(indexName); - stmt.getItems().add(item); - } else { - if (identifierEquals("COLUMN")) { - lexer.nextToken(); - } - SQLAlterTableDropColumnItem item = new SQLAlterTableDropColumnItem(); - item.setColumnName(this.exprParser.name()); - stmt.getItems().add(item); - } - } else if (identifierEquals("DISABLE")) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } else if (identifierEquals("ENABLE")) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } else if (identifierEquals("RENAME")) { - lexer.nextToken(); - MySqlRenameTableStatement renameStmt = new MySqlRenameTableStatement(); - MySqlRenameTableStatement.Item item = new MySqlRenameTableStatement.Item(); - item.setName(stmt.getTableSource().getExpr()); - item.setTo(this.exprParser.name()); - renameStmt.getItems().add(item); - - return renameStmt; - } else if (lexer.token() == Token.ORDER) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } else if (identifierEquals("CONVERT")) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } else if (lexer.token() == Token.DEFAULT) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } else if (identifierEquals("DISCARD")) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } else if (identifierEquals("IMPORT")) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } else if (identifierEquals("FORCE")) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } else if (identifierEquals("TRUNCATE")) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } else if (identifierEquals("COALESCE")) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - - } else if (identifierEquals("REORGANIZE")) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } else if (identifierEquals("EXCHANGE")) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } else if (identifierEquals("ANALYZE")) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } else if (identifierEquals("CHECK")) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } else if (identifierEquals("OPTIMIZE")) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } else if (identifierEquals("REBUILD")) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } else if (identifierEquals("REPAIR")) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } else if (identifierEquals("REMOVE")) { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } else if (identifierEquals(ENGINE)) { - lexer.nextToken(); - accept(Token.EQ); - stmt.getItems().add(new MySqlAlterTableOption(ENGINE, lexer.stringVal())); - lexer.nextToken(); - } else if (identifierEquals(COLLATE2)) { - lexer.nextToken(); - accept(Token.EQ); - stmt.getItems().add(new MySqlAlterTableOption(COLLATE2, lexer.stringVal())); - lexer.nextToken(); - } else if (identifierEquals("PACK_KEYS")) { - lexer.nextToken(); - accept(Token.EQ); - if (identifierEquals("PACK")) { - lexer.nextToken(); - accept(Token.ALL); - stmt.getItems().add(new MySqlAlterTableOption("PACK_KEYS", "PACK ALL")); - } else { - stmt.getItems().add(new MySqlAlterTableOption("PACK_KEYS", lexer.stringVal())); - lexer.nextToken(); - } - } else if (identifierEquals(CHARACTER)) { - lexer.nextToken(); - accept(Token.SET); - accept(Token.EQ); - MySqlAlterTableCharacter item = new MySqlAlterTableCharacter(); - item.setCharacterSet(this.exprParser.primary()); - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - acceptIdentifier(COLLATE2); - accept(Token.EQ); - item.setCollate(this.exprParser.primary()); - } - stmt.getItems().add(item); - } else { - break; - } - - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; - } else { - break; - } - } - - return stmt; - } - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } - - public SQLStatement parseRename() { - MySqlRenameTableStatement stmt = new MySqlRenameTableStatement(); - - acceptIdentifier("RENAME"); - - accept(Token.TABLE); - - for (;;) { - MySqlRenameTableStatement.Item item = new MySqlRenameTableStatement.Item(); - item.setName(this.exprParser.name()); - acceptIdentifier("TO"); - item.setTo(this.exprParser.name()); - - stmt.getItems().add(item); - - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; - } - - break; - } - - return stmt; - } - - public SQLStatement parseCreateDatabase() { - if (lexer.token() == Token.CREATE) { - lexer.nextToken(); - } - - accept(Token.DATABASE); - - SQLCreateDatabaseStatement stmt = new SQLCreateDatabaseStatement(); - stmt.setName(this.exprParser.name()); - - if (lexer.token() == Token.DEFAULT) { - lexer.nextToken(); - } - - return stmt; - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/visitor/MySql2OracleOutputVisitor.java b/src/main/java/org/durid/sql/dialect/mysql/visitor/MySql2OracleOutputVisitor.java deleted file mode 100644 index bac5d94d..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/visitor/MySql2OracleOutputVisitor.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.visitor; - -import org.durid.sql.dialect.mysql.ast.expr.MySqlBooleanExpr; - -public class MySql2OracleOutputVisitor extends MySqlOutputVisitor { - - public MySql2OracleOutputVisitor(Appendable appender){ - super(appender); - } - - public boolean visit(MySqlBooleanExpr x) { - return true; - } - - public void endVisit(MySqlBooleanExpr x) { - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlASTVisitor.java b/src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlASTVisitor.java deleted file mode 100644 index 5574275a..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlASTVisitor.java +++ /dev/null @@ -1,518 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.visitor; - -import org.durid.sql.dialect.mysql.ast.MySqlForceIndexHint; -import org.durid.sql.dialect.mysql.ast.MySqlIgnoreIndexHint; -import org.durid.sql.dialect.mysql.ast.MySqlKey; -import org.durid.sql.dialect.mysql.ast.MySqlPrimaryKey; -import org.durid.sql.dialect.mysql.ast.MySqlUseIndexHint; -import org.durid.sql.dialect.mysql.ast.expr.MySqlBinaryExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlBooleanExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlCharExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlExtractExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlIntervalExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlMatchAgainstExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlUserName; -import org.durid.sql.dialect.mysql.ast.statement.CobarShowStatus; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddColumn; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddIndex; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddUnique; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableCharacter; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableOption; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlBinlogStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlCommitStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlCreateIndexStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlDescribeStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlDropTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlDropUser; -import org.durid.sql.dialect.mysql.ast.statement.MySqlDropViewStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlExecuteStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlHelpStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlInsertStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlKillStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlLoadDataInFileStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlLoadXmlStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlPartitionByKey; -import org.durid.sql.dialect.mysql.ast.statement.MySqlPrepareStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlRenameTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlReplaceStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlResetStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlRollbackStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectGroupBy; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSetCharSetStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSetNamesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSetTransactionIsolationLevelStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowAuthorsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowBinLogEventsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowBinaryLogsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCharacterSetStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCollationStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowColumnsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowContributorsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateDatabaseStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateEventStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateFunctionStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateProcedureStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateTriggerStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateViewStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowDatabasesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowEngineStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowEnginesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowErrorsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowEventsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowFunctionCodeStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowFunctionStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowGrantsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowIndexesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowKeysStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowMasterStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowOpenTablesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowPluginsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowPrivilegesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowProcedureCodeStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowProcedureStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowProcessListStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowProfileStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowProfilesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowRelayLogEventsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowSlaveHostsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowSlaveStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowTableStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowTriggersStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowVariantsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlStartTransactionStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlTableIndex; -import org.durid.sql.dialect.mysql.ast.statement.MySqlUnionQuery; -import org.durid.sql.dialect.mysql.ast.statement.MySqlUnlockTablesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement; -import org.durid.sql.visitor.SQLASTVisitor; - -public interface MySqlASTVisitor extends SQLASTVisitor { - - boolean visit(MySqlBooleanExpr x); - - void endVisit(MySqlBooleanExpr x); - - boolean visit(MySqlSelectQueryBlock.Limit x); - - void endVisit(MySqlSelectQueryBlock.Limit x); - - boolean visit(MySqlTableIndex x); - - void endVisit(MySqlTableIndex x); - - boolean visit(MySqlKey x); - - void endVisit(MySqlKey x); - - boolean visit(MySqlPrimaryKey x); - - void endVisit(MySqlPrimaryKey x); - - void endVisit(MySqlIntervalExpr x); - - boolean visit(MySqlIntervalExpr x); - - void endVisit(MySqlExtractExpr x); - - boolean visit(MySqlExtractExpr x); - - void endVisit(MySqlMatchAgainstExpr x); - - boolean visit(MySqlMatchAgainstExpr x); - - void endVisit(MySqlBinaryExpr x); - - boolean visit(MySqlBinaryExpr x); - - void endVisit(MySqlPrepareStatement x); - - boolean visit(MySqlPrepareStatement x); - - void endVisit(MySqlExecuteStatement x); - - boolean visit(MySqlExecuteStatement x); - - void endVisit(MySqlDeleteStatement x); - - boolean visit(MySqlDeleteStatement x); - - void endVisit(MySqlInsertStatement x); - - boolean visit(MySqlInsertStatement x); - - void endVisit(MySqlLoadDataInFileStatement x); - - boolean visit(MySqlLoadDataInFileStatement x); - - void endVisit(MySqlLoadXmlStatement x); - - boolean visit(MySqlLoadXmlStatement x); - - void endVisit(MySqlReplaceStatement x); - - boolean visit(MySqlReplaceStatement x); - - void endVisit(MySqlSelectGroupBy x); - - boolean visit(MySqlSelectGroupBy x); - - void endVisit(MySqlStartTransactionStatement x); - - boolean visit(MySqlStartTransactionStatement x); - - void endVisit(MySqlCommitStatement x); - - boolean visit(MySqlCommitStatement x); - - void endVisit(MySqlRollbackStatement x); - - boolean visit(MySqlRollbackStatement x); - - void endVisit(MySqlShowColumnsStatement x); - - boolean visit(MySqlShowColumnsStatement x); - - void endVisit(MySqlShowTablesStatement x); - - boolean visit(MySqlShowTablesStatement x); - - void endVisit(MySqlShowDatabasesStatement x); - - boolean visit(MySqlShowDatabasesStatement x); - - void endVisit(MySqlShowWarningsStatement x); - - boolean visit(MySqlShowWarningsStatement x); - - void endVisit(MySqlShowStatusStatement x); - - boolean visit(MySqlShowStatusStatement x); - - void endVisit(MySqlShowAuthorsStatement x); - - boolean visit(MySqlShowAuthorsStatement x); - - void endVisit(CobarShowStatus x); - - boolean visit(CobarShowStatus x); - - void endVisit(MySqlKillStatement x); - - boolean visit(MySqlKillStatement x); - - void endVisit(MySqlBinlogStatement x); - - boolean visit(MySqlBinlogStatement x); - - void endVisit(MySqlResetStatement x); - - boolean visit(MySqlResetStatement x); - - void endVisit(MySqlDropUser x); - - boolean visit(MySqlDropUser x); - - void endVisit(MySqlCreateUserStatement x); - - boolean visit(MySqlCreateUserStatement x); - - void endVisit(MySqlCreateUserStatement.UserSpecification x); - - boolean visit(MySqlCreateUserStatement.UserSpecification x); - - void endVisit(MySqlDropTableStatement x); - - boolean visit(MySqlDropTableStatement x); - - void endVisit(MySqlPartitionByKey x); - - boolean visit(MySqlPartitionByKey x); - - boolean visit(MySqlSelectQueryBlock x); - - void endVisit(MySqlSelectQueryBlock x); - - boolean visit(MySqlOutFileExpr x); - - void endVisit(MySqlOutFileExpr x); - - boolean visit(MySqlDescribeStatement x); - - void endVisit(MySqlDescribeStatement x); - - boolean visit(MySqlUpdateStatement x); - - void endVisit(MySqlUpdateStatement x); - - boolean visit(MySqlSetTransactionIsolationLevelStatement x); - - void endVisit(MySqlSetTransactionIsolationLevelStatement x); - - boolean visit(MySqlSetNamesStatement x); - - void endVisit(MySqlSetNamesStatement x); - - boolean visit(MySqlSetCharSetStatement x); - - void endVisit(MySqlSetCharSetStatement x); - - boolean visit(MySqlShowBinaryLogsStatement x); - - void endVisit(MySqlShowBinaryLogsStatement x); - - boolean visit(MySqlShowMasterLogsStatement x); - - void endVisit(MySqlShowMasterLogsStatement x); - - boolean visit(MySqlShowCharacterSetStatement x); - - void endVisit(MySqlShowCharacterSetStatement x); - - boolean visit(MySqlShowCollationStatement x); - - void endVisit(MySqlShowCollationStatement x); - - boolean visit(MySqlShowBinLogEventsStatement x); - - void endVisit(MySqlShowBinLogEventsStatement x); - - boolean visit(MySqlShowContributorsStatement x); - - void endVisit(MySqlShowContributorsStatement x); - - boolean visit(MySqlShowCreateDatabaseStatement x); - - void endVisit(MySqlShowCreateDatabaseStatement x); - - boolean visit(MySqlShowCreateEventStatement x); - - void endVisit(MySqlShowCreateEventStatement x); - - boolean visit(MySqlShowCreateFunctionStatement x); - - void endVisit(MySqlShowCreateFunctionStatement x); - - boolean visit(MySqlShowCreateProcedureStatement x); - - void endVisit(MySqlShowCreateProcedureStatement x); - - boolean visit(MySqlShowCreateTableStatement x); - - void endVisit(MySqlShowCreateTableStatement x); - - boolean visit(MySqlShowCreateTriggerStatement x); - - void endVisit(MySqlShowCreateTriggerStatement x); - - boolean visit(MySqlShowCreateViewStatement x); - - void endVisit(MySqlShowCreateViewStatement x); - - boolean visit(MySqlShowEngineStatement x); - - void endVisit(MySqlShowEngineStatement x); - - boolean visit(MySqlShowEnginesStatement x); - - void endVisit(MySqlShowEnginesStatement x); - - boolean visit(MySqlShowErrorsStatement x); - - void endVisit(MySqlShowErrorsStatement x); - - boolean visit(MySqlShowEventsStatement x); - - void endVisit(MySqlShowEventsStatement x); - - boolean visit(MySqlShowFunctionCodeStatement x); - - void endVisit(MySqlShowFunctionCodeStatement x); - - boolean visit(MySqlShowFunctionStatusStatement x); - - void endVisit(MySqlShowFunctionStatusStatement x); - - boolean visit(MySqlShowGrantsStatement x); - - void endVisit(MySqlShowGrantsStatement x); - - boolean visit(MySqlUserName x); - - void endVisit(MySqlUserName x); - - boolean visit(MySqlShowIndexesStatement x); - - void endVisit(MySqlShowIndexesStatement x); - - boolean visit(MySqlShowKeysStatement x); - - void endVisit(MySqlShowKeysStatement x); - - boolean visit(MySqlShowMasterStatusStatement x); - - void endVisit(MySqlShowMasterStatusStatement x); - - boolean visit(MySqlShowOpenTablesStatement x); - - void endVisit(MySqlShowOpenTablesStatement x); - - boolean visit(MySqlShowPluginsStatement x); - - void endVisit(MySqlShowPluginsStatement x); - - boolean visit(MySqlShowPrivilegesStatement x); - - void endVisit(MySqlShowPrivilegesStatement x); - - boolean visit(MySqlShowProcedureCodeStatement x); - - void endVisit(MySqlShowProcedureCodeStatement x); - - boolean visit(MySqlShowProcedureStatusStatement x); - - void endVisit(MySqlShowProcedureStatusStatement x); - - boolean visit(MySqlShowProcessListStatement x); - - void endVisit(MySqlShowProcessListStatement x); - - boolean visit(MySqlShowProfileStatement x); - - void endVisit(MySqlShowProfileStatement x); - - boolean visit(MySqlShowProfilesStatement x); - - void endVisit(MySqlShowProfilesStatement x); - - boolean visit(MySqlShowRelayLogEventsStatement x); - - void endVisit(MySqlShowRelayLogEventsStatement x); - - boolean visit(MySqlShowSlaveHostsStatement x); - - void endVisit(MySqlShowSlaveHostsStatement x); - - boolean visit(MySqlShowSlaveStatusStatement x); - - void endVisit(MySqlShowSlaveStatusStatement x); - - boolean visit(MySqlShowTableStatusStatement x); - - void endVisit(MySqlShowTableStatusStatement x); - - boolean visit(MySqlShowTriggersStatement x); - - void endVisit(MySqlShowTriggersStatement x); - - boolean visit(MySqlShowVariantsStatement x); - - void endVisit(MySqlShowVariantsStatement x); - - boolean visit(MySqlAlterTableStatement x); - - void endVisit(MySqlAlterTableStatement x); - - boolean visit(MySqlAlterTableAddColumn x); - - void endVisit(MySqlAlterTableAddColumn x); - - boolean visit(MySqlCreateIndexStatement x); - - void endVisit(MySqlCreateIndexStatement x); - - boolean visit(MySqlRenameTableStatement.Item x); - - void endVisit(MySqlRenameTableStatement.Item x); - - boolean visit(MySqlRenameTableStatement x); - - void endVisit(MySqlRenameTableStatement x); - - boolean visit(MySqlDropViewStatement x); - - void endVisit(MySqlDropViewStatement x); - - boolean visit(MySqlUnionQuery x); - - void endVisit(MySqlUnionQuery x); - - boolean visit(MySqlUseIndexHint x); - - void endVisit(MySqlUseIndexHint x); - - boolean visit(MySqlIgnoreIndexHint x); - - void endVisit(MySqlIgnoreIndexHint x); - - boolean visit(MySqlLockTableStatement x); - - void endVisit(MySqlLockTableStatement x); - - boolean visit(MySqlUnlockTablesStatement x); - - void endVisit(MySqlUnlockTablesStatement x); - - boolean visit(MySqlForceIndexHint x); - - void endVisit(MySqlForceIndexHint x); - - boolean visit(MySqlAlterTableChangeColumn x); - - void endVisit(MySqlAlterTableChangeColumn x); - - boolean visit(MySqlAlterTableCharacter x); - - void endVisit(MySqlAlterTableCharacter x); - - boolean visit(MySqlAlterTableAddIndex x); - - void endVisit(MySqlAlterTableAddIndex x); - - boolean visit(MySqlAlterTableOption x); - - void endVisit(MySqlAlterTableOption x); - - boolean visit(MySqlCreateTableStatement x); - - void endVisit(MySqlCreateTableStatement x); - - boolean visit(MySqlHelpStatement x); - - void endVisit(MySqlHelpStatement x); - - boolean visit(MySqlCharExpr x); - - void endVisit(MySqlCharExpr x); - - boolean visit(MySqlAlterTableAddUnique x); - - void endVisit(MySqlAlterTableAddUnique x); -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlASTVisitorAdapter.java b/src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlASTVisitorAdapter.java deleted file mode 100644 index 2bec6fe9..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlASTVisitorAdapter.java +++ /dev/null @@ -1,1141 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.visitor; - -import org.durid.sql.dialect.mysql.ast.MySqlForceIndexHint; -import org.durid.sql.dialect.mysql.ast.MySqlIgnoreIndexHint; -import org.durid.sql.dialect.mysql.ast.MySqlKey; -import org.durid.sql.dialect.mysql.ast.MySqlPrimaryKey; -import org.durid.sql.dialect.mysql.ast.MySqlUseIndexHint; -import org.durid.sql.dialect.mysql.ast.expr.MySqlBinaryExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlBooleanExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlCharExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlExtractExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlIntervalExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlMatchAgainstExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlUserName; -import org.durid.sql.dialect.mysql.ast.statement.CobarShowStatus; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddColumn; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddIndex; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddUnique; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableCharacter; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableOption; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlBinlogStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlCommitStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlCreateIndexStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement.UserSpecification; -import org.durid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlDescribeStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlDropTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlDropUser; -import org.durid.sql.dialect.mysql.ast.statement.MySqlDropViewStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlExecuteStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlHelpStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlInsertStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlKillStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlLoadDataInFileStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlLoadXmlStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlPartitionByKey; -import org.durid.sql.dialect.mysql.ast.statement.MySqlPrepareStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlRenameTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlReplaceStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlResetStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlRollbackStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectGroupBy; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSetCharSetStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSetNamesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSetTransactionIsolationLevelStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowAuthorsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowBinLogEventsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowBinaryLogsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCharacterSetStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCollationStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowColumnsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowContributorsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateDatabaseStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateEventStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateFunctionStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateProcedureStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateTriggerStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateViewStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowDatabasesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowEngineStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowEnginesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowErrorsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowEventsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowFunctionCodeStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowFunctionStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowGrantsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowIndexesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowKeysStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowMasterStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowOpenTablesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowPluginsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowPrivilegesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowProcedureCodeStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowProcedureStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowProcessListStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowProfileStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowProfilesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowRelayLogEventsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowSlaveHostsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowSlaveStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowTableStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowTriggersStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowVariantsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlStartTransactionStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlTableIndex; -import org.durid.sql.dialect.mysql.ast.statement.MySqlUnionQuery; -import org.durid.sql.dialect.mysql.ast.statement.MySqlUnlockTablesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement; -import org.durid.sql.visitor.SQLASTVisitorAdapter; - -public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements - MySqlASTVisitor { - - @Override - public boolean visit(MySqlBooleanExpr x) { - return true; - } - - @Override - public void endVisit(MySqlBooleanExpr x) { - - } - - @Override - public boolean visit(Limit x) { - return true; - } - - @Override - public void endVisit(Limit x) { - - } - - @Override - public boolean visit(MySqlTableIndex x) { - return true; - } - - @Override - public void endVisit(MySqlTableIndex x) { - - } - - @Override - public boolean visit(MySqlKey x) { - return true; - } - - @Override - public void endVisit(MySqlKey x) { - - } - - @Override - public boolean visit(MySqlPrimaryKey x) { - - return true; - } - - @Override - public void endVisit(MySqlPrimaryKey x) { - - } - - @Override - public void endVisit(MySqlIntervalExpr x) { - - } - - @Override - public boolean visit(MySqlIntervalExpr x) { - - return true; - } - - @Override - public void endVisit(MySqlExtractExpr x) { - - } - - @Override - public boolean visit(MySqlExtractExpr x) { - - return true; - } - - @Override - public void endVisit(MySqlMatchAgainstExpr x) { - - } - - @Override - public boolean visit(MySqlMatchAgainstExpr x) { - - return true; - } - - @Override - public void endVisit(MySqlBinaryExpr x) { - - } - - @Override - public boolean visit(MySqlBinaryExpr x) { - - return true; - } - - @Override - public void endVisit(MySqlPrepareStatement x) { - - } - - @Override - public boolean visit(MySqlPrepareStatement x) { - - return true; - } - - @Override - public void endVisit(MySqlExecuteStatement x) { - - } - - @Override - public boolean visit(MySqlExecuteStatement x) { - - return true; - } - - @Override - public void endVisit(MySqlDeleteStatement x) { - - } - - @Override - public boolean visit(MySqlDeleteStatement x) { - - return true; - } - - @Override - public void endVisit(MySqlInsertStatement x) { - - } - - @Override - public boolean visit(MySqlInsertStatement x) { - - return true; - } - - @Override - public void endVisit(MySqlLoadDataInFileStatement x) { - - } - - @Override - public boolean visit(MySqlLoadDataInFileStatement x) { - - return true; - } - - @Override - public void endVisit(MySqlLoadXmlStatement x) { - - } - - @Override - public boolean visit(MySqlLoadXmlStatement x) { - - return true; - } - - @Override - public void endVisit(MySqlReplaceStatement x) { - - } - - @Override - public boolean visit(MySqlReplaceStatement x) { - - return true; - } - - @Override - public void endVisit(MySqlSelectGroupBy x) { - - } - - @Override - public boolean visit(MySqlSelectGroupBy x) { - - return true; - } - - @Override - public void endVisit(MySqlStartTransactionStatement x) { - - } - - @Override - public boolean visit(MySqlStartTransactionStatement x) { - - return true; - } - - @Override - public void endVisit(MySqlCommitStatement x) { - - } - - @Override - public boolean visit(MySqlCommitStatement x) { - - return true; - } - - @Override - public void endVisit(MySqlRollbackStatement x) { - - } - - @Override - public boolean visit(MySqlRollbackStatement x) { - - return true; - } - - @Override - public void endVisit(MySqlShowColumnsStatement x) { - - } - - @Override - public boolean visit(MySqlShowColumnsStatement x) { - - return true; - } - - @Override - public void endVisit(MySqlShowTablesStatement x) { - - } - - @Override - public boolean visit(MySqlShowTablesStatement x) { - - return true; - } - - @Override - public void endVisit(MySqlShowDatabasesStatement x) { - - } - - @Override - public boolean visit(MySqlShowDatabasesStatement x) { - - return true; - } - - @Override - public void endVisit(MySqlShowWarningsStatement x) { - - } - - @Override - public boolean visit(MySqlShowWarningsStatement x) { - - return true; - } - - @Override - public void endVisit(MySqlShowStatusStatement x) { - - } - - @Override - public boolean visit(MySqlShowStatusStatement x) { - - return true; - } - - @Override - public void endVisit(CobarShowStatus x) { - - } - - @Override - public boolean visit(CobarShowStatus x) { - return true; - } - - @Override - public void endVisit(MySqlKillStatement x) { - - } - - @Override - public boolean visit(MySqlKillStatement x) { - return true; - } - - @Override - public void endVisit(MySqlBinlogStatement x) { - - } - - @Override - public boolean visit(MySqlBinlogStatement x) { - return true; - } - - @Override - public void endVisit(MySqlResetStatement x) { - - } - - @Override - public boolean visit(MySqlResetStatement x) { - return true; - } - - @Override - public void endVisit(MySqlCreateUserStatement x) { - - } - - @Override - public boolean visit(MySqlCreateUserStatement x) { - return true; - } - - @Override - public void endVisit(UserSpecification x) { - - } - - @Override - public boolean visit(UserSpecification x) { - return true; - } - - @Override - public void endVisit(MySqlDropUser x) { - - } - - @Override - public boolean visit(MySqlDropUser x) { - return true; - } - - @Override - public void endVisit(MySqlDropTableStatement x) { - - } - - @Override - public boolean visit(MySqlDropTableStatement x) { - return true; - } - - @Override - public void endVisit(MySqlPartitionByKey x) { - - } - - @Override - public boolean visit(MySqlPartitionByKey x) { - return true; - } - - @Override - public boolean visit(MySqlSelectQueryBlock x) { - return true; - } - - @Override - public void endVisit(MySqlSelectQueryBlock x) { - - } - - @Override - public boolean visit(MySqlOutFileExpr x) { - return true; - } - - @Override - public void endVisit(MySqlOutFileExpr x) { - - } - - @Override - public boolean visit(MySqlDescribeStatement x) { - return true; - } - - @Override - public void endVisit(MySqlDescribeStatement x) { - - } - - @Override - public boolean visit(MySqlUpdateStatement x) { - return true; - } - - @Override - public void endVisit(MySqlUpdateStatement x) { - - } - - @Override - public boolean visit(MySqlSetTransactionIsolationLevelStatement x) { - return true; - } - - @Override - public void endVisit(MySqlSetTransactionIsolationLevelStatement x) { - - } - - @Override - public boolean visit(MySqlSetNamesStatement x) { - return true; - } - - @Override - public void endVisit(MySqlSetNamesStatement x) { - - } - - @Override - public boolean visit(MySqlSetCharSetStatement x) { - return true; - } - - @Override - public void endVisit(MySqlSetCharSetStatement x) { - - } - - @Override - public boolean visit(MySqlShowAuthorsStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowAuthorsStatement x) { - - } - - @Override - public boolean visit(MySqlShowBinaryLogsStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowBinaryLogsStatement x) { - - } - - @Override - public boolean visit(MySqlShowMasterLogsStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowMasterLogsStatement x) { - - } - - @Override - public boolean visit(MySqlShowCollationStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowCollationStatement x) { - - } - - @Override - public boolean visit(MySqlShowBinLogEventsStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowBinLogEventsStatement x) { - - } - - @Override - public boolean visit(MySqlShowCharacterSetStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowCharacterSetStatement x) { - - } - - @Override - public boolean visit(MySqlShowContributorsStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowContributorsStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateDatabaseStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowCreateDatabaseStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateEventStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowCreateEventStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateFunctionStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowCreateFunctionStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateProcedureStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowCreateProcedureStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateTableStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowCreateTableStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateTriggerStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowCreateTriggerStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateViewStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowCreateViewStatement x) { - - } - - @Override - public boolean visit(MySqlShowEngineStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowEngineStatement x) { - - } - - @Override - public boolean visit(MySqlShowEnginesStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowEnginesStatement x) { - - } - - @Override - public boolean visit(MySqlShowErrorsStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowErrorsStatement x) { - - } - - @Override - public boolean visit(MySqlShowEventsStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowEventsStatement x) { - - } - - @Override - public boolean visit(MySqlShowFunctionCodeStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowFunctionCodeStatement x) { - - } - - @Override - public boolean visit(MySqlShowFunctionStatusStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowFunctionStatusStatement x) { - - } - - @Override - public boolean visit(MySqlShowGrantsStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowGrantsStatement x) { - } - - @Override - public boolean visit(MySqlUserName x) { - return true; - } - - @Override - public void endVisit(MySqlUserName x) { - - } - - @Override - public boolean visit(MySqlShowIndexesStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowIndexesStatement x) { - - } - - @Override - public boolean visit(MySqlShowKeysStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowKeysStatement x) { - - } - - @Override - public boolean visit(MySqlShowMasterStatusStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowMasterStatusStatement x) { - - } - - @Override - public boolean visit(MySqlShowOpenTablesStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowOpenTablesStatement x) { - - } - - @Override - public boolean visit(MySqlShowPluginsStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowPluginsStatement x) { - - } - - @Override - public boolean visit(MySqlShowPrivilegesStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowPrivilegesStatement x) { - - } - - @Override - public boolean visit(MySqlShowProcedureCodeStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowProcedureCodeStatement x) { - - } - - @Override - public boolean visit(MySqlShowProcedureStatusStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowProcedureStatusStatement x) { - - } - - @Override - public boolean visit(MySqlShowProcessListStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowProcessListStatement x) { - - } - - @Override - public boolean visit(MySqlShowProfileStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowProfileStatement x) { - - } - - @Override - public boolean visit(MySqlShowProfilesStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowProfilesStatement x) { - - } - - @Override - public boolean visit(MySqlShowRelayLogEventsStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowRelayLogEventsStatement x) { - - } - - @Override - public boolean visit(MySqlShowSlaveHostsStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowSlaveHostsStatement x) { - - } - - @Override - public boolean visit(MySqlShowSlaveStatusStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowSlaveStatusStatement x) { - - } - - @Override - public boolean visit(MySqlShowTableStatusStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowTableStatusStatement x) { - - } - - @Override - public boolean visit(MySqlShowTriggersStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowTriggersStatement x) { - - } - - @Override - public boolean visit(MySqlShowVariantsStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowVariantsStatement x) { - - } - - @Override - public boolean visit(MySqlAlterTableStatement x) { - return true; - } - - @Override - public void endVisit(MySqlAlterTableStatement x) { - - } - - @Override - public boolean visit(MySqlAlterTableAddColumn x) { - return true; - } - - @Override - public void endVisit(MySqlAlterTableAddColumn x) { - - } - - @Override - public boolean visit(MySqlCreateIndexStatement x) { - return true; - } - - @Override - public void endVisit(MySqlCreateIndexStatement x) { - - } - - @Override - public boolean visit(MySqlRenameTableStatement.Item x) { - return true; - } - - @Override - public void endVisit(MySqlRenameTableStatement.Item x) { - - } - - @Override - public boolean visit(MySqlRenameTableStatement x) { - return true; - } - - @Override - public void endVisit(MySqlRenameTableStatement x) { - - } - - @Override - public boolean visit(MySqlDropViewStatement x) { - return true; - } - - @Override - public void endVisit(MySqlDropViewStatement x) { - - } - - @Override - public boolean visit(MySqlUnionQuery x) { - return true; - } - - @Override - public void endVisit(MySqlUnionQuery x) { - - } - - @Override - public boolean visit(MySqlUseIndexHint x) { - return true; - } - - @Override - public void endVisit(MySqlUseIndexHint x) { - - } - - @Override - public boolean visit(MySqlIgnoreIndexHint x) { - return true; - } - - @Override - public void endVisit(MySqlIgnoreIndexHint x) { - - } - - @Override - public boolean visit(MySqlLockTableStatement x) { - return true; - } - - @Override - public void endVisit(MySqlLockTableStatement x) { - - } - - @Override - public boolean visit(MySqlUnlockTablesStatement x) { - return true; - } - - @Override - public void endVisit(MySqlUnlockTablesStatement x) { - - } - - @Override - public boolean visit(MySqlForceIndexHint x) { - return true; - } - - @Override - public void endVisit(MySqlForceIndexHint x) { - - } - - @Override - public boolean visit(MySqlAlterTableChangeColumn x) { - return true; - } - - @Override - public void endVisit(MySqlAlterTableChangeColumn x) { - - } - - @Override - public boolean visit(MySqlAlterTableCharacter x) { - return true; - } - - @Override - public void endVisit(MySqlAlterTableCharacter x) { - - } - - @Override - public boolean visit(MySqlAlterTableAddIndex x) { - return true; - } - - @Override - public void endVisit(MySqlAlterTableAddIndex x) { - - } - - @Override - public boolean visit(MySqlAlterTableOption x) { - return true; - } - - @Override - public void endVisit(MySqlAlterTableOption x) { - - } - - @Override - public boolean visit(MySqlCreateTableStatement x) { - return true; - } - - @Override - public void endVisit(MySqlCreateTableStatement x) { - - } - - @Override - public boolean visit(MySqlHelpStatement x) { - return true; - } - - @Override - public void endVisit(MySqlHelpStatement x) { - - } - - @Override - public boolean visit(MySqlCharExpr x) { - return true; - } - - @Override - public void endVisit(MySqlCharExpr x) { - - } - - @Override - public boolean visit(MySqlAlterTableAddUnique x) { - return true; - } - - @Override - public void endVisit(MySqlAlterTableAddUnique x) { - - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlEvalVisitorImpl.java b/src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlEvalVisitorImpl.java deleted file mode 100644 index 8725f025..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlEvalVisitorImpl.java +++ /dev/null @@ -1,132 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.visitor; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.expr.SQLBetweenExpr; -import org.durid.sql.ast.expr.SQLBinaryOpExpr; -import org.durid.sql.ast.expr.SQLCaseExpr; -import org.durid.sql.ast.expr.SQLCharExpr; -import org.durid.sql.ast.expr.SQLInListExpr; -import org.durid.sql.ast.expr.SQLIntegerExpr; -import org.durid.sql.ast.expr.SQLMethodInvokeExpr; -import org.durid.sql.ast.expr.SQLNullExpr; -import org.durid.sql.ast.expr.SQLNumberExpr; -import org.durid.sql.ast.expr.SQLQueryExpr; -import org.durid.sql.ast.expr.SQLVariantRefExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlBooleanExpr; -import org.durid.sql.visitor.SQLEvalVisitor; -import org.durid.sql.visitor.SQLEvalVisitorUtils; - -public class MySqlEvalVisitorImpl extends MySqlASTVisitorAdapter implements SQLEvalVisitor { - - private List parameters = new ArrayList(); - - private int variantIndex = -1; - - private boolean markVariantIndex = true; - - public MySqlEvalVisitorImpl(){ - this(new ArrayList(1)); - } - - public MySqlEvalVisitorImpl(List parameters){ - this.parameters = parameters; - } - - public List getParameters() { - return parameters; - } - - public void setParameters(List parameters) { - this.parameters = parameters; - } - - public boolean visit(SQLCharExpr x) { - return SQLEvalVisitorUtils.visit(this, x); - } - - public int incrementAndGetVariantIndex() { - return ++variantIndex; - } - - public int getVariantIndex() { - return variantIndex; - } - - public boolean visit(SQLVariantRefExpr x) { - return SQLEvalVisitorUtils.visit(this, x); - } - - public boolean visit(SQLBinaryOpExpr x) { - return SQLEvalVisitorUtils.visit(this, x); - } - - public boolean visit(SQLIntegerExpr x) { - return SQLEvalVisitorUtils.visit(this, x); - } - - public boolean visit(SQLNumberExpr x) { - return SQLEvalVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLCaseExpr x) { - return SQLEvalVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLBetweenExpr x) { - return SQLEvalVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLInListExpr x) { - return SQLEvalVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLNullExpr x) { - return SQLEvalVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLMethodInvokeExpr x) { - return SQLEvalVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLQueryExpr x) { - return SQLEvalVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(MySqlBooleanExpr x) { - x.getAttributes().put(EVAL_VALUE, x.getValue()); - return false; - } - - public boolean isMarkVariantIndex() { - return markVariantIndex; - } - - public void setMarkVariantIndex(boolean markVariantIndex) { - this.markVariantIndex = markVariantIndex; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlExportParameterVisitor.java b/src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlExportParameterVisitor.java deleted file mode 100644 index bc11aeda..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlExportParameterVisitor.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.visitor; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLOrderBy; -import org.durid.sql.ast.expr.SQLBetweenExpr; -import org.durid.sql.ast.expr.SQLBinaryOpExpr; -import org.durid.sql.ast.expr.SQLInListExpr; -import org.durid.sql.ast.expr.SQLMethodInvokeExpr; -import org.durid.sql.ast.statement.SQLSelectGroupByClause; -import org.durid.sql.ast.statement.SQLSelectItem; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit; -import org.durid.sql.visitor.ExportParameterVisitor; -import org.durid.sql.visitor.ExportParameterVisitorUtils; - -public class MySqlExportParameterVisitor extends MySqlASTVisitorAdapter implements ExportParameterVisitor { - - private final List parameters; - - public MySqlExportParameterVisitor() { - this(new ArrayList()); - } - - public MySqlExportParameterVisitor(List parameters){ - this.parameters = parameters; - } - - public List getParameters() { - return parameters; - } - - @Override - public boolean visit(SQLSelectItem x) { - return false; - } - - @Override - public boolean visit(Limit x) { - return false; - } - - @Override - public boolean visit(SQLOrderBy x) { - return false; - } - - @Override - public boolean visit(SQLSelectGroupByClause x) { - return false; - } - - @Override - public boolean visit(SQLMethodInvokeExpr x) { - ExportParameterVisitorUtils.exportParamterAndAccept(this.parameters, x.getParameters()); - - return true; - } - - @Override - public boolean visit(SQLInListExpr x) { - ExportParameterVisitorUtils.exportParamterAndAccept(this.parameters, x.getTargetList()); - - return true; - } - - @Override - public boolean visit(SQLBetweenExpr x) { - ExportParameterVisitorUtils.exportParameter(this.parameters, x); - return true; - } - - public boolean visit(SQLBinaryOpExpr x) { - ExportParameterVisitorUtils.exportParameter(this.parameters, x); - return true; - } - -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java b/src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java deleted file mode 100644 index d7fb88b9..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java +++ /dev/null @@ -1,2679 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.visitor; - -import java.util.Map; - -import org.durid.sql.ast.SQLCommentHint; -import org.durid.sql.ast.SQLDataType; -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLSetQuantifier; -import org.durid.sql.ast.expr.SQLCharExpr; -import org.durid.sql.ast.expr.SQLMethodInvokeExpr; -import org.durid.sql.ast.expr.SQLNullExpr; -import org.durid.sql.ast.expr.SQLVariantRefExpr; -import org.durid.sql.ast.statement.SQLAlterTableItem; -import org.durid.sql.ast.statement.SQLCharactorDataType; -import org.durid.sql.ast.statement.SQLColumnConstraint; -import org.durid.sql.ast.statement.SQLColumnDefinition; -import org.durid.sql.ast.statement.SQLCreateTableStatement; -import org.durid.sql.ast.statement.SQLExprTableSource; -import org.durid.sql.ast.statement.SQLSelectQueryBlock; -import org.durid.sql.dialect.mysql.ast.MySqlForceIndexHint; -import org.durid.sql.dialect.mysql.ast.MySqlIgnoreIndexHint; -import org.durid.sql.dialect.mysql.ast.MySqlKey; -import org.durid.sql.dialect.mysql.ast.MySqlPrimaryKey; -import org.durid.sql.dialect.mysql.ast.MySqlUseIndexHint; -import org.durid.sql.dialect.mysql.ast.expr.MySqlBinaryExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlBooleanExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlCharExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlExtractExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlIntervalExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlMatchAgainstExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlUserName; -import org.durid.sql.dialect.mysql.ast.statement.CobarShowStatus; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddColumn; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddIndex; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddUnique; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableCharacter; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableOption; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlBinlogStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlCommitStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlCreateIndexStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement.UserSpecification; -import org.durid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlDescribeStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlDropTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlDropUser; -import org.durid.sql.dialect.mysql.ast.statement.MySqlDropViewStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlExecuteStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlHelpStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlInsertStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlKillStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlLoadDataInFileStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlLoadXmlStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlPartitionByKey; -import org.durid.sql.dialect.mysql.ast.statement.MySqlPrepareStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlRenameTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlReplaceStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlResetStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlRollbackStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSQLColumnDefinition; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectGroupBy; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSetCharSetStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSetNamesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSetTransactionIsolationLevelStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowAuthorsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowBinLogEventsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowBinaryLogsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCharacterSetStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCollationStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowColumnsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowContributorsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateDatabaseStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateEventStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateFunctionStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateProcedureStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateTriggerStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateViewStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowDatabasesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowEngineStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowEnginesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowErrorsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowEventsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowFunctionCodeStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowFunctionStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowGrantsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowIndexesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowKeysStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowMasterStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowOpenTablesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowPluginsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowPrivilegesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowProcedureCodeStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowProcedureStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowProcessListStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowProfileStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowProfilesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowRelayLogEventsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowSlaveHostsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowSlaveStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowTableStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowTriggersStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowVariantsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlStartTransactionStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlTableIndex; -import org.durid.sql.dialect.mysql.ast.statement.MySqlUnionQuery; -import org.durid.sql.dialect.mysql.ast.statement.MySqlUnlockTablesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement; -import org.durid.sql.visitor.SQLASTOutputVisitor; - -public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTVisitor { - - public MySqlOutputVisitor(Appendable appender){ - super(appender); - } - - public boolean visit(MySqlBooleanExpr x) { - print(x.getValue() ? "true" : "false"); - - return false; - } - - public void endVisit(MySqlBooleanExpr x) { - } - - @Override - public boolean visit(SQLSelectQueryBlock select) { - if (select instanceof MySqlSelectQueryBlock) { - return visit((MySqlSelectQueryBlock) select); - } - - return false; - } - - public boolean visit(MySqlSelectQueryBlock x) { - if (x.getOrderBy() != null) { - x.getOrderBy().setParent(x); - } - - print("SELECT "); - - for (SQLCommentHint hint : x.getHints()) { - hint.accept(this); - print(' '); - } - - if (SQLSetQuantifier.ALL == x.getDistionOption()) { - print("ALL "); - } else if (SQLSetQuantifier.DISTINCT == x.getDistionOption()) { - print("DISTINCT "); - } else if (SQLSetQuantifier.DISTINCTROW == x.getDistionOption()) { - print("DISTINCTROW "); - } - - if (x.isHignPriority()) { - print("HIGH_PRIORITY "); - } - - if (x.isStraightJoin()) { - print("STRAIGHT_JOIN "); - } - - if (x.isSmallResult()) { - print("SQL_SMALL_RESULT "); - } - - if (x.isBigResult()) { - print("SQL_BIG_RESULT "); - } - - if (x.isBufferResult()) { - print("SQL_BUFFER_RESULT "); - } - - if (x.getCache() != null) { - if (x.getCache().booleanValue()) { - print("SQL_CACHE "); - } else { - print("SQL_NO_CACHE "); - } - } - - if (x.isCalcFoundRows()) { - print("SQL_CALC_FOUND_ROWS "); - } - - printSelectList(x.getSelectList()); - - if (x.getInto() != null) { - println(); - print("INTO "); - x.getInto().accept(this); - } - - if (x.getFrom() != null) { - println(); - print("FROM "); - x.getFrom().accept(this); - } - - if (x.getWhere() != null) { - println(); - print("WHERE "); - x.getWhere().setParent(x); - x.getWhere().accept(this); - } - - if (x.getGroupBy() != null) { - println(); - x.getGroupBy().accept(this); - } - - if (x.getOrderBy() != null) { - println(); - x.getOrderBy().accept(this); - } - - if (x.getLimit() != null) { - println(); - x.getLimit().accept(this); - } - - if (x.getProcedureName() != null) { - print(" PROCEDURE "); - x.getProcedureName().accept(this); - if (x.getProcedureArgumentList().size() > 0) { - print("("); - printAndAccept(x.getProcedureArgumentList(), ", "); - print(")"); - } - } - - if (x.isForUpdate()) { - println(); - print("FOR UPDATE"); - } - - if (x.isLockInShareMode()) { - println(); - print("LOCK IN SHARE MODE"); - } - - return false; - } - - public boolean visit(SQLColumnDefinition x) { - MySqlSQLColumnDefinition mysqlColumn = null; - - if (x instanceof MySqlSQLColumnDefinition) { - mysqlColumn = (MySqlSQLColumnDefinition) x; - } - - x.getName().accept(this); - print(' '); - x.getDataType().accept(this); - - if (x.getDefaultExpr() != null) { - if (x.getDefaultExpr() instanceof SQLNullExpr) { - print(" NULL"); - } else { - print(" DEFAULT"); - x.getDefaultExpr().accept(this); - } - } - - if (mysqlColumn != null && mysqlColumn.isAutoIncrement()) { - print(" AUTO_INCREMENT"); - } - - for (SQLColumnConstraint item : x.getConstaints()) { - print(' '); - item.accept(this); - } - - return false; - } - - public boolean visit(MySqlSelectQueryBlock.Limit x) { - print("LIMIT "); - if (x.getOffset() != null) { - x.getOffset().accept(this); - print(", "); - } - x.getRowCount().accept(this); - - return false; - } - - public boolean visit(SQLDataType x) { - print(x.getName()); - if (x.getArguments().size() > 0) { - print("("); - printAndAccept(x.getArguments(), ", "); - print(")"); - } - - if (x instanceof SQLCharactorDataType) { - SQLCharactorDataType charType = (SQLCharactorDataType) x; - if (charType.getCharSetName() != null) { - print(" CHARACTER SET "); - print(charType.getCharSetName()); - - if (charType.getCollate() != null) { - print(" COLLATE "); - print(charType.getCollate()); - } - } - } - return false; - } - - @Override - public void endVisit(Limit x) { - - } - - @Override - public void endVisit(MySqlTableIndex x) { - - } - - @Override - public boolean visit(MySqlTableIndex x) { - print("INDEX"); - if (x.getName() != null) { - print(" "); - x.getName().accept(this); - } - - if (x.getIndexType() != null) { - print(" USING "); - print(x.getIndexType()); - } - - print("("); - for (int i = 0, size = x.getColumns().size(); i < size; ++i) { - if (i != 0) { - print(", "); - } - x.getColumns().get(i).accept(this); - } - print(")"); - return false; - } - - public boolean visit(MySqlCreateTableStatement x) { - - print("CREATE "); - - for (SQLCommentHint hint : x.getHints()) { - hint.accept(this); - print(' '); - } - - if (SQLCreateTableStatement.Type.GLOBAL_TEMPORARY.equals(x.getType())) { - print("TEMPORARY TABLE "); - } else { - print("TABLE "); - } - - if (x.isIfNotExiists()) { - print("IF NOT EXISTS "); - } - - x.getName().accept(this); - print(" ("); - incrementIndent(); - println(); - for (int i = 0, size = x.getTableElementList().size(); i < size; ++i) { - if (i != 0) { - print(", "); - println(); - } - x.getTableElementList().get(i).accept(this); - } - decrementIndent(); - println(); - print(")"); - - for (Map.Entry option : x.getTableOptions().entrySet()) { - print(" "); - print(option.getKey()); - print(" = "); - print(option.getValue()); - } - - if (x.getQuery() != null) { - print(" "); - incrementIndent(); - println(); - x.getQuery().accept(this); - decrementIndent(); - } - - return false; - } - - @Override - public void endVisit(MySqlKey x) { - - } - - @Override - public void endVisit(MySqlPrimaryKey x) { - - } - - @Override - public boolean visit(MySqlKey x) { - if (x.getName() != null) { - print("CONSTRAINT "); - x.accept(this); - print(' '); - } - - print("KEY"); - - if (x.getIndexType() != null) { - print(" USING "); - print(x.getIndexType()); - } - - print(" ("); - - for (int i = 0, size = x.getColumns().size(); i < size; ++i) { - if (i != 0) { - print(", "); - } - x.getColumns().get(i).accept(this); - } - print(")"); - - return false; - } - - @Override - public boolean visit(MySqlPrimaryKey x) { - if (x.getName() != null) { - print("CONSTRAINT "); - x.accept(this); - print(' '); - } - - print("PRIAMRY KEY"); - - if (x.getIndexType() != null) { - print(" USING "); - print(x.getIndexType()); - } - - print(" ("); - - for (int i = 0, size = x.getColumns().size(); i < size; ++i) { - if (i != 0) { - print(", "); - } - x.getColumns().get(i).accept(this); - } - print(")"); - - return false; - } - - public boolean visit(SQLCharExpr x) { - print('\''); - - String text = x.getText(); - text = text.replaceAll("'", "''"); - text = text.replaceAll("\\\\", "\\\\"); - - print(text); - - print('\''); - return false; - } - - public boolean visit(SQLVariantRefExpr x) { - if (x.isGlobal()) { - print("@@global."); - } else { - String varName = x.getName(); - if ((!varName.startsWith("@")) && (!varName.equals("?")) && (!varName.startsWith("#")) - && (!varName.startsWith("$"))) { - print("@@"); - } - } - - for (int i = 0; i < x.getName().length(); ++i) { - char ch = x.getName().charAt(i); - if (ch == '\'') { - if (x.getName().startsWith("@@") && i == 2) { - print(ch); - } else if (x.getName().startsWith("@") && i == 1) { - print(ch); - } else if (i != 0 && i != x.getName().length() - 1) { - print("\\'"); - } else { - print(ch); - } - } else { - print(ch); - } - } - - String collate = (String) x.getAttribute("COLLATE"); - if (collate != null) { - print(" COLLATE "); - print(collate); - } - - return false; - } - - public boolean visit(SQLMethodInvokeExpr x) { - if ("SUBSTRING".equalsIgnoreCase(x.getMethodName())) { - if (x.getOwner() != null) { - x.getOwner().accept(this); - print("."); - } - print(x.getMethodName()); - print("("); - printAndAccept(x.getParameters(), ", "); - SQLExpr from = (SQLExpr) x.getAttribute("FROM"); - if (from != null) { - print(" FROM "); - from.accept(this); - } - - SQLExpr forExpr = (SQLExpr) x.getAttribute("FOR"); - if (forExpr != null) { - print(" FOR "); - forExpr.accept(this); - } - print(")"); - - return false; - } - - if ("TRIM".equalsIgnoreCase(x.getMethodName())) { - if (x.getOwner() != null) { - x.getOwner().accept(this); - print("."); - } - print(x.getMethodName()); - print("("); - - String trimType = (String) x.getAttribute("TRIM_TYPE"); - if (trimType != null) { - print(trimType); - print(' '); - } - - printAndAccept(x.getParameters(), ", "); - - SQLExpr from = (SQLExpr) x.getAttribute("FROM"); - if (from != null) { - print(" FROM "); - from.accept(this); - } - - print(")"); - - return false; - } - - if ("CONVERT".equalsIgnoreCase(x.getMethodName())) { - if (x.getOwner() != null) { - x.getOwner().accept(this); - print("."); - } - print(x.getMethodName()); - print("("); - printAndAccept(x.getParameters(), ", "); - - String charset = (String) x.getAttribute("USING"); - if (charset != null) { - print(" USING "); - print(charset); - } - print(")"); - return false; - } - - return super.visit(x); - } - - @Override - public void endVisit(MySqlIntervalExpr x) { - - } - - @Override - public boolean visit(MySqlIntervalExpr x) { - print("INTERVAL "); - x.getValue().accept(this); - print(' '); - print(x.getUnit().name()); - return false; - } - - @Override - public boolean visit(MySqlExtractExpr x) { - print("EXTRACT("); - print(x.getUnit().name()); - print(" FROM "); - x.getValue().accept(this); - print(')'); - return false; - } - - @Override - public void endVisit(MySqlExtractExpr x) { - - } - - @Override - public void endVisit(MySqlMatchAgainstExpr x) { - - } - - @Override - public boolean visit(MySqlMatchAgainstExpr x) { - print("MATCH ("); - printAndAccept(x.getColumns(), ", "); - print(")"); - - print(" AGAINST ("); - x.getAgainst().accept(this); - if (x.getSearchModifier() != null) { - print(' '); - print(x.getSearchModifier().name); - } - print(')'); - - return false; - } - - @Override - public void endVisit(MySqlBinaryExpr x) { - - } - - @Override - public boolean visit(MySqlBinaryExpr x) { - print("b'"); - print(x.getValue()); - print('\''); - - return false; - } - - @Override - public void endVisit(MySqlPrepareStatement x) { - } - - @Override - public boolean visit(MySqlPrepareStatement x) { - print("PREPARE "); - x.getName().accept(this); - print(" FROM "); - x.getFrom().accept(this); - return false; - } - - @Override - public void endVisit(MySqlExecuteStatement x) { - - } - - @Override - public boolean visit(MySqlExecuteStatement x) { - print("EXECUTE "); - x.getStatementName().accept(this); - if (x.getParameters().size() > 0) { - print(" USING "); - printAndAccept(x.getParameters(), ", "); - } - return false; - } - - @Override - public void endVisit(MySqlDeleteStatement x) { - - } - - @Override - public boolean visit(MySqlDeleteStatement x) { - print("DELETE "); - - if (x.isLowPriority()) { - print("LOW_PRIORITY "); - } - - if (x.isQuick()) { - print("QUICK "); - } - - if (x.isIgnore()) { - print("IGNORE "); - } - - if (x.getFrom() == null) { - print("FROM "); - x.getTableSource().accept(this); - } else { - x.getTableSource().accept(this); - println(); - print("FROM "); - x.getFrom().accept(this); - } - - if (x.getUsing() != null) { - println(); - print("USING "); - x.getUsing().accept(this); - } - - if (x.getWhere() != null) { - println(); - incrementIndent(); - print("WHERE "); - x.getWhere().setParent(x); - x.getWhere().accept(this); - decrementIndent(); - } - - if (x.getOrderBy() != null) { - println(); - x.getOrderBy().accept(this); - } - - if (x.getLimit() != null) { - println(); - x.getLimit().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlInsertStatement x) { - - } - - @Override - public boolean visit(MySqlInsertStatement x) { - print("INSERT "); - - if (x.isLowPriority()) { - print("LOW_PRIORITY "); - } - - if (x.isDelayed()) { - print("DELAYED "); - } - - if (x.isHighPriority()) { - print("HIGH_PRIORITY "); - } - - if (x.isIgnore()) { - print("IGNORE "); - } - - print("INTO "); - - x.getTableName().accept(this); - - if (x.getColumns().size() > 0) { - incrementIndent(); - print(" ("); - for (int i = 0, size = x.getColumns().size(); i < size; ++i) { - if (i != 0) { - if (i % 5 == 0) { - println(); - } - print(", "); - } - - x.getColumns().get(i).accept(this); - } - print(")"); - decrementIndent(); - } - - if (x.getValuesList().size() != 0) { - println(); - print("VALUES "); - if (x.getValuesList().size() > 1) { - incrementIndent(); - } - for (int i = 0, size = x.getValuesList().size(); i < size; ++i) { - if (i != 0) { - print(","); - println(); - } - x.getValuesList().get(i).accept(this); - } - if (x.getValuesList().size() > 1) { - decrementIndent(); - } - } - - if (x.getQuery() != null) { - println(); - x.getQuery().accept(this); - } - - if (x.getDuplicateKeyUpdate().size() != 0) { - println(); - print("ON DUPLICATE KEY UPDATE "); - for (int i = 0, size = x.getDuplicateKeyUpdate().size(); i < size; ++i) { - if (i != 0) { - if (i % 5 == 0) { - println(); - } - print(", "); - } - x.getDuplicateKeyUpdate().get(i).accept(this); - } - } - - return false; - } - - @Override - public void endVisit(MySqlLoadDataInFileStatement x) { - - } - - @Override - public boolean visit(MySqlLoadDataInFileStatement x) { - print("LOAD DATA "); - - if (x.isLowPriority()) { - print("LOW_PRIORITY "); - } - - if (x.isConcurrent()) { - print("CONCURRENT "); - } - - if (x.isLocal()) { - print("LOCAL "); - } - - print("INFILE "); - - x.getFileName().accept(this); - - if (x.isReplicate()) { - print(" REPLACE "); - } - - if (x.isIgnore()) { - print(" IGNORE "); - } - - print(" INTO TABLE "); - x.getTableName().accept(this); - - if (x.getColumnsTerminatedBy() != null || x.getColumnsEnclosedBy() != null || x.getColumnsEscaped() != null) { - print(" COLUMNS"); - if (x.getColumnsTerminatedBy() != null) { - print(" TERMINATED BY "); - x.getColumnsTerminatedBy().accept(this); - } - - if (x.getColumnsEnclosedBy() != null) { - if (x.isColumnsEnclosedOptionally()) { - print(" OPTIONALLY"); - } - print(" ENCLOSED BY "); - x.getColumnsEnclosedBy().accept(this); - } - - if (x.getColumnsEscaped() != null) { - print(" ESCAPED BY "); - x.getColumnsEscaped().accept(this); - } - } - - if (x.getLinesStartingBy() != null || x.getLinesTerminatedBy() != null) { - print(" LINES"); - if (x.getLinesStartingBy() != null) { - print(" STARTING BY "); - x.getLinesStartingBy().accept(this); - } - - if (x.getLinesTerminatedBy() != null) { - print(" TERMINATED BY "); - x.getLinesTerminatedBy().accept(this); - } - } - - if (x.getSetList().size() != 0) { - print(" SET "); - printAndAccept(x.getSetList(), ", "); - } - - return false; - } - - @Override - public void endVisit(MySqlReplaceStatement x) { - - } - - @Override - public boolean visit(MySqlReplaceStatement x) { - print("REPLACE "); - - if (x.isLowPriority()) { - print("LOW_PRIORITY "); - } - - if (x.isDelayed()) { - print("DELAYED "); - } - - print("INTO "); - - x.getTableName().accept(this); - - if (x.getColumns().size() > 0) { - print(" ("); - for (int i = 0, size = x.getColumns().size(); i < size; ++i) { - if (i != 0) { - print(", "); - } - x.getColumns().get(i).accept(this); - } - print(")"); - } - - if (x.getValuesList().size() != 0) { - println(); - print("VALUES "); - int size = x.getValuesList().size(); - if (size == 0) { - print("()"); - } else { - for (int i = 0; i < size; ++i) { - if (i != 0) { - print(", "); - } - x.getValuesList().get(i).accept(this); - } - } - } - - if (x.getQuery() != null) { - x.getQuery().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlSelectGroupBy x) { - - } - - @Override - public boolean visit(MySqlSelectGroupBy x) { - super.visit(x); - - if (x.isRollUp()) { - print(" WITH ROLLUP"); - } - - return false; - } - - @Override - public void endVisit(MySqlStartTransactionStatement x) { - - } - - @Override - public boolean visit(MySqlStartTransactionStatement x) { - print("START TRANSACTION"); - if (x.isConsistentSnapshot()) { - print(" WITH CONSISTENT SNAPSHOT"); - } - - if (x.isBegin()) { - print(" BEGIN"); - } - - if (x.isWork()) { - print(" WORK"); - } - - return false; - } - - @Override - public void endVisit(MySqlCommitStatement x) { - - } - - @Override - public boolean visit(MySqlCommitStatement x) { - print("COMMIT"); - - if (x.isWork()) { - print(" WORK"); - } - - if (x.getChain() != null) { - if (x.getChain().booleanValue()) { - print(" AND CHAIN"); - } else { - print(" AND NO CHAIN"); - } - } - - if (x.getRelease() != null) { - if (x.getRelease().booleanValue()) { - print(" AND RELEASE"); - } else { - print(" AND NO RELEASE"); - } - } - - return false; - } - - @Override - public void endVisit(MySqlRollbackStatement x) { - - } - - @Override - public boolean visit(MySqlRollbackStatement x) { - print("ROLLBACK"); - - if (x.getChain() != null) { - if (x.getChain().booleanValue()) { - print(" AND CHAIN"); - } else { - print(" AND NO CHAIN"); - } - } - - if (x.getRelease() != null) { - if (x.getRelease().booleanValue()) { - print(" AND RELEASE"); - } else { - print(" AND NO RELEASE"); - } - } - - if (x.getTo() != null) { - print(" TO "); - x.getTo().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlShowColumnsStatement x) { - - } - - @Override - public boolean visit(MySqlShowColumnsStatement x) { - if (x.isFull()) { - print("SHOW FULL COLUMNS"); - } else { - print("SHOW COLUMNS"); - } - - if (x.getTable() != null) { - print(" FROM "); - if (x.getDatabase() != null) { - x.getDatabase().accept(this); - print('.'); - } - x.getTable().accept(this); - } - - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().setParent(x); - x.getWhere().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlShowTablesStatement x) { - - } - - @Override - public boolean visit(MySqlShowTablesStatement x) { - if (x.isFull()) { - print("SHOW FULL TABLES"); - } else { - print("SHOW TABLES"); - } - - if (x.getDatabase() != null) { - print(" FROM "); - x.getDatabase().accept(this); - } - - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().setParent(x); - x.getWhere().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlShowDatabasesStatement x) { - - } - - @Override - public boolean visit(MySqlShowDatabasesStatement x) { - print("SHOW DATABASES"); - - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().setParent(x); - x.getWhere().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlShowWarningsStatement x) { - - } - - @Override - public boolean visit(MySqlShowWarningsStatement x) { - if (x.isCount()) { - print("SHOW COUNT(*) WARNINGS"); - } else { - print("SHOW WARNINGS"); - if (x.getLimit() != null) { - print(' '); - x.getLimit().accept(this); - } - } - - return false; - } - - @Override - public void endVisit(MySqlShowStatusStatement x) { - - } - - @Override - public boolean visit(MySqlShowStatusStatement x) { - print("SHOW "); - - if (x.isGlobal()) { - print("GLOBAL "); - } - - if (x.isSession()) { - print("SESSION "); - } - - print("STATUS"); - - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().setParent(x); - x.getWhere().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlLoadXmlStatement x) { - - } - - @Override - public boolean visit(MySqlLoadXmlStatement x) { - print("LOAD XML "); - - if (x.isLowPriority()) { - print("LOW_PRIORITY "); - } - - if (x.isConcurrent()) { - print("CONCURRENT "); - } - - if (x.isLocal()) { - print("LOCAL "); - } - - print("INFILE "); - - x.getFileName().accept(this); - - if (x.isReplicate()) { - print(" REPLACE "); - } - - if (x.isIgnore()) { - print(" IGNORE "); - } - - print(" INTO TABLE "); - x.getTableName().accept(this); - - if (x.getCharset() != null) { - print(" CHARSET "); - print(x.getCharset()); - } - - if (x.getRowsIdentifiedBy() != null) { - print(" ROWS IDENTIFIED BY "); - x.getRowsIdentifiedBy().accept(this); - } - - if (x.getSetList().size() != 0) { - print(" SET "); - printAndAccept(x.getSetList(), ", "); - } - - return false; - } - - @Override - public void endVisit(CobarShowStatus x) { - - } - - @Override - public boolean visit(CobarShowStatus x) { - print("SHOW COBAR_STATUS"); - return false; - } - - @Override - public void endVisit(MySqlKillStatement x) { - - } - - @Override - public boolean visit(MySqlKillStatement x) { - if (MySqlKillStatement.Type.CONNECTION.equals(x.getType())) { - print("KILL CONNECTION "); - } else if (MySqlKillStatement.Type.QUERY.equals(x.getType())) { - print("KILL QUERY "); - } - x.getThreadId().accept(this); - return false; - } - - @Override - public void endVisit(MySqlBinlogStatement x) { - - } - - @Override - public boolean visit(MySqlBinlogStatement x) { - print("BINLOG "); - x.getExpr().accept(this); - return false; - } - - @Override - public void endVisit(MySqlResetStatement x) { - - } - - @Override - public boolean visit(MySqlResetStatement x) { - print("RESET "); - for (int i = 0; i < x.getOptions().size(); ++i) { - if (i != 0) { - print(", "); - } - print(x.getOptions().get(i)); - } - return false; - } - - @Override - public void endVisit(MySqlCreateUserStatement x) { - - } - - @Override - public boolean visit(MySqlCreateUserStatement x) { - print("CREATE USER "); - printAndAccept(x.getUsers(), ", "); - return false; - } - - @Override - public void endVisit(UserSpecification x) { - - } - - @Override - public boolean visit(UserSpecification x) { - x.getUser().accept(this); - - if (x.getPassword() != null) { - print(" IDENTIFIED BY "); - x.getPassword().accept(this); - } - - if (x.getAuthPlugin() != null) { - print(" IDENTIFIED WITH "); - x.getAuthPlugin().accept(this); - } - return false; - } - - @Override - public void endVisit(MySqlDropUser x) { - - } - - @Override - public boolean visit(MySqlDropUser x) { - print("DROP USER "); - printAndAccept(x.getUsers(), ", "); - return false; - } - - @Override - public void endVisit(MySqlDropTableStatement x) { - - } - - @Override - public boolean visit(MySqlDropTableStatement x) { - if (x.isTemporary()) { - print("DROP TEMPORARY TABLE "); - } else { - print("DROP TABLE "); - } - if (x.isIfExists()) { - print("IF EXISTS "); - } - - printAndAccept(x.getTableSources(), ", "); - - if (x.getOption() != null) { - print(' '); - print(x.getOption()); - } - return false; - } - - @Override - public void endVisit(MySqlPartitionByKey x) { - - } - - @Override - public boolean visit(MySqlPartitionByKey x) { - print("PARTITION BY KEY ("); - printAndAccept(x.getColumns(), ", "); - print(")"); - - if (x.getPartitionCount() != null) { - print(" PARTITIONS "); - x.getPartitionCount().accept(this); - } - return false; - } - - @Override - public void endVisit(MySqlSelectQueryBlock x) { - - } - - @Override - public boolean visit(MySqlOutFileExpr x) { - print("OUTFILE "); - x.getFile().accept(this); - - if (x.getCharset() != null) { - print(" CHARACTER SET "); - print(x.getCharset()); - } - - if (x.getColumnsTerminatedBy() != null || x.getColumnsEnclosedBy() != null || x.getColumnsEscaped() != null) { - print(" COLUMNS"); - if (x.getColumnsTerminatedBy() != null) { - print(" TERMINATED BY "); - x.getColumnsTerminatedBy().accept(this); - } - - if (x.getColumnsEnclosedBy() != null) { - if (x.isColumnsEnclosedOptionally()) { - print(" OPTIONALLY"); - } - print(" ENCLOSED BY "); - x.getColumnsEnclosedBy().accept(this); - } - - if (x.getColumnsEscaped() != null) { - print(" ESCAPED BY "); - x.getColumnsEscaped().accept(this); - } - } - - if (x.getLinesStartingBy() != null || x.getLinesTerminatedBy() != null) { - print(" LINES"); - if (x.getLinesStartingBy() != null) { - print(" STARTING BY "); - x.getLinesStartingBy().accept(this); - } - - if (x.getLinesTerminatedBy() != null) { - print(" TERMINATED BY "); - x.getLinesTerminatedBy().accept(this); - } - } - - return false; - } - - @Override - public void endVisit(MySqlOutFileExpr x) { - - } - - @Override - public boolean visit(MySqlDescribeStatement x) { - print("DESC "); - x.getObject().accept(this); - return false; - } - - @Override - public void endVisit(MySqlDescribeStatement x) { - - } - - @Override - public boolean visit(MySqlUpdateStatement x) { - print("UPDATE "); - - if (x.isLowPriority()) { - print("LOW_PRIORITY "); - } - - if (x.isIgnore()) { - print("IGNORE "); - } - - x.getTableSource().accept(this); - - println(); - print("SET "); - for (int i = 0, size = x.getItems().size(); i < size; ++i) { - if (i != 0) { - print(", "); - } - x.getItems().get(i).accept(this); - } - - if (x.getWhere() != null) { - println(); - incrementIndent(); - print("WHERE "); - x.getWhere().setParent(x); - x.getWhere().accept(this); - decrementIndent(); - } - - if (x.getOrderBy() != null) { - println(); - x.getOrderBy().accept(this); - } - - if (x.getLimit() != null) { - println(); - x.getLimit().accept(this); - } - return false; - } - - @Override - public void endVisit(MySqlUpdateStatement x) { - - } - - @Override - public boolean visit(MySqlSetTransactionIsolationLevelStatement x) { - return false; - } - - @Override - public void endVisit(MySqlSetTransactionIsolationLevelStatement x) { - if (x.getGlobal() == null) { - print("SET TRANSACTION ISOLATION LEVEL "); - } else if (x.getGlobal().booleanValue()) { - print("SET GLOBAL TRANSACTION ISOLATION LEVEL "); - } else { - print("SET SESSION TRANSACTION ISOLATION LEVEL "); - } - print(x.getLevel()); - } - - @Override - public boolean visit(MySqlSetNamesStatement x) { - print("SET NAMES "); - if (x.isDefault()) { - print("DEFAULT"); - } else { - print(x.getCharSet()); - if (x.getCollate() != null) { - print(" COLLATE "); - print(x.getCollate()); - } - } - return false; - } - - @Override - public void endVisit(MySqlSetNamesStatement x) { - - } - - @Override - public boolean visit(MySqlSetCharSetStatement x) { - print("SET CHARACTER SET "); - if (x.isDefault()) { - print("DEFAULT"); - } else { - print(x.getCharSet()); - if (x.getCollate() != null) { - print(" COLLATE "); - print(x.getCollate()); - } - } - return false; - } - - @Override - public void endVisit(MySqlSetCharSetStatement x) { - - } - - @Override - public void endVisit(MySqlShowAuthorsStatement x) { - - } - - @Override - public boolean visit(MySqlShowAuthorsStatement x) { - print("SHOW AUTHORS"); - return false; - } - - @Override - public void endVisit(MySqlShowBinaryLogsStatement x) { - - } - - @Override - public boolean visit(MySqlShowBinaryLogsStatement x) { - print("SHOW BINARY LOGS"); - return false; - } - - @Override - public boolean visit(MySqlShowMasterLogsStatement x) { - print("SHOW MASTER LOGS"); - return false; - } - - @Override - public void endVisit(MySqlShowMasterLogsStatement x) { - - } - - @Override - public boolean visit(MySqlShowCollationStatement x) { - print("SHOW COLLATION"); - if (x.getPattern() != null) { - print(" LIKE "); - x.getPattern().accept(this); - } - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().accept(this); - } - return false; - } - - @Override - public void endVisit(MySqlShowCollationStatement x) { - - } - - @Override - public boolean visit(MySqlShowBinLogEventsStatement x) { - print("SHOW BINLOG EVENTS"); - if (x.getIn() != null) { - print(" IN "); - x.getIn().accept(this); - } - if (x.getFrom() != null) { - print(" FROM "); - x.getFrom().accept(this); - } - if (x.getLimit() != null) { - print(" "); - x.getLimit().accept(this); - } - return false; - } - - @Override - public void endVisit(MySqlShowBinLogEventsStatement x) { - - } - - @Override - public boolean visit(MySqlShowCharacterSetStatement x) { - print("SHOW CHARACTER SET"); - if (x.getPattern() != null) { - print(" LIKE "); - x.getPattern().accept(this); - } - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().accept(this); - } - return false; - } - - @Override - public void endVisit(MySqlShowCharacterSetStatement x) { - - } - - @Override - public boolean visit(MySqlShowContributorsStatement x) { - print("SHOW CONTRIBUTORS"); - return false; - } - - @Override - public void endVisit(MySqlShowContributorsStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateDatabaseStatement x) { - print("SHOW CREATE DATABASE "); - x.getDatabase().accept(this); - return false; - } - - @Override - public void endVisit(MySqlShowCreateDatabaseStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateEventStatement x) { - print("SHOW CREATE EVENT "); - x.getEventName().accept(this); - return false; - } - - @Override - public void endVisit(MySqlShowCreateEventStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateFunctionStatement x) { - print("SHOW CREATE FUNCTION "); - x.getName().accept(this); - return false; - } - - @Override - public void endVisit(MySqlShowCreateFunctionStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateProcedureStatement x) { - print("SHOW CREATE PROCEDURE "); - x.getName().accept(this); - return false; - } - - @Override - public void endVisit(MySqlShowCreateProcedureStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateTableStatement x) { - print("SHOW CREATE TABLE "); - x.getName().accept(this); - return false; - } - - @Override - public void endVisit(MySqlShowCreateTableStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateTriggerStatement x) { - print("SHOW CREATE TRIGGER "); - x.getName().accept(this); - return false; - } - - @Override - public void endVisit(MySqlShowCreateTriggerStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateViewStatement x) { - print("SHOW CREATE VIEW "); - x.getName().accept(this); - return false; - } - - @Override - public void endVisit(MySqlShowCreateViewStatement x) { - - } - - @Override - public boolean visit(MySqlShowEngineStatement x) { - print("SHOW ENGINE "); - x.getName().accept(this); - print(' '); - print(x.getOption().name()); - return false; - } - - @Override - public void endVisit(MySqlShowEngineStatement x) { - - } - - @Override - public boolean visit(MySqlShowEventsStatement x) { - print("SHOW EVENTS"); - if (x.getSchema() != null) { - print(" FROM "); - x.getSchema().accept(this); - } - - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlShowEventsStatement x) { - - } - - @Override - public boolean visit(MySqlShowFunctionCodeStatement x) { - print("SHOW FUNCTION CODE "); - x.getName().accept(this); - return false; - } - - @Override - public void endVisit(MySqlShowFunctionCodeStatement x) { - - } - - @Override - public boolean visit(MySqlShowFunctionStatusStatement x) { - print("SHOW FUNCTION STATUS"); - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlShowFunctionStatusStatement x) { - - } - - @Override - public boolean visit(MySqlShowEnginesStatement x) { - if (x.isStorage()) { - print("SHOW STORAGE ENGINES"); - } else { - print("SHOW ENGINES"); - } - return false; - } - - @Override - public void endVisit(MySqlShowEnginesStatement x) { - - } - - @Override - public boolean visit(MySqlShowErrorsStatement x) { - if (x.isCount()) { - print("SHOW COUNT(*) ERRORS"); - } else { - print("SHOW ERRORS"); - if (x.getLimit() != null) { - print(' '); - x.getLimit().accept(this); - } - } - return false; - } - - @Override - public void endVisit(MySqlShowErrorsStatement x) { - - } - - @Override - public boolean visit(MySqlShowGrantsStatement x) { - print("SHOW GRANTS"); - if (x.getUser() != null) { - print(" FOR "); - x.getUser().accept(this); - } - return false; - } - - @Override - public void endVisit(MySqlShowGrantsStatement x) { - - } - - @Override - public boolean visit(MySqlUserName x) { - print(x.getUserName()); - if (x.getHost() != null) { - print('@'); - print(x.getHost()); - } - return false; - } - - @Override - public void endVisit(MySqlUserName x) { - - } - - @Override - public boolean visit(MySqlShowIndexesStatement x) { - print("SHOW INDEX"); - - if (x.getTable() != null) { - print(" FROM "); - if (x.getDatabase() != null) { - x.getDatabase().accept(this); - print('.'); - } - x.getTable().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlShowIndexesStatement x) { - - } - - @Override - public boolean visit(MySqlShowKeysStatement x) { - print("SHOW KEYS"); - - if (x.getTable() != null) { - print(" FROM "); - if (x.getDatabase() != null) { - x.getDatabase().accept(this); - print('.'); - } - x.getTable().accept(this); - } - return false; - } - - @Override - public void endVisit(MySqlShowKeysStatement x) { - - } - - @Override - public boolean visit(MySqlShowMasterStatusStatement x) { - print("SHOW MASTER STATUS"); - return false; - } - - @Override - public void endVisit(MySqlShowMasterStatusStatement x) { - - } - - @Override - public boolean visit(MySqlShowOpenTablesStatement x) { - print("SHOW OPEN TABLES"); - - if (x.getDatabase() != null) { - print(" FROM "); - x.getDatabase().accept(this); - } - - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlShowOpenTablesStatement x) { - - } - - @Override - public boolean visit(MySqlShowPluginsStatement x) { - print("SHOW PLUGINS"); - return false; - } - - @Override - public void endVisit(MySqlShowPluginsStatement x) { - - } - - @Override - public boolean visit(MySqlShowPrivilegesStatement x) { - print("SHOW PRIVILEGES"); - return false; - } - - @Override - public void endVisit(MySqlShowPrivilegesStatement x) { - - } - - @Override - public boolean visit(MySqlShowProcedureCodeStatement x) { - print("SHOW PROCEDURE CODE "); - x.getName().accept(this); - return false; - } - - @Override - public void endVisit(MySqlShowProcedureCodeStatement x) { - - } - - @Override - public boolean visit(MySqlShowProcedureStatusStatement x) { - print("SHOW PROCEDURE STATUS"); - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().accept(this); - } - return false; - } - - @Override - public void endVisit(MySqlShowProcedureStatusStatement x) { - - } - - @Override - public boolean visit(MySqlShowProcessListStatement x) { - if (x.isFull()) { - print("SHOW FULL PROCESSLIST"); - } else { - print("SHOW PROCESSLIST"); - } - return false; - } - - @Override - public void endVisit(MySqlShowProcessListStatement x) { - - } - - @Override - public boolean visit(MySqlShowProfileStatement x) { - print("SHOW PROFILE"); - for (int i = 0; i < x.getTypes().size(); ++i) { - if (i == 0) { - print(' '); - } else { - print(", "); - } - print(x.getTypes().get(i).name); - } - - if (x.getForQuery() != null) { - print(" FOR QUERY "); - x.getForQuery().accept(this); - } - - if (x.getLimit() != null) { - print(' '); - x.getLimit().accept(this); - } - return false; - } - - @Override - public void endVisit(MySqlShowProfileStatement x) { - - } - - @Override - public boolean visit(MySqlShowProfilesStatement x) { - print("SHOW PROFILES"); - return false; - } - - @Override - public void endVisit(MySqlShowProfilesStatement x) { - - } - - @Override - public boolean visit(MySqlShowRelayLogEventsStatement x) { - print("SHOW RELAYLOG EVENTS"); - - if (x.getLogName() != null) { - print(" IN "); - x.getLogName().accept(this); - } - - if (x.getFrom() != null) { - print(" FROM "); - x.getFrom().accept(this); - } - - if (x.getLimit() != null) { - print(' '); - x.getLimit().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlShowRelayLogEventsStatement x) { - - } - - @Override - public boolean visit(MySqlShowSlaveHostsStatement x) { - print("SHOW SLAVE HOSTS"); - return false; - } - - @Override - public void endVisit(MySqlShowSlaveHostsStatement x) { - - } - - @Override - public boolean visit(MySqlShowSlaveStatusStatement x) { - print("SHOW SLAVE STATUS"); - return false; - } - - @Override - public void endVisit(MySqlShowSlaveStatusStatement x) { - - } - - @Override - public boolean visit(MySqlShowTableStatusStatement x) { - print("SHOW TABLE STATUS"); - if (x.getDatabase() != null) { - print(" FROM "); - x.getDatabase().accept(this); - } - - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlShowTableStatusStatement x) { - - } - - @Override - public boolean visit(MySqlShowTriggersStatement x) { - print("SHOW TRIGGERS"); - - if (x.getDatabase() != null) { - print(" FROM "); - x.getDatabase().accept(this); - } - - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().setParent(x); - x.getWhere().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlShowTriggersStatement x) { - - } - - @Override - public boolean visit(MySqlShowVariantsStatement x) { - print("SHOW "); - - if (x.isGlobal()) { - print("GLOBAL "); - } - - if (x.isSession()) { - print("SESSION "); - } - - print("VARIABLES"); - - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().setParent(x); - x.getWhere().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlShowVariantsStatement x) { - - } - - @Override - public boolean visit(MySqlAlterTableStatement x) { - if (x.isIgnore()) { - print("ALTER IGNORE TABLE "); - } else { - print("ALTER TABLE "); - } - x.getName().accept(this); - incrementIndent(); - for (int i = 0; i < x.getItems().size(); ++i) { - SQLAlterTableItem item = x.getItems().get(i); - if (i != 0) { - print(','); - } - println(); - item.accept(this); - } - decrementIndent(); - return false; - } - - @Override - public void endVisit(MySqlAlterTableStatement x) { - - } - - @Override - public boolean visit(MySqlAlterTableAddColumn x) { - print("ADD COLUMN "); - printAndAccept(x.getColumns(), ", "); - if (x.getFirstColumn() != null) { - print(" FIRST "); - x.getFirstColumn().accept(this); - } else if (x.getAfterColumn() != null) { - print(" AFTER "); - x.getAfterColumn().accept(this); - } else if (x.isFirst()) { - print(" FIRST"); - } - return false; - } - - @Override - public void endVisit(MySqlAlterTableAddColumn x) { - - } - - @Override - public boolean visit(MySqlCreateIndexStatement x) { - print("CREATE "); - if (x.getType() != null) { - print(x.getType()); - print(" "); - } - - print("INDEX "); - - x.getName().accept(this); - print(" ON "); - x.getTable().accept(this); - print(" ("); - printAndAccept(x.getItems(), ", "); - print(")"); - - if (x.getUsing() != null) { - print(" USING "); - print(x.getUsing()); - } - return false; - } - - @Override - public void endVisit(MySqlCreateIndexStatement x) { - - } - - @Override - public boolean visit(MySqlRenameTableStatement.Item x) { - x.getName().accept(this); - print(" TO "); - x.getTo().accept(this); - return false; - } - - @Override - public void endVisit(MySqlRenameTableStatement.Item x) { - - } - - @Override - public boolean visit(MySqlRenameTableStatement x) { - print("RENAME TABLE "); - printAndAccept(x.getItems(), ", "); - return false; - } - - @Override - public void endVisit(MySqlRenameTableStatement x) { - - } - - @Override - public boolean visit(MySqlDropViewStatement x) { - print("DROP VIEW "); - if (x.isIfExists()) { - print("IF EXISTS "); - } - - printAndAccept(x.getTableSources(), ", "); - - if (x.getOption() != null) { - print(' '); - print(x.getOption()); - } - return false; - } - - @Override - public void endVisit(MySqlDropViewStatement x) { - - } - - @Override - public boolean visit(MySqlUnionQuery x) { - { - boolean needParen = false; - if (x.getLeft() instanceof MySqlSelectQueryBlock) { - MySqlSelectQueryBlock right = (MySqlSelectQueryBlock) x.getLeft(); - if (right.getOrderBy() != null || right.getLimit() != null) { - needParen = true; - } - } - if (needParen) { - print('('); - x.getLeft().accept(this); - print(')'); - } else { - x.getLeft().accept(this); - } - } - println(); - print(x.getOperator().name); - println(); - - boolean needParen = false; - - if (x.getOrderBy() != null || x.getLimit() != null) { - needParen = true; - } else if (x.getRight() instanceof MySqlSelectQueryBlock) { - MySqlSelectQueryBlock right = (MySqlSelectQueryBlock) x.getRight(); - if (right.getOrderBy() != null || right.getLimit() != null) { - needParen = true; - } - } - - if (needParen) { - print('('); - x.getRight().accept(this); - print(')'); - } else { - x.getRight().accept(this); - } - - if (x.getOrderBy() != null) { - println(); - x.getOrderBy().accept(this); - } - - if (x.getLimit() != null) { - println(); - x.getLimit().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlUnionQuery x) { - - } - - @Override - public boolean visit(MySqlUseIndexHint x) { - print("USE INDEX "); - if (x.getOption() != null) { - print("FOR "); - print(x.getOption().name); - print(' '); - } - print('('); - printAndAccept(x.getIndexList(), ", "); - print(')'); - return false; - } - - @Override - public void endVisit(MySqlUseIndexHint x) { - - } - - @Override - public boolean visit(MySqlIgnoreIndexHint x) { - print("IGNORE INDEX "); - if (x.getOption() != null) { - print("FOR "); - print(x.getOption().name); - print(' '); - } - print('('); - printAndAccept(x.getIndexList(), ", "); - print(')'); - return false; - } - - @Override - public void endVisit(MySqlIgnoreIndexHint x) { - - } - - public boolean visit(SQLExprTableSource x) { - x.getExpr().accept(this); - - if (x.getAlias() != null) { - print(' '); - print(x.getAlias()); - } - - for (int i = 0; i < x.getHints().size(); ++i) { - print(' '); - x.getHints().get(i).accept(this); - } - - return false; - } - - @Override - public boolean visit(MySqlLockTableStatement x) { - print("LOCK TABLES "); - x.getTableSource().accept(this); - if (x.getLockType() != null) { - print(' '); - print(x.getLockType().name); - } - return false; - } - - @Override - public void endVisit(MySqlLockTableStatement x) { - - } - - @Override - public boolean visit(MySqlUnlockTablesStatement x) { - print("UNLOCK TABLES"); - return false; - } - - @Override - public void endVisit(MySqlUnlockTablesStatement x) { - - } - - @Override - public boolean visit(MySqlForceIndexHint x) { - print("FORCE INDEX "); - if (x.getOption() != null) { - print("FOR "); - print(x.getOption().name); - print(' '); - } - print('('); - printAndAccept(x.getIndexList(), ", "); - print(')'); - return false; - } - - @Override - public void endVisit(MySqlForceIndexHint x) { - - } - - @Override - public boolean visit(MySqlAlterTableChangeColumn x) { - print("CHANGE COLUMN "); - x.getColumnName().accept(this); - print(' '); - x.getNewColumnDefinition().accept(this); - if (x.getFirstColumn() != null) { - print(" FIRST "); - x.getFirstColumn().accept(this); - } else if (x.getAfterColumn() != null) { - print(" AFTER "); - x.getAfterColumn().accept(this); - } else if (x.isFirst()) { - print(" FIRST"); - } - - return false; - } - - @Override - public void endVisit(MySqlAlterTableChangeColumn x) { - - } - - @Override - public boolean visit(MySqlAlterTableCharacter x) { - print("CHARACTER SET = "); - x.getCharacterSet().accept(this); - - if (x.getCollate() != null) { - print(", COLLATE = "); - x.getCollate().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlAlterTableCharacter x) { - - } - - @Override - public boolean visit(MySqlAlterTableAddIndex x) { - print("ADD "); - if (x.getType() != null) { - print(x.getType()); - print(" "); - } - - print("INDEX "); - - if (x.getName() != null) { - x.getName().accept(this); - print(' '); - } - print("("); - printAndAccept(x.getItems(), ", "); - print(")"); - - if (x.getUsing() != null) { - print(" USING "); - print(x.getUsing()); - } - return false; - } - - @Override - public void endVisit(MySqlAlterTableAddIndex x) { - - } - - @Override - public boolean visit(MySqlAlterTableAddUnique x) { - print("ADD "); - if (x.getType() != null) { - print(x.getType()); - print(" "); - } - - print("UNIQUE "); - - if (x.getName() != null) { - x.getName().accept(this); - print(' '); - } - print("("); - printAndAccept(x.getItems(), ", "); - print(")"); - - if (x.getUsing() != null) { - print(" USING "); - print(x.getUsing()); - } - return false; - } - - @Override - public void endVisit(MySqlAlterTableAddUnique x) { - - } - - @Override - public boolean visit(MySqlAlterTableOption x) { - print(x.getName()); - print(" = "); - print(x.getValue()); - return false; - } - - @Override - public void endVisit(MySqlAlterTableOption x) { - - } - - @Override - public void endVisit(MySqlCreateTableStatement x) { - - } - - @Override - public boolean visit(MySqlHelpStatement x) { - print("HELP "); - x.getContent().accept(this); - return false; - } - - @Override - public void endVisit(MySqlHelpStatement x) { - - } - - @Override - public boolean visit(MySqlCharExpr x) { - print(x.toString()); - return false; - } - - @Override - public void endVisit(MySqlCharExpr x) { - - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlParameterizedOutputVisitor.java b/src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlParameterizedOutputVisitor.java deleted file mode 100644 index 2de8c388..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlParameterizedOutputVisitor.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.visitor; - -import org.durid.sql.ast.expr.SQLBinaryOpExpr; -import org.durid.sql.ast.expr.SQLCharExpr; -import org.durid.sql.ast.expr.SQLInListExpr; -import org.durid.sql.ast.expr.SQLIntegerExpr; -import org.durid.sql.ast.expr.SQLNCharExpr; -import org.durid.sql.ast.expr.SQLNullExpr; -import org.durid.sql.ast.expr.SQLNumberExpr; -import org.durid.sql.dialect.mysql.ast.statement.MySqlInsertStatement; -import org.durid.sql.visitor.ParameterizedOutputVisitorUtils; - -public class MySqlParameterizedOutputVisitor extends MySqlOutputVisitor { - - public MySqlParameterizedOutputVisitor() { - this(new StringBuilder()); - } - - public MySqlParameterizedOutputVisitor(Appendable appender){ - super(appender); - } - - public boolean visit(SQLInListExpr x) { - return ParameterizedOutputVisitorUtils.visit(this, x); - } - - public boolean visit(SQLBinaryOpExpr x) { - x = ParameterizedOutputVisitorUtils.merge(x); - - return super.visit(x); - } - - public boolean visit(SQLNullExpr x) { - print('?'); - return false; - } - - public boolean visit(SQLIntegerExpr x) { - if (Boolean.TRUE.equals(x.getAttribute(ParameterizedOutputVisitorUtils.ATTR_PARAMS_SKIP))) { - return super.visit(x); - } - - print('?'); - return false; - } - - public boolean visit(SQLNumberExpr x) { - if (Boolean.TRUE.equals(x.getAttribute(ParameterizedOutputVisitorUtils.ATTR_PARAMS_SKIP))) { - return super.visit(x); - } - - print('?'); - return false; - } - - public boolean visit(SQLCharExpr x) { - if (Boolean.TRUE.equals(x.getAttribute(ParameterizedOutputVisitorUtils.ATTR_PARAMS_SKIP))) { - return super.visit(x); - } - - print('?'); - return false; - } - - public boolean visit(SQLNCharExpr x) { - if (Boolean.TRUE.equals(x.getAttribute(ParameterizedOutputVisitorUtils.ATTR_PARAMS_SKIP))) { - return super.visit(x); - } - - print('?'); - return false; - } - - @Override - public boolean visit(MySqlInsertStatement x) { - print("INSERT "); - - if (x.isLowPriority()) { - print("LOW_PRIORITY "); - } - - if (x.isDelayed()) { - print("DELAYED "); - } - - if (x.isHighPriority()) { - print("HIGH_PRIORITY "); - } - - if (x.isIgnore()) { - print("IGNORE "); - } - - print("INTO "); - - x.getTableName().accept(this); - - if (x.getColumns().size() > 0) { - print(" ("); - for (int i = 0, size = x.getColumns().size(); i < size; ++i) { - if (i != 0) { - print(", "); - } - x.getColumns().get(i).accept(this); - } - print(")"); - } - - if (x.getValuesList().size() != 0) { - print(" VALUES "); - int size = x.getValuesList().size(); - if (size == 0) { - print("()"); - } else { - for (int i = 0; i < 1; ++i) { - if (i != 0) { - print(", "); - } - x.getValuesList().get(i).accept(this); - } - } - } - if (x.getQuery() != null) { - print(" "); - x.getQuery().accept(this); - } - - if (x.getDuplicateKeyUpdate().size() != 0) { - print(" ON DUPLICATE KEY UPDATE "); - printAndAccept(x.getDuplicateKeyUpdate(), ", "); - } - - return false; - } -} diff --git a/src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlSchemaStatVisitor.java b/src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlSchemaStatVisitor.java deleted file mode 100644 index 905f3636..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/visitor/MySqlSchemaStatVisitor.java +++ /dev/null @@ -1,1211 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.visitor; - -import java.util.Map; - -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.expr.SQLIdentifierExpr; -import org.durid.sql.ast.statement.SQLCreateTableStatement; -import org.durid.sql.ast.statement.SQLDropTableStatement; -import org.durid.sql.ast.statement.SQLExprTableSource; -import org.durid.sql.ast.statement.SQLSelectQueryBlock; -import org.durid.sql.ast.statement.SQLSelectStatement; -import org.durid.sql.ast.statement.SQLUnionQuery; -import org.durid.sql.ast.statement.SQLUpdateStatement; -import org.durid.sql.dialect.mysql.ast.MySqlForceIndexHint; -import org.durid.sql.dialect.mysql.ast.MySqlIgnoreIndexHint; -import org.durid.sql.dialect.mysql.ast.MySqlKey; -import org.durid.sql.dialect.mysql.ast.MySqlPrimaryKey; -import org.durid.sql.dialect.mysql.ast.MySqlUseIndexHint; -import org.durid.sql.dialect.mysql.ast.expr.MySqlBinaryExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlBooleanExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlCharExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlExtractExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlIntervalExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlMatchAgainstExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlUserName; -import org.durid.sql.dialect.mysql.ast.statement.CobarShowStatus; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddColumn; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddIndex; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddUnique; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableCharacter; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableOption; -import org.durid.sql.dialect.mysql.ast.statement.MySqlAlterTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlBinlogStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlCommitStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlCreateIndexStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement.UserSpecification; -import org.durid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlDescribeStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlDropTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlDropUser; -import org.durid.sql.dialect.mysql.ast.statement.MySqlDropViewStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlExecuteStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlHelpStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlInsertStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlKillStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlLoadDataInFileStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlLoadXmlStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlPartitionByKey; -import org.durid.sql.dialect.mysql.ast.statement.MySqlPrepareStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlRenameTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlReplaceStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlResetStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlRollbackStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectGroupBy; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSetCharSetStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSetNamesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSetTransactionIsolationLevelStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowAuthorsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowBinLogEventsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowBinaryLogsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCharacterSetStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCollationStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowColumnsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowContributorsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateDatabaseStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateEventStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateFunctionStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateProcedureStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateTableStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateTriggerStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowCreateViewStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowDatabasesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowEngineStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowEnginesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowErrorsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowEventsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowFunctionCodeStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowFunctionStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowGrantsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowIndexesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowKeysStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowMasterStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowOpenTablesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowPluginsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowPrivilegesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowProcedureCodeStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowProcedureStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowProcessListStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowProfileStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowProfilesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowRelayLogEventsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowSlaveHostsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowSlaveStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowTableStatusStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowTriggersStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowVariantsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlStartTransactionStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlTableIndex; -import org.durid.sql.dialect.mysql.ast.statement.MySqlUnionQuery; -import org.durid.sql.dialect.mysql.ast.statement.MySqlUnlockTablesStatement; -import org.durid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement; -import org.durid.sql.visitor.SchemaStatVisitor; -import org.durid.stat.TableStat; -import org.durid.stat.TableStat.Mode; - -public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlASTVisitor { - - public boolean visit(SQLSelectStatement x) { - setAliasMap(); - getAliasMap().put("DUAL", null); - - return true; - } - - @Override - public String getDbType() { - return "mysql"; - } - - // DUAL - public boolean visit(MySqlDeleteStatement x) { - setAliasMap(); - - setMode(x, Mode.Delete); - - accept(x.getFrom()); - accept(x.getUsing()); - x.getTableSource().accept(this); - - if (x.getTableSource() instanceof SQLExprTableSource) { - SQLName tableName = (SQLName) ((SQLExprTableSource) x.getTableSource()).getExpr(); - String ident = tableName.toString(); - setCurrentTable(x, ident); - - TableStat stat = this.getTableStat(ident); - stat.incrementDeleteCount(); - } - - accept(x.getWhere()); - - accept(x.getOrderBy()); - accept(x.getLimit()); - - return false; - } - - public void endVisit(MySqlDeleteStatement x) { - setAliasMap(null); - } - - @Override - public void endVisit(MySqlInsertStatement x) { - setModeOrigin(x); - } - - @Override - public boolean visit(MySqlInsertStatement x) { - setMode(x, Mode.Insert); - - setAliasMap(); - - if (x.getTableName() instanceof SQLIdentifierExpr) { - String ident = ((SQLIdentifierExpr) x.getTableName()).getName(); - setCurrentTable(x, ident); - - TableStat stat = getTableStat(ident); - stat.incrementInsertCount(); - - Map aliasMap = getAliasMap(); - if (aliasMap != null) { - if (x.getAlias() != null) { - aliasMap.put(x.getAlias(), ident); - } - aliasMap.put(ident, ident); - } - } - - accept(x.getColumns()); - accept(x.getValuesList()); - accept(x.getQuery()); - accept(x.getDuplicateKeyUpdate()); - - return false; - } - - @Override - public boolean visit(MySqlBooleanExpr x) { - - return true; - } - - @Override - public void endVisit(MySqlBooleanExpr x) { - - } - - @Override - public boolean visit(Limit x) { - - return true; - } - - @Override - public void endVisit(Limit x) { - - } - - @Override - public boolean visit(MySqlTableIndex x) { - - return true; - } - - @Override - public void endVisit(MySqlTableIndex x) { - - } - - @Override - public boolean visit(MySqlKey x) { - - return true; - } - - @Override - public void endVisit(MySqlKey x) { - - } - - @Override - public boolean visit(MySqlPrimaryKey x) { - - return true; - } - - @Override - public void endVisit(MySqlPrimaryKey x) { - - } - - @Override - public void endVisit(MySqlIntervalExpr x) { - - } - - @Override - public boolean visit(MySqlIntervalExpr x) { - - return true; - } - - @Override - public void endVisit(MySqlExtractExpr x) { - - } - - @Override - public boolean visit(MySqlExtractExpr x) { - - return true; - } - - @Override - public void endVisit(MySqlMatchAgainstExpr x) { - - } - - @Override - public boolean visit(MySqlMatchAgainstExpr x) { - - return true; - } - - @Override - public void endVisit(MySqlBinaryExpr x) { - - } - - @Override - public boolean visit(MySqlBinaryExpr x) { - - return true; - } - - @Override - public void endVisit(MySqlPrepareStatement x) { - - } - - @Override - public boolean visit(MySqlPrepareStatement x) { - - return true; - } - - @Override - public void endVisit(MySqlExecuteStatement x) { - - } - - @Override - public boolean visit(MySqlExecuteStatement x) { - - return true; - } - - @Override - public void endVisit(MySqlLoadDataInFileStatement x) { - - } - - @Override - public boolean visit(MySqlLoadDataInFileStatement x) { - - return true; - } - - @Override - public void endVisit(MySqlLoadXmlStatement x) { - - } - - @Override - public boolean visit(MySqlLoadXmlStatement x) { - - return true; - } - - @Override - public void endVisit(MySqlReplaceStatement x) { - - } - - @Override - public boolean visit(MySqlReplaceStatement x) { - - return true; - } - - @Override - public void endVisit(MySqlSelectGroupBy x) { - - } - - @Override - public boolean visit(MySqlSelectGroupBy x) { - - return true; - } - - @Override - public void endVisit(MySqlStartTransactionStatement x) { - - } - - @Override - public boolean visit(MySqlStartTransactionStatement x) { - - return true; - } - - @Override - public void endVisit(MySqlCommitStatement x) { - - } - - @Override - public boolean visit(MySqlCommitStatement x) { - - return true; - } - - @Override - public void endVisit(MySqlRollbackStatement x) { - - } - - @Override - public boolean visit(MySqlRollbackStatement x) { - - return true; - } - - @Override - public void endVisit(MySqlShowColumnsStatement x) { - - } - - @Override - public boolean visit(MySqlShowColumnsStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowTablesStatement x) { - - } - - @Override - public boolean visit(MySqlShowTablesStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowDatabasesStatement x) { - - } - - @Override - public boolean visit(MySqlShowDatabasesStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowWarningsStatement x) { - - } - - @Override - public boolean visit(MySqlShowWarningsStatement x) { - return true; - } - - @Override - public void endVisit(MySqlShowStatusStatement x) { - - } - - @Override - public boolean visit(MySqlShowStatusStatement x) { - return true; - } - - @Override - public void endVisit(CobarShowStatus x) { - - } - - @Override - public boolean visit(CobarShowStatus x) { - return true; - } - - @Override - public void endVisit(MySqlKillStatement x) { - - } - - @Override - public boolean visit(MySqlKillStatement x) { - return true; - } - - @Override - public void endVisit(MySqlBinlogStatement x) { - - } - - @Override - public boolean visit(MySqlBinlogStatement x) { - return true; - } - - @Override - public void endVisit(MySqlResetStatement x) { - - } - - @Override - public boolean visit(MySqlResetStatement x) { - return true; - } - - @Override - public void endVisit(MySqlCreateUserStatement x) { - - } - - @Override - public boolean visit(MySqlCreateUserStatement x) { - return true; - } - - @Override - public void endVisit(UserSpecification x) { - - } - - @Override - public boolean visit(UserSpecification x) { - return true; - } - - @Override - public void endVisit(MySqlDropUser x) { - - } - - @Override - public boolean visit(MySqlDropUser x) { - return true; - } - - @Override - public void endVisit(MySqlDropTableStatement x) { - - } - - @Override - public boolean visit(MySqlDropTableStatement x) { - return visit((SQLDropTableStatement) x); - } - - @Override - public void endVisit(MySqlPartitionByKey x) { - - } - - @Override - public boolean visit(MySqlPartitionByKey x) { - accept(x.getColumns()); - return false; - } - - @Override - public boolean visit(MySqlSelectQueryBlock x) { - return this.visit((SQLSelectQueryBlock) x); - } - - @Override - public void endVisit(MySqlSelectQueryBlock x) { - - } - - @Override - public boolean visit(MySqlOutFileExpr x) { - return false; - } - - @Override - public void endVisit(MySqlOutFileExpr x) { - - } - - @Override - public boolean visit(MySqlDescribeStatement x) { - getTableStat(x.getObject().toString()); - return false; - } - - @Override - public void endVisit(MySqlDescribeStatement x) { - - } - - @Override - public boolean visit(MySqlUpdateStatement x) { - return visit((SQLUpdateStatement) x); - } - - @Override - public void endVisit(MySqlUpdateStatement x) { - - } - - @Override - public boolean visit(MySqlSetTransactionIsolationLevelStatement x) { - return false; - } - - @Override - public void endVisit(MySqlSetTransactionIsolationLevelStatement x) { - - } - - @Override - public boolean visit(MySqlSetNamesStatement x) { - return false; - } - - @Override - public void endVisit(MySqlSetNamesStatement x) { - - } - - @Override - public boolean visit(MySqlSetCharSetStatement x) { - return false; - } - - @Override - public void endVisit(MySqlSetCharSetStatement x) { - - } - - @Override - public boolean visit(MySqlShowAuthorsStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowAuthorsStatement x) { - - } - - @Override - public boolean visit(MySqlShowBinaryLogsStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowBinaryLogsStatement x) { - - } - - @Override - public boolean visit(MySqlShowMasterLogsStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowMasterLogsStatement x) { - - } - - @Override - public boolean visit(MySqlShowCollationStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowCollationStatement x) { - - } - - @Override - public boolean visit(MySqlShowBinLogEventsStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowBinLogEventsStatement x) { - - } - - @Override - public boolean visit(MySqlShowCharacterSetStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowCharacterSetStatement x) { - - } - - @Override - public boolean visit(MySqlShowContributorsStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowContributorsStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateDatabaseStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowCreateDatabaseStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateEventStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowCreateEventStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateFunctionStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowCreateFunctionStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateProcedureStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowCreateProcedureStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateTableStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowCreateTableStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateTriggerStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowCreateTriggerStatement x) { - - } - - @Override - public boolean visit(MySqlShowCreateViewStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowCreateViewStatement x) { - - } - - @Override - public boolean visit(MySqlShowEngineStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowEngineStatement x) { - - } - - @Override - public boolean visit(MySqlShowEnginesStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowEnginesStatement x) { - - } - - @Override - public boolean visit(MySqlShowErrorsStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowErrorsStatement x) { - - } - - @Override - public boolean visit(MySqlShowEventsStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowEventsStatement x) { - - } - - @Override - public boolean visit(MySqlShowFunctionCodeStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowFunctionCodeStatement x) { - - } - - @Override - public boolean visit(MySqlShowFunctionStatusStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowFunctionStatusStatement x) { - - } - - @Override - public boolean visit(MySqlShowGrantsStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowGrantsStatement x) { - - } - - @Override - public boolean visit(MySqlUserName x) { - return false; - } - - @Override - public void endVisit(MySqlUserName x) { - - } - - @Override - public boolean visit(MySqlShowIndexesStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowIndexesStatement x) { - - } - - @Override - public boolean visit(MySqlShowKeysStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowKeysStatement x) { - - } - - @Override - public boolean visit(MySqlShowMasterStatusStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowMasterStatusStatement x) { - - } - - @Override - public boolean visit(MySqlShowOpenTablesStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowOpenTablesStatement x) { - - } - - @Override - public boolean visit(MySqlShowPluginsStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowPluginsStatement x) { - - } - - @Override - public boolean visit(MySqlShowPrivilegesStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowPrivilegesStatement x) { - - } - - @Override - public boolean visit(MySqlShowProcedureCodeStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowProcedureCodeStatement x) { - - } - - @Override - public boolean visit(MySqlShowProcedureStatusStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowProcedureStatusStatement x) { - - } - - @Override - public boolean visit(MySqlShowProcessListStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowProcessListStatement x) { - - } - - @Override - public boolean visit(MySqlShowProfileStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowProfileStatement x) { - - } - - @Override - public boolean visit(MySqlShowProfilesStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowProfilesStatement x) { - - } - - @Override - public boolean visit(MySqlShowRelayLogEventsStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowRelayLogEventsStatement x) { - - } - - @Override - public boolean visit(MySqlShowSlaveHostsStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowSlaveHostsStatement x) { - - } - - @Override - public boolean visit(MySqlShowSlaveStatusStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowSlaveStatusStatement x) { - - } - - @Override - public boolean visit(MySqlShowTableStatusStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowTableStatusStatement x) { - - } - - @Override - public boolean visit(MySqlShowTriggersStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowTriggersStatement x) { - - } - - @Override - public boolean visit(MySqlShowVariantsStatement x) { - return false; - } - - @Override - public void endVisit(MySqlShowVariantsStatement x) { - - } - - @Override - public boolean visit(MySqlAlterTableStatement x) { - return false; - } - - @Override - public void endVisit(MySqlAlterTableStatement x) { - - } - - @Override - public boolean visit(MySqlAlterTableAddColumn x) { - return false; - } - - @Override - public void endVisit(MySqlAlterTableAddColumn x) { - - } - - @Override - public boolean visit(MySqlCreateIndexStatement x) { - return false; - } - - @Override - public void endVisit(MySqlCreateIndexStatement x) { - - } - - @Override - public boolean visit(MySqlRenameTableStatement.Item x) { - return false; - } - - @Override - public void endVisit(MySqlRenameTableStatement.Item x) { - - } - - @Override - public boolean visit(MySqlRenameTableStatement x) { - return false; - } - - @Override - public void endVisit(MySqlRenameTableStatement x) { - - } - - @Override - public boolean visit(MySqlDropViewStatement x) { - return true; - } - - @Override - public void endVisit(MySqlDropViewStatement x) { - - } - - @Override - public boolean visit(MySqlUnionQuery x) { - return visit((SQLUnionQuery) x); - } - - @Override - public void endVisit(MySqlUnionQuery x) { - - } - - @Override - public boolean visit(MySqlUseIndexHint x) { - return false; - } - - @Override - public void endVisit(MySqlUseIndexHint x) { - - } - - @Override - public boolean visit(MySqlIgnoreIndexHint x) { - return false; - } - - @Override - public void endVisit(MySqlIgnoreIndexHint x) { - - } - - @Override - public boolean visit(MySqlLockTableStatement x) { - return false; - } - - @Override - public void endVisit(MySqlLockTableStatement x) { - - } - - @Override - public boolean visit(MySqlUnlockTablesStatement x) { - return false; - } - - @Override - public void endVisit(MySqlUnlockTablesStatement x) { - - } - - @Override - public boolean visit(MySqlForceIndexHint x) { - return false; - } - - @Override - public void endVisit(MySqlForceIndexHint x) { - - } - - @Override - public boolean visit(MySqlAlterTableChangeColumn x) { - return false; - } - - @Override - public void endVisit(MySqlAlterTableChangeColumn x) { - - } - - @Override - public boolean visit(MySqlAlterTableCharacter x) { - return false; - } - - @Override - public void endVisit(MySqlAlterTableCharacter x) { - - } - - @Override - public boolean visit(MySqlAlterTableAddIndex x) { - return false; - } - - @Override - public void endVisit(MySqlAlterTableAddIndex x) { - - } - - @Override - public boolean visit(MySqlAlterTableOption x) { - return false; - } - - @Override - public void endVisit(MySqlAlterTableOption x) { - - } - - @Override - public boolean visit(MySqlCreateTableStatement x) { - return super.visit((SQLCreateTableStatement) x); - } - - @Override - public void endVisit(MySqlCreateTableStatement x) { - - } - - @Override - public boolean visit(MySqlHelpStatement x) { - return false; - } - - @Override - public void endVisit(MySqlHelpStatement x) { - - } - - @Override - public boolean visit(MySqlCharExpr x) { - return false; - } - - @Override - public void endVisit(MySqlCharExpr x) { - - } - - @Override - public boolean visit(MySqlAlterTableAddUnique x) { - return false; - } - - @Override - public void endVisit(MySqlAlterTableAddUnique x) { - - } -} diff --git a/src/main/java/org/durid/sql/dialect/transact/ast/TransactSQLObject.java b/src/main/java/org/durid/sql/dialect/transact/ast/TransactSQLObject.java deleted file mode 100644 index 1de0aedd..00000000 --- a/src/main/java/org/durid/sql/dialect/transact/ast/TransactSQLObject.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.transact.ast; - -import org.durid.sql.ast.SQLObject; - -public interface TransactSQLObject extends SQLObject { - -} diff --git a/src/main/java/org/durid/sql/dialect/transact/ast/TransactSQLObjectImpl.java b/src/main/java/org/durid/sql/dialect/transact/ast/TransactSQLObjectImpl.java deleted file mode 100644 index ad76690f..00000000 --- a/src/main/java/org/durid/sql/dialect/transact/ast/TransactSQLObjectImpl.java +++ /dev/null @@ -1,36 +0,0 @@ -///* -// * Copyright 1999-2011 Alibaba Group Holding Ltd. -// * -// * Licensed under the Apache License, Version 2.0 (the "License"); -// * you may not use this file except in compliance with the License. -// * You may obtain a copy of the License at -// * -// * http://www.apache.org/licenses/LICENSE-2.0 -// * -// * Unless required by applicable law or agreed to in writing, software -// * distributed under the License is distributed on an "AS IS" BASIS, -// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// * See the License for the specific language governing permissions and -// * limitations under the License. -// */ -//package org.durid.sql.dialect.transact.ast; -// -//import org.durid.sql.ast.SQLObjectImpl; -//import org.durid.sql.dialect.oracle.visitor.OracleASTVisitor; -//import org.durid.sql.visitor.SQLASTVisitor; -// -//public abstract class TransactSQLObjectImpl extends SQLObjectImpl implements TransactSQLObject { -// -// private static final long serialVersionUID = 1L; -// -// public TransactSQLObjectImpl(){ -// -// } -// -// @Override -// protected void accept0(SQLASTVisitor visitor) { -// this.accept0((OracleASTVisitor) visitor); -// } -// -// public abstract void accept0(OracleASTVisitor visitor); -//} diff --git a/src/main/java/org/durid/sql/dialect/transact/ast/TransactSQLSelect.java b/src/main/java/org/durid/sql/dialect/transact/ast/TransactSQLSelect.java deleted file mode 100644 index b2edb860..00000000 --- a/src/main/java/org/durid/sql/dialect/transact/ast/TransactSQLSelect.java +++ /dev/null @@ -1,20 +0,0 @@ -///* -// * Copyright 1999-2011 Alibaba Group Holding Ltd. -// * -// * Licensed under the Apache License, Version 2.0 (the "License"); -// * you may not use this file except in compliance with the License. -// * You may obtain a copy of the License at -// * -// * http://www.apache.org/licenses/LICENSE-2.0 -// * -// * Unless required by applicable law or agreed to in writing, software -// * distributed under the License is distributed on an "AS IS" BASIS, -// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// * See the License for the specific language governing permissions and -// * limitations under the License. -// */ -//package org.durid.sql.dialect.transact.ast; -// -//public class TransactSQLSelect { -// -//} diff --git a/src/main/java/org/durid/sql/parser/CharTypes.java b/src/main/java/org/durid/sql/parser/CharTypes.java deleted file mode 100644 index 7b6edb72..00000000 --- a/src/main/java/org/durid/sql/parser/CharTypes.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.parser; - -public class CharTypes { - - private final static boolean[] hexFlags = new boolean[256]; - static { - for (char c = 0; c < hexFlags.length; ++c) { - if (c >= 'A' && c <= 'F') { - hexFlags[c] = true; - } else if (c >= 'a' && c <= 'f') { - hexFlags[c] = true; - } else if (c >= '0' && c <= '9') { - hexFlags[c] = true; - } - } - } - - public static boolean isHex(char c) { - return c < 256 && hexFlags[c]; - } - - public static boolean isDigit(char c) { - return c >= '0' && c <= '9'; - } - - private final static boolean[] firstIdentifierFlags = new boolean[256]; - static { - for (char c = 0; c < firstIdentifierFlags.length; ++c) { - if (c >= 'A' && c <= 'Z') { - firstIdentifierFlags[c] = true; - } else if (c >= 'a' && c <= 'z') { - firstIdentifierFlags[c] = true; - } - } - firstIdentifierFlags['`'] = true; - firstIdentifierFlags['_'] = true; - firstIdentifierFlags['$'] = true; - } - - public static boolean isFirstIdentifierChar(char c) { - return c > firstIdentifierFlags.length || firstIdentifierFlags[c]; - } - - private final static boolean[] identifierFlags = new boolean[256]; - static { - for (char c = 0; c < identifierFlags.length; ++c) { - if (c >= 'A' && c <= 'Z') { - identifierFlags[c] = true; - } else if (c >= 'a' && c <= 'z') { - identifierFlags[c] = true; - } else if (c >= '0' && c <= '9') { - identifierFlags[c] = true; - } - } - // identifierFlags['`'] = true; - identifierFlags['_'] = true; - identifierFlags['$'] = true; - identifierFlags['#'] = true; - } - - public static boolean isIdentifierChar(char c) { - return c > identifierFlags.length || identifierFlags[c]; - } - - private final static boolean[] whitespaceFlags = new boolean[256]; - static { - whitespaceFlags[' '] = true; - whitespaceFlags['\n'] = true; - whitespaceFlags['\r'] = true; - whitespaceFlags['\t'] = true; - whitespaceFlags['\f'] = true; - whitespaceFlags['\b'] = true; - whitespaceFlags[160] = true; // 特别处理 - } - - /** - * @return false if {@link LayoutCharacters#EOI} - */ - public static boolean isWhitespace(char c) { - return c <= whitespaceFlags.length && whitespaceFlags[c]; - } - -} diff --git a/src/main/java/org/durid/sql/parser/Keywords.java b/src/main/java/org/durid/sql/parser/Keywords.java deleted file mode 100644 index e53e9148..00000000 --- a/src/main/java/org/durid/sql/parser/Keywords.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.parser; - -import java.util.HashMap; -import java.util.Map; - -/** - * @author wenshao - */ -public class Keywords { - - private final Map keywords; - - public final static Keywords DEFAULT_KEYWORDS; - - static { - Map map = new HashMap(); - - map.put("ALL", Token.ALL); - map.put("ALTER", Token.ALTER); - map.put("AND", Token.AND); - map.put("ANY", Token.ANY); - map.put("AS", Token.AS); - - map.put("ASC", Token.ASC); - map.put("BETWEEN", Token.BETWEEN); - map.put("BY", Token.BY); - map.put("CASE", Token.CASE); - map.put("CAST", Token.CAST); - - map.put("CHECK", Token.CHECK); - map.put("CONSTRAINT", Token.CONSTRAINT); - map.put("CREATE", Token.CREATE); - map.put("DATABASE", Token.DATABASE); - map.put("DEFAULT", Token.DEFAULT); - - map.put("DELETE", Token.DELETE); - map.put("DESC", Token.DESC); - map.put("DISTINCT", Token.DISTINCT); - map.put("DROP", Token.DROP); - map.put("ELSE", Token.ELSE); - - map.put("END", Token.END); - map.put("ESCAPE", Token.ESCAPE); - map.put("EXISTS", Token.EXISTS); - map.put("FOR", Token.FOR); - map.put("FOREIGN", Token.FOREIGN); - - map.put("FROM", Token.FROM); - map.put("FULL", Token.FULL); - map.put("GROUP", Token.GROUP); - map.put("HAVING", Token.HAVING); - map.put("IN", Token.IN); - - map.put("INDEX", Token.INDEX); - map.put("INNER", Token.INNER); - map.put("INSERT", Token.INSERT); - map.put("INTERSECT", Token.INTERSECT); - map.put("INTERVAL", Token.INTERVAL); - - map.put("INTO", Token.INTO); - map.put("IS", Token.IS); - map.put("JOIN", Token.JOIN); - map.put("KEY", Token.KEY); - map.put("LEFT", Token.LEFT); - - map.put("LIKE", Token.LIKE); - map.put("LOCK", Token.LOCK); - map.put("MINUS", Token.MINUS); - map.put("NEW", Token.NEW); - map.put("NOT", Token.NOT); - - map.put("NULL", Token.NULL); - map.put("ON", Token.ON); - map.put("OR", Token.OR); - map.put("ORDER", Token.ORDER); - map.put("OUTER", Token.OUTER); - - map.put("PRIMARY", Token.PRIMARY); - map.put("REFERENCES", Token.REFERENCES); - map.put("RIGHT", Token.RIGHT); - map.put("SCHEMA", Token.SCHEMA); - map.put("SELECT", Token.SELECT); - - map.put("SET", Token.SET); - map.put("SOME", Token.SOME); - map.put("TABLE", Token.TABLE); - map.put("THEN", Token.THEN); - map.put("TRUNCATE", Token.TRUNCATE); - - map.put("UNION", Token.UNION); - map.put("UNIQUE", Token.UNIQUE); - map.put("UPDATE", Token.UPDATE); - map.put("VALUES", Token.VALUES); - map.put("VIEW", Token.VIEW); - - map.put("WHEN", Token.WHEN); - map.put("WHERE", Token.WHERE); - map.put("XOR", Token.XOR); - - map.put("OVER", Token.OVER); - - DEFAULT_KEYWORDS = new Keywords(map); - } - - public boolean containsValue(Token token) { - return this.keywords.containsValue(token); - } - - public Keywords(Map keywords){ - this.keywords = keywords; - } - - public Token getKeyword(String key) { - key = key.toUpperCase(); - return keywords.get(key); - } - - public Map getKeywords() { - return keywords; - } - -} diff --git a/src/main/java/org/durid/sql/parser/LayoutCharacters.java b/src/main/java/org/durid/sql/parser/LayoutCharacters.java deleted file mode 100644 index f6bae8bf..00000000 --- a/src/main/java/org/durid/sql/parser/LayoutCharacters.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.parser; - -/** - * @author wenshao - */ -public interface LayoutCharacters { - - /** - * Tabulator column increment. - */ - final static int TabInc = 8; - - /** - * Tabulator character. - */ - final static byte TAB = 0x8; - - /** - * Line feed character. - */ - final static byte LF = 0xA; - - /** - * Form feed character. - */ - final static byte FF = 0xC; - - /** - * Carriage return character. - */ - final static byte CR = 0xD; - - /** - * QS_TODO 为什么不是0x0?
- * End of input character. Used as a sentinel to denote the character one beyond the last defined character in a - * source file. - */ - final static byte EOI = 0x1A; -} diff --git a/src/main/java/org/durid/sql/parser/Lexer.java b/src/main/java/org/durid/sql/parser/Lexer.java deleted file mode 100644 index 1499d55e..00000000 --- a/src/main/java/org/durid/sql/parser/Lexer.java +++ /dev/null @@ -1,978 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.parser; - -import static org.durid.sql.parser.CharTypes.isFirstIdentifierChar; -import static org.durid.sql.parser.CharTypes.isIdentifierChar; -import static org.durid.sql.parser.CharTypes.isWhitespace; -import static org.durid.sql.parser.LayoutCharacters.EOI; -import static org.durid.sql.parser.Token.COLONEQ; -import static org.durid.sql.parser.Token.COMMA; -import static org.durid.sql.parser.Token.EOF; -import static org.durid.sql.parser.Token.ERROR; -import static org.durid.sql.parser.Token.LBRACE; -import static org.durid.sql.parser.Token.LBRACKET; -import static org.durid.sql.parser.Token.LITERAL_ALIAS; -import static org.durid.sql.parser.Token.LITERAL_CHARS; -import static org.durid.sql.parser.Token.LPAREN; -import static org.durid.sql.parser.Token.RBRACE; -import static org.durid.sql.parser.Token.RBRACKET; -import static org.durid.sql.parser.Token.RPAREN; - -import java.math.BigDecimal; -import java.math.BigInteger; -import java.util.Arrays; - -/** - * @author wenshao - */ -public class Lexer { - - private final String text; - protected int pos; - protected int mark; - - protected char ch; - - protected char[] buf; - protected int bufPos; - - protected Token token; - - protected Keywords keywods = Keywords.DEFAULT_KEYWORDS; - - protected String stringVal; - - protected boolean skipComment = true; - - private SavePoint savePoint = null; - - /* - * anti sql injection - */ - private boolean allowComment = true; - - private int varIndex = -1; - - - public Lexer(String input){ - this(input, true); - } - - - public Lexer(String input, boolean skipComment){ - this.skipComment = skipComment; - - this.text = input; - this.pos = -1; - - scanChar(); - } - - - public Lexer(char[] input, int inputLength, boolean skipComment){ - this(new String(input, 0, inputLength), skipComment); - } - - public final char charAt(int index) { - if (index >= text.length()) { - return EOI; - } - - return text.charAt(index); - } - - public String addSymbol() { - return subString(mark, bufPos); - } - - public final String subString(int offset, int count) { - return text.substring(offset, offset + count); - } - - protected void initBuff(int size) { - if (buf == null) { - if (size < 32) { - buf = new char[32]; - } else { - buf = new char[size + 32]; - } - } else if(buf.length < size) { - buf = Arrays.copyOf(buf, size); - } - } - - public void arraycopy(int srcPos, char[] dest, int destPos, int length) { - text.getChars(srcPos, srcPos + length, dest, destPos); - } - - public boolean isAllowComment() { - return allowComment; - } - - public void setAllowComment(boolean allowComment) { - this.allowComment = allowComment; - } - - public int nextVarIndex() { - return ++varIndex; - } - - private static class SavePoint { - int bp; - int sp; - int np; - char ch; - Token token; - } - - public Keywords getKeywods() { - return keywods; - } - - public void mark() { - SavePoint savePoint = new SavePoint(); - savePoint.bp = pos; - savePoint.sp = bufPos; - savePoint.np = mark; - savePoint.ch = ch; - savePoint.token = token; - this.savePoint = savePoint; - } - - public void reset() { - this.pos = savePoint.bp; - this.bufPos = savePoint.sp; - this.mark = savePoint.np; - this.ch = savePoint.ch; - this.token = savePoint.token; - } - - protected final void scanChar() { - ch = charAt(++pos); - } - - protected void unscan() { - ch = charAt(--pos); - } - - public boolean isEOF() { - return pos >= text.length(); - } - - /** - * Report an error at the given position using the provided arguments. - */ - protected void lexError(String key, Object... args) { - token = ERROR; - } - - /** - * Return the current token, set by nextToken(). - */ - public final Token token() { - return token; - } - - public String info() { - return this.token + " " + this.stringVal(); - } - - public final void nextToken() { - bufPos = 0; - - for (;;) { - if (isWhitespace(ch)) { - scanChar(); - continue; - } - - if (ch == '$' && charAt(pos + 1) == '{') { - scanVariable(); - return; - } - - if (isFirstIdentifierChar(ch)) { - if (ch == 'N') { - if (charAt(pos + 1) == '\'') { - ++pos; - ch = '\''; - scanString(); - token = Token.LITERAL_NCHARS; - return; - } - } - - scanIdentifier(); - return; - } - - switch (ch) { - case '0': - if (charAt(pos + 1) == 'x') { - scanChar(); - scanChar(); - scanHexaDecimal(); - } else { - scanNumber(); - } - return; - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - scanNumber(); - return; - case ',': - scanChar(); - token = COMMA; - return; - case '(': - scanChar(); - token = LPAREN; - return; - case ')': - scanChar(); - token = RPAREN; - return; - case '[': - scanChar(); - token = LBRACKET; - return; - case ']': - scanChar(); - token = RBRACKET; - return; - case '{': - scanChar(); - token = LBRACE; - return; - case '}': - scanChar(); - token = RBRACE; - return; - case ':': - scanChar(); - if (ch == '=') { - scanChar(); - token = COLONEQ; - } else { - if (isDigit(ch)) { - unscan(); - scanVariable(); - } else { - unscan(); - scanVariable(); - } - } - return; - case '#': - scanVariable(); - return; - case '.': - scanChar(); - if (isDigit(ch)) { - unscan(); - scanNumber(); - return; - } else if (ch == '.') { - scanChar(); - if (ch == '.') { - scanChar(); - token = Token.DOTDOTDOT; - } else { - token = Token.DOTDOT; - } - } else { - token = Token.DOT; - } - return; - case '\'': - scanString(); - return; - case '\"': - scanAlias(); - return; - case '*': - scanChar(); - token = Token.STAR; - return; - case '?': - scanChar(); - token = Token.QUES; - return; - case ';': - scanChar(); - token = Token.SEMI; - return; - case '`': - throw new SQLParseException("TODO"); // TODO - case '@': - scanVariable(); - return; - case '-': - int subNextChar = charAt(pos + 1); - if (subNextChar == '-') { - scanComment(); - if ((token() == Token.LINE_COMMENT || token() == Token.MULTI_LINE_COMMENT) && skipComment) { - bufPos = 0; - continue; - } - } else { - scanOperator(); - } - return; - case '/': - int nextChar = charAt(pos + 1); - if (nextChar == '/' || nextChar == '*') { - scanComment(); - if ((token() == Token.LINE_COMMENT || token() == Token.MULTI_LINE_COMMENT) && skipComment) { - bufPos = 0; - continue; - } - } else { - token = Token.SLASH; - scanChar(); - } - return; - default: - if (Character.isLetter(ch)) { - scanIdentifier(); - return; - } - - if (isOperator(ch)) { - scanOperator(); - return; - } - - // QS_TODO ? - if (isEOF()) { // JLS - token = EOF; - } else { - lexError("illegal.char", String.valueOf((int) ch)); - scanChar(); - } - - return; - } - } - - } - - - /** - * Scan all values until first whitespace, spaces near ',' are ignored, - * so values like 'hello , world' will capture as one token. - * @return all names as string. - */ - public String scanNames() { - String restOfText = text.substring(pos); - String[] splittedText = restOfText.split(","); - - StringBuilder names = new StringBuilder(); - for (String textPart : splittedText) { - String trimmedTextPart = textPart.trim(); - - // is last part? - if(trimmedTextPart.contains(" ")) { - int whitespaceIndex = trimmedTextPart.indexOf(" "); - if(whitespaceIndex != -1) { - trimmedTextPart = trimmedTextPart.substring(0, whitespaceIndex); - } - names.append(trimmedTextPart); - while(isWhitespace(charAt(pos))) { - scanChar(); - } - pos += whitespaceIndex + 1; - break; - } - - names.append(trimmedTextPart + ","); - pos += textPart.length() + 1; - } - - ch = charAt(pos); - return names.toString(); - } - - private final void scanOperator() { - switch (ch) { - case '+': - scanChar(); - token = Token.PLUS; - break; - case '-': - scanChar(); - token = Token.SUB; - break; - case '*': - scanChar(); - token = Token.STAR; - break; - case '/': - scanChar(); - token = Token.SLASH; - break; - case '&': - scanChar(); - if (ch == '&') { - scanChar(); - token = Token.AMPAMP; - } else { - token = Token.AMP; - } - break; - case '|': - scanChar(); - if (ch == '|') { - scanChar(); - token = Token.BARBAR; - } else { - token = Token.BAR; - } - break; - case '^': - scanChar(); - token = Token.CARET; - break; - case '%': - scanChar(); - token = Token.PERCENT; - break; - case '=': - scanChar(); - if (ch == '=') { - scanChar(); - token = Token.EQEQ; - } else { - token = Token.EQ; - } - break; - case '>': - scanChar(); - if (ch == '=') { - scanChar(); - token = Token.GTEQ; - } else if (ch == '>') { - scanChar(); - token = Token.GTGT; - } else { - token = Token.GT; - } - break; - case '<': - scanChar(); - if (ch == '=') { - scanChar(); - if (ch == '>') { - token = Token.LTEQGT; - scanChar(); - } else { - token = Token.LTEQ; - } - } else if (ch == '>') { - scanChar(); - token = Token.LTGT; - } else if (ch == '<') { - scanChar(); - token = Token.LTLT; - } else { - token = Token.LT; - } - break; - case '!': - scanChar(); - if (ch == '=') { - scanChar(); - token = Token.BANGEQ; - } else if (ch == '>') { - scanChar(); - token = Token.BANGGT; - } else if (ch == '<') { - scanChar(); - token = Token.BANGLT; - } else { - token = Token.BANG; - } - break; - case '?': - scanChar(); - token = Token.QUES; - break; - case '~': - scanChar(); - token = Token.TILDE; - break; - default: - throw new SQLParseException("TODO"); - } - } - - protected void scanString() { - mark = pos; - boolean hasSpecial = false; - - for (;;) { - if (isEOF()) { - lexError("unclosed.str.lit"); - return; - } - - ch = charAt(++pos); - - if (ch == '\'') { - scanChar(); - if (ch != '\'') { - token = LITERAL_CHARS; - break; - } else { - if (!hasSpecial) { - initBuff(bufPos); - arraycopy(mark + 1, buf, 0, bufPos); - hasSpecial = true; - } - putChar('\''); - continue; - } - } - - if (!hasSpecial) { - bufPos++; - continue; - } - - if (bufPos == buf.length) { - putChar(ch); - } else { - buf[bufPos++] = ch; - } - } - - if (!hasSpecial) { - stringVal = subString(mark + 1, bufPos); - } else { - stringVal = new String(buf, 0, bufPos); - } - } - - private final void scanAlias() { - mark = pos; - - if (buf == null) { - buf = new char[32]; - } - - for (;;) { - if (isEOF()) { - lexError("unclosed.str.lit"); - return; - } - - ch = charAt(++pos); - - if (ch == '\"') { - scanChar(); - token = LITERAL_ALIAS; - break; - } - - if (bufPos == buf.length) { - putChar(ch); - } else { - buf[bufPos++] = ch; - } - } - - stringVal = subString(mark + 1, bufPos); - } - - public void scanVariable() { - if (ch != '@' && ch != ':' && ch != '#' && ch != '$') { - throw new SQLParseException("illegal variable"); - } - - mark = pos; - bufPos = 1; - char ch; - - boolean mybatisFlag = false; - if (charAt(pos + 1) == '@') { - ch = charAt(++pos); - - bufPos++; - } else if (charAt(pos + 1) == '{') { - pos++; - bufPos++; - mybatisFlag = true; - } - - for (;;) { - ch = charAt(++pos); - - if (!isIdentifierChar(ch)) { - break; - } - - bufPos++; - continue; - } - - if (mybatisFlag) { - if (ch != '}') { - throw new SQLParseException("syntax error"); - } - ++pos; - bufPos++; - } - - this.ch = charAt(pos); - - stringVal = addSymbol(); - token = Token.VARIANT; - } - - public void scanComment() { - if (!allowComment) { - throw new NotAllowCommentException(); - } - - if (ch != '/') { - throw new IllegalStateException(); - } - - mark = pos; - bufPos = 0; - scanChar(); - - if (ch == '*') { - scanChar(); - bufPos++; - - for (;;) { - if (ch == '*' && charAt(pos + 1) == '/') { - bufPos += 2; - scanChar(); - scanChar(); - break; - } - - scanChar(); - bufPos++; - } - - stringVal = subString(mark, bufPos); - token = Token.MULTI_LINE_COMMENT; - return; - } - - if (ch == '/') { - scanChar(); - bufPos++; - - for (;;) { - if (ch == '\r') { - if (charAt(pos + 1) == '\n') { - bufPos += 2; - scanChar(); - break; - } - bufPos++; - break; - } - - if (ch == '\n') { - scanChar(); - bufPos++; - break; - } - - scanChar(); - bufPos++; - } - - stringVal = subString(mark + 1, bufPos); - token = Token.LINE_COMMENT; - return; - } - } - - public void scanIdentifier() { - final char first = ch; - - final boolean firstFlag = isFirstIdentifierChar(first); - if (!firstFlag) { - throw new SQLParseException("illegal identifier"); - } - - mark = pos; - bufPos = 1; - char ch; - for (;;) { - ch = charAt(++pos); - - if (!isIdentifierChar(ch)) { - break; - } - - - bufPos++; - continue; - } - - this.ch = charAt(pos); - - stringVal = addSymbol(); - Token tok = keywods.getKeyword(stringVal); - if (tok != null) { - token = tok; - } else { - token = Token.IDENTIFIER; - } - } - - public void scanNumber() { - mark = pos; - - if (ch == '-') { - bufPos++; - ch = charAt(++pos); - } - - for (;;) { - if (ch >= '0' && ch <= '9') { - bufPos++; - } else { - break; - } - ch = charAt(++pos); - } - - boolean isDouble = false; - - if (ch == '.') { - if (charAt(pos + 1) == '.') { - token = Token.LITERAL_INT; - return; - } - bufPos++; - ch = charAt(++pos); - isDouble = true; - - for (;;) { - if (ch >= '0' && ch <= '9') { - bufPos++; - } else { - break; - } - ch = charAt(++pos); - } - } - - if (ch == 'e' || ch == 'E') { - bufPos++; - ch = charAt(++pos); - - if (ch == '+' || ch == '-') { - bufPos++; - ch = charAt(++pos); - } - - for (;;) { - if (ch >= '0' && ch <= '9') { - bufPos++; - } else { - break; - } - ch = charAt(++pos); - } - - isDouble = true; - } - - if (isDouble) { - token = Token.LITERAL_FLOAT; - } else { - token = Token.LITERAL_INT; - } - } - - public void scanHexaDecimal() { - mark = pos; - - if (ch == '-') { - bufPos++; - ch = charAt(++pos); - } - - for (;;) { - if (CharTypes.isHex(ch)) { - bufPos++; - } else { - break; - } - ch = charAt(++pos); - } - - token = Token.LITERAL_HEX; - } - - public String hexString() { - return subString(mark, bufPos); - } - - public final boolean isDigit(char ch) { - return ch >= '0' && ch <= '9'; - } - - /** - * Append a character to sbuf. - */ - protected final void putChar(char ch) { - if (bufPos == buf.length) { - char[] newsbuf = new char[buf.length * 2]; - System.arraycopy(buf, 0, newsbuf, 0, buf.length); - buf = newsbuf; - } - buf[bufPos++] = ch; - } - - /** - * Return the current token's position: a 0-based offset from beginning of the raw input stream (before unicode - * translation) - */ - public final int pos() { - return pos; - } - - /** - * The value of a literal token, recorded as a string. For integers, leading 0x and 'l' suffixes are suppressed. - */ - public final String stringVal() { - return stringVal; - } - - private boolean isOperator(char ch) { - switch (ch) { - case '!': - case '%': - case '&': - case '*': - case '+': - case '-': - case '<': - case '=': - case '>': - case '^': - case '|': - case '~': - case ';': - return true; - default: - return false; - } - } - - private static final long MULTMIN_RADIX_TEN = Long.MIN_VALUE / 10; - private static final long N_MULTMAX_RADIX_TEN = -Long.MAX_VALUE / 10; - - private final static int[] digits = new int[(int) '9' + 1]; - - static { - for (int i = '0'; i <= '9'; ++i) { - digits[i] = i - '0'; - } - } - - // QS_TODO negative number is invisible for lexer - public Number integerValue() { - long result = 0; - boolean negative = false; - int i = mark, max = mark + bufPos; - long limit; - long multmin; - int digit; - - if (charAt(mark) == '-') { - negative = true; - limit = Long.MIN_VALUE; - i++; - } else { - limit = -Long.MAX_VALUE; - } - multmin = negative ? MULTMIN_RADIX_TEN : N_MULTMAX_RADIX_TEN; - if (i < max) { - digit = digits[charAt(i++)]; - result = -digit; - } - while (i < max) { - // Accumulating negatively avoids surprises near MAX_VALUE - digit = digits[charAt(i++)]; - if (result < multmin) { - return new BigInteger(numberString()); - } - result *= 10; - if (result < limit + digit) { - return new BigInteger(numberString()); - } - result -= digit; - } - - if (negative) { - if (i > mark + 1) { - if (result >= Integer.MIN_VALUE) { - return (int) result; - } - return result; - } else { /* Only got "-" */ - throw new NumberFormatException(numberString()); - } - } else { - result = -result; - if (result <= Integer.MAX_VALUE) { - return (int) result; - } - return result; - } - } - - public int bp() { - return this.pos; - } - - public char current() { - return this.ch; - } - - public void reset(int mark, char markChar, Token token) { - this.pos = mark; - this.ch = markChar; - this.token = token; - } - - public final String numberString() { - return subString(mark, bufPos); - } - - public BigDecimal decimalValue() { - return new BigDecimal(text.toCharArray(), mark, bufPos); - } -} diff --git a/src/main/java/org/durid/sql/parser/NotAllowCommentException.java b/src/main/java/org/durid/sql/parser/NotAllowCommentException.java deleted file mode 100644 index 19a95080..00000000 --- a/src/main/java/org/durid/sql/parser/NotAllowCommentException.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.parser; - -public class NotAllowCommentException extends ParserException { - - private static final long serialVersionUID = 1L; - - public NotAllowCommentException(){ - super(); - } - - public NotAllowCommentException(String message, Throwable e){ - super(message, e); - } - - public NotAllowCommentException(String message){ - super(message); - } - -} diff --git a/src/main/java/org/durid/sql/parser/ParserException.java b/src/main/java/org/durid/sql/parser/ParserException.java deleted file mode 100644 index 0c8416c8..00000000 --- a/src/main/java/org/durid/sql/parser/ParserException.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.parser; - -import java.io.Serializable; - -public class ParserException extends RuntimeException implements Serializable { - - private static final long serialVersionUID = 1L; - - public ParserException(){ - } - - public ParserException(String message){ - super(message); - } - - public ParserException(String message, Throwable e){ - super(message, e); - } - - public ParserException(String message, int line, int col){ - super(message); - } - - public ParserException(Throwable ex, String ksql){ - super("parse error. detail message is :\n" + ex.getMessage() + "\nsource sql is : \n" + ksql, ex); - } -} diff --git a/src/main/java/org/durid/sql/parser/SQLCreateTableParser.java b/src/main/java/org/durid/sql/parser/SQLCreateTableParser.java deleted file mode 100644 index 33f427ad..00000000 --- a/src/main/java/org/durid/sql/parser/SQLCreateTableParser.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.parser; - -import org.durid.sql.ast.statement.SQLColumnDefinition; -import org.durid.sql.ast.statement.SQLCreateTableStatement; -import org.durid.sql.ast.statement.SQLPrimaryKey; - -public class SQLCreateTableParser extends SQLDDLParser { - - public SQLCreateTableParser(String sql){ - super(sql); - } - - public SQLCreateTableParser(SQLExprParser exprParser){ - super(exprParser); - } - - public SQLCreateTableStatement parseCrateTable() { - return parseCrateTable(true); - } - - public SQLCreateTableStatement parseCrateTable(boolean acceptCreate) { - if (acceptCreate) { - accept(Token.CREATE); - } - - SQLCreateTableStatement createTable = newCreateStatement(); - - if (identifierEquals("GLOBAL")) { - lexer.nextToken(); - - if (identifierEquals("TEMPORARY")) { - lexer.nextToken(); - createTable.setType(SQLCreateTableStatement.Type.GLOBAL_TEMPORARY); - } else { - throw new ParserException("syntax error " + lexer.token() + " " + lexer.stringVal()); - } - } else if (lexer.token() == Token.IDENTIFIER && lexer.stringVal().equalsIgnoreCase("LOCAL")) { - lexer.nextToken(); - if (lexer.token() == Token.IDENTIFIER && lexer.stringVal().equalsIgnoreCase("TEMPORAY")) { - lexer.nextToken(); - createTable.setType(SQLCreateTableStatement.Type.LOCAL_TEMPORARY); - } else { - throw new ParserException("syntax error"); - } - } - - accept(Token.TABLE); - - createTable.setName(this.exprParser.name()); - - if (lexer.token() == Token.LPAREN) { - lexer.nextToken(); - - for (;;) { - if (lexer.token() == Token.IDENTIFIER || lexer.token() == Token.LITERAL_ALIAS) { - SQLColumnDefinition column = this.exprParser.parseColumn(); - createTable.getTableElementList().add(column); - } else if (lexer.token() == Token.PRIMARY) { - SQLPrimaryKey primaryKey = exprParser.parsePrimaryKey(); - createTable.getTableElementList().add(primaryKey); - } else { - throw new ParserException("TODO " + lexer.token()); - } - - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; - } - - break; - } - - // while - // (this.tokenList.current().equals(OracleToken.ConstraintToken)) { - // parseConstaint(table.getConstaints()); - // - // if (this.tokenList.current().equals(OracleToken.CommaToken)) - // ; - // lexer.nextToken(); - // } - - accept(Token.RPAREN); - - } - - return createTable; - } - - protected SQLCreateTableStatement newCreateStatement() { - return new SQLCreateTableStatement(); - } -} diff --git a/src/main/java/org/durid/sql/parser/SQLDDLParser.java b/src/main/java/org/durid/sql/parser/SQLDDLParser.java deleted file mode 100644 index b41e3a97..00000000 --- a/src/main/java/org/durid/sql/parser/SQLDDLParser.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.parser; - -import org.durid.sql.ast.statement.SQLTableConstaint; - -public class SQLDDLParser extends SQLStatementParser { - - public SQLDDLParser(String sql){ - super(sql); - } - - public SQLDDLParser(SQLExprParser exprParser){ - super(exprParser); - } - - protected SQLTableConstaint parseConstraint() { - if (lexer.token() == Token.CONSTRAINT) { - lexer.nextToken(); - } - - if (lexer.token() == Token.IDENTIFIER) { - this.exprParser.name(); - throw new ParserException("TODO"); - } - - if (lexer.token() == Token.PRIMARY) { - lexer.nextToken(); - accept(Token.KEY); - - throw new ParserException("TODO"); - } - - throw new ParserException("TODO"); - } -} diff --git a/src/main/java/org/durid/sql/parser/SQLExprParser.java b/src/main/java/org/durid/sql/parser/SQLExprParser.java deleted file mode 100644 index 01e1006b..00000000 --- a/src/main/java/org/durid/sql/parser/SQLExprParser.java +++ /dev/null @@ -1,1274 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.parser; - -import java.math.BigInteger; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -import org.durid.sql.ast.SQLCommentHint; -import org.durid.sql.ast.SQLDataType; -import org.durid.sql.ast.SQLDataTypeImpl; -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLOrderBy; -import org.durid.sql.ast.SQLOrderingSpecification; -import org.durid.sql.ast.SQLOver; -import org.durid.sql.ast.expr.*; -import org.durid.sql.ast.statement.NotNullConstraint; -import org.durid.sql.ast.statement.SQLAssignItem; -import org.durid.sql.ast.statement.SQLCharactorDataType; -import org.durid.sql.ast.statement.SQLColumnDefinition; -import org.durid.sql.ast.statement.SQLPrimaryKey; -import org.durid.sql.ast.statement.SQLSelect; -import org.durid.sql.ast.statement.SQLSelectOrderByItem; - -public class SQLExprParser extends SQLParser { - - public SQLExprParser(String sql){ - super(sql); - } - - public SQLExprParser(Lexer lexer){ - super(lexer); - } - - public SQLExpr expr() { - if (lexer.token() == Token.STAR) { - lexer.nextToken(); - - return new SQLAllColumnExpr(); - } - - SQLExpr expr = primary(); - - if (lexer.token() == Token.COMMA) { - return expr; - } - - return exprRest(expr); - } - - public SQLExpr exprRest(SQLExpr expr) { - expr = bitXorRest(expr); - expr = multiplicativeRest(expr); - expr = additiveRest(expr); - expr = shiftRest(expr); - expr = bitAndRest(expr); - expr = bitOrRest(expr); - expr = inRest(expr); - expr = relationalRest(expr); - expr = equalityRest(expr); - expr = andRest(expr); - expr = orRest(expr); - - return expr; - } - - public final SQLExpr bitXor() { - SQLExpr expr = primary(); - return bitXorRest(expr); - } - - public SQLExpr bitXorRest(SQLExpr expr) { - if (lexer.token() == Token.CARET) { - lexer.nextToken(); - SQLExpr rightExp = primary(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BitwiseXor, rightExp); - expr = bitXorRest(expr); - } - - return expr; - } - - public final SQLExpr multiplicative() { - SQLExpr expr = primary(); - return multiplicativeRest(expr); - } - - public SQLExpr multiplicativeRest(SQLExpr expr) { - if (lexer.token() == Token.STAR) { - lexer.nextToken(); - SQLExpr rightExp = bitXor(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Multiply, rightExp); - expr = multiplicativeRest(expr); - } else if (lexer.token() == Token.SLASH) { - lexer.nextToken(); - SQLExpr rightExp = bitXor(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Divide, rightExp); - expr = multiplicativeRest(expr); - } else if (lexer.token() == Token.PERCENT) { - lexer.nextToken(); - SQLExpr rightExp = bitXor(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Modulus, rightExp); - expr = multiplicativeRest(expr); - } - return expr; - } - - public SQLExpr primary() { - SQLExpr sqlExpr = null; - - final Token tok = lexer.token(); - - switch (tok) { - case LPAREN: - lexer.nextToken(); - sqlExpr = expr(); - if (lexer.token() == Token.COMMA) { - SQLListExpr listExpr = new SQLListExpr(); - listExpr.getItems().add(sqlExpr); - do { - lexer.nextToken(); - listExpr.getItems().add(expr()); - } while (lexer.token() == Token.COMMA); - - sqlExpr = listExpr; - } - accept(Token.RPAREN); - if (sqlExpr instanceof SQLIdentifierExpr) { - ((SQLIdentifierExpr) sqlExpr).setWrappedInParens(true); - } - break; - case LBRACE: - lexer.nextToken(); - boolean foundRBrace = false; - if(lexer.stringVal().equals(Token.TS.name)){ - String current = lexer.stringVal(); - do { - if(current.equals(tok.RBRACE.name())){ - foundRBrace = true; - break; - } - lexer.nextToken(); - current = lexer.token().name(); - }while(!foundRBrace && !current.trim().equals("")); - - if(foundRBrace){ - SQLOdbcExpr sdle = new SQLOdbcExpr(lexer.stringVal()); - sqlExpr = sdle; - accept(Token.RBRACE); - }else{ - throw new ParserException("Error. Unable to find closing RBRACE"); - } - }else{ - throw new ParserException("Error. Unable to parse ODBC Literal Timestamp"); - } - break; - case INSERT: - lexer.nextToken(); - if (lexer.token() != Token.LPAREN) { - throw new ParserException("syntax error"); - } - sqlExpr = new SQLIdentifierExpr("INSERT"); - break; - case IDENTIFIER: - sqlExpr = new SQLIdentifierExpr(lexer.stringVal()); - lexer.nextToken(); - break; - case NEW: - throw new ParserException("TODO"); - case LITERAL_INT: - sqlExpr = new SQLIntegerExpr(lexer.integerValue()); - lexer.nextToken(); - break; - case LITERAL_FLOAT: - sqlExpr = new SQLNumberExpr(lexer.decimalValue()); - lexer.nextToken(); - break; - case LITERAL_CHARS: - sqlExpr = new SQLCharExpr(lexer.stringVal()); - lexer.nextToken(); - break; - case LITERAL_NCHARS: - sqlExpr = new SQLNCharExpr(lexer.stringVal()); - lexer.nextToken(); - break; - case VARIANT: - SQLVariantRefExpr varRefExpr = new SQLVariantRefExpr(lexer.stringVal()); - lexer.nextToken(); - if (varRefExpr.getName().equals("@") && lexer.token() == Token.LITERAL_CHARS) { - varRefExpr.setName("@'" + lexer.stringVal() + "'"); - lexer.nextToken(); - } else if (varRefExpr.getName().equals("@@") && lexer.token() == Token.LITERAL_CHARS) { - varRefExpr.setName("@@'" + lexer.stringVal() + "'"); - lexer.nextToken(); - } - sqlExpr = varRefExpr; - break; - case DEFAULT: - sqlExpr = new SQLDefaultExpr(); - lexer.nextToken(); - break; - case DUAL: - case KEY: - case DISTINCT: - case LIMIT: - case SCHEMA: - case COLUMN: - case IF: - sqlExpr = new SQLIdentifierExpr(lexer.stringVal()); - lexer.nextToken(); - break; - case CASE: - SQLCaseExpr caseExpr = new SQLCaseExpr(); - lexer.nextToken(); - if (lexer.token() != Token.WHEN) { - caseExpr.setValueExpr(expr()); - } - - accept(Token.WHEN); - SQLExpr testExpr = expr(); - accept(Token.THEN); - SQLExpr valueExpr = expr(); - SQLCaseExpr.Item caseItem = new SQLCaseExpr.Item(testExpr, valueExpr); - caseExpr.getItems().add(caseItem); - - while (lexer.token() == Token.WHEN) { - lexer.nextToken(); - testExpr = expr(); - accept(Token.THEN); - valueExpr = expr(); - caseItem = new SQLCaseExpr.Item(testExpr, valueExpr); - caseExpr.getItems().add(caseItem); - } - - if (lexer.token() == Token.ELSE) { - lexer.nextToken(); - caseExpr.setElseExpr(expr()); - } - - accept(Token.END); - - sqlExpr = caseExpr; - break; - case EXISTS: - lexer.nextToken(); - accept(Token.LPAREN); - sqlExpr = new SQLExistsExpr(createSelectParser().select()); - accept(Token.RPAREN); - break; - case NOT: - lexer.nextToken(); - if (lexer.token() == Token.EXISTS) { - lexer.nextToken(); - accept(Token.LPAREN); - sqlExpr = new SQLExistsExpr(createSelectParser().select(), true); - accept(Token.RPAREN); - } else if (lexer.token() == Token.LPAREN) { - lexer.nextToken(); - - SQLExpr notTarget = expr(); - - accept(Token.RPAREN); - notTarget = exprRest(notTarget); - - sqlExpr = new SQLNotExpr(notTarget); - - return primaryRest(sqlExpr); - } else { - SQLExpr restExpr = expr(); - sqlExpr = new SQLNotExpr(restExpr); - } - break; - case SELECT: - SQLQueryExpr queryExpr = new SQLQueryExpr(createSelectParser().select()); - sqlExpr = queryExpr; - break; - case CAST: - lexer.nextToken(); - accept(Token.LPAREN); - SQLCastExpr cast = new SQLCastExpr(); - cast.setExpr(expr()); - accept(Token.AS); - cast.setDataType(parseDataType()); - accept(Token.RPAREN); - - sqlExpr = cast; - break; - case SUB: - lexer.nextToken(); - switch (lexer.token()) { - case LITERAL_INT: - Number integerValue = lexer.integerValue(); - if (integerValue instanceof Integer) { - int intVal = ((Integer) integerValue).intValue(); - if (intVal == Integer.MIN_VALUE) { - integerValue = Long.valueOf(((long) intVal) * -1); - } else { - integerValue = Integer.valueOf(intVal * -1); - } - } else if (integerValue instanceof Long) { - long longVal = ((Long) integerValue).longValue(); - if (longVal == 2147483648L) { - integerValue = Integer.valueOf((int) (((long) longVal) * -1)); - } else { - integerValue = Long.valueOf(longVal * -1); - } - } else { - integerValue = ((BigInteger) integerValue).negate(); - } - sqlExpr = new SQLIntegerExpr(integerValue); - lexer.nextToken(); - break; - case LITERAL_FLOAT: - sqlExpr = new SQLNumberExpr(lexer.decimalValue().negate()); - lexer.nextToken(); - break; - case IDENTIFIER: // 当负号后面为字段的情况 - sqlExpr = new SQLIdentifierExpr('-' + lexer.stringVal()); - lexer.nextToken(); - break; - default: - throw new ParserException("TODO"); - } - break; - case PLUS: - lexer.nextToken(); - switch (lexer.token()) { - case LITERAL_INT: - sqlExpr = new SQLIntegerExpr(lexer.integerValue()); - lexer.nextToken(); - break; - case LITERAL_FLOAT: - sqlExpr = new SQLNumberExpr(lexer.decimalValue()); - lexer.nextToken(); - break; - default: - throw new ParserException("TODO"); - } - break; - case TILDE: - lexer.nextToken(); - SQLExpr unaryValueExpr = expr(); - SQLUnaryExpr unary = new SQLUnaryExpr(SQLUnaryOperator.Compl, unaryValueExpr); - sqlExpr = unary; - break; - case QUES: - lexer.nextToken(); - SQLVariantRefExpr quesVarRefExpr = new SQLVariantRefExpr("?"); - quesVarRefExpr.setIndex(lexer.nextVarIndex()); - sqlExpr = quesVarRefExpr; - break; - case LEFT: - sqlExpr = new SQLIdentifierExpr("LEFT"); - lexer.nextToken(); - break; - case RIGHT: - sqlExpr = new SQLIdentifierExpr("RIGHT"); - lexer.nextToken(); - break; - case DATABASE: - sqlExpr = new SQLIdentifierExpr("DATABASE"); - lexer.nextToken(); - break; - case LOCK: - sqlExpr = new SQLIdentifierExpr("LOCK"); - lexer.nextToken(); - break; - case NULL: - sqlExpr = new SQLNullExpr(); - lexer.nextToken(); - break; - case BANG: - lexer.nextToken(); - SQLExpr bangExpr = expr(); - sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Not, bangExpr); - break; - case LITERAL_HEX: - String hex = lexer.hexString(); - sqlExpr = new SQLHexExpr(hex); - lexer.nextToken(); - break; - case INTERVAL: - sqlExpr = parseInterval(); - break; - case COLON: - lexer.nextToken(); - if (lexer.token == Token.LITERAL_ALIAS) { - sqlExpr = new SQLVariantRefExpr(":\"" + lexer.stringVal() + "\""); - lexer.nextToken(); - } - break; - case ANY: - lexer.nextToken(); - if (lexer.token() == Token.LPAREN) { - SQLAnyExpr anyExpr = new SQLAnyExpr(); - - accept(Token.LPAREN); - SQLSelect anySubQuery = createSelectParser().select(); - anyExpr.setSubQuery(anySubQuery); - accept(Token.RPAREN); - - anySubQuery.setParent(anyExpr); - - sqlExpr = anyExpr; - } else { - sqlExpr = new SQLIdentifierExpr("ANY"); - } - break; - case SOME: - lexer.nextToken(); - SQLSomeExpr someExpr = new SQLSomeExpr(); - - accept(Token.LPAREN); - SQLSelect someSubQuery = createSelectParser().select(); - someExpr.setSubQuery(someSubQuery); - accept(Token.RPAREN); - - someSubQuery.setParent(someExpr); - - sqlExpr = someExpr; - break; - case ALL: - lexer.nextToken(); - SQLAllExpr allExpr = new SQLAllExpr(); - - accept(Token.LPAREN); - SQLSelect allSubQuery = createSelectParser().select(); - allExpr.setSubQuery(allSubQuery); - accept(Token.RPAREN); - - allSubQuery.setParent(allExpr); - - sqlExpr = allExpr; - break; - default: - throw new ParserException("ERROR. token : " + tok + " " + lexer.stringVal()); - } - - return primaryRest(sqlExpr); - } - - protected SQLExpr parseInterval() { - throw new ParserException("TODO"); - } - - public SQLSelectParser createSelectParser() { - return new SQLSelectParser(this); - } - - public SQLExpr primaryRest(SQLExpr expr) { - if (expr == null) { - throw new IllegalArgumentException("expr"); - } - - if (lexer.token() == Token.OF) { - if (expr instanceof SQLIdentifierExpr) { - String name = ((SQLIdentifierExpr) expr).getName(); - if ("CURRENT".equalsIgnoreCase(name)) { - lexer.nextToken(); - SQLName cursorName = this.name(); - return new SQLCurrentOfCursorExpr(cursorName); - } - } - } - - if (lexer.token() == Token.DOT) { - lexer.nextToken(); - - if (expr instanceof SQLCharExpr) { - String text = ((SQLCharExpr) expr).getText(); - expr = new SQLIdentifierExpr(text); - } - - expr = dotRest(expr); - return primaryRest(expr); - } else { - if (lexer.token() == Token.LPAREN) { - return methodRest(expr, true); - } - } - - return expr; - } - - protected SQLExpr methodRest(SQLExpr expr, boolean acceptLPAREN) { - if (acceptLPAREN) { - accept(Token.LPAREN); - } - - if (expr instanceof SQLName || expr instanceof SQLDefaultExpr) { - String methodName; - - SQLMethodInvokeExpr methodInvokeExpr; - if (expr instanceof SQLPropertyExpr) { - methodName = ((SQLPropertyExpr) expr).getName(); - methodInvokeExpr = new SQLMethodInvokeExpr(methodName); - methodInvokeExpr.setOwner(((SQLPropertyExpr) expr).getOwner()); - } else { - methodName = expr.toString(); - methodInvokeExpr = new SQLMethodInvokeExpr(methodName); - } - - if (isAggreateFunction(methodName)) { - SQLAggregateExpr aggregateExpr = parseAggregateExpr(methodName); - - return aggregateExpr; - } - - if (lexer.token() != Token.RPAREN) { - exprList(methodInvokeExpr.getParameters()); - } - - accept(Token.RPAREN); - - return primaryRest(methodInvokeExpr); - } - - throw new ParserException("not support token:" + lexer.token()); - } - - protected SQLExpr dotRest(SQLExpr expr) { - if (lexer.token() == Token.STAR) { - lexer.nextToken(); - expr = new SQLPropertyExpr(expr, "*"); - } else { - String name; - - if (lexer.token() == Token.IDENTIFIER || lexer.token() == Token.LITERAL_CHARS - || lexer.token() == Token.LITERAL_ALIAS) { - name = lexer.stringVal(); - lexer.nextToken(); - } else if (lexer.getKeywods().containsValue(lexer.token())) { - name = lexer.stringVal(); - lexer.nextToken(); - } else { - throw new ParserException("error : " + lexer.stringVal()); - } - - if (lexer.token() == Token.LPAREN) { - lexer.nextToken(); - - SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr(name); - methodInvokeExpr.setOwner(expr); - if (lexer.token() == Token.RPAREN) { - lexer.nextToken(); - } else { - if (lexer.token() == Token.PLUS) { - methodInvokeExpr.getParameters().add(new SQLIdentifierExpr("+")); - lexer.nextToken(); - } else { - exprList(methodInvokeExpr.getParameters()); - } - accept(Token.RPAREN); - } - expr = methodInvokeExpr; - } else { - expr = new SQLPropertyExpr(expr, name); - } - } - - expr = primaryRest(expr); - return expr; - } - - public final SQLExpr groupComparisionRest(SQLExpr expr) { - return expr; - } - - public final void names(Collection exprCol) { - if (lexer.token() == Token.RBRACE) { - return; - } - - if (lexer.token() == Token.EOF) { - return; - } - - exprCol.add(name()); - - while (lexer.token() == Token.COMMA) { - lexer.nextToken(); - exprCol.add(name()); - } - } - - public final void exprList(Collection exprCol) { - if (lexer.token() == Token.RPAREN || lexer.token() == Token.RBRACKET) { - return; - } - - if (lexer.token() == Token.EOF) { - return; - } - - SQLExpr expr = expr(); - exprCol.add(expr); - - while (lexer.token() == Token.COMMA) { - lexer.nextToken(); - expr = expr(); - exprCol.add(expr); - } - } - - public SQLName name() { - String identName; - if (lexer.token() == Token.LITERAL_ALIAS) { - identName = '"' + lexer.stringVal() + '"'; - lexer.nextToken(); - } else if (lexer.token() == Token.IDENTIFIER) { - identName = lexer.stringVal(); - - lexer.nextToken(); - } else if (lexer.token() == Token.LITERAL_CHARS) { - identName = '\'' + lexer.stringVal() + '\''; - lexer.nextToken(); - } else { - throw new ParserException("error " + lexer.token()); - } - - SQLName name = new SQLIdentifierExpr(identName); - - name = nameRest(name); - - return name; - } - - public SQLName nameRest(SQLName name) { - if (lexer.token() == Token.DOT) { - lexer.nextToken(); - - if (lexer.token() == Token.KEY) { - name = new SQLPropertyExpr(name, "KEY"); - lexer.nextToken(); - return name; - } - - if (lexer.token() != Token.LITERAL_ALIAS && lexer.token() != Token.IDENTIFIER - && (!lexer.getKeywods().containsValue(lexer.token()))) { - throw new ParserException("error, " + lexer.token()); - } - - if (lexer.token() == Token.LITERAL_ALIAS) { - name = new SQLPropertyExpr(name, '"' + lexer.stringVal() + '"'); - } else { - name = new SQLPropertyExpr(name, lexer.stringVal()); - } - lexer.nextToken(); - name = nameRest(name); - } - - return name; - } - - public boolean isAggreateFunction(String word) { - String[] aggregateFunctions = { "AVG", "COUNT", "MAX", "MIN", "STDDEV", "SUM" }; - - for (int i = 0; i < aggregateFunctions.length; ++i) { - if (aggregateFunctions[i].compareToIgnoreCase(word) == 0) { - return true; - } - } - - return false; - } - - protected SQLAggregateExpr parseAggregateExpr(String methodName) { - methodName = methodName.toUpperCase(); - - SQLAggregateExpr aggregateExpr; - if (lexer.token() == Token.ALL) { - aggregateExpr = new SQLAggregateExpr(methodName, SQLAggregateExpr.Option.ALL); - lexer.nextToken(); - } else if (lexer.token() == Token.DISTINCT) { - aggregateExpr = new SQLAggregateExpr(methodName, SQLAggregateExpr.Option.DISTINCT); - lexer.nextToken(); - } else { - aggregateExpr = new SQLAggregateExpr(methodName); - } - - exprList(aggregateExpr.getArguments()); - - accept(Token.RPAREN); - - if (lexer.token() == Token.OVER) { - lexer.nextToken(); - SQLOver over = new SQLOver(); - accept(Token.LPAREN); - - if (identifierEquals("PARTITION")) { - lexer.nextToken(); - accept(Token.BY); - - if (lexer.token() == (Token.LPAREN)) { - lexer.nextToken(); - exprList(over.getPartitionBy()); - accept(Token.RPAREN); - } else { - exprList(over.getPartitionBy()); - } - } - - - over.setOrderBy(parseOrderBy()); - - // if (over.getOrderBy() != null) { - // //TODO window - // } - - accept(Token.RPAREN); - aggregateExpr.setOver(over); - - } - - return aggregateExpr; - } - - public SQLOrderBy parseOrderBy() { - if (lexer.token() == Token.ORDER) { - SQLOrderBy orderBy = new SQLOrderBy(); - - lexer.nextToken(); - - accept(Token.BY); - - orderBy.getItems().add(parseSelectOrderByItem()); - - while (lexer.token() == Token.COMMA) { - lexer.nextToken(); - orderBy.getItems().add(parseSelectOrderByItem()); - } - - return orderBy; - } - - return null; - } - - public SQLSelectOrderByItem parseSelectOrderByItem() { - SQLSelectOrderByItem item = new SQLSelectOrderByItem(); - - item.setExpr(expr()); - - if (lexer.token() == Token.ASC) { - lexer.nextToken(); - item.setType(SQLOrderingSpecification.ASC); - } else if (lexer.token() == Token.DESC) { - lexer.nextToken(); - item.setType(SQLOrderingSpecification.DESC); - } - - return item; - } - - public final SQLExpr bitAnd() { - SQLExpr expr = shift(); - return bitAndRest(expr); - } - - public final SQLExpr bitAndRest(SQLExpr expr) { - while (lexer.token() == Token.AMP) { - lexer.nextToken(); - SQLExpr rightExp = shift(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BitwiseAnd, rightExp); - } - return expr; - } - - public final SQLExpr bitOr() { - SQLExpr expr = bitAnd(); - return bitOrRest(expr); - } - - public final SQLExpr bitOrRest(SQLExpr expr) { - if (lexer.token() == Token.BAR) { - lexer.nextToken(); - SQLExpr rightExp = bitAnd(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BitwiseOr, rightExp); - expr = bitAndRest(expr); - } else if (lexer.token() == Token.TILDE) { - lexer.nextToken(); - SQLExpr rightExp = bitAnd(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.InvertBits, rightExp); - expr = bitAndRest(expr); - } - return expr; - } - - public final SQLExpr equality() { - SQLExpr expr = shift(); - return equalityRest(expr); - } - - public SQLExpr equalityRest(SQLExpr expr) { - SQLExpr rightExp; - if (lexer.token() == Token.EQ) { - lexer.nextToken(); - rightExp = shift(); - - rightExp = equalityRest(rightExp); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Equality, rightExp); - } else if (lexer.token() == Token.BANGEQ) { - lexer.nextToken(); - rightExp = shift(); - - rightExp = equalityRest(rightExp); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotEqual, rightExp); - } else if (lexer.token() == Token.COLONEQ) { - lexer.nextToken(); - rightExp = expr(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Assignment, rightExp); - } - - return expr; - } - - public final SQLExpr inRest(SQLExpr expr) { - if (lexer.token() == Token.IN) { - lexer.nextToken(); - accept(Token.LPAREN); - - SQLInListExpr inListExpr = new SQLInListExpr(expr); - exprList(inListExpr.getTargetList()); - expr = inListExpr; - - accept(Token.RPAREN); - expr = inListExpr; - - if (inListExpr.getTargetList().size() == 1) { - SQLExpr targetExpr = inListExpr.getTargetList().get(0); - if (targetExpr instanceof SQLQueryExpr) { - SQLInSubQueryExpr inSubQueryExpr = new SQLInSubQueryExpr(); - inSubQueryExpr.setExpr(inListExpr.getExpr()); - inSubQueryExpr.setSubQuery(((SQLQueryExpr) targetExpr).getSubQuery()); - expr = inSubQueryExpr; - } - } - } - - return expr; - } - - public final SQLExpr additive() { - SQLExpr expr = multiplicative(); - return additiveRest(expr); - } - - public SQLExpr additiveRest(SQLExpr expr) { - if (lexer.token() == Token.PLUS) { - lexer.nextToken(); - SQLExpr rightExp = multiplicative(); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Add, rightExp); - expr = additiveRest(expr); - } else if (lexer.token() == Token.BARBAR) { - lexer.nextToken(); - SQLExpr rightExp = multiplicative(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Concat, rightExp); - expr = additiveRest(expr); - } else if (lexer.token() == Token.SUB) { - lexer.nextToken(); - SQLExpr rightExp = multiplicative(); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Subtract, rightExp); - expr = additiveRest(expr); - } - - return expr; - } - - public final SQLExpr shift() { - SQLExpr expr = additive(); - return shiftRest(expr); - } - - public SQLExpr shiftRest(SQLExpr expr) { - if (lexer.token() == Token.LTLT) { - lexer.nextToken(); - SQLExpr rightExp = additive(); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.LeftShift, rightExp); - expr = shiftRest(expr); - } else if (lexer.token() == Token.GTGT) { - lexer.nextToken(); - SQLExpr rightExp = additive(); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.RightShift, rightExp); - expr = shiftRest(expr); - } - - return expr; - } - - public SQLExpr and() { - SQLExpr expr = relational(); - return andRest(expr); - } - - public SQLExpr andRest(SQLExpr expr) { - for (;;) { - if (lexer.token() == Token.AND || lexer.token() == Token.AMPAMP) { - lexer.nextToken(); - SQLExpr rightExp = relational(); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BooleanAnd, rightExp); - } else { - break; - } - } - - return expr; - } - - public SQLExpr or() { - SQLExpr expr = and(); - return orRest(expr); - } - - public SQLExpr orRest(SQLExpr expr) { - - for (;;) { - if (lexer.token() == Token.OR) { - lexer.nextToken(); - SQLExpr rightExp = and(); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BooleanOr, rightExp); - } else if (lexer.token() == Token.XOR) { - lexer.nextToken(); - SQLExpr rightExp = and(); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BooleanXor, rightExp); - } else { - break; - } - } - - return expr; - } - - public SQLExpr relational() { - SQLExpr expr = equality(); - - return relationalRest(expr); - } - - public SQLExpr relationalRest(SQLExpr expr) { - SQLExpr rightExp; - - if (lexer.token() == Token.LT) { - SQLBinaryOperator op = SQLBinaryOperator.LessThan; - - lexer.nextToken(); - if (lexer.token() == Token.EQ) { - lexer.nextToken(); - op = SQLBinaryOperator.LessThanOrEqual; - } - - rightExp = bitOr(); - expr = new SQLBinaryOpExpr(expr, op, rightExp); - // expr = relationalRest(expr); - } else if (lexer.token() == Token.LTEQ) { - lexer.nextToken(); - rightExp = bitOr(); - - // rightExp = relationalRest(rightExp); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.LessThanOrEqual, rightExp); - } else if (lexer.token() == Token.LTEQGT) { - lexer.nextToken(); - rightExp = bitOr(); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.LessThanOrEqualOrGreaterThan, rightExp); - } else if (lexer.token() == Token.GT) { - SQLBinaryOperator op = SQLBinaryOperator.GreaterThan; - - lexer.nextToken(); - - if (lexer.token() == Token.EQ) { - lexer.nextToken(); - op = SQLBinaryOperator.GreaterThanOrEqual; - } - - rightExp = bitOr(); - - expr = new SQLBinaryOpExpr(expr, op, rightExp); - } else if (lexer.token() == Token.GTEQ) { - lexer.nextToken(); - rightExp = bitOr(); - - // rightExp = relationalRest(rightExp); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.GreaterThanOrEqual, rightExp); - } else if (lexer.token() == Token.BANGLT) { - lexer.nextToken(); - rightExp = bitOr(); - - // rightExp = relationalRest(rightExp); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotLessThan, rightExp); - } else if (lexer.token() == Token.BANGGT) { - lexer.nextToken(); - rightExp = bitOr(); - - rightExp = relationalRest(rightExp); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotGreaterThan, rightExp); - } else if (lexer.token() == Token.LTGT) { - lexer.nextToken(); - rightExp = bitOr(); - - // rightExp = relationalRest(rightExp); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.LessThanOrGreater, rightExp); - } else if (lexer.token() == Token.LIKE) { - lexer.nextToken(); - rightExp = bitOr(); - - // rightExp = relationalRest(rightExp); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Like, rightExp); - - if (lexer.token() == Token.ESCAPE) { - lexer.nextToken(); - rightExp = expr(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Escape, rightExp); - } - } else if (lexer.token() == (Token.NOT)) { - lexer.nextToken(); - expr = notRationalRest(expr); - } else if (lexer.token() == (Token.BETWEEN)) { - lexer.nextToken(); - SQLExpr beginExpr = bitOr(); - accept(Token.AND); - SQLExpr endExpr = bitOr(); - expr = new SQLBetweenExpr(expr, beginExpr, endExpr); - } else if (lexer.token() == (Token.IS)) { - lexer.nextToken(); - - if (lexer.token() == (Token.NOT)) { - lexer.nextToken(); - SQLExpr rightExpr = primary(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.IsNot, rightExpr); - } else { - SQLExpr rightExpr = primary(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Is, rightExpr); - } - } else if (lexer.token() == Token.IN) { - expr = inRest(expr); - } - - return expr; - } - - public SQLExpr notRationalRest(SQLExpr expr) { - if (lexer.token() == (Token.LIKE)) { - lexer.nextToken(); - SQLExpr rightExp = equality(); - - rightExp = relationalRest(rightExp); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotLike, rightExp); - - if (lexer.token() == Token.ESCAPE) { - lexer.nextToken(); - rightExp = expr(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Escape, rightExp); - } - } else if (lexer.token() == Token.IN) { - lexer.nextToken(); - accept(Token.LPAREN); - - SQLInListExpr inListExpr = new SQLInListExpr(expr, true); - exprList(inListExpr.getTargetList()); - expr = inListExpr; - - accept(Token.RPAREN); - - if (inListExpr.getTargetList().size() == 1) { - SQLExpr targetExpr = inListExpr.getTargetList().get(0); - if (targetExpr instanceof SQLQueryExpr) { - SQLInSubQueryExpr inSubQueryExpr = new SQLInSubQueryExpr(); - inSubQueryExpr.setNot(true); - inSubQueryExpr.setExpr(inListExpr.getExpr()); - inSubQueryExpr.setSubQuery(((SQLQueryExpr) targetExpr).getSubQuery()); - expr = inSubQueryExpr; - } - } - - expr = relationalRest(expr); - return expr; - } else if (lexer.token() == (Token.BETWEEN)) { - lexer.nextToken(); - SQLExpr beginExpr = bitOr(); - accept(Token.AND); - SQLExpr endExpr = bitOr(); - - expr = new SQLBetweenExpr(expr, true, beginExpr, endExpr); - - return expr; - } else { - throw new ParserException("TODO " + lexer.token()); - } - return expr; - } - - public SQLDataType parseDataType() { - - if (lexer.token() == Token.DEFAULT || lexer.token() == Token.NOT || lexer.token() == Token.NULL) { - return null; - } - - SQLName typeExpr = name(); - String typeName = typeExpr.toString(); - - if ("character".equalsIgnoreCase(typeName) && "varying".equalsIgnoreCase(lexer.stringVal())) { - typeName += ' ' + lexer.stringVal(); - lexer.nextToken(); - } - - SQLDataType dataType = new SQLDataTypeImpl(typeName); - return parseDataTypeRest(dataType); - } - - protected SQLDataType parseDataTypeRest(SQLDataType dataType) { - if (lexer.token() == Token.LPAREN) { - lexer.nextToken(); - exprList(dataType.getArguments()); - accept(Token.RPAREN); - - return parseCharTypeRest(dataType); - } - - return dataType; - } - - protected boolean isCharType(SQLDataType dataType) { - String dataTypeName = dataType.getName(); - - return "char".equalsIgnoreCase(dataTypeName) // - || "varchar".equalsIgnoreCase(dataTypeName) - || "nchar".equalsIgnoreCase(dataTypeName) - || "nvarchar".equalsIgnoreCase(dataTypeName) - // - ; - } - - protected SQLDataType parseCharTypeRest(SQLDataType dataType) { - if (!isCharType(dataType)) { - return dataType; - } - - SQLCharactorDataType charType = new SQLCharactorDataType(dataType.getName()); - charType.getArguments().addAll(dataType.getArguments()); - - if (identifierEquals("CHARACTER")) { - lexer.nextToken(); - - accept(Token.SET); - - if (lexer.token() != Token.IDENTIFIER && lexer.token() != Token.LITERAL_CHARS) { - throw new ParserException(); - } - charType.setCharSetName(lexer.stringVal()); - lexer.nextToken(); - - if (lexer.token() == Token.IDENTIFIER) { - if (lexer.stringVal().equalsIgnoreCase("COLLATE")) { - lexer.nextToken(); - - if (lexer.token() != Token.IDENTIFIER) { - throw new ParserException(); - } - charType.setCollate(lexer.stringVal()); - lexer.nextToken(); - } - } - } - return charType; - } - - public void accept(Token token) { - if (lexer.token() == token) { - lexer.nextToken(); - } else { - throw new SQLParseException("syntax error, expect " + token + ", actual " + lexer.token() + " " - + lexer.stringVal()); - } - } - - public SQLColumnDefinition parseColumn() { - SQLColumnDefinition column = new SQLColumnDefinition(); - column.setName(name()); - column.setDataType(parseDataType()); - - return parseColumnRest(column); - } - - public SQLColumnDefinition parseColumnRest(SQLColumnDefinition column) { - if (lexer.token() == Token.DEFAULT) { - lexer.nextToken(); - column.setDefaultExpr(bitOr()); - return parseColumnRest(column); - } - - if (lexer.token() == Token.NOT) { - lexer.nextToken(); - accept(Token.NULL); - column.getConstaints().add(new NotNullConstraint()); - return parseColumnRest(column); - } - - if (lexer.token() == Token.NULL) { - lexer.nextToken(); - column.setDefaultExpr(new SQLNullExpr()); - return parseColumnRest(column); - } - - return column; - } - - public SQLPrimaryKey parsePrimaryKey() { - throw new ParserException("TODO"); - } - - public SQLAssignItem parseAssignItem() { - SQLAssignItem item = new SQLAssignItem(); - - SQLExpr var = primary(); - - if (var instanceof SQLIdentifierExpr) { - var = new SQLVariantRefExpr(((SQLIdentifierExpr) var).getName()); - } - item.setTarget(var); - if (lexer.token() == Token.COLONEQ) { - lexer.nextToken(); - } else { - accept(Token.EQ); - } - item.setValue(expr()); - - return item; - } - - public List parseHints() { - List hints = new ArrayList(); - parseHints(hints); - return hints; - } - - @SuppressWarnings({ "unchecked", "rawtypes" }) - public void parseHints(List hints) { - if (lexer.token() == Token.HINT) { - hints.add(new SQLCommentHint(lexer.stringVal())); - lexer.nextToken(); - } - } -} diff --git a/src/main/java/org/durid/sql/parser/SQLParseException.java b/src/main/java/org/durid/sql/parser/SQLParseException.java deleted file mode 100644 index 1620048a..00000000 --- a/src/main/java/org/durid/sql/parser/SQLParseException.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.parser; - -import org.durid.DruidRuntimeException; - -/** - * @author wenshao - */ -public class SQLParseException extends DruidRuntimeException { - - private static final long serialVersionUID = 1L; - - public SQLParseException(){ - super(); - } - - public SQLParseException(String message){ - super(message); - } - - public SQLParseException(String message, Throwable cause){ - super(message, cause); - } -} diff --git a/src/main/java/org/durid/sql/parser/SQLParser.java b/src/main/java/org/durid/sql/parser/SQLParser.java deleted file mode 100644 index 10d09d5d..00000000 --- a/src/main/java/org/durid/sql/parser/SQLParser.java +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.parser; - -public class SQLParser { - - protected final Lexer lexer; - - public SQLParser(String sql){ - this(new Lexer(sql)); - this.lexer.nextToken(); - } - - public SQLParser(Lexer lexer){ - this.lexer = lexer; - } - - public final Lexer getLexer() { - return lexer; - } - - protected boolean identifierEquals(String text) { - return lexer.token() == Token.IDENTIFIER && lexer.stringVal().equalsIgnoreCase(text); - } - - protected void acceptIdentifier(String text) { - if (identifierEquals(text)) { - lexer.nextToken(); - } else { - setErrorEndPos(lexer.pos()); - throw new SQLParseException("syntax error, expect " + text + ", actual " + lexer.token()); - } - } - - protected String as() { - String alias = null; - - if (lexer.token() == Token.AS) { - lexer.nextToken(); - - if (lexer.token() == Token.LITERAL_ALIAS) { - alias = '"' + lexer.stringVal() + '"'; - lexer.nextToken(); - return alias; - } - - if (lexer.token() == Token.IDENTIFIER) { - alias = lexer.stringVal(); - lexer.nextToken(); - return alias; - } - - if (lexer.token() == Token.LITERAL_CHARS) { - alias = "'" + lexer.stringVal() + "'"; - lexer.nextToken(); - return alias; - } - - if (lexer.token() == Token.KEY || lexer.token() == Token.CASE) { - alias = lexer.token.name(); - lexer.nextToken(); - return alias; - } - - switch (lexer.token()) { - case KEY: - alias = lexer.token().name(); - lexer.nextToken(); - return alias; - default: - break; - } - - throw new ParserException("Error : " + lexer.token()); - } - - if (lexer.token() == Token.LITERAL_ALIAS) { - alias = '"' + lexer.stringVal() + '"'; - lexer.nextToken(); - } else if (lexer.token() == Token.IDENTIFIER) { - alias = lexer.stringVal(); - lexer.nextToken(); - } else if (lexer.token() == Token.LITERAL_CHARS) { - alias = "'" + lexer.stringVal() + "'"; - lexer.nextToken(); - } else if (lexer.token() == Token.CASE) { - alias = lexer.token.name(); - lexer.nextToken(); - } - - switch (lexer.token()) { - case KEY: - alias = lexer.token().name(); - lexer.nextToken(); - return alias; - default: - break; - } - - return alias; - } - - public void accept(Token token) { - if (lexer.token() == token) { - lexer.nextToken(); - } else { - setErrorEndPos(lexer.pos()); - throw new SQLParseException("syntax error, expect " + token + ", actual " + lexer.token() + " " - + lexer.stringVal()); - } - } - - public void match(Token token) { - if (lexer.token() != token) { - throw new SQLParseException("syntax error, expect " + token + ", actual " + lexer.token() + " " - + lexer.stringVal()); - } - } - - private int errorEndPos = -1; - - protected void setErrorEndPos(int errPos) { - if (errPos > errorEndPos) { - errorEndPos = errPos; - } - } - -} diff --git a/src/main/java/org/durid/sql/parser/SQLParserUtils.java b/src/main/java/org/durid/sql/parser/SQLParserUtils.java deleted file mode 100644 index 49b09c81..00000000 --- a/src/main/java/org/durid/sql/parser/SQLParserUtils.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.parser; - -import org.durid.sql.dialect.mysql.parser.MySqlExprParser; -import org.durid.sql.dialect.mysql.parser.MySqlStatementParser; -import org.durid.util.JdbcUtils; - -public class SQLParserUtils { - - public static SQLStatementParser createSQLStatementParser(String sql, String dbType) { - - if (JdbcUtils.H2.equals(dbType)) { - return new MySqlStatementParser(sql); - } - - return new SQLStatementParser(sql); - } - - public static SQLExprParser createExprParser(String sql, String dbType) { - - if (JdbcUtils.H2.equals(dbType)) { - return new MySqlExprParser(sql); - } - - if (JdbcUtils.MYSQL.equals(dbType)) { - return new MySqlExprParser(sql); - } - - return new SQLExprParser(sql); - } -} diff --git a/src/main/java/org/durid/sql/parser/SQLSelectParser.java b/src/main/java/org/durid/sql/parser/SQLSelectParser.java deleted file mode 100644 index 4a5c7ea3..00000000 --- a/src/main/java/org/durid/sql/parser/SQLSelectParser.java +++ /dev/null @@ -1,348 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.parser; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLOrderBy; -import org.durid.sql.ast.SQLSetQuantifier; -import org.durid.sql.ast.statement.SQLExprTableSource; -import org.durid.sql.ast.statement.SQLJoinTableSource; -import org.durid.sql.ast.statement.SQLSelect; -import org.durid.sql.ast.statement.SQLSelectGroupByClause; -import org.durid.sql.ast.statement.SQLSelectItem; -import org.durid.sql.ast.statement.SQLSelectQuery; -import org.durid.sql.ast.statement.SQLSelectQueryBlock; -import org.durid.sql.ast.statement.SQLSubqueryTableSource; -import org.durid.sql.ast.statement.SQLTableSource; -import org.durid.sql.ast.statement.SQLUnionOperator; -import org.durid.sql.ast.statement.SQLUnionQuery; - -public class SQLSelectParser extends SQLParser { - protected SQLExprParser exprParser; - - public SQLSelectParser(String sql){ - super(sql); - } - - public SQLSelectParser(Lexer lexer){ - super(lexer); - } - - public SQLSelectParser(SQLExprParser exprParser){ - super(exprParser.getLexer()); - this.exprParser = exprParser; - } - - public SQLSelect select() { - SQLSelect select = new SQLSelect(); - - select.setQuery(query()); - select.setOrderBy(parseOrderBy()); - - if (select.getOrderBy() == null) { - select.setOrderBy(parseOrderBy()); - } - - return select; - } - - protected SQLUnionQuery createSQLUnionQuery() { - return new SQLUnionQuery(); - } - - public SQLUnionQuery unionRest(SQLUnionQuery union) { - if (lexer.token() == Token.ORDER) { - SQLOrderBy orderBy = this.exprParser.parseOrderBy(); - union.setOrderBy(orderBy); - return unionRest(union); - } - return union; - } - - public SQLSelectQuery queryRest(SQLSelectQuery selectQuery) { - if (lexer.token() == Token.UNION) { - lexer.nextToken(); - - SQLUnionQuery union = createSQLUnionQuery(); - union.setLeft(selectQuery); - - if (lexer.token() == Token.ALL) { - union.setOperator(SQLUnionOperator.UNION_ALL); - lexer.nextToken(); - } else if (lexer.token() == Token.DISTINCT) { - union.setOperator(SQLUnionOperator.DISTINCT); - lexer.nextToken(); - } - SQLSelectQuery right = this.query(); - union.setRight(right); - - return unionRest(union); - } - - if (lexer.token() == Token.INTERSECT) { - lexer.nextToken(); - - SQLUnionQuery union = new SQLUnionQuery(); - union.setLeft(selectQuery); - - union.setOperator(SQLUnionOperator.INTERSECT); - - SQLSelectQuery right = this.query(); - union.setRight(right); - - return union; - } - - if (lexer.token() == Token.MINUS) { - lexer.nextToken(); - - SQLUnionQuery union = new SQLUnionQuery(); - union.setLeft(selectQuery); - - union.setOperator(SQLUnionOperator.MINUS); - - SQLSelectQuery right = this.query(); - union.setRight(right); - - return union; - } - - return selectQuery; - } - - public SQLSelectQuery query() { - if (lexer.token() == Token.LPAREN) { - lexer.nextToken(); - - SQLSelectQuery select = query(); - accept(Token.RPAREN); - - return queryRest(select); - } - - accept(Token.SELECT); - - SQLSelectQueryBlock queryBlock = new SQLSelectQueryBlock(); - - if (lexer.token() == Token.DISTINCT) { - queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT); - lexer.nextToken(); - } else if (lexer.token() == Token.UNIQUE) { - queryBlock.setDistionOption(SQLSetQuantifier.UNIQUE); - lexer.nextToken(); - } else if (lexer.token() == Token.ALL) { - queryBlock.setDistionOption(SQLSetQuantifier.ALL); - lexer.nextToken(); - } - - parseSelectList(queryBlock); - - parseFrom(queryBlock); - - parseWhere(queryBlock); - - parseGroupBy(queryBlock); - - return queryRest(queryBlock); - } - - protected void parseWhere(SQLSelectQueryBlock queryBlock) { - if (lexer.token() == Token.WHERE) { - lexer.nextToken(); - - queryBlock.setWhere(expr()); - } - } - - protected void parseGroupBy(SQLSelectQueryBlock queryBlock) { - if (lexer.token() == Token.GROUP) { - lexer.nextToken(); - accept(Token.BY); - - SQLSelectGroupByClause groupBy = new SQLSelectGroupByClause(); - while (true) { - groupBy.getItems().add(expr()); - if (lexer.token() != Token.COMMA) { - break; - } - - lexer.nextToken(); - } - - if (lexer.token() == Token.HAVING) { - lexer.nextToken(); - - groupBy.setHaving(expr()); - } - - queryBlock.setGroupBy(groupBy); - } else if (lexer.token() == (Token.HAVING)) { - lexer.nextToken(); - - SQLSelectGroupByClause groupBy = new SQLSelectGroupByClause(); - groupBy.setHaving(this.expr()); - queryBlock.setGroupBy(groupBy); - } - } - - protected void parseSelectList(SQLSelectQueryBlock queryBlock) { - queryBlock.getSelectList().add(new SQLSelectItem(expr(), as())); - - while (lexer.token() == Token.COMMA) { - lexer.nextToken(); - queryBlock.getSelectList().add(new SQLSelectItem(expr(), as())); - } - } - - public void parseFrom(SQLSelectQueryBlock queryBlock) { - if (lexer.token() != Token.FROM) { - return; - } - - SQLTableSource source = new SQLExprTableSource(lexer.scanNames()); - queryBlock.setFrom(source); - lexer.nextToken(); - } - - public SQLTableSource parseTableSource() { - if (lexer.token() == Token.LPAREN) { - lexer.nextToken(); - SQLTableSource tableSource; - if (lexer.token() == Token.SELECT) { - SQLSelect select = select(); - accept(Token.RPAREN); - queryRest(select.getQuery()); - tableSource = new SQLSubqueryTableSource(select); - } else if (lexer.token() == Token.LPAREN) { - tableSource = parseTableSource(); - accept(Token.RPAREN); - } else { - tableSource = parseTableSource(); - accept(Token.RPAREN); - } - - return parseTableSourceRest(tableSource); - } - - if (lexer.token() == Token.SELECT) { - throw new ParserException("TODO"); - } - - SQLExprTableSource tableReference = new SQLExprTableSource(); - - parseTableSourceQueryTableExpr(tableReference); - - return parseTableSourceRest(tableReference); - } - - private void parseTableSourceQueryTableExpr(SQLExprTableSource tableReference) { - if (lexer.token() == Token.LITERAL_ALIAS || lexer.token() == Token.IDENTIFIED - || lexer.token() == Token.LITERAL_CHARS) { - tableReference.setExpr(this.exprParser.name()); - return; - } - tableReference.setExpr(expr()); - } - - protected SQLTableSource parseTableSourceRest(SQLTableSource tableSource) { - if ((tableSource.getAlias() == null) || (tableSource.getAlias().length() == 0)) { - if (lexer.token() != Token.LEFT && lexer.token() != Token.RIGHT && lexer.token() != Token.FULL) { - String alias = as(); - if (alias != null) { - tableSource.setAlias(alias); - return parseTableSourceRest(tableSource); - } - } - } - - SQLJoinTableSource.JoinType joinType = null; - - if (lexer.token() == Token.LEFT) { - lexer.nextToken(); - if (lexer.token() == Token.OUTER) { - lexer.nextToken(); - } - - accept(Token.JOIN); - joinType = SQLJoinTableSource.JoinType.LEFT_OUTER_JOIN; - } else if (lexer.token() == Token.RIGHT) { - lexer.nextToken(); - if (lexer.token() == Token.OUTER) { - lexer.nextToken(); - } - accept(Token.JOIN); - joinType = SQLJoinTableSource.JoinType.RIGHT_OUTER_JOIN; - } else if (lexer.token() == Token.FULL) { - lexer.nextToken(); - if (lexer.token() == Token.OUTER) { - lexer.nextToken(); - } - accept(Token.JOIN); - joinType = SQLJoinTableSource.JoinType.FULL_OUTER_JOIN; - } else if (lexer.token() == Token.INNER) { - lexer.nextToken(); - accept(Token.JOIN); - joinType = SQLJoinTableSource.JoinType.INNER_JOIN; - } else if (lexer.token() == Token.JOIN) { - lexer.nextToken(); - joinType = SQLJoinTableSource.JoinType.JOIN; - } else if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - joinType = SQLJoinTableSource.JoinType.COMMA; - } else if (identifierEquals("STRAIGHT_JOIN")) { - lexer.nextToken(); - joinType = SQLJoinTableSource.JoinType.STRAIGHT_JOIN; - } else if (identifierEquals("CROSS")) { - lexer.nextToken(); - accept(Token.JOIN); - joinType = SQLJoinTableSource.JoinType.CROSS_JOIN; - } - - if (joinType != null) { - SQLJoinTableSource join = new SQLJoinTableSource(); - join.setLeft(tableSource); - join.setJoinType(joinType); - join.setRight(parseTableSource()); - - if (lexer.token() == Token.ON) { - lexer.nextToken(); - join.setCondition(expr()); - } - - return parseTableSourceRest(join); - } - - return tableSource; - } - - public SQLExpr expr() { - return this.exprParser.expr(); - } - - public SQLOrderBy parseOrderBy() { - return this.exprParser.parseOrderBy(); - } - - public void acceptKeyword(String ident) { - if (lexer.token() == Token.IDENTIFIER && ident.equalsIgnoreCase(lexer.stringVal())) { - lexer.nextToken(); - } else { - setErrorEndPos(lexer.pos()); - throw new SQLParseException("syntax error, expect " + ident + ", actual " + lexer.token()); - } - } - -} diff --git a/src/main/java/org/durid/sql/parser/SQLStatementParser.java b/src/main/java/org/durid/sql/parser/SQLStatementParser.java deleted file mode 100644 index de1dbe18..00000000 --- a/src/main/java/org/durid/sql/parser/SQLStatementParser.java +++ /dev/null @@ -1,652 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.parser; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLStatement; -import org.durid.sql.ast.expr.SQLQueryExpr; -import org.durid.sql.ast.statement.SQLAssignItem; -import org.durid.sql.ast.statement.SQLCallStatement; -import org.durid.sql.ast.statement.SQLCommentStatement; -import org.durid.sql.ast.statement.SQLCreateDatabaseStatement; -import org.durid.sql.ast.statement.SQLCreateTableStatement; -import org.durid.sql.ast.statement.SQLCreateViewStatement; -import org.durid.sql.ast.statement.SQLDeleteStatement; -import org.durid.sql.ast.statement.SQLDropIndexStatement; -import org.durid.sql.ast.statement.SQLDropTableStatement; -import org.durid.sql.ast.statement.SQLDropViewStatement; -import org.durid.sql.ast.statement.SQLExprTableSource; -import org.durid.sql.ast.statement.SQLInsertInto; -import org.durid.sql.ast.statement.SQLInsertStatement; -import org.durid.sql.ast.statement.SQLReleaseSavePointStatement; -import org.durid.sql.ast.statement.SQLRollbackStatement; -import org.durid.sql.ast.statement.SQLSavePointStatement; -import org.durid.sql.ast.statement.SQLSelectStatement; -import org.durid.sql.ast.statement.SQLSetStatement; -import org.durid.sql.ast.statement.SQLTableSource; -import org.durid.sql.ast.statement.SQLTruncateStatement; -import org.durid.sql.ast.statement.SQLUpdateSetItem; -import org.durid.sql.ast.statement.SQLUpdateStatement; -import org.durid.sql.ast.statement.SQLUseStatement; - -public class SQLStatementParser extends SQLParser { - - protected SQLExprParser exprParser; - - public SQLStatementParser(String sql){ - this(new SQLExprParser(sql)); - } - - public SQLStatementParser(SQLExprParser exprParser){ - super(exprParser.getLexer()); - this.exprParser = exprParser; - } - - public SQLExprParser getExprParser() { - return exprParser; - } - - public List parseStatementList() { - List statementList = new ArrayList(); - parseStatementList(statementList); - return statementList; - } - - public void parseStatementList(List statementList) { - for (;;) { - if (lexer.token() == Token.EOF) { - return; - } - - if (lexer.token() == (Token.SEMI)) { - lexer.nextToken(); - continue; - } - - if (lexer.token() == (Token.SELECT)) { - statementList.add(parseSelect()); - continue; - } - - if (lexer.token() == (Token.UPDATE)) { - statementList.add(parseUpdateStatement()); - continue; - } - - if (lexer.token() == (Token.CREATE)) { - statementList.add(parseCreate()); - continue; - } - - if (lexer.token() == (Token.INSERT)) { - SQLStatement insertStatement = parseInsert(); - statementList.add(insertStatement); - - continue; - } - - if (lexer.token() == (Token.DELETE)) { - statementList.add(parseDeleteStatement()); - continue; - } - - if (lexer.token() == Token.SET) { - statementList.add(parseSet()); - continue; - } - - if (lexer.token() == Token.ALTER) { - statementList.add(parseAlter()); - continue; - } - - if (lexer.token() == Token.DROP) { - lexer.nextToken(); - - if (lexer.token() == Token.TABLE || identifierEquals("TEMPORARY")) { - - SQLDropTableStatement stmt = parseDropTable(false); - - statementList.add(stmt); - continue; - } else if (identifierEquals("USER")) { - SQLStatement stmt = parseDropUser(); - statementList.add(stmt); - continue; - } else if (lexer.token() == Token.INDEX) { - SQLStatement stmt = parseDropIndex(); - statementList.add(stmt); - continue; - } else if (lexer.token() == Token.VIEW) { - SQLStatement stmt = parseDropView(false); - statementList.add(stmt); - continue; - } else { - throw new ParserException("TODO " + lexer.token()); - } - } - - if (lexer.token() == Token.TRUNCATE) { - SQLStatement stmt = parseTruncate(); - statementList.add(stmt); - continue; - } - - if (lexer.token() == Token.USE) { - SQLStatement stmt = parseUse(); - statementList.add(stmt); - continue; - } - - if (lexer.token() == Token.LBRACE || identifierEquals("CALL")) { - SQLCallStatement stmt = parseCall(); - statementList.add(stmt); - continue; - } - - if (identifierEquals("RENAME")) { - SQLStatement stmt = parseRename(); - statementList.add(stmt); - continue; - } - - if (identifierEquals("RELEASE")) { - SQLStatement stmt = parseReleaseSavePoint(); - statementList.add(stmt); - continue; - } - - if (identifierEquals("SAVEPOINT")) { - SQLStatement stmt = parseSavePoint(); - statementList.add(stmt); - continue; - } - - if (identifierEquals("ROLLBACK")) { - SQLRollbackStatement stmt = parseRollback(); - - statementList.add(stmt); - continue; - } - - if (identifierEquals("COMMIT")) { - SQLStatement stmt = parseCommit(); - - statementList.add(stmt); - continue; - } - - if (identifierEquals("SHOW")) { - SQLStatement stmt = parseShow(); - - statementList.add(stmt); - continue; - } - - if (lexer.token() == Token.LPAREN) { - char markChar = lexer.current(); - int markBp = lexer.bp(); - lexer.nextToken(); - if (lexer.token() == Token.SELECT) { - lexer.reset(markBp, markChar, Token.LPAREN); - SQLStatement stmt = parseSelect(); - statementList.add(stmt); - continue; - } - } - - if (parseStatementListDialect(statementList)) { - continue; - } - - if (lexer.token() == Token.HINT) { - lexer.nextToken(); - continue; - } - - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } - } - - public SQLRollbackStatement parseRollback() { - lexer.nextToken(); - - if (identifierEquals("WORK")) { - lexer.nextToken(); - } - - SQLRollbackStatement stmt = new SQLRollbackStatement(); - - if (identifierEquals("TO")) { - lexer.nextToken(); - - if (identifierEquals("SAVEPOINT")) { - lexer.nextToken(); - } - - stmt.setTo(this.exprParser.name()); - } - return stmt; - } - - public SQLStatement parseCommit() { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } - - public SQLStatement parseShow() { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } - - public SQLUseStatement parseUse() { - accept(Token.USE); - SQLUseStatement stmt = new SQLUseStatement(); - stmt.setDatabase(this.exprParser.name()); - return stmt; - } - - public SQLStatement parseSavePoint() { - acceptIdentifier("SAVEPOINT"); - SQLSavePointStatement stmt = new SQLSavePointStatement(); - stmt.setName(this.exprParser.name()); - return stmt; - } - - public SQLStatement parseReleaseSavePoint() { - acceptIdentifier("RELEASE"); - acceptIdentifier("SAVEPOINT"); - SQLReleaseSavePointStatement stmt = new SQLReleaseSavePointStatement(); - stmt.setName(this.exprParser.name()); - return stmt; - } - - public SQLStatement parseAlter() { - accept(Token.ALTER); - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } - - public SQLStatement parseRename() { - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } - - protected SQLDropTableStatement parseDropTable(boolean acceptDrop) { - if (acceptDrop) { - accept(Token.DROP); - } - accept(Token.TABLE); - - SQLDropTableStatement stmt = new SQLDropTableStatement(); - - for (;;) { - SQLName name = this.exprParser.name(); - stmt.getTableSources().add(new SQLExprTableSource(name)); - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; - } - break; - } - return stmt; - } - - protected SQLDropViewStatement parseDropView(boolean acceptDrop) { - if (acceptDrop) { - accept(Token.DROP); - } - accept(Token.VIEW); - - SQLDropViewStatement stmt = new SQLDropViewStatement(); - - for (;;) { - SQLName name = this.exprParser.name(); - stmt.getTableSources().add(new SQLExprTableSource(name)); - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; - } - break; - } - return stmt; - } - - public SQLStatement parseTruncate() { - accept(Token.TRUNCATE); - if (lexer.token() == Token.TABLE) { - lexer.nextToken(); - } - SQLTruncateStatement stmt = new SQLTruncateStatement(); - - for (;;) { - SQLName name = this.exprParser.name(); - stmt.addTableSource(name); - - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; - } - - break; - } - - return stmt; - } - - public SQLStatement parseInsert() { - SQLInsertStatement insertStatement = new SQLInsertStatement(); - - if (lexer.token() == Token.INSERT) { - accept(Token.INSERT); - } - - parseInsert0(insertStatement); - return insertStatement; - } - - protected void parseInsert0(SQLInsertInto insertStatement) { - parseInsert0(insertStatement, true); - } - - protected void parseInsert0_hinits(SQLInsertInto insertStatement) { - - } - - protected void parseInsert0(SQLInsertInto insertStatement, boolean acceptSubQuery) { - if (lexer.token() == Token.INTO) { - lexer.nextToken(); - - SQLName tableName = this.exprParser.name(); - insertStatement.setTableName(tableName); - - if (lexer.token() == Token.LITERAL_ALIAS) { - insertStatement.setAlias(as()); - } - - parseInsert0_hinits(insertStatement); - - if (lexer.token() == Token.IDENTIFIER) { - insertStatement.setAlias(lexer.stringVal()); - lexer.nextToken(); - } - } - - if (lexer.token() == (Token.LPAREN)) { - lexer.nextToken(); - this.exprParser.exprList(insertStatement.getColumns()); - accept(Token.RPAREN); - } - - if (lexer.token() == Token.VALUES) { - lexer.nextToken(); - accept(Token.LPAREN); - SQLInsertStatement.ValuesClause values = new SQLInsertStatement.ValuesClause(); - this.exprParser.exprList(values.getValues()); - insertStatement.setValues(values); - accept(Token.RPAREN); - } else if (acceptSubQuery && (lexer.token() == Token.SELECT || lexer.token() == Token.LPAREN)) { - SQLQueryExpr queryExpr = (SQLQueryExpr) this.exprParser.expr(); - insertStatement.setQuery(queryExpr.getSubQuery()); - } - } - - public boolean parseStatementListDialect(List statementList) { - return false; - } - - public SQLStatement parseDropUser() { - throw new ParserException("TODO " + lexer.token()); - } - - public SQLStatement parseDropIndex() { - accept(Token.INDEX); - SQLDropIndexStatement stmt = new SQLDropIndexStatement(); - stmt.setIndexName(this.exprParser.name()); - accept(Token.ON); - stmt.setTableName(this.exprParser.name()); - return stmt; - } - - public SQLCallStatement parseCall() { - - boolean brace = false; - if (lexer.token() == Token.LBRACE) { - lexer.nextToken(); - brace = true; - } - - acceptIdentifier("CALL"); - - SQLCallStatement stmt = new SQLCallStatement(); - stmt.setProcedureName(exprParser.name()); - - if (lexer.token() == Token.LPAREN) { - lexer.nextToken(); - exprParser.exprList(stmt.getParameters()); - accept(Token.RPAREN); - } - - if (brace) { - accept(Token.RBRACE); - } - - return stmt; - } - - public SQLStatement parseSet() { - accept(Token.SET); - SQLSetStatement stmt = new SQLSetStatement(); - - parseAssignItems(stmt.getItems()); - - return stmt; - } - - public void parseAssignItems(List items) { - for (;;) { - SQLAssignItem item = exprParser.parseAssignItem(); - items.add(item); - - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; - } else { - break; - } - } - } - - public SQLStatement parseCreate() { - char markChar = lexer.current(); - int markBp = lexer.bp(); - - accept(Token.CREATE); - - Token token = lexer.token(); - - if (token == Token.TABLE || identifierEquals("GLOBAL")) { - SQLCreateTableParser createTableParser = getSQLCreateTableParser(); - return createTableParser.parseCrateTable(false); - } else if (token == Token.INDEX || token == Token.UNIQUE) { - return parseCreateIndex(false); - } else if (identifierEquals("SEQUENCE")) { - return parseCreateSequence(false); - } else if (token == Token.OR) { - lexer.nextToken(); - acceptIdentifier("REPLACE"); - if (lexer.token() == Token.PROCEDURE) { - lexer.reset(markBp, markChar, Token.CREATE); - return parseCreateProcedure(); - } - - // lexer.reset(mark_bp, mark_ch, Token.CREATE); - throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal()); - } else if (token == Token.DATABASE) { - return parseCreateDatabase(); - } - - throw new ParserException("TODO " + lexer.token()); - } - - public SQLStatement parseCreateDatabase() { - if (lexer.token() == Token.CREATE) { - lexer.nextToken(); - } - - accept(Token.DATABASE); - - SQLCreateDatabaseStatement stmt = new SQLCreateDatabaseStatement(); - stmt.setName(this.exprParser.name()); - return stmt; - } - - public SQLStatement parseCreateProcedure() { - throw new ParserException("TODO " + lexer.token()); - } - - public SQLStatement parseCreateSequence(boolean acceptCreate) { - throw new ParserException("TODO " + lexer.token()); - } - - public SQLStatement parseCreateIndex(boolean acceptCreate) { - throw new ParserException("TODO " + lexer.token()); - } - - public SQLCreateTableParser getSQLCreateTableParser() { - return new SQLCreateTableParser(this.exprParser); - } - - public SQLSelectStatement parseSelect() { - return new SQLSelectStatement(createSQLSelectParser().select()); - } - - public SQLSelectParser createSQLSelectParser() { - return new SQLSelectParser(this.exprParser); - } - - public SQLUpdateStatement parseUpdateStatement() { - SQLUpdateStatement udpateStatement = createUpdateStatement(); - - if (lexer.token() == Token.UPDATE) { - lexer.nextToken(); - - SQLTableSource tableSource = this.exprParser.createSelectParser().parseTableSource(); - udpateStatement.setTableSource(tableSource); - } - - accept(Token.SET); - - for (;;) { - SQLUpdateSetItem item = new SQLUpdateSetItem(); - item.setColumn(this.exprParser.name()); - accept(Token.EQ); - item.setValue(this.exprParser.expr()); - - udpateStatement.getItems().add(item); - - if (lexer.token() == (Token.COMMA)) { - lexer.nextToken(); - continue; - } - - break; - } - - if (lexer.token() == (Token.WHERE)) { - lexer.nextToken(); - udpateStatement.setWhere(this.exprParser.expr()); - } - - return udpateStatement; - } - - protected SQLUpdateStatement createUpdateStatement() { - return new SQLUpdateStatement(); - } - - public SQLDeleteStatement parseDeleteStatement() { - SQLDeleteStatement deleteStatement = new SQLDeleteStatement(); - - if (lexer.token() == Token.DELETE) { - lexer.nextToken(); - if (lexer.token() == (Token.FROM)) { - lexer.nextToken(); - } - - SQLExpr tableName = exprParser.expr(); - - deleteStatement.setTableSource(tableName); - } - - if (lexer.token() == (Token.WHERE)) { - lexer.nextToken(); - SQLExpr where = this.exprParser.expr(); - deleteStatement.setWhere(where); - } - - return deleteStatement; - } - - public SQLCreateTableStatement parseCreateTable() { - // SQLCreateTableParser parser = new SQLCreateTableParser(this.lexer); - // return parser.parseCrateTable(); - throw new ParserException("TODO"); - } - - public SQLCreateViewStatement parseCreateView() { - SQLCreateViewStatement createView = new SQLCreateViewStatement(); - - this.accept(Token.CREATE); - - this.accept(Token.VIEW); - - createView.setName(exprParser.name()); - - if (lexer.token() == Token.LPAREN) { - lexer.nextToken(); - this.exprParser.exprList(createView.getColumns()); - accept(Token.RPAREN); - } - - this.accept(Token.AS); - - createView.setSubQuery(new SQLSelectParser(this.exprParser).select()); - return createView; - } - - public SQLCommentStatement parseComment() { - accept(Token.COMMENT); - SQLCommentStatement stmt = new SQLCommentStatement(); - - accept(Token.ON); - - if (lexer.token() == Token.TABLE) { - stmt.setType(SQLCommentStatement.Type.TABLE); - lexer.nextToken(); - } else if (lexer.token() == Token.COLUMN) { - stmt.setType(SQLCommentStatement.Type.COLUMN); - lexer.nextToken(); - } - - stmt.setOn(this.exprParser.name()); - - accept(Token.IS); - stmt.setComment(this.exprParser.expr()); - - return stmt; - } -} diff --git a/src/main/java/org/durid/sql/parser/Token.java b/src/main/java/org/durid/sql/parser/Token.java deleted file mode 100644 index af0de84f..00000000 --- a/src/main/java/org/durid/sql/parser/Token.java +++ /dev/null @@ -1,242 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.parser; - -/** - * - * SQL Token - * @author wenshao 2011-5-18 下午05:16:49 - * @formatter:off - */ -public enum Token { - FOR("FOR"), - IF("IF"), - INDEX("INDEX"), - PRIMARY("PRIMARY"), - KEY("KEY"), - DEFAULT("DEFAULT"), - CONSTRAINT("CONSTRAINT"), - CHECK("CHECK"), - VIEW("VIEW"), - CREATE("CREATE"), - ALTER("ALTER"), - DROP("DROP"), - TABLE("TABLE"), - UPDATE("UPDATE"), - SET("SET"), - SELECT("SELECT"), - FROM("FROM"), - WHERE("WHERE"), - ORDER("ORDER"), - BY("BY"), - GROUP("GROUP"), - HAVING("HAVING"), - INSERT("INSERT"), - INTO("INTO"), - NULL("NULL"), - NOT("NOT"), - AS("AS"), - DELETE("DELETE"), - DISTINCT("DISTINCT"), - UNIQUE("UNIQUE"), - FOREIGN("FOREIGN"), - REFERENCES("REFERENCES"), - ALL("ALL"), - UNION("UNION"), - INTERSECT("INTERSECT"), - MINUS("MINUS"), - INNER("INNER"), - LEFT("LEFT"), - RIGHT("RIGHT"), - FULL("FULL"), - OUTER("OUTER"), - JOIN("JOIN"), - ON("ON"), - SCHEMA("SCHEMA"), - CAST("CAST"), - COLUMN("COLUMN"), - USE("USE"), - DATABASE("DATABASE"), - - AND("AND"), - OR("OR"), - XOR("XOR"), - CASE("CASE"), - WHEN("WHEN"), - THEN("THEN"), - ELSE("ELSE"), - END("END"), - EXISTS("EXISTS"), - IN("IN"), - - NEW("NEW"), - ASC("ASC"), - DESC("DESC"), - IS("IS"), - LIKE("LIKE"), - ESCAPE("ESCAPE"), - BETWEEN("BETWEEN"), - VALUES("VALUES"), - INTERVAL("INTERVAL"), - - LOCK("LOCK"), - SOME("SOME"), - ANY("ANY"), - TRUNCATE("TRUNCATE"), - - // mysql - TRUE("TRUE"), - FALSE("FALSE"), - LIMIT("LIMIT"), - KILL("KILL"), - IDENTIFIED("IDENTIFIED"), - PASSWORD("PASSWORD"), - DUAL("DUAL"), - - //postgresql - WINDOW("WINDOW"), - OFFSET("OFFSET"), - ROW("ROW"), - ROWS("ROWS"), - ONLY("ONLY"), - FIRST("FIRST"), - NEXT("NEXT"), - FETCH("FETCH"), - OF("OF"), - SHARE("SHARE"), - NOWAIT("NOWAIT"), - RECURSIVE("RECURSIVE"), - TEMPORARY("TEMPORARY"), - TEMP("TEMP"), - UNLOGGED("UNLOGGED"), - RESTART("RESTART"), - IDENTITY("IDENTITY"), - CONTINUE("CONTINUE"), - CASCADE("CASCADE"), - RESTRICT("RESTRICT"), - USING("USING"), - CURRENT("CURRENT"), - RETURNING("RETURNING"), - COMMENT("COMMENT"), - OVER("OVER"), - - // oracle - START("START"), - PRIOR("PRIOR"), - CONNECT("CONNECT"), - WITH("WITH"), - EXTRACT("EXTRACT"), - CURSOR("CURSOR"), - MODEL("MODEL"), - MERGE("MERGE"), - MATCHED("MATCHED"), - ERRORS("ERRORS"), - REJECT("REJECT"), - UNLIMITED("UNLIMITED"), - BEGIN("BEGIN"), - EXCLUSIVE("EXCLUSIVE"), - MODE("MODE"), - WAIT("WAIT"), - ADVISE("ADVISE"), - SESSION("SESSION"), - PROCEDURE("PROCEDURE"), - LOCAL("LOCAL"), - SYSDATE("SYSDATE"), - DECLARE("DECLARE"), - EXCEPTION("EXCEPTION"), - GRANT("GRANT"), - LOOP("LOOP"), - GOTO("GOTO"), - COMMIT("COMMIT"), - SAVEPOINT("SAVEPOINT"), - CROSS("CROSS"), - - // transact-sql - TOP("TOP"), - - // hive - - EOF, - ERROR, - IDENTIFIER, - HINT, - VARIANT, - LITERAL_INT, - LITERAL_FLOAT, - LITERAL_HEX, - LITERAL_CHARS, - LITERAL_NCHARS, - - LITERAL_ALIAS, - LINE_COMMENT, - MULTI_LINE_COMMENT, - - // Oracle - BINARY_FLOAT, - BINARY_DOUBLE, - - LPAREN("("), - RPAREN(")"), - LBRACE("{"), - RBRACE("}"), - LBRACKET("["), - RBRACKET("]"), - SEMI(";"), - COMMA(","), - DOT("."), - DOTDOT(".."), - DOTDOTDOT("..,"), - EQ("="), - GT(">"), - LT("<"), - BANG("!"), - TILDE("~"), - QUES("?"), - COLON(":"), - COLONEQ(":="), - EQEQ("=="), - LTEQ("<="), - LTEQGT("<=>"), - LTGT("<>"), - GTEQ(">="), - BANGEQ("!="), - BANGGT("!>"), - BANGLT("!<"), - AMPAMP("&&"), - BARBAR("||"), - PLUS("+"), - SUB("-"), - STAR("*"), - SLASH("/"), - AMP("&"), - BAR("|"), - CARET("^"), - PERCENT("%"), - LTLT("<<"), - GTGT(">>"), - MONKEYS_AT("@"), - TS("ts"); - - public final String name; - - Token(){ - this(null); - } - - Token(String name){ - this.name = name; - } -} diff --git a/src/main/java/org/durid/sql/visitor/ExportParameterVisitor.java b/src/main/java/org/durid/sql/visitor/ExportParameterVisitor.java deleted file mode 100644 index 0f982ad4..00000000 --- a/src/main/java/org/durid/sql/visitor/ExportParameterVisitor.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.visitor; - -import java.util.List; - - -public interface ExportParameterVisitor extends SQLASTVisitor { - List getParameters(); -} diff --git a/src/main/java/org/durid/sql/visitor/ExportParameterVisitorUtils.java b/src/main/java/org/durid/sql/visitor/ExportParameterVisitorUtils.java deleted file mode 100644 index 0fff9c11..00000000 --- a/src/main/java/org/durid/sql/visitor/ExportParameterVisitorUtils.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.visitor; - -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.expr.SQLBetweenExpr; -import org.durid.sql.ast.expr.SQLBinaryOpExpr; -import org.durid.sql.ast.expr.SQLCharExpr; -import org.durid.sql.ast.expr.SQLLiteralExpr; -import org.durid.sql.ast.expr.SQLNumericLiteralExpr; -import org.durid.sql.ast.expr.SQLVariantRefExpr; -import org.durid.sql.dialect.mysql.ast.expr.MySqlBooleanExpr; - -public class ExportParameterVisitorUtils { - - public static boolean exportParamterAndAccept(final List parameters, List list) { - for (int i = 0, size = list.size(); i < size; ++i) { - SQLExpr param = list.get(i); - - SQLExpr result = exportParameter(parameters, param); - if (result != param) { - list.set(i, result); - } - } - - return false; - } - - public static SQLExpr exportParameter(final List parameters, final SQLExpr param) { - if (param instanceof SQLCharExpr) { - Object value = ((SQLCharExpr) param).getText(); - parameters.add(value); - return new SQLVariantRefExpr("?"); - } - - if (param instanceof MySqlBooleanExpr) { - Object value = ((MySqlBooleanExpr) param).getValue(); - parameters.add(value); - return new SQLVariantRefExpr("?"); - } - - if (param instanceof SQLNumericLiteralExpr) { - Object value = ((SQLNumericLiteralExpr) param).getNumber(); - parameters.add(value); - return new SQLVariantRefExpr("?"); - } - - return param; - } - - public static void exportParameter(final List parameters, SQLBinaryOpExpr x) { - if (x.getLeft() instanceof SQLLiteralExpr && x.getRight() instanceof SQLLiteralExpr && x.getOperator().isRelational()) { - return; - } - - { - SQLExpr leftResult = ExportParameterVisitorUtils.exportParameter(parameters, x.getLeft()); - if (leftResult != x.getLeft()) { - x.setLeft(leftResult); - } - } - - { - SQLExpr rightResult = exportParameter(parameters, x.getRight()); - if (rightResult != x.getRight()) { - x.setRight(rightResult); - } - } - } - - public static void exportParameter(final List parameters, SQLBetweenExpr x) { - { - SQLExpr result = exportParameter(parameters, x.getBeginExpr()); - if (result != x.getBeginExpr()) { - x.setBeginExpr(result); - } - } - - { - SQLExpr result = exportParameter(parameters, x.getEndExpr()); - if (result != x.getBeginExpr()) { - x.setEndExpr(result); - } - } - - } -} diff --git a/src/main/java/org/durid/sql/visitor/ParameterizedOutputVisitor.java b/src/main/java/org/durid/sql/visitor/ParameterizedOutputVisitor.java deleted file mode 100644 index 5f492307..00000000 --- a/src/main/java/org/durid/sql/visitor/ParameterizedOutputVisitor.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.visitor; - -import org.durid.sql.ast.expr.SQLBinaryOpExpr; -import org.durid.sql.ast.expr.SQLCharExpr; -import org.durid.sql.ast.expr.SQLInListExpr; -import org.durid.sql.ast.expr.SQLIntegerExpr; -import org.durid.sql.ast.expr.SQLNCharExpr; -import org.durid.sql.ast.expr.SQLNullExpr; -import org.durid.sql.ast.expr.SQLNumberExpr; - -public class ParameterizedOutputVisitor extends SQLASTOutputVisitor { - public ParameterizedOutputVisitor() { - this (new StringBuilder()); - } - - public ParameterizedOutputVisitor(Appendable appender){ - super(appender); - } - - public boolean visit(SQLInListExpr x) { - return ParameterizedOutputVisitorUtils.visit(this, x); - } - - public boolean visit(SQLBinaryOpExpr x) { - x = ParameterizedOutputVisitorUtils.merge(x); - - return super.visit(x); - } - - public boolean visit(SQLNullExpr x) { - print('?'); - return false; - } - - public boolean visit(SQLIntegerExpr x) { - if (Boolean.TRUE.equals(x.getAttribute(ParameterizedOutputVisitorUtils.ATTR_PARAMS_SKIP))) { - return super.visit(x); - } - - print('?'); - return false; - } - - public boolean visit(SQLNumberExpr x) { - if (Boolean.TRUE.equals(x.getAttribute(ParameterizedOutputVisitorUtils.ATTR_PARAMS_SKIP))) { - return super.visit(x); - } - - print('?'); - return false; - } - - public boolean visit(SQLCharExpr x) { - if (Boolean.TRUE.equals(x.getAttribute(ParameterizedOutputVisitorUtils.ATTR_PARAMS_SKIP))) { - return super.visit(x); - } - - print('?'); - return false; - } - - public boolean visit(SQLNCharExpr x) { - if (Boolean.TRUE.equals(x.getAttribute(ParameterizedOutputVisitorUtils.ATTR_PARAMS_SKIP))) { - return super.visit(x); - } - - print('?'); - return false; - } -} diff --git a/src/main/java/org/durid/sql/visitor/ParameterizedOutputVisitorUtils.java b/src/main/java/org/durid/sql/visitor/ParameterizedOutputVisitorUtils.java deleted file mode 100644 index 0e0dce0a..00000000 --- a/src/main/java/org/durid/sql/visitor/ParameterizedOutputVisitorUtils.java +++ /dev/null @@ -1,173 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.visitor; - -import java.util.List; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLStatement; -import org.durid.sql.ast.expr.SQLBinaryOpExpr; -import org.durid.sql.ast.expr.SQLBinaryOperator; -import org.durid.sql.ast.expr.SQLInListExpr; -import org.durid.sql.ast.expr.SQLLiteralExpr; -import org.durid.sql.ast.expr.SQLVariantRefExpr; -import org.durid.sql.dialect.mysql.visitor.MySqlParameterizedOutputVisitor; -import org.durid.sql.parser.SQLParserUtils; -import org.durid.sql.parser.SQLStatementParser; -import org.durid.util.JdbcUtils; - -public class ParameterizedOutputVisitorUtils { - - public static final String ATTR_PARAMS_SKIP = "druid.parameterized.skip"; - - public static String parameterize(String sql, String dbType) { - SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType); - List statementList = parser.parseStatementList(); - SQLStatement statemen = statementList.get(0); - - StringBuilder out = new StringBuilder(); - SQLASTOutputVisitor visitor = createParameterizedOutputVisitor(out, dbType); - statemen.accept(visitor); - - return out.toString(); - } - - public static SQLASTOutputVisitor createParameterizedOutputVisitor(Appendable out, String dbType) { - - - if (JdbcUtils.MYSQL.equals(dbType)) { - return new MySqlParameterizedOutputVisitor(out); - } - - if (JdbcUtils.H2.equals(dbType)) { - return new MySqlParameterizedOutputVisitor(out); - } - - return new ParameterizedOutputVisitor(out); - } - - public static boolean visit(SQLASTOutputVisitor v, SQLInListExpr x) { - x.getExpr().accept(v); - - if (x.isNot()) { - v.print(" NOT IN (?)"); - } else { - v.print(" IN (?)"); - } - - return false; - } - - public static SQLBinaryOpExpr merge(SQLBinaryOpExpr x) { - if (x.getLeft() instanceof SQLLiteralExpr && x.getRight() instanceof SQLLiteralExpr) { - if (x.getOperator() == SQLBinaryOperator.Equality || x.getOperator() == SQLBinaryOperator.NotEqual) { - x.getLeft().getAttributes().put(ATTR_PARAMS_SKIP, true); - x.getRight().getAttributes().put(ATTR_PARAMS_SKIP, true); - } - return x; - } - - if (x.getRight() instanceof SQLLiteralExpr) { - x = new SQLBinaryOpExpr(x.getLeft(), x.getOperator(), new SQLVariantRefExpr("?")); - } - - if (x.getLeft() instanceof SQLLiteralExpr) { - x = new SQLBinaryOpExpr(new SQLVariantRefExpr("?"), x.getOperator(), x.getRight()); - } - - for (;;) { - if (x.getRight() instanceof SQLBinaryOpExpr) { - if (x.getLeft() instanceof SQLBinaryOpExpr) { - SQLBinaryOpExpr leftBinaryExpr = (SQLBinaryOpExpr) x.getLeft(); - if (leftBinaryExpr.getRight().equals(x.getRight())) { - x = leftBinaryExpr; - continue; - } - } - x = new SQLBinaryOpExpr(x.getLeft(), x.getOperator(), merge((SQLBinaryOpExpr) x.getRight())); - } - - break; - } - - if (x.getLeft() instanceof SQLBinaryOpExpr) { - x = new SQLBinaryOpExpr(merge((SQLBinaryOpExpr) x.getLeft()), x.getOperator(), x.getRight()); - } - - // ID = ? OR ID = ? => ID = ? - if (x.getOperator() == SQLBinaryOperator.BooleanOr) { - if ((x.getLeft() instanceof SQLBinaryOpExpr) && (x.getRight() instanceof SQLBinaryOpExpr)) { - SQLBinaryOpExpr left = (SQLBinaryOpExpr) x.getLeft(); - SQLBinaryOpExpr right = (SQLBinaryOpExpr) x.getRight(); - - if (mergeEqual(left, right)) { - return left; - } - - if (isLiteralExpr(left.getLeft()) && left.getOperator() == SQLBinaryOperator.BooleanOr) { - if (mergeEqual(left.getRight(), right)) { - return left; - } - } - } - } - - return x; - } - - private static boolean mergeEqual(SQLExpr a, SQLExpr b) { - if (!(a instanceof SQLBinaryOpExpr)) { - return false; - } - if (!(b instanceof SQLBinaryOpExpr)) { - return false; - } - - SQLBinaryOpExpr binaryA = (SQLBinaryOpExpr) a; - SQLBinaryOpExpr binaryB = (SQLBinaryOpExpr) b; - - if (binaryA.getOperator() != SQLBinaryOperator.Equality) { - return false; - } - - if (binaryB.getOperator() != SQLBinaryOperator.Equality) { - return false; - } - - if (!(binaryA.getRight() instanceof SQLLiteralExpr || binaryA.getRight() instanceof SQLVariantRefExpr)) { - return false; - } - - if (!(binaryB.getRight() instanceof SQLLiteralExpr || binaryB.getRight() instanceof SQLVariantRefExpr)) { - return false; - } - - return binaryA.getLeft().toString().equals(binaryB.getLeft().toString()); - } - - private static boolean isLiteralExpr(SQLExpr expr) { - if (expr instanceof SQLLiteralExpr) { - return true; - } - - if (expr instanceof SQLBinaryOpExpr) { - SQLBinaryOpExpr binary = (SQLBinaryOpExpr) expr; - return isLiteralExpr(binary.getLeft()) && isLiteralExpr(binary.getRight()); - } - - return false; - } -} diff --git a/src/main/java/org/durid/sql/visitor/SQLASTOutputVisitor.java b/src/main/java/org/durid/sql/visitor/SQLASTOutputVisitor.java deleted file mode 100644 index 357d4456..00000000 --- a/src/main/java/org/durid/sql/visitor/SQLASTOutputVisitor.java +++ /dev/null @@ -1,1178 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.visitor; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.SQLCommentHint; -import org.durid.sql.ast.SQLDataType; -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLObject; -import org.durid.sql.ast.SQLOrderBy; -import org.durid.sql.ast.SQLOver; -import org.durid.sql.ast.SQLSetQuantifier; -import org.durid.sql.ast.SQLStatement; -import org.durid.sql.ast.expr.SQLAggregateExpr; -import org.durid.sql.ast.expr.SQLAllColumnExpr; -import org.durid.sql.ast.expr.SQLAllExpr; -import org.durid.sql.ast.expr.SQLAnyExpr; -import org.durid.sql.ast.expr.SQLBetweenExpr; -import org.durid.sql.ast.expr.SQLBinaryOpExpr; -import org.durid.sql.ast.expr.SQLBinaryOperator; -import org.durid.sql.ast.expr.SQLCaseExpr; -import org.durid.sql.ast.expr.SQLCastExpr; -import org.durid.sql.ast.expr.SQLCharExpr; -import org.durid.sql.ast.expr.SQLCurrentOfCursorExpr; -import org.durid.sql.ast.expr.SQLDefaultExpr; -import org.durid.sql.ast.expr.SQLExistsExpr; -import org.durid.sql.ast.expr.SQLHexExpr; -import org.durid.sql.ast.expr.SQLIdentifierExpr; -import org.durid.sql.ast.expr.SQLInListExpr; -import org.durid.sql.ast.expr.SQLInSubQueryExpr; -import org.durid.sql.ast.expr.SQLIntegerExpr; -import org.durid.sql.ast.expr.SQLListExpr; -import org.durid.sql.ast.expr.SQLMethodInvokeExpr; -import org.durid.sql.ast.expr.SQLNCharExpr; -import org.durid.sql.ast.expr.SQLNotExpr; -import org.durid.sql.ast.expr.SQLNullExpr; -import org.durid.sql.ast.expr.SQLNumberExpr; -import org.durid.sql.ast.expr.SQLPropertyExpr; -import org.durid.sql.ast.expr.SQLQueryExpr; -import org.durid.sql.ast.expr.SQLSomeExpr; -import org.durid.sql.ast.expr.SQLUnaryExpr; -import org.durid.sql.ast.expr.SQLVariantRefExpr; -import org.durid.sql.ast.statement.NotNullConstraint; -import org.durid.sql.ast.statement.SQLAlterTableAddColumn; -import org.durid.sql.ast.statement.SQLAlterTableAddPrimaryKey; -import org.durid.sql.ast.statement.SQLAlterTableDropColumnItem; -import org.durid.sql.ast.statement.SQLAlterTableDropIndex; -import org.durid.sql.ast.statement.SQLAssignItem; -import org.durid.sql.ast.statement.SQLCallStatement; -import org.durid.sql.ast.statement.SQLColumnConstraint; -import org.durid.sql.ast.statement.SQLColumnDefinition; -import org.durid.sql.ast.statement.SQLCommentStatement; -import org.durid.sql.ast.statement.SQLCreateDatabaseStatement; -import org.durid.sql.ast.statement.SQLCreateTableStatement; -import org.durid.sql.ast.statement.SQLDeleteStatement; -import org.durid.sql.ast.statement.SQLDropIndexStatement; -import org.durid.sql.ast.statement.SQLDropTableStatement; -import org.durid.sql.ast.statement.SQLDropViewStatement; -import org.durid.sql.ast.statement.SQLExprTableSource; -import org.durid.sql.ast.statement.SQLInsertStatement; -import org.durid.sql.ast.statement.SQLInsertStatement.ValuesClause; -import org.durid.sql.ast.statement.SQLJoinTableSource; -import org.durid.sql.ast.statement.SQLJoinTableSource.JoinType; -import org.durid.sql.ast.statement.SQLReleaseSavePointStatement; -import org.durid.sql.ast.statement.SQLRollbackStatement; -import org.durid.sql.ast.statement.SQLSavePointStatement; -import org.durid.sql.ast.statement.SQLSelect; -import org.durid.sql.ast.statement.SQLSelectGroupByClause; -import org.durid.sql.ast.statement.SQLSelectItem; -import org.durid.sql.ast.statement.SQLSelectOrderByItem; -import org.durid.sql.ast.statement.SQLSelectQueryBlock; -import org.durid.sql.ast.statement.SQLSelectStatement; -import org.durid.sql.ast.statement.SQLSetStatement; -import org.durid.sql.ast.statement.SQLSubqueryTableSource; -import org.durid.sql.ast.statement.SQLTableElement; -import org.durid.sql.ast.statement.SQLTruncateStatement; -import org.durid.sql.ast.statement.SQLUnionQuery; -import org.durid.sql.ast.statement.SQLUniqueConstraint; -import org.durid.sql.ast.statement.SQLUpdateSetItem; -import org.durid.sql.ast.statement.SQLUpdateStatement; -import org.durid.sql.ast.statement.SQLUseStatement; - -public class SQLASTOutputVisitor extends SQLASTVisitorAdapter { - - protected final Appendable appender; - private String indent = "\t"; - private int indentCount = 0; - private boolean prettyFormat = true; - - public SQLASTOutputVisitor(Appendable appender){ - this.appender = appender; - } - - public int getIndentCount() { - return indentCount; - } - - public Appendable getAppender() { - return appender; - } - - public boolean isPrettyFormat() { - return prettyFormat; - } - - public void setPrettyFormat(boolean prettyFormat) { - this.prettyFormat = prettyFormat; - } - - public void decrementIndent() { - this.indentCount -= 1; - } - - public void incrementIndent() { - this.indentCount += 1; - } - - public void print(char value) { - try { - this.appender.append(value); - } catch (IOException e) { - throw new RuntimeException("println error", e); - } - } - - public void print(int value) { - print(Integer.toString(value)); - } - - public void print(long value) { - print(Long.toString(value)); - } - - public void print(String text) { - try { - this.appender.append(text); - } catch (IOException e) { - throw new RuntimeException("println error", e); - } - } - - protected void printAlias(String alias) { - if ((alias != null) && (alias.length() > 0)) { - print(" "); - print(alias); - } - } - - protected void printAndAccept(List nodes, String seperator) { - for (int i = 0, size = nodes.size(); i < size; ++i) { - if (i != 0) { - print(seperator); - } - nodes.get(i).accept(this); - } - } - - protected void printSelectList(List selectList) { - incrementIndent(); - for (int i = 0, size = selectList.size(); i < size; ++i) { - if (i != 0) { - if (i % 5 == 0) { - println(); - } - - print(", "); - } - - selectList.get(i).accept(this); - } - decrementIndent(); - } - - protected void printlnAndAccept(List nodes, String seperator) { - for (int i = 0, size = nodes.size(); i < size; ++i) { - if (i != 0) { - println(seperator); - } - - ((SQLObject) nodes.get(i)).accept(this); - } - } - - public void printIndent() { - for (int i = 0; i < this.indentCount; ++i) { - print(this.indent); - } - } - - public void println() { - if (!isPrettyFormat()) { - print(' '); - return; - } - - print("\n"); - printIndent(); - } - - public void println(String text) { - print(text); - println(); - } - - // //////////////////// - - public boolean visit(SQLBetweenExpr x) { - x.getTestExpr().accept(this); - - if (x.isNot()) { - print(" NOT BETWEEN "); - } else { - print(" BETWEEN "); - } - - x.getBeginExpr().accept(this); - print(" AND "); - x.getEndExpr().accept(this); - - return false; - } - - public boolean visit(SQLBinaryOpExpr x) { - SQLObject parent = x.getParent(); - boolean isRoot = parent instanceof SQLSelectQueryBlock; - boolean relational = x.getOperator() == SQLBinaryOperator.BooleanAnd - || x.getOperator() == SQLBinaryOperator.BooleanOr; - - if (isRoot && relational) { - incrementIndent(); - } - - List groupList = new ArrayList(); - SQLExpr left = x.getLeft(); - for (;;) { - if (left instanceof SQLBinaryOpExpr && ((SQLBinaryOpExpr) left).getOperator() == x.getOperator()) { - SQLBinaryOpExpr binaryLeft = (SQLBinaryOpExpr) left; - groupList.add(binaryLeft.getRight()); - left = binaryLeft.getLeft(); - } else { - groupList.add(left); - break; - } - } - - for (int i = groupList.size() - 1; i >= 0; --i) { - SQLExpr item = groupList.get(i); - visitBinaryLeft(item, x.getOperator()); - - if (relational) { - println(); - } else { - print(" "); - } - print(x.getOperator().name); - print(" "); - } - - visitorBinaryRight(x); - - if (isRoot && relational) { - decrementIndent(); - } - - return false; - } - - private void visitorBinaryRight(SQLBinaryOpExpr x) { - if (x.getRight() instanceof SQLBinaryOpExpr) { - SQLBinaryOpExpr right = (SQLBinaryOpExpr) x.getRight(); - boolean rightRational = right.getOperator() == SQLBinaryOperator.BooleanAnd - || right.getOperator() == SQLBinaryOperator.BooleanOr; - - if (right.getOperator().priority >= x.getOperator().priority) { - if (rightRational) { - incrementIndent(); - } - - print('('); - right.accept(this); - print(')'); - - if (rightRational) { - decrementIndent(); - } - } else { - right.accept(this); - } - } else { - x.getRight().accept(this); - } - } - - private void visitBinaryLeft(SQLExpr left, SQLBinaryOperator op) { - if (left instanceof SQLBinaryOpExpr) { - SQLBinaryOpExpr binaryLeft = (SQLBinaryOpExpr) left; - boolean leftRational = binaryLeft.getOperator() == SQLBinaryOperator.BooleanAnd - || binaryLeft.getOperator() == SQLBinaryOperator.BooleanOr; - - if (binaryLeft.getOperator().priority > op.priority) { - if (leftRational) { - incrementIndent(); - } - print('('); - left.accept(this); - print(')'); - - if (leftRational) { - decrementIndent(); - } - } else { - left.accept(this); - } - } else { - left.accept(this); - } - } - - public boolean visit(SQLCaseExpr x) { - print("CASE "); - if (x.getValueExpr() != null) { - x.getValueExpr().accept(this); - print(" "); - } - - printAndAccept(x.getItems(), " "); - - if (x.getElseExpr() != null) { - print(" ELSE "); - x.getElseExpr().accept(this); - } - - print(" END"); - return false; - } - - public boolean visit(SQLCaseExpr.Item x) { - print("WHEN "); - x.getConditionExpr().accept(this); - print(" THEN "); - x.getValueExpr().accept(this); - return false; - } - - public boolean visit(SQLCastExpr x) { - print("CAST("); - x.getExpr().accept(this); - print(" AS "); - x.getDataType().accept(this); - print(")"); - - return false; - } - - public boolean visit(SQLCharExpr x) { - if ((x.getText() == null) || (x.getText().length() == 0)) { - print("NULL"); - } else { - print("'"); - print(x.getText().replaceAll("'", "''")); - print("'"); - } - - return false; - } - - public boolean visit(SQLDataType x) { - print(x.getName()); - if (x.getArguments().size() > 0) { - print("("); - printAndAccept(x.getArguments(), ", "); - print(")"); - } - - return false; - } - - public boolean visit(SQLExistsExpr x) { - if (x.isNot()) { - print("NOT EXISTS ("); - } else { - print("EXISTS ("); - } - incrementIndent(); - x.getSubQuery().accept(this); - decrementIndent(); - print(")"); - return false; - } - - public boolean visit(SQLIdentifierExpr astNode) { - print(astNode.getName()); - return false; - } - - public boolean visit(SQLInListExpr x) { - x.getExpr().accept(this); - - if (x.isNot()) { - print(" NOT IN ("); - } else { - print(" IN ("); - } - - printAndAccept(x.getTargetList(), ", "); - print(')'); - return false; - } - - public boolean visit(SQLIntegerExpr x) { - print(x.getNumber().toString()); - return false; - } - - public boolean visit(SQLMethodInvokeExpr x) { - if (x.getOwner() != null) { - x.getOwner().accept(this); - print("."); - } - print(x.getMethodName()); - print("("); - printAndAccept(x.getParameters(), ", "); - print(")"); - return false; - } - - public boolean visit(SQLAggregateExpr x) { - print(x.getMethodName()); - print("("); - - if (x.getOption() != null) { - print(x.getOption().toString()); - print(' '); - } - - printAndAccept(x.getArguments(), ", "); - print(")"); - - if (x.getOver() != null) { - print(" "); - x.getOver().accept(this); - } - return false; - } - - public boolean visit(SQLAllColumnExpr x) { - print("*"); - return true; - } - - public boolean visit(SQLNCharExpr x) { - if ((x.getText() == null) || (x.getText().length() == 0)) { - print("NULL"); - } else { - print("N'"); - print(x.getText().replace("'", "''")); - print("'"); - } - return false; - } - - public boolean visit(SQLNotExpr x) { - print("NOT "); - x.getExpr().accept(this); - return false; - } - - public boolean visit(SQLNullExpr x) { - print("NULL"); - return false; - } - - public boolean visit(SQLNumberExpr x) { - print(x.getNumber().toString()); - return false; - } - - - public boolean visit(SQLPropertyExpr x) { - x.getOwner().accept(this); - print("."); - print(x.getName()); - return false; - } - - public boolean visit(SQLQueryExpr x) { - SQLObject parent = x.getParent(); - if (parent instanceof SQLSelect) { - parent = parent.getParent(); - } - - if (parent instanceof SQLStatement) { - incrementIndent(); - - println(); - x.getSubQuery().accept(this); - - decrementIndent(); - } else if (parent instanceof ValuesClause) { - println(); - x.getSubQuery().accept(this); - println(); - } else { - print("("); - incrementIndent(); - println(); - x.getSubQuery().accept(this); - println(); - decrementIndent(); - print(")"); - } - return false; - } - - public boolean visit(SQLSelectGroupByClause x) { - if (x.getItems().size() > 0) { - print("GROUP BY "); - printAndAccept(x.getItems(), ", "); - } - - if (x.getHaving() != null) { - println(); - print("HAVING "); - x.getHaving().accept(this); - } - return false; - } - - public boolean visit(SQLSelect x) { - x.getQuery().setParent(x); - x.getQuery().accept(this); - - if (x.getOrderBy() != null) { - println(); - x.getOrderBy().accept(this); - } - - return false; - } - - public boolean visit(SQLSelectQueryBlock x) { - print("SELECT "); - - if (SQLSetQuantifier.ALL == x.getDistionOption()) { - print("ALL "); - } else if (SQLSetQuantifier.DISTINCT == x.getDistionOption()) { - print("DISTINCT "); - } else if (SQLSetQuantifier.UNIQUE == x.getDistionOption()) { - print("UNIQUE "); - } - - printSelectList(x.getSelectList()); - - if (x.getFrom() != null) { - println(); - print("FROM "); - x.getFrom().accept(this); - } - - if (x.getWhere() != null) { - println(); - print("WHERE "); - x.getWhere().setParent(x); - x.getWhere().accept(this); - } - - if (x.getGroupBy() != null) { - print(" "); - x.getGroupBy().accept(this); - } - - return false; - } - - public boolean visit(SQLSelectItem x) { - x.getExpr().accept(this); - - if ((x.getAlias() != null) && (x.getAlias().length() > 0)) { - print(" AS "); - print(x.getAlias()); - } - return false; - } - - public boolean visit(SQLOrderBy x) { - if (x.getItems().size() > 0) { - print("ORDER BY "); - - printAndAccept(x.getItems(), ", "); - } - return false; - } - - public boolean visit(SQLSelectOrderByItem x) { - x.getExpr().accept(this); - if (x.getType() != null) { - print(" "); - print(x.getType().name().toUpperCase()); - } - - if (x.getCollate() != null) { - print(" COLLATE "); - print(x.getCollate()); - } - - return false; - } - - public boolean visit(SQLExprTableSource x) { - x.getExpr().accept(this); - - if (x.getAlias() != null) { - print(' '); - print(x.getAlias()); - } - - return false; - } - - public boolean visit(SQLSelectStatement stmt) { - SQLSelect select = stmt.getSelect(); - - select.accept(this); - - return false; - } - - public boolean visit(SQLVariantRefExpr x) { - print(x.getName()); - return false; - } - - public boolean visit(SQLDropTableStatement x) { - print("DROP TABLE "); - printAndAccept(x.getTableSources(), ", "); - return false; - } - - public boolean visit(SQLDropViewStatement x) { - print("DROP VIEW "); - printAndAccept(x.getTableSources(), ", "); - return false; - } - - public boolean visit(SQLTableElement x) { - if (x instanceof SQLColumnDefinition) { - return visit((SQLColumnDefinition) x); - } - - throw new RuntimeException("TODO"); - } - - public boolean visit(SQLColumnDefinition x) { - x.getName().accept(this); - - if (x.getDataType() != null) { - print(' '); - x.getDataType().accept(this); - } - - if (x.getDefaultExpr() != null) { - visitColumnDefault(x); - } - - for (SQLColumnConstraint item : x.getConstaints()) { - print(' '); - item.accept(this); - } - - if (x.getEnable() != null) { - if (x.getEnable().booleanValue()) { - print(" ENABLE"); - } - } - - return false; - } - - protected void visitColumnDefault(SQLColumnDefinition x) { - print(" DEFAULT "); - x.getDefaultExpr().accept(this); - } - - public boolean visit(SQLDeleteStatement x) { - print("DELETE FROM "); - - x.getTableName().accept(this); - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().setParent(x); - x.getWhere().accept(this); - } - - return false; - } - - public boolean visit(SQLCurrentOfCursorExpr x) { - print("CURRENT OF "); - x.getCursorName().accept(this); - return false; - } - - public boolean visit(SQLInsertStatement x) { - print("INSERT INTO "); - - x.getTableName().accept(this); - - if (x.getColumns().size() > 0) { - incrementIndent(); - println(); - print("("); - for (int i = 0, size = x.getColumns().size(); i < size; ++i) { - if (i != 0) { - if (i % 5 == 0) { - println(); - } - print(", "); - } - x.getColumns().get(i).accept(this); - } - print(")"); - decrementIndent(); - } - - if (x.getValues() != null) { - println(); - print("VALUES"); - println(); - x.getValues().accept(this); - } else { - if (x.getQuery() != null) { - println(); - x.getQuery().setParent(x); - x.getQuery().accept(this); - } - } - - return false; - } - - public boolean visit(SQLUpdateSetItem x) { - x.getColumn().accept(this); - print(" = "); - x.getValue().accept(this); - return false; - } - - public boolean visit(SQLUpdateStatement x) { - print("UPDATE "); - - x.getTableSource().accept(this); - - println(); - print("SET "); - for (int i = 0, size = x.getItems().size(); i < size; ++i) { - if (i != 0) { - print(", "); - } - x.getItems().get(i).accept(this); - } - - if (x.getWhere() != null) { - println(); - print("WHERE "); - x.getWhere().setParent(x); - x.getWhere().accept(this); - } - - return false; - } - - public boolean visit(SQLCreateTableStatement x) { - print("CREATE TABLE "); - if (SQLCreateTableStatement.Type.GLOBAL_TEMPORARY.equals(x.getType())) { - print("GLOBAL TEMPORARY "); - } else if (SQLCreateTableStatement.Type.LOCAL_TEMPORARY.equals(x.getType())) { - print("LOCAL TEMPORARY "); - } - - x.getName().accept(this); - - int size = x.getTableElementList().size(); - - if (size > 0) { - print(" ("); - incrementIndent(); - println(); - for (int i = 0; i < size; ++i) { - if (i != 0) { - print(", "); - println(); - } - x.getTableElementList().get(i).accept(this); - } - decrementIndent(); - println(); - print(")"); - } - - return false; - } - - public boolean visit(SQLUniqueConstraint x) { - if (x.getName() != null) { - print("CONSTRAINT "); - x.getName().accept(this); - print(' '); - } - - print("UNIQUE ("); - for (int i = 0, size = x.getColumns().size(); i < size; ++i) { - if (i != 0) { - print(", "); - } - x.getColumns().get(i).accept(this); - } - print(")"); - return false; - } - - public boolean visit(NotNullConstraint x) { - print("NOT NULL"); - return false; - } - - @Override - public boolean visit(SQLUnionQuery x) { - x.getLeft().accept(this); - println(); - print(x.getOperator().name); - println(); - - boolean needParen = false; - - if (x.getOrderBy() != null) { - needParen = true; - } - - if (needParen) { - print('('); - x.getRight().accept(this); - print(')'); - } else { - x.getRight().accept(this); - } - - if (x.getOrderBy() != null) { - println(); - x.getOrderBy().accept(this); - } - - return false; - } - - @Override - public boolean visit(SQLUnaryExpr x) { - print(x.getOperator().name); - SQLExpr expr = x.getExpr(); - if (expr instanceof SQLBinaryOpExpr) { - print('('); - expr.accept(this); - print(')'); - } else if (expr instanceof SQLUnaryExpr) { - print('('); - expr.accept(this); - print(')'); - } else { - expr.accept(this); - } - return false; - } - - @Override - public boolean visit(SQLHexExpr x) { - print("0x"); - print(x.getHex()); - - String charset = (String) x.getAttribute("USING"); - if (charset != null) { - print(" USING "); - print(charset); - } - - return false; - } - - @Override - public boolean visit(SQLSetStatement x) { - print("SET "); - printAndAccept(x.getItems(), ", "); - return false; - } - - @Override - public boolean visit(SQLAssignItem x) { - x.getTarget().accept(this); - print(" = "); - x.getValue().accept(this); - return false; - } - - @Override - public boolean visit(SQLCallStatement x) { - print("CALL "); - x.getProcedureName().accept(this); - print('('); - printAndAccept(x.getParameters(), ", "); - print(')'); - return false; - } - - @Override - public boolean visit(SQLJoinTableSource x) { - x.getLeft().accept(this); - if (x.getJoinType() == JoinType.COMMA) { - print(","); - } else { - print(" "); - print(JoinType.toString(x.getJoinType())); - } - print(" "); - x.getRight().accept(this); - - if (x.getCondition() != null) { - print(" ON "); - x.getCondition().accept(this); - } - - return false; - } - - @Override - public boolean visit(ValuesClause x) { - print("("); - incrementIndent(); - for (int i = 0, size = x.getValues().size(); i < size; ++i) { - if (i != 0) { - if (i % 5 == 0) { - println(); - } - print(", "); - } - - SQLExpr expr = x.getValues().get(i); - expr.setParent(x); - expr.accept(this); - } - decrementIndent(); - print(")"); - return false; - } - - @Override - public boolean visit(SQLSomeExpr x) { - print("SOME ("); - - incrementIndent(); - x.getSubQuery().accept(this); - decrementIndent(); - print(")"); - return false; - } - - @Override - public boolean visit(SQLAnyExpr x) { - print("ANY ("); - - incrementIndent(); - x.getSubQuery().accept(this); - decrementIndent(); - print(")"); - return false; - } - - @Override - public boolean visit(SQLAllExpr x) { - print("ALL ("); - - incrementIndent(); - x.getSubQuery().accept(this); - decrementIndent(); - print(")"); - return false; - } - - @Override - public boolean visit(SQLInSubQueryExpr x) { - x.getExpr().accept(this); - if (x.isNot()) { - print(" NOT IN ("); - } else { - print(" IN ("); - } - - incrementIndent(); - x.getSubQuery().accept(this); - decrementIndent(); - print(")"); - - return false; - } - - @Override - public boolean visit(SQLListExpr x) { - print("("); - printAndAccept(x.getItems(), ", "); - print(")"); - - return false; - } - - @Override - public boolean visit(SQLSubqueryTableSource x) { - print("("); - incrementIndent(); - x.getSelect().accept(this); - println(); - decrementIndent(); - print(")"); - - if (x.getAlias() != null) { - print(' '); - print(x.getAlias()); - } - - return false; - } - - @Override - public boolean visit(SQLTruncateStatement x) { - print("TRUNCATE TABLE "); - printAndAccept(x.getTableSources(), ", "); - return false; - } - - @Override - public boolean visit(SQLDefaultExpr x) { - print("DEFAULT"); - return false; - } - - @Override - public void endVisit(SQLCommentStatement x) { - - } - - @Override - public boolean visit(SQLCommentStatement x) { - print("COMMENT ON "); - if (x.getType() != null) { - print(x.getType().name()); - print(" "); - } - x.getOn().accept(this); - - print(" IS "); - x.getComment().accept(this); - - return false; - } - - @Override - public boolean visit(SQLUseStatement x) { - print("USE "); - x.getDatabase().accept(this); - return false; - } - - @Override - public boolean visit(SQLAlterTableAddColumn x) { - print("ADD ("); - printAndAccept(x.getColumns(), ", "); - print(")"); - return false; - } - - @Override - public boolean visit(SQLAlterTableDropColumnItem x) { - print("DROP COLUMN "); - x.getColumnName().accept(this); - return false; - } - - @Override - public void endVisit(SQLAlterTableAddColumn x) { - - } - - @Override - public boolean visit(SQLDropIndexStatement x) { - print("DROP INDEX "); - x.getIndexName().accept(this); - print(" ON "); - x.getTableName().accept(this); - return false; - } - - @Override - public boolean visit(SQLSavePointStatement x) { - print("SAVEPOINT "); - x.getName().accept(this); - return false; - } - - @Override - public boolean visit(SQLReleaseSavePointStatement x) { - print("RELEASE SAVEPOINT "); - x.getName().accept(this); - return false; - } - - @Override - public boolean visit(SQLRollbackStatement x) { - print("ROLLBACK"); - if (x.getTo() != null) { - print(" TO "); - x.getTo().accept(this); - } - return false; - } - - public boolean visit(SQLCommentHint x) { - print("/*"); - print(x.getText()); - print("*/"); - return false; - } - - @Override - public boolean visit(SQLCreateDatabaseStatement x) { - print("CREATE DATABASE "); - x.getName().accept(this); - return false; - } - - @Override - public boolean visit(SQLAlterTableDropIndex x) { - print("DROP INDEX "); - x.getIndexName().accept(this); - return false; - } - - @Override - public boolean visit(SQLAlterTableAddPrimaryKey x) { - print("ADD "); - x.getPrimaryKey().accept(this); - return false; - } - - @Override - public boolean visit(SQLOver x) { - print("OVER ("); - printAndAccept(x.getPartitionBy(), ", "); - print(")"); - if (x.getOrderBy() != null) { - print(" "); - x.getOrderBy().accept(this); - } - return false; - } -} diff --git a/src/main/java/org/durid/sql/visitor/SQLASTVisitor.java b/src/main/java/org/durid/sql/visitor/SQLASTVisitor.java deleted file mode 100644 index 75b6bb99..00000000 --- a/src/main/java/org/durid/sql/visitor/SQLASTVisitor.java +++ /dev/null @@ -1,371 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.visitor; - -import org.durid.sql.ast.SQLCommentHint; -import org.durid.sql.ast.SQLDataType; -import org.durid.sql.ast.SQLObject; -import org.durid.sql.ast.SQLOrderBy; -import org.durid.sql.ast.SQLOver; -import org.durid.sql.ast.expr.*; -import org.durid.sql.ast.statement.NotNullConstraint; -import org.durid.sql.ast.statement.SQLAlterTableAddColumn; -import org.durid.sql.ast.statement.SQLAlterTableAddPrimaryKey; -import org.durid.sql.ast.statement.SQLAlterTableDropColumnItem; -import org.durid.sql.ast.statement.SQLAlterTableDropIndex; -import org.durid.sql.ast.statement.SQLAssignItem; -import org.durid.sql.ast.statement.SQLCallStatement; -import org.durid.sql.ast.statement.SQLColumnDefinition; -import org.durid.sql.ast.statement.SQLCommentStatement; -import org.durid.sql.ast.statement.SQLCreateDatabaseStatement; -import org.durid.sql.ast.statement.SQLCreateTableStatement; -import org.durid.sql.ast.statement.SQLCreateViewStatement; -import org.durid.sql.ast.statement.SQLDeleteStatement; -import org.durid.sql.ast.statement.SQLDropIndexStatement; -import org.durid.sql.ast.statement.SQLDropTableStatement; -import org.durid.sql.ast.statement.SQLDropViewStatement; -import org.durid.sql.ast.statement.SQLExprTableSource; -import org.durid.sql.ast.statement.SQLInsertStatement; -import org.durid.sql.ast.statement.SQLJoinTableSource; -import org.durid.sql.ast.statement.SQLReleaseSavePointStatement; -import org.durid.sql.ast.statement.SQLRollbackStatement; -import org.durid.sql.ast.statement.SQLSavePointStatement; -import org.durid.sql.ast.statement.SQLSelect; -import org.durid.sql.ast.statement.SQLSelectGroupByClause; -import org.durid.sql.ast.statement.SQLSelectItem; -import org.durid.sql.ast.statement.SQLSelectOrderByItem; -import org.durid.sql.ast.statement.SQLSelectQueryBlock; -import org.durid.sql.ast.statement.SQLSelectStatement; -import org.durid.sql.ast.statement.SQLSetStatement; -import org.durid.sql.ast.statement.SQLSubqueryTableSource; -import org.durid.sql.ast.statement.SQLTableElement; -import org.durid.sql.ast.statement.SQLTruncateStatement; -import org.durid.sql.ast.statement.SQLUnionQuery; -import org.durid.sql.ast.statement.SQLUniqueConstraint; -import org.durid.sql.ast.statement.SQLUpdateSetItem; -import org.durid.sql.ast.statement.SQLUpdateStatement; -import org.durid.sql.ast.statement.SQLUseStatement; - -public interface SQLASTVisitor { - - void endVisit(SQLAllColumnExpr x); - - void endVisit(SQLBetweenExpr x); - - void endVisit(SQLBinaryOpExpr x); - - void endVisit(SQLOdbcExpr x); - - void endVisit(SQLCaseExpr x); - - void endVisit(SQLCaseExpr.Item x); - - void endVisit(SQLCharExpr x); - - void endVisit(SQLIdentifierExpr x); - - void endVisit(SQLInListExpr x); - - void endVisit(SQLIntegerExpr x); - - void endVisit(SQLExistsExpr x); - - void endVisit(SQLNCharExpr x); - - void endVisit(SQLNotExpr x); - - void endVisit(SQLNullExpr x); - - void endVisit(SQLNumberExpr x); - - void endVisit(SQLPropertyExpr x); - - void endVisit(SQLSelectGroupByClause x); - - void endVisit(SQLSelectItem x); - - void endVisit(SQLSelectStatement selectStatement); - - void postVisit(SQLObject astNode); - - void preVisit(SQLObject astNode); - - boolean visit(SQLAllColumnExpr x); - - boolean visit(SQLBetweenExpr x); - - boolean visit(SQLBinaryOpExpr x); - - boolean visit(SQLOdbcExpr x); - - boolean visit(SQLCaseExpr x); - - boolean visit(SQLCaseExpr.Item x); - - boolean visit(SQLCastExpr x); - - boolean visit(SQLCharExpr x); - - boolean visit(SQLExistsExpr x); - - boolean visit(SQLIdentifierExpr x); - - boolean visit(SQLInListExpr x); - - boolean visit(SQLIntegerExpr x); - - boolean visit(SQLNCharExpr x); - - boolean visit(SQLNotExpr x); - - boolean visit(SQLNullExpr x); - - boolean visit(SQLNumberExpr x); - - boolean visit(SQLPropertyExpr x); - - boolean visit(SQLSelectGroupByClause x); - - boolean visit(SQLSelectItem x); - - void endVisit(SQLCastExpr x); - - boolean visit(SQLSelectStatement astNode); - - void endVisit(SQLAggregateExpr astNode); - - boolean visit(SQLAggregateExpr astNode); - - boolean visit(SQLVariantRefExpr x); - - void endVisit(SQLVariantRefExpr x); - - boolean visit(SQLQueryExpr x); - - void endVisit(SQLQueryExpr x); - - boolean visit(SQLUnaryExpr x); - - void endVisit(SQLUnaryExpr x); - - boolean visit(SQLHexExpr x); - - void endVisit(SQLHexExpr x); - - boolean visit(SQLBitStringLiteralExpr x); - - void endVisit(SQLBitStringLiteralExpr x); - - boolean visit(SQLHexStringLiteralExpr x); - - void endVisit(SQLHexStringLiteralExpr x); - - boolean visit(SQLDateLiteralExpr x); - - void endVisit(SQLDateLiteralExpr x); - - boolean visit(SQLSelect x); - - void endVisit(SQLSelect select); - - boolean visit(SQLSelectQueryBlock x); - - void endVisit(SQLSelectQueryBlock x); - - boolean visit(SQLExprTableSource x); - - void endVisit(SQLExprTableSource x); - - boolean visit(SQLIntervalLiteralExpr x); - - void endVisit(SQLIntervalLiteralExpr x); - - boolean visit(SQLOrderBy x); - - void endVisit(SQLOrderBy x); - - boolean visit(SQLSelectOrderByItem x); - - void endVisit(SQLSelectOrderByItem x); - - boolean visit(SQLDropTableStatement x); - - void endVisit(SQLDropTableStatement x); - - boolean visit(SQLCreateTableStatement x); - - void endVisit(SQLCreateTableStatement x); - - boolean visit(SQLTableElement x); - - void endVisit(SQLTableElement x); - - boolean visit(SQLColumnDefinition x); - - void endVisit(SQLColumnDefinition x); - - boolean visit(SQLDataType x); - - void endVisit(SQLDataType x); - - boolean visit(SQLDeleteStatement x); - - void endVisit(SQLDeleteStatement x); - - boolean visit(SQLCurrentOfCursorExpr x); - - void endVisit(SQLCurrentOfCursorExpr x); - - boolean visit(SQLInsertStatement x); - - void endVisit(SQLInsertStatement x); - - boolean visit(SQLInsertStatement.ValuesClause x); - - void endVisit(SQLInsertStatement.ValuesClause x); - - boolean visit(SQLUpdateSetItem x); - - void endVisit(SQLUpdateSetItem x); - - boolean visit(SQLUpdateStatement x); - - void endVisit(SQLUpdateStatement x); - - boolean visit(SQLCreateViewStatement x); - - void endVisit(SQLCreateViewStatement x); - - boolean visit(SQLUniqueConstraint x); - - void endVisit(SQLUniqueConstraint x); - - boolean visit(NotNullConstraint x); - - void endVisit(NotNullConstraint x); - - void endVisit(SQLMethodInvokeExpr x); - - boolean visit(SQLMethodInvokeExpr x); - - void endVisit(SQLUnionQuery x); - - boolean visit(SQLUnionQuery x); - - void endVisit(SQLSetStatement x); - - boolean visit(SQLSetStatement x); - - void endVisit(SQLAssignItem x); - - boolean visit(SQLAssignItem x); - - void endVisit(SQLCallStatement x); - - boolean visit(SQLCallStatement x); - - void endVisit(SQLJoinTableSource x); - - boolean visit(SQLJoinTableSource x); - - void endVisit(SQLSomeExpr x); - - boolean visit(SQLSomeExpr x); - - void endVisit(SQLAnyExpr x); - - boolean visit(SQLAnyExpr x); - - void endVisit(SQLAllExpr x); - - boolean visit(SQLAllExpr x); - - void endVisit(SQLInSubQueryExpr x); - - boolean visit(SQLInSubQueryExpr x); - - void endVisit(SQLListExpr x); - - boolean visit(SQLListExpr x); - - void endVisit(SQLSubqueryTableSource x); - - boolean visit(SQLSubqueryTableSource x); - - void endVisit(SQLTruncateStatement x); - - boolean visit(SQLTruncateStatement x); - - void endVisit(SQLDefaultExpr x); - - boolean visit(SQLDefaultExpr x); - - void endVisit(SQLCommentStatement x); - - boolean visit(SQLCommentStatement x); - - void endVisit(SQLUseStatement x); - - boolean visit(SQLUseStatement x); - - boolean visit(SQLAlterTableAddColumn x); - - void endVisit(SQLAlterTableAddColumn x); - - boolean visit(SQLAlterTableDropColumnItem x); - - void endVisit(SQLAlterTableDropColumnItem x); - - boolean visit(SQLAlterTableDropIndex x); - - void endVisit(SQLAlterTableDropIndex x); - - boolean visit(SQLAlterTableAddPrimaryKey x); - - void endVisit(SQLAlterTableAddPrimaryKey x); - - boolean visit(SQLDropIndexStatement x); - - void endVisit(SQLDropIndexStatement x); - - boolean visit(SQLDropViewStatement x); - - void endVisit(SQLDropViewStatement x); - - boolean visit(SQLSavePointStatement x); - - void endVisit(SQLSavePointStatement x); - - boolean visit(SQLRollbackStatement x); - - void endVisit(SQLRollbackStatement x); - - boolean visit(SQLReleaseSavePointStatement x); - - void endVisit(SQLReleaseSavePointStatement x); - - void endVisit(SQLCommentHint x); - - boolean visit(SQLCommentHint x); - - void endVisit(SQLCreateDatabaseStatement x); - - boolean visit(SQLCreateDatabaseStatement x); - - void endVisit(SQLOver x); - - boolean visit(SQLOver x); -} diff --git a/src/main/java/org/durid/sql/visitor/SQLASTVisitorAdapter.java b/src/main/java/org/durid/sql/visitor/SQLASTVisitorAdapter.java deleted file mode 100644 index 153d872b..00000000 --- a/src/main/java/org/durid/sql/visitor/SQLASTVisitorAdapter.java +++ /dev/null @@ -1,691 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.visitor; - -import org.durid.sql.ast.SQLCommentHint; -import org.durid.sql.ast.SQLDataType; -import org.durid.sql.ast.SQLObject; -import org.durid.sql.ast.SQLOrderBy; -import org.durid.sql.ast.SQLOver; -import org.durid.sql.ast.expr.*; -import org.durid.sql.ast.statement.NotNullConstraint; -import org.durid.sql.ast.statement.SQLAlterTableAddColumn; -import org.durid.sql.ast.statement.SQLAlterTableAddPrimaryKey; -import org.durid.sql.ast.statement.SQLAlterTableDropColumnItem; -import org.durid.sql.ast.statement.SQLAlterTableDropIndex; -import org.durid.sql.ast.statement.SQLAssignItem; -import org.durid.sql.ast.statement.SQLCallStatement; -import org.durid.sql.ast.statement.SQLColumnDefinition; -import org.durid.sql.ast.statement.SQLCommentStatement; -import org.durid.sql.ast.statement.SQLCreateDatabaseStatement; -import org.durid.sql.ast.statement.SQLCreateTableStatement; -import org.durid.sql.ast.statement.SQLCreateViewStatement; -import org.durid.sql.ast.statement.SQLDeleteStatement; -import org.durid.sql.ast.statement.SQLDropIndexStatement; -import org.durid.sql.ast.statement.SQLDropTableStatement; -import org.durid.sql.ast.statement.SQLDropViewStatement; -import org.durid.sql.ast.statement.SQLExprTableSource; -import org.durid.sql.ast.statement.SQLInsertStatement; -import org.durid.sql.ast.statement.SQLInsertStatement.ValuesClause; -import org.durid.sql.ast.statement.SQLJoinTableSource; -import org.durid.sql.ast.statement.SQLReleaseSavePointStatement; -import org.durid.sql.ast.statement.SQLRollbackStatement; -import org.durid.sql.ast.statement.SQLSavePointStatement; -import org.durid.sql.ast.statement.SQLSelect; -import org.durid.sql.ast.statement.SQLSelectGroupByClause; -import org.durid.sql.ast.statement.SQLSelectItem; -import org.durid.sql.ast.statement.SQLSelectOrderByItem; -import org.durid.sql.ast.statement.SQLSelectQueryBlock; -import org.durid.sql.ast.statement.SQLSelectStatement; -import org.durid.sql.ast.statement.SQLSetStatement; -import org.durid.sql.ast.statement.SQLSubqueryTableSource; -import org.durid.sql.ast.statement.SQLTableElement; -import org.durid.sql.ast.statement.SQLTruncateStatement; -import org.durid.sql.ast.statement.SQLUnionQuery; -import org.durid.sql.ast.statement.SQLUniqueConstraint; -import org.durid.sql.ast.statement.SQLUpdateSetItem; -import org.durid.sql.ast.statement.SQLUpdateStatement; -import org.durid.sql.ast.statement.SQLUseStatement; - -public class SQLASTVisitorAdapter implements SQLASTVisitor { - - public void endVisit(SQLAllColumnExpr x) { - } - - public void endVisit(SQLBetweenExpr x) { - } - - public void endVisit(SQLBinaryOpExpr x) { - } - - public void endVisit(SQLOdbcExpr x) { - } - - public void endVisit(SQLCaseExpr x) { - } - - public void endVisit(SQLCaseExpr.Item x) { - } - - public void endVisit(SQLCharExpr x) { - } - - public void endVisit(SQLIdentifierExpr x) { - } - - public void endVisit(SQLInListExpr x) { - } - - public void endVisit(SQLIntegerExpr x) { - } - - public void endVisit(SQLExistsExpr x) { - } - - public void endVisit(SQLNCharExpr x) { - } - - public void endVisit(SQLNotExpr x) { - } - - public void endVisit(SQLNullExpr x) { - } - - public void endVisit(SQLNumberExpr x) { - } - - public void endVisit(SQLPropertyExpr x) { - } - - public void endVisit(SQLSelectGroupByClause x) { - } - - public void endVisit(SQLSelectItem x) { - } - - public void endVisit(SQLSelectStatement selectStatement) { - } - - public void postVisit(SQLObject astNode) { - } - - public void preVisit(SQLObject astNode) { - } - - public boolean visit(SQLAllColumnExpr x) { - return true; - } - - public boolean visit(SQLBetweenExpr x) { - return true; - } - - public boolean visit(SQLBinaryOpExpr x) { - return true; - } - - public boolean visit(SQLOdbcExpr x) { return false; } - - public boolean visit(SQLCaseExpr x) { - return true; - } - - public boolean visit(SQLCaseExpr.Item x) { - return true; - } - - public boolean visit(SQLCastExpr x) { - return true; - } - - public boolean visit(SQLCharExpr x) { - return true; - } - - public boolean visit(SQLExistsExpr x) { - return true; - } - - public boolean visit(SQLIdentifierExpr x) { - return true; - } - - public boolean visit(SQLInListExpr x) { - return true; - } - - public boolean visit(SQLIntegerExpr x) { - return true; - } - - public boolean visit(SQLNCharExpr x) { - return true; - } - - public boolean visit(SQLNotExpr x) { - return true; - } - - public boolean visit(SQLNullExpr x) { - return true; - } - - public boolean visit(SQLNumberExpr x) { - return true; - } - - public boolean visit(SQLPropertyExpr x) { - return true; - } - - public boolean visit(SQLSelectGroupByClause x) { - return true; - } - - public boolean visit(SQLSelectItem x) { - return true; - } - - public void endVisit(SQLCastExpr x) { - } - - public boolean visit(SQLSelectStatement astNode) { - return true; - } - - public void endVisit(SQLAggregateExpr x) { - } - - public boolean visit(SQLAggregateExpr x) { - return true; - } - - public boolean visit(SQLVariantRefExpr x) { - return true; - } - - public void endVisit(SQLVariantRefExpr x) { - } - - public boolean visit(SQLQueryExpr x) { - return true; - } - - public void endVisit(SQLQueryExpr x) { - } - - public boolean visit(SQLBitStringLiteralExpr x) { - return true; - } - - public void endVisit(SQLBitStringLiteralExpr x) { - } - - public boolean visit(SQLHexStringLiteralExpr x) { - return true; - } - - public void endVisit(SQLHexStringLiteralExpr x) { - } - - public boolean visit(SQLDateLiteralExpr x) { - return true; - } - - public void endVisit(SQLDateLiteralExpr x) { - } - - public boolean visit(SQLSelect x) { - return true; - } - - public void endVisit(SQLSelect select) { - } - - public boolean visit(SQLSelectQueryBlock x) { - return true; - } - - public void endVisit(SQLSelectQueryBlock x) { - } - - public boolean visit(SQLExprTableSource x) { - return true; - } - - public void endVisit(SQLExprTableSource x) { - } - - public boolean visit(SQLIntervalLiteralExpr x) { - return true; - } - - public void endVisit(SQLIntervalLiteralExpr x) { - } - - public boolean visit(SQLOrderBy x) { - return true; - } - - public void endVisit(SQLOrderBy x) { - } - - public boolean visit(SQLSelectOrderByItem x) { - return true; - } - - public void endVisit(SQLSelectOrderByItem x) { - } - - public boolean visit(SQLDropTableStatement x) { - return true; - } - - public void endVisit(SQLDropTableStatement x) { - } - - public boolean visit(SQLCreateTableStatement x) { - return true; - } - - public void endVisit(SQLCreateTableStatement x) { - } - - public boolean visit(SQLTableElement x) { - return true; - } - - public void endVisit(SQLTableElement x) { - } - - public boolean visit(SQLColumnDefinition x) { - return true; - } - - public void endVisit(SQLColumnDefinition x) { - } - - public boolean visit(SQLDataType x) { - return true; - } - - public void endVisit(SQLDataType x) { - } - - public boolean visit(SQLDeleteStatement x) { - return true; - } - - public void endVisit(SQLDeleteStatement x) { - } - - public boolean visit(SQLCurrentOfCursorExpr x) { - return true; - } - - public void endVisit(SQLCurrentOfCursorExpr x) { - } - - public boolean visit(SQLInsertStatement x) { - return true; - } - - public void endVisit(SQLInsertStatement x) { - } - - public boolean visit(SQLUpdateSetItem x) { - return true; - } - - public void endVisit(SQLUpdateSetItem x) { - } - - public boolean visit(SQLUpdateStatement x) { - return true; - } - - public void endVisit(SQLUpdateStatement x) { - } - - public boolean visit(SQLCreateViewStatement x) { - return true; - } - - public void endVisit(SQLCreateViewStatement x) { - } - - public boolean visit(SQLUniqueConstraint x) { - return true; - } - - public void endVisit(SQLUniqueConstraint x) { - } - - public boolean visit(NotNullConstraint x) { - return true; - } - - public void endVisit(NotNullConstraint x) { - } - - @Override - public void endVisit(SQLMethodInvokeExpr x) { - - } - - @Override - public boolean visit(SQLMethodInvokeExpr x) { - return true; - } - - @Override - public void endVisit(SQLUnionQuery x) { - - } - - @Override - public boolean visit(SQLUnionQuery x) { - return true; - } - - @Override - public boolean visit(SQLUnaryExpr x) { - return true; - } - - @Override - public void endVisit(SQLUnaryExpr x) { - - } - - @Override - public boolean visit(SQLHexExpr x) { - return false; - } - - @Override - public void endVisit(SQLHexExpr x) { - - } - - @Override - public void endVisit(SQLSetStatement x) { - - } - - @Override - public boolean visit(SQLSetStatement x) { - return true; - } - - @Override - public void endVisit(SQLAssignItem x) { - - } - - @Override - public boolean visit(SQLAssignItem x) { - return true; - } - - @Override - public void endVisit(SQLCallStatement x) { - - } - - @Override - public boolean visit(SQLCallStatement x) { - return true; - } - - @Override - public void endVisit(SQLJoinTableSource x) { - - } - - @Override - public boolean visit(SQLJoinTableSource x) { - return true; - } - - @Override - public boolean visit(ValuesClause x) { - return true; - } - - @Override - public void endVisit(ValuesClause x) { - - } - - @Override - public void endVisit(SQLSomeExpr x) { - - } - - @Override - public boolean visit(SQLSomeExpr x) { - return true; - } - - @Override - public void endVisit(SQLAnyExpr x) { - - } - - @Override - public boolean visit(SQLAnyExpr x) { - return true; - } - - @Override - public void endVisit(SQLAllExpr x) { - - } - - @Override - public boolean visit(SQLAllExpr x) { - return true; - } - - @Override - public void endVisit(SQLInSubQueryExpr x) { - - } - - @Override - public boolean visit(SQLInSubQueryExpr x) { - return true; - } - - @Override - public void endVisit(SQLListExpr x) { - - } - - @Override - public boolean visit(SQLListExpr x) { - return true; - } - - @Override - public void endVisit(SQLSubqueryTableSource x) { - - } - - @Override - public boolean visit(SQLSubqueryTableSource x) { - return true; - } - - @Override - public void endVisit(SQLTruncateStatement x) { - - } - - @Override - public boolean visit(SQLTruncateStatement x) { - return true; - } - - @Override - public void endVisit(SQLDefaultExpr x) { - - } - - @Override - public boolean visit(SQLDefaultExpr x) { - return true; - } - - @Override - public void endVisit(SQLCommentStatement x) { - - } - - @Override - public boolean visit(SQLCommentStatement x) { - return true; - } - - @Override - public void endVisit(SQLUseStatement x) { - - } - - @Override - public boolean visit(SQLUseStatement x) { - return true; - } - - @Override - public boolean visit(SQLAlterTableAddColumn x) { - return true; - } - - @Override - public void endVisit(SQLAlterTableAddColumn x) { - - } - - @Override - public boolean visit(SQLAlterTableDropColumnItem x) { - return true; - } - - @Override - public void endVisit(SQLAlterTableDropColumnItem x) { - - } - - @Override - public boolean visit(SQLDropIndexStatement x) { - return true; - } - - @Override - public void endVisit(SQLDropIndexStatement x) { - - } - - @Override - public boolean visit(SQLDropViewStatement x) { - return true; - } - - @Override - public void endVisit(SQLDropViewStatement x) { - - } - - @Override - public boolean visit(SQLSavePointStatement x) { - return true; - } - - @Override - public void endVisit(SQLSavePointStatement x) { - - } - - @Override - public boolean visit(SQLRollbackStatement x) { - return true; - } - - @Override - public void endVisit(SQLRollbackStatement x) { - - } - - @Override - public boolean visit(SQLReleaseSavePointStatement x) { - return true; - } - - @Override - public void endVisit(SQLReleaseSavePointStatement x) { - } - - @Override - public boolean visit(SQLCommentHint x) { - return true; - } - - @Override - public void endVisit(SQLCommentHint x) { - - } - - @Override - public void endVisit(SQLCreateDatabaseStatement x) { - - } - - @Override - public boolean visit(SQLCreateDatabaseStatement x) { - return true; - } - - @Override - public boolean visit(SQLAlterTableDropIndex x) { - return true; - } - - @Override - public void endVisit(SQLAlterTableDropIndex x) { - - } - - @Override - public boolean visit(SQLAlterTableAddPrimaryKey x) { - return true; - } - - @Override - public void endVisit(SQLAlterTableAddPrimaryKey x) { - - } - - @Override - public void endVisit(SQLOver x) { - } - - @Override - public boolean visit(SQLOver x) { - return true; - } -} diff --git a/src/main/java/org/durid/sql/visitor/SQLEvalVisitor.java b/src/main/java/org/durid/sql/visitor/SQLEvalVisitor.java deleted file mode 100644 index 9f5d853f..00000000 --- a/src/main/java/org/durid/sql/visitor/SQLEvalVisitor.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.visitor; - -import java.util.List; - -public interface SQLEvalVisitor extends SQLASTVisitor { - - public static final String EVAL_VALUE = "eval.value"; - - List getParameters(); - - void setParameters(List parameters); - - int incrementAndGetVariantIndex(); - - boolean isMarkVariantIndex(); - - void setMarkVariantIndex(boolean markVariantIndex); -} diff --git a/src/main/java/org/durid/sql/visitor/SQLEvalVisitorImpl.java b/src/main/java/org/durid/sql/visitor/SQLEvalVisitorImpl.java deleted file mode 100644 index 45316bd2..00000000 --- a/src/main/java/org/durid/sql/visitor/SQLEvalVisitorImpl.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.visitor; - -import java.util.ArrayList; -import java.util.List; - -import org.durid.sql.ast.expr.SQLBinaryOpExpr; -import org.durid.sql.ast.expr.SQLCaseExpr; -import org.durid.sql.ast.expr.SQLCharExpr; -import org.durid.sql.ast.expr.SQLInListExpr; -import org.durid.sql.ast.expr.SQLIntegerExpr; -import org.durid.sql.ast.expr.SQLMethodInvokeExpr; -import org.durid.sql.ast.expr.SQLNullExpr; -import org.durid.sql.ast.expr.SQLNumberExpr; -import org.durid.sql.ast.expr.SQLQueryExpr; -import org.durid.sql.ast.expr.SQLVariantRefExpr; - -public class SQLEvalVisitorImpl extends SQLASTVisitorAdapter implements SQLEvalVisitor { - - private List parameters = new ArrayList(); - - private int variantIndex = -1; - - private boolean markVariantIndex = true; - - public SQLEvalVisitorImpl(){ - this(new ArrayList(1)); - } - - public SQLEvalVisitorImpl(List parameters){ - this.parameters = parameters; - } - - public List getParameters() { - return parameters; - } - - public void setParameters(List parameters) { - this.parameters = parameters; - } - - public boolean visit(SQLCharExpr x) { - return SQLEvalVisitorUtils.visit(this, x); - } - - public int incrementAndGetVariantIndex() { - return ++variantIndex; - } - - public int getVariantIndex() { - return variantIndex; - } - - public boolean visit(SQLVariantRefExpr x) { - return SQLEvalVisitorUtils.visit(this, x); - } - - public boolean visit(SQLBinaryOpExpr x) { - return SQLEvalVisitorUtils.visit(this, x); - } - - public boolean visit(SQLIntegerExpr x) { - return SQLEvalVisitorUtils.visit(this, x); - } - - public boolean visit(SQLNumberExpr x) { - return SQLEvalVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLCaseExpr x) { - return SQLEvalVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLInListExpr x) { - return SQLEvalVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLNullExpr x) { - return SQLEvalVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLMethodInvokeExpr x) { - return SQLEvalVisitorUtils.visit(this, x); - } - - @Override - public boolean visit(SQLQueryExpr x) { - return SQLEvalVisitorUtils.visit(this, x); - } - - public boolean isMarkVariantIndex() { - return markVariantIndex; - } - - public void setMarkVariantIndex(boolean markVariantIndex) { - this.markVariantIndex = markVariantIndex; - } - -} diff --git a/src/main/java/org/durid/sql/visitor/SQLEvalVisitorUtils.java b/src/main/java/org/durid/sql/visitor/SQLEvalVisitorUtils.java deleted file mode 100644 index 7cf4e673..00000000 --- a/src/main/java/org/durid/sql/visitor/SQLEvalVisitorUtils.java +++ /dev/null @@ -1,1429 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.visitor; - -import static org.durid.sql.visitor.SQLEvalVisitor.EVAL_VALUE; - -import java.math.BigDecimal; -import java.math.BigInteger; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Date; -import java.util.List; -import java.util.Map; -import java.util.regex.Pattern; - -import org.durid.DruidRuntimeException; -import org.durid.sql.SQLUtils; -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLObject; -import org.durid.sql.ast.expr.SQLBetweenExpr; -import org.durid.sql.ast.expr.SQLBinaryOpExpr; -import org.durid.sql.ast.expr.SQLCaseExpr; -import org.durid.sql.ast.expr.SQLCharExpr; -import org.durid.sql.ast.expr.SQLIdentifierExpr; -import org.durid.sql.ast.expr.SQLInListExpr; -import org.durid.sql.ast.expr.SQLMethodInvokeExpr; -import org.durid.sql.ast.expr.SQLNullExpr; -import org.durid.sql.ast.expr.SQLNumericLiteralExpr; -import org.durid.sql.ast.expr.SQLQueryExpr; -import org.durid.sql.ast.expr.SQLVariantRefExpr; -import org.durid.sql.ast.statement.SQLExprTableSource; -import org.durid.sql.ast.statement.SQLSelectItem; -import org.durid.sql.ast.statement.SQLSelectQueryBlock; -import org.durid.sql.dialect.mysql.visitor.MySqlEvalVisitorImpl; -import org.durid.util.JdbcUtils; - -public class SQLEvalVisitorUtils { - - public static Object evalExpr(String dbType, String expr, Object... parameters) { - SQLExpr sqlExpr = SQLUtils.toSQLExpr(expr, dbType); - return eval(dbType, sqlExpr, parameters); - } - - public static Object evalExpr(String dbType, String expr, List parameters) { - SQLExpr sqlExpr = SQLUtils.toSQLExpr(expr); - return eval(dbType, sqlExpr, parameters); - } - - public static Object eval(String dbType, SQLObject sqlObject, Object... parameters) { - return eval(dbType, sqlObject, Arrays.asList(parameters)); - } - - public static Object getValue(SQLObject sqlObject) { - if (sqlObject instanceof SQLNumericLiteralExpr) { - return ((SQLNumericLiteralExpr) sqlObject).getNumber(); - } - - return sqlObject.getAttributes().get(EVAL_VALUE); - } - - public static Object eval(String dbType, SQLObject sqlObject, List parameters) { - return eval(dbType, sqlObject, parameters, true); - } - - public static Object eval(String dbType, SQLObject sqlObject, List parameters, boolean throwError) { - SQLEvalVisitor visitor = createEvalVisitor(dbType); - visitor.setParameters(parameters); - sqlObject.accept(visitor); - - Object value = getValue(sqlObject); - if (value == null) { - if (throwError && !sqlObject.getAttributes().containsKey(EVAL_VALUE)) { - throw new DruidRuntimeException("eval error : " + SQLUtils.toSQLString(sqlObject, dbType)); - } - } - - return value; - } - - public static SQLEvalVisitor createEvalVisitor(String dbType) { - if (JdbcUtils.MYSQL.equals(dbType)) { - return new MySqlEvalVisitorImpl(); - } - - if (JdbcUtils.H2.equals(dbType)) { - return new MySqlEvalVisitorImpl(); - } - - return new SQLEvalVisitorImpl(); - } - - public static boolean visit(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) { - if ("concat".equalsIgnoreCase(x.getMethodName())) { - StringBuilder buf = new StringBuilder(); - - for (SQLExpr item : x.getParameters()) { - item.accept(visitor); - - Object itemValue = item.getAttributes().get(EVAL_VALUE); - if (itemValue == null) { - continue; - } - buf.append(itemValue.toString()); - } - - x.getAttributes().put(EVAL_VALUE, buf.toString()); - } else if ("now".equalsIgnoreCase(x.getMethodName())) { - x.getAttributes().put(EVAL_VALUE, new Date()); - } else if ("ascii".equalsIgnoreCase(x.getMethodName())) { - if (x.getParameters().size() == 0) { - return false; - } - SQLExpr param = x.getParameters().get(0); - param.accept(visitor); - - Object paramValue = param.getAttributes().get(EVAL_VALUE); - if (paramValue == null) { - return false; - } - - String strValue = paramValue.toString(); - if (strValue.length() == 0) { - return false; - } - - int ascii = strValue.charAt(0); - x.getAttributes().put(EVAL_VALUE, ascii); - } else if ("instr".equalsIgnoreCase(x.getMethodName())) { - if (x.getParameters().size() != 2) { - return false; - } - - SQLExpr param0 = x.getParameters().get(0); - SQLExpr param1 = x.getParameters().get(1); - param0.accept(visitor); - param1.accept(visitor); - - Object param0Value = param0.getAttributes().get(EVAL_VALUE); - Object param1Value = param1.getAttributes().get(EVAL_VALUE); - if (param0Value == null || param1Value == null) { - return false; - } - - String strValue0 = param0Value.toString(); - String strValue1 = param1Value.toString(); - - int result = strValue0.indexOf(strValue1) + 1; - - x.putAttribute(EVAL_VALUE, result); - } else if ("left".equalsIgnoreCase(x.getMethodName())) { - if (x.getParameters().size() != 2) { - return false; - } - - SQLExpr param0 = x.getParameters().get(0); - SQLExpr param1 = x.getParameters().get(1); - param0.accept(visitor); - param1.accept(visitor); - - Object param0Value = param0.getAttributes().get(EVAL_VALUE); - Object param1Value = param1.getAttributes().get(EVAL_VALUE); - if (param0Value == null || param1Value == null) { - return false; - } - - String strValue = param0Value.toString(); - int intValue = _int(param1Value); - - String result = strValue.substring(0, intValue); - - x.putAttribute(EVAL_VALUE, result); - } else if ("right".equalsIgnoreCase(x.getMethodName())) { - if (x.getParameters().size() != 2) { - return false; - } - - SQLExpr param0 = x.getParameters().get(0); - SQLExpr param1 = x.getParameters().get(1); - param0.accept(visitor); - param1.accept(visitor); - - Object param0Value = param0.getAttributes().get(EVAL_VALUE); - Object param1Value = param1.getAttributes().get(EVAL_VALUE); - if (param0Value == null || param1Value == null) { - return false; - } - - String strValue = param0Value.toString(); - int intValue = _int(param1Value); - - String result = strValue.substring(strValue.length() - intValue, strValue.length()); - - x.putAttribute(EVAL_VALUE, result); - } else if ("reverse".equalsIgnoreCase(x.getMethodName())) { - if (x.getParameters().size() != 1) { - return false; - } - - SQLExpr param0 = x.getParameters().get(0); - param0.accept(visitor); - - Object param0Value = param0.getAttributes().get(EVAL_VALUE); - if (param0Value == null) { - return false; - } - - String strValue = param0Value.toString(); - - StringBuilder buf = new StringBuilder(); - for (int i = strValue.length() - 1; i >= 0; --i) { - buf.append(strValue.charAt(i)); - } - String result = buf.toString(); - - x.putAttribute(EVAL_VALUE, result); - } else if ("trim".equalsIgnoreCase(x.getMethodName())) { - if (x.getParameters().size() != 1) { - return false; - } - - SQLExpr param0 = x.getParameters().get(0); - param0.accept(visitor); - - Object param0Value = param0.getAttributes().get(EVAL_VALUE); - if (param0Value == null) { - return false; - } - - String strValue = param0Value.toString(); - String result = strValue.trim(); - - x.putAttribute(EVAL_VALUE, result); - } else if ("length".equalsIgnoreCase(x.getMethodName())) { - if (x.getParameters().size() != 1) { - return false; - } - - SQLExpr param0 = x.getParameters().get(0); - param0.accept(visitor); - - Object param0Value = param0.getAttributes().get(EVAL_VALUE); - if (param0Value == null) { - return false; - } - - String strValue = param0Value.toString(); - - int result = strValue.length(); - - x.putAttribute(EVAL_VALUE, result); - } else if ("ucase".equalsIgnoreCase(x.getMethodName()) || "upper".equalsIgnoreCase(x.getMethodName())) { - if (x.getParameters().size() != 1) { - return false; - } - - SQLExpr param0 = x.getParameters().get(0); - param0.accept(visitor); - - Object param0Value = param0.getAttributes().get(EVAL_VALUE); - if (param0Value == null) { - return false; - } - - String strValue = param0Value.toString(); - - String result = strValue.toUpperCase(); - - x.putAttribute(EVAL_VALUE, result); - } else if ("lcase".equalsIgnoreCase(x.getMethodName()) || "lower".equalsIgnoreCase(x.getMethodName())) { - if (x.getParameters().size() != 1) { - return false; - } - - SQLExpr param0 = x.getParameters().get(0); - param0.accept(visitor); - - Object param0Value = param0.getAttributes().get(EVAL_VALUE); - if (param0Value == null) { - return false; - } - - String strValue = param0Value.toString(); - - String result = strValue.toLowerCase(); - - x.putAttribute(EVAL_VALUE, result); - - } else if ("mod".equalsIgnoreCase(x.getMethodName())) { - if (x.getParameters().size() != 2) { - return false; - } - - SQLExpr param0 = x.getParameters().get(0); - SQLExpr param1 = x.getParameters().get(1); - param0.accept(visitor); - param1.accept(visitor); - - Object param0Value = param0.getAttributes().get(EVAL_VALUE); - Object param1Value = param1.getAttributes().get(EVAL_VALUE); - if (param0Value == null || param1Value == null) { - return false; - } - - int intValue0 = _int(param0Value); - int intValue1 = _int(param1Value); - - int result = intValue0 % intValue1; - - x.putAttribute(EVAL_VALUE, result); - } else if ("abs".equalsIgnoreCase(x.getMethodName())) { - if (x.getParameters().size() != 1) { - return false; - } - - SQLExpr param0 = x.getParameters().get(0); - param0.accept(visitor); - - Object paramValue = param0.getAttributes().get(EVAL_VALUE); - if (paramValue == null) { - return false; - } - - Object result; - if (paramValue instanceof Integer) { - result = Math.abs(((Integer) paramValue).intValue()); - } else if (paramValue instanceof Long) { - result = Math.abs(((Long) paramValue).longValue()); - } else { - result = _decimal(paramValue).abs(); - } - - x.putAttribute(EVAL_VALUE, result); - } else if ("acos".equalsIgnoreCase(x.getMethodName())) { - if (x.getParameters().size() != 1) { - return false; - } - - SQLExpr param0 = x.getParameters().get(0); - param0.accept(visitor); - - Object paramValue = param0.getAttributes().get(EVAL_VALUE); - if (paramValue == null) { - return false; - } - - double doubleValue = _double(paramValue); - double result = Math.acos(doubleValue); - - if (Double.isNaN(result)) { - x.putAttribute(EVAL_VALUE, null); - } else { - x.putAttribute(EVAL_VALUE, result); - } - } else if ("asin".equalsIgnoreCase(x.getMethodName())) { - if (x.getParameters().size() != 1) { - return false; - } - - SQLExpr param0 = x.getParameters().get(0); - param0.accept(visitor); - - Object paramValue = param0.getAttributes().get(EVAL_VALUE); - if (paramValue == null) { - return false; - } - - double doubleValue = _double(paramValue); - double result = Math.asin(doubleValue); - - if (Double.isNaN(result)) { - x.putAttribute(EVAL_VALUE, null); - } else { - x.putAttribute(EVAL_VALUE, result); - } - } else if ("atan".equalsIgnoreCase(x.getMethodName())) { - if (x.getParameters().size() != 1) { - return false; - } - - SQLExpr param0 = x.getParameters().get(0); - param0.accept(visitor); - - Object paramValue = param0.getAttributes().get(EVAL_VALUE); - if (paramValue == null) { - return false; - } - - double doubleValue = _double(paramValue); - double result = Math.atan(doubleValue); - - if (Double.isNaN(result)) { - x.putAttribute(EVAL_VALUE, null); - } else { - x.putAttribute(EVAL_VALUE, result); - } - } else if ("atan2".equalsIgnoreCase(x.getMethodName())) { - if (x.getParameters().size() != 2) { - return false; - } - - SQLExpr param0 = x.getParameters().get(0); - SQLExpr param1 = x.getParameters().get(1); - param0.accept(visitor); - param1.accept(visitor); - - Object param0Value = param0.getAttributes().get(EVAL_VALUE); - Object param1Value = param1.getAttributes().get(EVAL_VALUE); - if (param0Value == null || param1Value == null) { - return false; - } - - double doubleValue0 = _double(param0Value); - double doubleValue1 = _double(param1Value); - double result = Math.atan2(doubleValue0, doubleValue1); - - if (Double.isNaN(result)) { - x.putAttribute(EVAL_VALUE, null); - } else { - x.putAttribute(EVAL_VALUE, result); - } - } else if ("ceil".equalsIgnoreCase(x.getMethodName()) || "ceiling".equalsIgnoreCase(x.getMethodName())) { - if (x.getParameters().size() != 1) { - return false; - } - - SQLExpr param0 = x.getParameters().get(0); - param0.accept(visitor); - - Object paramValue = param0.getAttributes().get(EVAL_VALUE); - if (paramValue == null) { - return false; - } - - double doubleValue = _double(paramValue); - int result = (int) Math.ceil(doubleValue); - - if (Double.isNaN(result)) { - x.putAttribute(EVAL_VALUE, null); - } else { - x.putAttribute(EVAL_VALUE, result); - } - } else if ("cos".equalsIgnoreCase(x.getMethodName())) { - if (x.getParameters().size() != 1) { - return false; - } - - SQLExpr param0 = x.getParameters().get(0); - param0.accept(visitor); - - Object paramValue = param0.getAttributes().get(EVAL_VALUE); - if (paramValue == null) { - return false; - } - - double doubleValue = _double(paramValue); - int result = (int) Math.cos(doubleValue); - - if (Double.isNaN(result)) { - x.putAttribute(EVAL_VALUE, null); - } else { - x.putAttribute(EVAL_VALUE, result); - } - } else if ("sin".equalsIgnoreCase(x.getMethodName())) { - if (x.getParameters().size() != 1) { - return false; - } - - SQLExpr param0 = x.getParameters().get(0); - param0.accept(visitor); - - Object paramValue = param0.getAttributes().get(EVAL_VALUE); - if (paramValue == null) { - return false; - } - - double doubleValue = _double(paramValue); - int result = (int) Math.sin(doubleValue); - - if (Double.isNaN(result)) { - x.putAttribute(EVAL_VALUE, null); - } else { - x.putAttribute(EVAL_VALUE, result); - } - } else if ("log".equalsIgnoreCase(x.getMethodName())) { - if (x.getParameters().size() != 1) { - return false; - } - - SQLExpr param0 = x.getParameters().get(0); - param0.accept(visitor); - - Object paramValue = param0.getAttributes().get(EVAL_VALUE); - if (paramValue == null) { - return false; - } - - double doubleValue = _double(paramValue); - int result = (int) Math.log(doubleValue); - - if (Double.isNaN(result)) { - x.putAttribute(EVAL_VALUE, null); - } else { - x.putAttribute(EVAL_VALUE, result); - } - } else if ("log10".equalsIgnoreCase(x.getMethodName())) { - if (x.getParameters().size() != 1) { - return false; - } - - SQLExpr param0 = x.getParameters().get(0); - param0.accept(visitor); - - Object paramValue = param0.getAttributes().get(EVAL_VALUE); - if (paramValue == null) { - return false; - } - - double doubleValue = _double(paramValue); - int result = (int) Math.log10(doubleValue); - - if (Double.isNaN(result)) { - x.putAttribute(EVAL_VALUE, null); - } else { - x.putAttribute(EVAL_VALUE, result); - } - } else if ("tan".equalsIgnoreCase(x.getMethodName())) { - if (x.getParameters().size() != 1) { - return false; - } - - SQLExpr param0 = x.getParameters().get(0); - param0.accept(visitor); - - Object paramValue = param0.getAttributes().get(EVAL_VALUE); - if (paramValue == null) { - return false; - } - - double doubleValue = _double(paramValue); - int result = (int) Math.tan(doubleValue); - - if (Double.isNaN(result)) { - x.putAttribute(EVAL_VALUE, null); - } else { - x.putAttribute(EVAL_VALUE, result); - } - } else if ("sqrt".equalsIgnoreCase(x.getMethodName())) { - if (x.getParameters().size() != 1) { - return false; - } - - SQLExpr param0 = x.getParameters().get(0); - param0.accept(visitor); - - Object paramValue = param0.getAttributes().get(EVAL_VALUE); - if (paramValue == null) { - return false; - } - - double doubleValue = _double(paramValue); - int result = (int) Math.sqrt(doubleValue); - - if (Double.isNaN(result)) { - x.putAttribute(EVAL_VALUE, null); - } else { - x.putAttribute(EVAL_VALUE, result); - } - } else if ("power".equalsIgnoreCase(x.getMethodName())) { - if (x.getParameters().size() != 2) { - return false; - } - - SQLExpr param0 = x.getParameters().get(0); - SQLExpr param1 = x.getParameters().get(1); - param0.accept(visitor); - param1.accept(visitor); - - Object param0Value = param0.getAttributes().get(EVAL_VALUE); - Object param1Value = param1.getAttributes().get(EVAL_VALUE); - if (param0Value == null || param1Value == null) { - return false; - } - - double doubleValue0 = _double(param0Value); - double doubleValue1 = _double(param1Value); - double result = Math.pow(doubleValue0, doubleValue1); - - if (Double.isNaN(result)) { - x.putAttribute(EVAL_VALUE, null); - } else { - x.putAttribute(EVAL_VALUE, result); - } - } else if ("pi".equalsIgnoreCase(x.getMethodName())) { - x.putAttribute(EVAL_VALUE, Math.PI); - } else if ("rand".equalsIgnoreCase(x.getMethodName())) { - x.putAttribute(EVAL_VALUE, Math.random()); - } - return false; - } - - public static boolean visit(SQLEvalVisitor visitor, SQLCharExpr x) { - x.putAttribute(EVAL_VALUE, x.getText()); - return true; - } - - public static boolean visit(SQLEvalVisitor visitor, SQLBetweenExpr x) { - x.getTestExpr().accept(visitor); - - if (!x.getTestExpr().getAttributes().containsKey(EVAL_VALUE)) { - return false; - } - - Object value = x.getTestExpr().getAttribute(EVAL_VALUE); - - x.getBeginExpr().accept(visitor); - if (!x.getBeginExpr().getAttributes().containsKey(EVAL_VALUE)) { - return false; - } - - Object begin = x.getBeginExpr().getAttribute(EVAL_VALUE); - - if (lt(value, begin)) { - x.getAttributes().put(EVAL_VALUE, x.isNot() ? true : false); - return false; - } - - x.getEndExpr().accept(visitor); - if (!x.getEndExpr().getAttributes().containsKey(EVAL_VALUE)) { - return false; - } - - Object end = x.getEndExpr().getAttribute(EVAL_VALUE); - - if (gt(value, end)) { - x.getAttributes().put(EVAL_VALUE, x.isNot() ? true : false); - return false; - } - - x.getAttributes().put(EVAL_VALUE, x.isNot() ? false : true); - return false; - } - - public static boolean visit(SQLEvalVisitor visitor, SQLNullExpr x) { - x.getAttributes().put(EVAL_VALUE, null); - return false; - } - - public static boolean visit(SQLEvalVisitor visitor, SQLCaseExpr x) { - Object value; - if (x.getValueExpr() != null) { - x.getValueExpr().accept(visitor); - - if (!x.getValueExpr().getAttributes().containsKey(EVAL_VALUE)) { - return false; - } - - value = x.getValueExpr().getAttribute(EVAL_VALUE); - } else { - value = null; - } - - for (SQLCaseExpr.Item item : x.getItems()) { - item.getConditionExpr().accept(visitor); - - if (!item.getConditionExpr().getAttributes().containsKey(EVAL_VALUE)) { - return false; - } - - Object conditionValue = item.getConditionExpr().getAttribute(EVAL_VALUE); - - if (eq(value, conditionValue)) { - item.getValueExpr().accept(visitor); - - if (item.getValueExpr().getAttributes().containsKey(EVAL_VALUE)) { - x.getAttributes().put(EVAL_VALUE, item.getValueExpr().getAttribute(EVAL_VALUE)); - } - - return false; - } - } - - if (x.getElseExpr() != null) { - x.getElseExpr().accept(visitor); - - if (x.getElseExpr().getAttributes().containsKey(EVAL_VALUE)) { - x.getAttributes().put(EVAL_VALUE, x.getElseExpr().getAttribute(EVAL_VALUE)); - } - } - - return false; - } - - public static boolean visit(SQLEvalVisitor visitor, SQLInListExpr x) { - SQLExpr valueExpr = x.getExpr(); - valueExpr.accept(visitor); - if (!valueExpr.getAttributes().containsKey(EVAL_VALUE)) { - return false; - } - Object value = valueExpr.getAttribute(EVAL_VALUE); - - for (SQLExpr item : x.getTargetList()) { - item.accept(visitor); - if (!item.getAttributes().containsKey(EVAL_VALUE)) { - return false; - } - Object itemValue = item.getAttribute(EVAL_VALUE); - if (eq(value, itemValue)) { - x.getAttributes().put(EVAL_VALUE, x.isNot() ? false : true); - return false; - } - } - - x.getAttributes().put(EVAL_VALUE, x.isNot() ? true : false); - return false; - } - - public static boolean visit(SQLEvalVisitor visitor, SQLQueryExpr x) { - if (x.getSubQuery().getQuery() instanceof SQLSelectQueryBlock) { - SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) x.getSubQuery().getQuery(); - - boolean nullFrom = false; - if (queryBlock.getFrom() == null) { - nullFrom = true; - } else if (queryBlock.getFrom() instanceof SQLExprTableSource) { - SQLExpr expr = ((SQLExprTableSource) queryBlock.getFrom()).getExpr(); - if (expr instanceof SQLIdentifierExpr) { - if ("dual".equalsIgnoreCase(((SQLIdentifierExpr) expr).getName())) { - nullFrom = true; - } - } - } - - if (nullFrom) { - List row = new ArrayList(queryBlock.getSelectList().size()); - for (int i = 0; i < queryBlock.getSelectList().size(); ++i) { - SQLSelectItem item = queryBlock.getSelectList().get(i); - item.getExpr().accept(visitor); - Object cell = item.getExpr().getAttribute(EVAL_VALUE); - row.add(cell); - } - List> rows = new ArrayList>(1); - rows.add(row); - - Object result = rows; - queryBlock.putAttribute(EVAL_VALUE, result); - x.getSubQuery().putAttribute(EVAL_VALUE, result); - x.putAttribute(EVAL_VALUE, result); - - return false; - } - } - - return false; - } - - public static boolean visit(SQLEvalVisitor visitor, SQLBinaryOpExpr x) { - SQLExpr left = x.getLeft(); - SQLExpr right = x.getRight(); - - left.accept(visitor); - if (!left.getAttributes().containsKey(EVAL_VALUE)) { - return false; - } - - right.accept(visitor); - if (!right.getAttributes().containsKey(EVAL_VALUE)) { - return false; - } - - Object value = null; - switch (x.getOperator()) { - case Add: - value = add(left.getAttribute(EVAL_VALUE), right.getAttributes().get(EVAL_VALUE)); - x.putAttribute(EVAL_VALUE, value); - break; - case Subtract: - value = sub(left.getAttribute(EVAL_VALUE), right.getAttributes().get(EVAL_VALUE)); - x.putAttribute(EVAL_VALUE, value); - break; - case Multiply: - value = multi(left.getAttribute(EVAL_VALUE), right.getAttributes().get(EVAL_VALUE)); - x.putAttribute(EVAL_VALUE, value); - break; - case Divide: - value = div(left.getAttribute(EVAL_VALUE), right.getAttributes().get(EVAL_VALUE)); - x.putAttribute(EVAL_VALUE, value); - break; - case GreaterThan: - value = gt(left.getAttribute(EVAL_VALUE), right.getAttributes().get(EVAL_VALUE)); - x.putAttribute(EVAL_VALUE, value); - break; - case GreaterThanOrEqual: - value = gteq(left.getAttribute(EVAL_VALUE), right.getAttributes().get(EVAL_VALUE)); - x.putAttribute(EVAL_VALUE, value); - break; - case LessThan: - value = lt(left.getAttribute(EVAL_VALUE), right.getAttributes().get(EVAL_VALUE)); - x.putAttribute(EVAL_VALUE, value); - break; - case LessThanOrEqual: - value = lteq(left.getAttribute(EVAL_VALUE), right.getAttributes().get(EVAL_VALUE)); - x.putAttribute(EVAL_VALUE, value); - break; - case Is: - value = eq(left.getAttribute(EVAL_VALUE), right.getAttributes().get(EVAL_VALUE)); - x.putAttribute(EVAL_VALUE, value); - break; - case IsNot: - value = !eq(left.getAttribute(EVAL_VALUE), right.getAttributes().get(EVAL_VALUE)); - x.putAttribute(EVAL_VALUE, value); - break; - case RegExp: - case RLike: { - String pattern = _string(right.getAttributes().get(EVAL_VALUE)); - String input = _string(left.getAttributes().get(EVAL_VALUE)); - boolean matchResult = Pattern.matches(pattern, input); - x.putAttribute(EVAL_VALUE, matchResult); - } - break; - case NotRegExp: - case NotRLike: { - String pattern = _string(right.getAttributes().get(EVAL_VALUE)); - String input = _string(left.getAttributes().get(EVAL_VALUE)); - boolean matchResult = !Pattern.matches(pattern, input); - x.putAttribute(EVAL_VALUE, matchResult); - } - break; - default: - break; - } - - return false; - } - - public static boolean visit(SQLEvalVisitor visitor, SQLNumericLiteralExpr x) { - x.getAttributes().put(EVAL_VALUE, x.getNumber()); - return false; - } - - public static boolean visit(SQLEvalVisitor visitor, SQLVariantRefExpr x) { - if (!"?".equals(x.getName())) { - return false; - } - - Map attributes = x.getAttributes(); - - int varIndex = x.getIndex(); - - if (varIndex != -1 && visitor.getParameters().size() > varIndex) { - boolean containsValue = attributes.containsKey(EVAL_VALUE); - if (!containsValue) { - Object value = visitor.getParameters().get(varIndex); - attributes.put(EVAL_VALUE, value); - } - } - - return false; - } - - public static Boolean _bool(Object val) { - if (val == null) { - return null; - } - - if (val instanceof Boolean) { - return (Boolean) val; - } - - if (val instanceof Number) { - return ((Number) val).intValue() == 1; - } - - throw new IllegalArgumentException(); - } - - public static String _string(Object val) { - Object value = val; - - if (value == null) { - return null; - } - - return value.toString(); - } - - public static Byte _byte(Object val) { - if (val == null) { - return null; - } - - if (val instanceof Byte) { - return (Byte) val; - } - - return ((Number) val).byteValue(); - } - - public static Short _short(Object val) { - if (val == null) { - return null; - } - - if (val instanceof Short) { - return (Short) val; - } - - return ((Number) val).shortValue(); - } - - public static Integer _int(Object val) { - if (val == null) { - return null; - } - - if (val instanceof Integer) { - return (Integer) val; - } - - return ((Number) val).intValue(); - } - - public static Long _long(Object val) { - if (val == null) { - return null; - } - - if (val instanceof Long) { - return (Long) val; - } - - return ((Number) val).longValue(); - } - - public static Float _float(Object val) { - if (val == null) { - return null; - } - - if (val instanceof Float) { - return (Float) val; - } - - return ((Number) val).floatValue(); - } - - public static Double _double(Object val) { - if (val == null) { - return null; - } - - if (val instanceof Double) { - return (Double) val; - } - - return ((Number) val).doubleValue(); - } - - public static BigInteger _bigInt(Object val) { - if (val == null) { - return null; - } - - if (val instanceof BigInteger) { - return (BigInteger) val; - } - - if (val instanceof String) { - return new BigInteger((String) val); - } - - return BigInteger.valueOf(((Number) val).longValue()); - } - - public static Date _date(Object val) { - if (val == null) { - return null; - } - - if (val instanceof Date) { - return (Date) val; - } - - if (val instanceof Number) { - return new Date(((Number) val).longValue()); - } - - if (val instanceof String) { - return _date((String) val); - } - - throw new DruidRuntimeException("can cast to date"); - } - - public static Date _date(String text) { - if (text == null || text.length() == 0) { - return null; - } - - String format; - - if (text.length() == "yyyy-MM-dd".length()) { - format = "yyyy-MM-dd"; - } else { - format = "yyyy-MM-dd HH:mm:ss"; - } - - try { - return new SimpleDateFormat(format).parse(text); - } catch (ParseException e) { - throw new DruidRuntimeException("format : " + format + ", value : " + text, e); - } - } - - public static BigDecimal _decimal(Object val) { - if (val == null) { - return null; - } - - if (val instanceof BigDecimal) { - return (BigDecimal) val; - } - - if (val instanceof String) { - return new BigDecimal((String) val); - } - - if (val instanceof Float) { - return new BigDecimal((Float) val); - } - - if (val instanceof Double) { - return new BigDecimal((Double) val); - } - - return BigDecimal.valueOf(((Number) val).longValue()); - } - - public static Object _sum(Object a, Object b) { - if (a == null) { - return b; - } - - if (b == null) { - return a; - } - - if (a instanceof BigDecimal || b instanceof BigDecimal) { - return _decimal(a).add(_decimal(b)); - } - - if (a instanceof BigInteger || b instanceof BigInteger) { - return _bigInt(a).add(_bigInt(b)); - } - - if (a instanceof Long || b instanceof Long) { - return _long(a) + _long(b); - } - - if (a instanceof Integer || b instanceof Integer) { - return _int(a) + _int(b); - } - - if (a instanceof Short || b instanceof Short) { - return _short(a) + _short(b); - } - - if (a instanceof Byte || b instanceof Byte) { - return _byte(a) + _byte(b); - } - - throw new IllegalArgumentException(); - } - - public static Object div(Object a, Object b) { - if (a == null || b == null) { - return null; - } - - if (a instanceof BigDecimal || b instanceof BigDecimal) { - return _decimal(a).divide(_decimal(b)); - } - - if (a instanceof BigInteger || b instanceof BigInteger) { - return _bigInt(a).divide(_bigInt(b)); - } - - if (a instanceof Long || b instanceof Long) { - return _long(a) / _long(b); - } - - if (a instanceof Integer || b instanceof Integer) { - return _int(a) / _int(b); - } - - if (a instanceof Short || b instanceof Short) { - return _short(a) / _short(b); - } - - if (a instanceof Byte || b instanceof Byte) { - return _byte(a) / _byte(b); - } - - throw new IllegalArgumentException(); - } - - public static boolean gt(Object a, Object b) { - if (a == null) { - return false; - } - - if (b == null) { - return true; - } - - if (a instanceof BigDecimal || b instanceof BigDecimal) { - return _decimal(a).compareTo(_decimal(b)) > 0; - } - - if (a instanceof BigInteger || b instanceof BigInteger) { - return _bigInt(a).compareTo(_bigInt(b)) > 0; - } - - if (a instanceof Long || b instanceof Long) { - return _long(a) > _long(b); - } - - if (a instanceof Integer || b instanceof Integer) { - return _int(a) > _int(b); - } - - if (a instanceof Short || b instanceof Short) { - return _short(a) > _short(b); - } - - if (a instanceof Byte || b instanceof Byte) { - return _byte(a) > _byte(b); - } - - if (a instanceof Date || b instanceof Date) { - Date d1 = _date(a); - Date d2 = _date(b); - - if (d1 == d2) { - return false; - } - - if (d1 == null) { - return false; - } - - if (d2 == null) { - return true; - } - - return d1.compareTo(d2) > 0; - } - - throw new IllegalArgumentException(); - } - - public static boolean gteq(Object a, Object b) { - if (eq(a, b)) { - return true; - } - - return gt(a, b); - } - - public static boolean lt(Object a, Object b) { - if (a == null) { - return true; - } - - if (b == null) { - return false; - } - - if (a instanceof BigDecimal || b instanceof BigDecimal) { - return _decimal(a).compareTo(_decimal(b)) < 0; - } - - if (a instanceof BigInteger || b instanceof BigInteger) { - return _bigInt(a).compareTo(_bigInt(b)) < 0; - } - - if (a instanceof Long || b instanceof Long) { - return _long(a) < _long(b); - } - - if (a instanceof Integer || b instanceof Integer) { - return _int(a) < _int(b); - } - - if (a instanceof Short || b instanceof Short) { - return _short(a) < _short(b); - } - - if (a instanceof Byte || b instanceof Byte) { - return _byte(a) < _byte(b); - } - - if (a instanceof Date || b instanceof Date) { - Date d1 = _date(a); - Date d2 = _date(b); - - if (d1 == d2) { - return false; - } - - if (d1 == null) { - return true; - } - - if (d2 == null) { - return false; - } - - return d1.compareTo(d2) < 0; - } - - throw new IllegalArgumentException(); - } - - public static boolean lteq(Object a, Object b) { - if (eq(a, b)) { - return true; - } - - return lt(a, b); - } - - public static boolean eq(Object a, Object b) { - if (a == b) { - return true; - } - - if (a == null || b == null) { - return false; - } - - if (a.equals(b)) { - return true; - } - - if (a instanceof BigDecimal || b instanceof BigDecimal) { - return _decimal(a).compareTo(_decimal(b)) == 0; - } - - if (a instanceof BigInteger || b instanceof BigInteger) { - return _bigInt(a).compareTo(_bigInt(b)) == 0; - } - - if (a instanceof Long || b instanceof Long) { - return _long(a) == _long(b); - } - - if (a instanceof Integer || b instanceof Integer) { - return _int(a) == _int(b); - } - - if (a instanceof Short || b instanceof Short) { - return _short(a) == _short(b); - } - - if (a instanceof Byte || b instanceof Byte) { - return _byte(a) == _byte(b); - } - - if (a instanceof Date || b instanceof Date) { - Date d1 = _date(a); - Date d2 = _date(b); - - if (d1 == d2) { - return true; - } - - if (d1 == null || d2 == null) { - return false; - } - - return d1.equals(d2); - } - - if (a instanceof String || b instanceof String) { - return _string(a).equals(_string(b)); - } - - throw new IllegalArgumentException(); - } - - public static Object add(Object a, Object b) { - if (a == null) { - return b; - } - - if (b == null) { - return a; - } - - if (a instanceof BigDecimal || b instanceof BigDecimal) { - return _decimal(a).add(_decimal(b)); - } - - if (a instanceof BigInteger || b instanceof BigInteger) { - return _bigInt(a).add(_bigInt(b)); - } - - if (a instanceof Double || b instanceof Double) { - return _double(a) + _double(b); - } - - if (a instanceof Float || b instanceof Float) { - return _float(a) + _float(b); - } - - if (a instanceof Long || b instanceof Long) { - return _long(a) + _long(b); - } - - if (a instanceof Integer || b instanceof Integer) { - return _int(a) + _int(b); - } - - if (a instanceof Short || b instanceof Short) { - return _short(a) + _short(b); - } - - if (a instanceof Byte || b instanceof Byte) { - return _byte(a) + _byte(b); - } - - if (a instanceof String || b instanceof String) { - return _string(a) + _string(b); - } - - throw new IllegalArgumentException(); - } - - public static Object sub(Object a, Object b) { - if (a == null) { - return null; - } - - if (b == null) { - return a; - } - - if (a instanceof BigDecimal || b instanceof BigDecimal) { - return _decimal(a).subtract(_decimal(b)); - } - - if (a instanceof BigInteger || b instanceof BigInteger) { - return _bigInt(a).subtract(_bigInt(b)); - } - - if (a instanceof Double || b instanceof Double) { - return _double(a) - _double(b); - } - - if (a instanceof Float || b instanceof Float) { - return _float(a) - _float(b); - } - - if (a instanceof Long || b instanceof Long) { - return _long(a) - _long(b); - } - - if (a instanceof Integer || b instanceof Integer) { - return _int(a) - _int(b); - } - - if (a instanceof Short || b instanceof Short) { - return _short(a) - _short(b); - } - - if (a instanceof Byte || b instanceof Byte) { - return _byte(a) - _byte(b); - } - - throw new IllegalArgumentException(); - } - - public static Object multi(Object a, Object b) { - if (a == null || b == null) { - return null; - } - - if (a instanceof BigDecimal || b instanceof BigDecimal) { - return _decimal(a).multiply(_decimal(b)); - } - - if (a instanceof BigInteger || b instanceof BigInteger) { - return _bigInt(a).multiply(_bigInt(b)); - } - - if (a instanceof Long || b instanceof Long) { - return _long(a) * _long(b); - } - - if (a instanceof Integer || b instanceof Integer) { - return _int(a) * _int(b); - } - - if (a instanceof Short || b instanceof Short) { - return _short(a) * _short(b); - } - - if (a instanceof Byte || b instanceof Byte) { - return _byte(a) * _byte(b); - } - - throw new IllegalArgumentException(); - } -} diff --git a/src/main/java/org/durid/sql/visitor/SQLShardingContext.java b/src/main/java/org/durid/sql/visitor/SQLShardingContext.java deleted file mode 100644 index 70510031..00000000 --- a/src/main/java/org/durid/sql/visitor/SQLShardingContext.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.visitor; - - -public class SQLShardingContext { - -} diff --git a/src/main/java/org/durid/sql/visitor/SQLShardingVisitor.java b/src/main/java/org/durid/sql/visitor/SQLShardingVisitor.java deleted file mode 100644 index fec75ade..00000000 --- a/src/main/java/org/durid/sql/visitor/SQLShardingVisitor.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.visitor; - -import org.durid.sql.ast.SQLObject; -import org.durid.sql.ast.statement.SQLInsertStatement; - -public class SQLShardingVisitor extends SQLASTVisitorAdapter { - - public void postVisit(SQLObject x) { - } - - public void preVisit(SQLObject x) { - } - - public boolean visit(SQLInsertStatement x) { - return false; - } -} diff --git a/src/main/java/org/durid/sql/visitor/SchemaStatVisitor.java b/src/main/java/org/durid/sql/visitor/SchemaStatVisitor.java deleted file mode 100644 index 2344cc89..00000000 --- a/src/main/java/org/durid/sql/visitor/SchemaStatVisitor.java +++ /dev/null @@ -1,985 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.visitor; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLObject; -import org.durid.sql.ast.SQLOrderBy; -import org.durid.sql.ast.SQLOrderingSpecification; -import org.durid.sql.ast.expr.SQLAggregateExpr; -import org.durid.sql.ast.expr.SQLAllColumnExpr; -import org.durid.sql.ast.expr.SQLBinaryOpExpr; -import org.durid.sql.ast.expr.SQLCurrentOfCursorExpr; -import org.durid.sql.ast.expr.SQLIdentifierExpr; -import org.durid.sql.ast.expr.SQLInListExpr; -import org.durid.sql.ast.expr.SQLInSubQueryExpr; -import org.durid.sql.ast.expr.SQLIntegerExpr; -import org.durid.sql.ast.expr.SQLMethodInvokeExpr; -import org.durid.sql.ast.expr.SQLPropertyExpr; -import org.durid.sql.ast.statement.SQLAlterTableAddColumn; -import org.durid.sql.ast.statement.SQLAlterTableStatement; -import org.durid.sql.ast.statement.SQLCallStatement; -import org.durid.sql.ast.statement.SQLColumnDefinition; -import org.durid.sql.ast.statement.SQLCommentStatement; -import org.durid.sql.ast.statement.SQLCreateTableStatement; -import org.durid.sql.ast.statement.SQLDeleteStatement; -import org.durid.sql.ast.statement.SQLDropTableStatement; -import org.durid.sql.ast.statement.SQLExprTableSource; -import org.durid.sql.ast.statement.SQLInsertStatement; -import org.durid.sql.ast.statement.SQLJoinTableSource; -import org.durid.sql.ast.statement.SQLSelect; -import org.durid.sql.ast.statement.SQLSelectItem; -import org.durid.sql.ast.statement.SQLSelectOrderByItem; -import org.durid.sql.ast.statement.SQLSelectQuery; -import org.durid.sql.ast.statement.SQLSelectQueryBlock; -import org.durid.sql.ast.statement.SQLSelectStatement; -import org.durid.sql.ast.statement.SQLSubqueryTableSource; -import org.durid.sql.ast.statement.SQLTableElement; -import org.durid.sql.ast.statement.SQLTruncateStatement; -import org.durid.sql.ast.statement.SQLUpdateStatement; -import org.durid.stat.TableStat; -import org.durid.stat.TableStat.Column; -import org.durid.stat.TableStat.Condition; -import org.durid.stat.TableStat.Mode; -import org.durid.stat.TableStat.Relationship; - -public class SchemaStatVisitor extends SQLASTVisitorAdapter { - - protected final HashMap tableStats = new LinkedHashMap(); - protected final Set columns = new LinkedHashSet(); - protected final List conditions = new ArrayList(); - protected final Set relationships = new LinkedHashSet(); - protected final List orderByColumns = new ArrayList(); - protected final Set groupByColumns = new LinkedHashSet(); - - protected final Map subQueryMap = new LinkedHashMap(); - - protected final Map variants = new LinkedHashMap(); - - protected Map aliasMap = new HashMap(); - - protected String currentTable; - - public final static String ATTR_TABLE = "_table_"; - public final static String ATTR_COLUMN = "_column_"; - - private List parameters; - - private Mode mode; - - public SchemaStatVisitor(){ - this(new ArrayList()); - } - - public SchemaStatVisitor(List parameters){ - this.parameters = parameters; - } - - public List getParameters() { - return parameters; - } - - public void setParameters(List parameters) { - this.parameters = parameters; - } - - public TableStat getTableStat(String ident) { - return getTableStat(ident, null); - } - - public Column addColumn(String tableName, String columnName) { - tableName = handleName(tableName); - columnName = handleName(columnName); - - Column column = new Column(tableName, columnName); - columns.add(column); - return column; - } - - public TableStat getTableStat(String tableName, String alias) { - if (variants.containsKey(tableName)) { - return null; - } - - tableName = handleName(tableName); - TableStat stat = tableStats.get(tableName); - if (stat == null) { - stat = new TableStat(); - tableStats.put(new TableStat.Name(tableName), stat); - if (alias != null) { - aliasMap.put(alias, tableName); - } - } - return stat; - } - - private String handleName(String ident) { - ident = ident.replaceAll("\"", ""); - ident = ident.replaceAll("`", ""); - ident = ident.replaceAll(" ", ""); - - ident = aliasWrap(ident); - - return ident; - } - - public Map getVariants() { - return variants; - } - - public void setAliasMap() { - this.setAliasMap(new HashMap()); - } - - public void clearAliasMap() { - this.aliasMap = null; - } - - public void setAliasMap(Map aliasMap) { - this.aliasMap = aliasMap; - } - - public Map getAliasMap() { - return aliasMap; - } - - public void setCurrentTable(String table) { - this.currentTable = table; - } - - public void setCurrentTable(SQLObject x) { - x.putAttribute("_old_local_", this.currentTable); - } - - public void restoreCurrentTable(SQLObject x) { - String table = (String) x.getAttribute("_old_local_"); - this.currentTable = table; - } - - public void setCurrentTable(SQLObject x, String table) { - x.putAttribute("_old_local_", this.currentTable); - this.currentTable = table; - } - - public String getCurrentTable() { - return currentTable; - } - - protected Mode getMode() { - return mode; - } - - protected void setModeOrigin(SQLObject x) { - Mode originalMode = (Mode) x.getAttribute("_original_use_mode"); - mode = originalMode; - } - - protected Mode setMode(SQLObject x, Mode mode) { - Mode oldMode = this.mode; - x.putAttribute("_original_use_mode", oldMode); - this.mode = mode; - return oldMode; - } - - public class OrderByStatVisitor extends SQLASTVisitorAdapter { - - private final SQLOrderBy orderBy; - - public OrderByStatVisitor(SQLOrderBy orderBy){ - this.orderBy = orderBy; - for (SQLSelectOrderByItem item : orderBy.getItems()) { - item.getExpr().setParent(item); - } - } - - public SQLOrderBy getOrderBy() { - return orderBy; - } - - public boolean visit(SQLIdentifierExpr x) { - if (subQueryMap.containsKey(currentTable)) { - return false; - } - - if (currentTable != null) { - addOrderByColumn(currentTable, x.getName(), x); - } else { - addOrderByColumn("UNKOWN", x.getName(), x); - } - return false; - } - - public boolean visit(SQLPropertyExpr x) { - if (x.getOwner() instanceof SQLIdentifierExpr) { - String owner = ((SQLIdentifierExpr) x.getOwner()).getName(); - - if (subQueryMap.containsKey(owner)) { - return false; - } - - owner = aliasWrap(owner); - - if (owner != null) { - addOrderByColumn(owner, x.getName(), x); - } - } - - return false; - } - - public void addOrderByColumn(String table, String columnName, SQLObject expr) { - Column column = new Column(table, columnName); - - SQLObject parent = expr.getParent(); - if (parent instanceof SQLSelectOrderByItem) { - SQLOrderingSpecification type = ((SQLSelectOrderByItem) parent).getType(); - column.getAttributes().put("orderBy.type", type); - } - - orderByColumns.add(column); - } - } - - public boolean visit(SQLOrderBy x) { - OrderByStatVisitor orderByVisitor = new OrderByStatVisitor(x); - SQLSelectQueryBlock query = null; - if (x.getParent() instanceof SQLSelectQueryBlock) { - query = (SQLSelectQueryBlock) x.getParent(); - } - if (query != null) { - for (SQLSelectOrderByItem item : x.getItems()) { - SQLExpr expr = item.getExpr(); - if (expr instanceof SQLIntegerExpr) { - int intValue = ((SQLIntegerExpr) expr).getNumber().intValue() - 1; - if (intValue < query.getSelectList().size()) { - SQLSelectItem selectItem = query.getSelectList().get(intValue); - selectItem.getExpr().accept(orderByVisitor); - } - } - } - } - x.accept(orderByVisitor); - return true; - } - - public Set getRelationships() { - return relationships; - } - - public List getOrderByColumns() { - return orderByColumns; - } - - public Set getGroupByColumns() { - return groupByColumns; - } - - public List getConditions() { - return conditions; - } - - public boolean visit(SQLBinaryOpExpr x) { - x.getLeft().setParent(x); - x.getRight().setParent(x); - - switch (x.getOperator()) { - case Equality: - case NotEqual: - case GreaterThan: - case GreaterThanOrEqual: - case LessThan: - case LessThanOrEqual: - case LessThanOrEqualOrGreaterThan: - case Like: - case NotLike: - case Is: - case IsNot: - handleCondition(x.getLeft(), x.getOperator().name, x.getRight()); - handleCondition(x.getRight(), x.getOperator().name, x.getLeft()); - - handleRelationship(x.getLeft(), x.getOperator().name, x.getRight()); - break; - default: - break; - } - return true; - } - - protected void handleRelationship(SQLExpr left, String operator, SQLExpr right) { - Column leftColumn = getColumn(left); - if (leftColumn == null) { - return; - } - - Column rightColumn = getColumn(right); - if (rightColumn == null) { - return; - } - - Relationship relationship = new Relationship(); - relationship.setLeft(leftColumn); - relationship.setRight(rightColumn); - relationship.setOperator(operator); - - this.relationships.add(relationship); - } - - protected void handleCondition(SQLExpr expr, String operator, List values) { - handleCondition(expr, operator, values.toArray(new SQLExpr[values.size()])); - } - - protected void handleCondition(SQLExpr expr, String operator, SQLExpr... valueExprs) { - Column column = getColumn(expr); - if (column == null) { - return; - } - - Condition condition = null; - for (Condition item : this.getConditions()) { - if (item.getColumn().equals(column) && item.getOperator().equals(operator)) { - condition = item; - break; - } - } - - if (condition == null) { - condition = new Condition(); - condition.setColumn(column); - condition.setOperator(operator); - this.conditions.add(condition); - } - - for (SQLExpr item : valueExprs) { - Object value = SQLEvalVisitorUtils.eval(getDbType(), item, parameters, false); - condition.getValues().add(value); - } - } - - public String getDbType() { - return null; - } - - protected Column getColumn(SQLExpr expr) { - Map aliasMap = getAliasMap(); - if (aliasMap == null) { - return null; - } - - if (expr instanceof SQLPropertyExpr) { - SQLExpr owner = ((SQLPropertyExpr) expr).getOwner(); - String column = ((SQLPropertyExpr) expr).getName(); - - if (owner instanceof SQLIdentifierExpr) { - String tableName = ((SQLIdentifierExpr) owner).getName(); - String table = tableName; - if (aliasMap.containsKey(table)) { - table = aliasMap.get(table); - } - - if (variants.containsKey(table)) { - return null; - } - - if (table != null) { - return new Column(table, column); - } - - return handleSubQueryColumn(tableName, column); - } - - return null; - } - - if (expr instanceof SQLIdentifierExpr) { - Column attrColumn = (Column) expr.getAttribute(ATTR_COLUMN); - if (attrColumn != null) { - return attrColumn; - } - - String column = ((SQLIdentifierExpr) expr).getName(); - String table = getCurrentTable(); - if (table != null && aliasMap.containsKey(table)) { - table = aliasMap.get(table); - if (table == null) { - return null; - } - } - - if (table != null) { - return new Column(table, column); - } - - if (variants.containsKey(column)) { - return null; - } - - return new Column("UNKNOWN", column); - } - - return null; - } - - @Override - public boolean visit(SQLTruncateStatement x) { - setMode(x, Mode.Delete); - - setAliasMap(); - - String originalTable = getCurrentTable(); - - for (SQLExprTableSource tableSource : x.getTableSources()) { - SQLName name = (SQLName) tableSource.getExpr(); - - String ident = name.toString(); - setCurrentTable(ident); - x.putAttribute("_old_local_", originalTable); - - TableStat stat = getTableStat(ident); - stat.incrementDeleteCount(); - - Map aliasMap = getAliasMap(); - if (aliasMap != null) { - aliasMap.put(ident, ident); - } - } - - return false; - } - - @Override - public boolean visit(SQLDropTableStatement x) { - setMode(x, Mode.Insert); - - setAliasMap(); - - String originalTable = getCurrentTable(); - - for (SQLExprTableSource tableSource : x.getTableSources()) { - SQLName name = (SQLName) tableSource.getExpr(); - String ident = name.toString(); - setCurrentTable(ident); - x.putAttribute("_old_local_", originalTable); - - TableStat stat = getTableStat(ident); - stat.incrementDropCount(); - - Map aliasMap = getAliasMap(); - if (aliasMap != null) { - aliasMap.put(ident, ident); - } - } - - return false; - } - - @Override - public boolean visit(SQLInsertStatement x) { - setMode(x, Mode.Insert); - - setAliasMap(); - - String originalTable = getCurrentTable(); - - if (x.getTableName() instanceof SQLName) { - String ident = ((SQLName) x.getTableName()).toString(); - setCurrentTable(ident); - x.putAttribute("_old_local_", originalTable); - - TableStat stat = getTableStat(ident); - stat.incrementInsertCount(); - - Map aliasMap = getAliasMap(); - if (aliasMap != null) { - if (x.getAlias() != null) { - aliasMap.put(x.getAlias(), ident); - } - aliasMap.put(ident, ident); - } - } - - accept(x.getColumns()); - accept(x.getQuery()); - - return false; - } - - protected void accept(SQLObject x) { - if (x != null) { - x.accept(this); - } - } - - protected void accept(List nodes) { - for (int i = 0, size = nodes.size(); i < size; ++i) { - accept(nodes.get(i)); - } - } - - public boolean visit(SQLSelectQueryBlock x) { - setMode(x, Mode.Select); - - if (x.getFrom() instanceof SQLSubqueryTableSource) { - x.getFrom().accept(this); - return false; - } - - if (x.getInto() != null && x.getInto().getExpr() instanceof SQLName) { - SQLName into = (SQLName) x.getInto().getExpr(); - String ident = into.toString(); - TableStat stat = getTableStat(ident); - if (stat != null) { - stat.incrementInsertCount(); - } - } - - String originalTable = getCurrentTable(); - - if (x.getFrom() instanceof SQLExprTableSource) { - SQLExprTableSource tableSource = (SQLExprTableSource) x.getFrom(); - if (tableSource.getExpr() instanceof SQLName) { - String ident = tableSource.getExpr().toString(); - - setCurrentTable(x, ident); - x.putAttribute(ATTR_TABLE, ident); - if (x.getParent() instanceof SQLSelect) { - x.getParent().putAttribute(ATTR_TABLE, ident); - } - x.putAttribute("_old_local_", originalTable); - } - } - - if (x.getFrom() != null) { - x.getFrom().accept(this); // 提前执行,获得aliasMap - String table = (String) x.getFrom().getAttribute(ATTR_TABLE); - if (table != null) { - x.putAttribute(ATTR_TABLE, table); - } - } - - // String ident = x.getTable().toString(); - // - // TableStat stat = getTableStat(ident); - // stat.incrementInsertCount(); - // return false; - - if (x.getWhere() != null) { - x.getWhere().setParent(x); - } - - return true; - } - - public void endVisit(SQLSelectQueryBlock x) { - String originalTable = (String) x.getAttribute("_old_local_"); - x.putAttribute("table", getCurrentTable()); - setCurrentTable(originalTable); - - setModeOrigin(x); - } - - public boolean visit(SQLJoinTableSource x) { - return true; - } - - public boolean visit(SQLPropertyExpr x) { - if (x.getOwner() instanceof SQLIdentifierExpr) { - String owner = ((SQLIdentifierExpr) x.getOwner()).getName(); - - if (subQueryMap.containsKey(owner)) { - return false; - } - - owner = aliasWrap(owner); - - if (owner != null) { - Column column = addColumn(owner, x.getName()); - x.putAttribute(ATTR_COLUMN, column); - } - } - return false; - } - - protected String aliasWrap(String name) { - Map aliasMap = getAliasMap(); - - if (aliasMap != null) { - for (Map.Entry entry : aliasMap.entrySet()) { - if (entry.getKey() == null) { - continue; - } - if (entry.getKey().equalsIgnoreCase(name)) { - return entry.getValue(); - } - } - } - - return name; - } - - protected Column handleSubQueryColumn(String owner, String alias) { - SQLObject query = subQueryMap.get(owner); - - if (query == null) { - return null; - } - - List selectList = null; - if (query instanceof SQLSelectQueryBlock) { - selectList = ((SQLSelectQueryBlock) query).getSelectList(); - } - - if (selectList != null) { - for (SQLSelectItem item : selectList) { - String itemAlias = item.getAlias(); - SQLExpr itemExpr = item.getExpr(); - if (itemAlias == null) { - if (itemExpr instanceof SQLIdentifierExpr) { - itemAlias = itemExpr.toString(); - } else if (itemExpr instanceof SQLPropertyExpr) { - itemAlias = ((SQLPropertyExpr) itemExpr).getName(); - } - } - - if (alias.equalsIgnoreCase(itemAlias)) { - Column column = (Column) itemExpr.getAttribute(ATTR_COLUMN); - return column; - } - - } - } - - return null; - } - - public boolean visit(SQLIdentifierExpr x) { - String currentTable = getCurrentTable(); - - if (subQueryMap.containsKey(currentTable)) { - return false; - } - - String ident = x.toString(); - - if (variants.containsKey(ident)) { - return false; - } - - if (currentTable != null) { - Column column = addColumn(currentTable, ident); - x.putAttribute(ATTR_COLUMN, column); - } else { - Column column = handleUnkownColumn(ident); - if (column != null) { - x.putAttribute(ATTR_COLUMN, column); - } - } - return false; - } - - protected Column handleUnkownColumn(String columnName) { - return addColumn("UNKNOWN", columnName); - } - - public boolean visit(SQLAllColumnExpr x) { - String currentTable = getCurrentTable(); - - if (subQueryMap.containsKey(currentTable)) { - return false; - } - - if (currentTable != null) { - addColumn(currentTable, "*"); - } - return false; - } - - public Map getTables() { - return tableStats; - } - - public boolean containsTable(String tableName) { - return tableStats.containsKey(new TableStat.Name(tableName)); - } - - public Set getColumns() { - return columns; - } - - public boolean visit(SQLSelectStatement x) { - setAliasMap(); - return true; - } - - public void endVisit(SQLSelectStatement x) { - } - - public boolean visit(SQLSubqueryTableSource x) { - x.getSelect().accept(this); - - SQLSelectQuery query = x.getSelect().getQuery(); - - Map aliasMap = getAliasMap(); - if (aliasMap != null && x.getAlias() != null) { - aliasMap.put(x.getAlias(), null); - subQueryMap.put(x.getAlias(), query); - } - return false; - } - - protected boolean isSimpleExprTableSource(SQLExprTableSource x) { - return x.getExpr() instanceof SQLName; - } - - public boolean visit(SQLExprTableSource x) { - if (isSimpleExprTableSource(x)) { - String ident = x.getExpr().toString(); - - if (variants.containsKey(ident)) { - return false; - } - - if (subQueryMap.containsKey(ident)) { - return false; - } - - Map aliasMap = getAliasMap(); - - TableStat stat = getTableStat(ident); - - Mode mode = getMode(); - switch (mode) { - case Delete: - stat.incrementDeleteCount(); - break; - case Insert: - stat.incrementInsertCount(); - break; - case Update: - stat.incrementUpdateCount(); - break; - case Select: - stat.incrementSelectCount(); - break; - case Merge: - stat.incrementMergeCount(); - break; - default: - break; - } - - if (aliasMap != null) { - String alias = x.getAlias(); - if (alias != null && !aliasMap.containsKey(alias)) { - aliasMap.put(alias, ident); - } - if (!aliasMap.containsKey(ident)) { - aliasMap.put(ident, ident); - } - } - } else { - accept(x.getExpr()); - } - - return false; - } - - public boolean visit(SQLSelectItem x) { - x.getExpr().setParent(x); - return true; - } - - public void endVisit(SQLSelect x) { - restoreCurrentTable(x); - } - - public boolean visit(SQLSelect x) { - setCurrentTable(x); - - if (x.getOrderBy() != null) { - x.getOrderBy().setParent(x); - } - - accept(x.getQuery()); - - String originalTable = getCurrentTable(); - - setCurrentTable((String) x.getQuery().getAttribute("table")); - x.putAttribute("_old_local_", originalTable); - - accept(x.getOrderBy()); - - setCurrentTable(originalTable); - - return false; - } - - public boolean visit(SQLAggregateExpr x) { - accept(x.getArguments()); - return false; - } - - public boolean visit(SQLMethodInvokeExpr x) { - accept(x.getParameters()); - return false; - } - - public boolean visit(SQLUpdateStatement x) { - setAliasMap(); - - String ident = x.getTableName().toString(); - setCurrentTable(ident); - - TableStat stat = getTableStat(ident); - stat.incrementUpdateCount(); - - Map aliasMap = getAliasMap(); - aliasMap.put(ident, ident); - - accept(x.getItems()); - accept(x.getWhere()); - - return false; - } - - public boolean visit(SQLDeleteStatement x) { - setAliasMap(); - - setMode(x, Mode.Delete); - - String tableName = x.getTableName().toString(); - setCurrentTable(tableName); - - if (x.getAlias() != null) { - this.aliasMap.put(x.getAlias(), tableName); - } - - TableStat stat = getTableStat(tableName); - stat.incrementDeleteCount(); - - accept(x.getWhere()); - - return false; - } - - public boolean visit(SQLInListExpr x) { - if (x.isNot()) { - handleCondition(x.getExpr(), "NOT IN", x.getTargetList()); - } else { - handleCondition(x.getExpr(), "IN", x.getTargetList()); - } - - return true; - } - - @Override - public boolean visit(SQLInSubQueryExpr x) { - if (x.isNot()) { - handleCondition(x.getExpr(), "NOT IN"); - } else { - handleCondition(x.getExpr(), "IN"); - } - return true; - } - - public void endVisit(SQLDeleteStatement x) { - - } - - public void endVisit(SQLUpdateStatement x) { - } - - public boolean visit(SQLCreateTableStatement x) { - for (SQLTableElement e : x.getTableElementList()) { - e.setParent(x); - } - - String tableName = x.getName().toString(); - - TableStat stat = getTableStat(tableName); - stat.incrementCreateCount(); - setCurrentTable(x, tableName); - - accept(x.getTableElementList()); - - restoreCurrentTable(x); - - return false; - } - - public boolean visit(SQLColumnDefinition x) { - String tableName = null; - { - SQLObject parent = x.getParent(); - if (parent instanceof SQLCreateTableStatement) { - tableName = ((SQLCreateTableStatement) parent).getName().toString(); - } - } - - if (tableName == null) { - return true; - } - - String columnName = x.getName().toString(); - addColumn(tableName, columnName); - - return false; - } - - @Override - public boolean visit(SQLCallStatement x) { - return false; - } - - @Override - public void endVisit(SQLCommentStatement x) { - - } - - @Override - public boolean visit(SQLCommentStatement x) { - return false; - } - - public boolean visit(SQLCurrentOfCursorExpr x) { - return false; - } - - @Override - public boolean visit(SQLAlterTableAddColumn x) { - SQLAlterTableStatement stmt = (SQLAlterTableStatement) x.getParent(); - String table = stmt.getName().toString(); - - for (SQLColumnDefinition column : x.getColumns()) { - String columnName = column.getName().toString(); - addColumn(table, columnName); - } - return false; - } - - @Override - public void endVisit(SQLAlterTableAddColumn x) { - - } -} diff --git a/src/main/java/org/durid/stat/TableStat.java b/src/main/java/org/durid/stat/TableStat.java deleted file mode 100644 index 50597752..00000000 --- a/src/main/java/org/durid/stat/TableStat.java +++ /dev/null @@ -1,454 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.stat; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class TableStat { - - int selectCount = 0; - int updateCount = 0; - int deleteCount = 0; - int insertCount = 0; - int dropCount = 0; - int mergeCount = 0; - int createCount = 0; - int alterCount = 0; - int createIndexCount = 0; - - public int getCreateIndexCount() { - return createIndexCount; - } - - public void incrementCreateIndexCount() { - createIndexCount++; - } - - public int getAlterCount() { - return alterCount; - } - - public void incrementAlterCount() { - this.alterCount++; - } - - public int getCreateCount() { - return createCount; - } - - public void incrementCreateCount() { - this.createCount++; - } - - public int getMergeCount() { - return mergeCount; - } - - public void incrementMergeCount() { - this.mergeCount++; - } - - public int getDropCount() { - return dropCount; - } - - public void incrementDropCount() { - dropCount++; - } - - public void setDropCount(int dropCount) { - this.dropCount = dropCount; - } - - public int getSelectCount() { - return selectCount; - } - - public void incrementSelectCount() { - selectCount++; - } - - public void setSelectCount(int selectCount) { - this.selectCount = selectCount; - } - - public int getUpdateCount() { - return updateCount; - } - - public void incrementUpdateCount() { - updateCount++; - } - - public void setUpdateCount(int updateCount) { - this.updateCount = updateCount; - } - - public int getDeleteCount() { - return deleteCount; - } - - public void incrementDeleteCount() { - this.deleteCount++; - } - - public void setDeleteCount(int deleteCount) { - this.deleteCount = deleteCount; - } - - public void incrementInsertCount() { - this.insertCount++; - } - - public int getInsertCount() { - return insertCount; - } - - public void setInsertCount(int insertCount) { - this.insertCount = insertCount; - } - - public String toString() { - StringBuilder buf = new StringBuilder(4); - if (mergeCount > 0) { - buf.append("Merge"); - } - if (insertCount > 0) { - buf.append("Insert"); - } - if (updateCount > 0) { - buf.append("Update"); - } - if (selectCount > 0) { - buf.append("Select"); - } - if (deleteCount > 0) { - buf.append("Delete"); - } - if (dropCount > 0) { - buf.append("Drop"); - } - if (createCount > 0) { - buf.append("Create"); - } - if (alterCount > 0) { - buf.append("Alter"); - } - if (createIndexCount > 0) { - buf.append("CreateIndex"); - } - - return buf.toString(); - } - - public static class Name { - - private String name; - - public Name(String name){ - this.name = name; - } - - public String getName() { - return this.name; - } - - public int hashCode() { - return this.name.toLowerCase().hashCode(); - } - - public boolean equals(Object o) { - if (!(o instanceof Name)) { - return false; - } - - Name other = (Name) o; - - return this.name.equalsIgnoreCase(other.name); - } - - public String toString() { - return this.name; - } - } - - public static class Relationship { - - private Column left; - private Column right; - private String operator; - - public Column getLeft() { - return left; - } - - public void setLeft(Column left) { - this.left = left; - } - - public Column getRight() { - return right; - } - - public void setRight(Column right) { - this.right = right; - } - - public String getOperator() { - return operator; - } - - public void setOperator(String operator) { - this.operator = operator; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((left == null) ? 0 : left.hashCode()); - result = prime * result + ((operator == null) ? 0 : operator.hashCode()); - result = prime * result + ((right == null) ? 0 : right.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - Relationship other = (Relationship) obj; - if (left == null) { - if (other.left != null) { - return false; - } - } else if (!left.equals(other.left)) { - return false; - } - if (operator == null) { - if (other.operator != null) { - return false; - } - } else if (!operator.equals(other.operator)) { - return false; - } - if (right == null) { - if (other.right != null) { - return false; - } - } else if (!right.equals(other.right)) { - return false; - } - return true; - } - - @Override - public String toString() { - return left + " " + operator + " " + right; - } - - } - - public static class Condition { - - private Column column; - private String operator; - - private List values = new ArrayList(); - - public Column getColumn() { - return column; - } - - public void setColumn(Column column) { - this.column = column; - } - - public String getOperator() { - return operator; - } - - public void setOperator(String operator) { - this.operator = operator; - } - - public List getValues() { - return values; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((column == null) ? 0 : column.hashCode()); - result = prime * result + ((operator == null) ? 0 : operator.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - Condition other = (Condition) obj; - if (column == null) { - if (other.column != null) { - return false; - } - } else if (!column.equals(other.column)) { - return false; - } - if (operator == null) { - if (other.operator != null) { - return false; - } - } else if (!operator.equals(other.operator)) { - return false; - } - return true; - } - - public String toString() { - StringBuffer buf = new StringBuffer(); - buf.append(this.column.toString()); - buf.append(' '); - buf.append(this.operator); - - if (values.size() == 1) { - buf.append(' '); - buf.append(String.valueOf(this.values.get(0))); - } else if (values.size() > 0) { - buf.append(" ("); - for (int i = 0; i < values.size(); ++i) { - if (i != 0) { - buf.append(", "); - } - buf.append(String.valueOf(values.get(i))); - } - buf.append(")"); - } - - return buf.toString(); - } - } - - public static class Column { - - private String table; - private String name; - - private Map attributes = new HashMap(); - - public Column(){ - - } - - public Column(String table, String name){ - this.table = table; - this.name = name; - } - - public String getTable() { - return table; - } - - public void setTable(String table) { - this.table = table; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Map getAttributes() { - return attributes; - } - - public void setAttributes(Map attributes) { - this.attributes = attributes; - } - - public int hashCode() { - int tableHashCode = table != null ? table.toLowerCase().hashCode() : 0; - int nameHashCode = name != null ? name.toLowerCase().hashCode() : 0; - - return tableHashCode + nameHashCode; - } - - public String toString() { - if (table != null) { - return table + "." + name; - } - - return name; - } - - public boolean equals(Object obj) { - Column column = (Column) obj; - - if (table == null) { - if (column.getTable() != null) { - return false; - } - } else { - if (!table.equalsIgnoreCase(column.getTable())) { - return false; - } - } - - if (name == null) { - if (column.getName() != null) { - return false; - } - } else { - if (!name.equalsIgnoreCase(column.getName())) { - return false; - } - } - - return true; - } - } - - public static enum Mode { - Insert(1), Update(2), Delete(4), Select(8), Merge(16), Truncate(32); - - public final int mark; - - private Mode(int mark){ - this.mark = mark; - } - } -} diff --git a/src/main/java/org/durid/support/logging/Jdk14LoggingImpl.java b/src/main/java/org/durid/support/logging/Jdk14LoggingImpl.java deleted file mode 100644 index a82cb30f..00000000 --- a/src/main/java/org/durid/support/logging/Jdk14LoggingImpl.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.support.logging; - -import java.util.logging.Level; -import java.util.logging.Logger; - -public class Jdk14LoggingImpl implements Log { - - private Logger log; - - private int errorCount; - private int warnCount; - private int infoCount; - - private String clazzName; - - public Jdk14LoggingImpl(Class clazz){ - clazzName = clazz.getName(); - log = Logger.getLogger(clazzName); - } - - public boolean isDebugEnabled() { - return log.isLoggable(Level.FINE); - } - - public void error(String s, Throwable e) { - log.logp(Level.SEVERE,clazzName, Thread.currentThread().getStackTrace()[1].getMethodName(), s, e); - errorCount++; - } - - public void error(String s) { - log.logp(Level.SEVERE,clazzName, Thread.currentThread().getStackTrace()[1].getMethodName(), s); - errorCount++; - } - - public void debug(String s) { - log.logp(Level.FINE, clazzName, Thread.currentThread().getStackTrace()[1].getMethodName(), s); - } - - public void debug(String s, Throwable e) { - log.logp(Level.FINE, clazzName, Thread.currentThread().getStackTrace()[1].getMethodName(), s, e); - } - - public void warn(String s) { - log.logp(Level.WARNING, clazzName, Thread.currentThread().getStackTrace()[1].getMethodName(), s); - warnCount++; - } - - @Override - public void warn(String s, Throwable e) { - log.logp(Level.WARNING, clazzName, Thread.currentThread().getStackTrace()[1].getMethodName(), s, e); - warnCount++; - } - - @Override - public int getWarnCount() { - return warnCount; - } - - public int getErrorCount() { - return errorCount; - } - - @Override - public void resetStat() { - errorCount = 0; - warnCount = 0; - infoCount = 0; - } - - @Override - public boolean isInfoEnabled() { - return log.isLoggable(Level.INFO); - } - - @Override - public void info(String msg) { - log.logp(Level.INFO, clazzName, Thread.currentThread().getStackTrace()[1].getMethodName(), msg); - infoCount++; - } - - @Override - public int getInfoCount() { - return infoCount; - } - - @Override - public boolean isWarnEnabled() { - return log.isLoggable(Level.WARNING); - } -} diff --git a/src/main/java/org/durid/support/logging/Log.java b/src/main/java/org/durid/support/logging/Log.java deleted file mode 100644 index 0823e16d..00000000 --- a/src/main/java/org/durid/support/logging/Log.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.support.logging; - -public interface Log { - - boolean isDebugEnabled(); - - void error(String msg, Throwable e); - - void error(String msg); - - boolean isInfoEnabled(); - - void info(String msg); - - void debug(String msg); - - void debug(String msg, Throwable e); - - boolean isWarnEnabled(); - - void warn(String msg); - - void warn(String msg, Throwable e); - - int getErrorCount(); - - int getWarnCount(); - - int getInfoCount(); - - void resetStat(); -} diff --git a/src/main/java/org/durid/support/logging/Log4jImpl.java b/src/main/java/org/durid/support/logging/Log4jImpl.java deleted file mode 100644 index 5ac778f2..00000000 --- a/src/main/java/org/durid/support/logging/Log4jImpl.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.support.logging; - -import org.apache.log4j.Level; -import org.apache.log4j.Logger; - -public class Log4jImpl implements Log { - - private static final String callerFQCN = Log4jImpl.class.getName(); - - private Logger log; - - private int errorCount; - private int warnCount; - private int infoCount; - - public Log4jImpl(Class clazz){ - log = Logger.getLogger(clazz); - } - - public boolean isDebugEnabled() { - return log.isDebugEnabled(); - } - - public void error(String s, Throwable e) { - errorCount++; - log.log(callerFQCN, Level.ERROR, s, e); - } - - public void error(String s) { - errorCount++; - log.log(callerFQCN, Level.ERROR, s, null); - } - - public void debug(String s) { - log.log(callerFQCN, Level.DEBUG, s, null); - } - - public void debug(String s, Throwable e) { - log.log(callerFQCN, Level.DEBUG, s, e); - } - - public void warn(String s) { - log.log(callerFQCN, Level.WARN, s, null); - warnCount++; - } - - public void warn(String s, Throwable e) { - log.log(callerFQCN, Level.WARN, s, e); - warnCount++; - } - - public int getWarnCount() { - return warnCount; - } - - public int getErrorCount() { - return errorCount; - } - - public void resetStat() { - errorCount = 0; - warnCount = 0; - infoCount = 0; - } - - public boolean isInfoEnabled() { - return log.isInfoEnabled(); - } - - public void info(String msg) { - infoCount++; - log.log(callerFQCN, Level.INFO, msg, null); - } - - public boolean isWarnEnabled() { - return log.isEnabledFor(Level.WARN); - } - - public int getInfoCount() { - return infoCount; - } - - public String toString() { - return log.toString(); - } -} diff --git a/src/main/java/org/durid/support/logging/LogFactory.java b/src/main/java/org/durid/support/logging/LogFactory.java deleted file mode 100644 index 42ac58e1..00000000 --- a/src/main/java/org/durid/support/logging/LogFactory.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.support.logging; - -import java.lang.reflect.Constructor; - -@SuppressWarnings("rawtypes") -public class LogFactory { - - private static Constructor logConstructor; - - static { - // TODO add slf4j logging - - //优先选择log4j,而非Apache Common Logging. 因为后者无法设置真实Log调用者的信息 - tryImplementation("org.apache.log4j.Logger", "org.durid.support.logging.Log4jImpl"); - tryImplementation("java.util.logging.Logger", "org.durid.support.logging.Jdk14LoggingImpl"); - tryImplementation("org.apache.commons.logging.LogFactory", - "org.durid.support.logging.JakartaCommonsLoggingImpl"); - tryImplementation("java.lang.Object", "org.durid.support.logging.NoLoggingImpl"); - } - - @SuppressWarnings("unchecked") - private static void tryImplementation(String testClassName, String implClassName) { - if (logConstructor == null) { - try { - Resources.classForName(testClassName); - Class implClass = Resources.classForName(implClassName); - logConstructor = implClass.getConstructor(new Class[] { Class.class }); - } catch (Throwable t) { - } - } - } - - public static Log getLog(Class aClass) { - try { - return (Log) logConstructor.newInstance(new Object[] { aClass }); - } catch (Throwable t) { - throw new RuntimeException("Error creating logger for class " + aClass + ". Cause: " + t, t); - } - } - - /** - * This method will switch the logging implementation to Log4J if Log4J is available on the classpath. This is - * useful in situations where you want to use Log4J to log iBATIS activity but commons logging is on the classpath. - * Note that this method is only effective for log classes obtained after calling this method. If you intend to use - * this method you should call it before calling any other iBATIS method. - */ - @SuppressWarnings("unchecked") - public static synchronized void selectLog4JLogging() { - try { - Resources.classForName("org.apache.log4j.Logger"); - Class implClass = Resources.classForName("org.durid.support.logging.Log4jImpl"); - logConstructor = implClass.getConstructor(new Class[] { Class.class }); - } catch (Throwable t) { - } - } - - /** - * This method will switch the logging implementation to Java native logging if you are running in JRE 1.4 or above. - * This is useful in situations where you want to use Java native logging to log iBATIS activity but commons logging - * or Log4J is on the classpath. Note that this method is only effective for log classes obtained after calling this - * method. If you intend to use this method you should call it before calling any other iBATIS method. - */ - @SuppressWarnings("unchecked") - public static synchronized void selectJavaLogging() { - try { - Resources.classForName("java.util.logging.Logger"); - Class implClass = Resources.classForName("org.durid.support.logging.Jdk14LoggingImpl"); - logConstructor = implClass.getConstructor(new Class[] { Class.class }); - } catch (Throwable t) { - } - } -} diff --git a/src/main/java/org/durid/support/logging/NoLoggingImpl.java b/src/main/java/org/durid/support/logging/NoLoggingImpl.java deleted file mode 100644 index b30e2a49..00000000 --- a/src/main/java/org/durid/support/logging/NoLoggingImpl.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.support.logging; - -public class NoLoggingImpl implements Log { - - private int infoCount; - private int errorCount; - private int warnCount; - - public NoLoggingImpl(Class clazz){ - } - - public boolean isDebugEnabled() { - return false; - } - - public void error(String s, Throwable e) { - errorCount++; - } - - public void error(String s) { - errorCount++; - } - - public void debug(String s) { - } - - public void debug(String s, Throwable e) { - } - - public void warn(String s) { - warnCount++; - } - - @Override - public void warn(String s, Throwable e) { - warnCount++; - } - - public int getErrorCount() { - return errorCount; - } - - @Override - public int getWarnCount() { - return warnCount; - } - - @Override - public void resetStat() { - errorCount = 0; - warnCount = 0; - infoCount = 0; - } - - @Override - public boolean isInfoEnabled() { - return false; - } - - @Override - public void info(String s) { - infoCount++; - } - - @Override - public boolean isWarnEnabled() { - return false; - } - - public int getInfoCount() { - return infoCount; - } -} diff --git a/src/main/java/org/durid/support/logging/Resources.java b/src/main/java/org/durid/support/logging/Resources.java deleted file mode 100644 index 4bd4417f..00000000 --- a/src/main/java/org/durid/support/logging/Resources.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.support.logging; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.net.URL; -import java.net.URLConnection; -import java.nio.charset.Charset; -import java.util.Properties; - -/** - * A class to simplify access to resources through the classloader. - */ -public final class Resources extends Object { - - private static ClassLoader defaultClassLoader; - - /** - * Charset to use when calling getResourceAsReader. null means use the system default. - */ - private static Charset charset; - - private Resources(){ - } - - /** - * Returns the default classloader (may be null). - * - * @return The default classloader - */ - public static ClassLoader getDefaultClassLoader() { - return defaultClassLoader; - } - - /** - * Sets the default classloader - * - * @param defaultClassLoader - the new default ClassLoader - */ - public static void setDefaultClassLoader(ClassLoader defaultClassLoader) { - Resources.defaultClassLoader = defaultClassLoader; - } - - /** - * Returns the URL of the resource on the classpath - * - * @param resource The resource to find - * @return The resource - * @throws IOException If the resource cannot be found or read - */ - public static URL getResourceURL(String resource) throws IOException { - return getResourceURL(getClassLoader(), resource); - } - - /** - * Returns the URL of the resource on the classpath - * - * @param loader The classloader used to load the resource - * @param resource The resource to find - * @return The resource - * @throws IOException If the resource cannot be found or read - */ - public static URL getResourceURL(ClassLoader loader, String resource) throws IOException { - URL url = null; - - if (loader != null) { - url = loader.getResource(resource); - } - - if (url == null) { - url = ClassLoader.getSystemResource(resource); - } - - if (url == null) { - throw new IOException("Could not find resource " + resource); - } - - return url; - } - - /** - * Returns a resource on the classpath as a Stream object - * - * @param resource The resource to find - * @return The resource - * @throws IOException If the resource cannot be found or read - */ - public static InputStream getResourceAsStream(String resource) throws IOException { - return getResourceAsStream(getClassLoader(), resource); - } - - /** - * Returns a resource on the classpath as a Stream object - * - * @param loader The classloader used to load the resource - * @param resource The resource to find - * @return The resource - * @throws IOException If the resource cannot be found or read - */ - public static InputStream getResourceAsStream(ClassLoader loader, String resource) throws IOException { - InputStream in = null; - if (loader != null) { - in = loader.getResourceAsStream(resource); - } - if (in == null) { - in = ClassLoader.getSystemResourceAsStream(resource); - } - if (in == null) { - throw new IOException("Could not find resource " + resource); - } - return in; - } - - /** - * Returns a resource on the classpath as a Properties object - * - * @param resource The resource to find - * @return The resource - * @throws IOException If the resource cannot be found or read - */ - public static Properties getResourceAsProperties(String resource) throws IOException { - Properties props = new Properties(); - InputStream in = null; - String propfile = resource; - in = getResourceAsStream(propfile); - props.load(in); - in.close(); - return props; - } - - /** - * Returns a resource on the classpath as a Properties object - * - * @param loader The classloader used to load the resource - * @param resource The resource to find - * @return The resource - * @throws IOException If the resource cannot be found or read - */ - public static Properties getResourceAsProperties(ClassLoader loader, String resource) throws IOException { - Properties props = new Properties(); - InputStream in = null; - String propfile = resource; - in = getResourceAsStream(loader, propfile); - props.load(in); - in.close(); - return props; - } - - /** - * Returns a resource on the classpath as a Reader object - * - * @param resource The resource to find - * @return The resource - * @throws IOException If the resource cannot be found or read - */ - public static Reader getResourceAsReader(String resource) throws IOException { - Reader reader; - if (charset == null) { - reader = new InputStreamReader(getResourceAsStream(resource)); - } else { - reader = new InputStreamReader(getResourceAsStream(resource), charset); - } - - return reader; - } - - /** - * Returns a resource on the classpath as a Reader object - * - * @param loader The classloader used to load the resource - * @param resource The resource to find - * @return The resource - * @throws IOException If the resource cannot be found or read - */ - public static Reader getResourceAsReader(ClassLoader loader, String resource) throws IOException { - Reader reader; - if (charset == null) { - reader = new InputStreamReader(getResourceAsStream(loader, resource)); - } else { - reader = new InputStreamReader(getResourceAsStream(loader, resource), charset); - } - - return reader; - } - - /** - * Returns a resource on the classpath as a File object - * - * @param resource The resource to find - * @return The resource - * @throws IOException If the resource cannot be found or read - */ - public static File getResourceAsFile(String resource) throws IOException { - return new File(getResourceURL(resource).getFile()); - } - - /** - * Returns a resource on the classpath as a File object - * - * @param loader - the classloader used to load the resource - * @param resource - the resource to find - * @return The resource - * @throws IOException If the resource cannot be found or read - */ - public static File getResourceAsFile(ClassLoader loader, String resource) throws IOException { - return new File(getResourceURL(loader, resource).getFile()); - } - - /** - * Gets a URL as an input stream - * - * @param urlString - the URL to get - * @return An input stream with the data from the URL - * @throws IOException If the resource cannot be found or read - */ - public static InputStream getUrlAsStream(String urlString) throws IOException { - URL url = new URL(urlString); - URLConnection conn = url.openConnection(); - return conn.getInputStream(); - } - - /** - * Gets a URL as a Reader - * - * @param urlString - the URL to get - * @return A Reader with the data from the URL - * @throws IOException If the resource cannot be found or read - */ - public static Reader getUrlAsReader(String urlString) throws IOException { - return new InputStreamReader(getUrlAsStream(urlString)); - } - - /** - * Gets a URL as a Properties object - * - * @param urlString - the URL to get - * @return A Properties object with the data from the URL - * @throws IOException If the resource cannot be found or read - */ - public static Properties getUrlAsProperties(String urlString) throws IOException { - Properties props = new Properties(); - InputStream in = null; - String propfile = urlString; - in = getUrlAsStream(propfile); - props.load(in); - in.close(); - return props; - } - - /** - * Loads a class - * - * @param className - the class to load - * @return The loaded class - * @throws ClassNotFoundException If the class cannot be found (duh!) - */ - public static Class classForName(String className) throws ClassNotFoundException { - Class clazz = null; - try { - clazz = getClassLoader().loadClass(className); - } catch (Exception e) { - // Ignore. Failsafe below. - } - if (clazz == null) { - clazz = Class.forName(className); - } - return clazz; - } - - /** - * Creates an instance of a class - * - * @param className - the class to create - * @return An instance of the class - * @throws ClassNotFoundException If the class cannot be found (duh!) - * @throws InstantiationException If the class cannot be instantiaed - * @throws IllegalAccessException If the class is not public, or other access problems arise - */ - public static Object instantiate(String className) throws ClassNotFoundException, InstantiationException, - IllegalAccessException { - return instantiate(classForName(className)); - } - - /** - * Creates an instance of a class - * - * @param clazz - the class to create - * @return An instance of the class - * @throws InstantiationException If the class cannot be instantiaed - * @throws IllegalAccessException If the class is not public, or other access problems arise - */ - public static Object instantiate(Class clazz) throws InstantiationException, IllegalAccessException { - return clazz.newInstance(); - } - - private static ClassLoader getClassLoader() { - if (defaultClassLoader != null) { - return defaultClassLoader; - } else { - return Thread.currentThread().getContextClassLoader(); - } - } - - public static Charset getCharset() { - return charset; - } - - /** - * Use this method to set the Charset to be used when calling the getResourceAsReader methods. This will allow - * iBATIS to function properly when the system default encoding doesn't deal well with unicode (IBATIS-340, - * IBATIS-349) - * - * @param charset - */ - public static void setCharset(Charset charset) { - Resources.charset = charset; - } - -} diff --git a/src/main/java/org/durid/util/HexBin.java b/src/main/java/org/durid/util/HexBin.java deleted file mode 100644 index bc3632c6..00000000 --- a/src/main/java/org/durid/util/HexBin.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.util; - - -/** - * format validation - * - * This class encodes/decodes hexadecimal data - * - * @xerces.internal - * - * @author Jeffrey Rodriguez - * @version $Id: HexBin.java,v 1.4 2007/07/19 04:38:32 ofung Exp $ - */ -public final class HexBin { - static private final int BASELENGTH = 128; - static private final int LOOKUPLENGTH = 16; - static final private byte [] hexNumberTable = new byte[BASELENGTH]; - static final private char [] lookUpHexAlphabet = new char[LOOKUPLENGTH]; - - - static { - for (int i = 0; i < BASELENGTH; i++ ) { - hexNumberTable[i] = -1; - } - for ( int i = '9'; i >= '0'; i--) { - hexNumberTable[i] = (byte) (i-'0'); - } - for ( int i = 'F'; i>= 'A'; i--) { - hexNumberTable[i] = (byte) ( i-'A' + 10 ); - } - for ( int i = 'f'; i>= 'a'; i--) { - hexNumberTable[i] = (byte) ( i-'a' + 10 ); - } - - for(int i = 0; i<10; i++ ) { - lookUpHexAlphabet[i] = (char)('0'+i); - } - for(int i = 10; i<=15; i++ ) { - lookUpHexAlphabet[i] = (char)('A'+i -10); - } - } - - /** - * Encode a byte array to hex string - * - * @param binaryData array of byte to encode - * @return return encoded string - */ - static public String encode(byte[] binaryData) { - if (binaryData == null) { - return null; - } - - int lengthData = binaryData.length; - int lengthEncode = lengthData * 2; - char[] encodedData = new char[lengthEncode]; - int temp; - for (int i = 0; i < lengthData; i++) { - temp = binaryData[i]; - if (temp < 0) { - temp += 256; - } - encodedData[i*2] = lookUpHexAlphabet[temp >> 4]; - encodedData[i*2+1] = lookUpHexAlphabet[temp & 0xf]; - } - return new String(encodedData); - } - - /** - * Decode hex string to a byte array - * - * @param encoded encoded string - * @return return array of byte to encode - */ - static public byte[] decode(String encoded) { - if (encoded == null) { - return null; - } - - int lengthData = encoded.length(); - if (lengthData % 2 != 0) { - return null; - } - - char[] binaryData = encoded.toCharArray(); - int lengthDecode = lengthData / 2; - byte[] decodedData = new byte[lengthDecode]; - byte temp1, temp2; - char tempChar; - for( int i = 0; i - */ -public final class JdbcUtils implements JdbcConstants { - - - - private final static Log LOG = LogFactory.getLog(JdbcUtils.class); - - private static final Properties driverUrlMapping = new Properties(); - - static { - try { - for (Enumeration e = Thread.currentThread().getContextClassLoader().getResources("META-INF/druid-driver.properties"); e.hasMoreElements();) { - URL url = e.nextElement(); - - Properties property = new Properties(); - - InputStream is = null; - try { - is = url.openStream(); - property.load(is); - } finally { - JdbcUtils.close(is); - } - - driverUrlMapping.putAll(property); - } - } catch (Exception e) { - LOG.error("load druid-driver.properties error", e); - } - } - - public final static void close(Connection x) { - if (x != null) { - try { - x.close(); - } catch (Exception e) { - LOG.error("close connection error", e); - } - } - } - - public final static void close(Statement x) { - if (x != null) { - try { - x.close(); - } catch (Exception e) { - LOG.error("close statement error", e); - } - } - } - - public final static void close(ResultSet x) { - if (x != null) { - try { - x.close(); - } catch (Exception e) { - LOG.error("close resultset error", e); - } - } - } - - public final static void close(Closeable x) { - if (x != null) { - try { - x.close(); - } catch (Exception e) { - LOG.error("close error", e); - } - } - } - - public final static void printResultSet(ResultSet rs) throws SQLException { - printResultSet(rs, System.out); - } - - public final static void printResultSet(ResultSet rs, PrintStream out) throws SQLException { - ResultSetMetaData metadata = rs.getMetaData(); - int columnCount = metadata.getColumnCount(); - for (int columnIndex = 1; columnIndex <= columnCount; ++columnIndex) { - if (columnIndex != 1) { - out.print('\t'); - } - out.print(metadata.getColumnName(columnIndex)); - } - - out.println(); - - while (rs.next()) { - - for (int columnIndex = 1; columnIndex <= columnCount; ++columnIndex) { - if (columnIndex != 1) { - out.print('\t'); - } - - int type = metadata.getColumnType(columnIndex); - - if (type == Types.VARCHAR || type == Types.CHAR || type == Types.NVARCHAR || type == Types.NCHAR) { - out.print(rs.getString(columnIndex)); - } else if (type == Types.DATE) { - Date date = rs.getDate(columnIndex); - if (rs.wasNull()) { - out.print("null"); - } else { - out.print(date.toString()); - } - } else if (type == Types.BIT) { - boolean value = rs.getBoolean(columnIndex); - if (rs.wasNull()) { - out.print("null"); - } else { - out.print(Boolean.toString(value)); - } - } else if (type == Types.BOOLEAN) { - boolean value = rs.getBoolean(columnIndex); - if (rs.wasNull()) { - out.print("null"); - } else { - out.print(Boolean.toString(value)); - } - } else if (type == Types.TINYINT) { - byte value = rs.getByte(columnIndex); - if (rs.wasNull()) { - out.print("null"); - } else { - out.print(Byte.toString(value)); - } - } else if (type == Types.SMALLINT) { - short value = rs.getShort(columnIndex); - if (rs.wasNull()) { - out.print("null"); - } else { - out.print(Short.toString(value)); - } - } else if (type == Types.INTEGER) { - int value = rs.getInt(columnIndex); - if (rs.wasNull()) { - out.print("null"); - } else { - out.print(Integer.toString(value)); - } - } else if (type == Types.BIGINT) { - long value = rs.getLong(columnIndex); - if (rs.wasNull()) { - out.print("null"); - } else { - out.print(Long.toString(value)); - } - } else if (type == Types.TIMESTAMP) { - out.print(String.valueOf(rs.getTimestamp(columnIndex))); - } else if (type == Types.DECIMAL) { - out.print(String.valueOf(rs.getBigDecimal(columnIndex))); - } else if (type == Types.CLOB) { - out.print(String.valueOf(rs.getString(columnIndex))); - } else if (type == Types.JAVA_OBJECT) { - Object objec = rs.getObject(columnIndex); - - if (rs.wasNull()) { - out.print("null"); - } else { - out.print(String.valueOf(objec)); - } - } else if (type == Types.LONGVARCHAR) { - Object objec = rs.getString(columnIndex); - - if (rs.wasNull()) { - out.print("null"); - } else { - out.print(String.valueOf(objec)); - } - } else { - Object objec = rs.getObject(columnIndex); - - if (rs.wasNull()) { - out.print("null"); - } else { - out.print(String.valueOf(objec)); - } - } - } - out.println(); - } - } - - public static String getTypeName(int sqlType) { - switch (sqlType) { - case Types.ARRAY: - return "ARRAY"; - - case Types.BIGINT: - return "BIGINT"; - - case Types.BINARY: - return "BINARY"; - - case Types.BIT: - return "BIT"; - - case Types.BLOB: - return "BLOB"; - - case Types.BOOLEAN: - return "BOOLEAN"; - - case Types.CHAR: - return "CHAR"; - - case Types.CLOB: - return "CLOB"; - - case Types.DATALINK: - return "DATALINK"; - - case Types.DATE: - return "DATE"; - - case Types.DECIMAL: - return "DECIMAL"; - - case Types.DISTINCT: - return "DISTINCT"; - - case Types.DOUBLE: - return "DOUBLE"; - - case Types.FLOAT: - return "FLOAT"; - - case Types.INTEGER: - return "INTEGER"; - - case Types.JAVA_OBJECT: - return "JAVA_OBJECT"; - - case Types.LONGNVARCHAR: - return "LONGNVARCHAR"; - - case Types.LONGVARBINARY: - return "LONGVARBINARY"; - - case Types.NCHAR: - return "NCHAR"; - - case Types.NCLOB: - return "NCLOB"; - - case Types.NULL: - return "NULL"; - - case Types.NUMERIC: - return "NUMERIC"; - - case Types.NVARCHAR: - return "NVARCHAR"; - - case Types.REAL: - return "REAL"; - - case Types.REF: - return "REF"; - - case Types.ROWID: - return "ROWID"; - - case Types.SMALLINT: - return "SMALLINT"; - - case Types.SQLXML: - return "SQLXML"; - - case Types.STRUCT: - return "STRUCT"; - - case Types.TIME: - return "TIME"; - - case Types.TIMESTAMP: - return "TIMESTAMP"; - - case Types.TINYINT: - return "TINYINT"; - - case Types.VARBINARY: - return "VARBINARY"; - - case Types.VARCHAR: - return "VARCHAR"; - - default: - return "OTHER"; - - } - } - - public static String getDriverClassName(String rawUrl) throws SQLException { - if (rawUrl.startsWith("jdbc:derby:")) { - return "org.apache.derby.jdbc.EmbeddedDriver"; - } else if (rawUrl.startsWith("jdbc:mysql:")) { - return MYSQL_DRIVER; - } else if (rawUrl.startsWith("jdbc:oracle:")) { - return ORACLE_DRIVER; - } else if (rawUrl.startsWith("jdbc:alibaba:oracle:")) { - return ALI_ORACLE_DRIVER; - } else if (rawUrl.startsWith("jdbc:microsoft:")) { - return "com.microsoft.jdbc.sqlserver.SQLServerDriver"; - } else if (rawUrl.startsWith("jdbc:sqlserver:")) { - return "com.microsoft.sqlserver.jdbc.SQLServerDriver"; - } else if (rawUrl.startsWith("jdbc:sybase:Tds:")) { - return "com.sybase.jdbc2.jdbc.SybDriver"; - } else if (rawUrl.startsWith("jdbc:jtds:")) { - return "net.sourceforge.jtds.jdbc.Driver"; - } else if (rawUrl.startsWith("jdbc:fake:") || rawUrl.startsWith("jdbc:mock:")) { - return "com.alibaba.druid.mock.MockDriver"; - } else if (rawUrl.startsWith("jdbc:postgresql:")) { - return "org.postgresql.Driver"; - } else if (rawUrl.startsWith("jdbc:hsqldb:")) { - return "org.hsqldb.jdbcDriver"; - } else if (rawUrl.startsWith("jdbc:db2:")) { - return DB2_DRIVER; - } else if (rawUrl.startsWith("jdbc:sqlite:")) { - return "org.sqlite.JDBC"; - } else if (rawUrl.startsWith("jdbc:ingres:")) { - return "com.ingres.jdbc.IngresDriver"; - } else if (rawUrl.startsWith("jdbc:h2:")) { - return H2_DRIVER; - } else if (rawUrl.startsWith("jdbc:mckoi:")) { - return "com.mckoi.JDBCDriver"; - } else if (rawUrl.startsWith("jdbc:cloudscape:")) { - return "COM.cloudscape.core.JDBCDriver"; - } else if (rawUrl.startsWith("jdbc:informix-sqli:")) { - return "com.informix.jdbc.IfxDriver"; - } else if (rawUrl.startsWith("jdbc:timesten:")) { - return "com.timesten.jdbc.TimesTenDriver"; - } else if (rawUrl.startsWith("jdbc:as400:")) { - return "com.ibm.as400.access.AS400JDBCDriver"; - } else if (rawUrl.startsWith("jdbc:sapdb:")) { - return "com.sap.dbtech.jdbc.DriverSapDB"; - } else if (rawUrl.startsWith("jdbc:JSQLConnect:")) { - return "com.jnetdirect.jsql.JSQLDriver"; - } else if (rawUrl.startsWith("jdbc:JTurbo:")) { - return "com.newatlanta.jturbo.driver.Driver"; - } else if (rawUrl.startsWith("jdbc:firebirdsql:")) { - return "org.firebirdsql.jdbc.FBDriver"; - } else if (rawUrl.startsWith("jdbc:interbase:")) { - return "interbase.interclient.Driver"; - } else if (rawUrl.startsWith("jdbc:pointbase:")) { - return "com.pointbase.jdbc.jdbcUniversalDriver"; - } else if (rawUrl.startsWith("jdbc:edbc:")) { - return "ca.edbc.jdbc.EdbcDriver"; - } else if (rawUrl.startsWith("jdbc:mimer:multi1:")) { - return "com.mimer.jdbc.Driver"; - } else { - throw new SQLException("unkow jdbc driver : " + rawUrl); - } - } - - public static String getDbType(String rawUrl, String driverClassName) { - if (rawUrl == null) { - return null; - } - - if (rawUrl.startsWith("jdbc:derby:")) { - return DERBY; - } else if (rawUrl.startsWith("jdbc:mysql:")) { - return MYSQL; - } else if (rawUrl.startsWith("jdbc:oracle:")) { - return ORACLE; - } else if (rawUrl.startsWith("jdbc:alibaba:oracle:")) { - return ALI_ORACLE; - } else if (rawUrl.startsWith("jdbc:microsoft:")) { - return SQL_SERVER; - } else if (rawUrl.startsWith("jdbc:sybase:Tds:")) { - return SYBASE; - } else if (rawUrl.startsWith("jdbc:jtds:")) { - return JTDS; - } else if (rawUrl.startsWith("jdbc:fake:") || rawUrl.startsWith("jdbc:mock:")) { - return MOCK; - } else if (rawUrl.startsWith("jdbc:postgresql:")) { - return POSTGRESQL; - } else if (rawUrl.startsWith("jdbc:hsqldb:")) { - return HSQL; - } else if (rawUrl.startsWith("jdbc:db2:")) { - return DB2; - } else if (rawUrl.startsWith("jdbc:sqlite:")) { - return "sqlite"; - } else if (rawUrl.startsWith("jdbc:ingres:")) { - return "ingres"; - } else if (rawUrl.startsWith("jdbc:h2:")) { - return H2; - } else if (rawUrl.startsWith("jdbc:mckoi:")) { - return "mckoi"; - } else if (rawUrl.startsWith("jdbc:cloudscape:")) { - return "cloudscape"; - } else if (rawUrl.startsWith("jdbc:informix-sqli:")) { - return "informix"; - } else if (rawUrl.startsWith("jdbc:timesten:")) { - return "timesten"; - } else if (rawUrl.startsWith("jdbc:as400:")) { - return "as400"; - } else if (rawUrl.startsWith("jdbc:sapdb:")) { - return "sapdb"; - } else if (rawUrl.startsWith("jdbc:JSQLConnect:")) { - return "JSQLConnect"; - } else if (rawUrl.startsWith("jdbc:JTurbo:")) { - return "JTurbo"; - } else if (rawUrl.startsWith("jdbc:firebirdsql:")) { - return "firebirdsql"; - } else if (rawUrl.startsWith("jdbc:interbase:")) { - return "interbase"; - } else if (rawUrl.startsWith("jdbc:pointbase:")) { - return "pointbase"; - } else if (rawUrl.startsWith("jdbc:edbc:")) { - return "edbc"; - } else if (rawUrl.startsWith("jdbc:mimer:multi1:")) { - return "mimer"; - } else { - return null; - } - } - - public static Driver createDriver(String driverClassName) throws SQLException { - return createDriver(null, driverClassName); - } - - public static Driver createDriver(ClassLoader classLoader, String driverClassName) throws SQLException { - if (classLoader != null) { - try { - return (Driver) classLoader.loadClass(driverClassName).newInstance(); - } catch (IllegalAccessException e) { - throw new SQLException(e.getMessage(), e); - } catch (InstantiationException e) { - throw new SQLException(e.getMessage(), e); - } catch (ClassNotFoundException e) { - throw new SQLException(e.getMessage(), e); - } - } - - try { - return (Driver) Class.forName(driverClassName).newInstance(); - } catch (IllegalAccessException e) { - throw new SQLException(e.getMessage(), e); - } catch (InstantiationException e) { - throw new SQLException(e.getMessage(), e); - } catch (ClassNotFoundException e) { - // skip - } - - try { - return (Driver) Thread.currentThread().getContextClassLoader().loadClass(driverClassName).newInstance(); - } catch (IllegalAccessException e) { - throw new SQLException(e.getMessage(), e); - } catch (InstantiationException e) { - throw new SQLException(e.getMessage(), e); - } catch (ClassNotFoundException e) { - throw new SQLException(e.getMessage(), e); - } - } - - public static int executeUpdate(DataSource dataSource, String sql, Object... parameters) throws SQLException { - return executeUpdate(dataSource, sql, Arrays.asList(parameters)); - } - - public static int executeUpdate(DataSource dataSource, String sql, List parameters) throws SQLException { - Connection conn = null; - try { - conn = dataSource.getConnection(); - return executeUpdate(conn, sql, parameters); - } finally { - close(conn); - } - } - - public static int executeUpdate(Connection conn, String sql, List parameters) throws SQLException { - PreparedStatement stmt = null; - - int updateCount; - try { - stmt = conn.prepareStatement(sql); - - setParameters(stmt, parameters); - - updateCount = stmt.executeUpdate(); - } finally { - JdbcUtils.close(stmt); - } - - return updateCount; - } - - public static void execute(DataSource dataSource, String sql, Object... parameters) throws SQLException { - execute(dataSource, sql, Arrays.asList(parameters)); - } - - public static void execute(DataSource dataSource, String sql, List parameters) throws SQLException { - Connection conn = null; - try { - conn = dataSource.getConnection(); - execute(conn, sql, parameters); - } finally { - close(conn); - } - } - - public static void execute(Connection conn, String sql, List parameters) throws SQLException { - PreparedStatement stmt = null; - - try { - stmt = conn.prepareStatement(sql); - - setParameters(stmt, parameters); - - stmt.executeUpdate(); - } finally { - JdbcUtils.close(stmt); - } - } - - public static List> executeQuery(DataSource dataSource, String sql, Object... parameters) - throws SQLException { - return executeQuery(dataSource, sql, Arrays.asList(parameters)); - } - - public static List> executeQuery(DataSource dataSource, String sql, List parameters) - throws SQLException { - Connection conn = null; - try { - conn = dataSource.getConnection(); - return executeQuery(conn, sql, parameters); - } finally { - close(conn); - } - } - - public static List> executeQuery(Connection conn, String sql, List parameters) - throws SQLException { - List> rows = new ArrayList>(); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = conn.prepareStatement(sql); - - setParameters(stmt, parameters); - - rs = stmt.executeQuery(); - - ResultSetMetaData rsMeta = rs.getMetaData(); - - while (rs.next()) { - Map row = new LinkedHashMap(); - - for (int i = 0, size = rsMeta.getColumnCount(); i < size; ++i) { - String columName = rsMeta.getColumnLabel(i + 1); - Object value = rs.getObject(i + 1); - row.put(columName, value); - } - - rows.add(row); - } - } finally { - JdbcUtils.close(rs); - JdbcUtils.close(stmt); - } - - return rows; - } - - private static void setParameters(PreparedStatement stmt, List parameters) throws SQLException { - for (int i = 0, size = parameters.size(); i < size; ++i) { - stmt.setObject(i + 1, parameters.get(i)); - } - } - - public static Class loadDriverClass(String className) { - Class clazz = null; - - if (className == null) { - return null; - } - - try { - clazz = Thread.currentThread().getContextClassLoader().loadClass(className); - } catch (ClassNotFoundException e) { - - } - - if (clazz != null) { - return clazz; - } - - try { - return Class.forName(className); - } catch (ClassNotFoundException e) { - return null; - } - } - - public static void insertToTable(DataSource dataSource, String tableName, Map data) - throws SQLException { - Connection conn = null; - try { - conn = dataSource.getConnection(); - insertToTable(conn, tableName, data); - } finally { - close(conn); - } - } - - public static void insertToTable(Connection conn, String tableName, Map data) throws SQLException { - String sql = makeInsertToTableSql(tableName, data.keySet()); - List parameters = new ArrayList(data.values()); - execute(conn, sql, parameters); - } - - public static String makeInsertToTableSql(String tableName, Collection names) { - StringBuilder sql = new StringBuilder() // - .append("insert into ") // - .append(tableName) // - .append("("); // - - int nameCount = 0; - for (String name : names) { - if (nameCount > 0) { - sql.append(","); - } - sql.append(name); - nameCount++; - } - sql.append(") values ("); - for (int i = 0; i < nameCount; ++i) { - if (i != 0) { - sql.append(","); - } - sql.append("?"); - } - sql.append(")"); - - return sql.toString(); - } -} diff --git a/src/main/java/org/nlpcn/es4sql/Util.java b/src/main/java/org/nlpcn/es4sql/Util.java index e40fce52..de4aa6ec 100644 --- a/src/main/java/org/nlpcn/es4sql/Util.java +++ b/src/main/java/org/nlpcn/es4sql/Util.java @@ -3,15 +3,12 @@ import java.util.List; import java.util.Map; +import com.alibaba.druid.sql.ast.expr.*; import org.nlpcn.es4sql.domain.KVValue; import org.nlpcn.es4sql.exception.SqlParseException; -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.expr.SQLAllColumnExpr; -import org.durid.sql.ast.expr.SQLCharExpr; -import org.durid.sql.ast.expr.SQLIdentifierExpr; -import org.durid.sql.ast.expr.SQLNumericLiteralExpr; -import org.durid.sql.ast.expr.SQLPropertyExpr; +import com.alibaba.druid.sql.ast.*; + public class Util { public static String joiner(List lists, String oper) { diff --git a/src/main/java/org/nlpcn/es4sql/domain/From.java b/src/main/java/org/nlpcn/es4sql/domain/From.java index f12742f2..6b8414bc 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/From.java +++ b/src/main/java/org/nlpcn/es4sql/domain/From.java @@ -16,6 +16,7 @@ public class From { * @param from The part after the FROM keyword. */ public From(String from) { + from.replaceAll("#","*"); String[] parts = from.split("/"); this.index = parts[0].trim(); if (parts.length == 2) { diff --git a/src/main/java/org/nlpcn/es4sql/domain/Paramer.java b/src/main/java/org/nlpcn/es4sql/domain/Paramer.java index 4a166d09..0fb1ddd4 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/Paramer.java +++ b/src/main/java/org/nlpcn/es4sql/domain/Paramer.java @@ -2,16 +2,17 @@ import java.util.List; +import com.alibaba.druid.sql.ast.SQLExpr; +import com.alibaba.druid.sql.ast.expr.SQLCharExpr; +import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr; +import com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.index.query.MatchQueryBuilder; import org.elasticsearch.index.query.QueryStringQueryBuilder; import org.elasticsearch.index.query.WildcardQueryBuilder; import org.nlpcn.es4sql.exception.SqlParseException; -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.expr.SQLCharExpr; -import org.durid.sql.ast.expr.SQLMethodInvokeExpr; -import org.durid.sql.ast.expr.SQLNumericLiteralExpr; + public class Paramer { public String analysis; diff --git a/src/main/java/org/nlpcn/es4sql/parse/ElasticLexer.java b/src/main/java/org/nlpcn/es4sql/parse/ElasticLexer.java new file mode 100644 index 00000000..a8f8c41c --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/parse/ElasticLexer.java @@ -0,0 +1,94 @@ +package org.nlpcn.es4sql.parse; + +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlLexer; +import com.alibaba.druid.sql.parser.Lexer; +import com.alibaba.druid.sql.parser.ParserException; +import com.alibaba.druid.sql.parser.Token; + +import static com.alibaba.druid.sql.parser.CharTypes.isFirstIdentifierChar; +import static com.alibaba.druid.sql.parser.CharTypes.isIdentifierChar; +import static com.alibaba.druid.sql.parser.LayoutCharacters.EOI; + +/** + * Created by Eliran on 18/8/2015. + */ +public class ElasticLexer extends MySqlLexer { + public ElasticLexer(String input) { + super(input); + } + + + public ElasticLexer(char[] input, int inputLength, boolean skipComment) { + super(input, inputLength, skipComment); + } + + public void scanIdentifier() { + final char first = ch; + + if (ch == '`') { + + mark = pos; + bufPos = 1; + char ch; + for (;;) { + ch = charAt(++pos); + + if (ch == '`') { + bufPos++; + ch = charAt(++pos); + break; + } else if (ch == EOI) { + throw new ParserException("illegal identifier"); + } + + bufPos++; + continue; + } + + this.ch = charAt(pos); + + stringVal = subString(mark, bufPos); + Token tok = keywods.getKeyword(stringVal); + if (tok != null) { + token = tok; + } else { + token = Token.IDENTIFIER; + } + } else { + + final boolean firstFlag = isFirstIdentifierChar(first); + if (!firstFlag) { + throw new ParserException("illegal identifier"); + } + + mark = pos; + bufPos = 1; + char ch; + for (;;) { + ch = charAt(++pos); + + if (!isElasticIdentifierChar(ch)) { + break; + } + + bufPos++; + continue; + } + + this.ch = charAt(pos); + + stringVal = addSymbol(); + Token tok = keywods.getKeyword(stringVal); + if (tok != null) { + token = tok; + } else { + token = Token.IDENTIFIER; + } + } + } + + + private boolean isElasticIdentifierChar(char ch) { + return ch == '*' || ch == '-' || isIdentifierChar(ch); + } +} diff --git a/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java b/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java new file mode 100644 index 00000000..7156d62e --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java @@ -0,0 +1,19 @@ +package org.nlpcn.es4sql.parse; + +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlExprParser; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlLexer; +import com.alibaba.druid.sql.parser.Lexer; + +/** + * Created by Eliran on 18/8/2015. + */ +public class ElasticSqlExprParser extends MySqlExprParser { + public ElasticSqlExprParser(Lexer lexer) { + super(lexer); + } + + public ElasticSqlExprParser(String sql) { + this(new ElasticLexer(sql)); + this.lexer.nextToken(); + } +} diff --git a/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java b/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java index 5681a003..1d766ccf 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java +++ b/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java @@ -3,21 +3,14 @@ import java.util.LinkedList; import java.util.List; +import com.alibaba.druid.sql.ast.expr.*; +import com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey; import org.nlpcn.es4sql.Util; import org.nlpcn.es4sql.domain.Field; import org.nlpcn.es4sql.domain.KVValue; import org.nlpcn.es4sql.domain.MethodField; import org.nlpcn.es4sql.exception.SqlParseException; -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.expr.SQLAggregateExpr; -import org.durid.sql.ast.expr.SQLAggregateExpr.Option; -import org.durid.sql.ast.expr.SQLAllColumnExpr; -import org.durid.sql.ast.expr.SQLBinaryOpExpr; -import org.durid.sql.ast.expr.SQLIdentifierExpr; -import org.durid.sql.ast.expr.SQLMethodInvokeExpr; -import org.durid.sql.ast.expr.SQLPropertyExpr; -import org.durid.sql.ast.expr.SQLQueryExpr; - +import com.alibaba.druid.sql.ast.*; /** * 一些具有参数的一般在 select 函数.或者group by 函数 * @@ -44,7 +37,7 @@ public static Field makeField(SQLExpr expr, String alias) throws SqlParseExcepti return null; } - private static MethodField makeMethodField(String name, List arguments, Option option, String alias) throws SqlParseException { + private static MethodField makeMethodField(String name, List arguments, SQLAggregateOption option, String alias) throws SqlParseException { List paramers = new LinkedList<>(); for (SQLExpr object : arguments) { if (object instanceof SQLBinaryOpExpr) { diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index 88f0e4c2..3766dfe3 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -1,18 +1,20 @@ package org.nlpcn.es4sql.parse; import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; -import org.durid.sql.ast.expr.*; -import org.durid.sql.ast.statement.*; +import com.alibaba.druid.sql.ast.expr.*; +import com.alibaba.druid.sql.ast.statement.*; +import com.alibaba.druid.sql.ast.*; +import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlSelectGroupByExpr; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; +import com.alibaba.druid.sql.parser.SQLStatementParser; + + import org.nlpcn.es4sql.domain.*; import org.nlpcn.es4sql.domain.Where.CONN; import org.nlpcn.es4sql.exception.SqlParseException; -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLOrderBy; -import org.durid.sql.ast.SQLOrderingSpecification; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; -import org.durid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit; import org.nlpcn.es4sql.spatial.SpatialParamsFactory; /** @@ -76,8 +78,8 @@ private boolean isCond(SQLBinaryOpExpr expr) { private void parseWhere(SQLExpr expr, Where where) throws SqlParseException { if (expr instanceof SQLBinaryOpExpr && !isCond((SQLBinaryOpExpr) expr)) { SQLBinaryOpExpr bExpr = (SQLBinaryOpExpr) expr; - routeCond(bExpr, bExpr.left, where); - routeCond(bExpr, bExpr.right, where); + routeCond(bExpr, bExpr.getLeft(), where); + routeCond(bExpr, bExpr.getRight(), where); } else { explanCond("AND", expr, where); } @@ -87,15 +89,15 @@ private void routeCond(SQLBinaryOpExpr bExpr, SQLExpr sub, Where where) throws S if (sub instanceof SQLBinaryOpExpr) { parseWhere(bExpr, (SQLBinaryOpExpr) sub, where); } else { - explanCond(bExpr.operator.name, sub, where); + explanCond(bExpr.getOperator().name, sub, where); } } private void parseWhere(SQLBinaryOpExpr expr, SQLBinaryOpExpr sub, Where where) throws SqlParseException { if (isCond(sub)) { - explanCond(expr.operator.name, sub, where); + explanCond(expr.getOperator().name, sub, where); } else { - if (sub.operator.priority != expr.operator.priority) { + if (sub.getOperator().priority != expr.getOperator().priority) { Where subWhere = new Where(expr.getOperator().name); where.addWhere(subWhere); parseWhere(sub, subWhere); @@ -186,7 +188,13 @@ private void findGroupBy(MySqlSelectQueryBlock query, Select select) throws SqlP List standardGroupBys = new ArrayList<>(); for (SQLExpr sqlExpr : items) { - if ((!(sqlExpr instanceof SQLIdentifierExpr) || ((SQLIdentifierExpr) sqlExpr).isWrappedInParens()) && !standardGroupBys.isEmpty()) { + //todo: mysql expr patch + if (sqlExpr instanceof MySqlSelectGroupByExpr) { + MySqlSelectGroupByExpr sqlSelectGroupByExpr = (MySqlSelectGroupByExpr) sqlExpr; + sqlExpr = sqlSelectGroupByExpr.getExpr(); + } + + if (!(sqlExpr instanceof SQLIdentifierExpr) && !standardGroupBys.isEmpty()) { // flush the standard group bys select.addGroupBy(convertExprsToFields(standardGroupBys)); standardGroupBys = new ArrayList<>(); @@ -194,14 +202,10 @@ private void findGroupBy(MySqlSelectQueryBlock query, Select select) throws SqlP if (sqlExpr instanceof SQLIdentifierExpr) { SQLIdentifierExpr identifierExpr = (SQLIdentifierExpr) sqlExpr; - if (identifierExpr.isWrappedInParens()) { - // single item with parens (should be its own agg) - select.addGroupBy(FieldMaker.makeField(identifierExpr, null)); - } else { // single item without parens (should latch to before or after list) standardGroupBys.add(identifierExpr); - } - } else if (sqlExpr instanceof SQLListExpr) { + + } else if (sqlExpr instanceof SQLListExpr) { // multiple items in their own list SQLListExpr listExpr = (SQLListExpr) sqlExpr; select.addGroupBy(convertExprsToFields(listExpr.getItems())); @@ -250,7 +254,7 @@ private void findOrderBy(MySqlSelectQueryBlock query, Select select) throws SqlP } private void findLimit(MySqlSelectQueryBlock query, Select select) { - Limit limit = query.getLimit(); + MySqlSelectQueryBlock.Limit limit = query.getLimit(); if (limit == null) { return; @@ -268,14 +272,22 @@ private void findLimit(MySqlSelectQueryBlock query, Select select) { * @return list of From objects represents all the sources. */ private List findFrom(SQLTableSource from) { - String[] split = from.getTablename().toString().split(","); - - ArrayList fromList = new ArrayList<>(); - for (String source : split) { - fromList.add(new From(source.trim())); - } + boolean isSqlExprTable = from.getClass().isAssignableFrom(SQLExprTableSource.class); + + if(isSqlExprTable){ + String[] split = ((SQLExprTableSource) from).getExpr().toString().replaceAll(" ","").split(","); + ArrayList fromList = new ArrayList<>(); + for (String source : split) { + fromList.add(new From(source.trim())); + } + return fromList; + } - return fromList; - } + SQLJoinTableSource joinTableSource = ((SQLJoinTableSource) from); + List fromList = new ArrayList<>(); + fromList.addAll(findFrom(joinTableSource.getLeft())); + fromList.addAll(findFrom(joinTableSource.getRight())); + return fromList; + } } diff --git a/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java b/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java index 121eea18..89cd7970 100644 --- a/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java +++ b/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java @@ -1,16 +1,18 @@ package org.nlpcn.es4sql.query; -import org.durid.sql.SQLUtils; -import org.durid.sql.ast.expr.SQLQueryExpr; -import org.durid.sql.ast.statement.SQLDeleteStatement; -import org.durid.sql.parser.SQLParserUtils; -import org.durid.sql.parser.SQLStatementParser; -import org.durid.util.JdbcUtils; +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLExpr; +import com.alibaba.druid.sql.ast.expr.SQLQueryExpr; +import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlExprParser; +import com.alibaba.druid.sql.parser.*; +import com.alibaba.druid.util.JdbcUtils; import org.elasticsearch.client.Client; import org.nlpcn.es4sql.domain.Delete; import org.nlpcn.es4sql.domain.Select; import org.nlpcn.es4sql.exception.SqlParseException; +import org.nlpcn.es4sql.parse.ElasticSqlExprParser; import org.nlpcn.es4sql.parse.SqlParser; import java.sql.SQLFeatureNotSupportedException; @@ -28,7 +30,8 @@ public static QueryAction create(Client client, String sql) throws SqlParseExcep String firstWord = sql.substring(0, sql.indexOf(' ')); switch (firstWord.toUpperCase()) { case "SELECT": - SQLQueryExpr sqlExpr = (SQLQueryExpr) SQLUtils.toMySqlExpr(sql); + SQLQueryExpr sqlExpr = (SQLQueryExpr) toSqlExpr(sql); + Select select = new SqlParser().parseSelect(sqlExpr); if (select.isAgg) { @@ -46,4 +49,15 @@ public static QueryAction create(Client client, String sql) throws SqlParseExcep throw new SQLFeatureNotSupportedException(String.format("Unsupported query: %s", sql)); } } + + private static SQLExpr toSqlExpr(String sql) { + SQLExprParser parser = new ElasticSqlExprParser(sql); + SQLExpr expr = parser.expr(); + + if (parser.getLexer().token() != Token.EOF) { + throw new ParserException("illegal sql expr : " + sql); + } + + return expr; + } } diff --git a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java index e29b723b..519def13 100644 --- a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java +++ b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java @@ -3,6 +3,8 @@ import java.io.IOException; import java.util.Set; +import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; +import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr; import org.elasticsearch.common.collect.Sets; import org.elasticsearch.common.geo.ShapeRelation; import org.elasticsearch.common.geo.builders.ShapeBuilder; @@ -15,8 +17,7 @@ import org.nlpcn.es4sql.domain.Paramer; import org.nlpcn.es4sql.exception.SqlParseException; -import org.durid.sql.ast.expr.SQLIdentifierExpr; -import org.durid.sql.ast.expr.SQLMethodInvokeExpr; + import org.nlpcn.es4sql.spatial.*; public abstract class Maker { diff --git a/src/main/java/org/nlpcn/es4sql/spatial/SpatialParamsFactory.java b/src/main/java/org/nlpcn/es4sql/spatial/SpatialParamsFactory.java index dee176df..e0102840 100644 --- a/src/main/java/org/nlpcn/es4sql/spatial/SpatialParamsFactory.java +++ b/src/main/java/org/nlpcn/es4sql/spatial/SpatialParamsFactory.java @@ -1,7 +1,8 @@ package org.nlpcn.es4sql.spatial; -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.parser.SQLParseException; + +import com.alibaba.druid.sql.ast.SQLExpr; +import com.alibaba.druid.sql.parser.SQLParseException; import java.util.LinkedList; import java.util.List; @@ -14,11 +15,11 @@ public static Object generateSpatialParamsObject(String methodName,List switch(methodName){ case "GEO_INTERSECTS": if(params.size() != 2) - throw new SQLParseException("GEO_INTERSECTS should have exactly 2 parameters : (fieldName,'WKT') "); + throw new RuntimeException("GEO_INTERSECTS should have exactly 2 parameters : (fieldName,'WKT') "); return params.get(1).toString(); case "GEO_BOUNDING_BOX": if(params.size() != 5) - throw new SQLParseException("GEO_BOUNDING_BOX should have exactly 5 parameters : (fieldName,topLeftLon,topLeftLan,bottomRightLon,bottomRightLan) "); + throw new RuntimeException("GEO_BOUNDING_BOX should have exactly 5 parameters : (fieldName,topLeftLon,topLeftLan,bottomRightLon,bottomRightLan) "); double topLeftLon = Double.parseDouble(params.get(1).toString()); double topLeftLat = Double.parseDouble(params.get(2).toString()); double bottomRightLon = Double.parseDouble(params.get(3).toString()); @@ -26,14 +27,14 @@ public static Object generateSpatialParamsObject(String methodName,List return new BoundingBoxFilterParams(new Point(topLeftLon,topLeftLat),new Point(bottomRightLon,bottomRightLat)); case "GEO_DISTANCE": if(params.size()!=4) - throw new SQLParseException("GEO_DISTANCE should have exactly 4 parameters : (fieldName,distance,fromLon,fromLat) "); + throw new RuntimeException("GEO_DISTANCE should have exactly 4 parameters : (fieldName,distance,fromLon,fromLat) "); String distance = params.get(1).toString(); double lon = Double.parseDouble(params.get(2).toString()); double lat = Double.parseDouble(params.get(3).toString()); return new DistanceFilterParams(distance ,new Point(lon,lat)); case "GEO_DISTANCE_RANGE": if(params.size()!=5) - throw new SQLParseException("GEO_DISTANCE should have exactly 5 parameters : (fieldName,distanceFrom,distanceTo,fromLon,fromLat) "); + throw new RuntimeException("GEO_DISTANCE should have exactly 5 parameters : (fieldName,distanceFrom,distanceTo,fromLon,fromLat) "); String distanceFrom = params.get(1).toString(); String distanceTo = params.get(2).toString(); lon = Double.parseDouble(params.get(3).toString()); @@ -41,7 +42,7 @@ public static Object generateSpatialParamsObject(String methodName,List return new RangeDistanceFilterParams(distanceFrom,distanceTo ,new Point(lon,lat)); case "GEO_POLYGON": if(params.size()%2 == 0 || params.size() <= 5) - throw new SQLParseException("GEO_POLYGON should have odd num of parameters and > 5 : (fieldName,lon1,lat1,lon2,lat2,lon3,lat3,...) "); + throw new RuntimeException("GEO_POLYGON should have odd num of parameters and > 5 : (fieldName,lon1,lat1,lon2,lat2,lon3,lat3,...) "); int numberOfPoints = (params.size()-1)/2; List points = new LinkedList<>(); for(int i =0 ;i< numberOfPoints ;i ++){ @@ -53,7 +54,7 @@ public static Object generateSpatialParamsObject(String methodName,List return new PolygonFilterParams(points); case "GEO_CELL": if(params.size()< 4 || params.size() > 5) - throw new SQLParseException("GEO_CELL should have 4 or 5 params (fieldName,lon,lat,precision,neighbors(optional)) "); + throw new RuntimeException("GEO_CELL should have 4 or 5 params (fieldName,lon,lat,precision,neighbors(optional)) "); lon = Double.parseDouble(params.get(1).toString()); lat = Double.parseDouble(params.get(2).toString()); Point geoHashPoint = new Point(lon,lat); @@ -63,7 +64,7 @@ public static Object generateSpatialParamsObject(String methodName,List boolean neighbors = Boolean.parseBoolean(params.get(4).toString()); return new CellFilterParams(geoHashPoint,precision,neighbors); default: - throw new SQLParseException(String.format("Unknown method name: %s", methodName)); + throw new RuntimeException(String.format("Unknown method name: %s", methodName)); } } } diff --git a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java index 9cac1586..e61a64e1 100644 --- a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java +++ b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java @@ -48,6 +48,7 @@ public static void setUp() throws Exception { loadBulk("src/test/resources/accounts.json"); loadBulk("src/test/resources/online.json"); loadBulk("src/test/resources/phrases.json"); + loadBulk("src/test/resources/dogs.json"); prepareOdbcIndex(); loadBulk("src/test/resources/odbc-date-formats.json"); From 91baba01dcc157110252c57a8bf9b8d7fa05c19f Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Tue, 18 Aug 2015 21:08:00 +0300 Subject: [PATCH 043/559] comment datebracessearch test - this is not sql! --- src/test/java/org/nlpcn/es4sql/QueryTest.java | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java index 6a15225c..12c9076d 100644 --- a/src/test/java/org/nlpcn/es4sql/QueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java @@ -303,24 +303,24 @@ public void dateSearch() throws IOException, SqlParseException, SQLFeatureNotSup } } - @Test - public void dateSearchBraces() throws IOException, SqlParseException, SQLFeatureNotSupportedException, ParseException { - DateTimeFormatter formatter = DateTimeFormat.forPattern(TS_DATE_FORMAT); - DateTime dateToCompare = new DateTime(2015, 3, 15, 0, 0, 0); - - SearchHits response = query(String.format("SELECT insert_time FROM %s/odbc WHERE insert_time < {ts '2015-03-15 00:00:00.000'}", TEST_INDEX)); - SearchHit[] hits = response.getHits(); - for(SearchHit hit : hits) { - Map source = hit.getSource(); - String insertTimeStr = (String) source.get("insert_time"); - insertTimeStr = insertTimeStr.replace("{ts '", "").replace("'}", ""); - - DateTime insertTime = formatter.parseDateTime(insertTimeStr); - - String errorMessage = String.format("insert_time must be smaller then 2015-03-15. found: %s", insertTime); - Assert.assertTrue(errorMessage, insertTime.isBefore(dateToCompare)); - } - } +// @Test +// public void dateSearchBraces() throws IOException, SqlParseException, SQLFeatureNotSupportedException, ParseException { +// DateTimeFormatter formatter = DateTimeFormat.forPattern(TS_DATE_FORMAT); +// DateTime dateToCompare = new DateTime(2015, 3, 15, 0, 0, 0); +// +// SearchHits response = query(String.format("SELECT insert_time FROM %s/odbc WHERE insert_time < {ts '2015-03-15 00:00:00.000'}", TEST_INDEX)); +// SearchHit[] hits = response.getHits(); +// for(SearchHit hit : hits) { +// Map source = hit.getSource(); +// String insertTimeStr = (String) source.get("insert_time"); +// insertTimeStr = insertTimeStr.replace("{ts '", "").replace("'}", ""); +// +// DateTime insertTime = formatter.parseDateTime(insertTimeStr); +// +// String errorMessage = String.format("insert_time must be smaller then 2015-03-15. found: %s", insertTime); +// Assert.assertTrue(errorMessage, insertTime.isBefore(dateToCompare)); +// } +// } @Test From 8d8d137df010bd0c4c30445510c02a5b62147982 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Thu, 20 Aug 2015 00:24:30 +0300 Subject: [PATCH 044/559] prepared infrastructure for join --- .../plugin/nlpcn/RestSqlAction.java | 7 ++- src/main/java/org/nlpcn/es4sql/SearchDao.java | 3 +- .../org/nlpcn/es4sql/parse/SqlParser.java | 4 +- .../es4sql/query/AggregationQueryAction.java | 5 +- .../es4sql/query/DefaultQueryAction.java | 6 +- .../nlpcn/es4sql/query/DeleteQueryAction.java | 5 +- .../nlpcn/es4sql/query/ESActionFactory.java | 39 +++++++++--- .../org/nlpcn/es4sql/query/JoinResponse.java | 9 +++ .../org/nlpcn/es4sql/query/QueryAction.java | 2 +- ...SqlElasticDeleteByQueryRequestBuilder.java | 50 ++++++++++++++++ .../query/SqlElasticRequestBuilder.java | 13 ++++ .../query/SqlElasticSearchRequestBuilder.java | 49 +++++++++++++++ .../es4sql/query/explain/ExplainManager.java | 40 ------------- .../org/nlpcn/es4sql/AggregationTest.java | 16 ++--- src/test/java/org/nlpcn/es4sql/BugTest.java | 3 +- .../java/org/nlpcn/es4sql/ExplainTest.java | 5 +- src/test/java/org/nlpcn/es4sql/JoinTest.java | 60 +++++++++++++++++++ .../java/org/nlpcn/es4sql/MainTestSuite.java | 1 + .../org/nlpcn/es4sql/MethodQueryTest.java | 11 ++-- src/test/java/org/nlpcn/es4sql/QueryTest.java | 10 ++-- src/test/resources/dogs.json | 4 ++ 21 files changed, 261 insertions(+), 81 deletions(-) create mode 100644 src/main/java/org/nlpcn/es4sql/query/JoinResponse.java create mode 100644 src/main/java/org/nlpcn/es4sql/query/SqlElasticDeleteByQueryRequestBuilder.java create mode 100644 src/main/java/org/nlpcn/es4sql/query/SqlElasticRequestBuilder.java create mode 100644 src/main/java/org/nlpcn/es4sql/query/SqlElasticSearchRequestBuilder.java delete mode 100644 src/main/java/org/nlpcn/es4sql/query/explain/ExplainManager.java create mode 100644 src/test/java/org/nlpcn/es4sql/JoinTest.java create mode 100644 src/test/resources/dogs.json diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java b/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java index 42b0e163..19a66a73 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java @@ -7,6 +7,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.rest.*; import org.nlpcn.es4sql.SearchDao; +import org.nlpcn.es4sql.query.SqlElasticRequestBuilder; import org.nlpcn.es4sql.query.explain.ExplainManager; public class RestSqlAction extends BaseRestHandler { @@ -29,12 +30,12 @@ protected void handleRequest(RestRequest request, RestChannel channel, final Cli } SearchDao searchDao = new SearchDao(client); - ActionRequestBuilder actionRequestBuilder = searchDao.explain(sql); - ActionRequest actionRequest = actionRequestBuilder.request(); + SqlElasticRequestBuilder actionRequestBuilder = searchDao.explain(sql); + ActionRequest actionRequest = actionRequestBuilder.request(); // TODO add unittests to explain. (rest level?) if (request.path().endsWith("/_explain")) { - String jsonExplanation = ExplainManager.explain(actionRequestBuilder); + String jsonExplanation = actionRequestBuilder.explain(); BytesRestResponse bytesRestResponse = new BytesRestResponse(RestStatus.OK, jsonExplanation); channel.sendResponse(bytesRestResponse); } else { diff --git a/src/main/java/org/nlpcn/es4sql/SearchDao.java b/src/main/java/org/nlpcn/es4sql/SearchDao.java index a225a1b9..d8f5cf5c 100644 --- a/src/main/java/org/nlpcn/es4sql/SearchDao.java +++ b/src/main/java/org/nlpcn/es4sql/SearchDao.java @@ -9,6 +9,7 @@ import org.nlpcn.es4sql.exception.SqlParseException; import org.nlpcn.es4sql.query.ESActionFactory; import org.nlpcn.es4sql.query.QueryAction; +import org.nlpcn.es4sql.query.SqlElasticRequestBuilder; public class SearchDao { @@ -38,7 +39,7 @@ public SearchDao(Client client) { * @return ES request * @throws SqlParseException */ - public ActionRequestBuilder explain(String sql) throws SqlParseException, SQLFeatureNotSupportedException { + public SqlElasticRequestBuilder explain(String sql) throws SqlParseException, SQLFeatureNotSupportedException { QueryAction query = ESActionFactory.create(client, sql); return query.explain(); diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index 3766dfe3..e421996a 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -33,9 +33,9 @@ public Select parseSelect(SQLQueryExpr mySqlExpr) throws SqlParseException { MySqlSelectQueryBlock query = (MySqlSelectQueryBlock) mySqlExpr.getSubQuery().getQuery(); Select select = new Select(); - + //TODO: JOIN: maby other select parse? we need to know which field to get from which index findSelect(query, select); - + //TODO: here we will know if this is join. select.getFrom().addAll(findFrom(query.getFrom())); select.setWhere(findWhere(query.getWhere())); diff --git a/src/main/java/org/nlpcn/es4sql/query/AggregationQueryAction.java b/src/main/java/org/nlpcn/es4sql/query/AggregationQueryAction.java index bdbc2dc8..2d1ce978 100644 --- a/src/main/java/org/nlpcn/es4sql/query/AggregationQueryAction.java +++ b/src/main/java/org/nlpcn/es4sql/query/AggregationQueryAction.java @@ -39,7 +39,7 @@ public AggregationQueryAction(Client client, Select select) { } @Override - public SearchRequestBuilder explain() throws SqlParseException { + public SqlElasticSearchRequestBuilder explain() throws SqlParseException { this.request = client.prepareSearch(); request.setListenerThreaded(false); setIndicesAndTypes(); @@ -105,7 +105,8 @@ public SearchRequestBuilder explain() throws SqlParseException { setLimit(select.getOffset(), select.getRowCount()); request.setSearchType(SearchType.DEFAULT); - return request; + SqlElasticSearchRequestBuilder sqlElasticRequestBuilder = new SqlElasticSearchRequestBuilder(request); + return sqlElasticRequestBuilder; } private boolean isASC(Order order) { diff --git a/src/main/java/org/nlpcn/es4sql/query/DefaultQueryAction.java b/src/main/java/org/nlpcn/es4sql/query/DefaultQueryAction.java index 9f43d22c..85956b12 100644 --- a/src/main/java/org/nlpcn/es4sql/query/DefaultQueryAction.java +++ b/src/main/java/org/nlpcn/es4sql/query/DefaultQueryAction.java @@ -31,7 +31,7 @@ public DefaultQueryAction(Client client, Select select) { } @Override - public SearchRequestBuilder explain() throws SqlParseException { + public SqlElasticSearchRequestBuilder explain() throws SqlParseException { this.request = client.prepareSearch(); request.setListenerThreaded(false); setIndicesAndTypes(); @@ -43,8 +43,8 @@ public SearchRequestBuilder explain() throws SqlParseException { // set SearchType. request.setSearchType(SearchType.DFS_QUERY_THEN_FETCH); - - return request; + SqlElasticSearchRequestBuilder sqlElasticRequestBuilder = new SqlElasticSearchRequestBuilder(request); + return sqlElasticRequestBuilder; } /** diff --git a/src/main/java/org/nlpcn/es4sql/query/DeleteQueryAction.java b/src/main/java/org/nlpcn/es4sql/query/DeleteQueryAction.java index 3401209d..8b3a9c0c 100644 --- a/src/main/java/org/nlpcn/es4sql/query/DeleteQueryAction.java +++ b/src/main/java/org/nlpcn/es4sql/query/DeleteQueryAction.java @@ -25,13 +25,14 @@ public DeleteQueryAction(Client client, Delete delete) { } @Override - public DeleteByQueryRequestBuilder explain() throws SqlParseException { + public SqlElasticDeleteByQueryRequestBuilder explain() throws SqlParseException { this.request = client.prepareDeleteByQuery(); request.setListenerThreaded(false); setIndicesAndTypes(); setWhere(delete.getWhere()); - return request; + SqlElasticDeleteByQueryRequestBuilder deleteByQueryRequestBuilder = new SqlElasticDeleteByQueryRequestBuilder(request); + return deleteByQueryRequestBuilder; } diff --git a/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java b/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java index 89cd7970..c0110590 100644 --- a/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java +++ b/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java @@ -5,6 +5,10 @@ import com.alibaba.druid.sql.ast.SQLExpr; import com.alibaba.druid.sql.ast.expr.SQLQueryExpr; import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement; +import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource; +import com.alibaba.druid.sql.ast.statement.SQLSelectQuery; +import com.alibaba.druid.sql.ast.statement.SQLTableSource; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; import com.alibaba.druid.sql.dialect.mysql.parser.MySqlExprParser; import com.alibaba.druid.sql.parser.*; import com.alibaba.druid.util.JdbcUtils; @@ -28,17 +32,31 @@ public class ESActionFactory { */ public static QueryAction create(Client client, String sql) throws SqlParseException, SQLFeatureNotSupportedException { String firstWord = sql.substring(0, sql.indexOf(' ')); - switch (firstWord.toUpperCase()) { + //Join + //NestedLoops ? + // contains: + // s1 = select from SqlParser for one of them (need to take only first table wheres) + // c = conditions for crossed -> each condition is field = s2Field , value = s1Field + // s2 = select from SqlParser for the 2nd (need to take only 2nd table wheres) + // res1 = DefaultQueryAction(client, s1); + // for each res1 item => s2.conditions = union(s2.conditions,c(replace value to res1 value)) + + switch (firstWord.toUpperCase()) { case "SELECT": SQLQueryExpr sqlExpr = (SQLQueryExpr) toSqlExpr(sql); + if(isJoin(sqlExpr,sql)){ + //NestedLoopQueryAction(client) + return null; + } + else { + Select select = new SqlParser().parseSelect(sqlExpr); - Select select = new SqlParser().parseSelect(sqlExpr); - - if (select.isAgg) { - return new AggregationQueryAction(client, select); - } else { - return new DefaultQueryAction(client, select); - } + if (select.isAgg) { + return new AggregationQueryAction(client, select); + } else { + return new DefaultQueryAction(client, select); + } + } case "DELETE": SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, JdbcUtils.MYSQL); SQLDeleteStatement deleteStatement = parser.parseDeleteStatement(); @@ -50,6 +68,11 @@ public static QueryAction create(Client client, String sql) throws SqlParseExcep } } + private static boolean isJoin(SQLQueryExpr sqlExpr,String sql) { + MySqlSelectQueryBlock query = (MySqlSelectQueryBlock) sqlExpr.getSubQuery().getQuery(); + return query.getFrom() instanceof SQLJoinTableSource && sql.toLowerCase().contains("join"); + } + private static SQLExpr toSqlExpr(String sql) { SQLExprParser parser = new ElasticSqlExprParser(sql); SQLExpr expr = parser.expr(); diff --git a/src/main/java/org/nlpcn/es4sql/query/JoinResponse.java b/src/main/java/org/nlpcn/es4sql/query/JoinResponse.java new file mode 100644 index 00000000..5c2ec869 --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/query/JoinResponse.java @@ -0,0 +1,9 @@ +package org.nlpcn.es4sql.query; + +import org.elasticsearch.action.ActionResponse; + +/** + * Created by Eliran on 19/8/2015. + */ +public class JoinResponse extends ActionResponse { +} diff --git a/src/main/java/org/nlpcn/es4sql/query/QueryAction.java b/src/main/java/org/nlpcn/es4sql/query/QueryAction.java index 9d4e7578..4e21bec5 100644 --- a/src/main/java/org/nlpcn/es4sql/query/QueryAction.java +++ b/src/main/java/org/nlpcn/es4sql/query/QueryAction.java @@ -29,6 +29,6 @@ public QueryAction(Client client, Query query) { * @return ActionRequestBuilder (ES request) * @throws SqlParseException */ - public abstract ActionRequestBuilder explain() throws SqlParseException; + public abstract SqlElasticRequestBuilder explain() throws SqlParseException; } diff --git a/src/main/java/org/nlpcn/es4sql/query/SqlElasticDeleteByQueryRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/SqlElasticDeleteByQueryRequestBuilder.java new file mode 100644 index 00000000..cd5f2e85 --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/query/SqlElasticDeleteByQueryRequestBuilder.java @@ -0,0 +1,50 @@ +package org.nlpcn.es4sql.query; + +import org.elasticsearch.action.ActionRequest; +import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.action.deletebyquery.DeleteByQueryRequestBuilder; +import org.elasticsearch.action.support.QuerySourceBuilder; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentType; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * Created by Eliran on 19/8/2015. + */ +public class SqlElasticDeleteByQueryRequestBuilder implements SqlElasticRequestBuilder { + DeleteByQueryRequestBuilder deleteByQueryRequestBuilder; + + public SqlElasticDeleteByQueryRequestBuilder(DeleteByQueryRequestBuilder deleteByQueryRequestBuilder) { + this.deleteByQueryRequestBuilder = deleteByQueryRequestBuilder; + } + + @Override + public ActionRequest request() { + return deleteByQueryRequestBuilder.request(); + } + + @Override + public String explain() { + try { + XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint(); + Method method = deleteByQueryRequestBuilder.getClass().getDeclaredMethod("sourceBuilder"); + method.setAccessible(true); + QuerySourceBuilder sourceBuilder = (QuerySourceBuilder) method.invoke(deleteByQueryRequestBuilder); + sourceBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS); + return builder.string(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + @Override + public ActionResponse get() { + return this.deleteByQueryRequestBuilder.get(); + } +} diff --git a/src/main/java/org/nlpcn/es4sql/query/SqlElasticRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/SqlElasticRequestBuilder.java new file mode 100644 index 00000000..a773bca2 --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/query/SqlElasticRequestBuilder.java @@ -0,0 +1,13 @@ +package org.nlpcn.es4sql.query; + +import org.elasticsearch.action.ActionRequest; +import org.elasticsearch.action.ActionResponse; + +/** + * Created by Eliran on 19/8/2015. + */ +public interface SqlElasticRequestBuilder { + public ActionRequest request(); + public String explain(); + public ActionResponse get(); +} diff --git a/src/main/java/org/nlpcn/es4sql/query/SqlElasticSearchRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/SqlElasticSearchRequestBuilder.java new file mode 100644 index 00000000..0a8a1d6b --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/query/SqlElasticSearchRequestBuilder.java @@ -0,0 +1,49 @@ +package org.nlpcn.es4sql.query; + +import org.elasticsearch.action.ActionRequest; +import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.action.search.SearchRequestBuilder; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentType; + +import java.io.IOException; + +/** + * Created by Eliran on 19/8/2015. + */ +public class SqlElasticSearchRequestBuilder implements SqlElasticRequestBuilder { + SearchRequestBuilder requestBuilder; + + public SqlElasticSearchRequestBuilder(SearchRequestBuilder requestBuilder) { + this.requestBuilder = requestBuilder; + } + + @Override + public ActionRequest request() { + return requestBuilder.request(); + } + + @Override + public String explain() { + try { + XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint(); + requestBuilder.internalBuilder().toXContent(builder, ToXContent.EMPTY_PARAMS); + return builder.string(); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + @Override + public ActionResponse get() { + return requestBuilder.get(); + } + + @Override + public String toString(){ + return this.requestBuilder.toString(); + } +} diff --git a/src/main/java/org/nlpcn/es4sql/query/explain/ExplainManager.java b/src/main/java/org/nlpcn/es4sql/query/explain/ExplainManager.java deleted file mode 100644 index 084248f2..00000000 --- a/src/main/java/org/nlpcn/es4sql/query/explain/ExplainManager.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.nlpcn.es4sql.query.explain; - -import org.elasticsearch.action.ActionRequestBuilder; -import org.elasticsearch.action.deletebyquery.DeleteByQueryRequestBuilder; -import org.elasticsearch.action.search.SearchRequestBuilder; -import org.elasticsearch.action.support.QuerySourceBuilder; -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentType; - -import java.io.IOException; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.sql.SQLFeatureNotSupportedException; - -/** - * Transform ActionRequestBuilder into json. - */ -public class ExplainManager { - - public static String explain(ActionRequestBuilder actionRequest) throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, SQLFeatureNotSupportedException { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint(); - - if (actionRequest instanceof SearchRequestBuilder) { - ((SearchRequestBuilder) actionRequest).internalBuilder().toXContent(builder, ToXContent.EMPTY_PARAMS); - } else if (actionRequest instanceof DeleteByQueryRequestBuilder) { - // access private method to get the explain... - DeleteByQueryRequestBuilder deleteRequest = ((DeleteByQueryRequestBuilder) actionRequest); - Method method = deleteRequest.getClass().getDeclaredMethod("sourceBuilder"); - method.setAccessible(true); - QuerySourceBuilder sourceBuilder = (QuerySourceBuilder) method.invoke(deleteRequest); - sourceBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS); - } else { - throw new SQLFeatureNotSupportedException(String.format("Failed to explain class %s", actionRequest.getClass().getName())); - } - - return builder.string(); - } -} diff --git a/src/test/java/org/nlpcn/es4sql/AggregationTest.java b/src/test/java/org/nlpcn/es4sql/AggregationTest.java index 88f046b8..2441f89c 100644 --- a/src/test/java/org/nlpcn/es4sql/AggregationTest.java +++ b/src/test/java/org/nlpcn/es4sql/AggregationTest.java @@ -19,6 +19,8 @@ import org.junit.Assert; import org.junit.Test; import org.nlpcn.es4sql.exception.SqlParseException; +import org.nlpcn.es4sql.query.SqlElasticSearchRequestBuilder; + import java.io.IOException; import java.sql.SQLFeatureNotSupportedException; import java.util.*; @@ -188,7 +190,7 @@ public void countGroupByRange() throws IOException, SqlParseException, SQLFeatur */ @Test public void countGroupByDateTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { - SearchRequestBuilder result = (SearchRequestBuilder) MainTestSuite.getSearchDao().explain("select insert_time from online group by date_histogram(field='insert_time','interval'='1.5h','format'='yyyy-MM') "); + SqlElasticSearchRequestBuilder result = (SqlElasticSearchRequestBuilder) MainTestSuite.getSearchDao().explain("select insert_time from online group by date_histogram(field='insert_time','interval'='1.5h','format'='yyyy-MM') "); System.out.println(result); } @@ -202,7 +204,7 @@ public void countGroupByDateTest() throws IOException, SqlParseException, SQLFea */ @Test public void countDateRangeTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { - SearchRequestBuilder result = (SearchRequestBuilder) MainTestSuite.getSearchDao().explain("select online from online group by date_range(field='insert_time','format'='yyyy-MM-dd' ,'2014-08-18','2014-08-17','now-8d','now-7d','now-6d','now') "); + SqlElasticSearchRequestBuilder result = (SqlElasticSearchRequestBuilder) MainTestSuite.getSearchDao().explain("select online from online group by date_range(field='insert_time','format'='yyyy-MM-dd' ,'2014-08-18','2014-08-17','now-8d','now-7d','now-6d','now') "); System.out.println(result); } @@ -217,14 +219,14 @@ public void countDateRangeTest() throws IOException, SqlParseException, SQLFeatu */ @Test public void topHitTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { - SearchRequestBuilder result = (SearchRequestBuilder) MainTestSuite.getSearchDao().explain("select topHits('size'=3,age='desc') from bank group by gender "); + SqlElasticSearchRequestBuilder result = (SqlElasticSearchRequestBuilder) MainTestSuite.getSearchDao().explain("select topHits('size'=3,age='desc') from bank group by gender "); System.out.println(result); } private Aggregations query(String query) throws SqlParseException, SQLFeatureNotSupportedException { SearchDao searchDao = MainTestSuite.getSearchDao(); - SearchRequestBuilder select = (SearchRequestBuilder) searchDao.explain(query); - return select.get().getAggregations(); + SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) searchDao.explain(query); + return ((SearchResponse)select.get()).getAggregations(); } @Test @@ -235,8 +237,8 @@ public void testSubAggregations() throws Exception { Map> buckets = new HashMap<>(); SearchDao searchDao = MainTestSuite.getSearchDao(); - SearchRequestBuilder select = (SearchRequestBuilder) searchDao.explain(query); - SearchResponse response = select.get(); + SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) searchDao.explain(query); + SearchResponse response = (SearchResponse) select.get(); Aggregations result = response.getAggregations(); Terms gender = result.get("gender"); diff --git a/src/test/java/org/nlpcn/es4sql/BugTest.java b/src/test/java/org/nlpcn/es4sql/BugTest.java index ae095ec9..249f9877 100644 --- a/src/test/java/org/nlpcn/es4sql/BugTest.java +++ b/src/test/java/org/nlpcn/es4sql/BugTest.java @@ -6,6 +6,7 @@ import org.elasticsearch.action.search.SearchRequestBuilder; import org.junit.Test; import org.nlpcn.es4sql.exception.SqlParseException; +import org.nlpcn.es4sql.query.SqlElasticSearchRequestBuilder; /** * some bad case @@ -18,7 +19,7 @@ public class BugTest { @Test public void bug1() throws IOException, SqlParseException, SQLFeatureNotSupportedException { - SearchRequestBuilder select = (SearchRequestBuilder) MainTestSuite.getSearchDao().explain("select count(*),sum(age) from bank"); + SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) MainTestSuite.getSearchDao().explain("select count(*),sum(age) from bank"); System.out.println(select); } } diff --git a/src/test/java/org/nlpcn/es4sql/ExplainTest.java b/src/test/java/org/nlpcn/es4sql/ExplainTest.java index 7854181c..61037a86 100644 --- a/src/test/java/org/nlpcn/es4sql/ExplainTest.java +++ b/src/test/java/org/nlpcn/es4sql/ExplainTest.java @@ -5,6 +5,7 @@ import org.elasticsearch.action.ActionRequestBuilder; import org.junit.Test; import org.nlpcn.es4sql.exception.SqlParseException; +import org.nlpcn.es4sql.query.SqlElasticRequestBuilder; import org.nlpcn.es4sql.query.explain.ExplainManager; import java.io.File; @@ -45,7 +46,7 @@ public void spatialFilterExplainTest() throws IOException, SqlParseException, No private String explain(String sql) throws SQLFeatureNotSupportedException, SqlParseException, InvocationTargetException, NoSuchMethodException, IllegalAccessException, IOException { SearchDao searchDao = MainTestSuite.getSearchDao(); - ActionRequestBuilder requestBuilder = searchDao.explain(sql); - return ExplainManager.explain(requestBuilder); + SqlElasticRequestBuilder requestBuilder = searchDao.explain(sql); + return requestBuilder.explain(); } } diff --git a/src/test/java/org/nlpcn/es4sql/JoinTest.java b/src/test/java/org/nlpcn/es4sql/JoinTest.java new file mode 100644 index 00000000..d06e0457 --- /dev/null +++ b/src/test/java/org/nlpcn/es4sql/JoinTest.java @@ -0,0 +1,60 @@ +package org.nlpcn.es4sql; + +import org.elasticsearch.action.ActionRequestBuilder; +import org.elasticsearch.action.search.SearchRequestBuilder; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.search.SearchHits; +import org.junit.Assert; +import org.junit.Test; +import org.nlpcn.es4sql.exception.SqlParseException; +import org.nlpcn.es4sql.query.SqlElasticRequestBuilder; +import org.nlpcn.es4sql.query.SqlElasticSearchRequestBuilder; +import org.nlpcn.es4sql.query.explain.ExplainManager; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.sql.SQLFeatureNotSupportedException; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.greaterThan; +import static org.nlpcn.es4sql.TestsConstants.TEST_INDEX; + +/** + * Created by Eliran on 15/8/2015. + */ +public class JoinTest { + @Test + public void leftJoinTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { + String query = "SELECT * FROM elasticsearch-sql_test_index/account LEFT JOIN elasticsearch-sql_test_index/dog on dog.holdersName = account.firstname"; + //SearchHits response = query(query); + // Assert.assertEquals(1004, response.getTotalHits()); + + String explain = explain(query); +// System.out.println("explain = " + explain); + + } + + @Test + public void straightJoinTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { + String query = "SELECT * FROM elasticsearch-sql_test_index/account JOIN elasticsearch-sql_test_index/dog on dog.holdersName = account.firstname"; + //SearchHits response = query(query); + // Assert.assertEquals(1004, response.getTotalHits()); + + String explain = explain(query); +// System.out.println("explain = " + explain); + + } + + private SearchHits query(String query) throws SqlParseException, SQLFeatureNotSupportedException, SQLFeatureNotSupportedException { + SearchDao searchDao = MainTestSuite.getSearchDao(); + SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) searchDao.explain(query); + return ((SearchResponse)select.get()).getHits(); + } + + private String explain(String sql) throws SQLFeatureNotSupportedException, SqlParseException, InvocationTargetException, NoSuchMethodException, IllegalAccessException, IOException { + SearchDao searchDao = MainTestSuite.getSearchDao(); + SqlElasticRequestBuilder requestBuilder = searchDao.explain(sql); + return requestBuilder.explain(); + } + +} diff --git a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java index e61a64e1..d97a1337 100644 --- a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java +++ b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java @@ -28,6 +28,7 @@ DeleteTest.class, ExplainTest.class, WktToGeoJsonConverterTests.class +// JoinTest.class }) public class MainTestSuite { diff --git a/src/test/java/org/nlpcn/es4sql/MethodQueryTest.java b/src/test/java/org/nlpcn/es4sql/MethodQueryTest.java index ba04cc31..688a006c 100644 --- a/src/test/java/org/nlpcn/es4sql/MethodQueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/MethodQueryTest.java @@ -3,6 +3,7 @@ import org.elasticsearch.action.search.SearchRequestBuilder; import org.junit.Test; import org.nlpcn.es4sql.exception.SqlParseException; +import org.nlpcn.es4sql.query.SqlElasticSearchRequestBuilder; import java.io.IOException; import java.sql.SQLFeatureNotSupportedException; @@ -23,7 +24,7 @@ public class MethodQueryTest { */ @Test public void queryTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { - SearchRequestBuilder select = (SearchRequestBuilder) MainTestSuite.getSearchDao().explain("select address from bank where q= query('address:880 Holmes Lane') limit 3"); + SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) MainTestSuite.getSearchDao().explain("select address from bank where q= query('address:880 Holmes Lane') limit 3"); System.out.println(select); } @@ -36,7 +37,7 @@ public void queryTest() throws IOException, SqlParseException, SQLFeatureNotSupp */ @Test public void matchQueryTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { - SearchRequestBuilder select = (SearchRequestBuilder) MainTestSuite.getSearchDao().explain("select address from bank where address= matchQuery('880 Holmes Lane') limit 3"); + SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) MainTestSuite.getSearchDao().explain("select address from bank where address= matchQuery('880 Holmes Lane') limit 3"); System.out.println(select); } @@ -52,7 +53,7 @@ public void matchQueryTest() throws IOException, SqlParseException, SQLFeatureNo */ @Test public void scoreQueryTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { - SearchRequestBuilder select = (SearchRequestBuilder) MainTestSuite.getSearchDao().explain("select address from bank where address= score(matchQuery('Lane'),100) or address= score(matchQuery('Street'),0.5) order by _score desc limit 3"); + SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) MainTestSuite.getSearchDao().explain("select address from bank where address= score(matchQuery('Lane'),100) or address= score(matchQuery('Street'),0.5) order by _score desc limit 3"); System.out.println(select); } @@ -65,7 +66,7 @@ public void scoreQueryTest() throws IOException, SqlParseException, SQLFeatureNo */ @Test public void wildcardQueryTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { - SearchRequestBuilder select = (SearchRequestBuilder) MainTestSuite.getSearchDao().explain("select address from bank where address= wildcardQuery('l*e') order by _score desc limit 3"); + SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) MainTestSuite.getSearchDao().explain("select address from bank where address= wildcardQuery('l*e') order by _score desc limit 3"); System.out.println(select); } @@ -82,7 +83,7 @@ public void wildcardQueryTest() throws IOException, SqlParseException, SQLFeatur */ @Test public void matchPhraseQueryTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { - SearchRequestBuilder select = (SearchRequestBuilder) MainTestSuite.getSearchDao().explain("select address from bank where address= matchPhrase('671 Bristol Street') order by _score desc limit 3"); + SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) MainTestSuite.getSearchDao().explain("select address from bank where address= matchPhrase('671 Bristol Street') order by _score desc limit 3"); System.out.println(select); } } diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java index 12c9076d..87e8fe89 100644 --- a/src/test/java/org/nlpcn/es4sql/QueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java @@ -1,6 +1,7 @@ package org.nlpcn.es4sql; import org.elasticsearch.action.search.SearchRequestBuilder; +import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.common.joda.time.DateTime; import org.elasticsearch.common.joda.time.format.DateTimeFormat; import org.elasticsearch.common.joda.time.format.DateTimeFormatter; @@ -9,6 +10,7 @@ import org.junit.Assert; import org.junit.Test; import org.nlpcn.es4sql.exception.SqlParseException; +import org.nlpcn.es4sql.query.SqlElasticSearchRequestBuilder; import java.io.IOException; import java.sql.SQLFeatureNotSupportedException; @@ -503,8 +505,8 @@ public void geoPolygon() throws SQLFeatureNotSupportedException, SqlParseExcepti private SearchHits query(String query) throws SqlParseException, SQLFeatureNotSupportedException, SQLFeatureNotSupportedException { - SearchDao searchDao = MainTestSuite.getSearchDao(); - SearchRequestBuilder select = (SearchRequestBuilder)searchDao.explain(query); - return select.get().getHits(); - } + SearchDao searchDao = MainTestSuite.getSearchDao(); + SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) searchDao.explain(query); + return ((SearchResponse)select.get()).getHits(); + } } diff --git a/src/test/resources/dogs.json b/src/test/resources/dogs.json new file mode 100644 index 00000000..744bb6aa --- /dev/null +++ b/src/test/resources/dogs.json @@ -0,0 +1,4 @@ +{"index":{"_type": "dog", "_id":"1"}} +{"name":"rex","holdersName":"Amber","age":2} +{"index":{"_type": "dog", "_id":"6"}} +{"name":"snoopy","holdersName":"Hattie","age":4} From 3952521b84c127d09bcda5a48db55d6c2f63ec26 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Fri, 21 Aug 2015 20:22:22 +0300 Subject: [PATCH 045/559] finished parsing joinSql --- .../org/nlpcn/es4sql/domain/Condition.java | 4 +- .../java/org/nlpcn/es4sql/domain/From.java | 16 +- .../org/nlpcn/es4sql/domain/JoinSelect.java | 100 +++++++++ .../java/org/nlpcn/es4sql/domain/Select.java | 2 +- .../org/nlpcn/es4sql/parse/FieldMaker.java | 13 +- .../org/nlpcn/es4sql/parse/SqlParser.java | 207 ++++++++++++++++-- .../nlpcn/es4sql/query/ESActionFactory.java | 62 ++++-- src/test/java/org/nlpcn/es4sql/JoinTest.java | 60 ----- .../java/org/nlpcn/es4sql/MainTestSuite.java | 4 +- .../java/org/nlpcn/es4sql/SqlParserTests.java | 129 +++++++++++ 10 files changed, 495 insertions(+), 102 deletions(-) create mode 100644 src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java delete mode 100644 src/test/java/org/nlpcn/es4sql/JoinTest.java create mode 100644 src/test/java/org/nlpcn/es4sql/SqlParserTests.java diff --git a/src/main/java/org/nlpcn/es4sql/domain/Condition.java b/src/main/java/org/nlpcn/es4sql/domain/Condition.java index 431ea23a..2f7f3d56 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/Condition.java +++ b/src/main/java/org/nlpcn/es4sql/domain/Condition.java @@ -20,7 +20,9 @@ public static enum OPEAR { private Object value; private OPEAR opear; - + + private String aliasTableName; + public Condition(CONN conn, String name, OPEAR oper, Object value) throws SqlParseException { super(conn); this.opear = null; diff --git a/src/main/java/org/nlpcn/es4sql/domain/From.java b/src/main/java/org/nlpcn/es4sql/domain/From.java index 6b8414bc..ef42167d 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/From.java +++ b/src/main/java/org/nlpcn/es4sql/domain/From.java @@ -9,14 +9,13 @@ public class From { private String index; private String type; - + private String alias; /** * Extract index and type from the 'from' string * @param from The part after the FROM keyword. */ public From(String from) { - from.replaceAll("#","*"); String[] parts = from.split("/"); this.index = parts[0].trim(); if (parts.length == 2) { @@ -24,6 +23,11 @@ public From(String from) { } } + public From(String from,String alias){ + this(from); + this.alias = alias; + } + public String getIndex() { return index ; } @@ -39,4 +43,12 @@ public String getType() { public void setType(String type) { this.type = type; } + + public String getAlias() { + return alias; + } + + public void setAlias(String alias) { + this.alias = alias; + } } diff --git a/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java b/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java new file mode 100644 index 00000000..2a6d7aca --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java @@ -0,0 +1,100 @@ +package org.nlpcn.es4sql.domain; + +import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource; + +import java.util.List; + +/** + * Created by Eliran on 20/8/2015. + */ +public class JoinSelect { + private List connectedConditions; + + //todo: make it an object + private Select t1Select; + private List t1ConnectedFields; + private List t1OnlyFields; + + private Select t2Select; + private List t2ConnectedFields; + private List t2OnlyFields; + private SQLJoinTableSource.JoinType joinType; + + public JoinSelect() { + } + + public JoinSelect(List connectedConditions, Select t1Select, List t1ConnectedFields, List t1OnlyFields, Select t2Select, List t2ConnectedFields, List t2OnlyFields,SQLJoinTableSource.JoinType joinType) { + this.connectedConditions = connectedConditions; + this.t1Select = t1Select; + this.t1ConnectedFields = t1ConnectedFields; + this.t1OnlyFields = t1OnlyFields; + this.t2Select = t2Select; + this.t2ConnectedFields = t2ConnectedFields; + this.t2OnlyFields = t2OnlyFields; + this.joinType = joinType; + } + + public List getConnectedConditions() { + return connectedConditions; + } + + public void setConnectedConditions(List connectedConditions) { + this.connectedConditions = connectedConditions; + } + + public Select getT1Select() { + return t1Select; + } + + public void setT1Select(Select t1Select) { + this.t1Select = t1Select; + } + + public List getT1ConnectedFields() { + return t1ConnectedFields; + } + + public void setT1ConnectedFields(List t1ConnectedFields) { + this.t1ConnectedFields = t1ConnectedFields; + } + + public List getT1OnlyFields() { + return t1OnlyFields; + } + + public void setT1OnlyFields(List t1OnlyFields) { + this.t1OnlyFields = t1OnlyFields; + } + + public Select getT2Select() { + return t2Select; + } + + public void setT2Select(Select t2Select) { + this.t2Select = t2Select; + } + + public List getT2ConnectedFields() { + return t2ConnectedFields; + } + + public void setT2ConnectedFields(List t2ConnectedFields) { + this.t2ConnectedFields = t2ConnectedFields; + } + + public List getT2OnlyFields() { + return t2OnlyFields; + } + + public void setT2OnlyFields(List t2OnlyFields) { + this.t2OnlyFields = t2OnlyFields; + } + + public SQLJoinTableSource.JoinType getJoinType() { + return joinType; + } + + public void setJoinType(SQLJoinTableSource.JoinType joinType) { + this.joinType = joinType; + } +} diff --git a/src/main/java/org/nlpcn/es4sql/domain/Select.java b/src/main/java/org/nlpcn/es4sql/domain/Select.java index 10c14b2d..43f3889d 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/Select.java +++ b/src/main/java/org/nlpcn/es4sql/domain/Select.java @@ -75,7 +75,7 @@ public void addOrderBy(String name, String type) { public void addField(Field field) { - if (field == null) { + if (field == null || field.getName() == "*") { return; } diff --git a/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java b/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java index 1d766ccf..076b1f42 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java +++ b/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java @@ -18,10 +18,19 @@ * */ public class FieldMaker { - public static Field makeField(SQLExpr expr, String alias) throws SqlParseException { + public static Field makeField(SQLExpr expr, String alias,String tableAlias) throws SqlParseException { if (expr instanceof SQLIdentifierExpr || expr instanceof SQLPropertyExpr) { String name = expr.toString().replace("`", ""); - return new Field(name, alias); + if(tableAlias==null) return new Field(name, alias); + else if(tableAlias!=null){ + String aliasPrefix = tableAlias + "."; + if(name.startsWith(aliasPrefix)) + { + name = name.replaceFirst(aliasPrefix,""); + return new Field(name, alias); + } + } + return null; } else if (expr instanceof SQLQueryExpr) { throw new SqlParseException("unknow field name : " + expr); } else if (expr instanceof SQLAllColumnExpr) { diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index e421996a..194ceae9 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -1,15 +1,12 @@ package org.nlpcn.es4sql.parse; -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; +import java.util.*; import com.alibaba.druid.sql.ast.expr.*; import com.alibaba.druid.sql.ast.statement.*; import com.alibaba.druid.sql.ast.*; import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlSelectGroupByExpr; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; -import com.alibaba.druid.sql.parser.SQLStatementParser; import org.nlpcn.es4sql.domain.*; @@ -33,14 +30,14 @@ public Select parseSelect(SQLQueryExpr mySqlExpr) throws SqlParseException { MySqlSelectQueryBlock query = (MySqlSelectQueryBlock) mySqlExpr.getSubQuery().getQuery(); Select select = new Select(); - //TODO: JOIN: maby other select parse? we need to know which field to get from which index - findSelect(query, select); - //TODO: here we will know if this is join. + + findSelect(query, select,null); + select.getFrom().addAll(findFrom(query.getFrom())); select.setWhere(findWhere(query.getWhere())); - findLimit(query, select); + findLimit(query.getLimit(), select); findOrderBy(query, select); @@ -164,7 +161,9 @@ private Object parseValue(SQLExpr expr) throws SqlParseException { } else if (expr instanceof SQLNullExpr) { return null; } else if (expr instanceof SQLIdentifierExpr) { - return expr; + return expr; + } else if (expr instanceof SQLPropertyExpr){ + return expr; } else { throw new SqlParseException( String.format("Failed to parse SqlExpression of type %s. expression value: %s", expr.getClass(), expr) @@ -172,12 +171,14 @@ private Object parseValue(SQLExpr expr) throws SqlParseException { } } - private void findSelect(MySqlSelectQueryBlock query, Select select) throws SqlParseException { - List selectList = query.getSelectList(); - for (SQLSelectItem sqlSelectItem : selectList) { - select.addField(FieldMaker.makeField(sqlSelectItem.getExpr(), sqlSelectItem.getAlias())); - } - } + + private void findSelect(MySqlSelectQueryBlock query, Select select,String tableAlias) throws SqlParseException { + List selectList = query.getSelectList(); + for (SQLSelectItem sqlSelectItem : selectList) { + Field field = FieldMaker.makeField(sqlSelectItem.getExpr(), sqlSelectItem.getAlias(),tableAlias); + select.addField(field); + } + } private void findGroupBy(MySqlSelectQueryBlock query, Select select) throws SqlParseException { SQLSelectGroupByClause groupBy = query.getGroupBy(); @@ -222,12 +223,33 @@ private void findGroupBy(MySqlSelectQueryBlock query, Select select) throws SqlP private List convertExprsToFields(List exprs) throws SqlParseException { List fields = new ArrayList<>(exprs.size()); for (SQLExpr expr : exprs) { - fields.add(FieldMaker.makeField(expr, null)); + fields.add(FieldMaker.makeField(expr, null,null)); } return fields; } + private String sameAliasWhere(Where where, String... aliases) throws SqlParseException { + if(where instanceof Condition) + { + Condition condition = (Condition) where; + String fieldName = condition.getName(); + for (String alias : aliases){ + String prefix = alias + "."; + if(fieldName.startsWith(prefix)){ + return alias; + } + } + throw new SqlParseException(String.format("fieldName : %s on codition:%s does not contain alias", fieldName, condition.toString())); + } + List sameAliases = new ArrayList<>(); + for ( Where innerWhere : where.getWheres()) + sameAliases.add(sameAliasWhere(innerWhere, aliases)); + + if ( sameAliases.contains(null) ) return null; + if ( sameAliases.stream().distinct().count() != 1 ) return null; + return sameAliases.get(0); + } private void findOrderBy(MySqlSelectQueryBlock query, Select select) throws SqlParseException { SQLOrderBy orderBy = query.getOrderBy(); @@ -239,7 +261,7 @@ private void findOrderBy(MySqlSelectQueryBlock query, Select select) throws SqlP List lists = new ArrayList<>(); for (SQLSelectOrderByItem sqlSelectOrderByItem : items) { SQLExpr expr = sqlSelectOrderByItem.getExpr(); - lists.add(FieldMaker.makeField(expr, null).toString()); + lists.add(FieldMaker.makeField(expr, null,null).toString()); if (sqlSelectOrderByItem.getType() == null) { sqlSelectOrderByItem.setType(SQLOrderingSpecification.ASC); } @@ -253,8 +275,7 @@ private void findOrderBy(MySqlSelectQueryBlock query, Select select) throws SqlP } - private void findLimit(MySqlSelectQueryBlock query, Select select) { - MySqlSelectQueryBlock.Limit limit = query.getLimit(); + private void findLimit(MySqlSelectQueryBlock.Limit limit, Select select) { if (limit == null) { return; @@ -275,10 +296,12 @@ private List findFrom(SQLTableSource from) { boolean isSqlExprTable = from.getClass().isAssignableFrom(SQLExprTableSource.class); if(isSqlExprTable){ - String[] split = ((SQLExprTableSource) from).getExpr().toString().replaceAll(" ","").split(","); + SQLExprTableSource fromExpr = (SQLExprTableSource) from; + String[] split = fromExpr.getExpr().toString().replaceAll(" ","").split(","); + ArrayList fromList = new ArrayList<>(); for (String source : split) { - fromList.add(new From(source.trim())); + fromList.add(new From(source.trim(),fromExpr.getAlias())); } return fromList; } @@ -290,4 +313,146 @@ private List findFrom(SQLTableSource from) { return fromList; } + public JoinSelect parseJoinSelect(SQLQueryExpr sqlExpr) throws SqlParseException { + MySqlSelectQueryBlock query = (MySqlSelectQueryBlock) sqlExpr.getSubQuery().getQuery(); + + List joinedFrom = findJoinedFrom(query.getFrom()); + if(joinedFrom.size() != 2) + throw new RuntimeException("currently supports only 2 tables join"); + + Where where = findWhere(query.getWhere()); + String firstTableAlias = joinedFrom.get(0).getAlias(); + String secondTableAlias = joinedFrom.get(1).getAlias(); + + Map aliasToWhere = splitWheres(where, firstTableAlias, secondTableAlias); + + + JoinSelect joinSelect = new JoinSelect(); + + joinSelect.setT1Select(fillSelect(joinedFrom.get(0), aliasToWhere, query)); + joinSelect.setT2Select(fillSelect(joinedFrom.get(1), aliasToWhere, query)); + + List conditions = getJoinConditionsFlatten((SQLJoinTableSource) query.getFrom()); + joinSelect.setConnectedConditions(conditions); + + joinSelect.setT1ConnectedFields(getConnectedFields(conditions,firstTableAlias)); + joinSelect.setT2ConnectedFields(getConnectedFields(conditions, secondTableAlias)); + //todo: throw error feature not supported: no group bys on joins ? + + SQLJoinTableSource.JoinType joinType = ((SQLJoinTableSource) query.getFrom()).getJoinType(); + joinSelect.setJoinType(joinType); + + joinSelect.setT1OnlyFields(joinSelect.getT1Select().getFields()); + joinSelect.setT2OnlyFields(joinSelect.getT2Select().getFields()); + + return joinSelect; + } + + private List getConnectedFields(List conditions, String alias) throws SqlParseException { + List fields = new ArrayList<>(); + String prefix = alias + "."; + for(Condition condition : conditions) { + if(condition.getName().startsWith(prefix)){ + fields.add(new Field(condition.getName().replaceFirst(prefix,""),null)); + } + else { + if(! (condition.getValue() instanceof SQLPropertyExpr)){ + throw new SqlParseException("conditions on join should be one side is firstTable second Other , condition was:" + condition.toString()); + } + SQLPropertyExpr conditionValue = (SQLPropertyExpr) condition.getValue(); + SQLIdentifierExpr owner = (SQLIdentifierExpr) conditionValue.getOwner(); + if(owner.getName().equals(alias)) + fields.add(new Field(conditionValue.getName(),null)); + } + } + return fields; + } + + private Select fillSelect(From from, Map aliasToWhere, MySqlSelectQueryBlock query) throws SqlParseException { + Select select = new Select(); + select.getFrom().add(from); + findSelect(query, select,from.getAlias()); + findLimit(query.getLimit(),select); + select.setWhere(aliasToWhere.get(from.getAlias())); + return select; + } + + private List getJoinConditionsFlatten(SQLJoinTableSource from) throws SqlParseException { + List conditions = new ArrayList<>(); + Where where = Where.newInstance(); + parseWhere(from.getCondition(), where); + addIfConditionRecursive(where, conditions); + return conditions; + } + + + private Map splitWheres(Where where, String... aliases) throws SqlParseException { + Map aliasToWhere = new HashMap<>(); + for(String alias : aliases){ + aliasToWhere.put(alias,null); + } + + String allWhereFromSameAlias = sameAliasWhere(where, aliases); + if( allWhereFromSameAlias != null ) { + removeAliasPrefix(where,allWhereFromSameAlias); + aliasToWhere.put(allWhereFromSameAlias,where); + return aliasToWhere; + } + for(Where innerWhere : where.getWheres()){ + String sameAlias = sameAliasWhere(innerWhere, aliases); + if(sameAlias == null ) + throw new SqlParseException("Currently support only one hierarchy on different tables where"); + removeAliasPrefix(innerWhere,sameAlias); + Where aliasCurrentWhere = aliasToWhere.get(sameAlias); + if(aliasCurrentWhere == null) { + aliasToWhere.put(sameAlias, innerWhere); + } + else { + Where andWhereContainer = Where.newInstance(); + andWhereContainer.addWhere(aliasCurrentWhere); + andWhereContainer.addWhere(innerWhere); + aliasToWhere.put(sameAlias,andWhereContainer); + } + } + + return aliasToWhere; + } + + private void removeAliasPrefix(Where where, String alias) { + + if(where instanceof Condition) { + Condition cond = (Condition) where; + String fieldName = cond.getName(); + String aliasPrefix = alias + "."; + cond.setName(cond.getName().replaceFirst(aliasPrefix, "")); + return; + } + for(Where innerWhere : where.getWheres()) + { + removeAliasPrefix(innerWhere, alias); + } + } + + private void addIfConditionRecursive(Where where, List conditions) throws SqlParseException { + if(where instanceof Condition){ + Condition cond = (Condition) where; + if( ! (cond.getValue() instanceof SQLPropertyExpr)){ + throw new SqlParseException("conditions on join should be one side is firstTable second Other , condition was:" + cond.toString()); + } + conditions.add(cond); + } + for(Where innerWhere : where.getWheres()) + { + addIfConditionRecursive(innerWhere,conditions); + } + } + + private List findJoinedFrom(SQLTableSource from) { + SQLJoinTableSource joinTableSource = ((SQLJoinTableSource) from); + List fromList = new ArrayList<>(); + fromList.addAll(findFrom(joinTableSource.getLeft())); + fromList.addAll(findFrom(joinTableSource.getRight())); + return fromList; + } + } diff --git a/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java b/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java index c0110590..ba11eb9b 100644 --- a/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java +++ b/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java @@ -1,19 +1,16 @@ package org.nlpcn.es4sql.query; -import com.alibaba.druid.sql.SQLUtils; import com.alibaba.druid.sql.ast.SQLExpr; import com.alibaba.druid.sql.ast.expr.SQLQueryExpr; import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement; import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource; -import com.alibaba.druid.sql.ast.statement.SQLSelectQuery; -import com.alibaba.druid.sql.ast.statement.SQLTableSource; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; -import com.alibaba.druid.sql.dialect.mysql.parser.MySqlExprParser; import com.alibaba.druid.sql.parser.*; import com.alibaba.druid.util.JdbcUtils; import org.elasticsearch.client.Client; import org.nlpcn.es4sql.domain.Delete; +import org.nlpcn.es4sql.domain.JoinSelect; import org.nlpcn.es4sql.domain.Select; import org.nlpcn.es4sql.exception.SqlParseException; import org.nlpcn.es4sql.parse.ElasticSqlExprParser; @@ -32,20 +29,59 @@ public class ESActionFactory { */ public static QueryAction create(Client client, String sql) throws SqlParseException, SQLFeatureNotSupportedException { String firstWord = sql.substring(0, sql.indexOf(' ')); - //Join - //NestedLoops ? - // contains: - // s1 = select from SqlParser for one of them (need to take only first table wheres) - // c = conditions for crossed -> each condition is field = s2Field , value = s1Field - // s2 = select from SqlParser for the 2nd (need to take only 2nd table wheres) - // res1 = DefaultQueryAction(client, s1); - // for each res1 item => s2.conditions = union(s2.conditions,c(replace value to res1 value)) - switch (firstWord.toUpperCase()) { case "SELECT": SQLQueryExpr sqlExpr = (SQLQueryExpr) toSqlExpr(sql); if(isJoin(sqlExpr,sql)){ + JoinSelect joinSelect = new SqlParser().parseJoinSelect(sqlExpr); //NestedLoopQueryAction(client) + //Join between two tables : s1 and s2 + // Query contains: + // fields: s1 connected fields , s2 connected fields , s1 fields for query + // s1 = select from SqlParser for one of them (need to take only first table wheres) + // c = conditions for crossed -> each condition is field = s2Field , value = s1Field + // s2 = select from SqlParser for the 2nd (need to take only 2nd table wheres) + + //NestedLoopsStrategy + // choose arbitrary one of them + // res1 = DefaultQueryAction(client, s1 -> add connectedFields to selected fields); + // for each r1 : res1 + // duplicate s2 ; s2.conditions += c(replace value to r1 values) + // res2_r1 = DefaultQueryAction(client,s2) + // foreach r2: res2 + // results_set+= union(r2.fields,r1.only_return_fields) + + //HashJoinStrategy - only when all equals + //FirstStrategy + //Choose arbitrary one of them + //res1 = DefaultQueryAction(client, s1 -> add connectedFields to selected fields); + // create HashMap + // foreach r1 : res1 + // str1 = String.concat(connectedFields.getValues(r1)) + // add str1,r1 to hashMap + // res2 = DefaultQueryAction(client, s2 -> add connectedFields to selected fields) + //foreach r2 : res2 + // str2 = String.concat(connectedFields.getValues(r1)) + // if map.contains(str2) + // results_set += union(r2.only_return_fields , r1.only_return_fields) + + //SecondStrategy + //res1 = DefaultQueryAction(client, s1 -> add connectedFields to selected fields); + // create dict = HashMap + // create filters = HashMap> + // foreach r1 : res1 + // str1 = String.concat(connectedFields.getValues(r1)) + // add str1,r1 to hashMap + // foreach cond : c + //filters[c.field2Name]+=c.value(r,field1Name) + //foreach f : filters + // s2.addFilter terms/in Filter (f.value) + // res2 = DefaultQueryAction(client, s2 -> add connectedFields to selected fields) + //foreach r2 : res2 + // str2 = String.concat(connectedFields.getValues(r1)) + // if map.contains(str2) + // results_set += union(r2.only_return_fields , r1.only_return_fields) + return null; } else { diff --git a/src/test/java/org/nlpcn/es4sql/JoinTest.java b/src/test/java/org/nlpcn/es4sql/JoinTest.java deleted file mode 100644 index d06e0457..00000000 --- a/src/test/java/org/nlpcn/es4sql/JoinTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package org.nlpcn.es4sql; - -import org.elasticsearch.action.ActionRequestBuilder; -import org.elasticsearch.action.search.SearchRequestBuilder; -import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.search.SearchHits; -import org.junit.Assert; -import org.junit.Test; -import org.nlpcn.es4sql.exception.SqlParseException; -import org.nlpcn.es4sql.query.SqlElasticRequestBuilder; -import org.nlpcn.es4sql.query.SqlElasticSearchRequestBuilder; -import org.nlpcn.es4sql.query.explain.ExplainManager; - -import java.io.IOException; -import java.lang.reflect.InvocationTargetException; -import java.sql.SQLFeatureNotSupportedException; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.greaterThan; -import static org.nlpcn.es4sql.TestsConstants.TEST_INDEX; - -/** - * Created by Eliran on 15/8/2015. - */ -public class JoinTest { - @Test - public void leftJoinTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { - String query = "SELECT * FROM elasticsearch-sql_test_index/account LEFT JOIN elasticsearch-sql_test_index/dog on dog.holdersName = account.firstname"; - //SearchHits response = query(query); - // Assert.assertEquals(1004, response.getTotalHits()); - - String explain = explain(query); -// System.out.println("explain = " + explain); - - } - - @Test - public void straightJoinTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { - String query = "SELECT * FROM elasticsearch-sql_test_index/account JOIN elasticsearch-sql_test_index/dog on dog.holdersName = account.firstname"; - //SearchHits response = query(query); - // Assert.assertEquals(1004, response.getTotalHits()); - - String explain = explain(query); -// System.out.println("explain = " + explain); - - } - - private SearchHits query(String query) throws SqlParseException, SQLFeatureNotSupportedException, SQLFeatureNotSupportedException { - SearchDao searchDao = MainTestSuite.getSearchDao(); - SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) searchDao.explain(query); - return ((SearchResponse)select.get()).getHits(); - } - - private String explain(String sql) throws SQLFeatureNotSupportedException, SqlParseException, InvocationTargetException, NoSuchMethodException, IllegalAccessException, IOException { - SearchDao searchDao = MainTestSuite.getSearchDao(); - SqlElasticRequestBuilder requestBuilder = searchDao.explain(sql); - return requestBuilder.explain(); - } - -} diff --git a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java index d97a1337..a3125e71 100644 --- a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java +++ b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java @@ -27,8 +27,8 @@ BugTest.class, DeleteTest.class, ExplainTest.class, - WktToGeoJsonConverterTests.class -// JoinTest.class + WktToGeoJsonConverterTests.class, + SqlParserTests.class }) public class MainTestSuite { diff --git a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java new file mode 100644 index 00000000..9559a5e5 --- /dev/null +++ b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java @@ -0,0 +1,129 @@ +package org.nlpcn.es4sql; + +import com.alibaba.druid.sql.ast.SQLExpr; +import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; +import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr; +import com.alibaba.druid.sql.ast.expr.SQLQueryExpr; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.nlpcn.es4sql.domain.Condition; +import org.nlpcn.es4sql.domain.From; +import org.nlpcn.es4sql.domain.JoinSelect; +import org.nlpcn.es4sql.exception.SqlParseException; +import org.nlpcn.es4sql.parse.ElasticSqlExprParser; +import org.nlpcn.es4sql.parse.SqlParser; + +import java.util.List; + +/** + * Created by Eliran on 21/8/2015. + */ +public class SqlParserTests { + private static SqlParser parser; + + @BeforeClass + public static void init(){ + parser = new SqlParser(); + } + + @Test + public void joinParseFromsAreSplitedCorrectly() throws SqlParseException { + String query = "SELECT a.firstname ,a.lastname , a.gender , d.holdersName ,d.name FROM elasticsearch-sql_test_index/account a " + + "LEFT JOIN elasticsearch-sql_test_index/dog d on d.holdersName = a.firstname" + + " WHERE a.firstname = 'eliran' AND " + + " (a.age > 10 OR a.balance > 2000)" + + " AND d.age > 1"; + + JoinSelect joinSelect = parser.parseJoinSelect((SQLQueryExpr) queryToExpr(query)); + List t1From = joinSelect.getT1Select().getFrom(); + Assert.assertNotNull(t1From); + Assert.assertEquals(1,t1From.size()); + From t1 = t1From.get(0); + Assert.assertEquals("a",t1.getAlias()); + Assert.assertEquals("elasticsearch-sql_test_index",t1.getIndex()); + Assert.assertEquals("account",t1.getType()); + + List t2From = joinSelect.getT2Select().getFrom(); + Assert.assertNotNull(t2From); + Assert.assertEquals(1,t2From.size()); + From t2 = t2From.get(0); + Assert.assertEquals("d",t2.getAlias()); + Assert.assertEquals("elasticsearch-sql_test_index",t2.getIndex()); + Assert.assertEquals("dog",t2.getType()); + } + + @Test + public void joinParseConditionsTestOneCondition() throws SqlParseException { + String query = "SELECT a.*, a.firstname ,a.lastname , a.gender , d.holdersName ,d.name FROM elasticsearch-sql_test_index/account a " + + "LEFT JOIN elasticsearch-sql_test_index/dog d on d.holdersName = a.firstname" + + " WHERE a.firstname = 'eliran' AND " + + " (a.age > 10 OR a.balance > 2000)" + + " AND d.age > 1"; + + JoinSelect joinSelect = parser.parseJoinSelect((SQLQueryExpr) queryToExpr(query)); + List conditions = joinSelect.getConnectedConditions(); + Assert.assertNotNull(conditions); + Assert.assertEquals(1,conditions.size()); + Assert.assertTrue("condition not exist: d.holdersName = a.firstname",conditionExist(conditions, "d.holdersName", "a.firstname", Condition.OPEAR.EQ)); + } + + @Test + public void joinParseConditionsTestTwoConditions() throws SqlParseException { + String query = "SELECT a.*, a.firstname ,a.lastname , a.gender , d.holdersName ,d.name FROM elasticsearch-sql_test_index/account a " + + "LEFT JOIN elasticsearch-sql_test_index/dog d on d.holdersName = a.firstname " + + " AND d.age < a.age " + + " WHERE a.firstname = 'eliran' AND " + + " (a.age > 10 OR a.balance > 2000)" + + " AND d.age > 1"; + + JoinSelect joinSelect = parser.parseJoinSelect((SQLQueryExpr) queryToExpr(query)); + List conditions = joinSelect.getConnectedConditions(); + Assert.assertNotNull(conditions); + Assert.assertEquals(2,conditions.size()); + Assert.assertTrue("condition not exist: d.holdersName = a.firstname",conditionExist(conditions, "d.holdersName", "a.firstname",Condition.OPEAR.EQ)); + Assert.assertTrue("condition not exist: d.age < a.age",conditionExist(conditions, "d.age", "a.age", Condition.OPEAR.LT)); + } + + + @Test + public void joinSplitWhereCorrectly() throws SqlParseException { + String query = "SELECT a.*, a.firstname ,a.lastname , a.gender , d.holdersName ,d.name FROM elasticsearch-sql_test_index/account a " + + "LEFT JOIN elasticsearch-sql_test_index/dog d on d.holdersName = a.firstname" + + " WHERE a.firstname = 'eliran' AND " + + " (a.age > 10 OR a.balance > 2000)" + + " AND d.age > 1"; + + JoinSelect joinSelect = parser.parseJoinSelect((SQLQueryExpr) queryToExpr(query)); + String s1Where = joinSelect.getT1Select().getWhere().toString(); + Assert.assertEquals("AND ( AND firstname EQ eliran, AND ( OR age GT 10, OR balance GT 2000 ) ) " , s1Where); + String s2Where = joinSelect.getT2Select().getWhere().toString(); + Assert.assertEquals("AND age GT 1",s2Where); + } + + private SQLExpr queryToExpr(String query) { + return new ElasticSqlExprParser(query).expr(); + } + + private boolean conditionExist(List conditions, String from, String to, Condition.OPEAR opear) { + String[] aliasAndField = to.split("\\."); + String toAlias = aliasAndField[0]; + String toField = aliasAndField[1]; + for (Condition condition : conditions){ + if(condition.getOpear() != opear) continue; + + boolean fromIsEqual = condition.getName().equals(from); + if(!fromIsEqual) continue; + + SQLPropertyExpr value = (SQLPropertyExpr) condition.getValue(); + boolean toFieldNameIsEqual =value.getName().equals(toField); + boolean toAliasIsEqual = ((SQLIdentifierExpr) value.getOwner()).getName().equals(toAlias); + boolean toIsEqual = toAliasIsEqual && toFieldNameIsEqual; + + if(toIsEqual) return true; + } + return false; + } + + +} From 5c955e1f1f7b76a066935e3fdc2f1051fd558ad6 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sat, 22 Aug 2015 02:38:58 +0300 Subject: [PATCH 046/559] more sqlparser tests --- .../plugin/nlpcn/RestSqlAction.java | 2 +- .../org/nlpcn/es4sql/domain/JoinSelect.java | 26 +++---- .../org/nlpcn/es4sql/parse/SqlParser.java | 4 +- .../java/org/nlpcn/es4sql/ExplainTest.java | 2 +- .../java/org/nlpcn/es4sql/SqlParserTests.java | 74 ++++++++++++++++--- 5 files changed, 80 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java b/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java index 19a66a73..8907a3a6 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java @@ -8,7 +8,7 @@ import org.elasticsearch.rest.*; import org.nlpcn.es4sql.SearchDao; import org.nlpcn.es4sql.query.SqlElasticRequestBuilder; -import org.nlpcn.es4sql.query.explain.ExplainManager; + public class RestSqlAction extends BaseRestHandler { diff --git a/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java b/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java index 2a6d7aca..9762c308 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java +++ b/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java @@ -13,24 +13,24 @@ public class JoinSelect { //todo: make it an object private Select t1Select; private List t1ConnectedFields; - private List t1OnlyFields; + private List t1SelectedFields; private Select t2Select; private List t2ConnectedFields; - private List t2OnlyFields; + private List t2SelectedFields; private SQLJoinTableSource.JoinType joinType; public JoinSelect() { } - public JoinSelect(List connectedConditions, Select t1Select, List t1ConnectedFields, List t1OnlyFields, Select t2Select, List t2ConnectedFields, List t2OnlyFields,SQLJoinTableSource.JoinType joinType) { + public JoinSelect(List connectedConditions, Select t1Select, List t1ConnectedFields, List t1SelectedFields, Select t2Select, List t2ConnectedFields, List t2SelectedFields,SQLJoinTableSource.JoinType joinType) { this.connectedConditions = connectedConditions; this.t1Select = t1Select; this.t1ConnectedFields = t1ConnectedFields; - this.t1OnlyFields = t1OnlyFields; + this.t1SelectedFields = t1SelectedFields; this.t2Select = t2Select; this.t2ConnectedFields = t2ConnectedFields; - this.t2OnlyFields = t2OnlyFields; + this.t2SelectedFields = t2SelectedFields; this.joinType = joinType; } @@ -58,12 +58,12 @@ public void setT1ConnectedFields(List t1ConnectedFields) { this.t1ConnectedFields = t1ConnectedFields; } - public List getT1OnlyFields() { - return t1OnlyFields; + public List getT1SelectedFields() { + return t1SelectedFields; } - public void setT1OnlyFields(List t1OnlyFields) { - this.t1OnlyFields = t1OnlyFields; + public void setT1SelectedFields(List t1SelectedFields) { + this.t1SelectedFields = t1SelectedFields; } public Select getT2Select() { @@ -82,12 +82,12 @@ public void setT2ConnectedFields(List t2ConnectedFields) { this.t2ConnectedFields = t2ConnectedFields; } - public List getT2OnlyFields() { - return t2OnlyFields; + public List getT2SelectedFields() { + return t2SelectedFields; } - public void setT2OnlyFields(List t2OnlyFields) { - this.t2OnlyFields = t2OnlyFields; + public void setT2SelectedFields(List t2SelectedFields) { + this.t2SelectedFields = t2SelectedFields; } public SQLJoinTableSource.JoinType getJoinType() { diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index 194ceae9..e3c45eb4 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -342,8 +342,8 @@ public JoinSelect parseJoinSelect(SQLQueryExpr sqlExpr) throws SqlParseException SQLJoinTableSource.JoinType joinType = ((SQLJoinTableSource) query.getFrom()).getJoinType(); joinSelect.setJoinType(joinType); - joinSelect.setT1OnlyFields(joinSelect.getT1Select().getFields()); - joinSelect.setT2OnlyFields(joinSelect.getT2Select().getFields()); + joinSelect.setT1SelectedFields(joinSelect.getT1Select().getFields()); + joinSelect.setT2SelectedFields(joinSelect.getT2Select().getFields()); return joinSelect; } diff --git a/src/test/java/org/nlpcn/es4sql/ExplainTest.java b/src/test/java/org/nlpcn/es4sql/ExplainTest.java index 61037a86..f622a4fa 100644 --- a/src/test/java/org/nlpcn/es4sql/ExplainTest.java +++ b/src/test/java/org/nlpcn/es4sql/ExplainTest.java @@ -6,7 +6,7 @@ import org.junit.Test; import org.nlpcn.es4sql.exception.SqlParseException; import org.nlpcn.es4sql.query.SqlElasticRequestBuilder; -import org.nlpcn.es4sql.query.explain.ExplainManager; + import java.io.File; import java.io.IOException; diff --git a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java index 9559a5e5..fe9ef3ec 100644 --- a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java +++ b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java @@ -7,9 +7,7 @@ import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; -import org.nlpcn.es4sql.domain.Condition; -import org.nlpcn.es4sql.domain.From; -import org.nlpcn.es4sql.domain.JoinSelect; +import org.nlpcn.es4sql.domain.*; import org.nlpcn.es4sql.exception.SqlParseException; import org.nlpcn.es4sql.parse.ElasticSqlExprParser; import org.nlpcn.es4sql.parse.SqlParser; @@ -27,6 +25,60 @@ public static void init(){ parser = new SqlParser(); } + + @Test + public void joinParseCheckSelectedFieldsSplit() throws SqlParseException { + String query = "SELECT a.firstname ,a.lastname , a.gender , d.holdersName ,d.name FROM elasticsearch-sql_test_index/account a " + + "LEFT JOIN elasticsearch-sql_test_index/dog d on d.holdersName = a.firstname " + + " AND d.age < a.age " + + " WHERE a.firstname = 'eliran' AND " + + " (a.age > 10 OR a.balance > 2000)" + + " AND d.age > 1"; + + JoinSelect joinSelect = parser.parseJoinSelect((SQLQueryExpr) queryToExpr(query)); + + List t1Fields = joinSelect.getT1SelectedFields(); + Assert.assertEquals(t1Fields.size(),3); + Assert.assertTrue(fieldExist(t1Fields, "firstname")); + Assert.assertTrue(fieldExist(t1Fields, "lastname")); + Assert.assertTrue(fieldExist(t1Fields, "gender")); + + List t2Fields = joinSelect.getT2SelectedFields(); + Assert.assertEquals(t2Fields.size(),2); + Assert.assertTrue(fieldExist(t2Fields,"holdersName")); + Assert.assertTrue(fieldExist(t2Fields,"name")); + } + + @Test + public void joinParseCheckConnectedFields() throws SqlParseException { + String query = "SELECT a.firstname ,a.lastname , a.gender , d.holdersName ,d.name FROM elasticsearch-sql_test_index/account a " + + "LEFT JOIN elasticsearch-sql_test_index/dog d on d.holdersName = a.firstname " + + " AND d.age < a.age " + + " WHERE a.firstname = 'eliran' AND " + + " (a.age > 10 OR a.balance > 2000)" + + " AND d.age > 1"; + + JoinSelect joinSelect = parser.parseJoinSelect((SQLQueryExpr) queryToExpr(query)); + + List t1Fields = joinSelect.getT1ConnectedFields(); + Assert.assertEquals(t1Fields.size(),2); + Assert.assertTrue(fieldExist(t1Fields, "firstname")); + Assert.assertTrue(fieldExist(t1Fields, "age")); + + List t2Fields = joinSelect.getT2ConnectedFields(); + Assert.assertEquals(t2Fields.size(),2); + Assert.assertTrue(fieldExist(t2Fields,"holdersName")); + Assert.assertTrue(fieldExist(t2Fields,"age")); + } + + private boolean fieldExist(List fields, String fieldName) { + for(Field field : fields) + if(field.getName().equals(fieldName)) return true; + + return false; + } + + @Test public void joinParseFromsAreSplitedCorrectly() throws SqlParseException { String query = "SELECT a.firstname ,a.lastname , a.gender , d.holdersName ,d.name FROM elasticsearch-sql_test_index/account a " + @@ -37,20 +89,20 @@ public void joinParseFromsAreSplitedCorrectly() throws SqlParseException { JoinSelect joinSelect = parser.parseJoinSelect((SQLQueryExpr) queryToExpr(query)); List t1From = joinSelect.getT1Select().getFrom(); + Assert.assertNotNull(t1From); Assert.assertEquals(1,t1From.size()); - From t1 = t1From.get(0); - Assert.assertEquals("a",t1.getAlias()); - Assert.assertEquals("elasticsearch-sql_test_index",t1.getIndex()); - Assert.assertEquals("account",t1.getType()); + Assert.assertTrue(checkFrom(t1From.get(0),"elasticsearch-sql_test_index","account","a")); List t2From = joinSelect.getT2Select().getFrom(); Assert.assertNotNull(t2From); Assert.assertEquals(1,t2From.size()); - From t2 = t2From.get(0); - Assert.assertEquals("d",t2.getAlias()); - Assert.assertEquals("elasticsearch-sql_test_index",t2.getIndex()); - Assert.assertEquals("dog",t2.getType()); + Assert.assertTrue(checkFrom(t2From.get(0),"elasticsearch-sql_test_index","dog","d")); + } + + private boolean checkFrom(From from, String index, String type, String alias) { + return from.getAlias().equals(alias) && from.getIndex().equals(index) + && from.getType().equals(type); } @Test From 417456394fb9e73e3481907fd6aba05d89c801fc Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sat, 22 Aug 2015 23:11:02 +0300 Subject: [PATCH 047/559] hashjoin request builder done --- .../plugin/nlpcn/ActionRequestExecuter.java | 16 ++- .../plugin/nlpcn/RestSqlAction.java | 2 +- .../java/org/nlpcn/es4sql/domain/Field.java | 19 ++- .../org/nlpcn/es4sql/domain/JoinSelect.java | 22 ++- .../org/nlpcn/es4sql/parse/SqlParser.java | 7 +- .../es4sql/query/DefaultQueryAction.java | 4 +- .../nlpcn/es4sql/query/ESActionFactory.java | 2 +- .../es4sql/query/ESHashJoinQueryAction.java | 82 +++++++++++ .../query/HashJoinElasticRequestBuilder.java | 131 ++++++++++++++++++ src/test/java/org/nlpcn/es4sql/JoinTests.java | 30 ++++ .../java/org/nlpcn/es4sql/MainTestSuite.java | 3 +- .../java/org/nlpcn/es4sql/SqlParserTests.java | 2 + 12 files changed, 308 insertions(+), 12 deletions(-) create mode 100644 src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java create mode 100644 src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java create mode 100644 src/test/java/org/nlpcn/es4sql/JoinTests.java diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestExecuter.java b/src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestExecuter.java index 493ca426..9cdbb0de 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestExecuter.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestExecuter.java @@ -1,22 +1,25 @@ package org.elasticsearch.plugin.nlpcn; import org.elasticsearch.action.ActionRequest; +import org.elasticsearch.action.ActionRunnable; import org.elasticsearch.action.deletebyquery.DeleteByQueryRequest; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; import org.elasticsearch.rest.RestChannel; import org.elasticsearch.rest.action.support.RestStatusToXContentListener; +import org.nlpcn.es4sql.query.ESHashJoinQueryAction; +import org.nlpcn.es4sql.query.SqlElasticRequestBuilder; public class ActionRequestExecuter { private RestChannel channel; private Client client; - private ActionRequest request; + private SqlElasticRequestBuilder requestBuilder; - public ActionRequestExecuter(ActionRequest request, RestChannel channel, final Client client) { - this.request = request; + public ActionRequestExecuter(SqlElasticRequestBuilder requestBuilder, RestChannel channel, final Client client) { + this.requestBuilder = requestBuilder; this.channel = channel; this.client = client; } @@ -25,13 +28,18 @@ public ActionRequestExecuter(ActionRequest request, RestChannel channel, final C * Execute the ActionRequest and returns the REST response using the channel. */ public void execute() throws Exception { - request.listenerThreaded(false); + ActionRequest request = requestBuilder.request(); + request.listenerThreaded(false); if (request instanceof SearchRequest) { client.search((SearchRequest) request, new RestStatusToXContentListener(channel)); } else if (request instanceof DeleteByQueryRequest) { client.deleteByQuery((DeleteByQueryRequest) request, new DeleteByQueryRestListener(channel)); } + //todo: join + /*else if (request instanceof ){ + + }*/ else { throw new Exception(String.format("Unsupported ActionRequest provided: %s", request.getClass().getName())); } diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java b/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java index 8907a3a6..a1057be5 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java @@ -39,7 +39,7 @@ protected void handleRequest(RestRequest request, RestChannel channel, final Cli BytesRestResponse bytesRestResponse = new BytesRestResponse(RestStatus.OK, jsonExplanation); channel.sendResponse(bytesRestResponse); } else { - new ActionRequestExecuter(actionRequest, channel, client).execute(); + new ActionRequestExecuter(actionRequestBuilder, channel, client).execute(); } } } \ No newline at end of file diff --git a/src/main/java/org/nlpcn/es4sql/domain/Field.java b/src/main/java/org/nlpcn/es4sql/domain/Field.java index fc333849..37b25713 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/Field.java +++ b/src/main/java/org/nlpcn/es4sql/domain/Field.java @@ -6,7 +6,7 @@ * @author ansj * */ -public class Field { +public class Field implements Cloneable{ protected String name; private String alias; @@ -36,4 +36,21 @@ public void setAlias(String alias) { public String toString() { return this.name; } + + @Override + public boolean equals(Object obj) { + if(obj == null) return false; + if(obj.getClass() != this.getClass()) return false; + Field other = (Field) obj; + boolean namesAreEqual = (other.getName() == null && this.name == null ) + || other.getName().equals(this.name) ; + if(!namesAreEqual) return false; + return (other.getAlias() == null && this.alias == null ) + || other.getAlias().equals(this.alias) ; + } + + @Override + protected Object clone() throws CloneNotSupportedException { + return new Field(new String(this.name),new String(this.alias)); + } } diff --git a/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java b/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java index 9762c308..111dd818 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java +++ b/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java @@ -14,21 +14,25 @@ public class JoinSelect { private Select t1Select; private List t1ConnectedFields; private List t1SelectedFields; + private String t1Alias; private Select t2Select; private List t2ConnectedFields; private List t2SelectedFields; private SQLJoinTableSource.JoinType joinType; + private String t2Alias; public JoinSelect() { } - public JoinSelect(List connectedConditions, Select t1Select, List t1ConnectedFields, List t1SelectedFields, Select t2Select, List t2ConnectedFields, List t2SelectedFields,SQLJoinTableSource.JoinType joinType) { + public JoinSelect(List connectedConditions, Select t1Select,String t1Alias, List t1ConnectedFields, List t1SelectedFields, Select t2Select, String t2Alias, List t2ConnectedFields, List t2SelectedFields,SQLJoinTableSource.JoinType joinType) { this.connectedConditions = connectedConditions; this.t1Select = t1Select; + this.t1Alias = t1Alias; this.t1ConnectedFields = t1ConnectedFields; this.t1SelectedFields = t1SelectedFields; this.t2Select = t2Select; + this.t2Alias = t2Alias; this.t2ConnectedFields = t2ConnectedFields; this.t2SelectedFields = t2SelectedFields; this.joinType = joinType; @@ -97,4 +101,20 @@ public SQLJoinTableSource.JoinType getJoinType() { public void setJoinType(SQLJoinTableSource.JoinType joinType) { this.joinType = joinType; } + + public String getT1Alias() { + return t1Alias; + } + + public void setT1Alias(String t1Alias) { + this.t1Alias = t1Alias; + } + + public String getT2Alias() { + return t2Alias; + } + + public void setT2Alias(String t2Alias) { + this.t2Alias = t2Alias; + } } diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index e3c45eb4..ce970987 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -342,8 +342,11 @@ public JoinSelect parseJoinSelect(SQLQueryExpr sqlExpr) throws SqlParseException SQLJoinTableSource.JoinType joinType = ((SQLJoinTableSource) query.getFrom()).getJoinType(); joinSelect.setJoinType(joinType); - joinSelect.setT1SelectedFields(joinSelect.getT1Select().getFields()); - joinSelect.setT2SelectedFields(joinSelect.getT2Select().getFields()); + joinSelect.setT1SelectedFields(new ArrayList(joinSelect.getT1Select().getFields())); + joinSelect.setT2SelectedFields(new ArrayList(joinSelect.getT2Select().getFields())); + + joinSelect.setT1Alias(firstTableAlias); + joinSelect.setT2Alias(secondTableAlias); return joinSelect; } diff --git a/src/main/java/org/nlpcn/es4sql/query/DefaultQueryAction.java b/src/main/java/org/nlpcn/es4sql/query/DefaultQueryAction.java index 85956b12..51393f87 100644 --- a/src/main/java/org/nlpcn/es4sql/query/DefaultQueryAction.java +++ b/src/main/java/org/nlpcn/es4sql/query/DefaultQueryAction.java @@ -124,5 +124,7 @@ private void setLimit(int from, int size) { } } - + public SearchRequestBuilder getRequestBuilder() { + return request; + } } diff --git a/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java b/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java index ba11eb9b..c819d14e 100644 --- a/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java +++ b/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java @@ -34,6 +34,7 @@ public static QueryAction create(Client client, String sql) throws SqlParseExcep SQLQueryExpr sqlExpr = (SQLQueryExpr) toSqlExpr(sql); if(isJoin(sqlExpr,sql)){ JoinSelect joinSelect = new SqlParser().parseJoinSelect(sqlExpr); + return new ESHashJoinQueryAction(client,joinSelect); //NestedLoopQueryAction(client) //Join between two tables : s1 and s2 // Query contains: @@ -82,7 +83,6 @@ public static QueryAction create(Client client, String sql) throws SqlParseExcep // if map.contains(str2) // results_set += union(r2.only_return_fields , r1.only_return_fields) - return null; } else { Select select = new SqlParser().parseSelect(sqlExpr); diff --git a/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java b/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java new file mode 100644 index 00000000..7dc00a90 --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java @@ -0,0 +1,82 @@ +package org.nlpcn.es4sql.query; + +import org.elasticsearch.client.Client; +import org.nlpcn.es4sql.domain.*; +import org.nlpcn.es4sql.exception.SqlParseException; + +import java.util.*; + +/** + * Created by Eliran on 22/8/2015. + */ +public class ESHashJoinQueryAction extends QueryAction { + + private JoinSelect joinSelect; + + public ESHashJoinQueryAction(Client client,JoinSelect joinSelect) { + super(client, null); + this.joinSelect = joinSelect; + } + + @Override + public SqlElasticRequestBuilder explain() throws SqlParseException { + //TODO: on map entry change names to t1ToT2 and make sure of this order here (based on aliases) + HashJoinElasticRequestBuilder hashRequest = new HashJoinElasticRequestBuilder(); + + Select t1Select = joinSelect.getT1Select(); + List t1ConnectedFields = joinSelect.getT1ConnectedFields(); + addFieldsToSelectIfMissing(t1Select,t1ConnectedFields); + DefaultQueryAction t1QueryAction = new DefaultQueryAction(client,t1Select); + t1QueryAction.explain(); + hashRequest.setFirstTableRequest(t1QueryAction.getRequestBuilder()); + + Select t2Select = joinSelect.getT2Select(); + List t2ConnectedFields = joinSelect.getT2ConnectedFields(); + addFieldsToSelectIfMissing(t2Select,t2ConnectedFields); + DefaultQueryAction t2QueryAction = new DefaultQueryAction(client,t2Select); + t2QueryAction.explain(); + hashRequest.setSecondTableRequest(t2QueryAction.getRequestBuilder()); + + + hashRequest.setFirstTableReturnedField(joinSelect.getT1SelectedFields()); + hashRequest.setSecondTableReturnedField(joinSelect.getT2SelectedFields()); + + String t1Alias = joinSelect.getT1Alias(); + String t2Alias = joinSelect.getT2Alias(); + List> comparisonFields = new ArrayList<>(); + for(Condition condition : joinSelect.getConnectedConditions()){ + String firstField = condition.getName(); + String secondField = condition.getValue().toString(); + Field t1Field,t2Field; + if(firstField.startsWith(t1Alias)){ + t1Field = new Field(removeAlias(firstField,t1Alias),null); + t2Field = new Field(removeAlias(secondField,t2Alias),null); + } + else { + t1Field = new Field(removeAlias(secondField,t1Alias),null); + t2Field = new Field(removeAlias(firstField,t2Alias),null); + } + comparisonFields.add(new AbstractMap.SimpleEntry(t1Field, t2Field)); + } + hashRequest.setT1ToT2FieldsComparison(comparisonFields); + + return hashRequest; + } + + private String removeAlias(String field, String alias) { + return field.replace(alias+".",""); + } + + private void addFieldsToSelectIfMissing(Select select, List fields) { + //this means all fields + if(select.getFields() == null || select.getFields().size() == 0) return; + + List selectedFields = select.getFields(); + for(Field field : fields){ + if(!selectedFields.contains(field)){ + selectedFields.add(field); + } + } + + } +} diff --git a/src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java new file mode 100644 index 00000000..5df4548d --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java @@ -0,0 +1,131 @@ +package org.nlpcn.es4sql.query; + +import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource; +import org.elasticsearch.action.ActionRequest; +import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.action.search.MultiSearchRequest; +import org.elasticsearch.action.search.SearchRequest; +import org.elasticsearch.action.search.SearchRequestBuilder; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentType; +import org.nlpcn.es4sql.domain.Field; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +/** + * Created by Eliran on 22/8/2015. + */ +public class HashJoinElasticRequestBuilder implements SqlElasticRequestBuilder{ + + private SearchRequestBuilder firstTableRequest; + private SearchRequestBuilder secondTableRequest; + private MultiSearchRequest multi; + private List firstTableReturnedField; + private List secondTableReturnedField; + private List> t1ToT2FieldsComparison; + private SQLJoinTableSource.JoinType joinType; + + public HashJoinElasticRequestBuilder() { + } + + + @Override + public ActionRequest request() { + //todo: it should recieve client + if(multi == null) + buildMulti(); + return multi; + + } + + private void buildMulti() { + multi = new MultiSearchRequest(); + multi.add(firstTableRequest); + multi.add(secondTableRequest); + multi.listenerThreaded(false); + } + + @Override + public String explain() { + try { + XContentBuilder firstBuilder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint(); + firstTableRequest.internalBuilder().toXContent(firstBuilder, ToXContent.EMPTY_PARAMS); + + XContentBuilder secondBuilder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint(); + secondTableRequest.internalBuilder().toXContent(secondBuilder, ToXContent.EMPTY_PARAMS); + String explained = String.format("HashJoin. first query:\n%s\n second query:\n%s", firstBuilder.string(), secondBuilder.string()); + + return explained; + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + @Override + public ActionResponse get() { + return null; + } + + + public SearchRequestBuilder getFirstTableRequest() { + return firstTableRequest; + } + + public void setFirstTableRequest(SearchRequestBuilder firstTableRequest) { + this.firstTableRequest = firstTableRequest; + } + + public SearchRequestBuilder getSecondTableRequest() { + return secondTableRequest; + } + + public void setSecondTableRequest(SearchRequestBuilder secondTableRequest) { + this.secondTableRequest = secondTableRequest; + } + + public MultiSearchRequest getMulti() { + return multi; + } + + public void setMulti(MultiSearchRequest multi) { + this.multi = multi; + } + + public List getFirstTableReturnedField() { + return firstTableReturnedField; + } + + public void setFirstTableReturnedField(List firstTableReturnedField) { + this.firstTableReturnedField = firstTableReturnedField; + } + + public List getSecondTableReturnedField() { + return secondTableReturnedField; + } + + public void setSecondTableReturnedField(List secondTableReturnedField) { + this.secondTableReturnedField = secondTableReturnedField; + } + + public List> getT1ToT2FieldsComparison() { + return t1ToT2FieldsComparison; + } + + public void setT1ToT2FieldsComparison(List> t1ToT2FieldsComparison) { + this.t1ToT2FieldsComparison = t1ToT2FieldsComparison; + } + + public SQLJoinTableSource.JoinType getJoinType() { + return joinType; + } + + public void setJoinType(SQLJoinTableSource.JoinType joinType) { + this.joinType = joinType; + } + +} diff --git a/src/test/java/org/nlpcn/es4sql/JoinTests.java b/src/test/java/org/nlpcn/es4sql/JoinTests.java new file mode 100644 index 00000000..1cdcd2e7 --- /dev/null +++ b/src/test/java/org/nlpcn/es4sql/JoinTests.java @@ -0,0 +1,30 @@ +package org.nlpcn.es4sql; + +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.search.SearchHits; +import org.junit.Test; +import org.nlpcn.es4sql.exception.SqlParseException; +import org.nlpcn.es4sql.query.SqlElasticRequestBuilder; +import org.nlpcn.es4sql.query.SqlElasticSearchRequestBuilder; + +import java.sql.SQLFeatureNotSupportedException; + +/** + * Created by Eliran on 22/8/2015. + */ +public class JoinTests { + + + @Test + public void joinParseCheckSelectedFieldsSplit() throws SqlParseException, SQLFeatureNotSupportedException { + String query = "SELECT a.firstname ,a.lastname , a.gender , d.holdersName ,d.name FROM elasticsearch-sql_test_index/account a " + + "LEFT JOIN elasticsearch-sql_test_index/dog d on d.holdersName = a.firstname " + + " AND d.age < a.age " + + " WHERE a.firstname = 'eliran' AND " + + " (a.age > 10 OR a.balance > 2000)" + + " AND d.age > 1"; + SearchDao searchDao = MainTestSuite.getSearchDao(); + SqlElasticRequestBuilder explain = searchDao.explain(query); + } + +} diff --git a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java index a3125e71..44d8a092 100644 --- a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java +++ b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java @@ -28,7 +28,8 @@ DeleteTest.class, ExplainTest.class, WktToGeoJsonConverterTests.class, - SqlParserTests.class + SqlParserTests.class, + //JoinTests.class }) public class MainTestSuite { diff --git a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java index fe9ef3ec..5f1c6f2b 100644 --- a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java +++ b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java @@ -11,6 +11,7 @@ import org.nlpcn.es4sql.exception.SqlParseException; import org.nlpcn.es4sql.parse.ElasticSqlExprParser; import org.nlpcn.es4sql.parse.SqlParser; +import org.nlpcn.es4sql.query.ESHashJoinQueryAction; import java.util.List; @@ -26,6 +27,7 @@ public static void init(){ } + @Test public void joinParseCheckSelectedFieldsSplit() throws SqlParseException { String query = "SELECT a.firstname ,a.lastname , a.gender , d.holdersName ,d.name FROM elasticsearch-sql_test_index/account a " + From 02e699cbdcaa26bde451276f46fef5a890493033 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sun, 23 Aug 2015 22:34:04 +0300 Subject: [PATCH 048/559] first hashJoin implementation (working! need to refactor) --- .../plugin/nlpcn/ActionRequestExecuter.java | 14 +- .../plugin/nlpcn/HashJoinElasticExecutor.java | 143 ++++++++++++++++++ src/main/java/org/nlpcn/es4sql/SearchDao.java | 5 +- .../es4sql/query/ESHashJoinQueryAction.java | 5 + .../query/HashJoinElasticRequestBuilder.java | 1 - src/test/java/org/nlpcn/es4sql/JoinTests.java | 46 +++++- .../java/org/nlpcn/es4sql/MainTestSuite.java | 3 +- src/test/java/org/nlpcn/es4sql/QueryTest.java | 2 +- src/test/resources/dogs.json | 2 +- src/test/resources/peoples.json | 24 +++ 10 files changed, 232 insertions(+), 13 deletions(-) create mode 100644 src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java create mode 100644 src/test/resources/peoples.json diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestExecuter.java b/src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestExecuter.java index 9cdbb0de..fc7270b1 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestExecuter.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestExecuter.java @@ -9,6 +9,7 @@ import org.elasticsearch.rest.RestChannel; import org.elasticsearch.rest.action.support.RestStatusToXContentListener; import org.nlpcn.es4sql.query.ESHashJoinQueryAction; +import org.nlpcn.es4sql.query.HashJoinElasticRequestBuilder; import org.nlpcn.es4sql.query.SqlElasticRequestBuilder; @@ -31,15 +32,20 @@ public void execute() throws Exception { ActionRequest request = requestBuilder.request(); request.listenerThreaded(false); - if (request instanceof SearchRequest) { + //todo: maby change to instanceof multi? + if(requestBuilder instanceof HashJoinElasticRequestBuilder){ + HashJoinElasticRequestBuilder hashJoin = (HashJoinElasticRequestBuilder) requestBuilder; + HashJoinElasticExecutor executor = new HashJoinElasticExecutor(client,hashJoin); + executor.run(); + executor.sendResponse(channel); + } + else if (request instanceof SearchRequest) { client.search((SearchRequest) request, new RestStatusToXContentListener(channel)); } else if (request instanceof DeleteByQueryRequest) { client.deleteByQuery((DeleteByQueryRequest) request, new DeleteByQueryRestListener(channel)); } - //todo: join - /*else if (request instanceof ){ - }*/ + else { throw new Exception(String.format("Unsupported ActionRequest provided: %s", request.getClass().getName())); } diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java new file mode 100644 index 00000000..c3daf952 --- /dev/null +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java @@ -0,0 +1,143 @@ +package org.elasticsearch.plugin.nlpcn; + +import org.elasticsearch.action.search.SearchRequest; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.client.Client; +import org.elasticsearch.common.Base64; +import org.elasticsearch.common.io.stream.BytesStreamInput; +import org.elasticsearch.common.text.StringText; +import org.elasticsearch.common.text.Text; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.rest.BytesRestResponse; +import org.elasticsearch.rest.RestChannel; +import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.rest.action.support.RestStatusToXContentListener; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchHitField; +import org.elasticsearch.search.SearchHits; +import org.elasticsearch.search.internal.InternalSearchHit; +import org.elasticsearch.search.internal.InternalSearchHitField; +import org.elasticsearch.search.internal.InternalSearchHits; +import org.nlpcn.es4sql.domain.Field; +import org.nlpcn.es4sql.query.HashJoinElasticRequestBuilder; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by Eliran on 22/8/2015. + */ +public class HashJoinElasticExecutor { + private HashJoinElasticRequestBuilder requestBuilder; + private SearchHits results ; + private Client client; + + public HashJoinElasticExecutor(Client client,HashJoinElasticRequestBuilder requestBuilder) { + this.client = client; + this.requestBuilder = requestBuilder; + } + + public SearchHits getHits(){ + return results; + } + public void sendResponse(RestChannel channel){ + try { + XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint(); + results.toXContent(builder, ToXContent.EMPTY_PARAMS); + String json = builder.string(); + BytesRestResponse bytesRestResponse = new BytesRestResponse(RestStatus.OK, json); + channel.sendResponse(bytesRestResponse); + } catch (IOException e) { + e.printStackTrace(); + } + + } + public void run() throws IOException { + SearchHits firstTableHits = requestBuilder.getFirstTableRequest().get().getHits(); + Map> comparisonKeyToSearchHits = new HashMap<>(); + List> t1ToT2FieldsComparison = requestBuilder.getT1ToT2FieldsComparison(); + int ids = 1; + for(SearchHit hit : firstTableHits){ + String key = ""; + Map sourceAsMap = hit.sourceAsMap(); + for(Map.Entry t1ToT2 : t1ToT2FieldsComparison){ + //todo: change to our function find if key contains '.' + String data = sourceAsMap.get(t1ToT2.getKey().getName()).toString(); + if(data == null) + key+="|null|"; + else + key+="|"+data+"|"; + } + List currentSearchHits = comparisonKeyToSearchHits.get(key); + if(currentSearchHits == null) { + currentSearchHits = new ArrayList<>(); + comparisonKeyToSearchHits.put(key,currentSearchHits); + } + //int docid , id + //InternalSearchHit(int docId, String id, Text type, Map fields) + + InternalSearchHit searchHit = new InternalSearchHit(ids, hit.id(), new StringText(hit.getType()), hit.getFields()); + searchHit.sourceRef(hit.getSourceRef()); + + + onlyReturnedFields(searchHit.sourceAsMap(), requestBuilder.getFirstTableReturnedField()); + ids++; + currentSearchHits.add(searchHit); + } + + + ids = 0; + List finalResult = new ArrayList<>(); + SearchHits secondTableHits = requestBuilder.getSecondTableRequest().get().getHits(); + for(SearchHit secondTableHit : secondTableHits){ + + String key = ""; + Map sourceAsMap = secondTableHit.sourceAsMap(); + for(Map.Entry t1ToT2 : t1ToT2FieldsComparison){ + //todo: change to our function find if key contains '.' + String data = sourceAsMap.get(t1ToT2.getValue().getName()).toString(); + if(data == null) + key+="|null|"; + else + key+="|"+data+"|"; + } + + List searchHits = comparisonKeyToSearchHits.get(key); + //TODO decide what to do according to left join. now assume regular join. + if(searchHits!=null && searchHits.size() > 0){ + for(InternalSearchHit matchingHit : searchHits){ + onlyReturnedFields(secondTableHit.sourceAsMap(), requestBuilder.getSecondTableReturnedField()); + + //todo: decide which id to put or type. or maby its ok this way. just need to doc. + InternalSearchHit searchHit = new InternalSearchHit(ids, matchingHit.id() + "|" + secondTableHit.getId(), new StringText(matchingHit.getType() + "|" + secondTableHit.getType()), matchingHit.getFields()); + searchHit.sourceRef(matchingHit.getSourceRef()); + searchHit.getSource().putAll(secondTableHit.getSource()); + + + finalResult.add(searchHit); + ids++; + } + } + } + + InternalSearchHit[] hits = finalResult.toArray(new InternalSearchHit[ids]); + this.results = new InternalSearchHits(hits,ids,1.0f); + + } + + private void onlyReturnedFields(Map fieldsMap, List required) { + for(String key : fieldsMap.keySet()){ + //todo: alias? recursiveFind ? give map instead to better performance? + if(!required.contains(new Field(key,null))){ + fieldsMap.remove(key); + } + } + + } +} diff --git a/src/main/java/org/nlpcn/es4sql/SearchDao.java b/src/main/java/org/nlpcn/es4sql/SearchDao.java index d8f5cf5c..e0eef34c 100644 --- a/src/main/java/org/nlpcn/es4sql/SearchDao.java +++ b/src/main/java/org/nlpcn/es4sql/SearchDao.java @@ -31,8 +31,11 @@ public SearchDao(Client client) { this.client = client; } + public Client getClient() { + return client; + } - /** + /** * Prepare action And transform sql * into ES ActionRequest * @param sql SQL query to execute. diff --git a/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java b/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java index 7dc00a90..5563589f 100644 --- a/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java +++ b/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java @@ -45,6 +45,11 @@ public SqlElasticRequestBuilder explain() throws SqlParseException { String t2Alias = joinSelect.getT2Alias(); List> comparisonFields = new ArrayList<>(); for(Condition condition : joinSelect.getConnectedConditions()){ + + if(condition.getOpear() != Condition.OPEAR.EQ){ + throw new SqlParseException(String.format("HashJoin should only be with EQ conditions, got:%s on condition:%s", condition.getOpear().name(), condition.toString())); + } + String firstField = condition.getName(); String secondField = condition.getValue().toString(); Field t1Field,t2Field; diff --git a/src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java index 5df4548d..2277ab8e 100644 --- a/src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java +++ b/src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java @@ -35,7 +35,6 @@ public HashJoinElasticRequestBuilder() { @Override public ActionRequest request() { - //todo: it should recieve client if(multi == null) buildMulti(); return multi; diff --git a/src/test/java/org/nlpcn/es4sql/JoinTests.java b/src/test/java/org/nlpcn/es4sql/JoinTests.java index 1cdcd2e7..9b9f4875 100644 --- a/src/test/java/org/nlpcn/es4sql/JoinTests.java +++ b/src/test/java/org/nlpcn/es4sql/JoinTests.java @@ -1,13 +1,21 @@ package org.nlpcn.es4sql; + import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.plugin.nlpcn.HashJoinElasticExecutor; +import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.junit.Test; +import org.junit.Assert; import org.nlpcn.es4sql.exception.SqlParseException; +import org.nlpcn.es4sql.query.HashJoinElasticRequestBuilder; import org.nlpcn.es4sql.query.SqlElasticRequestBuilder; import org.nlpcn.es4sql.query.SqlElasticSearchRequestBuilder; +import java.io.IOException; import java.sql.SQLFeatureNotSupportedException; +import java.util.HashMap; +import java.util.Map; /** * Created by Eliran on 22/8/2015. @@ -16,15 +24,45 @@ public class JoinTests { @Test - public void joinParseCheckSelectedFieldsSplit() throws SqlParseException, SQLFeatureNotSupportedException { - String query = "SELECT a.firstname ,a.lastname , a.gender , d.holdersName ,d.name FROM elasticsearch-sql_test_index/account a " + + public void joinParseCheckSelectedFieldsSplit() throws SqlParseException, SQLFeatureNotSupportedException, IOException { + String query = "SELECT a.firstname ,a.lastname , a.gender ,d.name FROM elasticsearch-sql_test_index/people a " + "LEFT JOIN elasticsearch-sql_test_index/dog d on d.holdersName = a.firstname " + - " AND d.age < a.age " + - " WHERE a.firstname = 'eliran' AND " + + " WHERE " + " (a.age > 10 OR a.balance > 2000)" + " AND d.age > 1"; SearchDao searchDao = MainTestSuite.getSearchDao(); SqlElasticRequestBuilder explain = searchDao.explain(query); + HashJoinElasticExecutor executor = new HashJoinElasticExecutor(searchDao.getClient(), (HashJoinElasticRequestBuilder) explain); + executor.run(); + SearchHit[] hits = executor.getHits().getHits(); + Assert.assertEquals(2,hits.length); + Map oneMatch = new HashMap<>(); + oneMatch.put("firstname","Daenerys"); + oneMatch.put("lastname","Targaryen"); + oneMatch.put("gender","M"); + oneMatch.put("name", "rex"); + Map secondMatch = new HashMap<>(); + secondMatch.put("firstname","Hattie"); + secondMatch.put("lastname","Bond"); + secondMatch.put("gender","M"); + secondMatch.put("name","snoopy"); + + Assert.assertTrue(hitsContains(hits, oneMatch)); + Assert.assertTrue(hitsContains(hits,secondMatch)); + + } + + private boolean hitsContains(SearchHit[] hits, Map matchMap) { + + for(SearchHit hit : hits){ + Map hitMap = hit.sourceAsMap(); + for(Map.Entry entry: hitMap.entrySet()){ + if(!matchMap.containsKey(entry.getKey())) continue;; + if(!matchMap.get(entry.getKey()).equals(entry.getValue())) continue; + } + return true; + } + return false; } } diff --git a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java index 44d8a092..b95bb059 100644 --- a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java +++ b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java @@ -29,7 +29,7 @@ ExplainTest.class, WktToGeoJsonConverterTests.class, SqlParserTests.class, - //JoinTests.class + JoinTests.class }) public class MainTestSuite { @@ -51,6 +51,7 @@ public static void setUp() throws Exception { loadBulk("src/test/resources/online.json"); loadBulk("src/test/resources/phrases.json"); loadBulk("src/test/resources/dogs.json"); + loadBulk("src/test/resources/peoples.json"); prepareOdbcIndex(); loadBulk("src/test/resources/odbc-date-formats.json"); diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java index 87e8fe89..7fa7d24f 100644 --- a/src/test/java/org/nlpcn/es4sql/QueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java @@ -174,7 +174,7 @@ public void lessThanOrEqualTest() throws IOException, SqlParseException, SQLFeat @Test public void orTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { - SearchHits response = query(String.format("SELECT * FROM %s WHERE gender='F' OR gender='M' LIMIT 1000", TEST_INDEX)); + SearchHits response = query(String.format("SELECT * FROM %s/account WHERE gender='F' OR gender='M' LIMIT 1000", TEST_INDEX)); // Assert all documents from accounts.json is returned. Assert.assertEquals(1000, response.getTotalHits()); } diff --git a/src/test/resources/dogs.json b/src/test/resources/dogs.json index 744bb6aa..de9f3ef8 100644 --- a/src/test/resources/dogs.json +++ b/src/test/resources/dogs.json @@ -1,4 +1,4 @@ {"index":{"_type": "dog", "_id":"1"}} -{"name":"rex","holdersName":"Amber","age":2} +{"name":"rex","holdersName":"Daenerys","age":2} {"index":{"_type": "dog", "_id":"6"}} {"name":"snoopy","holdersName":"Hattie","age":4} diff --git a/src/test/resources/peoples.json b/src/test/resources/peoples.json new file mode 100644 index 00000000..77c34593 --- /dev/null +++ b/src/test/resources/peoples.json @@ -0,0 +1,24 @@ +{"index":{"_type": "people", "_id":"1"}} +{"account_number":1,"balance":39225,"firstname":"Daenerys","lastname":"Targaryen","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"} +{"index":{"_type": "people", "_id":"6"}} +{"account_number":6,"balance":5686,"firstname":"Hattie","lastname":"Bond","age":36,"gender":"M","address":"671 Bristol Street","employer":"Netagy","email":"hattiebond@netagy.com","city":"Dante","state":"TN"} +{"index":{"_type": "people", "_id":"13"}} +{"account_number":13,"balance":32838,"firstname":"Nanette","lastname":"Bates","age":28,"gender":"F","address":"789 Madison Street","employer":"Quility","email":"nanettebates@quility.com","city":"Nogal","state":"VA"} +{"index":{"_type": "people", "_id":"18"}} +{"account_number":18,"balance":4180,"firstname":"Dale","lastname":"Adams","age":33,"gender":"M","address":"467 Hutchinson Court","employer":"Boink","email":"daleadams@boink.com","city":"Orick","state":"MD"} +{"index":{"_type": "people", "_id":"20"}} +{"account_number":20,"balance":16418,"firstname":"Elinor","lastname":"Ratliff","age":36,"gender":"M","address":"282 Kings Place","employer":"Scentric","email":"elinorratliff@scentric.com","city":"Ribera","state":"WA"} +{"index":{"_type": "people", "_id":"25"}} +{"account_number":25,"balance":40540,"firstname":"Virginia","lastname":"Ayala","age":39,"gender":"F","address":"171 Putnam Avenue","employer":"Filodyne","email":"virginiaayala@filodyne.com","city":"Nicholson","state":"PA"} +{"index":{"_type": "people", "_id":"32"}} +{"account_number":32,"balance":48086,"firstname":"Dillard","lastname":"Mcpherson","age":34,"gender":"F","address":"702 Quentin Street","employer":"Quailcom","email":"dillardmcpherson@quailcom.com","city":"Veguita","state":"IN"} +{"index":{"_type": "people", "_id":"37"}} +{"account_number":37,"balance":18612,"firstname":"Mcgee","lastname":"Mooney","age":39,"gender":"M","address":"826 Fillmore Place","employer":"Reversus","email":"mcgeemooney@reversus.com","city":"Tooleville","state":"OK"} +{"index":{"_type": "people", "_id":"44"}} +{"account_number":44,"balance":34487,"firstname":"Aurelia","lastname":"Harding","age":37,"gender":"M","address":"502 Baycliff Terrace","employer":"Orbalix","email":"aureliaharding@orbalix.com","city":"Yardville","state":"DE"} +{"index":{"_type": "people", "_id":"49"}} +{"account_number":49,"balance":29104,"firstname":"Fulton","lastname":"Holt","age":23,"gender":"F","address":"451 Humboldt Street","employer":"Anocha","email":"fultonholt@anocha.com","city":"Sunriver","state":"RI"} +{"index":{"_type": "people", "_id":"51"}} +{"account_number":51,"balance":14097,"firstname":"Burton","lastname":"Meyers","age":31,"gender":"F","address":"334 River Street","employer":"Bezal","email":"burtonmeyers@bezal.com","city":"Jacksonburg","state":"MO"} +{"index":{"_type": "people", "_id":"56"}} +{"account_number":56,"balance":14992,"firstname":"Josie","lastname":"Nelson","age":32,"gender":"M","address":"857 Tabor Court","employer":"Emtrac","email":"josienelson@emtrac.com","city":"Sunnyside","state":"UT"} \ No newline at end of file From d0e587efdee1f31ccbb9acd92ebcc9e73ff361a3 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Wed, 26 Aug 2015 23:54:17 +0300 Subject: [PATCH 049/559] fixed resultString and alias on return results (preventing same fieldname collision) --- .../plugin/nlpcn/HashJoinElasticExecutor.java | 52 ++++++++++++++++--- .../es4sql/query/ESHashJoinQueryAction.java | 3 ++ .../query/HashJoinElasticRequestBuilder.java | 18 +++++++ src/test/java/org/nlpcn/es4sql/JoinTests.java | 30 ++++++----- .../java/org/nlpcn/es4sql/MainTestSuite.java | 16 +++--- 5 files changed, 91 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java index c3daf952..a4547033 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java @@ -25,10 +25,7 @@ import org.nlpcn.es4sql.query.HashJoinElasticRequestBuilder; import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** * Created by Eliran on 22/8/2015. @@ -48,9 +45,7 @@ public SearchHits getHits(){ } public void sendResponse(RestChannel channel){ try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint(); - results.toXContent(builder, ToXContent.EMPTY_PARAMS); - String json = builder.string(); + String json = resultAsString(); BytesRestResponse bytesRestResponse = new BytesRestResponse(RestStatus.OK, json); channel.sendResponse(bytesRestResponse); } catch (IOException e) { @@ -58,6 +53,31 @@ public void sendResponse(RestChannel channel){ } } + + //use our deserializer instead of results toXcontent because the source field is differnet from sourceAsMap. + private String resultAsString() throws IOException { + Object[] searchHits; + searchHits = new Object[(int) this.results.totalHits()]; + int i = 0; + for(SearchHit hit : this.results) { + HashMap value = new HashMap<>(); + value.put("_id",hit.getId()); + value.put("_type", hit.getType()); + value.put("_score", hit.score()); + value.put("_source", hit.sourceAsMap()); + searchHits[i] = value; + i++; + } + XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint(); + + builder.startObject("hits"); + builder.field("total").value(this.results.totalHits()); + builder.field("max_score").value(this.results.maxScore()); + builder.array("hits",searchHits); + builder.endObject(); + return builder.string(); + } + public void run() throws IOException { SearchHits firstTableHits = requestBuilder.getFirstTableRequest().get().getHits(); Map> comparisonKeyToSearchHits = new HashMap<>(); @@ -117,7 +137,7 @@ public void run() throws IOException { //todo: decide which id to put or type. or maby its ok this way. just need to doc. InternalSearchHit searchHit = new InternalSearchHit(ids, matchingHit.id() + "|" + secondTableHit.getId(), new StringText(matchingHit.getType() + "|" + secondTableHit.getType()), matchingHit.getFields()); searchHit.sourceRef(matchingHit.getSourceRef()); - searchHit.getSource().putAll(secondTableHit.getSource()); + mergeSourceAndAddAliases(secondTableHit, searchHit); finalResult.add(searchHit); @@ -131,6 +151,22 @@ public void run() throws IOException { } + private void mergeSourceAndAddAliases(SearchHit secondTableHit, InternalSearchHit searchHit) { + //todo: aliases place + addAlias(searchHit.getSource(), requestBuilder.getFirstTableAlias()); + addAlias(secondTableHit.getSource(),requestBuilder.getSecondTableAlias()); + searchHit.getSource().putAll(secondTableHit.getSource()); + } + + private void addAlias(Map source, String alias) { + Map mapWithAliases = new HashMap<>(); + for(Map.Entry fieldNameToValue : source.entrySet()) { + mapWithAliases.put(alias + "." + fieldNameToValue.getKey(), fieldNameToValue.getValue()); + } + source.clear(); + source.putAll(mapWithAliases); + } + private void onlyReturnedFields(Map fieldsMap, List required) { for(String key : fieldsMap.keySet()){ //todo: alias? recursiveFind ? give map instead to better performance? diff --git a/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java b/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java index 5563589f..06544aef 100644 --- a/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java +++ b/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java @@ -43,6 +43,8 @@ public SqlElasticRequestBuilder explain() throws SqlParseException { String t1Alias = joinSelect.getT1Alias(); String t2Alias = joinSelect.getT2Alias(); + hashRequest.setFirstTableAlias(t1Alias); + hashRequest.setSecondTableAlias(t2Alias); List> comparisonFields = new ArrayList<>(); for(Condition condition : joinSelect.getConnectedConditions()){ @@ -65,6 +67,7 @@ public SqlElasticRequestBuilder explain() throws SqlParseException { } hashRequest.setT1ToT2FieldsComparison(comparisonFields); + hashRequest.setJoinType(joinSelect.getJoinType()); return hashRequest; } diff --git a/src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java index 2277ab8e..419bd61c 100644 --- a/src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java +++ b/src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java @@ -24,6 +24,8 @@ public class HashJoinElasticRequestBuilder implements SqlElasticRequestBuilder{ private SearchRequestBuilder firstTableRequest; private SearchRequestBuilder secondTableRequest; private MultiSearchRequest multi; + private String firstTableAlias; + private String secondTableAlias; private List firstTableReturnedField; private List secondTableReturnedField; private List> t1ToT2FieldsComparison; @@ -123,6 +125,22 @@ public SQLJoinTableSource.JoinType getJoinType() { return joinType; } + public String getFirstTableAlias() { + return firstTableAlias; + } + + public void setFirstTableAlias(String firstTableAlias) { + this.firstTableAlias = firstTableAlias; + } + + public String getSecondTableAlias() { + return secondTableAlias; + } + + public void setSecondTableAlias(String secondTableAlias) { + this.secondTableAlias = secondTableAlias; + } + public void setJoinType(SQLJoinTableSource.JoinType joinType) { this.joinType = joinType; } diff --git a/src/test/java/org/nlpcn/es4sql/JoinTests.java b/src/test/java/org/nlpcn/es4sql/JoinTests.java index 9b9f4875..e13fcfbf 100644 --- a/src/test/java/org/nlpcn/es4sql/JoinTests.java +++ b/src/test/java/org/nlpcn/es4sql/JoinTests.java @@ -37,15 +37,15 @@ public void joinParseCheckSelectedFieldsSplit() throws SqlParseException, SQLFea SearchHit[] hits = executor.getHits().getHits(); Assert.assertEquals(2,hits.length); Map oneMatch = new HashMap<>(); - oneMatch.put("firstname","Daenerys"); - oneMatch.put("lastname","Targaryen"); - oneMatch.put("gender","M"); - oneMatch.put("name", "rex"); + oneMatch.put("a.firstname","Daenerys"); + oneMatch.put("a.lastname","Targaryen"); + oneMatch.put("a.gender","M"); + oneMatch.put("d.name", "rex"); Map secondMatch = new HashMap<>(); - secondMatch.put("firstname","Hattie"); - secondMatch.put("lastname","Bond"); - secondMatch.put("gender","M"); - secondMatch.put("name","snoopy"); + secondMatch.put("a.firstname","Hattie"); + secondMatch.put("a.lastname","Bond"); + secondMatch.put("a.gender","M"); + secondMatch.put("d.name","snoopy"); Assert.assertTrue(hitsContains(hits, oneMatch)); Assert.assertTrue(hitsContains(hits,secondMatch)); @@ -53,14 +53,20 @@ public void joinParseCheckSelectedFieldsSplit() throws SqlParseException, SQLFea } private boolean hitsContains(SearchHit[] hits, Map matchMap) { - for(SearchHit hit : hits){ Map hitMap = hit.sourceAsMap(); + boolean matchedHit = true; for(Map.Entry entry: hitMap.entrySet()){ - if(!matchMap.containsKey(entry.getKey())) continue;; - if(!matchMap.get(entry.getKey()).equals(entry.getValue())) continue; + if(!matchMap.containsKey(entry.getKey())) { + matchedHit = false; + break; + } + if(!matchMap.get(entry.getKey()).equals(entry.getValue())){ + matchedHit = false; + break; + } } - return true; + if(matchedHit) return true; } return false; } diff --git a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java index b95bb059..e82a20b3 100644 --- a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java +++ b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java @@ -21,14 +21,14 @@ @RunWith(Suite.class) @Suite.SuiteClasses({ - QueryTest.class, - MethodQueryTest.class, - AggregationTest.class, - BugTest.class, - DeleteTest.class, - ExplainTest.class, - WktToGeoJsonConverterTests.class, - SqlParserTests.class, +// QueryTest.class, +// MethodQueryTest.class, +// AggregationTest.class, +// BugTest.class, +// DeleteTest.class, +// ExplainTest.class, +// WktToGeoJsonConverterTests.class, +// SqlParserTests.class, JoinTests.class }) public class MainTestSuite { From 5a75a0c2bea6af40df99f9143a8b78eadd060e00 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Fri, 28 Aug 2015 11:10:39 +0300 Subject: [PATCH 050/559] support more join usecases : no conditions,no where , no both. --- .../plugin/nlpcn/HashJoinElasticExecutor.java | 43 ++++++------- .../org/nlpcn/es4sql/parse/SqlParser.java | 11 +++- src/test/java/org/nlpcn/es4sql/JoinTests.java | 60 +++++++++++++++---- .../java/org/nlpcn/es4sql/MainTestSuite.java | 17 +++--- src/test/java/org/nlpcn/es4sql/QueryTest.java | 16 ++++- .../resources/game_of_thrones_complex.json | 14 +++++ 6 files changed, 112 insertions(+), 49 deletions(-) create mode 100644 src/test/resources/game_of_thrones_complex.json diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java index a4547033..8172c738 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java @@ -1,25 +1,16 @@ package org.elasticsearch.plugin.nlpcn; -import org.elasticsearch.action.search.SearchRequest; -import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; -import org.elasticsearch.common.Base64; -import org.elasticsearch.common.io.stream.BytesStreamInput; import org.elasticsearch.common.text.StringText; -import org.elasticsearch.common.text.Text; -import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestChannel; import org.elasticsearch.rest.RestStatus; -import org.elasticsearch.rest.action.support.RestStatusToXContentListener; import org.elasticsearch.search.SearchHit; -import org.elasticsearch.search.SearchHitField; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.internal.InternalSearchHit; -import org.elasticsearch.search.internal.InternalSearchHitField; import org.elasticsearch.search.internal.InternalSearchHits; import org.nlpcn.es4sql.domain.Field; import org.nlpcn.es4sql.query.HashJoinElasticRequestBuilder; @@ -100,12 +91,9 @@ public void run() throws IOException { comparisonKeyToSearchHits.put(key,currentSearchHits); } //int docid , id - //InternalSearchHit(int docId, String id, Text type, Map fields) - InternalSearchHit searchHit = new InternalSearchHit(ids, hit.id(), new StringText(hit.getType()), hit.getFields()); searchHit.sourceRef(hit.getSourceRef()); - onlyReturnedFields(searchHit.sourceAsMap(), requestBuilder.getFirstTableReturnedField()); ids++; currentSearchHits.add(searchHit); @@ -137,9 +125,10 @@ public void run() throws IOException { //todo: decide which id to put or type. or maby its ok this way. just need to doc. InternalSearchHit searchHit = new InternalSearchHit(ids, matchingHit.id() + "|" + secondTableHit.getId(), new StringText(matchingHit.getType() + "|" + secondTableHit.getType()), matchingHit.getFields()); searchHit.sourceRef(matchingHit.getSourceRef()); + searchHit.sourceAsMap().clear(); + searchHit.sourceAsMap().putAll(matchingHit.sourceAsMap()); mergeSourceAndAddAliases(secondTableHit, searchHit); - finalResult.add(searchHit); ids++; } @@ -152,28 +141,30 @@ public void run() throws IOException { } private void mergeSourceAndAddAliases(SearchHit secondTableHit, InternalSearchHit searchHit) { - //todo: aliases place - addAlias(searchHit.getSource(), requestBuilder.getFirstTableAlias()); - addAlias(secondTableHit.getSource(),requestBuilder.getSecondTableAlias()); - searchHit.getSource().putAll(secondTableHit.getSource()); + Map results = mapWithAliases(searchHit.getSource(), requestBuilder.getFirstTableAlias()); + results.putAll(mapWithAliases(secondTableHit.getSource(), requestBuilder.getSecondTableAlias())); + searchHit.getSource().clear(); + searchHit.getSource().putAll(results); } - private void addAlias(Map source, String alias) { + private Map mapWithAliases(Map source, String alias) { Map mapWithAliases = new HashMap<>(); for(Map.Entry fieldNameToValue : source.entrySet()) { mapWithAliases.put(alias + "." + fieldNameToValue.getKey(), fieldNameToValue.getValue()); } - source.clear(); - source.putAll(mapWithAliases); + return mapWithAliases; } private void onlyReturnedFields(Map fieldsMap, List required) { - for(String key : fieldsMap.keySet()){ - //todo: alias? recursiveFind ? give map instead to better performance? - if(!required.contains(new Field(key,null))){ - fieldsMap.remove(key); - } - } + List keysToRemove = new ArrayList<>(); + for (String key : fieldsMap.keySet()) { + //todo: alias? recursiveFind ? give map instead to better performance? + if (!required.contains(new Field(key, null))) { + keysToRemove.add(key); + } + } + for (String key : keysToRemove) + fieldsMap.remove(key); } } diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index ce970987..e5bcc0c1 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -229,6 +229,8 @@ private List convertExprsToFields(List exprs) throws S } private String sameAliasWhere(Where where, String... aliases) throws SqlParseException { + if(where == null) return null; + if(where instanceof Condition) { Condition condition = (Condition) where; @@ -242,9 +244,10 @@ private String sameAliasWhere(Where where, String... aliases) throws SqlParseExc throw new SqlParseException(String.format("fieldName : %s on codition:%s does not contain alias", fieldName, condition.toString())); } List sameAliases = new ArrayList<>(); - - for ( Where innerWhere : where.getWheres()) - sameAliases.add(sameAliasWhere(innerWhere, aliases)); + if(where.getWheres()!=null && where.getWheres().size() > 0) { + for (Where innerWhere : where.getWheres()) + sameAliases.add(sameAliasWhere(innerWhere, aliases)); + } if ( sameAliases.contains(null) ) return null; if ( sameAliases.stream().distinct().count() != 1 ) return null; @@ -382,6 +385,7 @@ private Select fillSelect(From from, Map aliasToWhere, MySqlSelec private List getJoinConditionsFlatten(SQLJoinTableSource from) throws SqlParseException { List conditions = new ArrayList<>(); + if(from.getCondition() == null ) return conditions; Where where = Where.newInstance(); parseWhere(from.getCondition(), where); addIfConditionRecursive(where, conditions); @@ -394,6 +398,7 @@ private Map splitWheres(Where where, String... aliases) throws Sql for(String alias : aliases){ aliasToWhere.put(alias,null); } + if(where == null) return aliasToWhere; String allWhereFromSameAlias = sameAliasWhere(where, aliases); if( allWhereFromSameAlias != null ) { diff --git a/src/test/java/org/nlpcn/es4sql/JoinTests.java b/src/test/java/org/nlpcn/es4sql/JoinTests.java index e13fcfbf..ca046692 100644 --- a/src/test/java/org/nlpcn/es4sql/JoinTests.java +++ b/src/test/java/org/nlpcn/es4sql/JoinTests.java @@ -2,6 +2,7 @@ import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.common.collect.ImmutableMap; import org.elasticsearch.plugin.nlpcn.HashJoinElasticExecutor; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; @@ -17,12 +18,13 @@ import java.util.HashMap; import java.util.Map; +import static org.nlpcn.es4sql.TestsConstants.TEST_INDEX; + /** * Created by Eliran on 22/8/2015. */ public class JoinTests { - @Test public void joinParseCheckSelectedFieldsSplit() throws SqlParseException, SQLFeatureNotSupportedException, IOException { String query = "SELECT a.firstname ,a.lastname , a.gender ,d.name FROM elasticsearch-sql_test_index/people a " + @@ -30,17 +32,11 @@ public void joinParseCheckSelectedFieldsSplit() throws SqlParseException, SQLFea " WHERE " + " (a.age > 10 OR a.balance > 2000)" + " AND d.age > 1"; - SearchDao searchDao = MainTestSuite.getSearchDao(); - SqlElasticRequestBuilder explain = searchDao.explain(query); - HashJoinElasticExecutor executor = new HashJoinElasticExecutor(searchDao.getClient(), (HashJoinElasticRequestBuilder) explain); - executor.run(); - SearchHit[] hits = executor.getHits().getHits(); + SearchHit[] hits = hashJoinGetHits(query); Assert.assertEquals(2,hits.length); - Map oneMatch = new HashMap<>(); - oneMatch.put("a.firstname","Daenerys"); - oneMatch.put("a.lastname","Targaryen"); - oneMatch.put("a.gender","M"); - oneMatch.put("d.name", "rex"); + + Map oneMatch = ImmutableMap.of("a.firstname", (Object)"Daenerys", "a.lastname","Targaryen", + "a.gender","M","d.name", "rex"); Map secondMatch = new HashMap<>(); secondMatch.put("a.firstname","Hattie"); secondMatch.put("a.lastname","Bond"); @@ -52,6 +48,14 @@ public void joinParseCheckSelectedFieldsSplit() throws SqlParseException, SQLFea } + private SearchHit[] hashJoinGetHits(String query) throws SqlParseException, SQLFeatureNotSupportedException, IOException { + SearchDao searchDao = MainTestSuite.getSearchDao(); + SqlElasticRequestBuilder explain = searchDao.explain(query); + HashJoinElasticExecutor executor = new HashJoinElasticExecutor(searchDao.getClient(), (HashJoinElasticRequestBuilder) explain); + executor.run(); + return executor.getHits().getHits(); + } + private boolean hitsContains(SearchHit[] hits, Map matchMap) { for(SearchHit hit : hits){ Map hitMap = hit.sourceAsMap(); @@ -71,4 +75,38 @@ private boolean hitsContains(SearchHit[] hits, Map matchMap) { return false; } + + @Test + public void joinWithNoWhereButWithCondition() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + String query = String.format("select c.gender , h.name,h.words from %s/gotCharacters c " + + "JOIN %s/gotHouses h " + + "on c.house = h.name",TEST_INDEX,TEST_INDEX); + SearchHit[] hits = hashJoinGetHits(query); + Assert.assertEquals(4,hits.length); + Map someMatch = ImmutableMap.of("c.gender", (Object)"F", "h.name","Targaryen", + "h.words","fireAndBlood"); + Assert.assertTrue(hitsContains(hits, someMatch)); + } + @Test + public void joinNoConditionButWithWhere() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + + String query = String.format("select c.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + + "JOIN %s/gotHouses h " + + "where c.firstname='Daenerys'",TEST_INDEX,TEST_INDEX); + SearchHit[] hits = hashJoinGetHits(query); + Assert.assertEquals(3,hits.length); + + } + + @Test + public void joinNoConditionAndNoWhere() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + + String query = String.format("select c.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + + "JOIN %s/gotHouses h ",TEST_INDEX,TEST_INDEX); + SearchHit[] hits = hashJoinGetHits(query); + Assert.assertEquals(12,hits.length); + + } + + } diff --git a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java index e82a20b3..8bb4b318 100644 --- a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java +++ b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java @@ -21,14 +21,14 @@ @RunWith(Suite.class) @Suite.SuiteClasses({ -// QueryTest.class, -// MethodQueryTest.class, -// AggregationTest.class, -// BugTest.class, -// DeleteTest.class, -// ExplainTest.class, -// WktToGeoJsonConverterTests.class, -// SqlParserTests.class, + QueryTest.class, + MethodQueryTest.class, + AggregationTest.class, + BugTest.class, + DeleteTest.class, + ExplainTest.class, + WktToGeoJsonConverterTests.class, + SqlParserTests.class, JoinTests.class }) public class MainTestSuite { @@ -52,6 +52,7 @@ public static void setUp() throws Exception { loadBulk("src/test/resources/phrases.json"); loadBulk("src/test/resources/dogs.json"); loadBulk("src/test/resources/peoples.json"); + loadBulk("src/test/resources/game_of_thrones_complex.json"); prepareOdbcIndex(); loadBulk("src/test/resources/odbc-date-formats.json"); diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java index 7fa7d24f..f54549f4 100644 --- a/src/test/java/org/nlpcn/es4sql/QueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java @@ -32,7 +32,8 @@ public void searchTypeTest() throws IOException, SqlParseException, SQLFeatureNo Assert.assertEquals(4, response.getTotalHits()); } - @Test + + @Test public void multipleFromTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException{ SearchHits response = query(String.format("SELECT * FROM %s/phrase, %s/account LIMIT 2000", TEST_INDEX, TEST_INDEX)); Assert.assertEquals(1004, response.getTotalHits()); @@ -503,6 +504,19 @@ public void geoPolygon() throws SQLFeatureNotSupportedException, SqlParseExcepti Assert.assertEquals("square",result.getSource().get("description")); } + @Test + public void complexObjectSearch() throws IOException, SqlParseException, SQLFeatureNotSupportedException{ + SearchHits response = query(String.format("SELECT * FROM %s/gotCharacters where name.firstname = 'Jaime' LIMIT 1000", TEST_INDEX)); + Assert.assertEquals(1, response.getTotalHits()); + } + + @Test + public void complexObjectReutrnField() throws IOException, SqlParseException, SQLFeatureNotSupportedException{ + SearchHits response = query(String.format("SELECT parents.father FROM %s/gotCharacters where name.firstname = 'Brandon' LIMIT 1000", TEST_INDEX)); + Assert.assertEquals(1, response.getTotalHits()); + Map sourceAsMap = response.getHits()[0].sourceAsMap(); + Assert.assertEquals("Eddard",((HashMap)sourceAsMap.get("parents")).get("father")); + } private SearchHits query(String query) throws SqlParseException, SQLFeatureNotSupportedException, SQLFeatureNotSupportedException { SearchDao searchDao = MainTestSuite.getSearchDao(); diff --git a/src/test/resources/game_of_thrones_complex.json b/src/test/resources/game_of_thrones_complex.json new file mode 100644 index 00000000..08dcff35 --- /dev/null +++ b/src/test/resources/game_of_thrones_complex.json @@ -0,0 +1,14 @@ +{"index":{"_type": "gotCharacters", "_id":"1"}} +{"name":{"firstname":"Daenerys","lastname":"Targaryen","ofHerName":1},"house":"Targaryen","gender":"F","parents":{"father":"Aerys" , "mother":"Rhaella"},"titles":["motherOfDragons","queenOfTheAndals","breakerOfChains","Khaleesi"]} +{"index":{"_type": "gotCharacters", "_id":"2"}} +{"name":{"firstname":"Eddard","lastname":"Stark","ofHisName":1},"house":"Stark", "parents":{"father":"Rickard" , "mother":"Lyarra"} ,"gender":"M","titles":["lordOfWinterfell","wardenOfTheNorth","handOfTheKing"]} +{"index":{"_type": "gotCharacters", "_id":"3"}} +{"name":{"firstname":"Brandon","lastname":"Stark","ofHisName":4},"house":"Stark","parents":{"father":"Eddard","mother":"Catelyn"},"gender":"M","titles":["princeOfWinterfell"]} +{"index":{"_type": "gotCharacters", "_id":"4"}} +{"name":{"firstname":"Jaime","lastname":"Lannister","ofHisName":1},"gender":"M","house":"Lannister","parents":{"father":"Tywin","mother":"Joanna"},"titles":["kingSlayer","lordCommanderOfTheKingsguard","Ser"]} +{"index":{"_type": "gotHouses", "_id":"1"}} +{"words":"fireAndBlood","name":"Targaryen","sigil":"Dragon","seat":"Dragonstone"} +{"index":{"_type": "gotHouses", "_id":"2"}} +{"words":"winterIsComing" , "name":"Stark","sigil":"direwolf","seat":"Winterfell"} +{"index":{"_type": "gotHouses", "_id":"3"}} +{"words":"hearMeRoar" , "name":"Lannister","sigil":"lion","seat":"CasterlyRock"} From 7e503c6682b504fc7305db40fd92dc107b2dfe56 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Fri, 28 Aug 2015 15:30:05 +0300 Subject: [PATCH 051/559] hashJoin - support nested objects on return and on conditions --- .../plugin/nlpcn/HashJoinElasticExecutor.java | 69 +++++++++++-------- src/test/java/org/nlpcn/es4sql/JoinTests.java | 35 +++++++++- .../java/org/nlpcn/es4sql/MainTestSuite.java | 16 ++--- 3 files changed, 82 insertions(+), 38 deletions(-) diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java index 8172c738..507a4899 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java @@ -75,16 +75,7 @@ public void run() throws IOException { List> t1ToT2FieldsComparison = requestBuilder.getT1ToT2FieldsComparison(); int ids = 1; for(SearchHit hit : firstTableHits){ - String key = ""; - Map sourceAsMap = hit.sourceAsMap(); - for(Map.Entry t1ToT2 : t1ToT2FieldsComparison){ - //todo: change to our function find if key contains '.' - String data = sourceAsMap.get(t1ToT2.getKey().getName()).toString(); - if(data == null) - key+="|null|"; - else - key+="|"+data+"|"; - } + String key = getComparisonKey(t1ToT2FieldsComparison, hit,true); List currentSearchHits = comparisonKeyToSearchHits.get(key); if(currentSearchHits == null) { currentSearchHits = new ArrayList<>(); @@ -105,16 +96,7 @@ public void run() throws IOException { SearchHits secondTableHits = requestBuilder.getSecondTableRequest().get().getHits(); for(SearchHit secondTableHit : secondTableHits){ - String key = ""; - Map sourceAsMap = secondTableHit.sourceAsMap(); - for(Map.Entry t1ToT2 : t1ToT2FieldsComparison){ - //todo: change to our function find if key contains '.' - String data = sourceAsMap.get(t1ToT2.getValue().getName()).toString(); - if(data == null) - key+="|null|"; - else - key+="|"+data+"|"; - } + String key = getComparisonKey(t1ToT2FieldsComparison,secondTableHit,false); List searchHits = comparisonKeyToSearchHits.get(key); //TODO decide what to do according to left join. now assume regular join. @@ -140,6 +122,24 @@ public void run() throws IOException { } + private String getComparisonKey(List> t1ToT2FieldsComparison, SearchHit hit, boolean firstTable) { + String key = ""; + Map sourceAsMap = hit.sourceAsMap(); + for(Map.Entry t1ToT2 : t1ToT2FieldsComparison){ + //todo: change to our function find if key contains '.' + String name; + if(firstTable) name = t1ToT2.getKey().getName(); + else name = t1ToT2.getValue().getName(); + + Object data = deepSearchInMap(sourceAsMap,name); + if(data == null) + key+="|null|"; + else + key+="|"+data.toString()+"|"; + } + return key; + } + private void mergeSourceAndAddAliases(SearchHit secondTableHit, InternalSearchHit searchHit) { Map results = mapWithAliases(searchHit.getSource(), requestBuilder.getFirstTableAlias()); results.putAll(mapWithAliases(secondTableHit.getSource(), requestBuilder.getSecondTableAlias())); @@ -156,15 +156,30 @@ private Map mapWithAliases(Map source, String al } private void onlyReturnedFields(Map fieldsMap, List required) { - List keysToRemove = new ArrayList<>(); - for (String key : fieldsMap.keySet()) { - //todo: alias? recursiveFind ? give map instead to better performance? - if (!required.contains(new Field(key, null))) { - keysToRemove.add(key); + HashMap filteredMap = new HashMap<>(); + + for(Field field: required){ + String name = field.getName(); + filteredMap.put(name, deepSearchInMap(fieldsMap, name)); + } + fieldsMap.clear(); + fieldsMap.putAll(filteredMap); + + } + + private Object deepSearchInMap(Map fieldsMap, String name) { + if(name.contains(".")){ + String[] path = name.split("\\."); + Map currentObject = fieldsMap; + for(int i=0;i) valueFromCurrentMap; } + return currentObject.get(path[path.length-1]); } - for (String key : keysToRemove) - fieldsMap.remove(key); + return fieldsMap.get(name); } } diff --git a/src/test/java/org/nlpcn/es4sql/JoinTests.java b/src/test/java/org/nlpcn/es4sql/JoinTests.java index ca046692..1a6f55e5 100644 --- a/src/test/java/org/nlpcn/es4sql/JoinTests.java +++ b/src/test/java/org/nlpcn/es4sql/JoinTests.java @@ -87,12 +87,13 @@ public void joinWithNoWhereButWithCondition() throws SQLFeatureNotSupportedExcep "h.words","fireAndBlood"); Assert.assertTrue(hitsContains(hits, someMatch)); } + @Test public void joinNoConditionButWithWhere() throws SQLFeatureNotSupportedException, IOException, SqlParseException { - String query = String.format("select c.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + + String query = String.format("select c.gender , h.name,h.words from %s/gotCharacters c " + "JOIN %s/gotHouses h " + - "where c.firstname='Daenerys'",TEST_INDEX,TEST_INDEX); + "where c.name.firstname='Daenerys'",TEST_INDEX,TEST_INDEX); SearchHit[] hits = hashJoinGetHits(query); Assert.assertEquals(3,hits.length); @@ -101,12 +102,40 @@ public void joinNoConditionButWithWhere() throws SQLFeatureNotSupportedException @Test public void joinNoConditionAndNoWhere() throws SQLFeatureNotSupportedException, IOException, SqlParseException { - String query = String.format("select c.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + + String query = String.format("select c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + "JOIN %s/gotHouses h ",TEST_INDEX,TEST_INDEX); SearchHit[] hits = hashJoinGetHits(query); Assert.assertEquals(12,hits.length); } + @Test + public void joinWithNestedFieldsOnReturn() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + String query = String.format("select c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + + "JOIN %s/gotHouses h " + + "on c.house = h.name " + + "where c.name.firstname='Daenerys'", TEST_INDEX,TEST_INDEX); + SearchHit[] hits = hashJoinGetHits(query); + Assert.assertEquals(1,hits.length); + //use flatten? + Map someMatch = ImmutableMap.of("c.name.firstname", (Object)"Daenerys","c.parents.father","Aerys", "h.name","Targaryen", + "h.words","fireAndBlood"); + Assert.assertTrue(hitsContains(hits, someMatch)); + } + + @Test + public void joinWithNestedFieldsOnComparisonAndOnReturn() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + String query = String.format("select c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + + "JOIN %s/gotHouses h " + + "on c.name.lastname = h.name " + + "where c.name.firstname='Daenerys'", TEST_INDEX,TEST_INDEX); + SearchHit[] hits = hashJoinGetHits(query); + Assert.assertEquals(1,hits.length); + //use flatten? + Map someMatch = ImmutableMap.of("c.name.firstname", (Object)"Daenerys","c.parents.father","Aerys", "h.name","Targaryen", + "h.words","fireAndBlood"); + Assert.assertTrue(hitsContains(hits, someMatch)); + } + } diff --git a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java index 8bb4b318..8cf815f9 100644 --- a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java +++ b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java @@ -21,14 +21,14 @@ @RunWith(Suite.class) @Suite.SuiteClasses({ - QueryTest.class, - MethodQueryTest.class, - AggregationTest.class, - BugTest.class, - DeleteTest.class, - ExplainTest.class, - WktToGeoJsonConverterTests.class, - SqlParserTests.class, +// QueryTest.class, +// MethodQueryTest.class, +// AggregationTest.class, +// BugTest.class, +// DeleteTest.class, +// ExplainTest.class, +// WktToGeoJsonConverterTests.class, +// SqlParserTests.class, JoinTests.class }) public class MainTestSuite { From aeff3bc1ab5c97ac610ed2c1ade99a1aa30f0f2f Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Fri, 28 Aug 2015 17:23:41 +0300 Subject: [PATCH 052/559] refactoring --- .../plugin/nlpcn/HashJoinElasticExecutor.java | 18 ++-- .../org/nlpcn/es4sql/domain/JoinSelect.java | 90 +++---------------- .../es4sql/domain/TableOnJoinSelect.java | 41 +++++++++ .../org/nlpcn/es4sql/parse/SqlParser.java | 50 ++++++----- .../es4sql/query/ESHashJoinQueryAction.java | 45 +++++----- .../query/HashJoinElasticRequestBuilder.java | 72 ++++----------- .../query/TableInJoinRequestBuilder.java | 42 +++++++++ src/test/java/org/nlpcn/es4sql/JoinTests.java | 8 +- .../java/org/nlpcn/es4sql/MainTestSuite.java | 16 ++-- .../java/org/nlpcn/es4sql/SqlParserTests.java | 16 ++-- 10 files changed, 187 insertions(+), 211 deletions(-) create mode 100644 src/main/java/org/nlpcn/es4sql/domain/TableOnJoinSelect.java create mode 100644 src/main/java/org/nlpcn/es4sql/query/TableInJoinRequestBuilder.java diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java index 507a4899..e63fe5d2 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java @@ -14,6 +14,7 @@ import org.elasticsearch.search.internal.InternalSearchHits; import org.nlpcn.es4sql.domain.Field; import org.nlpcn.es4sql.query.HashJoinElasticRequestBuilder; +import org.nlpcn.es4sql.query.TableInJoinRequestBuilder; import java.io.IOException; import java.util.*; @@ -70,7 +71,8 @@ private String resultAsString() throws IOException { } public void run() throws IOException { - SearchHits firstTableHits = requestBuilder.getFirstTableRequest().get().getHits(); + TableInJoinRequestBuilder firstTableRequest = requestBuilder.getFirstTable(); + SearchHits firstTableHits = firstTableRequest.getRequestBuilder().get().getHits(); Map> comparisonKeyToSearchHits = new HashMap<>(); List> t1ToT2FieldsComparison = requestBuilder.getT1ToT2FieldsComparison(); int ids = 1; @@ -85,7 +87,7 @@ public void run() throws IOException { InternalSearchHit searchHit = new InternalSearchHit(ids, hit.id(), new StringText(hit.getType()), hit.getFields()); searchHit.sourceRef(hit.getSourceRef()); - onlyReturnedFields(searchHit.sourceAsMap(), requestBuilder.getFirstTableReturnedField()); + onlyReturnedFields(searchHit.sourceAsMap(), firstTableRequest.getReturnedFields()); ids++; currentSearchHits.add(searchHit); } @@ -93,7 +95,9 @@ public void run() throws IOException { ids = 0; List finalResult = new ArrayList<>(); - SearchHits secondTableHits = requestBuilder.getSecondTableRequest().get().getHits(); + TableInJoinRequestBuilder secondTableRequest = requestBuilder.getSecondTable(); + + SearchHits secondTableHits = secondTableRequest.getRequestBuilder().get().getHits(); for(SearchHit secondTableHit : secondTableHits){ String key = getComparisonKey(t1ToT2FieldsComparison,secondTableHit,false); @@ -102,7 +106,7 @@ public void run() throws IOException { //TODO decide what to do according to left join. now assume regular join. if(searchHits!=null && searchHits.size() > 0){ for(InternalSearchHit matchingHit : searchHits){ - onlyReturnedFields(secondTableHit.sourceAsMap(), requestBuilder.getSecondTableReturnedField()); + onlyReturnedFields(secondTableHit.sourceAsMap(), secondTableRequest.getReturnedFields()); //todo: decide which id to put or type. or maby its ok this way. just need to doc. InternalSearchHit searchHit = new InternalSearchHit(ids, matchingHit.id() + "|" + secondTableHit.getId(), new StringText(matchingHit.getType() + "|" + secondTableHit.getType()), matchingHit.getFields()); @@ -131,7 +135,7 @@ private String getComparisonKey(List> t1ToT2FieldsCompar if(firstTable) name = t1ToT2.getKey().getName(); else name = t1ToT2.getValue().getName(); - Object data = deepSearchInMap(sourceAsMap,name); + Object data = deepSearchInMap(sourceAsMap, name); if(data == null) key+="|null|"; else @@ -141,8 +145,8 @@ private String getComparisonKey(List> t1ToT2FieldsCompar } private void mergeSourceAndAddAliases(SearchHit secondTableHit, InternalSearchHit searchHit) { - Map results = mapWithAliases(searchHit.getSource(), requestBuilder.getFirstTableAlias()); - results.putAll(mapWithAliases(secondTableHit.getSource(), requestBuilder.getSecondTableAlias())); + Map results = mapWithAliases(searchHit.getSource(), requestBuilder.getFirstTable().getAlias()); + results.putAll(mapWithAliases(secondTableHit.getSource(), requestBuilder.getSecondTable().getAlias())); searchHit.getSource().clear(); searchHit.getSource().putAll(results); } diff --git a/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java b/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java index 111dd818..a21fb531 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java +++ b/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java @@ -8,35 +8,20 @@ * Created by Eliran on 20/8/2015. */ public class JoinSelect { - private List connectedConditions; - //todo: make it an object - private Select t1Select; - private List t1ConnectedFields; - private List t1SelectedFields; - private String t1Alias; - private Select t2Select; - private List t2ConnectedFields; - private List t2SelectedFields; + private TableOnJoinSelect firstTable; + private TableOnJoinSelect secondTable; + private List connectedConditions; + private SQLJoinTableSource.JoinType joinType; - private String t2Alias; + public JoinSelect() { + firstTable = new TableOnJoinSelect(); + secondTable = new TableOnJoinSelect(); } - public JoinSelect(List connectedConditions, Select t1Select,String t1Alias, List t1ConnectedFields, List t1SelectedFields, Select t2Select, String t2Alias, List t2ConnectedFields, List t2SelectedFields,SQLJoinTableSource.JoinType joinType) { - this.connectedConditions = connectedConditions; - this.t1Select = t1Select; - this.t1Alias = t1Alias; - this.t1ConnectedFields = t1ConnectedFields; - this.t1SelectedFields = t1SelectedFields; - this.t2Select = t2Select; - this.t2Alias = t2Alias; - this.t2ConnectedFields = t2ConnectedFields; - this.t2SelectedFields = t2SelectedFields; - this.joinType = joinType; - } public List getConnectedConditions() { return connectedConditions; @@ -46,53 +31,15 @@ public void setConnectedConditions(List connectedConditions) { this.connectedConditions = connectedConditions; } - public Select getT1Select() { - return t1Select; - } - - public void setT1Select(Select t1Select) { - this.t1Select = t1Select; - } - - public List getT1ConnectedFields() { - return t1ConnectedFields; - } - - public void setT1ConnectedFields(List t1ConnectedFields) { - this.t1ConnectedFields = t1ConnectedFields; - } - - public List getT1SelectedFields() { - return t1SelectedFields; - } - - public void setT1SelectedFields(List t1SelectedFields) { - this.t1SelectedFields = t1SelectedFields; - } - - public Select getT2Select() { - return t2Select; - } - - public void setT2Select(Select t2Select) { - this.t2Select = t2Select; - } - - public List getT2ConnectedFields() { - return t2ConnectedFields; + public TableOnJoinSelect getFirstTable() { + return firstTable; } - public void setT2ConnectedFields(List t2ConnectedFields) { - this.t2ConnectedFields = t2ConnectedFields; + public TableOnJoinSelect getSecondTable() { + return secondTable; } - public List getT2SelectedFields() { - return t2SelectedFields; - } - public void setT2SelectedFields(List t2SelectedFields) { - this.t2SelectedFields = t2SelectedFields; - } public SQLJoinTableSource.JoinType getJoinType() { return joinType; @@ -102,19 +49,4 @@ public void setJoinType(SQLJoinTableSource.JoinType joinType) { this.joinType = joinType; } - public String getT1Alias() { - return t1Alias; - } - - public void setT1Alias(String t1Alias) { - this.t1Alias = t1Alias; - } - - public String getT2Alias() { - return t2Alias; - } - - public void setT2Alias(String t2Alias) { - this.t2Alias = t2Alias; - } } diff --git a/src/main/java/org/nlpcn/es4sql/domain/TableOnJoinSelect.java b/src/main/java/org/nlpcn/es4sql/domain/TableOnJoinSelect.java new file mode 100644 index 00000000..1c6c8251 --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/domain/TableOnJoinSelect.java @@ -0,0 +1,41 @@ +package org.nlpcn.es4sql.domain; + +import java.util.List; + +/** + * Created by Eliran on 28/8/2015. + */ +public class TableOnJoinSelect extends Select { + + private List connectedFields; + private List selectedFields; + private String alias; + + public TableOnJoinSelect() { + } + + + public List getConnectedFields() { + return connectedFields; + } + + public void setConnectedFields(List connectedFields) { + this.connectedFields = connectedFields; + } + + public List getSelectedFields() { + return selectedFields; + } + + public void setSelectedFields(List selectedFields) { + this.selectedFields = selectedFields; + } + + public String getAlias() { + return alias; + } + + public void setAlias(String alias) { + this.alias = alias; + } +} diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index e5bcc0c1..9ffccaa3 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -317,41 +317,47 @@ private List findFrom(SQLTableSource from) { } public JoinSelect parseJoinSelect(SQLQueryExpr sqlExpr) throws SqlParseException { + MySqlSelectQueryBlock query = (MySqlSelectQueryBlock) sqlExpr.getSubQuery().getQuery(); List joinedFrom = findJoinedFrom(query.getFrom()); if(joinedFrom.size() != 2) throw new RuntimeException("currently supports only 2 tables join"); - Where where = findWhere(query.getWhere()); + JoinSelect joinSelect = createBasicJoinSelectAccordingToTableSource((SQLJoinTableSource) query.getFrom()); + String firstTableAlias = joinedFrom.get(0).getAlias(); String secondTableAlias = joinedFrom.get(1).getAlias(); + Map aliasToWhere = splitAndFindWhere(query.getWhere(), firstTableAlias, secondTableAlias); - Map aliasToWhere = splitWheres(where, firstTableAlias, secondTableAlias); + fillTableSelectedJoin(joinSelect.getFirstTable(), query, joinedFrom.get(0), aliasToWhere.get(firstTableAlias), joinSelect.getConnectedConditions()); + fillTableSelectedJoin(joinSelect.getSecondTable(), query, joinedFrom.get(1), aliasToWhere.get(secondTableAlias), joinSelect.getConnectedConditions()); + //todo: throw error feature not supported: no group bys on joins ? - JoinSelect joinSelect = new JoinSelect(); - - joinSelect.setT1Select(fillSelect(joinedFrom.get(0), aliasToWhere, query)); - joinSelect.setT2Select(fillSelect(joinedFrom.get(1), aliasToWhere, query)); + return joinSelect; + } - List conditions = getJoinConditionsFlatten((SQLJoinTableSource) query.getFrom()); + private JoinSelect createBasicJoinSelectAccordingToTableSource(SQLJoinTableSource joinTableSource) throws SqlParseException { + JoinSelect joinSelect = new JoinSelect(); + List conditions = getJoinConditionsFlatten(joinTableSource); joinSelect.setConnectedConditions(conditions); - - joinSelect.setT1ConnectedFields(getConnectedFields(conditions,firstTableAlias)); - joinSelect.setT2ConnectedFields(getConnectedFields(conditions, secondTableAlias)); - //todo: throw error feature not supported: no group bys on joins ? - - SQLJoinTableSource.JoinType joinType = ((SQLJoinTableSource) query.getFrom()).getJoinType(); + SQLJoinTableSource.JoinType joinType = joinTableSource.getJoinType(); joinSelect.setJoinType(joinType); + return joinSelect; + } - joinSelect.setT1SelectedFields(new ArrayList(joinSelect.getT1Select().getFields())); - joinSelect.setT2SelectedFields(new ArrayList(joinSelect.getT2Select().getFields())); - - joinSelect.setT1Alias(firstTableAlias); - joinSelect.setT2Alias(secondTableAlias); + private Map splitAndFindWhere(SQLExpr whereExpr, String firstTableAlias, String secondTableAlias) throws SqlParseException { + Where where = findWhere(whereExpr); + return splitWheres(where, firstTableAlias, secondTableAlias); + } - return joinSelect; + private void fillTableSelectedJoin(TableOnJoinSelect tableOnJoin,MySqlSelectQueryBlock query, From tableFrom, Where where, List conditions) throws SqlParseException { + String alias = tableFrom.getAlias(); + fillBasicTableSelectJoin(tableOnJoin, tableFrom, where, query); + tableOnJoin.setConnectedFields(getConnectedFields(conditions, alias)); + tableOnJoin.setSelectedFields(new ArrayList(tableOnJoin.getFields())); + tableOnJoin.setAlias(alias); } private List getConnectedFields(List conditions, String alias) throws SqlParseException { @@ -374,13 +380,11 @@ private List getConnectedFields(List conditions, String alias) return fields; } - private Select fillSelect(From from, Map aliasToWhere, MySqlSelectQueryBlock query) throws SqlParseException { - Select select = new Select(); + private void fillBasicTableSelectJoin(TableOnJoinSelect select, From from, Where where, MySqlSelectQueryBlock query) throws SqlParseException { select.getFrom().add(from); findSelect(query, select,from.getAlias()); findLimit(query.getLimit(),select); - select.setWhere(aliasToWhere.get(from.getAlias())); - return select; + select.setWhere(where); } private List getJoinConditionsFlatten(SQLJoinTableSource from) throws SqlParseException { diff --git a/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java b/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java index 06544aef..533e2abd 100644 --- a/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java +++ b/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java @@ -20,33 +20,25 @@ public ESHashJoinQueryAction(Client client,JoinSelect joinSelect) { @Override public SqlElasticRequestBuilder explain() throws SqlParseException { - //TODO: on map entry change names to t1ToT2 and make sure of this order here (based on aliases) HashJoinElasticRequestBuilder hashRequest = new HashJoinElasticRequestBuilder(); - Select t1Select = joinSelect.getT1Select(); - List t1ConnectedFields = joinSelect.getT1ConnectedFields(); - addFieldsToSelectIfMissing(t1Select,t1ConnectedFields); - DefaultQueryAction t1QueryAction = new DefaultQueryAction(client,t1Select); - t1QueryAction.explain(); - hashRequest.setFirstTableRequest(t1QueryAction.getRequestBuilder()); + String t1Alias = joinSelect.getFirstTable().getAlias(); + String t2Alias = joinSelect.getSecondTable().getAlias(); - Select t2Select = joinSelect.getT2Select(); - List t2ConnectedFields = joinSelect.getT2ConnectedFields(); - addFieldsToSelectIfMissing(t2Select,t2ConnectedFields); - DefaultQueryAction t2QueryAction = new DefaultQueryAction(client,t2Select); - t2QueryAction.explain(); - hashRequest.setSecondTableRequest(t2QueryAction.getRequestBuilder()); + fillRequestBuilder(hashRequest.getFirstTable(), joinSelect.getFirstTable()); + fillRequestBuilder(hashRequest.getSecondTable(), joinSelect.getSecondTable()); + List> comparisonFields = getComparisonFields(t1Alias, t2Alias,joinSelect.getConnectedConditions()); - hashRequest.setFirstTableReturnedField(joinSelect.getT1SelectedFields()); - hashRequest.setSecondTableReturnedField(joinSelect.getT2SelectedFields()); + hashRequest.setT1ToT2FieldsComparison(comparisonFields); + + hashRequest.setJoinType(joinSelect.getJoinType()); + return hashRequest; + } - String t1Alias = joinSelect.getT1Alias(); - String t2Alias = joinSelect.getT2Alias(); - hashRequest.setFirstTableAlias(t1Alias); - hashRequest.setSecondTableAlias(t2Alias); + private List> getComparisonFields(String t1Alias, String t2Alias, List connectedConditions) throws SqlParseException { List> comparisonFields = new ArrayList<>(); - for(Condition condition : joinSelect.getConnectedConditions()){ + for(Condition condition : connectedConditions){ if(condition.getOpear() != Condition.OPEAR.EQ){ throw new SqlParseException(String.format("HashJoin should only be with EQ conditions, got:%s on condition:%s", condition.getOpear().name(), condition.toString())); @@ -65,10 +57,17 @@ public SqlElasticRequestBuilder explain() throws SqlParseException { } comparisonFields.add(new AbstractMap.SimpleEntry(t1Field, t2Field)); } - hashRequest.setT1ToT2FieldsComparison(comparisonFields); + return comparisonFields; + } - hashRequest.setJoinType(joinSelect.getJoinType()); - return hashRequest; + private void fillRequestBuilder(TableInJoinRequestBuilder requestBuilder,TableOnJoinSelect tableOnJoinSelect) throws SqlParseException { + List connectedFields = tableOnJoinSelect.getConnectedFields(); + addFieldsToSelectIfMissing(tableOnJoinSelect,connectedFields); + DefaultQueryAction queryAction = new DefaultQueryAction(client,tableOnJoinSelect); + queryAction.explain(); + requestBuilder.setRequestBuilder(queryAction.getRequestBuilder()); + requestBuilder.setReturnedFields(tableOnJoinSelect.getSelectedFields()); + requestBuilder.setAlias(tableOnJoinSelect.getAlias()); } private String removeAlias(String field, String alias) { diff --git a/src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java index 419bd61c..dbd5438a 100644 --- a/src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java +++ b/src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java @@ -21,17 +21,16 @@ */ public class HashJoinElasticRequestBuilder implements SqlElasticRequestBuilder{ - private SearchRequestBuilder firstTableRequest; - private SearchRequestBuilder secondTableRequest; private MultiSearchRequest multi; - private String firstTableAlias; - private String secondTableAlias; - private List firstTableReturnedField; - private List secondTableReturnedField; + private TableInJoinRequestBuilder firstTable; + private TableInJoinRequestBuilder secondTable; + private List> t1ToT2FieldsComparison; private SQLJoinTableSource.JoinType joinType; public HashJoinElasticRequestBuilder() { + firstTable = new TableInJoinRequestBuilder(); + secondTable = new TableInJoinRequestBuilder(); } @@ -45,8 +44,8 @@ public ActionRequest request() { private void buildMulti() { multi = new MultiSearchRequest(); - multi.add(firstTableRequest); - multi.add(secondTableRequest); + multi.add(firstTable.getRequestBuilder()); + multi.add(secondTable.getRequestBuilder()); multi.listenerThreaded(false); } @@ -54,10 +53,10 @@ private void buildMulti() { public String explain() { try { XContentBuilder firstBuilder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint(); - firstTableRequest.internalBuilder().toXContent(firstBuilder, ToXContent.EMPTY_PARAMS); + firstTable.getRequestBuilder().internalBuilder().toXContent(firstBuilder, ToXContent.EMPTY_PARAMS); XContentBuilder secondBuilder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint(); - secondTableRequest.internalBuilder().toXContent(secondBuilder, ToXContent.EMPTY_PARAMS); + secondTable.getRequestBuilder().internalBuilder().toXContent(secondBuilder, ToXContent.EMPTY_PARAMS); String explained = String.format("HashJoin. first query:\n%s\n second query:\n%s", firstBuilder.string(), secondBuilder.string()); return explained; @@ -73,22 +72,6 @@ public ActionResponse get() { } - public SearchRequestBuilder getFirstTableRequest() { - return firstTableRequest; - } - - public void setFirstTableRequest(SearchRequestBuilder firstTableRequest) { - this.firstTableRequest = firstTableRequest; - } - - public SearchRequestBuilder getSecondTableRequest() { - return secondTableRequest; - } - - public void setSecondTableRequest(SearchRequestBuilder secondTableRequest) { - this.secondTableRequest = secondTableRequest; - } - public MultiSearchRequest getMulti() { return multi; } @@ -97,22 +80,6 @@ public void setMulti(MultiSearchRequest multi) { this.multi = multi; } - public List getFirstTableReturnedField() { - return firstTableReturnedField; - } - - public void setFirstTableReturnedField(List firstTableReturnedField) { - this.firstTableReturnedField = firstTableReturnedField; - } - - public List getSecondTableReturnedField() { - return secondTableReturnedField; - } - - public void setSecondTableReturnedField(List secondTableReturnedField) { - this.secondTableReturnedField = secondTableReturnedField; - } - public List> getT1ToT2FieldsComparison() { return t1ToT2FieldsComparison; } @@ -125,24 +92,15 @@ public SQLJoinTableSource.JoinType getJoinType() { return joinType; } - public String getFirstTableAlias() { - return firstTableAlias; - } - - public void setFirstTableAlias(String firstTableAlias) { - this.firstTableAlias = firstTableAlias; - } - - public String getSecondTableAlias() { - return secondTableAlias; + public void setJoinType(SQLJoinTableSource.JoinType joinType) { + this.joinType = joinType; } - public void setSecondTableAlias(String secondTableAlias) { - this.secondTableAlias = secondTableAlias; + public TableInJoinRequestBuilder getFirstTable() { + return firstTable; } - public void setJoinType(SQLJoinTableSource.JoinType joinType) { - this.joinType = joinType; + public TableInJoinRequestBuilder getSecondTable() { + return secondTable; } - } diff --git a/src/main/java/org/nlpcn/es4sql/query/TableInJoinRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/TableInJoinRequestBuilder.java new file mode 100644 index 00000000..ecde4b82 --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/query/TableInJoinRequestBuilder.java @@ -0,0 +1,42 @@ +package org.nlpcn.es4sql.query; + +import org.elasticsearch.action.search.SearchRequestBuilder; +import org.nlpcn.es4sql.domain.Field; + +import java.util.List; + +/** + * Created by Eliran on 28/8/2015. + */ +public class TableInJoinRequestBuilder { + private SearchRequestBuilder requestBuilder; + private String alias; + private List returnedFields; + + public TableInJoinRequestBuilder() { + } + + public SearchRequestBuilder getRequestBuilder() { + return requestBuilder; + } + + public void setRequestBuilder(SearchRequestBuilder requestBuilder) { + this.requestBuilder = requestBuilder; + } + + public String getAlias() { + return alias; + } + + public void setAlias(String alias) { + this.alias = alias; + } + + public List getReturnedFields() { + return returnedFields; + } + + public void setReturnedFields(List returnedFields) { + this.returnedFields = returnedFields; + } +} diff --git a/src/test/java/org/nlpcn/es4sql/JoinTests.java b/src/test/java/org/nlpcn/es4sql/JoinTests.java index 1a6f55e5..b003be05 100644 --- a/src/test/java/org/nlpcn/es4sql/JoinTests.java +++ b/src/test/java/org/nlpcn/es4sql/JoinTests.java @@ -37,11 +37,8 @@ public void joinParseCheckSelectedFieldsSplit() throws SqlParseException, SQLFea Map oneMatch = ImmutableMap.of("a.firstname", (Object)"Daenerys", "a.lastname","Targaryen", "a.gender","M","d.name", "rex"); - Map secondMatch = new HashMap<>(); - secondMatch.put("a.firstname","Hattie"); - secondMatch.put("a.lastname","Bond"); - secondMatch.put("a.gender","M"); - secondMatch.put("d.name","snoopy"); + Map secondMatch = ImmutableMap.of("a.firstname", (Object)"Hattie", "a.lastname","Bond", + "a.gender","M","d.name", "snoopy"); Assert.assertTrue(hitsContains(hits, oneMatch)); Assert.assertTrue(hitsContains(hits,secondMatch)); @@ -131,7 +128,6 @@ public void joinWithNestedFieldsOnComparisonAndOnReturn() throws SQLFeatureNotSu "where c.name.firstname='Daenerys'", TEST_INDEX,TEST_INDEX); SearchHit[] hits = hashJoinGetHits(query); Assert.assertEquals(1,hits.length); - //use flatten? Map someMatch = ImmutableMap.of("c.name.firstname", (Object)"Daenerys","c.parents.father","Aerys", "h.name","Targaryen", "h.words","fireAndBlood"); Assert.assertTrue(hitsContains(hits, someMatch)); diff --git a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java index 8cf815f9..8bb4b318 100644 --- a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java +++ b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java @@ -21,14 +21,14 @@ @RunWith(Suite.class) @Suite.SuiteClasses({ -// QueryTest.class, -// MethodQueryTest.class, -// AggregationTest.class, -// BugTest.class, -// DeleteTest.class, -// ExplainTest.class, -// WktToGeoJsonConverterTests.class, -// SqlParserTests.class, + QueryTest.class, + MethodQueryTest.class, + AggregationTest.class, + BugTest.class, + DeleteTest.class, + ExplainTest.class, + WktToGeoJsonConverterTests.class, + SqlParserTests.class, JoinTests.class }) public class MainTestSuite { diff --git a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java index 5f1c6f2b..c2a7dc27 100644 --- a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java +++ b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java @@ -39,13 +39,13 @@ public void joinParseCheckSelectedFieldsSplit() throws SqlParseException { JoinSelect joinSelect = parser.parseJoinSelect((SQLQueryExpr) queryToExpr(query)); - List t1Fields = joinSelect.getT1SelectedFields(); + List t1Fields = joinSelect.getFirstTable().getSelectedFields(); Assert.assertEquals(t1Fields.size(),3); Assert.assertTrue(fieldExist(t1Fields, "firstname")); Assert.assertTrue(fieldExist(t1Fields, "lastname")); Assert.assertTrue(fieldExist(t1Fields, "gender")); - List t2Fields = joinSelect.getT2SelectedFields(); + List t2Fields = joinSelect.getSecondTable().getSelectedFields(); Assert.assertEquals(t2Fields.size(),2); Assert.assertTrue(fieldExist(t2Fields,"holdersName")); Assert.assertTrue(fieldExist(t2Fields,"name")); @@ -62,12 +62,12 @@ public void joinParseCheckConnectedFields() throws SqlParseException { JoinSelect joinSelect = parser.parseJoinSelect((SQLQueryExpr) queryToExpr(query)); - List t1Fields = joinSelect.getT1ConnectedFields(); + List t1Fields = joinSelect.getFirstTable().getConnectedFields(); Assert.assertEquals(t1Fields.size(),2); Assert.assertTrue(fieldExist(t1Fields, "firstname")); Assert.assertTrue(fieldExist(t1Fields, "age")); - List t2Fields = joinSelect.getT2ConnectedFields(); + List t2Fields = joinSelect.getSecondTable().getConnectedFields(); Assert.assertEquals(t2Fields.size(),2); Assert.assertTrue(fieldExist(t2Fields,"holdersName")); Assert.assertTrue(fieldExist(t2Fields,"age")); @@ -90,13 +90,13 @@ public void joinParseFromsAreSplitedCorrectly() throws SqlParseException { " AND d.age > 1"; JoinSelect joinSelect = parser.parseJoinSelect((SQLQueryExpr) queryToExpr(query)); - List t1From = joinSelect.getT1Select().getFrom(); + List t1From = joinSelect.getFirstTable().getFrom(); Assert.assertNotNull(t1From); Assert.assertEquals(1,t1From.size()); Assert.assertTrue(checkFrom(t1From.get(0),"elasticsearch-sql_test_index","account","a")); - List t2From = joinSelect.getT2Select().getFrom(); + List t2From = joinSelect.getSecondTable().getFrom(); Assert.assertNotNull(t2From); Assert.assertEquals(1,t2From.size()); Assert.assertTrue(checkFrom(t2From.get(0),"elasticsearch-sql_test_index","dog","d")); @@ -149,9 +149,9 @@ public void joinSplitWhereCorrectly() throws SqlParseException { " AND d.age > 1"; JoinSelect joinSelect = parser.parseJoinSelect((SQLQueryExpr) queryToExpr(query)); - String s1Where = joinSelect.getT1Select().getWhere().toString(); + String s1Where = joinSelect.getFirstTable().getWhere().toString(); Assert.assertEquals("AND ( AND firstname EQ eliran, AND ( OR age GT 10, OR balance GT 2000 ) ) " , s1Where); - String s2Where = joinSelect.getT2Select().getWhere().toString(); + String s2Where = joinSelect.getSecondTable().getWhere().toString(); Assert.assertEquals("AND age GT 1",s2Where); } From 6b46b42be6ac3cb08072347b3a0114fde7a1f2e3 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Fri, 28 Aug 2015 20:03:35 +0300 Subject: [PATCH 053/559] left join support! +bug fix on conditions parsing --- .../plugin/nlpcn/HashJoinElasticExecutor.java | 58 +++++++++++++++---- .../plugin/nlpcn/SearchHitsResult.java | 39 +++++++++++++ .../org/nlpcn/es4sql/parse/SqlParser.java | 8 ++- src/test/java/org/nlpcn/es4sql/JoinTests.java | 34 +++++++++-- .../java/org/nlpcn/es4sql/SqlParserTests.java | 40 ++++++++++++- 5 files changed, 157 insertions(+), 22 deletions(-) create mode 100644 src/main/java/org/elasticsearch/plugin/nlpcn/SearchHitsResult.java diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java index e63fe5d2..94afb2bf 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java @@ -1,5 +1,6 @@ package org.elasticsearch.plugin.nlpcn; +import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource; import org.elasticsearch.client.Client; import org.elasticsearch.common.text.StringText; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -18,6 +19,7 @@ import java.io.IOException; import java.util.*; +import java.util.regex.MatchResult; /** * Created by Eliran on 22/8/2015. @@ -73,15 +75,15 @@ private String resultAsString() throws IOException { public void run() throws IOException { TableInJoinRequestBuilder firstTableRequest = requestBuilder.getFirstTable(); SearchHits firstTableHits = firstTableRequest.getRequestBuilder().get().getHits(); - Map> comparisonKeyToSearchHits = new HashMap<>(); + Map comparisonKeyToSearchHits = new HashMap<>(); List> t1ToT2FieldsComparison = requestBuilder.getT1ToT2FieldsComparison(); int ids = 1; for(SearchHit hit : firstTableHits){ String key = getComparisonKey(t1ToT2FieldsComparison, hit,true); - List currentSearchHits = comparisonKeyToSearchHits.get(key); - if(currentSearchHits == null) { - currentSearchHits = new ArrayList<>(); - comparisonKeyToSearchHits.put(key,currentSearchHits); + SearchHitsResult currentSearchHitsResult = comparisonKeyToSearchHits.get(key); + if(currentSearchHitsResult == null) { + currentSearchHitsResult = new SearchHitsResult(new ArrayList(),false); + comparisonKeyToSearchHits.put(key,currentSearchHitsResult); } //int docid , id InternalSearchHit searchHit = new InternalSearchHit(ids, hit.id(), new StringText(hit.getType()), hit.getFields()); @@ -89,7 +91,7 @@ public void run() throws IOException { onlyReturnedFields(searchHit.sourceAsMap(), firstTableRequest.getReturnedFields()); ids++; - currentSearchHits.add(searchHit); + currentSearchHitsResult.getSearchHits().add(searchHit); } @@ -102,9 +104,11 @@ public void run() throws IOException { String key = getComparisonKey(t1ToT2FieldsComparison,secondTableHit,false); - List searchHits = comparisonKeyToSearchHits.get(key); - //TODO decide what to do according to left join. now assume regular join. - if(searchHits!=null && searchHits.size() > 0){ + SearchHitsResult searchHitsResult = comparisonKeyToSearchHits.get(key); + + if(searchHitsResult!=null && searchHitsResult.getSearchHits().size() > 0){ + searchHitsResult.setMatchedWithOtherTable(true); + List searchHits = searchHitsResult.getSearchHits(); for(InternalSearchHit matchingHit : searchHits){ onlyReturnedFields(secondTableHit.sourceAsMap(), secondTableRequest.getReturnedFields()); @@ -113,7 +117,7 @@ public void run() throws IOException { searchHit.sourceRef(matchingHit.getSourceRef()); searchHit.sourceAsMap().clear(); searchHit.sourceAsMap().putAll(matchingHit.sourceAsMap()); - mergeSourceAndAddAliases(secondTableHit, searchHit); + mergeSourceAndAddAliases(secondTableHit.getSource(), searchHit); finalResult.add(searchHit); ids++; @@ -121,11 +125,41 @@ public void run() throws IOException { } } + if(requestBuilder.getJoinType() == SQLJoinTableSource.JoinType.LEFT_OUTER_JOIN){ + addUnmatchedResults(finalResult,comparisonKeyToSearchHits.values(),requestBuilder.getSecondTable().getReturnedFields(),ids); + } InternalSearchHit[] hits = finalResult.toArray(new InternalSearchHit[ids]); this.results = new InternalSearchHits(hits,ids,1.0f); } + private void addUnmatchedResults(List finalResult, Collection firstTableSearchHits, List secondTableReturnedFields,int currentNumOfIds) { + for(SearchHitsResult hitsResult : firstTableSearchHits){ + if(!hitsResult.isMatchedWithOtherTable()){ + for(SearchHit hit: hitsResult.getSearchHits() ) { + //todo: decide which id to put or type. or maby its ok this way. just need to doc. + InternalSearchHit searchHit = new InternalSearchHit(currentNumOfIds, hit.id() + "|0", new StringText(hit.getType() + "|null"), hit.getFields()); + searchHit.sourceRef(hit.getSourceRef()); + searchHit.sourceAsMap().clear(); + searchHit.sourceAsMap().putAll(hit.sourceAsMap()); + Map emptySecondTableHitSource = createNullsSource(secondTableReturnedFields); + mergeSourceAndAddAliases(emptySecondTableHitSource, searchHit); + + finalResult.add(searchHit); + currentNumOfIds++; + } + } + } + } + + private Map createNullsSource(List secondTableReturnedFields) { + Map nulledSource = new HashMap<>(); + for(Field field : secondTableReturnedFields){ + nulledSource.put(field.getName(),null); + } + return nulledSource; + } + private String getComparisonKey(List> t1ToT2FieldsComparison, SearchHit hit, boolean firstTable) { String key = ""; Map sourceAsMap = hit.sourceAsMap(); @@ -144,9 +178,9 @@ private String getComparisonKey(List> t1ToT2FieldsCompar return key; } - private void mergeSourceAndAddAliases(SearchHit secondTableHit, InternalSearchHit searchHit) { + private void mergeSourceAndAddAliases(Map secondTableHitSource, InternalSearchHit searchHit) { Map results = mapWithAliases(searchHit.getSource(), requestBuilder.getFirstTable().getAlias()); - results.putAll(mapWithAliases(secondTableHit.getSource(), requestBuilder.getSecondTable().getAlias())); + results.putAll(mapWithAliases(secondTableHitSource, requestBuilder.getSecondTable().getAlias())); searchHit.getSource().clear(); searchHit.getSource().putAll(results); } diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/SearchHitsResult.java b/src/main/java/org/elasticsearch/plugin/nlpcn/SearchHitsResult.java new file mode 100644 index 00000000..3bc20e6c --- /dev/null +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/SearchHitsResult.java @@ -0,0 +1,39 @@ +package org.elasticsearch.plugin.nlpcn; + +import org.elasticsearch.search.internal.InternalSearchHit; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Eliran on 28/8/2015. + */ +public class SearchHitsResult { + private List searchHits; + private boolean matchedWithOtherTable; + + public SearchHitsResult() { + searchHits = new ArrayList<>(); + } + + public SearchHitsResult(List searchHits, boolean matchedWithOtherTable) { + this.searchHits = searchHits; + this.matchedWithOtherTable = matchedWithOtherTable; + } + + public List getSearchHits() { + return searchHits; + } + + public void setSearchHits(List searchHits) { + this.searchHits = searchHits; + } + + public boolean isMatchedWithOtherTable() { + return matchedWithOtherTable; + } + + public void setMatchedWithOtherTable(boolean matchedWithOtherTable) { + this.matchedWithOtherTable = matchedWithOtherTable; + } +} diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index 9ffccaa3..35cd111b 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -372,9 +372,11 @@ private List getConnectedFields(List conditions, String alias) throw new SqlParseException("conditions on join should be one side is firstTable second Other , condition was:" + condition.toString()); } SQLPropertyExpr conditionValue = (SQLPropertyExpr) condition.getValue(); - SQLIdentifierExpr owner = (SQLIdentifierExpr) conditionValue.getOwner(); - if(owner.getName().equals(alias)) - fields.add(new Field(conditionValue.getName(),null)); + String aliasDotValue = conditionValue.toString(); + int indexOfDot = aliasDotValue.indexOf("."); + String owner = aliasDotValue.substring(0, indexOfDot); + if(owner.equals(alias)) + fields.add(new Field(aliasDotValue.substring(indexOfDot+1),null)); } } return fields; diff --git a/src/test/java/org/nlpcn/es4sql/JoinTests.java b/src/test/java/org/nlpcn/es4sql/JoinTests.java index b003be05..45baaf3a 100644 --- a/src/test/java/org/nlpcn/es4sql/JoinTests.java +++ b/src/test/java/org/nlpcn/es4sql/JoinTests.java @@ -28,7 +28,7 @@ public class JoinTests { @Test public void joinParseCheckSelectedFieldsSplit() throws SqlParseException, SQLFeatureNotSupportedException, IOException { String query = "SELECT a.firstname ,a.lastname , a.gender ,d.name FROM elasticsearch-sql_test_index/people a " + - "LEFT JOIN elasticsearch-sql_test_index/dog d on d.holdersName = a.firstname " + + " JOIN elasticsearch-sql_test_index/dog d on d.holdersName = a.firstname " + " WHERE " + " (a.age > 10 OR a.balance > 2000)" + " AND d.age > 1"; @@ -37,8 +37,8 @@ public void joinParseCheckSelectedFieldsSplit() throws SqlParseException, SQLFea Map oneMatch = ImmutableMap.of("a.firstname", (Object)"Daenerys", "a.lastname","Targaryen", "a.gender","M","d.name", "rex"); - Map secondMatch = ImmutableMap.of("a.firstname", (Object)"Hattie", "a.lastname","Bond", - "a.gender","M","d.name", "snoopy"); + Map secondMatch = ImmutableMap.of("a.firstname", (Object) "Hattie", "a.lastname", "Bond", + "a.gender", "M", "d.name", "snoopy"); Assert.assertTrue(hitsContains(hits, oneMatch)); Assert.assertTrue(hitsContains(hits,secondMatch)); @@ -62,7 +62,7 @@ private boolean hitsContains(SearchHit[] hits, Map matchMap) { matchedHit = false; break; } - if(!matchMap.get(entry.getKey()).equals(entry.getValue())){ + if(!equalsWithNullCheck(matchMap.get(entry.getKey()), entry.getValue())){ matchedHit = false; break; } @@ -72,6 +72,11 @@ private boolean hitsContains(SearchHit[] hits, Map matchMap) { return false; } + private boolean equalsWithNullCheck(Object one, Object other) { + if(one == null) return other == null; + return one.equals(other); + } + @Test public void joinWithNoWhereButWithCondition() throws SQLFeatureNotSupportedException, IOException, SqlParseException { @@ -134,4 +139,25 @@ public void joinWithNestedFieldsOnComparisonAndOnReturn() throws SQLFeatureNotSu } + @Test + public void testLeftJoin() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + String query = String.format("select c.name.firstname, f.name.firstname,f.name.lastname from %s/gotCharacters c " + + "LEFT JOIN %s/gotCharacters f " + + "on c.parents.father = f.name.firstname " + , TEST_INDEX,TEST_INDEX); + SearchHit[] hits = hashJoinGetHits(query); + Assert.assertEquals(4,hits.length); + + Map oneMatch = new HashMap<>(); + oneMatch.put("c.name.firstname", "Daenerys"); + oneMatch.put("f.name.firstname",null); + oneMatch.put("f.name.lastname",null); + + Assert.assertTrue(hitsContains(hits, oneMatch)); + Map secondMatch = ImmutableMap.of("c.name.firstname", (Object)"Brandon", + "f.name.firstname","Eddard", "f.name.lastname","Stark"); + Assert.assertTrue(hitsContains(hits, secondMatch)); + } + + } diff --git a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java index c2a7dc27..aba28e1e 100644 --- a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java +++ b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java @@ -4,6 +4,8 @@ import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr; import com.alibaba.druid.sql.ast.expr.SQLQueryExpr; +import org.elasticsearch.common.collect.ImmutableMap; +import org.elasticsearch.search.SearchHit; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; @@ -13,7 +15,12 @@ import org.nlpcn.es4sql.parse.SqlParser; import org.nlpcn.es4sql.query.ESHashJoinQueryAction; +import java.io.IOException; +import java.sql.SQLFeatureNotSupportedException; import java.util.List; +import java.util.Map; + +import static org.nlpcn.es4sql.TestsConstants.TEST_INDEX; /** * Created by Eliran on 21/8/2015. @@ -155,12 +162,38 @@ public void joinSplitWhereCorrectly() throws SqlParseException { Assert.assertEquals("AND age GT 1",s2Where); } + @Test + public void joinConditionWithComplexObjectComparisonRightSide() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + String query = String.format("select c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + + "JOIN %s/gotHouses h " + + "on h.name = c.name.lastname " + + "where c.name.firstname='Daenerys'", TEST_INDEX,TEST_INDEX); + JoinSelect joinSelect = parser.parseJoinSelect((SQLQueryExpr) queryToExpr(query)); + List conditions = joinSelect.getConnectedConditions(); + Assert.assertNotNull(conditions); + Assert.assertEquals(1,conditions.size()); + Assert.assertTrue("condition not exist: h.name = c.name.lastname",conditionExist(conditions, "h.name", "c.name.lastname", Condition.OPEAR.EQ)); + } + + @Test + public void joinConditionWithComplexObjectComparisonLeftSide() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + String query = String.format("select c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + + "JOIN %s/gotHouses h " + + "on c.name.lastname = h.name " + + "where c.name.firstname='Daenerys'", TEST_INDEX,TEST_INDEX); + JoinSelect joinSelect = parser.parseJoinSelect((SQLQueryExpr) queryToExpr(query)); + List conditions = joinSelect.getConnectedConditions(); + Assert.assertNotNull(conditions); + Assert.assertEquals(1,conditions.size()); + Assert.assertTrue("condition not exist: c.name.lastname = h.name",conditionExist(conditions, "c.name.lastname", "h.name", Condition.OPEAR.EQ)); + } + private SQLExpr queryToExpr(String query) { return new ElasticSqlExprParser(query).expr(); } private boolean conditionExist(List conditions, String from, String to, Condition.OPEAR opear) { - String[] aliasAndField = to.split("\\."); + String[] aliasAndField = to.split("\\.",2); String toAlias = aliasAndField[0]; String toField = aliasAndField[1]; for (Condition condition : conditions){ @@ -170,8 +203,9 @@ private boolean conditionExist(List conditions, String from, String t if(!fromIsEqual) continue; SQLPropertyExpr value = (SQLPropertyExpr) condition.getValue(); - boolean toFieldNameIsEqual =value.getName().equals(toField); - boolean toAliasIsEqual = ((SQLIdentifierExpr) value.getOwner()).getName().equals(toAlias); + String[] valueAliasAndField = value.toString().split("\\.",2); + boolean toFieldNameIsEqual = valueAliasAndField[1].equals(toField); + boolean toAliasIsEqual = valueAliasAndField[0].equals(toAlias); boolean toIsEqual = toAliasIsEqual && toFieldNameIsEqual; if(toIsEqual) return true; From 3604aed4c4825144b70b8714ab87da050d61aea5 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Fri, 28 Aug 2015 20:34:45 +0300 Subject: [PATCH 054/559] add test for pr 82 to check new parser. --- src/test/java/org/nlpcn/es4sql/QueryTest.java | 6 ++++++ src/test/resources/game_of_thrones_complex.json | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java index f54549f4..384e3fa1 100644 --- a/src/test/java/org/nlpcn/es4sql/QueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java @@ -504,6 +504,12 @@ public void geoPolygon() throws SQLFeatureNotSupportedException, SqlParseExcepti Assert.assertEquals("square",result.getSource().get("description")); } + @Test + public void escapedCharactersCheck() throws IOException, SqlParseException, SQLFeatureNotSupportedException{ + SearchHits response = query(String.format("SELECT * FROM %s/gotCharacters where nickname = 'Daenerys \"Stormborn\"' LIMIT 1000", TEST_INDEX)); + Assert.assertEquals(1, response.getTotalHits()); + } + @Test public void complexObjectSearch() throws IOException, SqlParseException, SQLFeatureNotSupportedException{ SearchHits response = query(String.format("SELECT * FROM %s/gotCharacters where name.firstname = 'Jaime' LIMIT 1000", TEST_INDEX)); diff --git a/src/test/resources/game_of_thrones_complex.json b/src/test/resources/game_of_thrones_complex.json index 08dcff35..2f576424 100644 --- a/src/test/resources/game_of_thrones_complex.json +++ b/src/test/resources/game_of_thrones_complex.json @@ -1,5 +1,5 @@ {"index":{"_type": "gotCharacters", "_id":"1"}} -{"name":{"firstname":"Daenerys","lastname":"Targaryen","ofHerName":1},"house":"Targaryen","gender":"F","parents":{"father":"Aerys" , "mother":"Rhaella"},"titles":["motherOfDragons","queenOfTheAndals","breakerOfChains","Khaleesi"]} +{"name":{"firstname":"Daenerys","lastname":"Targaryen","ofHerName":1},"nickname":"Daenerys \"Stormborn\"","house":"Targaryen","gender":"F","parents":{"father":"Aerys" , "mother":"Rhaella"},"titles":["motherOfDragons","queenOfTheAndals","breakerOfChains","Khaleesi"]} {"index":{"_type": "gotCharacters", "_id":"2"}} {"name":{"firstname":"Eddard","lastname":"Stark","ofHisName":1},"house":"Stark", "parents":{"father":"Rickard" , "mother":"Lyarra"} ,"gender":"M","titles":["lordOfWinterfell","wardenOfTheNorth","handOfTheKing"]} {"index":{"_type": "gotCharacters", "_id":"3"}} From 93bb2f3ae51f53c49a97169ecffb978d90cd955b Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sat, 29 Aug 2015 19:41:57 +0300 Subject: [PATCH 055/559] optimization to hashJoin with hint : /*! HASH_WITH_TERMS_FILTER*/ filter second table results on query --- .../plugin/nlpcn/HashJoinElasticExecutor.java | 145 ++++++++++++++---- .../java/org/nlpcn/es4sql/domain/Hint.java | 19 +++ .../org/nlpcn/es4sql/domain/JoinSelect.java | 8 + .../org/nlpcn/es4sql/parse/SqlParser.java | 12 +- .../es4sql/query/ESHashJoinQueryAction.java | 8 + .../query/HashJoinElasticRequestBuilder.java | 10 +- .../query/TableInJoinRequestBuilder.java | 10 ++ src/test/java/org/nlpcn/es4sql/JoinTests.java | 19 +++ .../java/org/nlpcn/es4sql/SqlParserTests.java | 17 ++ 9 files changed, 212 insertions(+), 36 deletions(-) create mode 100644 src/main/java/org/nlpcn/es4sql/domain/Hint.java diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java index 94afb2bf..2015781d 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java @@ -6,6 +6,10 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.index.query.BoolFilterBuilder; +import org.elasticsearch.index.query.BoolQueryBuilder; +import org.elasticsearch.index.query.FilterBuilders; +import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestChannel; import org.elasticsearch.rest.RestStatus; @@ -14,12 +18,16 @@ import org.elasticsearch.search.internal.InternalSearchHit; import org.elasticsearch.search.internal.InternalSearchHits; import org.nlpcn.es4sql.domain.Field; +import org.nlpcn.es4sql.domain.Select; +import org.nlpcn.es4sql.domain.Where; +import org.nlpcn.es4sql.exception.SqlParseException; import org.nlpcn.es4sql.query.HashJoinElasticRequestBuilder; import org.nlpcn.es4sql.query.TableInJoinRequestBuilder; +import org.nlpcn.es4sql.query.maker.FilterMaker; +import org.nlpcn.es4sql.query.maker.QueryMaker; import java.io.IOException; import java.util.*; -import java.util.regex.MatchResult; /** * Created by Eliran on 22/8/2015. @@ -28,10 +36,11 @@ public class HashJoinElasticExecutor { private HashJoinElasticRequestBuilder requestBuilder; private SearchHits results ; private Client client; - + private boolean useQueryTermsFilterOptimization = false; public HashJoinElasticExecutor(Client client,HashJoinElasticRequestBuilder requestBuilder) { this.client = client; this.requestBuilder = requestBuilder; + this.useQueryTermsFilterOptimization = requestBuilder.isUseTermFiltersOptimization(); } public SearchHits getHits(){ @@ -72,37 +81,35 @@ private String resultAsString() throws IOException { return builder.string(); } - public void run() throws IOException { - TableInJoinRequestBuilder firstTableRequest = requestBuilder.getFirstTable(); - SearchHits firstTableHits = firstTableRequest.getRequestBuilder().get().getHits(); - Map comparisonKeyToSearchHits = new HashMap<>(); + public void run() throws IOException, SqlParseException { + Map> optimizationTermsFilterStructure = new HashMap<>(); List> t1ToT2FieldsComparison = requestBuilder.getT1ToT2FieldsComparison(); - int ids = 1; - for(SearchHit hit : firstTableHits){ - String key = getComparisonKey(t1ToT2FieldsComparison, hit,true); - SearchHitsResult currentSearchHitsResult = comparisonKeyToSearchHits.get(key); - if(currentSearchHitsResult == null) { - currentSearchHitsResult = new SearchHitsResult(new ArrayList(),false); - comparisonKeyToSearchHits.put(key,currentSearchHitsResult); - } - //int docid , id - InternalSearchHit searchHit = new InternalSearchHit(ids, hit.id(), new StringText(hit.getType()), hit.getFields()); - searchHit.sourceRef(hit.getSourceRef()); - onlyReturnedFields(searchHit.sourceAsMap(), firstTableRequest.getReturnedFields()); - ids++; - currentSearchHitsResult.getSearchHits().add(searchHit); + TableInJoinRequestBuilder firstTableRequest = requestBuilder.getFirstTable(); + Map comparisonKeyToSearchHits = createKeyToResultsAndFillOptimizationStructure(optimizationTermsFilterStructure, t1ToT2FieldsComparison, firstTableRequest); + + TableInJoinRequestBuilder secondTableRequest = requestBuilder.getSecondTable(); + if(needToOptimize(optimizationTermsFilterStructure)){ + updateRequestWithTermsFilter(optimizationTermsFilterStructure, secondTableRequest); } + List combinedResult = createCombinedResults(optimizationTermsFilterStructure, t1ToT2FieldsComparison, comparisonKeyToSearchHits, secondTableRequest); - ids = 0; - List finalResult = new ArrayList<>(); - TableInJoinRequestBuilder secondTableRequest = requestBuilder.getSecondTable(); + if(requestBuilder.getJoinType() == SQLJoinTableSource.JoinType.LEFT_OUTER_JOIN){ + addUnmatchedResults(combinedResult,comparisonKeyToSearchHits.values(),requestBuilder.getSecondTable().getReturnedFields(),combinedResult.size()); + } + InternalSearchHit[] hits = combinedResult.toArray(new InternalSearchHit[combinedResult.size()]); + this.results = new InternalSearchHits(hits,combinedResult.size(),1.0f); + } + + private List createCombinedResults(Map> optimizationTermsFilterStructure, List> t1ToT2FieldsComparison, Map comparisonKeyToSearchHits, TableInJoinRequestBuilder secondTableRequest) { + List combinedResult = new ArrayList<>(); + int resultIds = 0; SearchHits secondTableHits = secondTableRequest.getRequestBuilder().get().getHits(); for(SearchHit secondTableHit : secondTableHits){ - String key = getComparisonKey(t1ToT2FieldsComparison,secondTableHit,false); + String key = getComparisonKey(t1ToT2FieldsComparison,secondTableHit,false, optimizationTermsFilterStructure); SearchHitsResult searchHitsResult = comparisonKeyToSearchHits.get(key); @@ -113,27 +120,79 @@ public void run() throws IOException { onlyReturnedFields(secondTableHit.sourceAsMap(), secondTableRequest.getReturnedFields()); //todo: decide which id to put or type. or maby its ok this way. just need to doc. - InternalSearchHit searchHit = new InternalSearchHit(ids, matchingHit.id() + "|" + secondTableHit.getId(), new StringText(matchingHit.getType() + "|" + secondTableHit.getType()), matchingHit.getFields()); + InternalSearchHit searchHit = new InternalSearchHit(resultIds, matchingHit.id() + "|" + secondTableHit.getId(), new StringText(matchingHit.getType() + "|" + secondTableHit.getType()), matchingHit.getFields()); searchHit.sourceRef(matchingHit.getSourceRef()); searchHit.sourceAsMap().clear(); searchHit.sourceAsMap().putAll(matchingHit.sourceAsMap()); mergeSourceAndAddAliases(secondTableHit.getSource(), searchHit); - finalResult.add(searchHit); - ids++; + combinedResult.add(searchHit); + resultIds++; } } } + return combinedResult; + } - if(requestBuilder.getJoinType() == SQLJoinTableSource.JoinType.LEFT_OUTER_JOIN){ - addUnmatchedResults(finalResult,comparisonKeyToSearchHits.values(),requestBuilder.getSecondTable().getReturnedFields(),ids); + private Map createKeyToResultsAndFillOptimizationStructure(Map> optimizationTermsFilterStructure, List> t1ToT2FieldsComparison, TableInJoinRequestBuilder firstTableRequest) { + SearchHits firstTableHits = firstTableRequest.getRequestBuilder().get().getHits(); + Map comparisonKeyToSearchHits = new HashMap<>(); + + int resultIds = 1; + for(SearchHit hit : firstTableHits){ + String key = getComparisonKey(t1ToT2FieldsComparison, hit,true,optimizationTermsFilterStructure); + SearchHitsResult currentSearchHitsResult = comparisonKeyToSearchHits.get(key); + if(currentSearchHitsResult == null) { + currentSearchHitsResult = new SearchHitsResult(new ArrayList(),false); + comparisonKeyToSearchHits.put(key,currentSearchHitsResult); + } + //int docid , id + InternalSearchHit searchHit = new InternalSearchHit(resultIds, hit.id(), new StringText(hit.getType()), hit.getFields()); + searchHit.sourceRef(hit.getSourceRef()); + + onlyReturnedFields(searchHit.sourceAsMap(), firstTableRequest.getReturnedFields()); + resultIds++; + currentSearchHitsResult.getSearchHits().add(searchHit); } - InternalSearchHit[] hits = finalResult.toArray(new InternalSearchHit[ids]); - this.results = new InternalSearchHits(hits,ids,1.0f); + return comparisonKeyToSearchHits; + } + private boolean needToOptimize(Map> optimizationTermsFilterStructure) { + return useQueryTermsFilterOptimization && optimizationTermsFilterStructure!=null && optimizationTermsFilterStructure.size()>0; } - private void addUnmatchedResults(List finalResult, Collection firstTableSearchHits, List secondTableReturnedFields,int currentNumOfIds) { + private void updateRequestWithTermsFilter(Map> optimizationTermsFilterStructure, TableInJoinRequestBuilder secondTableRequest) throws SqlParseException { + Select select = secondTableRequest.getOriginalSelect(); + //todo: change to list with or when more than one object is allowed, and do foreach on map + BoolFilterBuilder orFilter = FilterBuilders.boolFilter(); + BoolQueryBuilder orQuery = QueryBuilders.boolQuery(); + for(Map.Entry> keyToValues : optimizationTermsFilterStructure.entrySet()){ + String fieldName = keyToValues.getKey(); + List values = keyToValues.getValue(); + if(select.isQuery) orQuery.should(QueryBuilders.termsQuery(fieldName,values)); + else orFilter.should(FilterBuilders.termsFilter(fieldName,values)); + } + Where where = select.getWhere(); + if (select.isQuery) { + BoolQueryBuilder boolQuery; + if(where != null ){ + boolQuery = QueryMaker.explan(where); + boolQuery.must(orQuery); + } + else boolQuery = orQuery; + secondTableRequest.getRequestBuilder().setQuery(boolQuery); + } else { + BoolFilterBuilder boolFilter; + if(where!=null) { + boolFilter = FilterMaker.explan(where); + boolFilter.must(orFilter); + } + else boolFilter = orFilter; + secondTableRequest.getRequestBuilder().setQuery(QueryBuilders.filteredQuery(null, boolFilter)); + } + } + + private void addUnmatchedResults(List combinedResults, Collection firstTableSearchHits, List secondTableReturnedFields,int currentNumOfIds) { for(SearchHitsResult hitsResult : firstTableSearchHits){ if(!hitsResult.isMatchedWithOtherTable()){ for(SearchHit hit: hitsResult.getSearchHits() ) { @@ -145,7 +204,7 @@ private void addUnmatchedResults(List finalResult, Collection Map emptySecondTableHitSource = createNullsSource(secondTableReturnedFields); mergeSourceAndAddAliases(emptySecondTableHitSource, searchHit); - finalResult.add(searchHit); + combinedResults.add(searchHit); currentNumOfIds++; } } @@ -160,7 +219,7 @@ private Map createNullsSource(List secondTableReturnedFie return nulledSource; } - private String getComparisonKey(List> t1ToT2FieldsComparison, SearchHit hit, boolean firstTable) { + private String getComparisonKey(List> t1ToT2FieldsComparison, SearchHit hit, boolean firstTable, Map> optimizationTermsFilterStructure) { String key = ""; Map sourceAsMap = hit.sourceAsMap(); for(Map.Entry t1ToT2 : t1ToT2FieldsComparison){ @@ -170,6 +229,9 @@ private String getComparisonKey(List> t1ToT2FieldsCompar else name = t1ToT2.getValue().getName(); Object data = deepSearchInMap(sourceAsMap, name); + if(firstTable && useQueryTermsFilterOptimization){ + updateOptimizationData(optimizationTermsFilterStructure, data, t1ToT2.getValue().getName()); + } if(data == null) key+="|null|"; else @@ -178,6 +240,19 @@ private String getComparisonKey(List> t1ToT2FieldsCompar return key; } + private void updateOptimizationData(Map> optimizationTermsFilterStructure, Object data, String queryOptimizationKey) { + List values = optimizationTermsFilterStructure.get(queryOptimizationKey); + if(values == null){ + values = new ArrayList<>(); + optimizationTermsFilterStructure.put(queryOptimizationKey,values); + } + if(data instanceof String){ + //todo: analyzed or not analyzed check.. + data = ((String) data).toLowerCase(); + } + values.add(data); + } + private void mergeSourceAndAddAliases(Map secondTableHitSource, InternalSearchHit searchHit) { Map results = mapWithAliases(searchHit.getSource(), requestBuilder.getFirstTable().getAlias()); results.putAll(mapWithAliases(secondTableHitSource, requestBuilder.getSecondTable().getAlias())); @@ -220,4 +295,6 @@ private Object deepSearchInMap(Map fieldsMap, String name) { return fieldsMap.get(name); } + + } diff --git a/src/main/java/org/nlpcn/es4sql/domain/Hint.java b/src/main/java/org/nlpcn/es4sql/domain/Hint.java new file mode 100644 index 00000000..06112399 --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/domain/Hint.java @@ -0,0 +1,19 @@ +package org.nlpcn.es4sql.domain; + +import org.nlpcn.es4sql.exception.SqlParseException; + +/** + * Created by Eliran on 29/8/2015. + */ +public enum Hint { + HASH_WITH_TERMS_FILTER; + + public static Hint hintFromString(String hint){ + switch (hint) { + case "! HASH_WITH_TERMS_FILTER": + return HASH_WITH_TERMS_FILTER; + default: + return null; + } + } +} diff --git a/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java b/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java index a21fb531..6a9978f4 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java +++ b/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java @@ -13,6 +13,7 @@ public class JoinSelect { private TableOnJoinSelect firstTable; private TableOnJoinSelect secondTable; private List connectedConditions; + private List hints; private SQLJoinTableSource.JoinType joinType; @@ -49,4 +50,11 @@ public void setJoinType(SQLJoinTableSource.JoinType joinType) { this.joinType = joinType; } + public List getHints() { + return hints; + } + + public void setHints(List hints) { + this.hints = hints; + } } diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index 35cd111b..57a4dd0a 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -325,7 +325,8 @@ public JoinSelect parseJoinSelect(SQLQueryExpr sqlExpr) throws SqlParseException throw new RuntimeException("currently supports only 2 tables join"); JoinSelect joinSelect = createBasicJoinSelectAccordingToTableSource((SQLJoinTableSource) query.getFrom()); - + List hints = parseHints(query.getHints()); + joinSelect.setHints(hints); String firstTableAlias = joinedFrom.get(0).getAlias(); String secondTableAlias = joinedFrom.get(1).getAlias(); Map aliasToWhere = splitAndFindWhere(query.getWhere(), firstTableAlias, secondTableAlias); @@ -338,6 +339,15 @@ public JoinSelect parseJoinSelect(SQLQueryExpr sqlExpr) throws SqlParseException return joinSelect; } + private List parseHints(List sqlHints) { + List hints = new ArrayList<>(); + for (SQLCommentHint sqlHint : sqlHints) { + Hint hint = Hint.hintFromString(sqlHint.getText()); + if (hint != null) hints.add(hint); + } + return hints; + } + private JoinSelect createBasicJoinSelectAccordingToTableSource(SQLJoinTableSource joinTableSource) throws SqlParseException { JoinSelect joinSelect = new JoinSelect(); List conditions = getJoinConditionsFlatten(joinTableSource); diff --git a/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java b/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java index 533e2abd..dab2775e 100644 --- a/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java +++ b/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java @@ -33,9 +33,16 @@ public SqlElasticRequestBuilder explain() throws SqlParseException { hashRequest.setT1ToT2FieldsComparison(comparisonFields); hashRequest.setJoinType(joinSelect.getJoinType()); + + updateHashRequestWithHints(hashRequest); + return hashRequest; } + private void updateHashRequestWithHints(HashJoinElasticRequestBuilder hashRequest) { + hashRequest.setUseTermFiltersOptimization(joinSelect.getHints().contains(Hint.HASH_WITH_TERMS_FILTER)); + } + private List> getComparisonFields(String t1Alias, String t2Alias, List connectedConditions) throws SqlParseException { List> comparisonFields = new ArrayList<>(); for(Condition condition : connectedConditions){ @@ -63,6 +70,7 @@ private List> getComparisonFields(String t1Alias, String private void fillRequestBuilder(TableInJoinRequestBuilder requestBuilder,TableOnJoinSelect tableOnJoinSelect) throws SqlParseException { List connectedFields = tableOnJoinSelect.getConnectedFields(); addFieldsToSelectIfMissing(tableOnJoinSelect,connectedFields); + requestBuilder.setOriginalSelect(tableOnJoinSelect); DefaultQueryAction queryAction = new DefaultQueryAction(client,tableOnJoinSelect); queryAction.explain(); requestBuilder.setRequestBuilder(queryAction.getRequestBuilder()); diff --git a/src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java index dbd5438a..b9f246b8 100644 --- a/src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java +++ b/src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java @@ -27,7 +27,7 @@ public class HashJoinElasticRequestBuilder implements SqlElasticRequestBuilder{ private List> t1ToT2FieldsComparison; private SQLJoinTableSource.JoinType joinType; - + private boolean useTermFiltersOptimization; public HashJoinElasticRequestBuilder() { firstTable = new TableInJoinRequestBuilder(); secondTable = new TableInJoinRequestBuilder(); @@ -103,4 +103,12 @@ public TableInJoinRequestBuilder getFirstTable() { public TableInJoinRequestBuilder getSecondTable() { return secondTable; } + + public boolean isUseTermFiltersOptimization() { + return useTermFiltersOptimization; + } + + public void setUseTermFiltersOptimization(boolean useTermFiltersOptimization) { + this.useTermFiltersOptimization = useTermFiltersOptimization; + } } diff --git a/src/main/java/org/nlpcn/es4sql/query/TableInJoinRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/TableInJoinRequestBuilder.java index ecde4b82..23ab7e55 100644 --- a/src/main/java/org/nlpcn/es4sql/query/TableInJoinRequestBuilder.java +++ b/src/main/java/org/nlpcn/es4sql/query/TableInJoinRequestBuilder.java @@ -2,6 +2,7 @@ import org.elasticsearch.action.search.SearchRequestBuilder; import org.nlpcn.es4sql.domain.Field; +import org.nlpcn.es4sql.domain.Select; import java.util.List; @@ -12,6 +13,7 @@ public class TableInJoinRequestBuilder { private SearchRequestBuilder requestBuilder; private String alias; private List returnedFields; + private Select originalSelect; public TableInJoinRequestBuilder() { } @@ -39,4 +41,12 @@ public List getReturnedFields() { public void setReturnedFields(List returnedFields) { this.returnedFields = returnedFields; } + + public Select getOriginalSelect() { + return originalSelect; + } + + public void setOriginalSelect(Select originalSelect) { + this.originalSelect = originalSelect; + } } diff --git a/src/test/java/org/nlpcn/es4sql/JoinTests.java b/src/test/java/org/nlpcn/es4sql/JoinTests.java index 45baaf3a..fc1d01b1 100644 --- a/src/test/java/org/nlpcn/es4sql/JoinTests.java +++ b/src/test/java/org/nlpcn/es4sql/JoinTests.java @@ -45,6 +45,25 @@ public void joinParseCheckSelectedFieldsSplit() throws SqlParseException, SQLFea } + @Test + public void joinParseWithHintsCheckSelectedFieldsSplit() throws SqlParseException, SQLFeatureNotSupportedException, IOException { + String query = "SELECT /*! HASH_WITH_TERMS_FILTER*/ a.firstname ,a.lastname , a.gender ,d.name FROM elasticsearch-sql_test_index/people a " + + " JOIN elasticsearch-sql_test_index/dog d on d.holdersName = a.firstname " + + " WHERE " + + " (a.age > 10 OR a.balance > 2000)" + + " AND d.age > 1"; + String explainedQuery = hashJoinRunAndExplain(query); + boolean containTerms = explainedQuery.replaceAll("\\s+","").contains("\"terms\":{\"holdersName\":[\"daenerys\",\"hattie\",\"nanette\",\"dale\",\"elinor\",\"virginia\",\"dillard\",\"mcgee\",\"aurelia\",\"fulton\",\"burton\"]}"); + Assert.assertTrue(containTerms); + } + + private String hashJoinRunAndExplain(String query) throws IOException, SqlParseException, SQLFeatureNotSupportedException { + SearchDao searchDao = MainTestSuite.getSearchDao(); + HashJoinElasticRequestBuilder explain = (HashJoinElasticRequestBuilder) searchDao.explain(query); + HashJoinElasticExecutor executor = new HashJoinElasticExecutor(searchDao.getClient(), explain); + executor.run(); + return explain.explain(); + } private SearchHit[] hashJoinGetHits(String query) throws SqlParseException, SQLFeatureNotSupportedException, IOException { SearchDao searchDao = MainTestSuite.getSearchDao(); SqlElasticRequestBuilder explain = searchDao.explain(query); diff --git a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java index aba28e1e..d7537f81 100644 --- a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java +++ b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java @@ -188,6 +188,23 @@ public void joinConditionWithComplexObjectComparisonLeftSide() throws SQLFeature Assert.assertTrue("condition not exist: c.name.lastname = h.name",conditionExist(conditions, "c.name.lastname", "h.name", Condition.OPEAR.EQ)); } + @Test + public void hints() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + String query = String.format("select /*! SECOND_TABLE_FILTERS */ c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + + "use KEY (termsFilter) "+ + "JOIN %s/gotHouses h " + + "on c.name.lastname = h.name " + + "where c.name.firstname='Daenerys'", TEST_INDEX,TEST_INDEX); + JoinSelect joinSelect = parser.parseJoinSelect((SQLQueryExpr) queryToExpr(query)); + //use KEY (termsFilter) --> ((SQLExprTableSource)((SQLJoinTableSource)query.from).left).hints [com.alibaba.druid.sql.dialect.mysql.ast.MySqlUseIndexHint@2a742aa2] + ///*! SQL_NO_CACHE */ --> query.getHints() + ////*! SECOND_TABLE_FILTERS */ => query.getHints() CHOSEN :) + List conditions = joinSelect.getConnectedConditions(); + Assert.assertNotNull(conditions); + Assert.assertEquals(1,conditions.size()); + Assert.assertTrue("condition not exist: c.name.lastname = h.name",conditionExist(conditions, "c.name.lastname", "h.name", Condition.OPEAR.EQ)); + } + private SQLExpr queryToExpr(String query) { return new ElasticSqlExprParser(query).expr(); } From da0d65885494774a4ca53d082e8ce57915d3a87f Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Tue, 1 Sep 2015 08:12:27 +0300 Subject: [PATCH 056/559] add copy to druid jar on pom.xml and fix rest result for hashjoin --- pom.xml | 43 +++++++++++++++---- .../plugin/nlpcn/HashJoinElasticExecutor.java | 23 +++++++--- .../org/nlpcn/es4sql/parse/ElasticLexer.java | 1 - 3 files changed, 51 insertions(+), 16 deletions(-) diff --git a/pom.xml b/pom.xml index a03ce0ab..763ce0be 100644 --- a/pom.xml +++ b/pom.xml @@ -66,13 +66,6 @@ provided - - com.alibaba - fastjson - 1.1.41 - test - - org.hamcrest hamcrest-all @@ -87,6 +80,13 @@ test + + com.alibaba + fastjson + 1.1.41 + test + + com.alibaba druid @@ -131,7 +131,34 @@ - + + org.apache.maven.plugins + maven-dependency-plugin + 2.10 + + + copy + package + + copy + + + + + com.alibaba + druid + false + ${project.build.directory} + druid.jar + + + ${project.build.directory} + false + true + + + + maven-assembly-plugin diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java index 2015781d..fb37c956 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java @@ -1,6 +1,7 @@ package org.elasticsearch.plugin.nlpcn; import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource; +import com.google.common.collect.ImmutableMap; import org.elasticsearch.client.Client; import org.elasticsearch.common.text.StringText; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -35,6 +36,7 @@ public class HashJoinElasticExecutor { private HashJoinElasticRequestBuilder requestBuilder; private SearchHits results ; + private long tookImMilli; private Client client; private boolean useQueryTermsFilterOptimization = false; public HashJoinElasticExecutor(Client client,HashJoinElasticRequestBuilder requestBuilder) { @@ -58,7 +60,7 @@ public void sendResponse(RestChannel channel){ } //use our deserializer instead of results toXcontent because the source field is differnet from sourceAsMap. - private String resultAsString() throws IOException { + public String resultAsString() throws IOException { Object[] searchHits; searchHits = new Object[(int) this.results.totalHits()]; int i = 0; @@ -71,17 +73,23 @@ private String resultAsString() throws IOException { searchHits[i] = value; i++; } + HashMap hits = new HashMap<>(); + hits.put("total",this.results.totalHits()); + hits.put("max_score",this.results.maxScore()); + hits.put("hits",searchHits); XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint(); - - builder.startObject("hits"); - builder.field("total").value(this.results.totalHits()); - builder.field("max_score").value(this.results.maxScore()); - builder.array("hits",searchHits); + builder.startObject(); + builder.field("took", tookImMilli); + builder.field("timed_out",false); + builder.field("_shards",ImmutableMap.of("total",5,"successful",5,"failed",0)); + builder.field("hits",hits) ; builder.endObject(); + return builder.string(); } public void run() throws IOException, SqlParseException { + long timeBefore = System.currentTimeMillis(); Map> optimizationTermsFilterStructure = new HashMap<>(); List> t1ToT2FieldsComparison = requestBuilder.getT1ToT2FieldsComparison(); @@ -100,7 +108,8 @@ public void run() throws IOException, SqlParseException { } InternalSearchHit[] hits = combinedResult.toArray(new InternalSearchHit[combinedResult.size()]); this.results = new InternalSearchHits(hits,combinedResult.size(),1.0f); - + long timeAfter = System.currentTimeMillis(); + this.tookImMilli = timeAfter - timeBefore; } private List createCombinedResults(Map> optimizationTermsFilterStructure, List> t1ToT2FieldsComparison, Map comparisonKeyToSearchHits, TableInJoinRequestBuilder secondTableRequest) { diff --git a/src/main/java/org/nlpcn/es4sql/parse/ElasticLexer.java b/src/main/java/org/nlpcn/es4sql/parse/ElasticLexer.java index a8f8c41c..2adfe678 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/ElasticLexer.java +++ b/src/main/java/org/nlpcn/es4sql/parse/ElasticLexer.java @@ -1,7 +1,6 @@ package org.nlpcn.es4sql.parse; import com.alibaba.druid.sql.dialect.mysql.parser.MySqlLexer; -import com.alibaba.druid.sql.parser.Lexer; import com.alibaba.druid.sql.parser.ParserException; import com.alibaba.druid.sql.parser.Token; From 26b359c1e647807f95a3765e72d1dbea4cc3768a Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Fri, 4 Sep 2015 09:33:52 +0300 Subject: [PATCH 057/559] fixed metasearch results (total/successful/failed shards , timedout and time ) --- .../plugin/nlpcn/HashJoinElasticExecutor.java | 31 +++++++--- .../plugin/nlpcn/MetaSearchResult.java | 60 +++++++++++++++++++ 2 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 src/main/java/org/elasticsearch/plugin/nlpcn/MetaSearchResult.java diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java index fb37c956..ed9d3446 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java @@ -2,6 +2,7 @@ import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource; import com.google.common.collect.ImmutableMap; +import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; import org.elasticsearch.common.text.StringText; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -36,13 +37,14 @@ public class HashJoinElasticExecutor { private HashJoinElasticRequestBuilder requestBuilder; private SearchHits results ; - private long tookImMilli; + private MetaSearchResult metaResults; private Client client; private boolean useQueryTermsFilterOptimization = false; public HashJoinElasticExecutor(Client client,HashJoinElasticRequestBuilder requestBuilder) { this.client = client; this.requestBuilder = requestBuilder; this.useQueryTermsFilterOptimization = requestBuilder.isUseTermFiltersOptimization(); + metaResults = new MetaSearchResult(); } public SearchHits getHits(){ @@ -79,9 +81,11 @@ public String resultAsString() throws IOException { hits.put("hits",searchHits); XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint(); builder.startObject(); - builder.field("took", tookImMilli); - builder.field("timed_out",false); - builder.field("_shards",ImmutableMap.of("total",5,"successful",5,"failed",0)); + builder.field("took", metaResults.getTookImMilli()); + builder.field("timed_out",metaResults.isTimedOut()); + builder.field("_shards",ImmutableMap.of("total",metaResults.getTotalNumOfShards(), + "successful",metaResults.getSuccessfulShards() + ,"failed",metaResults.getFailedShards())); builder.field("hits",hits) ; builder.endObject(); @@ -108,14 +112,16 @@ public void run() throws IOException, SqlParseException { } InternalSearchHit[] hits = combinedResult.toArray(new InternalSearchHit[combinedResult.size()]); this.results = new InternalSearchHits(hits,combinedResult.size(),1.0f); - long timeAfter = System.currentTimeMillis(); - this.tookImMilli = timeAfter - timeBefore; + long joinTimeInMilli = System.currentTimeMillis() - timeBefore; + this.metaResults.setTookImMilli(joinTimeInMilli); } private List createCombinedResults(Map> optimizationTermsFilterStructure, List> t1ToT2FieldsComparison, Map comparisonKeyToSearchHits, TableInJoinRequestBuilder secondTableRequest) { List combinedResult = new ArrayList<>(); int resultIds = 0; - SearchHits secondTableHits = secondTableRequest.getRequestBuilder().get().getHits(); + SearchResponse searchResponse = secondTableRequest.getRequestBuilder().get(); + updateMetaSearchResults(searchResponse); + SearchHits secondTableHits = searchResponse.getHits(); for(SearchHit secondTableHit : secondTableHits){ String key = getComparisonKey(t1ToT2FieldsComparison,secondTableHit,false, optimizationTermsFilterStructure); @@ -144,7 +150,9 @@ private List createCombinedResults(Map> } private Map createKeyToResultsAndFillOptimizationStructure(Map> optimizationTermsFilterStructure, List> t1ToT2FieldsComparison, TableInJoinRequestBuilder firstTableRequest) { - SearchHits firstTableHits = firstTableRequest.getRequestBuilder().get().getHits(); + SearchResponse searchResponse = firstTableRequest.getRequestBuilder().get(); + updateMetaSearchResults(searchResponse); + SearchHits firstTableHits = searchResponse.getHits(); Map comparisonKeyToSearchHits = new HashMap<>(); int resultIds = 1; @@ -166,6 +174,13 @@ private Map createKeyToResultsAndFillOptimizationStruc return comparisonKeyToSearchHits; } + private void updateMetaSearchResults( SearchResponse searchResponse) { + this.metaResults.addSuccessfulShards(searchResponse.getSuccessfulShards()); + this.metaResults.addFailedShards(searchResponse.getFailedShards()); + this.metaResults.addTotalNumOfShards(searchResponse.getTotalShards()); + this.metaResults.updateTimeOut(searchResponse.isTimedOut()); + } + private boolean needToOptimize(Map> optimizationTermsFilterStructure) { return useQueryTermsFilterOptimization && optimizationTermsFilterStructure!=null && optimizationTermsFilterStructure.size()>0; } diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/MetaSearchResult.java b/src/main/java/org/elasticsearch/plugin/nlpcn/MetaSearchResult.java new file mode 100644 index 00000000..ef63ac2f --- /dev/null +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/MetaSearchResult.java @@ -0,0 +1,60 @@ +package org.elasticsearch.plugin.nlpcn; + +/** + * Created by Eliran on 4/9/2015. + */ +public class MetaSearchResult { + private long tookImMilli; + private int totalNumOfShards; + private int successfulShards; + private int failedShards; + private boolean isTimedOut; + + public MetaSearchResult() { + totalNumOfShards = 0; + failedShards = 0; + successfulShards = 0; + isTimedOut = false; + } + + public int getTotalNumOfShards() { + return totalNumOfShards; + } + + public int getSuccessfulShards() { + return successfulShards; + } + + public int getFailedShards() { + return failedShards; + } + + public boolean isTimedOut() { + return isTimedOut; + } + + public long getTookImMilli() { + return tookImMilli; + } + + public void setTookImMilli(long tookImMilli) { + this.tookImMilli = tookImMilli; + } + + public void addFailedShards(int shards){ + this.failedShards+=shards; + } + + public void addSuccessfulShards(int shards){ + this.successfulShards+=shards; + } + + public void addTotalNumOfShards(int shards){ + this.totalNumOfShards+=shards; + } + + public void updateTimeOut(boolean isTimedOut){ + this.isTimedOut = this.isTimedOut || isTimedOut; + } + +} From 4713a1b4956de721d41ad607b75c5164c315476a Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sat, 5 Sep 2015 10:28:07 +0300 Subject: [PATCH 058/559] bugfix optimization should be and not or . hints refactor+tests+multiple hints support --- .../plugin/nlpcn/HashJoinElasticExecutor.java | 16 ++--- .../java/org/nlpcn/es4sql/domain/Hint.java | 19 ----- .../org/nlpcn/es4sql/domain/JoinSelect.java | 2 + .../org/nlpcn/es4sql/domain/hints/Hint.java | 21 ++++++ .../es4sql/domain/hints/HintFactory.java | 39 ++++++++++ .../nlpcn/es4sql/domain/hints/HintType.java | 14 ++++ .../es4sql/parse/ElasticSqlExprParser.java | 14 ++++ .../org/nlpcn/es4sql/parse/SqlParser.java | 4 +- .../es4sql/query/ESHashJoinQueryAction.java | 11 ++- .../java/org/nlpcn/es4sql/SqlParserTests.java | 72 +++++++++++++++---- 10 files changed, 169 insertions(+), 43 deletions(-) delete mode 100644 src/main/java/org/nlpcn/es4sql/domain/Hint.java create mode 100644 src/main/java/org/nlpcn/es4sql/domain/hints/Hint.java create mode 100644 src/main/java/org/nlpcn/es4sql/domain/hints/HintFactory.java create mode 100644 src/main/java/org/nlpcn/es4sql/domain/hints/HintType.java diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java index ed9d3446..6415ab03 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java @@ -188,30 +188,30 @@ private boolean needToOptimize(Map> optimizationTermsFilter private void updateRequestWithTermsFilter(Map> optimizationTermsFilterStructure, TableInJoinRequestBuilder secondTableRequest) throws SqlParseException { Select select = secondTableRequest.getOriginalSelect(); //todo: change to list with or when more than one object is allowed, and do foreach on map - BoolFilterBuilder orFilter = FilterBuilders.boolFilter(); - BoolQueryBuilder orQuery = QueryBuilders.boolQuery(); + BoolFilterBuilder andFilter = FilterBuilders.boolFilter(); + BoolQueryBuilder andQuery = QueryBuilders.boolQuery(); for(Map.Entry> keyToValues : optimizationTermsFilterStructure.entrySet()){ String fieldName = keyToValues.getKey(); List values = keyToValues.getValue(); - if(select.isQuery) orQuery.should(QueryBuilders.termsQuery(fieldName,values)); - else orFilter.should(FilterBuilders.termsFilter(fieldName,values)); + if(select.isQuery) andQuery.must(QueryBuilders.termsQuery(fieldName,values)); + else andFilter.must(FilterBuilders.termsFilter(fieldName,values)); } Where where = select.getWhere(); if (select.isQuery) { BoolQueryBuilder boolQuery; if(where != null ){ boolQuery = QueryMaker.explan(where); - boolQuery.must(orQuery); + boolQuery.must(andQuery); } - else boolQuery = orQuery; + else boolQuery = andQuery; secondTableRequest.getRequestBuilder().setQuery(boolQuery); } else { BoolFilterBuilder boolFilter; if(where!=null) { boolFilter = FilterMaker.explan(where); - boolFilter.must(orFilter); + boolFilter.must(andFilter); } - else boolFilter = orFilter; + else boolFilter = andFilter; secondTableRequest.getRequestBuilder().setQuery(QueryBuilders.filteredQuery(null, boolFilter)); } } diff --git a/src/main/java/org/nlpcn/es4sql/domain/Hint.java b/src/main/java/org/nlpcn/es4sql/domain/Hint.java deleted file mode 100644 index 06112399..00000000 --- a/src/main/java/org/nlpcn/es4sql/domain/Hint.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.nlpcn.es4sql.domain; - -import org.nlpcn.es4sql.exception.SqlParseException; - -/** - * Created by Eliran on 29/8/2015. - */ -public enum Hint { - HASH_WITH_TERMS_FILTER; - - public static Hint hintFromString(String hint){ - switch (hint) { - case "! HASH_WITH_TERMS_FILTER": - return HASH_WITH_TERMS_FILTER; - default: - return null; - } - } -} diff --git a/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java b/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java index 6a9978f4..541ced62 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java +++ b/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java @@ -1,6 +1,8 @@ package org.nlpcn.es4sql.domain; import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource; +import org.nlpcn.es4sql.domain.hints.Hint; + import java.util.List; diff --git a/src/main/java/org/nlpcn/es4sql/domain/hints/Hint.java b/src/main/java/org/nlpcn/es4sql/domain/hints/Hint.java new file mode 100644 index 00000000..e1f92dc2 --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/domain/hints/Hint.java @@ -0,0 +1,21 @@ +package org.nlpcn.es4sql.domain.hints; + +/** + * Created by Eliran on 5/9/2015. + */ +public class Hint { + private HintType type; + private Object[] params; + public Hint(HintType type,Object[] params) { + this.type = type; + this.params = params; + } + + public HintType getType() { + return type; + } + + public Object[] getParams() { + return params; + } +} diff --git a/src/main/java/org/nlpcn/es4sql/domain/hints/HintFactory.java b/src/main/java/org/nlpcn/es4sql/domain/hints/HintFactory.java new file mode 100644 index 00000000..86078815 --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/domain/hints/HintFactory.java @@ -0,0 +1,39 @@ +package org.nlpcn.es4sql.domain.hints; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Eliran on 5/9/2015. + */ +public class HintFactory { + + public static Hint getHintFromString(String hintAsString){ + + if(hintAsString.equals("! HASH_WITH_TERMS_FILTER")) + return new Hint(HintType.HASH_WITH_TERMS_FILTER,null); + if(hintAsString.startsWith("! JOIN_TABLES_LIMIT")){ + String[] numbers = getParamsFromHint(hintAsString, "! JOIN_TABLES_LIMIT"); + List params = new ArrayList<>(); + for (String number : numbers){ + if(number.equals("null")){ + params.add(null); + } + else { + params.add(Integer.parseInt(number)); + } + } + + return new Hint(HintType.JOIN_LIMIT,params.toArray()); + } + return null; + } + + + private static String[] getParamsFromHint(String hint, String prefix) { + String onlyParams = hint.replace(prefix, "").replaceAll("\\s*\\(\\s*","").replaceAll("\\s*\\,\\s*", ",").replaceAll("\\s*\\)\\s*", ""); + return onlyParams.split(","); + } + + +} diff --git a/src/main/java/org/nlpcn/es4sql/domain/hints/HintType.java b/src/main/java/org/nlpcn/es4sql/domain/hints/HintType.java new file mode 100644 index 00000000..629242ea --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/domain/hints/HintType.java @@ -0,0 +1,14 @@ +package org.nlpcn.es4sql.domain.hints; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Eliran on 29/8/2015. + */ +public enum HintType +{ + HASH_WITH_TERMS_FILTER, + JOIN_LIMIT; + +} diff --git a/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java b/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java index 7156d62e..801e4744 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java @@ -1,8 +1,12 @@ package org.nlpcn.es4sql.parse; +import com.alibaba.druid.sql.ast.SQLCommentHint; import com.alibaba.druid.sql.dialect.mysql.parser.MySqlExprParser; import com.alibaba.druid.sql.dialect.mysql.parser.MySqlLexer; import com.alibaba.druid.sql.parser.Lexer; +import com.alibaba.druid.sql.parser.Token; + +import java.util.List; /** * Created by Eliran on 18/8/2015. @@ -16,4 +20,14 @@ public ElasticSqlExprParser(String sql) { this(new ElasticLexer(sql)); this.lexer.nextToken(); } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + @Override + public void parseHints(List hints) { + while (lexer.token() == Token.HINT) { + hints.add(new SQLCommentHint(lexer.stringVal())); + lexer.nextToken(); + } + } + } diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index 57a4dd0a..c2917205 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -11,6 +11,8 @@ import org.nlpcn.es4sql.domain.*; import org.nlpcn.es4sql.domain.Where.CONN; +import org.nlpcn.es4sql.domain.hints.Hint; +import org.nlpcn.es4sql.domain.hints.HintFactory; import org.nlpcn.es4sql.exception.SqlParseException; import org.nlpcn.es4sql.spatial.SpatialParamsFactory; @@ -342,7 +344,7 @@ public JoinSelect parseJoinSelect(SQLQueryExpr sqlExpr) throws SqlParseException private List parseHints(List sqlHints) { List hints = new ArrayList<>(); for (SQLCommentHint sqlHint : sqlHints) { - Hint hint = Hint.hintFromString(sqlHint.getText()); + Hint hint = HintFactory.getHintFromString(sqlHint.getText()); if (hint != null) hints.add(hint); } return hints; diff --git a/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java b/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java index dab2775e..417aeaa6 100644 --- a/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java +++ b/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java @@ -2,6 +2,8 @@ import org.elasticsearch.client.Client; import org.nlpcn.es4sql.domain.*; +import org.nlpcn.es4sql.domain.hints.Hint; +import org.nlpcn.es4sql.domain.hints.HintType; import org.nlpcn.es4sql.exception.SqlParseException; import java.util.*; @@ -40,7 +42,14 @@ public SqlElasticRequestBuilder explain() throws SqlParseException { } private void updateHashRequestWithHints(HashJoinElasticRequestBuilder hashRequest) { - hashRequest.setUseTermFiltersOptimization(joinSelect.getHints().contains(Hint.HASH_WITH_TERMS_FILTER)); + boolean foundTermsFilterHint = false; + for(Hint hint : joinSelect.getHints()){ + if(hint.getType() == HintType.HASH_WITH_TERMS_FILTER) { + foundTermsFilterHint = true; + break; + } + } + hashRequest.setUseTermFiltersOptimization(foundTermsFilterHint); } private List> getComparisonFields(String t1Alias, String t2Alias, List connectedConditions) throws SqlParseException { diff --git a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java index d7537f81..8c4a8121 100644 --- a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java +++ b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java @@ -1,24 +1,21 @@ package org.nlpcn.es4sql; import com.alibaba.druid.sql.ast.SQLExpr; -import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr; import com.alibaba.druid.sql.ast.expr.SQLQueryExpr; -import org.elasticsearch.common.collect.ImmutableMap; -import org.elasticsearch.search.SearchHit; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.nlpcn.es4sql.domain.*; +import org.nlpcn.es4sql.domain.hints.Hint; +import org.nlpcn.es4sql.domain.hints.HintType; import org.nlpcn.es4sql.exception.SqlParseException; import org.nlpcn.es4sql.parse.ElasticSqlExprParser; import org.nlpcn.es4sql.parse.SqlParser; -import org.nlpcn.es4sql.query.ESHashJoinQueryAction; import java.io.IOException; import java.sql.SQLFeatureNotSupportedException; import java.util.List; -import java.util.Map; import static org.nlpcn.es4sql.TestsConstants.TEST_INDEX; @@ -188,23 +185,70 @@ public void joinConditionWithComplexObjectComparisonLeftSide() throws SQLFeature Assert.assertTrue("condition not exist: c.name.lastname = h.name",conditionExist(conditions, "c.name.lastname", "h.name", Condition.OPEAR.EQ)); } + @Test - public void hints() throws SQLFeatureNotSupportedException, IOException, SqlParseException { - String query = String.format("select /*! SECOND_TABLE_FILTERS */ c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + + public void limitHintsOnJoin() throws SqlParseException { + String query = String.format("select /*! JOIN_TABLES_LIMIT(1000,null) */ c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + "use KEY (termsFilter) "+ "JOIN %s/gotHouses h " + "on c.name.lastname = h.name " + "where c.name.firstname='Daenerys'", TEST_INDEX,TEST_INDEX); JoinSelect joinSelect = parser.parseJoinSelect((SQLQueryExpr) queryToExpr(query)); - //use KEY (termsFilter) --> ((SQLExprTableSource)((SQLJoinTableSource)query.from).left).hints [com.alibaba.druid.sql.dialect.mysql.ast.MySqlUseIndexHint@2a742aa2] - ///*! SQL_NO_CACHE */ --> query.getHints() - ////*! SECOND_TABLE_FILTERS */ => query.getHints() CHOSEN :) - List conditions = joinSelect.getConnectedConditions(); - Assert.assertNotNull(conditions); - Assert.assertEquals(1,conditions.size()); - Assert.assertTrue("condition not exist: c.name.lastname = h.name",conditionExist(conditions, "c.name.lastname", "h.name", Condition.OPEAR.EQ)); + List hints = joinSelect.getHints(); + Assert.assertNotNull(hints); + Assert.assertEquals("hints size was not 1", 1, hints.size()); + Hint hint = hints.get(0); + Assert.assertEquals(HintType.JOIN_LIMIT,hint.getType()); + Object[] params = hint.getParams(); + Assert.assertNotNull(params); + Assert.assertEquals("params size was not 2", 2, params.length); + Assert.assertEquals(1000,params[0]); + Assert.assertEquals(null,params[1]); } + @Test + public void hashTermsFilterHint() throws SqlParseException { + String query = String.format("select /*! HASH_WITH_TERMS_FILTER*/ c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + + "use KEY (termsFilter) "+ + "JOIN %s/gotHouses h " + + "on c.name.lastname = h.name " + + "where c.name.firstname='Daenerys'", TEST_INDEX,TEST_INDEX); + JoinSelect joinSelect = parser.parseJoinSelect((SQLQueryExpr) queryToExpr(query)); + List hints = joinSelect.getHints(); + Assert.assertNotNull(hints); + Assert.assertEquals("hints size was not 1", 1, hints.size()); + Hint hint = hints.get(0); + Assert.assertEquals(HintType.HASH_WITH_TERMS_FILTER,hint.getType()); + } + + @Test + public void multipleHints() throws SqlParseException { + String query = String.format("select /*! HASH_WITH_TERMS_FILTER*/ /*! JOIN_TABLES_LIMIT(1000,null) */ " + + " /*! JOIN_TABLES_LIMIT(100,200) */ " + + "c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + + "use KEY (termsFilter) "+ + "JOIN %s/gotHouses h " + + "on c.name.lastname = h.name " + + "where c.name.firstname='Daenerys'", TEST_INDEX,TEST_INDEX); + + JoinSelect joinSelect = parser.parseJoinSelect((SQLQueryExpr) queryToExpr(query)); + List hints = joinSelect.getHints(); + + Assert.assertNotNull(hints); + Assert.assertEquals("hints size was not 3", 3, hints.size()); + Hint firstHint = hints.get(0); + Assert.assertEquals(HintType.HASH_WITH_TERMS_FILTER, firstHint.getType()); + Hint secondHint = hints.get(1); + Assert.assertEquals(HintType.JOIN_LIMIT, secondHint.getType()); + Assert.assertEquals(1000,secondHint.getParams()[0]); + Assert.assertEquals(null,secondHint.getParams()[1]); + Hint thirdHint = hints.get(2); + Assert.assertEquals(100,thirdHint.getParams()[0]); + Assert.assertEquals(200,thirdHint.getParams()[1]); + Assert.assertEquals(HintType.JOIN_LIMIT, thirdHint.getType()); + } + + private SQLExpr queryToExpr(String query) { return new ElasticSqlExprParser(query).expr(); } From 5ef07eac57d0166488b989eaa6767bc4a24001f0 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sun, 6 Sep 2015 00:50:00 +0300 Subject: [PATCH 059/559] use scroll api when fetching for join (full on first table, several on second). added hint to control max size. --- .../plugin/nlpcn/HashJoinElasticExecutor.java | 139 ++++++++++++++---- .../org/nlpcn/es4sql/domain/JoinSelect.java | 13 ++ .../es4sql/domain/hints/HintFactory.java | 2 +- .../org/nlpcn/es4sql/parse/SqlParser.java | 11 +- .../es4sql/query/ESHashJoinQueryAction.java | 14 +- .../query/HashJoinElasticRequestBuilder.java | 10 ++ .../query/TableInJoinRequestBuilder.java | 9 ++ src/test/java/org/nlpcn/es4sql/JoinTests.java | 11 ++ 8 files changed, 173 insertions(+), 36 deletions(-) diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java index 6415ab03..500b0d75 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java @@ -2,9 +2,12 @@ import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource; import com.google.common.collect.ImmutableMap; +import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.action.search.SearchType; import org.elasticsearch.client.Client; import org.elasticsearch.common.text.StringText; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; @@ -40,6 +43,10 @@ public class HashJoinElasticExecutor { private MetaSearchResult metaResults; private Client client; private boolean useQueryTermsFilterOptimization = false; + + private final int MAX_RESULTS_ON_ONE_FETCH = 10000; + private final int MAX_RESULTS_FOR_FIRST_TABLE = 100000; + public HashJoinElasticExecutor(Client client,HashJoinElasticRequestBuilder requestBuilder) { this.client = client; this.requestBuilder = requestBuilder; @@ -107,8 +114,9 @@ public void run() throws IOException, SqlParseException { List combinedResult = createCombinedResults(optimizationTermsFilterStructure, t1ToT2FieldsComparison, comparisonKeyToSearchHits, secondTableRequest); - if(requestBuilder.getJoinType() == SQLJoinTableSource.JoinType.LEFT_OUTER_JOIN){ - addUnmatchedResults(combinedResult,comparisonKeyToSearchHits.values(),requestBuilder.getSecondTable().getReturnedFields(),combinedResult.size()); + int currentNumOfResults = combinedResult.size(); + if(requestBuilder.getJoinType() == SQLJoinTableSource.JoinType.LEFT_OUTER_JOIN && currentNumOfResults < requestBuilder.getTotalLimit()){ + addUnmatchedResults(combinedResult,comparisonKeyToSearchHits.values(),requestBuilder.getSecondTable().getReturnedFields(), currentNumOfResults); } InternalSearchHit[] hits = combinedResult.toArray(new InternalSearchHit[combinedResult.size()]); this.results = new InternalSearchHits(hits,combinedResult.size(),1.0f); @@ -119,40 +127,69 @@ public void run() throws IOException, SqlParseException { private List createCombinedResults(Map> optimizationTermsFilterStructure, List> t1ToT2FieldsComparison, Map comparisonKeyToSearchHits, TableInJoinRequestBuilder secondTableRequest) { List combinedResult = new ArrayList<>(); int resultIds = 0; - SearchResponse searchResponse = secondTableRequest.getRequestBuilder().get(); + int totalLimit = this.requestBuilder.getTotalLimit(); + Integer hintLimit = secondTableRequest.getHintLimit(); + SearchResponse searchResponse; + boolean finishedScrolling; + if(hintLimit!=null && hintLimit < MAX_RESULTS_ON_ONE_FETCH) { + searchResponse = secondTableRequest.getRequestBuilder().setSize(hintLimit).get(); + finishedScrolling = true; + } + else { + searchResponse = secondTableRequest.getRequestBuilder() + .setSearchType(SearchType.SCAN) + .setScroll(new TimeValue(60000)) + .setSize(MAX_RESULTS_ON_ONE_FETCH).get(); + searchResponse = client.prepareSearchScroll(searchResponse.getScrollId()).setScroll(new TimeValue(600000)).get(); + finishedScrolling = false; + } updateMetaSearchResults(searchResponse); - SearchHits secondTableHits = searchResponse.getHits(); - for(SearchHit secondTableHit : secondTableHits){ - - String key = getComparisonKey(t1ToT2FieldsComparison,secondTableHit,false, optimizationTermsFilterStructure); - - SearchHitsResult searchHitsResult = comparisonKeyToSearchHits.get(key); - if(searchHitsResult!=null && searchHitsResult.getSearchHits().size() > 0){ - searchHitsResult.setMatchedWithOtherTable(true); - List searchHits = searchHitsResult.getSearchHits(); - for(InternalSearchHit matchingHit : searchHits){ - onlyReturnedFields(secondTableHit.sourceAsMap(), secondTableRequest.getReturnedFields()); - - //todo: decide which id to put or type. or maby its ok this way. just need to doc. - InternalSearchHit searchHit = new InternalSearchHit(resultIds, matchingHit.id() + "|" + secondTableHit.getId(), new StringText(matchingHit.getType() + "|" + secondTableHit.getType()), matchingHit.getFields()); - searchHit.sourceRef(matchingHit.getSourceRef()); - searchHit.sourceAsMap().clear(); - searchHit.sourceAsMap().putAll(matchingHit.sourceAsMap()); - mergeSourceAndAddAliases(secondTableHit.getSource(), searchHit); - - combinedResult.add(searchHit); - resultIds++; + boolean limitReached =false; + int fetchedSoFarFromSecondTable = 0; + while(!limitReached ) { + SearchHit[] secondTableHits = searchResponse.getHits().getHits(); + fetchedSoFarFromSecondTable += secondTableHits.length; + for (SearchHit secondTableHit : secondTableHits) { + if (limitReached) break; + String key = getComparisonKey(t1ToT2FieldsComparison, secondTableHit, false, optimizationTermsFilterStructure); + + SearchHitsResult searchHitsResult = comparisonKeyToSearchHits.get(key); + + if (searchHitsResult != null && searchHitsResult.getSearchHits().size() > 0) { + searchHitsResult.setMatchedWithOtherTable(true); + List searchHits = searchHitsResult.getSearchHits(); + for (InternalSearchHit matchingHit : searchHits) { + onlyReturnedFields(secondTableHit.sourceAsMap(), secondTableRequest.getReturnedFields()); + + //todo: decide which id to put or type. or maby its ok this way. just need to doc. + InternalSearchHit searchHit = new InternalSearchHit(resultIds, matchingHit.id() + "|" + secondTableHit.getId(), new StringText(matchingHit.getType() + "|" + secondTableHit.getType()), matchingHit.getFields()); + searchHit.sourceRef(matchingHit.getSourceRef()); + searchHit.sourceAsMap().clear(); + searchHit.sourceAsMap().putAll(matchingHit.sourceAsMap()); + mergeSourceAndAddAliases(secondTableHit.getSource(), searchHit); + + combinedResult.add(searchHit); + resultIds++; + if (resultIds >= totalLimit) { + limitReached = true; + break; + } + } } } + if(!finishedScrolling){ + if(secondTableHits.length>0 && (hintLimit == null || fetchedSoFarFromSecondTable >= hintLimit)){ + searchResponse = client.prepareSearchScroll(searchResponse.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet(); + } + else break; + } } return combinedResult; } private Map createKeyToResultsAndFillOptimizationStructure(Map> optimizationTermsFilterStructure, List> t1ToT2FieldsComparison, TableInJoinRequestBuilder firstTableRequest) { - SearchResponse searchResponse = firstTableRequest.getRequestBuilder().get(); - updateMetaSearchResults(searchResponse); - SearchHits firstTableHits = searchResponse.getHits(); + List firstTableHits = fetchAllHits(firstTableRequest.getRequestBuilder(),firstTableRequest.getHintLimit()); Map comparisonKeyToSearchHits = new HashMap<>(); int resultIds = 1; @@ -174,6 +211,43 @@ private Map createKeyToResultsAndFillOptimizationStruc return comparisonKeyToSearchHits; } + private List fetchAllHits(SearchRequestBuilder requestBuilder, Integer hintLimit) { + if(hintLimit != null && hintLimit < MAX_RESULTS_ON_ONE_FETCH ) { + requestBuilder.setSize(hintLimit); + SearchResponse searchResponse = requestBuilder.get(); + updateMetaSearchResults(searchResponse); + return Arrays.asList(searchResponse.getHits().getHits()); + } + return scrollTillLimit(requestBuilder, hintLimit); + } + + private List scrollTillLimit(SearchRequestBuilder requestBuilder, Integer hintLimit) { + SearchResponse scrollResp = requestBuilder.setSearchType(SearchType.SCAN) + .setScroll(new TimeValue(60000)) + .setSize(MAX_RESULTS_ON_ONE_FETCH).get(); + + scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).get(); + updateMetaSearchResults(scrollResp); + List hitsWithScan = new ArrayList<>(); + int curentNumOfResults = 0; + SearchHit[] hits = scrollResp.getHits().hits(); + + if(hintLimit == null) hintLimit = MAX_RESULTS_FOR_FIRST_TABLE; + + while (hits.length != 0 && curentNumOfResults < hintLimit) { + curentNumOfResults += hits.length; + Collections.addAll(hitsWithScan, hits); + if(curentNumOfResults >= MAX_RESULTS_FOR_FIRST_TABLE ) { + //todo: log or exception? + System.out.println("too many results for first table, stoping at:" + curentNumOfResults); + break; + } + scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet(); + hits = scrollResp.getHits().getHits(); + } + return hitsWithScan; + } + private void updateMetaSearchResults( SearchResponse searchResponse) { this.metaResults.addSuccessfulShards(searchResponse.getSuccessfulShards()); this.metaResults.addFailedShards(searchResponse.getFailedShards()); @@ -193,8 +267,8 @@ private void updateRequestWithTermsFilter(Map> optimization for(Map.Entry> keyToValues : optimizationTermsFilterStructure.entrySet()){ String fieldName = keyToValues.getKey(); List values = keyToValues.getValue(); - if(select.isQuery) andQuery.must(QueryBuilders.termsQuery(fieldName,values)); - else andFilter.must(FilterBuilders.termsFilter(fieldName,values)); + if(select.isQuery) andQuery.must(QueryBuilders.termsQuery(fieldName, values)); + else andFilter.must(FilterBuilders.termsFilter(fieldName, values)); } Where where = select.getWhere(); if (select.isQuery) { @@ -217,6 +291,7 @@ private void updateRequestWithTermsFilter(Map> optimization } private void addUnmatchedResults(List combinedResults, Collection firstTableSearchHits, List secondTableReturnedFields,int currentNumOfIds) { + boolean limitReached = false; for(SearchHitsResult hitsResult : firstTableSearchHits){ if(!hitsResult.isMatchedWithOtherTable()){ for(SearchHit hit: hitsResult.getSearchHits() ) { @@ -230,8 +305,14 @@ private void addUnmatchedResults(List combinedResults, Collec combinedResults.add(searchHit); currentNumOfIds++; + if(currentNumOfIds >= requestBuilder.getTotalLimit()){ + limitReached = true; + break; + } + } } + if(limitReached) break; } } diff --git a/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java b/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java index 541ced62..fc0c2120 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java +++ b/src/main/java/org/nlpcn/es4sql/domain/JoinSelect.java @@ -16,6 +16,9 @@ public class JoinSelect { private TableOnJoinSelect secondTable; private List connectedConditions; private List hints; + private int totalLimit; + + private final int DEAFULT_NUM_OF_RESULTS = 200; private SQLJoinTableSource.JoinType joinType; @@ -23,6 +26,8 @@ public class JoinSelect { public JoinSelect() { firstTable = new TableOnJoinSelect(); secondTable = new TableOnJoinSelect(); + + totalLimit = DEAFULT_NUM_OF_RESULTS; } @@ -59,4 +64,12 @@ public List getHints() { public void setHints(List hints) { this.hints = hints; } + + public int getTotalLimit() { + return totalLimit; + } + + public void setTotalLimit(int totalLimit) { + this.totalLimit = totalLimit; + } } diff --git a/src/main/java/org/nlpcn/es4sql/domain/hints/HintFactory.java b/src/main/java/org/nlpcn/es4sql/domain/hints/HintFactory.java index 86078815..82486b9d 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/hints/HintFactory.java +++ b/src/main/java/org/nlpcn/es4sql/domain/hints/HintFactory.java @@ -16,7 +16,7 @@ public static Hint getHintFromString(String hintAsString){ String[] numbers = getParamsFromHint(hintAsString, "! JOIN_TABLES_LIMIT"); List params = new ArrayList<>(); for (String number : numbers){ - if(number.equals("null")){ + if(number.equals("null") || number.equals("infinity")){ params.add(null); } else { diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index c2917205..f5d44ff7 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -336,11 +336,19 @@ public JoinSelect parseJoinSelect(SQLQueryExpr sqlExpr) throws SqlParseException fillTableSelectedJoin(joinSelect.getFirstTable(), query, joinedFrom.get(0), aliasToWhere.get(firstTableAlias), joinSelect.getConnectedConditions()); fillTableSelectedJoin(joinSelect.getSecondTable(), query, joinedFrom.get(1), aliasToWhere.get(secondTableAlias), joinSelect.getConnectedConditions()); - //todo: throw error feature not supported: no group bys on joins ? + updateJoinLimit(query.getLimit(), joinSelect); + //todo: throw error feature not supported: no group bys on joins ? return joinSelect; } + private void updateJoinLimit(MySqlSelectQueryBlock.Limit limit, JoinSelect joinSelect) { + if(limit != null && limit.getRowCount()!= null) { + int sizeLimit = Integer.parseInt(limit.getRowCount().toString()); + joinSelect.setTotalLimit(sizeLimit); + } + } + private List parseHints(List sqlHints) { List hints = new ArrayList<>(); for (SQLCommentHint sqlHint : sqlHints) { @@ -397,7 +405,6 @@ private List getConnectedFields(List conditions, String alias) private void fillBasicTableSelectJoin(TableOnJoinSelect select, From from, Where where, MySqlSelectQueryBlock query) throws SqlParseException { select.getFrom().add(from); findSelect(query, select,from.getAlias()); - findLimit(query.getLimit(),select); select.setWhere(where); } diff --git a/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java b/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java index 417aeaa6..d8aebbac 100644 --- a/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java +++ b/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java @@ -1,5 +1,6 @@ package org.nlpcn.es4sql.query; +import org.elasticsearch.action.search.MultiSearchResponse; import org.elasticsearch.client.Client; import org.nlpcn.es4sql.domain.*; import org.nlpcn.es4sql.domain.hints.Hint; @@ -36,20 +37,25 @@ public SqlElasticRequestBuilder explain() throws SqlParseException { hashRequest.setJoinType(joinSelect.getJoinType()); + hashRequest.setTotalLimit(joinSelect.getTotalLimit()); + updateHashRequestWithHints(hashRequest); return hashRequest; } private void updateHashRequestWithHints(HashJoinElasticRequestBuilder hashRequest) { - boolean foundTermsFilterHint = false; for(Hint hint : joinSelect.getHints()){ if(hint.getType() == HintType.HASH_WITH_TERMS_FILTER) { - foundTermsFilterHint = true; - break; + hashRequest.setUseTermFiltersOptimization(true); + } + if(hint.getType() == HintType.JOIN_LIMIT){ + Object[] params = hint.getParams(); + hashRequest.getFirstTable().setHintLimit((Integer) params[0]); + hashRequest.getSecondTable().setHintLimit((Integer) params[1]); } } - hashRequest.setUseTermFiltersOptimization(foundTermsFilterHint); + } private List> getComparisonFields(String t1Alias, String t2Alias, List connectedConditions) throws SqlParseException { diff --git a/src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java index b9f246b8..c4621432 100644 --- a/src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java +++ b/src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java @@ -28,6 +28,8 @@ public class HashJoinElasticRequestBuilder implements SqlElasticRequestBuilder{ private List> t1ToT2FieldsComparison; private SQLJoinTableSource.JoinType joinType; private boolean useTermFiltersOptimization; + private int totalLimit; + public HashJoinElasticRequestBuilder() { firstTable = new TableInJoinRequestBuilder(); secondTable = new TableInJoinRequestBuilder(); @@ -111,4 +113,12 @@ public boolean isUseTermFiltersOptimization() { public void setUseTermFiltersOptimization(boolean useTermFiltersOptimization) { this.useTermFiltersOptimization = useTermFiltersOptimization; } + + public int getTotalLimit() { + return totalLimit; + } + + public void setTotalLimit(int totalLimit) { + this.totalLimit = totalLimit; + } } diff --git a/src/main/java/org/nlpcn/es4sql/query/TableInJoinRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/TableInJoinRequestBuilder.java index 23ab7e55..ffb33381 100644 --- a/src/main/java/org/nlpcn/es4sql/query/TableInJoinRequestBuilder.java +++ b/src/main/java/org/nlpcn/es4sql/query/TableInJoinRequestBuilder.java @@ -14,6 +14,7 @@ public class TableInJoinRequestBuilder { private String alias; private List returnedFields; private Select originalSelect; + private Integer hintLimit; public TableInJoinRequestBuilder() { } @@ -49,4 +50,12 @@ public Select getOriginalSelect() { public void setOriginalSelect(Select originalSelect) { this.originalSelect = originalSelect; } + + public Integer getHintLimit() { + return hintLimit; + } + + public void setHintLimit(Integer hintLimit) { + this.hintLimit = hintLimit; + } } diff --git a/src/test/java/org/nlpcn/es4sql/JoinTests.java b/src/test/java/org/nlpcn/es4sql/JoinTests.java index fc1d01b1..7b0b5b03 100644 --- a/src/test/java/org/nlpcn/es4sql/JoinTests.java +++ b/src/test/java/org/nlpcn/es4sql/JoinTests.java @@ -130,6 +130,17 @@ public void joinNoConditionAndNoWhere() throws SQLFeatureNotSupportedException, } + + @Test + public void joinNoConditionAndNoWhereWithTotalLimit() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + + String query = String.format("select c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + + "JOIN %s/gotHouses h LIMIT 10",TEST_INDEX,TEST_INDEX); + SearchHit[] hits = hashJoinGetHits(query); + Assert.assertEquals(10,hits.length); + + } + @Test public void joinWithNestedFieldsOnReturn() throws SQLFeatureNotSupportedException, IOException, SqlParseException { String query = String.format("select c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + From 0e85989cfc83cde26ace1d40ac0d176e01390015 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sun, 6 Sep 2015 22:40:48 +0300 Subject: [PATCH 060/559] hintLimit optimization and tests --- .../plugin/nlpcn/HashJoinElasticExecutor.java | 14 +++++++ src/test/java/org/nlpcn/es4sql/JoinTests.java | 38 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java index 500b0d75..03d1d6d2 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java @@ -104,6 +104,7 @@ public void run() throws IOException, SqlParseException { Map> optimizationTermsFilterStructure = new HashMap<>(); List> t1ToT2FieldsComparison = requestBuilder.getT1ToT2FieldsComparison(); + updateFirstTableLimitIfNeeded(); TableInJoinRequestBuilder firstTableRequest = requestBuilder.getFirstTable(); Map comparisonKeyToSearchHits = createKeyToResultsAndFillOptimizationStructure(optimizationTermsFilterStructure, t1ToT2FieldsComparison, firstTableRequest); @@ -124,6 +125,16 @@ public void run() throws IOException, SqlParseException { this.metaResults.setTookImMilli(joinTimeInMilli); } + private void updateFirstTableLimitIfNeeded() { + if(requestBuilder.getJoinType() == SQLJoinTableSource.JoinType.LEFT_OUTER_JOIN ){ + Integer firstTableHintLimit = requestBuilder.getFirstTable().getHintLimit(); + int totalLimit = requestBuilder.getTotalLimit(); + if(firstTableHintLimit == null || firstTableHintLimit > totalLimit){ + requestBuilder.getFirstTable().setHintLimit(totalLimit); + } + } + } + private List createCombinedResults(Map> optimizationTermsFilterStructure, List> t1ToT2FieldsComparison, Map comparisonKeyToSearchHits, TableInJoinRequestBuilder secondTableRequest) { List combinedResult = new ArrayList<>(); int resultIds = 0; @@ -184,6 +195,9 @@ private List createCombinedResults(Map> } else break; } + else { + break; + } } return combinedResult; } diff --git a/src/test/java/org/nlpcn/es4sql/JoinTests.java b/src/test/java/org/nlpcn/es4sql/JoinTests.java index 7b0b5b03..59953d34 100644 --- a/src/test/java/org/nlpcn/es4sql/JoinTests.java +++ b/src/test/java/org/nlpcn/es4sql/JoinTests.java @@ -189,5 +189,43 @@ public void testLeftJoin() throws SQLFeatureNotSupportedException, IOException, Assert.assertTrue(hitsContains(hits, secondMatch)); } + @Test + public void hintLimits_firstLimitSecondNull() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + + String query = String.format("select /*! JOIN_TABLES_LIMIT(2,null) */ c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + + "JOIN %s/gotHouses h ",TEST_INDEX,TEST_INDEX); + SearchHit[] hits = hashJoinGetHits(query); + Assert.assertEquals(6,hits.length); + } + + @Test + public void hintLimits_firstLimitSecondLimit() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + + String query = String.format("select /*! JOIN_TABLES_LIMIT(2,2) */ c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + + "JOIN %s/gotHouses h ",TEST_INDEX,TEST_INDEX); + SearchHit[] hits = hashJoinGetHits(query); + Assert.assertEquals(4,hits.length); + } + + @Test + public void hintLimits_firstNullSecondLimit() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + + String query = String.format("select /*! JOIN_TABLES_LIMIT(null,2) */ c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + + "JOIN %s/gotHouses h ",TEST_INDEX,TEST_INDEX); + SearchHit[] hits = hashJoinGetHits(query); + Assert.assertEquals(8,hits.length); + } + + @Test + public void testLeftJoinWithLimit() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + String query = String.format("select /*! JOIN_TABLES_LIMIT(3,null) */ c.name.firstname, f.name.firstname,f.name.lastname from %s/gotCharacters c " + + "LEFT JOIN %s/gotCharacters f " + + "on c.parents.father = f.name.firstname " + , TEST_INDEX,TEST_INDEX); + SearchHit[] hits = hashJoinGetHits(query); + Assert.assertEquals(3,hits.length); + + } + } From d8e6c2ea64fb18993c239f09143cbf67bb796d9f Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Fri, 11 Sep 2015 10:04:36 +0300 Subject: [PATCH 061/559] not like manual merge + test --- .../java/org/nlpcn/es4sql/parse/SqlParser.java | 10 +++++----- .../java/org/nlpcn/es4sql/query/maker/Maker.java | 2 +- src/test/java/org/nlpcn/es4sql/QueryTest.java | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index f5d44ff7..51e8061e 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -122,13 +122,13 @@ private void explanCond(String opear, SQLExpr expr, Where where) throws SqlParse parseValue(between.endExpr)}); where.addWhere(condition); } else if (expr instanceof SQLNotExpr){ - SQLBinaryOpExpr notExpr = (SQLBinaryOpExpr) ((SQLNotExpr) expr).getExpr(); + SQLBinaryOpExpr notExpr = (SQLBinaryOpExpr) ((SQLNotExpr) expr).getExpr(); String left = notExpr.getLeft().toString(); SQLExpr right = notExpr.getRight(); - // add a check here to see if the not'd value is a 'like' operator - Condition.OPEAR notOpear = notExpr.getOperator() == SQLBinaryOperator.Like ? Condition.OPEAR.NLIKE : Condition.OPEAR.N; - Condition condition = new Condition(CONN.valueOf(opear), left, notOpear, parseValue(right)); - where.addWhere(condition); + // add a check here to see if the not'd value is a 'like' operator + Condition.OPEAR notOpear = notExpr.getOperator() == SQLBinaryOperator.Like ? Condition.OPEAR.NLIKE : Condition.OPEAR.N; + Condition condition = new Condition(CONN.valueOf(opear), left, notOpear, parseValue(right)); + where.addWhere(condition); } else if (expr instanceof SQLMethodInvokeExpr) { SQLMethodInvokeExpr methodExpr = (SQLMethodInvokeExpr) expr; diff --git a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java index 519def13..04082cdf 100644 --- a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java +++ b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java @@ -143,7 +143,7 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar break; } case LIKE: - case NLIKE: + case NLIKE: String queryStr = ((String) value).replace('%', '*').replace('_', '?'); WildcardQueryBuilder wildcardQuery = QueryBuilders.wildcardQuery(name, queryStr); x = isQuery ? wildcardQuery : FilterBuilders.queryFilter(wildcardQuery); diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java index 384e3fa1..0b0a74aa 100644 --- a/src/test/java/org/nlpcn/es4sql/QueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java @@ -12,6 +12,7 @@ import org.nlpcn.es4sql.exception.SqlParseException; import org.nlpcn.es4sql.query.SqlElasticSearchRequestBuilder; +import javax.naming.directory.SearchControls; import java.io.IOException; import java.sql.SQLFeatureNotSupportedException; import java.text.ParseException; @@ -524,6 +525,19 @@ public void complexObjectReutrnField() throws IOException, SqlParseException, SQ Assert.assertEquals("Eddard",((HashMap)sourceAsMap.get("parents")).get("father")); } + + @Test + public void notLikeTests() throws IOException, SqlParseException, SQLFeatureNotSupportedException{ + //cant use string.format cause of %d + SearchHits response = query("SELECT name FROM " +TEST_INDEX + "/gotCharacters where name.firstname not like '%d' LIMIT 1000"); + Assert.assertEquals(3, response.getTotalHits()); + for(SearchHit hit : response.getHits()) { + Map sourceAsMap = hit.sourceAsMap(); + String name = ((HashMap) sourceAsMap.get("name")).get("firstname").toString(); + Assert.assertFalse(name+" was in not like %d",name.startsWith("d")); + } + } + private SearchHits query(String query) throws SqlParseException, SQLFeatureNotSupportedException, SQLFeatureNotSupportedException { SearchDao searchDao = MainTestSuite.getSearchDao(); SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) searchDao.explain(query); From f9874b3cd8a42157cedfe42f91f3d30b7c3ada4b Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Fri, 11 Sep 2015 18:39:22 +0300 Subject: [PATCH 062/559] bring datesearchBraces support back --- .../es4sql/parse/ElasticSqlExprParser.java | 34 ++++++++++ .../org/nlpcn/es4sql/parse/SQLOdbcExpr.java | 65 +++++++++++++++++++ src/test/java/org/nlpcn/es4sql/QueryTest.java | 36 +++++----- .../java/org/nlpcn/es4sql/SqlParserTests.java | 11 ++++ 4 files changed, 128 insertions(+), 18 deletions(-) create mode 100644 src/main/java/org/nlpcn/es4sql/parse/SQLOdbcExpr.java diff --git a/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java b/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java index 801e4744..f10dcfe8 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java @@ -1,9 +1,12 @@ package org.nlpcn.es4sql.parse; import com.alibaba.druid.sql.ast.SQLCommentHint; +import com.alibaba.druid.sql.ast.SQLExpr; +import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; import com.alibaba.druid.sql.dialect.mysql.parser.MySqlExprParser; import com.alibaba.druid.sql.dialect.mysql.parser.MySqlLexer; import com.alibaba.druid.sql.parser.Lexer; +import com.alibaba.druid.sql.parser.ParserException; import com.alibaba.druid.sql.parser.Token; import java.util.List; @@ -30,4 +33,35 @@ public void parseHints(List hints) { } } + @Override + public SQLExpr primary() { + + if(lexer.token() == Token.LBRACE){ + lexer.nextToken(); + boolean foundRBrace = false; + if(lexer.stringVal().equals("ts")){ + String current = lexer.stringVal(); + do { + if(current.equals(lexer.token().RBRACE.name())){ + foundRBrace = true; + break; + } + lexer.nextToken(); + current = lexer.token().name(); + }while(!foundRBrace && !current.trim().equals("")); + + if(foundRBrace){ + SQLOdbcExpr sdle = new SQLOdbcExpr(lexer.stringVal()); + + accept(Token.RBRACE); + return sdle; + }else{ + throw new ParserException("Error. Unable to find closing RBRACE"); + } + }else{ + throw new ParserException("Error. Unable to parse ODBC Literal Timestamp"); + } + } + return super.primary(); + } } diff --git a/src/main/java/org/nlpcn/es4sql/parse/SQLOdbcExpr.java b/src/main/java/org/nlpcn/es4sql/parse/SQLOdbcExpr.java new file mode 100644 index 00000000..6c5f2026 --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/parse/SQLOdbcExpr.java @@ -0,0 +1,65 @@ +package org.nlpcn.es4sql.parse; + +import com.alibaba.druid.sql.ast.expr.SQLCharExpr; +import com.alibaba.druid.sql.visitor.SQLASTVisitor; + + +/** + * Created by jheimbouch on 3/17/15. + */ +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + + +public class SQLOdbcExpr extends SQLCharExpr { + + private static final long serialVersionUID = 1L; + + public SQLOdbcExpr(){ + + } + + public SQLOdbcExpr(String text){ + super(text); + } + + @Override + public void output(StringBuffer buf) { + if ((this.text == null) || (this.text.length() == 0)) { + buf.append("NULL"); + } else { + buf.append("{ts '"); + buf.append(this.text.replaceAll("'", "''")); + buf.append("'}"); + } + } + + @Override + public String getText() { + StringBuilder sb = new StringBuilder(); + sb.append("{ts '"); + sb.append(this.text); + sb.append("'}"); + return sb.toString(); + } + + protected void accept0(SQLASTVisitor visitor) { + visitor.visit(this); + visitor.endVisit(this); + } +} + diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java index 0b0a74aa..ba460b4f 100644 --- a/src/test/java/org/nlpcn/es4sql/QueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java @@ -307,24 +307,24 @@ public void dateSearch() throws IOException, SqlParseException, SQLFeatureNotSup } } -// @Test -// public void dateSearchBraces() throws IOException, SqlParseException, SQLFeatureNotSupportedException, ParseException { -// DateTimeFormatter formatter = DateTimeFormat.forPattern(TS_DATE_FORMAT); -// DateTime dateToCompare = new DateTime(2015, 3, 15, 0, 0, 0); -// -// SearchHits response = query(String.format("SELECT insert_time FROM %s/odbc WHERE insert_time < {ts '2015-03-15 00:00:00.000'}", TEST_INDEX)); -// SearchHit[] hits = response.getHits(); -// for(SearchHit hit : hits) { -// Map source = hit.getSource(); -// String insertTimeStr = (String) source.get("insert_time"); -// insertTimeStr = insertTimeStr.replace("{ts '", "").replace("'}", ""); -// -// DateTime insertTime = formatter.parseDateTime(insertTimeStr); -// -// String errorMessage = String.format("insert_time must be smaller then 2015-03-15. found: %s", insertTime); -// Assert.assertTrue(errorMessage, insertTime.isBefore(dateToCompare)); -// } -// } + @Test + public void dateSearchBraces() throws IOException, SqlParseException, SQLFeatureNotSupportedException, ParseException { + DateTimeFormatter formatter = DateTimeFormat.forPattern(TS_DATE_FORMAT); + DateTime dateToCompare = new DateTime(2015, 3, 15, 0, 0, 0); + + SearchHits response = query(String.format("SELECT insert_time FROM %s/odbc WHERE insert_time < {ts '2015-03-15 00:00:00.000'}", TEST_INDEX)); + SearchHit[] hits = response.getHits(); + for(SearchHit hit : hits) { + Map source = hit.getSource(); + String insertTimeStr = (String) source.get("insert_time"); + insertTimeStr = insertTimeStr.replace("{ts '", "").replace("'}", ""); + + DateTime insertTime = formatter.parseDateTime(insertTimeStr); + + String errorMessage = String.format("insert_time must be smaller then 2015-03-15. found: %s", insertTime); + Assert.assertTrue(errorMessage, insertTime.isBefore(dateToCompare)); + } + } @Test diff --git a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java index 8c4a8121..91137196 100644 --- a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java +++ b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java @@ -15,6 +15,7 @@ import java.io.IOException; import java.sql.SQLFeatureNotSupportedException; +import java.util.LinkedList; import java.util.List; import static org.nlpcn.es4sql.TestsConstants.TEST_INDEX; @@ -248,7 +249,17 @@ public void multipleHints() throws SqlParseException { Assert.assertEquals(HintType.JOIN_LIMIT, thirdHint.getType()); } + @Test + public void searchWithOdbcTimeFormatParse() throws SqlParseException { + String query = String.format("SELECT insert_time FROM %s/odbc WHERE insert_time < {ts '2015-03-15 00:00:00.000'}", TEST_INDEX); + SQLExpr sqlExpr = queryToExpr(query); + Select select = parser.parseSelect((SQLQueryExpr) sqlExpr); + LinkedList wheres = select.getWhere().getWheres(); + Assert.assertEquals(1,wheres.size()); + Condition condition = (Condition) wheres.get(0); + Assert.assertEquals("{ts '2015-03-15 00:00:00.000'}",condition.getValue().toString()); + } private SQLExpr queryToExpr(String query) { return new ElasticSqlExprParser(query).expr(); } From 0ffa99179fd3a67998b54aca32e0507f5bf9e36c Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sat, 12 Sep 2015 18:09:42 +0300 Subject: [PATCH 063/559] #89 ,index name with spaces if surrounded with brackets --- .../es4sql/parse/ElasticSqlExprParser.java | 15 +++++ .../org/nlpcn/es4sql/parse/SqlParser.java | 2 +- .../nlpcn/es4sql/query/ESActionFactory.java | 58 +++---------------- .../java/org/nlpcn/es4sql/MainTestSuite.java | 5 +- .../java/org/nlpcn/es4sql/SqlParserTests.java | 40 +++++++++++++ 5 files changed, 68 insertions(+), 52 deletions(-) diff --git a/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java b/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java index f10dcfe8..4ea7839f 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java @@ -9,6 +9,7 @@ import com.alibaba.druid.sql.parser.ParserException; import com.alibaba.druid.sql.parser.Token; +import java.util.ArrayList; import java.util.List; /** @@ -62,6 +63,20 @@ public SQLExpr primary() { throw new ParserException("Error. Unable to parse ODBC Literal Timestamp"); } } + else if(lexer.token() == Token.LBRACKET){ + List identifiers = new ArrayList<>(); + lexer.nextToken(); + while(lexer.token()!=Token.RBRACKET){ + if(lexer.token() != Token.IDENTIFIER && lexer.token()!=Token.INDEX){ + throw new ParserException("All items between Brackets should be identifiers , got:" +lexer.token()); + } + identifiers.add(lexer.stringVal()); + lexer.nextToken(); + } + String identifier = String.join(" ", identifiers); + accept(Token.RBRACKET); + return new SQLIdentifierExpr(identifier); + } return super.primary(); } } diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index 51e8061e..2ef8505f 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -302,7 +302,7 @@ private List findFrom(SQLTableSource from) { if(isSqlExprTable){ SQLExprTableSource fromExpr = (SQLExprTableSource) from; - String[] split = fromExpr.getExpr().toString().replaceAll(" ","").split(","); + String[] split = fromExpr.getExpr().toString().split(","); ArrayList fromList = new ArrayList<>(); for (String source : split) { diff --git a/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java b/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java index c819d14e..a16fc56b 100644 --- a/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java +++ b/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java @@ -6,6 +6,7 @@ import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement; import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; import com.alibaba.druid.sql.parser.*; import com.alibaba.druid.util.JdbcUtils; import org.elasticsearch.client.Client; @@ -13,6 +14,7 @@ import org.nlpcn.es4sql.domain.JoinSelect; import org.nlpcn.es4sql.domain.Select; import org.nlpcn.es4sql.exception.SqlParseException; +import org.nlpcn.es4sql.parse.ElasticLexer; import org.nlpcn.es4sql.parse.ElasticSqlExprParser; import org.nlpcn.es4sql.parse.SqlParser; @@ -35,54 +37,6 @@ public static QueryAction create(Client client, String sql) throws SqlParseExcep if(isJoin(sqlExpr,sql)){ JoinSelect joinSelect = new SqlParser().parseJoinSelect(sqlExpr); return new ESHashJoinQueryAction(client,joinSelect); - //NestedLoopQueryAction(client) - //Join between two tables : s1 and s2 - // Query contains: - // fields: s1 connected fields , s2 connected fields , s1 fields for query - // s1 = select from SqlParser for one of them (need to take only first table wheres) - // c = conditions for crossed -> each condition is field = s2Field , value = s1Field - // s2 = select from SqlParser for the 2nd (need to take only 2nd table wheres) - - //NestedLoopsStrategy - // choose arbitrary one of them - // res1 = DefaultQueryAction(client, s1 -> add connectedFields to selected fields); - // for each r1 : res1 - // duplicate s2 ; s2.conditions += c(replace value to r1 values) - // res2_r1 = DefaultQueryAction(client,s2) - // foreach r2: res2 - // results_set+= union(r2.fields,r1.only_return_fields) - - //HashJoinStrategy - only when all equals - //FirstStrategy - //Choose arbitrary one of them - //res1 = DefaultQueryAction(client, s1 -> add connectedFields to selected fields); - // create HashMap - // foreach r1 : res1 - // str1 = String.concat(connectedFields.getValues(r1)) - // add str1,r1 to hashMap - // res2 = DefaultQueryAction(client, s2 -> add connectedFields to selected fields) - //foreach r2 : res2 - // str2 = String.concat(connectedFields.getValues(r1)) - // if map.contains(str2) - // results_set += union(r2.only_return_fields , r1.only_return_fields) - - //SecondStrategy - //res1 = DefaultQueryAction(client, s1 -> add connectedFields to selected fields); - // create dict = HashMap - // create filters = HashMap> - // foreach r1 : res1 - // str1 = String.concat(connectedFields.getValues(r1)) - // add str1,r1 to hashMap - // foreach cond : c - //filters[c.field2Name]+=c.value(r,field1Name) - //foreach f : filters - // s2.addFilter terms/in Filter (f.value) - // res2 = DefaultQueryAction(client, s2 -> add connectedFields to selected fields) - //foreach r2 : res2 - // str2 = String.concat(connectedFields.getValues(r1)) - // if map.contains(str2) - // results_set += union(r2.only_return_fields , r1.only_return_fields) - } else { Select select = new SqlParser().parseSelect(sqlExpr); @@ -94,7 +48,7 @@ public static QueryAction create(Client client, String sql) throws SqlParseExcep } } case "DELETE": - SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, JdbcUtils.MYSQL); + SQLStatementParser parser = createSqlStatementParser(sql); SQLDeleteStatement deleteStatement = parser.parseDeleteStatement(); Delete delete = new SqlParser().parseDelete(deleteStatement); return new DeleteQueryAction(client, delete); @@ -104,6 +58,12 @@ public static QueryAction create(Client client, String sql) throws SqlParseExcep } } + private static SQLStatementParser createSqlStatementParser(String sql) { + ElasticLexer lexer = new ElasticLexer(sql); + lexer.nextToken(); + return new MySqlStatementParser(lexer); + } + private static boolean isJoin(SQLQueryExpr sqlExpr,String sql) { MySqlSelectQueryBlock query = (MySqlSelectQueryBlock) sqlExpr.getSubQuery().getQuery(); return query.getFrom() instanceof SQLJoinTableSource && sql.toLowerCase().contains("join"); diff --git a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java index 8bb4b318..1b91865b 100644 --- a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java +++ b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java @@ -25,11 +25,12 @@ MethodQueryTest.class, AggregationTest.class, BugTest.class, + JoinTests.class, DeleteTest.class, ExplainTest.class, WktToGeoJsonConverterTests.class, - SqlParserTests.class, - JoinTests.class + SqlParserTests.class + }) public class MainTestSuite { diff --git a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java index 91137196..3fd3c02d 100644 --- a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java +++ b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java @@ -260,6 +260,46 @@ public void searchWithOdbcTimeFormatParse() throws SqlParseException { Assert.assertEquals("{ts '2015-03-15 00:00:00.000'}",condition.getValue().toString()); } + + @Test + public void indexWithSpacesWithinBrackets() throws SqlParseException { + String query = "SELECT insert_time FROM [Test Index] WHERE age > 3"; + SQLExpr sqlExpr = queryToExpr(query); + Select select = parser.parseSelect((SQLQueryExpr) sqlExpr); + List fromList = select.getFrom(); + Assert.assertEquals(1, fromList.size()); + From from = fromList.get(0); + Assert.assertEquals("Test Index",from.getIndex()); + } + + @Test + public void indexWithSpacesWithTypeWithinBrackets() throws SqlParseException { + String query = "SELECT insert_time FROM [Test Index]/type1 WHERE age > 3"; + SQLExpr sqlExpr = queryToExpr(query); + Select select = parser.parseSelect((SQLQueryExpr) sqlExpr); + List fromList = select.getFrom(); + Assert.assertEquals(1, fromList.size()); + From from = fromList.get(0); + Assert.assertEquals("Test Index",from.getIndex()); + Assert.assertEquals("type1",from.getType()); + } + @Test + public void twoIndices() throws SqlParseException { + String query = "SELECT insert_time FROM index1/type1 , index2/type2 WHERE age > 3"; + SQLExpr sqlExpr = queryToExpr(query); + Select select = parser.parseSelect((SQLQueryExpr) sqlExpr); + List fromList = select.getFrom(); + Assert.assertEquals(2, fromList.size()); + From from1 = fromList.get(0); + From from2 = fromList.get(1); + boolean preservedOrder = from1.getIndex().equals("index1") && from1.getType().equals("type1") + && from2.getIndex().equals("index2") && from2.getType().equals("type2"); + boolean notPreservedOrder = from1.getIndex().equals("index2") && from1.getType().equals("type2") + && from2.getIndex().equals("index1") && from2.getType().equals("type1"); + Assert.assertTrue(preservedOrder || notPreservedOrder); + } + + private SQLExpr queryToExpr(String query) { return new ElasticSqlExprParser(query).expr(); } From c3ab42f09a141aa5daed61bc044dfebbc5624820 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sat, 12 Sep 2015 20:18:03 +0300 Subject: [PATCH 064/559] able to query and select fields with '@' --- .../org/nlpcn/es4sql/parse/ElasticLexer.java | 2 +- .../org/nlpcn/es4sql/parse/FieldMaker.java | 2 +- .../org/nlpcn/es4sql/parse/SqlParser.java | 2 +- src/test/java/org/nlpcn/es4sql/QueryTest.java | 8 +++++++ .../java/org/nlpcn/es4sql/SqlParserTests.java | 21 +++++++++++++++++++ .../resources/game_of_thrones_complex.json | 2 +- 6 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/nlpcn/es4sql/parse/ElasticLexer.java b/src/main/java/org/nlpcn/es4sql/parse/ElasticLexer.java index 2adfe678..f1d163ae 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/ElasticLexer.java +++ b/src/main/java/org/nlpcn/es4sql/parse/ElasticLexer.java @@ -88,6 +88,6 @@ public void scanIdentifier() { private boolean isElasticIdentifierChar(char ch) { - return ch == '*' || ch == '-' || isIdentifierChar(ch); + return ch == '*' || ch == '-' || isIdentifierChar(ch); } } diff --git a/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java b/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java index 076b1f42..fae90625 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java +++ b/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java @@ -19,7 +19,7 @@ */ public class FieldMaker { public static Field makeField(SQLExpr expr, String alias,String tableAlias) throws SqlParseException { - if (expr instanceof SQLIdentifierExpr || expr instanceof SQLPropertyExpr) { + if (expr instanceof SQLIdentifierExpr || expr instanceof SQLPropertyExpr || expr instanceof SQLVariantRefExpr) { String name = expr.toString().replace("`", ""); if(tableAlias==null) return new Field(name, alias); else if(tableAlias!=null){ diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index 2ef8505f..44cd2199 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -71,7 +71,7 @@ private Where findWhere(SQLExpr where) throws SqlParseException { private boolean isCond(SQLBinaryOpExpr expr) { - return expr.getLeft() instanceof SQLIdentifierExpr || expr.getLeft() instanceof SQLPropertyExpr; + return expr.getLeft() instanceof SQLIdentifierExpr || expr.getLeft() instanceof SQLPropertyExpr || expr.getLeft() instanceof SQLVariantRefExpr; } private void parseWhere(SQLExpr expr, Where where) throws SqlParseException { diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java index ba460b4f..9fc2ec7f 100644 --- a/src/test/java/org/nlpcn/es4sql/QueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java @@ -525,6 +525,14 @@ public void complexObjectReutrnField() throws IOException, SqlParseException, SQ Assert.assertEquals("Eddard",((HashMap)sourceAsMap.get("parents")).get("father")); } + @Test + public void queryWithATfieldOnWhere() throws IOException, SqlParseException, SQLFeatureNotSupportedException{ + SearchHits response = query(String.format("SELECT * FROM %s/gotCharacters where @wolf = 'Summer' LIMIT 1000", TEST_INDEX)); + Assert.assertEquals(1, response.getTotalHits()); + Map sourceAsMap = response.getHits()[0].sourceAsMap(); + Assert.assertEquals("Summer",sourceAsMap.get("@wolf")); + Assert.assertEquals("Brandon",((HashMap)sourceAsMap.get("name")).get("firstname")); + } @Test public void notLikeTests() throws IOException, SqlParseException, SQLFeatureNotSupportedException{ diff --git a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java index 3fd3c02d..4ea00f72 100644 --- a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java +++ b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java @@ -299,6 +299,27 @@ public void twoIndices() throws SqlParseException { Assert.assertTrue(preservedOrder || notPreservedOrder); } + @Test + public void fieldWithATcharAtWhere() throws SqlParseException { + String query = "SELECT * FROM index/type where @field = 6 "; + SQLExpr sqlExpr = queryToExpr(query); + Select select = parser.parseSelect((SQLQueryExpr) sqlExpr); + LinkedList wheres = select.getWhere().getWheres(); + Assert.assertEquals(1,wheres.size()); + Condition condition = (Condition) wheres.get(0); + Assert.assertEquals("@field", condition.getName()); + } + + @Test + public void fieldWithATcharAtSelect() throws SqlParseException { + String query = "SELECT @field FROM index/type where field2 = 6 "; + SQLExpr sqlExpr = queryToExpr(query); + Select select = parser.parseSelect((SQLQueryExpr) sqlExpr); + List fields = select.getFields(); + Assert.assertEquals(1,fields.size()); + Field field = fields.get(0); + Assert.assertEquals(field.getName(),"@field"); + } private SQLExpr queryToExpr(String query) { return new ElasticSqlExprParser(query).expr(); diff --git a/src/test/resources/game_of_thrones_complex.json b/src/test/resources/game_of_thrones_complex.json index 2f576424..47fb491c 100644 --- a/src/test/resources/game_of_thrones_complex.json +++ b/src/test/resources/game_of_thrones_complex.json @@ -3,7 +3,7 @@ {"index":{"_type": "gotCharacters", "_id":"2"}} {"name":{"firstname":"Eddard","lastname":"Stark","ofHisName":1},"house":"Stark", "parents":{"father":"Rickard" , "mother":"Lyarra"} ,"gender":"M","titles":["lordOfWinterfell","wardenOfTheNorth","handOfTheKing"]} {"index":{"_type": "gotCharacters", "_id":"3"}} -{"name":{"firstname":"Brandon","lastname":"Stark","ofHisName":4},"house":"Stark","parents":{"father":"Eddard","mother":"Catelyn"},"gender":"M","titles":["princeOfWinterfell"]} +{"name":{"firstname":"Brandon","lastname":"Stark","ofHisName":4},"house":"Stark","parents":{"father":"Eddard","mother":"Catelyn"},"gender":"M","titles":["princeOfWinterfell"],"@wolf":"Summer"} {"index":{"_type": "gotCharacters", "_id":"4"}} {"name":{"firstname":"Jaime","lastname":"Lannister","ofHisName":1},"gender":"M","house":"Lannister","parents":{"father":"Tywin","mother":"Joanna"},"titles":["kingSlayer","lordCommanderOfTheKingsguard","Ser"]} {"index":{"_type": "gotHouses", "_id":"1"}} From e413a23d1ca5ad182642eb65ce39385e03b818fa Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sat, 12 Sep 2015 20:23:26 +0300 Subject: [PATCH 065/559] enable colon on fields #86 --- .../org/nlpcn/es4sql/parse/ElasticLexer.java | 2 +- .../java/org/nlpcn/es4sql/SqlParserTests.java | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/nlpcn/es4sql/parse/ElasticLexer.java b/src/main/java/org/nlpcn/es4sql/parse/ElasticLexer.java index f1d163ae..24f0a75e 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/ElasticLexer.java +++ b/src/main/java/org/nlpcn/es4sql/parse/ElasticLexer.java @@ -88,6 +88,6 @@ public void scanIdentifier() { private boolean isElasticIdentifierChar(char ch) { - return ch == '*' || ch == '-' || isIdentifierChar(ch); + return ch == '*' || ch == ':' || ch == '-' || isIdentifierChar(ch); } } diff --git a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java index 4ea00f72..ea4c2d28 100644 --- a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java +++ b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java @@ -320,6 +320,27 @@ public void fieldWithATcharAtSelect() throws SqlParseException { Field field = fields.get(0); Assert.assertEquals(field.getName(),"@field"); } + @Test + public void fieldWithColonCharAtSelect() throws SqlParseException { + String query = "SELECT a:b FROM index/type where field2 = 6 "; + SQLExpr sqlExpr = queryToExpr(query); + Select select = parser.parseSelect((SQLQueryExpr) sqlExpr); + List fields = select.getFields(); + Assert.assertEquals(1,fields.size()); + Field field = fields.get(0); + Assert.assertEquals(field.getName(),"a:b"); + } + + @Test + public void fieldWithColonCharAtWhere() throws SqlParseException { + String query = "SELECT * FROM index/type where a:b = 6 "; + SQLExpr sqlExpr = queryToExpr(query); + Select select = parser.parseSelect((SQLQueryExpr) sqlExpr); + LinkedList wheres = select.getWhere().getWheres(); + Assert.assertEquals(1,wheres.size()); + Condition condition = (Condition) wheres.get(0); + Assert.assertEquals("a:b", condition.getName()); + } private SQLExpr queryToExpr(String query) { return new ElasticSqlExprParser(query).expr(); From a61e3b72e85f765c13f3feabb533d1f11ca39323 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sat, 12 Sep 2015 23:16:24 +0300 Subject: [PATCH 066/559] IS NULL and IS NOT NULL working --- .../java/org/nlpcn/es4sql/query/maker/Maker.java | 7 +++---- src/test/java/org/nlpcn/es4sql/QueryTest.java | 12 ++++++++++++ src/test/java/org/nlpcn/es4sql/SqlParserTests.java | 12 ++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java index 04082cdf..3e9c0466 100644 --- a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java +++ b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java @@ -122,16 +122,15 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar case IS: case N: case EQ: - if (value instanceof SQLIdentifierExpr) { - SQLIdentifierExpr identifier = (SQLIdentifierExpr) value; - if(identifier.getName().equalsIgnoreCase("missing")) { + if (value == null || value instanceof SQLIdentifierExpr) { + if(value == null || ((SQLIdentifierExpr) value).getName().equalsIgnoreCase("missing")) { x = FilterBuilders.missingFilter(name); if (isQuery) { x = QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), FilterBuilders.missingFilter(name)); } } else { - throw new SqlParseException(String.format("Cannot recoginze Sql identifer %s", identifier.getName())); + throw new SqlParseException(String.format("Cannot recoginze Sql identifer %s", ((SQLIdentifierExpr) value).getName())); } break; } else { diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java index 9fc2ec7f..9399ff75 100644 --- a/src/test/java/org/nlpcn/es4sql/QueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java @@ -546,6 +546,18 @@ public void notLikeTests() throws IOException, SqlParseException, SQLFeatureNotS } } + @Test + public void isNullTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException{ + SearchHits response = query("SELECT name FROM " +TEST_INDEX + "/gotCharacters where nickname IS NULL LIMIT 1000"); + Assert.assertEquals(3, response.getTotalHits()); + } + + @Test + public void isNotNullTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException{ + SearchHits response = query("SELECT name FROM " +TEST_INDEX + "/gotCharacters where nickname IS NOT NULL LIMIT 1000"); + Assert.assertEquals(1, response.getTotalHits()); + } + private SearchHits query(String query) throws SqlParseException, SQLFeatureNotSupportedException, SQLFeatureNotSupportedException { SearchDao searchDao = MainTestSuite.getSearchDao(); SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) searchDao.explain(query); diff --git a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java index ea4c2d28..d83a56ae 100644 --- a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java +++ b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java @@ -342,6 +342,18 @@ public void fieldWithColonCharAtWhere() throws SqlParseException { Assert.assertEquals("a:b", condition.getName()); } + @Test + public void fieldIsNull() throws SqlParseException { + String query = "SELECT * FROM index/type where a IS NOT NULL"; + SQLExpr sqlExpr = queryToExpr(query); + Select select = parser.parseSelect((SQLQueryExpr) sqlExpr); + LinkedList wheres = select.getWhere().getWheres(); + Assert.assertEquals(1,wheres.size()); + Condition condition = (Condition) wheres.get(0); + Assert.assertEquals("a", condition.getName()); + Assert.assertNull(condition.getValue()); + } + private SQLExpr queryToExpr(String query) { return new ElasticSqlExprParser(query).expr(); } From 428496f1e645508a596e24c448ec90c8b5999510 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Tue, 15 Sep 2015 23:37:17 +0300 Subject: [PATCH 067/559] nested_loops initial implemntation --- .../plugin/nlpcn/ActionRequestExecuter.java | 22 +- .../plugin/nlpcn/ElasticJoinExecutor.java | 204 ++++++++++++++++++ .../plugin/nlpcn/HashJoinElasticExecutor.java | 166 ++------------ .../nlpcn/NestedLoopsElasticExecutor.java | 140 ++++++++++++ .../es4sql/domain/hints/HintFactory.java | 4 +- .../nlpcn/es4sql/domain/hints/HintType.java | 3 +- .../nlpcn/es4sql/query/ESActionFactory.java | 4 +- .../es4sql/query/ESHashJoinQueryAction.java | 112 ---------- .../org/nlpcn/es4sql/query/JoinResponse.java | 9 - .../query/join/ESHashJoinQueryAction.java | 74 +++++++ .../es4sql/query/join/ESJoinQueryAction.java | 90 ++++++++ .../query/join/ESJoinQueryActionFactory.java | 43 ++++ .../query/join/ESNestedLoopsQueryAction.java | 43 ++++ .../join/HashJoinElasticRequestBuilder.java | 49 +++++ .../JoinRequestBuilder.java} | 32 +-- .../NestedLoopsElasticRequestBuilder.java | 31 +++ .../{ => join}/TableInJoinRequestBuilder.java | 2 +- src/test/java/org/nlpcn/es4sql/JoinTests.java | 148 +++++++++++-- .../java/org/nlpcn/es4sql/MainTestSuite.java | 1 - 19 files changed, 849 insertions(+), 328 deletions(-) create mode 100644 src/main/java/org/elasticsearch/plugin/nlpcn/ElasticJoinExecutor.java create mode 100644 src/main/java/org/elasticsearch/plugin/nlpcn/NestedLoopsElasticExecutor.java delete mode 100644 src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java delete mode 100644 src/main/java/org/nlpcn/es4sql/query/JoinResponse.java create mode 100644 src/main/java/org/nlpcn/es4sql/query/join/ESHashJoinQueryAction.java create mode 100644 src/main/java/org/nlpcn/es4sql/query/join/ESJoinQueryAction.java create mode 100644 src/main/java/org/nlpcn/es4sql/query/join/ESJoinQueryActionFactory.java create mode 100644 src/main/java/org/nlpcn/es4sql/query/join/ESNestedLoopsQueryAction.java create mode 100644 src/main/java/org/nlpcn/es4sql/query/join/HashJoinElasticRequestBuilder.java rename src/main/java/org/nlpcn/es4sql/query/{HashJoinElasticRequestBuilder.java => join/JoinRequestBuilder.java} (74%) create mode 100644 src/main/java/org/nlpcn/es4sql/query/join/NestedLoopsElasticRequestBuilder.java rename src/main/java/org/nlpcn/es4sql/query/{ => join}/TableInJoinRequestBuilder.java (97%) diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestExecuter.java b/src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestExecuter.java index fc7270b1..8b00fdf5 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestExecuter.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestExecuter.java @@ -1,16 +1,19 @@ package org.elasticsearch.plugin.nlpcn; import org.elasticsearch.action.ActionRequest; -import org.elasticsearch.action.ActionRunnable; import org.elasticsearch.action.deletebyquery.DeleteByQueryRequest; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; import org.elasticsearch.rest.RestChannel; import org.elasticsearch.rest.action.support.RestStatusToXContentListener; -import org.nlpcn.es4sql.query.ESHashJoinQueryAction; -import org.nlpcn.es4sql.query.HashJoinElasticRequestBuilder; +import org.nlpcn.es4sql.exception.SqlParseException; +import org.nlpcn.es4sql.query.join.HashJoinElasticRequestBuilder; import org.nlpcn.es4sql.query.SqlElasticRequestBuilder; +import org.nlpcn.es4sql.query.join.JoinRequestBuilder; +import org.nlpcn.es4sql.query.join.NestedLoopsElasticRequestBuilder; + +import java.io.IOException; public class ActionRequestExecuter { @@ -33,11 +36,8 @@ public void execute() throws Exception { request.listenerThreaded(false); //todo: maby change to instanceof multi? - if(requestBuilder instanceof HashJoinElasticRequestBuilder){ - HashJoinElasticRequestBuilder hashJoin = (HashJoinElasticRequestBuilder) requestBuilder; - HashJoinElasticExecutor executor = new HashJoinElasticExecutor(client,hashJoin); - executor.run(); - executor.sendResponse(channel); + if(requestBuilder instanceof JoinRequestBuilder){ + executeJoinRequestAndSendResponse(); } else if (request instanceof SearchRequest) { client.search((SearchRequest) request, new RestStatusToXContentListener(channel)); @@ -51,4 +51,10 @@ else if (request instanceof SearchRequest) { } } + private void executeJoinRequestAndSendResponse() throws IOException, SqlParseException { + ElasticJoinExecutor executor = ElasticJoinExecutor.createJoinExecutor(client,requestBuilder); + executor.run(); + executor.sendResponse(channel); + } + } diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticJoinExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticJoinExecutor.java new file mode 100644 index 00000000..b9ca3bba --- /dev/null +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticJoinExecutor.java @@ -0,0 +1,204 @@ +package org.elasticsearch.plugin.nlpcn; + +import com.google.common.collect.ImmutableMap; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.client.Client; +import org.elasticsearch.common.text.StringText; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.rest.BytesRestResponse; +import org.elasticsearch.rest.RestChannel; +import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchHits; +import org.elasticsearch.search.internal.InternalSearchHit; +import org.elasticsearch.search.internal.InternalSearchHits; +import org.nlpcn.es4sql.domain.Field; +import org.nlpcn.es4sql.exception.SqlParseException; +import org.nlpcn.es4sql.query.SqlElasticRequestBuilder; +import org.nlpcn.es4sql.query.join.HashJoinElasticRequestBuilder; +import org.nlpcn.es4sql.query.join.NestedLoopsElasticRequestBuilder; + +import java.io.IOException; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by Eliran on 15/9/2015. + */ +public abstract class ElasticJoinExecutor { + protected SearchHits results ; + protected MetaSearchResult metaResults; + protected final int MAX_RESULTS_ON_ONE_FETCH = 10000; + + protected ElasticJoinExecutor() { + metaResults = new MetaSearchResult(); + } + + public void sendResponse(RestChannel channel){ + try { + String json = resultAsString(); + BytesRestResponse bytesRestResponse = new BytesRestResponse(RestStatus.OK, json); + channel.sendResponse(bytesRestResponse); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void run() throws IOException, SqlParseException { + long timeBefore = System.currentTimeMillis(); + List combinedSearchHits = innerRun(); + int resultsSize = combinedSearchHits.size(); + InternalSearchHit[] hits = combinedSearchHits.toArray(new InternalSearchHit[resultsSize]); + this.results = new InternalSearchHits(hits, resultsSize,1.0f); + long joinTimeInMilli = System.currentTimeMillis() - timeBefore; + this.metaResults.setTookImMilli(joinTimeInMilli); + } + + //use our deserializer instead of results toXcontent because the source field is differnet from sourceAsMap. + public String resultAsString() throws IOException { + if(this.results == null) return null; + Object[] searchHits; + searchHits = new Object[(int) this.results.totalHits()]; + int i = 0; + for(SearchHit hit : this.results) { + HashMap value = new HashMap<>(); + value.put("_id",hit.getId()); + value.put("_type", hit.getType()); + value.put("_score", hit.score()); + value.put("_source", hit.sourceAsMap()); + searchHits[i] = value; + i++; + } + HashMap hits = new HashMap<>(); + hits.put("total",this.results.totalHits()); + hits.put("max_score",this.results.maxScore()); + hits.put("hits",searchHits); + XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint(); + builder.startObject(); + builder.field("took", metaResults.getTookImMilli()); + builder.field("timed_out",metaResults.isTimedOut()); + builder.field("_shards", ImmutableMap.of("total", metaResults.getTotalNumOfShards(), + "successful", metaResults.getSuccessfulShards() + , "failed", metaResults.getFailedShards())); + builder.field("hits",hits) ; + builder.endObject(); + + return builder.string(); + } + + protected abstract List innerRun() throws IOException, SqlParseException ; + + public SearchHits getHits(){ + return results; + } + + public static ElasticJoinExecutor createJoinExecutor(Client client, SqlElasticRequestBuilder requestBuilder){ + if(requestBuilder instanceof HashJoinElasticRequestBuilder) { + HashJoinElasticRequestBuilder hashJoin = (HashJoinElasticRequestBuilder) requestBuilder; + return new HashJoinElasticExecutor(client, hashJoin); + } + else if (requestBuilder instanceof NestedLoopsElasticRequestBuilder){ + NestedLoopsElasticRequestBuilder nestedLoops = (NestedLoopsElasticRequestBuilder) requestBuilder; + return new NestedLoopsElasticExecutor(client,nestedLoops); + } + else { + throw new RuntimeException("Unsuported requestBuilder of type: " + requestBuilder.getClass()); + } + } + + protected void mergeSourceAndAddAliases(Map secondTableHitSource, InternalSearchHit searchHit,String t1Alias,String t2Alias) { + Map results = mapWithAliases(searchHit.getSource(), t1Alias); + results.putAll(mapWithAliases(secondTableHitSource, t2Alias)); + searchHit.getSource().clear(); + searchHit.getSource().putAll(results); + } + + protected Map mapWithAliases(Map source, String alias) { + Map mapWithAliases = new HashMap<>(); + for(Map.Entry fieldNameToValue : source.entrySet()) { + mapWithAliases.put(alias + "." + fieldNameToValue.getKey(), fieldNameToValue.getValue()); + } + return mapWithAliases; + } + + protected void onlyReturnedFields(Map fieldsMap, List required) { + HashMap filteredMap = new HashMap<>(); + + for(Field field: required){ + String name = field.getName(); + filteredMap.put(name, deepSearchInMap(fieldsMap, name)); + } + fieldsMap.clear(); + fieldsMap.putAll(filteredMap); + + } + + protected Object deepSearchInMap(Map fieldsMap, String name) { + if(name.contains(".")){ + String[] path = name.split("\\."); + Map currentObject = fieldsMap; + for(int i=0;i) valueFromCurrentMap; + } + return currentObject.get(path[path.length-1]); + } + + return fieldsMap.get(name); + } + + + protected void addUnmatchedResults(List combinedResults, Collection firstTableSearchHits, List secondTableReturnedFields,int currentNumOfIds, int totalLimit,String t1Alias,String t2Alias) { + boolean limitReached = false; + for(SearchHitsResult hitsResult : firstTableSearchHits){ + if(!hitsResult.isMatchedWithOtherTable()){ + for(SearchHit hit: hitsResult.getSearchHits() ) { + + //todo: decide which id to put or type. or maby its ok this way. just need to doc. + addUnmachedResult(combinedResults, secondTableReturnedFields, currentNumOfIds, t1Alias, t2Alias, hit); + currentNumOfIds++; + if(currentNumOfIds >= totalLimit){ + limitReached = true; + break; + } + + } + } + if(limitReached) break; + } + } + + protected void addUnmachedResult(List combinedResults, List secondTableReturnedFields, int currentNumOfIds, String t1Alias, String t2Alias, SearchHit hit) { + InternalSearchHit searchHit = new InternalSearchHit(currentNumOfIds, hit.id() + "|0", new StringText(hit.getType() + "|null"), hit.getFields()); + searchHit.sourceRef(hit.getSourceRef()); + searchHit.sourceAsMap().clear(); + searchHit.sourceAsMap().putAll(hit.sourceAsMap()); + Map emptySecondTableHitSource = createNullsSource(secondTableReturnedFields); + mergeSourceAndAddAliases(emptySecondTableHitSource, searchHit,t1Alias,t2Alias); + + combinedResults.add(searchHit); + } + + protected Map createNullsSource(List secondTableReturnedFields) { + Map nulledSource = new HashMap<>(); + for(Field field : secondTableReturnedFields){ + nulledSource.put(field.getName(),null); + } + return nulledSource; + } + + protected void updateMetaSearchResults( SearchResponse searchResponse) { + this.metaResults.addSuccessfulShards(searchResponse.getSuccessfulShards()); + this.metaResults.addFailedShards(searchResponse.getFailedShards()); + this.metaResults.addTotalNumOfShards(searchResponse.getTotalShards()); + this.metaResults.updateTimeOut(searchResponse.isTimedOut()); + } + + +} diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java index 03d1d6d2..fa3c2bdb 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java @@ -26,8 +26,8 @@ import org.nlpcn.es4sql.domain.Select; import org.nlpcn.es4sql.domain.Where; import org.nlpcn.es4sql.exception.SqlParseException; -import org.nlpcn.es4sql.query.HashJoinElasticRequestBuilder; -import org.nlpcn.es4sql.query.TableInJoinRequestBuilder; +import org.nlpcn.es4sql.query.join.HashJoinElasticRequestBuilder; +import org.nlpcn.es4sql.query.join.TableInJoinRequestBuilder; import org.nlpcn.es4sql.query.maker.FilterMaker; import org.nlpcn.es4sql.query.maker.QueryMaker; @@ -37,70 +37,22 @@ /** * Created by Eliran on 22/8/2015. */ -public class HashJoinElasticExecutor { +public class HashJoinElasticExecutor extends ElasticJoinExecutor { private HashJoinElasticRequestBuilder requestBuilder; - private SearchHits results ; - private MetaSearchResult metaResults; + + private Client client; private boolean useQueryTermsFilterOptimization = false; - - private final int MAX_RESULTS_ON_ONE_FETCH = 10000; private final int MAX_RESULTS_FOR_FIRST_TABLE = 100000; public HashJoinElasticExecutor(Client client,HashJoinElasticRequestBuilder requestBuilder) { this.client = client; this.requestBuilder = requestBuilder; this.useQueryTermsFilterOptimization = requestBuilder.isUseTermFiltersOptimization(); - metaResults = new MetaSearchResult(); - } - - public SearchHits getHits(){ - return results; - } - public void sendResponse(RestChannel channel){ - try { - String json = resultAsString(); - BytesRestResponse bytesRestResponse = new BytesRestResponse(RestStatus.OK, json); - channel.sendResponse(bytesRestResponse); - } catch (IOException e) { - e.printStackTrace(); - } - } - //use our deserializer instead of results toXcontent because the source field is differnet from sourceAsMap. - public String resultAsString() throws IOException { - Object[] searchHits; - searchHits = new Object[(int) this.results.totalHits()]; - int i = 0; - for(SearchHit hit : this.results) { - HashMap value = new HashMap<>(); - value.put("_id",hit.getId()); - value.put("_type", hit.getType()); - value.put("_score", hit.score()); - value.put("_source", hit.sourceAsMap()); - searchHits[i] = value; - i++; - } - HashMap hits = new HashMap<>(); - hits.put("total",this.results.totalHits()); - hits.put("max_score",this.results.maxScore()); - hits.put("hits",searchHits); - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint(); - builder.startObject(); - builder.field("took", metaResults.getTookImMilli()); - builder.field("timed_out",metaResults.isTimedOut()); - builder.field("_shards",ImmutableMap.of("total",metaResults.getTotalNumOfShards(), - "successful",metaResults.getSuccessfulShards() - ,"failed",metaResults.getFailedShards())); - builder.field("hits",hits) ; - builder.endObject(); - - return builder.string(); - } + public List innerRun() throws IOException, SqlParseException { - public void run() throws IOException, SqlParseException { - long timeBefore = System.currentTimeMillis(); Map> optimizationTermsFilterStructure = new HashMap<>(); List> t1ToT2FieldsComparison = requestBuilder.getT1ToT2FieldsComparison(); @@ -116,13 +68,17 @@ public void run() throws IOException, SqlParseException { List combinedResult = createCombinedResults(optimizationTermsFilterStructure, t1ToT2FieldsComparison, comparisonKeyToSearchHits, secondTableRequest); int currentNumOfResults = combinedResult.size(); - if(requestBuilder.getJoinType() == SQLJoinTableSource.JoinType.LEFT_OUTER_JOIN && currentNumOfResults < requestBuilder.getTotalLimit()){ - addUnmatchedResults(combinedResult,comparisonKeyToSearchHits.values(),requestBuilder.getSecondTable().getReturnedFields(), currentNumOfResults); + int totalLimit = requestBuilder.getTotalLimit(); + if(requestBuilder.getJoinType() == SQLJoinTableSource.JoinType.LEFT_OUTER_JOIN && currentNumOfResults < totalLimit){ + String t1Alias = requestBuilder.getFirstTable().getAlias(); + String t2Alias = requestBuilder.getSecondTable().getAlias(); + addUnmatchedResults(combinedResult,comparisonKeyToSearchHits.values(), + requestBuilder.getSecondTable().getReturnedFields(), + currentNumOfResults, totalLimit, + t1Alias, + t2Alias); } - InternalSearchHit[] hits = combinedResult.toArray(new InternalSearchHit[combinedResult.size()]); - this.results = new InternalSearchHits(hits,combinedResult.size(),1.0f); - long joinTimeInMilli = System.currentTimeMillis() - timeBefore; - this.metaResults.setTookImMilli(joinTimeInMilli); + return combinedResult; } private void updateFirstTableLimitIfNeeded() { @@ -178,7 +134,9 @@ private List createCombinedResults(Map> searchHit.sourceRef(matchingHit.getSourceRef()); searchHit.sourceAsMap().clear(); searchHit.sourceAsMap().putAll(matchingHit.sourceAsMap()); - mergeSourceAndAddAliases(secondTableHit.getSource(), searchHit); + String t1Alias = requestBuilder.getFirstTable().getAlias(); + String t2Alias = requestBuilder.getSecondTable().getAlias(); + mergeSourceAndAddAliases(secondTableHit.getSource(), searchHit, t1Alias, t2Alias); combinedResult.add(searchHit); resultIds++; @@ -262,13 +220,6 @@ private List scrollTillLimit(SearchRequestBuilder requestBuilder, Int return hitsWithScan; } - private void updateMetaSearchResults( SearchResponse searchResponse) { - this.metaResults.addSuccessfulShards(searchResponse.getSuccessfulShards()); - this.metaResults.addFailedShards(searchResponse.getFailedShards()); - this.metaResults.addTotalNumOfShards(searchResponse.getTotalShards()); - this.metaResults.updateTimeOut(searchResponse.isTimedOut()); - } - private boolean needToOptimize(Map> optimizationTermsFilterStructure) { return useQueryTermsFilterOptimization && optimizationTermsFilterStructure!=null && optimizationTermsFilterStructure.size()>0; } @@ -304,40 +255,6 @@ private void updateRequestWithTermsFilter(Map> optimization } } - private void addUnmatchedResults(List combinedResults, Collection firstTableSearchHits, List secondTableReturnedFields,int currentNumOfIds) { - boolean limitReached = false; - for(SearchHitsResult hitsResult : firstTableSearchHits){ - if(!hitsResult.isMatchedWithOtherTable()){ - for(SearchHit hit: hitsResult.getSearchHits() ) { - //todo: decide which id to put or type. or maby its ok this way. just need to doc. - InternalSearchHit searchHit = new InternalSearchHit(currentNumOfIds, hit.id() + "|0", new StringText(hit.getType() + "|null"), hit.getFields()); - searchHit.sourceRef(hit.getSourceRef()); - searchHit.sourceAsMap().clear(); - searchHit.sourceAsMap().putAll(hit.sourceAsMap()); - Map emptySecondTableHitSource = createNullsSource(secondTableReturnedFields); - mergeSourceAndAddAliases(emptySecondTableHitSource, searchHit); - - combinedResults.add(searchHit); - currentNumOfIds++; - if(currentNumOfIds >= requestBuilder.getTotalLimit()){ - limitReached = true; - break; - } - - } - } - if(limitReached) break; - } - } - - private Map createNullsSource(List secondTableReturnedFields) { - Map nulledSource = new HashMap<>(); - for(Field field : secondTableReturnedFields){ - nulledSource.put(field.getName(),null); - } - return nulledSource; - } - private String getComparisonKey(List> t1ToT2FieldsComparison, SearchHit hit, boolean firstTable, Map> optimizationTermsFilterStructure) { String key = ""; Map sourceAsMap = hit.sourceAsMap(); @@ -371,49 +288,4 @@ private void updateOptimizationData(Map> optimizationTermsF } values.add(data); } - - private void mergeSourceAndAddAliases(Map secondTableHitSource, InternalSearchHit searchHit) { - Map results = mapWithAliases(searchHit.getSource(), requestBuilder.getFirstTable().getAlias()); - results.putAll(mapWithAliases(secondTableHitSource, requestBuilder.getSecondTable().getAlias())); - searchHit.getSource().clear(); - searchHit.getSource().putAll(results); - } - - private Map mapWithAliases(Map source, String alias) { - Map mapWithAliases = new HashMap<>(); - for(Map.Entry fieldNameToValue : source.entrySet()) { - mapWithAliases.put(alias + "." + fieldNameToValue.getKey(), fieldNameToValue.getValue()); - } - return mapWithAliases; - } - - private void onlyReturnedFields(Map fieldsMap, List required) { - HashMap filteredMap = new HashMap<>(); - - for(Field field: required){ - String name = field.getName(); - filteredMap.put(name, deepSearchInMap(fieldsMap, name)); - } - fieldsMap.clear(); - fieldsMap.putAll(filteredMap); - - } - - private Object deepSearchInMap(Map fieldsMap, String name) { - if(name.contains(".")){ - String[] path = name.split("\\."); - Map currentObject = fieldsMap; - for(int i=0;i) valueFromCurrentMap; - } - return currentObject.get(path[path.length-1]); - } - - return fieldsMap.get(name); - } - - } diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/NestedLoopsElasticExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/NestedLoopsElasticExecutor.java new file mode 100644 index 00000000..7b3fb641 --- /dev/null +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/NestedLoopsElasticExecutor.java @@ -0,0 +1,140 @@ +package org.elasticsearch.plugin.nlpcn; + +import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource; +import org.elasticsearch.action.get.MultiGetResponse; +import org.elasticsearch.action.search.MultiSearchRequest; +import org.elasticsearch.action.search.MultiSearchResponse; +import org.elasticsearch.action.search.SearchRequestBuilder; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.client.Client; +import org.elasticsearch.common.text.StringText; +import org.elasticsearch.index.query.BoolQueryBuilder; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.rest.RestChannel; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchHits; +import org.elasticsearch.search.internal.InternalSearchHit; +import org.nlpcn.es4sql.domain.Condition; +import org.nlpcn.es4sql.domain.Field; +import org.nlpcn.es4sql.domain.Select; +import org.nlpcn.es4sql.domain.Where; +import org.nlpcn.es4sql.exception.SqlParseException; +import org.nlpcn.es4sql.query.DefaultQueryAction; +import org.nlpcn.es4sql.query.join.NestedLoopsElasticRequestBuilder; +import org.nlpcn.es4sql.query.maker.FilterMaker; +import org.nlpcn.es4sql.query.maker.QueryMaker; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +/** + * Created by Eliran on 15/9/2015. + */ +public class NestedLoopsElasticExecutor extends ElasticJoinExecutor { + private static final int MULTI_SIZE = 100; + private final NestedLoopsElasticRequestBuilder + + nestedLoopsRequest; + private final Client client; + + public NestedLoopsElasticExecutor(Client client, NestedLoopsElasticRequestBuilder nestedLoops) { + this.client = client; + this.nestedLoopsRequest = nestedLoops; + } + + @Override + protected List innerRun() throws SqlParseException { + List combinedResults = new ArrayList<>(); + int totalLimit = nestedLoopsRequest.getTotalLimit(); + SearchResponse response = nestedLoopsRequest.getFirstTable().getRequestBuilder().get(); + orderConditions(nestedLoopsRequest.getFirstTable().getAlias(),nestedLoopsRequest.getSecondTable().getAlias()); + Collection conditions = nestedLoopsRequest.getT1FieldToCondition().values(); + int currentCombinedResults = 0; + boolean finishedWithFirstTable = false; + while (totalLimit > currentCombinedResults && !finishedWithFirstTable){ + SearchHit[] hits = response.getHits().getHits(); + if(hits.length == 0 || hits.length < MAX_RESULTS_ON_ONE_FETCH) finishedWithFirstTable = true; + Select secondTableSelect = nestedLoopsRequest.getSecondTable().getOriginalSelect(); + Where originalWhere = secondTableSelect.getWhere(); + String t1Alias = nestedLoopsRequest.getFirstTable().getAlias(); + String t2Alias = nestedLoopsRequest.getSecondTable().getAlias(); + boolean finishedMultiSearches = hits.length == 0; + int currentIndex = 0 ; + + while(!finishedMultiSearches){ + MultiSearchRequest multiSearchRequest = new MultiSearchRequest(); + int multiSearchSize = 0; + for(int i = currentIndex ; i < currentIndex + MULTI_SIZE && i< hits.length ; i++ ){ + Map hitFromFirstTableAsMap = hits[i].sourceAsMap(); + Where newWhere = Where.newInstance(); + if(originalWhere!=null) newWhere.addWhere(originalWhere); + for(Condition c : conditions){ + Object value = deepSearchInMap(hitFromFirstTableAsMap,c.getValue().toString()); + Condition conditionWithValue = new Condition(Where.CONN.AND,c.getName(),c.getOpear(),value); + newWhere.addWhere(conditionWithValue); + } + //using the 2nd table select and DefaultAction because we can't just change query on request (need to create lot of requests) + if(newWhere.getWheres().size() != 0) { + secondTableSelect.setWhere(newWhere); + } + DefaultQueryAction action = new DefaultQueryAction(this.client , secondTableSelect); + action.explain(); + multiSearchRequest.add(action.getRequestBuilder()); + multiSearchSize++; + } + MultiSearchResponse.Item[] responses = client.multiSearch(multiSearchRequest).actionGet().getResponses(); + for(int j =0 ; j < responses.length && currentCombinedResults < totalLimit ; j++){ + SearchHits responseForHit = responses[j].getResponse().getHits(); + SearchHit hitFromFirstTable = hits[currentIndex+j]; + //todo: if responseForHit.getHits.length < responseForHit.getTotalHits(). need to fetch more! + onlyReturnedFields(hitFromFirstTable.sourceAsMap(), nestedLoopsRequest.getFirstTable().getReturnedFields()); + if(responseForHit.getHits().length == 0 && nestedLoopsRequest.getJoinType() == SQLJoinTableSource.JoinType.LEFT_OUTER_JOIN){ + addUnmachedResult(combinedResults, nestedLoopsRequest.getSecondTable().getReturnedFields(), currentCombinedResults, t1Alias, t2Alias, hitFromFirstTable); + currentCombinedResults++; + + continue; + } + + for(SearchHit matchedHit : responseForHit.getHits() ){ + onlyReturnedFields(matchedHit.sourceAsMap(), nestedLoopsRequest.getSecondTable().getReturnedFields()); + + //todo: decide which id to put or type. or maby its ok this way. just need to doc. + InternalSearchHit searchHit = new InternalSearchHit(currentCombinedResults, hitFromFirstTable.id() + "|" + matchedHit.getId(), new StringText(hitFromFirstTable.getType() + "|" + matchedHit.getType()), hitFromFirstTable.getFields()); + searchHit.sourceRef(hitFromFirstTable.getSourceRef()); + searchHit.sourceAsMap().clear(); + searchHit.sourceAsMap().putAll(hitFromFirstTable.sourceAsMap()); + + mergeSourceAndAddAliases(matchedHit.getSource(), searchHit, t1Alias, t2Alias); + + combinedResults.add(searchHit); + currentCombinedResults++; + if(currentCombinedResults >= totalLimit) break; + } + if(currentCombinedResults >= totalLimit) break; + + } + currentIndex += multiSearchSize; + finishedMultiSearches = currentIndex >= hits.length-1; + } + + + } + return combinedResults; + } + + private void orderConditions(String t1Alias, String t2Alias) { + Collection conditions = nestedLoopsRequest.getT1FieldToCondition().values(); + for(Condition c : conditions){ + //TODO: support all orders and for each OPEAR find his related OPEAR (< is > , EQ is EQ ,etc..) + if(!c.getName().startsWith(t2Alias+".") || !c.getValue().toString().startsWith(t1Alias +".")) + throw new RuntimeException("On NestedLoops currently only supported Ordered conditions (t2.field2 OPEAR t1.field1) , badCondition was:" + c); + c.setName(c.getName().replaceFirst(t2Alias+".","")); + c.setValue(c.getValue().toString().replaceFirst(t1Alias+ ".", "")); + } + } + + +} diff --git a/src/main/java/org/nlpcn/es4sql/domain/hints/HintFactory.java b/src/main/java/org/nlpcn/es4sql/domain/hints/HintFactory.java index 82486b9d..73e714bd 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/hints/HintFactory.java +++ b/src/main/java/org/nlpcn/es4sql/domain/hints/HintFactory.java @@ -9,7 +9,9 @@ public class HintFactory { public static Hint getHintFromString(String hintAsString){ - + if(hintAsString.startsWith("! USE_NESTED_LOOPS") || hintAsString.startsWith("! USE_NL")){ + return new Hint(HintType.USE_NESTED_LOOPS,null); + } if(hintAsString.equals("! HASH_WITH_TERMS_FILTER")) return new Hint(HintType.HASH_WITH_TERMS_FILTER,null); if(hintAsString.startsWith("! JOIN_TABLES_LIMIT")){ diff --git a/src/main/java/org/nlpcn/es4sql/domain/hints/HintType.java b/src/main/java/org/nlpcn/es4sql/domain/hints/HintType.java index 629242ea..f1dfddd3 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/hints/HintType.java +++ b/src/main/java/org/nlpcn/es4sql/domain/hints/HintType.java @@ -9,6 +9,7 @@ public enum HintType { HASH_WITH_TERMS_FILTER, - JOIN_LIMIT; + JOIN_LIMIT, + USE_NESTED_LOOPS; } diff --git a/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java b/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java index a16fc56b..37c71dcc 100644 --- a/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java +++ b/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java @@ -8,7 +8,6 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; import com.alibaba.druid.sql.parser.*; -import com.alibaba.druid.util.JdbcUtils; import org.elasticsearch.client.Client; import org.nlpcn.es4sql.domain.Delete; import org.nlpcn.es4sql.domain.JoinSelect; @@ -17,6 +16,7 @@ import org.nlpcn.es4sql.parse.ElasticLexer; import org.nlpcn.es4sql.parse.ElasticSqlExprParser; import org.nlpcn.es4sql.parse.SqlParser; +import org.nlpcn.es4sql.query.join.ESJoinQueryActionFactory; import java.sql.SQLFeatureNotSupportedException; @@ -36,7 +36,7 @@ public static QueryAction create(Client client, String sql) throws SqlParseExcep SQLQueryExpr sqlExpr = (SQLQueryExpr) toSqlExpr(sql); if(isJoin(sqlExpr,sql)){ JoinSelect joinSelect = new SqlParser().parseJoinSelect(sqlExpr); - return new ESHashJoinQueryAction(client,joinSelect); + return ESJoinQueryActionFactory.createJoinAction(client, joinSelect); } else { Select select = new SqlParser().parseSelect(sqlExpr); diff --git a/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java b/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java deleted file mode 100644 index d8aebbac..00000000 --- a/src/main/java/org/nlpcn/es4sql/query/ESHashJoinQueryAction.java +++ /dev/null @@ -1,112 +0,0 @@ -package org.nlpcn.es4sql.query; - -import org.elasticsearch.action.search.MultiSearchResponse; -import org.elasticsearch.client.Client; -import org.nlpcn.es4sql.domain.*; -import org.nlpcn.es4sql.domain.hints.Hint; -import org.nlpcn.es4sql.domain.hints.HintType; -import org.nlpcn.es4sql.exception.SqlParseException; - -import java.util.*; - -/** - * Created by Eliran on 22/8/2015. - */ -public class ESHashJoinQueryAction extends QueryAction { - - private JoinSelect joinSelect; - - public ESHashJoinQueryAction(Client client,JoinSelect joinSelect) { - super(client, null); - this.joinSelect = joinSelect; - } - - @Override - public SqlElasticRequestBuilder explain() throws SqlParseException { - HashJoinElasticRequestBuilder hashRequest = new HashJoinElasticRequestBuilder(); - - String t1Alias = joinSelect.getFirstTable().getAlias(); - String t2Alias = joinSelect.getSecondTable().getAlias(); - - fillRequestBuilder(hashRequest.getFirstTable(), joinSelect.getFirstTable()); - fillRequestBuilder(hashRequest.getSecondTable(), joinSelect.getSecondTable()); - - List> comparisonFields = getComparisonFields(t1Alias, t2Alias,joinSelect.getConnectedConditions()); - - hashRequest.setT1ToT2FieldsComparison(comparisonFields); - - hashRequest.setJoinType(joinSelect.getJoinType()); - - hashRequest.setTotalLimit(joinSelect.getTotalLimit()); - - updateHashRequestWithHints(hashRequest); - - return hashRequest; - } - - private void updateHashRequestWithHints(HashJoinElasticRequestBuilder hashRequest) { - for(Hint hint : joinSelect.getHints()){ - if(hint.getType() == HintType.HASH_WITH_TERMS_FILTER) { - hashRequest.setUseTermFiltersOptimization(true); - } - if(hint.getType() == HintType.JOIN_LIMIT){ - Object[] params = hint.getParams(); - hashRequest.getFirstTable().setHintLimit((Integer) params[0]); - hashRequest.getSecondTable().setHintLimit((Integer) params[1]); - } - } - - } - - private List> getComparisonFields(String t1Alias, String t2Alias, List connectedConditions) throws SqlParseException { - List> comparisonFields = new ArrayList<>(); - for(Condition condition : connectedConditions){ - - if(condition.getOpear() != Condition.OPEAR.EQ){ - throw new SqlParseException(String.format("HashJoin should only be with EQ conditions, got:%s on condition:%s", condition.getOpear().name(), condition.toString())); - } - - String firstField = condition.getName(); - String secondField = condition.getValue().toString(); - Field t1Field,t2Field; - if(firstField.startsWith(t1Alias)){ - t1Field = new Field(removeAlias(firstField,t1Alias),null); - t2Field = new Field(removeAlias(secondField,t2Alias),null); - } - else { - t1Field = new Field(removeAlias(secondField,t1Alias),null); - t2Field = new Field(removeAlias(firstField,t2Alias),null); - } - comparisonFields.add(new AbstractMap.SimpleEntry(t1Field, t2Field)); - } - return comparisonFields; - } - - private void fillRequestBuilder(TableInJoinRequestBuilder requestBuilder,TableOnJoinSelect tableOnJoinSelect) throws SqlParseException { - List connectedFields = tableOnJoinSelect.getConnectedFields(); - addFieldsToSelectIfMissing(tableOnJoinSelect,connectedFields); - requestBuilder.setOriginalSelect(tableOnJoinSelect); - DefaultQueryAction queryAction = new DefaultQueryAction(client,tableOnJoinSelect); - queryAction.explain(); - requestBuilder.setRequestBuilder(queryAction.getRequestBuilder()); - requestBuilder.setReturnedFields(tableOnJoinSelect.getSelectedFields()); - requestBuilder.setAlias(tableOnJoinSelect.getAlias()); - } - - private String removeAlias(String field, String alias) { - return field.replace(alias+".",""); - } - - private void addFieldsToSelectIfMissing(Select select, List fields) { - //this means all fields - if(select.getFields() == null || select.getFields().size() == 0) return; - - List selectedFields = select.getFields(); - for(Field field : fields){ - if(!selectedFields.contains(field)){ - selectedFields.add(field); - } - } - - } -} diff --git a/src/main/java/org/nlpcn/es4sql/query/JoinResponse.java b/src/main/java/org/nlpcn/es4sql/query/JoinResponse.java deleted file mode 100644 index 5c2ec869..00000000 --- a/src/main/java/org/nlpcn/es4sql/query/JoinResponse.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.nlpcn.es4sql.query; - -import org.elasticsearch.action.ActionResponse; - -/** - * Created by Eliran on 19/8/2015. - */ -public class JoinResponse extends ActionResponse { -} diff --git a/src/main/java/org/nlpcn/es4sql/query/join/ESHashJoinQueryAction.java b/src/main/java/org/nlpcn/es4sql/query/join/ESHashJoinQueryAction.java new file mode 100644 index 00000000..88e2bd6f --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/query/join/ESHashJoinQueryAction.java @@ -0,0 +1,74 @@ +package org.nlpcn.es4sql.query.join; + +import org.elasticsearch.client.Client; +import org.nlpcn.es4sql.domain.*; +import org.nlpcn.es4sql.domain.hints.Hint; +import org.nlpcn.es4sql.domain.hints.HintType; +import org.nlpcn.es4sql.exception.SqlParseException; +import org.nlpcn.es4sql.query.*; + +import java.util.*; + +/** + * Created by Eliran on 22/8/2015. + */ +public class ESHashJoinQueryAction extends ESJoinQueryAction { + + public ESHashJoinQueryAction(Client client,JoinSelect joinSelect) { + super(client, joinSelect); + } + + @Override + protected void fillSpecificRequestBuilder(JoinRequestBuilder requestBuilder) throws SqlParseException { + String t1Alias = joinSelect.getFirstTable().getAlias(); + String t2Alias = joinSelect.getSecondTable().getAlias(); + + List> comparisonFields = getComparisonFields(t1Alias, t2Alias,joinSelect.getConnectedConditions()); + + ((HashJoinElasticRequestBuilder) requestBuilder).setT1ToT2FieldsComparison(comparisonFields); + } + + @Override + protected JoinRequestBuilder createSpecificBuilder() { + return new HashJoinElasticRequestBuilder(); + } + + @Override + protected void updateRequestWithHints(JoinRequestBuilder requestBuilder) { + super.updateRequestWithHints(requestBuilder); + for(Hint hint : joinSelect.getHints()){ + if(hint.getType() == HintType.HASH_WITH_TERMS_FILTER) { + ((HashJoinElasticRequestBuilder) requestBuilder).setUseTermFiltersOptimization(true); + } + } + } + + private List> getComparisonFields(String t1Alias, String t2Alias, List connectedConditions) throws SqlParseException { + List> comparisonFields = new ArrayList<>(); + for(Condition condition : connectedConditions){ + + if(condition.getOpear() != Condition.OPEAR.EQ){ + throw new SqlParseException(String.format("HashJoin should only be with EQ conditions, got:%s on condition:%s", condition.getOpear().name(), condition.toString())); + } + + String firstField = condition.getName(); + String secondField = condition.getValue().toString(); + Field t1Field,t2Field; + if(firstField.startsWith(t1Alias)){ + t1Field = new Field(removeAlias(firstField,t1Alias),null); + t2Field = new Field(removeAlias(secondField,t2Alias),null); + } + else { + t1Field = new Field(removeAlias(secondField,t1Alias),null); + t2Field = new Field(removeAlias(firstField,t2Alias),null); + } + comparisonFields.add(new AbstractMap.SimpleEntry(t1Field, t2Field)); + } + return comparisonFields; + } + + private String removeAlias(String field, String alias) { + return field.replace(alias+".",""); + } + +} diff --git a/src/main/java/org/nlpcn/es4sql/query/join/ESJoinQueryAction.java b/src/main/java/org/nlpcn/es4sql/query/join/ESJoinQueryAction.java new file mode 100644 index 00000000..d22eb508 --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/query/join/ESJoinQueryAction.java @@ -0,0 +1,90 @@ +package org.nlpcn.es4sql.query.join; + +import org.elasticsearch.client.Client; +import org.nlpcn.es4sql.domain.Field; +import org.nlpcn.es4sql.domain.JoinSelect; +import org.nlpcn.es4sql.domain.Select; +import org.nlpcn.es4sql.domain.TableOnJoinSelect; +import org.nlpcn.es4sql.domain.hints.Hint; +import org.nlpcn.es4sql.domain.hints.HintType; +import org.nlpcn.es4sql.exception.SqlParseException; +import org.nlpcn.es4sql.query.DefaultQueryAction; +import org.nlpcn.es4sql.query.QueryAction; +import org.nlpcn.es4sql.query.SqlElasticRequestBuilder; + +import java.util.List; + +/** + * Created by Eliran on 15/9/2015. + */ +public abstract class ESJoinQueryAction extends QueryAction { + + protected JoinSelect joinSelect; + + public ESJoinQueryAction(Client client, JoinSelect joinSelect) { + super(client, null); + this.joinSelect = joinSelect; + } + + @Override + public SqlElasticRequestBuilder explain() throws SqlParseException { + JoinRequestBuilder requestBuilder = createSpecificBuilder(); + fillBasicJoinRequestBuilder(requestBuilder); + fillSpecificRequestBuilder(requestBuilder); + return requestBuilder; + } + + protected abstract void fillSpecificRequestBuilder(JoinRequestBuilder requestBuilder) throws SqlParseException; + + protected abstract JoinRequestBuilder createSpecificBuilder(); + + + private void fillBasicJoinRequestBuilder(JoinRequestBuilder requestBuilder) throws SqlParseException { + + fillTableInJoinRequestBuilder(requestBuilder.getFirstTable(), joinSelect.getFirstTable()); + fillTableInJoinRequestBuilder(requestBuilder.getSecondTable(), joinSelect.getSecondTable()); + + requestBuilder.setJoinType(joinSelect.getJoinType()); + + requestBuilder.setTotalLimit(joinSelect.getTotalLimit()); + + updateRequestWithHints(requestBuilder); + + + } + + protected void updateRequestWithHints(JoinRequestBuilder requestBuilder){ + for(Hint hint : joinSelect.getHints()) { + if (hint.getType() == HintType.JOIN_LIMIT) { + Object[] params = hint.getParams(); + requestBuilder.getFirstTable().setHintLimit((Integer) params[0]); + requestBuilder.getSecondTable().setHintLimit((Integer) params[1]); + } + } + } + + private void fillTableInJoinRequestBuilder(TableInJoinRequestBuilder requestBuilder, TableOnJoinSelect tableOnJoinSelect) throws SqlParseException { + List connectedFields = tableOnJoinSelect.getConnectedFields(); + addFieldsToSelectIfMissing(tableOnJoinSelect,connectedFields); + requestBuilder.setOriginalSelect(tableOnJoinSelect); + DefaultQueryAction queryAction = new DefaultQueryAction(client,tableOnJoinSelect); + queryAction.explain(); + requestBuilder.setRequestBuilder(queryAction.getRequestBuilder()); + requestBuilder.setReturnedFields(tableOnJoinSelect.getSelectedFields()); + requestBuilder.setAlias(tableOnJoinSelect.getAlias()); + } + + private void addFieldsToSelectIfMissing(Select select, List fields) { + //this means all fields + if(select.getFields() == null || select.getFields().size() == 0) return; + + List selectedFields = select.getFields(); + for(Field field : fields){ + if(!selectedFields.contains(field)){ + selectedFields.add(field); + } + } + + } + +} diff --git a/src/main/java/org/nlpcn/es4sql/query/join/ESJoinQueryActionFactory.java b/src/main/java/org/nlpcn/es4sql/query/join/ESJoinQueryActionFactory.java new file mode 100644 index 00000000..88f1e2bb --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/query/join/ESJoinQueryActionFactory.java @@ -0,0 +1,43 @@ +package org.nlpcn.es4sql.query.join; + +import org.elasticsearch.client.Client; +import org.nlpcn.es4sql.domain.Condition; +import org.nlpcn.es4sql.domain.JoinSelect; +import org.nlpcn.es4sql.domain.hints.Hint; +import org.nlpcn.es4sql.domain.hints.HintType; +import org.nlpcn.es4sql.query.QueryAction; +import org.nlpcn.es4sql.query.join.ESHashJoinQueryAction; + +import java.util.List; + +/** + * Created by Eliran on 15/9/2015. + */ +public class ESJoinQueryActionFactory { + public static QueryAction createJoinAction(Client client, JoinSelect joinSelect) { + List connectedConditions = joinSelect.getConnectedConditions(); + boolean allEqual = true; + for (Condition condition : connectedConditions) { + if (condition.getOpear() != Condition.OPEAR.EQ) { + allEqual = false; + break; + } + + } + if (!allEqual) + return new ESNestedLoopsQueryAction(client, joinSelect); + + boolean useNestedLoopsHintExist = false; + for (Hint hint : joinSelect.getHints()) { + if (hint.getType() == HintType.USE_NESTED_LOOPS) { + useNestedLoopsHintExist = true; + break; + } + } + if (useNestedLoopsHintExist) + return new ESNestedLoopsQueryAction(client, joinSelect); + + return new ESHashJoinQueryAction(client, joinSelect); + + } +} diff --git a/src/main/java/org/nlpcn/es4sql/query/join/ESNestedLoopsQueryAction.java b/src/main/java/org/nlpcn/es4sql/query/join/ESNestedLoopsQueryAction.java new file mode 100644 index 00000000..1f8e4493 --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/query/join/ESNestedLoopsQueryAction.java @@ -0,0 +1,43 @@ +package org.nlpcn.es4sql.query.join; + +import org.elasticsearch.client.Client; +import org.nlpcn.es4sql.domain.Condition; +import org.nlpcn.es4sql.domain.JoinSelect; +import org.nlpcn.es4sql.exception.SqlParseException; +import org.nlpcn.es4sql.query.QueryAction; +import org.nlpcn.es4sql.query.SqlElasticRequestBuilder; + +import java.util.List; + +/** + * Created by Eliran on 15/9/2015. + */ +public class ESNestedLoopsQueryAction extends ESJoinQueryAction { + + public ESNestedLoopsQueryAction(Client client, JoinSelect joinSelect) { + super(client, joinSelect); + } + + @Override + protected void fillSpecificRequestBuilder(JoinRequestBuilder requestBuilder) throws SqlParseException { + NestedLoopsElasticRequestBuilder nestedBuilder = (NestedLoopsElasticRequestBuilder) requestBuilder; + List connectedConditions = joinSelect.getConnectedConditions(); + + for(Condition c : connectedConditions){ + nestedBuilder.addConditionMapping(c); + } + } + + @Override + protected JoinRequestBuilder createSpecificBuilder() { + return new NestedLoopsElasticRequestBuilder(); + } + + private String removeAlias(String field) { + String alias = joinSelect.getFirstTable().getAlias(); + if(!field.startsWith(alias+".")) + alias = joinSelect.getSecondTable().getAlias(); + return field.replace(alias+".",""); + } + +} diff --git a/src/main/java/org/nlpcn/es4sql/query/join/HashJoinElasticRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/join/HashJoinElasticRequestBuilder.java new file mode 100644 index 00000000..17821dd1 --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/query/join/HashJoinElasticRequestBuilder.java @@ -0,0 +1,49 @@ +package org.nlpcn.es4sql.query.join; + +import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource; +import org.elasticsearch.action.ActionRequest; +import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.action.search.MultiSearchRequest; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentType; +import org.nlpcn.es4sql.domain.Field; +import org.nlpcn.es4sql.query.SqlElasticRequestBuilder; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +/** + * Created by Eliran on 22/8/2015. + */ +public class HashJoinElasticRequestBuilder extends JoinRequestBuilder { + + private List> t1ToT2FieldsComparison; + private boolean useTermFiltersOptimization; + + public HashJoinElasticRequestBuilder() { + } + + @Override + public String explain() { + return "HashJoin "+ super.explain(); + } + + public List> getT1ToT2FieldsComparison() { + return t1ToT2FieldsComparison; + } + + public void setT1ToT2FieldsComparison(List> t1ToT2FieldsComparison) { + this.t1ToT2FieldsComparison = t1ToT2FieldsComparison; + } + + public boolean isUseTermFiltersOptimization() { + return useTermFiltersOptimization; + } + + public void setUseTermFiltersOptimization(boolean useTermFiltersOptimization) { + this.useTermFiltersOptimization = useTermFiltersOptimization; + } +} diff --git a/src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/join/JoinRequestBuilder.java similarity index 74% rename from src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java rename to src/main/java/org/nlpcn/es4sql/query/join/JoinRequestBuilder.java index c4621432..28792bab 100644 --- a/src/main/java/org/nlpcn/es4sql/query/HashJoinElasticRequestBuilder.java +++ b/src/main/java/org/nlpcn/es4sql/query/join/JoinRequestBuilder.java @@ -1,36 +1,32 @@ -package org.nlpcn.es4sql.query; +package org.nlpcn.es4sql.query.join; import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource; import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.search.MultiSearchRequest; -import org.elasticsearch.action.search.SearchRequest; -import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import org.nlpcn.es4sql.domain.Field; +import org.nlpcn.es4sql.query.SqlElasticRequestBuilder; import java.io.IOException; import java.util.List; import java.util.Map; /** - * Created by Eliran on 22/8/2015. + * Created by Eliran on 15/9/2015. */ -public class HashJoinElasticRequestBuilder implements SqlElasticRequestBuilder{ +public class JoinRequestBuilder implements SqlElasticRequestBuilder { private MultiSearchRequest multi; private TableInJoinRequestBuilder firstTable; private TableInJoinRequestBuilder secondTable; - - private List> t1ToT2FieldsComparison; private SQLJoinTableSource.JoinType joinType; - private boolean useTermFiltersOptimization; private int totalLimit; - public HashJoinElasticRequestBuilder() { + public JoinRequestBuilder() { firstTable = new TableInJoinRequestBuilder(); secondTable = new TableInJoinRequestBuilder(); } @@ -73,7 +69,6 @@ public ActionResponse get() { return null; } - public MultiSearchRequest getMulti() { return multi; } @@ -82,14 +77,6 @@ public void setMulti(MultiSearchRequest multi) { this.multi = multi; } - public List> getT1ToT2FieldsComparison() { - return t1ToT2FieldsComparison; - } - - public void setT1ToT2FieldsComparison(List> t1ToT2FieldsComparison) { - this.t1ToT2FieldsComparison = t1ToT2FieldsComparison; - } - public SQLJoinTableSource.JoinType getJoinType() { return joinType; } @@ -106,14 +93,6 @@ public TableInJoinRequestBuilder getSecondTable() { return secondTable; } - public boolean isUseTermFiltersOptimization() { - return useTermFiltersOptimization; - } - - public void setUseTermFiltersOptimization(boolean useTermFiltersOptimization) { - this.useTermFiltersOptimization = useTermFiltersOptimization; - } - public int getTotalLimit() { return totalLimit; } @@ -121,4 +100,5 @@ public int getTotalLimit() { public void setTotalLimit(int totalLimit) { this.totalLimit = totalLimit; } + } diff --git a/src/main/java/org/nlpcn/es4sql/query/join/NestedLoopsElasticRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/join/NestedLoopsElasticRequestBuilder.java new file mode 100644 index 00000000..00c96add --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/query/join/NestedLoopsElasticRequestBuilder.java @@ -0,0 +1,31 @@ +package org.nlpcn.es4sql.query.join; + +import org.nlpcn.es4sql.domain.Condition; +import org.nlpcn.es4sql.domain.Field; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by Eliran on 15/9/2015. + */ +public class NestedLoopsElasticRequestBuilder extends JoinRequestBuilder { + private Map t1FieldToCondition; + + public NestedLoopsElasticRequestBuilder() { + t1FieldToCondition = new HashMap<>(); + } + + public Map getT1FieldToCondition() { + return t1FieldToCondition; + } + + public void setT1FieldToCondition(Map t1FieldToCondition) { + this.t1FieldToCondition = t1FieldToCondition; + } + + + public void addConditionMapping(Condition c){ + t1FieldToCondition.put(new Field(c.getName(),null),c); + } +} diff --git a/src/main/java/org/nlpcn/es4sql/query/TableInJoinRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/join/TableInJoinRequestBuilder.java similarity index 97% rename from src/main/java/org/nlpcn/es4sql/query/TableInJoinRequestBuilder.java rename to src/main/java/org/nlpcn/es4sql/query/join/TableInJoinRequestBuilder.java index ffb33381..d3f55b6a 100644 --- a/src/main/java/org/nlpcn/es4sql/query/TableInJoinRequestBuilder.java +++ b/src/main/java/org/nlpcn/es4sql/query/join/TableInJoinRequestBuilder.java @@ -1,4 +1,4 @@ -package org.nlpcn.es4sql.query; +package org.nlpcn.es4sql.query.join; import org.elasticsearch.action.search.SearchRequestBuilder; import org.nlpcn.es4sql.domain.Field; diff --git a/src/test/java/org/nlpcn/es4sql/JoinTests.java b/src/test/java/org/nlpcn/es4sql/JoinTests.java index 59953d34..4e701410 100644 --- a/src/test/java/org/nlpcn/es4sql/JoinTests.java +++ b/src/test/java/org/nlpcn/es4sql/JoinTests.java @@ -1,17 +1,15 @@ package org.nlpcn.es4sql; -import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.common.collect.ImmutableMap; +import org.elasticsearch.plugin.nlpcn.ElasticJoinExecutor; import org.elasticsearch.plugin.nlpcn.HashJoinElasticExecutor; import org.elasticsearch.search.SearchHit; -import org.elasticsearch.search.SearchHits; import org.junit.Test; import org.junit.Assert; import org.nlpcn.es4sql.exception.SqlParseException; -import org.nlpcn.es4sql.query.HashJoinElasticRequestBuilder; +import org.nlpcn.es4sql.query.join.HashJoinElasticRequestBuilder; import org.nlpcn.es4sql.query.SqlElasticRequestBuilder; -import org.nlpcn.es4sql.query.SqlElasticSearchRequestBuilder; import java.io.IOException; import java.sql.SQLFeatureNotSupportedException; @@ -26,13 +24,13 @@ public class JoinTests { @Test - public void joinParseCheckSelectedFieldsSplit() throws SqlParseException, SQLFeatureNotSupportedException, IOException { + public void joinParseCheckSelectedFieldsSplitHASH() throws SqlParseException, SQLFeatureNotSupportedException, IOException { String query = "SELECT a.firstname ,a.lastname , a.gender ,d.name FROM elasticsearch-sql_test_index/people a " + " JOIN elasticsearch-sql_test_index/dog d on d.holdersName = a.firstname " + " WHERE " + " (a.age > 10 OR a.balance > 2000)" + " AND d.age > 1"; - SearchHit[] hits = hashJoinGetHits(query); + SearchHit[] hits = joinAndGetHits(query); Assert.assertEquals(2,hits.length); Map oneMatch = ImmutableMap.of("a.firstname", (Object)"Daenerys", "a.lastname","Targaryen", @@ -46,7 +44,27 @@ public void joinParseCheckSelectedFieldsSplit() throws SqlParseException, SQLFea } @Test - public void joinParseWithHintsCheckSelectedFieldsSplit() throws SqlParseException, SQLFeatureNotSupportedException, IOException { + public void joinParseCheckSelectedFieldsSplitNL() throws SqlParseException, SQLFeatureNotSupportedException, IOException { + String query = "SELECT /*! USE_NL*/ a.firstname ,a.lastname , a.gender ,d.name FROM elasticsearch-sql_test_index/people a " + + " JOIN elasticsearch-sql_test_index/dog d on d.holdersName = a.firstname " + + " WHERE " + + " (a.age > 10 OR a.balance > 2000)" + + " AND d.age > 1"; + SearchHit[] hits = joinAndGetHits(query); + Assert.assertEquals(2,hits.length); + + Map oneMatch = ImmutableMap.of("a.firstname", (Object)"Daenerys", "a.lastname","Targaryen", + "a.gender","M","d.name", "rex"); + Map secondMatch = ImmutableMap.of("a.firstname", (Object) "Hattie", "a.lastname", "Bond", + "a.gender", "M", "d.name", "snoopy"); + + Assert.assertTrue(hitsContains(hits, oneMatch)); + Assert.assertTrue(hitsContains(hits,secondMatch)); + + } + + @Test + public void joinParseWithHintsCheckSelectedFieldsSplitHASH() throws SqlParseException, SQLFeatureNotSupportedException, IOException { String query = "SELECT /*! HASH_WITH_TERMS_FILTER*/ a.firstname ,a.lastname , a.gender ,d.name FROM elasticsearch-sql_test_index/people a " + " JOIN elasticsearch-sql_test_index/dog d on d.holdersName = a.firstname " + " WHERE " + @@ -64,10 +82,10 @@ private String hashJoinRunAndExplain(String query) throws IOException, SqlParseE executor.run(); return explain.explain(); } - private SearchHit[] hashJoinGetHits(String query) throws SqlParseException, SQLFeatureNotSupportedException, IOException { + private SearchHit[] joinAndGetHits(String query) throws SqlParseException, SQLFeatureNotSupportedException, IOException { SearchDao searchDao = MainTestSuite.getSearchDao(); SqlElasticRequestBuilder explain = searchDao.explain(query); - HashJoinElasticExecutor executor = new HashJoinElasticExecutor(searchDao.getClient(), (HashJoinElasticRequestBuilder) explain); + ElasticJoinExecutor executor = ElasticJoinExecutor.createJoinExecutor(searchDao.getClient(),explain); executor.run(); return executor.getHits().getHits(); } @@ -102,7 +120,20 @@ public void joinWithNoWhereButWithCondition() throws SQLFeatureNotSupportedExcep String query = String.format("select c.gender , h.name,h.words from %s/gotCharacters c " + "JOIN %s/gotHouses h " + "on c.house = h.name",TEST_INDEX,TEST_INDEX); - SearchHit[] hits = hashJoinGetHits(query); + SearchHit[] hits = joinAndGetHits(query); + Assert.assertEquals(4,hits.length); + Map someMatch = ImmutableMap.of("c.gender", (Object)"F", "h.name","Targaryen", + "h.words","fireAndBlood"); + Assert.assertTrue(hitsContains(hits, someMatch)); + } + + + @Test + public void joinWithNoWhereButWithConditionNL() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + String query = String.format("select /*! USE_NL*/ c.gender , h.name,h.words from %s/gotCharacters c " + + "JOIN %s/gotHouses h " + + "on h.name = c.house ",TEST_INDEX,TEST_INDEX); + SearchHit[] hits = joinAndGetHits(query); Assert.assertEquals(4,hits.length); Map someMatch = ImmutableMap.of("c.gender", (Object)"F", "h.name","Targaryen", "h.words","fireAndBlood"); @@ -115,7 +146,17 @@ public void joinNoConditionButWithWhere() throws SQLFeatureNotSupportedException String query = String.format("select c.gender , h.name,h.words from %s/gotCharacters c " + "JOIN %s/gotHouses h " + "where c.name.firstname='Daenerys'",TEST_INDEX,TEST_INDEX); - SearchHit[] hits = hashJoinGetHits(query); + SearchHit[] hits = joinAndGetHits(query); + Assert.assertEquals(3,hits.length); + + } + @Test + public void joinNoConditionButWithWhereNL() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + + String query = String.format("select /*! USE_NL*/ c.gender , h.name,h.words from %s/gotCharacters c " + + "JOIN %s/gotHouses h " + + "where c.name.firstname='Daenerys'",TEST_INDEX,TEST_INDEX); + SearchHit[] hits = joinAndGetHits(query); Assert.assertEquals(3,hits.length); } @@ -125,7 +166,17 @@ public void joinNoConditionAndNoWhere() throws SQLFeatureNotSupportedException, String query = String.format("select c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + "JOIN %s/gotHouses h ",TEST_INDEX,TEST_INDEX); - SearchHit[] hits = hashJoinGetHits(query); + SearchHit[] hits = joinAndGetHits(query); + Assert.assertEquals(12,hits.length); + + } + +@Test + public void joinNoConditionAndNoWhereNL() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + + String query = String.format("select /*! USE_NL*/ c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + + "JOIN %s/gotHouses h ",TEST_INDEX,TEST_INDEX); + SearchHit[] hits = joinAndGetHits(query); Assert.assertEquals(12,hits.length); } @@ -136,7 +187,17 @@ public void joinNoConditionAndNoWhereWithTotalLimit() throws SQLFeatureNotSuppor String query = String.format("select c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + "JOIN %s/gotHouses h LIMIT 10",TEST_INDEX,TEST_INDEX); - SearchHit[] hits = hashJoinGetHits(query); + SearchHit[] hits = joinAndGetHits(query); + Assert.assertEquals(10,hits.length); + + } + + @Test + public void joinNoConditionAndNoWhereWithTotalLimitNL() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + + String query = String.format("select /*! USE_NL*/ c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + + "JOIN %s/gotHouses h LIMIT 10",TEST_INDEX,TEST_INDEX); + SearchHit[] hits = joinAndGetHits(query); Assert.assertEquals(10,hits.length); } @@ -147,7 +208,21 @@ public void joinWithNestedFieldsOnReturn() throws SQLFeatureNotSupportedExceptio "JOIN %s/gotHouses h " + "on c.house = h.name " + "where c.name.firstname='Daenerys'", TEST_INDEX,TEST_INDEX); - SearchHit[] hits = hashJoinGetHits(query); + SearchHit[] hits = joinAndGetHits(query); + Assert.assertEquals(1,hits.length); + //use flatten? + Map someMatch = ImmutableMap.of("c.name.firstname", (Object)"Daenerys","c.parents.father","Aerys", "h.name","Targaryen", + "h.words","fireAndBlood"); + Assert.assertTrue(hitsContains(hits, someMatch)); + } + + @Test + public void joinWithNestedFieldsOnReturnNL() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + String query = String.format("select /*! USE_NL*/ c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + + "JOIN %s/gotHouses h " + + "on h.name = c.house " + + "where c.name.firstname='Daenerys'", TEST_INDEX,TEST_INDEX); + SearchHit[] hits = joinAndGetHits(query); Assert.assertEquals(1,hits.length); //use flatten? Map someMatch = ImmutableMap.of("c.name.firstname", (Object)"Daenerys","c.parents.father","Aerys", "h.name","Targaryen", @@ -161,7 +236,20 @@ public void joinWithNestedFieldsOnComparisonAndOnReturn() throws SQLFeatureNotSu "JOIN %s/gotHouses h " + "on c.name.lastname = h.name " + "where c.name.firstname='Daenerys'", TEST_INDEX,TEST_INDEX); - SearchHit[] hits = hashJoinGetHits(query); + SearchHit[] hits = joinAndGetHits(query); + Assert.assertEquals(1,hits.length); + Map someMatch = ImmutableMap.of("c.name.firstname", (Object)"Daenerys","c.parents.father","Aerys", "h.name","Targaryen", + "h.words","fireAndBlood"); + Assert.assertTrue(hitsContains(hits, someMatch)); + } + + @Test + public void joinWithNestedFieldsOnComparisonAndOnReturnNL() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + String query = String.format("select /*! USE_NL*/ c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + + "JOIN %s/gotHouses h " + + "on h.name = c.name.lastname " + + "where c.name.firstname='Daenerys'", TEST_INDEX,TEST_INDEX); + SearchHit[] hits = joinAndGetHits(query); Assert.assertEquals(1,hits.length); Map someMatch = ImmutableMap.of("c.name.firstname", (Object)"Daenerys","c.parents.father","Aerys", "h.name","Targaryen", "h.words","fireAndBlood"); @@ -175,7 +263,27 @@ public void testLeftJoin() throws SQLFeatureNotSupportedException, IOException, "LEFT JOIN %s/gotCharacters f " + "on c.parents.father = f.name.firstname " , TEST_INDEX,TEST_INDEX); - SearchHit[] hits = hashJoinGetHits(query); + SearchHit[] hits = joinAndGetHits(query); + Assert.assertEquals(4,hits.length); + + Map oneMatch = new HashMap<>(); + oneMatch.put("c.name.firstname", "Daenerys"); + oneMatch.put("f.name.firstname",null); + oneMatch.put("f.name.lastname",null); + + Assert.assertTrue(hitsContains(hits, oneMatch)); + Map secondMatch = ImmutableMap.of("c.name.firstname", (Object)"Brandon", + "f.name.firstname","Eddard", "f.name.lastname","Stark"); + Assert.assertTrue(hitsContains(hits, secondMatch)); + } + + @Test + public void testLeftJoinNL() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + String query = String.format("select /*! USE_NL*/ c.name.firstname, f.name.firstname,f.name.lastname from %s/gotCharacters c " + + "LEFT JOIN %s/gotCharacters f " + + "on f.name.firstname = c.parents.father " + , TEST_INDEX,TEST_INDEX); + SearchHit[] hits = joinAndGetHits(query); Assert.assertEquals(4,hits.length); Map oneMatch = new HashMap<>(); @@ -194,7 +302,7 @@ public void hintLimits_firstLimitSecondNull() throws SQLFeatureNotSupportedExcep String query = String.format("select /*! JOIN_TABLES_LIMIT(2,null) */ c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + "JOIN %s/gotHouses h ",TEST_INDEX,TEST_INDEX); - SearchHit[] hits = hashJoinGetHits(query); + SearchHit[] hits = joinAndGetHits(query); Assert.assertEquals(6,hits.length); } @@ -203,7 +311,7 @@ public void hintLimits_firstLimitSecondLimit() throws SQLFeatureNotSupportedExce String query = String.format("select /*! JOIN_TABLES_LIMIT(2,2) */ c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + "JOIN %s/gotHouses h ",TEST_INDEX,TEST_INDEX); - SearchHit[] hits = hashJoinGetHits(query); + SearchHit[] hits = joinAndGetHits(query); Assert.assertEquals(4,hits.length); } @@ -212,7 +320,7 @@ public void hintLimits_firstNullSecondLimit() throws SQLFeatureNotSupportedExcep String query = String.format("select /*! JOIN_TABLES_LIMIT(null,2) */ c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + "JOIN %s/gotHouses h ",TEST_INDEX,TEST_INDEX); - SearchHit[] hits = hashJoinGetHits(query); + SearchHit[] hits = joinAndGetHits(query); Assert.assertEquals(8,hits.length); } @@ -222,7 +330,7 @@ public void testLeftJoinWithLimit() throws SQLFeatureNotSupportedException, IOEx "LEFT JOIN %s/gotCharacters f " + "on c.parents.father = f.name.firstname " , TEST_INDEX,TEST_INDEX); - SearchHit[] hits = hashJoinGetHits(query); + SearchHit[] hits = joinAndGetHits(query); Assert.assertEquals(3,hits.length); } diff --git a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java index 1b91865b..588bbb6e 100644 --- a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java +++ b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java @@ -193,5 +193,4 @@ private static InetSocketTransportAddress getTransportAddress() { return new InetSocketTransportAddress(host, Integer.parseInt(port)); } - } From 8794199d9c2948d5a90bdcfbdfa1d864ad5dffd4 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sat, 19 Sep 2015 17:05:41 +0300 Subject: [PATCH 068/559] nestedLoops with hints --- .../plugin/nlpcn/ElasticJoinExecutor.java | 1 + .../nlpcn/NestedLoopsElasticExecutor.java | 215 +++++++---- .../es4sql/domain/hints/HintFactory.java | 7 + .../nlpcn/es4sql/domain/hints/HintType.java | 3 +- .../query/join/ESNestedLoopsQueryAction.java | 13 + .../NestedLoopsElasticRequestBuilder.java | 10 +- src/test/java/org/nlpcn/es4sql/JoinTests.java | 360 ++++++++++-------- 7 files changed, 367 insertions(+), 242 deletions(-) diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticJoinExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticJoinExecutor.java index b9ca3bba..57478736 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticJoinExecutor.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticJoinExecutor.java @@ -180,6 +180,7 @@ protected void addUnmachedResult(List combinedResults, List emptySecondTableHitSource = createNullsSource(secondTableReturnedFields); + mergeSourceAndAddAliases(emptySecondTableHitSource, searchHit,t1Alias,t2Alias); combinedResults.add(searchHit); diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/NestedLoopsElasticExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/NestedLoopsElasticExecutor.java index 7b3fb641..54947c24 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/NestedLoopsElasticExecutor.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/NestedLoopsElasticExecutor.java @@ -1,30 +1,21 @@ package org.elasticsearch.plugin.nlpcn; import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource; -import org.elasticsearch.action.get.MultiGetResponse; -import org.elasticsearch.action.search.MultiSearchRequest; -import org.elasticsearch.action.search.MultiSearchResponse; -import org.elasticsearch.action.search.SearchRequestBuilder; -import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.action.search.*; import org.elasticsearch.client.Client; import org.elasticsearch.common.text.StringText; -import org.elasticsearch.index.query.BoolQueryBuilder; -import org.elasticsearch.index.query.QueryBuilders; -import org.elasticsearch.rest.RestChannel; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.internal.InternalSearchHit; import org.nlpcn.es4sql.domain.Condition; -import org.nlpcn.es4sql.domain.Field; import org.nlpcn.es4sql.domain.Select; import org.nlpcn.es4sql.domain.Where; import org.nlpcn.es4sql.exception.SqlParseException; import org.nlpcn.es4sql.query.DefaultQueryAction; import org.nlpcn.es4sql.query.join.NestedLoopsElasticRequestBuilder; -import org.nlpcn.es4sql.query.maker.FilterMaker; -import org.nlpcn.es4sql.query.maker.QueryMaker; +import org.nlpcn.es4sql.query.join.TableInJoinRequestBuilder; -import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -34,7 +25,7 @@ * Created by Eliran on 15/9/2015. */ public class NestedLoopsElasticExecutor extends ElasticJoinExecutor { - private static final int MULTI_SIZE = 100; + private final NestedLoopsElasticRequestBuilder nestedLoopsRequest; @@ -49,82 +40,140 @@ public NestedLoopsElasticExecutor(Client client, NestedLoopsElasticRequestBuilde protected List innerRun() throws SqlParseException { List combinedResults = new ArrayList<>(); int totalLimit = nestedLoopsRequest.getTotalLimit(); - SearchResponse response = nestedLoopsRequest.getFirstTable().getRequestBuilder().get(); + int multiSearchMaxSize = nestedLoopsRequest.getMultiSearchMaxSize(); + Select secondTableSelect = nestedLoopsRequest.getSecondTable().getOriginalSelect(); + Where originalSecondTableWhere = secondTableSelect.getWhere(); + orderConditions(nestedLoopsRequest.getFirstTable().getAlias(),nestedLoopsRequest.getSecondTable().getAlias()); Collection conditions = nestedLoopsRequest.getT1FieldToCondition().values(); + + FetchWithScrollResponse fetchWithScrollResponse = firstFetch(this.nestedLoopsRequest.getFirstTable()); + SearchResponse firstTableResponse = fetchWithScrollResponse.getResponse(); + boolean needScrollForFirstTable = fetchWithScrollResponse.isNeedScrollForFirstTable(); + int currentCombinedResults = 0; boolean finishedWithFirstTable = false; + while (totalLimit > currentCombinedResults && !finishedWithFirstTable){ - SearchHit[] hits = response.getHits().getHits(); - if(hits.length == 0 || hits.length < MAX_RESULTS_ON_ONE_FETCH) finishedWithFirstTable = true; - Select secondTableSelect = nestedLoopsRequest.getSecondTable().getOriginalSelect(); - Where originalWhere = secondTableSelect.getWhere(); - String t1Alias = nestedLoopsRequest.getFirstTable().getAlias(); - String t2Alias = nestedLoopsRequest.getSecondTable().getAlias(); + + SearchHit[] hits = firstTableResponse.getHits().getHits(); boolean finishedMultiSearches = hits.length == 0; - int currentIndex = 0 ; + int currentHitsIndex = 0 ; while(!finishedMultiSearches){ - MultiSearchRequest multiSearchRequest = new MultiSearchRequest(); - int multiSearchSize = 0; - for(int i = currentIndex ; i < currentIndex + MULTI_SIZE && i< hits.length ; i++ ){ - Map hitFromFirstTableAsMap = hits[i].sourceAsMap(); - Where newWhere = Where.newInstance(); - if(originalWhere!=null) newWhere.addWhere(originalWhere); - for(Condition c : conditions){ - Object value = deepSearchInMap(hitFromFirstTableAsMap,c.getValue().toString()); - Condition conditionWithValue = new Condition(Where.CONN.AND,c.getName(),c.getOpear(),value); - newWhere.addWhere(conditionWithValue); - } - //using the 2nd table select and DefaultAction because we can't just change query on request (need to create lot of requests) - if(newWhere.getWheres().size() != 0) { - secondTableSelect.setWhere(newWhere); - } - DefaultQueryAction action = new DefaultQueryAction(this.client , secondTableSelect); - action.explain(); - multiSearchRequest.add(action.getRequestBuilder()); - multiSearchSize++; - } - MultiSearchResponse.Item[] responses = client.multiSearch(multiSearchRequest).actionGet().getResponses(); - for(int j =0 ; j < responses.length && currentCombinedResults < totalLimit ; j++){ - SearchHits responseForHit = responses[j].getResponse().getHits(); - SearchHit hitFromFirstTable = hits[currentIndex+j]; - //todo: if responseForHit.getHits.length < responseForHit.getTotalHits(). need to fetch more! - onlyReturnedFields(hitFromFirstTable.sourceAsMap(), nestedLoopsRequest.getFirstTable().getReturnedFields()); - if(responseForHit.getHits().length == 0 && nestedLoopsRequest.getJoinType() == SQLJoinTableSource.JoinType.LEFT_OUTER_JOIN){ - addUnmachedResult(combinedResults, nestedLoopsRequest.getSecondTable().getReturnedFields(), currentCombinedResults, t1Alias, t2Alias, hitFromFirstTable); - currentCombinedResults++; - - continue; - } - - for(SearchHit matchedHit : responseForHit.getHits() ){ - onlyReturnedFields(matchedHit.sourceAsMap(), nestedLoopsRequest.getSecondTable().getReturnedFields()); - - //todo: decide which id to put or type. or maby its ok this way. just need to doc. - InternalSearchHit searchHit = new InternalSearchHit(currentCombinedResults, hitFromFirstTable.id() + "|" + matchedHit.getId(), new StringText(hitFromFirstTable.getType() + "|" + matchedHit.getType()), hitFromFirstTable.getFields()); - searchHit.sourceRef(hitFromFirstTable.getSourceRef()); - searchHit.sourceAsMap().clear(); - searchHit.sourceAsMap().putAll(hitFromFirstTable.sourceAsMap()); - - mergeSourceAndAddAliases(matchedHit.getSource(), searchHit, t1Alias, t2Alias); - - combinedResults.add(searchHit); - currentCombinedResults++; - if(currentCombinedResults >= totalLimit) break; - } - if(currentCombinedResults >= totalLimit) break; - - } - currentIndex += multiSearchSize; - finishedMultiSearches = currentIndex >= hits.length-1; + MultiSearchRequest multiSearchRequest = createMultiSearchRequest(multiSearchMaxSize, conditions, hits, secondTableSelect, originalSecondTableWhere, currentHitsIndex); + int multiSearchSize = multiSearchRequest.requests().size(); + currentCombinedResults = combineResultsFromMultiResponses(combinedResults, totalLimit, currentCombinedResults, hits, currentHitsIndex, multiSearchRequest); + currentHitsIndex += multiSearchSize; + finishedMultiSearches = currentHitsIndex >= hits.length-1 || currentCombinedResults >= totalLimit; } + if( hits.length < MAX_RESULTS_ON_ONE_FETCH ) needScrollForFirstTable = false; + + if(!finishedWithFirstTable) + { + if(needScrollForFirstTable) + firstTableResponse = client.prepareSearchScroll(firstTableResponse.getScrollId()).setScroll(new TimeValue(600000)).get(); + else finishedWithFirstTable = true; + } } return combinedResults; } + private int combineResultsFromMultiResponses(List combinedResults, int totalLimit, int currentCombinedResults, SearchHit[] hits, int currentIndex, MultiSearchRequest multiSearchRequest) { + MultiSearchResponse.Item[] responses = client.multiSearch(multiSearchRequest).actionGet().getResponses(); + String t1Alias = nestedLoopsRequest.getFirstTable().getAlias(); + String t2Alias = nestedLoopsRequest.getSecondTable().getAlias(); + + for(int j =0 ; j < responses.length && currentCombinedResults < totalLimit ; j++){ + SearchHit hitFromFirstTable = hits[currentIndex+j]; + onlyReturnedFields(hitFromFirstTable.sourceAsMap(), nestedLoopsRequest.getFirstTable().getReturnedFields()); + + SearchResponse multiItemResponse = responses[j].getResponse(); + updateMetaSearchResults(multiItemResponse); + + //todo: if responseForHit.getHits.length < responseForHit.getTotalHits(). need to fetch more! + SearchHits responseForHit = multiItemResponse.getHits(); + + if(responseForHit.getHits().length == 0 && nestedLoopsRequest.getJoinType() == SQLJoinTableSource.JoinType.LEFT_OUTER_JOIN){ + addUnmachedResult(combinedResults, nestedLoopsRequest.getSecondTable().getReturnedFields(), currentCombinedResults, t1Alias, t2Alias, hitFromFirstTable); + currentCombinedResults++; + continue; + } + + for(SearchHit matchedHit : responseForHit.getHits() ){ + InternalSearchHit searchHit = getMergedHit(currentCombinedResults, t1Alias, t2Alias, hitFromFirstTable, matchedHit); + combinedResults.add(searchHit); + currentCombinedResults++; + if(currentCombinedResults >= totalLimit) break; + } + if(currentCombinedResults >= totalLimit) break; + + } + return currentCombinedResults; + } + + private InternalSearchHit getMergedHit(int currentCombinedResults, String t1Alias, String t2Alias, SearchHit hitFromFirstTable, SearchHit matchedHit) { + onlyReturnedFields(matchedHit.sourceAsMap(), nestedLoopsRequest.getSecondTable().getReturnedFields()); + InternalSearchHit searchHit = new InternalSearchHit(currentCombinedResults, hitFromFirstTable.id() + "|" + matchedHit.getId(), new StringText(hitFromFirstTable.getType() + "|" + matchedHit.getType()), hitFromFirstTable.getFields()); + searchHit.sourceRef(hitFromFirstTable.getSourceRef()); + searchHit.sourceAsMap().clear(); + searchHit.sourceAsMap().putAll(hitFromFirstTable.sourceAsMap()); + + mergeSourceAndAddAliases(matchedHit.getSource(), searchHit, t1Alias, t2Alias); + return searchHit; + } + + private MultiSearchRequest createMultiSearchRequest(int multiSearchMaxSize, Collection conditions, SearchHit[] hits, Select secondTableSelect, Where originalWhere, int currentIndex) throws SqlParseException { + MultiSearchRequest multiSearchRequest = new MultiSearchRequest(); + for(int i = currentIndex ; i < currentIndex + multiSearchMaxSize && i< hits.length ; i++ ){ + Map hitFromFirstTableAsMap = hits[i].sourceAsMap(); + Where newWhere = Where.newInstance(); + if(originalWhere!=null) newWhere.addWhere(originalWhere); + for(Condition c : conditions){ + Object value = deepSearchInMap(hitFromFirstTableAsMap,c.getValue().toString()); + Condition conditionWithValue = new Condition(Where.CONN.AND,c.getName(),c.getOpear(),value); + newWhere.addWhere(conditionWithValue); + } + //using the 2nd table select and DefaultAction because we can't just change query on request (need to create lot of requests) + if(newWhere.getWheres().size() != 0) { + secondTableSelect.setWhere(newWhere); + } + DefaultQueryAction action = new DefaultQueryAction(this.client , secondTableSelect); + action.explain(); + SearchRequestBuilder secondTableRequest = action.getRequestBuilder(); + Integer secondTableHintLimit = this.nestedLoopsRequest.getSecondTable().getHintLimit(); + if(secondTableHintLimit != null && secondTableHintLimit <= MAX_RESULTS_ON_ONE_FETCH) + secondTableRequest.setSize(secondTableHintLimit); + multiSearchRequest.add(secondTableRequest); + } + return multiSearchRequest; + } + + private FetchWithScrollResponse firstFetch(TableInJoinRequestBuilder tableRequest) { + Integer hintLimit = tableRequest.getHintLimit(); + boolean needScrollForFirstTable = false; + SearchResponse responseWithHits; + if(hintLimit != null && hintLimit < MAX_RESULTS_ON_ONE_FETCH){ + + responseWithHits = tableRequest.getRequestBuilder().setSize(hintLimit).get(); + needScrollForFirstTable=true; + } + else { + //scroll request with max. + responseWithHits = tableRequest.getRequestBuilder().setSearchType(SearchType.SCAN) + .setScroll(new TimeValue(60000)) + .setSize(MAX_RESULTS_ON_ONE_FETCH).get(); + if(responseWithHits.getHits().getTotalHits() < MAX_RESULTS_ON_ONE_FETCH) + needScrollForFirstTable = true; + responseWithHits = client.prepareSearchScroll(responseWithHits.getScrollId()).setScroll(new TimeValue(600000)).get(); + } + + updateMetaSearchResults(responseWithHits); + return new FetchWithScrollResponse(responseWithHits,needScrollForFirstTable); + } + private void orderConditions(String t1Alias, String t2Alias) { Collection conditions = nestedLoopsRequest.getT1FieldToCondition().values(); for(Condition c : conditions){ @@ -137,4 +186,22 @@ private void orderConditions(String t1Alias, String t2Alias) { } + private class FetchWithScrollResponse { + private SearchResponse response; + private boolean needScrollForFirstTable; + + private FetchWithScrollResponse(SearchResponse response, boolean needScrollForFirstTable) { + this.response = response; + this.needScrollForFirstTable = needScrollForFirstTable; + } + + public SearchResponse getResponse() { + return response; + } + + public boolean isNeedScrollForFirstTable() { + return needScrollForFirstTable; + } + + } } diff --git a/src/main/java/org/nlpcn/es4sql/domain/hints/HintFactory.java b/src/main/java/org/nlpcn/es4sql/domain/hints/HintFactory.java index 73e714bd..e63cca42 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/hints/HintFactory.java +++ b/src/main/java/org/nlpcn/es4sql/domain/hints/HintFactory.java @@ -16,6 +16,7 @@ public static Hint getHintFromString(String hintAsString){ return new Hint(HintType.HASH_WITH_TERMS_FILTER,null); if(hintAsString.startsWith("! JOIN_TABLES_LIMIT")){ String[] numbers = getParamsFromHint(hintAsString, "! JOIN_TABLES_LIMIT"); + //todo: check if numbers etc.. List params = new ArrayList<>(); for (String number : numbers){ if(number.equals("null") || number.equals("infinity")){ @@ -28,6 +29,12 @@ public static Hint getHintFromString(String hintAsString){ return new Hint(HintType.JOIN_LIMIT,params.toArray()); } + if(hintAsString.startsWith("! NL_MULTISEARCH_SIZE")) { + String[] number = getParamsFromHint(hintAsString,"! NL_MULTISEARCH_SIZE"); + //todo: check if numbers etc.. + int multiSearchSize = Integer.parseInt(number[0]); + return new Hint(HintType.NL_MULTISEARCH_SIZE,new Object[]{multiSearchSize}); + } return null; } diff --git a/src/main/java/org/nlpcn/es4sql/domain/hints/HintType.java b/src/main/java/org/nlpcn/es4sql/domain/hints/HintType.java index f1dfddd3..8891f9e5 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/hints/HintType.java +++ b/src/main/java/org/nlpcn/es4sql/domain/hints/HintType.java @@ -10,6 +10,7 @@ public enum HintType { HASH_WITH_TERMS_FILTER, JOIN_LIMIT, - USE_NESTED_LOOPS; + USE_NESTED_LOOPS, + NL_MULTISEARCH_SIZE; } diff --git a/src/main/java/org/nlpcn/es4sql/query/join/ESNestedLoopsQueryAction.java b/src/main/java/org/nlpcn/es4sql/query/join/ESNestedLoopsQueryAction.java index 1f8e4493..af2ed833 100644 --- a/src/main/java/org/nlpcn/es4sql/query/join/ESNestedLoopsQueryAction.java +++ b/src/main/java/org/nlpcn/es4sql/query/join/ESNestedLoopsQueryAction.java @@ -3,6 +3,8 @@ import org.elasticsearch.client.Client; import org.nlpcn.es4sql.domain.Condition; import org.nlpcn.es4sql.domain.JoinSelect; +import org.nlpcn.es4sql.domain.hints.Hint; +import org.nlpcn.es4sql.domain.hints.HintType; import org.nlpcn.es4sql.exception.SqlParseException; import org.nlpcn.es4sql.query.QueryAction; import org.nlpcn.es4sql.query.SqlElasticRequestBuilder; @@ -33,6 +35,17 @@ protected JoinRequestBuilder createSpecificBuilder() { return new NestedLoopsElasticRequestBuilder(); } + @Override + protected void updateRequestWithHints(JoinRequestBuilder requestBuilder) { + super.updateRequestWithHints(requestBuilder); + for(Hint hint : this.joinSelect.getHints()){ + if(hint.getType() == HintType.NL_MULTISEARCH_SIZE){ + Integer multiSearchMaxSize = (Integer) hint.getParams()[0]; + ((NestedLoopsElasticRequestBuilder) requestBuilder).setMultiSearchMaxSize(multiSearchMaxSize); + } + } + } + private String removeAlias(String field) { String alias = joinSelect.getFirstTable().getAlias(); if(!field.startsWith(alias+".")) diff --git a/src/main/java/org/nlpcn/es4sql/query/join/NestedLoopsElasticRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/join/NestedLoopsElasticRequestBuilder.java index 00c96add..605b1945 100644 --- a/src/main/java/org/nlpcn/es4sql/query/join/NestedLoopsElasticRequestBuilder.java +++ b/src/main/java/org/nlpcn/es4sql/query/join/NestedLoopsElasticRequestBuilder.java @@ -11,9 +11,10 @@ */ public class NestedLoopsElasticRequestBuilder extends JoinRequestBuilder { private Map t1FieldToCondition; - + private int multiSearchMaxSize; public NestedLoopsElasticRequestBuilder() { t1FieldToCondition = new HashMap<>(); + multiSearchMaxSize = 100; } public Map getT1FieldToCondition() { @@ -24,6 +25,13 @@ public void setT1FieldToCondition(Map t1FieldToCondition) { this.t1FieldToCondition = t1FieldToCondition; } + public int getMultiSearchMaxSize() { + return multiSearchMaxSize; + } + + public void setMultiSearchMaxSize(int multiSearchMaxSize) { + this.multiSearchMaxSize = multiSearchMaxSize; + } public void addConditionMapping(Condition c){ t1FieldToCondition.put(new Field(c.getName(),null),c); diff --git a/src/test/java/org/nlpcn/es4sql/JoinTests.java b/src/test/java/org/nlpcn/es4sql/JoinTests.java index 4e701410..d539e050 100644 --- a/src/test/java/org/nlpcn/es4sql/JoinTests.java +++ b/src/test/java/org/nlpcn/es4sql/JoinTests.java @@ -25,42 +25,31 @@ public class JoinTests { @Test public void joinParseCheckSelectedFieldsSplitHASH() throws SqlParseException, SQLFeatureNotSupportedException, IOException { - String query = "SELECT a.firstname ,a.lastname , a.gender ,d.name FROM elasticsearch-sql_test_index/people a " + - " JOIN elasticsearch-sql_test_index/dog d on d.holdersName = a.firstname " + - " WHERE " + - " (a.age > 10 OR a.balance > 2000)" + - " AND d.age > 1"; - SearchHit[] hits = joinAndGetHits(query); - Assert.assertEquals(2,hits.length); - - Map oneMatch = ImmutableMap.of("a.firstname", (Object)"Daenerys", "a.lastname","Targaryen", - "a.gender","M","d.name", "rex"); - Map secondMatch = ImmutableMap.of("a.firstname", (Object) "Hattie", "a.lastname", "Bond", - "a.gender", "M", "d.name", "snoopy"); - - Assert.assertTrue(hitsContains(hits, oneMatch)); - Assert.assertTrue(hitsContains(hits,secondMatch)); - + joinParseCheckSelectedFieldsSplit(false); } @Test public void joinParseCheckSelectedFieldsSplitNL() throws SqlParseException, SQLFeatureNotSupportedException, IOException { - String query = "SELECT /*! USE_NL*/ a.firstname ,a.lastname , a.gender ,d.name FROM elasticsearch-sql_test_index/people a " + + joinParseCheckSelectedFieldsSplit(true); + } + + private void joinParseCheckSelectedFieldsSplit(boolean useNestedLoops) throws SqlParseException, SQLFeatureNotSupportedException, IOException { + String query = "SELECT a.firstname ,a.lastname , a.gender ,d.name FROM elasticsearch-sql_test_index/people a " + " JOIN elasticsearch-sql_test_index/dog d on d.holdersName = a.firstname " + " WHERE " + " (a.age > 10 OR a.balance > 2000)" + " AND d.age > 1"; + if(useNestedLoops) query = query.replace("SELECT","SELECT /*! USE_NL*/ "); SearchHit[] hits = joinAndGetHits(query); - Assert.assertEquals(2,hits.length); + Assert.assertEquals(2, hits.length); - Map oneMatch = ImmutableMap.of("a.firstname", (Object)"Daenerys", "a.lastname","Targaryen", - "a.gender","M","d.name", "rex"); + Map oneMatch = ImmutableMap.of("a.firstname", (Object) "Daenerys", "a.lastname", "Targaryen", + "a.gender", "M", "d.name", "rex"); Map secondMatch = ImmutableMap.of("a.firstname", (Object) "Hattie", "a.lastname", "Bond", "a.gender", "M", "d.name", "snoopy"); Assert.assertTrue(hitsContains(hits, oneMatch)); Assert.assertTrue(hitsContains(hits,secondMatch)); - } @Test @@ -75,216 +64,151 @@ public void joinParseWithHintsCheckSelectedFieldsSplitHASH() throws SqlParseExce Assert.assertTrue(containTerms); } - private String hashJoinRunAndExplain(String query) throws IOException, SqlParseException, SQLFeatureNotSupportedException { - SearchDao searchDao = MainTestSuite.getSearchDao(); - HashJoinElasticRequestBuilder explain = (HashJoinElasticRequestBuilder) searchDao.explain(query); - HashJoinElasticExecutor executor = new HashJoinElasticExecutor(searchDao.getClient(), explain); - executor.run(); - return explain.explain(); - } - private SearchHit[] joinAndGetHits(String query) throws SqlParseException, SQLFeatureNotSupportedException, IOException { - SearchDao searchDao = MainTestSuite.getSearchDao(); - SqlElasticRequestBuilder explain = searchDao.explain(query); - ElasticJoinExecutor executor = ElasticJoinExecutor.createJoinExecutor(searchDao.getClient(),explain); - executor.run(); - return executor.getHits().getHits(); - } - - private boolean hitsContains(SearchHit[] hits, Map matchMap) { - for(SearchHit hit : hits){ - Map hitMap = hit.sourceAsMap(); - boolean matchedHit = true; - for(Map.Entry entry: hitMap.entrySet()){ - if(!matchMap.containsKey(entry.getKey())) { - matchedHit = false; - break; - } - if(!equalsWithNullCheck(matchMap.get(entry.getKey()), entry.getValue())){ - matchedHit = false; - break; - } - } - if(matchedHit) return true; - } - return false; - } - - private boolean equalsWithNullCheck(Object one, Object other) { - if(one == null) return other == null; - return one.equals(other); - } - @Test - public void joinWithNoWhereButWithCondition() throws SQLFeatureNotSupportedException, IOException, SqlParseException { - String query = String.format("select c.gender , h.name,h.words from %s/gotCharacters c " + - "JOIN %s/gotHouses h " + - "on c.house = h.name",TEST_INDEX,TEST_INDEX); - SearchHit[] hits = joinAndGetHits(query); - Assert.assertEquals(4,hits.length); - Map someMatch = ImmutableMap.of("c.gender", (Object)"F", "h.name","Targaryen", - "h.words","fireAndBlood"); - Assert.assertTrue(hitsContains(hits, someMatch)); + public void joinWithNoWhereButWithConditionHash() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + joinWithNoWhereButWithCondition(false); } - @Test public void joinWithNoWhereButWithConditionNL() throws SQLFeatureNotSupportedException, IOException, SqlParseException { - String query = String.format("select /*! USE_NL*/ c.gender , h.name,h.words from %s/gotCharacters c " + + joinWithNoWhereButWithCondition(true); + } + + private void joinWithNoWhereButWithCondition(boolean useNestedLoops) throws SqlParseException, SQLFeatureNotSupportedException, IOException { + String query = String.format("select c.gender , h.name,h.words from %s/gotCharacters c " + "JOIN %s/gotHouses h " + "on h.name = c.house ",TEST_INDEX,TEST_INDEX); + if(useNestedLoops) query = query.replace("select","select /*! USE_NL*/ "); SearchHit[] hits = joinAndGetHits(query); - Assert.assertEquals(4,hits.length); - Map someMatch = ImmutableMap.of("c.gender", (Object)"F", "h.name","Targaryen", - "h.words","fireAndBlood"); + Assert.assertEquals(4, hits.length); + Map someMatch = ImmutableMap.of("c.gender", (Object) "F", "h.name", "Targaryen", + "h.words", "fireAndBlood"); Assert.assertTrue(hitsContains(hits, someMatch)); } @Test - public void joinNoConditionButWithWhere() throws SQLFeatureNotSupportedException, IOException, SqlParseException { - - String query = String.format("select c.gender , h.name,h.words from %s/gotCharacters c " + - "JOIN %s/gotHouses h " + - "where c.name.firstname='Daenerys'",TEST_INDEX,TEST_INDEX); - SearchHit[] hits = joinAndGetHits(query); - Assert.assertEquals(3,hits.length); - + public void joinNoConditionButWithWhereHASH() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + joinNoConditionButWithWhere(false); } @Test public void joinNoConditionButWithWhereNL() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + joinNoConditionButWithWhere(true); + } - String query = String.format("select /*! USE_NL*/ c.gender , h.name,h.words from %s/gotCharacters c " + + private void joinNoConditionButWithWhere(boolean useNestedLoops) throws SqlParseException, SQLFeatureNotSupportedException, IOException { + String query = String.format("select c.gender , h.name,h.words from %s/gotCharacters c " + "JOIN %s/gotHouses h " + "where c.name.firstname='Daenerys'",TEST_INDEX,TEST_INDEX); + if(useNestedLoops) query = query.replace("select","select /*! USE_NL*/ "); SearchHit[] hits = joinAndGetHits(query); - Assert.assertEquals(3,hits.length); - + Assert.assertEquals(3, hits.length); } @Test - public void joinNoConditionAndNoWhere() throws SQLFeatureNotSupportedException, IOException, SqlParseException { - - String query = String.format("select c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + - "JOIN %s/gotHouses h ",TEST_INDEX,TEST_INDEX); - SearchHit[] hits = joinAndGetHits(query); - Assert.assertEquals(12,hits.length); - + public void joinNoConditionAndNoWhereHASH() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + joinNoConditionAndNoWhere(false); } -@Test + @Test public void joinNoConditionAndNoWhereNL() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + joinNoConditionAndNoWhere(true); + } - String query = String.format("select /*! USE_NL*/ c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + + private void joinNoConditionAndNoWhere(boolean useNestedLoops) throws SqlParseException, SQLFeatureNotSupportedException, IOException { + String query = String.format("select c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + "JOIN %s/gotHouses h ",TEST_INDEX,TEST_INDEX); + if(useNestedLoops) query = query.replace("select","select /*! USE_NL*/ "); SearchHit[] hits = joinAndGetHits(query); - Assert.assertEquals(12,hits.length); - + Assert.assertEquals(12, hits.length); } - @Test - public void joinNoConditionAndNoWhereWithTotalLimit() throws SQLFeatureNotSupportedException, IOException, SqlParseException { - - String query = String.format("select c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + - "JOIN %s/gotHouses h LIMIT 10",TEST_INDEX,TEST_INDEX); - SearchHit[] hits = joinAndGetHits(query); - Assert.assertEquals(10,hits.length); - + public void joinNoConditionAndNoWhereWithTotalLimitHASH() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + joinNoConditionAndNoWhereWithTotalLimit(false); } @Test public void joinNoConditionAndNoWhereWithTotalLimitNL() throws SQLFeatureNotSupportedException, IOException, SqlParseException { - String query = String.format("select /*! USE_NL*/ c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + - "JOIN %s/gotHouses h LIMIT 10",TEST_INDEX,TEST_INDEX); - SearchHit[] hits = joinAndGetHits(query); - Assert.assertEquals(10,hits.length); + joinNoConditionAndNoWhereWithTotalLimit(true); } - @Test - public void joinWithNestedFieldsOnReturn() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + private void joinNoConditionAndNoWhereWithTotalLimit(boolean useNestedLoops) throws SqlParseException, SQLFeatureNotSupportedException, IOException { String query = String.format("select c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + - "JOIN %s/gotHouses h " + - "on c.house = h.name " + - "where c.name.firstname='Daenerys'", TEST_INDEX,TEST_INDEX); + "JOIN %s/gotHouses h LIMIT 10",TEST_INDEX,TEST_INDEX); + if(useNestedLoops) query = query.replace("select","select /*! USE_NL*/ "); SearchHit[] hits = joinAndGetHits(query); - Assert.assertEquals(1,hits.length); - //use flatten? - Map someMatch = ImmutableMap.of("c.name.firstname", (Object)"Daenerys","c.parents.father","Aerys", "h.name","Targaryen", - "h.words","fireAndBlood"); - Assert.assertTrue(hitsContains(hits, someMatch)); + Assert.assertEquals(10, hits.length); + } + + @Test + public void joinWithNestedFieldsOnReturnHASH() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + joinWithNestedFieldsOnReturn(false); } @Test public void joinWithNestedFieldsOnReturnNL() throws SQLFeatureNotSupportedException, IOException, SqlParseException { - String query = String.format("select /*! USE_NL*/ c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + + joinWithNestedFieldsOnReturn(true); + } + + private void joinWithNestedFieldsOnReturn(boolean useNestedLoops) throws SqlParseException, SQLFeatureNotSupportedException, IOException { + String query = String.format("select c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + "JOIN %s/gotHouses h " + "on h.name = c.house " + "where c.name.firstname='Daenerys'", TEST_INDEX,TEST_INDEX); + if(useNestedLoops) query = query.replace("select","select /*! USE_NL*/ "); SearchHit[] hits = joinAndGetHits(query); - Assert.assertEquals(1,hits.length); + Assert.assertEquals(1, hits.length); //use flatten? - Map someMatch = ImmutableMap.of("c.name.firstname", (Object)"Daenerys","c.parents.father","Aerys", "h.name","Targaryen", - "h.words","fireAndBlood"); + Map someMatch = ImmutableMap.of("c.name.firstname", (Object) "Daenerys", "c.parents.father", "Aerys", "h.name", "Targaryen", + "h.words", "fireAndBlood"); Assert.assertTrue(hitsContains(hits, someMatch)); } @Test - public void joinWithNestedFieldsOnComparisonAndOnReturn() throws SQLFeatureNotSupportedException, IOException, SqlParseException { - String query = String.format("select c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + - "JOIN %s/gotHouses h " + - "on c.name.lastname = h.name " + - "where c.name.firstname='Daenerys'", TEST_INDEX,TEST_INDEX); - SearchHit[] hits = joinAndGetHits(query); - Assert.assertEquals(1,hits.length); - Map someMatch = ImmutableMap.of("c.name.firstname", (Object)"Daenerys","c.parents.father","Aerys", "h.name","Targaryen", - "h.words","fireAndBlood"); - Assert.assertTrue(hitsContains(hits, someMatch)); + public void joinWithNestedFieldsOnComparisonAndOnReturnHASH() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + joinWithNestedFieldsOnComparisonAndOnReturn(false); } @Test public void joinWithNestedFieldsOnComparisonAndOnReturnNL() throws SQLFeatureNotSupportedException, IOException, SqlParseException { - String query = String.format("select /*! USE_NL*/ c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + + joinWithNestedFieldsOnComparisonAndOnReturn(true); + } + + private void joinWithNestedFieldsOnComparisonAndOnReturn(boolean useNestedLoops) throws SqlParseException, SQLFeatureNotSupportedException, IOException { + String query = String.format("select c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + "JOIN %s/gotHouses h " + "on h.name = c.name.lastname " + "where c.name.firstname='Daenerys'", TEST_INDEX,TEST_INDEX); + if(useNestedLoops) query = query.replace("select","select /*! USE_NL*/ "); SearchHit[] hits = joinAndGetHits(query); - Assert.assertEquals(1,hits.length); - Map someMatch = ImmutableMap.of("c.name.firstname", (Object)"Daenerys","c.parents.father","Aerys", "h.name","Targaryen", - "h.words","fireAndBlood"); + Assert.assertEquals(1, hits.length); + Map someMatch = ImmutableMap.of("c.name.firstname", (Object) "Daenerys", "c.parents.father", "Aerys", "h.name", "Targaryen", + "h.words", "fireAndBlood"); Assert.assertTrue(hitsContains(hits, someMatch)); } @Test - public void testLeftJoin() throws SQLFeatureNotSupportedException, IOException, SqlParseException { - String query = String.format("select c.name.firstname, f.name.firstname,f.name.lastname from %s/gotCharacters c " + - "LEFT JOIN %s/gotCharacters f " + - "on c.parents.father = f.name.firstname " - , TEST_INDEX,TEST_INDEX); - SearchHit[] hits = joinAndGetHits(query); - Assert.assertEquals(4,hits.length); - - Map oneMatch = new HashMap<>(); - oneMatch.put("c.name.firstname", "Daenerys"); - oneMatch.put("f.name.firstname",null); - oneMatch.put("f.name.lastname",null); - - Assert.assertTrue(hitsContains(hits, oneMatch)); - Map secondMatch = ImmutableMap.of("c.name.firstname", (Object)"Brandon", - "f.name.firstname","Eddard", "f.name.lastname","Stark"); - Assert.assertTrue(hitsContains(hits, secondMatch)); + public void testLeftJoinHASH() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + testLeftJoin(false); } @Test public void testLeftJoinNL() throws SQLFeatureNotSupportedException, IOException, SqlParseException { - String query = String.format("select /*! USE_NL*/ c.name.firstname, f.name.firstname,f.name.lastname from %s/gotCharacters c " + + testLeftJoin(true); + } + + private void testLeftJoin(boolean useNestedLoops) throws SqlParseException, SQLFeatureNotSupportedException, IOException { + String query = String.format("select c.name.firstname, f.name.firstname,f.name.lastname from %s/gotCharacters c " + "LEFT JOIN %s/gotCharacters f " + "on f.name.firstname = c.parents.father " , TEST_INDEX,TEST_INDEX); + if(useNestedLoops) query = query.replace("select","select /*! USE_NL*/ "); SearchHit[] hits = joinAndGetHits(query); - Assert.assertEquals(4,hits.length); + Assert.assertEquals(4, hits.length); Map oneMatch = new HashMap<>(); oneMatch.put("c.name.firstname", "Daenerys"); @@ -292,48 +216,152 @@ public void testLeftJoinNL() throws SQLFeatureNotSupportedException, IOException oneMatch.put("f.name.lastname",null); Assert.assertTrue(hitsContains(hits, oneMatch)); - Map secondMatch = ImmutableMap.of("c.name.firstname", (Object)"Brandon", - "f.name.firstname","Eddard", "f.name.lastname","Stark"); + Map secondMatch = ImmutableMap.of("c.name.firstname", (Object) "Brandon", + "f.name.firstname", "Eddard", "f.name.lastname", "Stark"); Assert.assertTrue(hitsContains(hits, secondMatch)); } @Test - public void hintLimits_firstLimitSecondNull() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + public void hintLimits_firstLimitSecondNullHASH() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + hintLimits_firstLimitSecondNull(false); + } + + @Test + public void hintLimits_firstLimitSecondNullNL() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + hintLimits_firstLimitSecondNull(true); + } + private void hintLimits_firstLimitSecondNull(boolean useNestedLoops) throws SqlParseException, SQLFeatureNotSupportedException, IOException { String query = String.format("select /*! JOIN_TABLES_LIMIT(2,null) */ c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + "JOIN %s/gotHouses h ",TEST_INDEX,TEST_INDEX); + if(useNestedLoops) query = query.replace("select","select /*! USE_NL*/ "); SearchHit[] hits = joinAndGetHits(query); - Assert.assertEquals(6,hits.length); + Assert.assertEquals(6, hits.length); } @Test - public void hintLimits_firstLimitSecondLimit() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + public void hintLimits_firstLimitSecondLimitHASH() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + hintLimits_firstLimitSecondLimit(false); + } + + @Test + public void hintLimits_firstLimitSecondLimitNL() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + hintLimits_firstLimitSecondLimit(true); + } + private void hintLimits_firstLimitSecondLimit(boolean useNestedLoops) throws SqlParseException, SQLFeatureNotSupportedException, IOException { String query = String.format("select /*! JOIN_TABLES_LIMIT(2,2) */ c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + "JOIN %s/gotHouses h ",TEST_INDEX,TEST_INDEX); + if(useNestedLoops) query = query.replace("select","select /*! USE_NL*/ "); + SearchHit[] hits = joinAndGetHits(query); + Assert.assertEquals(4, hits.length); + } + + @Test + public void hintLimits_firstLimitSecondLimitOnlyOneNL() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + hintLimits_firstLimitSecondLimitOnlyOne(true); + } + + @Test + public void hintLimits_firstLimitSecondLimitOnlyOneHASH() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + hintLimits_firstLimitSecondLimitOnlyOne(false); + } + + private void hintLimits_firstLimitSecondLimitOnlyOne(boolean useNestedLoops) throws SqlParseException, SQLFeatureNotSupportedException, IOException { + String query = String.format("select /*! JOIN_TABLES_LIMIT(3,1) */ c.name.firstname,c.parents.father , h.name,h.words from %s/gotHouses h " + + "JOIN %s/gotCharacters c ON c.name.lastname = h.name ",TEST_INDEX,TEST_INDEX); + if(useNestedLoops) query = query.replace("select","select /*! USE_NL*/ "); SearchHit[] hits = joinAndGetHits(query); - Assert.assertEquals(4,hits.length); + if(useNestedLoops) Assert.assertEquals(3, hits.length); + else Assert.assertEquals(1, hits.length); + } + + @Test + public void hintLimits_firstNullSecondLimitHASH() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + hintLimits_firstNullSecondLimit(false); } @Test - public void hintLimits_firstNullSecondLimit() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + public void hintLimits_firstNullSecondLimitNL() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + hintLimits_firstNullSecondLimit(true); + } + private void hintLimits_firstNullSecondLimit(boolean useNestedLoops) throws SqlParseException, SQLFeatureNotSupportedException, IOException { String query = String.format("select /*! JOIN_TABLES_LIMIT(null,2) */ c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + "JOIN %s/gotHouses h ",TEST_INDEX,TEST_INDEX); + if(useNestedLoops) query = query.replace("select","select /*! USE_NL*/ "); SearchHit[] hits = joinAndGetHits(query); - Assert.assertEquals(8,hits.length); + Assert.assertEquals(8, hits.length); + } + + @Test + public void testLeftJoinWithLimitHASH() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + testLeftJoinWithLimit(false); } @Test - public void testLeftJoinWithLimit() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + public void testLeftJoinWithLimitNL() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + testLeftJoinWithLimit(true); + } + + private void testLeftJoinWithLimit(boolean useNestedLoops) throws SqlParseException, SQLFeatureNotSupportedException, IOException { String query = String.format("select /*! JOIN_TABLES_LIMIT(3,null) */ c.name.firstname, f.name.firstname,f.name.lastname from %s/gotCharacters c " + "LEFT JOIN %s/gotCharacters f " + - "on c.parents.father = f.name.firstname " + "on f.name.firstname = c.parents.father" , TEST_INDEX,TEST_INDEX); + if(useNestedLoops) query = query.replace("select","select /*! USE_NL*/ "); SearchHit[] hits = joinAndGetHits(query); - Assert.assertEquals(3,hits.length); + Assert.assertEquals(3, hits.length); + } + @Test + public void hintMultiSearchCanRunFewTimesNL() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + String query = String.format("select /*! USE_NL*/ /*! NL_MULTISEARCH_SIZE(2)*/ c.name.firstname,c.parents.father , h.name,h.words from %s/gotCharacters c " + + "JOIN %s/gotHouses h ",TEST_INDEX,TEST_INDEX); + SearchHit[] hits = joinAndGetHits(query); + Assert.assertEquals(12, hits.length); } + + private String hashJoinRunAndExplain(String query) throws IOException, SqlParseException, SQLFeatureNotSupportedException { + SearchDao searchDao = MainTestSuite.getSearchDao(); + HashJoinElasticRequestBuilder explain = (HashJoinElasticRequestBuilder) searchDao.explain(query); + HashJoinElasticExecutor executor = new HashJoinElasticExecutor(searchDao.getClient(), explain); + executor.run(); + return explain.explain(); + } + + private SearchHit[] joinAndGetHits(String query) throws SqlParseException, SQLFeatureNotSupportedException, IOException { + SearchDao searchDao = MainTestSuite.getSearchDao(); + SqlElasticRequestBuilder explain = searchDao.explain(query); + ElasticJoinExecutor executor = ElasticJoinExecutor.createJoinExecutor(searchDao.getClient(),explain); + executor.run(); + return executor.getHits().getHits(); + } + + private boolean hitsContains(SearchHit[] hits, Map matchMap) { + for(SearchHit hit : hits){ + Map hitMap = hit.sourceAsMap(); + boolean matchedHit = true; + for(Map.Entry entry: hitMap.entrySet()){ + if(!matchMap.containsKey(entry.getKey())) { + matchedHit = false; + break; + } + if(!equalsWithNullCheck(matchMap.get(entry.getKey()), entry.getValue())){ + matchedHit = false; + break; + } + } + if(matchedHit) return true; + } + return false; + } + + private boolean equalsWithNullCheck(Object one, Object other) { + if(one == null) return other == null; + return one.equals(other); + } + } From c7245f213466c65d79ef8608fa9661a89c1a1f8f Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sat, 19 Sep 2015 17:28:36 +0300 Subject: [PATCH 069/559] delete , rebase --- .../sql/dialect/mysql/parser/MySqlLexer.java | 386 ------------------ 1 file changed, 386 deletions(-) delete mode 100644 src/main/java/org/durid/sql/dialect/mysql/parser/MySqlLexer.java diff --git a/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlLexer.java b/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlLexer.java deleted file mode 100644 index edf4dafa..00000000 --- a/src/main/java/org/durid/sql/dialect/mysql/parser/MySqlLexer.java +++ /dev/null @@ -1,386 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.dialect.mysql.parser; - -import static org.durid.sql.parser.CharTypes.isFirstIdentifierChar; -import static org.durid.sql.parser.CharTypes.isIdentifierChar; -import static org.durid.sql.parser.LayoutCharacters.EOI; -import static org.durid.sql.parser.Token.LITERAL_CHARS; - -import java.util.HashMap; -import java.util.Map; - -import org.durid.sql.parser.Keywords; -import org.durid.sql.parser.Lexer; -import org.durid.sql.parser.NotAllowCommentException; -import org.durid.sql.parser.SQLParseException; -import org.durid.sql.parser.Token; - -public class MySqlLexer extends Lexer { - - public final static Keywords DEFAULT_MYSQL_KEYWORDS; - - static { - Map map = new HashMap(); - - map.putAll(Keywords.DEFAULT_KEYWORDS.getKeywords()); - - map.put("DUAL", Token.DUAL); - map.put("FALSE", Token.FALSE); - map.put("IDENTIFIED", Token.IDENTIFIED); - map.put("IF", Token.IF); - map.put("KILL", Token.KILL); - - map.put("LIMIT", Token.LIMIT); - map.put("TRUE", Token.TRUE); - - DEFAULT_MYSQL_KEYWORDS = new Keywords(map); - } - - public MySqlLexer(char[] input, int inputLength, boolean skipComment){ - super(input, inputLength, skipComment); - super.keywods = DEFAULT_MYSQL_KEYWORDS; - } - - public MySqlLexer(String input){ - super(input); - super.keywods = DEFAULT_MYSQL_KEYWORDS; - } - - public void scanVariable() { - if (ch != '@' && ch != ':' && ch != '#' && ch != '$') { - throw new SQLParseException("illegal variable"); - } - - mark = pos; - bufPos = 1; - - if (charAt(pos + 1) == '@') { - ch = charAt(++pos); - bufPos++; - } - - if (charAt(pos + 1) == '`') { - ++pos; - ++bufPos; - char ch; - for (;;) { - ch = charAt(++pos); - - if (ch == '`') { - bufPos++; - ch = charAt(++pos); - break; - } else if (ch == EOI) { - throw new SQLParseException("illegal identifier"); - } - - bufPos++; - continue; - } - - this.ch = charAt(pos); - - stringVal = subString(mark, bufPos); - token = Token.VARIANT; - } else if (charAt(pos + 1) == '{') { - ++pos; - ++bufPos; - char ch; - for (;;) { - ch = charAt(++pos); - - if (ch == '}') { - bufPos++; - ch = charAt(++pos); - break; - } else if (ch == EOI) { - throw new SQLParseException("illegal identifier"); - } - - bufPos++; - continue; - } - - this.ch = charAt(pos); - - stringVal = subString(mark, bufPos); - token = Token.VARIANT; - } else { - for (;;) { - ch = charAt(++pos); - - if (!isIdentifierChar(ch)) { - break; - } - - bufPos++; - continue; - } - } - - this.ch = charAt(pos); - - stringVal = subString(mark, bufPos); - token = Token.VARIANT; - } - - public void scanIdentifier() { - final char first = ch; - - if (ch == '`') { - - mark = pos; - bufPos = 1; - char ch; - for (;;) { - ch = charAt(++pos); - - if (ch == '`') { - bufPos++; - ch = charAt(++pos); - break; - } else if (ch == EOI) { - throw new SQLParseException("illegal identifier"); - } - - bufPos++; - continue; - } - - this.ch = charAt(pos); - - stringVal = subString(mark, bufPos); - Token tok = keywods.getKeyword(stringVal); - if (tok != null) { - token = tok; - } else { - token = Token.IDENTIFIER; - } - } else { - - final boolean firstFlag = isFirstIdentifierChar(first); - if (!firstFlag) { - throw new SQLParseException("illegal identifier"); - } - - mark = pos; - bufPos = 1; - char ch; - for (;;) { - ch = charAt(++pos); - - if (!isIdentifierChar(ch)) { - break; - } - - bufPos++; - continue; - } - - this.ch = charAt(pos); - - stringVal = addSymbol(); - Token tok = keywods.getKeyword(stringVal); - if (tok != null) { - token = tok; - } else { - token = Token.IDENTIFIER; - } - } - } - - protected void scanString() { - mark = pos; - boolean hasSpecial = false; - - for (;;) { - if (isEOF()) { - lexError("unclosed.str.lit"); - return; - } - - ch = charAt(++pos); - - if (ch == '\\') { - scanChar(); - if (!hasSpecial) { - initBuff(bufPos); - arraycopy(mark + 1, buf, 0, bufPos); - hasSpecial = true; - } - - switch (ch) { - case '\0': - putChar('\0'); - break; - case '\'': - putChar('\''); - break; - case '"': - putChar('"'); - break; - case 'b': - putChar('\b'); - break; - case 'n': - putChar('\n'); - break; - case 'r': - putChar('\r'); - break; - case 't': - putChar('\t'); - break; - case '\\': - putChar('\\'); - break; - case 'Z': - putChar((char) 0x1A); // ctrl + Z - break; - default: - putChar(ch); - break; - } - scanChar(); - } - - if (ch == '\'') { - scanChar(); - if (ch != '\'') { - token = LITERAL_CHARS; - break; - } else { - if (!hasSpecial) { - initBuff(bufPos); - arraycopy(mark + 1, buf, 0, bufPos); - hasSpecial = true; - } - putChar('\''); - continue; - } - } - - if (!hasSpecial) { - bufPos++; - continue; - } - - if (bufPos == buf.length) { - putChar(ch); - } else { - buf[bufPos++] = ch; - } - } - - if (!hasSpecial) { - stringVal = subString(mark + 1, bufPos); - } else { - stringVal = new String(buf, 0, bufPos); - } - } - - public void scanComment() { - if (ch != '/' && ch != '-') { - throw new IllegalStateException(); - } - - mark = pos; - bufPos = 0; - scanChar(); - - // /*+ */ - if (ch == '*') { - scanChar(); - bufPos++; - - while (ch == ' ') { - scanChar(); - bufPos++; - } - - boolean isHint = false; - int startHintSp = bufPos + 1; - if (ch == '!') { - isHint = true; - scanChar(); - bufPos++; - } - - for (;;) { - if (ch == '*' && charAt(pos + 1) == '/') { - bufPos += 2; - scanChar(); - scanChar(); - break; - } - - scanChar(); - bufPos++; - } - - if (isHint) { - stringVal = subString(mark + startHintSp, (bufPos - startHintSp) - 1); - token = Token.HINT; - } else { - stringVal = subString(mark, bufPos); - token = Token.MULTI_LINE_COMMENT; - } - - if (token != Token.HINT && !isAllowComment()) { - throw new NotAllowCommentException(); - } - - return; - } - - if (!isAllowComment()) { - throw new NotAllowCommentException(); - } - - if (ch == '/' || ch == '-') { - scanChar(); - bufPos++; - - for (;;) { - if (ch == '\r') { - if (charAt(pos + 1) == '\n') { - bufPos += 2; - scanChar(); - break; - } - bufPos++; - break; - } else if (ch == EOI) { - break; - } - - if (ch == '\n') { - scanChar(); - bufPos++; - break; - } - - scanChar(); - bufPos++; - } - - stringVal = subString(mark + 1, bufPos); - token = Token.LINE_COMMENT; - return; - } - } -} From 0823c7f274e1d6dd4465f6e850693aaf172aa278 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sat, 19 Sep 2015 18:07:02 +0300 Subject: [PATCH 070/559] nested_loops explain implementation --- .../es4sql/query/join/JoinRequestBuilder.java | 2 +- .../NestedLoopsElasticRequestBuilder.java | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/nlpcn/es4sql/query/join/JoinRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/join/JoinRequestBuilder.java index 28792bab..ae7be00d 100644 --- a/src/main/java/org/nlpcn/es4sql/query/join/JoinRequestBuilder.java +++ b/src/main/java/org/nlpcn/es4sql/query/join/JoinRequestBuilder.java @@ -55,7 +55,7 @@ public String explain() { XContentBuilder secondBuilder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint(); secondTable.getRequestBuilder().internalBuilder().toXContent(secondBuilder, ToXContent.EMPTY_PARAMS); - String explained = String.format("HashJoin. first query:\n%s\n second query:\n%s", firstBuilder.string(), secondBuilder.string()); + String explained = String.format(" first query:\n%s\n second query:\n%s", firstBuilder.string(), secondBuilder.string()); return explained; } catch (IOException e) { diff --git a/src/main/java/org/nlpcn/es4sql/query/join/NestedLoopsElasticRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/join/NestedLoopsElasticRequestBuilder.java index 605b1945..99b9249f 100644 --- a/src/main/java/org/nlpcn/es4sql/query/join/NestedLoopsElasticRequestBuilder.java +++ b/src/main/java/org/nlpcn/es4sql/query/join/NestedLoopsElasticRequestBuilder.java @@ -1,7 +1,12 @@ package org.nlpcn.es4sql.query.join; +import org.elasticsearch.index.query.BoolFilterBuilder; import org.nlpcn.es4sql.domain.Condition; import org.nlpcn.es4sql.domain.Field; +import org.nlpcn.es4sql.domain.Where; +import org.nlpcn.es4sql.exception.SqlParseException; +import org.nlpcn.es4sql.query.maker.FilterMaker; +import org.nlpcn.es4sql.query.maker.Maker; import java.util.HashMap; import java.util.Map; @@ -17,6 +22,23 @@ public NestedLoopsElasticRequestBuilder() { multiSearchMaxSize = 100; } + @Override + public String explain() { + String baseExplain = super.explain(); + Where where = Where.newInstance(); + for(Condition c : t1FieldToCondition.values()){ + where.addWhere(c); + } + BoolFilterBuilder explan = null; + try { + explan = FilterMaker.explan(where); + } catch (SqlParseException e) { + } + String conditions = explan == null ? "Could not parse conditions" : explan.toString(); + String nestedExplain = "Nested Loops \n run first query , and for each result run second query with additional conditions :\n" +conditions +"\n"+ baseExplain; + return nestedExplain; + } + public Map getT1FieldToCondition() { return t1FieldToCondition; } From 2dbcce32c3feccc2b7be0a0a15f81694ae9dd085 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sat, 19 Sep 2015 18:22:10 +0300 Subject: [PATCH 071/559] java 6 support --- .../org/nlpcn/es4sql/parse/ElasticSqlExprParser.java | 11 +++++++---- src/main/java/org/nlpcn/es4sql/parse/SqlParser.java | 8 ++++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java b/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java index 4ea7839f..c4cdbb30 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java @@ -64,18 +64,21 @@ public SQLExpr primary() { } } else if(lexer.token() == Token.LBRACKET){ - List identifiers = new ArrayList<>(); + StringBuilder identifier = new StringBuilder(); lexer.nextToken(); + String prefix = ""; while(lexer.token()!=Token.RBRACKET){ if(lexer.token() != Token.IDENTIFIER && lexer.token()!=Token.INDEX){ throw new ParserException("All items between Brackets should be identifiers , got:" +lexer.token()); } - identifiers.add(lexer.stringVal()); + identifier.append(prefix); + identifier.append(lexer.stringVal()); + prefix = " "; lexer.nextToken(); } - String identifier = String.join(" ", identifiers); + accept(Token.RBRACKET); - return new SQLIdentifierExpr(identifier); + return new SQLIdentifierExpr(identifier.toString()); } return super.primary(); } diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index 44cd2199..f21cbca3 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -252,8 +252,12 @@ private String sameAliasWhere(Where where, String... aliases) throws SqlParseExc } if ( sameAliases.contains(null) ) return null; - if ( sameAliases.stream().distinct().count() != 1 ) return null; - return sameAliases.get(0); + String firstAlias = sameAliases.get(0); + //return null if more than one alias + for(String alias : sameAliases){ + if(!alias.equals(firstAlias)) return null; + } + return firstAlias; } private void findOrderBy(MySqlSelectQueryBlock query, Select select) throws SqlParseException { From da111a833b8417ab4d1e0ff89420e6f45dde6ce4 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Thu, 24 Sep 2015 14:16:40 +0300 Subject: [PATCH 072/559] join with geo_intersects between two indices --- .../org/nlpcn/es4sql/parse/SqlParser.java | 9 ++++----- .../org/nlpcn/es4sql/query/maker/Maker.java | 20 ++++++++++++++++--- src/test/java/org/nlpcn/es4sql/JoinTests.java | 10 ++++++++++ .../java/org/nlpcn/es4sql/MainTestSuite.java | 11 ++++++---- src/test/java/org/nlpcn/es4sql/QueryTest.java | 12 +++++------ .../java/org/nlpcn/es4sql/SqlParserTests.java | 14 +++++++++++++ src/test/resources/locations2.json | 4 ++++ 7 files changed, 62 insertions(+), 18 deletions(-) create mode 100644 src/test/resources/locations2.json diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index f21cbca3..168358f3 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -392,11 +392,10 @@ private List getConnectedFields(List conditions, String alias) fields.add(new Field(condition.getName().replaceFirst(prefix,""),null)); } else { - if(! (condition.getValue() instanceof SQLPropertyExpr)){ + if(! ((condition.getValue() instanceof SQLPropertyExpr)||(condition.getValue() instanceof String))){ throw new SqlParseException("conditions on join should be one side is firstTable second Other , condition was:" + condition.toString()); } - SQLPropertyExpr conditionValue = (SQLPropertyExpr) condition.getValue(); - String aliasDotValue = conditionValue.toString(); + String aliasDotValue = condition.getValue().toString(); int indexOfDot = aliasDotValue.indexOf("."); String owner = aliasDotValue.substring(0, indexOfDot); if(owner.equals(alias)) @@ -473,8 +472,8 @@ private void removeAliasPrefix(Where where, String alias) { private void addIfConditionRecursive(Where where, List conditions) throws SqlParseException { if(where instanceof Condition){ Condition cond = (Condition) where; - if( ! (cond.getValue() instanceof SQLPropertyExpr)){ - throw new SqlParseException("conditions on join should be one side is firstTable second Other , condition was:" + cond.toString()); + if( ! ((cond.getValue() instanceof SQLPropertyExpr)|| (cond.getValue() instanceof String))){ + throw new SqlParseException("conditions on join should be one side is secondTable OPEAR firstTable, condition was:" + cond.toString()); } conditions.add(cond); } diff --git a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java index 3e9c0466..f25b5534 100644 --- a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java +++ b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java @@ -206,7 +206,7 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar case GEO_INTERSECTS: String wkt = cond.getValue().toString(); try { - ShapeBuilder shapeBuilder = getShapeBuilderFromWkt(wkt); + ShapeBuilder shapeBuilder = getShapeBuilderFromString(wkt); if(isQuery) x = QueryBuilders.geoShapeQuery(cond.getName(), shapeBuilder); else @@ -266,11 +266,25 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar return x; } - private ShapeBuilder getShapeBuilderFromWkt(String wkt) throws IOException { - String json = WktToGeoJsonConverter.toGeoJson(trimApostrophes(wkt)); + private ShapeBuilder getShapeBuilderFromString(String str) throws IOException { + String json; + if(str.contains("{")) json = fixJsonFromElastic(str); + else json = WktToGeoJsonConverter.toGeoJson(trimApostrophes(str)); + return getShapeBuilderFromJson(json); } + /* + * elastic sends {coordinates=[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]], type=Polygon} + * proper form is {"coordinates":[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]], "type":"Polygon"} + * */ + private String fixJsonFromElastic(String elasticJson) { + String properJson = elasticJson.replaceAll("=",":"); + properJson = properJson.replaceAll("(type)(:)([a-zA-Z]+)","\"type\":\"$3\""); + properJson = properJson.replaceAll("coordinates","\"coordinates\""); + return properJson; + } + private ShapeBuilder getShapeBuilderFromJson(String json) throws IOException { XContentParser parser = null; parser = JsonXContent.jsonXContent.createParser(json); diff --git a/src/test/java/org/nlpcn/es4sql/JoinTests.java b/src/test/java/org/nlpcn/es4sql/JoinTests.java index d539e050..072d32d7 100644 --- a/src/test/java/org/nlpcn/es4sql/JoinTests.java +++ b/src/test/java/org/nlpcn/es4sql/JoinTests.java @@ -322,6 +322,16 @@ public void hintMultiSearchCanRunFewTimesNL() throws SQLFeatureNotSupportedExcep Assert.assertEquals(12, hits.length); } + @Test + public void joinWithGeoIntersectNL() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + String query = String.format("select p1.description,p2.description from %s/location p1 " + + "JOIN %s/location2 p2 " + + "ON GEO_INTERSECTS(p2.place,p1.place)",TEST_INDEX,TEST_INDEX); + SearchHit[] hits = joinAndGetHits(query); + Assert.assertEquals(2, hits.length); + Assert.assertEquals("squareRelated",hits[0].getSource().get("p2.description")); + Assert.assertEquals("squareRelated",hits[1].getSource().get("p2.description")); + } private String hashJoinRunAndExplain(String query) throws IOException, SqlParseException, SQLFeatureNotSupportedException { diff --git a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java index 588bbb6e..ec7ab116 100644 --- a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java +++ b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java @@ -58,9 +58,12 @@ public static void setUp() throws Exception { prepareOdbcIndex(); loadBulk("src/test/resources/odbc-date-formats.json"); - prepareSpatialIndex(); + prepareSpatialIndex("location"); loadBulk("src/test/resources/locations.json"); + prepareSpatialIndex("location2"); + loadBulk("src/test/resources/locations2.json"); + searchDao = new SearchDao(client); //refresh to make sure all the docs will return on queries @@ -125,9 +128,9 @@ public static void loadBulk(String jsonPath) throws Exception { throw new Exception(String.format("Failed during bulk load of file %s. failure message: %s", jsonPath, response.buildFailureMessage())); } } - public static void prepareSpatialIndex(){ + public static void prepareSpatialIndex(String type){ String dataMapping = "{\n" + - "\t\"location\" :{\n" + + "\t\""+type+"\" :{\n" + "\t\t\"properties\":{\n" + "\t\t\t\"place\":{\n" + "\t\t\t\t\"type\":\"geo_shape\",\n" + @@ -147,7 +150,7 @@ public static void prepareSpatialIndex(){ "\t}\n" + "}"; - client.admin().indices().preparePutMapping(TEST_INDEX).setType("location").setSource(dataMapping).execute().actionGet(); + client.admin().indices().preparePutMapping(TEST_INDEX).setType(type).setSource(dataMapping).execute().actionGet(); } public static void prepareOdbcIndex(){ String dataMapping = "{\n" + diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java index 9399ff75..74c14f98 100644 --- a/src/test/java/org/nlpcn/es4sql/QueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java @@ -460,7 +460,7 @@ public void testMultipartWhere3() throws IOException, SqlParseException, SQLFeat @Test public void filterPolygonTest() throws SQLFeatureNotSupportedException, SqlParseException, InterruptedException { - SearchHits results = query(String.format("SELECT * FROM %s WHERE GEO_INTERSECTS(place,'POLYGON ((102 2, 103 2, 103 3, 102 3, 102 2))')", TEST_INDEX)); + SearchHits results = query(String.format("SELECT * FROM %s/location WHERE GEO_INTERSECTS(place,'POLYGON ((102 2, 103 2, 103 3, 102 3, 102 2))')", TEST_INDEX)); org.junit.Assert.assertEquals(1,results.getTotalHits()); SearchHit result = results.getAt(0); Assert.assertEquals("bigSquare",result.getSource().get("description")); @@ -468,14 +468,14 @@ public void filterPolygonTest() throws SQLFeatureNotSupportedException, SqlParse @Test public void boundingBox() throws SQLFeatureNotSupportedException, SqlParseException, InterruptedException { - SearchHits results = query(String.format("SELECT * FROM %s WHERE GEO_BOUNDING_BOX(center,100.0,1.0,101,0.0)", TEST_INDEX)); + SearchHits results = query(String.format("SELECT * FROM %s/location WHERE GEO_BOUNDING_BOX(center,100.0,1.0,101,0.0)", TEST_INDEX)); org.junit.Assert.assertEquals(1,results.getTotalHits()); SearchHit result = results.getAt(0); Assert.assertEquals("square",result.getSource().get("description")); } @Test public void geoDistance() throws SQLFeatureNotSupportedException, SqlParseException, InterruptedException { - SearchHits results = query(String.format("SELECT * FROM %s WHERE GEO_DISTANCE(center,'1km',100.5,0.500001)", TEST_INDEX)); + SearchHits results = query(String.format("SELECT * FROM %s/location WHERE GEO_DISTANCE(center,'1km',100.5,0.500001)", TEST_INDEX)); org.junit.Assert.assertEquals(1,results.getTotalHits()); SearchHit result = results.getAt(0); Assert.assertEquals("square",result.getSource().get("description")); @@ -483,7 +483,7 @@ public void geoDistance() throws SQLFeatureNotSupportedException, SqlParseExcept @Test public void geoDistanceRange() throws SQLFeatureNotSupportedException, SqlParseException, InterruptedException { - SearchHits results = query(String.format("SELECT * FROM %s WHERE GEO_DISTANCE_RANGE(center,'1m','1km',100.5,0.50001)", TEST_INDEX)); + SearchHits results = query(String.format("SELECT * FROM %s/location WHERE GEO_DISTANCE_RANGE(center,'1m','1km',100.5,0.50001)", TEST_INDEX)); org.junit.Assert.assertEquals(1,results.getTotalHits()); SearchHit result = results.getAt(0); Assert.assertEquals("square",result.getSource().get("description")); @@ -491,7 +491,7 @@ public void geoDistanceRange() throws SQLFeatureNotSupportedException, SqlParseE @Test public void geoCell() throws SQLFeatureNotSupportedException, SqlParseException, InterruptedException { - SearchHits results = query(String.format("SELECT * FROM %s WHERE GEO_CELL(center,100.5,0.50001,7)", TEST_INDEX)); + SearchHits results = query(String.format("SELECT * FROM %s/location WHERE GEO_CELL(center,100.5,0.50001,7)", TEST_INDEX)); org.junit.Assert.assertEquals(1,results.getTotalHits()); SearchHit result = results.getAt(0); Assert.assertEquals("square",result.getSource().get("description")); @@ -499,7 +499,7 @@ public void geoCell() throws SQLFeatureNotSupportedException, SqlParseException, @Test public void geoPolygon() throws SQLFeatureNotSupportedException, SqlParseException, InterruptedException { - SearchHits results = query(String.format("SELECT * FROM %s WHERE GEO_POLYGON(center,100,0,100.5,2,101.0,0)", TEST_INDEX)); + SearchHits results = query(String.format("SELECT * FROM %s/location WHERE GEO_POLYGON(center,100,0,100.5,2,101.0,0)", TEST_INDEX)); org.junit.Assert.assertEquals(1,results.getTotalHits()); SearchHit result = results.getAt(0); Assert.assertEquals("square",result.getSource().get("description")); diff --git a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java index d83a56ae..16cd25fb 100644 --- a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java +++ b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java @@ -283,6 +283,20 @@ public void indexWithSpacesWithTypeWithinBrackets() throws SqlParseException { Assert.assertEquals("Test Index",from.getIndex()); Assert.assertEquals("type1",from.getType()); } + + + @Test + public void fieldWithSpacesWithinBrackets() throws SqlParseException { + String query = "SELECT insert_time FROM name/type1 WHERE [first name] = 'Name'"; + SQLExpr sqlExpr = queryToExpr(query); + Select select = parser.parseSelect((SQLQueryExpr) sqlExpr); + List where = select.getWhere().getWheres(); + Assert.assertEquals(1,where.size()); + Condition condition = (Condition) where.get(0); + Assert.assertEquals("first name",condition.getName()); + Assert.assertEquals("Name",condition.getValue()); + } + @Test public void twoIndices() throws SqlParseException { String query = "SELECT insert_time FROM index1/type1 , index2/type2 WHERE age > 3"; diff --git a/src/test/resources/locations2.json b/src/test/resources/locations2.json new file mode 100644 index 00000000..988d0add --- /dev/null +++ b/src/test/resources/locations2.json @@ -0,0 +1,4 @@ +{"index":{"_type": "location2", "_id":"1"}} +{"description":"square","place":{"type": "Polygon","coordinates": [[ [115.0, 112.0], [116.0, 112.0], [116.0, 113.0],[115.0, 113.0], [115.0, 112.0]]]},"center":{"lat": 0.5, "lon": 100.5 }} +{"index":{"_type": "location2", "_id":"2"}} +{"description":"squareRelated","place":{"type": "Polygon","coordinates": [[ [100.0, 0.0], [110.0, 0.0], [110.0, 10.0],[100.0, 10.0], [100.0, 0.0]]]},"center":{"lat": 5.0, "lon": 105.0 }} From e11a02fd9b08eefa1f5134f1539151cce0f6de37 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Thu, 24 Sep 2015 17:42:09 +0300 Subject: [PATCH 073/559] alias support on join returned fields --- .../plugin/nlpcn/ElasticJoinExecutor.java | 18 +++++--- src/test/java/org/nlpcn/es4sql/JoinTests.java | 46 ++++++++++++++++++- 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticJoinExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticJoinExecutor.java index 57478736..874613b0 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticJoinExecutor.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticJoinExecutor.java @@ -21,10 +21,7 @@ import org.nlpcn.es4sql.query.join.NestedLoopsElasticRequestBuilder; import java.io.IOException; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** * Created by Eliran on 15/9/2015. @@ -33,9 +30,11 @@ public abstract class ElasticJoinExecutor { protected SearchHits results ; protected MetaSearchResult metaResults; protected final int MAX_RESULTS_ON_ONE_FETCH = 10000; + private Set aliasesOnReturn; protected ElasticJoinExecutor() { metaResults = new MetaSearchResult(); + aliasesOnReturn = new HashSet<>(); } public void sendResponse(RestChannel channel){ @@ -120,7 +119,8 @@ protected void mergeSourceAndAddAliases(Map secondTableHitSource, protected Map mapWithAliases(Map source, String alias) { Map mapWithAliases = new HashMap<>(); for(Map.Entry fieldNameToValue : source.entrySet()) { - mapWithAliases.put(alias + "." + fieldNameToValue.getKey(), fieldNameToValue.getValue()); + if(!aliasesOnReturn.contains(fieldNameToValue.getKey())) + mapWithAliases.put(alias + "." + fieldNameToValue.getKey(), fieldNameToValue.getValue()); } return mapWithAliases; } @@ -130,7 +130,13 @@ protected void onlyReturnedFields(Map fieldsMap, List re for(Field field: required){ String name = field.getName(); - filteredMap.put(name, deepSearchInMap(fieldsMap, name)); + String returnName = name; + String alias = field.getAlias(); + if(alias !=null && alias !=""){ + returnName = alias; + aliasesOnReturn.add(alias); + } + filteredMap.put(returnName, deepSearchInMap(fieldsMap, name)); } fieldsMap.clear(); fieldsMap.putAll(filteredMap); diff --git a/src/test/java/org/nlpcn/es4sql/JoinTests.java b/src/test/java/org/nlpcn/es4sql/JoinTests.java index 072d32d7..3b1f1946 100644 --- a/src/test/java/org/nlpcn/es4sql/JoinTests.java +++ b/src/test/java/org/nlpcn/es4sql/JoinTests.java @@ -167,6 +167,50 @@ private void joinWithNestedFieldsOnReturn(boolean useNestedLoops) throws SqlPars Assert.assertTrue(hitsContains(hits, someMatch)); } + @Test + public void joinWithAllAliasOnReturnHASH() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + joinWithAllAliasOnReturn(false); + } + @Test + public void joinWithAllAliasOnReturnNL() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + joinWithAllAliasOnReturn(true); + } + + private void joinWithAllAliasOnReturn(boolean useNestedLoops) throws SqlParseException, SQLFeatureNotSupportedException, IOException { + String query = String.format("select c.name.firstname name,c.parents.father father, h.name house from %s/gotCharacters c " + + "JOIN %s/gotHouses h " + + "on h.name = c.house " + + "where c.name.firstname='Daenerys'", TEST_INDEX,TEST_INDEX); + if(useNestedLoops) query = query.replace("select","select /*! USE_NL*/ "); + SearchHit[] hits = joinAndGetHits(query); + Assert.assertEquals(1, hits.length); + + Map someMatch = ImmutableMap.of("name", (Object) "Daenerys", "father", "Aerys", "house", "Targaryen"); + Assert.assertTrue(hitsContains(hits, someMatch)); + } + + @Test + public void joinWithSomeAliasOnReturnHASH() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + joinWithSomeAliasOnReturn(false); + } + @Test + public void joinWithSomeAliasOnReturnNL() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + joinWithSomeAliasOnReturn(true); + } + + private void joinWithSomeAliasOnReturn(boolean useNestedLoops) throws SqlParseException, SQLFeatureNotSupportedException, IOException { + String query = String.format("select c.name.firstname ,c.parents.father father, h.name house from %s/gotCharacters c " + + "JOIN %s/gotHouses h " + + "on h.name = c.house " + + "where c.name.firstname='Daenerys'", TEST_INDEX,TEST_INDEX); + if(useNestedLoops) query = query.replace("select","select /*! USE_NL*/ "); + SearchHit[] hits = joinAndGetHits(query); + Assert.assertEquals(1, hits.length); + + Map someMatch = ImmutableMap.of("c.name.firstname", (Object) "Daenerys", "father", "Aerys", "house", "Targaryen"); + Assert.assertTrue(hitsContains(hits, someMatch)); + } + @Test public void joinWithNestedFieldsOnComparisonAndOnReturnHASH() throws SQLFeatureNotSupportedException, IOException, SqlParseException { joinWithNestedFieldsOnComparisonAndOnReturn(false); @@ -329,7 +373,7 @@ public void joinWithGeoIntersectNL() throws SQLFeatureNotSupportedException, IOE "ON GEO_INTERSECTS(p2.place,p1.place)",TEST_INDEX,TEST_INDEX); SearchHit[] hits = joinAndGetHits(query); Assert.assertEquals(2, hits.length); - Assert.assertEquals("squareRelated",hits[0].getSource().get("p2.description")); + Assert.assertEquals("squareRelated", hits[0].getSource().get("p2.description")); Assert.assertEquals("squareRelated",hits[1].getSource().get("p2.description")); } From f2c27460cf9527e52c8ed55b7f60ec9cc7e32d0a Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Thu, 24 Sep 2015 23:10:02 +0300 Subject: [PATCH 074/559] join bug fixes - select with star and aliases --- .../plugin/nlpcn/ElasticJoinExecutor.java | 14 +++++++++++-- .../plugin/nlpcn/HashJoinElasticExecutor.java | 1 + .../nlpcn/NestedLoopsElasticExecutor.java | 1 + src/test/java/org/nlpcn/es4sql/JoinTests.java | 20 ++++++++++++++++++- 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticJoinExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticJoinExecutor.java index 874613b0..cfd9d680 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticJoinExecutor.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticJoinExecutor.java @@ -18,6 +18,7 @@ import org.nlpcn.es4sql.exception.SqlParseException; import org.nlpcn.es4sql.query.SqlElasticRequestBuilder; import org.nlpcn.es4sql.query.join.HashJoinElasticRequestBuilder; +import org.nlpcn.es4sql.query.join.JoinRequestBuilder; import org.nlpcn.es4sql.query.join.NestedLoopsElasticRequestBuilder; import java.io.IOException; @@ -31,10 +32,15 @@ public abstract class ElasticJoinExecutor { protected MetaSearchResult metaResults; protected final int MAX_RESULTS_ON_ONE_FETCH = 10000; private Set aliasesOnReturn; + private boolean allFieldsReturn; - protected ElasticJoinExecutor() { + protected ElasticJoinExecutor(JoinRequestBuilder requestBuilder) { metaResults = new MetaSearchResult(); aliasesOnReturn = new HashSet<>(); + List firstTableReturnedField = requestBuilder.getFirstTable().getReturnedFields(); + List secondTableReturnedField = requestBuilder.getSecondTable().getReturnedFields(); + allFieldsReturn = (firstTableReturnedField == null || firstTableReturnedField.size() == 0) + && (secondTableReturnedField == null || secondTableReturnedField.size() == 0); } public void sendResponse(RestChannel channel){ @@ -121,13 +127,17 @@ protected Map mapWithAliases(Map source, String a for(Map.Entry fieldNameToValue : source.entrySet()) { if(!aliasesOnReturn.contains(fieldNameToValue.getKey())) mapWithAliases.put(alias + "." + fieldNameToValue.getKey(), fieldNameToValue.getValue()); + else mapWithAliases.put(fieldNameToValue.getKey(),fieldNameToValue.getValue()); } return mapWithAliases; } protected void onlyReturnedFields(Map fieldsMap, List required) { HashMap filteredMap = new HashMap<>(); - + if(allFieldsReturn) { + filteredMap.putAll(fieldsMap); + return; + } for(Field field: required){ String name = field.getName(); String returnName = name; diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java index fa3c2bdb..f43ff197 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/HashJoinElasticExecutor.java @@ -46,6 +46,7 @@ public class HashJoinElasticExecutor extends ElasticJoinExecutor { private final int MAX_RESULTS_FOR_FIRST_TABLE = 100000; public HashJoinElasticExecutor(Client client,HashJoinElasticRequestBuilder requestBuilder) { + super(requestBuilder); this.client = client; this.requestBuilder = requestBuilder; this.useQueryTermsFilterOptimization = requestBuilder.isUseTermFiltersOptimization(); diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/NestedLoopsElasticExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/NestedLoopsElasticExecutor.java index 54947c24..1ddfbf8f 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/NestedLoopsElasticExecutor.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/NestedLoopsElasticExecutor.java @@ -32,6 +32,7 @@ public class NestedLoopsElasticExecutor extends ElasticJoinExecutor { private final Client client; public NestedLoopsElasticExecutor(Client client, NestedLoopsElasticRequestBuilder nestedLoops) { + super(nestedLoops); this.client = client; this.nestedLoopsRequest = nestedLoops; } diff --git a/src/test/java/org/nlpcn/es4sql/JoinTests.java b/src/test/java/org/nlpcn/es4sql/JoinTests.java index 3b1f1946..dc4c65a5 100644 --- a/src/test/java/org/nlpcn/es4sql/JoinTests.java +++ b/src/test/java/org/nlpcn/es4sql/JoinTests.java @@ -87,6 +87,25 @@ private void joinWithNoWhereButWithCondition(boolean useNestedLoops) throws SqlP Assert.assertTrue(hitsContains(hits, someMatch)); } + @Test + public void joinWithStarASH() throws SQLFeatureNotSupportedException, IOException, SqlParseException { + joinWithStar(false); + } + + private void joinWithStar(boolean useNestedLoops) throws SqlParseException, SQLFeatureNotSupportedException, IOException { + String query = String.format("select * from %s/gotCharacters c " + + "JOIN %s/gotHouses h " + + "on h.name = c.house ",TEST_INDEX,TEST_INDEX); + if(useNestedLoops) query = query.replace("select","select /*! USE_NL*/ "); + SearchHit[] hits = joinAndGetHits(query); + Assert.assertEquals(4, hits.length); + String house = hits[0].sourceAsMap().get("c.house").toString(); + boolean someHouse = house.equals("Targaryen") || house.equals( "Stark") || house.equals("Lannister"); + Assert.assertTrue(someHouse );; + String houseName = hits[0].sourceAsMap().get("h.name").toString(); + Assert.assertEquals(house,houseName); + } + @Test public void joinNoConditionButWithWhereHASH() throws SQLFeatureNotSupportedException, IOException, SqlParseException { joinNoConditionButWithWhere(false); @@ -417,5 +436,4 @@ private boolean equalsWithNullCheck(Object one, Object other) { if(one == null) return other == null; return one.equals(other); } - } From 89ca672288f45e10851769104e700ee3f1201d41 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Thu, 24 Sep 2015 23:13:00 +0300 Subject: [PATCH 075/559] site - add download plugin (can download csv and name file now) fix csv on object return field (stringify) --- src/_site/controllers.js | 8 +- src/_site/index.html | 2 + src/_site/vendor/download/download.js | 137 ++++++++++++++++++++++ src/_site/vendor/download/download.min.js | 2 + 4 files changed, 146 insertions(+), 3 deletions(-) create mode 100644 src/_site/vendor/download/download.js create mode 100644 src/_site/vendor/download/download.min.js diff --git a/src/_site/controllers.js b/src/_site/controllers.js index 0643bf0c..7e325b89 100644 --- a/src/_site/controllers.js +++ b/src/_site/controllers.js @@ -1,3 +1,4 @@ + var elasticsearchSqlApp = angular.module('elasticsearchSqlApp', ["ngAnimate", "ngSanitize"]); elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) { @@ -88,7 +89,8 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) data += map2csvStr(columns,rows[i],',') ; } - window.location='data:text/csv;charset=utf8,' + encodeURIComponent(data); + var plain = 'data:text/csv;charset=utf8,' + encodeURIComponent(data); + download(plain, "query_result.csv", "text/plain"); return true; } @@ -109,10 +111,10 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) } function map2csvStr(columns,arr,op){ - var data = arr[columns[0]]; + var data = JSON.stringify(arr[columns[0]]); for(var i=1; iResults + + diff --git a/src/_site/vendor/download/download.js b/src/_site/vendor/download/download.js new file mode 100644 index 00000000..751a0335 --- /dev/null +++ b/src/_site/vendor/download/download.js @@ -0,0 +1,137 @@ +//download.js v4.0, by dandavis; 2008-2015. [CCBY2] see http://danml.com/download.html for tests/usage +// v1 landed a FF+Chrome compat way of downloading strings to local un-named files, upgraded to use a hidden frame and optional mime +// v2 added named files via a[download], msSaveBlob, IE (10+) support, and window.URL support for larger+faster saves than dataURLs +// v3 added dataURL and Blob Input, bind-toggle arity, and legacy dataURL fallback was improved with force-download mime and base64 support. 3.1 improved safari handling. +// v4 adds AMD/UMD, commonJS, and plain browser support +// https://github.com/rndme/download + +(function (root, factory) { + if (typeof define === 'function' && define.amd) { + // AMD. Register as an anonymous module. + define([], factory); + } else if (typeof exports === 'object') { + // Node. Does not work with strict CommonJS, but + // only CommonJS-like environments that support module.exports, + // like Node. + module.exports = factory(); + } else { + // Browser globals (root is window) + root.download = factory(); + } +}(this, function () { + + return function download(data, strFileName, strMimeType) { + + var self = window, // this script is only for browsers anyway... + u = "application/octet-stream", // this default mime also triggers iframe downloads + m = strMimeType || u, + x = data, + D = document, + a = D.createElement("a"), + z = function(a){return String(a);}, + B = (self.Blob || self.MozBlob || self.WebKitBlob || z); + B=B.call ? B.bind(self) : Blob ; + var fn = strFileName || "download", + blob, + fr; + + + if(String(this)==="true"){ //reverse arguments, allowing download.bind(true, "text/xml", "export.xml") to act as a callback + x=[x, m]; + m=x[0]; + x=x[1]; + } + + + + + //go ahead and download dataURLs right away + if(String(x).match(/^data\:[\w+\-]+\/[\w+\-]+[,;]/)){ + return navigator.msSaveBlob ? // IE10 can't do a[download], only Blobs: + navigator.msSaveBlob(d2b(x), fn) : + saver(x) ; // everyone else can save dataURLs un-processed + }//end if dataURL passed? + + blob = x instanceof B ? + x : + new B([x], {type: m}) ; + + + function d2b(u) { + var p= u.split(/[:;,]/), + t= p[1], + dec= p[2] == "base64" ? atob : decodeURIComponent, + bin= dec(p.pop()), + mx= bin.length, + i= 0, + uia= new Uint8Array(mx); + + for(i;i Date: Sun, 27 Sep 2015 17:48:35 +0300 Subject: [PATCH 076/559] scroll support with USE_SCROLL hint, also add site support --- src/_site/controllers.js | 58 ++++++++++++++++++- src/_site/index.html | 3 + src/_site/query.js | 11 ++++ src/_site/style.css | 6 ++ .../java/org/nlpcn/es4sql/domain/Select.java | 9 ++- .../es4sql/domain/hints/HintFactory.java | 11 ++++ .../nlpcn/es4sql/domain/hints/HintType.java | 3 +- .../org/nlpcn/es4sql/parse/SqlParser.java | 2 + .../es4sql/query/DefaultQueryAction.java | 29 +++++++++- src/test/java/org/nlpcn/es4sql/QueryTest.java | 26 +++++++++ 10 files changed, 153 insertions(+), 5 deletions(-) diff --git a/src/_site/controllers.js b/src/_site/controllers.js index 7e325b89..817a7a10 100644 --- a/src/_site/controllers.js +++ b/src/_site/controllers.js @@ -8,9 +8,61 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) $scope.resultsRows = []; $scope.searchLoading = false; $scope.explainLoading = false; + $scope.nextLoading = false; $scope.resultExplan = false; - + $scope.scrollId = null; + $scope.gotNext = false; + + $scope.nextSearch = function(){ + $scope.error = ""; + $scope.nextLoading = true; + $scope.$apply(); + + + if($scope.scrollId == null || $scope.scrollId == "" ){ + $scope.error = "tryed scrolling with empty scrollId"; + return; + } + $http.get($scope.url + "_search/scroll?scroll=1m&scroll_id=" + $scope.scrollId) + .success(function(data, status, headers, config) { + var handler = ResultHandlerFactory.create(data); + var body = handler.getBody() + + if(body.length ==null || body.length == 0){ + $scope.gotNext=false; + } + else + { + $scope.scrollId = handler.getScrollId(); + } + + if($scope.resultsRows.length > 0){ + $scope.resultsRows = $scope.resultsRows.concat(handler.getBody()); + } + else { + $scope.resultsColumns = handler.getHead(); + $scope.resultsRows = handler.getBody(); + + } + + + }) + .error(function(data, status, headers, config) { + if(data == "") { + $scope.error = "Error occured! response is not avalible."; + } + else { + $scope.error = JSON.stringify(data); + $scope.scrollId = null; + } + }) + .finally(function() { + $scope.nextLoading = false; + $scope.$apply() + }); + + } $scope.search = function() { // Reset results and error box @@ -28,6 +80,10 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) $http.post($scope.url + "_sql", query) .success(function(data, status, headers, config) { var handler = ResultHandlerFactory.create(data); + if(handler.isScroll){ + $scope.gotNext=true; + $scope.scrollId = handler.getScrollId(); + } $scope.resultsColumns = handler.getHead(); $scope.resultsRows = handler.getBody(); diff --git a/src/_site/index.html b/src/_site/index.html index 728833c8..60e81720 100644 --- a/src/_site/index.html +++ b/src/_site/index.html @@ -105,6 +105,9 @@

SQL Query

+ + diff --git a/src/_site/query.js b/src/_site/query.js index 94505500..92bf01f6 100644 --- a/src/_site/query.js +++ b/src/_site/query.js @@ -42,8 +42,19 @@ var DefaultQueryResultHandler = function(data) { this.data = data this.head = createScheme() + this.scrollId = data["_scroll_id"] + this.isScroll = this.scrollId!=null && this.scrollId!=""; }; +DefaultQueryResultHandler.prototype.isScroll = function() { + return this.isScroll; +}; + +DefaultQueryResultHandler.prototype.getScrollId = function() { + return this.scrollId; +}; + + DefaultQueryResultHandler.prototype.getHead = function() { return this.head }; diff --git a/src/_site/style.css b/src/_site/style.css index 83d333af..aa370e0c 100644 --- a/src/_site/style.css +++ b/src/_site/style.css @@ -123,6 +123,12 @@ body { } +.next-button { + float:left; + margin-top: 10px; +} + + .keyboardSection { margin-top:35px; } diff --git a/src/main/java/org/nlpcn/es4sql/domain/Select.java b/src/main/java/org/nlpcn/es4sql/domain/Select.java index 43f3889d..ee354da0 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/Select.java +++ b/src/main/java/org/nlpcn/es4sql/domain/Select.java @@ -1,5 +1,7 @@ package org.nlpcn.es4sql.domain; +import org.nlpcn.es4sql.domain.hints.Hint; + import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -13,7 +15,7 @@ public class Select extends Query { // Using this functions, will cause query to execute as aggregation. private final List aggsFunctions = Arrays.asList("SUM", "MAX", "MIN", "AVG", "TOPHITS", "COUNT", "STATS"); - + private List hints = new ArrayList<>(); private List fields = new ArrayList<>(); private List> groupBys = new ArrayList<>(); private List orderBys = new ArrayList<>(); @@ -86,5 +88,10 @@ public void addField(Field field) { fields.add(field); } + public List getHints() { + return hints; + } + + } diff --git a/src/main/java/org/nlpcn/es4sql/domain/hints/HintFactory.java b/src/main/java/org/nlpcn/es4sql/domain/hints/HintFactory.java index e63cca42..42026ef0 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/hints/HintFactory.java +++ b/src/main/java/org/nlpcn/es4sql/domain/hints/HintFactory.java @@ -35,11 +35,22 @@ public static Hint getHintFromString(String hintAsString){ int multiSearchSize = Integer.parseInt(number[0]); return new Hint(HintType.NL_MULTISEARCH_SIZE,new Object[]{multiSearchSize}); } + if(hintAsString.startsWith("! USE_SCROLL")){ + String[] scrollParams = getParamsFromHint(hintAsString,"! USE_SCROLL"); + int docsPerFetch = 10000; + int timeout = 60000; + if(scrollParams != null && scrollParams.length ==2) { + docsPerFetch = Integer.parseInt(scrollParams[0]); + timeout = Integer.parseInt(scrollParams[1]); + } + return new Hint(HintType.USE_SCROLL, new Object[]{docsPerFetch,timeout}); + } return null; } private static String[] getParamsFromHint(String hint, String prefix) { + if(!hint.contains("(")) return null; String onlyParams = hint.replace(prefix, "").replaceAll("\\s*\\(\\s*","").replaceAll("\\s*\\,\\s*", ",").replaceAll("\\s*\\)\\s*", ""); return onlyParams.split(","); } diff --git a/src/main/java/org/nlpcn/es4sql/domain/hints/HintType.java b/src/main/java/org/nlpcn/es4sql/domain/hints/HintType.java index 8891f9e5..d8513fe7 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/hints/HintType.java +++ b/src/main/java/org/nlpcn/es4sql/domain/hints/HintType.java @@ -11,6 +11,7 @@ public enum HintType HASH_WITH_TERMS_FILTER, JOIN_LIMIT, USE_NESTED_LOOPS, - NL_MULTISEARCH_SIZE; + NL_MULTISEARCH_SIZE, + USE_SCROLL; } diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index 168358f3..55139e50 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -39,6 +39,8 @@ public Select parseSelect(SQLQueryExpr mySqlExpr) throws SqlParseException { select.setWhere(findWhere(query.getWhere())); + select.getHints().addAll(parseHints(query.getHints())); + findLimit(query.getLimit(), select); findOrderBy(query, select); diff --git a/src/main/java/org/nlpcn/es4sql/query/DefaultQueryAction.java b/src/main/java/org/nlpcn/es4sql/query/DefaultQueryAction.java index 51393f87..58c20e14 100644 --- a/src/main/java/org/nlpcn/es4sql/query/DefaultQueryAction.java +++ b/src/main/java/org/nlpcn/es4sql/query/DefaultQueryAction.java @@ -5,6 +5,7 @@ import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchType; import org.elasticsearch.client.Client; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.index.query.BoolFilterBuilder; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; @@ -13,6 +14,8 @@ import org.nlpcn.es4sql.domain.Order; import org.nlpcn.es4sql.domain.Select; import org.nlpcn.es4sql.domain.Where; +import org.nlpcn.es4sql.domain.hints.Hint; +import org.nlpcn.es4sql.domain.hints.HintType; import org.nlpcn.es4sql.exception.SqlParseException; import org.nlpcn.es4sql.query.maker.FilterMaker; import org.nlpcn.es4sql.query.maker.QueryMaker; @@ -42,12 +45,34 @@ public SqlElasticSearchRequestBuilder explain() throws SqlParseException { setLimit(select.getOffset(), select.getRowCount()); // set SearchType. - request.setSearchType(SearchType.DFS_QUERY_THEN_FETCH); + boolean usedScroll = useScrollIfNeeded(); + if(!usedScroll){ + request.setSearchType(SearchType.DFS_QUERY_THEN_FETCH); + } + SqlElasticSearchRequestBuilder sqlElasticRequestBuilder = new SqlElasticSearchRequestBuilder(request); return sqlElasticRequestBuilder; } - /** + private boolean useScrollIfNeeded() { + Hint scrollHint = null; + for(Hint hint: select.getHints()){ + if(hint.getType() == HintType.USE_SCROLL){ + scrollHint = hint; + break; + } + } + if(scrollHint!=null) { + int scrollSize = (Integer) scrollHint.getParams()[0]; + int timeoutInMilli = (Integer) scrollHint.getParams()[1]; + request.setSearchType(SearchType.SCAN) + .setScroll(new TimeValue(timeoutInMilli)) + .setSize(scrollSize); + } + return scrollHint !=null ; + } + + /** * Set indices and types to the search request. */ private void setIndicesAndTypes() { diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java index 74c14f98..78ddc89a 100644 --- a/src/test/java/org/nlpcn/es4sql/QueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java @@ -1,5 +1,6 @@ package org.nlpcn.es4sql; +import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.common.joda.time.DateTime; @@ -558,9 +559,34 @@ public void isNotNullTest() throws IOException, SqlParseException, SQLFeatureNot Assert.assertEquals(1, response.getTotalHits()); } + + @Test + public void useScrollNoParams() throws IOException, SqlParseException, SQLFeatureNotSupportedException{ + SearchResponse response = getSearchResponse(String.format("SELECT /*! USE_SCROLL*/ age,gender,firstname,balance FROM %s/account LIMIT 2000", TEST_INDEX, TEST_INDEX)); + Assert.assertNotNull(response.getScrollId()); + SearchHits hits = response.getHits(); + Assert.assertEquals(0,hits.getHits().length); + Assert.assertEquals(1000,hits.getTotalHits()); + } + + @Test + public void useScrollWithParams() throws IOException, SqlParseException, SQLFeatureNotSupportedException{ + SearchResponse response = getSearchResponse(String.format("SELECT /*! USE_SCROLL(10,5000)*/ age,gender,firstname,balance FROM %s/account ", TEST_INDEX, TEST_INDEX)); + Assert.assertNotNull(response.getScrollId()); + SearchHits hits = response.getHits(); + Assert.assertEquals(0,hits.getHits().length); + Assert.assertEquals(1000,hits.getTotalHits()); + } + private SearchHits query(String query) throws SqlParseException, SQLFeatureNotSupportedException, SQLFeatureNotSupportedException { SearchDao searchDao = MainTestSuite.getSearchDao(); SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) searchDao.explain(query); return ((SearchResponse)select.get()).getHits(); } + + private SearchResponse getSearchResponse(String query) throws SqlParseException, SQLFeatureNotSupportedException, SQLFeatureNotSupportedException { + SearchDao searchDao = MainTestSuite.getSearchDao(); + SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) searchDao.explain(query); + return ((SearchResponse)select.get()); + } } From d8f1e0ef5ed2dbe616908926c7c736a61435b616 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Mon, 28 Sep 2015 10:54:27 +0300 Subject: [PATCH 077/559] new release , new version to support limited join and more features --- README.md | 3 ++- pom.xml | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 50ede710..6be4b4af 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ Elasticsearch-SQL Query elasticsearch using familiar SQL syntax. You can also use ES functions in SQL. +**Check out our [wiki!](https://github.com/NLPchina/elasticsearch-sql/wiki)** ## Web frontend overview @@ -17,7 +18,7 @@ Install as plugin: ### Elasticsearch 1.6.X ```` -./bin/plugin -u https://github.com/NLPchina/elasticsearch-sql/releases/download/1.3.5/elasticsearch-sql-1.3.5.zip --install sql +./bin/plugin -u https://github.com/NLPchina/elasticsearch-sql/releases/download/1.4/elasticsearch-sql-1.4.zip --install sql ```` After doing this, you need to restart the Elasticsearch server. Otherwise you may get errors like `Invalid index name [sql], must not start with '']; ","status":400}`. diff --git a/pom.xml b/pom.xml index 763ce0be..d2436e46 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.nlpcn elasticsearch-sql - 1.3.5 + 1.4 jar Query elasticsearch using SQL elasticsearch-sql From 69f2569be316427f0231c97a73507c88575b091c Mon Sep 17 00:00:00 2001 From: Caleb Ott Date: Mon, 28 Sep 2015 09:07:06 -0500 Subject: [PATCH 078/559] Finished merging and added guava as a built jar dependency --- pom.xml | 7 + .../org/durid/sql/parser/SQLExprParser.java | 1274 ----------------- 2 files changed, 7 insertions(+), 1274 deletions(-) delete mode 100644 src/main/java/org/durid/sql/parser/SQLExprParser.java diff --git a/pom.xml b/pom.xml index d2436e46..6ff1bc0c 100644 --- a/pom.xml +++ b/pom.xml @@ -151,6 +151,13 @@ ${project.build.directory} druid.jar + + com.google.guava + guava + false + ${project.build.directory} + guava.jar + ${project.build.directory} false diff --git a/src/main/java/org/durid/sql/parser/SQLExprParser.java b/src/main/java/org/durid/sql/parser/SQLExprParser.java deleted file mode 100644 index 01e1006b..00000000 --- a/src/main/java/org/durid/sql/parser/SQLExprParser.java +++ /dev/null @@ -1,1274 +0,0 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.durid.sql.parser; - -import java.math.BigInteger; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -import org.durid.sql.ast.SQLCommentHint; -import org.durid.sql.ast.SQLDataType; -import org.durid.sql.ast.SQLDataTypeImpl; -import org.durid.sql.ast.SQLExpr; -import org.durid.sql.ast.SQLName; -import org.durid.sql.ast.SQLOrderBy; -import org.durid.sql.ast.SQLOrderingSpecification; -import org.durid.sql.ast.SQLOver; -import org.durid.sql.ast.expr.*; -import org.durid.sql.ast.statement.NotNullConstraint; -import org.durid.sql.ast.statement.SQLAssignItem; -import org.durid.sql.ast.statement.SQLCharactorDataType; -import org.durid.sql.ast.statement.SQLColumnDefinition; -import org.durid.sql.ast.statement.SQLPrimaryKey; -import org.durid.sql.ast.statement.SQLSelect; -import org.durid.sql.ast.statement.SQLSelectOrderByItem; - -public class SQLExprParser extends SQLParser { - - public SQLExprParser(String sql){ - super(sql); - } - - public SQLExprParser(Lexer lexer){ - super(lexer); - } - - public SQLExpr expr() { - if (lexer.token() == Token.STAR) { - lexer.nextToken(); - - return new SQLAllColumnExpr(); - } - - SQLExpr expr = primary(); - - if (lexer.token() == Token.COMMA) { - return expr; - } - - return exprRest(expr); - } - - public SQLExpr exprRest(SQLExpr expr) { - expr = bitXorRest(expr); - expr = multiplicativeRest(expr); - expr = additiveRest(expr); - expr = shiftRest(expr); - expr = bitAndRest(expr); - expr = bitOrRest(expr); - expr = inRest(expr); - expr = relationalRest(expr); - expr = equalityRest(expr); - expr = andRest(expr); - expr = orRest(expr); - - return expr; - } - - public final SQLExpr bitXor() { - SQLExpr expr = primary(); - return bitXorRest(expr); - } - - public SQLExpr bitXorRest(SQLExpr expr) { - if (lexer.token() == Token.CARET) { - lexer.nextToken(); - SQLExpr rightExp = primary(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BitwiseXor, rightExp); - expr = bitXorRest(expr); - } - - return expr; - } - - public final SQLExpr multiplicative() { - SQLExpr expr = primary(); - return multiplicativeRest(expr); - } - - public SQLExpr multiplicativeRest(SQLExpr expr) { - if (lexer.token() == Token.STAR) { - lexer.nextToken(); - SQLExpr rightExp = bitXor(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Multiply, rightExp); - expr = multiplicativeRest(expr); - } else if (lexer.token() == Token.SLASH) { - lexer.nextToken(); - SQLExpr rightExp = bitXor(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Divide, rightExp); - expr = multiplicativeRest(expr); - } else if (lexer.token() == Token.PERCENT) { - lexer.nextToken(); - SQLExpr rightExp = bitXor(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Modulus, rightExp); - expr = multiplicativeRest(expr); - } - return expr; - } - - public SQLExpr primary() { - SQLExpr sqlExpr = null; - - final Token tok = lexer.token(); - - switch (tok) { - case LPAREN: - lexer.nextToken(); - sqlExpr = expr(); - if (lexer.token() == Token.COMMA) { - SQLListExpr listExpr = new SQLListExpr(); - listExpr.getItems().add(sqlExpr); - do { - lexer.nextToken(); - listExpr.getItems().add(expr()); - } while (lexer.token() == Token.COMMA); - - sqlExpr = listExpr; - } - accept(Token.RPAREN); - if (sqlExpr instanceof SQLIdentifierExpr) { - ((SQLIdentifierExpr) sqlExpr).setWrappedInParens(true); - } - break; - case LBRACE: - lexer.nextToken(); - boolean foundRBrace = false; - if(lexer.stringVal().equals(Token.TS.name)){ - String current = lexer.stringVal(); - do { - if(current.equals(tok.RBRACE.name())){ - foundRBrace = true; - break; - } - lexer.nextToken(); - current = lexer.token().name(); - }while(!foundRBrace && !current.trim().equals("")); - - if(foundRBrace){ - SQLOdbcExpr sdle = new SQLOdbcExpr(lexer.stringVal()); - sqlExpr = sdle; - accept(Token.RBRACE); - }else{ - throw new ParserException("Error. Unable to find closing RBRACE"); - } - }else{ - throw new ParserException("Error. Unable to parse ODBC Literal Timestamp"); - } - break; - case INSERT: - lexer.nextToken(); - if (lexer.token() != Token.LPAREN) { - throw new ParserException("syntax error"); - } - sqlExpr = new SQLIdentifierExpr("INSERT"); - break; - case IDENTIFIER: - sqlExpr = new SQLIdentifierExpr(lexer.stringVal()); - lexer.nextToken(); - break; - case NEW: - throw new ParserException("TODO"); - case LITERAL_INT: - sqlExpr = new SQLIntegerExpr(lexer.integerValue()); - lexer.nextToken(); - break; - case LITERAL_FLOAT: - sqlExpr = new SQLNumberExpr(lexer.decimalValue()); - lexer.nextToken(); - break; - case LITERAL_CHARS: - sqlExpr = new SQLCharExpr(lexer.stringVal()); - lexer.nextToken(); - break; - case LITERAL_NCHARS: - sqlExpr = new SQLNCharExpr(lexer.stringVal()); - lexer.nextToken(); - break; - case VARIANT: - SQLVariantRefExpr varRefExpr = new SQLVariantRefExpr(lexer.stringVal()); - lexer.nextToken(); - if (varRefExpr.getName().equals("@") && lexer.token() == Token.LITERAL_CHARS) { - varRefExpr.setName("@'" + lexer.stringVal() + "'"); - lexer.nextToken(); - } else if (varRefExpr.getName().equals("@@") && lexer.token() == Token.LITERAL_CHARS) { - varRefExpr.setName("@@'" + lexer.stringVal() + "'"); - lexer.nextToken(); - } - sqlExpr = varRefExpr; - break; - case DEFAULT: - sqlExpr = new SQLDefaultExpr(); - lexer.nextToken(); - break; - case DUAL: - case KEY: - case DISTINCT: - case LIMIT: - case SCHEMA: - case COLUMN: - case IF: - sqlExpr = new SQLIdentifierExpr(lexer.stringVal()); - lexer.nextToken(); - break; - case CASE: - SQLCaseExpr caseExpr = new SQLCaseExpr(); - lexer.nextToken(); - if (lexer.token() != Token.WHEN) { - caseExpr.setValueExpr(expr()); - } - - accept(Token.WHEN); - SQLExpr testExpr = expr(); - accept(Token.THEN); - SQLExpr valueExpr = expr(); - SQLCaseExpr.Item caseItem = new SQLCaseExpr.Item(testExpr, valueExpr); - caseExpr.getItems().add(caseItem); - - while (lexer.token() == Token.WHEN) { - lexer.nextToken(); - testExpr = expr(); - accept(Token.THEN); - valueExpr = expr(); - caseItem = new SQLCaseExpr.Item(testExpr, valueExpr); - caseExpr.getItems().add(caseItem); - } - - if (lexer.token() == Token.ELSE) { - lexer.nextToken(); - caseExpr.setElseExpr(expr()); - } - - accept(Token.END); - - sqlExpr = caseExpr; - break; - case EXISTS: - lexer.nextToken(); - accept(Token.LPAREN); - sqlExpr = new SQLExistsExpr(createSelectParser().select()); - accept(Token.RPAREN); - break; - case NOT: - lexer.nextToken(); - if (lexer.token() == Token.EXISTS) { - lexer.nextToken(); - accept(Token.LPAREN); - sqlExpr = new SQLExistsExpr(createSelectParser().select(), true); - accept(Token.RPAREN); - } else if (lexer.token() == Token.LPAREN) { - lexer.nextToken(); - - SQLExpr notTarget = expr(); - - accept(Token.RPAREN); - notTarget = exprRest(notTarget); - - sqlExpr = new SQLNotExpr(notTarget); - - return primaryRest(sqlExpr); - } else { - SQLExpr restExpr = expr(); - sqlExpr = new SQLNotExpr(restExpr); - } - break; - case SELECT: - SQLQueryExpr queryExpr = new SQLQueryExpr(createSelectParser().select()); - sqlExpr = queryExpr; - break; - case CAST: - lexer.nextToken(); - accept(Token.LPAREN); - SQLCastExpr cast = new SQLCastExpr(); - cast.setExpr(expr()); - accept(Token.AS); - cast.setDataType(parseDataType()); - accept(Token.RPAREN); - - sqlExpr = cast; - break; - case SUB: - lexer.nextToken(); - switch (lexer.token()) { - case LITERAL_INT: - Number integerValue = lexer.integerValue(); - if (integerValue instanceof Integer) { - int intVal = ((Integer) integerValue).intValue(); - if (intVal == Integer.MIN_VALUE) { - integerValue = Long.valueOf(((long) intVal) * -1); - } else { - integerValue = Integer.valueOf(intVal * -1); - } - } else if (integerValue instanceof Long) { - long longVal = ((Long) integerValue).longValue(); - if (longVal == 2147483648L) { - integerValue = Integer.valueOf((int) (((long) longVal) * -1)); - } else { - integerValue = Long.valueOf(longVal * -1); - } - } else { - integerValue = ((BigInteger) integerValue).negate(); - } - sqlExpr = new SQLIntegerExpr(integerValue); - lexer.nextToken(); - break; - case LITERAL_FLOAT: - sqlExpr = new SQLNumberExpr(lexer.decimalValue().negate()); - lexer.nextToken(); - break; - case IDENTIFIER: // 当负号后面为字段的情况 - sqlExpr = new SQLIdentifierExpr('-' + lexer.stringVal()); - lexer.nextToken(); - break; - default: - throw new ParserException("TODO"); - } - break; - case PLUS: - lexer.nextToken(); - switch (lexer.token()) { - case LITERAL_INT: - sqlExpr = new SQLIntegerExpr(lexer.integerValue()); - lexer.nextToken(); - break; - case LITERAL_FLOAT: - sqlExpr = new SQLNumberExpr(lexer.decimalValue()); - lexer.nextToken(); - break; - default: - throw new ParserException("TODO"); - } - break; - case TILDE: - lexer.nextToken(); - SQLExpr unaryValueExpr = expr(); - SQLUnaryExpr unary = new SQLUnaryExpr(SQLUnaryOperator.Compl, unaryValueExpr); - sqlExpr = unary; - break; - case QUES: - lexer.nextToken(); - SQLVariantRefExpr quesVarRefExpr = new SQLVariantRefExpr("?"); - quesVarRefExpr.setIndex(lexer.nextVarIndex()); - sqlExpr = quesVarRefExpr; - break; - case LEFT: - sqlExpr = new SQLIdentifierExpr("LEFT"); - lexer.nextToken(); - break; - case RIGHT: - sqlExpr = new SQLIdentifierExpr("RIGHT"); - lexer.nextToken(); - break; - case DATABASE: - sqlExpr = new SQLIdentifierExpr("DATABASE"); - lexer.nextToken(); - break; - case LOCK: - sqlExpr = new SQLIdentifierExpr("LOCK"); - lexer.nextToken(); - break; - case NULL: - sqlExpr = new SQLNullExpr(); - lexer.nextToken(); - break; - case BANG: - lexer.nextToken(); - SQLExpr bangExpr = expr(); - sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Not, bangExpr); - break; - case LITERAL_HEX: - String hex = lexer.hexString(); - sqlExpr = new SQLHexExpr(hex); - lexer.nextToken(); - break; - case INTERVAL: - sqlExpr = parseInterval(); - break; - case COLON: - lexer.nextToken(); - if (lexer.token == Token.LITERAL_ALIAS) { - sqlExpr = new SQLVariantRefExpr(":\"" + lexer.stringVal() + "\""); - lexer.nextToken(); - } - break; - case ANY: - lexer.nextToken(); - if (lexer.token() == Token.LPAREN) { - SQLAnyExpr anyExpr = new SQLAnyExpr(); - - accept(Token.LPAREN); - SQLSelect anySubQuery = createSelectParser().select(); - anyExpr.setSubQuery(anySubQuery); - accept(Token.RPAREN); - - anySubQuery.setParent(anyExpr); - - sqlExpr = anyExpr; - } else { - sqlExpr = new SQLIdentifierExpr("ANY"); - } - break; - case SOME: - lexer.nextToken(); - SQLSomeExpr someExpr = new SQLSomeExpr(); - - accept(Token.LPAREN); - SQLSelect someSubQuery = createSelectParser().select(); - someExpr.setSubQuery(someSubQuery); - accept(Token.RPAREN); - - someSubQuery.setParent(someExpr); - - sqlExpr = someExpr; - break; - case ALL: - lexer.nextToken(); - SQLAllExpr allExpr = new SQLAllExpr(); - - accept(Token.LPAREN); - SQLSelect allSubQuery = createSelectParser().select(); - allExpr.setSubQuery(allSubQuery); - accept(Token.RPAREN); - - allSubQuery.setParent(allExpr); - - sqlExpr = allExpr; - break; - default: - throw new ParserException("ERROR. token : " + tok + " " + lexer.stringVal()); - } - - return primaryRest(sqlExpr); - } - - protected SQLExpr parseInterval() { - throw new ParserException("TODO"); - } - - public SQLSelectParser createSelectParser() { - return new SQLSelectParser(this); - } - - public SQLExpr primaryRest(SQLExpr expr) { - if (expr == null) { - throw new IllegalArgumentException("expr"); - } - - if (lexer.token() == Token.OF) { - if (expr instanceof SQLIdentifierExpr) { - String name = ((SQLIdentifierExpr) expr).getName(); - if ("CURRENT".equalsIgnoreCase(name)) { - lexer.nextToken(); - SQLName cursorName = this.name(); - return new SQLCurrentOfCursorExpr(cursorName); - } - } - } - - if (lexer.token() == Token.DOT) { - lexer.nextToken(); - - if (expr instanceof SQLCharExpr) { - String text = ((SQLCharExpr) expr).getText(); - expr = new SQLIdentifierExpr(text); - } - - expr = dotRest(expr); - return primaryRest(expr); - } else { - if (lexer.token() == Token.LPAREN) { - return methodRest(expr, true); - } - } - - return expr; - } - - protected SQLExpr methodRest(SQLExpr expr, boolean acceptLPAREN) { - if (acceptLPAREN) { - accept(Token.LPAREN); - } - - if (expr instanceof SQLName || expr instanceof SQLDefaultExpr) { - String methodName; - - SQLMethodInvokeExpr methodInvokeExpr; - if (expr instanceof SQLPropertyExpr) { - methodName = ((SQLPropertyExpr) expr).getName(); - methodInvokeExpr = new SQLMethodInvokeExpr(methodName); - methodInvokeExpr.setOwner(((SQLPropertyExpr) expr).getOwner()); - } else { - methodName = expr.toString(); - methodInvokeExpr = new SQLMethodInvokeExpr(methodName); - } - - if (isAggreateFunction(methodName)) { - SQLAggregateExpr aggregateExpr = parseAggregateExpr(methodName); - - return aggregateExpr; - } - - if (lexer.token() != Token.RPAREN) { - exprList(methodInvokeExpr.getParameters()); - } - - accept(Token.RPAREN); - - return primaryRest(methodInvokeExpr); - } - - throw new ParserException("not support token:" + lexer.token()); - } - - protected SQLExpr dotRest(SQLExpr expr) { - if (lexer.token() == Token.STAR) { - lexer.nextToken(); - expr = new SQLPropertyExpr(expr, "*"); - } else { - String name; - - if (lexer.token() == Token.IDENTIFIER || lexer.token() == Token.LITERAL_CHARS - || lexer.token() == Token.LITERAL_ALIAS) { - name = lexer.stringVal(); - lexer.nextToken(); - } else if (lexer.getKeywods().containsValue(lexer.token())) { - name = lexer.stringVal(); - lexer.nextToken(); - } else { - throw new ParserException("error : " + lexer.stringVal()); - } - - if (lexer.token() == Token.LPAREN) { - lexer.nextToken(); - - SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr(name); - methodInvokeExpr.setOwner(expr); - if (lexer.token() == Token.RPAREN) { - lexer.nextToken(); - } else { - if (lexer.token() == Token.PLUS) { - methodInvokeExpr.getParameters().add(new SQLIdentifierExpr("+")); - lexer.nextToken(); - } else { - exprList(methodInvokeExpr.getParameters()); - } - accept(Token.RPAREN); - } - expr = methodInvokeExpr; - } else { - expr = new SQLPropertyExpr(expr, name); - } - } - - expr = primaryRest(expr); - return expr; - } - - public final SQLExpr groupComparisionRest(SQLExpr expr) { - return expr; - } - - public final void names(Collection exprCol) { - if (lexer.token() == Token.RBRACE) { - return; - } - - if (lexer.token() == Token.EOF) { - return; - } - - exprCol.add(name()); - - while (lexer.token() == Token.COMMA) { - lexer.nextToken(); - exprCol.add(name()); - } - } - - public final void exprList(Collection exprCol) { - if (lexer.token() == Token.RPAREN || lexer.token() == Token.RBRACKET) { - return; - } - - if (lexer.token() == Token.EOF) { - return; - } - - SQLExpr expr = expr(); - exprCol.add(expr); - - while (lexer.token() == Token.COMMA) { - lexer.nextToken(); - expr = expr(); - exprCol.add(expr); - } - } - - public SQLName name() { - String identName; - if (lexer.token() == Token.LITERAL_ALIAS) { - identName = '"' + lexer.stringVal() + '"'; - lexer.nextToken(); - } else if (lexer.token() == Token.IDENTIFIER) { - identName = lexer.stringVal(); - - lexer.nextToken(); - } else if (lexer.token() == Token.LITERAL_CHARS) { - identName = '\'' + lexer.stringVal() + '\''; - lexer.nextToken(); - } else { - throw new ParserException("error " + lexer.token()); - } - - SQLName name = new SQLIdentifierExpr(identName); - - name = nameRest(name); - - return name; - } - - public SQLName nameRest(SQLName name) { - if (lexer.token() == Token.DOT) { - lexer.nextToken(); - - if (lexer.token() == Token.KEY) { - name = new SQLPropertyExpr(name, "KEY"); - lexer.nextToken(); - return name; - } - - if (lexer.token() != Token.LITERAL_ALIAS && lexer.token() != Token.IDENTIFIER - && (!lexer.getKeywods().containsValue(lexer.token()))) { - throw new ParserException("error, " + lexer.token()); - } - - if (lexer.token() == Token.LITERAL_ALIAS) { - name = new SQLPropertyExpr(name, '"' + lexer.stringVal() + '"'); - } else { - name = new SQLPropertyExpr(name, lexer.stringVal()); - } - lexer.nextToken(); - name = nameRest(name); - } - - return name; - } - - public boolean isAggreateFunction(String word) { - String[] aggregateFunctions = { "AVG", "COUNT", "MAX", "MIN", "STDDEV", "SUM" }; - - for (int i = 0; i < aggregateFunctions.length; ++i) { - if (aggregateFunctions[i].compareToIgnoreCase(word) == 0) { - return true; - } - } - - return false; - } - - protected SQLAggregateExpr parseAggregateExpr(String methodName) { - methodName = methodName.toUpperCase(); - - SQLAggregateExpr aggregateExpr; - if (lexer.token() == Token.ALL) { - aggregateExpr = new SQLAggregateExpr(methodName, SQLAggregateExpr.Option.ALL); - lexer.nextToken(); - } else if (lexer.token() == Token.DISTINCT) { - aggregateExpr = new SQLAggregateExpr(methodName, SQLAggregateExpr.Option.DISTINCT); - lexer.nextToken(); - } else { - aggregateExpr = new SQLAggregateExpr(methodName); - } - - exprList(aggregateExpr.getArguments()); - - accept(Token.RPAREN); - - if (lexer.token() == Token.OVER) { - lexer.nextToken(); - SQLOver over = new SQLOver(); - accept(Token.LPAREN); - - if (identifierEquals("PARTITION")) { - lexer.nextToken(); - accept(Token.BY); - - if (lexer.token() == (Token.LPAREN)) { - lexer.nextToken(); - exprList(over.getPartitionBy()); - accept(Token.RPAREN); - } else { - exprList(over.getPartitionBy()); - } - } - - - over.setOrderBy(parseOrderBy()); - - // if (over.getOrderBy() != null) { - // //TODO window - // } - - accept(Token.RPAREN); - aggregateExpr.setOver(over); - - } - - return aggregateExpr; - } - - public SQLOrderBy parseOrderBy() { - if (lexer.token() == Token.ORDER) { - SQLOrderBy orderBy = new SQLOrderBy(); - - lexer.nextToken(); - - accept(Token.BY); - - orderBy.getItems().add(parseSelectOrderByItem()); - - while (lexer.token() == Token.COMMA) { - lexer.nextToken(); - orderBy.getItems().add(parseSelectOrderByItem()); - } - - return orderBy; - } - - return null; - } - - public SQLSelectOrderByItem parseSelectOrderByItem() { - SQLSelectOrderByItem item = new SQLSelectOrderByItem(); - - item.setExpr(expr()); - - if (lexer.token() == Token.ASC) { - lexer.nextToken(); - item.setType(SQLOrderingSpecification.ASC); - } else if (lexer.token() == Token.DESC) { - lexer.nextToken(); - item.setType(SQLOrderingSpecification.DESC); - } - - return item; - } - - public final SQLExpr bitAnd() { - SQLExpr expr = shift(); - return bitAndRest(expr); - } - - public final SQLExpr bitAndRest(SQLExpr expr) { - while (lexer.token() == Token.AMP) { - lexer.nextToken(); - SQLExpr rightExp = shift(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BitwiseAnd, rightExp); - } - return expr; - } - - public final SQLExpr bitOr() { - SQLExpr expr = bitAnd(); - return bitOrRest(expr); - } - - public final SQLExpr bitOrRest(SQLExpr expr) { - if (lexer.token() == Token.BAR) { - lexer.nextToken(); - SQLExpr rightExp = bitAnd(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BitwiseOr, rightExp); - expr = bitAndRest(expr); - } else if (lexer.token() == Token.TILDE) { - lexer.nextToken(); - SQLExpr rightExp = bitAnd(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.InvertBits, rightExp); - expr = bitAndRest(expr); - } - return expr; - } - - public final SQLExpr equality() { - SQLExpr expr = shift(); - return equalityRest(expr); - } - - public SQLExpr equalityRest(SQLExpr expr) { - SQLExpr rightExp; - if (lexer.token() == Token.EQ) { - lexer.nextToken(); - rightExp = shift(); - - rightExp = equalityRest(rightExp); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Equality, rightExp); - } else if (lexer.token() == Token.BANGEQ) { - lexer.nextToken(); - rightExp = shift(); - - rightExp = equalityRest(rightExp); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotEqual, rightExp); - } else if (lexer.token() == Token.COLONEQ) { - lexer.nextToken(); - rightExp = expr(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Assignment, rightExp); - } - - return expr; - } - - public final SQLExpr inRest(SQLExpr expr) { - if (lexer.token() == Token.IN) { - lexer.nextToken(); - accept(Token.LPAREN); - - SQLInListExpr inListExpr = new SQLInListExpr(expr); - exprList(inListExpr.getTargetList()); - expr = inListExpr; - - accept(Token.RPAREN); - expr = inListExpr; - - if (inListExpr.getTargetList().size() == 1) { - SQLExpr targetExpr = inListExpr.getTargetList().get(0); - if (targetExpr instanceof SQLQueryExpr) { - SQLInSubQueryExpr inSubQueryExpr = new SQLInSubQueryExpr(); - inSubQueryExpr.setExpr(inListExpr.getExpr()); - inSubQueryExpr.setSubQuery(((SQLQueryExpr) targetExpr).getSubQuery()); - expr = inSubQueryExpr; - } - } - } - - return expr; - } - - public final SQLExpr additive() { - SQLExpr expr = multiplicative(); - return additiveRest(expr); - } - - public SQLExpr additiveRest(SQLExpr expr) { - if (lexer.token() == Token.PLUS) { - lexer.nextToken(); - SQLExpr rightExp = multiplicative(); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Add, rightExp); - expr = additiveRest(expr); - } else if (lexer.token() == Token.BARBAR) { - lexer.nextToken(); - SQLExpr rightExp = multiplicative(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Concat, rightExp); - expr = additiveRest(expr); - } else if (lexer.token() == Token.SUB) { - lexer.nextToken(); - SQLExpr rightExp = multiplicative(); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Subtract, rightExp); - expr = additiveRest(expr); - } - - return expr; - } - - public final SQLExpr shift() { - SQLExpr expr = additive(); - return shiftRest(expr); - } - - public SQLExpr shiftRest(SQLExpr expr) { - if (lexer.token() == Token.LTLT) { - lexer.nextToken(); - SQLExpr rightExp = additive(); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.LeftShift, rightExp); - expr = shiftRest(expr); - } else if (lexer.token() == Token.GTGT) { - lexer.nextToken(); - SQLExpr rightExp = additive(); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.RightShift, rightExp); - expr = shiftRest(expr); - } - - return expr; - } - - public SQLExpr and() { - SQLExpr expr = relational(); - return andRest(expr); - } - - public SQLExpr andRest(SQLExpr expr) { - for (;;) { - if (lexer.token() == Token.AND || lexer.token() == Token.AMPAMP) { - lexer.nextToken(); - SQLExpr rightExp = relational(); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BooleanAnd, rightExp); - } else { - break; - } - } - - return expr; - } - - public SQLExpr or() { - SQLExpr expr = and(); - return orRest(expr); - } - - public SQLExpr orRest(SQLExpr expr) { - - for (;;) { - if (lexer.token() == Token.OR) { - lexer.nextToken(); - SQLExpr rightExp = and(); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BooleanOr, rightExp); - } else if (lexer.token() == Token.XOR) { - lexer.nextToken(); - SQLExpr rightExp = and(); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BooleanXor, rightExp); - } else { - break; - } - } - - return expr; - } - - public SQLExpr relational() { - SQLExpr expr = equality(); - - return relationalRest(expr); - } - - public SQLExpr relationalRest(SQLExpr expr) { - SQLExpr rightExp; - - if (lexer.token() == Token.LT) { - SQLBinaryOperator op = SQLBinaryOperator.LessThan; - - lexer.nextToken(); - if (lexer.token() == Token.EQ) { - lexer.nextToken(); - op = SQLBinaryOperator.LessThanOrEqual; - } - - rightExp = bitOr(); - expr = new SQLBinaryOpExpr(expr, op, rightExp); - // expr = relationalRest(expr); - } else if (lexer.token() == Token.LTEQ) { - lexer.nextToken(); - rightExp = bitOr(); - - // rightExp = relationalRest(rightExp); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.LessThanOrEqual, rightExp); - } else if (lexer.token() == Token.LTEQGT) { - lexer.nextToken(); - rightExp = bitOr(); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.LessThanOrEqualOrGreaterThan, rightExp); - } else if (lexer.token() == Token.GT) { - SQLBinaryOperator op = SQLBinaryOperator.GreaterThan; - - lexer.nextToken(); - - if (lexer.token() == Token.EQ) { - lexer.nextToken(); - op = SQLBinaryOperator.GreaterThanOrEqual; - } - - rightExp = bitOr(); - - expr = new SQLBinaryOpExpr(expr, op, rightExp); - } else if (lexer.token() == Token.GTEQ) { - lexer.nextToken(); - rightExp = bitOr(); - - // rightExp = relationalRest(rightExp); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.GreaterThanOrEqual, rightExp); - } else if (lexer.token() == Token.BANGLT) { - lexer.nextToken(); - rightExp = bitOr(); - - // rightExp = relationalRest(rightExp); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotLessThan, rightExp); - } else if (lexer.token() == Token.BANGGT) { - lexer.nextToken(); - rightExp = bitOr(); - - rightExp = relationalRest(rightExp); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotGreaterThan, rightExp); - } else if (lexer.token() == Token.LTGT) { - lexer.nextToken(); - rightExp = bitOr(); - - // rightExp = relationalRest(rightExp); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.LessThanOrGreater, rightExp); - } else if (lexer.token() == Token.LIKE) { - lexer.nextToken(); - rightExp = bitOr(); - - // rightExp = relationalRest(rightExp); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Like, rightExp); - - if (lexer.token() == Token.ESCAPE) { - lexer.nextToken(); - rightExp = expr(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Escape, rightExp); - } - } else if (lexer.token() == (Token.NOT)) { - lexer.nextToken(); - expr = notRationalRest(expr); - } else if (lexer.token() == (Token.BETWEEN)) { - lexer.nextToken(); - SQLExpr beginExpr = bitOr(); - accept(Token.AND); - SQLExpr endExpr = bitOr(); - expr = new SQLBetweenExpr(expr, beginExpr, endExpr); - } else if (lexer.token() == (Token.IS)) { - lexer.nextToken(); - - if (lexer.token() == (Token.NOT)) { - lexer.nextToken(); - SQLExpr rightExpr = primary(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.IsNot, rightExpr); - } else { - SQLExpr rightExpr = primary(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Is, rightExpr); - } - } else if (lexer.token() == Token.IN) { - expr = inRest(expr); - } - - return expr; - } - - public SQLExpr notRationalRest(SQLExpr expr) { - if (lexer.token() == (Token.LIKE)) { - lexer.nextToken(); - SQLExpr rightExp = equality(); - - rightExp = relationalRest(rightExp); - - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotLike, rightExp); - - if (lexer.token() == Token.ESCAPE) { - lexer.nextToken(); - rightExp = expr(); - expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Escape, rightExp); - } - } else if (lexer.token() == Token.IN) { - lexer.nextToken(); - accept(Token.LPAREN); - - SQLInListExpr inListExpr = new SQLInListExpr(expr, true); - exprList(inListExpr.getTargetList()); - expr = inListExpr; - - accept(Token.RPAREN); - - if (inListExpr.getTargetList().size() == 1) { - SQLExpr targetExpr = inListExpr.getTargetList().get(0); - if (targetExpr instanceof SQLQueryExpr) { - SQLInSubQueryExpr inSubQueryExpr = new SQLInSubQueryExpr(); - inSubQueryExpr.setNot(true); - inSubQueryExpr.setExpr(inListExpr.getExpr()); - inSubQueryExpr.setSubQuery(((SQLQueryExpr) targetExpr).getSubQuery()); - expr = inSubQueryExpr; - } - } - - expr = relationalRest(expr); - return expr; - } else if (lexer.token() == (Token.BETWEEN)) { - lexer.nextToken(); - SQLExpr beginExpr = bitOr(); - accept(Token.AND); - SQLExpr endExpr = bitOr(); - - expr = new SQLBetweenExpr(expr, true, beginExpr, endExpr); - - return expr; - } else { - throw new ParserException("TODO " + lexer.token()); - } - return expr; - } - - public SQLDataType parseDataType() { - - if (lexer.token() == Token.DEFAULT || lexer.token() == Token.NOT || lexer.token() == Token.NULL) { - return null; - } - - SQLName typeExpr = name(); - String typeName = typeExpr.toString(); - - if ("character".equalsIgnoreCase(typeName) && "varying".equalsIgnoreCase(lexer.stringVal())) { - typeName += ' ' + lexer.stringVal(); - lexer.nextToken(); - } - - SQLDataType dataType = new SQLDataTypeImpl(typeName); - return parseDataTypeRest(dataType); - } - - protected SQLDataType parseDataTypeRest(SQLDataType dataType) { - if (lexer.token() == Token.LPAREN) { - lexer.nextToken(); - exprList(dataType.getArguments()); - accept(Token.RPAREN); - - return parseCharTypeRest(dataType); - } - - return dataType; - } - - protected boolean isCharType(SQLDataType dataType) { - String dataTypeName = dataType.getName(); - - return "char".equalsIgnoreCase(dataTypeName) // - || "varchar".equalsIgnoreCase(dataTypeName) - || "nchar".equalsIgnoreCase(dataTypeName) - || "nvarchar".equalsIgnoreCase(dataTypeName) - // - ; - } - - protected SQLDataType parseCharTypeRest(SQLDataType dataType) { - if (!isCharType(dataType)) { - return dataType; - } - - SQLCharactorDataType charType = new SQLCharactorDataType(dataType.getName()); - charType.getArguments().addAll(dataType.getArguments()); - - if (identifierEquals("CHARACTER")) { - lexer.nextToken(); - - accept(Token.SET); - - if (lexer.token() != Token.IDENTIFIER && lexer.token() != Token.LITERAL_CHARS) { - throw new ParserException(); - } - charType.setCharSetName(lexer.stringVal()); - lexer.nextToken(); - - if (lexer.token() == Token.IDENTIFIER) { - if (lexer.stringVal().equalsIgnoreCase("COLLATE")) { - lexer.nextToken(); - - if (lexer.token() != Token.IDENTIFIER) { - throw new ParserException(); - } - charType.setCollate(lexer.stringVal()); - lexer.nextToken(); - } - } - } - return charType; - } - - public void accept(Token token) { - if (lexer.token() == token) { - lexer.nextToken(); - } else { - throw new SQLParseException("syntax error, expect " + token + ", actual " + lexer.token() + " " - + lexer.stringVal()); - } - } - - public SQLColumnDefinition parseColumn() { - SQLColumnDefinition column = new SQLColumnDefinition(); - column.setName(name()); - column.setDataType(parseDataType()); - - return parseColumnRest(column); - } - - public SQLColumnDefinition parseColumnRest(SQLColumnDefinition column) { - if (lexer.token() == Token.DEFAULT) { - lexer.nextToken(); - column.setDefaultExpr(bitOr()); - return parseColumnRest(column); - } - - if (lexer.token() == Token.NOT) { - lexer.nextToken(); - accept(Token.NULL); - column.getConstaints().add(new NotNullConstraint()); - return parseColumnRest(column); - } - - if (lexer.token() == Token.NULL) { - lexer.nextToken(); - column.setDefaultExpr(new SQLNullExpr()); - return parseColumnRest(column); - } - - return column; - } - - public SQLPrimaryKey parsePrimaryKey() { - throw new ParserException("TODO"); - } - - public SQLAssignItem parseAssignItem() { - SQLAssignItem item = new SQLAssignItem(); - - SQLExpr var = primary(); - - if (var instanceof SQLIdentifierExpr) { - var = new SQLVariantRefExpr(((SQLIdentifierExpr) var).getName()); - } - item.setTarget(var); - if (lexer.token() == Token.COLONEQ) { - lexer.nextToken(); - } else { - accept(Token.EQ); - } - item.setValue(expr()); - - return item; - } - - public List parseHints() { - List hints = new ArrayList(); - parseHints(hints); - return hints; - } - - @SuppressWarnings({ "unchecked", "rawtypes" }) - public void parseHints(List hints) { - if (lexer.token() == Token.HINT) { - hints.add(new SQLCommentHint(lexer.stringVal())); - lexer.nextToken(); - } - } -} From 78682c46f278b7c9e40f5fa6d030d463df47ca14 Mon Sep 17 00:00:00 2001 From: Caleb Ott Date: Mon, 28 Sep 2015 09:40:42 -0500 Subject: [PATCH 079/559] Added in NOT check to SqlExprParser to fix a bug with the sqlParser in the druid --- .../es4sql/parse/ElasticSqlExprParser.java | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java b/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java index c4cdbb30..0b272ab4 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java @@ -2,14 +2,14 @@ import com.alibaba.druid.sql.ast.SQLCommentHint; import com.alibaba.druid.sql.ast.SQLExpr; +import com.alibaba.druid.sql.ast.expr.SQLExistsExpr; import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; +import com.alibaba.druid.sql.ast.expr.SQLNotExpr; import com.alibaba.druid.sql.dialect.mysql.parser.MySqlExprParser; -import com.alibaba.druid.sql.dialect.mysql.parser.MySqlLexer; import com.alibaba.druid.sql.parser.Lexer; import com.alibaba.druid.sql.parser.ParserException; import com.alibaba.druid.sql.parser.Token; -import java.util.ArrayList; import java.util.List; /** @@ -80,6 +80,30 @@ else if(lexer.token() == Token.LBRACKET){ accept(Token.RBRACKET); return new SQLIdentifierExpr(identifier.toString()); } + else if (lexer.token() == Token.NOT) { + lexer.nextToken(); + SQLExpr sqlExpr; + if (lexer.token() == Token.EXISTS) { + lexer.nextToken(); + accept(Token.LPAREN); + sqlExpr = new SQLExistsExpr(createSelectParser().select(), true); + accept(Token.RPAREN); + } else if (lexer.token() == Token.LPAREN) { + lexer.nextToken(); + + SQLExpr notTarget = expr(); + + accept(Token.RPAREN); + + sqlExpr = new SQLNotExpr(notTarget); + + return primaryRest(sqlExpr); + } else { + SQLExpr restExpr = relational(); + sqlExpr = new SQLNotExpr(restExpr); + } + return sqlExpr; + } return super.primary(); } } From 8009c49e973e85aedb9160e46899002de5151957 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Tue, 29 Sep 2015 19:06:50 +0300 Subject: [PATCH 080/559] alias support on bucket aggregations #81 --- .../nlpcn/es4sql/query/maker/AggMaker.java | 40 +++++++++++++++---- .../org/nlpcn/es4sql/AggregationTest.java | 9 +++++ src/test/java/org/nlpcn/es4sql/QueryTest.java | 2 +- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java b/src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java index 7d5891d1..5d0cb0b9 100644 --- a/src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java +++ b/src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java @@ -99,7 +99,8 @@ private AggregationBuilder makeRangeGroup(MethodField field) throws SqlParseE } private AggregationBuilder geohashGrid(MethodField field) throws SqlParseException { - GeoHashGridBuilder geoHashGrid = AggregationBuilders.geohashGrid(field.getAlias()); + String aggName = gettAggNameFromParamsOrAlias(field); + GeoHashGridBuilder geoHashGrid = AggregationBuilders.geohashGrid(aggName); String value = null; for (KVValue kv : field.getParams()) { value = kv.value.toString(); @@ -116,6 +117,8 @@ private AggregationBuilder geohashGrid(MethodField field) throws SqlParseExce case "shard_size": geoHashGrid.shardSize(Integer.parseInt(value)); break; + case "alias": + break; default: throw new SqlParseException("geohash grid err or not define field " + kv.toString()); } @@ -126,7 +129,8 @@ private AggregationBuilder geohashGrid(MethodField field) throws SqlParseExce private static final String TIME_FARMAT = "yyyy-MM-dd HH:mm:ss"; private ValuesSourceAggregationBuilder dateRange(MethodField field) { - DateRangeBuilder dateRange = AggregationBuilders.dateRange(field.getAlias()).format(TIME_FARMAT); + String alias = gettAggNameFromParamsOrAlias(field); + DateRangeBuilder dateRange = AggregationBuilders.dateRange(alias).format(TIME_FARMAT); String value = null; List ranges = new ArrayList<>(); @@ -142,8 +146,10 @@ private ValuesSourceAggregationBuilder dateRange(MethodField field) { dateRange.addUnboundedFrom(kv.value); continue; } else if ("to".equals(kv.key)) { - dateRange.addUnboundedTo(kv.value); - continue; + dateRange.addUnboundedTo(kv.value); + continue; + } else if ("alias".equals(kv.key)){ + continue; } else { ranges.add(value); } @@ -164,7 +170,8 @@ private ValuesSourceAggregationBuilder dateRange(MethodField field) { * @throws SqlParseException */ private DateHistogramBuilder dateHistogram(MethodField field) throws SqlParseException { - DateHistogramBuilder dateHistogram = AggregationBuilders.dateHistogram(field.getAlias()).format(TIME_FARMAT); + String alias = gettAggNameFromParamsOrAlias(field); + DateHistogramBuilder dateHistogram = AggregationBuilders.dateHistogram(alias).format(TIME_FARMAT); String value = null; for (KVValue kv : field.getParams()) { value = kv.value.toString(); @@ -191,6 +198,8 @@ private DateHistogramBuilder dateHistogram(MethodField field) throws SqlParseExc case "pre_offset": dateHistogram.preOffset(value); break; + case "alias": + break; default: throw new SqlParseException("date range err or not define field " + kv.toString()); } @@ -198,8 +207,18 @@ private DateHistogramBuilder dateHistogram(MethodField field) throws SqlParseExc return dateHistogram; } - private HistogramBuilder histogram(MethodField field) throws SqlParseException { - HistogramBuilder histogram = AggregationBuilders.histogram(field.getAlias()); + private String gettAggNameFromParamsOrAlias(MethodField field) { + String alias = field.getAlias(); + for (KVValue kv : field.getParams()) { + if(kv.key != null &&kv.key.equals("alias")) + alias = kv.value.toString(); + } + return alias; + } + + private HistogramBuilder histogram(MethodField field) throws SqlParseException { + String aggName = gettAggNameFromParamsOrAlias(field); + HistogramBuilder histogram = AggregationBuilders.histogram(aggName); String value = null; for (KVValue kv : field.getParams()) { value = kv.value.toString(); @@ -218,6 +237,8 @@ private HistogramBuilder histogram(MethodField field) throws SqlParseException { if (bounds.length == 2) histogram.extendedBounds(Long.valueOf(bounds[0]), Long.valueOf(bounds[1])); break; + case "alias": + break; case "order": Histogram.Order order = null; switch (value) { @@ -298,7 +319,8 @@ private AbstractAggregationBuilder makeCountAgg(MethodField field) { * @return */ private AbstractAggregationBuilder makeTopHitsAgg(MethodField field) { - TopHitsBuilder topHits = AggregationBuilders.topHits(field.getAlias()); + String alias = gettAggNameFromParamsOrAlias(field); + TopHitsBuilder topHits = AggregationBuilders.topHits(alias); List params = field.getParams(); for (KVValue kv : params) { switch (kv.key) { @@ -308,6 +330,8 @@ private AbstractAggregationBuilder makeTopHitsAgg(MethodField field) { case "size": topHits.setSize((int) kv.value); break; + case "alias": + break; default: topHits.addSort(kv.key, SortOrder.valueOf(kv.value.toString().toUpperCase())); break; diff --git a/src/test/java/org/nlpcn/es4sql/AggregationTest.java b/src/test/java/org/nlpcn/es4sql/AggregationTest.java index 2441f89c..5034ee33 100644 --- a/src/test/java/org/nlpcn/es4sql/AggregationTest.java +++ b/src/test/java/org/nlpcn/es4sql/AggregationTest.java @@ -194,6 +194,13 @@ public void countGroupByDateTest() throws IOException, SqlParseException, SQLFea System.out.println(result); } + @Test + public void countGroupByDateTestWithAlias() throws IOException, SqlParseException, SQLFeatureNotSupportedException { + SqlElasticSearchRequestBuilder result = (SqlElasticSearchRequestBuilder) MainTestSuite.getSearchDao().explain("select insert_time from online group by date_histogram(field='insert_time','interval'='1.5h','format'='yyyy-MM','alias'='myAlias') "); + boolean containAlias = result.toString().replaceAll("\\s+","").contains("myAlias\":{\"date_histogram\":{\"field\":\"insert_time\",\"interval\":\"1.5h\",\"format\":\"yyyy-MM\"}}"); + Assert.assertTrue(containAlias); + } + /** * 时间范围聚合 * @@ -223,12 +230,14 @@ public void topHitTest() throws IOException, SqlParseException, SQLFeatureNotSup System.out.println(result); } + private Aggregations query(String query) throws SqlParseException, SQLFeatureNotSupportedException { SearchDao searchDao = MainTestSuite.getSearchDao(); SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) searchDao.explain(query); return ((SearchResponse)select.get()).getAggregations(); } + @Test public void testSubAggregations() throws Exception { Set expectedAges = new HashSet<>(ContiguousSet.create(Range.closed(20, 40), DiscreteDomain.integers())); diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java index ca2226b7..c838e164 100644 --- a/src/test/java/org/nlpcn/es4sql/QueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java @@ -206,7 +206,7 @@ public void likeTest() throws IOException, SqlParseException, SQLFeatureNotSuppo @Test public void notLikeTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { - SearchHits response = query(String.format("SELECT * FROM %s WHERE firstname NOT LIKE 'amb%%'", TEST_INDEX)); + SearchHits response = query(String.format("SELECT * FROM %s/account WHERE firstname NOT LIKE 'amb%%'", TEST_INDEX)); SearchHit[] hits = response.getHits(); // assert we got hits From 8f02f4433980ac4bf0f9e115184c6d5850f03d63 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Tue, 29 Sep 2015 20:37:31 +0300 Subject: [PATCH 081/559] terms and term filter/query support --- .../org/nlpcn/es4sql/query/maker/Maker.java | 21 +++++++++ src/test/java/org/nlpcn/es4sql/QueryTest.java | 44 ++++++++++++++++++- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java index f25b5534..8b67ee18 100644 --- a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java +++ b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java @@ -107,6 +107,27 @@ private ToXContent make(Condition cond, String name, SQLMethodInvokeExpr value) bqb = FilterBuilders.queryFilter((QueryBuilder) bqb); } break; + case "match_term": + case "matchterm": + case "term": + if(isQuery){ + bqb = QueryBuilders.termQuery(name,value.getParameters().get(0)); + } + else { + bqb = FilterBuilders.termFilter(name,value.getParameters().get(0)); + } + break; + case "in_terms": + case "interms": + case "terms": + Object[] values = value.getParameters().toArray(); + if(isQuery){ + bqb =QueryBuilders.termsQuery(name,values); + } + else { + bqb = FilterBuilders.termsFilter(name,values); + } + break; default: throw new SqlParseException("it did not support this query method " + value.getMethodName()); diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java index c838e164..a0db5823 100644 --- a/src/test/java/org/nlpcn/es4sql/QueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java @@ -283,8 +283,50 @@ public void inTestWithStrings() throws IOException, SqlParseException, SQLFeatur } } + @Test + public void inTermsTestWithStrings() throws IOException, SqlParseException, SQLFeatureNotSupportedException{ + SearchHits response = query(String.format("SELECT name FROM %s/gotCharacters WHERE name.firstname = IN_TERMS(daenerys,eddard) LIMIT 1000", TEST_INDEX)); + SearchHit[] hits = response.getHits(); + Assert.assertEquals(2, response.getTotalHits()); + for(SearchHit hit : hits) { + String firstname = ((Map) hit.getSource().get("name")).get("firstname").toString(); + assertThat(firstname, isOneOf("Daenerys", "Eddard")); + } + } + + @Test + public void inTermsTestWithNumbers() throws IOException, SqlParseException, SQLFeatureNotSupportedException{ + SearchHits response = query(String.format("SELECT name FROM %s/gotCharacters WHERE name.ofHisName = IN_TERMS(4,2) LIMIT 1000", TEST_INDEX)); + SearchHit[] hits = response.getHits(); + Assert.assertEquals(1, response.getTotalHits()); + SearchHit hit = hits[0]; + String firstname = ((Map) hit.getSource().get("name")).get("firstname").toString(); + Assert.assertEquals("Brandon",firstname); + } + + + @Test + public void termQueryWithNumber() throws IOException, SqlParseException, SQLFeatureNotSupportedException{ + SearchHits response = query(String.format("SELECT name FROM %s/gotCharacters WHERE name.ofHisName = term(4) LIMIT 1000", TEST_INDEX)); + SearchHit[] hits = response.getHits(); + Assert.assertEquals(1, response.getTotalHits()); + SearchHit hit = hits[0]; + String firstname = ((Map) hit.getSource().get("name")).get("firstname").toString(); + Assert.assertEquals("Brandon",firstname); + } + + @Test + public void termQueryWithString() throws IOException, SqlParseException, SQLFeatureNotSupportedException{ + SearchHits response = query(String.format("SELECT name FROM %s/gotCharacters WHERE name.firstname = term(brandon) LIMIT 1000", TEST_INDEX)); + SearchHit[] hits = response.getHits(); + Assert.assertEquals(1, response.getTotalHits()); + SearchHit hit = hits[0]; + String firstname = ((Map) hit.getSource().get("name")).get("firstname").toString(); + Assert.assertEquals("Brandon",firstname); + } + - /* TODO when using not in on some field, documents that not contains this + /* TODO when using not in on some field, documents that not contains this field will return as well, That may considered a Wrong behaivor. */ @Test From e555926f3f302af212e6a340acc3da23d7d65522 Mon Sep 17 00:00:00 2001 From: eliranmoyal Date: Tue, 29 Sep 2015 20:41:55 +0300 Subject: [PATCH 082/559] add Travis ci status --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6be4b4af..45629a19 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ Elasticsearch-SQL ================= +[![Build Status](https://travis-ci.org/NLPchina/elasticsearch-sql.svg?branch=master)](https://travis-ci.org/NLPchina/elasticsearch-sql) Query elasticsearch using familiar SQL syntax. You can also use ES functions in SQL. From 512269c3d05df50c71b6bdead8be2842c35a5b65 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sat, 3 Oct 2015 18:55:12 +0300 Subject: [PATCH 083/559] sub/inner query support on where. --- ...er.java => ActionRequestRestExecuter.java} | 4 +- .../plugin/nlpcn/ElasticResultHandler.java | 37 ++++++++++++ .../nlpcn/QueryActionElasticExecutor.java | 55 +++++++++++++++++ .../plugin/nlpcn/RestSqlAction.java | 3 +- .../java/org/nlpcn/es4sql/domain/Select.java | 32 +++++++++- .../org/nlpcn/es4sql/parse/SqlParser.java | 39 ++++++++---- .../es4sql/parse/SubQueryExpression.java | 38 ++++++++++++ .../nlpcn/es4sql/query/ESActionFactory.java | 59 +++++++++++++++++-- .../org/nlpcn/es4sql/query/maker/Maker.java | 19 ++++-- src/test/java/org/nlpcn/es4sql/QueryTest.java | 29 +++++++++ .../java/org/nlpcn/es4sql/SqlParserTests.java | 22 ++++++- 11 files changed, 308 insertions(+), 29 deletions(-) rename src/main/java/org/elasticsearch/plugin/nlpcn/{ActionRequestExecuter.java => ActionRequestRestExecuter.java} (92%) create mode 100644 src/main/java/org/elasticsearch/plugin/nlpcn/ElasticResultHandler.java create mode 100644 src/main/java/org/elasticsearch/plugin/nlpcn/QueryActionElasticExecutor.java create mode 100644 src/main/java/org/nlpcn/es4sql/parse/SubQueryExpression.java diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestExecuter.java b/src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestRestExecuter.java similarity index 92% rename from src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestExecuter.java rename to src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestRestExecuter.java index 8b00fdf5..07e6a641 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestExecuter.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestRestExecuter.java @@ -16,13 +16,13 @@ import java.io.IOException; -public class ActionRequestExecuter { +public class ActionRequestRestExecuter { private RestChannel channel; private Client client; private SqlElasticRequestBuilder requestBuilder; - public ActionRequestExecuter(SqlElasticRequestBuilder requestBuilder, RestChannel channel, final Client client) { + public ActionRequestRestExecuter(SqlElasticRequestBuilder requestBuilder, RestChannel channel, final Client client) { this.requestBuilder = requestBuilder; this.channel = channel; this.client = client; diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticResultHandler.java b/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticResultHandler.java new file mode 100644 index 00000000..18b48158 --- /dev/null +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/ElasticResultHandler.java @@ -0,0 +1,37 @@ +package org.elasticsearch.plugin.nlpcn; + +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.aggregations.Aggregation; +import org.elasticsearch.search.aggregations.Aggregations; +import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +/** + * Created by Eliran on 3/10/2015. + */ +public class ElasticResultHandler { + public static Object getFieldValue(SearchHit hit,String field){ + return deepSearchInMap(hit.sourceAsMap(),field); + } + + private static Object deepSearchInMap(Map fieldsMap, String name) { + if(name.contains(".")){ + String[] path = name.split("\\."); + Map currentObject = fieldsMap; + for(int i=0;i) valueFromCurrentMap; + } + return currentObject.get(path[path.length-1]); + } + + return fieldsMap.get(name); + } + +} diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/QueryActionElasticExecutor.java b/src/main/java/org/elasticsearch/plugin/nlpcn/QueryActionElasticExecutor.java new file mode 100644 index 00000000..b0fe0667 --- /dev/null +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/QueryActionElasticExecutor.java @@ -0,0 +1,55 @@ +package org.elasticsearch.plugin.nlpcn; + +import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.client.Client; +import org.elasticsearch.search.SearchHits; +import org.elasticsearch.search.aggregations.Aggregations; +import org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid; +import org.nlpcn.es4sql.SearchDao; +import org.nlpcn.es4sql.exception.SqlParseException; +import org.nlpcn.es4sql.query.*; +import org.nlpcn.es4sql.query.join.ESJoinQueryAction; +import org.nlpcn.es4sql.query.join.JoinRequestBuilder; + +import java.io.IOException; + +/** + * Created by Eliran on 3/10/2015. + */ +public class QueryActionElasticExecutor { + public static SearchHits executeSearchAction(DefaultQueryAction searchQueryAction) throws SqlParseException { + SqlElasticSearchRequestBuilder builder = searchQueryAction.explain(); + return ((SearchResponse) builder.get()).getHits(); + } + + public static SearchHits executeJoinSearchAction(Client client , ESJoinQueryAction joinQueryAction) throws IOException, SqlParseException { + SqlElasticRequestBuilder joinRequestBuilder = joinQueryAction.explain(); + ElasticJoinExecutor executor = ElasticJoinExecutor.createJoinExecutor(client,joinRequestBuilder); + executor.run(); + return executor.getHits(); + } + + public static Aggregations executeAggregationAction(AggregationQueryAction aggregationQueryAction) throws SqlParseException { + SqlElasticSearchRequestBuilder select = aggregationQueryAction.explain(); + return ((SearchResponse)select.get()).getAggregations(); + } + + public static ActionResponse executeDeleteAction(DeleteQueryAction deleteQueryAction) throws SqlParseException { + return deleteQueryAction.explain().get(); + } + + public static Object executeAnyAction(Client client , QueryAction queryAction) throws SqlParseException, IOException { + if(queryAction instanceof DefaultQueryAction) + return executeSearchAction((DefaultQueryAction) queryAction); + if(queryAction instanceof AggregationQueryAction) + return executeAggregationAction((AggregationQueryAction) queryAction); + if(queryAction instanceof ESJoinQueryAction) + return executeJoinSearchAction(client, (ESJoinQueryAction) queryAction); + if(queryAction instanceof DeleteQueryAction ) + return executeDeleteAction((DeleteQueryAction) queryAction); + return null; + } + + +} diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java b/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java index a1057be5..8f95709f 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/RestSqlAction.java @@ -1,7 +1,6 @@ package org.elasticsearch.plugin.nlpcn; import org.elasticsearch.action.ActionRequest; -import org.elasticsearch.action.ActionRequestBuilder; import org.elasticsearch.client.Client; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; @@ -39,7 +38,7 @@ protected void handleRequest(RestRequest request, RestChannel channel, final Cli BytesRestResponse bytesRestResponse = new BytesRestResponse(RestStatus.OK, jsonExplanation); channel.sendResponse(bytesRestResponse); } else { - new ActionRequestExecuter(actionRequestBuilder, channel, client).execute(); + new ActionRequestRestExecuter(actionRequestBuilder, channel, client).execute(); } } } \ No newline at end of file diff --git a/src/main/java/org/nlpcn/es4sql/domain/Select.java b/src/main/java/org/nlpcn/es4sql/domain/Select.java index ee354da0..119c36ad 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/Select.java +++ b/src/main/java/org/nlpcn/es4sql/domain/Select.java @@ -1,6 +1,7 @@ package org.nlpcn.es4sql.domain; import org.nlpcn.es4sql.domain.hints.Hint; +import org.nlpcn.es4sql.parse.SubQueryExpression; import java.util.ArrayList; import java.util.Arrays; @@ -21,7 +22,8 @@ public class Select extends Query { private List orderBys = new ArrayList<>(); private int offset; private int rowCount = 200; - + private boolean containsSubQueries; + private List subQueries; public boolean isQuery = false; public boolean isAgg = false; @@ -93,5 +95,33 @@ public List getHints() { } + public void fillSubQueries() { + subQueries = new ArrayList<>(); + Where where = this.getWhere(); + fillSubQueriesFromWhereRecursive(where); + } + + private void fillSubQueriesFromWhereRecursive(Where where) { + if(where == null) return; + if(where instanceof Condition){ + Condition condition = (Condition) where; + if ( condition.getValue() instanceof SubQueryExpression){ + this.subQueries.add((SubQueryExpression) condition.getValue()); + this.containsSubQueries = true; + } + } + else { + for(Where innerWhere : where.getWheres()) + fillSubQueriesFromWhereRecursive(innerWhere); + } + } + + public boolean containsSubQueries() { + return containsSubQueries; + } + + public List getSubQueries() { + return subQueries; + } } diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index ec1e1584..e43becc4 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -31,27 +31,34 @@ public Select parseSelect(SQLQueryExpr mySqlExpr) throws SqlParseException { MySqlSelectQueryBlock query = (MySqlSelectQueryBlock) mySqlExpr.getSubQuery().getQuery(); - Select select = new Select(); + Select select = parseSelect(query); - findSelect(query, select,null); + return select; + } - select.getFrom().addAll(findFrom(query.getFrom())); + private Select parseSelect(MySqlSelectQueryBlock query) throws SqlParseException { + Select select = new Select(); - select.setWhere(findWhere(query.getWhere())); + findSelect(query, select,null); - select.getHints().addAll(parseHints(query.getHints())); + select.getFrom().addAll(findFrom(query.getFrom())); - findLimit(query.getLimit(), select); + select.setWhere(findWhere(query.getWhere())); - findOrderBy(query, select); + select.fillSubQueries(); - findGroupBy(query, select); + select.getHints().addAll(parseHints(query.getHints())); - return select; - } + findLimit(query.getLimit(), select); + + findOrderBy(query, select); + findGroupBy(query, select); + return select; + } - public Delete parseDelete(SQLDeleteStatement deleteStatement) throws SqlParseException { + + public Delete parseDelete(SQLDeleteStatement deleteStatement) throws SqlParseException { Delete delete = new Delete(); delete.getFrom().addAll(findFrom(deleteStatement.getTableSource())); @@ -134,7 +141,15 @@ else if (expr instanceof SQLMethodInvokeExpr) { Condition condition = new Condition(CONN.valueOf(opear), fieldName, methodName, spatialParamsObject); where.addWhere(condition); - }else { + } else if (expr instanceof SQLInSubQueryExpr){ + SQLInSubQueryExpr sqlIn = (SQLInSubQueryExpr) expr; + Select innerSelect = parseSelect((MySqlSelectQueryBlock) sqlIn.getSubQuery().getQuery()); + if(innerSelect.getFields() == null || innerSelect.getFields().size()!=1) + throw new SqlParseException("should only have one return field in subQuery"); + SubQueryExpression subQueryExpression = new SubQueryExpression(innerSelect); + Condition condition = new Condition(CONN.valueOf(opear), sqlIn.getExpr().toString(), sqlIn.isNot() ? "NOT IN" : "IN",subQueryExpression); + where.addWhere(condition); + } else { throw new SqlParseException("err find condition " + expr.getClass()); } } diff --git a/src/main/java/org/nlpcn/es4sql/parse/SubQueryExpression.java b/src/main/java/org/nlpcn/es4sql/parse/SubQueryExpression.java new file mode 100644 index 00000000..484242e2 --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/parse/SubQueryExpression.java @@ -0,0 +1,38 @@ +package org.nlpcn.es4sql.parse; + +import org.nlpcn.es4sql.domain.Select; + +/** + * Created by Eliran on 3/10/2015. + */ +public class SubQueryExpression { + private Object[] values; + private Select select; + private String returnField; + + public SubQueryExpression(Select innerSelect) { + this.select = innerSelect; + this.returnField = select.getFields().get(0).getName(); + values = null; + } + + public Object[] getValues() { + return values; + } + + public void setValues(Object[] values) { + this.values = values; + } + + public Select getSelect() { + return select; + } + + public void setSelect(Select select) { + this.select = select; + } + + public String getReturnField() { + return returnField; + } +} diff --git a/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java b/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java index 37c71dcc..93ee7a89 100644 --- a/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java +++ b/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java @@ -8,7 +8,16 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; import com.alibaba.druid.sql.parser.*; +import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; +import org.elasticsearch.cluster.routing.allocation.decider.Decision; +import org.elasticsearch.plugin.nlpcn.ElasticResultHandler; +import org.elasticsearch.plugin.nlpcn.QueryActionElasticExecutor; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchHits; +import org.elasticsearch.search.aggregations.Aggregation; +import org.elasticsearch.search.aggregations.Aggregations; +import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation; import org.nlpcn.es4sql.domain.Delete; import org.nlpcn.es4sql.domain.JoinSelect; import org.nlpcn.es4sql.domain.Select; @@ -16,9 +25,15 @@ import org.nlpcn.es4sql.parse.ElasticLexer; import org.nlpcn.es4sql.parse.ElasticSqlExprParser; import org.nlpcn.es4sql.parse.SqlParser; +import org.nlpcn.es4sql.parse.SubQueryExpression; import org.nlpcn.es4sql.query.join.ESJoinQueryActionFactory; +import java.io.IOException; import java.sql.SQLFeatureNotSupportedException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; public class ESActionFactory { @@ -41,11 +56,14 @@ public static QueryAction create(Client client, String sql) throws SqlParseExcep else { Select select = new SqlParser().parseSelect(sqlExpr); - if (select.isAgg) { - return new AggregationQueryAction(client, select); - } else { - return new DefaultQueryAction(client, select); + if (select.containsSubQueries()) + { + for(SubQueryExpression subQueryExpression : select.getSubQueries()){ + QueryAction queryAction = handleSelect(client, subQueryExpression.getSelect()); + executeAndFillSubQuery(client , subQueryExpression,queryAction); + } } + return handleSelect(client, select); } case "DELETE": SQLStatementParser parser = createSqlStatementParser(sql); @@ -58,6 +76,36 @@ public static QueryAction create(Client client, String sql) throws SqlParseExcep } } + private static void executeAndFillSubQuery(Client client , SubQueryExpression subQueryExpression,QueryAction queryAction) throws SqlParseException { + List values = new ArrayList<>(); + Object queryResult; + try { + queryResult = QueryActionElasticExecutor.executeAnyAction(client,queryAction); + } catch (Exception e) { + throw new SqlParseException("could not execute SubQuery: " + e.getMessage()); + } + + String returnField = subQueryExpression.getReturnField(); + if(queryResult instanceof SearchHits) { + SearchHits hits = (SearchHits) queryResult; + for (SearchHit hit : hits) { + values.add(ElasticResultHandler.getFieldValue(hit,returnField)); + } + } + else { + throw new SqlParseException("on sub queries only support queries that return Hits and not aggregations"); + } + subQueryExpression.setValues(values.toArray()); + } + + private static QueryAction handleSelect(Client client, Select select) { + if (select.isAgg) { + return new AggregationQueryAction(client, select); + } else { + return new DefaultQueryAction(client, select); + } + } + private static SQLStatementParser createSqlStatementParser(String sql) { ElasticLexer lexer = new ElasticLexer(sql); lexer.nextToken(); @@ -79,4 +127,7 @@ private static SQLExpr toSqlExpr(String sql) { return expr; } + + + } diff --git a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java index 8b67ee18..4f25cd54 100644 --- a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java +++ b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java @@ -18,6 +18,7 @@ import org.nlpcn.es4sql.exception.SqlParseException; +import org.nlpcn.es4sql.parse.SubQueryExpression; import org.nlpcn.es4sql.spatial.*; public abstract class Maker { @@ -39,16 +40,21 @@ protected Maker(Boolean isQuery) { */ protected ToXContent make(Condition cond) throws SqlParseException { - String name = cond.getName(); - Object value = cond.getValue(); + String name = cond.getName(); + Object value = cond.getValue(); - ToXContent x = null; - if (value instanceof SQLMethodInvokeExpr) { - x = make(cond, name, (SQLMethodInvokeExpr) value); - } else { + ToXContent x = null; + + if (value instanceof SQLMethodInvokeExpr) { + x = make(cond, name, (SQLMethodInvokeExpr) value); + } + else if (value instanceof SubQueryExpression){ + x = make(cond,name,((SubQueryExpression)value).getValues()); + } else { x = make(cond, name, value); } + return x; } @@ -196,6 +202,7 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar break; case NIN: case IN: + //todo: value is subquery? here or before Object[] values = (Object[]) value; MatchQueryBuilder[] matchQueries = new MatchQueryBuilder[values.length]; for(int i = 0; i < values.length; i++) { diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java index a0db5823..e7ba25a2 100644 --- a/src/test/java/org/nlpcn/es4sql/QueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java @@ -1,5 +1,7 @@ package org.nlpcn.es4sql; +import com.alibaba.druid.sql.ast.SQLExpr; +import com.alibaba.druid.sql.ast.expr.SQLQueryExpr; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchResponse; @@ -10,6 +12,7 @@ import org.elasticsearch.search.SearchHits; import org.junit.Assert; import org.junit.Test; +import org.nlpcn.es4sql.domain.Select; import org.nlpcn.es4sql.exception.SqlParseException; import org.nlpcn.es4sql.query.SqlElasticSearchRequestBuilder; @@ -649,6 +652,31 @@ public void useScrollWithParams() throws IOException, SqlParseException, SQLFeat Assert.assertEquals(1000,hits.getTotalHits()); } + @Test + public void innerQueryTest() throws SqlParseException, SQLFeatureNotSupportedException { + String query = String.format("select * from %s/dog where holdersName IN (select firstname from %s/account where firstname = 'Hattie')",TEST_INDEX,TEST_INDEX); + SearchHit[] hits = query(query).getHits(); + Assert.assertEquals(1,hits.length); + Map hitAsMap = hits[0].sourceAsMap(); + Assert.assertEquals("snoopy",hitAsMap.get("name")); + Assert.assertEquals("Hattie",hitAsMap.get("holdersName")); + Assert.assertEquals(4,hitAsMap.get("age")); + + } + + @Test + public void twoSubQueriesTest() throws SqlParseException, SQLFeatureNotSupportedException { + String query = String.format("select * from %s/dog where holdersName IN (select firstname from %s/account where firstname = 'eliran') and age IN (select name.ofHisName from %s/gotCharacters where name.firstname <> 'Daenerys') ",TEST_INDEX,TEST_INDEX,TEST_INDEX); + SearchHit[] hits = query(query).getHits(); + Assert.assertEquals(1,hits.length); + Map hitAsMap = hits[0].sourceAsMap(); + Assert.assertEquals("snoopy",hitAsMap.get("name")); + Assert.assertEquals("Hattie",hitAsMap.get("holdersName")); + Assert.assertEquals(4,hitAsMap.get("age")); + + } + + private SearchHits query(String query) throws SqlParseException, SQLFeatureNotSupportedException, SQLFeatureNotSupportedException { SearchDao searchDao = MainTestSuite.getSearchDao(); SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) searchDao.explain(query); @@ -660,4 +688,5 @@ private SearchResponse getSearchResponse(String query) throws SqlParseException, SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) searchDao.explain(query); return ((SearchResponse)select.get()); } + } diff --git a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java index 16cd25fb..9957f001 100644 --- a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java +++ b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java @@ -31,8 +31,6 @@ public static void init(){ parser = new SqlParser(); } - - @Test public void joinParseCheckSelectedFieldsSplit() throws SqlParseException { String query = "SELECT a.firstname ,a.lastname , a.gender , d.holdersName ,d.name FROM elasticsearch-sql_test_index/account a " + @@ -368,6 +366,26 @@ public void fieldIsNull() throws SqlParseException { Assert.assertNull(condition.getValue()); } + @Test + public void innerQueryTest() throws SqlParseException { + String query = String.format("select * from %s/dog where holdersName IN (select firstname from %s/account where firstname = 'eliran')",TEST_INDEX,TEST_INDEX); + SQLExpr sqlExpr = queryToExpr(query); + Select select = parser.parseSelect((SQLQueryExpr) sqlExpr); + Assert.assertTrue(select.containsSubQueries()); + Assert.assertEquals(1,select.getSubQueries().size()); + } + + @Test + public void innerQueryTestTwoQueries() throws SqlParseException { + String query = String.format("select * from %s/dog where holdersName IN (select firstname from %s/account where firstname = 'eliran') and age IN (select name.ofHisName from %s/gotCharacters) ",TEST_INDEX,TEST_INDEX,TEST_INDEX); + SQLExpr sqlExpr = queryToExpr(query); + Select select = parser.parseSelect((SQLQueryExpr) sqlExpr); + Assert.assertTrue(select.containsSubQueries()); + Assert.assertEquals(2,select.getSubQueries().size()); + } + + + private SQLExpr queryToExpr(String query) { return new ElasticSqlExprParser(query).expr(); } From d8ceb4b87c4e28b814bff095766a7c2ba11e8f38 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sun, 4 Oct 2015 13:52:30 +0300 Subject: [PATCH 084/559] subquery with terms support --- .../org/nlpcn/es4sql/domain/Condition.java | 8 +++-- .../org/nlpcn/es4sql/parse/SqlParser.java | 31 +++++++++++++++++-- .../org/nlpcn/es4sql/query/maker/Maker.java | 25 ++++++++------- src/test/java/org/nlpcn/es4sql/QueryTest.java | 14 ++++++++- .../java/org/nlpcn/es4sql/SqlParserTests.java | 10 ++++++ 5 files changed, 71 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/nlpcn/es4sql/domain/Condition.java b/src/main/java/org/nlpcn/es4sql/domain/Condition.java index 1edd712d..eda2f2fd 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/Condition.java +++ b/src/main/java/org/nlpcn/es4sql/domain/Condition.java @@ -1,9 +1,11 @@ package org.nlpcn.es4sql.domain; import java.util.Arrays; +import java.util.Map; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; +import org.elasticsearch.common.collect.ImmutableMap; import org.nlpcn.es4sql.exception.SqlParseException; /** @@ -14,9 +16,11 @@ public class Condition extends Where { public enum OPEAR { - EQ, GT, LT, GTE, LTE, N, LIKE, NLIKE, IS, ISN, IN, NIN , BETWEEN ,NBETWEEN , GEO_INTERSECTS , GEO_BOUNDING_BOX , GEO_DISTANCE , GEO_DISTANCE_RANGE, GEO_POLYGON , GEO_CELL; + EQ, GT, LT, GTE, LTE, N, LIKE, NLIKE, IS, ISN, IN, NIN , BETWEEN ,NBETWEEN , GEO_INTERSECTS , GEO_BOUNDING_BOX , GEO_DISTANCE , GEO_DISTANCE_RANGE, GEO_POLYGON , GEO_CELL,IN_TERMS; + + public static Map methodNameToOpear = ImmutableMap.of("in_terms",IN_TERMS,"terms",IN_TERMS); + private static BiMap negatives; - private static BiMap negatives; static { negatives = HashBiMap.create(7); diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index e43becc4..1714a362 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -119,8 +119,22 @@ private void routeCond(SQLBinaryOpExpr bExpr, SQLExpr sub, Where where) throws S private void explanCond(String opear, SQLExpr expr, Where where) throws SqlParseException { if (expr instanceof SQLBinaryOpExpr) { SQLBinaryOpExpr soExpr = (SQLBinaryOpExpr) expr; - Condition condition = new Condition(CONN.valueOf(opear), soExpr.getLeft().toString(), soExpr.getOperator().name, parseValue(soExpr.getRight())); - where.addWhere(condition); + boolean methodAsOpear = false; + if(soExpr.getRight() instanceof SQLMethodInvokeExpr){ + SQLMethodInvokeExpr method = (SQLMethodInvokeExpr) soExpr.getRight(); + String methodName = method.getMethodName().toLowerCase(); + + if(Condition.OPEAR.methodNameToOpear.containsKey(methodName)){ + Object methodParametersValue = getMethodValuesWithSubQueries(method); + Condition condition = new Condition(CONN.valueOf(opear) ,soExpr.getLeft().toString(), Condition.OPEAR.methodNameToOpear.get(methodName),methodParametersValue); + where.addWhere(condition); + methodAsOpear = true; + } + } + if(!methodAsOpear){ + Condition condition = new Condition(CONN.valueOf(opear), soExpr.getLeft().toString(), soExpr.getOperator().name, parseValue(soExpr.getRight())); + where.addWhere(condition); + } } else if (expr instanceof SQLInListExpr) { SQLInListExpr siExpr = (SQLInListExpr) expr; Condition condition = new Condition(CONN.valueOf(opear), siExpr.getExpr().toString(), siExpr.isNot() ? "NOT IN" : "IN", parseValue(siExpr.getTargetList())); @@ -154,7 +168,18 @@ else if (expr instanceof SQLMethodInvokeExpr) { } } - private Object[] parseValue(List targetList) throws SqlParseException { + private Object getMethodValuesWithSubQueries(SQLMethodInvokeExpr method) throws SqlParseException { + List values = new ArrayList<>(); + boolean foundSubQuery = false; + if(method.getParameters().size() == 1 && method.getParameters().get(0) instanceof SQLQueryExpr){ + SQLQueryExpr sqlSubQuery = (SQLQueryExpr) method.getParameters().get(0); + Select select = parseSelect((MySqlSelectQueryBlock) sqlSubQuery.getSubQuery().getQuery()); + return new SubQueryExpression(select); + } + return method.getParameters().toArray(); + } + + private Object[] parseValue(List targetList) throws SqlParseException { Object[] value = new Object[targetList.size()]; for (int i = 0; i < targetList.size(); i++) { value[i] = parseValue(targetList.get(i)); diff --git a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java index 4f25cd54..55d3ab95 100644 --- a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java +++ b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java @@ -122,17 +122,6 @@ private ToXContent make(Condition cond, String name, SQLMethodInvokeExpr value) else { bqb = FilterBuilders.termFilter(name,value.getParameters().get(0)); } - break; - case "in_terms": - case "interms": - case "terms": - Object[] values = value.getParameters().toArray(); - if(isQuery){ - bqb =QueryBuilders.termsQuery(name,values); - } - else { - bqb = FilterBuilders.termsFilter(name,values); - } break; default: throw new SqlParseException("it did not support this query method " + value.getMethodName()); @@ -286,6 +275,20 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar Point geoHashPoint = cellFilterParams.getGeohashPoint(); x = FilterBuilders.geoHashCellFilter(cond.getName()).point(geoHashPoint.getLat(),geoHashPoint.getLon()).precision(cellFilterParams.getPrecision()).neighbors(cellFilterParams.isNeighbors()); break; + case IN_TERMS: + Object[] termValues; + if(value instanceof SubQueryExpression) + termValues = ((SubQueryExpression) value).getValues(); + else { + termValues = (Object[]) value; + } + if(isQuery){ + x = QueryBuilders.termsQuery(name,termValues); + } + else { + x = FilterBuilders.termsFilter(name,termValues); + } + break; default: throw new SqlParseException("not define type " + cond.getName()); } diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java index e7ba25a2..21eb67fa 100644 --- a/src/test/java/org/nlpcn/es4sql/QueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java @@ -666,7 +666,7 @@ public void innerQueryTest() throws SqlParseException, SQLFeatureNotSupportedExc @Test public void twoSubQueriesTest() throws SqlParseException, SQLFeatureNotSupportedException { - String query = String.format("select * from %s/dog where holdersName IN (select firstname from %s/account where firstname = 'eliran') and age IN (select name.ofHisName from %s/gotCharacters where name.firstname <> 'Daenerys') ",TEST_INDEX,TEST_INDEX,TEST_INDEX); + String query = String.format("select * from %s/dog where holdersName IN (select firstname from %s/account where firstname = 'Hattie') and age IN (select name.ofHisName from %s/gotCharacters where name.firstname <> 'Daenerys') ",TEST_INDEX,TEST_INDEX,TEST_INDEX); SearchHit[] hits = query(query).getHits(); Assert.assertEquals(1,hits.length); Map hitAsMap = hits[0].sourceAsMap(); @@ -676,6 +676,18 @@ public void twoSubQueriesTest() throws SqlParseException, SQLFeatureNotSupported } + @Test + public void inTermsSubQueryTest() throws SqlParseException, SQLFeatureNotSupportedException { + String query = String.format("select * from %s/dog where age = IN_TERMS (select name.ofHisName from %s/gotCharacters where name.firstname <> 'Daenerys')",TEST_INDEX,TEST_INDEX); + SearchHit[] hits = query(query).getHits(); + Assert.assertEquals(1,hits.length); + Map hitAsMap = hits[0].sourceAsMap(); + Assert.assertEquals("snoopy",hitAsMap.get("name")); + Assert.assertEquals("Hattie",hitAsMap.get("holdersName")); + Assert.assertEquals(4, hitAsMap.get("age")); + + } + private SearchHits query(String query) throws SqlParseException, SQLFeatureNotSupportedException, SQLFeatureNotSupportedException { SearchDao searchDao = MainTestSuite.getSearchDao(); diff --git a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java index 9957f001..2620b4d6 100644 --- a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java +++ b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java @@ -375,6 +375,16 @@ public void innerQueryTest() throws SqlParseException { Assert.assertEquals(1,select.getSubQueries().size()); } + @Test + public void inTermsSubQueryTest() throws SqlParseException { + String query = String.format("select * from %s/dog where holdersName = IN_TERMS (select firstname from %s/account where firstname = 'eliran')",TEST_INDEX,TEST_INDEX); + SQLExpr sqlExpr = queryToExpr(query); + Select select = parser.parseSelect((SQLQueryExpr) sqlExpr); + Assert.assertTrue(select.containsSubQueries()); + Assert.assertEquals(1,select.getSubQueries().size()); + } + + @Test public void innerQueryTestTwoQueries() throws SqlParseException { String query = String.format("select * from %s/dog where holdersName IN (select firstname from %s/account where firstname = 'eliran') and age IN (select name.ofHisName from %s/gotCharacters) ",TEST_INDEX,TEST_INDEX,TEST_INDEX); From edc26439da22ab1f4c0d85a51289aecb7bb60482 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sun, 4 Oct 2015 17:55:00 +0300 Subject: [PATCH 085/559] ids filter/query support --- .../org/nlpcn/es4sql/domain/Condition.java | 4 +- .../java/org/nlpcn/es4sql/domain/Select.java | 10 +++++ .../org/nlpcn/es4sql/parse/SqlParser.java | 25 +++++++++---- .../org/nlpcn/es4sql/query/maker/Maker.java | 37 ++++++++++++++++--- src/test/java/org/nlpcn/es4sql/QueryTest.java | 37 +++++++++++++++++++ 5 files changed, 98 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/nlpcn/es4sql/domain/Condition.java b/src/main/java/org/nlpcn/es4sql/domain/Condition.java index eda2f2fd..e4027b31 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/Condition.java +++ b/src/main/java/org/nlpcn/es4sql/domain/Condition.java @@ -16,9 +16,9 @@ public class Condition extends Where { public enum OPEAR { - EQ, GT, LT, GTE, LTE, N, LIKE, NLIKE, IS, ISN, IN, NIN , BETWEEN ,NBETWEEN , GEO_INTERSECTS , GEO_BOUNDING_BOX , GEO_DISTANCE , GEO_DISTANCE_RANGE, GEO_POLYGON , GEO_CELL,IN_TERMS; + EQ, GT, LT, GTE, LTE, N, LIKE, NLIKE, IS, ISN, IN, NIN , BETWEEN ,NBETWEEN , GEO_INTERSECTS , GEO_BOUNDING_BOX , GEO_DISTANCE , GEO_DISTANCE_RANGE, GEO_POLYGON , GEO_CELL, IN_TERMS , IDS_QUERY; - public static Map methodNameToOpear = ImmutableMap.of("in_terms",IN_TERMS,"terms",IN_TERMS); + public static Map methodNameToOpear = ImmutableMap.of("in_terms",IN_TERMS,"terms",IN_TERMS,"ids",IDS_QUERY,"ids_query",IDS_QUERY); private static BiMap negatives; diff --git a/src/main/java/org/nlpcn/es4sql/domain/Select.java b/src/main/java/org/nlpcn/es4sql/domain/Select.java index 119c36ad..a373017f 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/Select.java +++ b/src/main/java/org/nlpcn/es4sql/domain/Select.java @@ -3,6 +3,7 @@ import org.nlpcn.es4sql.domain.hints.Hint; import org.nlpcn.es4sql.parse.SubQueryExpression; +import java.sql.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -109,6 +110,15 @@ private void fillSubQueriesFromWhereRecursive(Where where) { this.subQueries.add((SubQueryExpression) condition.getValue()); this.containsSubQueries = true; } + if(condition.getValue() instanceof Object[]){ + + for(Object o : (Object[]) condition.getValue()){ + if ( o instanceof SubQueryExpression){ + this.subQueries.add((SubQueryExpression) o); + this.containsSubQueries = true; + } + } + } } else { for(Where innerWhere : where.getWheres()) diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index 1714a362..0cc752e4 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -125,7 +125,7 @@ private void explanCond(String opear, SQLExpr expr, Where where) throws SqlParse String methodName = method.getMethodName().toLowerCase(); if(Condition.OPEAR.methodNameToOpear.containsKey(methodName)){ - Object methodParametersValue = getMethodValuesWithSubQueries(method); + Object[] methodParametersValue = getMethodValuesWithSubQueries(method); Condition condition = new Condition(CONN.valueOf(opear) ,soExpr.getLeft().toString(), Condition.OPEAR.methodNameToOpear.get(methodName),methodParametersValue); where.addWhere(condition); methodAsOpear = true; @@ -168,15 +168,26 @@ else if (expr instanceof SQLMethodInvokeExpr) { } } - private Object getMethodValuesWithSubQueries(SQLMethodInvokeExpr method) throws SqlParseException { + private Object[] getMethodValuesWithSubQueries(SQLMethodInvokeExpr method) throws SqlParseException { List values = new ArrayList<>(); boolean foundSubQuery = false; - if(method.getParameters().size() == 1 && method.getParameters().get(0) instanceof SQLQueryExpr){ - SQLQueryExpr sqlSubQuery = (SQLQueryExpr) method.getParameters().get(0); - Select select = parseSelect((MySqlSelectQueryBlock) sqlSubQuery.getSubQuery().getQuery()); - return new SubQueryExpression(select); + for(SQLExpr innerExpr : method.getParameters()){ + if(innerExpr instanceof SQLQueryExpr){ + foundSubQuery = true; + Select select = parseSelect((MySqlSelectQueryBlock) ((SQLQueryExpr) innerExpr).getSubQuery().getQuery()); + values.add(new SubQueryExpression(select)); + } + else { + values.add(innerExpr); + } + } - return method.getParameters().toArray(); + Object[] conditionValues ; + if(foundSubQuery) + conditionValues = values.toArray(); + else + conditionValues = method.getParameters().toArray(); + return conditionValues; } private Object[] parseValue(List targetList) throws SqlParseException { diff --git a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java index 55d3ab95..9a98ea79 100644 --- a/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java +++ b/src/main/java/org/nlpcn/es4sql/query/maker/Maker.java @@ -276,12 +276,9 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar x = FilterBuilders.geoHashCellFilter(cond.getName()).point(geoHashPoint.getLat(),geoHashPoint.getLon()).precision(cellFilterParams.getPrecision()).neighbors(cellFilterParams.isNeighbors()); break; case IN_TERMS: - Object[] termValues; - if(value instanceof SubQueryExpression) - termValues = ((SubQueryExpression) value).getValues(); - else { - termValues = (Object[]) value; - } + Object[] termValues = (Object[]) value; + if(termValues.length == 1 && termValues[0] instanceof SubQueryExpression) + termValues = ((SubQueryExpression) termValues[0]).getValues(); if(isQuery){ x = QueryBuilders.termsQuery(name,termValues); } @@ -289,6 +286,24 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar x = FilterBuilders.termsFilter(name,termValues); } break; + case IDS_QUERY: + Object[] idsParameters = (Object[]) value; + String[] ids; + String type = idsParameters[0].toString(); + if(idsParameters.length ==2 && idsParameters[1] instanceof SubQueryExpression){ + Object[] idsFromSubQuery = ((SubQueryExpression) idsParameters[1]).getValues(); + ids = arrayOfObjectsToStringArray(idsFromSubQuery,0,idsFromSubQuery.length-1); + } + else { + ids =arrayOfObjectsToStringArray(idsParameters,1,idsParameters.length-1); + } + if(isQuery){ + x = QueryBuilders.idsQuery(type).addIds(ids); + } + else { + x = FilterBuilders.idsFilter(type).addIds(ids); + } + break; default: throw new SqlParseException("not define type " + cond.getName()); } @@ -297,6 +312,16 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar return x; } + private String[] arrayOfObjectsToStringArray(Object[] values, int from, int to) { + String[] strings = new String[to - from + 1]; + int counter =0; + for(int i = from ;i<=to;i++){ + strings[counter] = values[i].toString(); + counter++; + } + return strings; + } + private ShapeBuilder getShapeBuilderFromString(String str) throws IOException { String json; if(str.contains("{")) json = fixJsonFromElastic(str); diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java index 21eb67fa..c636d4cb 100644 --- a/src/test/java/org/nlpcn/es4sql/QueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java @@ -688,6 +688,43 @@ public void inTermsSubQueryTest() throws SqlParseException, SQLFeatureNotSupport } + @Test + public void idsQueryOneId() throws SqlParseException, SQLFeatureNotSupportedException { + String query = String.format("select * from %s/dog where _id = IDS_QUERY(dog,1)",TEST_INDEX,TEST_INDEX); + SearchHit[] hits = query(query).getHits(); + Assert.assertEquals(1,hits.length); + Map hitAsMap = hits[0].sourceAsMap(); + Assert.assertEquals("rex",hitAsMap.get("name")); + Assert.assertEquals("Daenerys",hitAsMap.get("holdersName")); + Assert.assertEquals(2, hitAsMap.get("age")); + + } + + @Test + public void idsQueryMultipleId() throws SqlParseException, SQLFeatureNotSupportedException { + String query = String.format("select * from %s/dog where _id = IDS_QUERY(dog,1,2,3)",TEST_INDEX,TEST_INDEX); + SearchHit[] hits = query(query).getHits(); + Assert.assertEquals(1,hits.length); + Map hitAsMap = hits[0].sourceAsMap(); + Assert.assertEquals("rex",hitAsMap.get("name")); + Assert.assertEquals("Daenerys",hitAsMap.get("holdersName")); + Assert.assertEquals(2, hitAsMap.get("age")); + + } + + @Test + public void idsQuerySubQueryIds() throws SqlParseException, SQLFeatureNotSupportedException { + String query = String.format("select * from %s/dog where _id = IDS_QUERY(dog,(select name.ofHisName from %s/gotCharacters where name.firstname <> 'Daenerys'))",TEST_INDEX,TEST_INDEX); + SearchHit[] hits = query(query).getHits(); + Assert.assertEquals(1,hits.length); + Map hitAsMap = hits[0].sourceAsMap(); + Assert.assertEquals("rex",hitAsMap.get("name")); + Assert.assertEquals("Daenerys",hitAsMap.get("holdersName")); + Assert.assertEquals(2, hitAsMap.get("age")); + + } + + private SearchHits query(String query) throws SqlParseException, SQLFeatureNotSupportedException, SQLFeatureNotSupportedException { SearchDao searchDao = MainTestSuite.getSearchDao(); From 8d2ed11c0614c6aac4e3ebf7c2e76c3b703cacb6 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sun, 4 Oct 2015 18:11:46 +0300 Subject: [PATCH 086/559] new version --- README.md | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 45629a19..aefdf328 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Install as plugin: ### Elasticsearch 1.6.X ```` -./bin/plugin -u https://github.com/NLPchina/elasticsearch-sql/releases/download/1.4/elasticsearch-sql-1.4.zip --install sql +./bin/plugin -u https://github.com/NLPchina/elasticsearch-sql/releases/download/1.4.1/elasticsearch-sql-1.4.1.zip --install sql ```` After doing this, you need to restart the Elasticsearch server. Otherwise you may get errors like `Invalid index name [sql], must not start with '']; ","status":400}`. diff --git a/pom.xml b/pom.xml index 6ff1bc0c..bd2929c1 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.nlpcn elasticsearch-sql - 1.4 + 1.4.1 jar Query elasticsearch using SQL elasticsearch-sql From a5d7b1e875b9827bc99b57a9cd69624e061ebef0 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Mon, 5 Oct 2015 11:50:04 +0300 Subject: [PATCH 087/559] #80 bugfixes - dotSupport and newLine support. 1.4.2 --- README.md | 2 +- pom.xml | 2 +- .../java/org/nlpcn/es4sql/parse/ElasticLexer.java | 2 +- src/main/java/org/nlpcn/es4sql/parse/SqlParser.java | 4 ++-- .../java/org/nlpcn/es4sql/query/ESActionFactory.java | 3 ++- src/test/java/org/nlpcn/es4sql/SqlParserTests.java | 12 +++++++++--- 6 files changed, 16 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index aefdf328..9908b6f6 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Install as plugin: ### Elasticsearch 1.6.X ```` -./bin/plugin -u https://github.com/NLPchina/elasticsearch-sql/releases/download/1.4.1/elasticsearch-sql-1.4.1.zip --install sql +./bin/plugin -u https://github.com/NLPchina/elasticsearch-sql/releases/download/1.4.2/elasticsearch-sql-1.4.2.zip --install sql ```` After doing this, you need to restart the Elasticsearch server. Otherwise you may get errors like `Invalid index name [sql], must not start with '']; ","status":400}`. diff --git a/pom.xml b/pom.xml index bd2929c1..22998f05 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.nlpcn elasticsearch-sql - 1.4.1 + 1.4.2 jar Query elasticsearch using SQL elasticsearch-sql diff --git a/src/main/java/org/nlpcn/es4sql/parse/ElasticLexer.java b/src/main/java/org/nlpcn/es4sql/parse/ElasticLexer.java index 24f0a75e..c281d7b2 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/ElasticLexer.java +++ b/src/main/java/org/nlpcn/es4sql/parse/ElasticLexer.java @@ -88,6 +88,6 @@ public void scanIdentifier() { private boolean isElasticIdentifierChar(char ch) { - return ch == '*' || ch == ':' || ch == '-' || isIdentifierChar(ch); + return ch == '*' || ch == ':' || ch == '-' || ch == '.' || isIdentifierChar(ch); } } diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index 0cc752e4..e85367cb 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -437,7 +437,7 @@ private List getConnectedFields(List conditions, String alias) fields.add(new Field(condition.getName().replaceFirst(prefix,""),null)); } else { - if(! ((condition.getValue() instanceof SQLPropertyExpr)||(condition.getValue() instanceof String))){ + if(! ((condition.getValue() instanceof SQLPropertyExpr)||(condition.getValue() instanceof SQLIdentifierExpr)||(condition.getValue() instanceof String))){ throw new SqlParseException("conditions on join should be one side is firstTable second Other , condition was:" + condition.toString()); } String aliasDotValue = condition.getValue().toString(); @@ -517,7 +517,7 @@ private void removeAliasPrefix(Where where, String alias) { private void addIfConditionRecursive(Where where, List conditions) throws SqlParseException { if(where instanceof Condition){ Condition cond = (Condition) where; - if( ! ((cond.getValue() instanceof SQLPropertyExpr)|| (cond.getValue() instanceof String))){ + if( ! ((cond.getValue() instanceof SQLIdentifierExpr) ||(cond.getValue() instanceof SQLPropertyExpr)|| (cond.getValue() instanceof String))){ throw new SqlParseException("conditions on join should be one side is secondTable OPEAR firstTable, condition was:" + cond.toString()); } conditions.add(cond); diff --git a/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java b/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java index 93ee7a89..442d4b70 100644 --- a/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java +++ b/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java @@ -45,7 +45,8 @@ public class ESActionFactory { * @return Query object. */ public static QueryAction create(Client client, String sql) throws SqlParseException, SQLFeatureNotSupportedException { - String firstWord = sql.substring(0, sql.indexOf(' ')); + sql = sql.replaceAll("\n"," "); + String firstWord = sql.substring(0, sql.indexOf(' ')); switch (firstWord.toUpperCase()) { case "SELECT": SQLQueryExpr sqlExpr = (SQLQueryExpr) toSqlExpr(sql); diff --git a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java index 2620b4d6..b4f67904 100644 --- a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java +++ b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java @@ -394,7 +394,14 @@ public void innerQueryTestTwoQueries() throws SqlParseException { Assert.assertEquals(2,select.getSubQueries().size()); } - + @Test + public void indexWithDotsAndHyphen() throws SqlParseException { + String query = "select * from data-2015.08.22"; + SQLExpr sqlExpr = queryToExpr(query); + Select select = parser.parseSelect((SQLQueryExpr) sqlExpr); + Assert.assertEquals(1,select.getFrom().size()); + Assert.assertEquals("data-2015.08.22",select.getFrom().get(0).getIndex()); + } private SQLExpr queryToExpr(String query) { return new ElasticSqlExprParser(query).expr(); @@ -410,8 +417,7 @@ private boolean conditionExist(List conditions, String from, String t boolean fromIsEqual = condition.getName().equals(from); if(!fromIsEqual) continue; - SQLPropertyExpr value = (SQLPropertyExpr) condition.getValue(); - String[] valueAliasAndField = value.toString().split("\\.",2); + String[] valueAliasAndField = condition.getValue().toString().split("\\.",2); boolean toFieldNameIsEqual = valueAliasAndField[1].equals(toField); boolean toAliasIsEqual = valueAliasAndField[0].equals(toAlias); boolean toIsEqual = toAliasIsEqual && toFieldNameIsEqual; From e91a263790f416804736fa6361f5b8a04fa5d4c8 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Mon, 5 Oct 2015 23:08:31 +0300 Subject: [PATCH 088/559] percentiles and extended_stats support #97 --- src/_site/query.js | 4 +++- src/main/java/org/nlpcn/es4sql/domain/Select.java | 2 +- src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java | 4 ++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/_site/query.js b/src/_site/query.js index 92bf01f6..1c733c42 100644 --- a/src/_site/query.js +++ b/src/_site/query.js @@ -113,7 +113,9 @@ var AggregationQueryResultHandler = function(data) { obj[field] = bucket[field].value } else { - continue; + if(typeof(bucket[field])=="object"){ + obj[field] = bucket[field]; + } } } rows.push(obj) diff --git a/src/main/java/org/nlpcn/es4sql/domain/Select.java b/src/main/java/org/nlpcn/es4sql/domain/Select.java index a373017f..a53cd7af 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/Select.java +++ b/src/main/java/org/nlpcn/es4sql/domain/Select.java @@ -16,7 +16,7 @@ public class Select extends Query { // Using this functions, will cause query to execute as aggregation. - private final List aggsFunctions = Arrays.asList("SUM", "MAX", "MIN", "AVG", "TOPHITS", "COUNT", "STATS"); + private final List aggsFunctions = Arrays.asList("SUM", "MAX", "MIN", "AVG", "TOPHITS", "COUNT", "STATS","EXTENDED_STATS","PERCENTILES"); private List hints = new ArrayList<>(); private List fields = new ArrayList<>(); private List> groupBys = new ArrayList<>(); diff --git a/src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java b/src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java index 5d0cb0b9..b1f6244a 100644 --- a/src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java +++ b/src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java @@ -68,6 +68,10 @@ public AbstractAggregationBuilder makeFieldAgg(MethodField field, AbstractAggreg return AggregationBuilders.avg(field.getAlias()).field(field.getParams().get(0).toString()); case "STATS": return AggregationBuilders.stats(field.getAlias()).field(field.getParams().get(0).toString()); + case "EXTENDED_STATS": + return AggregationBuilders.extendedStats(field.getAlias()).field(field.getParams().get(0).toString()); + case "PERCENTILES": + return AggregationBuilders.percentiles(field.getAlias()).field(field.getParams().get(0).toString()); case "TOPHITS": return makeTopHitsAgg(field); case "COUNT": From 9f5ecd88b9802b5123b40a6ccf9f66fcef3aaa87 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Mon, 5 Oct 2015 23:42:21 +0300 Subject: [PATCH 089/559] stats,extended_stats and percentiles show fix --- src/_site/query.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/_site/query.js b/src/_site/query.js index 1c733c42..344a039d 100644 --- a/src/_site/query.js +++ b/src/_site/query.js @@ -114,7 +114,7 @@ var AggregationQueryResultHandler = function(data) { } else { if(typeof(bucket[field])=="object"){ - obj[field] = bucket[field]; + fillFieldsForSpecificAggregation(obj,bucket[field],field); } } } @@ -125,6 +125,19 @@ var AggregationQueryResultHandler = function(data) { } + function fillFieldsForSpecificAggregation(obj,value,field) + { + for(key in value){ + if(key == "values"){ + fillFieldsForSpecificAggregation(obj,value[key],field); + } + else { + obj[field+"." +key] = value[key]; + } + } + return; + } + function getSubBuckets(bucket) { var subBuckets = []; for(var field in bucket) { From 5a062118de8b0943eaf2daece1e6a911d62c5271 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Mon, 5 Oct 2015 23:42:49 +0300 Subject: [PATCH 090/559] support lower case on aggregations --- src/main/java/org/nlpcn/es4sql/domain/Select.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/nlpcn/es4sql/domain/Select.java b/src/main/java/org/nlpcn/es4sql/domain/Select.java index a53cd7af..46f506f1 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/Select.java +++ b/src/main/java/org/nlpcn/es4sql/domain/Select.java @@ -84,7 +84,7 @@ public void addField(Field field) { return; } - if(field instanceof MethodField && aggsFunctions.contains(field.getName())) { + if(field instanceof MethodField && aggsFunctions.contains(field.getName().toUpperCase())) { isAgg = true; } From 0c42869910bbee5c7c45883149e405de1a640be5 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Tue, 6 Oct 2015 21:14:00 +0300 Subject: [PATCH 091/559] new version 1.4.3 --- README.md | 2 +- pom.xml | 2 +- .../org/nlpcn/es4sql/AggregationTest.java | 30 ++++++++++++++++++- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9908b6f6..d1c3cf94 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Install as plugin: ### Elasticsearch 1.6.X ```` -./bin/plugin -u https://github.com/NLPchina/elasticsearch-sql/releases/download/1.4.2/elasticsearch-sql-1.4.2.zip --install sql +./bin/plugin -u https://github.com/NLPchina/elasticsearch-sql/releases/download/1.4.3/elasticsearch-sql-1.4.3.zip --install sql ```` After doing this, you need to restart the Elasticsearch server. Otherwise you may get errors like `Invalid index name [sql], must not start with '']; ","status":400}`. diff --git a/pom.xml b/pom.xml index 22998f05..231006a0 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.nlpcn elasticsearch-sql - 1.4.2 + 1.4.3 jar Query elasticsearch using SQL elasticsearch-sql diff --git a/src/test/java/org/nlpcn/es4sql/AggregationTest.java b/src/test/java/org/nlpcn/es4sql/AggregationTest.java index 5034ee33..5dd59b5d 100644 --- a/src/test/java/org/nlpcn/es4sql/AggregationTest.java +++ b/src/test/java/org/nlpcn/es4sql/AggregationTest.java @@ -13,7 +13,9 @@ import org.elasticsearch.search.aggregations.metrics.avg.Avg; import org.elasticsearch.search.aggregations.metrics.max.Max; import org.elasticsearch.search.aggregations.metrics.min.Min; +import org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles; import org.elasticsearch.search.aggregations.metrics.stats.Stats; +import org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats; import org.elasticsearch.search.aggregations.metrics.sum.Sum; import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCount; import org.junit.Assert; @@ -77,7 +79,33 @@ public void statsTest() throws IOException, SqlParseException, SQLFeatureNotSupp assertThat(stats.getAvg(), equalTo(30.171)); } - @Test + @Test + public void extendedStatsTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { + Aggregations result = query(String.format("SELECT EXTENDED_STATS(age) FROM %s/account", TEST_INDEX)); + ExtendedStats stats = result.get("EXTENDED_STATS(age)"); + Assert.assertEquals(1000, stats.getCount()); + assertThat(stats.getMin(),equalTo(20.0)); + assertThat(stats.getMax(),equalTo(40.0)); + assertThat(stats.getAvg(),equalTo(30.171)); + assertThat(stats.getSum(),equalTo(30171.0)); + assertThat(stats.getSumOfSquares(),equalTo(946393.0)); + Assert.assertTrue(Math.abs(stats.getStdDeviation()- 6.008640362012022) < 0.0001); + Assert.assertTrue(Math.abs(stats.getVariance()- 36.10375899999996) < 0.0001); + } + + @Test + public void percentileTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { + Aggregations result = query(String.format("SELECT PERCENTILES(age) FROM %s/account", TEST_INDEX)); + Percentiles percentiles = result.get("PERCENTILES(age)"); + Assert.assertTrue(Math.abs(percentiles.percentile(1.0) - 20.0) < 0.001 ); + Assert.assertTrue(Math.abs(percentiles.percentile(5.0) - 21.0) < 0.001 ); + Assert.assertTrue(Math.abs(percentiles.percentile(25.0) - 25.0) < 0.001 ); + Assert.assertTrue(Math.abs(percentiles.percentile(75.0) - 35.0) < 0.001 ); + Assert.assertTrue(Math.abs(percentiles.percentile(95.0) - 39.0) < 0.001 ); + Assert.assertTrue(Math.abs(percentiles.percentile(99.0) - 40.0) < 0.001 ); + } + + @Test public void aliasTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { Aggregations result = query(String.format("SELECT COUNT(*) AS mycount FROM %s/account", TEST_INDEX)); assertThat(result.asMap(), hasKey("mycount")); From 24adbd7288a0692361682771081070fdde8a905d Mon Sep 17 00:00:00 2001 From: Caleb Ott Date: Mon, 12 Oct 2015 10:34:50 -0500 Subject: [PATCH 092/559] Fixed aggregations with single items in the group by. Added a test to match the missing functionality. Now "group by (state),(gender)" is supported again as two separate aggregations. Whereas "group by state,gender" still works as normal. --- .../es4sql/parse/ElasticSqlExprParser.java | 12 +++++- .../es4sql/parse/SQLParensIdentifierExpr.java | 39 +++++++++++++++++++ .../org/nlpcn/es4sql/parse/SqlParser.java | 20 +++++----- .../org/nlpcn/es4sql/AggregationTest.java | 27 +++++++++++++ 4 files changed, 86 insertions(+), 12 deletions(-) create mode 100644 src/main/java/org/nlpcn/es4sql/parse/SQLParensIdentifierExpr.java diff --git a/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java b/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java index 0b272ab4..28c7b02d 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/ElasticSqlExprParser.java @@ -104,6 +104,16 @@ else if (lexer.token() == Token.NOT) { } return sqlExpr; } - return super.primary(); + + boolean parenWrapped = lexer.token() == Token.LPAREN; + + SQLExpr expr = super.primary(); + + // keep track of if the identifier is wrapped in parens + if (parenWrapped && expr instanceof SQLIdentifierExpr) { + expr = new SQLParensIdentifierExpr((SQLIdentifierExpr) expr); + } + + return expr; } } diff --git a/src/main/java/org/nlpcn/es4sql/parse/SQLParensIdentifierExpr.java b/src/main/java/org/nlpcn/es4sql/parse/SQLParensIdentifierExpr.java new file mode 100644 index 00000000..56d33a02 --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/parse/SQLParensIdentifierExpr.java @@ -0,0 +1,39 @@ +package org.nlpcn.es4sql.parse; + +import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; + + +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * An Identifier that is wrapped in parenthesis. + * This is for tracking in group bys the difference between "group by state, age" and "group by (state), (age)". + * For non group by identifiers, it acts as a normal SQLIdentifierExpr. + */ +public class SQLParensIdentifierExpr extends SQLIdentifierExpr { + + public SQLParensIdentifierExpr() { + } + + public SQLParensIdentifierExpr(String name) { + super(name); + } + + public SQLParensIdentifierExpr(SQLIdentifierExpr expr) { + super(expr.getName()); + } +} diff --git a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java index e85367cb..d7a0d521 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java +++ b/src/main/java/org/nlpcn/es4sql/parse/SqlParser.java @@ -242,23 +242,21 @@ private void findGroupBy(MySqlSelectQueryBlock query, Select select) throws SqlP sqlExpr = sqlSelectGroupByExpr.getExpr(); } - if (!(sqlExpr instanceof SQLIdentifierExpr) && !standardGroupBys.isEmpty()) { - // flush the standard group bys - select.addGroupBy(convertExprsToFields(standardGroupBys)); - standardGroupBys = new ArrayList<>(); - } - - if (sqlExpr instanceof SQLIdentifierExpr) { - SQLIdentifierExpr identifierExpr = (SQLIdentifierExpr) sqlExpr; - // single item without parens (should latch to before or after list) - standardGroupBys.add(identifierExpr); + if ((sqlExpr instanceof SQLParensIdentifierExpr || !(sqlExpr instanceof SQLIdentifierExpr)) && !standardGroupBys.isEmpty()) { + // flush the standard group bys + select.addGroupBy(convertExprsToFields(standardGroupBys)); + standardGroupBys = new ArrayList<>(); + } + if (sqlExpr instanceof SQLParensIdentifierExpr) { + // single item with parens (should get its own aggregation) + select.addGroupBy(FieldMaker.makeField(sqlExpr, null,null)); } else if (sqlExpr instanceof SQLListExpr) { // multiple items in their own list SQLListExpr listExpr = (SQLListExpr) sqlExpr; select.addGroupBy(convertExprsToFields(listExpr.getItems())); } else { - // something else + // everything else gets added to the running list of standard group bys standardGroupBys.add(sqlExpr); } } diff --git a/src/test/java/org/nlpcn/es4sql/AggregationTest.java b/src/test/java/org/nlpcn/es4sql/AggregationTest.java index 5dd59b5d..cc4a0a34 100644 --- a/src/test/java/org/nlpcn/es4sql/AggregationTest.java +++ b/src/test/java/org/nlpcn/es4sql/AggregationTest.java @@ -303,6 +303,33 @@ public void testSubAggregations() throws Exception { Assert.assertEquals(response.getHits().hits().length, 10); } + @Test + public void testSimpleSubAggregations() throws Exception { + final String query = String.format("SELECT * FROM %s/account GROUP BY (gender), (state) LIMIT 0,10", TEST_INDEX); + + SearchDao searchDao = MainTestSuite.getSearchDao(); + SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) searchDao.explain(query); + SearchResponse response = (SearchResponse) select.get(); + Aggregations result = response.getAggregations(); + + Terms gender = result.get("gender"); + for(Terms.Bucket genderBucket : gender.getBuckets()) { + String genderKey = genderBucket.getKey(); + Assert.assertTrue("Gender should be m or f", genderKey.equals("m") || genderKey.equals("f")); + } + + Assert.assertEquals(2, gender.getBuckets().size()); + + Terms state = result.get("state"); + for(Terms.Bucket stateBucket : state.getBuckets()) { + if(stateBucket.getKey().equalsIgnoreCase("ak")) { + Assert.assertTrue("There are 22 entries for state ak", stateBucket.getDocCount() == 22); + } + } + + Assert.assertEquals(response.getHits().totalHits(), 1000); + Assert.assertEquals(response.getHits().hits().length, 10); + } @Test public void geoHashGrid() throws SQLFeatureNotSupportedException, SqlParseException { From 7657a08063f8856de8415341d5f2112f3faff783 Mon Sep 17 00:00:00 2001 From: Caleb Ott Date: Thu, 15 Oct 2015 15:47:20 -0500 Subject: [PATCH 093/559] Fixed a bug where not's would not evaulate properly. Removed the loop to go to the last where if the where's only had a single sub where. This breaks the NOT where because its operator is important. The test shows that if the where's are skipped to the last where the AND (must) changes to a OR (should), which changes the result. Added a test for the use case and updated the other test the change affected. --- .../nlpcn/es4sql/query/maker/FilterMaker.java | 3 --- src/test/java/org/nlpcn/es4sql/QueryTest.java | 25 ++++++++++++++++--- .../search_spatial_explain.json | 22 +++++++++------- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/nlpcn/es4sql/query/maker/FilterMaker.java b/src/main/java/org/nlpcn/es4sql/query/maker/FilterMaker.java index f04e77b8..dea911b2 100644 --- a/src/main/java/org/nlpcn/es4sql/query/maker/FilterMaker.java +++ b/src/main/java/org/nlpcn/es4sql/query/maker/FilterMaker.java @@ -28,9 +28,6 @@ private FilterMaker() { } private void explanWhere(BoolFilterBuilder boolFilter, Where where) throws SqlParseException { - while (where.getWheres().size() == 1) { - where = where.getWheres().getFirst(); - } if (where instanceof Condition) { addSubFilter(boolFilter, where, (BaseFilterBuilder) make((Condition) where)); } else { diff --git a/src/test/java/org/nlpcn/es4sql/QueryTest.java b/src/test/java/org/nlpcn/es4sql/QueryTest.java index c636d4cb..09ab0c78 100644 --- a/src/test/java/org/nlpcn/es4sql/QueryTest.java +++ b/src/test/java/org/nlpcn/es4sql/QueryTest.java @@ -219,6 +219,25 @@ public void notLikeTest() throws IOException, SqlParseException, SQLFeatureNotSu } } + @Test + public void doubleNotTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { + SearchHits response1 = query(String.format("SELECT * FROM %s/account WHERE not gender like 'm' and not gender like 'f'", TEST_INDEX)); + Assert.assertEquals(0, response1.getTotalHits()); + + SearchHits response2 = query(String.format("SELECT * FROM %s/account WHERE not gender like 'm' and gender not like 'f'", TEST_INDEX)); + Assert.assertEquals(0, response2.getTotalHits()); + + SearchHits response3 = query(String.format("SELECT * FROM %s/account WHERE gender not like 'm' and gender not like 'f'", TEST_INDEX)); + Assert.assertEquals(0, response3.getTotalHits()); + + SearchHits response4 = query(String.format("SELECT * FROM %s/account WHERE gender like 'm' and not gender like 'f'", TEST_INDEX)); + // assert there are results and they all have gender 'm' + Assert.assertNotEquals(0, response4.getTotalHits()); + for (SearchHit hit : response4.getHits()) { + Assert.assertEquals("m", hit.getSource().get("gender").toString().toLowerCase()); + } + } + @Test public void limitTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { SearchHits response = query(String.format("SELECT * FROM %s LIMIT 30", TEST_INDEX)); @@ -346,8 +365,8 @@ public void notInTest() throws IOException, SqlParseException, SQLFeatureNotSupp } } } - - + + @Test public void dateSearch() throws IOException, SqlParseException, SQLFeatureNotSupportedException, ParseException { DateTimeFormatter formatter = DateTimeFormat.forPattern(DATE_FORMAT); @@ -429,7 +448,7 @@ public void notMissFilterSearch() throws IOException, SqlParseException, SQLFeat assertThat(hit.getSource(), hasKey("insert_time")); } } - + @Test diff --git a/src/test/resources/expectedOutput/search_spatial_explain.json b/src/test/resources/expectedOutput/search_spatial_explain.json index a71a703d..6b732c6e 100644 --- a/src/test/resources/expectedOutput/search_spatial_explain.json +++ b/src/test/resources/expectedOutput/search_spatial_explain.json @@ -6,15 +6,19 @@ "filter" : { "bool" : { "must" : { - "geo_shape" : { - "place" : { - "shape" : { - "type" : "polygon", - "coordinates" : [ [ [ 102.0, 2.0 ], [ 103.0, 2.0 ], [ 103.0, 3.0 ], [ 102.0, 3.0 ], [ 102.0, 2.0 ] ] ] - }, - "relation" : "intersects" - }, - "_name" : null + "bool" : { + "must" : { + "geo_shape" : { + "place" : { + "shape" : { + "type" : "polygon", + "coordinates" : [ [ [ 102.0, 2.0 ], [ 103.0, 2.0 ], [ 103.0, 3.0 ], [ 102.0, 3.0 ], [ 102.0, 2.0 ] ] ] + }, + "relation" : "intersects" + }, + "_name" : null + } + } } } } From 13a2fce158491dfd47a9ab1d31d8b896a6e4584b Mon Sep 17 00:00:00 2001 From: Caleb Ott Date: Fri, 16 Oct 2015 07:55:54 -0500 Subject: [PATCH 094/559] Re-added the logic to move to the last where - except put it only on the first where so it clears out the excess bool -> must -> bool -> must, but doesn't cause problems later. --- .../nlpcn/es4sql/query/maker/FilterMaker.java | 3 +++ .../search_spatial_explain.json | 22 ++++++++----------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/nlpcn/es4sql/query/maker/FilterMaker.java b/src/main/java/org/nlpcn/es4sql/query/maker/FilterMaker.java index dea911b2..3915cb85 100644 --- a/src/main/java/org/nlpcn/es4sql/query/maker/FilterMaker.java +++ b/src/main/java/org/nlpcn/es4sql/query/maker/FilterMaker.java @@ -19,6 +19,9 @@ public class FilterMaker extends Maker { */ public static BoolFilterBuilder explan(Where where) throws SqlParseException { BoolFilterBuilder boolFilter = FilterBuilders.boolFilter(); + while (where.getWheres().size() == 1) { + where = where.getWheres().getFirst(); + } new FilterMaker().explanWhere(boolFilter, where); return boolFilter; } diff --git a/src/test/resources/expectedOutput/search_spatial_explain.json b/src/test/resources/expectedOutput/search_spatial_explain.json index 6b732c6e..a71a703d 100644 --- a/src/test/resources/expectedOutput/search_spatial_explain.json +++ b/src/test/resources/expectedOutput/search_spatial_explain.json @@ -6,19 +6,15 @@ "filter" : { "bool" : { "must" : { - "bool" : { - "must" : { - "geo_shape" : { - "place" : { - "shape" : { - "type" : "polygon", - "coordinates" : [ [ [ 102.0, 2.0 ], [ 103.0, 2.0 ], [ 103.0, 3.0 ], [ 102.0, 3.0 ], [ 102.0, 2.0 ] ] ] - }, - "relation" : "intersects" - }, - "_name" : null - } - } + "geo_shape" : { + "place" : { + "shape" : { + "type" : "polygon", + "coordinates" : [ [ [ 102.0, 2.0 ], [ 103.0, 2.0 ], [ 103.0, 3.0 ], [ 102.0, 3.0 ], [ 102.0, 2.0 ] ] ] + }, + "relation" : "intersects" + }, + "_name" : null } } } From e9f81c023fd977a29e2dd25eecb0dff8a9bd9905 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Fri, 16 Oct 2015 23:55:21 +0300 Subject: [PATCH 095/559] add "show" command #33 and fix delete command #101 --- src/_site/index.html | 1 + src/_site/mapping_parser.js | 52 ++++++++ src/_site/query.js | 108 ++++++++++++++++- .../nlpcn/ActionRequestRestExecuter.java | 18 ++- .../nlpcn/GetIndexRequestRestListener.java | 112 ++++++++++++++++++ .../nlpcn/es4sql/query/ESActionFactory.java | 11 +- .../nlpcn/es4sql/query/ShowQueryAction.java | 65 ++++++++++ ...SqlElasticDeleteByQueryRequestBuilder.java | 8 ++ .../query/SqlElasticRequestBuilder.java | 3 + .../query/SqlElasticSearchRequestBuilder.java | 6 + .../es4sql/query/join/JoinRequestBuilder.java | 6 + .../java/org/nlpcn/es4sql/MainTestSuite.java | 4 +- src/test/java/org/nlpcn/es4sql/ShowTest.java | 69 +++++++++++ 13 files changed, 445 insertions(+), 18 deletions(-) create mode 100644 src/_site/mapping_parser.js create mode 100644 src/main/java/org/elasticsearch/plugin/nlpcn/GetIndexRequestRestListener.java create mode 100644 src/main/java/org/nlpcn/es4sql/query/ShowQueryAction.java create mode 100644 src/test/java/org/nlpcn/es4sql/ShowTest.java diff --git a/src/_site/index.html b/src/_site/index.html index 60e81720..39aebfc0 100644 --- a/src/_site/index.html +++ b/src/_site/index.html @@ -182,6 +182,7 @@

Results

+ diff --git a/src/_site/mapping_parser.js b/src/_site/mapping_parser.js new file mode 100644 index 00000000..f7318f13 --- /dev/null +++ b/src/_site/mapping_parser.js @@ -0,0 +1,52 @@ + +var MappingParser = function(data) { + + var parsedMapping = parseMapping(data); + this.mapping = parsedMapping; +} + + +function parseMapping(mapping){ + var indexToTypeToFields = {}; + for(index in mapping){ + var types = mapping[index]["mappings"]; + var typeToFields = {}; + for(type in types){ + var fields = types[type]["properties"]; + fieldsFlatten = {}; + getFieldsRecursive(fields,fieldsFlatten,""); + typeToFields[type] = fieldsFlatten; + } + + indexToTypeToFields[index] = typeToFields; + } + return indexToTypeToFields; +} + +function getFieldsRecursive(fields,fieldsFlatten,prefix){ + for(field in fields){ + var fieldMapping = fields[field]; + if("type" in fieldMapping){ + fieldsFlatten[prefix+field] = fieldMapping; + } + else { + getFieldsRecursive(fieldMapping["properties"],fieldsFlatten,field+"."); + } + } +} + +MappingParser.prototype.getIndices = function() { + return Object.keys(this.mapping); +}; + +MappingParser.prototype.getTypes = function(index) { + return Object.keys(this.mapping[index]); +}; + +MappingParser.prototype.getFieldsForType = function(index,type) { + return Object.keys(this.mapping[index][type]); +}; +MappingParser.prototype.getFieldsForTypeWithMapping = function(index,type) { + return this.mapping[index][type]; +}; + diff --git a/src/_site/query.js b/src/_site/query.js index 344a039d..e1352c47 100644 --- a/src/_site/query.js +++ b/src/_site/query.js @@ -4,13 +4,26 @@ Returns the right Result Handler depend on the results */ var ResultHandlerFactory = { "create": function(data) { - + function isSearch(){ + return "hits" in data + } // Is query is of aggregation type? (SQL group by) function isAggregation() { return "aggregations" in data } + function isDelete(){ + return "_indices" in data + } + + if(isSearch()){ + return isAggregation() ? new AggregationQueryResultHandler(data) : new DefaultQueryResultHandler(data) + } - return isAggregation() ? new AggregationQueryResultHandler(data) : new DefaultQueryResultHandler(data) + if(isDelete()){ + return new DeleteQueryResultHandler(data); + } + return new ShowQueryResultHandler(data); + } } @@ -175,3 +188,94 @@ AggregationQueryResultHandler.prototype.getBody = function() { }; + + + +/* ShowQueryResultHandler object + for showing mapping in some levels (cluster, index and types) +*/ +var ShowQueryResultHandler = function(data) { + + var mappingParser = new MappingParser(data); + var indices = mappingParser.getIndices(); + body = []; + if(indices.length > 1){ + this.head = ["index","types"]; + for(indexOfIndex in indices){ + var indexToTypes = {}; + var index = indices[indexOfIndex] + indexToTypes["index"] = index; + indexToTypes["types"] = mappingParser.getTypes(index); + body.push(indexToTypes); + } + } + else { + var index = indices[0]; + var types = mappingParser.getTypes(index); + if(types.length > 1) { + this.head = ["type","fields"]; + for(typeIndex in types){ + var typeToFields = {}; + var type = types[typeIndex]; + typeToFields["type"] = type; + typeToFields["fields"] = mappingParser.getFieldsForType(index,type); + body.push(typeToFields) + } + } + else { + this.head = ["field","type","more"]; + fieldsWithMapping = mappingParser.getFieldsForTypeWithMapping(index,types[0]); + for(field in fieldsWithMapping){ + fieldRow = {}; + fieldMapping = fieldsWithMapping[field]; + fieldRow["field"] = field; + fieldRow["type"] = fieldMapping["type"]; + delete fieldMapping["type"]; + fieldRow["more"] = fieldMapping; + body.push(fieldRow); + } + } + } + + this.body = body; + +}; + + +ShowQueryResultHandler.prototype.getHead = function() { + return this.head +}; + +ShowQueryResultHandler.prototype.getBody = function() { + return this.body; +}; + + + +/* DeleteQueryResultHandler object + to show delete result status +*/ +var DeleteQueryResultHandler = function(data) { + this.head = ["index_deleted_from","shards_successful","shards_failed"]; + body = [] + deleteData = data["_indices"]; + for(index in deleteData){ + deleteStat = {}; + deleteStat["index_deleted_from"] = index; + shardsData = deleteData[index]["_shards"]; + deleteStat["shards_successful"] = shardsData["successful"]; + deleteStat["shards_failed"] = shardsData["failed"]; + body.push(deleteStat); + } + this.body = body; + +}; + + +DeleteQueryResultHandler.prototype.getHead = function() { + return this.head; +}; + +DeleteQueryResultHandler.prototype.getBody = function() { + return this.body; +}; diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestRestExecuter.java b/src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestRestExecuter.java index 07e6a641..fe9a1813 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestRestExecuter.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/ActionRequestRestExecuter.java @@ -1,17 +1,19 @@ package org.elasticsearch.plugin.nlpcn; import org.elasticsearch.action.ActionRequest; +import org.elasticsearch.action.ActionRequestBuilder; +import org.elasticsearch.action.admin.indices.get.GetIndexRequest; +import org.elasticsearch.action.admin.indices.get.GetIndexResponse; import org.elasticsearch.action.deletebyquery.DeleteByQueryRequest; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; import org.elasticsearch.rest.RestChannel; +import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.action.support.RestStatusToXContentListener; import org.nlpcn.es4sql.exception.SqlParseException; -import org.nlpcn.es4sql.query.join.HashJoinElasticRequestBuilder; import org.nlpcn.es4sql.query.SqlElasticRequestBuilder; import org.nlpcn.es4sql.query.join.JoinRequestBuilder; -import org.nlpcn.es4sql.query.join.NestedLoopsElasticRequestBuilder; import java.io.IOException; @@ -28,7 +30,9 @@ public ActionRequestRestExecuter(SqlElasticRequestBuilder requestBuilder, RestCh this.client = client; } - /** + + + /** * Execute the ActionRequest and returns the REST response using the channel. */ public void execute() throws Exception { @@ -42,8 +46,12 @@ public void execute() throws Exception { else if (request instanceof SearchRequest) { client.search((SearchRequest) request, new RestStatusToXContentListener(channel)); } else if (request instanceof DeleteByQueryRequest) { - client.deleteByQuery((DeleteByQueryRequest) request, new DeleteByQueryRestListener(channel)); - } + ActionRequestBuilder elasticRequestBuilder = this.requestBuilder.getBuilder(); + elasticRequestBuilder.execute(new DeleteByQueryRestListener(channel)); + } + else if(request instanceof GetIndexRequest) { + this.requestBuilder.getBuilder().execute( new GetIndexRequestRestListener(channel, (GetIndexRequest) request)); + } else { diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/GetIndexRequestRestListener.java b/src/main/java/org/elasticsearch/plugin/nlpcn/GetIndexRequestRestListener.java new file mode 100644 index 00000000..a08b1886 --- /dev/null +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/GetIndexRequestRestListener.java @@ -0,0 +1,112 @@ +package org.elasticsearch.plugin.nlpcn; + +import org.elasticsearch.action.admin.indices.get.GetIndexRequest; +import org.elasticsearch.action.admin.indices.get.GetIndexResponse; +import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse; +import org.elasticsearch.cluster.metadata.AliasMetaData; +import org.elasticsearch.cluster.metadata.MappingMetaData; +import org.elasticsearch.common.collect.ImmutableOpenMap; +import org.elasticsearch.common.hppc.cursors.ObjectObjectCursor; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentBuilderString; +import org.elasticsearch.rest.BytesRestResponse; +import org.elasticsearch.rest.RestChannel; +import org.elasticsearch.rest.RestResponse; +import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.rest.action.support.RestBuilderListener; +import org.elasticsearch.search.warmer.IndexWarmersMetaData; + +import java.io.IOException; +import java.util.List; + +/** + * Created by Eliran on 6/10/2015. + */ +public class GetIndexRequestRestListener extends RestBuilderListener { + + private GetIndexRequest getIndexRequest; + + public GetIndexRequestRestListener(RestChannel channel,GetIndexRequest getIndexRequest) { + super(channel); + this.getIndexRequest = getIndexRequest; + } + + @Override + public RestResponse buildResponse(GetIndexResponse getIndexResponse, XContentBuilder builder) throws Exception { + GetIndexRequest.Feature[] features = getIndexRequest.featuresAsEnums(); + String[] indices = getIndexResponse.indices(); + + builder.startObject(); + for (String index : indices) { + builder.startObject(index); + for (GetIndexRequest.Feature feature : features) { + switch (feature) { + case ALIASES: + writeAliases(getIndexResponse.aliases().get(index), builder, channel.request()); + break; + case MAPPINGS: + writeMappings(getIndexResponse.mappings().get(index), builder, channel.request()); + break; + case SETTINGS: + writeSettings(getIndexResponse.settings().get(index), builder, channel.request()); + break; + case WARMERS: + writeWarmers(getIndexResponse.warmers().get(index), builder, channel.request()); + break; + default: + throw new IllegalStateException("feature [" + feature + "] is not valid"); + } + } + builder.endObject(); + + } + builder.endObject(); + + return new BytesRestResponse(RestStatus.OK, builder); + } + private void writeAliases(List aliases, XContentBuilder builder, ToXContent.Params params) throws IOException { + builder.startObject(Fields.ALIASES); + if (aliases != null) { + for (AliasMetaData alias : aliases) { + AliasMetaData.Builder.toXContent(alias, builder, params); + } + } + builder.endObject(); + } + + private void writeMappings(ImmutableOpenMap mappings, XContentBuilder builder, ToXContent.Params params) throws IOException { + builder.startObject(Fields.MAPPINGS); + if (mappings != null) { + for (ObjectObjectCursor typeEntry : mappings) { + builder.field(typeEntry.key); + builder.map(typeEntry.value.sourceAsMap()); + } + } + builder.endObject(); + } + + private void writeSettings(Settings settings, XContentBuilder builder, ToXContent.Params params) throws IOException { + builder.startObject(Fields.SETTINGS); + settings.toXContent(builder, params); + builder.endObject(); + } + + private void writeWarmers(List warmers, XContentBuilder builder, ToXContent.Params params) throws IOException { + builder.startObject(Fields.WARMERS); + if (warmers != null) { + for (IndexWarmersMetaData.Entry warmer : warmers) { + IndexWarmersMetaData.FACTORY.toXContent(warmer, builder, params); + } + } + builder.endObject(); + } + + static class Fields { + static final XContentBuilderString ALIASES = new XContentBuilderString("aliases"); + static final XContentBuilderString MAPPINGS = new XContentBuilderString("mappings"); + static final XContentBuilderString SETTINGS = new XContentBuilderString("settings"); + static final XContentBuilderString WARMERS = new XContentBuilderString("warmers"); + } +} \ No newline at end of file diff --git a/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java b/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java index 442d4b70..ea3a0d1d 100644 --- a/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java +++ b/src/main/java/org/nlpcn/es4sql/query/ESActionFactory.java @@ -8,16 +8,11 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; import com.alibaba.druid.sql.parser.*; -import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; -import org.elasticsearch.cluster.routing.allocation.decider.Decision; import org.elasticsearch.plugin.nlpcn.ElasticResultHandler; import org.elasticsearch.plugin.nlpcn.QueryActionElasticExecutor; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; -import org.elasticsearch.search.aggregations.Aggregation; -import org.elasticsearch.search.aggregations.Aggregations; -import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation; import org.nlpcn.es4sql.domain.Delete; import org.nlpcn.es4sql.domain.JoinSelect; import org.nlpcn.es4sql.domain.Select; @@ -28,12 +23,9 @@ import org.nlpcn.es4sql.parse.SubQueryExpression; import org.nlpcn.es4sql.query.join.ESJoinQueryActionFactory; -import java.io.IOException; import java.sql.SQLFeatureNotSupportedException; import java.util.ArrayList; -import java.util.Collection; import java.util.List; -import java.util.Map; public class ESActionFactory { @@ -71,7 +63,8 @@ public static QueryAction create(Client client, String sql) throws SqlParseExcep SQLDeleteStatement deleteStatement = parser.parseDeleteStatement(); Delete delete = new SqlParser().parseDelete(deleteStatement); return new DeleteQueryAction(client, delete); - + case "SHOW": + return new ShowQueryAction(client,sql); default: throw new SQLFeatureNotSupportedException(String.format("Unsupported query: %s", sql)); } diff --git a/src/main/java/org/nlpcn/es4sql/query/ShowQueryAction.java b/src/main/java/org/nlpcn/es4sql/query/ShowQueryAction.java new file mode 100644 index 00000000..2e1db1c4 --- /dev/null +++ b/src/main/java/org/nlpcn/es4sql/query/ShowQueryAction.java @@ -0,0 +1,65 @@ +package org.nlpcn.es4sql.query; + +import org.elasticsearch.action.ActionRequest; +import org.elasticsearch.action.ActionRequestBuilder; +import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.action.admin.indices.get.GetIndexRequest; +import org.elasticsearch.action.admin.indices.get.GetIndexRequestBuilder; +import org.elasticsearch.client.Client; +import org.nlpcn.es4sql.exception.SqlParseException; + +/** + * Created by Eliran on 6/10/2015. + */ +public class ShowQueryAction extends QueryAction { + private String sql; + public ShowQueryAction(Client client, String sql) { + super(client,null); + this.sql = sql; + } + + @Override + public SqlElasticRequestBuilder explain() throws SqlParseException { + String sql = this.sql.replaceAll("\\s+"," "); + //todo: support indices with space? + String indexName = sql.split(" ")[1]; + final GetIndexRequestBuilder indexRequestBuilder ; + String type = null; + if(indexName.contains("/")){ + String[] indexAndType = indexName.split("\\/"); + indexName = indexAndType[0]; + type = indexAndType[1]; + } + indexRequestBuilder = client.admin().indices().prepareGetIndex(); + + if(!indexName.equals("*")){ + indexRequestBuilder.addIndices(indexName); + if(type!=null && !type.equals("")){ + indexRequestBuilder.setTypes(type); + } + } + indexRequestBuilder.addFeatures(GetIndexRequest.Feature.MAPPINGS); + + return new SqlElasticRequestBuilder() { + @Override + public ActionRequest request() { + return indexRequestBuilder.request(); + } + + @Override + public String explain() { + return indexRequestBuilder.toString(); + } + + @Override + public ActionResponse get() { + return indexRequestBuilder.get(); + } + + @Override + public ActionRequestBuilder getBuilder() { + return indexRequestBuilder; + } + }; + } +} diff --git a/src/main/java/org/nlpcn/es4sql/query/SqlElasticDeleteByQueryRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/SqlElasticDeleteByQueryRequestBuilder.java index cd5f2e85..5873b749 100644 --- a/src/main/java/org/nlpcn/es4sql/query/SqlElasticDeleteByQueryRequestBuilder.java +++ b/src/main/java/org/nlpcn/es4sql/query/SqlElasticDeleteByQueryRequestBuilder.java @@ -1,6 +1,7 @@ package org.nlpcn.es4sql.query; import org.elasticsearch.action.ActionRequest; +import org.elasticsearch.action.ActionRequestBuilder; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.deletebyquery.DeleteByQueryRequestBuilder; import org.elasticsearch.action.support.QuerySourceBuilder; @@ -45,6 +46,13 @@ public String explain() { @Override public ActionResponse get() { + return this.deleteByQueryRequestBuilder.get(); } + + @Override + public ActionRequestBuilder getBuilder() { + return deleteByQueryRequestBuilder; + } + } diff --git a/src/main/java/org/nlpcn/es4sql/query/SqlElasticRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/SqlElasticRequestBuilder.java index a773bca2..883b6ea1 100644 --- a/src/main/java/org/nlpcn/es4sql/query/SqlElasticRequestBuilder.java +++ b/src/main/java/org/nlpcn/es4sql/query/SqlElasticRequestBuilder.java @@ -1,6 +1,7 @@ package org.nlpcn.es4sql.query; import org.elasticsearch.action.ActionRequest; +import org.elasticsearch.action.ActionRequestBuilder; import org.elasticsearch.action.ActionResponse; /** @@ -10,4 +11,6 @@ public interface SqlElasticRequestBuilder { public ActionRequest request(); public String explain(); public ActionResponse get(); + public ActionRequestBuilder getBuilder(); + } diff --git a/src/main/java/org/nlpcn/es4sql/query/SqlElasticSearchRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/SqlElasticSearchRequestBuilder.java index 0a8a1d6b..01343232 100644 --- a/src/main/java/org/nlpcn/es4sql/query/SqlElasticSearchRequestBuilder.java +++ b/src/main/java/org/nlpcn/es4sql/query/SqlElasticSearchRequestBuilder.java @@ -1,6 +1,7 @@ package org.nlpcn.es4sql.query; import org.elasticsearch.action.ActionRequest; +import org.elasticsearch.action.ActionRequestBuilder; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.common.xcontent.ToXContent; @@ -42,6 +43,11 @@ public ActionResponse get() { return requestBuilder.get(); } + @Override + public ActionRequestBuilder getBuilder() { + return requestBuilder; + } + @Override public String toString(){ return this.requestBuilder.toString(); diff --git a/src/main/java/org/nlpcn/es4sql/query/join/JoinRequestBuilder.java b/src/main/java/org/nlpcn/es4sql/query/join/JoinRequestBuilder.java index ae7be00d..5b7bcb5f 100644 --- a/src/main/java/org/nlpcn/es4sql/query/join/JoinRequestBuilder.java +++ b/src/main/java/org/nlpcn/es4sql/query/join/JoinRequestBuilder.java @@ -2,6 +2,7 @@ import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource; import org.elasticsearch.action.ActionRequest; +import org.elasticsearch.action.ActionRequestBuilder; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.search.MultiSearchRequest; import org.elasticsearch.common.xcontent.ToXContent; @@ -69,6 +70,11 @@ public ActionResponse get() { return null; } + @Override + public ActionRequestBuilder getBuilder() { + return this.firstTable.getRequestBuilder(); + } + public MultiSearchRequest getMulti() { return multi; } diff --git a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java index ec7ab116..e414a727 100644 --- a/src/test/java/org/nlpcn/es4sql/MainTestSuite.java +++ b/src/test/java/org/nlpcn/es4sql/MainTestSuite.java @@ -29,8 +29,8 @@ DeleteTest.class, ExplainTest.class, WktToGeoJsonConverterTests.class, - SqlParserTests.class - + SqlParserTests.class, + ShowTest.class }) public class MainTestSuite { diff --git a/src/test/java/org/nlpcn/es4sql/ShowTest.java b/src/test/java/org/nlpcn/es4sql/ShowTest.java new file mode 100644 index 00000000..d2eeca31 --- /dev/null +++ b/src/test/java/org/nlpcn/es4sql/ShowTest.java @@ -0,0 +1,69 @@ +package org.nlpcn.es4sql; + +import com.alibaba.druid.sql.ast.SQLExpr; +import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.action.admin.indices.get.GetIndexResponse; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.cluster.metadata.MappingMetaData; +import org.elasticsearch.common.collect.ImmutableOpenMap; +import org.elasticsearch.search.SearchHits; +import org.junit.Assert; +import org.junit.Test; +import org.nlpcn.es4sql.exception.SqlParseException; +import org.nlpcn.es4sql.parse.ElasticSqlExprParser; +import org.nlpcn.es4sql.query.SqlElasticRequestBuilder; +import org.nlpcn.es4sql.query.SqlElasticSearchRequestBuilder; + +import java.io.IOException; +import java.sql.SQLFeatureNotSupportedException; + +import static org.nlpcn.es4sql.TestsConstants.*; + +/** + * Created by Eliran on 16/10/2015. + */ +public class ShowTest { + + @Test + public void showAll_atLeastOneIndexReturns() throws SqlParseException, SQLFeatureNotSupportedException, IOException { + String query = "show *"; + GetIndexResponse getIndexResponse = runShowQuery(query); + ImmutableOpenMap> mappings = getIndexResponse.getMappings(); + Assert.assertTrue(mappings.size() >= 1); + + } + + @Test + public void showIndex_onlyOneIndexReturn() throws SqlParseException, SQLFeatureNotSupportedException, IOException { + String query = "show "+ TEST_INDEX; + GetIndexResponse getIndexResponse = runShowQuery(query); + ImmutableOpenMap> mappings = getIndexResponse.getMappings(); + Assert.assertEquals(1, mappings.size()); + Assert.assertTrue(mappings.containsKey(TEST_INDEX)); + + } + @Test + public void showIndex_onlyOneIndexReturWithMoreThanOneTypes() throws SqlParseException, SQLFeatureNotSupportedException, IOException { + String query = "show "+ TEST_INDEX; + GetIndexResponse getIndexResponse = runShowQuery(query); + ImmutableOpenMap> mappings = getIndexResponse.getMappings(); + ImmutableOpenMap typeToData = mappings.get(TEST_INDEX); + Assert.assertTrue(typeToData.size()>1); + } + + @Test + public void showIndexType_onlyOneTypeReturn() throws SqlParseException, SQLFeatureNotSupportedException, IOException { + String query = String.format("show %s/account", TEST_INDEX); + GetIndexResponse getIndexResponse = runShowQuery(query); + ImmutableOpenMap> mappings = getIndexResponse.getMappings(); + ImmutableOpenMap typeToData = mappings.get(TEST_INDEX); + Assert.assertEquals(1,typeToData.size()); + } + + private GetIndexResponse runShowQuery(String query) throws SqlParseException, SQLFeatureNotSupportedException, SQLFeatureNotSupportedException { + SearchDao searchDao = MainTestSuite.getSearchDao(); + SqlElasticRequestBuilder requestBuilder = searchDao.explain(query); + return (GetIndexResponse) requestBuilder.get(); + + } +} From 980acdfc2802c74cd3ddcb6807523b98e8971607 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sat, 17 Oct 2015 00:07:48 +0300 Subject: [PATCH 096/559] support min and max date on web ui #100 --- src/_site/query.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/_site/query.js b/src/_site/query.js index e1352c47..f13bf052 100644 --- a/src/_site/query.js +++ b/src/_site/query.js @@ -121,13 +121,19 @@ var AggregationQueryResultHandler = function(data) { obj[bucketName] = bucket.key } - for(var field in bucket) { - if(bucket[field].value != undefined) { - obj[field] = bucket[field].value + for(var field in bucket) { + var bucketValue = bucket[field] + if(bucketValue.value != undefined) { + if("value_as_string" in bucket[field]){ + obj[field] = bucketValue["value_as_string"] + } + else { + obj[field] = bucketValue.value + } } else { if(typeof(bucket[field])=="object"){ - fillFieldsForSpecificAggregation(obj,bucket[field],field); + fillFieldsForSpecificAggregation(obj,bucketValue,field); } } } From fc7e95375798bea290eb4b0e0361539a1f95f2c7 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sat, 17 Oct 2015 00:36:52 +0300 Subject: [PATCH 097/559] bug fix on aggregation field that starts with '@' --- src/main/java/org/nlpcn/es4sql/Util.java | 4 +++- src/test/java/org/nlpcn/es4sql/SqlParserTests.java | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/nlpcn/es4sql/Util.java b/src/main/java/org/nlpcn/es4sql/Util.java index de4aa6ec..19a16eab 100644 --- a/src/main/java/org/nlpcn/es4sql/Util.java +++ b/src/main/java/org/nlpcn/es4sql/Util.java @@ -40,7 +40,9 @@ public static Object expr2Object(SQLExpr expr) throws SqlParseException { } else if (expr instanceof SQLIdentifierExpr) { value = expr.toString(); } else if (expr instanceof SQLPropertyExpr) { - value = expr.toString(); + value = expr.toString(); + }else if (expr instanceof SQLVariantRefExpr ){ + value = expr.toString(); }else if (expr instanceof SQLAllColumnExpr) { value = "*"; } else { diff --git a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java index b4f67904..2965dd1d 100644 --- a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java +++ b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java @@ -332,6 +332,18 @@ public void fieldWithATcharAtSelect() throws SqlParseException { Field field = fields.get(0); Assert.assertEquals(field.getName(),"@field"); } + + @Test + public void fieldWithATcharAtSelectOnAgg() throws SqlParseException { + String query = "SELECT max(@field) FROM index/type where field2 = 6 "; + SQLExpr sqlExpr = queryToExpr(query); + Select select = parser.parseSelect((SQLQueryExpr) sqlExpr); + List fields = select.getFields(); + Assert.assertEquals(1,fields.size()); + Field field = fields.get(0); + Assert.assertEquals("MAX(@field)",field.toString()); + } + @Test public void fieldWithColonCharAtSelect() throws SqlParseException { String query = "SELECT a:b FROM index/type where field2 = 6 "; From 20cea8007084db041d5f5cf3b015322e908e1c54 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Sat, 17 Oct 2015 08:57:06 +0300 Subject: [PATCH 098/559] new version 1.4.4 --- README.md | 2 +- pom.xml | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d1c3cf94..a4f9b089 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Install as plugin: ### Elasticsearch 1.6.X ```` -./bin/plugin -u https://github.com/NLPchina/elasticsearch-sql/releases/download/1.4.3/elasticsearch-sql-1.4.3.zip --install sql +./bin/plugin -u https://github.com/NLPchina/elasticsearch-sql/releases/download/1.4.4/elasticsearch-sql-1.4.4.zip --install sql ```` After doing this, you need to restart the Elasticsearch server. Otherwise you may get errors like `Invalid index name [sql], must not start with '']; ","status":400}`. diff --git a/pom.xml b/pom.xml index 231006a0..558ecc77 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.nlpcn elasticsearch-sql - 1.4.3 + 1.4.4 jar Query elasticsearch using SQL elasticsearch-sql @@ -27,6 +27,11 @@ Omer shelef shlaflaf@gmail.com + + eliranmoyal + Eliran Moyal + eliran.moyal1@gmail.com + From 7c5d368dcc619b6b6388bf6ec85524099462cab2 Mon Sep 17 00:00:00 2001 From: Caleb Ott Date: Mon, 19 Oct 2015 09:45:23 -0500 Subject: [PATCH 099/559] Added the order by to be applied to the search results even if the order by is part of an aggregation --- .../java/org/nlpcn/es4sql/query/AggregationQueryAction.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/nlpcn/es4sql/query/AggregationQueryAction.java b/src/main/java/org/nlpcn/es4sql/query/AggregationQueryAction.java index 2d1ce978..16336dea 100644 --- a/src/main/java/org/nlpcn/es4sql/query/AggregationQueryAction.java +++ b/src/main/java/org/nlpcn/es4sql/query/AggregationQueryAction.java @@ -90,6 +90,8 @@ public SqlElasticSearchRequestBuilder explain() throws SqlParseException { break; case "KEY": termsBuilder.order(Terms.Order.term(isASC(order))); + // add the sort to the request also so the results get sorted as well + request.addSort(order.getName(), SortOrder.valueOf(order.getType())); break; case "FIELD": termsBuilder.order(Terms.Order.aggregation(order.getName(), isASC(order))); From 7a1dfe6a648f302b028be65759a15fc7cf415713 Mon Sep 17 00:00:00 2001 From: Caleb Ott Date: Tue, 20 Oct 2015 10:24:07 -0500 Subject: [PATCH 100/559] Updated the build to inject the version into the properties file. This way the proper version is in elastic. --- pom.xml | 10 ++++++++++ src/main/resources/es-plugin.properties | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 558ecc77..725a2aa2 100644 --- a/pom.xml +++ b/pom.xml @@ -114,6 +114,16 @@ + + + src/main/resources + true + + es-plugin.properties + + + + maven-compiler-plugin diff --git a/src/main/resources/es-plugin.properties b/src/main/resources/es-plugin.properties index ad083973..09e50087 100644 --- a/src/main/resources/es-plugin.properties +++ b/src/main/resources/es-plugin.properties @@ -1 +1,2 @@ -plugin=org.elasticsearch.plugin.nlpcn.SqlPlug \ No newline at end of file +plugin=org.elasticsearch.plugin.nlpcn.SqlPlug +version=${project.version} \ No newline at end of file From 04b84a8fd37244f915748c745ae0e3d6f518eef2 Mon Sep 17 00:00:00 2001 From: Caleb Ott Date: Tue, 20 Oct 2015 13:25:18 -0500 Subject: [PATCH 101/559] Updated the SQL Plugin UI to include the version information in the header of the page --- src/_site/controllers.js | 66 ++++++++++++++++++++++++---------------- src/_site/index.html | 15 ++++++--- 2 files changed, 50 insertions(+), 31 deletions(-) diff --git a/src/_site/controllers.js b/src/_site/controllers.js index 817a7a10..6eb1ad42 100644 --- a/src/_site/controllers.js +++ b/src/_site/controllers.js @@ -3,7 +3,7 @@ var elasticsearchSqlApp = angular.module('elasticsearchSqlApp', ["ngAnimate", "n elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) { $scope.url = getUrl(); - $scope.error = ""; + $scope.error = ""; $scope.resultsColumns = []; $scope.resultsRows = []; $scope.searchLoading = false; @@ -13,6 +13,18 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) $scope.scrollId = null; $scope.gotNext = false; + // pull version and put it on the scope + $http.get($scope.url).success(function (data) { + $http.get($scope.url + "_nodes/" + data.name).success(function (nodeData) { + var node = nodeData.nodes[Object.keys(nodeData.nodes)[0]]; + angular.forEach(node.plugins, function (plugin) { + if (plugin.name === "sql") { + $scope.version = plugin.version; + } + }); + }); + }); + $scope.nextSearch = function(){ $scope.error = ""; $scope.nextLoading = true; @@ -28,11 +40,11 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) .success(function(data, status, headers, config) { var handler = ResultHandlerFactory.create(data); var body = handler.getBody() - + if(body.length ==null || body.length == 0){ $scope.gotNext=false; } - else + else { $scope.scrollId = handler.getScrollId(); } @@ -43,12 +55,12 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) else { $scope.resultsColumns = handler.getHead(); $scope.resultsRows = handler.getBody(); - + } - + }) - .error(function(data, status, headers, config) { + .error(function(data, status, headers, config) { if(data == "") { $scope.error = "Error occured! response is not avalible."; } @@ -59,7 +71,7 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) }) .finally(function() { $scope.nextLoading = false; - $scope.$apply() + $scope.$apply() }); } @@ -86,9 +98,9 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) } $scope.resultsColumns = handler.getHead(); $scope.resultsRows = handler.getBody(); - + }) - .error(function(data, status, headers, config) { + .error(function(data, status, headers, config) { if(data == "") { $scope.error = "Error occured! response is not avalible."; } @@ -98,10 +110,10 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) }) .finally(function() { $scope.searchLoading = false; - $scope.$apply() + $scope.$apply() }); } - + $scope.explain = function() { // Reset results and error box $scope.error = ""; @@ -119,7 +131,7 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) $scope.resultExplan = true; window.explanResult.setValue(JSON.stringify(data, null, "\t")); }) - .error(function(data, status, headers, config) { + .error(function(data, status, headers, config) { $scope.resultExplan = false; if(data == "") { $scope.error = "Error occured! response is not avalible."; @@ -130,24 +142,24 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) }) .finally(function() { $scope.explainLoading = false; - $scope.$apply() + $scope.$apply() }); } - - - - $scope.exportCSV = function() { + + + + $scope.exportCSV = function() { var columns = $scope.resultsColumns ; var rows = $scope.resultsRows ; var data =arr2csvStr(columns,',') ; for(var i=0; i From a1b0de2a1ef28234ae0654dad96488f3209c7e37 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Thu, 22 Oct 2015 22:38:56 +0300 Subject: [PATCH 102/559] bug fix on site SHOW command on more than 2 hierarchy on document --- src/_site/mapping_parser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_site/mapping_parser.js b/src/_site/mapping_parser.js index f7318f13..f5886d79 100644 --- a/src/_site/mapping_parser.js +++ b/src/_site/mapping_parser.js @@ -30,7 +30,7 @@ function getFieldsRecursive(fields,fieldsFlatten,prefix){ fieldsFlatten[prefix+field] = fieldMapping; } else { - getFieldsRecursive(fieldMapping["properties"],fieldsFlatten,field+"."); + getFieldsRecursive(fieldMapping["properties"],fieldsFlatten,prefix+field+"."); } } } From 1212e06ea8f18387db98dd1144578b9fef170f1e Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Fri, 23 Oct 2015 01:22:11 +0300 Subject: [PATCH 103/559] add script field support (no tests because its not enabled by default and will cause failures) --- src/_site/query.js | 380 +++++++++--------- .../es4sql/query/DefaultQueryAction.java | 38 +- 2 files changed, 228 insertions(+), 190 deletions(-) diff --git a/src/_site/query.js b/src/_site/query.js index f13bf052..2206ad1a 100644 --- a/src/_site/query.js +++ b/src/_site/query.js @@ -1,147 +1,163 @@ /* ResultHandlerFactory -Returns the right Result Handler depend -on the results */ + Returns the right Result Handler depend + on the results */ var ResultHandlerFactory = { - "create": function(data) { - function isSearch(){ - return "hits" in data - } - // Is query is of aggregation type? (SQL group by) - function isAggregation() { - return "aggregations" in data - } - function isDelete(){ - return "_indices" in data - } + "create": function(data) { + function isSearch(){ + return "hits" in data + } + // Is query is of aggregation type? (SQL group by) + function isAggregation() { + return "aggregations" in data + } + function isDelete(){ + return "_indices" in data + } - if(isSearch()){ - return isAggregation() ? new AggregationQueryResultHandler(data) : new DefaultQueryResultHandler(data) - } + if(isSearch()){ + return isAggregation() ? new AggregationQueryResultHandler(data) : new DefaultQueryResultHandler(data) + } + + if(isDelete()){ + return new DeleteQueryResultHandler(data); + } + return new ShowQueryResultHandler(data); - if(isDelete()){ - return new DeleteQueryResultHandler(data); } - return new ShowQueryResultHandler(data); - - } } /* DefaultQueryResultHandler object -Handle the query result, -in case of regular query -(Not aggregation) -*/ + Handle the query result, + in case of regular query + (Not aggregation) + */ var DefaultQueryResultHandler = function(data) { - // createScheme by traverse hits field - function createScheme() { - var hits = data.hits.hits - scheme = [] - for(index=0; index 1) + row[field] = fieldValue; + else row[field] = fieldValue[0]; + } + else { + row[field] = fieldValue; + } + } +} /* AggregationQueryResultHandler object -Handle the query result, -in case of Aggregation query -(SQL group by) -*/ + Handle the query result, + in case of Aggregation query + (SQL group by) + */ var AggregationQueryResultHandler = function(data) { - function getRows(bucketName, bucket, additionalColumns) { - var rows = [] - - var subBuckets = getSubBuckets(bucket) - if(subBuckets.length > 0) { - for(var i = 0; i < subBuckets.length; i++) { - var subBucketName = subBuckets[i]["bucketName"]; - var subBucket = subBuckets[i]["bucket"]; - - var newAdditionalColumns = {}; - // bucket without parents. - if(bucketName != undefined) { - var newColumn = {}; - newColumn[bucketName] = bucket.key; - newAdditionalColumns = $.extend(newColumn, additionalColumns); + function getRows(bucketName, bucket, additionalColumns) { + var rows = [] + + var subBuckets = getSubBuckets(bucket) + if(subBuckets.length > 0) { + for(var i = 0; i < subBuckets.length; i++) { + var subBucketName = subBuckets[i]["bucketName"]; + var subBucket = subBuckets[i]["bucket"]; + + var newAdditionalColumns = {}; + // bucket without parents. + if(bucketName != undefined) { + var newColumn = {}; + newColumn[bucketName] = bucket.key; + newAdditionalColumns = $.extend(newColumn, additionalColumns); + } + + var newRows = getRows(subBucketName, subBucket, newAdditionalColumns) + $.merge(rows, newRows); + } } - var newRows = getRows(subBucketName, subBucket, newAdditionalColumns) - $.merge(rows, newRows); - } - } - - else { - var obj = $.extend({}, additionalColumns) - if(bucketName != undefined) { - obj[bucketName] = bucket.key - } - - for(var field in bucket) { - var bucketValue = bucket[field] - if(bucketValue.value != undefined) { - if("value_as_string" in bucket[field]){ - obj[field] = bucketValue["value_as_string"] - } - else { - obj[field] = bucketValue.value - } - } else { - if(typeof(bucket[field])=="object"){ - fillFieldsForSpecificAggregation(obj,bucketValue,field); + var obj = $.extend({}, additionalColumns) + if(bucketName != undefined) { + obj[bucketName] = bucket.key } + + for(var field in bucket) { + var bucketValue = bucket[field] + if(bucketValue.value != undefined) { + if("value_as_string" in bucket[field]){ + obj[field] = bucketValue["value_as_string"] + } + else { + obj[field] = bucketValue.value + } + } + else { + if(typeof(bucket[field])=="object"){ + fillFieldsForSpecificAggregation(obj,bucketValue,field); + } + } + } + rows.push(obj) } - } - rows.push(obj) - } - return rows - } + return rows + } function fillFieldsForSpecificAggregation(obj,value,field) @@ -157,40 +173,40 @@ var AggregationQueryResultHandler = function(data) { return; } - function getSubBuckets(bucket) { - var subBuckets = []; - for(var field in bucket) { - var buckets = bucket[field].buckets - if(buckets != undefined) { - for(var i = 0; i < buckets.length; i++) { - subBuckets.push({"bucketName": field, "bucket": buckets[i]}) + function getSubBuckets(bucket) { + var subBuckets = []; + for(var field in bucket) { + var buckets = bucket[field].buckets + if(buckets != undefined) { + for(var i = 0; i < buckets.length; i++) { + subBuckets.push({"bucketName": field, "bucket": buckets[i]}) + } + } } - } + + return subBuckets } - return subBuckets - } - - this.data = data - this.flattenBuckets = getRows(undefined, data.aggregations, {}) + this.data = data + this.flattenBuckets = getRows(undefined, data.aggregations, {}) }; AggregationQueryResultHandler.prototype.getHead = function() { - head = [] - for(var i = 0; i < this.flattenBuckets.length; i++) { - var keys = Object.keys(this.flattenBuckets[i]) - for(var j = 0; j < keys.length; j++) { - if($.inArray(keys[j], head) == -1) { - head.push(keys[j]) - } + head = [] + for(var i = 0; i < this.flattenBuckets.length; i++) { + var keys = Object.keys(this.flattenBuckets[i]) + for(var j = 0; j < keys.length; j++) { + if($.inArray(keys[j], head) == -1) { + head.push(keys[j]) + } + } } - } - return head + return head }; AggregationQueryResultHandler.prototype.getBody = function() { - return this.flattenBuckets + return this.flattenBuckets }; @@ -198,90 +214,90 @@ AggregationQueryResultHandler.prototype.getBody = function() { /* ShowQueryResultHandler object - for showing mapping in some levels (cluster, index and types) -*/ + for showing mapping in some levels (cluster, index and types) + */ var ShowQueryResultHandler = function(data) { - var mappingParser = new MappingParser(data); - var indices = mappingParser.getIndices(); - body = []; - if(indices.length > 1){ - this.head = ["index","types"]; - for(indexOfIndex in indices){ - var indexToTypes = {}; - var index = indices[indexOfIndex] - indexToTypes["index"] = index; - indexToTypes["types"] = mappingParser.getTypes(index); - body.push(indexToTypes); - } - } - else { - var index = indices[0]; - var types = mappingParser.getTypes(index); - if(types.length > 1) { - this.head = ["type","fields"]; - for(typeIndex in types){ - var typeToFields = {}; - var type = types[typeIndex]; - typeToFields["type"] = type; - typeToFields["fields"] = mappingParser.getFieldsForType(index,type); - body.push(typeToFields) - } + var mappingParser = new MappingParser(data); + var indices = mappingParser.getIndices(); + body = []; + if(indices.length > 1){ + this.head = ["index","types"]; + for(indexOfIndex in indices){ + var indexToTypes = {}; + var index = indices[indexOfIndex] + indexToTypes["index"] = index; + indexToTypes["types"] = mappingParser.getTypes(index); + body.push(indexToTypes); + } } else { - this.head = ["field","type","more"]; - fieldsWithMapping = mappingParser.getFieldsForTypeWithMapping(index,types[0]); - for(field in fieldsWithMapping){ - fieldRow = {}; - fieldMapping = fieldsWithMapping[field]; - fieldRow["field"] = field; - fieldRow["type"] = fieldMapping["type"]; - delete fieldMapping["type"]; - fieldRow["more"] = fieldMapping; - body.push(fieldRow); - } + var index = indices[0]; + var types = mappingParser.getTypes(index); + if(types.length > 1) { + this.head = ["type","fields"]; + for(typeIndex in types){ + var typeToFields = {}; + var type = types[typeIndex]; + typeToFields["type"] = type; + typeToFields["fields"] = mappingParser.getFieldsForType(index,type); + body.push(typeToFields) + } + } + else { + this.head = ["field","type","more"]; + fieldsWithMapping = mappingParser.getFieldsForTypeWithMapping(index,types[0]); + for(field in fieldsWithMapping){ + fieldRow = {}; + fieldMapping = fieldsWithMapping[field]; + fieldRow["field"] = field; + fieldRow["type"] = fieldMapping["type"]; + delete fieldMapping["type"]; + fieldRow["more"] = fieldMapping; + body.push(fieldRow); + } + } } - } - this.body = body; - + this.body = body; + }; ShowQueryResultHandler.prototype.getHead = function() { - return this.head + return this.head }; ShowQueryResultHandler.prototype.getBody = function() { - return this.body; + return this.body; }; /* DeleteQueryResultHandler object - to show delete result status -*/ + to show delete result status + */ var DeleteQueryResultHandler = function(data) { - this.head = ["index_deleted_from","shards_successful","shards_failed"]; - body = [] - deleteData = data["_indices"]; - for(index in deleteData){ - deleteStat = {}; - deleteStat["index_deleted_from"] = index; - shardsData = deleteData[index]["_shards"]; - deleteStat["shards_successful"] = shardsData["successful"]; - deleteStat["shards_failed"] = shardsData["failed"]; - body.push(deleteStat); - } - this.body = body; - + this.head = ["index_deleted_from","shards_successful","shards_failed"]; + body = [] + deleteData = data["_indices"]; + for(index in deleteData){ + deleteStat = {}; + deleteStat["index_deleted_from"] = index; + shardsData = deleteData[index]["_shards"]; + deleteStat["shards_successful"] = shardsData["successful"]; + deleteStat["shards_failed"] = shardsData["failed"]; + body.push(deleteStat); + } + this.body = body; + }; DeleteQueryResultHandler.prototype.getHead = function() { - return this.head; + return this.head; }; DeleteQueryResultHandler.prototype.getBody = function() { - return this.body; + return this.body; }; diff --git a/src/main/java/org/nlpcn/es4sql/query/DefaultQueryAction.java b/src/main/java/org/nlpcn/es4sql/query/DefaultQueryAction.java index 58c20e14..b3f7f5cb 100644 --- a/src/main/java/org/nlpcn/es4sql/query/DefaultQueryAction.java +++ b/src/main/java/org/nlpcn/es4sql/query/DefaultQueryAction.java @@ -10,10 +10,7 @@ import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.sort.SortOrder; -import org.nlpcn.es4sql.domain.Field; -import org.nlpcn.es4sql.domain.Order; -import org.nlpcn.es4sql.domain.Select; -import org.nlpcn.es4sql.domain.Where; +import org.nlpcn.es4sql.domain.*; import org.nlpcn.es4sql.domain.hints.Hint; import org.nlpcn.es4sql.domain.hints.HintType; import org.nlpcn.es4sql.exception.SqlParseException; @@ -23,7 +20,8 @@ /** * Transform SQL query to standard Elasticsearch search query */ -public class DefaultQueryAction extends QueryAction { +public class + DefaultQueryAction extends QueryAction { private final Select select; private SearchRequestBuilder request; @@ -40,6 +38,7 @@ public SqlElasticSearchRequestBuilder explain() throws SqlParseException { setIndicesAndTypes(); setFields(select.getFields()); + setWhere(select.getWhere()); setSorts(select.getOrderBys()); setLimit(select.getOffset(), select.getRowCount()); @@ -89,12 +88,15 @@ private void setIndicesAndTypes() { * Set source filtering on a search request. * @param fields list of fields to source filter. */ - private void setFields(List fields) { + private void setFields(List fields) throws SqlParseException { if (select.getFields().size() > 0) { ArrayList includeFields = new ArrayList(); for (Field field : fields) { - if (field instanceof Field) { + if(field instanceof MethodField){ + handleMethodField((MethodField) field); + } + else if (field instanceof Field) { includeFields.add(field.getName()); } } @@ -103,8 +105,28 @@ private void setFields(List fields) { } } + private void handleMethodField(MethodField field) throws SqlParseException { + MethodField method = (MethodField) field; + if(method.getName().toLowerCase().equals("script")){ + handleScriptField(method); + } + } - /** + private void handleScriptField(MethodField method) throws SqlParseException { + List params = method.getParams(); + if(params.size() == 2){ + request.addScriptField(params.get(0).value.toString(),params.get(1).value.toString()); + } + else if(params.size() == 3){ + request.addScriptField(params.get(0).value.toString(),params.get(1).value.toString(),params.get(2).value.toString(),null); + } + else { + throw new SqlParseException("scripted_field only allows script(name,script) or script(name,lang,script)"); + } + } + + + /** * Create filters or queries based on * the Where clause. * @param where the 'WHERE' part of the SQL query. From 56d6ed806d23c423630ae0cde041188bc0205321 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Fri, 23 Oct 2015 15:09:45 +0300 Subject: [PATCH 104/559] translate to script when using binaryOper on select --- src/main/java/org/nlpcn/es4sql/Util.java | 4 +- .../org/nlpcn/es4sql/parse/FieldMaker.java | 67 ++++++++++++++----- .../java/org/nlpcn/es4sql/SqlParserTests.java | 67 +++++++++++++++++++ 3 files changed, 121 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/nlpcn/es4sql/Util.java b/src/main/java/org/nlpcn/es4sql/Util.java index 19a16eab..cb2e268e 100644 --- a/src/main/java/org/nlpcn/es4sql/Util.java +++ b/src/main/java/org/nlpcn/es4sql/Util.java @@ -45,7 +45,9 @@ public static Object expr2Object(SQLExpr expr) throws SqlParseException { value = expr.toString(); }else if (expr instanceof SQLAllColumnExpr) { value = "*"; - } else { + } else if (expr instanceof SQLValuableExpr){ + value = ((SQLValuableExpr)expr).getValue(); + } else { throw new SqlParseException("can not support this type " + expr.getClass()); } return value; diff --git a/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java b/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java index fae90625..669d4716 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java +++ b/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java @@ -1,5 +1,6 @@ package org.nlpcn.es4sql.parse; +import java.util.ArrayList; import java.util.LinkedList; import java.util.List; @@ -19,21 +20,31 @@ */ public class FieldMaker { public static Field makeField(SQLExpr expr, String alias,String tableAlias) throws SqlParseException { - if (expr instanceof SQLIdentifierExpr || expr instanceof SQLPropertyExpr || expr instanceof SQLVariantRefExpr) { - String name = expr.toString().replace("`", ""); - if(tableAlias==null) return new Field(name, alias); - else if(tableAlias!=null){ - String aliasPrefix = tableAlias + "."; - if(name.startsWith(aliasPrefix)) - { - name = name.replaceFirst(aliasPrefix,""); - return new Field(name, alias); - } - } - return null; - } else if (expr instanceof SQLQueryExpr) { - throw new SqlParseException("unknow field name : " + expr); - } else if (expr instanceof SQLAllColumnExpr) { + if (expr instanceof SQLIdentifierExpr || expr instanceof SQLPropertyExpr || expr instanceof SQLVariantRefExpr) { + return handleIdentifer(expr, alias, tableAlias); + } else if (expr instanceof SQLQueryExpr) { + throw new SqlParseException("unknow field name : " + expr); + } else if (expr instanceof SQLBinaryOpExpr) { + //make a SCRIPT method field; + SQLBinaryOpExpr binaryExpr = (SQLBinaryOpExpr) expr; + List params = new ArrayList<>(); + + String scriptFieldAlias; + if(alias == null || alias.equals("")) + scriptFieldAlias = binaryExpr.toString(); + else + scriptFieldAlias = alias; + params.add(new SQLCharExpr(scriptFieldAlias)); + + Object left = getScriptValue(binaryExpr.getLeft()); + Object right = getScriptValue(binaryExpr.getRight()); + String script = String.format("%s %s %s" , left ,binaryExpr.getOperator().getName() , right); + + params.add(new SQLCharExpr(script)); + + return makeMethodField("script",params,null,null); + + } else if (expr instanceof SQLAllColumnExpr) { } else if (expr instanceof SQLMethodInvokeExpr) { SQLMethodInvokeExpr mExpr = (SQLMethodInvokeExpr) expr; return makeMethodField(mExpr.getMethodName(), mExpr.getParameters(), null, alias); @@ -46,7 +57,31 @@ else if(tableAlias!=null){ return null; } - private static MethodField makeMethodField(String name, List arguments, SQLAggregateOption option, String alias) throws SqlParseException { + private static Object getScriptValue(SQLExpr expr) throws SqlParseException { + if (expr instanceof SQLIdentifierExpr || expr instanceof SQLPropertyExpr || expr instanceof SQLVariantRefExpr) { + return "doc['" + expr.toString() + "'].value"; + } + else if (expr instanceof SQLValuableExpr){ + return ((SQLValuableExpr)expr).getValue(); + } + throw new SqlParseException("could not parse sqlBinaryOpExpr need to be identifier/valuable got" + expr.getClass().getTypeName() + " with value:" +expr.toString() ); + } + + private static Field handleIdentifer(SQLExpr expr, String alias, String tableAlias) { + String name = expr.toString().replace("`", ""); + if(tableAlias==null) return new Field(name, alias); + else if(tableAlias!=null){ + String aliasPrefix = tableAlias + "."; + if(name.startsWith(aliasPrefix)) + { + name = name.replaceFirst(aliasPrefix,""); + return new Field(name, alias); + } + } + return null; + } + + private static MethodField makeMethodField(String name, List arguments, SQLAggregateOption option, String alias) throws SqlParseException { List paramers = new LinkedList<>(); for (SQLExpr object : arguments) { if (object instanceof SQLBinaryOpExpr) { diff --git a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java index 2965dd1d..7e46bdfa 100644 --- a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java +++ b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java @@ -1,6 +1,7 @@ package org.nlpcn.es4sql; import com.alibaba.druid.sql.ast.SQLExpr; +import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr; import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr; import com.alibaba.druid.sql.ast.expr.SQLQueryExpr; import org.junit.Assert; @@ -415,6 +416,72 @@ public void indexWithDotsAndHyphen() throws SqlParseException { Assert.assertEquals("data-2015.08.22",select.getFrom().get(0).getIndex()); } + @Test + public void scriptFiledPlusLiteralTest() throws SqlParseException { + String query = "SELECT field1 + 3 FROM index/type"; + SQLExpr sqlExpr = queryToExpr(query); + Select select = parser.parseSelect((SQLQueryExpr) sqlExpr); + List fields = select.getFields(); + Assert.assertEquals(1,fields.size()); + Field field = fields.get(0); + Assert.assertTrue(field instanceof MethodField); + MethodField scriptMethod = (MethodField) field; + Assert.assertEquals("script",scriptMethod.getName().toLowerCase()); + Assert.assertEquals(2,scriptMethod.getParams().size()); + Assert.assertEquals("field1 + 3" ,scriptMethod.getParams().get(0).toString()); + Assert.assertEquals("doc['field1'].value + 3" ,scriptMethod.getParams().get(1).toString()); + } + + @Test + public void scriptFieldPlusFieldTest() throws SqlParseException { + String query = "SELECT field1 + field2 FROM index/type"; + SQLExpr sqlExpr = queryToExpr(query); + Select select = parser.parseSelect((SQLQueryExpr) sqlExpr); + List fields = select.getFields(); + Assert.assertEquals(1,fields.size()); + Field field = fields.get(0); + Assert.assertTrue(field instanceof MethodField); + MethodField scriptMethod = (MethodField) field; + Assert.assertEquals("script",scriptMethod.getName().toLowerCase()); + Assert.assertEquals(2,scriptMethod.getParams().size()); + Assert.assertEquals("field1 + field2" ,scriptMethod.getParams().get(0).toString()); + Assert.assertEquals("doc['field1'].value + doc['field2'].value" ,scriptMethod.getParams().get(1).toString()); + } + + + @Test + public void scriptLiteralPlusLiteralTest() throws SqlParseException { + String query = "SELECT 1 + 2 FROM index/type"; + SQLExpr sqlExpr = queryToExpr(query); + Select select = parser.parseSelect((SQLQueryExpr) sqlExpr); + List fields = select.getFields(); + Assert.assertEquals(1,fields.size()); + Field field = fields.get(0); + Assert.assertTrue(field instanceof MethodField); + MethodField scriptMethod = (MethodField) field; + Assert.assertEquals("script",scriptMethod.getName().toLowerCase()); + Assert.assertEquals(2,scriptMethod.getParams().size()); + Assert.assertEquals("1 + 2" ,scriptMethod.getParams().get(0).toString()); + Assert.assertEquals("1 + 2" ,scriptMethod.getParams().get(1).toString()); + } + + @Test + public void scriptFieldPlusFieldWithAliasTest() throws SqlParseException { + String query = "SELECT field1 + field2 as myfield FROM index/type"; + SQLExpr sqlExpr = queryToExpr(query); + Select select = parser.parseSelect((SQLQueryExpr) sqlExpr); + List fields = select.getFields(); + Assert.assertEquals(1,fields.size()); + Field field = fields.get(0); + Assert.assertTrue(field instanceof MethodField); + MethodField scriptMethod = (MethodField) field; + Assert.assertEquals("script",scriptMethod.getName().toLowerCase()); + Assert.assertEquals(2,scriptMethod.getParams().size()); + Assert.assertEquals("myfield" ,scriptMethod.getParams().get(0).toString()); + Assert.assertEquals("doc['field1'].value + doc['field2'].value" ,scriptMethod.getParams().get(1).toString()); + } + + private SQLExpr queryToExpr(String query) { return new ElasticSqlExprParser(query).expr(); } From 71e3365a95a062b5ad961d6c2e82fdd9306b1f66 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Fri, 23 Oct 2015 18:42:11 +0300 Subject: [PATCH 105/559] script on metrics aggregations --- .../org/nlpcn/es4sql/parse/FieldMaker.java | 68 ++++++++++++------- .../nlpcn/es4sql/query/maker/AggMaker.java | 50 +++++++++++--- .../org/nlpcn/es4sql/AggregationTest.java | 22 ++++++ .../java/org/nlpcn/es4sql/SqlParserTests.java | 35 ++++++++++ 4 files changed, 141 insertions(+), 34 deletions(-) diff --git a/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java b/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java index 669d4716..aeb9bb0a 100644 --- a/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java +++ b/src/main/java/org/nlpcn/es4sql/parse/FieldMaker.java @@ -5,7 +5,6 @@ import java.util.List; import com.alibaba.druid.sql.ast.expr.*; -import com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey; import org.nlpcn.es4sql.Util; import org.nlpcn.es4sql.domain.Field; import org.nlpcn.es4sql.domain.KVValue; @@ -21,28 +20,12 @@ public class FieldMaker { public static Field makeField(SQLExpr expr, String alias,String tableAlias) throws SqlParseException { if (expr instanceof SQLIdentifierExpr || expr instanceof SQLPropertyExpr || expr instanceof SQLVariantRefExpr) { - return handleIdentifer(expr, alias, tableAlias); + return handleIdentifier(expr, alias, tableAlias); } else if (expr instanceof SQLQueryExpr) { throw new SqlParseException("unknow field name : " + expr); } else if (expr instanceof SQLBinaryOpExpr) { //make a SCRIPT method field; - SQLBinaryOpExpr binaryExpr = (SQLBinaryOpExpr) expr; - List params = new ArrayList<>(); - - String scriptFieldAlias; - if(alias == null || alias.equals("")) - scriptFieldAlias = binaryExpr.toString(); - else - scriptFieldAlias = alias; - params.add(new SQLCharExpr(scriptFieldAlias)); - - Object left = getScriptValue(binaryExpr.getLeft()); - Object right = getScriptValue(binaryExpr.getRight()); - String script = String.format("%s %s %s" , left ,binaryExpr.getOperator().getName() , right); - - params.add(new SQLCharExpr(script)); - - return makeMethodField("script",params,null,null); + return makeScriptMethodField((SQLBinaryOpExpr) expr, alias); } else if (expr instanceof SQLAllColumnExpr) { } else if (expr instanceof SQLMethodInvokeExpr) { @@ -57,6 +40,25 @@ public static Field makeField(SQLExpr expr, String alias,String tableAlias) thro return null; } + private static Field makeScriptMethodField(SQLBinaryOpExpr binaryExpr, String alias) throws SqlParseException { + List params = new ArrayList<>(); + + String scriptFieldAlias; + if(alias == null || alias.equals("")) + scriptFieldAlias = binaryExpr.toString(); + else + scriptFieldAlias = alias; + params.add(new SQLCharExpr(scriptFieldAlias)); + + Object left = getScriptValue(binaryExpr.getLeft()); + Object right = getScriptValue(binaryExpr.getRight()); + String script = String.format("%s %s %s" , left ,binaryExpr.getOperator().getName() , right); + + params.add(new SQLCharExpr(script)); + + return makeMethodField("script",params,null,null); + } + private static Object getScriptValue(SQLExpr expr) throws SqlParseException { if (expr instanceof SQLIdentifierExpr || expr instanceof SQLPropertyExpr || expr instanceof SQLVariantRefExpr) { return "doc['" + expr.toString() + "'].value"; @@ -64,10 +66,10 @@ private static Object getScriptValue(SQLExpr expr) throws SqlParseException { else if (expr instanceof SQLValuableExpr){ return ((SQLValuableExpr)expr).getValue(); } - throw new SqlParseException("could not parse sqlBinaryOpExpr need to be identifier/valuable got" + expr.getClass().getTypeName() + " with value:" +expr.toString() ); + throw new SqlParseException("could not parse sqlBinaryOpExpr need to be identifier/valuable got" + expr.getClass().toString() + " with value:" +expr.toString() ); } - private static Field handleIdentifer(SQLExpr expr, String alias, String tableAlias) { + private static Field handleIdentifier(SQLExpr expr, String alias, String tableAlias) { String name = expr.toString().replace("`", ""); if(tableAlias==null) return new Field(name, alias); else if(tableAlias!=null){ @@ -84,11 +86,25 @@ else if(tableAlias!=null){ private static MethodField makeMethodField(String name, List arguments, SQLAggregateOption option, String alias) throws SqlParseException { List paramers = new LinkedList<>(); for (SQLExpr object : arguments) { - if (object instanceof SQLBinaryOpExpr) { - SQLExpr right = ((SQLBinaryOpExpr) object).getRight(); - Object value = Util.expr2Object(right); - paramers.add(new KVValue(((SQLBinaryOpExpr) object).getLeft().toString(), value)); - } else { + if (object instanceof SQLBinaryOpExpr) { + //todo: if operator is different from equal .... + SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) object; + if(!binaryOpExpr.getOperator().getName().equals("=")){ + paramers.add(new KVValue("script", makeScriptMethodField(binaryOpExpr,null))); + } + else { + SQLExpr right = binaryOpExpr.getRight(); + Object value = Util.expr2Object(right); + paramers.add(new KVValue(binaryOpExpr.getLeft().toString(), value)); + } + } else if(object instanceof SQLMethodInvokeExpr) { + SQLMethodInvokeExpr mExpr = (SQLMethodInvokeExpr) object; + if(mExpr.getMethodName().toLowerCase().equals("script")){ + KVValue script = new KVValue("script", makeMethodField(mExpr.getMethodName(), mExpr.getParameters(), null, alias)); + paramers.add(script); + } + else throw new SqlParseException("only support script as nested functions"); + }else { paramers.add(new KVValue(Util.expr2Object(object))); } diff --git a/src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java b/src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java index b1f6244a..e941c349 100644 --- a/src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java +++ b/src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java @@ -18,6 +18,8 @@ import org.elasticsearch.search.aggregations.bucket.range.RangeBuilder; import org.elasticsearch.search.aggregations.bucket.range.date.DateRangeBuilder; import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder; +import org.elasticsearch.search.aggregations.metrics.MetricsAggregationBuilder; +import org.elasticsearch.search.aggregations.metrics.ValuesSourceMetricsAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.tophits.TopHitsBuilder; import org.elasticsearch.search.sort.SortOrder; import org.nlpcn.es4sql.Util; @@ -57,21 +59,37 @@ public AggregationBuilder makeGroupAgg(Field field) throws SqlParseException */ public AbstractAggregationBuilder makeFieldAgg(MethodField field, AbstractAggregationBuilder parent) throws SqlParseException { groupMap.put(field.getAlias(), new KVValue("FIELD", parent)); + ValuesSourceMetricsAggregationBuilder builder; + field.setAlias(fixAlias(field.getAlias())); switch (field.getName().toUpperCase()) { case "SUM": - return AggregationBuilders.sum(field.getAlias()).field(field.getParams().get(0).toString()); + builder = AggregationBuilders.sum(field.getAlias()); + addFieldOrScriptToAggregation(field, builder); + return builder; case "MAX": - return AggregationBuilders.max(field.getAlias()).field(field.getParams().get(0).toString()); + builder = AggregationBuilders.max(field.getAlias()); + addFieldOrScriptToAggregation(field, builder); + return builder; case "MIN": - return AggregationBuilders.min(field.getAlias()).field(field.getParams().get(0).toString()); + builder = AggregationBuilders.min(field.getAlias()); + addFieldOrScriptToAggregation(field, builder); + return builder; case "AVG": - return AggregationBuilders.avg(field.getAlias()).field(field.getParams().get(0).toString()); + builder = AggregationBuilders.avg(field.getAlias()); + addFieldOrScriptToAggregation(field, builder); + return builder; case "STATS": - return AggregationBuilders.stats(field.getAlias()).field(field.getParams().get(0).toString()); + builder = AggregationBuilders.stats(field.getAlias()); + addFieldOrScriptToAggregation(field, builder); + return builder; case "EXTENDED_STATS": - return AggregationBuilders.extendedStats(field.getAlias()).field(field.getParams().get(0).toString()); + builder = AggregationBuilders.extendedStats(field.getAlias()); + addFieldOrScriptToAggregation(field, builder); + return builder; case "PERCENTILES": - return AggregationBuilders.percentiles(field.getAlias()).field(field.getParams().get(0).toString()); + builder = AggregationBuilders.percentiles(field.getAlias()); + addFieldOrScriptToAggregation(field, builder); + return builder; case "TOPHITS": return makeTopHitsAgg(field); case "COUNT": @@ -82,7 +100,23 @@ public AbstractAggregationBuilder makeFieldAgg(MethodField field, AbstractAggreg } } - private AggregationBuilder makeRangeGroup(MethodField field) throws SqlParseException { + private String fixAlias(String alias) { + //because [ is not legal as alias + return alias.replaceAll("\\[","(").replaceAll("\\]",")"); + } + + private void addFieldOrScriptToAggregation(MethodField field, ValuesSourceMetricsAggregationBuilder builder) { + KVValue kvValue = field.getParams().get(0); + if(kvValue.key==null || !kvValue.key.equals("script") ) + builder.field(kvValue.toString()); + else + { + //todo: support different lang script + builder.script(((MethodField)kvValue.value).getParams().get(1).toString()); + } + } + + private AggregationBuilder makeRangeGroup(MethodField field) throws SqlParseException { switch (field.getName().toLowerCase()) { case "range": return rangeBuilder(field); diff --git a/src/test/java/org/nlpcn/es4sql/AggregationTest.java b/src/test/java/org/nlpcn/es4sql/AggregationTest.java index cc4a0a34..3cd2ffcd 100644 --- a/src/test/java/org/nlpcn/es4sql/AggregationTest.java +++ b/src/test/java/org/nlpcn/es4sql/AggregationTest.java @@ -47,6 +47,28 @@ public void sumTest() throws IOException, SqlParseException, SQLFeatureNotSuppor assertThat(sum.getValue(), equalTo(25714837.0)); } + // script on metric aggregation tests. uncomment if your elastic has scripts enable +// @Test +// public void sumWithScriptTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { +// Aggregations result = query(String.format("SELECT SUM(script('','doc[\\'balance\\'].value + doc[\\'balance\\'].value')) as doubleSum FROM %s/account", TEST_INDEX)); +// Sum sum = result.get("doubleSum"); +// assertThat(sum.getValue(), equalTo(25714837.0*2)); +// } +// +// @Test +// public void sumWithImplicitScriptTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { +// Aggregations result = query(String.format("SELECT SUM(balance + balance) as doubleSum FROM %s/account", TEST_INDEX)); +// Sum sum = result.get("doubleSum"); +// assertThat(sum.getValue(), equalTo(25714837.0*2)); +// } +// +// @Test +// public void sumWithScriptTestNoAlias() throws IOException, SqlParseException, SQLFeatureNotSupportedException { +// Aggregations result = query(String.format("SELECT SUM(balance + balance) FROM %s/account", TEST_INDEX)); +// Sum sum = result.get("SUM(script=script(balance + balance,doc('balance').value + doc('balance').value))"); +// assertThat(sum.getValue(), equalTo(25714837.0*2)); +// } + @Test public void minTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { Aggregations result = query(String.format("SELECT MIN(age) FROM %s/account", TEST_INDEX)); diff --git a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java index 7e46bdfa..f9fe2292 100644 --- a/src/test/java/org/nlpcn/es4sql/SqlParserTests.java +++ b/src/test/java/org/nlpcn/es4sql/SqlParserTests.java @@ -482,6 +482,41 @@ public void scriptFieldPlusFieldWithAliasTest() throws SqlParseException { } + @Test + public void explicitScriptOnAggregation() throws SqlParseException { + String query = "SELECT avg( script('add','doc[\\'field1\\'].value + doc[\\'field2\\'].value') ) FROM index/type"; + SQLExpr sqlExpr = queryToExpr(query); + Select select = parser.parseSelect((SQLQueryExpr) sqlExpr); + List fields = select.getFields(); + Assert.assertEquals(1,fields.size()); + Field field = fields.get(0); + Assert.assertTrue(field instanceof MethodField); + MethodField avgMethodField = (MethodField) field; + Assert.assertEquals("avg",avgMethodField.getName().toLowerCase()); + Assert.assertEquals(1,avgMethodField.getParams().size()); + MethodField scriptMethod = (MethodField) avgMethodField.getParams().get(0).value; + Assert.assertEquals("script",scriptMethod.getName().toLowerCase()); + Assert.assertEquals(2,scriptMethod.getParams().size()); + Assert.assertEquals("doc['field1'].value + doc['field2'].value" ,scriptMethod.getParams().get(1).toString()); + } + @Test + + public void implicitScriptOnAggregation() throws SqlParseException { + String query = "SELECT avg(field1 + field2) FROM index/type"; + SQLExpr sqlExpr = queryToExpr(query); + Select select = parser.parseSelect((SQLQueryExpr) sqlExpr); + List fields = select.getFields(); + Assert.assertEquals(1,fields.size()); + Field field = fields.get(0); + Assert.assertTrue(field instanceof MethodField); + MethodField avgMethodField = (MethodField) field; + Assert.assertEquals("avg",avgMethodField.getName().toLowerCase()); + Assert.assertEquals(1,avgMethodField.getParams().size()); + MethodField scriptMethod = (MethodField) avgMethodField.getParams().get(0).value; + Assert.assertEquals("script",scriptMethod.getName().toLowerCase()); + Assert.assertEquals(2,scriptMethod.getParams().size()); + Assert.assertEquals("doc['field1'].value + doc['field2'].value" ,scriptMethod.getParams().get(1).toString()); + } private SQLExpr queryToExpr(String query) { return new ElasticSqlExprParser(query).expr(); } From 7c1e253da7fce0a581f19c2cb5d13f999912da9e Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Mon, 26 Oct 2015 23:49:42 +0200 Subject: [PATCH 106/559] more site options. autoSave(download directly), flat results , and change csv delimiter --- src/_site/controllers.js | 132 +++++++++++++++++++++++++++++++++------ src/_site/index.html | 19 +++++- src/_site/query.js | 93 ++++++++++++++++++++++++--- src/_site/style.css | 17 +++++ 4 files changed, 231 insertions(+), 30 deletions(-) diff --git a/src/_site/controllers.js b/src/_site/controllers.js index 6eb1ad42..594362a4 100644 --- a/src/_site/controllers.js +++ b/src/_site/controllers.js @@ -2,7 +2,9 @@ var elasticsearchSqlApp = angular.module('elasticsearchSqlApp', ["ngAnimate", "ngSanitize"]); elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) { + scroll_url = "_search/scroll?scroll=1m&scroll_id="; $scope.url = getUrl(); + $scope.showResults = false; $scope.error = ""; $scope.resultsColumns = []; $scope.resultsRows = []; @@ -12,7 +14,10 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) $scope.resultExplan = false; $scope.scrollId = null; $scope.gotNext = false; - + $scope.delimiter = ','; + $scope.amountDescription = ""; + var fetched = 0; + var total = 0 ; // pull version and put it on the scope $http.get($scope.url).success(function (data) { $http.get($scope.url + "_nodes/" + data.name).success(function (nodeData) { @@ -25,23 +30,87 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) }); }); + function searchTillEndAndExportCsv (scrollId) { + //todo: get total amount show fetched/total + head = [] + body = [] + $scope.showResults = true; + callScrollAndFillBodyTillEnd(scrollId,head,body,true); + } + function updateDescription (handler) { + total = handler.getTotal(); + fetched += handler.getCurrentHitsSize(); + $scope.amountDescription = fetched + "/" + total + } + function callScrollAndFillBodyTillEnd (scrollId,head,body,firstTime) { + var url = $scope.url + scroll_url + scrollId; + $http.get(url) + .success(function(data, status, headers, config) { + + var handler = ResultHandlerFactory.create(data,$scope.isFlat); + + updateDescription(handler); + recieved = handler.getBody() + if(body.length > 0){ + body = body.concat(recieved); + //todo: extend head? + head = handler.getHead(); + } + else { + body = recieved; + head = handler.getHead(); + + } + if(recieved == undefined || recieved.length == undefined || recieved.length == 0){ + if(firstTime){ + callScrollAndFillBodyTillEnd(handler.getScrollId(),head,body,false); + } + else { + exportCSVWithoutScope(head,body); + } + } + else { + callScrollAndFillBodyTillEnd(handler.getScrollId(),head,body,false); + } + + + }) + .error(function(data, status, headers, config) { + if(data == "") { + $scope.error = "Error occured! response is not avalible."; + } + else { + $scope.error = JSON.stringify(data); + $scope.scrollId = undefined; + } + }) + .finally(function() { + $scope.nextLoading = false; + $scope.$apply() + }); + + + // body... + } + $scope.nextSearch = function(){ $scope.error = ""; $scope.nextLoading = true; $scope.$apply(); - if($scope.scrollId == null || $scope.scrollId == "" ){ + if($scope.scrollId == undefined || $scope.scrollId == "" ){ $scope.error = "tryed scrolling with empty scrollId"; return; } - $http.get($scope.url + "_search/scroll?scroll=1m&scroll_id=" + $scope.scrollId) + $http.get($scope.url + scroll_url + $scope.scrollId) .success(function(data, status, headers, config) { - var handler = ResultHandlerFactory.create(data); + var handler = ResultHandlerFactory.create(data,$scope.isFlat); + updateDescription(handler); var body = handler.getBody() - if(body.length ==null || body.length == 0){ + if(body.length == undefined || body.length == 0){ $scope.gotNext=false; } else @@ -79,6 +148,9 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) $scope.search = function() { // Reset results and error box $scope.error = ""; + fetched = 0; + total = 0; + $scope.amountDescription = 0; $scope.resultsColumns = []; $scope.resultsRows = []; $scope.searchLoading = true; @@ -91,14 +163,30 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) $http.post($scope.url + "_sql", query) .success(function(data, status, headers, config) { - var handler = ResultHandlerFactory.create(data); + var handler = ResultHandlerFactory.create(data,$scope.isFlat); + updateDescription(handler); if(handler.isScroll){ - $scope.gotNext=true; + $scope.scrollId = handler.getScrollId(); + if($scope.isAutoSave){ + searchTillEndAndExportCsv($scope.scrollId); + } + else { + $scope.gotNext=true; + } } - $scope.resultsColumns = handler.getHead(); - $scope.resultsRows = handler.getBody(); + + else { + if($scope.isAutoSave){ + $scope.showResults=true; + exportCSVWithoutScope(handler.getHead(),handler.getBody()); + } + else { + $scope.resultsColumns = handler.getHead(); + $scope.resultsRows = handler.getBody(); + } + } }) .error(function(data, status, headers, config) { if(data == "") { @@ -147,19 +235,23 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) } + function exportCSVWithoutScope(columns,rows) { + var delimiter = $scope.delimiter; + var data =arr2csvStr(columns,delimiter) ; + for(var i=0; iKeyboard tips
-
+
+

Options

+
+ +
+
+ +
+
+ + +
+
+

SQL Query

@@ -126,9 +139,9 @@

SQL Query

-
+
-

Results

+

Results {{amountDescription}}

diff --git a/src/_site/query.js b/src/_site/query.js index 2206ad1a..4f758ba8 100644 --- a/src/_site/query.js +++ b/src/_site/query.js @@ -3,7 +3,7 @@ Returns the right Result Handler depend on the results */ var ResultHandlerFactory = { - "create": function(data) { + "create": function(data,isFlat) { function isSearch(){ return "hits" in data } @@ -16,7 +16,7 @@ var ResultHandlerFactory = { } if(isSearch()){ - return isAggregation() ? new AggregationQueryResultHandler(data) : new DefaultQueryResultHandler(data) + return isAggregation() ? new AggregationQueryResultHandler(data) : new DefaultQueryResultHandler(data,isFlat) } if(isDelete()){ @@ -35,7 +35,7 @@ var ResultHandlerFactory = { in case of regular query (Not aggregation) */ -var DefaultQueryResultHandler = function(data) { +var DefaultQueryResultHandler = function(data,isFlat) { // createScheme by traverse hits field function createScheme() { @@ -44,17 +44,26 @@ var DefaultQueryResultHandler = function(data) { for(index=0; index Date: Wed, 28 Oct 2015 23:38:04 +0200 Subject: [PATCH 107/559] scripted_metric support --- .../org/nlpcn/es4sql/domain/MethodField.java | 10 ++++ .../java/org/nlpcn/es4sql/domain/Select.java | 2 +- .../nlpcn/es4sql/query/maker/AggMaker.java | 58 +++++++++++++++++++ .../org/nlpcn/es4sql/AggregationTest.java | 13 ++++- 4 files changed, 81 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/nlpcn/es4sql/domain/MethodField.java b/src/main/java/org/nlpcn/es4sql/domain/MethodField.java index 1f8fbace..88e46956 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/MethodField.java +++ b/src/main/java/org/nlpcn/es4sql/domain/MethodField.java @@ -1,6 +1,8 @@ package org.nlpcn.es4sql.domain; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.nlpcn.es4sql.Util; @@ -27,6 +29,14 @@ public List getParams() { return params; } + public Map getParamsAsMap(){ + Map paramsAsMap = new HashMap<>(); + for(KVValue kvValue : this.params){ + paramsAsMap.put(kvValue.key,kvValue.value); + } + return paramsAsMap; + } + @Override public String toString() { if (option != null) { diff --git a/src/main/java/org/nlpcn/es4sql/domain/Select.java b/src/main/java/org/nlpcn/es4sql/domain/Select.java index 46f506f1..e753fdb9 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/Select.java +++ b/src/main/java/org/nlpcn/es4sql/domain/Select.java @@ -16,7 +16,7 @@ public class Select extends Query { // Using this functions, will cause query to execute as aggregation. - private final List aggsFunctions = Arrays.asList("SUM", "MAX", "MIN", "AVG", "TOPHITS", "COUNT", "STATS","EXTENDED_STATS","PERCENTILES"); + private final List aggsFunctions = Arrays.asList("SUM", "MAX", "MIN", "AVG", "TOPHITS", "COUNT", "STATS","EXTENDED_STATS","PERCENTILES","SCRIPTED_METRIC"); private List hints = new ArrayList<>(); private List fields = new ArrayList<>(); private List> groupBys = new ArrayList<>(); diff --git a/src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java b/src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java index e941c349..ab84a259 100644 --- a/src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java +++ b/src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java @@ -20,6 +20,7 @@ import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder; import org.elasticsearch.search.aggregations.metrics.MetricsAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.ValuesSourceMetricsAggregationBuilder; +import org.elasticsearch.search.aggregations.metrics.scripted.ScriptedMetricBuilder; import org.elasticsearch.search.aggregations.metrics.tophits.TopHitsBuilder; import org.elasticsearch.search.sort.SortOrder; import org.nlpcn.es4sql.Util; @@ -92,6 +93,8 @@ public AbstractAggregationBuilder makeFieldAgg(MethodField field, AbstractAggreg return builder; case "TOPHITS": return makeTopHitsAgg(field); + case "SCRIPTED_METRIC": + return scriptedMetric(field); case "COUNT": groupMap.put(field.getAlias(), new KVValue("COUNT", parent)); return makeCountAgg(field); @@ -136,6 +139,61 @@ private AggregationBuilder makeRangeGroup(MethodField field) throws SqlParseE } + private AbstractAggregationBuilder scriptedMetric(MethodField field) throws SqlParseException { + String aggName = gettAggNameFromParamsOrAlias(field); + ScriptedMetricBuilder scriptedMetricBuilder = AggregationBuilders.scriptedMetric(aggName); + Map scriptedMetricParams = field.getParamsAsMap(); + if(!scriptedMetricParams.containsKey("map_script") && !scriptedMetricParams.containsKey("map_script_id") && !scriptedMetricParams.containsKey("map_script_file")){ + throw new SqlParseException("scripted metric parameters must contain map_script/map_script_id/map_script_file parameter"); + } + for(Map.Entry param : scriptedMetricParams.entrySet()) { + String paramValue = param.getValue().toString(); + switch (param.getKey().toLowerCase()) { + case "map_script": + scriptedMetricBuilder.mapScript(paramValue); + break; + case "map_script_id": + scriptedMetricBuilder.mapScriptId(paramValue); + break; + case "map_script_file": + scriptedMetricBuilder.mapScriptFile(paramValue); + break; + case "init_script": + scriptedMetricBuilder.initScript(paramValue); + break; + case "init_script_id": + scriptedMetricBuilder.initScriptId(paramValue); + break; + case "init_script_file": + scriptedMetricBuilder.initScriptFile(paramValue); + break; + case "combine_script": + scriptedMetricBuilder.combineScript(paramValue); + break; + case "combine_script_id": + scriptedMetricBuilder.combineScriptId(paramValue); + break; + case "combine_script_file": + scriptedMetricBuilder.combineScriptFile(paramValue); + break; + case "reduce_script": + scriptedMetricBuilder.reduceScript(paramValue); + break; + case "reduce_script_id": + scriptedMetricBuilder.reduceScriptId(paramValue); + break; + case "reduce_script_file": + scriptedMetricBuilder.reduceScriptFile(paramValue); + break; + case "alias": + break; + default: + throw new SqlParseException("scripted_metric err or not define field " + param.getKey()); + } + } + return scriptedMetricBuilder; + } + private AggregationBuilder geohashGrid(MethodField field) throws SqlParseException { String aggName = gettAggNameFromParamsOrAlias(field); GeoHashGridBuilder geoHashGrid = AggregationBuilders.geohashGrid(aggName); diff --git a/src/test/java/org/nlpcn/es4sql/AggregationTest.java b/src/test/java/org/nlpcn/es4sql/AggregationTest.java index 3cd2ffcd..ec6573a4 100644 --- a/src/test/java/org/nlpcn/es4sql/AggregationTest.java +++ b/src/test/java/org/nlpcn/es4sql/AggregationTest.java @@ -14,6 +14,7 @@ import org.elasticsearch.search.aggregations.metrics.max.Max; import org.elasticsearch.search.aggregations.metrics.min.Min; import org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles; +import org.elasticsearch.search.aggregations.metrics.scripted.ScriptedMetric; import org.elasticsearch.search.aggregations.metrics.stats.Stats; import org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats; import org.elasticsearch.search.aggregations.metrics.sum.Sum; @@ -47,7 +48,8 @@ public void sumTest() throws IOException, SqlParseException, SQLFeatureNotSuppor assertThat(sum.getValue(), equalTo(25714837.0)); } - // script on metric aggregation tests. uncomment if your elastic has scripts enable + // script on metric aggregation tests. uncomment if your elastic has scripts enable (disabled by default) + //todo: find a way to check if scripts are enabled // @Test // public void sumWithScriptTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { // Aggregations result = query(String.format("SELECT SUM(script('','doc[\\'balance\\'].value + doc[\\'balance\\'].value')) as doubleSum FROM %s/account", TEST_INDEX)); @@ -67,6 +69,15 @@ public void sumTest() throws IOException, SqlParseException, SQLFeatureNotSuppor // Aggregations result = query(String.format("SELECT SUM(balance + balance) FROM %s/account", TEST_INDEX)); // Sum sum = result.get("SUM(script=script(balance + balance,doc('balance').value + doc('balance').value))"); // assertThat(sum.getValue(), equalTo(25714837.0*2)); +// } +// +// @Test +// public void scriptedMetricAggregation() throws SQLFeatureNotSupportedException, SqlParseException { +// Aggregations result = query ("select scripted_metric('map_script'='if(doc[\\'balance\\'].value > 49670){ if(!_agg.containsKey(\\'ages\\')) { _agg.put(\\'ages\\',doc[\\'age\\'].value); } " + +// "else { _agg.put(\\'ages\\',_agg.get(\\'ages\\')+doc[\\'age\\'].value); }}'," + +// "'reduce_script'='sumThem = 0; for (a in _aggs) { if(a.containsKey(\\'ages\\')){ sumThem += a.get(\\'ages\\');} }; return sumThem;') as wierdSum from " + TEST_INDEX + "/account"); +// ScriptedMetric metric = result.get("wierdSum"); +// Assert.assertEquals(136L,metric.aggregation()); // } @Test From c87d6fd823936b63e29a2f29a165e2f9a3323462 Mon Sep 17 00:00:00 2001 From: eliran moyal Date: Fri, 30 Oct 2015 22:20:11 +0200 Subject: [PATCH 108/559] new table presenter! + date_histogram now shows key_as_string --- src/_site/controllers.js | 183 +- src/_site/index.html | 34 +- src/_site/query.js | 13 +- src/_site/style.css | 13 + src/_site/table_presenter.js | 98 + .../dataTables/css/dataTables.bootstrap.css | 167 + .../css/dataTables.bootstrap.min.css | 1 + .../dataTables/css/dataTables.foundation.css | 104 + .../css/dataTables.foundation.min.css | 1 + .../dataTables/css/dataTables.jqueryui.css | 473 + .../css/dataTables.jqueryui.min.css | 1 + .../dataTables/css/jquery.dataTables.css | 450 + .../dataTables/css/jquery.dataTables.min.css | 1 + .../css/jquery.dataTables_themeroller.css | 416 + .../vendor/dataTables/images/sort_asc.png | Bin 0 -> 160 bytes .../dataTables/images/sort_asc_disabled.png | Bin 0 -> 148 bytes .../vendor/dataTables/images/sort_both.png | Bin 0 -> 201 bytes .../vendor/dataTables/images/sort_desc.png | Bin 0 -> 158 bytes .../dataTables/images/sort_desc_disabled.png | Bin 0 -> 146 bytes .../dataTables/js/dataTables.bootstrap.js | 206 + .../dataTables/js/dataTables.bootstrap.min.js | 8 + .../dataTables/js/dataTables.foundation.js | 146 + .../js/dataTables.foundation.min.js | 7 + .../dataTables/js/dataTables.jqueryui.js | 156 + .../dataTables/js/dataTables.jqueryui.min.js | 9 + .../vendor/dataTables/js/jquery.dataTables.js | 15129 ++++++++++++++++ .../dataTables/js/jquery.dataTables.min.js | 163 + 27 files changed, 17688 insertions(+), 91 deletions(-) create mode 100644 src/_site/table_presenter.js create mode 100644 src/_site/vendor/dataTables/css/dataTables.bootstrap.css create mode 100644 src/_site/vendor/dataTables/css/dataTables.bootstrap.min.css create mode 100644 src/_site/vendor/dataTables/css/dataTables.foundation.css create mode 100644 src/_site/vendor/dataTables/css/dataTables.foundation.min.css create mode 100644 src/_site/vendor/dataTables/css/dataTables.jqueryui.css create mode 100644 src/_site/vendor/dataTables/css/dataTables.jqueryui.min.css create mode 100644 src/_site/vendor/dataTables/css/jquery.dataTables.css create mode 100644 src/_site/vendor/dataTables/css/jquery.dataTables.min.css create mode 100644 src/_site/vendor/dataTables/css/jquery.dataTables_themeroller.css create mode 100644 src/_site/vendor/dataTables/images/sort_asc.png create mode 100644 src/_site/vendor/dataTables/images/sort_asc_disabled.png create mode 100644 src/_site/vendor/dataTables/images/sort_both.png create mode 100644 src/_site/vendor/dataTables/images/sort_desc.png create mode 100644 src/_site/vendor/dataTables/images/sort_desc_disabled.png create mode 100644 src/_site/vendor/dataTables/js/dataTables.bootstrap.js create mode 100644 src/_site/vendor/dataTables/js/dataTables.bootstrap.min.js create mode 100644 src/_site/vendor/dataTables/js/dataTables.foundation.js create mode 100644 src/_site/vendor/dataTables/js/dataTables.foundation.min.js create mode 100644 src/_site/vendor/dataTables/js/dataTables.jqueryui.js create mode 100644 src/_site/vendor/dataTables/js/dataTables.jqueryui.min.js create mode 100644 src/_site/vendor/dataTables/js/jquery.dataTables.js create mode 100644 src/_site/vendor/dataTables/js/jquery.dataTables.min.js diff --git a/src/_site/controllers.js b/src/_site/controllers.js index 594362a4..d65c9475 100644 --- a/src/_site/controllers.js +++ b/src/_site/controllers.js @@ -1,24 +1,30 @@ var elasticsearchSqlApp = angular.module('elasticsearchSqlApp', ["ngAnimate", "ngSanitize"]); -elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) { +elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce,$compile) { scroll_url = "_search/scroll?scroll=1m&scroll_id="; $scope.url = getUrl(); $scope.showResults = false; $scope.error = ""; - $scope.resultsColumns = []; + $scope.resultsColumns =[]; $scope.resultsRows = []; $scope.searchLoading = false; $scope.explainLoading = false; $scope.nextLoading = false; $scope.resultExplan = false; - $scope.scrollId = null; + $scope.scrollId = undefined; + $scope.amountDescription = ""; + var fetched = 0; + var total = 0 ; + //checkboxes $scope.gotNext = false; + $scope.useOldTable = false; $scope.delimiter = ','; - $scope.amountDescription = ""; - var fetched = 0; - var total = 0 ; - // pull version and put it on the scope + + var tablePresenter = new TablePresenter('searchResult','#searchResultZone'); + + + // pull version and put it on the scope $http.get($scope.url).success(function (data) { $http.get($scope.url + "_nodes/" + data.name).success(function (nodeData) { var node = nodeData.nodes[Object.keys(nodeData.nodes)[0]]; @@ -30,73 +36,12 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) }); }); - function searchTillEndAndExportCsv (scrollId) { - //todo: get total amount show fetched/total - head = [] - body = [] - $scope.showResults = true; - callScrollAndFillBodyTillEnd(scrollId,head,body,true); - } - function updateDescription (handler) { - total = handler.getTotal(); - fetched += handler.getCurrentHitsSize(); - $scope.amountDescription = fetched + "/" + total - } - function callScrollAndFillBodyTillEnd (scrollId,head,body,firstTime) { - var url = $scope.url + scroll_url + scrollId; - $http.get(url) - .success(function(data, status, headers, config) { - - var handler = ResultHandlerFactory.create(data,$scope.isFlat); - - updateDescription(handler); - recieved = handler.getBody() - if(body.length > 0){ - body = body.concat(recieved); - //todo: extend head? - head = handler.getHead(); - } - else { - body = recieved; - head = handler.getHead(); - - } - if(recieved == undefined || recieved.length == undefined || recieved.length == 0){ - if(firstTime){ - callScrollAndFillBodyTillEnd(handler.getScrollId(),head,body,false); - } - else { - exportCSVWithoutScope(head,body); - } - } - else { - callScrollAndFillBodyTillEnd(handler.getScrollId(),head,body,false); - } - - - }) - .error(function(data, status, headers, config) { - if(data == "") { - $scope.error = "Error occured! response is not avalible."; - } - else { - $scope.error = JSON.stringify(data); - $scope.scrollId = undefined; - } - }) - .finally(function() { - $scope.nextLoading = false; - $scope.$apply() - }); - - - // body... - } $scope.nextSearch = function(){ $scope.error = ""; $scope.nextLoading = true; $scope.$apply(); + var needToBuildTable = false; if($scope.scrollId == undefined || $scope.scrollId == "" ){ @@ -119,12 +64,13 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) } if($scope.resultsRows.length > 0){ + tablePresenter.addRows(handler.getBody()); $scope.resultsRows = $scope.resultsRows.concat(handler.getBody()); } else { $scope.resultsColumns = handler.getHead(); $scope.resultsRows = handler.getBody(); - + needToBuildTable = true; } @@ -135,12 +81,16 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) } else { $scope.error = JSON.stringify(data); - $scope.scrollId = null; + $scope.scrollId = undefined; } }) .finally(function() { $scope.nextLoading = false; - $scope.$apply() + $scope.$apply(); + if(needToBuildTable) { + tablePresenter.createOrReplace($scope.resultsColumns,$scope.resultsRows); + } + }); } @@ -154,12 +104,12 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) $scope.resultsColumns = []; $scope.resultsRows = []; $scope.searchLoading = true; - $scope.$apply(); + $scope.resultExplan = false; - + $scope.$apply(); saveUrl() - var query = window.editor.getValue(); + var query = window.editor.getValue(); $http.post($scope.url + "_sql", query) .success(function(data, status, headers, config) { @@ -185,6 +135,7 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) else { $scope.resultsColumns = handler.getHead(); $scope.resultsRows = handler.getBody(); + } } }) @@ -198,7 +149,11 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) }) .finally(function() { $scope.searchLoading = false; - $scope.$apply() + $scope.$apply(); + if($scope.resultsColumns.length >0){ + tablePresenter.createOrReplace($scope.resultsColumns,$scope.resultsRows); + } + }); } @@ -247,6 +202,82 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce) return true; } + $scope.onChangeTablePresnterType = function(){ + //value = ? + value = $scope.useOldTable; + tablePresenter.destroy(); + tablePresenter.changeTableType(value,$compile,$scope); + } + + function searchTillEndAndExportCsv (scrollId) { + //todo: get total amount show fetched/total + head = [] + body = [] + $scope.showResults = true; + callScrollAndFillBodyTillEnd(scrollId,head,body,true); + } + function updateDescription (handler) { + total = handler.getTotal(); + fetched += handler.getCurrentHitsSize(); + if(total == undefined){ + $scope.amountDescription = fetched + } + else { + $scope.amountDescription = fetched + "/" + total + } + } + function callScrollAndFillBodyTillEnd (scrollId,head,body,firstTime) { + var url = $scope.url + scroll_url + scrollId; + $http.get(url) + .success(function(data, status, headers, config) { + + var handler = ResultHandlerFactory.create(data,$scope.isFlat); + + updateDescription(handler); + recieved = handler.getBody() + if(body.length > 0){ + body = body.concat(recieved); + //todo: extend head? + head = handler.getHead(); + } + else { + body = recieved; + head = handler.getHead(); + + } + if(recieved == undefined || recieved.length == undefined || recieved.length == 0){ + if(firstTime){ + callScrollAndFillBodyTillEnd(handler.getScrollId(),head,body,false); + } + else { + exportCSVWithoutScope(head,body); + } + } + else { + callScrollAndFillBodyTillEnd(handler.getScrollId(),head,body,false); + } + + + }) + .error(function(data, status, headers, config) { + if(data == "") { + $scope.error = "Error occured! response is not avalible."; + } + else { + $scope.error = JSON.stringify(data); + $scope.scrollId = undefined; + } + }) + .finally(function() { + $scope.nextLoading = false; + $scope.$apply() + }); + + + // body... + } + + $scope.exportCSV = function() { var columns = $scope.resultsColumns ; diff --git a/src/_site/index.html b/src/_site/index.html index a4133813..6c70eb49 100644 --- a/src/_site/index.html +++ b/src/_site/index.html @@ -12,7 +12,8 @@ - + + @@ -108,8 +109,12 @@

Options

- + +
+
+
+
@@ -120,15 +125,16 @@

SQL Query

- + - - + +
@@ -141,12 +147,12 @@

SQL Query

-

Results {{amountDescription}}

+

Results ({{amountDescription}})

-
+
- +
- +
@@ -181,9 +187,11 @@

Results

+ - + + @@ -199,10 +207,12 @@

Results

+ - + + diff --git a/src/_site/query.js b/src/_site/query.js index 4f758ba8..f3304b2a 100644 --- a/src/_site/query.js +++ b/src/_site/query.js @@ -65,7 +65,7 @@ var DefaultQueryResultHandler = function(data,isFlat) { this.head = createScheme() this.isFlat = isFlat; this.scrollId = data["_scroll_id"] - this.isScroll = this.scrollId!=null && this.scrollId!=""; + this.isScroll = this.scrollId!=undefined && this.scrollId!=""; }; DefaultQueryResultHandler.prototype.isScroll = function() { @@ -189,7 +189,14 @@ var AggregationQueryResultHandler = function(data) { else { var obj = $.extend({}, additionalColumns) if(bucketName != undefined) { - obj[bucketName] = bucket.key + if(bucketName != undefined) { + if("key_as_string" in bucket){ + obj[bucketName] = bucket["key_as_string"] + } + else { + obj[bucketName] = bucket.key + } + } } for(var field in bucket) { @@ -266,7 +273,7 @@ AggregationQueryResultHandler.prototype.getBody = function() { AggregationQueryResultHandler.prototype.getTotal = function() { - return "?"; + return undefined; }; AggregationQueryResultHandler.prototype.getCurrentHitsSize = function() { diff --git a/src/_site/style.css b/src/_site/style.css index 42ed8a4a..8e029ccd 100644 --- a/src/_site/style.css +++ b/src/_site/style.css @@ -116,6 +116,13 @@ body { margin-top: 10px; } +#exportCSV { + float:left; + margin-top: 10px; + height: 100%; +} + + .explain-button { float:right; margin-right: 25px; @@ -125,6 +132,7 @@ body { .next-button { float:left; + margin-right:25px; margin-top: 10px; } @@ -194,4 +202,9 @@ body { .delimiterLable { font-weight: 100; +} + + +#searchResultZone{ + width: 80%; } \ No newline at end of file diff --git a/src/_site/table_presenter.js b/src/_site/table_presenter.js new file mode 100644 index 00000000..958b2972 --- /dev/null +++ b/src/_site/table_presenter.js @@ -0,0 +1,98 @@ + +var TablePresenter = function(tableId,tableZoneSelector) { + this.tableId = tableId; + this.tableSelector = "#"+tableId; + this.tableZoneSelector = tableZoneSelector; + this.oldTable = false; + this.table = undefined; +} + +TablePresenter.prototype.addRows = function(rows) { + if(this.oldTable) return; + table.rows.add(createRows(rows)).draw(); +}; + +TablePresenter.prototype.createOrReplace = function(columns,rows) { + if(this.oldTable) return; + + clearTableIfNeeded(this.table,this.tableZoneSelector,this.tableId); + + dataTablesColumns = createColumns(columns); + + dataTableRows = createRows(rows); + + this.table = $('#searchResult').DataTable( + { + "aaData": dataTableRows, + "aoColumns": dataTablesColumns, + "destroy": true, + "scrollX": true, + "lengthMenu": [[10, 25, 50,100, -1], [10, 25, 50,100, "All"]] + }); +}; + +TablePresenter.prototype.destroy = function() { + clearTableIfNeeded(this.table,this.tableZoneSelector,this.tableId); +}; + +TablePresenter.prototype.changeTableType = function(old,$compile,$scope){ + if(old) { + createOldTable(this.tableZoneSelector,this.tableId ,$compile,$scope); + this.oldTable = true; + } + else { + this.oldTable = false; + } +} + + +function clearTableIfNeeded(table,tableZoneSelector,tableId){ + if(table != undefined){ + table.clear(); + $(tableZoneSelector).empty(); + $(tableZoneSelector).html('
'); + } +} + +function createColumns(columns){ + dataTablesColumns = []; + + for(i = 0 ; i < columns.length ; i++){ + var column = columns[i]; + dataTableColumn = {} + dataTableColumn["data"] = column.replace(/\./g,"&"); + dataTableColumn["sTitle"] = column; + dataTableColumn["defaultContent"]=""; + + dataTableColumn["render"] = function(data,type,row){ + if(typeof(data)=="object") + return JSON.stringify(data); + else + return data; + } + + dataTablesColumns.push(dataTableColumn); + } + return dataTablesColumns; +} +function createRows(rows){ + dataTableRows = []; + for(i =0;i'; + html = html + '{{column}}'; + html += ' {{row[column]}} ' + $(tableZoneSelector).html($compile(html)($scope)); + +} diff --git a/src/_site/vendor/dataTables/css/dataTables.bootstrap.css b/src/_site/vendor/dataTables/css/dataTables.bootstrap.css new file mode 100644 index 00000000..789a6148 --- /dev/null +++ b/src/_site/vendor/dataTables/css/dataTables.bootstrap.css @@ -0,0 +1,167 @@ +table.dataTable { + clear: both; + margin-top: 6px !important; + margin-bottom: 6px !important; + max-width: none !important; +} +table.dataTable td, +table.dataTable th { + -webkit-box-sizing: content-box; + -moz-box-sizing: content-box; + box-sizing: content-box; +} +table.dataTable td.dataTables_empty, +table.dataTable th.dataTables_empty { + text-align: center; +} +table.dataTable.nowrap th, +table.dataTable.nowrap td { + white-space: nowrap; +} + +div.dataTables_wrapper div.dataTables_length label { + font-weight: normal; + text-align: left; + white-space: nowrap; +} +div.dataTables_wrapper div.dataTables_length select { + width: 75px; + display: inline-block; +} +div.dataTables_wrapper div.dataTables_filter { + text-align: right; +} +div.dataTables_wrapper div.dataTables_filter label { + font-weight: normal; + white-space: nowrap; + text-align: left; +} +div.dataTables_wrapper div.dataTables_filter input { + margin-left: 0.5em; + display: inline-block; + width: auto; +} +div.dataTables_wrapper div.dataTables_info { + padding-top: 8px; + white-space: nowrap; +} +div.dataTables_wrapper div.dataTables_paginate { + margin: 0; + white-space: nowrap; + text-align: right; +} +div.dataTables_wrapper div.dataTables_paginate ul.pagination { + margin: 2px 0; + white-space: nowrap; +} + +table.dataTable thead > tr > th.sorting_asc, table.dataTable thead > tr > th.sorting_desc, table.dataTable thead > tr > th.sorting, +table.dataTable thead > tr > td.sorting_asc, +table.dataTable thead > tr > td.sorting_desc, +table.dataTable thead > tr > td.sorting { + padding-right: 30px; +} +table.dataTable thead > tr > th:active, +table.dataTable thead > tr > td:active { + outline: none; +} +table.dataTable thead .sorting, +table.dataTable thead .sorting_asc, +table.dataTable thead .sorting_desc, +table.dataTable thead .sorting_asc_disabled, +table.dataTable thead .sorting_desc_disabled { + cursor: pointer; + position: relative; +} +table.dataTable thead .sorting:after, +table.dataTable thead .sorting_asc:after, +table.dataTable thead .sorting_desc:after, +table.dataTable thead .sorting_asc_disabled:after, +table.dataTable thead .sorting_desc_disabled:after { + position: absolute; + bottom: 8px; + right: 8px; + display: block; + font-family: 'Glyphicons Halflings'; + opacity: 0.5; +} +table.dataTable thead .sorting:after { + opacity: 0.2; + content: "\e150"; + /* sort */ +} +table.dataTable thead .sorting_asc:after { + content: "\e155"; + /* sort-by-attributes */ +} +table.dataTable thead .sorting_desc:after { + content: "\e156"; + /* sort-by-attributes-alt */ +} +table.dataTable thead .sorting_asc_disabled:after, +table.dataTable thead .sorting_desc_disabled:after { + color: #eee; +} + +div.dataTables_scrollHead table.dataTable { + margin-bottom: 0 !important; +} + +div.dataTables_scrollBody table { + border-top: none; + margin-top: 0 !important; + margin-bottom: 0 !important; +} +div.dataTables_scrollBody table thead .sorting:after, +div.dataTables_scrollBody table thead .sorting_asc:after, +div.dataTables_scrollBody table thead .sorting_desc:after { + display: none; +} +div.dataTables_scrollBody table tbody tr:first-child th, +div.dataTables_scrollBody table tbody tr:first-child td { + border-top: none; +} + +div.dataTables_scrollFoot table { + margin-top: 0 !important; + border-top: none; +} + +@media screen and (max-width: 767px) { + div.dataTables_wrapper div.dataTables_length, + div.dataTables_wrapper div.dataTables_filter, + div.dataTables_wrapper div.dataTables_info, + div.dataTables_wrapper div.dataTables_paginate { + text-align: center; + } +} +table.dataTable.table-condensed > thead > tr > th { + padding-right: 20px; +} +table.dataTable.table-condensed .sorting:after, +table.dataTable.table-condensed .sorting_asc:after, +table.dataTable.table-condensed .sorting_desc:after { + top: 6px; + right: 6px; +} + +table.table-bordered.dataTable { + border-collapse: separate !important; +} +table.table-bordered.dataTable th, +table.table-bordered.dataTable td { + border-left-width: 0; +} +table.table-bordered.dataTable th:last-child, table.table-bordered.dataTable th:last-child, +table.table-bordered.dataTable td:last-child, +table.table-bordered.dataTable td:last-child { + border-right-width: 0; +} +table.table-bordered.dataTable tbody th, +table.table-bordered.dataTable tbody td { + border-bottom-width: 0; +} + +div.dataTables_scrollHead table.table-bordered { + border-bottom-width: 0; +} diff --git a/src/_site/vendor/dataTables/css/dataTables.bootstrap.min.css b/src/_site/vendor/dataTables/css/dataTables.bootstrap.min.css new file mode 100644 index 00000000..0fb97577 --- /dev/null +++ b/src/_site/vendor/dataTables/css/dataTables.bootstrap.min.css @@ -0,0 +1 @@ +table.dataTable{clear:both;margin-top:6px !important;margin-bottom:6px !important;max-width:none !important}table.dataTable td,table.dataTable th{-webkit-box-sizing:content-box;-moz-box-sizing:content-box;box-sizing:content-box}table.dataTable td.dataTables_empty,table.dataTable th.dataTables_empty{text-align:center}table.dataTable.nowrap th,table.dataTable.nowrap td{white-space:nowrap}div.dataTables_wrapper div.dataTables_length label{font-weight:normal;text-align:left;white-space:nowrap}div.dataTables_wrapper div.dataTables_length select{width:75px;display:inline-block}div.dataTables_wrapper div.dataTables_filter{text-align:right}div.dataTables_wrapper div.dataTables_filter label{font-weight:normal;white-space:nowrap;text-align:left}div.dataTables_wrapper div.dataTables_filter input{margin-left:0.5em;display:inline-block;width:auto}div.dataTables_wrapper div.dataTables_info{padding-top:8px;white-space:nowrap}div.dataTables_wrapper div.dataTables_paginate{margin:0;white-space:nowrap;text-align:right}div.dataTables_wrapper div.dataTables_paginate ul.pagination{margin:2px 0;white-space:nowrap}table.dataTable thead>tr>th.sorting_asc,table.dataTable thead>tr>th.sorting_desc,table.dataTable thead>tr>th.sorting,table.dataTable thead>tr>td.sorting_asc,table.dataTable thead>tr>td.sorting_desc,table.dataTable thead>tr>td.sorting{padding-right:30px}table.dataTable thead>tr>th:active,table.dataTable thead>tr>td:active{outline:none}table.dataTable thead .sorting,table.dataTable thead .sorting_asc,table.dataTable thead .sorting_desc,table.dataTable thead .sorting_asc_disabled,table.dataTable thead .sorting_desc_disabled{cursor:pointer;position:relative}table.dataTable thead .sorting:after,table.dataTable thead .sorting_asc:after,table.dataTable thead .sorting_desc:after,table.dataTable thead .sorting_asc_disabled:after,table.dataTable thead .sorting_desc_disabled:after{position:absolute;bottom:8px;right:8px;display:block;font-family:'Glyphicons Halflings';opacity:0.5}table.dataTable thead .sorting:after{opacity:0.2;content:"\e150"}table.dataTable thead .sorting_asc:after{content:"\e155"}table.dataTable thead .sorting_desc:after{content:"\e156"}table.dataTable thead .sorting_asc_disabled:after,table.dataTable thead .sorting_desc_disabled:after{color:#eee}div.dataTables_scrollHead table.dataTable{margin-bottom:0 !important}div.dataTables_scrollBody table{border-top:none;margin-top:0 !important;margin-bottom:0 !important}div.dataTables_scrollBody table thead .sorting:after,div.dataTables_scrollBody table thead .sorting_asc:after,div.dataTables_scrollBody table thead .sorting_desc:after{display:none}div.dataTables_scrollBody table tbody tr:first-child th,div.dataTables_scrollBody table tbody tr:first-child td{border-top:none}div.dataTables_scrollFoot table{margin-top:0 !important;border-top:none}@media screen and (max-width: 767px){div.dataTables_wrapper div.dataTables_length,div.dataTables_wrapper div.dataTables_filter,div.dataTables_wrapper div.dataTables_info,div.dataTables_wrapper div.dataTables_paginate{text-align:center}}table.dataTable.table-condensed>thead>tr>th{padding-right:20px}table.dataTable.table-condensed .sorting:after,table.dataTable.table-condensed .sorting_asc:after,table.dataTable.table-condensed .sorting_desc:after{top:6px;right:6px}table.table-bordered.dataTable{border-collapse:separate !important}table.table-bordered.dataTable th,table.table-bordered.dataTable td{border-left-width:0}table.table-bordered.dataTable th:last-child,table.table-bordered.dataTable th:last-child,table.table-bordered.dataTable td:last-child,table.table-bordered.dataTable td:last-child{border-right-width:0}table.table-bordered.dataTable tbody th,table.table-bordered.dataTable tbody td{border-bottom-width:0}div.dataTables_scrollHead table.table-bordered{border-bottom-width:0} diff --git a/src/_site/vendor/dataTables/css/dataTables.foundation.css b/src/_site/vendor/dataTables/css/dataTables.foundation.css new file mode 100644 index 00000000..013ddbba --- /dev/null +++ b/src/_site/vendor/dataTables/css/dataTables.foundation.css @@ -0,0 +1,104 @@ +table.dataTable { + clear: both; + margin: 0.5em 0 !important; + max-width: none !important; + width: 100%; +} +table.dataTable td, +table.dataTable th { + -webkit-box-sizing: content-box; + -moz-box-sizing: content-box; + box-sizing: content-box; +} +table.dataTable td.dataTables_empty, +table.dataTable th.dataTables_empty { + text-align: center; +} +table.dataTable.nowrap th, table.dataTable.nowrap td { + white-space: nowrap; +} + +div.dataTables_wrapper div.dataTables_length label { + float: left; + text-align: left; + margin-bottom: 0; +} +div.dataTables_wrapper div.dataTables_length select { + width: 75px; + margin-bottom: 0; +} +div.dataTables_wrapper div.dataTables_filter label { + float: right; + margin-bottom: 0; +} +div.dataTables_wrapper div.dataTables_filter input { + display: inline-block !important; + width: auto !important; + margin-bottom: 0; + margin-left: 0.5em; +} +div.dataTables_wrapper div.dataTables_info { + padding-top: 2px; +} +div.dataTables_wrapper div.dataTables_paginate { + float: right; + margin: 0; +} + +table.dataTable thead > tr > th.sorting_asc, table.dataTable thead > tr > th.sorting_desc, table.dataTable thead > tr > th.sorting, +table.dataTable thead > tr > td.sorting_asc, +table.dataTable thead > tr > td.sorting_desc, +table.dataTable thead > tr > td.sorting { + padding-right: 1.5rem; +} +table.dataTable thead > tr > th:active, +table.dataTable thead > tr > td:active { + outline: none; +} +table.dataTable thead .sorting, +table.dataTable thead .sorting_asc, +table.dataTable thead .sorting_desc { + cursor: pointer; +} +table.dataTable thead .sorting, +table.dataTable thead .sorting_asc, +table.dataTable thead .sorting_desc, +table.dataTable thead .sorting_asc_disabled, +table.dataTable thead .sorting_desc_disabled { + background-repeat: no-repeat; + background-position: center right; +} +table.dataTable thead .sorting { + background-image: url("../images/sort_both.png"); +} +table.dataTable thead .sorting_asc { + background-image: url("../images/sort_asc.png"); +} +table.dataTable thead .sorting_desc { + background-image: url("../images/sort_desc.png"); +} +table.dataTable thead .sorting_asc_disabled { + background-image: url("../images/sort_asc_disabled.png"); +} +table.dataTable thead .sorting_desc_disabled { + background-image: url("../images/sort_desc_disabled.png"); +} + +div.dataTables_scrollHead table { + margin-bottom: 0 !important; +} + +div.dataTables_scrollBody table { + border-top: none; + margin-top: 0 !important; + margin-bottom: 0 !important; +} +div.dataTables_scrollBody table tbody tr:first-child th, +div.dataTables_scrollBody table tbody tr:first-child td { + border-top: none; +} + +div.dataTables_scrollFoot table { + margin-top: 0 !important; + border-top: none; +} diff --git a/src/_site/vendor/dataTables/css/dataTables.foundation.min.css b/src/_site/vendor/dataTables/css/dataTables.foundation.min.css new file mode 100644 index 00000000..38332110 --- /dev/null +++ b/src/_site/vendor/dataTables/css/dataTables.foundation.min.css @@ -0,0 +1 @@ +table.dataTable{clear:both;margin:0.5em 0 !important;max-width:none !important;width:100%}table.dataTable td,table.dataTable th{-webkit-box-sizing:content-box;-moz-box-sizing:content-box;box-sizing:content-box}table.dataTable td.dataTables_empty,table.dataTable th.dataTables_empty{text-align:center}table.dataTable.nowrap th,table.dataTable.nowrap td{white-space:nowrap}div.dataTables_wrapper div.dataTables_length label{float:left;text-align:left;margin-bottom:0}div.dataTables_wrapper div.dataTables_length select{width:75px;margin-bottom:0}div.dataTables_wrapper div.dataTables_filter label{float:right;margin-bottom:0}div.dataTables_wrapper div.dataTables_filter input{display:inline-block !important;width:auto !important;margin-bottom:0;margin-left:0.5em}div.dataTables_wrapper div.dataTables_info{padding-top:2px}div.dataTables_wrapper div.dataTables_paginate{float:right;margin:0}table.dataTable thead>tr>th.sorting_asc,table.dataTable thead>tr>th.sorting_desc,table.dataTable thead>tr>th.sorting,table.dataTable thead>tr>td.sorting_asc,table.dataTable thead>tr>td.sorting_desc,table.dataTable thead>tr>td.sorting{padding-right:1.5rem}table.dataTable thead>tr>th:active,table.dataTable thead>tr>td:active{outline:none}table.dataTable thead .sorting,table.dataTable thead .sorting_asc,table.dataTable thead .sorting_desc{cursor:pointer}table.dataTable thead .sorting,table.dataTable thead .sorting_asc,table.dataTable thead .sorting_desc,table.dataTable thead .sorting_asc_disabled,table.dataTable thead .sorting_desc_disabled{background-repeat:no-repeat;background-position:center right}table.dataTable thead .sorting{background-image:url("../images/sort_both.png")}table.dataTable thead .sorting_asc{background-image:url("../images/sort_asc.png")}table.dataTable thead .sorting_desc{background-image:url("../images/sort_desc.png")}table.dataTable thead .sorting_asc_disabled{background-image:url("../images/sort_asc_disabled.png")}table.dataTable thead .sorting_desc_disabled{background-image:url("../images/sort_desc_disabled.png")}div.dataTables_scrollHead table{margin-bottom:0 !important}div.dataTables_scrollBody table{border-top:none;margin-top:0 !important;margin-bottom:0 !important}div.dataTables_scrollBody table tbody tr:first-child th,div.dataTables_scrollBody table tbody tr:first-child td{border-top:none}div.dataTables_scrollFoot table{margin-top:0 !important;border-top:none} diff --git a/src/_site/vendor/dataTables/css/dataTables.jqueryui.css b/src/_site/vendor/dataTables/css/dataTables.jqueryui.css new file mode 100644 index 00000000..881ed309 --- /dev/null +++ b/src/_site/vendor/dataTables/css/dataTables.jqueryui.css @@ -0,0 +1,473 @@ +/* + * Table styles + */ +table.dataTable { + width: 100%; + margin: 0 auto; + clear: both; + border-collapse: separate; + border-spacing: 0; + /* + * Header and footer styles + */ + /* + * Body styles + */ +} +table.dataTable thead th, +table.dataTable tfoot th { + font-weight: bold; +} +table.dataTable thead th, +table.dataTable thead td { + padding: 10px 18px; +} +table.dataTable thead th:active, +table.dataTable thead td:active { + outline: none; +} +table.dataTable tfoot th, +table.dataTable tfoot td { + padding: 10px 18px 6px 18px; +} +table.dataTable tbody tr { + background-color: #ffffff; +} +table.dataTable tbody tr.selected { + background-color: #B0BED9; +} +table.dataTable tbody th, +table.dataTable tbody td { + padding: 8px 10px; +} +table.dataTable.row-border tbody th, table.dataTable.row-border tbody td, table.dataTable.display tbody th, table.dataTable.display tbody td { + border-top: 1px solid #ddd; +} +table.dataTable.row-border tbody tr:first-child th, +table.dataTable.row-border tbody tr:first-child td, table.dataTable.display tbody tr:first-child th, +table.dataTable.display tbody tr:first-child td { + border-top: none; +} +table.dataTable.cell-border tbody th, table.dataTable.cell-border tbody td { + border-top: 1px solid #ddd; + border-right: 1px solid #ddd; +} +table.dataTable.cell-border tbody tr th:first-child, +table.dataTable.cell-border tbody tr td:first-child { + border-left: 1px solid #ddd; +} +table.dataTable.cell-border tbody tr:first-child th, +table.dataTable.cell-border tbody tr:first-child td { + border-top: none; +} +table.dataTable.stripe tbody tr.odd, table.dataTable.display tbody tr.odd { + background-color: #f9f9f9; +} +table.dataTable.stripe tbody tr.odd.selected, table.dataTable.display tbody tr.odd.selected { + background-color: #acbad4; +} +table.dataTable.hover tbody tr:hover, table.dataTable.display tbody tr:hover { + background-color: #f6f6f6; +} +table.dataTable.hover tbody tr:hover.selected, table.dataTable.display tbody tr:hover.selected { + background-color: #aab7d1; +} +table.dataTable.order-column tbody tr > .sorting_1, +table.dataTable.order-column tbody tr > .sorting_2, +table.dataTable.order-column tbody tr > .sorting_3, table.dataTable.display tbody tr > .sorting_1, +table.dataTable.display tbody tr > .sorting_2, +table.dataTable.display tbody tr > .sorting_3 { + background-color: #fafafa; +} +table.dataTable.order-column tbody tr.selected > .sorting_1, +table.dataTable.order-column tbody tr.selected > .sorting_2, +table.dataTable.order-column tbody tr.selected > .sorting_3, table.dataTable.display tbody tr.selected > .sorting_1, +table.dataTable.display tbody tr.selected > .sorting_2, +table.dataTable.display tbody tr.selected > .sorting_3 { + background-color: #acbad5; +} +table.dataTable.display tbody tr.odd > .sorting_1, table.dataTable.order-column.stripe tbody tr.odd > .sorting_1 { + background-color: #f1f1f1; +} +table.dataTable.display tbody tr.odd > .sorting_2, table.dataTable.order-column.stripe tbody tr.odd > .sorting_2 { + background-color: #f3f3f3; +} +table.dataTable.display tbody tr.odd > .sorting_3, table.dataTable.order-column.stripe tbody tr.odd > .sorting_3 { + background-color: whitesmoke; +} +table.dataTable.display tbody tr.odd.selected > .sorting_1, table.dataTable.order-column.stripe tbody tr.odd.selected > .sorting_1 { + background-color: #a6b4cd; +} +table.dataTable.display tbody tr.odd.selected > .sorting_2, table.dataTable.order-column.stripe tbody tr.odd.selected > .sorting_2 { + background-color: #a8b5cf; +} +table.dataTable.display tbody tr.odd.selected > .sorting_3, table.dataTable.order-column.stripe tbody tr.odd.selected > .sorting_3 { + background-color: #a9b7d1; +} +table.dataTable.display tbody tr.even > .sorting_1, table.dataTable.order-column.stripe tbody tr.even > .sorting_1 { + background-color: #fafafa; +} +table.dataTable.display tbody tr.even > .sorting_2, table.dataTable.order-column.stripe tbody tr.even > .sorting_2 { + background-color: #fcfcfc; +} +table.dataTable.display tbody tr.even > .sorting_3, table.dataTable.order-column.stripe tbody tr.even > .sorting_3 { + background-color: #fefefe; +} +table.dataTable.display tbody tr.even.selected > .sorting_1, table.dataTable.order-column.stripe tbody tr.even.selected > .sorting_1 { + background-color: #acbad5; +} +table.dataTable.display tbody tr.even.selected > .sorting_2, table.dataTable.order-column.stripe tbody tr.even.selected > .sorting_2 { + background-color: #aebcd6; +} +table.dataTable.display tbody tr.even.selected > .sorting_3, table.dataTable.order-column.stripe tbody tr.even.selected > .sorting_3 { + background-color: #afbdd8; +} +table.dataTable.display tbody tr:hover > .sorting_1, table.dataTable.order-column.hover tbody tr:hover > .sorting_1 { + background-color: #eaeaea; +} +table.dataTable.display tbody tr:hover > .sorting_2, table.dataTable.order-column.hover tbody tr:hover > .sorting_2 { + background-color: #ececec; +} +table.dataTable.display tbody tr:hover > .sorting_3, table.dataTable.order-column.hover tbody tr:hover > .sorting_3 { + background-color: #efefef; +} +table.dataTable.display tbody tr:hover.selected > .sorting_1, table.dataTable.order-column.hover tbody tr:hover.selected > .sorting_1 { + background-color: #a2aec7; +} +table.dataTable.display tbody tr:hover.selected > .sorting_2, table.dataTable.order-column.hover tbody tr:hover.selected > .sorting_2 { + background-color: #a3b0c9; +} +table.dataTable.display tbody tr:hover.selected > .sorting_3, table.dataTable.order-column.hover tbody tr:hover.selected > .sorting_3 { + background-color: #a5b2cb; +} +table.dataTable.no-footer { + border-bottom: 1px solid #111; +} +table.dataTable.nowrap th, table.dataTable.nowrap td { + white-space: nowrap; +} +table.dataTable.compact thead th, +table.dataTable.compact thead td { + padding: 4px 17px 4px 4px; +} +table.dataTable.compact tfoot th, +table.dataTable.compact tfoot td { + padding: 4px; +} +table.dataTable.compact tbody th, +table.dataTable.compact tbody td { + padding: 4px; +} +table.dataTable th.dt-left, +table.dataTable td.dt-left { + text-align: left; +} +table.dataTable th.dt-center, +table.dataTable td.dt-center, +table.dataTable td.dataTables_empty { + text-align: center; +} +table.dataTable th.dt-right, +table.dataTable td.dt-right { + text-align: right; +} +table.dataTable th.dt-justify, +table.dataTable td.dt-justify { + text-align: justify; +} +table.dataTable th.dt-nowrap, +table.dataTable td.dt-nowrap { + white-space: nowrap; +} +table.dataTable thead th.dt-head-left, +table.dataTable thead td.dt-head-left, +table.dataTable tfoot th.dt-head-left, +table.dataTable tfoot td.dt-head-left { + text-align: left; +} +table.dataTable thead th.dt-head-center, +table.dataTable thead td.dt-head-center, +table.dataTable tfoot th.dt-head-center, +table.dataTable tfoot td.dt-head-center { + text-align: center; +} +table.dataTable thead th.dt-head-right, +table.dataTable thead td.dt-head-right, +table.dataTable tfoot th.dt-head-right, +table.dataTable tfoot td.dt-head-right { + text-align: right; +} +table.dataTable thead th.dt-head-justify, +table.dataTable thead td.dt-head-justify, +table.dataTable tfoot th.dt-head-justify, +table.dataTable tfoot td.dt-head-justify { + text-align: justify; +} +table.dataTable thead th.dt-head-nowrap, +table.dataTable thead td.dt-head-nowrap, +table.dataTable tfoot th.dt-head-nowrap, +table.dataTable tfoot td.dt-head-nowrap { + white-space: nowrap; +} +table.dataTable tbody th.dt-body-left, +table.dataTable tbody td.dt-body-left { + text-align: left; +} +table.dataTable tbody th.dt-body-center, +table.dataTable tbody td.dt-body-center { + text-align: center; +} +table.dataTable tbody th.dt-body-right, +table.dataTable tbody td.dt-body-right { + text-align: right; +} +table.dataTable tbody th.dt-body-justify, +table.dataTable tbody td.dt-body-justify { + text-align: justify; +} +table.dataTable tbody th.dt-body-nowrap, +table.dataTable tbody td.dt-body-nowrap { + white-space: nowrap; +} + +table.dataTable, +table.dataTable th, +table.dataTable td { + -webkit-box-sizing: content-box; + -moz-box-sizing: content-box; + box-sizing: content-box; +} + +/* + * Control feature layout + */ +.dataTables_wrapper { + position: relative; + clear: both; + *zoom: 1; + zoom: 1; +} +.dataTables_wrapper .dataTables_length { + float: left; +} +.dataTables_wrapper .dataTables_filter { + float: right; + text-align: right; +} +.dataTables_wrapper .dataTables_filter input { + margin-left: 0.5em; +} +.dataTables_wrapper .dataTables_info { + clear: both; + float: left; + padding-top: 0.755em; +} +.dataTables_wrapper .dataTables_paginate { + float: right; + text-align: right; + padding-top: 0.25em; +} +.dataTables_wrapper .dataTables_paginate .paginate_button { + box-sizing: border-box; + display: inline-block; + min-width: 1.5em; + padding: 0.5em 1em; + margin-left: 2px; + text-align: center; + text-decoration: none !important; + cursor: pointer; + *cursor: hand; + color: #333 !important; + border: 1px solid transparent; + border-radius: 2px; +} +.dataTables_wrapper .dataTables_paginate .paginate_button.current, .dataTables_wrapper .dataTables_paginate .paginate_button.current:hover { + color: #333 !important; + border: 1px solid #979797; + background-color: white; + background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, white), color-stop(100%, #dcdcdc)); + /* Chrome,Safari4+ */ + background: -webkit-linear-gradient(top, white 0%, #dcdcdc 100%); + /* Chrome10+,Safari5.1+ */ + background: -moz-linear-gradient(top, white 0%, #dcdcdc 100%); + /* FF3.6+ */ + background: -ms-linear-gradient(top, white 0%, #dcdcdc 100%); + /* IE10+ */ + background: -o-linear-gradient(top, white 0%, #dcdcdc 100%); + /* Opera 11.10+ */ + background: linear-gradient(to bottom, white 0%, #dcdcdc 100%); + /* W3C */ +} +.dataTables_wrapper .dataTables_paginate .paginate_button.disabled, .dataTables_wrapper .dataTables_paginate .paginate_button.disabled:hover, .dataTables_wrapper .dataTables_paginate .paginate_button.disabled:active { + cursor: default; + color: #666 !important; + border: 1px solid transparent; + background: transparent; + box-shadow: none; +} +.dataTables_wrapper .dataTables_paginate .paginate_button:hover { + color: white !important; + border: 1px solid #111; + background-color: #585858; + background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #585858), color-stop(100%, #111)); + /* Chrome,Safari4+ */ + background: -webkit-linear-gradient(top, #585858 0%, #111 100%); + /* Chrome10+,Safari5.1+ */ + background: -moz-linear-gradient(top, #585858 0%, #111 100%); + /* FF3.6+ */ + background: -ms-linear-gradient(top, #585858 0%, #111 100%); + /* IE10+ */ + background: -o-linear-gradient(top, #585858 0%, #111 100%); + /* Opera 11.10+ */ + background: linear-gradient(to bottom, #585858 0%, #111 100%); + /* W3C */ +} +.dataTables_wrapper .dataTables_paginate .paginate_button:active { + outline: none; + background-color: #2b2b2b; + background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #2b2b2b), color-stop(100%, #0c0c0c)); + /* Chrome,Safari4+ */ + background: -webkit-linear-gradient(top, #2b2b2b 0%, #0c0c0c 100%); + /* Chrome10+,Safari5.1+ */ + background: -moz-linear-gradient(top, #2b2b2b 0%, #0c0c0c 100%); + /* FF3.6+ */ + background: -ms-linear-gradient(top, #2b2b2b 0%, #0c0c0c 100%); + /* IE10+ */ + background: -o-linear-gradient(top, #2b2b2b 0%, #0c0c0c 100%); + /* Opera 11.10+ */ + background: linear-gradient(to bottom, #2b2b2b 0%, #0c0c0c 100%); + /* W3C */ + box-shadow: inset 0 0 3px #111; +} +.dataTables_wrapper .dataTables_paginate .ellipsis { + padding: 0 1em; +} +.dataTables_wrapper .dataTables_processing { + position: absolute; + top: 50%; + left: 50%; + width: 100%; + height: 40px; + margin-left: -50%; + margin-top: -25px; + padding-top: 20px; + text-align: center; + font-size: 1.2em; + background-color: white; + background: -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(255, 255, 255, 0)), color-stop(25%, rgba(255, 255, 255, 0.9)), color-stop(75%, rgba(255, 255, 255, 0.9)), color-stop(100%, rgba(255, 255, 255, 0))); + background: -webkit-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.9) 25%, rgba(255, 255, 255, 0.9) 75%, rgba(255, 255, 255, 0) 100%); + background: -moz-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.9) 25%, rgba(255, 255, 255, 0.9) 75%, rgba(255, 255, 255, 0) 100%); + background: -ms-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.9) 25%, rgba(255, 255, 255, 0.9) 75%, rgba(255, 255, 255, 0) 100%); + background: -o-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.9) 25%, rgba(255, 255, 255, 0.9) 75%, rgba(255, 255, 255, 0) 100%); + background: linear-gradient(to right, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.9) 25%, rgba(255, 255, 255, 0.9) 75%, rgba(255, 255, 255, 0) 100%); +} +.dataTables_wrapper .dataTables_length, +.dataTables_wrapper .dataTables_filter, +.dataTables_wrapper .dataTables_info, +.dataTables_wrapper .dataTables_processing, +.dataTables_wrapper .dataTables_paginate { + color: #333; +} +.dataTables_wrapper .dataTables_scroll { + clear: both; +} +.dataTables_wrapper .dataTables_scroll div.dataTables_scrollBody { + *margin-top: -1px; + -webkit-overflow-scrolling: touch; +} +.dataTables_wrapper .dataTables_scroll div.dataTables_scrollBody th > div.dataTables_sizing, +.dataTables_wrapper .dataTables_scroll div.dataTables_scrollBody td > div.dataTables_sizing { + height: 0; + overflow: hidden; + margin: 0 !important; + padding: 0 !important; +} +.dataTables_wrapper.no-footer .dataTables_scrollBody { + border-bottom: 1px solid #111; +} +.dataTables_wrapper.no-footer div.dataTables_scrollHead table, +.dataTables_wrapper.no-footer div.dataTables_scrollBody table { + border-bottom: none; +} +.dataTables_wrapper:after { + visibility: hidden; + display: block; + content: ""; + clear: both; + height: 0; +} + +@media screen and (max-width: 767px) { + .dataTables_wrapper .dataTables_info, + .dataTables_wrapper .dataTables_paginate { + float: none; + text-align: center; + } + .dataTables_wrapper .dataTables_paginate { + margin-top: 0.5em; + } +} +@media screen and (max-width: 640px) { + .dataTables_wrapper .dataTables_length, + .dataTables_wrapper .dataTables_filter { + float: none; + text-align: center; + } + .dataTables_wrapper .dataTables_filter { + margin-top: 0.5em; + } +} +table.dataTable thead th div.DataTables_sort_wrapper { + position: relative; +} +table.dataTable thead th div.DataTables_sort_wrapper span { + position: absolute; + top: 50%; + margin-top: -8px; + right: -18px; +} +table.dataTable thead th.ui-state-default, +table.dataTable tfoot th.ui-state-default { + border-right-width: 0; +} +table.dataTable thead th.ui-state-default:last-child, +table.dataTable tfoot th.ui-state-default:last-child { + border-right-width: 1px; +} + +/* + * Control feature layout + */ +.dataTables_wrapper .dataTables_paginate .fg-button { + box-sizing: border-box; + display: inline-block; + min-width: 1.5em; + padding: 0.5em; + margin-left: 2px; + text-align: center; + text-decoration: none !important; + cursor: pointer; + *cursor: hand; + color: #333 !important; + border: 1px solid transparent; +} +.dataTables_wrapper .dataTables_paginate .fg-button:active { + outline: none; +} +.dataTables_wrapper .dataTables_paginate .fg-button:first-child { + border-top-left-radius: 3px; + border-bottom-left-radius: 3px; +} +.dataTables_wrapper .dataTables_paginate .fg-button:last-child { + border-top-right-radius: 3px; + border-bottom-right-radius: 3px; +} +.dataTables_wrapper .ui-widget-header { + font-weight: normal; +} +.dataTables_wrapper .ui-toolbar { + padding: 8px; +} +.dataTables_wrapper.no-footer .dataTables_scrollBody { + border-bottom: none; +} diff --git a/src/_site/vendor/dataTables/css/dataTables.jqueryui.min.css b/src/_site/vendor/dataTables/css/dataTables.jqueryui.min.css new file mode 100644 index 00000000..65f9d486 --- /dev/null +++ b/src/_site/vendor/dataTables/css/dataTables.jqueryui.min.css @@ -0,0 +1 @@ +table.dataTable{width:100%;margin:0 auto;clear:both;border-collapse:separate;border-spacing:0}table.dataTable thead th,table.dataTable tfoot th{font-weight:bold}table.dataTable thead th,table.dataTable thead td{padding:10px 18px}table.dataTable thead th:active,table.dataTable thead td:active{outline:none}table.dataTable tfoot th,table.dataTable tfoot td{padding:10px 18px 6px 18px}table.dataTable tbody tr{background-color:#ffffff}table.dataTable tbody tr.selected{background-color:#B0BED9}table.dataTable tbody th,table.dataTable tbody td{padding:8px 10px}table.dataTable.row-border tbody th,table.dataTable.row-border tbody td,table.dataTable.display tbody th,table.dataTable.display tbody td{border-top:1px solid #ddd}table.dataTable.row-border tbody tr:first-child th,table.dataTable.row-border tbody tr:first-child td,table.dataTable.display tbody tr:first-child th,table.dataTable.display tbody tr:first-child td{border-top:none}table.dataTable.cell-border tbody th,table.dataTable.cell-border tbody td{border-top:1px solid #ddd;border-right:1px solid #ddd}table.dataTable.cell-border tbody tr th:first-child,table.dataTable.cell-border tbody tr td:first-child{border-left:1px solid #ddd}table.dataTable.cell-border tbody tr:first-child th,table.dataTable.cell-border tbody tr:first-child td{border-top:none}table.dataTable.stripe tbody tr.odd,table.dataTable.display tbody tr.odd{background-color:#f9f9f9}table.dataTable.stripe tbody tr.odd.selected,table.dataTable.display tbody tr.odd.selected{background-color:#acbad4}table.dataTable.hover tbody tr:hover,table.dataTable.display tbody tr:hover{background-color:#f6f6f6}table.dataTable.hover tbody tr:hover.selected,table.dataTable.display tbody tr:hover.selected{background-color:#aab7d1}table.dataTable.order-column tbody tr>.sorting_1,table.dataTable.order-column tbody tr>.sorting_2,table.dataTable.order-column tbody tr>.sorting_3,table.dataTable.display tbody tr>.sorting_1,table.dataTable.display tbody tr>.sorting_2,table.dataTable.display tbody tr>.sorting_3{background-color:#fafafa}table.dataTable.order-column tbody tr.selected>.sorting_1,table.dataTable.order-column tbody tr.selected>.sorting_2,table.dataTable.order-column tbody tr.selected>.sorting_3,table.dataTable.display tbody tr.selected>.sorting_1,table.dataTable.display tbody tr.selected>.sorting_2,table.dataTable.display tbody tr.selected>.sorting_3{background-color:#acbad5}table.dataTable.display tbody tr.odd>.sorting_1,table.dataTable.order-column.stripe tbody tr.odd>.sorting_1{background-color:#f1f1f1}table.dataTable.display tbody tr.odd>.sorting_2,table.dataTable.order-column.stripe tbody tr.odd>.sorting_2{background-color:#f3f3f3}table.dataTable.display tbody tr.odd>.sorting_3,table.dataTable.order-column.stripe tbody tr.odd>.sorting_3{background-color:whitesmoke}table.dataTable.display tbody tr.odd.selected>.sorting_1,table.dataTable.order-column.stripe tbody tr.odd.selected>.sorting_1{background-color:#a6b4cd}table.dataTable.display tbody tr.odd.selected>.sorting_2,table.dataTable.order-column.stripe tbody tr.odd.selected>.sorting_2{background-color:#a8b5cf}table.dataTable.display tbody tr.odd.selected>.sorting_3,table.dataTable.order-column.stripe tbody tr.odd.selected>.sorting_3{background-color:#a9b7d1}table.dataTable.display tbody tr.even>.sorting_1,table.dataTable.order-column.stripe tbody tr.even>.sorting_1{background-color:#fafafa}table.dataTable.display tbody tr.even>.sorting_2,table.dataTable.order-column.stripe tbody tr.even>.sorting_2{background-color:#fcfcfc}table.dataTable.display tbody tr.even>.sorting_3,table.dataTable.order-column.stripe tbody tr.even>.sorting_3{background-color:#fefefe}table.dataTable.display tbody tr.even.selected>.sorting_1,table.dataTable.order-column.stripe tbody tr.even.selected>.sorting_1{background-color:#acbad5}table.dataTable.display tbody tr.even.selected>.sorting_2,table.dataTable.order-column.stripe tbody tr.even.selected>.sorting_2{background-color:#aebcd6}table.dataTable.display tbody tr.even.selected>.sorting_3,table.dataTable.order-column.stripe tbody tr.even.selected>.sorting_3{background-color:#afbdd8}table.dataTable.display tbody tr:hover>.sorting_1,table.dataTable.order-column.hover tbody tr:hover>.sorting_1{background-color:#eaeaea}table.dataTable.display tbody tr:hover>.sorting_2,table.dataTable.order-column.hover tbody tr:hover>.sorting_2{background-color:#ececec}table.dataTable.display tbody tr:hover>.sorting_3,table.dataTable.order-column.hover tbody tr:hover>.sorting_3{background-color:#efefef}table.dataTable.display tbody tr:hover.selected>.sorting_1,table.dataTable.order-column.hover tbody tr:hover.selected>.sorting_1{background-color:#a2aec7}table.dataTable.display tbody tr:hover.selected>.sorting_2,table.dataTable.order-column.hover tbody tr:hover.selected>.sorting_2{background-color:#a3b0c9}table.dataTable.display tbody tr:hover.selected>.sorting_3,table.dataTable.order-column.hover tbody tr:hover.selected>.sorting_3{background-color:#a5b2cb}table.dataTable.no-footer{border-bottom:1px solid #111}table.dataTable.nowrap th,table.dataTable.nowrap td{white-space:nowrap}table.dataTable.compact thead th,table.dataTable.compact thead td{padding:4px 17px 4px 4px}table.dataTable.compact tfoot th,table.dataTable.compact tfoot td{padding:4px}table.dataTable.compact tbody th,table.dataTable.compact tbody td{padding:4px}table.dataTable th.dt-left,table.dataTable td.dt-left{text-align:left}table.dataTable th.dt-center,table.dataTable td.dt-center,table.dataTable td.dataTables_empty{text-align:center}table.dataTable th.dt-right,table.dataTable td.dt-right{text-align:right}table.dataTable th.dt-justify,table.dataTable td.dt-justify{text-align:justify}table.dataTable th.dt-nowrap,table.dataTable td.dt-nowrap{white-space:nowrap}table.dataTable thead th.dt-head-left,table.dataTable thead td.dt-head-left,table.dataTable tfoot th.dt-head-left,table.dataTable tfoot td.dt-head-left{text-align:left}table.dataTable thead th.dt-head-center,table.dataTable thead td.dt-head-center,table.dataTable tfoot th.dt-head-center,table.dataTable tfoot td.dt-head-center{text-align:center}table.dataTable thead th.dt-head-right,table.dataTable thead td.dt-head-right,table.dataTable tfoot th.dt-head-right,table.dataTable tfoot td.dt-head-right{text-align:right}table.dataTable thead th.dt-head-justify,table.dataTable thead td.dt-head-justify,table.dataTable tfoot th.dt-head-justify,table.dataTable tfoot td.dt-head-justify{text-align:justify}table.dataTable thead th.dt-head-nowrap,table.dataTable thead td.dt-head-nowrap,table.dataTable tfoot th.dt-head-nowrap,table.dataTable tfoot td.dt-head-nowrap{white-space:nowrap}table.dataTable tbody th.dt-body-left,table.dataTable tbody td.dt-body-left{text-align:left}table.dataTable tbody th.dt-body-center,table.dataTable tbody td.dt-body-center{text-align:center}table.dataTable tbody th.dt-body-right,table.dataTable tbody td.dt-body-right{text-align:right}table.dataTable tbody th.dt-body-justify,table.dataTable tbody td.dt-body-justify{text-align:justify}table.dataTable tbody th.dt-body-nowrap,table.dataTable tbody td.dt-body-nowrap{white-space:nowrap}table.dataTable,table.dataTable th,table.dataTable td{-webkit-box-sizing:content-box;-moz-box-sizing:content-box;box-sizing:content-box}.dataTables_wrapper{position:relative;clear:both;*zoom:1;zoom:1}.dataTables_wrapper .dataTables_length{float:left}.dataTables_wrapper .dataTables_filter{float:right;text-align:right}.dataTables_wrapper .dataTables_filter input{margin-left:0.5em}.dataTables_wrapper .dataTables_info{clear:both;float:left;padding-top:0.755em}.dataTables_wrapper .dataTables_paginate{float:right;text-align:right;padding-top:0.25em}.dataTables_wrapper .dataTables_paginate .paginate_button{box-sizing:border-box;display:inline-block;min-width:1.5em;padding:0.5em 1em;margin-left:2px;text-align:center;text-decoration:none !important;cursor:pointer;*cursor:hand;color:#333 !important;border:1px solid transparent;border-radius:2px}.dataTables_wrapper .dataTables_paginate .paginate_button.current,.dataTables_wrapper .dataTables_paginate .paginate_button.current:hover{color:#333 !important;border:1px solid #979797;background-color:white;background:-webkit-gradient(linear, left top, left bottom, color-stop(0%, #fff), color-stop(100%, #dcdcdc));background:-webkit-linear-gradient(top, #fff 0%, #dcdcdc 100%);background:-moz-linear-gradient(top, #fff 0%, #dcdcdc 100%);background:-ms-linear-gradient(top, #fff 0%, #dcdcdc 100%);background:-o-linear-gradient(top, #fff 0%, #dcdcdc 100%);background:linear-gradient(to bottom, #fff 0%, #dcdcdc 100%)}.dataTables_wrapper .dataTables_paginate .paginate_button.disabled,.dataTables_wrapper .dataTables_paginate .paginate_button.disabled:hover,.dataTables_wrapper .dataTables_paginate .paginate_button.disabled:active{cursor:default;color:#666 !important;border:1px solid transparent;background:transparent;box-shadow:none}.dataTables_wrapper .dataTables_paginate .paginate_button:hover{color:white !important;border:1px solid #111;background-color:#585858;background:-webkit-gradient(linear, left top, left bottom, color-stop(0%, #585858), color-stop(100%, #111));background:-webkit-linear-gradient(top, #585858 0%, #111 100%);background:-moz-linear-gradient(top, #585858 0%, #111 100%);background:-ms-linear-gradient(top, #585858 0%, #111 100%);background:-o-linear-gradient(top, #585858 0%, #111 100%);background:linear-gradient(to bottom, #585858 0%, #111 100%)}.dataTables_wrapper .dataTables_paginate .paginate_button:active{outline:none;background-color:#2b2b2b;background:-webkit-gradient(linear, left top, left bottom, color-stop(0%, #2b2b2b), color-stop(100%, #0c0c0c));background:-webkit-linear-gradient(top, #2b2b2b 0%, #0c0c0c 100%);background:-moz-linear-gradient(top, #2b2b2b 0%, #0c0c0c 100%);background:-ms-linear-gradient(top, #2b2b2b 0%, #0c0c0c 100%);background:-o-linear-gradient(top, #2b2b2b 0%, #0c0c0c 100%);background:linear-gradient(to bottom, #2b2b2b 0%, #0c0c0c 100%);box-shadow:inset 0 0 3px #111}.dataTables_wrapper .dataTables_paginate .ellipsis{padding:0 1em}.dataTables_wrapper .dataTables_processing{position:absolute;top:50%;left:50%;width:100%;height:40px;margin-left:-50%;margin-top:-25px;padding-top:20px;text-align:center;font-size:1.2em;background-color:white;background:-webkit-gradient(linear, left top, right top, color-stop(0%, rgba(255,255,255,0)), color-stop(25%, rgba(255,255,255,0.9)), color-stop(75%, rgba(255,255,255,0.9)), color-stop(100%, rgba(255,255,255,0)));background:-webkit-linear-gradient(left, rgba(255,255,255,0) 0%, rgba(255,255,255,0.9) 25%, rgba(255,255,255,0.9) 75%, rgba(255,255,255,0) 100%);background:-moz-linear-gradient(left, rgba(255,255,255,0) 0%, rgba(255,255,255,0.9) 25%, rgba(255,255,255,0.9) 75%, rgba(255,255,255,0) 100%);background:-ms-linear-gradient(left, rgba(255,255,255,0) 0%, rgba(255,255,255,0.9) 25%, rgba(255,255,255,0.9) 75%, rgba(255,255,255,0) 100%);background:-o-linear-gradient(left, rgba(255,255,255,0) 0%, rgba(255,255,255,0.9) 25%, rgba(255,255,255,0.9) 75%, rgba(255,255,255,0) 100%);background:linear-gradient(to right, rgba(255,255,255,0) 0%, rgba(255,255,255,0.9) 25%, rgba(255,255,255,0.9) 75%, rgba(255,255,255,0) 100%)}.dataTables_wrapper .dataTables_length,.dataTables_wrapper .dataTables_filter,.dataTables_wrapper .dataTables_info,.dataTables_wrapper .dataTables_processing,.dataTables_wrapper .dataTables_paginate{color:#333}.dataTables_wrapper .dataTables_scroll{clear:both}.dataTables_wrapper .dataTables_scroll div.dataTables_scrollBody{*margin-top:-1px;-webkit-overflow-scrolling:touch}.dataTables_wrapper .dataTables_scroll div.dataTables_scrollBody th>div.dataTables_sizing,.dataTables_wrapper .dataTables_scroll div.dataTables_scrollBody td>div.dataTables_sizing{height:0;overflow:hidden;margin:0 !important;padding:0 !important}.dataTables_wrapper.no-footer .dataTables_scrollBody{border-bottom:1px solid #111}.dataTables_wrapper.no-footer div.dataTables_scrollHead table,.dataTables_wrapper.no-footer div.dataTables_scrollBody table{border-bottom:none}.dataTables_wrapper:after{visibility:hidden;display:block;content:"";clear:both;height:0}@media screen and (max-width: 767px){.dataTables_wrapper .dataTables_info,.dataTables_wrapper .dataTables_paginate{float:none;text-align:center}.dataTables_wrapper .dataTables_paginate{margin-top:0.5em}}@media screen and (max-width: 640px){.dataTables_wrapper .dataTables_length,.dataTables_wrapper .dataTables_filter{float:none;text-align:center}.dataTables_wrapper .dataTables_filter{margin-top:0.5em}}table.dataTable thead th div.DataTables_sort_wrapper{position:relative}table.dataTable thead th div.DataTables_sort_wrapper span{position:absolute;top:50%;margin-top:-8px;right:-18px}table.dataTable thead th.ui-state-default,table.dataTable tfoot th.ui-state-default{border-right-width:0}table.dataTable thead th.ui-state-default:last-child,table.dataTable tfoot th.ui-state-default:last-child{border-right-width:1px}.dataTables_wrapper .dataTables_paginate .fg-button{box-sizing:border-box;display:inline-block;min-width:1.5em;padding:0.5em;margin-left:2px;text-align:center;text-decoration:none !important;cursor:pointer;*cursor:hand;color:#333 !important;border:1px solid transparent}.dataTables_wrapper .dataTables_paginate .fg-button:active{outline:none}.dataTables_wrapper .dataTables_paginate .fg-button:first-child{border-top-left-radius:3px;border-bottom-left-radius:3px}.dataTables_wrapper .dataTables_paginate .fg-button:last-child{border-top-right-radius:3px;border-bottom-right-radius:3px}.dataTables_wrapper .ui-widget-header{font-weight:normal}.dataTables_wrapper .ui-toolbar{padding:8px}.dataTables_wrapper.no-footer .dataTables_scrollBody{border-bottom:none} diff --git a/src/_site/vendor/dataTables/css/jquery.dataTables.css b/src/_site/vendor/dataTables/css/jquery.dataTables.css new file mode 100644 index 00000000..4e104e19 --- /dev/null +++ b/src/_site/vendor/dataTables/css/jquery.dataTables.css @@ -0,0 +1,450 @@ +/* + * Table styles + */ +table.dataTable { + width: 100%; + margin: 0 auto; + clear: both; + border-collapse: separate; + border-spacing: 0; + /* + * Header and footer styles + */ + /* + * Body styles + */ +} +table.dataTable thead th, +table.dataTable tfoot th { + font-weight: bold; +} +table.dataTable thead th, +table.dataTable thead td { + padding: 10px 18px; + border-bottom: 1px solid #111; +} +table.dataTable thead th:active, +table.dataTable thead td:active { + outline: none; +} +table.dataTable tfoot th, +table.dataTable tfoot td { + padding: 10px 18px 6px 18px; + border-top: 1px solid #111; +} +table.dataTable thead .sorting, +table.dataTable thead .sorting_asc, +table.dataTable thead .sorting_desc { + cursor: pointer; + *cursor: hand; +} +table.dataTable thead .sorting, +table.dataTable thead .sorting_asc, +table.dataTable thead .sorting_desc, +table.dataTable thead .sorting_asc_disabled, +table.dataTable thead .sorting_desc_disabled { + background-repeat: no-repeat; + background-position: center right; +} +table.dataTable thead .sorting { + background-image: url("../images/sort_both.png"); +} +table.dataTable thead .sorting_asc { + background-image: url("../images/sort_asc.png"); +} +table.dataTable thead .sorting_desc { + background-image: url("../images/sort_desc.png"); +} +table.dataTable thead .sorting_asc_disabled { + background-image: url("../images/sort_asc_disabled.png"); +} +table.dataTable thead .sorting_desc_disabled { + background-image: url("../images/sort_desc_disabled.png"); +} +table.dataTable tbody tr { + background-color: #ffffff; +} +table.dataTable tbody tr.selected { + background-color: #B0BED9; +} +table.dataTable tbody th, +table.dataTable tbody td { + padding: 8px 10px; +} +table.dataTable.row-border tbody th, table.dataTable.row-border tbody td, table.dataTable.display tbody th, table.dataTable.display tbody td { + border-top: 1px solid #ddd; +} +table.dataTable.row-border tbody tr:first-child th, +table.dataTable.row-border tbody tr:first-child td, table.dataTable.display tbody tr:first-child th, +table.dataTable.display tbody tr:first-child td { + border-top: none; +} +table.dataTable.cell-border tbody th, table.dataTable.cell-border tbody td { + border-top: 1px solid #ddd; + border-right: 1px solid #ddd; +} +table.dataTable.cell-border tbody tr th:first-child, +table.dataTable.cell-border tbody tr td:first-child { + border-left: 1px solid #ddd; +} +table.dataTable.cell-border tbody tr:first-child th, +table.dataTable.cell-border tbody tr:first-child td { + border-top: none; +} +table.dataTable.stripe tbody tr.odd, table.dataTable.display tbody tr.odd { + background-color: #f9f9f9; +} +table.dataTable.stripe tbody tr.odd.selected, table.dataTable.display tbody tr.odd.selected { + background-color: #acbad4; +} +table.dataTable.hover tbody tr:hover, table.dataTable.display tbody tr:hover { + background-color: #f6f6f6; +} +table.dataTable.hover tbody tr:hover.selected, table.dataTable.display tbody tr:hover.selected { + background-color: #aab7d1; +} +table.dataTable.order-column tbody tr > .sorting_1, +table.dataTable.order-column tbody tr > .sorting_2, +table.dataTable.order-column tbody tr > .sorting_3, table.dataTable.display tbody tr > .sorting_1, +table.dataTable.display tbody tr > .sorting_2, +table.dataTable.display tbody tr > .sorting_3 { + background-color: #fafafa; +} +table.dataTable.order-column tbody tr.selected > .sorting_1, +table.dataTable.order-column tbody tr.selected > .sorting_2, +table.dataTable.order-column tbody tr.selected > .sorting_3, table.dataTable.display tbody tr.selected > .sorting_1, +table.dataTable.display tbody tr.selected > .sorting_2, +table.dataTable.display tbody tr.selected > .sorting_3 { + background-color: #acbad5; +} +table.dataTable.display tbody tr.odd > .sorting_1, table.dataTable.order-column.stripe tbody tr.odd > .sorting_1 { + background-color: #f1f1f1; +} +table.dataTable.display tbody tr.odd > .sorting_2, table.dataTable.order-column.stripe tbody tr.odd > .sorting_2 { + background-color: #f3f3f3; +} +table.dataTable.display tbody tr.odd > .sorting_3, table.dataTable.order-column.stripe tbody tr.odd > .sorting_3 { + background-color: whitesmoke; +} +table.dataTable.display tbody tr.odd.selected > .sorting_1, table.dataTable.order-column.stripe tbody tr.odd.selected > .sorting_1 { + background-color: #a6b4cd; +} +table.dataTable.display tbody tr.odd.selected > .sorting_2, table.dataTable.order-column.stripe tbody tr.odd.selected > .sorting_2 { + background-color: #a8b5cf; +} +table.dataTable.display tbody tr.odd.selected > .sorting_3, table.dataTable.order-column.stripe tbody tr.odd.selected > .sorting_3 { + background-color: #a9b7d1; +} +table.dataTable.display tbody tr.even > .sorting_1, table.dataTable.order-column.stripe tbody tr.even > .sorting_1 { + background-color: #fafafa; +} +table.dataTable.display tbody tr.even > .sorting_2, table.dataTable.order-column.stripe tbody tr.even > .sorting_2 { + background-color: #fcfcfc; +} +table.dataTable.display tbody tr.even > .sorting_3, table.dataTable.order-column.stripe tbody tr.even > .sorting_3 { + background-color: #fefefe; +} +table.dataTable.display tbody tr.even.selected > .sorting_1, table.dataTable.order-column.stripe tbody tr.even.selected > .sorting_1 { + background-color: #acbad5; +} +table.dataTable.display tbody tr.even.selected > .sorting_2, table.dataTable.order-column.stripe tbody tr.even.selected > .sorting_2 { + background-color: #aebcd6; +} +table.dataTable.display tbody tr.even.selected > .sorting_3, table.dataTable.order-column.stripe tbody tr.even.selected > .sorting_3 { + background-color: #afbdd8; +} +table.dataTable.display tbody tr:hover > .sorting_1, table.dataTable.order-column.hover tbody tr:hover > .sorting_1 { + background-color: #eaeaea; +} +table.dataTable.display tbody tr:hover > .sorting_2, table.dataTable.order-column.hover tbody tr:hover > .sorting_2 { + background-color: #ececec; +} +table.dataTable.display tbody tr:hover > .sorting_3, table.dataTable.order-column.hover tbody tr:hover > .sorting_3 { + background-color: #efefef; +} +table.dataTable.display tbody tr:hover.selected > .sorting_1, table.dataTable.order-column.hover tbody tr:hover.selected > .sorting_1 { + background-color: #a2aec7; +} +table.dataTable.display tbody tr:hover.selected > .sorting_2, table.dataTable.order-column.hover tbody tr:hover.selected > .sorting_2 { + background-color: #a3b0c9; +} +table.dataTable.display tbody tr:hover.selected > .sorting_3, table.dataTable.order-column.hover tbody tr:hover.selected > .sorting_3 { + background-color: #a5b2cb; +} +table.dataTable.no-footer { + border-bottom: 1px solid #111; +} +table.dataTable.nowrap th, table.dataTable.nowrap td { + white-space: nowrap; +} +table.dataTable.compact thead th, +table.dataTable.compact thead td { + padding: 4px 17px 4px 4px; +} +table.dataTable.compact tfoot th, +table.dataTable.compact tfoot td { + padding: 4px; +} +table.dataTable.compact tbody th, +table.dataTable.compact tbody td { + padding: 4px; +} +table.dataTable th.dt-left, +table.dataTable td.dt-left { + text-align: left; +} +table.dataTable th.dt-center, +table.dataTable td.dt-center, +table.dataTable td.dataTables_empty { + text-align: center; +} +table.dataTable th.dt-right, +table.dataTable td.dt-right { + text-align: right; +} +table.dataTable th.dt-justify, +table.dataTable td.dt-justify { + text-align: justify; +} +table.dataTable th.dt-nowrap, +table.dataTable td.dt-nowrap { + white-space: nowrap; +} +table.dataTable thead th.dt-head-left, +table.dataTable thead td.dt-head-left, +table.dataTable tfoot th.dt-head-left, +table.dataTable tfoot td.dt-head-left { + text-align: left; +} +table.dataTable thead th.dt-head-center, +table.dataTable thead td.dt-head-center, +table.dataTable tfoot th.dt-head-center, +table.dataTable tfoot td.dt-head-center { + text-align: center; +} +table.dataTable thead th.dt-head-right, +table.dataTable thead td.dt-head-right, +table.dataTable tfoot th.dt-head-right, +table.dataTable tfoot td.dt-head-right { + text-align: right; +} +table.dataTable thead th.dt-head-justify, +table.dataTable thead td.dt-head-justify, +table.dataTable tfoot th.dt-head-justify, +table.dataTable tfoot td.dt-head-justify { + text-align: justify; +} +table.dataTable thead th.dt-head-nowrap, +table.dataTable thead td.dt-head-nowrap, +table.dataTable tfoot th.dt-head-nowrap, +table.dataTable tfoot td.dt-head-nowrap { + white-space: nowrap; +} +table.dataTable tbody th.dt-body-left, +table.dataTable tbody td.dt-body-left { + text-align: left; +} +table.dataTable tbody th.dt-body-center, +table.dataTable tbody td.dt-body-center { + text-align: center; +} +table.dataTable tbody th.dt-body-right, +table.dataTable tbody td.dt-body-right { + text-align: right; +} +table.dataTable tbody th.dt-body-justify, +table.dataTable tbody td.dt-body-justify { + text-align: justify; +} +table.dataTable tbody th.dt-body-nowrap, +table.dataTable tbody td.dt-body-nowrap { + white-space: nowrap; +} + +table.dataTable, +table.dataTable th, +table.dataTable td { + -webkit-box-sizing: content-box; + -moz-box-sizing: content-box; + box-sizing: content-box; +} + +/* + * Control feature layout + */ +.dataTables_wrapper { + position: relative; + clear: both; + *zoom: 1; + zoom: 1; +} +.dataTables_wrapper .dataTables_length { + float: left; +} +.dataTables_wrapper .dataTables_filter { + float: right; + text-align: right; +} +.dataTables_wrapper .dataTables_filter input { + margin-left: 0.5em; +} +.dataTables_wrapper .dataTables_info { + clear: both; + float: left; + padding-top: 0.755em; +} +.dataTables_wrapper .dataTables_paginate { + float: right; + text-align: right; + padding-top: 0.25em; +} +.dataTables_wrapper .dataTables_paginate .paginate_button { + box-sizing: border-box; + display: inline-block; + min-width: 1.5em; + padding: 0.5em 1em; + margin-left: 2px; + text-align: center; + text-decoration: none !important; + cursor: pointer; + *cursor: hand; + color: #333 !important; + border: 1px solid transparent; + border-radius: 2px; +} +.dataTables_wrapper .dataTables_paginate .paginate_button.current, .dataTables_wrapper .dataTables_paginate .paginate_button.current:hover { + color: #333 !important; + border: 1px solid #979797; + background-color: white; + background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, white), color-stop(100%, #dcdcdc)); + /* Chrome,Safari4+ */ + background: -webkit-linear-gradient(top, white 0%, #dcdcdc 100%); + /* Chrome10+,Safari5.1+ */ + background: -moz-linear-gradient(top, white 0%, #dcdcdc 100%); + /* FF3.6+ */ + background: -ms-linear-gradient(top, white 0%, #dcdcdc 100%); + /* IE10+ */ + background: -o-linear-gradient(top, white 0%, #dcdcdc 100%); + /* Opera 11.10+ */ + background: linear-gradient(to bottom, white 0%, #dcdcdc 100%); + /* W3C */ +} +.dataTables_wrapper .dataTables_paginate .paginate_button.disabled, .dataTables_wrapper .dataTables_paginate .paginate_button.disabled:hover, .dataTables_wrapper .dataTables_paginate .paginate_button.disabled:active { + cursor: default; + color: #666 !important; + border: 1px solid transparent; + background: transparent; + box-shadow: none; +} +.dataTables_wrapper .dataTables_paginate .paginate_button:hover { + color: white !important; + border: 1px solid #111; + background-color: #585858; + background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #585858), color-stop(100%, #111)); + /* Chrome,Safari4+ */ + background: -webkit-linear-gradient(top, #585858 0%, #111 100%); + /* Chrome10+,Safari5.1+ */ + background: -moz-linear-gradient(top, #585858 0%, #111 100%); + /* FF3.6+ */ + background: -ms-linear-gradient(top, #585858 0%, #111 100%); + /* IE10+ */ + background: -o-linear-gradient(top, #585858 0%, #111 100%); + /* Opera 11.10+ */ + background: linear-gradient(to bottom, #585858 0%, #111 100%); + /* W3C */ +} +.dataTables_wrapper .dataTables_paginate .paginate_button:active { + outline: none; + background-color: #2b2b2b; + background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #2b2b2b), color-stop(100%, #0c0c0c)); + /* Chrome,Safari4+ */ + background: -webkit-linear-gradient(top, #2b2b2b 0%, #0c0c0c 100%); + /* Chrome10+,Safari5.1+ */ + background: -moz-linear-gradient(top, #2b2b2b 0%, #0c0c0c 100%); + /* FF3.6+ */ + background: -ms-linear-gradient(top, #2b2b2b 0%, #0c0c0c 100%); + /* IE10+ */ + background: -o-linear-gradient(top, #2b2b2b 0%, #0c0c0c 100%); + /* Opera 11.10+ */ + background: linear-gradient(to bottom, #2b2b2b 0%, #0c0c0c 100%); + /* W3C */ + box-shadow: inset 0 0 3px #111; +} +.dataTables_wrapper .dataTables_paginate .ellipsis { + padding: 0 1em; +} +.dataTables_wrapper .dataTables_processing { + position: absolute; + top: 50%; + left: 50%; + width: 100%; + height: 40px; + margin-left: -50%; + margin-top: -25px; + padding-top: 20px; + text-align: center; + font-size: 1.2em; + background-color: white; + background: -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(255, 255, 255, 0)), color-stop(25%, rgba(255, 255, 255, 0.9)), color-stop(75%, rgba(255, 255, 255, 0.9)), color-stop(100%, rgba(255, 255, 255, 0))); + background: -webkit-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.9) 25%, rgba(255, 255, 255, 0.9) 75%, rgba(255, 255, 255, 0) 100%); + background: -moz-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.9) 25%, rgba(255, 255, 255, 0.9) 75%, rgba(255, 255, 255, 0) 100%); + background: -ms-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.9) 25%, rgba(255, 255, 255, 0.9) 75%, rgba(255, 255, 255, 0) 100%); + background: -o-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.9) 25%, rgba(255, 255, 255, 0.9) 75%, rgba(255, 255, 255, 0) 100%); + background: linear-gradient(to right, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.9) 25%, rgba(255, 255, 255, 0.9) 75%, rgba(255, 255, 255, 0) 100%); +} +.dataTables_wrapper .dataTables_length, +.dataTables_wrapper .dataTables_filter, +.dataTables_wrapper .dataTables_info, +.dataTables_wrapper .dataTables_processing, +.dataTables_wrapper .dataTables_paginate { + color: #333; +} +.dataTables_wrapper .dataTables_scroll { + clear: both; +} +.dataTables_wrapper .dataTables_scroll div.dataTables_scrollBody { + *margin-top: -1px; + -webkit-overflow-scrolling: touch; +} +.dataTables_wrapper .dataTables_scroll div.dataTables_scrollBody th > div.dataTables_sizing, +.dataTables_wrapper .dataTables_scroll div.dataTables_scrollBody td > div.dataTables_sizing { + height: 0; + overflow: hidden; + margin: 0 !important; + padding: 0 !important; +} +.dataTables_wrapper.no-footer .dataTables_scrollBody { + border-bottom: 1px solid #111; +} +.dataTables_wrapper.no-footer div.dataTables_scrollHead table, +.dataTables_wrapper.no-footer div.dataTables_scrollBody table { + border-bottom: none; +} +.dataTables_wrapper:after { + visibility: hidden; + display: block; + content: ""; + clear: both; + height: 0; +} + +@media screen and (max-width: 767px) { + .dataTables_wrapper .dataTables_info, + .dataTables_wrapper .dataTables_paginate { + float: none; + text-align: center; + } + .dataTables_wrapper .dataTables_paginate { + margin-top: 0.5em; + } +} +@media screen and (max-width: 640px) { + .dataTables_wrapper .dataTables_length, + .dataTables_wrapper .dataTables_filter { + float: none; + text-align: center; + } + .dataTables_wrapper .dataTables_filter { + margin-top: 0.5em; + } +} diff --git a/src/_site/vendor/dataTables/css/jquery.dataTables.min.css b/src/_site/vendor/dataTables/css/jquery.dataTables.min.css new file mode 100644 index 00000000..e6b36807 --- /dev/null +++ b/src/_site/vendor/dataTables/css/jquery.dataTables.min.css @@ -0,0 +1 @@ +table.dataTable{width:100%;margin:0 auto;clear:both;border-collapse:separate;border-spacing:0}table.dataTable thead th,table.dataTable tfoot th{font-weight:bold}table.dataTable thead th,table.dataTable thead td{padding:10px 18px;border-bottom:1px solid #111}table.dataTable thead th:active,table.dataTable thead td:active{outline:none}table.dataTable tfoot th,table.dataTable tfoot td{padding:10px 18px 6px 18px;border-top:1px solid #111}table.dataTable thead .sorting,table.dataTable thead .sorting_asc,table.dataTable thead .sorting_desc{cursor:pointer;*cursor:hand}table.dataTable thead .sorting,table.dataTable thead .sorting_asc,table.dataTable thead .sorting_desc,table.dataTable thead .sorting_asc_disabled,table.dataTable thead .sorting_desc_disabled{background-repeat:no-repeat;background-position:center right}table.dataTable thead .sorting{background-image:url("../images/sort_both.png")}table.dataTable thead .sorting_asc{background-image:url("../images/sort_asc.png")}table.dataTable thead .sorting_desc{background-image:url("../images/sort_desc.png")}table.dataTable thead .sorting_asc_disabled{background-image:url("../images/sort_asc_disabled.png")}table.dataTable thead .sorting_desc_disabled{background-image:url("../images/sort_desc_disabled.png")}table.dataTable tbody tr{background-color:#ffffff}table.dataTable tbody tr.selected{background-color:#B0BED9}table.dataTable tbody th,table.dataTable tbody td{padding:8px 10px}table.dataTable.row-border tbody th,table.dataTable.row-border tbody td,table.dataTable.display tbody th,table.dataTable.display tbody td{border-top:1px solid #ddd}table.dataTable.row-border tbody tr:first-child th,table.dataTable.row-border tbody tr:first-child td,table.dataTable.display tbody tr:first-child th,table.dataTable.display tbody tr:first-child td{border-top:none}table.dataTable.cell-border tbody th,table.dataTable.cell-border tbody td{border-top:1px solid #ddd;border-right:1px solid #ddd}table.dataTable.cell-border tbody tr th:first-child,table.dataTable.cell-border tbody tr td:first-child{border-left:1px solid #ddd}table.dataTable.cell-border tbody tr:first-child th,table.dataTable.cell-border tbody tr:first-child td{border-top:none}table.dataTable.stripe tbody tr.odd,table.dataTable.display tbody tr.odd{background-color:#f9f9f9}table.dataTable.stripe tbody tr.odd.selected,table.dataTable.display tbody tr.odd.selected{background-color:#acbad4}table.dataTable.hover tbody tr:hover,table.dataTable.display tbody tr:hover{background-color:#f6f6f6}table.dataTable.hover tbody tr:hover.selected,table.dataTable.display tbody tr:hover.selected{background-color:#aab7d1}table.dataTable.order-column tbody tr>.sorting_1,table.dataTable.order-column tbody tr>.sorting_2,table.dataTable.order-column tbody tr>.sorting_3,table.dataTable.display tbody tr>.sorting_1,table.dataTable.display tbody tr>.sorting_2,table.dataTable.display tbody tr>.sorting_3{background-color:#fafafa}table.dataTable.order-column tbody tr.selected>.sorting_1,table.dataTable.order-column tbody tr.selected>.sorting_2,table.dataTable.order-column tbody tr.selected>.sorting_3,table.dataTable.display tbody tr.selected>.sorting_1,table.dataTable.display tbody tr.selected>.sorting_2,table.dataTable.display tbody tr.selected>.sorting_3{background-color:#acbad5}table.dataTable.display tbody tr.odd>.sorting_1,table.dataTable.order-column.stripe tbody tr.odd>.sorting_1{background-color:#f1f1f1}table.dataTable.display tbody tr.odd>.sorting_2,table.dataTable.order-column.stripe tbody tr.odd>.sorting_2{background-color:#f3f3f3}table.dataTable.display tbody tr.odd>.sorting_3,table.dataTable.order-column.stripe tbody tr.odd>.sorting_3{background-color:whitesmoke}table.dataTable.display tbody tr.odd.selected>.sorting_1,table.dataTable.order-column.stripe tbody tr.odd.selected>.sorting_1{background-color:#a6b4cd}table.dataTable.display tbody tr.odd.selected>.sorting_2,table.dataTable.order-column.stripe tbody tr.odd.selected>.sorting_2{background-color:#a8b5cf}table.dataTable.display tbody tr.odd.selected>.sorting_3,table.dataTable.order-column.stripe tbody tr.odd.selected>.sorting_3{background-color:#a9b7d1}table.dataTable.display tbody tr.even>.sorting_1,table.dataTable.order-column.stripe tbody tr.even>.sorting_1{background-color:#fafafa}table.dataTable.display tbody tr.even>.sorting_2,table.dataTable.order-column.stripe tbody tr.even>.sorting_2{background-color:#fcfcfc}table.dataTable.display tbody tr.even>.sorting_3,table.dataTable.order-column.stripe tbody tr.even>.sorting_3{background-color:#fefefe}table.dataTable.display tbody tr.even.selected>.sorting_1,table.dataTable.order-column.stripe tbody tr.even.selected>.sorting_1{background-color:#acbad5}table.dataTable.display tbody tr.even.selected>.sorting_2,table.dataTable.order-column.stripe tbody tr.even.selected>.sorting_2{background-color:#aebcd6}table.dataTable.display tbody tr.even.selected>.sorting_3,table.dataTable.order-column.stripe tbody tr.even.selected>.sorting_3{background-color:#afbdd8}table.dataTable.display tbody tr:hover>.sorting_1,table.dataTable.order-column.hover tbody tr:hover>.sorting_1{background-color:#eaeaea}table.dataTable.display tbody tr:hover>.sorting_2,table.dataTable.order-column.hover tbody tr:hover>.sorting_2{background-color:#ececec}table.dataTable.display tbody tr:hover>.sorting_3,table.dataTable.order-column.hover tbody tr:hover>.sorting_3{background-color:#efefef}table.dataTable.display tbody tr:hover.selected>.sorting_1,table.dataTable.order-column.hover tbody tr:hover.selected>.sorting_1{background-color:#a2aec7}table.dataTable.display tbody tr:hover.selected>.sorting_2,table.dataTable.order-column.hover tbody tr:hover.selected>.sorting_2{background-color:#a3b0c9}table.dataTable.display tbody tr:hover.selected>.sorting_3,table.dataTable.order-column.hover tbody tr:hover.selected>.sorting_3{background-color:#a5b2cb}table.dataTable.no-footer{border-bottom:1px solid #111}table.dataTable.nowrap th,table.dataTable.nowrap td{white-space:nowrap}table.dataTable.compact thead th,table.dataTable.compact thead td{padding:4px 17px 4px 4px}table.dataTable.compact tfoot th,table.dataTable.compact tfoot td{padding:4px}table.dataTable.compact tbody th,table.dataTable.compact tbody td{padding:4px}table.dataTable th.dt-left,table.dataTable td.dt-left{text-align:left}table.dataTable th.dt-center,table.dataTable td.dt-center,table.dataTable td.dataTables_empty{text-align:center}table.dataTable th.dt-right,table.dataTable td.dt-right{text-align:right}table.dataTable th.dt-justify,table.dataTable td.dt-justify{text-align:justify}table.dataTable th.dt-nowrap,table.dataTable td.dt-nowrap{white-space:nowrap}table.dataTable thead th.dt-head-left,table.dataTable thead td.dt-head-left,table.dataTable tfoot th.dt-head-left,table.dataTable tfoot td.dt-head-left{text-align:left}table.dataTable thead th.dt-head-center,table.dataTable thead td.dt-head-center,table.dataTable tfoot th.dt-head-center,table.dataTable tfoot td.dt-head-center{text-align:center}table.dataTable thead th.dt-head-right,table.dataTable thead td.dt-head-right,table.dataTable tfoot th.dt-head-right,table.dataTable tfoot td.dt-head-right{text-align:right}table.dataTable thead th.dt-head-justify,table.dataTable thead td.dt-head-justify,table.dataTable tfoot th.dt-head-justify,table.dataTable tfoot td.dt-head-justify{text-align:justify}table.dataTable thead th.dt-head-nowrap,table.dataTable thead td.dt-head-nowrap,table.dataTable tfoot th.dt-head-nowrap,table.dataTable tfoot td.dt-head-nowrap{white-space:nowrap}table.dataTable tbody th.dt-body-left,table.dataTable tbody td.dt-body-left{text-align:left}table.dataTable tbody th.dt-body-center,table.dataTable tbody td.dt-body-center{text-align:center}table.dataTable tbody th.dt-body-right,table.dataTable tbody td.dt-body-right{text-align:right}table.dataTable tbody th.dt-body-justify,table.dataTable tbody td.dt-body-justify{text-align:justify}table.dataTable tbody th.dt-body-nowrap,table.dataTable tbody td.dt-body-nowrap{white-space:nowrap}table.dataTable,table.dataTable th,table.dataTable td{-webkit-box-sizing:content-box;-moz-box-sizing:content-box;box-sizing:content-box}.dataTables_wrapper{position:relative;clear:both;*zoom:1;zoom:1}.dataTables_wrapper .dataTables_length{float:left}.dataTables_wrapper .dataTables_filter{float:right;text-align:right}.dataTables_wrapper .dataTables_filter input{margin-left:0.5em}.dataTables_wrapper .dataTables_info{clear:both;float:left;padding-top:0.755em}.dataTables_wrapper .dataTables_paginate{float:right;text-align:right;padding-top:0.25em}.dataTables_wrapper .dataTables_paginate .paginate_button{box-sizing:border-box;display:inline-block;min-width:1.5em;padding:0.5em 1em;margin-left:2px;text-align:center;text-decoration:none !important;cursor:pointer;*cursor:hand;color:#333 !important;border:1px solid transparent;border-radius:2px}.dataTables_wrapper .dataTables_paginate .paginate_button.current,.dataTables_wrapper .dataTables_paginate .paginate_button.current:hover{color:#333 !important;border:1px solid #979797;background-color:white;background:-webkit-gradient(linear, left top, left bottom, color-stop(0%, #fff), color-stop(100%, #dcdcdc));background:-webkit-linear-gradient(top, #fff 0%, #dcdcdc 100%);background:-moz-linear-gradient(top, #fff 0%, #dcdcdc 100%);background:-ms-linear-gradient(top, #fff 0%, #dcdcdc 100%);background:-o-linear-gradient(top, #fff 0%, #dcdcdc 100%);background:linear-gradient(to bottom, #fff 0%, #dcdcdc 100%)}.dataTables_wrapper .dataTables_paginate .paginate_button.disabled,.dataTables_wrapper .dataTables_paginate .paginate_button.disabled:hover,.dataTables_wrapper .dataTables_paginate .paginate_button.disabled:active{cursor:default;color:#666 !important;border:1px solid transparent;background:transparent;box-shadow:none}.dataTables_wrapper .dataTables_paginate .paginate_button:hover{color:white !important;border:1px solid #111;background-color:#585858;background:-webkit-gradient(linear, left top, left bottom, color-stop(0%, #585858), color-stop(100%, #111));background:-webkit-linear-gradient(top, #585858 0%, #111 100%);background:-moz-linear-gradient(top, #585858 0%, #111 100%);background:-ms-linear-gradient(top, #585858 0%, #111 100%);background:-o-linear-gradient(top, #585858 0%, #111 100%);background:linear-gradient(to bottom, #585858 0%, #111 100%)}.dataTables_wrapper .dataTables_paginate .paginate_button:active{outline:none;background-color:#2b2b2b;background:-webkit-gradient(linear, left top, left bottom, color-stop(0%, #2b2b2b), color-stop(100%, #0c0c0c));background:-webkit-linear-gradient(top, #2b2b2b 0%, #0c0c0c 100%);background:-moz-linear-gradient(top, #2b2b2b 0%, #0c0c0c 100%);background:-ms-linear-gradient(top, #2b2b2b 0%, #0c0c0c 100%);background:-o-linear-gradient(top, #2b2b2b 0%, #0c0c0c 100%);background:linear-gradient(to bottom, #2b2b2b 0%, #0c0c0c 100%);box-shadow:inset 0 0 3px #111}.dataTables_wrapper .dataTables_paginate .ellipsis{padding:0 1em}.dataTables_wrapper .dataTables_processing{position:absolute;top:50%;left:50%;width:100%;height:40px;margin-left:-50%;margin-top:-25px;padding-top:20px;text-align:center;font-size:1.2em;background-color:white;background:-webkit-gradient(linear, left top, right top, color-stop(0%, rgba(255,255,255,0)), color-stop(25%, rgba(255,255,255,0.9)), color-stop(75%, rgba(255,255,255,0.9)), color-stop(100%, rgba(255,255,255,0)));background:-webkit-linear-gradient(left, rgba(255,255,255,0) 0%, rgba(255,255,255,0.9) 25%, rgba(255,255,255,0.9) 75%, rgba(255,255,255,0) 100%);background:-moz-linear-gradient(left, rgba(255,255,255,0) 0%, rgba(255,255,255,0.9) 25%, rgba(255,255,255,0.9) 75%, rgba(255,255,255,0) 100%);background:-ms-linear-gradient(left, rgba(255,255,255,0) 0%, rgba(255,255,255,0.9) 25%, rgba(255,255,255,0.9) 75%, rgba(255,255,255,0) 100%);background:-o-linear-gradient(left, rgba(255,255,255,0) 0%, rgba(255,255,255,0.9) 25%, rgba(255,255,255,0.9) 75%, rgba(255,255,255,0) 100%);background:linear-gradient(to right, rgba(255,255,255,0) 0%, rgba(255,255,255,0.9) 25%, rgba(255,255,255,0.9) 75%, rgba(255,255,255,0) 100%)}.dataTables_wrapper .dataTables_length,.dataTables_wrapper .dataTables_filter,.dataTables_wrapper .dataTables_info,.dataTables_wrapper .dataTables_processing,.dataTables_wrapper .dataTables_paginate{color:#333}.dataTables_wrapper .dataTables_scroll{clear:both}.dataTables_wrapper .dataTables_scroll div.dataTables_scrollBody{*margin-top:-1px;-webkit-overflow-scrolling:touch}.dataTables_wrapper .dataTables_scroll div.dataTables_scrollBody th>div.dataTables_sizing,.dataTables_wrapper .dataTables_scroll div.dataTables_scrollBody td>div.dataTables_sizing{height:0;overflow:hidden;margin:0 !important;padding:0 !important}.dataTables_wrapper.no-footer .dataTables_scrollBody{border-bottom:1px solid #111}.dataTables_wrapper.no-footer div.dataTables_scrollHead table,.dataTables_wrapper.no-footer div.dataTables_scrollBody table{border-bottom:none}.dataTables_wrapper:after{visibility:hidden;display:block;content:"";clear:both;height:0}@media screen and (max-width: 767px){.dataTables_wrapper .dataTables_info,.dataTables_wrapper .dataTables_paginate{float:none;text-align:center}.dataTables_wrapper .dataTables_paginate{margin-top:0.5em}}@media screen and (max-width: 640px){.dataTables_wrapper .dataTables_length,.dataTables_wrapper .dataTables_filter{float:none;text-align:center}.dataTables_wrapper .dataTables_filter{margin-top:0.5em}} diff --git a/src/_site/vendor/dataTables/css/jquery.dataTables_themeroller.css b/src/_site/vendor/dataTables/css/jquery.dataTables_themeroller.css new file mode 100644 index 00000000..1426a44a --- /dev/null +++ b/src/_site/vendor/dataTables/css/jquery.dataTables_themeroller.css @@ -0,0 +1,416 @@ +/* + * Table styles + */ +table.dataTable { + width: 100%; + margin: 0 auto; + clear: both; + border-collapse: separate; + border-spacing: 0; + /* + * Header and footer styles + */ + /* + * Body styles + */ +} +table.dataTable thead th, +table.dataTable thead td, +table.dataTable tfoot th, +table.dataTable tfoot td { + padding: 4px 10px; +} +table.dataTable thead th, +table.dataTable tfoot th { + font-weight: bold; +} +table.dataTable thead th:active, +table.dataTable thead td:active { + outline: none; +} +table.dataTable thead .sorting_asc, +table.dataTable thead .sorting_desc, +table.dataTable thead .sorting { + cursor: pointer; + *cursor: hand; +} +table.dataTable thead th div.DataTables_sort_wrapper { + position: relative; + padding-right: 10px; +} +table.dataTable thead th div.DataTables_sort_wrapper span { + position: absolute; + top: 50%; + margin-top: -8px; + right: -5px; +} +table.dataTable thead th.ui-state-default { + border-right-width: 0; +} +table.dataTable thead th.ui-state-default:last-child { + border-right-width: 1px; +} +table.dataTable tbody tr { + background-color: #ffffff; +} +table.dataTable tbody tr.selected { + background-color: #B0BED9; +} +table.dataTable tbody th, +table.dataTable tbody td { + padding: 8px 10px; +} +table.dataTable th.center, +table.dataTable td.center, +table.dataTable td.dataTables_empty { + text-align: center; +} +table.dataTable th.right, +table.dataTable td.right { + text-align: right; +} +table.dataTable.row-border tbody th, table.dataTable.row-border tbody td, table.dataTable.display tbody th, table.dataTable.display tbody td { + border-top: 1px solid #ddd; +} +table.dataTable.row-border tbody tr:first-child th, +table.dataTable.row-border tbody tr:first-child td, table.dataTable.display tbody tr:first-child th, +table.dataTable.display tbody tr:first-child td { + border-top: none; +} +table.dataTable.cell-border tbody th, table.dataTable.cell-border tbody td { + border-top: 1px solid #ddd; + border-right: 1px solid #ddd; +} +table.dataTable.cell-border tbody tr th:first-child, +table.dataTable.cell-border tbody tr td:first-child { + border-left: 1px solid #ddd; +} +table.dataTable.cell-border tbody tr:first-child th, +table.dataTable.cell-border tbody tr:first-child td { + border-top: none; +} +table.dataTable.stripe tbody tr.odd, table.dataTable.display tbody tr.odd { + background-color: #f9f9f9; +} +table.dataTable.stripe tbody tr.odd.selected, table.dataTable.display tbody tr.odd.selected { + background-color: #abb9d3; +} +table.dataTable.hover tbody tr:hover, +table.dataTable.hover tbody tr.odd:hover, +table.dataTable.hover tbody tr.even:hover, table.dataTable.display tbody tr:hover, +table.dataTable.display tbody tr.odd:hover, +table.dataTable.display tbody tr.even:hover { + background-color: whitesmoke; +} +table.dataTable.hover tbody tr:hover.selected, +table.dataTable.hover tbody tr.odd:hover.selected, +table.dataTable.hover tbody tr.even:hover.selected, table.dataTable.display tbody tr:hover.selected, +table.dataTable.display tbody tr.odd:hover.selected, +table.dataTable.display tbody tr.even:hover.selected { + background-color: #a9b7d1; +} +table.dataTable.order-column tbody tr > .sorting_1, +table.dataTable.order-column tbody tr > .sorting_2, +table.dataTable.order-column tbody tr > .sorting_3, table.dataTable.display tbody tr > .sorting_1, +table.dataTable.display tbody tr > .sorting_2, +table.dataTable.display tbody tr > .sorting_3 { + background-color: #f9f9f9; +} +table.dataTable.order-column tbody tr.selected > .sorting_1, +table.dataTable.order-column tbody tr.selected > .sorting_2, +table.dataTable.order-column tbody tr.selected > .sorting_3, table.dataTable.display tbody tr.selected > .sorting_1, +table.dataTable.display tbody tr.selected > .sorting_2, +table.dataTable.display tbody tr.selected > .sorting_3 { + background-color: #acbad4; +} +table.dataTable.display tbody tr.odd > .sorting_1, table.dataTable.order-column.stripe tbody tr.odd > .sorting_1 { + background-color: #f1f1f1; +} +table.dataTable.display tbody tr.odd > .sorting_2, table.dataTable.order-column.stripe tbody tr.odd > .sorting_2 { + background-color: #f3f3f3; +} +table.dataTable.display tbody tr.odd > .sorting_3, table.dataTable.order-column.stripe tbody tr.odd > .sorting_3 { + background-color: whitesmoke; +} +table.dataTable.display tbody tr.odd.selected > .sorting_1, table.dataTable.order-column.stripe tbody tr.odd.selected > .sorting_1 { + background-color: #a6b3cd; +} +table.dataTable.display tbody tr.odd.selected > .sorting_2, table.dataTable.order-column.stripe tbody tr.odd.selected > .sorting_2 { + background-color: #a7b5ce; +} +table.dataTable.display tbody tr.odd.selected > .sorting_3, table.dataTable.order-column.stripe tbody tr.odd.selected > .sorting_3 { + background-color: #a9b6d0; +} +table.dataTable.display tbody tr.even > .sorting_1, table.dataTable.order-column.stripe tbody tr.even > .sorting_1 { + background-color: #f9f9f9; +} +table.dataTable.display tbody tr.even > .sorting_2, table.dataTable.order-column.stripe tbody tr.even > .sorting_2 { + background-color: #fbfbfb; +} +table.dataTable.display tbody tr.even > .sorting_3, table.dataTable.order-column.stripe tbody tr.even > .sorting_3 { + background-color: #fdfdfd; +} +table.dataTable.display tbody tr.even.selected > .sorting_1, table.dataTable.order-column.stripe tbody tr.even.selected > .sorting_1 { + background-color: #acbad4; +} +table.dataTable.display tbody tr.even.selected > .sorting_2, table.dataTable.order-column.stripe tbody tr.even.selected > .sorting_2 { + background-color: #adbbd6; +} +table.dataTable.display tbody tr.even.selected > .sorting_3, table.dataTable.order-column.stripe tbody tr.even.selected > .sorting_3 { + background-color: #afbdd8; +} +table.dataTable.display tbody tr:hover > .sorting_1, +table.dataTable.display tbody tr.odd:hover > .sorting_1, +table.dataTable.display tbody tr.even:hover > .sorting_1, table.dataTable.order-column.hover tbody tr:hover > .sorting_1, +table.dataTable.order-column.hover tbody tr.odd:hover > .sorting_1, +table.dataTable.order-column.hover tbody tr.even:hover > .sorting_1 { + background-color: #eaeaea; +} +table.dataTable.display tbody tr:hover > .sorting_2, +table.dataTable.display tbody tr.odd:hover > .sorting_2, +table.dataTable.display tbody tr.even:hover > .sorting_2, table.dataTable.order-column.hover tbody tr:hover > .sorting_2, +table.dataTable.order-column.hover tbody tr.odd:hover > .sorting_2, +table.dataTable.order-column.hover tbody tr.even:hover > .sorting_2 { + background-color: #ebebeb; +} +table.dataTable.display tbody tr:hover > .sorting_3, +table.dataTable.display tbody tr.odd:hover > .sorting_3, +table.dataTable.display tbody tr.even:hover > .sorting_3, table.dataTable.order-column.hover tbody tr:hover > .sorting_3, +table.dataTable.order-column.hover tbody tr.odd:hover > .sorting_3, +table.dataTable.order-column.hover tbody tr.even:hover > .sorting_3 { + background-color: #eeeeee; +} +table.dataTable.display tbody tr:hover.selected > .sorting_1, +table.dataTable.display tbody tr.odd:hover.selected > .sorting_1, +table.dataTable.display tbody tr.even:hover.selected > .sorting_1, table.dataTable.order-column.hover tbody tr:hover.selected > .sorting_1, +table.dataTable.order-column.hover tbody tr.odd:hover.selected > .sorting_1, +table.dataTable.order-column.hover tbody tr.even:hover.selected > .sorting_1 { + background-color: #a1aec7; +} +table.dataTable.display tbody tr:hover.selected > .sorting_2, +table.dataTable.display tbody tr.odd:hover.selected > .sorting_2, +table.dataTable.display tbody tr.even:hover.selected > .sorting_2, table.dataTable.order-column.hover tbody tr:hover.selected > .sorting_2, +table.dataTable.order-column.hover tbody tr.odd:hover.selected > .sorting_2, +table.dataTable.order-column.hover tbody tr.even:hover.selected > .sorting_2 { + background-color: #a2afc8; +} +table.dataTable.display tbody tr:hover.selected > .sorting_3, +table.dataTable.display tbody tr.odd:hover.selected > .sorting_3, +table.dataTable.display tbody tr.even:hover.selected > .sorting_3, table.dataTable.order-column.hover tbody tr:hover.selected > .sorting_3, +table.dataTable.order-column.hover tbody tr.odd:hover.selected > .sorting_3, +table.dataTable.order-column.hover tbody tr.even:hover.selected > .sorting_3 { + background-color: #a4b2cb; +} +table.dataTable.nowrap th, table.dataTable.nowrap td { + white-space: nowrap; +} +table.dataTable.compact thead th, +table.dataTable.compact thead td { + padding: 5px 9px; +} +table.dataTable.compact tfoot th, +table.dataTable.compact tfoot td { + padding: 5px 9px 3px 9px; +} +table.dataTable.compact tbody th, +table.dataTable.compact tbody td { + padding: 4px 5px; +} +table.dataTable th.dt-left, +table.dataTable td.dt-left { + text-align: left; +} +table.dataTable th.dt-center, +table.dataTable td.dt-center, +table.dataTable td.dataTables_empty { + text-align: center; +} +table.dataTable th.dt-right, +table.dataTable td.dt-right { + text-align: right; +} +table.dataTable th.dt-justify, +table.dataTable td.dt-justify { + text-align: justify; +} +table.dataTable th.dt-nowrap, +table.dataTable td.dt-nowrap { + white-space: nowrap; +} +table.dataTable thead th.dt-head-left, +table.dataTable thead td.dt-head-left, +table.dataTable tfoot th.dt-head-left, +table.dataTable tfoot td.dt-head-left { + text-align: left; +} +table.dataTable thead th.dt-head-center, +table.dataTable thead td.dt-head-center, +table.dataTable tfoot th.dt-head-center, +table.dataTable tfoot td.dt-head-center { + text-align: center; +} +table.dataTable thead th.dt-head-right, +table.dataTable thead td.dt-head-right, +table.dataTable tfoot th.dt-head-right, +table.dataTable tfoot td.dt-head-right { + text-align: right; +} +table.dataTable thead th.dt-head-justify, +table.dataTable thead td.dt-head-justify, +table.dataTable tfoot th.dt-head-justify, +table.dataTable tfoot td.dt-head-justify { + text-align: justify; +} +table.dataTable thead th.dt-head-nowrap, +table.dataTable thead td.dt-head-nowrap, +table.dataTable tfoot th.dt-head-nowrap, +table.dataTable tfoot td.dt-head-nowrap { + white-space: nowrap; +} +table.dataTable tbody th.dt-body-left, +table.dataTable tbody td.dt-body-left { + text-align: left; +} +table.dataTable tbody th.dt-body-center, +table.dataTable tbody td.dt-body-center { + text-align: center; +} +table.dataTable tbody th.dt-body-right, +table.dataTable tbody td.dt-body-right { + text-align: right; +} +table.dataTable tbody th.dt-body-justify, +table.dataTable tbody td.dt-body-justify { + text-align: justify; +} +table.dataTable tbody th.dt-body-nowrap, +table.dataTable tbody td.dt-body-nowrap { + white-space: nowrap; +} + +table.dataTable, +table.dataTable th, +table.dataTable td { + -webkit-box-sizing: content-box; + -moz-box-sizing: content-box; + box-sizing: content-box; +} + +/* + * Control feature layout + */ +.dataTables_wrapper { + position: relative; + clear: both; + *zoom: 1; + zoom: 1; +} +.dataTables_wrapper .dataTables_length { + float: left; +} +.dataTables_wrapper .dataTables_filter { + float: right; + text-align: right; +} +.dataTables_wrapper .dataTables_filter input { + margin-left: 0.5em; +} +.dataTables_wrapper .dataTables_info { + clear: both; + float: left; + padding-top: 0.55em; +} +.dataTables_wrapper .dataTables_paginate { + float: right; + text-align: right; +} +.dataTables_wrapper .dataTables_paginate .fg-button { + box-sizing: border-box; + display: inline-block; + min-width: 1.5em; + padding: 0.5em; + margin-left: 2px; + text-align: center; + text-decoration: none !important; + cursor: pointer; + *cursor: hand; + color: #333 !important; + border: 1px solid transparent; +} +.dataTables_wrapper .dataTables_paginate .fg-button:active { + outline: none; +} +.dataTables_wrapper .dataTables_paginate .fg-button:first-child { + border-top-left-radius: 3px; + border-bottom-left-radius: 3px; +} +.dataTables_wrapper .dataTables_paginate .fg-button:last-child { + border-top-right-radius: 3px; + border-bottom-right-radius: 3px; +} +.dataTables_wrapper .dataTables_processing { + position: absolute; + top: 50%; + left: 50%; + width: 100%; + height: 40px; + margin-left: -50%; + margin-top: -25px; + padding-top: 20px; + text-align: center; + font-size: 1.2em; + background-color: white; + background: -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(255, 255, 255, 0)), color-stop(25%, rgba(255, 255, 255, 0.9)), color-stop(75%, rgba(255, 255, 255, 0.9)), color-stop(100%, rgba(255, 255, 255, 0))); + /* Chrome,Safari4+ */ + background: -webkit-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.9) 25%, rgba(255, 255, 255, 0.9) 75%, rgba(255, 255, 255, 0) 100%); + /* Chrome10+,Safari5.1+ */ + background: -moz-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.9) 25%, rgba(255, 255, 255, 0.9) 75%, rgba(255, 255, 255, 0) 100%); + /* FF3.6+ */ + background: -ms-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.9) 25%, rgba(255, 255, 255, 0.9) 75%, rgba(255, 255, 255, 0) 100%); + /* IE10+ */ + background: -o-linear-gradient(left, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.9) 25%, rgba(255, 255, 255, 0.9) 75%, rgba(255, 255, 255, 0) 100%); + /* Opera 11.10+ */ + background: linear-gradient(to right, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.9) 25%, rgba(255, 255, 255, 0.9) 75%, rgba(255, 255, 255, 0) 100%); + /* W3C */ +} +.dataTables_wrapper .dataTables_length, +.dataTables_wrapper .dataTables_filter, +.dataTables_wrapper .dataTables_info, +.dataTables_wrapper .dataTables_processing, +.dataTables_wrapper .dataTables_paginate { + color: #333; +} +.dataTables_wrapper .dataTables_scroll { + clear: both; +} +.dataTables_wrapper .dataTables_scrollBody { + *margin-top: -1px; + -webkit-overflow-scrolling: touch; +} +.dataTables_wrapper .ui-widget-header { + font-weight: normal; +} +.dataTables_wrapper .ui-toolbar { + padding: 8px; +} +.dataTables_wrapper:after { + visibility: hidden; + display: block; + content: ""; + clear: both; + height: 0; +} + +@media screen and (max-width: 767px) { + .dataTables_wrapper .dataTables_length, + .dataTables_wrapper .dataTables_filter, + .dataTables_wrapper .dataTables_info, + .dataTables_wrapper .dataTables_paginate { + float: none; + text-align: center; + } + .dataTables_wrapper .dataTables_filter, + .dataTables_wrapper .dataTables_paginate { + margin-top: 0.5em; + } +} diff --git a/src/_site/vendor/dataTables/images/sort_asc.png b/src/_site/vendor/dataTables/images/sort_asc.png new file mode 100644 index 0000000000000000000000000000000000000000..e1ba61a8055fcb18273f2468d335572204667b1f GIT binary patch literal 160 zcmeAS@N?(olHy`uVBq!ia0vp^!XV7S1|*9D%+3I*bWaz@5R22v2@;zYta_*?F5u6Q zWR@in#&u+WgT?Hi<}D3B3}GOXuX|8Oj3tosHiJ3*4TN zC7>_x-r1O=t(?KoTC+`+>7&2GzdqLHBg&F)2Q?&EGZ+}|Rpsc~9`m>jw35No)z4*} HQ$iB}HK{Sd literal 0 HcmV?d00001 diff --git a/src/_site/vendor/dataTables/images/sort_asc_disabled.png b/src/_site/vendor/dataTables/images/sort_asc_disabled.png new file mode 100644 index 0000000000000000000000000000000000000000..fb11dfe24a6c564cb7ddf8bc96703ebb121df1e7 GIT binary patch literal 148 zcmeAS@N?(olHy`uVBq!ia0vp^!XV7S0wixl{&NRX(Vi}jAsXkC6BcOhI9!^3NY?Do zDX;f`c1`y6n0RgO@$!H7chZT&|Jn0dmaqO^XNm-CGtk!Ur<_=Jws3;%W$<+Mb6Mw<&;$T1GdZXL literal 0 HcmV?d00001 diff --git a/src/_site/vendor/dataTables/images/sort_both.png b/src/_site/vendor/dataTables/images/sort_both.png new file mode 100644 index 0000000000000000000000000000000000000000..af5bc7c5a10b9d6d57cb641aeec752428a07f0ca GIT binary patch literal 201 zcmeAS@N?(olHy`uVBq!ia0vp^!XV7S0wixl{&NRX6FglULp08Bycxyy87-Q;~nRxO8@-UU*I^KVWyN+&SiMHu5xDOu|HNvwzODfTdXjhVyNu1 z#7^XbGKZ7LW3XeONb$RKLeE*WhqbYpIXPIqK@r4)v+qN8um%99%MPpS9d#7Ed7SL@Bp00i_>zopr0H-Zb Aj{pDw literal 0 HcmV?d00001 diff --git a/src/_site/vendor/dataTables/images/sort_desc.png b/src/_site/vendor/dataTables/images/sort_desc.png new file mode 100644 index 0000000000000000000000000000000000000000..0e156deb5f61d18f9e2ec5da4f6a8c94a5b4fb41 GIT binary patch literal 158 zcmeAS@N?(olHy`uVBq!ia0vp^!XV7S1|*9D%+3I*R8JSj5R22v2@yo z(czD9$NuDl3Ljm9c#_#4$vXUz=f1~&WY3aa=h!;z7fOEN>ySP9QA=6C-^Dmb&tuM= z4Z&=WZU;2WF>e%GI&mWJk^K!jrbro{W;-I>FeCfLGJl3}+Z^2)3Kw?+EoAU?^>bP0 Hl+XkKC^j|Q{b@g3TV7E(Grjn^aLC2o)_ptHrtUEoT$S@q)~)7U@V;W{6)!%@ u>N?4t-1qslpJw9!O?PJ&w0Cby<'col-sm-6'f>>" + + "<'row'<'col-sm-12'tr>>" + + "<'row'<'col-sm-5'i><'col-sm-7'p>>", + renderer: 'bootstrap' +} ); + + +/* Default class modification */ +$.extend( DataTable.ext.classes, { + sWrapper: "dataTables_wrapper form-inline dt-bootstrap", + sFilterInput: "form-control input-sm", + sLengthSelect: "form-control input-sm" +} ); + + +/* Bootstrap paging button renderer */ +DataTable.ext.renderer.pageButton.bootstrap = function ( settings, host, idx, buttons, page, pages ) { + var api = new DataTable.Api( settings ); + var classes = settings.oClasses; + var lang = settings.oLanguage.oPaginate; + var btnDisplay, btnClass, counter=0; + + var attach = function( container, buttons ) { + var i, ien, node, button; + var clickHandler = function ( e ) { + e.preventDefault(); + if ( !$(e.currentTarget).hasClass('disabled') ) { + api.page( e.data.action ).draw( 'page' ); + } + }; + + for ( i=0, ien=buttons.length ; i 0 ? + '' : ' disabled'); + break; + + case 'previous': + btnDisplay = lang.sPrevious; + btnClass = button + (page > 0 ? + '' : ' disabled'); + break; + + case 'next': + btnDisplay = lang.sNext; + btnClass = button + (page < pages-1 ? + '' : ' disabled'); + break; + + case 'last': + btnDisplay = lang.sLast; + btnClass = button + (page < pages-1 ? + '' : ' disabled'); + break; + + default: + btnDisplay = button + 1; + btnClass = page === button ? + 'active' : ''; + break; + } + + if ( btnDisplay ) { + node = $('
  • ', { + 'class': classes.sPageButton+' '+btnClass, + 'id': idx === 0 && typeof button === 'string' ? + settings.sTableId +'_'+ button : + null + } ) + .append( $('', { + 'href': '#', + 'aria-controls': settings.sTableId, + 'data-dt-idx': counter, + 'tabindex': settings.iTabIndex + } ) + .html( btnDisplay ) + ) + .appendTo( container ); + + settings.oApi._fnBindAction( + node, {action: button}, clickHandler + ); + + counter++; + } + } + } + }; + + // IE9 throws an 'unknown error' if document.activeElement is used + // inside an iframe or frame. + var activeEl; + + try { + // Because this approach is destroying and recreating the paging + // elements, focus is lost on the select button which is bad for + // accessibility. So we want to restore focus once the draw has + // completed + activeEl = $(host).find(document.activeElement).data('dt-idx'); + } + catch (e) {} + + attach( + $(host).empty().html('