001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.util.Triggers
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.util;
023:
024: import org.apache.derby.iapi.db.*;
025: import java.sql.*;
026: import java.math.BigDecimal;
027: import java.math.BigInteger;
028:
029: /**
030: * Methods for testing triggers
031: */
032: public class Triggers {
033: private Triggers() {
034: }
035:
036: public static String triggerFiresMinimal(String string)
037: throws Throwable {
038: System.out.println("TRIGGER: " + "<" + string + ">");
039: return "";
040: }
041:
042: public static String triggerFires(String string) throws Throwable {
043: TriggerExecutionContext tec = Factory
044: .getTriggerExecutionContext();
045: System.out.println("TRIGGER: " + "<" + string
046: + "> on statement " + tec.getEventStatementText());
047: printTriggerChanges();
048: return "";
049: }
050:
051: public static int doNothingInt() throws Throwable {
052: return 1;
053: }
054:
055: public static void doNothing() throws Throwable {
056: }
057:
058: public static int doConnCommitInt() throws Throwable {
059: Connection conn = DriverManager
060: .getConnection("jdbc:default:connection");
061: conn.commit();
062: return 1;
063: }
064:
065: public static void doConnCommit() throws Throwable {
066: Connection conn = DriverManager
067: .getConnection("jdbc:default:connection");
068: conn.commit();
069: }
070:
071: public static void doConnRollback() throws Throwable {
072: Connection conn = DriverManager
073: .getConnection("jdbc:default:connection");
074: conn.rollback();
075: }
076:
077: public static void doConnectionSetIsolation() throws Throwable {
078: Connection conn = DriverManager
079: .getConnection("jdbc:default:connection");
080: conn.setTransactionIsolation(conn.TRANSACTION_SERIALIZABLE);
081: }
082:
083: public static int doConnStmtIntNoRS(String text) throws Throwable {
084: doConnStmtNoRS(text);
085: return 1;
086: }
087:
088: public static void doConnStmtNoRS(String text) throws Throwable {
089: Connection conn = DriverManager
090: .getConnection("jdbc:default:connection");
091: Statement stmt = conn.createStatement();
092: stmt.execute(text);
093: }
094:
095: public static int doConnStmtInt(String text) throws Throwable {
096: doConnStmt(text);
097: return 1;
098: }
099:
100: public static void doConnStmt(String text) throws Throwable {
101: Connection conn = DriverManager
102: .getConnection("jdbc:default:connection");
103: Statement stmt = conn.createStatement();
104: if (stmt.execute(text)) {
105: ResultSet rs = stmt.getResultSet();
106: while (rs.next()) {
107: }
108: rs.close();
109: }
110: stmt.close();
111: conn.close();
112: }
113:
114: public static void getConnection() throws Throwable {
115: Connection conn = DriverManager
116: .getConnection("jdbc:default:connection");
117: conn.close();
118: System.out.println("getConnection() called");
119: }
120:
121: // used for performance numbers
122: static void zipThroughRs(ResultSet s) throws SQLException {
123: if (s == null)
124: return;
125:
126: while (s.next())
127: ;
128: }
129:
130: private static void printTriggerChanges() throws Throwable {
131: TriggerExecutionContext tec = Factory
132: .getTriggerExecutionContext();
133: System.out.println("BEFORE RESULT SET");
134: dumpRS(tec.getOldRowSet());
135: System.out.println("\nAFTER RESULT SET");
136: dumpRS(tec.getNewRowSet());
137: }
138:
139: // lifted from the metadata test
140: private static void dumpRS(ResultSet s) throws SQLException {
141: if (s == null) {
142: System.out.println("<NULL>");
143: return;
144: }
145:
146: ResultSetMetaData rsmd = s.getMetaData();
147:
148: // Get the number of columns in the result set
149: int numCols = rsmd.getColumnCount();
150:
151: if (numCols <= 0) {
152: System.out.println("(no columns!)");
153: return;
154: }
155:
156: StringBuffer heading = new StringBuffer("\t ");
157: StringBuffer underline = new StringBuffer("\t ");
158:
159: int len;
160: // Display column headings
161: for (int i = 1; i <= numCols; i++) {
162: if (i > 1) {
163: heading.append(",");
164: underline.append(" ");
165: }
166: len = heading.length();
167: heading.append(rsmd.getColumnLabel(i));
168: len = heading.length() - len;
169: for (int j = len; j > 0; j--) {
170: underline.append("-");
171: }
172: }
173: System.out.println(heading.toString());
174: System.out.println(underline.toString());
175:
176: StringBuffer row = new StringBuffer();
177: // Display data, fetching until end of the result set
178: while (s.next()) {
179: row.append("\t{");
180: // Loop through each column, getting the
181: // column data and displaying
182: for (int i = 1; i <= numCols; i++) {
183: if (i > 1)
184: row.append(",");
185: row.append(s.getString(i));
186: }
187: row.append("}\n");
188: }
189: System.out.println(row.toString());
190: s.close();
191: }
192:
193: public static long returnPrimLong(long x) {
194: return x;
195: }
196:
197: public static Long returnLong(Long x) {
198: return x;
199: }
200:
201: }
|