01: /*
02: * UpdateAnalyzer.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.gui.completion;
13:
14: import java.util.ArrayList;
15: import java.util.List;
16: import workbench.db.TableIdentifier;
17: import workbench.db.WbConnection;
18: import workbench.sql.formatter.SQLLexer;
19: import workbench.sql.formatter.SQLToken;
20: import workbench.util.SqlUtil;
21: import workbench.util.TableAlias;
22:
23: /**
24: * Analyze an UPDATE statement regarding the context for the auto-completion
25: * @author support@sql-workbench.net
26: */
27: public class UpdateAnalyzer extends BaseAnalyzer {
28: public UpdateAnalyzer(WbConnection conn, String statement,
29: int cursorPos) {
30: super (conn, statement, cursorPos);
31: }
32:
33: protected void checkContext() {
34: checkOverwrite();
35:
36: final int IN_SET = 1;
37: final int IN_UPDATE = 2;
38: final int IN_WHERE = 3;
39:
40: int state = -1;
41: boolean nextIsTable = false;
42: String table = null;
43:
44: SQLLexer lexer = new SQLLexer(sql);
45: SQLToken t = lexer.getNextToken(false, false);
46:
47: while (t != null) {
48: if (nextIsTable) {
49: table = t.getContents();
50: nextIsTable = false;
51: }
52: if (t.getContents().equals("UPDATE")) {
53: nextIsTable = true;
54: if (cursorPos > t.getCharEnd()) {
55: state = IN_UPDATE;
56: }
57: } else if (t.getContents().equals("SET")) {
58: if (cursorPos > t.getCharEnd()) {
59: state = IN_SET;
60: }
61: } else if (t.getContents().equals("WHERE")) {
62: if (cursorPos > t.getCharEnd()) {
63: state = IN_WHERE;
64: }
65: }
66: t = lexer.getNextToken(false, false);
67: }
68:
69: if (state == IN_UPDATE) {
70: context = CONTEXT_TABLE_LIST;
71: String q = this .getQualifierLeftOfCursor();
72: if (q != null) {
73: this .schemaForTableList = q;
74: } else {
75: this .schemaForTableList = this .dbConnection
76: .getMetadata().getCurrentSchema();
77: }
78: } else {
79: // "inside" the SET and after the WHERE we always need the column list
80: if (table != null) {
81: context = CONTEXT_COLUMN_LIST;
82: tableForColumnList = new TableIdentifier(table);
83: }
84: }
85: }
86:
87: public List<TableAlias> getTables() {
88: String table = SqlUtil.getUpdateTable(this .sql);
89: TableAlias a = new TableAlias(table);
90: List<TableAlias> result = new ArrayList<TableAlias>(1);
91: result.add(a);
92: return result;
93: }
94:
95: }
|