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.io.PrintWriter;
009: import java.io.StringWriter;
010: import java.sql.ResultSet;
011: import java.sql.ResultSetMetaData;
012: import java.sql.SQLException;
013: import java.util.ArrayList;
014: import java.util.Collections;
015:
016: import org.h2.test.TestBase;
017:
018: /**
019: * Represents an in-memory result.
020: */
021: class Result implements Comparable {
022: static final int SUCCESS = 0, BOOLEAN = 1, INT = 2, EXCEPTION = 3,
023: RESULT_SET = 4;
024: private int type;
025: private boolean bool;
026: private int intValue;
027: private SQLException exception;
028: private ArrayList rows;
029: private ArrayList header;
030: String sql;
031:
032: Result(String sql) {
033: this .sql = sql;
034: type = SUCCESS;
035: }
036:
037: Result(String sql, SQLException e) {
038: this .sql = sql;
039: type = EXCEPTION;
040: exception = e;
041: }
042:
043: Result(String sql, boolean b) {
044: this .sql = sql;
045: type = BOOLEAN;
046: this .bool = b;
047: }
048:
049: Result(String sql, int i) {
050: this .sql = sql;
051: type = INT;
052: this .intValue = i;
053: }
054:
055: Result(TestSynth config, String sql, ResultSet rs) {
056: this .sql = sql;
057: type = RESULT_SET;
058: try {
059: rows = new ArrayList();
060: header = new ArrayList();
061: ResultSetMetaData meta = rs.getMetaData();
062: int len = meta.getColumnCount();
063: Column[] cols = new Column[len];
064: for (int i = 0; i < len; i++) {
065: cols[i] = new Column(meta, i + 1);
066: }
067: while (rs.next()) {
068: Row row = new Row(config, rs, len);
069: rows.add(row);
070: }
071: Collections.sort(rows);
072: } catch (SQLException e) {
073: // type = EXCEPTION;
074: // exception = e;
075: TestBase.logError("error reading result set", e);
076: }
077: }
078:
079: public String toString() {
080: switch (type) {
081: case SUCCESS:
082: return "success";
083: case BOOLEAN:
084: return "boolean: " + this .bool;
085: case INT:
086: return "int: " + this .intValue;
087: case EXCEPTION: {
088: StringWriter w = new StringWriter();
089: exception.printStackTrace(new PrintWriter(w));
090: return "exception: " + exception.getSQLState() + ": "
091: + exception.getMessage() + "\r\n" + w.toString();
092: }
093: case RESULT_SET:
094: String result = "ResultSet { // size=" + rows.size()
095: + "\r\n ";
096: for (int i = 0; i < header.size(); i++) {
097: Column column = (Column) header.get(i);
098: result += column.toString() + "; ";
099: }
100: result += "} = {\r\n";
101: for (int i = 0; i < rows.size(); i++) {
102: Row row = (Row) rows.get(i);
103: result += " { " + row.toString() + "};\r\n";
104: }
105: return result + "}";
106: default:
107: throw new Error("internal");
108: }
109: }
110:
111: public int compareTo(Object o) {
112: Result r = (Result) o;
113: switch (type) {
114: case EXCEPTION:
115: if (r.type != EXCEPTION) {
116: return 1;
117: }
118: return 0;
119: // return
120: // exception.getSQLState().compareTo(r.exception.getSQLState());
121: case BOOLEAN:
122: case INT:
123: case SUCCESS:
124: case RESULT_SET:
125: return toString().compareTo(r.toString());
126: default:
127: throw new Error("internal");
128: }
129: }
130:
131: public void log() {
132: switch (type) {
133: case SUCCESS:
134: System.out.println("> ok");
135: break;
136: case EXCEPTION:
137: System.out.println("> exception");
138: break;
139: case INT:
140: if (intValue == 0) {
141: System.out.println("> ok");
142: } else {
143: System.out.println("> update count: " + intValue);
144: }
145: break;
146: case RESULT_SET:
147: System.out.println("> rs " + rows.size());
148: break;
149: }
150: System.out.println();
151: }
152:
153: }
|