001: /*
002: * CreateAnalyzer.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 workbench.db.TableIdentifier;
015: import workbench.db.WbConnection;
016: import workbench.log.LogMgr;
017: import workbench.sql.formatter.SQLLexer;
018: import workbench.sql.formatter.SQLToken;
019: import workbench.util.StringUtil;
020:
021: /**
022: * Analyze a CREATE INDEX statement to provide completion for tables and columns
023: * @author support@sql-workbench.net
024: */
025: public class CreateAnalyzer extends BaseAnalyzer {
026:
027: public CreateAnalyzer(WbConnection conn, String statement,
028: int cursorPos) {
029: super (conn, statement, cursorPos);
030: }
031:
032: protected void checkContext() {
033: SQLLexer lexer = new SQLLexer(this .sql);
034:
035: boolean isCreateIndex = false;
036: boolean showColumns = false;
037: boolean showTables = false;
038: int tableStartPos = -1;
039: int tableEndPos = -1;
040: int tokenCount = 0;
041: boolean afterCreate = true;
042:
043: try {
044: SQLToken token = lexer.getNextToken(false, false);
045: while (token != null) {
046: final String t = token.getContents();
047: tokenCount++;
048: if (tokenCount == 2) {
049: afterCreate = (token.getCharBegin() > this .cursorPos);
050: }
051:
052: if (isCreateIndex) {
053: if ("ON".equalsIgnoreCase(t)) {
054: if (this .cursorPos > token.getCharEnd()) {
055: showTables = true;
056: showColumns = false;
057: }
058: tableStartPos = token.getCharEnd();
059: } else if ("(".equals(t)) {
060: tableEndPos = token.getCharBegin() - 1;
061: if (this .cursorPos >= token.getCharBegin()) {
062: showTables = false;
063: showColumns = true;
064: }
065: } else if (")".equals(t)) {
066: if (this .cursorPos > token.getCharBegin()) {
067: showTables = false;
068: showColumns = false;
069: }
070: }
071: } else if ("INDEX".equalsIgnoreCase(t)) {
072: isCreateIndex = true;
073: }
074:
075: token = lexer.getNextToken(false, false);
076: }
077: } catch (Exception e) {
078: LogMgr.logError("AlterTableAnalyzer", "Error parsing SQL",
079: e);
080: }
081:
082: if (showTables) {
083: context = CONTEXT_TABLE_LIST;
084: String q = getQualifierLeftOfCursor();
085: if (StringUtil.isEmptyString(q)) {
086: this .schemaForTableList = this .dbConnection
087: .getMetadata().getCurrentSchema();
088: } else {
089: this .schemaForTableList = q;
090: }
091: } else if (showColumns) {
092: context = CONTEXT_COLUMN_LIST;
093: if (tableEndPos == -1)
094: tableEndPos = this .sql.length() - 1;
095: String table = this .sql.substring(tableStartPos,
096: tableEndPos).trim();
097: this .tableForColumnList = new TableIdentifier(table);
098: } else if (afterCreate) {
099: context = CONTEXT_KW_LIST;
100: this .keywordFile = "create_types.txt";
101: }
102: }
103:
104: }
|