KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > lang > forbitdata


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.lang.forbitdata
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.tests.lang;
23 import java.sql.*;
24
25 import org.apache.derby.tools.ij;
26 import java.io.*;
27 import java.math.BigInteger JavaDoc;
28 import java.math.BigDecimal JavaDoc;
29
30 public class forbitdata
31 {
32
33     static private boolean isDB2jNet;
34
35     public static void main (String JavaDoc[] argv) throws Throwable JavaDoc
36     {
37         try {
38         
39         ij.getPropertyArg(argv);
40             Connection conn = ij.startJBMS();
41             // waiting for meta data
42
String JavaDoc framework = System.getProperty("framework");
43             if (framework != null && framework.toUpperCase().equals("DB2JNET"))
44                 isDB2jNet = true;
45
46             runTests( conn);
47         } catch (Throwable JavaDoc t) {
48             System.out.println("FAIL " + t);
49             t.printStackTrace(System.out);
50         }
51     }
52
53     public static void runTests( Connection conn) throws Throwable JavaDoc
54     {
55         try {
56             testNegative(conn);
57             testTypes(conn);
58             testValues(conn);
59             testCompare(conn);
60             testEncodedLengths(conn);
61
62         } catch (SQLException sqle) {
63             org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException(System.out, sqle);
64             sqle.printStackTrace(System.out);
65         }
66         
67     }
68
69     /**
70         Negative for bit data tests.
71         FBD001,FBD007 negative syntax
72         FBD005 maximum char length
73         FBD009 maximum varchar length
74     */

75     public static void testNegative(Connection conn) throws SQLException {
76
77         System.out.println("START testNegative");
78
79         Statement s = conn.createStatement();
80
81         //
82
statementExceptionExpected(s, "CREATE TABLE FBDFAIL.T001 (C001 CHAR(255) FOR BIT DATA)");
83         statementExceptionExpected(s, "CREATE TABLE FBDFAIL.T002 (C002 VARCHAR(32673) FOR BIT DATA)");
84         statementExceptionExpected(s, "CREATE TABLE FBDFAIL.T003 (C003 VARCHAR FOR BIT DATA)");
85         statementExceptionExpected(s, "CREATE TABLE FBDFAIL.T004 (C004 LONG VARCHAR(100) FOR BIT DATA)");
86         
87         s.close();
88         System.out.println("END testNegative");
89     }
90
91     /**
92         FBD001,FBD007 - positive syntax
93         FBD004 - CHAR length defaults to one
94         FBD037 - create table
95         FBD006, FBD011, FBD014 - correct JDBC type
96
97     */

98     public static void testTypes(Connection conn) throws SQLException {
99
100         System.out.println("START testTypes");
101
102         Statement s = conn.createStatement();
103
104         for (int i = 1; i <= 8; i++)
105             executeDrop(s, "DROP TABLE FBDOK.T00" + i);
106
107         // FBD037
108

109         executeOK(s, "CREATE TABLE FBDOK.T001 (C001 CHAR FOR BIT DATA)");
110         executeOK(s, "CREATE TABLE FBDOK.T002 (C002 CHAR(1) FOR BIT DATA)");
111         executeOK(s, "CREATE TABLE FBDOK.T003 (C003 CHAR(10) FOR BIT DATA)");
112         executeOK(s, "CREATE TABLE FBDOK.T004 (C004 CHAR(254) FOR BIT DATA)");
113         executeOK(s, "CREATE TABLE FBDOK.T005 (C005 VARCHAR(1) FOR BIT DATA)");
114         executeOK(s, "CREATE TABLE FBDOK.T006 (C006 VARCHAR(100) FOR BIT DATA)");
115         executeOK(s, "CREATE TABLE FBDOK.T007 (C007 VARCHAR(32672) FOR BIT DATA)");
116         executeOK(s, "CREATE TABLE FBDOK.T008 (C008 LONG VARCHAR FOR BIT DATA)");
117
118         ResultSet rs = conn.getMetaData().getColumns(null, "FBDOK", null, null);
119         while (rs.next()) {
120             // skip 1 catalog
121
System.out.print(rs.getString(2) + ",");
122             System.out.print(rs.getString(3) + ",");
123             System.out.print(rs.getString(4) + ",");
124             System.out.print(rs.getString(5) + ",");
125             System.out.print(rs.getString(6) + ",");
126             System.out.print(rs.getString(7) + ",");
127             // skip 8 - unused
128
System.out.print(rs.getString(9) + ",");
129             System.out.print(rs.getString(10) + ",");
130             System.out.print(rs.getString(11) + ",");
131             // skip 12 remarks
132
System.out.print(rs.getString(13) + ",");
133             // skip 14,15 unused
134
System.out.print(rs.getString(16) + ",");
135             System.out.print(rs.getString(17) + ",");
136             System.out.println(rs.getString(18));
137         }
138         rs.close();
139
140         for (int i = 1; i <= 8; i++) {
141             try {
142                 PreparedStatement ps = conn.prepareStatement("SELECT * FROM FBDOK.T00" + i);
143                 ResultSetMetaData rsmd = ps.getMetaData();
144                 System.out.println("TABLE FBDOK.T00" + i);
145                 System.out.println(" " + rsmd.getColumnName(1) + " " + rsmd.getColumnTypeName(1) + " precision " + rsmd.getPrecision(1));
146                 ps.close();
147             } catch (SQLException sqle) {
148                 showSQLE(sqle);
149             }
150
151         }
152
153         for (int i = 1; i <= 8; i++)
154             executeDrop(s, "DROP TABLE FBDOK.T00" + i);
155
156         s.execute("DROP SCHEMA FBDOK RESTRICT");
157
158
159         System.out.println("DATABASE META DATA.getTypeInfo()");
160         DatabaseMetaData dmd = conn.getMetaData();
161
162         rs = dmd.getTypeInfo();
163         while (rs.next()) {
164             String JavaDoc name = rs.getString(1);
165             int jdbcType = rs.getInt(2);
166             switch (jdbcType) {
167             case Types.BINARY:
168             case Types.VARBINARY:
169             case Types.LONGVARBINARY:
170                 break;
171             default:
172                 continue;
173             }
174
175             System.out.print(name + "(" + jdbcType + ") ");
176             System.out.print("precision " + rs.getInt(3));
177             System.out.println("");
178         }
179
180         rs.close();
181
182         {
183         String JavaDoc sql = "VALUES X'2345d45a2e44'";
184         PreparedStatement psv = conn.prepareStatement(sql);
185         ResultSetMetaData rsmd = psv.getMetaData();
186         System.out.println(sql);
187         System.out.println(" " + rsmd.getColumnName(1) + " " + rsmd.getColumnTypeName(1) + " precision " + rsmd.getPrecision(1));
188         }
189
190         {
191         String JavaDoc sql = "VALUES X''";
192         PreparedStatement psv = conn.prepareStatement(sql);
193         ResultSetMetaData rsmd = psv.getMetaData();
194         System.out.println(sql);
195         System.out.println(" " + rsmd.getColumnName(1) + " " + rsmd.getColumnTypeName(1) + " precision " + rsmd.getPrecision(1));
196         }
197
198
199         s.close();
200         System.out.println("END testTypes");
201     }
202
203     public static void testCast(Connection conn) throws SQLException {
204     }
205
206     public static void testValues(Connection conn) throws SQLException {
207
208         System.out.println("START testValues");
209
210         Statement s = conn.createStatement();
211         executeDrop(s, "DROP TABLE FBDVAL.T001");
212         executeDrop(s, "DROP TABLE FBDVAL.X001");
213         s.execute("CREATE TABLE FBDVAL.T001(ID INT NOT NULL PRIMARY KEY, C1 CHAR(10) FOR BIT DATA, C2 VARCHAR(10) FOR BIT DATA, C3 LONG VARCHAR FOR BIT DATA, C4 BLOB(10))");
214         PreparedStatement psI = conn.prepareStatement("INSERT INTO FBDVAL.T001 VALUES(?, ?, ?, ?, ?)");
215         PreparedStatement psS = conn.prepareStatement("SELECT C1, C2, C3, C4, ID FROM FBDVAL.T001 WHERE ID >= ? AND ID < ? ORDER BY ID");
216
217
218         System.out.println("**** NULL");
219         insertData(psI, 0, null, 10, true);
220         showData(psS, 0, null);
221
222         System.out.println("**** 7 bytes (EMPTY)");
223         byte[] empty = new byte[7];
224         insertData(psI, 10, empty, 10, true);
225         showData(psS, 10, empty);
226
227         // DB2
228
// CHAR -- FAIL TOO BIG
229
// VARCHAR -- FAIL TOO BIG
230
// LONG VARCHAR -- OK
231
// BLOB -- FAIL TOO BIG
232
System.out.println("**** 15 bytes (EMPTY)");
233         byte[] empty2 = new byte[15];
234         insertData(psI, 20, empty2, 10, true);
235         showData(psS, 20, empty2);
236
237         // DB2 - ALL OK
238
System.out.println("**** 4 bytes");
239         byte[] four = new byte[4];
240         four[0] = (byte) 0x04;
241         four[1] = (byte) 0x23;
242         four[2] = (byte) 0xA2;
243         four[3] = (byte) 0xFD;
244
245         insertData(psI, 30, four, 10, true);
246         showData(psS, 30, four);
247
248         // DB2 - ALL OK
249
System.out.println("**** 10 bytes");
250         byte[] ten = new byte[10];
251         ten[0] = (byte) 0x0B;
252         ten[1] = (byte) 0x27;
253         ten[2] = (byte) 0xA2;
254         ten[3] = (byte) 0xFD;
255         ten[4] = (byte) 0x01;
256         ten[5] = (byte) 0x6D;
257         ten[6] = (byte) 0xE2;
258         ten[7] = (byte) 0x35;
259         ten[8] = (byte) 0x66;
260         ten[9] = (byte) 0x90;
261
262         insertData(psI, 40, ten, 10, true);
263         showData(psS, 40, ten);
264
265         // DB2
266
// CHAR -- FAIL TOO BIG
267
// VARCHAR -- FAIL TOO BIG
268
// LONG VARCHAR -- OK
269
// BLOB -- FAIL TOO BIG
270
System.out.println("**** 15 bytes");
271         byte[] l15 = new byte[15];
272         l15[0] = (byte) 0xEB;
273         l15[1] = (byte) 0xCA;
274         l15[2] = (byte) 0xFE;
275         l15[3] = (byte) 0xBA;
276         l15[4] = (byte) 0xBE;
277         l15[5] = (byte) 0xFE;
278         l15[6] = (byte) 0xED;
279         l15[7] = (byte) 0xFA;
280         l15[8] = (byte) 0xCE;
281         l15[9] = (byte) 0x24;
282         l15[10] = (byte) 0x78;
283         l15[11] = (byte) 0x43;
284         l15[12] = (byte) 0x92;
285         l15[13] = (byte) 0x31;
286         l15[14] = (byte) 0x6D;
287
288         insertData(psI, 50, l15, 10, true);
289         showData(psS, 50, l15);
290
291         // DB2 UDB LUW no truncation of spaces for VARCHAR FBD, LONG VARCHAR FBD
292
System.out.println("**** 4 spaces ");
293         byte[] space4 = new byte[4];
294         space4[0] = (byte) 0x20;
295         space4[1] = (byte) 0x20;
296         space4[2] = (byte) 0x20;
297         space4[3] = (byte) 0x20;
298         insertData(psI, 60, space4, 10, true);
299         showData(psS, 60, space4);
300
301
302         // DB2 UDB LUW no truncation of spaces for VARCHAR FBD, LONG VARCHAR FBD
303
System.out.println("**** 6 data with trailing space ");
304         byte[] space6 = new byte[6];
305         space6[0] = (byte) 0xca;
306         space6[1] = (byte) 0xfe;
307         space6[2] = (byte) 0x20;
308         space6[3] = (byte) 0x20;
309         space6[4] = (byte) 0x20;
310         space6[5] = (byte) 0x20;
311         insertData(psI, 70, space6, 10, true);
312         showData(psS, 70, space6);
313
314         // DB2
315
// CHAR -- FAIL TOO BIG
316
// VARCHAR -- FAIL TOO BIG
317
// LONG VARCHAR -- OK
318
// BLOB -- FAIL TOO BIG
319
System.out.println("**** 12 data with trailing space ");
320         byte[] space12 = new byte[12];
321         space12[0] = (byte) 0xca;
322         space12[1] = (byte) 0xfe;
323         space12[2] = (byte) 0x20;
324         space12[3] = (byte) 0x20;
325         space12[4] = (byte) 0x20;
326         space12[5] = (byte) 0x20;
327         space12[6] = (byte) 0xca;
328         space12[7] = (byte) 0xfe;
329         space12[8] = (byte) 0x20;
330         space12[9] = (byte) 0x20;
331         space12[10] = (byte) 0x20;
332         space12[11] = (byte) 0x20;
333         insertData(psI, 210, space12, 10, true);
334         showData(psS, 210, space12);
335
336
337         String JavaDoc sql = "INSERT INTO FBDVAL.T001 VALUES(80, X'2020202020', X'2020202020', X'2020202020', null)";
338         System.out.println("**** " + sql);
339         s.executeUpdate(sql);
340         showData(psS, 80, space4);
341
342         // With a literal the value is truncated into CHAR FBD
343
sql = "INSERT INTO FBDVAL.T001 VALUES(90, X'CAFE20202020CAFE20202020', null, null, null)";
344         System.out.println("**** " + sql);
345         s.executeUpdate(sql);
346         showData(psS, 90, space12);
347
348         sql = "INSERT INTO FBDVAL.T001 VALUES(100, null, X'CAFE20202020CAFE20202020', null, null)";
349         System.out.println("**** " + sql);
350         s.executeUpdate(sql);
351         showData(psS, 100, space12);
352
353         sql = "INSERT INTO FBDVAL.T001 VALUES(110, null, null, X'CAFE20202020CAFE20202020', null)";
354         System.out.println("**** " + sql);
355         s.executeUpdate(sql);
356         showData(psS, 110, space12);
357 /*
358         sql = "INSERT INTO FBDVAL.T001 VALUES(150, null, null, null, X'CAFE20202020CAFE20202020')";
359         System.out.println("**** " + sql);
360         s.executeUpdate(sql);
361         showData(psS, 150, space12);
362 */

363         // insert with non-trailing blank from literal
364
// DB2 22001 error.
365
sql = "INSERT INTO FBDVAL.T001 VALUES(120, X'CAFE20202020CAFE20202020DD', null, null, null)";
366         System.out.println("**** " + sql);
367         try {
368             s.executeUpdate(sql);
369             System.out.println("FAIL - literal too long on CHAR FBD");
370         } catch (SQLException sqle) {
371             if ("22001".equals(sqle.getSQLState()))
372                 System.out.println("22001 truncation error");
373             else
374                 showSQLE(sqle);
375         }
376
377         sql = "INSERT INTO FBDVAL.T001 VALUES(130, null, X'CAFE20202020CAFE20202020DD', null, null)";
378         System.out.println("**** " + sql);
379         try {
380             s.executeUpdate(sql);
381             System.out.println("FAIL - literal too long on VARCHAR FBD");
382         } catch (SQLException sqle) {
383             if ("22001".equals(sqle.getSQLState()))
384                 System.out.println("22001 truncation error");
385             else
386                 showSQLE(sqle);
387         }
388
389         sql = "INSERT INTO FBDVAL.T001 VALUES(140, null, null, X'CAFE20202020CAFE20202020DD', null)";
390         System.out.println("**** " + sql);
391         s.executeUpdate(sql);
392         showData(psS, 140, space12);
393
394         s.execute("CREATE TABLE FBDVAL.X001(XID INT NOT NULL PRIMARY KEY, X1 CHAR(12) FOR BIT DATA, C2 VARCHAR(12) FOR BIT DATA, C3 LONG VARCHAR FOR BIT DATA, C4 BLOB(12))");
395
396         sql = "INSERT INTO FBDVAL.X001 VALUES(200, X'CAFE20202020CAFE20202020', null, null, null)";
397         System.out.println("**** " + sql);
398         s.executeUpdate(sql);
399
400
401         sql = "INSERT INTO FBDVAL.T001 SELECT * FROM FBDVAL.X001";
402         System.out.println("**** " + sql);
403         s.executeUpdate(sql);
404         showData(psS, 200, space12);
405
406         System.out.println("END testValues");
407     }
408
409     private static void insertData(PreparedStatement psI, int id, byte[] original, int maxLen, boolean streamAsWell) throws SQLException {
410
411         int ol = original == null ? 0: original.length;
412
413         if (original == null || original.length <= maxLen) {
414             // simple case.
415
psI.setInt(1, id);
416             psI.setBytes(2, original);
417             psI.setBytes(3, original);
418             psI.setBytes(4, original);
419             psI.setBytes(5, original);
420             psI.executeUpdate();
421
422             if (streamAsWell) {
423                 psI.setInt(1, id+1);
424                 psI.setBinaryStream(2, original == null ? null : new ByteArrayInputStream(original), ol);
425                 psI.setBinaryStream(3, original == null ? null : new ByteArrayInputStream(original), ol);
426                 psI.setBinaryStream(4, original == null ? null : new ByteArrayInputStream(original), ol);
427                 psI.setBinaryStream(5, original == null ? null : new ByteArrayInputStream(original), ol);
428                 psI.executeUpdate();
429             }
430             return;
431         }
432
433         boolean okI1;
434         boolean okI2;
435
436         // Insert potentially out of range value one at a time into the table
437
System.out.println(" >> CHAR FOR BIT DATA");
438         try {
439         psI.setInt(1, id);
440         psI.setBytes(2, original);
441         psI.setBytes(3, null);
442         psI.setBytes(4, null);
443         psI.setBytes(5, null);
444         psI.executeUpdate();
445         okI1 = true;
446         } catch (SQLException sqle) {
447             okI1 = false;
448             if ("22001".equals(sqle.getSQLState())) {
449                 System.out.println("22001 truncation error");
450             } else
451                 showSQLE(sqle);
452         }
453         if (streamAsWell) {
454             try {
455             psI.setInt(1, id+1);
456             psI.setBinaryStream(2, original == null ? null : new ByteArrayInputStream(original), ol);
457             psI.executeUpdate();
458             okI2 = true;
459             } catch (SQLException sqle) {
460                 okI2 = false;
461                 if ("22001".equals(sqle.getSQLState())) {
462                     System.out.println("22001 truncation error");
463                 } else
464                     showSQLE(sqle);
465             }
466
467             if (okI1 != okI2)
468                 System.out.println("FAIL - mismatched failures");
469         }
470
471         System.out.println(" >> VARCHAR FOR BIT DATA");
472         try {
473         psI.setInt(1, id+2);
474         psI.setBytes(2, null);
475         psI.setBytes(3, original);
476         psI.setBytes(4, null);
477         psI.setBytes(5, null);
478         psI.executeUpdate();
479         okI1 = true;
480
481         } catch (SQLException sqle) {
482             okI1 = false;
483             if ("22001".equals(sqle.getSQLState()))
484                 System.out.println("22001 truncation error");
485             else
486                 showSQLE(sqle);
487         }
488         if (streamAsWell) {
489             try {
490             psI.setInt(1, id+3);
491             psI.setBinaryStream(3, original == null ? null : new ByteArrayInputStream(original), ol);
492             psI.executeUpdate();
493             okI2 = true;
494
495             } catch (SQLException sqle) {
496                 okI2 = false;
497                 if ("22001".equals(sqle.getSQLState()))
498                     System.out.println("22001 truncation error");
499                 else
500                     showSQLE(sqle);
501             }
502             if (okI1 != okI2)
503                 System.out.println("FAIL - mismatched failures");
504         }
505
506         System.out.println(" >> LONG VARCHAR FOR BIT DATA");
507         try {
508         psI.setInt(1, id+4);
509         psI.setBytes(2, null);
510         psI.setBytes(3, null);
511         psI.setBytes(4, original);
512         psI.setBytes(5, null);
513         psI.executeUpdate();
514         okI1 = true;
515         } catch (SQLException sqle) {
516             okI1 = false;
517             if ("22001".equals(sqle.getSQLState()))
518                 System.out.println("22001 truncation error");
519             else
520                 showSQLE(sqle);
521         }
522
523         if (streamAsWell) {
524             try {
525             psI.setInt(1, id+5);
526             psI.setBinaryStream(4, original == null ? null : new ByteArrayInputStream(original), ol);
527             psI.executeUpdate();
528             okI2 = true;
529             } catch (SQLException sqle) {
530                 okI2 = false;
531                 if ("22001".equals(sqle.getSQLState()))
532                     System.out.println("22001 truncation error");
533                 else
534                     showSQLE(sqle);
535             }
536             if (okI1 != okI2)
537                 System.out.println("FAIL - mismatched failures");
538         }
539
540             System.out.println(" >> BLOB");
541         try {
542         psI.setInt(1, id+6);
543         psI.setBytes(2, null);
544         psI.setBytes(3, null);
545         psI.setBytes(4, null);
546         psI.setBytes(5, original);
547         okI1 = true;
548         psI.executeUpdate();
549         } catch (SQLException sqle) {
550             okI1 = false;
551             if ("22001".equals(sqle.getSQLState()))
552                 System.out.println("22001 truncation error");
553             else
554                 showSQLE(sqle);
555         }
556         if (streamAsWell) {
557             try {
558             psI.setInt(1, id+7);
559             psI.setBinaryStream(5, original == null ? null : new ByteArrayInputStream(original), ol);
560             psI.executeUpdate();
561             okI2 = true;
562             } catch (SQLException sqle) {
563                 okI2 = false;
564                 if ("22001".equals(sqle.getSQLState()))
565                     System.out.println("22001 truncation error");
566                 else
567                     showSQLE(sqle);
568             }
569             if (okI1 != okI2)
570                 System.out.println("FAIL - mismatched failures");
571         }
572     }
573
574
575
576     
577     public static void testCompare(Connection conn) throws SQLException {
578
579         System.out.println("START testCompare");
580
581         Statement s = conn.createStatement();
582         executeDrop(s, "DROP TABLE FBDVAL.T001");
583         executeDrop(s, "DROP TABLE FBDVAL.T002");
584         s.execute("CREATE TABLE FBDVAL.T001(ID INT NOT NULL PRIMARY KEY, C1 CHAR(10) FOR BIT DATA, C2 VARCHAR(10) FOR BIT DATA, C3 LONG VARCHAR FOR BIT DATA, C4 BLOB(10))");
585         s.execute("CREATE TABLE FBDVAL.T002(ID INT NOT NULL PRIMARY KEY, C1 CHAR(10) FOR BIT DATA, C2 VARCHAR(10) FOR BIT DATA, C3 LONG VARCHAR FOR BIT DATA, C4 BLOB(10))");
586         PreparedStatement psI = conn.prepareStatement("INSERT INTO FBDVAL.T001 VALUES(?, ?, ?, ?, ?)");
587         PreparedStatement psI2 = conn.prepareStatement("INSERT INTO FBDVAL.T002 VALUES(?, ?, ?, ?, ?)");
588
589         insertData(psI, 0, null, 10, false);
590         insertData(psI2, 0, null, 10, false);
591
592         byte[] four = new byte[4];
593         four[0] = (byte) 0x04;
594         four[1] = (byte) 0x23;
595         four[2] = (byte) 0xA2;
596         four[3] = (byte) 0xFD;
597
598         insertData(psI, 30, four, 10, false);
599         insertData(psI2, 30, four, 10, false);
600         four[2] = (byte) 0xA1;
601         insertData(psI, 40, four, 10, false);
602         insertData(psI2, 40, four, 10, false);
603         four[2] = (byte) 0xA2;
604         four[3] = (byte) 0xFF;
605         insertData(psI, 50, four, 10, false);
606         insertData(psI2, 50, four, 10, false);
607
608         byte[] four_plus_space = new byte[5];
609         four_plus_space[0] = (byte) 0x04;
610         four_plus_space[1] = (byte) 0x23;
611         four_plus_space[2] = (byte) 0xA2;
612         four_plus_space[3] = (byte) 0xFD;
613         four_plus_space[4] = (byte) 0x20;
614         insertData(psI, 60, four_plus_space, 10, false);
615         insertData(psI2, 60, four_plus_space, 10, false);
616
617         byte[] ten = new byte[10];
618         ten[0] = (byte) 0x0B;
619         ten[1] = (byte) 0x27;
620         ten[2] = (byte) 0xA2;
621         ten[3] = (byte) 0xFD;
622         ten[4] = (byte) 0x01;
623         ten[5] = (byte) 0x6D;
624         ten[6] = (byte) 0xE2;
625         ten[7] = (byte) 0x35;
626         ten[8] = (byte) 0x66;
627         ten[9] = (byte) 0x90;
628
629         insertData(psI, 70, ten, 10, false);
630         insertData(psI2, 70, ten, 10, false);
631
632         String JavaDoc[] COLS = {"C1", "C2", "C3", "C4"};
633         String JavaDoc[] OPS = {"=", "<>", "<", "<=", ">", ">="};
634
635         for (int t = 0; t < COLS.length; t++) {
636             for (int o = 0; o < COLS.length; o++) {
637                 for (int a = 0; a < OPS.length; a++) {
638
639                     String JavaDoc sql = "SELECT T.ID, T." + COLS[t] + ", O.ID, O." + COLS[o] +
640                         " FROM FBDVAL.T001 O, FBDVAL.T002 T WHERE T." + COLS[t] + " " + OPS[a] + " O." + COLS[o] + " ORDER BY 1,3";
641
642                     System.out.println(sql);
643                     try {
644                         PreparedStatement psS = conn.prepareStatement(sql);
645                         showCompareData(psS);
646                     } catch (SQLException sqle) {
647                         if ("42818".equals(sqle.getSQLState()))
648                             System.out.println("42818 types not comparable " + COLS[t] + " ... " + COLS[o]);
649                         else
650                             showSQLE(sqle);
651                     }
652                     conn.commit();
653                 }
654             }
655         }
656         System.out.println("END testCompare");
657     }
658
659     /**
660         The length of a binary type is encoded when stored, this
661         test makes sure all the code paths are tested.
662         The encoded length is hidden from the JDBC client.
663     */

664     public static void testEncodedLengths(Connection conn) throws SQLException, IOException {
665
666         System.out.println("START testEncodedLengths");
667
668         Statement s = conn.createStatement();
669         executeDrop(s, "DROP TABLE FBDVAL.TEL");
670         s.execute("CREATE TABLE FBDVAL.TEL(C2 VARCHAR(32672) FOR BIT DATA, C3 LONG VARCHAR FOR BIT DATA, C4 BLOB(128k))");
671
672         PreparedStatement psi = conn.prepareStatement("INSERT INTO FBDVAL.TEL VALUES(?, ?, ?)");
673         PreparedStatement pss = conn.prepareStatement("SELECT * FROM FBDVAL.TEL");
674         PreparedStatement psd = conn.prepareStatement("DELETE FROM FBDVAL.TEL");
675
676         //insertEL(psi, pss, psd, 0);
677
insertEL(psi, pss, psd, 10);
678         insertEL(psi, pss, psd, 30);
679         insertEL(psi, pss, psd, 31);
680         insertEL(psi, pss, psd, 32); // switch to 2 byte length
681
insertEL(psi, pss, psd, 1345);
682         insertEL(psi, pss, psd, 23456);
683         insertEL(psi, pss, psd, 32672);
684         insertEL(psi, pss, psd, 32700);
685         insertEL(psi, pss, psd, (32*1024) - 1);
686         insertEL(psi, pss, psd, (32*1024));
687         insertEL(psi, pss, psd, (32*1024) + 1);
688         insertEL(psi, pss, psd, (64*1024) - 1);
689         insertEL(psi, pss, psd, (64*1024)); // switch to 4 byte length
690
insertEL(psi, pss, psd, (64*1024) + 1);
691         insertEL(psi, pss, psd, (110*1024) + 3242);
692
693
694
695
696
697         psi.close();
698         pss.close();
699         psd.close();
700         executeDrop(s, "DROP TABLE FBDVAL.TEL");
701         s.close();
702         System.out.println("END testEncodedLengths");
703
704     }
705
706     private static void insertEL(PreparedStatement psi, PreparedStatement pss, PreparedStatement psd, int length) throws SQLException, IOException {
707
708         Connection conn = psi.getConnection();
709         byte[] data = new byte[length];
710
711         // random simple value check
712
int off = (int) (System.currentTimeMillis() % ((long) length));
713         data[off] = 0x23;
714
715         psi.setBytes(1, (length <= 32672) ? data : null);
716         psi.setBytes(2, (length <= 32700) ? data : null);
717         psi.setBinaryStream(3, new java.io.ByteArrayInputStream JavaDoc(data), length); // BLOB column
718
psi.executeUpdate();
719         conn.commit();
720
721         selectData(pss,data,off,length);
722
723         conn.commit();
724
725         psd.executeUpdate();
726         conn.commit();
727
728
729         // Set values using stream and then verify that select is successful
730
psi.setBinaryStream(1, (length <= 32672) ? new java.io.ByteArrayInputStream JavaDoc(data) : null, length);
731         psi.setBinaryStream(2, (length <= 32700) ? new java.io.ByteArrayInputStream JavaDoc(data) : null, length);
732         psi.setBinaryStream(3, new java.io.ByteArrayInputStream JavaDoc(data), length); // BLOB column
733
psi.executeUpdate();
734         conn.commit();
735
736         selectData(pss,data,off,length);
737
738         conn.commit();
739
740
741         psd.executeUpdate();
742         conn.commit();
743
744
745     }
746
747     private static void selectData(PreparedStatement pss,byte[] data,int off,int length)
748         throws SQLException,IOException
749     {
750         
751         ResultSet rs = pss.executeQuery();
752         while (rs.next())
753         {
754             System.out.print(" EL byte[] " + length);
755             byte[] v = rs.getBytes(1);
756             if (v != null) {
757                 System.out.print(" C1 " + ((v.length == length) ? "OK" : ("FAIL <" + v.length + ">")));
758                 System.out.print(" DATA " + ((v[off] == 0x23) ? "OK" : ("FAIL " + off)));
759             }
760             else
761                 System.out.print(" C1 NULL");
762
763             v = rs.getBytes(2);
764             if (v != null) {
765                 System.out.print(" C2 " + ((v.length == length) ? "OK" : ("FAIL <" + v.length + ">")));
766                 System.out.print(" DATA " + ((v[off] == 0x23) ? "OK" : ("FAIL " + off)));
767             }
768             else
769                 System.out.print(" C2 NULL");
770             InputStream c3 = rs.getBinaryStream(3);
771             checkEncodedLengthValue("C3", c3, length, off);
772
773             System.out.println("");
774         }
775         rs.close();
776
777         rs = pss.executeQuery();
778         while (rs.next())
779         {
780             System.out.print(" EL stream " + length);
781
782             checkEncodedLengthValue("C1", rs.getBinaryStream(1), length, off);
783             checkEncodedLengthValue("C2", rs.getBinaryStream(2), length, off);
784             checkEncodedLengthValue("C3", rs.getBinaryStream(3), length, off);
785
786             System.out.println("");
787         }
788         rs.close();
789
790     }
791     private static void checkEncodedLengthValue(String JavaDoc col, InputStream is, int length, int off) throws IOException {
792
793         if (is == null) {
794             System.out.print(" " + col + " NULL");
795             return;
796         }
797         byte[] buf = new byte[3213];
798         boolean dataOK = false;
799         int sl = 0;
800         for (;;) {
801             int r = is.read(buf);
802             if (r < 0)
803                 break;
804
805             if ((off >= sl) && (off < (sl + r))) {
806                 if (buf[off - sl] == 0x23)
807                     dataOK = true;
808             }
809             sl += r;
810         }
811         System.out.print(" " + col + " " + ((sl == length) ? "OK" : ("FAIL <" + sl + ">")));
812         System.out.print(" DATA " + (dataOK ? "OK" : ("FAIL " + off)));
813     }
814
815     private static void showData(PreparedStatement psS, int id, byte[] original) throws SQLException {
816         psS.setInt(1, id);
817         psS.setInt(2, id + 10);
818         ResultSet rs = psS.executeQuery();
819         while (rs.next()) {
820
821             System.out.print(" ORG ");
822                 System.out.print(showData(original));
823             System.out.print("CHR ");
824                 System.out.print(showData(rs.getBytes(1)));
825             System.out.print("VAR ");
826                 System.out.print(showData(rs.getBytes(2)));
827             System.out.print("LVC ");
828                 System.out.print(showData(rs.getBytes(3)));
829             System.out.print("BLOB ");
830                 System.out.print(showData(rs.getBytes(4)));
831
832             System.out.println("");
833         }
834         rs.close();
835
836     }
837
838     private static void showCompareData(PreparedStatement psS) throws SQLException {
839         ResultSet rs = psS.executeQuery();
840         while (rs.next()) {
841             System.out.print(" " + rs.getInt(1) + " ");
842             System.out.print(showData(rs.getBytes(2)));
843             System.out.print(" " + rs.getInt(3) + " ");
844             System.out.println(showData(rs.getBytes(4)));
845         }
846         rs.close();
847         psS.close();
848     }
849
850     private static String JavaDoc showData(byte[] data) {
851         if (data == null)
852             return "<NULL> ";
853
854         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
855         for (int i = 0; i < data.length; i++) {
856             String JavaDoc s = Integer.toHexString(data[i] & 0xff);
857             if (s.length() == 1)
858                 sb.append('0');
859             sb.append(s);
860         }
861
862         sb.append(' ');
863         sb.append('(');
864         sb.append(data.length);
865         sb.append(')');
866         sb.append(' ');
867
868         return sb.toString();
869     }
870
871
872
873
874
875
876     private static void showSQLE(SQLException sqle) {
877         do {
878             System.out.println(sqle.getSQLState() + ": " + sqle.getMessage());
879             //sqle.printStackTrace(System.out);
880
sqle = sqle.getNextException();
881         } while (sqle != null);
882     }
883     private static void executeDrop(Statement s, String JavaDoc sql) {
884         try {
885             s.execute(sql);
886         } catch (SQLException sqle) {
887         }
888     }
889     private static void executeOK(Statement s, String JavaDoc sql) {
890         System.out.println(sql);
891         try {
892             s.execute(sql);
893         } catch (SQLException sqle) {
894             System.out.println("FAIL ");
895             showSQLE(sqle);
896         }
897     }
898     private static void statementExceptionExpected(Statement s, String JavaDoc sql) {
899         System.out.println(sql);
900         try {
901             s.execute(sql);
902             System.out.println("FAIL - SQL expected to throw exception");
903         } catch (SQLException sqle) {
904             expectedException(sqle);
905         }
906     }
907     private static void expectedException(SQLException sqle) {
908         String JavaDoc sqlState = sqle.getSQLState();
909         if (sqlState == null) {
910             sqlState = "<NULL>";
911         }
912         System.out.println("EXPECTED SQL Exception: (" + sqlState + ") " + sqle.getMessage());
913     }
914
915 }
916
Popular Tags