001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.prepStmtMetaData
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.tests.jdbcapi;
023:
024: import java.sql.Connection;
025: import java.sql.DriverManager;
026: import java.sql.DatabaseMetaData;
027: import java.sql.ResultSetMetaData;
028: import java.sql.Statement;
029: import java.sql.PreparedStatement;
030: import java.sql.ResultSet;
031: import java.sql.SQLException;
032: import java.sql.Types;
033:
034: import org.apache.derby.tools.ij;
035:
036: /**
037: * Test of prepared statement getMetaData for statements that don't produce a
038: * result set
039: *
040: * @author peachey
041: */
042:
043: public class prepStmtMetaData {
044:
045: public static void main(String[] args) {
046: ResultSetMetaData met;
047: Connection con;
048: PreparedStatement ps;
049: System.out.println("Test prepStmtMetaData starting");
050:
051: try {
052: // use the ij utility to read the property file and
053: // make the initial connection.
054: ij.getPropertyArg(args);
055: con = ij.startJBMS();
056: con.setAutoCommit(true); // make sure it is true
057:
058: // test create table - meta data should be null
059: testMetaData(con, "create table ab(a int)", true);
060:
061: // test alter table - meta data should be null
062: testMetaData(con, "alter table ab add column b int", true);
063:
064: // for test call statement
065: Statement s = con.createStatement();
066: s
067: .execute("create procedure testproc() language java external name "
068: + "'org.apache.derbyTesting.functionTests.tests.jdbcapi.prepStmtMetaData.tstmeth'"
069: + " parameter style java");
070:
071: // test call statement - shouldn't have meta data
072: testMetaData(con, "call testproc()", false);
073:
074: // test drop procedure - meta data should be null
075: testMetaData(con, "drop procedure testproc", true);
076:
077: // test create schema - meta data should be null
078: testMetaData(con, "create schema myschema", true);
079:
080: // test drop schema - meta data should be null
081: testMetaData(con, "drop schema myschema restrict", true);
082:
083: // test create trigger - meta data should be null
084: //testMetaData(con, "create trigger mytrig after insert on ab for each row mode db2sql create table yy(a int)", true);
085:
086: // test drop trigger - meta data should be null
087: //testMetaData(con, "drop trigger mytrig", true);
088:
089: // test create view - meta data should be null
090: testMetaData(con, "create view myview as select * from ab",
091: true);
092:
093: // test drop view - meta data should be null
094: testMetaData(con, "drop view myview", true);
095:
096: // test drop table - meta data should be null
097: testMetaData(con, "drop table ab", false);
098:
099: // test create index - meta data should be null
100: testMetaData(con, "create index aindex on ab(a)", true);
101:
102: // test drop index - meta data should be null
103: testMetaData(con, "drop index aindex", false);
104:
105: // test insert - meta data should be null
106: testMetaData(con, "insert into ab values(1,1)", true);
107:
108: // test update - meta data should be null
109: testMetaData(con, "update ab set a = 2", false);
110:
111: // test delete - meta data should be null
112: testMetaData(con, "delete from ab", false);
113:
114: // test select - meta data should be provided
115: ps = con.prepareStatement("select * from ab");
116: met = ps.getMetaData();
117: System.out.println("Result meta data for select");
118: dumpRSMetaData(met);
119: ps.close();
120:
121: // test set
122: testMetaData(con, "set schema rock", false);
123:
124: // bug 4579 : PreparedStatement.getMetaData should not return stale data
125: executeStmt(con, "create table bug4579 (c11 int)");
126: executeStmt(con, "insert into bug4579 values 1");
127: testMetaData(con, "set schema rick", false);
128: ps = con.prepareStatement("select * from bug4579");
129: met = ps.getMetaData();
130: System.out
131: .println("bug 4579 and 5338 : Result meta data for select *");
132: dumpRSMetaData(met);
133: executeStmt(con, "alter table bug4579 add column c12 int");
134: met = ps.getMetaData();
135: System.out
136: .println("bug 4579 and 5338 : Result meta data for select * after alter table but w/o execute query");
137: dumpRSMetaData(met);
138: executeStmt(con, "alter table bug4579 add column c13 int");
139: ps.execute();
140: met = ps.getMetaData();
141: System.out
142: .println("bug 4579 and 5338 : Result meta data for select * after alter table and execute query");
143: dumpRSMetaData(met);
144: ps.close();
145:
146: // clean up
147: executeStmt(con, "drop table ab");
148: executeStmt(con, "drop table bug4579");
149:
150: con.close();
151: } catch (SQLException e) {
152: dumpSQLExceptions(e);
153: e.printStackTrace(System.out);
154: } catch (Throwable e) {
155: System.out.println("FAIL -- unexpected exception:");
156: e.printStackTrace(System.out);
157: }
158:
159: System.out.println("Test prepStmtMetaData finished");
160: }
161:
162: static private void testMetaData(Connection con, String statement,
163: boolean execute) {
164: try {
165: PreparedStatement ps = con.prepareStatement(statement);
166: ResultSetMetaData met = ps.getMetaData();
167: dumpRSMetaData(met);
168: if (execute)
169: ps.execute();
170: ps.close();
171:
172: } catch (SQLException e) {
173: dumpSQLExceptions(e);
174: } catch (Throwable e) {
175: System.out.println("FAIL -- unexpected exception:");
176: e.printStackTrace(System.out);
177: }
178: }
179:
180: static private void executeStmt(Connection con, String statement) {
181: try {
182: PreparedStatement ps = con.prepareStatement(statement);
183: ps.execute();
184: ps.close();
185: } catch (SQLException e) {
186: dumpSQLExceptions(e);
187: } catch (Throwable e) {
188: System.out.println("FAIL -- unexpected exception:");
189: e.printStackTrace(System.out);
190: }
191: }
192:
193: static private void dumpSQLExceptions(SQLException se) {
194: System.out.println("FAIL -- unexpected exception");
195: while (se != null) {
196: System.out.print("SQLSTATE(" + se.getSQLState() + "):");
197: se.printStackTrace(System.out);
198: se = se.getNextException();
199: }
200: }
201:
202: static void dumpRSMetaData(ResultSetMetaData rsmd)
203: throws SQLException {
204: if (rsmd == null || rsmd.getColumnCount() == 0) {
205: System.out.println("ResultSetMetaData is Null or empty");
206: return;
207: }
208:
209: // Get the number of columns in the result set
210: int numCols = rsmd.getColumnCount();
211:
212: if (numCols <= 0) {
213: System.out.println("(no columns!)");
214: return;
215: }
216:
217: // Display column headings
218: for (int i = 1; i <= numCols; i++) {
219: if (i > 1)
220: System.out.print(",");
221: System.out.print(rsmd.getColumnLabel(i));
222: }
223: System.out.println();
224: }
225:
226: public static void tstmeth() {
227: // for purpose of test, method may do nothing
228: }
229:
230: }
|