01: /*
02: * AlterTableAnalyzer.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 workbench.db.TableIdentifier;
15: import workbench.db.WbConnection;
16: import workbench.log.LogMgr;
17: import workbench.sql.formatter.SQLLexer;
18: import workbench.sql.formatter.SQLToken;
19: import workbench.util.SqlUtil;
20:
21: /**
22: * Analyze an ALTER TABLE statement to provide completion for tables and columns
23: * @author support@sql-workbench.net
24: */
25: public class AlterTableAnalyzer extends BaseAnalyzer {
26:
27: public AlterTableAnalyzer(WbConnection conn, String statement,
28: int cursorPos) {
29: super (conn, statement, cursorPos);
30: }
31:
32: protected void checkContext() {
33: int addPos = -1;
34: int modifyPos = -1;
35: int tablePos = -1;
36: int tableEnd = -1;
37: int columnKeyWordPos = -1;
38: SQLLexer lexer = new SQLLexer(this .sql);
39:
40: try {
41: SQLToken token = lexer.getNextToken(false, false);
42: while (token != null) {
43: String v = token.getContents();
44: if ("TABLE".equalsIgnoreCase(v)) {
45: tablePos = token.getCharEnd();
46: } else if ("ADD".equalsIgnoreCase(v)) {
47: addPos = token.getCharEnd();
48: tableEnd = token.getCharBegin() - 1;
49: } else if ("MODIFY".equalsIgnoreCase(v)
50: || "DROP".equalsIgnoreCase(v)) {
51: modifyPos = token.getCharEnd();
52: tableEnd = token.getCharBegin() - 1;
53: } else if ("COLUMN".equalsIgnoreCase(v)) {
54: columnKeyWordPos = token.getCharEnd();
55: }
56: token = lexer.getNextToken(false, false);
57: }
58: } catch (Exception e) {
59: LogMgr.logError("AlterTableAnalyzer", "Error parsing SQL",
60: e);
61: }
62:
63: String q = this .getQualifierLeftOfCursor();
64: if (q != null) {
65: this .schemaForTableList = q;
66: } else {
67: this .schemaForTableList = this .dbConnection.getMetadata()
68: .getCurrentSchema();
69: }
70:
71: if (between(cursorPos, tablePos, modifyPos)
72: || between(cursorPos, tablePos, addPos)
73: || (modifyPos == -1 && addPos == -1)) {
74: context = CONTEXT_TABLE_LIST;
75: setTableTypeFilter(this .dbConnection.getMetadata()
76: .getTableTypeName());
77: } else if (modifyPos > -1 && tableEnd > -1) {
78: String table = this .sql.substring(tablePos, tableEnd)
79: .trim();
80: context = CONTEXT_COLUMN_LIST;
81: tableForColumnList = new TableIdentifier(table);
82: }
83:
84: }
85:
86: }
|