KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > util > Triggers


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.util.Triggers
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.util;
23
24 import org.apache.derby.iapi.db.*;
25 import java.sql.*;
26 import java.math.BigDecimal JavaDoc;
27 import java.math.BigInteger JavaDoc;
28
29 /**
30  * Methods for testing triggers
31  */

32 public class Triggers
33 {
34     private Triggers()
35     {
36     }
37
38     public static String JavaDoc triggerFiresMinimal(String JavaDoc string) throws Throwable JavaDoc
39     {
40         System.out.println("TRIGGER: " + "<"+string+">");
41         return "";
42     }
43
44     public static String JavaDoc triggerFires(String JavaDoc string) throws Throwable JavaDoc
45     {
46         TriggerExecutionContext tec = Factory.getTriggerExecutionContext();
47         System.out.println("TRIGGER: " + "<"+string+"> on statement "+tec.getEventStatementText());
48         printTriggerChanges();
49         return "";
50     }
51
52     public static int doNothingInt() throws Throwable JavaDoc
53     {
54         return 1;
55     }
56
57     public static void doNothing() throws Throwable JavaDoc
58     {}
59
60     public static int doConnCommitInt() throws Throwable JavaDoc
61     {
62         Connection conn = DriverManager.getConnection("jdbc:default:connection");
63         conn.commit();
64         return 1;
65     }
66
67     public static void doConnCommit() throws Throwable JavaDoc
68     {
69         Connection conn = DriverManager.getConnection("jdbc:default:connection");
70         conn.commit();
71     }
72             
73     public static void doConnRollback() throws Throwable JavaDoc
74     {
75         Connection conn = DriverManager.getConnection("jdbc:default:connection");
76         conn.rollback();
77     }
78
79     public static void doConnectionSetIsolation() throws Throwable JavaDoc
80     {
81         Connection conn = DriverManager.getConnection("jdbc:default:connection");
82         conn.setTransactionIsolation(conn.TRANSACTION_SERIALIZABLE);
83     }
84             
85     public static int doConnStmtIntNoRS(String JavaDoc text) throws Throwable JavaDoc
86     {
87         doConnStmtNoRS(text);
88         return 1;
89     }
90     public static void doConnStmtNoRS(String JavaDoc text) throws Throwable JavaDoc
91     {
92         Connection conn = DriverManager.getConnection("jdbc:default:connection");
93         Statement stmt = conn.createStatement();
94         stmt.execute(text);
95     }
96
97     public static int doConnStmtInt(String JavaDoc text) throws Throwable JavaDoc
98     {
99         doConnStmt(text);
100         return 1;
101     }
102     public static void doConnStmt(String JavaDoc text) throws Throwable JavaDoc
103     {
104         Connection conn = DriverManager.getConnection("jdbc:default:connection");
105         Statement stmt = conn.createStatement();
106         if (stmt.execute(text))
107         {
108             ResultSet rs = stmt.getResultSet();
109             while (rs.next())
110             {}
111             rs.close();
112         }
113         stmt.close();
114         conn.close();
115     }
116
117     public static void getConnection() throws Throwable JavaDoc
118     {
119         Connection conn = DriverManager.getConnection("jdbc:default:connection");
120         conn.close();
121         System.out.println("getConnection() called");
122     }
123     // used for performance numbers
124
static void zipThroughRs(ResultSet s) throws SQLException
125     {
126         if (s == null)
127             return;
128         
129         while (s.next()) ;
130     }
131
132     private static void printTriggerChanges() throws Throwable JavaDoc
133     {
134         TriggerExecutionContext tec = Factory.getTriggerExecutionContext();
135         System.out.println("BEFORE RESULT SET");
136         dumpRS(tec.getOldRowSet());
137         System.out.println("\nAFTER RESULT SET");
138         dumpRS(tec.getNewRowSet());
139     }
140
141     // lifted from the metadata test
142
private static void dumpRS(ResultSet s) throws SQLException
143     {
144         if (s == null)
145         {
146             System.out.println("<NULL>");
147             return;
148         }
149
150         ResultSetMetaData rsmd = s.getMetaData();
151
152         // Get the number of columns in the result set
153
int numCols = rsmd.getColumnCount();
154
155         if (numCols <= 0)
156         {
157             System.out.println("(no columns!)");
158             return;
159         }
160
161         StringBuffer JavaDoc heading = new StringBuffer JavaDoc("\t ");
162         StringBuffer JavaDoc underline = new StringBuffer JavaDoc("\t ");
163
164         int len;
165         // Display column headings
166
for (int i=1; i<=numCols; i++)
167         {
168             if (i > 1)
169             {
170                 heading.append(",");
171                 underline.append(" ");
172             }
173             len = heading.length();
174             heading.append(rsmd.getColumnLabel(i));
175             len = heading.length() - len;
176             for (int j = len; j > 0; j--)
177             {
178                 underline.append("-");
179             }
180         }
181         System.out.println(heading.toString());
182         System.out.println(underline.toString());
183         
184     
185         StringBuffer JavaDoc row = new StringBuffer JavaDoc();
186         // Display data, fetching until end of the result set
187
while (s.next())
188         {
189             row.append("\t{");
190             // Loop through each column, getting the
191
// column data and displaying
192
for (int i=1; i<=numCols; i++)
193             {
194                 if (i > 1) row.append(",");
195                 row.append(s.getString(i));
196             }
197             row.append("}\n");
198         }
199         System.out.println(row.toString());
200         s.close();
201     }
202
203     public static long returnPrimLong(long x)
204     {
205         return x;
206     }
207
208     public static Long JavaDoc returnLong(Long JavaDoc x)
209     {
210         return x;
211     }
212
213
214 }
Popular Tags