1 package com.quadcap.jdbc; 2 3 40 41 import java.io.File ; 42 import java.io.IOException ; 43 44 import java.util.Enumeration ; 45 import java.util.Hashtable ; 46 import java.util.Properties ; 47 import java.util.Vector ; 48 49 import java.sql.Driver ; 50 import java.sql.DriverManager ; 51 import java.sql.DriverPropertyInfo ; 52 import java.sql.SQLException ; 53 54 import com.quadcap.sql.Cursor; 55 import com.quadcap.sql.Database; 56 import com.quadcap.sql.QedResultSet; 57 import com.quadcap.sql.QDriver; 58 import com.quadcap.sql.Session; 59 import com.quadcap.sql.Version; 60 61 import com.quadcap.util.Config; 62 import com.quadcap.util.ConfigNumber; 63 import com.quadcap.util.Debug; 64 import com.quadcap.util.Util; 65 66 76 public class JdbcDriver implements QDriver { 77 81 82 86 87 99 static final ConfigNumber trace = 101 ConfigNumber.find("qed.trace.JdbcDriver", "0"); 102 104 static Hashtable dbs = new Hashtable (); 105 106 public static JdbcDriver jdbcDriver = null; 107 108 static { 109 try { 110 DriverManager.registerDriver(jdbcDriver = new JdbcDriver()); 111 } catch (SQLException e) { 112 Debug.print(e); 113 } 114 } 115 116 119 public JdbcDriver() { 120 } 121 122 130 public java.sql.Connection makeConnection(Database db, String auth, 131 String passwd) 132 throws SQLException 133 { 134 if (trace.bit(0)) { 136 Debug.println("JdbcDriver.makeConnection()"); 137 } 138 return new Connection(db, auth, passwd); 140 } 141 142 150 public QedResultSet makeResultSet(Cursor c) { 151 return new ResultSet(c); 152 } 153 154 161 public boolean acceptsURL(String url) { 162 if (url.startsWith("jdbc:")) { 163 url = url.substring(5); 164 int idx = url.indexOf(':'); 165 if (idx > 0) { 166 String protocol = url.substring(0, idx); 167 if (protocol.equals("quadcap")) return true; 168 if (protocol.equals("qed")) return true; 169 } 170 } 171 return false; 172 } 173 174 189 public java.sql.Connection connect(String url, Properties props) 190 throws SQLException 191 { 192 if (trace.bit(0)) { 194 Debug.println("JdbcDriver.connect(" + url + ", " + props + ")"); 195 } 196 if (!acceptsURL(url)) return null; 198 String origUrl = url; 199 url = url.substring(5); 200 int idx = url.indexOf(';'); 201 if (idx >= 0) { 202 String extraProps = url.substring(idx+1); 203 url = url.substring(0, idx); 204 Properties props2 = new Properties (Config.getPropSubset("qed.*")); 205 props2.putAll(props); 206 props2.putAll(Util.parsePropsString(extraProps)); 207 props = props2; 208 } 209 210 idx = url.indexOf(':'); 211 String param = url.substring(idx+1); 212 213 Database db = null; 214 try { 215 param = new File (param).getCanonicalPath(); 216 synchronized (dbs) { 217 db = (Database)dbs.get(param); 218 if (db == null) { 219 if (trace.bit(1)) { 221 Debug.println("Open database " + dbs.size() + ": " + param); 222 } 223 db = new Database(); 225 db.init(this, origUrl, param, props); 226 db.driverLock = dbs; 227 dbs.put(param, db); 228 } 229 db.addConnection(); 230 } 231 } catch (IOException e) { 232 Debug.print(e); 233 throw new SQLException (e.toString(), "Q000Z"); 234 } 235 236 String auth = props.getProperty("user"); 237 String pass = props.getProperty("passwd"); 238 Connection conn = null; 239 try { 240 conn = new Connection(db, auth, pass); 241 } finally { 242 if (conn == null) { 245 synchronized (dbs) { 246 db.removeConnection(); 247 } 248 } 249 } 250 return conn; 251 } 252 253 258 public int getMajorVersion() { return Version.majorVersion; } 259 260 265 public int getMinorVersion() { return Version.minorVersion; } 266 267 277 public DriverPropertyInfo [] getPropertyInfo(String url, 278 Properties info) 279 throws SQLException 280 { 281 int cnt = 0; 282 if (info == null || info.get("user") == null) cnt++; 283 DriverPropertyInfo [] ret = new DriverPropertyInfo [cnt]; 284 if (cnt != 0) { 285 ret[0] = new DriverPropertyInfo ("user", null); 286 ret[0].required = true; 287 } 288 return ret; 289 } 290 291 296 public boolean jdbcCompliant() { 297 return true; } 299 300 306 public void closeDatabase(final String name) { 307 if (trace.bit(1)) { 309 Debug.println("closeDatabase(" + name + ")"); 310 } 311 synchronized (dbs) { 313 Database db = (Database)dbs.get(name); 314 if (db != null) { 315 try { 316 db.close(); 317 } finally { 318 dbs.remove(name); 319 } 320 } 321 } 322 if (trace.bit(1)) { 324 Debug.println("closeDatabase(" + name + ") complete"); 325 } 326 } 328 329 335 public Enumeration getDatabaseNames() { 336 return dbs.keys(); 337 } 338 339 346 public Database getDatabase(String name) { 347 synchronized (dbs) { 348 return (Database)dbs.get(name); 349 } 350 } 351 352 355 public static void closeAll() { 356 synchronized (dbs) { 357 Enumeration e = JdbcDriver.dbs.elements(); 358 while (e.hasMoreElements()) { 359 Database db = (Database)e.nextElement(); 360 try { 361 db.close(); 362 } catch (Throwable t) { 363 } 364 } 365 dbs.clear(); 366 } 367 } 368 369 372 public Session getSession(java.sql.Connection conn) { 373 try { 374 QDriver d = (QDriver)DriverManager.getDriver("jdbc:qed"); 375 return d.getSession(conn); 376 } catch (SQLException ex) { 377 return null; 378 } 379 } 380 } 381 | Popular Tags |