1 5 package org.h2.test.synth; 6 7 import java.sql.Connection ; 8 import java.sql.DatabaseMetaData ; 9 import java.sql.DriverManager ; 10 import java.sql.ResultSet ; 11 import java.sql.SQLException ; 12 import java.sql.Statement ; 13 import java.util.ArrayList ; 14 15 class DbConnection implements DbInterface { 16 private TestSynth config; 17 private int id; 18 private String driver; 19 private String url; 20 private String user; 21 private String password; 22 private Connection conn; 23 private Connection sentinel; 24 private boolean useSentinel; 25 26 DbConnection(TestSynth config, String driver, String url, String user, String 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 { 38 log("reset;"); 39 DatabaseMetaData meta = conn.getMetaData(); 40 Statement stat = conn.createStatement(); 41 ArrayList tables = new ArrayList (); 42 ResultSet rs = meta.getTables(null, null, null, new String [] { "TABLE"}); 43 while (rs.next()) { 44 String 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 table = (String ) tables.get(i); 54 stat.execute("DROP TABLE " + table); 55 dropped++; 56 tables.remove(i); 57 i--; 58 } catch (SQLException e) { 59 } 61 } 62 if (dropped == 0 && tables.size() > 0) { 64 throw new Error ("Cannot drop "+tables); 65 } 66 } 67 } 68 69 public void connect() throws Exception { 70 if(useSentinel && sentinel == null) { 71 sentinel = getConnection(); 72 } 73 log("connect to "+url+";"); 74 conn = getConnection(); 75 } 76 77 private Connection getConnection() throws Exception { 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 { 88 log("disconnect "+url+";"); 89 conn.close(); 90 } 91 92 public void end() throws SQLException { 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 { 101 execute(table.getCreateSQL()); 102 } 103 104 public void dropTable(Table table) throws SQLException { 105 execute(table.getDropSQL()); 106 } 107 108 public void createIndex(Index index) throws SQLException { 109 execute(index.getCreateSQL()); 110 index.getTable().addIndex(index); 111 } 112 113 public void dropIndex(Index index) throws SQLException { 114 execute(index.getDropSQL()); 115 index.getTable().removeIndex(index); 116 } 117 118 public Result insert(Table table, Column[] c, Value[] v) throws SQLException { 119 String sql = table.getInsertSQL(c, v); 120 execute(sql); 121 return new Result(sql, 1); 122 } 123 124 private void execute(String sql) throws SQLException { 125 log(sql+";"); 126 conn.createStatement().execute(sql); 127 } 128 129 public Result select(String sql) throws SQLException { 130 log(sql+";"); 131 Statement 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 condition) throws SQLException { 137 String sql = "DELETE FROM " + table.getName(); 138 if(condition!=null) { 139 sql += " WHERE " + condition; 140 } 141 log(sql+";"); 142 Statement 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 condition) throws SQLException { 148 String 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 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 { 165 log("set autoCommit " + b+";"); 166 conn.setAutoCommit(b); 167 } 168 169 public void commit() throws SQLException { 170 log("commit;"); 171 conn.commit(); 172 } 173 174 public void rollback() throws SQLException { 175 log("rollback;"); 176 conn.rollback(); 177 } 178 179 private void log(String s) { 180 config.log(id, s); 181 } 182 183 public String toString() { 184 return url; 185 } 186 187 } 188 | Popular Tags |