001: /**
002: * com.mckoi.database.GTSQLTypeInfoDataSource 23 Mar 2002
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.database;
024:
025: import java.util.ArrayList;
026: import com.mckoi.util.BigNumber;
027: import com.mckoi.database.global.SQLTypes;
028:
029: /**
030: * A GTDataSource that models all SQL types that are available.
031: * <p>
032: * NOTE: This is not designed to be a long kept object. It must not last
033: * beyond the lifetime of a transaction.
034: *
035: * @author Tobias Downer
036: */
037:
038: public class GTSQLTypeInfoDataSource extends GTDataSource {
039:
040: /**
041: * The DatabaseConnection object. Currently this is not used, but it may
042: * be needed in the future if user-defined SQL types are supported.
043: */
044: private DatabaseConnection database;
045:
046: /**
047: * The list of info keys/values in this object.
048: */
049: private ArrayList key_value_pairs;
050:
051: /**
052: * Constant for type_nullable types.
053: */
054: private static final BigNumber TYPE_NULLABLE = BigNumber
055: .fromInt(java.sql.DatabaseMetaData.typeNullable);
056:
057: /**
058: * Constructor.
059: */
060: public GTSQLTypeInfoDataSource(DatabaseConnection connection) {
061: super (connection.getSystem());
062: this .database = connection;
063: this .key_value_pairs = new ArrayList();
064: }
065:
066: /**
067: * Adds a type description.
068: */
069: private void addType(String name, int type, int precision,
070: String prefix, String suffix, String oops,
071: boolean searchable) {
072: key_value_pairs.add(name);
073: key_value_pairs.add(BigNumber.fromLong(type));
074: key_value_pairs.add(BigNumber.fromLong(precision));
075: key_value_pairs.add(prefix);
076: key_value_pairs.add(suffix);
077: key_value_pairs.add(searchable ? BigNumber.fromLong(3)
078: : BigNumber.fromLong(0));
079: }
080:
081: /**
082: * Initialize the data source.
083: */
084: public GTSQLTypeInfoDataSource init() {
085:
086: addType("BIT", SQLTypes.BIT, 1, null, null, null, true);
087: addType("BOOLEAN", SQLTypes.BIT, 1, null, null, null, true);
088: addType("TINYINT", SQLTypes.TINYINT, 9, null, null, null, true);
089: addType("SMALLINT", SQLTypes.SMALLINT, 9, null, null, null,
090: true);
091: addType("INTEGER", SQLTypes.INTEGER, 9, null, null, null, true);
092: addType("BIGINT", SQLTypes.BIGINT, 9, null, null, null, true);
093: addType("FLOAT", SQLTypes.FLOAT, 9, null, null, null, true);
094: addType("REAL", SQLTypes.REAL, 9, null, null, null, true);
095: addType("DOUBLE", SQLTypes.DOUBLE, 9, null, null, null, true);
096: addType("NUMERIC", SQLTypes.NUMERIC, 9, null, null, null, true);
097: addType("DECIMAL", SQLTypes.DECIMAL, 9, null, null, null, true);
098: addType("CHAR", SQLTypes.CHAR, 9, "'", "'", null, true);
099: addType("VARCHAR", SQLTypes.VARCHAR, 9, "'", "'", null, true);
100: addType("LONGVARCHAR", SQLTypes.LONGVARCHAR, 9, "'", "'", null,
101: true);
102: addType("DATE", SQLTypes.DATE, 9, null, null, null, true);
103: addType("TIME", SQLTypes.TIME, 9, null, null, null, true);
104: addType("TIMESTAMP", SQLTypes.TIMESTAMP, 9, null, null, null,
105: true);
106: addType("BINARY", SQLTypes.BINARY, 9, null, null, null, false);
107: addType("VARBINARY", SQLTypes.VARBINARY, 9, null, null, null,
108: false);
109: addType("LONGVARBINARY", SQLTypes.LONGVARBINARY, 9, null, null,
110: null, false);
111: addType("JAVA_OBJECT", SQLTypes.JAVA_OBJECT, 9, null, null,
112: null, false);
113:
114: return this ;
115: }
116:
117: // ---------- Implemented from GTDataSource ----------
118:
119: public DataTableDef getDataTableDef() {
120: return DEF_DATA_TABLE_DEF;
121: }
122:
123: public int getRowCount() {
124: return key_value_pairs.size() / 6;
125: }
126:
127: public TObject getCellContents(final int column, final int row) {
128: int i = (row * 6);
129: switch (column) {
130: case 0: // type_name
131: return columnValue(column, (String) key_value_pairs.get(i));
132: case 1: // data_type
133: return columnValue(column, (BigNumber) key_value_pairs
134: .get(i + 1));
135: case 2: // precision
136: return columnValue(column, (BigNumber) key_value_pairs
137: .get(i + 2));
138: case 3: // literal_prefix
139: return columnValue(column, (String) key_value_pairs
140: .get(i + 3));
141: case 4: // literal_suffix
142: return columnValue(column, (String) key_value_pairs
143: .get(i + 4));
144: case 5: // create_params
145: return columnValue(column, null);
146: case 6: // nullable
147: return columnValue(column, TYPE_NULLABLE);
148: case 7: // case_sensitive
149: return columnValue(column, Boolean.TRUE);
150: case 8: // searchable
151: return columnValue(column, (BigNumber) key_value_pairs
152: .get(i + 5));
153: case 9: // unsigned_attribute
154: return columnValue(column, Boolean.FALSE);
155: case 10: // fixed_prec_scale
156: return columnValue(column, Boolean.FALSE);
157: case 11: // auto_increment
158: return columnValue(column, Boolean.FALSE);
159: case 12: // local_type_name
160: return columnValue(column, null);
161: case 13: // minimum_scale
162: return columnValue(column, BigNumber.fromLong(0));
163: case 14: // maximum_scale
164: return columnValue(column, BigNumber.fromLong(10000000));
165: case 15: // sql_data_type
166: return columnValue(column, null);
167: case 16: // sql_datetype_sub
168: return columnValue(column, null);
169: case 17: // num_prec_radix
170: return columnValue(column, BigNumber.fromLong(10));
171: default:
172: throw new Error("Column out of bounds.");
173: }
174: }
175:
176: // ---------- Overwritten from GTDataSource ----------
177:
178: public void dispose() {
179: super .dispose();
180: key_value_pairs = null;
181: database = null;
182: }
183:
184: // ---------- Static ----------
185:
186: /**
187: * The data table def that describes this table of data source.
188: */
189: static final DataTableDef DEF_DATA_TABLE_DEF;
190:
191: static {
192:
193: DataTableDef def = new DataTableDef();
194: def.setTableName(new TableName(Database.SYSTEM_SCHEMA,
195: "sUSRSQLTypeInfo"));
196:
197: // Add column definitions
198: def.addColumn(stringColumn("TYPE_NAME"));
199: def.addColumn(numericColumn("DATA_TYPE"));
200: def.addColumn(numericColumn("PRECISION"));
201: def.addColumn(stringColumn("LITERAL_PREFIX"));
202: def.addColumn(stringColumn("LITERAL_SUFFIX"));
203: def.addColumn(stringColumn("CREATE_PARAMS"));
204: def.addColumn(numericColumn("NULLABLE"));
205: def.addColumn(booleanColumn("CASE_SENSITIVE"));
206: def.addColumn(numericColumn("SEARCHABLE"));
207: def.addColumn(booleanColumn("UNSIGNED_ATTRIBUTE"));
208: def.addColumn(booleanColumn("FIXED_PREC_SCALE"));
209: def.addColumn(booleanColumn("AUTO_INCREMENT"));
210: def.addColumn(stringColumn("LOCAL_TYPE_NAME"));
211: def.addColumn(numericColumn("MINIMUM_SCALE"));
212: def.addColumn(numericColumn("MAXIMUM_SCALE"));
213: def.addColumn(stringColumn("SQL_DATA_TYPE"));
214: def.addColumn(stringColumn("SQL_DATETIME_SUB"));
215: def.addColumn(numericColumn("NUM_PREC_RADIX"));
216:
217: // Set to immutable
218: def.setImmutable();
219:
220: DEF_DATA_TABLE_DEF = def;
221:
222: }
223:
224: }
|