1 5 package org.h2.test.db; 6 7 import java.io.File ; 8 import java.io.FileOutputStream ; 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.io.InputStreamReader ; 12 import java.io.LineNumberReader ; 13 import java.io.PrintStream ; 14 import java.sql.Connection ; 15 import java.sql.PreparedStatement ; 16 import java.sql.ResultSet ; 17 import java.sql.ResultSetMetaData ; 18 import java.sql.SQLException ; 19 import java.sql.Statement ; 20 import java.util.ArrayList ; 21 22 import org.h2.test.TestAll; 23 import org.h2.test.TestBase; 24 import org.h2.util.StringUtils; 25 26 public class TestScript extends TestBase { 27 28 private boolean failFast; 29 30 private boolean alwaysReconnect; 31 private Connection conn; 32 private Statement stat; 33 private LineNumberReader in; 34 private int line; 35 private PrintStream out; 36 private ArrayList result = new ArrayList (); 37 private String putback; 38 private StringBuffer errors; 39 private ArrayList statements; 40 private String fileName = "org/h2/test/test.in.txt"; 41 42 public ArrayList getAllStatements(TestAll conf, String file) throws Exception { 43 config = conf; 44 fileName = file; 45 statements = new ArrayList (); 46 test(); 47 return statements; 48 } 49 50 public void test() throws Exception { 51 if(config.networked && config.big) { 52 return; 53 } 54 alwaysReconnect = false; 55 testScript(); 56 if(!config.memory) { 57 if(config.big) { 58 alwaysReconnect = true; 59 testScript(); 60 } 61 } 62 } 63 64 public void testScript() throws Exception { 65 deleteDb("script"); 66 String outfile = "test.out.txt"; 67 String infile = fileName; 68 conn = getConnection("script"); 69 stat = conn.createStatement(); 70 out = new PrintStream (new FileOutputStream (outfile)); 71 errors = new StringBuffer (); 72 testFile(infile); 73 conn.close(); 74 out.close(); 75 if(errors.length()>0) { 76 throw new Exception ("errors:\n" + errors.toString()); 77 } else { 78 new File (outfile).delete(); 79 } 80 } 81 82 private String readLine() throws IOException { 83 if (putback != null) { 84 String s = putback; 85 putback = null; 86 return s; 87 } 88 while(true) { 89 String s = in.readLine(); 90 if(s==null) { 91 return s; 92 } 93 s = s.trim(); 94 if(s.length() > 0) { 95 return s; 96 } 97 } 98 } 99 100 private void testFile(String infile) throws Exception { 101 InputStream is = getClass().getClassLoader().getResourceAsStream(infile); 102 in = new LineNumberReader (new InputStreamReader (is, "Cp1252")); 103 StringBuffer buff = new StringBuffer (); 104 while (true) { 105 String sql = readLine(); 106 if (sql == null) { 107 break; 108 } 109 if (sql.startsWith("--")) { 110 write(sql); 111 } else if (sql.startsWith(">")) { 112 } else if (sql.endsWith(";")) { 114 write(sql); 115 buff.append(sql.substring(0, sql.length() - 1)); 116 sql = buff.toString(); 117 buff = new StringBuffer (); 118 process(sql); 119 } else { 120 write(sql); 121 buff.append(sql); 122 buff.append('\n'); 123 } 124 } 125 } 126 127 private boolean containsTempTables() throws SQLException { 128 ResultSet rs = conn.getMetaData().getTables(null, null, null, new String []{"TABLE"}); 129 while(rs.next()) { 130 String sql = rs.getString("SQL"); 131 if(sql != null) { 132 if(sql.indexOf("TEMPORARY") >= 0) { 133 return true; 134 } 135 } 136 } 137 return false; 138 } 139 140 private void process(String sql) throws Exception { 141 if(alwaysReconnect) { 142 if(!containsTempTables()) { 143 boolean autocommit = conn.getAutoCommit(); 144 if(autocommit) { 145 conn.close(); 146 conn = getConnection("script"); 147 conn.setAutoCommit(autocommit); 148 stat = conn.createStatement(); 149 } 150 } 151 } 152 if(statements != null) { 153 statements.add(sql); 154 } 155 if (sql.indexOf('?') == -1) { 156 processStatement(sql); 157 } else { 158 String param = readLine(); 159 write(param); 160 if (!param.equals("{")) { 161 throw new Error ("expected '{', got " + param + " in " + sql); 162 } 163 try { 164 PreparedStatement prep = conn.prepareStatement(sql); 165 int count = 0; 166 while (true) { 167 param = readLine(); 168 write(param); 169 if (param.startsWith("}")) { 170 break; 171 } 172 count += processPrepared(sql, prep, param); 173 } 174 writeResult("update count: " + count, null); 175 } catch (SQLException e) { 176 writeException(e); 177 } 178 } 179 write(""); 180 } 181 182 private void setParameter(PreparedStatement prep, int i, String param) 183 throws SQLException { 184 if (param.equalsIgnoreCase("null")) { 185 param = null; 186 } 187 prep.setString(i, param); 188 } 189 190 private int processPrepared(String sql, PreparedStatement prep, String param) 191 throws Exception { 192 try { 193 StringBuffer buff = new StringBuffer (); 194 int index = 0; 195 for (int i = 0; i < param.length(); i++) { 196 char c = param.charAt(i); 197 if (c == ',') { 198 setParameter(prep, ++index, buff.toString()); 199 buff = new StringBuffer (); 200 } else if (c == '"') { 201 while (true) { 202 c = param.charAt(++i); 203 if (c == '"') { 204 break; 205 } 206 buff.append(c); 207 } 208 } else if (c > ' ') { 209 buff.append(c); 210 } 211 } 212 if (buff.length() > 0) { 213 setParameter(prep, ++index, buff.toString()); 214 } 215 if (prep.execute()) { 216 writeResultSet(sql, prep.getResultSet()); 217 return 0; 218 } 219 return prep.getUpdateCount(); 220 } catch (SQLException e) { 221 writeException(e); 222 return 0; 223 } 224 } 225 226 private int processStatement(String sql) throws Exception { 227 try { 228 if (stat.execute(sql)) { 229 writeResultSet(sql, stat.getResultSet()); 230 } else { 231 int count = stat.getUpdateCount(); 232 writeResult(count < 1 ? "ok" : "update count: " + count, null); 233 } 234 } catch (SQLException e) { 235 writeException(e); 236 } 237 return 0; 238 } 239 240 private String formatString(String s) { 241 if (s== null) { 242 return "null"; 243 } 244 return s.replace('\n', ' '); 245 } 246 247 private void writeResultSet(String sql, ResultSet rs) throws Exception { 248 boolean ordered = StringUtils.toLowerEnglish(sql).indexOf("order by") >= 0; 249 ResultSetMetaData meta = rs.getMetaData(); 250 int len = meta.getColumnCount(); 251 int[] max = new int[len]; 252 String [] head = new String [len]; 253 for (int i = 0; i < len; i++) { 254 String label = formatString(meta.getColumnLabel(i + 1)); 255 max[i] = label.length(); 256 head[i] = label; 257 } 258 result.clear(); 259 while (rs.next()) { 260 String [] row = new String [len]; 261 for (int i = 0; i < len; i++) { 262 String data = formatString(rs.getString(i + 1)); 263 if (max[i] < data.length()) { 264 max[i] = data.length(); 265 } 266 row[i] = data; 267 } 268 result.add(row); 269 } 270 rs.close(); 271 writeResult(format(head, max), null); 272 writeResult(format(null, max), null); 273 String [] array = new String [result.size()]; 274 for (int i = 0; i < result.size(); i++) { 275 array[i] = format((String []) result.get(i), max); 276 } 277 if (!ordered) { 278 sort(array); 279 } 280 int i = 0; 281 for (; i < array.length; i++) { 282 writeResult(array[i], null); 283 } 284 writeResult((ordered ? "rows (ordered): " : "rows: ") + i, null); 285 } 286 287 private String format(String [] row, int[] max) throws Exception { 288 int length = max.length; 289 StringBuffer buff = new StringBuffer (); 290 for (int i = 0; i < length; i++) { 291 if(i>0) { 292 buff.append(' '); 293 } 294 if (row == null) { 295 for (int j = 0; j < max[i]; j++) { 296 buff.append('-'); 297 } 298 } else { 299 int len = row[i].length(); 300 buff.append(row[i]); 301 if(i < length - 1) { 302 for (int j = len; j < max[i]; j++) { 303 buff.append(' '); 304 } 305 } 306 } 307 } 308 return buff.toString(); 309 } 310 311 private void writeException(SQLException e) throws Exception { 312 writeResult("exception", e); 313 } 314 315 private void writeResult(String s, SQLException e) throws Exception { 316 checkNotGeneralException(e); 317 s = ("> " + s).trim(); 318 String compare = readLine(); 319 if (compare != null && compare.startsWith(">")) { 320 if (!compare.equals(s)) { 321 errors.append("line: "); 322 errors.append(line); 323 errors.append("\nexp: "); 324 errors.append(compare); 325 errors.append("\ngot: "); 326 errors.append(s); 327 errors.append("\n"); 328 if(e != null) { 329 TestBase.logError("script", e); 330 } 331 if(failFast) { 332 TestBase.logError(errors.toString(), null); 333 conn.close(); 334 System.exit(1); 335 } 336 } 337 } else { 338 putback = compare; 339 } 340 write(s); 341 342 } 343 344 private void write(String s) throws Exception { 345 line++; 346 out.println(s); 347 } 348 349 private void sort(String [] a) { 350 for (int i = 1, j, len = a.length; i < len; i++) { 351 String t = a[i]; 352 for (j = i - 1; j >= 0 && t.compareTo(a[j]) < 0; j--) { 353 a[j + 1] = a[j]; 354 } 355 a[j + 1] = t; 356 } 357 } 358 359 } 360 | Popular Tags |