001: /*
002: * Copyright (C) 2003 Gerd Wagner
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: */
018: package net.sourceforge.squirrel_sql.plugins.codecompletion;
019:
020: import java.sql.SQLException;
021: import java.util.ArrayList;
022: import java.util.HashSet;
023:
024: import net.sourceforge.squirrel_sql.client.session.ExtendedColumnInfo;
025:
026: public class CodeCompletionTableInfo extends CodeCompletionInfo {
027: private String _tableName;
028: private String _tableType;
029: private ArrayList<CodeCompletionInfo> _colInfos;
030: String _toString;
031: private String _catalog;
032: private String _schema;
033:
034: public CodeCompletionTableInfo(String tableName, String tableType,
035: String catalog, String schema) {
036: _tableName = tableName;
037: _tableType = tableType;
038: _catalog = catalog;
039: _schema = schema;
040:
041: if (null != _tableType && !"TABLE".equals(_tableType)) {
042: _toString = _tableName + " (" + _tableType + ")";
043: } else {
044: _toString = _tableName;
045: }
046: }
047:
048: void setHasDuplicateNameInDfifferentSchemas() {
049:
050: String tabNameWithSchemaHint = _tableName
051: + (null == _catalog ? "" : " catalog=" + _catalog)
052: + (null == _schema ? "" : " schema=" + _schema);
053:
054: if (null != _tableType && !"TABLE".equals(_tableType)) {
055: _toString = tabNameWithSchemaHint + " (" + _tableType + ")";
056: } else {
057: _toString = tabNameWithSchemaHint;
058: }
059:
060: }
061:
062: public String getCompareString() {
063: return _tableName;
064: }
065:
066: public ArrayList<CodeCompletionInfo> getColumns(
067: net.sourceforge.squirrel_sql.client.session.schemainfo.SchemaInfo schemaInfo,
068: String colNamePattern) throws SQLException {
069: if (null == _colInfos) {
070: ExtendedColumnInfo[] schemColInfos = schemaInfo
071: .getExtendedColumnInfos(_catalog, _schema,
072: _tableName);
073:
074: ArrayList<CodeCompletionInfo> colInfosBuf = new ArrayList<CodeCompletionInfo>();
075: HashSet<String> uniqCols = new HashSet<String>();
076: for (int i = 0; i < schemColInfos.length; i++) {
077: if ((null == _catalog || ("" + _catalog).equals(""
078: + schemColInfos[i].getCatalog()))
079: && (null == _schema || ("" + _schema).equals(""
080: + schemColInfos[i].getSchema()))) {
081: String columnName = schemColInfos[i]
082: .getColumnName();
083: String columnType = schemColInfos[i]
084: .getColumnType();
085: int columnSize = schemColInfos[i].getColumnSize();
086: int decimalDigits = schemColInfos[i]
087: .getDecimalDigits();
088: boolean nullable = schemColInfos[i].isNullable();
089: CodeCompletionColumnInfo buf = new CodeCompletionColumnInfo(
090: columnName, columnType, columnSize,
091: decimalDigits, nullable);
092: String bufStr = buf.toString();
093: if (!uniqCols.contains(bufStr)) {
094: uniqCols.add(bufStr);
095: colInfosBuf.add(buf);
096: }
097: }
098: }
099:
100: _colInfos = colInfosBuf;
101: }
102:
103: String upperCaseColNamePattern = colNamePattern.toUpperCase()
104: .trim();
105:
106: if ("".equals(upperCaseColNamePattern)) {
107: return _colInfos;
108: }
109:
110: ArrayList<CodeCompletionInfo> ret = new ArrayList<CodeCompletionInfo>();
111:
112: for (CodeCompletionInfo colInfo : _colInfos) {
113: if (colInfo
114: .upperCaseCompletionStringStartsWith(upperCaseColNamePattern)) {
115: ret.add(colInfo);
116: }
117:
118: }
119:
120: return ret;
121: }
122:
123: public boolean hasColumns() {
124: return true;
125: }
126:
127: public String toString() {
128: return _toString;
129: }
130: }
|