|
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);
|