|
From: SVN by r. <sv...@ca...> - 2009-03-14 15:23:19
|
Author: roy
Date: 2009-03-14 16:23:00 +0100 (Sat, 14 Mar 2009)
New Revision: 394
Modified:
src/main/java/nl/improved/sqlclient/util/SQLParser.java
src/test/java/nl/improved/sqlclient/util/SQLParserTest.java
Log:
more sqlparsing fixes
Modified: src/main/java/nl/improved/sqlclient/util/SQLParser.java
===================================================================
--- src/main/java/nl/improved/sqlclient/util/SQLParser.java 2009-03-04 11:12:53 UTC (rev 393)
+++ src/main/java/nl/improved/sqlclient/util/SQLParser.java 2009-03-14 15:23:00 UTC (rev 394)
@@ -60,7 +60,9 @@
}
if (buf.length() > 0) {
buf.append(' ');
- position++;
+ if (y <= commandPoint.y) {
+ position++;
+ }
}
buf.append(seq);
}
@@ -70,7 +72,6 @@
public static TabCompletionInfo getTabCompletionInfo(CharSequence sql, int position) {
List<SQLPart> parts = parse(sql);
- System.err.println("PARTS: "+ parts);
int charsLeft = position;
for (SQLPart part : parts) {
if (charsLeft <= part.getPart().length()) {
@@ -176,6 +177,10 @@
private static TabCompletionInfo getFromTabCompletionInfo(SQLPart part, List<SQLPart> parts, int charsLeft) {
String stringPart = part.getPart().toString();
+ if (stringPart.equalsIgnoreCase("FROM")) {
+ return new TabCompletionInfo(TabCompletionInfo.MatchType.OTHER
+ , Arrays.asList(new String[]{" "}));
+ }
String subPart = stringPart.substring(stringPart.toUpperCase().indexOf("FROM") +"FROM".length(), charsLeft);
String end;
if (subPart.indexOf(',') > 0) {
@@ -188,9 +193,11 @@
String ORDER_BY = "O|OR|ORD|ORDE|ORDER|ORDER |ORDER B|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) {
+ if (end.lastIndexOf(" ") >0) {
end = end.substring(end.lastIndexOf(' ')+1);
}
+ System.err.println("SUB: "+"'"+ subPart+"'");
+ System.err.println("END: "+ "'"+end+"'");
return new TabCompletionInfo(TabCompletionInfo.MatchType.SQL_KEYWORD, Arrays.asList(new String[]{"WHERE", "GROUP BY", "ORDER BY", ","}), end.trim());
}
return new TabCompletionInfo(TabCompletionInfo.MatchType.TABLE_NAMES, Arrays.asList(new String[]{}), end.trim());
@@ -255,22 +262,13 @@
private static TabCompletionInfo getOrderByTabCompletionInfo(SQLPart part, List<SQLPart> parts, int charsLeft) {
String stringPart = part.getPart().toString();
String subPart = stringPart.substring(stringPart.toUpperCase().indexOf("ORDER BY")+"ORDER BY".length(), charsLeft);
- List<String> possibleMatches = new ArrayList<String>();
- for (SQLPart pPart : parts) {
- if (pPart.getSQLType() == SQLPart.SQLType.SELECT) {
- possibleMatches.addAll(pPart.getColumnNames());
- }
- }
- if (subPart.trim().length() == 0 || subPart.trim().endsWith(",")) {
- return new TabCompletionInfo(TabCompletionInfo.MatchType.OTHER, possibleMatches);
- }
String end;
if (subPart.indexOf(',') > 0) {
end = subPart.substring(subPart.lastIndexOf(',')).trim();
} else {
end = subPart.trim();
}
- return new TabCompletionInfo(TabCompletionInfo.MatchType.OTHER, possibleMatches, end);
+ return new TabCompletionInfo(TabCompletionInfo.MatchType.COLUMN_NAMES, getTableNames(parts), end);
}
private static TabCompletionInfo getSelectTabCompletionInfo(SQLPart part, List<SQLPart> parts, int position) {
@@ -279,6 +277,10 @@
// return "FROM"
String startOfCommand = part.getPart().toString();
String upCommand = startOfCommand.toUpperCase();
+ if (upCommand.equals("SELECT")) {
+ return new TabCompletionInfo(TabCompletionInfo.MatchType.OTHER
+ , Arrays.asList(new String[]{" "}));
+ }
if (upCommand.substring(upCommand.indexOf("SELECT") + "SELECT".length())
.matches("[\\s]+"+VAR+"(|[\\s]*,[\\s]*"+VAR+"[\\s]*)[\\s]+(|F|FR|FRO|FROM)")) {
String end = startOfCommand.substring(startOfCommand.lastIndexOf(' ')+1);
@@ -392,6 +394,10 @@
// or
// 'SELECT x FROM A,B WHERE A.x='x' AND/OR '
String startOfCommand = part.getPart().toString().substring(0, charsLeft);
+ if (startOfCommand.equalsIgnoreCase("WHERE")) {
+ return new TabCompletionInfo(TabCompletionInfo.MatchType.OTHER
+ , Arrays.asList(new String[]{" "}));
+ }
String tmpCommand = startOfCommand.substring(startOfCommand.toUpperCase().indexOf("WHERE")+"WHERE".length()+1);
String upperCommandString = tmpCommand.toUpperCase();
//String VAR = "(|"+TABLE+"\\.)(|"+COLUMN+")";
@@ -400,7 +406,6 @@
// VALUE IN (.*)
String regExp = "(|"+VALUE+"(|[\\s]*"+COMPARATOR+"[\\s]*(|"+VALUE+"(|([\\s]+(AND|OR)[\\s]+(|"+VALUE+"(|[\\s]*"+COMPARATOR+"[\\s]*(|"+VALUE+"))))*))|[\\s]*IN[\\s]*(.*)[\\s]*))";
if (upperCommandString.matches(regExp) || upperCommandString.endsWith(".")) {
- System.err.println("****** MATCHES: "+ upperCommandString);
int lastIndex = tmpCommand.lastIndexOf(' ');
lastIndex = Math.max(lastIndex, tmpCommand.lastIndexOf('\t'));
lastIndex = Math.max(lastIndex, tmpCommand.lastIndexOf('='));
@@ -418,12 +423,15 @@
return new TabCompletionInfo(TabCompletionInfo.MatchType.COLUMN_NAMES
, tableNames, end);
}
+ if (upperCommandString.matches(".*[\\s]+(AND|OR)[\\s]+")) {
+ return new TabCompletionInfo(TabCompletionInfo.MatchType.COLUMN_NAMES
+ , getTableNames(parts));
+ }
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;
if (upperCommandString.matches(".*[\\s]+")) {
Modified: src/test/java/nl/improved/sqlclient/util/SQLParserTest.java
===================================================================
--- src/test/java/nl/improved/sqlclient/util/SQLParserTest.java 2009-03-04 11:12:53 UTC (rev 393)
+++ src/test/java/nl/improved/sqlclient/util/SQLParserTest.java 2009-03-14 15:23:00 UTC (rev 394)
@@ -142,6 +142,15 @@
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.OTHER, info.getMatchType());
+ matches = info.getPossibleMatches();
+ assertEquals(1, matches.size());
+ assertEquals(" ", matches.get(0));
+
sqlCommand = Arrays.asList(new String[]{"SELECT * "});
cursorPos = new Point(sqlCommand.get(0).length(),0);
info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos);
@@ -190,6 +199,24 @@
assertEquals(1, matches.size());
assertEquals("A", matches.get(0));
assertEquals("", 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.TABLE_NAMES, info.getMatchType());
+ matches = info.getPossibleMatches();
+ assertEquals(0, matches.size());
+ assertEquals("A", info.getStart());
+
+ sqlCommand = Arrays.asList(new String[]{"SELECT 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());
}
public void testTabCompletionInfoFROM() {
@@ -264,6 +291,34 @@
matches = info.getPossibleMatches();
assertEquals(0, matches.size());
assertEquals("A", info.getStart());
+
+ sqlCommand = Arrays.asList(new String[]{"SELECT A.b, A.c FROM "});
+ 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("", info.getStart());
+
+ sqlCommand = Arrays.asList(new String[]{"SELECT A.b, A.c FROM"});
+ 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(1, matches.size());
+ assertEquals(" ", matches.get(0));
+ assertEquals("", info.getStart());
+
+ sqlCommand = Arrays.asList(new String[]{"select id, type from t, p", "where a=b"});
+ cursorPos = new Point(sqlCommand.get(0).length(),0);
+ info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos);
+ assertNotNull(info);
+ assertEquals(info.getPossibleMatches().toString(), TabCompletionInfo.MatchType.TABLE_NAMES, info.getMatchType());
+ matches = info.getPossibleMatches();
+ assertEquals(0, matches.size());
+ assertEquals("p", info.getStart());
}
public void testTabCompletionInfoWHERE() {
@@ -351,6 +406,28 @@
assertTrue(matches.contains("A"));
assertTrue(matches.contains("B"));
+ sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B WHERE "});
+ 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("", info.getStart());
+
+ sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B WHERE"});
+ 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(1, matches.size());
+ assertTrue(matches.contains(" "));
+ assertEquals("", info.getStart());
+
+
sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B WHERE I"});
cursorPos = new Point(sqlCommand.get(0).length(),0);
info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos);
@@ -510,6 +587,24 @@
matches = info.getPossibleMatches();
assertTrue(matches.contains("A"));
assertEquals("", info.getStart());
+
+ sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B WHERE d is null "});
+ 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("", info.getStart());
+
+ sqlCommand = Arrays.asList(new String[]{"SELECT * FROM A,B WHERE d is null and "});
+ cursorPos = new Point(sqlCommand.get(0).length(),0);
+ info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos);
+ assertNotNull(info);
+ assertEquals(info.getPossibleMatches().toString(), TabCompletionInfo.MatchType.COLUMN_NAMES, info.getMatchType());
+ matches = info.getPossibleMatches();
+ assertTrue(matches.contains("A"));
+ assertEquals("", info.getStart());
}
public void testGroupBy() {
@@ -589,22 +684,24 @@
Point cursorPos = new Point(sqlCommand.get(0).length(),0);
TabCompletionInfo info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos);
assertNotNull(info);
- assertEquals(TabCompletionInfo.MatchType.OTHER, info.getMatchType());
+ assertEquals(TabCompletionInfo.MatchType.COLUMN_NAMES, info.getMatchType());
List<String> matches = info.getPossibleMatches();
assertEquals(2, matches.size());
- assertTrue(matches.contains("c1"));
- assertTrue(matches.contains("c2"));
+ assertTrue(matches.contains("A"));
+ assertTrue(matches.contains("B"));
+ assertEquals("", info.getStart());
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());
+ assertEquals(TabCompletionInfo.MatchType.COLUMN_NAMES, info.getMatchType());
matches = info.getPossibleMatches();
assertEquals(2, matches.size());
- assertTrue(matches.contains("c1"));
- assertTrue(matches.contains("c2"));
+ assertTrue(matches.contains("A"));
+ assertTrue(matches.contains("B"));
+ assertEquals("", info.getStart());
}
public void testUpdateTabCompletion() {
@@ -682,6 +779,15 @@
assertTrue("A", matches.contains("A"));
assertEquals("", info.getStart());
+ sqlCommand = Arrays.asList(new String[]{"UPDATE A SET B = C, D"});
+ 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("A", matches.contains("A"));
+ assertEquals("D", info.getStart());
+
sqlCommand = Arrays.asList(new String[]{"UPDATE A SET B = B WH"});
cursorPos = new Point(sqlCommand.get(0).length(),0);
info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos);
|