001: package net.sourceforge.squirrel_sql.plugins.codecompletion;
002:
003: import net.sourceforge.squirrel_sql.client.session.ISession;
004: import net.sourceforge.squirrel_sql.plugins.codecompletion.prefs.CodeCompletionPreferences;
005: import net.sourceforge.squirrel_sql.plugins.codecompletion.prefs.PrefixedConfig;
006:
007: import java.sql.SQLException;
008: import java.sql.ResultSet;
009: import java.sql.DatabaseMetaData;
010: import java.util.Vector;
011:
012: public class CodeCompletionStoredProcedureInfo extends
013: CodeCompletionInfo {
014: private String _procName;
015: private int _procType;
016: private ISession _session;
017: private CodeCompletionPlugin _plugin;
018: private String _catalog;
019: private String _schema;
020: private int _moveCarretBackCount = 0;
021:
022: private String _toString;
023:
024: private String _params = null;
025:
026: /**
027: * This Sessions prefs. Note it is just a reference. Contents change when user changes Session properties.
028: */
029: private CodeCompletionPreferences _prefs;
030:
031: public CodeCompletionStoredProcedureInfo(String procName,
032: int procType, ISession session,
033: CodeCompletionPlugin plugin, String catalog, String schema) {
034: _procName = procName;
035: _procType = procType;
036: _session = session;
037: _plugin = plugin;
038: _prefs = (CodeCompletionPreferences) _session.getPluginObject(
039: _plugin, CodeCompletionPlugin.PLUGIN_OBJECT_PREFS_KEY);
040: _catalog = catalog;
041: _schema = schema;
042:
043: _toString = _procName + " (SP)";
044:
045: }
046:
047: public String getCompareString() {
048: return _procName;
049: }
050:
051: public String getCompletionString() {
052: try {
053: String ret = "";
054:
055: int completionConfig = getCopmpletionConfig();
056:
057: _moveCarretBackCount = 0;
058:
059: String params = getParams();
060: switch (completionConfig) {
061: case CodeCompletionPreferences.CONFIG_SP_WITH_PARARMS:
062: ret = "{call " + _procName + "(" + params + ")}";
063: if (0 < params.length()) {
064: _moveCarretBackCount = params.length() + 2;
065: }
066: break;
067: case CodeCompletionPreferences.CONFIG_SP_WITHOUT_PARARMS:
068: ret = "{call " + _procName + "()}";
069: if (0 < params.length()) {
070: _moveCarretBackCount = 2;
071: }
072: break;
073: case CodeCompletionPreferences.CONFIG_UDF_WITH_PARARMS:
074: ret = _procName + "(" + params + ")";
075: if (0 < params.length()) {
076: _moveCarretBackCount = params.length() + 1;
077: }
078: break;
079: case CodeCompletionPreferences.CONFIG_UDF_WITHOUT_PARARMS:
080: if (0 < params.length()) {
081: _moveCarretBackCount = 1;
082: }
083: ret = _procName + "()";
084: break;
085: }
086:
087: return ret;
088: } catch (SQLException e) {
089: throw new RuntimeException(e);
090: }
091: }
092:
093: private int getCopmpletionConfig() {
094: PrefixedConfig[] prefixedConfigs = _prefs.getPrefixedConfigs();
095:
096: for (int i = 0; i < prefixedConfigs.length; i++) {
097: if (_procName.toUpperCase().startsWith(
098: prefixedConfigs[i].getPrefix().toUpperCase())) {
099: return prefixedConfigs[i].getCompletionConfig();
100: }
101: }
102:
103: return _prefs.getGeneralCompletionConfig();
104: }
105:
106: private String getParams() throws SQLException {
107: if (null == _params) {
108: ResultSet res = _session.getSQLConnection().getConnection()
109: .getMetaData().getProcedureColumns(_catalog,
110: _schema, _procName, null);
111:
112: Vector<String> ret = new Vector<String>();
113: while (res.next()) {
114: switch (res.getInt("COLUMN_TYPE")) {
115: case DatabaseMetaData.procedureColumnIn:
116: case DatabaseMetaData.procedureColumnOut:
117: case DatabaseMetaData.procedureColumnInOut:
118: ret.add(getParamString(res));
119: }
120: }
121: res.close();
122:
123: String[] _paramStrings = ret
124: .toArray(new String[ret.size()]);
125:
126: _params = "";
127: if (0 < _paramStrings.length) {
128: _params += _paramStrings[0];
129: }
130:
131: for (int i = 1; i < _paramStrings.length; i++) {
132: _params += ", " + _paramStrings[i];
133: }
134: }
135:
136: return _params;
137: }
138:
139: private String getParamString(ResultSet res) throws SQLException {
140: String ret = "<";
141:
142: switch (res.getInt("COLUMN_TYPE")) {
143: case DatabaseMetaData.procedureColumnIn:
144: ret += "IN ";
145: break;
146: case DatabaseMetaData.procedureColumnOut:
147: ret += "OUT ";
148: break;
149: case DatabaseMetaData.procedureColumnInOut:
150: ret += "INOUT ";
151: break;
152: }
153:
154: ret += res.getString("TYPE_NAME") + " ";
155:
156: ret += res.getString("COLUMN_NAME");
157:
158: String remarks = res.getString("REMARKS");
159:
160: if (null != remarks) {
161: ret += " " + remarks.replace('\n', ' ');
162: }
163:
164: ret += ">";
165:
166: return ret;
167: }
168:
169: public String toString() {
170: return _toString;
171: }
172:
173: /**
174: * Will be called after getCompletionString()
175: * @return Position to move the carret back counted from the end of the completion string.
176: */
177: public int getMoveCarretBackCount() {
178: return _moveCarretBackCount;
179: }
180:
181: /**
182: * @return the _procType
183: */
184: public int getProcType() {
185: return _procType;
186: }
187:
188: /**
189: * @param type the _procType to set
190: */
191: public void setProcType(int type) {
192: _procType = type;
193: }
194: }
|