001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.test.db;
007:
008: import java.io.InputStream;
009: import java.sql.Connection;
010: import java.sql.DriverManager;
011: import java.sql.PreparedStatement;
012: import java.sql.Statement;
013: import java.util.HashMap;
014:
015: /**
016: * A simple wrapper around the JDBC API.
017: * Currently used for testing.
018: * Features:
019: * <ul>
020: * <li>No checked exceptions
021: * </li><li>Easy to use, fluent API
022: * </li></ul>
023: */
024: public class Db {
025:
026: private static final String[] DRIVERS = { "jdbc:h2:",
027: "org.h2.Driver", "jdbc:firebirdsql:",
028: "org.firebirdsql.jdbc.FBDriver", "jdbc:db2:",
029: "COM.ibm.db2.jdbc.net.DB2Driver", "jdbc:oracle:",
030: "oracle.jdbc.driver.OracleDriver", "jdbc:microsoft:",
031: "com.microsoft.jdbc.sqlserver.SQLServerDriver",
032: "jdbc:sqlserver:",
033: "com.microsoft.sqlserver.jdbc.SQLServerDriver",
034: "jdbc:postgresql:", "org.postgresql.Driver", "jdbc:mysql:",
035: "com.mysql.jdbc.Driver", "jdbc:derby://",
036: "org.apache.derby.jdbc.ClientDriver", "jdbc:derby:",
037: "org.apache.derby.jdbc.EmbeddedDriver", "jdbc:hsqldb:",
038: "org.hsqldb.jdbcDriver" };
039:
040: private Connection conn;
041: private Statement stat;
042: private HashMap prepared = new HashMap();
043: private long start;
044:
045: public static Db open(String url, String user, String password) {
046: try {
047: for (int i = 0; i < DRIVERS.length; i += 2) {
048: String prefix = DRIVERS[i];
049: if (url.startsWith(prefix)) {
050: Class.forName(DRIVERS[i + 1]);
051: break;
052: }
053: }
054: return new Db(DriverManager.getConnection(url, user,
055: password));
056: } catch (Exception e) {
057: throw convert(e);
058: }
059: }
060:
061: public Prepared prepare(String sql) {
062: try {
063: PreparedStatement prep = (PreparedStatement) prepared
064: .get(sql);
065: if (prep == null) {
066: prep = conn.prepareStatement(sql);
067: prepared.put(sql, prep);
068: }
069: return new Prepared(conn.prepareStatement(sql));
070: } catch (Exception e) {
071: throw convert(e);
072: }
073: }
074:
075: public void execute(String sql) {
076: try {
077: stat.execute(sql);
078: } catch (Exception e) {
079: throw convert(e);
080: }
081: }
082:
083: public void close() {
084: try {
085: conn.close();
086: } catch (Exception e) {
087: throw convert(e);
088: }
089: }
090:
091: private Db(Connection conn) {
092: try {
093: this .conn = conn;
094: stat = conn.createStatement();
095: } catch (Exception e) {
096: throw convert(e);
097: }
098: }
099:
100: public class Prepared {
101: private PreparedStatement prep;
102: private int index;
103:
104: Prepared(PreparedStatement prep) {
105: this .prep = prep;
106: }
107:
108: public Prepared set(int x) {
109: try {
110: prep.setInt(++index, x);
111: return this ;
112: } catch (Exception e) {
113: throw convert(e);
114: }
115: }
116:
117: public Prepared set(String x) {
118: try {
119: prep.setString(++index, x);
120: return this ;
121: } catch (Exception e) {
122: throw convert(e);
123: }
124: }
125:
126: public Prepared set(byte[] x) {
127: try {
128: prep.setBytes(++index, x);
129: return this ;
130: } catch (Exception e) {
131: throw convert(e);
132: }
133: }
134:
135: public Prepared set(InputStream x) {
136: try {
137: prep.setBinaryStream(++index, x, -1);
138: return this ;
139: } catch (Exception e) {
140: throw convert(e);
141: }
142: }
143:
144: public void execute() {
145: try {
146: prep.execute();
147: } catch (Exception e) {
148: throw convert(e);
149: }
150: }
151: }
152:
153: private static Error convert(Exception e) {
154: return new Error("Error: " + e.toString(), e);
155: }
156:
157: public void startTime() {
158: start = System.currentTimeMillis();
159: }
160:
161: public void printTime(String s) {
162: System.out.println(s + ": "
163: + (System.currentTimeMillis() - start));
164: }
165: }
|