KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > test > synth > DbConnection


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.test.synth;
6
7 import java.sql.Connection JavaDoc;
8 import java.sql.DatabaseMetaData JavaDoc;
9 import java.sql.DriverManager JavaDoc;
10 import java.sql.ResultSet JavaDoc;
11 import java.sql.SQLException JavaDoc;
12 import java.sql.Statement JavaDoc;
13 import java.util.ArrayList JavaDoc;
14
15 class DbConnection implements DbInterface {
16     private TestSynth config;
17     private int id;
18     private String JavaDoc driver;
19     private String JavaDoc url;
20     private String JavaDoc user;
21     private String JavaDoc password;
22     private Connection JavaDoc conn;
23     private Connection JavaDoc sentinel;
24     private boolean useSentinel;
25     
26     DbConnection(TestSynth config, String JavaDoc driver, String JavaDoc url, String JavaDoc user, String JavaDoc password, int id, boolean useSentinel) {
27         this.config = config;
28         this.driver = driver;
29         this.url = url;
30         this.user = user;
31         this.password = password;
32         this.id = id;
33         this.useSentinel = useSentinel;
34         log("url="+url);
35     }
36
37     public void reset() throws SQLException JavaDoc {
38         log("reset;");
39         DatabaseMetaData JavaDoc meta = conn.getMetaData();
40         Statement JavaDoc stat = conn.createStatement();
41         ArrayList JavaDoc tables = new ArrayList JavaDoc();
42         ResultSet JavaDoc rs = meta.getTables(null, null, null, new String JavaDoc[] { "TABLE"});
43         while (rs.next()) {
44             String JavaDoc schemaName = rs.getString("TABLE_SCHEM");
45             if(!"INFORMATION_SCHEMA".equals(schemaName)) {
46                 tables.add(rs.getString("TABLE_NAME"));
47             }
48         }
49         while (tables.size() > 0) {
50             int dropped = 0;
51             for (int i = 0; i < tables.size(); i++) {
52                 try {
53                     String JavaDoc table = (String JavaDoc) tables.get(i);
54                     stat.execute("DROP TABLE " + table);
55                     dropped++;
56                     tables.remove(i);
57                     i--;
58                 } catch (SQLException JavaDoc e) {
59                     // maybe a referencial integrity
60
}
61             }
62             // could not drop any table and still tables to drop
63
if (dropped == 0 && tables.size() > 0) {
64                 throw new Error JavaDoc("Cannot drop "+tables);
65             }
66         }
67     }
68
69     public void connect() throws Exception JavaDoc {
70         if(useSentinel && sentinel == null) {
71             sentinel = getConnection();
72         }
73         log("connect to "+url+";");
74         conn = getConnection();
75     }
76     
77     private Connection JavaDoc getConnection() throws Exception JavaDoc {
78         log("(getConnection to "+url+");");
79         if(driver==null) {
80             return config.getConnection("synth");
81         } else {
82             Class.forName(driver);
83             return DriverManager.getConnection(url, user, password);
84         }
85     }
86     
87     public void disconnect() throws SQLException JavaDoc {
88         log("disconnect "+url+";");
89         conn.close();
90     }
91
92     public void end() throws SQLException JavaDoc {
93         log("end "+url+";");
94         if(sentinel != null) {
95             sentinel.close();
96             sentinel = null;
97         }
98     }
99
100     public void createTable(Table table) throws SQLException JavaDoc {
101         execute(table.getCreateSQL());
102     }
103
104     public void dropTable(Table table) throws SQLException JavaDoc {
105         execute(table.getDropSQL());
106     }
107     
108     public void createIndex(Index index) throws SQLException JavaDoc {
109         execute(index.getCreateSQL());
110         index.getTable().addIndex(index);
111     }
112
113     public void dropIndex(Index index) throws SQLException JavaDoc {
114         execute(index.getDropSQL());
115         index.getTable().removeIndex(index);
116     }
117
118     public Result insert(Table table, Column[] c, Value[] v) throws SQLException JavaDoc {
119         String JavaDoc sql = table.getInsertSQL(c, v);
120         execute(sql);
121         return new Result(sql, 1);
122     }
123     
124     private void execute(String JavaDoc sql) throws SQLException JavaDoc {
125         log(sql+";");
126         conn.createStatement().execute(sql);
127     }
128
129     public Result select(String JavaDoc sql) throws SQLException JavaDoc {
130         log(sql+";");
131         Statement JavaDoc stat = conn.createStatement();
132         Result result = new Result(config, sql, stat.executeQuery(sql));
133         return result;
134     }
135
136     public Result delete(Table table, String JavaDoc condition) throws SQLException JavaDoc {
137         String JavaDoc sql = "DELETE FROM " + table.getName();
138         if(condition!=null) {
139             sql += " WHERE " + condition;
140         }
141         log(sql+";");
142         Statement JavaDoc stat = conn.createStatement();
143         Result result = new Result(sql, stat.executeUpdate(sql));
144         return result;
145     }
146
147     public Result update(Table table, Column[] columns, Value[] values, String JavaDoc condition) throws SQLException JavaDoc {
148         String JavaDoc sql = "UPDATE " + table.getName() + " SET ";
149         for(int i=0; i<columns.length; i++) {
150             if(i>0) {
151                 sql += ", ";
152             }
153             sql += columns[i].getName() + "=" + values[i].getSQL();
154         }
155         if(condition!=null) {
156             sql += " WHERE " + condition;
157         }
158         log(sql+";");
159         Statement JavaDoc stat = conn.createStatement();
160         Result result = new Result(sql, stat.executeUpdate(sql));
161         return result;
162     }
163
164     public void setAutoCommit(boolean b) throws SQLException JavaDoc {
165         log("set autoCommit " + b+";");
166         conn.setAutoCommit(b);
167     }
168
169     public void commit() throws SQLException JavaDoc {
170         log("commit;");
171         conn.commit();
172     }
173     
174     public void rollback() throws SQLException JavaDoc {
175         log("rollback;");
176         conn.rollback();
177     }
178     
179     private void log(String JavaDoc s) {
180         config.log(id, s);
181     }
182     
183     public String JavaDoc toString() {
184         return url;
185     }
186     
187 }
188
Popular Tags