From: SVN by r. <sv...@ca...> - 2009-02-28 13:29:20
|
Author: roy Date: 2009-02-28 14:29:07 +0100 (Sat, 28 Feb 2009) New Revision: 383 Added: src/test/java/nl/improved/sqlclient/util/ src/test/java/nl/improved/sqlclient/util/SQLParserTest.java Modified: src/main/java/nl/improved/sqlclient/util/SQLParser.java src/test/java/nl/improved/sqlclient/ Log: added tests fixed lowercase group by tab completion Modified: src/main/java/nl/improved/sqlclient/util/SQLParser.java =================================================================== --- src/main/java/nl/improved/sqlclient/util/SQLParser.java 2009-02-27 21:15:25 UTC (rev 382) +++ src/main/java/nl/improved/sqlclient/util/SQLParser.java 2009-02-28 13:29:07 UTC (rev 383) @@ -189,7 +189,7 @@ String WHERE = "W|WH|WHE|WHER|WHERE"; String GROUP_BY = "G|GR|GRO|GROU|GROUP|GROUP |GROUP B|GROUP BY"; String ORDER_BY = "O|OR|ORD|ORDE|ORDER|ORDER |ORDER B|ORDER BY"; - if (subPart.trim().length() > 0 && (end.endsWith(" ") || subPart.matches(".*"+TABLE+"[\\s]+("+WHERE+"|"+GROUP_BY+"|"+ORDER_BY+")")) && + if (subPart.trim().length() > 0 && (end.endsWith(" ") || subPart.toUpperCase().matches(".*"+TABLE+"[\\s]+("+WHERE+"|"+GROUP_BY+"|"+ORDER_BY+")")) && !subPart.trim().endsWith(",")) { if (subPart.lastIndexOf(" ") >0) { end = end.substring(end.lastIndexOf(' ')+1); Property changes on: src/test/java/nl/improved/sqlclient ___________________________________________________________________ Name: svn:ignore + .SQLUtilTest.java.swp Added: src/test/java/nl/improved/sqlclient/util/SQLParserTest.java =================================================================== --- src/test/java/nl/improved/sqlclient/util/SQLParserTest.java 2009-02-27 21:15:25 UTC (rev 382) +++ src/test/java/nl/improved/sqlclient/util/SQLParserTest.java 2009-02-28 13:29:07 UTC (rev 383) @@ -0,0 +1,645 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package nl.improved.sqlclient.util; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import junit.framework.TestCase; +import nl.improved.sqlclient.TabCompletionInfo; +import nl.improved.sqlclient.Point; + +/** + * + * @author roy + */ +public class SQLParserTest extends TestCase { + + public SQLParserTest(String testName) { + super(testName); + } + + + /** + * Test of parse method, of class SQLParser. + */ + public void testParseSimple() { + CharSequence sql = "SELECT * FROM TESTTABLE"; + List<SQLPart> expResult = Arrays.asList(new SQLPart[]{ + new SQLPart(SQLPart.SQLType.SELECT, "SELECT * "), + new SQLPart(SQLPart.SQLType.FROM, "FROM TESTTABLE") + }); + List<SQLPart> result = SQLParser.parse(sql); + assertEquals(expResult.size(), result.size()); + assertEquals(expResult, result); + } + + public void testParseWhere() { + CharSequence sql = "SELECT * FROM TESTTABLE WHERE A=B"; + List<SQLPart> expResult = 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") + }); + List<SQLPart> result = SQLParser.parse(sql); + assertEquals(expResult.size(), result.size()); + assertEquals(expResult, result); + } + + public void testParseGroupBy() { + CharSequence sql = "SELECT * FROM TESTTABLE WHERE A=B GROUP BY test.a"; + List<SQLPart> expResult = 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.GROUPBY, "GROUP BY test.a") + }); + List<SQLPart> result = SQLParser.parse(sql); + assertEquals(expResult.size(), result.size()); + assertEquals(expResult, result); + } + + 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 ") + })); + List<SQLPart> result = SQLParser.parse(sql); + assertEquals(expResult.size(), result.size()); + assertEquals(expResult, result); + + 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, result); + + sql = "SELECT * FROM TESTTABLE WHERE A=B GROUP BY test.a ORDER BY a"; + 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, "GROUP BY test.a "), + new SQLPart(SQLPart.SQLType.ORDERBY, "ORDER BY a"), + })); + result = SQLParser.parse(sql); + assertEquals(expResult.size(), result.size()); + assertEquals(expResult, result); + } + + public void testTabCompletionInfoBLANK() { + List<String> sqlCommand = Arrays.asList(new String[]{""}); + Point cursorPos = new Point(0,0); + TabCompletionInfo info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.SQL_KEYWORD, info.getMatchType()); + List<String> matches = info.getPossibleMatches(); + assertTrue(matches.contains("SELECT")); + assertTrue(matches.contains("INSERT INTO")); + assertTrue(matches.contains("DELETE FROM")); + assertTrue(matches.contains("UPDATE")); + } + + public void testTabCompletionInfoSELECT() { + List<String> sqlCommand = Arrays.asList(new String[]{"SELECT A"}); + Point cursorPos = new Point(sqlCommand.get(0).length(),0); + TabCompletionInfo info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.TABLE_NAMES, info.getMatchType()); + List<String> matches = info.getPossibleMatches(); + assertEquals(0, matches.size()); + assertEquals("A", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT "}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.TABLE_NAMES, info.getMatchType()); + matches = info.getPossibleMatches(); + assertEquals(0, matches.size()); + + sqlCommand = Arrays.asList(new String[]{"SELECT * "}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.SQL_KEYWORD, info.getMatchType()); + matches = info.getPossibleMatches(); + assertEquals(1, matches.size()); + assertTrue(matches.contains("FROM")); + + sqlCommand = Arrays.asList(new String[]{"SELECT * FR"}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.SQL_KEYWORD, info.getMatchType()); + matches = info.getPossibleMatches(); + assertEquals(1, matches.size()); + assertTrue(matches.contains("FROM")); + assertEquals("FR", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT a.test FR"}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.SQL_KEYWORD, info.getMatchType()); + matches = info.getPossibleMatches(); + assertEquals(1, matches.size()); + assertTrue(matches.contains("FROM")); + assertEquals("FR", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT a.test, a.tost FR"}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.SQL_KEYWORD, info.getMatchType()); + matches = info.getPossibleMatches(); + assertEquals(1, matches.size()); + assertTrue(matches.contains("FROM")); + assertEquals("FR", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT A."}); + 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(); + assertEquals(1, matches.size()); + assertEquals("A", matches.get(0)); + assertEquals("", info.getStart()); + } + + public void testTabCompletionInfoFROM() { + List<String> sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A"}); + Point cursorPos = new Point(sqlCommand.get(0).length(),0); + TabCompletionInfo info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.TABLE_NAMES, info.getMatchType()); + List<String> matches = info.getPossibleMatches(); + assertEquals(0, matches.size()); + assertEquals("A", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"select * from A"}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.TABLE_NAMES, info.getMatchType()); + matches = info.getPossibleMatches(); + assertEquals(0, matches.size()); + assertEquals("A", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A, B"}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.TABLE_NAMES, info.getMatchType()); + matches = info.getPossibleMatches(); + assertEquals(0, matches.size()); + assertEquals("B", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B"}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.TABLE_NAMES, info.getMatchType()); + matches = info.getPossibleMatches(); + assertEquals(0, matches.size()); + assertEquals("B", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B,C"}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.TABLE_NAMES, info.getMatchType()); + matches = info.getPossibleMatches(); + assertEquals(0, matches.size()); + assertEquals("C", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"select * from testdata, TESTDATA, TESTDATA, t"}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.TABLE_NAMES, info.getMatchType()); + matches = info.getPossibleMatches(); + assertEquals(0, matches.size()); + assertEquals("t", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A WHERE 1=1"}); + cursorPos = new Point("SELECT * FROM A".length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.TABLE_NAMES, info.getMatchType()); + matches = info.getPossibleMatches(); + assertEquals(0, matches.size()); + assertEquals("A", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT A.b, A.c FROM A"}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.TABLE_NAMES, info.getMatchType()); + matches = info.getPossibleMatches(); + assertEquals(0, matches.size()); + assertEquals("A", info.getStart()); + } + + public void testTabCompletionInfoWHERE() { + List<String> sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B "}); + Point cursorPos = new Point(sqlCommand.get(0).length(),0); + TabCompletionInfo info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.SQL_KEYWORD, info.getMatchType()); + List<String> matches = info.getPossibleMatches(); + //assertEquals(3, matches.size()); + assertTrue("At least 3 keyword matches required", matches.size() >=3); + assertTrue(matches.contains("WHERE")); + assertTrue(matches.contains("GROUP BY")); + assertTrue(matches.contains("ORDER BY")); + + sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A "}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.SQL_KEYWORD, info.getMatchType()); + matches = info.getPossibleMatches(); + assertTrue("At least 3 keyword matches required", matches.size() >=3); + assertTrue(matches.contains("WHERE")); + assertTrue(matches.contains("GROUP BY")); + assertEquals("", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A GR"}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.SQL_KEYWORD, info.getMatchType()); + matches = info.getPossibleMatches(); + assertTrue("At least 3 keyword matches required", matches.size() >=3); + assertTrue(matches.contains("WHERE")); + assertTrue(matches.contains("GROUP BY")); + assertEquals("GR", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A gr"}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.SQL_KEYWORD, info.getMatchType()); + matches = info.getPossibleMatches(); + assertTrue("At least 3 keyword matches required", matches.size() >=3); + assertTrue(matches.contains("WHERE")); + assertTrue(matches.contains("GROUP BY")); + assertEquals("gr", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A, "}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.TABLE_NAMES, info.getMatchType()); + assertEquals("", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B "}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.SQL_KEYWORD, info.getMatchType()); + matches = info.getPossibleMatches(); + assertTrue("At least 3 keyword matches required", matches.size() >=3); + assertTrue(matches.contains("WHERE")); + assertEquals("", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B WH"}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.SQL_KEYWORD, info.getMatchType()); + matches = info.getPossibleMatches(); + assertTrue("At least 3 keyword matches required", matches.size() >=3); + assertTrue(matches.contains("WHERE")); + assertEquals("WH", info.getStart()); + } + + public void testTabCompletionInfoWHEREConditions() { + List<String> sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B WHERE "}); + Point cursorPos = new Point(sqlCommand.get(0).length(),0); + TabCompletionInfo info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.COLUMN_NAMES, info.getMatchType()); + List<String> matches = info.getPossibleMatches(); + assertEquals(2, matches.size()); + assertTrue(matches.contains("A")); + assertTrue(matches.contains("B")); + + sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B WHERE I"}); + 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(); + assertEquals(2, matches.size()); + assertTrue(matches.contains("A")); + assertTrue(matches.contains("B")); + assertEquals("I", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B WHERE I=bla AND I"}); + 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(); + assertEquals(2, matches.size()); + assertTrue(matches.contains("A")); + assertTrue(matches.contains("B")); + assertEquals("I", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B WHERE A.I"}); + 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(); + //assertEquals(1, matches.size()); + assertTrue(matches.contains("A")); + //System.out.println("I: " + info.getStart()); + assertEquals("I", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B WHERE A."}); + 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(); + assertEquals(1, matches.size()); + assertTrue(matches.contains("A")); + //System.out.println("I:'" + info.getStart() +"'"); + assertEquals("", info.getStart()); + + // with other conditions + sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B WHERE A.x = 'x' AND "}); + 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(); + assertEquals(2, matches.size()); + assertTrue(matches.contains("A")); + assertTrue(matches.contains("B")); + + // same but with different casings + sqlCommand = Arrays.asList(new String[]{"select * From A,B whEre A.x = 'x' aNd "}); + 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(); + assertEquals(2, matches.size()); + assertTrue(matches.contains("A")); + assertTrue(matches.contains("B")); + + // with other conditions + sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B WHERE A.x = a"}); + 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")); + assertTrue(matches.contains("B")); + assertEquals("a", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B WHERE A.x = "}); + 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")); + assertTrue(matches.contains("B")); + assertEquals("", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B WHERE A.x = 'x' "}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.SQL_KEYWORD, info.getMatchType()); + matches = info.getPossibleMatches(); + assertTrue(matches.contains("AND")); + assertTrue(matches.contains("OR")); + assertTrue(matches.contains("GROUP BY")); + assertTrue(matches.contains("ORDER BY")); + assertEquals("", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"select makelaarnr from makelaar, tree, plugin where parent = makelaar.i_id and child = pl"}); + 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(); + assertEquals("pl", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"select makelaarnr from makelaar, tree, plugin where parent = makelaar.i_id and child=pl"}); + 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(); + assertEquals("pl", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B WHERE A.x = 'x' A"}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.SQL_KEYWORD, info.getMatchType()); + matches = info.getPossibleMatches(); + assertTrue(matches.contains("AND")); + assertEquals("A", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B WHERE A.x = 'x' a"}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.SQL_KEYWORD, info.getMatchType()); + matches = info.getPossibleMatches(); + assertTrue(matches.contains("AND")); + assertEquals("a", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B WHERE c in('c','d','e') and A.x"}); + 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.toString(), matches.contains("A")); + assertEquals("x", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B WHERE c like 'a%' and A.x"}); + 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("x", info.getStart()); + } + + public void testGroupBy() { + List<String> sqlCommand = Arrays.asList(new String[]{"SELECT c1,c2, count(*) FROM A,B WHERE a.b=b.b GROUP BY "}); + Point cursorPos = new Point(sqlCommand.get(0).length(),0); + TabCompletionInfo info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.OTHER, info.getMatchType()); + List<String> matches = info.getPossibleMatches(); + assertEquals(4, matches.size()); + assertTrue(matches.contains("c1")); + assertTrue(matches.contains("c2")); + assertTrue(matches.contains("count(*)")); + assertTrue(matches.contains("c1,c2")); + assertEquals("", info.getStart()); + + + sqlCommand = Arrays.asList(new String[]{"SELECT c1 , c2 FROM A,B WHERE a.b=b.b GROUP BY "}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.OTHER, info.getMatchType()); + matches = info.getPossibleMatches(); + assertEquals(3, matches.size()); + assertTrue(matches.contains("c1")); + assertTrue(matches.contains("c2")); + + sqlCommand = Arrays.asList(new String[]{"SELECT c1 , c2 FROM A,B WHERE a.b=b.b GROUP BY c"}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.OTHER, info.getMatchType()); + matches = info.getPossibleMatches(); + assertEquals(3, matches.size()); + assertTrue(matches.contains("c1")); + assertTrue(matches.contains("c2")); + + sqlCommand = Arrays.asList(new String[]{"SELECT c1 , c2 FROM A,B WHERE a.b=b.b GROUP BY c1, "}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.OTHER, info.getMatchType()); + matches = info.getPossibleMatches(); + assertEquals(3, matches.size()); + assertTrue(matches.contains("c2")); + + sqlCommand = Arrays.asList(new String[]{"SELECT c1 , c2 FROM A,B GR"}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.SQL_KEYWORD, info.getMatchType()); + matches = info.getPossibleMatches(); + assertTrue(matches.contains("GROUP BY")); + assertEquals("GR", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT c1 , c2 FROM A,B", "GR"}); + cursorPos = new Point(sqlCommand.get(1).length(),1); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.SQL_KEYWORD, info.getMatchType()); + matches = info.getPossibleMatches(); + assertTrue(matches.contains("GROUP BY")); + assertEquals("GR", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"SELECT c1 , c2 FROM A,B", "GROUP BY "}); + cursorPos = new Point(sqlCommand.get(1).length(),1); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.OTHER, info.getMatchType()); + matches = info.getPossibleMatches(); + assertTrue(matches.contains("c1")); + assertEquals("", info.getStart()); + } + + public void testOrderBy() { + List<String> sqlCommand = Arrays.asList(new String[]{"SELECT c1,c2 FROM A,B WHERE a.b=b.b ORDER BY "}); + Point cursorPos = new Point(sqlCommand.get(0).length(),0); + TabCompletionInfo info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.OTHER, info.getMatchType()); + List<String> matches = info.getPossibleMatches(); + assertEquals(2, matches.size()); + assertTrue(matches.contains("c1")); + assertTrue(matches.contains("c2")); + + + sqlCommand = Arrays.asList(new String[]{"SELECT c1 , c2 FROM A,B WHERE a.b=b.b ORDER BY "}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.OTHER, info.getMatchType()); + matches = info.getPossibleMatches(); + assertEquals(2, matches.size()); + assertTrue(matches.contains("c1")); + assertTrue(matches.contains("c2")); + } + + public void testUpdateTabCompletion() { + List<String> sqlCommand = Arrays.asList(new String[]{"UPDATE A"}); + Point cursorPos = new Point(sqlCommand.get(0).length(),0); + TabCompletionInfo info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(info.getPossibleMatches().toString(), TabCompletionInfo.MatchType.TABLE_NAMES, info.getMatchType()); + List<String> matches = info.getPossibleMatches(); + assertEquals(0, matches.size()); + assertEquals("A", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"UPDATE A "}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.SQL_KEYWORD, info.getMatchType()); + matches = info.getPossibleMatches(); + assertEquals(1, matches.size()); + assertEquals("SET", matches.get(0)); + assertEquals("", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"UPDATE A SET "}); + 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(); + assertEquals(1, matches.size()); + assertEquals("A", matches.get(0)); + assertEquals("", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"UPDATE A SET B=C "}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.SQL_KEYWORD, info.getMatchType()); + matches = info.getPossibleMatches(); + assertTrue("WHERE", matches.contains("WHERE")); + assertEquals("", info.getStart()); + + sqlCommand = Arrays.asList(new String[]{"UPDATE A SET B=C, ID "}); + cursorPos = new Point(sqlCommand.get(0).length(),0); + info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.SQL_KEYWORD, info.getMatchType()); + matches = info.getPossibleMatches(); + assertTrue("=", matches.contains("=")); + assertEquals("", info.getStart()); + } + + public void testGetTableName() { + List<String> sqlCommand = Arrays.asList(new String[]{"select * from a where mytable.b"}); + Point cursorPos = new Point(sqlCommand.get(0).length(),0); + TabCompletionInfo info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos); + assertNotNull(info); + assertEquals(TabCompletionInfo.MatchType.COLUMN_NAMES, info.getMatchType()); + List<String> matches = info.getPossibleMatches(); + assertEquals(1, matches.size()); + assertEquals("b", info.getStart()); + assertTrue("Table name should match 'mytable', but was " + matches.get(0), matches.contains("mytable")); + } + +} |