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.synth.sql;
007:
008: import java.sql.Connection;
009: import java.sql.DatabaseMetaData;
010: import java.sql.DriverManager;
011: import java.sql.ResultSet;
012: import java.sql.SQLException;
013: import java.sql.Statement;
014: import java.util.ArrayList;
015:
016: /**
017: * Represents a connection to a real database.
018: */
019: class DbConnection implements DbInterface {
020: private TestSynth config;
021: private int id;
022: private String driver;
023: private String url;
024: private String user;
025: private String password;
026: private Connection conn;
027: private Connection sentinel;
028: private boolean useSentinel;
029:
030: DbConnection(TestSynth config, String driver, String url,
031: String user, String password, int id, boolean useSentinel) {
032: this .config = config;
033: this .driver = driver;
034: this .url = url;
035: this .user = user;
036: this .password = password;
037: this .id = id;
038: this .useSentinel = useSentinel;
039: log("url=" + url);
040: }
041:
042: public void reset() throws SQLException {
043: log("reset;");
044: DatabaseMetaData meta = conn.getMetaData();
045: Statement stat = conn.createStatement();
046: ArrayList tables = new ArrayList();
047: ResultSet rs = meta.getTables(null, null, null,
048: new String[] { "TABLE" });
049: while (rs.next()) {
050: String schemaName = rs.getString("TABLE_SCHEM");
051: if (!"INFORMATION_SCHEMA".equals(schemaName)) {
052: tables.add(rs.getString("TABLE_NAME"));
053: }
054: }
055: while (tables.size() > 0) {
056: int dropped = 0;
057: for (int i = 0; i < tables.size(); i++) {
058: try {
059: String table = (String) tables.get(i);
060: stat.execute("DROP TABLE " + table);
061: dropped++;
062: tables.remove(i);
063: i--;
064: } catch (SQLException e) {
065: // maybe a referential integrity
066: }
067: }
068: // could not drop any table and still tables to drop
069: if (dropped == 0 && tables.size() > 0) {
070: throw new Error("Cannot drop " + tables);
071: }
072: }
073: }
074:
075: public void connect() throws Exception {
076: if (useSentinel && sentinel == null) {
077: sentinel = getConnection();
078: }
079: log("connect to " + url + ";");
080: conn = getConnection();
081: }
082:
083: private Connection getConnection() throws Exception {
084: log("(getConnection to " + url + ");");
085: if (driver == null) {
086: return config.getConnection("synth");
087: } else {
088: Class.forName(driver);
089: return DriverManager.getConnection(url, user, password);
090: }
091: }
092:
093: public void disconnect() throws SQLException {
094: log("disconnect " + url + ";");
095: conn.close();
096: }
097:
098: public void end() throws SQLException {
099: log("end " + url + ";");
100: if (sentinel != null) {
101: sentinel.close();
102: sentinel = null;
103: }
104: }
105:
106: public void createTable(Table table) throws SQLException {
107: execute(table.getCreateSQL());
108: }
109:
110: public void dropTable(Table table) throws SQLException {
111: execute(table.getDropSQL());
112: }
113:
114: public void createIndex(Index index) throws SQLException {
115: execute(index.getCreateSQL());
116: index.getTable().addIndex(index);
117: }
118:
119: public void dropIndex(Index index) throws SQLException {
120: execute(index.getDropSQL());
121: index.getTable().removeIndex(index);
122: }
123:
124: public Result insert(Table table, Column[] c, Value[] v)
125: throws SQLException {
126: String sql = table.getInsertSQL(c, v);
127: execute(sql);
128: return new Result(sql, 1);
129: }
130:
131: private void execute(String sql) throws SQLException {
132: log(sql + ";");
133: conn.createStatement().execute(sql);
134: }
135:
136: public Result select(String sql) throws SQLException {
137: log(sql + ";");
138: Statement stat = conn.createStatement();
139: Result result = new Result(config, sql, stat.executeQuery(sql));
140: return result;
141: }
142:
143: public Result delete(Table table, String condition)
144: throws SQLException {
145: String sql = "DELETE FROM " + table.getName();
146: if (condition != null) {
147: sql += " WHERE " + condition;
148: }
149: log(sql + ";");
150: Statement stat = conn.createStatement();
151: Result result = new Result(sql, stat.executeUpdate(sql));
152: return result;
153: }
154:
155: public Result update(Table table, Column[] columns, Value[] values,
156: String condition) throws SQLException {
157: String sql = "UPDATE " + table.getName() + " SET ";
158: for (int i = 0; i < columns.length; i++) {
159: if (i > 0) {
160: sql += ", ";
161: }
162: sql += columns[i].getName() + "=" + values[i].getSQL();
163: }
164: if (condition != null) {
165: sql += " WHERE " + condition;
166: }
167: log(sql + ";");
168: Statement stat = conn.createStatement();
169: Result result = new Result(sql, stat.executeUpdate(sql));
170: return result;
171: }
172:
173: public void setAutoCommit(boolean b) throws SQLException {
174: log("set autoCommit " + b + ";");
175: conn.setAutoCommit(b);
176: }
177:
178: public void commit() throws SQLException {
179: log("commit;");
180: conn.commit();
181: }
182:
183: public void rollback() throws SQLException {
184: log("rollback;");
185: conn.rollback();
186: }
187:
188: private void log(String s) {
189: config.log(id, s);
190: }
191:
192: public String toString() {
193: return url;
194: }
195:
196: }
|