00001
package com.quadcap.jdbc;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.File;
00042
import java.io.IOException;
00043
00044
import java.util.Enumeration;
00045
import java.util.Hashtable;
00046
import java.util.Properties;
00047
import java.util.Vector;
00048
00049
import java.sql.Driver;
00050
import java.sql.DriverManager;
00051
import java.sql.DriverPropertyInfo;
00052
import java.sql.SQLException;
00053
00054
import com.quadcap.sql.Cursor;
00055
import com.quadcap.sql.Database;
00056
import com.quadcap.sql.QedResultSet;
00057
import com.quadcap.sql.QDriver;
00058
import com.quadcap.sql.Session;
00059
import com.quadcap.sql.Version;
00060
00061
import com.quadcap.util.Config;
00062
import com.quadcap.util.ConfigNumber;
00063
import com.quadcap.util.Debug;
00064
import com.quadcap.util.Util;
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 public class JdbcDriver implements QDriver {
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 static final ConfigNumber
trace =
00101 ConfigNumber.find(
"qed.trace.JdbcDriver",
"0");
00102
00103
00104 static Hashtable
dbs =
new Hashtable();
00105
00106 public static JdbcDriver jdbcDriver = null;
00107
00108
static {
00109
try {
00110 DriverManager.registerDriver(
jdbcDriver =
new JdbcDriver());
00111 }
catch (SQLException e) {
00112
Debug.print(e);
00113 }
00114 }
00115
00116
00117
00118
00119 public JdbcDriver() {
00120 }
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130 public java.sql.Connection
makeConnection(
Database db, String auth,
00131 String passwd)
00132
throws SQLException
00133 {
00134
00135
if (
trace.bit(0)) {
00136
Debug.println(
"JdbcDriver.makeConnection()");
00137 }
00138
00139
return new Connection(db, auth, passwd);
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150 public QedResultSet makeResultSet(
Cursor c) {
00151
return new ResultSet(c);
00152 }
00153
00154
00155
00156
00157
00158
00159
00160
00161 public boolean acceptsURL(String url) {
00162
if (url.startsWith(
"jdbc:")) {
00163 url = url.substring(5);
00164
int idx = url.indexOf(
':');
00165
if (idx > 0) {
00166 String protocol = url.substring(0, idx);
00167
if (protocol.equals(
"quadcap"))
return true;
00168
if (protocol.equals(
"qed"))
return true;
00169 }
00170 }
00171
return false;
00172 }
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189 public java.sql.Connection
connect(String url, Properties props)
00190
throws SQLException
00191 {
00192
00193
if (
trace.bit(0)) {
00194
Debug.println(
"JdbcDriver.connect(" + url +
", " + props +
")");
00195 }
00196
00197
if (!acceptsURL(url))
return null;
00198 String origUrl = url;
00199 url = url.substring(5);
00200
int idx = url.indexOf(
';');
00201
if (idx >= 0) {
00202 String extraProps = url.substring(idx+1);
00203 url = url.substring(0, idx);
00204 Properties props2 =
new Properties(
Config.getPropSubset(
"qed.*"));
00205 props2.putAll(props);
00206 props2.putAll(
Util.parsePropsString(extraProps));
00207 props = props2;
00208 }
00209
00210 idx = url.indexOf(
':');
00211 String param = url.substring(idx+1);
00212
00213
Database db = null;
00214
try {
00215 param =
new File(param).getCanonicalPath();
00216
synchronized (
dbs) {
00217 db = (
Database)
dbs.get(param);
00218
if (db == null) {
00219
00220
if (
trace.bit(1)) {
00221
Debug.println(
"Open database " +
dbs.size() +
": " + param);
00222 }
00223
00224 db =
new Database();
00225 db.
init(
this, origUrl, param, props);
00226 db.
driverLock =
dbs;
00227
dbs.put(param, db);
00228 }
00229 db.
addConnection();
00230 }
00231 }
catch (IOException e) {
00232
Debug.print(e);
00233
throw new SQLException(e.toString(),
"Q000Z");
00234 }
00235
00236 String auth = props.getProperty(
"user");
00237 String pass = props.getProperty(
"passwd");
00238
Connection conn = null;
00239
try {
00240 conn =
new Connection(db, auth, pass);
00241 } finally {
00242
00243
00244
if (conn == null) {
00245
synchronized (
dbs) {
00246 db.
removeConnection();
00247 }
00248 }
00249 }
00250
return conn;
00251 }
00252
00253
00254
00255
00256
00257
00258 public int getMajorVersion() {
return Version.majorVersion; }
00259
00260
00261
00262
00263
00264
00265 public int getMinorVersion() {
return Version.minorVersion; }
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277 public DriverPropertyInfo[]
getPropertyInfo(String url,
00278 Properties info)
00279
throws SQLException
00280 {
00281
int cnt = 0;
00282
if (info == null || info.get(
"user") == null) cnt++;
00283 DriverPropertyInfo[] ret =
new DriverPropertyInfo[cnt];
00284
if (cnt != 0) {
00285 ret[0] =
new DriverPropertyInfo(
"user", null);
00286 ret[0].required =
true;
00287 }
00288
return ret;
00289 }
00290
00291
00292
00293
00294
00295
00296 public boolean jdbcCompliant() {
00297
return true;
00298 }
00299
00300
00301
00302
00303
00304
00305
00306 public void closeDatabase(
final String name) {
00307
00308
if (
trace.bit(1)) {
00309
Debug.println(
"closeDatabase(" + name +
")");
00310 }
00311
00312
synchronized (
dbs) {
00313
Database db = (
Database)
dbs.get(name);
00314
if (db != null) {
00315
try {
00316 db.close();
00317 } finally {
00318
dbs.remove(name);
00319 }
00320 }
00321 }
00322
00323
if (
trace.bit(1)) {
00324
Debug.println(
"closeDatabase(" + name +
") complete");
00325 }
00326
00327 }
00328
00329
00330
00331
00332
00333
00334
00335 public Enumeration
getDatabaseNames() {
00336
return dbs.keys();
00337 }
00338
00339
00340
00341
00342
00343
00344
00345
00346 public Database getDatabase(String name) {
00347
synchronized (
dbs) {
00348
return (
Database)
dbs.get(name);
00349 }
00350 }
00351
00352
00353
00354
00355 public static void closeAll() {
00356
synchronized (
dbs) {
00357 Enumeration e =
JdbcDriver.dbs.elements();
00358
while (e.hasMoreElements()) {
00359
Database db = (
Database)e.nextElement();
00360
try {
00361 db.close();
00362 }
catch (Throwable t) {
00363 }
00364 }
00365
dbs.clear();
00366 }
00367 }
00368
00369
00370
00371
00372 public Session getSession(java.sql.Connection conn) {
00373
try {
00374 QDriver d = (QDriver)DriverManager.getDriver(
"jdbc:qed");
00375
return d.getSession(conn);
00376 }
catch (SQLException ex) {
00377
return null;
00378 }
00379 }
00380 }