1 19 20 package org.netbeans.lib.ddl.impl; 21 22 import java.sql.DatabaseMetaData ; 23 import java.sql.ResultSet ; 24 import java.sql.SQLException ; 25 import java.util.HashMap ; 26 import java.util.LinkedList ; 27 28 import org.openide.ErrorManager; 29 30 import org.netbeans.lib.ddl.DriverSpecificationFactory; 31 32 public class DriverSpecification { 33 34 35 private HashMap desc; 36 37 private String catalog, schema; 38 39 private DatabaseMetaData dmd; 40 41 private ResultSet rs; 42 43 private String quoteString; 44 45 46 SpecificationFactory factory; 47 48 49 public DriverSpecification(HashMap description) { 50 desc = description; 51 quoteString = null; 52 } 53 54 public DriverSpecificationFactory getDriverSpecificationFactory() { 55 return factory; 56 } 57 58 public void setDriverSpecificationFactory(DriverSpecificationFactory fac) { 59 factory = (SpecificationFactory) fac; 60 } 61 62 public void setCatalog(String catalog) { 63 if (catalog == null || dmd == null) { 64 this.catalog = catalog; 65 return; 66 } else 67 catalog.trim(); 68 69 ResultSet rs; 70 LinkedList list = new LinkedList (); 71 72 try { 73 rs = dmd.getCatalogs(); 74 while (rs.next()) 75 list.add(rs.getString(1).trim()); 76 rs.close(); 77 } catch (SQLException exc) { 78 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc); 79 80 this.catalog = null; rs = null; 83 return; 84 } 85 86 if (list.contains(catalog)) 87 this.catalog = catalog; 88 else 89 this.catalog = null; } 91 92 public String getCatalog() { 93 return catalog; 94 } 95 96 public void setSchema(String schema) { 97 this.schema = schema; 98 } 99 100 public String getSchema() { 101 return schema; 102 } 103 104 public void setMetaData(DatabaseMetaData dmd) { 105 this.dmd = dmd; 106 } 107 108 public DatabaseMetaData getMetaData() { 109 return dmd; 110 } 111 112 public void getTables(String tableNamePattern, String [] types) throws SQLException { 113 try { 114 tableNamePattern = quoteString(tableNamePattern); 115 rs = dmd.getTables(catalog, schema, tableNamePattern, types); 116 } catch (SQLException exc) { 117 rs = null; 118 throw exc; 119 } 120 } 121 122 public void getProcedures(String procedureNamePattern) throws SQLException { 123 try { 124 procedureNamePattern = quoteString(procedureNamePattern); 125 rs = dmd.getProcedures(catalog, schema, procedureNamePattern); 126 } catch (SQLException exc) { 127 rs = null; 128 throw exc; 129 } 130 } 131 132 public void getPrimaryKeys(String table) throws SQLException { 133 try { 134 table = quoteString(table); 135 rs = dmd.getPrimaryKeys(catalog, schema, table); 136 } catch (SQLException exc) { 137 rs = null; 138 throw exc; 139 } 140 } 141 142 public void getIndexInfo(String table, boolean unique, boolean approximate) throws SQLException { 143 try { 144 table = quoteString(table); 145 rs = dmd.getIndexInfo(catalog, schema, table, unique, approximate); 146 } catch (SQLException exc) { 147 rs = null; 148 throw exc; 149 } 150 } 151 152 public void getColumns(String tableNamePattern, String columnNamePattern) throws SQLException { 153 try { 154 tableNamePattern = quoteString(tableNamePattern); 155 columnNamePattern = quoteString(columnNamePattern); 156 rs = dmd.getColumns(catalog, schema, tableNamePattern, columnNamePattern); 157 } catch (SQLException exc) { 158 rs = null; 159 throw exc; 160 } 161 } 162 163 public void getProcedureColumns(String procedureNamePattern, String columnNamePattern) throws SQLException { 164 try { 165 procedureNamePattern = quoteString(procedureNamePattern); 166 columnNamePattern = quoteString(columnNamePattern); 167 rs = dmd.getProcedureColumns(catalog, schema, procedureNamePattern, columnNamePattern); 168 } catch (SQLException exc) { 169 rs = null; 170 throw exc; 171 } 172 } 173 174 public void getExportedKeys(String table) throws SQLException { 175 try { 176 table = quoteString(table); 177 rs = dmd.getExportedKeys(catalog, schema, table); 178 } catch (SQLException exc) { 179 rs = null; 180 throw exc; 181 } 182 } 183 184 public void getImportedKeys(String table) throws SQLException { 185 try { 186 table = quoteString(table); 187 rs = dmd.getImportedKeys(catalog, schema, table); 188 } catch (SQLException exc) { 189 rs = null; 190 throw exc; 191 } 192 } 193 194 public ResultSet getResultSet() { 195 return rs; 196 } 197 198 public HashMap getRow() throws SQLException { 199 HashMap rset = new HashMap (); 200 Object value; 201 202 try { 203 int count = rs.getMetaData().getColumnCount(); 204 205 for (int i = 1; i <= count; i++) { 206 value = null; 207 try { 208 value = rs.getString(i); 209 } catch (SQLException exc) { 211 rset = null; 212 throw exc; 214 } 215 rset.put(new Integer (i), value); 216 } 217 } catch (SQLException exc) { 218 rset = null; 219 throw exc; 220 } 221 222 return rset; 223 } 224 225 227 public boolean areViewsSupported() { 228 try { 229 String productName = dmd.getDatabaseProductName().trim(); 230 231 if ("PointBase".equals(productName)) { int driverMajorVersion = dmd.getDriverMajorVersion(); 233 int driverMinorVersion = dmd.getDriverMinorVersion(); 234 return ((driverMajorVersion == 4 && driverMinorVersion >= 1) || driverMajorVersion > 4); 235 } else if ("MySQL".equals(productName)) { int databaseMajorVersion = dmd.getDatabaseMajorVersion(); 237 return (databaseMajorVersion >= 5); 238 } else if ("HypersonicSQL".equals(productName)) { return false; 241 } 242 } catch(SQLException exc) { 243 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc); 244 } 245 246 return true; 247 } 248 249 private String getQuoteString() { 250 if (quoteString == null) { 251 try { 252 quoteString = dmd.getIdentifierQuoteString(); 253 if (quoteString == null || quoteString.equals(" ")) quoteString = ""; else 256 quoteString.trim(); 257 } catch (SQLException exc) { 258 quoteString = ""; } 260 } 261 262 return quoteString; 263 } 264 265 private String quoteString(String str) { 266 try { 267 if (dmd.getDatabaseProductName().trim().equals("PointBase")) { String quoteStr = getQuoteString(); 270 if (str != null && !str.equals("%") && !quoteStr.equals("")) str = quoteStr + str + quoteStr; 272 } 273 } catch (SQLException exc) { 274 } 276 277 return str; 278 } 279 280 public String getDBName() { 281 try { 282 return dmd.getDatabaseProductName().trim(); 283 } catch (SQLException exc) { 284 return null; 286 } 287 } 288 } 289 | Popular Tags |