diff options
author | Tschaen, Brendan <ctschaen@att.com> | 2019-02-05 15:12:48 -0500 |
---|---|---|
committer | Tschaen, Brendan <ctschaen@att.com> | 2019-02-07 11:03:57 -0500 |
commit | 019a55afae68f555348d60cc197b0d188eaaf4af (patch) | |
tree | 5974bf9d9035c3d2c7eac37a3db50e8e2a7692de | |
parent | c8b529749f19a1705a4fd0791eb83710c0b53e7d (diff) |
Update sql parse interface
Created junit tests
Fix files code formatting
Issue-ID: MUSIC-311
Change-Id: I9e11a17482a2567055fdee0da8e206ac92235d5f
Signed-off-by: Tschaen, Brendan <ctschaen@att.com>
-rwxr-xr-x | mdbc-server/pom.xml | 2 | ||||
-rw-r--r-- | mdbc-server/src/main/java/org/onap/music/mdbc/query/QueryProcessor.java | 377 | ||||
-rw-r--r-- | mdbc-server/src/test/java/org/onap/music/mdbc/query/QueryProcessorTest.java | 105 | ||||
-rwxr-xr-x | pom.xml | 2 |
4 files changed, 288 insertions, 198 deletions
diff --git a/mdbc-server/pom.xml b/mdbc-server/pom.xml index 5f9048f..d98fcc3 100755 --- a/mdbc-server/pom.xml +++ b/mdbc-server/pom.xml @@ -182,7 +182,7 @@ <dependency> <groupId>org.onap.music</groupId> <artifactId>dev-MUSIC-cassandra</artifactId> - <version>3.2.1-SNAPSHOT</version> + <version>3.2.1</version> <exclusions> <exclusion> <groupId>io.netty</groupId> diff --git a/mdbc-server/src/main/java/org/onap/music/mdbc/query/QueryProcessor.java b/mdbc-server/src/main/java/org/onap/music/mdbc/query/QueryProcessor.java index 8e09065..4134540 100644 --- a/mdbc-server/src/main/java/org/onap/music/mdbc/query/QueryProcessor.java +++ b/mdbc-server/src/main/java/org/onap/music/mdbc/query/QueryProcessor.java @@ -29,6 +29,7 @@ import org.apache.calcite.avatica.util.Casing; import org.apache.calcite.avatica.util.Quoting; import org.apache.calcite.sql.SqlCall; import org.apache.calcite.sql.SqlInsert; +import org.apache.calcite.sql.SqlKind; import org.apache.calcite.sql.SqlNode; import org.apache.calcite.sql.SqlNodeList; import org.apache.calcite.sql.SqlSelect; @@ -55,209 +56,193 @@ import net.sf.jsqlparser.util.TablesNamesFinder; public class QueryProcessor { - private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(QueryProcessor.class); + private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(QueryProcessor.class); - public List<String> tables = null; + public List<String> tables = null; - public QueryProcessor() { + public QueryProcessor() { - } + } - protected static SqlParserImplFactory parserImplFactory() { - return SqlParserImpl.FACTORY; - } + protected static SqlParserImplFactory parserImplFactory() { + return SqlParserImpl.FACTORY; + } - protected static SqlParser getSqlParser(String sql) { - Quoting quoting = Quoting.DOUBLE_QUOTE; - Casing unquotedCasing = Casing.TO_UPPER; - Casing quotedCasing = Casing.UNCHANGED; - SqlConformance conformance = SqlConformanceEnum.DEFAULT; - - return SqlParser.create(sql, SqlParser.configBuilder().setParserFactory(parserImplFactory()).setQuoting(quoting) - .setUnquotedCasing(unquotedCasing).setQuotedCasing(quotedCasing).setConformance(conformance).build()); - } + protected static SqlParser getSqlParser(String sql) { + Quoting quoting = Quoting.DOUBLE_QUOTE; + Casing unquotedCasing = Casing.TO_UPPER; + Casing quotedCasing = Casing.UNCHANGED; + SqlConformance conformance = SqlConformanceEnum.DEFAULT; - public static Map<String, List<String>> parseSqlQuery(String query) throws SqlParseException { - logger.info(EELFLoggerDelegate.applicationLogger, "Parsing query: "+query); - Map<String, List<String>> tableOpsMap = new HashMap<>(); - //for Create no need to check locks. - if(query.toUpperCase().startsWith("CREATE")) { - logger.error(EELFLoggerDelegate.errorLogger, "CREATE TABLE DDL not currently supported currently."); - return tableOpsMap; - } - - /*SqlParser parser = SqlParser.create(query); - SqlNode sqlNode = parser.parseQuery();*/ - SqlNode sqlNode = getSqlParser(query).parseStmt(); - - SqlBasicVisitor<Void> visitor = new SqlBasicVisitor<Void>() { - - public Void visit(SqlCall call) { - if (call.getOperator() instanceof SqlInOperator) { - throw new Util.FoundOne(call); - } - return super.visit(call); - } - - }; - - // sqlNode.accept(new SqlAnalyzer()); - sqlNode.accept(visitor); - - if (sqlNode instanceof SqlInsert) { - SqlInsert sqlInsert = (SqlInsert) sqlNode; - SqlNodeList targetColumnList = sqlInsert.getTargetColumnList(); - System.out.println("targetColumnList: "+targetColumnList); - String tableName = sqlInsert.getTargetTable().toString(); - List<String> Ops = tableOpsMap.get(tableName); - if (Ops == null) - Ops = new ArrayList<>(); - Ops.add(Operation.INSERT.getOperation()); - tableOpsMap.put(tableName, Ops); - } else if (sqlNode instanceof SqlUpdate) { - SqlUpdate sqlUpdate = (SqlUpdate) sqlNode; - String tableName = sqlUpdate.getTargetTable().toString(); - List<String> Ops = tableOpsMap.get(tableName); - if (Ops == null) - Ops = new ArrayList<>(); - Ops.add(Operation.UPDATE.getOperation()); - tableOpsMap.put(tableName, Ops); - } else if (sqlNode instanceof SqlSelect) { - SqlSelect sqlSelect = (SqlSelect) sqlNode; - SqlNodeList selectList = sqlSelect.getSelectList(); - String tables = sqlSelect.getFrom().toString(); - String[] tablesArr = tables.split(","); - - SqlNode where = sqlSelect.getWhere(); - - for (String table : tablesArr) { - - String tableName = null; - if(table.contains("`")) { - String[] split = table.split("`"); - tableName = split[1]; - } else - tableName = table; - - List<String> Ops = tableOpsMap.get(tableName); - if (Ops == null) Ops = new ArrayList<>(); - if (where == null) { - Ops.add(Operation.TABLE.getOperation()); - tableOpsMap.put(tableName, Ops); - } else { - Ops.add(Operation.SELECT.getOperation()); - tableOpsMap.put(tableName, Ops); - } - } - } - - return tableOpsMap; - } + return SqlParser.create(sql, SqlParser.configBuilder().setParserFactory(parserImplFactory()).setQuoting(quoting) + .setUnquotedCasing(unquotedCasing).setQuotedCasing(quotedCasing).setConformance(conformance).build()); + } - @Deprecated - public static Map<String, List<String>> extractTableFromQuery(String sqlQuery) { - List<String> tables = null; - Map<String, List<String>> tableOpsMap = new HashMap<>(); - try { - net.sf.jsqlparser.statement.Statement stmt = CCJSqlParserUtil.parse(sqlQuery); - if (stmt instanceof Insert) { - Insert s = (Insert) stmt; - String tbl = s.getTable().getName(); - List<String> Ops = tableOpsMap.get(tbl); - if (Ops == null) - Ops = new ArrayList<>(); - Ops.add(Operation.INSERT.getOperation()); - tableOpsMap.put(tbl, Ops); - logger.debug(EELFLoggerDelegate.applicationLogger, "Inserting into table: " + tbl); - } else { - String tbl; - String where = ""; - if (stmt instanceof Update) { - Update u = (Update) stmt; - tbl = u.getTables().get(0).getName(); - List<String> Ops = tableOpsMap.get(tbl); - if (Ops == null) - Ops = new ArrayList<>(); - if (u.getWhere() != null) { - where = u.getWhere().toString(); - logger.debug(EELFLoggerDelegate.applicationLogger, "Updating table: " + tbl); - Ops.add(Operation.UPDATE.getOperation()); - } else { - Ops.add(Operation.TABLE.getOperation()); - } - tableOpsMap.put(tbl, Ops); - } else if (stmt instanceof Delete) { - Delete d = (Delete) stmt; - tbl = d.getTable().getName(); - List<String> Ops = tableOpsMap.get(tbl); - if (Ops == null) - Ops = new ArrayList<>(); - if (d.getWhere() != null) { - where = d.getWhere().toString(); - Ops.add(Operation.DELETE.getOperation()); - } else { - Ops.add(Operation.TABLE.getOperation()); - } - tableOpsMap.put(tbl, Ops); - logger.debug(EELFLoggerDelegate.applicationLogger, "Deleting from table: " + tbl); - } else if (stmt instanceof Select) { - TablesNamesFinder tablesNamesFinder = new TablesNamesFinder(); - tables = tablesNamesFinder.getTableList(stmt); - for (String table : tables) { - List<String> Ops = tableOpsMap.get(table); - if (Ops == null) - Ops = new ArrayList<>(); - Ops.add(Operation.SELECT.getOperation()); - tableOpsMap.put(table, Ops); - } - } else if (stmt instanceof CreateTable) { - CreateTable ct = (CreateTable) stmt; - List<String> Ops = new ArrayList<>(); - Ops.add(Operation.TABLE.getOperation()); - tableOpsMap.put(ct.getTable().getName(), Ops); - } else { - logger.error(EELFLoggerDelegate.errorLogger, "Not recognized sql type:" + stmt.getClass()); - tbl = ""; - } - } - } catch (JSQLParserException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return tableOpsMap; - } + /** + * + * @param query + * @return map of table name to {@link org.onap.music.mdbc.query.Operation} + * @throws SqlParseException + */ + public static Map<String, List<Operation>> parseSqlQuery(String query) throws SqlParseException { + logger.info(EELFLoggerDelegate.applicationLogger, "Parsing query: "+query); + Map<String, List<Operation>> tableOpsMap = new HashMap<>(); + //for Create no need to check locks. + if(query.toUpperCase().startsWith("CREATE")) { + logger.error(EELFLoggerDelegate.errorLogger, "CREATE TABLE DDL not currently supported currently."); + return tableOpsMap; + } - public static void main(String[] args) throws SqlParseException { - - String sqlQuery = "CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20))"; - System.out.println(parseSqlQuery(sqlQuery)); - - sqlQuery = "SELECT name, age FROM table1 t1, table2 t2 WHERE t1.id = t2.id"; - //Map<String, List<String>> tableOpsMap = extractTableFromQuery(sqlQuery); - System.out.println(parseSqlQuery(sqlQuery)); - - sqlQuery = "SELECT name, age FROM table1, table2 t2 WHERE id = t2.id"; - // Map<String, List<String>> tableOpsMap = extractTableFromQuery(sqlQuery); - System.out.println(parseSqlQuery(sqlQuery)); - - sqlQuery = "SELECT name, age FROM table1 t1"; - // Map<String, List<String>> tableOpsMap = extractTableFromQuery(sqlQuery); - System.out.println(parseSqlQuery(sqlQuery)); - - sqlQuery = "INSERT INTO Employees (id, name) values ('1','Vikram')"; - // tableOpsMap = extractTableFromQuery(sqlQuery); - // System.out.println(tableOpsMap); - System.out.println(parseSqlQuery(sqlQuery)); - - sqlQuery = "UPDATE Employees SET id = 1 WHERE id = 2"; - System.out.println(parseSqlQuery(sqlQuery)); - - sqlQuery = "UPDATE Employees SET id = 1"; - System.out.println(parseSqlQuery(sqlQuery)); - - sqlQuery = "UPDATE table1 SET id = 1"; - System.out.println(parseSqlQuery(sqlQuery)); - - } + /*SqlParser parser = SqlParser.create(query); + SqlNode sqlNode = parser.parseQuery();*/ + SqlNode sqlNode = getSqlParser(query).parseStmt(); + + SqlBasicVisitor<Void> visitor = new SqlBasicVisitor<Void>() { + + public Void visit(SqlCall call) { + if (call.getOperator() instanceof SqlInOperator) { + throw new Util.FoundOne(call); + } + return super.visit(call); + } + + }; + + // sqlNode.accept(new SqlAnalyzer()); + sqlNode.accept(visitor); + switch (sqlNode.getKind()) { + case INSERT: + parseInsert((SqlInsert) sqlNode, tableOpsMap); + break; + case UPDATE: + parseUpdate((SqlUpdate) sqlNode, tableOpsMap); + break; + case SELECT: + parseSelect((SqlSelect) sqlNode, tableOpsMap); + break; + default: + logger.error("Unhandled sql query type " + sqlNode.getKind() +" for query " + query); + } + return tableOpsMap; + } + + private static void parseInsert(SqlInsert sqlNode, Map<String, List<Operation>> tableOpsMap) { + SqlInsert sqlInsert = (SqlInsert) sqlNode; + String tableName = sqlInsert.getTargetTable().toString(); + //handle insert into select query + if (sqlInsert.getSource().getKind()==SqlKind.SELECT) { + parseSelect((SqlSelect) sqlInsert.getSource(), tableOpsMap); + } + List<Operation> Ops = tableOpsMap.get(tableName); + if (Ops == null) + Ops = new ArrayList<>(); + Ops.add(Operation.INSERT); + tableOpsMap.put(tableName, Ops); + } + + private static void parseUpdate(SqlUpdate sqlNode, Map<String, List<Operation>> tableOpsMap) { + SqlUpdate sqlUpdate = (SqlUpdate) sqlNode; + String tableName = sqlUpdate.getTargetTable().toString(); + List<Operation> Ops = tableOpsMap.get(tableName); + if (Ops == null) + Ops = new ArrayList<>(); + Ops.add(Operation.UPDATE); + tableOpsMap.put(tableName, Ops); + } + + private static void parseSelect(SqlSelect sqlNode, Map<String, List<Operation>> tableOpsMap ) { + SqlSelect sqlSelect = (SqlSelect) sqlNode; + SqlNodeList selectList = sqlSelect.getSelectList(); + String tables = sqlSelect.getFrom().toString(); + String[] tablesArr = tables.split(","); + + for (String table : tablesArr) { + + String tableName = null; + if(table.contains("`")) { + String[] split = table.split("`"); + tableName = split[1]; + } else { + tableName = table; + } + List<Operation> Ops = tableOpsMap.get(tableName); + if (Ops == null) Ops = new ArrayList<>(); + Ops.add(Operation.SELECT); + tableOpsMap.put(tableName, Ops); + } + } + + @Deprecated + public static Map<String, List<String>> extractTableFromQuery(String sqlQuery) { + List<String> tables = null; + Map<String, List<String>> tableOpsMap = new HashMap<>(); + try { + net.sf.jsqlparser.statement.Statement stmt = CCJSqlParserUtil.parse(sqlQuery); + if (stmt instanceof Insert) { + Insert s = (Insert) stmt; + String tbl = s.getTable().getName(); + List<String> Ops = tableOpsMap.get(tbl); + if (Ops == null) + Ops = new ArrayList<>(); + Ops.add(Operation.INSERT.getOperation()); + tableOpsMap.put(tbl, Ops); + logger.debug(EELFLoggerDelegate.applicationLogger, "Inserting into table: " + tbl); + } else { + String tbl; + String where = ""; + if (stmt instanceof Update) { + Update u = (Update) stmt; + tbl = u.getTables().get(0).getName(); + List<String> Ops = tableOpsMap.get(tbl); + if (Ops == null) + Ops = new ArrayList<>(); + if (u.getWhere() != null) { + where = u.getWhere().toString(); + logger.debug(EELFLoggerDelegate.applicationLogger, "Updating table: " + tbl); + Ops.add(Operation.UPDATE.getOperation()); + } else { + Ops.add(Operation.TABLE.getOperation()); + } + tableOpsMap.put(tbl, Ops); + } else if (stmt instanceof Delete) { + Delete d = (Delete) stmt; + tbl = d.getTable().getName(); + List<String> Ops = tableOpsMap.get(tbl); + if (Ops == null) + Ops = new ArrayList<>(); + if (d.getWhere() != null) { + where = d.getWhere().toString(); + Ops.add(Operation.DELETE.getOperation()); + } else { + Ops.add(Operation.TABLE.getOperation()); + } + tableOpsMap.put(tbl, Ops); + logger.debug(EELFLoggerDelegate.applicationLogger, "Deleting from table: " + tbl); + } else if (stmt instanceof Select) { + TablesNamesFinder tablesNamesFinder = new TablesNamesFinder(); + tables = tablesNamesFinder.getTableList(stmt); + for (String table : tables) { + List<String> Ops = tableOpsMap.get(table); + if (Ops == null) + Ops = new ArrayList<>(); + Ops.add(Operation.SELECT.getOperation()); + tableOpsMap.put(table, Ops); + } + } else if (stmt instanceof CreateTable) { + CreateTable ct = (CreateTable) stmt; + List<String> Ops = new ArrayList<>(); + Ops.add(Operation.TABLE.getOperation()); + tableOpsMap.put(ct.getTable().getName(), Ops); + } else { + logger.error(EELFLoggerDelegate.errorLogger, "Not recognized sql type:" + stmt.getClass()); + tbl = ""; + } + } + } catch (JSQLParserException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return tableOpsMap; + } } diff --git a/mdbc-server/src/test/java/org/onap/music/mdbc/query/QueryProcessorTest.java b/mdbc-server/src/test/java/org/onap/music/mdbc/query/QueryProcessorTest.java new file mode 100644 index 0000000..63147e3 --- /dev/null +++ b/mdbc-server/src/test/java/org/onap/music/mdbc/query/QueryProcessorTest.java @@ -0,0 +1,105 @@ +/* + * ============LICENSE_START==================================================== org.onap.music.mdbc + * ============================================================================= Copyright (C) 2018 AT&T Intellectual + * Property. All rights reserved. ============================================================================= Licensed + * under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * ============LICENSE_END====================================================== + */ + +package org.onap.music.mdbc.query; + +import static org.junit.Assert.*; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import org.apache.calcite.sql.parser.SqlParseException; +import org.junit.Test; +import org.onap.music.mdbc.tables.MusicTxDigest; +import org.onap.music.mdbc.tables.StagingTable; + + +public class QueryProcessorTest { + + @Test + public void tableQuery() throws Exception { + String sqlQuery = "CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20))"; + HashMap<String, List<Operation>> expectedOut = new HashMap<>(); + List<Operation> op = new ArrayList<>(); + // no table ops for now + // op.add(Operation.TABLE); + // expectedOut.put("pet", op); + assertEquals(expectedOut, QueryProcessor.parseSqlQuery(sqlQuery)); + } + + @Test + public void selectQuery() throws SqlParseException { + String sqlQuery = "SELECT name, age FROM table1 t1"; + HashMap<String, List<Operation>> expectedOut = new HashMap<>(); + List<Operation> t1op = new ArrayList<>(); + t1op.add(Operation.SELECT); + expectedOut.put("TABLE1", t1op); + assertEquals(expectedOut, QueryProcessor.parseSqlQuery(sqlQuery)); + } + + @Test + public void selectQuery2Table() throws SqlParseException { + String sqlQuery = "SELECT name, age FROM table1 t1, table2 t2 WHERE t1.id = t2.id"; + HashMap<String, List<Operation>> expectedOut = new HashMap<>(); + List<Operation> t1op = new ArrayList<>(); + List<Operation> t2op = new ArrayList<>(); + t1op.add(Operation.SELECT); + t2op.add(Operation.SELECT); + expectedOut.put("TABLE1", t1op); + expectedOut.put("TABLE2", t2op); + assertEquals(expectedOut, QueryProcessor.parseSqlQuery(sqlQuery)); + + sqlQuery = "SELECT name, age FROM table1, table2 t2 WHERE id = t2.id"; + assertEquals(expectedOut, QueryProcessor.parseSqlQuery(sqlQuery)); + } + + @Test + public void insertQuery() throws SqlParseException { + String sqlQuery = "INSERT INTO Employees (id, name) values ('1','Vikram')"; + HashMap<String, List<Operation>> expectedOut = new HashMap<>(); + List<Operation> t1op = new ArrayList<>(); + t1op.add(Operation.INSERT); + expectedOut.put("EMPLOYEES", t1op); + assertEquals(expectedOut, QueryProcessor.parseSqlQuery(sqlQuery)); + } + + @Test + public void updateQuery() throws SqlParseException { + String sqlQuery = "UPDATE Employees SET id = 1 WHERE id = 2"; + HashMap<String, List<Operation>> expectedOut = new HashMap<>(); + List<Operation> t1op = new ArrayList<>(); + t1op.add(Operation.UPDATE); + expectedOut.put("EMPLOYEES", t1op); + assertEquals(expectedOut, QueryProcessor.parseSqlQuery(sqlQuery)); + + sqlQuery = "UPDATE Employees SET id = 1"; + assertEquals(expectedOut, QueryProcessor.parseSqlQuery(sqlQuery)); + } + + @Test + public void insertSelect() throws SqlParseException { + String sqlQuery = + "INSERT INTO table1 (CustomerName, City, Country) SELECT SupplierName, City, Country FROM table2"; + HashMap<String, List<Operation>> expectedOut = new HashMap<>(); + List<Operation> t1op = new ArrayList<>(); + List<Operation> t2op = new ArrayList<>(); + t1op.add(Operation.INSERT); + t2op.add(Operation.SELECT); + expectedOut.put("TABLE1", t1op); + expectedOut.put("TABLE2", t2op); + assertEquals(expectedOut, QueryProcessor.parseSqlQuery(sqlQuery)); + } + +} @@ -243,7 +243,7 @@ <dependency> <groupId>org.onap.music</groupId> <artifactId>dev-MUSIC-cassandra</artifactId> - <version>3.2.1-SNAPSHOT</version> + <version>3.2.1</version> <exclusions> <exclusion> <groupId>io.netty</groupId> |