001: /*
002: * InsertAnalyzer.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.completion;
013:
014: import java.util.regex.Matcher;
015: import java.util.regex.Pattern;
016: import workbench.db.TableIdentifier;
017: import workbench.db.WbConnection;
018: import workbench.log.LogMgr;
019: import workbench.sql.formatter.SQLLexer;
020: import workbench.sql.formatter.SQLToken;
021:
022: /**
023: * Analyze an UPDATE statement regarding the context for the auto-completion
024: * @author support@sql-workbench.net
025: */
026: public class InsertAnalyzer extends BaseAnalyzer {
027:
028: public InsertAnalyzer(WbConnection conn, String statement,
029: int cursorPos) {
030: super (conn, statement, cursorPos);
031: }
032:
033: public void checkContext() {
034: SQLLexer lexer = new SQLLexer(this .sql);
035:
036: int intoEnd = Integer.MAX_VALUE;
037: int intoStart = Integer.MAX_VALUE;
038: int tableStart = Integer.MAX_VALUE;
039: int columnBracketStart = Integer.MAX_VALUE;
040: int columnBracketEnd = Integer.MAX_VALUE;
041: int valuesPos = Integer.MAX_VALUE;
042: boolean inColumnBracket = false;
043:
044: String schemaName = null;
045: String tableName = null;
046: try {
047: int bracketCount = 0;
048: boolean nextTokenIsTable = false;
049: SQLToken t = lexer.getNextToken(false, false);
050:
051: while (t != null) {
052: String value = t.getContents();
053: if ("(".equals(value)) {
054: bracketCount++;
055: // if the INTO keyword was already read but not the VALUES
056: // keyword, the opening bracket marks the end of the table
057: // definition between INTO and the column list
058: if (intoStart != Integer.MAX_VALUE
059: && valuesPos == Integer.MAX_VALUE) {
060: intoEnd = t.getCharBegin();
061: columnBracketStart = t.getCharEnd();
062: inColumnBracket = true;
063: }
064: } else if (")".equals(value)) {
065: if (inColumnBracket) {
066: columnBracketEnd = t.getCharBegin();
067: }
068: bracketCount--;
069: inColumnBracket = false;
070: } else if (bracketCount == 0) {
071: if (nextTokenIsTable) {
072: tableStart = t.getCharBegin();
073: tableName = t.getContents();
074: nextTokenIsTable = false;
075: }
076: if ("INTO".equals(value)) {
077: intoStart = t.getCharEnd();
078: nextTokenIsTable = true;
079: } else if ("VALUES".equals(value)) {
080: valuesPos = t.getCharBegin();
081: break;
082: }
083: }
084: t = lexer.getNextToken(false, false);
085: }
086: } catch (Exception e) {
087: LogMgr.logError("InsertAnalyzer.checkContext()",
088: "Error parsing insert statement", e);
089: this .context = NO_CONTEXT;
090: }
091:
092: TableIdentifier table = null;
093: if (tableName != null) {
094: table = new TableIdentifier(tableName);
095: }
096:
097: if (cursorPos > intoStart && cursorPos < intoEnd) {
098: if (cursorPos > tableStart) {
099: if (table != null)
100: schemaForTableList = table.getSchema();
101: }
102:
103: if (schemaForTableList == null) {
104: schemaForTableList = this.dbConnection.getMetadata()
105: .getSchemaToUse();
106: }
107:
108: context = CONTEXT_TABLE_LIST;
109: } else if (cursorPos >= columnBracketStart
110: && cursorPos <= columnBracketEnd) {
111: tableForColumnList = table;
112: context = CONTEXT_COLUMN_LIST;
113: }
114: }
115:
116: }
|