From: SVN by r. <sv...@ca...> - 2009-03-02 08:54:14
|
Author: roy Date: 2009-03-02 09:54:07 +0100 (Mon, 02 Mar 2009) New Revision: 391 Modified: src/main/java/nl/improved/sqlclient/util/SQLParser.java src/test/java/nl/improved/sqlclient/util/SQLParserTest.java Log: more tests and fixes Modified: src/main/java/nl/improved/sqlclient/util/SQLParser.java =================================================================== --- src/main/java/nl/improved/sqlclient/util/SQLParser.java 2009-03-02 08:53:55 UTC (rev 390) +++ src/main/java/nl/improved/sqlclient/util/SQLParser.java 2009-03-02 08:54:07 UTC (rev 391) @@ -132,10 +132,11 @@ } else if (sql.matches("[\\s]*WHERE.*")) { sqlType = SQLPart.SQLType.WHERE; if (sql.matches("[\\s]*WHERE.*ORDER BY.*")) { - end = sql.indexOf(" ORDER BY"); - } else if (sql.matches("[\\s]*WHERE.*GROUP BY.*")) { - end = sql.indexOf("GROUP BY"); + end = sql.indexOf("ORDER BY"); } + if (sql.matches("[\\s]*WHERE.*GROUP BY.*")) { + end = Math.min(end, sql.indexOf("GROUP BY")); + } } else if (sql.matches("[\\s]*ORDER BY.*")) { sqlType = SQLPart.SQLType.ORDERBY; if (sql.matches("[\\s]*ORDER BY.*GROUP BY.*")) { @@ -143,27 +144,23 @@ } } else if (sql.matches("[\\s]*GROUP BY.*")) { sqlType = SQLPart.SQLType.GROUPBY; - end = sqlSequence.length(); + if (sql.matches("[\\s]*GROUP BY.*ORDER BY.*")) { + end = sql.indexOf("ORDER BY"); + } } else if (sql.matches("[\\s]*DELETE.*")) { sqlType = SQLPart.SQLType.DELETE; if (sql.matches("[\\s]*DELETE FROM.*")) { end = sql.indexOf("FROM"); - } else { - end = sqlSequence.length(); } } else if (sql.matches("[\\s]*UPDATE.*")) { sqlType = SQLPart.SQLType.UPDATE; if (sql.matches("[\\s]*UPDATE.*SET.*")) { end = sql.indexOf("SET"); - } else { - end = sqlSequence.length(); } } else if (sql.matches("[\\s]*SET.*")) { sqlType = SQLPart.SQLType.SET; if (sql.matches("[\\s]*WHERE.*")) { end = sql.indexOf("WHERE"); - } else { - end = sqlSequence.length(); } } else { sqlType = SQLPart.SQLType.OTHER; @@ -412,6 +409,11 @@ return new TabCompletionInfo(TabCompletionInfo.MatchType.COLUMN_NAMES , tableNames, end); } + if (upperCommandString.matches(".*"+NAMING_CHAR+"\\(.*")) { + String end = tmpCommand.substring(tmpCommand.lastIndexOf('(')+1); + return new TabCompletionInfo(TabCompletionInfo.MatchType.COLUMN_NAMES + , getTableNames(parts), end); + } System.err.println("****** NOT MATCHES: "+ upperCommandString+"\n"+ regExp); //else System.out.println("'"+upperCommandString +"'\n not matches\n"+regExp); String end; Modified: src/test/java/nl/improved/sqlclient/util/SQLParserTest.java =================================================================== --- src/test/java/nl/improved/sqlclient/util/SQLParserTest.java 2009-03-02 08:53:55 UTC (rev 390) +++ src/test/java/nl/improved/sqlclient/util/SQLParserTest.java 2009-03-02 08:54:07 UTC (rev 391) @@ -62,23 +62,34 @@ assertEquals(expResult, result); } - public void _testParseOrderBy() { + public void testParseOrderBy() { CharSequence sql = "SELECT * FROM TESTTABLE WHERE A=B ORDER BY a"; List<SQLPart> expResult = new ArrayList(Arrays.asList(new SQLPart[]{ new SQLPart(SQLPart.SQLType.SELECT, "SELECT * "), new SQLPart(SQLPart.SQLType.FROM, "FROM TESTTABLE "), new SQLPart(SQLPart.SQLType.WHERE, "WHERE A=B "), - new SQLPart(SQLPart.SQLType.ORDERBY, "ORDER BY a ") + new SQLPart(SQLPart.SQLType.ORDERBY, "ORDER BY a") })); List<SQLPart> result = SQLParser.parse(sql); assertEquals(expResult.size(), result.size()); + assertEquals(expResult.get(0), result.get(0)); + assertEquals(expResult.get(1), result.get(1)); + assertEquals(expResult.get(2), result.get(2)); + assertEquals(expResult.get(3), result.get(3)); assertEquals(expResult, result); + expResult.remove(expResult.size()-1); + expResult.add(new SQLPart(SQLPart.SQLType.ORDERBY, "ORDER BY a ")); expResult.add(new SQLPart(SQLPart.SQLType.GROUPBY, "GROUP BY test.a")); sql = "SELECT * FROM TESTTABLE WHERE A=B ORDER BY a GROUP BY test.a"; result = SQLParser.parse(sql); assertEquals(expResult.size(), result.size()); + assertEquals(expResult.get(0), result.get(0)); + assertEquals(expResult.get(1), result.get(1)); + assertEquals(expResult.get(2), result.get(2)); + assertEquals(expResult.get(3), result.get(3)); + assertEquals(expResult.get(4), result.get(4)); assertEquals(expResult, result); sql = "SELECT * FROM TESTTABLE WHERE A=B GROUP BY test.a ORDER BY a"; @@ -86,11 +97,17 @@ new SQLPart(SQLPart.SQLType.SELECT, "SELECT * "), new SQLPart(SQLPart.SQLType.FROM, "FROM TESTTABLE "), new SQLPart(SQLPart.SQLType.WHERE, "WHERE A=B "), - new SQLPart(SQLPart.SQLType.ORDERBY, "GROUP BY test.a "), + new SQLPart(SQLPart.SQLType.GROUPBY, "GROUP BY test.a "), new SQLPart(SQLPart.SQLType.ORDERBY, "ORDER BY a"), })); result = SQLParser.parse(sql); + assertEquals(result.toString(), expResult.size(), result.size()); assertEquals(expResult.size(), result.size()); + assertEquals(expResult.get(0), result.get(0)); + assertEquals(expResult.get(1), result.get(1)); + assertEquals(expResult.get(2), result.get(2)); + assertEquals(expResult.get(3), result.get(3)); + assertEquals(expResult.get(4), result.get(4)); assertEquals(expResult, result); } @@ -484,6 +501,15 @@ matches = info.getPossibleMatches(); assertTrue(matches.contains("A")); assertEquals("x", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B WHERE to_char("}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.COLUMN_NAMES, info.getMatchType()); + matches = info.getPossibleMatches(); + assertTrue(matches.contains("A")); + assertEquals("", info.getStart()); } public void testGroupBy() { |