01: /*
02: * DdlAnalyzer.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.WbConnection;
15: import workbench.sql.formatter.SQLLexer;
16: import workbench.sql.formatter.SQLToken;
17:
18: /**
19: * Analyze a DDL statement regarding the context for the auto-completion
20: * @author support@sql-workbench.net
21: */
22: public class DdlAnalyzer extends BaseAnalyzer {
23: public DdlAnalyzer(WbConnection conn, String statement,
24: int cursorPos) {
25: super (conn, statement, cursorPos);
26: }
27:
28: protected void checkContext() {
29: SQLLexer lexer = new SQLLexer(this .sql);
30: SQLToken verbToken = lexer.getNextToken(false, false);
31: if (verbToken == null) {
32: this .context = NO_CONTEXT;
33: return;
34: }
35:
36: String verb = verbToken.getContents();
37:
38: if ("TRUNCATE".equalsIgnoreCase(verb)) {
39: context = CONTEXT_TABLE_LIST;
40: return;
41: }
42:
43: SQLToken typeToken = lexer.getNextToken(false, false);
44: String type = (typeToken != null ? typeToken.getContents()
45: : null);
46:
47: String q = this .getQualifierLeftOfCursor();
48: if (q != null) {
49: this .schemaForTableList = q;
50: } else {
51: this .schemaForTableList = this .dbConnection.getMetadata()
52: .getCurrentSchema();
53: }
54:
55: if ("DROP".equals(verb)) {
56: if (type == null
57: || between(cursorPos, verbToken.getCharEnd(),
58: typeToken.getCharBegin())) {
59: context = CONTEXT_KW_LIST;
60: this .keywordFile = "drop_types.txt";
61: }
62:
63: // for DROP etc, we'll need to be after the table keyword
64: // otherwise it could be a DROP PROCEDURE as well.
65: if ("TABLE".equals(type)
66: && cursorPos >= typeToken.getCharEnd()) {
67: context = CONTEXT_TABLE_LIST;
68: setTableTypeFilter(this .dbConnection.getMetadata()
69: .getTableTypeName());
70: } else if ("VIEW".equals(type)
71: && cursorPos >= typeToken.getCharEnd()) {
72: context = CONTEXT_TABLE_LIST;
73: setTableTypeFilter(this.dbConnection.getMetadata()
74: .getViewTypeName());
75: }
76: } else {
77: context = NO_CONTEXT;
78: }
79:
80: }
81:
82: }
|