KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > GTSQLTypeInfoDataSource


1 /**
2  * com.mckoi.database.GTSQLTypeInfoDataSource 23 Mar 2002
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database;
26
27 import java.util.ArrayList JavaDoc;
28 import com.mckoi.util.BigNumber;
29 import com.mckoi.database.global.SQLTypes;
30
31 /**
32  * A GTDataSource that models all SQL types that are available.
33  * <p>
34  * NOTE: This is not designed to be a long kept object. It must not last
35  * beyond the lifetime of a transaction.
36  *
37  * @author Tobias Downer
38  */

39
40 public class GTSQLTypeInfoDataSource extends GTDataSource {
41
42   /**
43    * The DatabaseConnection object. Currently this is not used, but it may
44    * be needed in the future if user-defined SQL types are supported.
45    */

46   private DatabaseConnection database;
47
48   /**
49    * The list of info keys/values in this object.
50    */

51   private ArrayList JavaDoc key_value_pairs;
52
53   /**
54    * Constant for type_nullable types.
55    */

56   private static final BigNumber TYPE_NULLABLE =
57                     BigNumber.fromInt(java.sql.DatabaseMetaData.typeNullable);
58   
59   /**
60    * Constructor.
61    */

62   public GTSQLTypeInfoDataSource(DatabaseConnection connection) {
63     super(connection.getSystem());
64     this.database = connection;
65     this.key_value_pairs = new ArrayList JavaDoc();
66   }
67
68   /**
69    * Adds a type description.
70    */

71   private void addType(String JavaDoc name, int type, int precision,
72                        String JavaDoc prefix, String JavaDoc suffix, String JavaDoc oops,
73                        boolean searchable) {
74     key_value_pairs.add(name);
75     key_value_pairs.add(BigNumber.fromLong(type));
76     key_value_pairs.add(BigNumber.fromLong(precision));
77     key_value_pairs.add(prefix);
78     key_value_pairs.add(suffix);
79     key_value_pairs.add(searchable ? BigNumber.fromLong(3) :
80                                      BigNumber.fromLong(0));
81   }
82
83   /**
84    * Initialize the data source.
85    */

86   public GTSQLTypeInfoDataSource init() {
87
88     addType("BIT", SQLTypes.BIT, 1, null, null, null, true);
89     addType("BOOLEAN", SQLTypes.BIT, 1, null, null, null, true);
90     addType("TINYINT", SQLTypes.TINYINT, 9, null, null, null, true);
91     addType("SMALLINT", SQLTypes.SMALLINT, 9, null, null, null, true);
92     addType("INTEGER", SQLTypes.INTEGER, 9, null, null, null, true);
93     addType("BIGINT", SQLTypes.BIGINT, 9, null, null, null, true);
94     addType("FLOAT", SQLTypes.FLOAT, 9, null, null, null, true);
95     addType("REAL", SQLTypes.REAL, 9, null, null, null, true);
96     addType("DOUBLE", SQLTypes.DOUBLE, 9, null, null, null, true);
97     addType("NUMERIC", SQLTypes.NUMERIC, 9, null, null, null, true);
98     addType("DECIMAL", SQLTypes.DECIMAL, 9, null, null, null, true);
99     addType("CHAR", SQLTypes.CHAR, 9, "'", "'", null, true);
100     addType("VARCHAR", SQLTypes.VARCHAR, 9, "'", "'", null, true);
101     addType("LONGVARCHAR", SQLTypes.LONGVARCHAR, 9, "'", "'", null, 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, true);
105     addType("BINARY", SQLTypes.BINARY, 9, null, null, null, false);
106     addType("VARBINARY", SQLTypes.VARBINARY, 9, null, null, null, false);
107     addType("LONGVARBINARY", SQLTypes.LONGVARBINARY,
108                              9, null, null, null, false);
109     addType("JAVA_OBJECT", SQLTypes.JAVA_OBJECT, 9, null, null, null, false);
110
111     return this;
112   }
113
114   // ---------- Implemented from GTDataSource ----------
115

116   public DataTableDef getDataTableDef() {
117     return DEF_DATA_TABLE_DEF;
118   }
119
120   public int getRowCount() {
121     return key_value_pairs.size() / 6;
122   }
123
124   public TObject getCellContents(final int column, final int row) {
125     int i = (row * 6);
126     switch (column) {
127       case 0: // type_name
128
return columnValue(column, (String JavaDoc) key_value_pairs.get(i));
129       case 1: // data_type
130
return columnValue(column, (BigNumber) key_value_pairs.get(i + 1));
131       case 2: // precision
132
return columnValue(column, (BigNumber) key_value_pairs.get(i + 2));
133       case 3: // literal_prefix
134
return columnValue(column, (String JavaDoc) key_value_pairs.get(i + 3));
135       case 4: // literal_suffix
136
return columnValue(column, (String JavaDoc) key_value_pairs.get(i + 4));
137       case 5: // create_params
138
return columnValue(column, null);
139       case 6: // nullable
140
return columnValue(column, TYPE_NULLABLE);
141       case 7: // case_sensitive
142
return columnValue(column, Boolean.TRUE);
143       case 8: // searchable
144
return columnValue(column, (BigNumber) key_value_pairs.get(i + 5));
145       case 9: // unsigned_attribute
146
return columnValue(column, Boolean.FALSE);
147       case 10: // fixed_prec_scale
148
return columnValue(column, Boolean.FALSE);
149       case 11: // auto_increment
150
return columnValue(column, Boolean.FALSE);
151       case 12: // local_type_name
152
return columnValue(column, null);
153       case 13: // minimum_scale
154
return columnValue(column, BigNumber.fromLong(0));
155       case 14: // maximum_scale
156
return columnValue(column, BigNumber.fromLong(10000000));
157       case 15: // sql_data_type
158
return columnValue(column, null);
159       case 16: // sql_datetype_sub
160
return columnValue(column, null);
161       case 17: // num_prec_radix
162
return columnValue(column, BigNumber.fromLong(10));
163       default:
164         throw new Error JavaDoc("Column out of bounds.");
165     }
166   }
167
168   // ---------- Overwritten from GTDataSource ----------
169

170   public void dispose() {
171     super.dispose();
172     key_value_pairs = null;
173     database = null;
174   }
175
176   // ---------- Static ----------
177

178   /**
179    * The data table def that describes this table of data source.
180    */

181   static final DataTableDef DEF_DATA_TABLE_DEF;
182
183   static {
184
185     DataTableDef def = new DataTableDef();
186     def.setTableName(
187              new TableName(Database.SYSTEM_SCHEMA, "sUSRSQLTypeInfo"));
188
189     // Add column definitions
190
def.addColumn( stringColumn("TYPE_NAME"));
191     def.addColumn(numericColumn("DATA_TYPE"));
192     def.addColumn(numericColumn("PRECISION"));
193     def.addColumn( stringColumn("LITERAL_PREFIX"));
194     def.addColumn( stringColumn("LITERAL_SUFFIX"));
195     def.addColumn( stringColumn("CREATE_PARAMS"));
196     def.addColumn(numericColumn("NULLABLE"));
197     def.addColumn(booleanColumn("CASE_SENSITIVE"));
198     def.addColumn(numericColumn("SEARCHABLE"));
199     def.addColumn(booleanColumn("UNSIGNED_ATTRIBUTE"));
200     def.addColumn(booleanColumn("FIXED_PREC_SCALE"));
201     def.addColumn(booleanColumn("AUTO_INCREMENT"));
202     def.addColumn( stringColumn("LOCAL_TYPE_NAME"));
203     def.addColumn(numericColumn("MINIMUM_SCALE"));
204     def.addColumn(numericColumn("MAXIMUM_SCALE"));
205     def.addColumn( stringColumn("SQL_DATA_TYPE"));
206     def.addColumn( stringColumn("SQL_DATETIME_SUB"));
207     def.addColumn(numericColumn("NUM_PREC_RADIX"));
208
209     // Set to immutable
210
def.setImmutable();
211
212     DEF_DATA_TABLE_DEF = def;
213
214   }
215
216 }
217
Popular Tags