1417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughespackage SQLite;
2417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
3417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughesimport java.sql.*;
4417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughesimport java.util.Properties;
5417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
6417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughespublic class JDBCDriver implements java.sql.Driver {
7417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
8417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    public static final int MAJORVERSION = 1;
97a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes
107a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes    public static boolean sharedCache = false;
117a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes
127a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes    public static String vfs = null;
13417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
14417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    private static java.lang.reflect.Constructor makeConn = null;
15417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
16417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    protected Connection conn;
17417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
18417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    static {
197a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	try {
207a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    Class connClass = null;
217a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    Class args[] = new Class[5];
227a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    args[0] = Class.forName("java.lang.String");
237a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    args[1] = args[0];
247a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    args[2] = args[0];
257a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    args[3] = args[0];
267a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    args[4] = args[0];
277a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    String jvers = java.lang.System.getProperty("java.version");
287a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    String cvers;
297a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    if (jvers == null || jvers.startsWith("1.0")) {
307a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		throw new java.lang.Exception("unsupported java version");
317a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    } else if (jvers.startsWith("1.1")) {
327a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		cvers = "SQLite.JDBC1.JDBCConnection";
337a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    } else if (jvers.startsWith("1.2") || jvers.startsWith("1.3")) {
347a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		cvers = "SQLite.JDBC2.JDBCConnection";
357a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    } else if (jvers.startsWith("1.4")) {
367a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		cvers = "SQLite.JDBC2x.JDBCConnection";
377a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    } else if (jvers.startsWith("1.5")) {
387a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		cvers = "SQLite.JDBC2y.JDBCConnection";
397a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		try {
407a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		    Class.forName(cvers);
417a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		} catch (java.lang.Exception e) {
427a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		    cvers = "SQLite.JDBC2x.JDBCConnection";
437a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		}
447a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    } else {
457a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		cvers = "SQLite.JDBC2z.JDBCConnection";
467a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		try {
477a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		    Class.forName(cvers);
487a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		} catch (java.lang.Exception e) {
497a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		    cvers = "SQLite.JDBC2y.JDBCConnection";
507a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		    try {
517a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes			Class.forName(cvers);
527a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		    } catch (java.lang.Exception ee) {
537a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes			cvers = "SQLite.JDBC2x.JDBCConnection";
547a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		    }
557a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		}
567a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    }
577a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    connClass = Class.forName(cvers);
587a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    makeConn = connClass.getConstructor(args);
597a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    java.sql.DriverManager.registerDriver(new JDBCDriver());
607a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    try {
617a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		String shcache =
627a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		    java.lang.System.getProperty("SQLite.sharedcache");
637a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		if (shcache != null &&
647a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		    (shcache.startsWith("y") || shcache.startsWith("Y"))) {
657a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		    sharedCache = SQLite.Database._enable_shared_cache(true);
667a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		}
677a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    } catch (java.lang.Exception e) {
687a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    }
697a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    try {
707a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		String tvfs =
717a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		    java.lang.System.getProperty("SQLite.vfs");
727a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		if (tvfs != null) {
737a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		    vfs = tvfs;
747a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		}
757a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    } catch (java.lang.Exception e) {
767a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    }
777a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	} catch (java.lang.Exception e) {
787a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    System.err.println(e);
797a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	}
80417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    }
81417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
82417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    public JDBCDriver() {
83417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    }
847a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes
85417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    public boolean acceptsURL(String url) throws SQLException {
867a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	return url.startsWith("sqlite:/") ||
877a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    url.startsWith("jdbc:sqlite:/");
88417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    }
89417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
90417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    public Connection connect(String url, Properties info)
917a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	throws SQLException {
927a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	if (!acceptsURL(url)) {
937a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    return null;
947a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	}
957a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	Object args[] = new Object[5];
967a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	args[0] = url;
977a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	if (info != null) {
987a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    args[1] = info.getProperty("encoding");
997a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    args[2] = info.getProperty("password");
1007a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    args[3] = info.getProperty("daterepr");
1017a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    args[4] = info.getProperty("vfs");
1027a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	}
1037a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	if (args[1] == null) {
1047a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    args[1] = java.lang.System.getProperty("SQLite.encoding");
1057a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	}
1067a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	if (args[4] == null) {
1077a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    args[4] = vfs;
1087a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	}
1097a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	try {
1107a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    conn = (Connection) makeConn.newInstance(args);
1117a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	} catch (java.lang.reflect.InvocationTargetException ie) {
1127a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    throw new SQLException(ie.getTargetException().toString());
1137a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	} catch (java.lang.Exception e) {
1147a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    throw new SQLException(e.toString());
1157a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	}
1167a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	return conn;
117417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    }
118417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
119417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    public int getMajorVersion() {
1207a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	return MAJORVERSION;
121417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    }
122417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
123417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    public int getMinorVersion() {
1247a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	return Constants.drv_minor;
125417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    }
126417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
127417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
1287a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	throws SQLException {
1297a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	DriverPropertyInfo p[] = new DriverPropertyInfo[4];
1307a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	DriverPropertyInfo pp = new DriverPropertyInfo("encoding", "");
1317a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	p[0] = pp;
1327a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	pp = new DriverPropertyInfo("password", "");
1337a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	p[1] = pp;
1347a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	pp = new DriverPropertyInfo("daterepr", "normal");
1357a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	p[2] = pp;
1367a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	pp = new DriverPropertyInfo("vfs", vfs);
1377a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	p[3] = pp;
1387a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	return p;
139417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    }
140417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
141417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    public boolean jdbcCompliant() {
1427a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	return false;
143417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    }
144417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes}
145