001: /*
002: * WbCommandAnalyzer.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.ArrayList;
015: import java.util.Collections;
016: import java.util.List;
017: import workbench.db.ConnectionMgr;
018: import workbench.db.ConnectionProfile;
019: import workbench.db.WbConnection;
020: import workbench.sql.CommandMapper;
021: import workbench.sql.SqlCommand;
022: import workbench.util.ArgumentParser;
023: import workbench.util.ArgumentType;
024: import workbench.util.SqlUtil;
025: import workbench.util.StringUtil;
026:
027: /**
028: * @author support@sql-workbench.net
029: */
030: public class WbCommandAnalyzer extends BaseAnalyzer {
031: // True if the the parameters are put into the
032: // elements list. This is used by the CompletionPopup
033: // to check if the selected value should be enhanced with - and =
034: private boolean isParameter;
035:
036: public WbCommandAnalyzer(WbConnection conn, String statement,
037: int cursorPos) {
038: super (conn, statement, cursorPos);
039: }
040:
041: public boolean isWbParam() {
042: return this .isParameter;
043: }
044:
045: public char quoteCharForValue(String value) {
046: if (this .isParameter)
047: return 0;
048: if (value.indexOf('-') > -1 || value.indexOf(' ') > -1) {
049: if (value.indexOf('\'') > -1)
050: return '"';
051: else
052: return '\'';
053: }
054: return 0;
055: }
056:
057: public void checkContext() {
058: CommandMapper mapper = new CommandMapper();
059: SqlCommand cmd = mapper.getCommandToUse(this .sql);
060: ArgumentParser p = cmd.getArgumentParser();
061: if (p == null) {
062: this .context = NO_CONTEXT;
063: this .elements = null;
064: return;
065: }
066:
067: this .context = CONTEXT_WB_PARAMS;
068:
069: String parameter = getCurrentParameter();
070: this .isParameter = false;
071:
072: if (p.isRegistered(parameter)) {
073: ArgumentType type = p.getArgumentType(parameter);
074: if (type == ArgumentType.BoolArgument) {
075: this .elements = new ArrayList(2);
076: this .elements.add("true");
077: this .elements.add("false");
078: } else if (type == ArgumentType.TableArgument) {
079: this .context = CONTEXT_TABLE_LIST;
080: this .schemaForTableList = this .dbConnection
081: .getCurrentSchema();
082: } else if (type == ArgumentType.ListArgument) {
083: this .elements = new ArrayList(p
084: .getAllowedValues(parameter));
085: } else if (type == ArgumentType.ProfileArgument) {
086: List<ConnectionProfile> profiles = ConnectionMgr
087: .getInstance().getProfiles();
088: this .elements = new ArrayList(profiles.size());
089: for (ConnectionProfile profile : profiles) {
090: this .elements.add(profile.getKey().toString());
091: }
092: Collections.sort(this .elements);
093: } else {
094: this .context = NO_CONTEXT;
095: this .elements = null;
096: }
097: } else {
098: this .elements = p.getRegisteredArguments();
099: String params = SqlUtil.stripVerb(this .sql);
100: p.parse(params);
101: List argsPresent = p.getArgumentsOnCommandLine();
102: this .elements.removeAll(argsPresent);
103: Collections.sort(this .elements);
104: isParameter = p.needsSwitch();
105: }
106: }
107:
108: /**
109: * Returns the name of the parameter where the cursor is currently located.
110: * If the previous non-whitespace character left of the cursor is the equal
111: * sign, then this is assumed to be the "current parameter" and the
112: * corresponding string is returned.
113: * Otherwise it is assumed that the cursor is "between" two parameters
114: * and the list of available parameters should be displayed.
115: *
116: * @return the value of the current parameter or null if no parameter was found
117: */
118: protected String getCurrentParameter() {
119: if (cursorPos > 1 && cursorPos <= this .sql.length()) {
120: char c = this .sql.charAt(cursorPos - 1);
121: if (Character.isWhitespace(c))
122: return null;
123: }
124:
125: String word = StringUtil.getWordLeftOfCursor(this .sql,
126: this .cursorPos, " \t");
127: if (word == null)
128: return null;
129: if (word.startsWith("-") && word.length() > 2) {
130: int end = word.indexOf('=');
131: if (end == -1) {
132: end = word.length() - 1;
133: }
134: return word.substring(1, end);
135: }
136: return null;
137: }
138:
139: }
|