KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > jdbc > typing > DBMSInfo


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  * Copyright (C) 2003 XQuark Group.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
19  * You can also get it at http://www.gnu.org/licenses/lgpl.html
20  *
21  * For more information on this software, see http://www.xquark.org.
22  */

23
24 /**
25  * Created on 15 nov. 2002
26  *
27  * To change this generated comment edit the template variable "filecomment":
28  * Window>Preferences>Java>Templates.
29  * To enable and disable the creation of file comments go to
30  * Window>Preferences>Java>Code Generation.
31  */

32 package org.xquark.jdbc.typing;
33
34 import java.sql.*;
35 import java.sql.DatabaseMetaData JavaDoc;
36 import java.sql.ResultSet JavaDoc;
37 import java.sql.SQLException JavaDoc;
38 import java.util.*;
39
40 /**
41  * Object containing DBMS specific information
42  *
43  */

44 public class DBMSInfo
45 {
46     private static final String JavaDoc RCSRevision = "$Revision: 1.2 $";
47     private static final String JavaDoc RCSName = "$Name: $";
48
49     private short dbms;
50     private int systemCollectionsDataLength = 255;
51     private int maxUserCollectionsDataLength = 255; // indexable size
52
private String JavaDoc rowId = null;
53     private boolean useDoubleQuotes4DDLNames = false;
54     private SQLStatementCustomizer selectLockStatement = null;
55     private SQLStatementCustomizer temporaryTableStatement = null;
56     private SQLStatementCustomizer indexOrganizedStatement = null;
57     private TypeMap typeMap = null;
58
59     /**
60      * Returns the typeMap.
61      * @return TypeMap
62      */

63     public TypeMap getTypeMap()
64     {
65         return typeMap;
66     }
67
68     /**
69      * Returns the dbms.
70      * @return short
71      */

72     public short getDbms()
73     {
74         return dbms;
75     }
76
77     /**
78      * Returns the selectLockStatement.
79      * @return String
80      */

81     public SQLStatementCustomizer getSelectLockStatement()
82     {
83         return selectLockStatement;
84     }
85
86     /**
87      * Returns the maxUserCollectionsDataLength.
88      * @return int
89      */

90     public int getMaxUserCollectionsDataLength()
91     {
92         return maxUserCollectionsDataLength;
93     }
94
95     /**
96      * Returns the useDoubleQuotesForDatabaseObjects.
97      * @return boolean
98      */

99     public boolean useDoubleQuotes4DDLNames()
100     {
101         return useDoubleQuotes4DDLNames;
102     }
103
104     /**
105      * Returns the rowId.
106      * @return String
107      */

108     public String JavaDoc getRowId()
109     {
110         return rowId;
111     }
112
113     /**
114      * Returns the systemCollectionsDataLength.
115      * @return int
116      */

117     public int getSystemCollectionsDataLength()
118     {
119         return systemCollectionsDataLength;
120     }
121
122     /**
123      * Sets the typeMap.
124      * @param typeMap The typeMap to set
125      */

126     public void setTypeMap(TypeMap typeMap)
127     {
128         this.typeMap = typeMap;
129     }
130
131     /**
132      * Sets the dbms.
133      * @param dbms The dbms to set
134      */

135     public void setDbms(short dbms)
136     {
137         this.dbms = dbms;
138     }
139
140     /**
141      * Sets the selectLockStatement.
142      * @param selectLockStatement The selectLockStatement to set
143      */

144     public void setSelectLockStatement(SQLStatementCustomizer selectLockStatement)
145     {
146         this.selectLockStatement = selectLockStatement;
147     }
148
149     /**
150      * Sets the maxUserCollectionsDataLength.
151      * @param maxUserCollectionsDataLength The maxUserCollectionsDataLength to set
152      */

153     public void setMaxUserCollectionsDataLength(int maxUserCollectionsDataLength)
154     {
155         this.maxUserCollectionsDataLength = maxUserCollectionsDataLength;
156     }
157
158     /**
159      * Sets the useDoubleQuotesForDatabaseObjects.
160      * @param useDoubleQuotesForDatabaseObjects The useDoubleQuotesForDatabaseObjects to set
161      */

162     public void setUseDoubleQuotes4DDLNames(boolean useDoubleQuotesForDatabaseObjects)
163     {
164         this.useDoubleQuotes4DDLNames = useDoubleQuotesForDatabaseObjects;
165     }
166
167     /**
168      * Sets the rowId.
169      * @param rowId The rowId to set
170      */

171     public void setRowId(String JavaDoc rowId)
172     {
173         this.rowId = rowId;
174     }
175
176     /**
177      * Sets the systemCollectionsDataLength.
178      * @param systemCollectionsDataLength The systemCollectionsDataLength to set
179      */

180     public void setSystemCollectionsDataLength(int systemCollectionsDataLength)
181     {
182         this.systemCollectionsDataLength = systemCollectionsDataLength;
183     }
184
185     /**
186      * Returns the indexOrganizedStatement.
187      * @return SQLStatementCustomizer
188      */

189     public SQLStatementCustomizer getIndexOrganizedStatement()
190     {
191         return indexOrganizedStatement;
192     }
193
194     /**
195      * Returns the temporaryTableStatement.
196      * @return SQLStatementCustomizer
197      */

198     public SQLStatementCustomizer getTemporaryTableStatement()
199     {
200         return temporaryTableStatement;
201     }
202
203     /**
204      * Sets the indexOrganizedStatement.
205      * @param indexOrganizedStatement The indexOrganizedStatement to set
206      */

207     public void setIndexOrganizedStatement(SQLStatementCustomizer indexOrganizedHint)
208     {
209         this.indexOrganizedStatement = indexOrganizedHint;
210     }
211
212     /**
213      * Sets the temporaryTableStatement.
214      * @param temporaryTableStatement The temporaryTableStatement to set
215      */

216     public void setTemporaryTableStatement(SQLStatementCustomizer temporaryTableHint)
217     {
218         this.temporaryTableStatement = temporaryTableHint;
219     }
220     
221     /**
222      * Retrieves org.xquark.repository.mapping.ColumnMetadata from the
223      * underlying database.
224      * @param tableName The table name
225      * @throws SQLException If a database error occurs while creation
226      */

227     public List getTableMetadata(String JavaDoc catalogName, String JavaDoc schemaName,
228                                     String JavaDoc tableName, Connection connection,
229                                         JDBCProperties properties)
230     throws SQLException JavaDoc {
231         if (properties.useResultSet4ColumnMetadata())
232             return getTableMetadataUsingRSMetadata(catalogName, schemaName,
233                     tableName, connection, properties);
234         else
235             return getTableMetadataUsingDBMetadata(catalogName, schemaName,
236                     tableName, connection, properties);
237     }
238
239     private List getTableMetadataUsingDBMetadata(String JavaDoc catalogName, String JavaDoc schemaName,
240                             String JavaDoc tableName, Connection connection, JDBCProperties properties)
241     throws SQLException JavaDoc {
242         List columns = new ArrayList();
243         DatabaseMetaData JavaDoc metadata = connection.getMetaData();
244         ResultSet JavaDoc rs = metadata.getColumns(catalogName, schemaName, tableName, null);
245         try {
246             while (rs.next()) {
247                 columns.add(new ColumnMetaDataImpl(rs, getTypeMap(), properties));
248             }
249         } finally {
250             rs.close();
251         }
252         return columns;
253     }
254
255     private List getTableMetadataUsingRSMetadata(String JavaDoc catalogName, String JavaDoc schemaName,
256                             String JavaDoc tableName, Connection connection, JDBCProperties properties)
257     throws SQLException JavaDoc {
258         List columns = new ArrayList();
259         Statement stmt = null;
260         ResultSetMetaData rsetdata = null;
261         if (dbms == DBMSConstants.MYSQL || dbms == DBMSConstants.MYSQL323) {
262             stmt = connection.createStatement();
263             stmt.execute(
264                 createQuery(catalogName, schemaName, tableName));
265             rsetdata = stmt.getResultSet().getMetaData();
266         }
267         else {
268             stmt =
269             connection.prepareStatement(
270                     createQuery(catalogName, schemaName, tableName));
271             ((PreparedStatement) stmt).execute();
272             rsetdata = ((PreparedStatement) stmt).getMetaData();
273         }
274         
275         try {
276             for (int i = 1; i <= rsetdata.getColumnCount(); i++) {
277                 columns.add(new ColumnMetaDataImpl(rsetdata, i, getTypeMap(), properties));
278             }
279         } finally {
280             if (stmt != null)
281                 stmt.close();
282         }
283         return columns;
284     }
285
286     /**
287      * Create a dummy costless query in order to explore result set JDBC
288      * metadata that seems more reliable on most drivers than exploring
289      * database metadata.
290      * @param catalogName
291      * @param schemaName
292      * @param origTableName
293      * @return
294      */

295     protected String JavaDoc createQuery(String JavaDoc catalogName, String JavaDoc schemaName, String JavaDoc origTableName) {
296         StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
297     
298         retVal.append("SELECT * FROM ");
299         if (null != catalogName && 0 < catalogName.trim().length()) {
300             retVal.append(catalogName.trim());
301             retVal.append(".");
302         }
303         if (null != schemaName && 0 < schemaName.trim().length()) {
304             retVal.append(schemaName.trim());
305             retVal.append(".");
306         }
307         if (null != origTableName && 0 < origTableName.trim().length()) {
308             retVal.append(origTableName.trim());
309         }
310         retVal.append(" WHERE 0=1");
311         return retVal.toString();
312     }
313 }
314
Popular Tags