1 23 24 32 package org.xquark.jdbc.typing; 33 34 import java.sql.*; 35 import java.sql.DatabaseMetaData ; 36 import java.sql.ResultSet ; 37 import java.sql.SQLException ; 38 import java.util.*; 39 40 44 public class DBMSInfo 45 { 46 private static final String RCSRevision = "$Revision: 1.2 $"; 47 private static final String RCSName = "$Name: $"; 48 49 private short dbms; 50 private int systemCollectionsDataLength = 255; 51 private int maxUserCollectionsDataLength = 255; private String 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 63 public TypeMap getTypeMap() 64 { 65 return typeMap; 66 } 67 68 72 public short getDbms() 73 { 74 return dbms; 75 } 76 77 81 public SQLStatementCustomizer getSelectLockStatement() 82 { 83 return selectLockStatement; 84 } 85 86 90 public int getMaxUserCollectionsDataLength() 91 { 92 return maxUserCollectionsDataLength; 93 } 94 95 99 public boolean useDoubleQuotes4DDLNames() 100 { 101 return useDoubleQuotes4DDLNames; 102 } 103 104 108 public String getRowId() 109 { 110 return rowId; 111 } 112 113 117 public int getSystemCollectionsDataLength() 118 { 119 return systemCollectionsDataLength; 120 } 121 122 126 public void setTypeMap(TypeMap typeMap) 127 { 128 this.typeMap = typeMap; 129 } 130 131 135 public void setDbms(short dbms) 136 { 137 this.dbms = dbms; 138 } 139 140 144 public void setSelectLockStatement(SQLStatementCustomizer selectLockStatement) 145 { 146 this.selectLockStatement = selectLockStatement; 147 } 148 149 153 public void setMaxUserCollectionsDataLength(int maxUserCollectionsDataLength) 154 { 155 this.maxUserCollectionsDataLength = maxUserCollectionsDataLength; 156 } 157 158 162 public void setUseDoubleQuotes4DDLNames(boolean useDoubleQuotesForDatabaseObjects) 163 { 164 this.useDoubleQuotes4DDLNames = useDoubleQuotesForDatabaseObjects; 165 } 166 167 171 public void setRowId(String rowId) 172 { 173 this.rowId = rowId; 174 } 175 176 180 public void setSystemCollectionsDataLength(int systemCollectionsDataLength) 181 { 182 this.systemCollectionsDataLength = systemCollectionsDataLength; 183 } 184 185 189 public SQLStatementCustomizer getIndexOrganizedStatement() 190 { 191 return indexOrganizedStatement; 192 } 193 194 198 public SQLStatementCustomizer getTemporaryTableStatement() 199 { 200 return temporaryTableStatement; 201 } 202 203 207 public void setIndexOrganizedStatement(SQLStatementCustomizer indexOrganizedHint) 208 { 209 this.indexOrganizedStatement = indexOrganizedHint; 210 } 211 212 216 public void setTemporaryTableStatement(SQLStatementCustomizer temporaryTableHint) 217 { 218 this.temporaryTableStatement = temporaryTableHint; 219 } 220 221 227 public List getTableMetadata(String catalogName, String schemaName, 228 String tableName, Connection connection, 229 JDBCProperties properties) 230 throws SQLException { 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 catalogName, String schemaName, 240 String tableName, Connection connection, JDBCProperties properties) 241 throws SQLException { 242 List columns = new ArrayList(); 243 DatabaseMetaData metadata = connection.getMetaData(); 244 ResultSet 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 catalogName, String schemaName, 256 String tableName, Connection connection, JDBCProperties properties) 257 throws SQLException { 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 295 protected String createQuery(String catalogName, String schemaName, String origTableName) { 296 StringBuffer retVal = new StringBuffer (); 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 |