|
[Sqlshell-svn] /devel/src: main/java/nl/improved/sqlclient/util
test/java/nl/improved/sqlclient/util
From: SVN by r. <sv...@ca...> - 2009-03-01 14:23:02
|
Author: roy
Date: 2009-03-01 15:22:52 +0100 (Sun, 01 Mar 2009)
New Revision: 387
Modified:
src/main/java/nl/improved/sqlclient/util/SQLParser.java
src/main/java/nl/improved/sqlclient/util/SQLPart.java
src/test/java/nl/improved/sqlclient/util/SQLParserTest.java
Log:
added more test
fix where tab completion when using table alias names
Modified: src/main/java/nl/improved/sqlclient/util/SQLParser.java
===================================================================
--- src/main/java/nl/improved/sqlclient/util/SQLParser.java 2009-02-28 13:42:08 UTC (rev 386)
+++ src/main/java/nl/improved/sqlclient/util/SQLParser.java 2009-03-01 14:22:52 UTC (rev 387)
@@ -350,6 +350,14 @@
, Arrays.asList(new String[]{",", "WHERE"})); // TODO END
}
+ private static String getTableName(String alias, List<SQLPart> parts) {
+ for (SQLPart tmpPart : parts) {
+ if (tmpPart.getSQLType() == SQLPart.SQLType.FROM || tmpPart.getSQLType() == SQLPart.SQLType.UPDATE) {
+ return tmpPart.getTableName(alias);
+ }
+ }
+ return alias;
+ }
private static List<String> getTableNames(List<SQLPart> parts) {
SQLPart fromSqlPart = null;
for (SQLPart tmpPart : parts) {
@@ -399,7 +407,7 @@
lastBreakIndex = 0;
}
List tableNames = (lastIndex > lastBreakIndex) && lastBreakIndex >=0 ?
- Arrays.asList(new String[]{tmpCommand.substring(lastBreakIndex,lastIndex).trim()}) :
+ Arrays.asList(new String[]{getTableName(tmpCommand.substring(lastBreakIndex,lastIndex).trim(), parts)}) :
getTableNames(parts);
return new TabCompletionInfo(TabCompletionInfo.MatchType.COLUMN_NAMES
, tableNames, end);
Modified: src/main/java/nl/improved/sqlclient/util/SQLPart.java
===================================================================
--- src/main/java/nl/improved/sqlclient/util/SQLPart.java 2009-02-28 13:42:08 UTC (rev 386)
+++ src/main/java/nl/improved/sqlclient/util/SQLPart.java 2009-03-01 14:22:52 UTC (rev 387)
@@ -74,6 +74,37 @@
}
throw new IllegalStateException("Table names are only available for SELECT ("+ sqlType+")");
}
+ public String getTableName(String alias) {
+ if (sqlType == SQLType.FROM || sqlType == SQLType.UPDATE) {
+ String stringPart = getPart().toString();
+ String sub;
+ if (sqlType == SQLType.FROM) {
+ sub = stringPart.substring(stringPart.toUpperCase().indexOf("FROM")+"FROM".length());
+ } else {
+ sub = stringPart.substring(stringPart.toUpperCase().indexOf("UPDATE")+"UPDATE".length());
+ }
+ int lastIndex = 0;
+ int newIndex;
+ while ( (newIndex = sub.indexOf(',', lastIndex)) > 0) {
+ String tableName = sub.substring(lastIndex, newIndex).trim();
+ if (tableName.indexOf(" ") > 0 && tableName.endsWith(" "+alias)) {
+ tableName = tableName.substring(0, tableName.indexOf(" ")).trim();
+ return tableName;
+ }
+ lastIndex = newIndex+1;
+ }
+ String tmp = sub.substring(lastIndex).trim();
+ if (tmp.length() > 0) {
+ if (tmp.indexOf(" ") > 0 && tmp.endsWith(" "+alias)) {
+ tmp = tmp.substring(0, tmp.indexOf(" ")).trim();
+ return tmp;
+ }
+ }
+ return alias;
+ }
+ throw new IllegalStateException("Table names are only available for FROM,UPDATE ("+ sqlType+")");
+ }
+
public List<String> getTableNames() {
if (sqlType == SQLType.FROM || sqlType == SQLType.UPDATE) {
List<String> returnValue = new ArrayList<String>();
@@ -106,4 +137,4 @@
throw new IllegalStateException("Table names are only available for FROM,UPDATE ("+ sqlType+")");
}
-}
\ No newline at end of file
+}
Modified: src/test/java/nl/improved/sqlclient/util/SQLParserTest.java
===================================================================
--- src/test/java/nl/improved/sqlclient/util/SQLParserTest.java 2009-02-28 13:42:08 UTC (rev 386)
+++ src/test/java/nl/improved/sqlclient/util/SQLParserTest.java 2009-03-01 14:22:52 UTC (rev 387)
@@ -658,6 +658,36 @@
assertEquals(1, matches.size());
assertEquals("b", info.getStart());
assertTrue("Table name should match 'mytable', but was " + matches.get(0), matches.contains("mytable"));
+
+ sqlCommand = Arrays.asList(new String[]{"select * from a where b"});
+ 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("b", info.getStart());
+ assertTrue("Table name should match 'a', but was " + matches.get(0), matches.contains("a"));
+
+ sqlCommand = Arrays.asList(new String[]{"select * from testtable a where b"});
+ 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("b", info.getStart());
+ assertTrue("Table name should match 'testtable', but was " + matches.get(0), matches.contains("testtable"));
+
+ sqlCommand = Arrays.asList(new String[]{"select * from testtable a where a.b"});
+ 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("b", info.getStart());
+ assertTrue("Table name should match 'testtable', but was " + matches.get(0), matches.contains("testtable"));
}
}
|
|
[Sqlshell-svn] /devel/src: main/java/nl/improved/sqlclient/util
test/java/nl/improved/sqlclient/util
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() {
|
|
[Sqlshell-svn] /devel/src: main/java/nl/improved/sqlclient/util
test/java/nl/improved/sqlclient/util
From: SVN by r. <sv...@ca...> - 2009-03-04 08:09:00
|
Author: roy
Date: 2009-03-04 09:08:46 +0100 (Wed, 04 Mar 2009)
New Revision: 392
Modified:
src/main/java/nl/improved/sqlclient/util/SQLParser.java
src/test/java/nl/improved/sqlclient/util/SQLParserTest.java
Log:
some tab compl fixes
Modified: src/main/java/nl/improved/sqlclient/util/SQLParser.java
===================================================================
--- src/main/java/nl/improved/sqlclient/util/SQLParser.java 2009-03-02 08:54:07 UTC (rev 391)
+++ src/main/java/nl/improved/sqlclient/util/SQLParser.java 2009-03-04 08:08:46 UTC (rev 392)
@@ -336,15 +336,24 @@
String end;
if (tmpCommand.indexOf(' ') > 0) {
end = tmpCommand.substring(tmpCommand.lastIndexOf(' ')+1).trim();
+ if (tmpCommand.matches(".*"+COMPARATOR+"[\\s]*"+VAR+"[\\s]+(|W|WH|WHE|WHER|WHERE)")) {
+ return new TabCompletionInfo(TabCompletionInfo.MatchType.SQL_KEYWORD
+ , Arrays.asList(new String[]{",", "WHERE"}), end);
+ }
+ return new TabCompletionInfo(TabCompletionInfo.MatchType.SQL_KEYWORD
+ , Arrays.asList(new String[]{"=", "<>", "LIKE", "<", ">"}), end);
} else {
end = tmpCommand.trim();
+ return new TabCompletionInfo(TabCompletionInfo.MatchType.COLUMN_NAMES
+ , getTableNames(parts), end);
}
- if (tmpCommand.trim().length() > 0) {
+ /*if (tmpCommand.trim().length() > 0) {
return new TabCompletionInfo(TabCompletionInfo.MatchType.SQL_KEYWORD
, Arrays.asList(new String[]{"=", "<>", "LIKE", "<", ">"}), end);
}
return new TabCompletionInfo(TabCompletionInfo.MatchType.SQL_KEYWORD
, Arrays.asList(new String[]{",", "WHERE"})); // TODO END
+*/
}
private static String getTableName(String alias, List<SQLPart> parts) {
Modified: src/test/java/nl/improved/sqlclient/util/SQLParserTest.java
===================================================================
--- src/test/java/nl/improved/sqlclient/util/SQLParserTest.java 2009-03-02 08:54:07 UTC (rev 391)
+++ src/test/java/nl/improved/sqlclient/util/SQLParserTest.java 2009-03-04 08:08:46 UTC (rev 392)
@@ -664,6 +664,15 @@
assertTrue("=", matches.contains("="));
assertEquals("", info.getStart());
+ sqlCommand = Arrays.asList(new String[]{"UPDATE A SET B"});
+ 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("B", info.getStart());
+
sqlCommand = Arrays.asList(new String[]{"UPDATE A SET B = "});
cursorPos = new Point(sqlCommand.get(0).length(),0);
info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos);
@@ -672,6 +681,15 @@
matches = info.getPossibleMatches();
assertTrue("A", matches.contains("A"));
assertEquals("", 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);
+ assertNotNull(info);
+ assertEquals(TabCompletionInfo.MatchType.SQL_KEYWORD, info.getMatchType());
+ matches = info.getPossibleMatches();
+ assertTrue("WHERE not in " + matches , matches.contains("WHERE"));
+ assertEquals("WH", info.getStart());
}
public void testGetTableName() {
|
|
[Sqlshell-svn] /devel/src: main/java/nl/improved/sqlclient/util
test/java/nl/improved/sqlclient/util
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);
|
|
[Sqlshell-svn] /devel/src: main/java/nl/improved/sqlclient/util
test/java/nl/improved/sqlclient/util
From: SVN by r. <sv...@ca...> - 2009-03-15 20:44:51
|
Author: roy
Date: 2009-03-15 21:44:38 +0100 (Sun, 15 Mar 2009)
New Revision: 395
Modified:
src/main/java/nl/improved/sqlclient/util/SQLParser.java
src/test/java/nl/improved/sqlclient/util/SQLParserTest.java
Log:
set/where unit test added + fix
Modified: src/main/java/nl/improved/sqlclient/util/SQLParser.java
===================================================================
--- src/main/java/nl/improved/sqlclient/util/SQLParser.java 2009-03-14 15:23:00 UTC (rev 394)
+++ src/main/java/nl/improved/sqlclient/util/SQLParser.java 2009-03-15 20:44:38 UTC (rev 395)
@@ -160,7 +160,7 @@
}
} else if (sql.matches("[\\s]*SET.*")) {
sqlType = SQLPart.SQLType.SET;
- if (sql.matches("[\\s]*WHERE.*")) {
+ if (sql.matches(".*[\\s]*WHERE.*")) {
end = sql.indexOf("WHERE");
}
} else {
@@ -196,8 +196,6 @@
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());
@@ -423,7 +421,7 @@
return new TabCompletionInfo(TabCompletionInfo.MatchType.COLUMN_NAMES
, tableNames, end);
}
- if (upperCommandString.matches(".*[\\s]+(AND|OR)[\\s]+")) {
+ if (upperCommandString.matches("[\\s]*") || upperCommandString.matches(".*[\\s]+(AND|OR)[\\s]+")) {
return new TabCompletionInfo(TabCompletionInfo.MatchType.COLUMN_NAMES
, getTableNames(parts));
}
Modified: src/test/java/nl/improved/sqlclient/util/SQLParserTest.java
===================================================================
--- src/test/java/nl/improved/sqlclient/util/SQLParserTest.java 2009-03-14 15:23:00 UTC (rev 394)
+++ src/test/java/nl/improved/sqlclient/util/SQLParserTest.java 2009-03-15 20:44:38 UTC (rev 395)
@@ -796,6 +796,16 @@
matches = info.getPossibleMatches();
assertTrue("WHERE not in " + matches , matches.contains("WHERE"));
assertEquals("WH", info.getStart());
+
+ sqlCommand = Arrays.asList(new String[]{"update l set s='' where "});
+ 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();
+ assertEquals(1, matches.size());
+ assertTrue(matches.contains("l"));
+ assertEquals("", info.getStart());
}
public void testGetTableName() {
|
|
[Sqlshell-svn] /devel/src: main/java/nl/improved/sqlclient/util
test/java/nl/improved/sqlclient/util
From: SVN by r. <sv...@ca...> - 2009-03-16 08:38:46
|
Author: roy
Date: 2009-03-16 09:38:36 +0100 (Mon, 16 Mar 2009)
New Revision: 396
Modified:
src/main/java/nl/improved/sqlclient/util/SQLParser.java
src/test/java/nl/improved/sqlclient/util/SQLParserTest.java
Log:
more tab compl fixes
Modified: src/main/java/nl/improved/sqlclient/util/SQLParser.java
===================================================================
--- src/main/java/nl/improved/sqlclient/util/SQLParser.java 2009-03-15 20:44:38 UTC (rev 395)
+++ src/main/java/nl/improved/sqlclient/util/SQLParser.java 2009-03-16 08:38:36 UTC (rev 396)
@@ -47,6 +47,7 @@
List<? extends CharSequence> lines = command.getLines();
return getTabCompletionInfo(lines, commandPoint);
}
+
public static TabCompletionInfo getTabCompletionInfo(List<? extends CharSequence> lines, Point commandPoint) {
StringBuffer buf = new StringBuffer();
int position = 0;
@@ -396,13 +397,29 @@
return new TabCompletionInfo(TabCompletionInfo.MatchType.OTHER
, Arrays.asList(new String[]{" "}));
}
- String tmpCommand = startOfCommand.substring(startOfCommand.toUpperCase().indexOf("WHERE")+"WHERE".length()+1);
+ String tmpCommand = startOfCommand.substring(startOfCommand.toUpperCase().indexOf("WHERE")+"WHERE".length());
String upperCommandString = tmpCommand.toUpperCase();
+ if (upperCommandString.indexOf("AND") >= 0) {
+ String tmp = upperCommandString.substring(upperCommandString.lastIndexOf("AND")+3);
+ if (tmp.trim().length() > 0) {
+ upperCommandString = tmp;
+ }
+ }
+ if (upperCommandString.indexOf("OR") >= 0) {
+ String tmp = upperCommandString.substring(upperCommandString.lastIndexOf("OR")+2);
+ if (tmp.trim().length() > 0) {
+ upperCommandString = tmp;
+ }
+ }
+ if (upperCommandString.equals("")) {
+ return new TabCompletionInfo(TabCompletionInfo.MatchType.OTHER
+ , Arrays.asList(new String[]{" "}));
+ }
//String VAR = "(|"+TABLE+"\\.)(|"+COLUMN+")";
//String VALUE = "('.*'|[0-9]+|"+VAR+")";
// VALUE COMPERATOR VALUE
// VALUE IN (.*)
- String regExp = "(|"+VALUE+"(|[\\s]*"+COMPARATOR+"[\\s]*(|"+VALUE+"(|([\\s]+(AND|OR)[\\s]+(|"+VALUE+"(|[\\s]*"+COMPARATOR+"[\\s]*(|"+VALUE+"))))*))|[\\s]*IN[\\s]*(.*)[\\s]*))";
+ String regExp = "[\\s]*(|"+VALUE+"(|[\\s]*"+COMPARATOR+"[\\s]*(|"+VALUE+"(|([\\s]+(AND|OR)[\\s]+(|"+VALUE+"(|[\\s]*"+COMPARATOR+"[\\s]*(|"+VALUE+"))))*))|[\\s]*IN[\\s]*(.*)[\\s]*))";
if (upperCommandString.matches(regExp) || upperCommandString.endsWith(".")) {
int lastIndex = tmpCommand.lastIndexOf(' ');
lastIndex = Math.max(lastIndex, tmpCommand.lastIndexOf('\t'));
Modified: src/test/java/nl/improved/sqlclient/util/SQLParserTest.java
===================================================================
--- src/test/java/nl/improved/sqlclient/util/SQLParserTest.java 2009-03-15 20:44:38 UTC (rev 395)
+++ src/test/java/nl/improved/sqlclient/util/SQLParserTest.java 2009-03-16 08:38:36 UTC (rev 396)
@@ -400,7 +400,7 @@
Point cursorPos = new Point(sqlCommand.get(0).length(),0);
TabCompletionInfo info = SQLParser.getTabCompletionInfo(sqlCommand, cursorPos);
assertNotNull(info);
- assertEquals(TabCompletionInfo.MatchType.COLUMN_NAMES, info.getMatchType());
+ assertEquals(info.getPossibleMatches().toString(), TabCompletionInfo.MatchType.COLUMN_NAMES, info.getMatchType());
List<String> matches = info.getPossibleMatches();
assertEquals(2, matches.size());
assertTrue(matches.contains("A"));
@@ -605,6 +605,15 @@
matches = info.getPossibleMatches();
assertTrue(matches.contains("A"));
assertEquals("", info.getStart());
+
+ sqlCommand = Arrays.asList(new String[]{"select * from L where s is not null and t='p' and m"});
+ 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("L"));
+ assertEquals("m", info.getStart());
}
public void testGroupBy() {
|
|
[Sqlshell-svn] /devel/src: main/java/nl/improved/sqlclient/util
test/java/nl/improved/sqlclient/util
From: SVN by r. <sv...@ca...> - 2009-03-26 18:50:00
|
Author: roy
Date: 2009-03-26 19:49:51 +0100 (Thu, 26 Mar 2009)
New Revision: 397
Modified:
src/main/java/nl/improved/sqlclient/util/SQLParser.java
src/main/java/nl/improved/sqlclient/util/SQLPart.java
src/test/java/nl/improved/sqlclient/util/SQLParserTest.java
Log:
added support for tab completion for 'insert into' sql statements
Modified: src/main/java/nl/improved/sqlclient/util/SQLParser.java
===================================================================
--- src/main/java/nl/improved/sqlclient/util/SQLParser.java 2009-03-16 08:38:36 UTC (rev 396)
+++ src/main/java/nl/improved/sqlclient/util/SQLParser.java 2009-03-26 18:49:51 UTC (rev 397)
@@ -93,6 +93,10 @@
return getUpdateTabCompletionInfo(part, parts, charsLeft);
case SET :
return getSetTabCompletionInfo(part, parts, charsLeft);
+ case INSERT :
+ return getInsertTabCompletionInfo(part, parts, charsLeft);
+ case VALUES :
+ return getValuesTabCompletionInfo(part, parts, charsLeft);
}
} else {
charsLeft -=part.getPart().length();
@@ -164,6 +168,13 @@
if (sql.matches(".*[\\s]*WHERE.*")) {
end = sql.indexOf("WHERE");
}
+ } else if (sql.matches("[\\s]*INSERT INTO.*")) {
+ sqlType = SQLPart.SQLType.INSERT;
+ if (sql.matches(".*[\\s]*VALUES.*")) {
+ end = sql.indexOf("VALUES");
+ }
+ } else if (sql.matches("[\\s]*VALUES.*")) {
+ sqlType = SQLPart.SQLType.VALUES;
} else {
sqlType = SQLPart.SQLType.OTHER;
}
@@ -258,6 +269,36 @@
return new TabCompletionInfo(TabCompletionInfo.MatchType.OTHER, possibleMatches, end);
}
+ private static TabCompletionInfo getInsertTabCompletionInfo(SQLPart part, List<SQLPart> parts, int charsLeft) {
+ String startOfCommand = part.getPart().toString().substring(0, charsLeft);
+ if (startOfCommand.equalsIgnoreCase("INSERT INTO")) {
+ return new TabCompletionInfo(TabCompletionInfo.MatchType.OTHER
+ , Arrays.asList(new String[]{" "}));
+ }
+ String tmpCommand = startOfCommand.substring(startOfCommand.toUpperCase().indexOf("INSERT INTO")+"INSERT INTO".length()+1);
+ if (tmpCommand.toUpperCase().matches("[\\s]*"+TABLE+"[\\s]+")) {
+ return new TabCompletionInfo(TabCompletionInfo.MatchType.OTHER
+ , Arrays.asList(new String[]{"(", "VALUES"}));
+ }
+ if (tmpCommand.indexOf('(') >=0 && tmpCommand.indexOf(')') < 0) {
+ String start;
+ if (tmpCommand.indexOf(',') > 0) {
+ start = tmpCommand.substring(tmpCommand.lastIndexOf(',')+1);
+ } else {
+ start = tmpCommand.substring(tmpCommand.indexOf('(')+1);
+ }
+ return new TabCompletionInfo(TabCompletionInfo.MatchType.COLUMN_NAMES
+ , getTableNames(parts), start.trim());
+ }
+ if (tmpCommand.indexOf(')') > 0) {
+ return new TabCompletionInfo(TabCompletionInfo.MatchType.SQL_KEYWORD
+ , Arrays.asList(new String[]{"VALUES"}), tmpCommand.substring(tmpCommand.lastIndexOf(')')+1).trim());
+ }
+ return new TabCompletionInfo(TabCompletionInfo.MatchType.TABLE_NAMES
+ , Arrays.asList(new String[]{}), tmpCommand.trim());
+
+ }
+
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);
@@ -366,13 +407,13 @@
return alias;
}
private static List<String> getTableNames(List<SQLPart> parts) {
- SQLPart fromSqlPart = null;
+ SQLPart tableNamesSqlPart = null;
for (SQLPart tmpPart : parts) {
- if (tmpPart.getSQLType() == SQLPart.SQLType.FROM || tmpPart.getSQLType() == SQLPart.SQLType.UPDATE) {
- fromSqlPart = tmpPart;
+ if (tmpPart.getSQLType() == SQLPart.SQLType.FROM || tmpPart.getSQLType() == SQLPart.SQLType.UPDATE || tmpPart.getSQLType() == SQLPart.SQLType.INSERT) {
+ tableNamesSqlPart = tmpPart;
}
}
- return fromSqlPart.getTableNames();
+ return tableNamesSqlPart.getTableNames();
}
private static TabCompletionInfo getUpdateTabCompletionInfo(SQLPart part, List<SQLPart> parts, int charsLeft) {
@@ -387,6 +428,26 @@
, new ArrayList(), tmpCommand.trim());
}
+ private static TabCompletionInfo getValuesTabCompletionInfo(SQLPart part, List<SQLPart> parts, int charsLeft) {
+ String startOfCommand = part.getPart().toString().substring(0, charsLeft);
+ if (startOfCommand.equalsIgnoreCase("VALUES")) {
+ return new TabCompletionInfo(TabCompletionInfo.MatchType.OTHER
+ , Arrays.asList(new String[]{" ", "("}));
+ }
+ String tmpCommand = startOfCommand.substring(startOfCommand.toUpperCase().indexOf("VALUES")+"VALUES".length());
+ if (tmpCommand.indexOf('(') < 0) {
+ return new TabCompletionInfo(TabCompletionInfo.MatchType.OTHER
+ , Arrays.asList(new String[]{"("}));
+ }
+ tmpCommand = tmpCommand.substring(tmpCommand.indexOf('(')+1);
+ if (tmpCommand.indexOf(',') > 0) {
+ tmpCommand = tmpCommand.substring(tmpCommand.lastIndexOf(',')+1);
+ }
+ return new TabCompletionInfo(TabCompletionInfo.MatchType.COLUMN_NAMES
+ , getTableNames(parts), tmpCommand);
+
+ }
+
private static TabCompletionInfo getWhereTabCompletionInfo(SQLPart part, List<SQLPart> parts, int charsLeft) {
// find out if it looks like
// 'SELECT x FROM A,B WHERE'
Modified: src/main/java/nl/improved/sqlclient/util/SQLPart.java
===================================================================
--- src/main/java/nl/improved/sqlclient/util/SQLPart.java 2009-03-16 08:38:36 UTC (rev 396)
+++ src/main/java/nl/improved/sqlclient/util/SQLPart.java 2009-03-26 18:49:51 UTC (rev 397)
@@ -6,6 +6,7 @@
package nl.improved.sqlclient.util;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.List;
import nl.improved.sqlclient.TabCompletionInfo;
@@ -106,6 +107,14 @@
}
public List<String> getTableNames() {
+ if (sqlType == SQLType.INSERT) {
+ String stringPart = getPart().toString();
+ String tableName = stringPart.substring(stringPart.toUpperCase().indexOf("INSERT INTO")+"INSERT INTO".length()).trim();
+ if (tableName.indexOf(" ") > 0) {
+ tableName = tableName.substring(0, tableName.indexOf(' '));
+ }
+ return Arrays.asList(new String[]{tableName});
+ }
if (sqlType == SQLType.FROM || sqlType == SQLType.UPDATE) {
List<String> returnValue = new ArrayList<String>();
String stringPart = getPart().toString();
@@ -134,7 +143,7 @@
}
return returnValue;
}
- throw new IllegalStateException("Table names are only available for FROM,UPDATE ("+ sqlType+")");
+ throw new IllegalStateException("Table names are only available for FROM,UPDATE,INSERT INTO ("+ sqlType+")");
}
}
Modified: src/test/java/nl/improved/sqlclient/util/SQLParserTest.java
===================================================================
--- src/test/java/nl/improved/sqlclient/util/SQLParserTest.java 2009-03-16 08:38:36 UTC (rev 396)
+++ src/test/java/nl/improved/sqlclient/util/SQLParserTest.java 2009-03-26 18:49:51 UTC (rev 397)
@@ -817,6 +817,77 @@
assertEquals("", info.getStart());
}
+ public void testInsertInto() {
+ List<String> sqlCommand = Arrays.asList(new String[]{"insert into "});
+ 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("", info.getStart());
+
+ sqlCommand = Arrays.asList(new String[]{"insert into tab"});
+ 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("tab", info.getStart());
+
+ sqlCommand = Arrays.asList(new String[]{"insert into tab "});
+ 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("("));
+ assertTrue(matches.contains("VALUES"));
+ assertEquals("", info.getStart());
+
+ sqlCommand = Arrays.asList(new String[]{"insert into tab ("});
+ 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("tab"));
+ assertEquals("", info.getStart());
+
+ sqlCommand = Arrays.asList(new String[]{"insert into tab (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("tab"));
+ assertEquals("A", info.getStart());
+
+ sqlCommand = Arrays.asList(new String[]{"insert into tab (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("tab"));
+ assertEquals("", info.getStart());
+
+ sqlCommand = Arrays.asList(new String[]{"insert into tab (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();
+ assertEquals(1, matches.size());
+ assertTrue(matches.contains("VALUES"));
+ 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);
|
|
[Sqlshell-svn] /devel/src: main/java/nl/improved/sqlclient/util
test/java/nl/improved/sqlclient/util
From: SVN by r. <sv...@ca...> - 2009-09-30 18:15:03
|
Author: roy
Date: 2009-09-30 20:14:51 +0200 (Wed, 30 Sep 2009)
New Revision: 426
Added:
src/test/java/nl/improved/sqlclient/util/ResultBuilderTest.java
Modified:
src/main/java/nl/improved/sqlclient/util/ResultBuilder.java
Log:
fix output when no rows returned
added unit tests
Modified: src/main/java/nl/improved/sqlclient/util/ResultBuilder.java
===================================================================
--- src/main/java/nl/improved/sqlclient/util/ResultBuilder.java 2009-09-30 17:50:15 UTC (rev 425)
+++ src/main/java/nl/improved/sqlclient/util/ResultBuilder.java 2009-09-30 18:14:51 UTC (rev 426)
@@ -105,10 +105,13 @@
result.append(horizontalSeperator);
}
}
- result.append('\n');
Iterator<Cell> cells = rows.iterator();
int prevRow = 0;
- result.append(verticalSeparator);
+ boolean rowsAvailable = cells.hasNext();
+ if (rowsAvailable) {
+ result.append('\n');
+ result.append(verticalSeparator);
+ }
while (cells.hasNext()) {
Cell cell = cells.next();
if (cell != null && cell.row != prevRow) {
@@ -137,12 +140,12 @@
}
}
result.append('\n');
- if (horizontalSeperatorEnabled) {
+ if (rowsAvailable && horizontalSeperatorEnabled) {
for (int i = 0; i < rowLength-1; i++) {
result.append(horizontalSeperator);
}
+ result.append('\n');
}
- result.append('\n');
if (footer != null) {
result.append(footer);
result.append('\n');
Added: src/test/java/nl/improved/sqlclient/util/ResultBuilderTest.java
===================================================================
--- src/test/java/nl/improved/sqlclient/util/ResultBuilderTest.java 2009-09-30 17:50:15 UTC (rev 425)
+++ src/test/java/nl/improved/sqlclient/util/ResultBuilderTest.java 2009-09-30 18:14:51 UTC (rev 426)
@@ -0,0 +1,56 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package nl.improved.sqlclient.util;
+
+import java.util.Arrays;
+import junit.framework.TestCase;
+import nl.improved.sqlclient.util.ResultBuilder.Alignment;
+
+/**
+ *
+ * @author roy
+ */
+public class ResultBuilderTest extends TestCase {
+
+ public ResultBuilderTest(String testName) {
+ super(testName);
+ }
+
+
+ /**
+ * Test of toString method, of class ResultBuilder.
+ */
+ public void testToString() {
+ ResultBuilder instance = new ResultBuilder();
+ instance.setHeader(Arrays.asList(new String[]{"ID"}));
+ String expected =
+ "----\n" +
+ "|ID|\n"+
+ "----\n" ;
+ assertEquals(expected, instance.toString());
+ expected =
+ "----\n" +
+ "|ID|\n"+
+ "----\n" +
+ "| 1|\n"+
+ "----\n" ;
+ instance.set(0, 0, "1", Alignment.RIGHT);
+ assertEquals(expected, instance.toString());
+ expected =
+ "----\n" +
+ "|ID|\n"+
+ "----\n" +
+ "| 1|\n"+
+ "----\n"+
+ "F\n";
+ instance.setFooter("F");
+ //System.out.println("---------");
+ //System.out.println(instance.toString());
+ //System.out.println("---------");
+ assertEquals(expected, instance.toString());
+ }
+
+}
|
|
[Sqlshell-svn] /devel/src: main/java/nl/improved/sqlclient/util
test/java/nl/improved/sqlclient/util
From: SVN by r. <sv...@ca...> - 2010-01-27 08:01:08
|
Author: roy
Date: 2010-01-27 09:00:57 +0100 (Wed, 27 Jan 2010)
New Revision: 463
Modified:
src/main/java/nl/improved/sqlclient/util/SQLParser.java
src/main/java/nl/improved/sqlclient/util/SQLPart.java
src/test/java/nl/improved/sqlclient/util/SQLParserTest.java
Log:
fix group by tab completion when using to_char(date, 'format')
Modified: src/main/java/nl/improved/sqlclient/util/SQLParser.java
===================================================================
--- src/main/java/nl/improved/sqlclient/util/SQLParser.java 2010-01-24 14:35:18 UTC (rev 462)
+++ src/main/java/nl/improved/sqlclient/util/SQLParser.java 2010-01-27 08:00:57 UTC (rev 463)
@@ -220,42 +220,21 @@
List<String> possibleMatches = new ArrayList<String>();
for (SQLPart pPart : parts) {
if (pPart.getSQLType() == SQLPart.SQLType.SELECT) {
- String pSubPart = pPart.getPart().toString().substring("SELECT".length()).trim();
- int lastIndex = 0;
- int newIndex;
- StringBuffer bestMatch = new StringBuffer();
- while ( (newIndex = pSubPart.indexOf(',', lastIndex)) > 0) {
- String colName = pSubPart.substring(lastIndex, newIndex).trim();
- if (colName.indexOf(' ') >=0) {
- colName = colName.substring(colName.lastIndexOf(' '));
- }
+ StringBuffer allColumns = new StringBuffer();
+ for (String colName : pPart.getColumnNames()) {
if (!possibleMatches.contains(colName)) {
- possibleMatches.add(colName);
if (colName.indexOf("max") < 0 && colName.indexOf("min") < 0 && colName.indexOf("count") < 0) {
- if (bestMatch.length() > 0) {
- bestMatch.append(',');
+ possibleMatches.add(colName);
+ if (allColumns.length() > 0) {
+ allColumns.append(", ");
}
- bestMatch.append(colName);
+ allColumns.append(colName);
}
}
- lastIndex = newIndex+1;
}
- String colName = pSubPart.substring(lastIndex).trim();
- if (colName.indexOf(' ') >=0) { // alias
- colName = colName.substring(colName.lastIndexOf(' '));
+ if (allColumns.length() > 0) {
+ possibleMatches.add(allColumns.toString());
}
- if (!possibleMatches.contains(colName) && colName.length() > 0) {
- possibleMatches.add(colName);
- if (colName.indexOf("max") < 0 && colName.indexOf("min") < 0 && colName.indexOf("count") < 0) {
- if (bestMatch.length() > 0) {
- bestMatch.append(',');
- }
- bestMatch.append(colName);
- }
- }
- if (bestMatch.length() > 0 && !possibleMatches.contains(bestMatch)) {
- possibleMatches.add(0, bestMatch.toString());
- }
}
}
if (subPart.trim().length() == 0 || subPart.trim().endsWith(",")) {
Modified: src/main/java/nl/improved/sqlclient/util/SQLPart.java
===================================================================
--- src/main/java/nl/improved/sqlclient/util/SQLPart.java 2010-01-24 14:35:18 UTC (rev 462)
+++ src/main/java/nl/improved/sqlclient/util/SQLPart.java 2010-01-27 08:00:57 UTC (rev 463)
@@ -49,7 +49,47 @@
}
public List<String> getColumnNames() {
+ if (sqlType != SQLType.SELECT) {
+ throw new IllegalStateException("Table names are only available for SELECT ("+ sqlType+")");
+ }
+ List<String> returnValue = new ArrayList<String>();
+ int openQuotes = 0;
+ int openRondeHaakjes = 0;
+ String stringPart = getPart().toString();
+ char[] sub = stringPart.substring(stringPart.toUpperCase().indexOf("SELECT")+"SELECT".length()).toCharArray();
+
+ StringBuffer colName = new StringBuffer();
+ for (char c : sub) {
+ if (c == '\'') {
+ openQuotes++;
+ } else if (c == '(') {
+ if (openQuotes %2 == 0) { // don't count ( in strings
+ openRondeHaakjes++;
+ }
+ } else if (c == ')') {
+ if (openQuotes %2 == 0) { // don't count ( in strings
+ openRondeHaakjes--;
+ }
+ }
+ if (c == ',' && openQuotes % 2 == 0 && openRondeHaakjes % 2 == 0) {
+ String col = colName.toString().trim();
+ // TODO cut off alias
+ returnValue.add(col);
+ colName = new StringBuffer();
+ } else {
+ colName.append(c);
+ }
+ }
+ if (colName.length() > 0) {
+ returnValue.add(colName.toString().trim()); // TODO cut off alias
+
+ }
+ return returnValue;
+
+ }
+ public List<String> _getColumnNames() {
+
if (sqlType == SQLType.SELECT) {
String stringPart = getPart().toString();
String sub = stringPart.substring(stringPart.toUpperCase().indexOf("SELECT")+"SELECT".length());
Modified: src/test/java/nl/improved/sqlclient/util/SQLParserTest.java
===================================================================
--- src/test/java/nl/improved/sqlclient/util/SQLParserTest.java 2010-01-24 14:35:18 UTC (rev 462)
+++ src/test/java/nl/improved/sqlclient/util/SQLParserTest.java 2010-01-27 08:00:57 UTC (rev 463)
@@ -23,6 +23,31 @@
}
+ public void testGetColumnNames() {
+ SQLPart part =new SQLPart(SQLPart.SQLType.SELECT, "SELECT a,b,c");
+ List<String> columnNames = part.getColumnNames();
+ assertEquals(columnNames.toString(), 3, columnNames.size());
+ assertTrue(columnNames.contains("a"));
+ assertTrue(columnNames.contains("b"));
+ assertTrue(columnNames.contains("c"));
+
+ part =new SQLPart(SQLPart.SQLType.SELECT, "SELECT a,b,count(*)");
+ columnNames = part.getColumnNames();
+ assertEquals(columnNames.toString(), 3, columnNames.size());
+ assertTrue(columnNames.contains("a"));
+ assertTrue(columnNames.contains("b"));
+ assertTrue(columnNames.contains("count(*)"));
+
+
+ part =new SQLPart(SQLPart.SQLType.SELECT, "SELECT to_char(mydate, 'yyyymm'),b,count(*)");
+ columnNames = part.getColumnNames();
+ assertEquals(columnNames.toString(), 3, columnNames.size());
+ assertTrue(columnNames.contains("to_char(mydate, 'yyyymm')"));
+ assertTrue(columnNames.contains("b"));
+ assertTrue(columnNames.contains("count(*)"));
+
+
+ }
/**
* Test of parse method, of class SQLParser.
*/
@@ -645,11 +670,10 @@
assertNotNull(info);
assertEquals(TabCompletionInfo.MatchType.OTHER, info.getMatchType());
List<String> matches = info.getPossibleMatches();
- assertEquals(4, matches.size());
+ assertEquals(matches.toString(), 3, matches.size());
assertTrue(matches.contains("c1"));
assertTrue(matches.contains("c2"));
- assertTrue(matches.contains("count(*)"));
- assertTrue(matches.contains("c1,c2"));
+ assertTrue(matches.contains("c1, c2"));
assertEquals("", info.getStart());
@@ -708,6 +732,18 @@
matches = info.getPossibleMatches();
assertTrue(matches.contains("c1"));
assertEquals("", info.getStart());
+
+ sqlCommand = Arrays.asList(new String[]{"SELECT to_char(mydate, 'yyyymm') , 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();
+ assertEquals(matches.toString(), 3, matches.size());
+ assertTrue(matches.contains("c2"));
+ assertTrue(matches.contains("to_char(mydate, 'yyyymm')"));
+ assertTrue(matches.contains("to_char(mydate, 'yyyymm'), c2"));
+ assertEquals("", info.getStart());
}
public void testOrderBy() {
|
|
[Sqlshell-svn] /devel/src: main/java/nl/improved/sqlclient/util
test/java/nl/improved/sqlclient/util
From: SVN by r. <sv...@ca...> - 2010-01-27 08:04:38
|
Author: roy
Date: 2010-01-27 09:04:27 +0100 (Wed, 27 Jan 2010)
New Revision: 464
Modified:
src/main/java/nl/improved/sqlclient/util/SQLParser.java
src/test/java/nl/improved/sqlclient/util/SQLParserTest.java
Log:
fix duplication in group by
Modified: src/main/java/nl/improved/sqlclient/util/SQLParser.java
===================================================================
--- src/main/java/nl/improved/sqlclient/util/SQLParser.java 2010-01-27 08:00:57 UTC (rev 463)
+++ src/main/java/nl/improved/sqlclient/util/SQLParser.java 2010-01-27 08:04:27 UTC (rev 464)
@@ -232,7 +232,7 @@
}
}
}
- if (allColumns.length() > 0) {
+ if (allColumns.length() > 0 && !possibleMatches.contains(allColumns.toString())) {
possibleMatches.add(allColumns.toString());
}
}
Modified: src/test/java/nl/improved/sqlclient/util/SQLParserTest.java
===================================================================
--- src/test/java/nl/improved/sqlclient/util/SQLParserTest.java 2010-01-27 08:00:57 UTC (rev 463)
+++ src/test/java/nl/improved/sqlclient/util/SQLParserTest.java 2010-01-27 08:04:27 UTC (rev 464)
@@ -744,6 +744,26 @@
assertTrue(matches.contains("to_char(mydate, 'yyyymm')"));
assertTrue(matches.contains("to_char(mydate, 'yyyymm'), c2"));
assertEquals("", info.getStart());
+
+ sqlCommand = Arrays.asList(new String[]{"SELECT to_char(mydate, 'yyyymm') , count(*) 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();
+ assertEquals(matches.toString(), 1, matches.size());
+ assertTrue(matches.contains("to_char(mydate, 'yyyymm')"));
+ assertEquals("", info.getStart());
+
+ sqlCommand = Arrays.asList(new String[]{"SELECT count(*), to_char(mydate, 'yyyymm') 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();
+ assertEquals(matches.toString(), 1, matches.size());
+ assertTrue(matches.contains("to_char(mydate, 'yyyymm')"));
+ assertEquals("", info.getStart());
}
public void testOrderBy() {
|