1 21 22 package org.apache.derbyTesting.functionTests.tests.lang; 23 24 import java.sql.Connection ; 25 import java.sql.DriverManager ; 26 import java.sql.Statement ; 27 import java.sql.PreparedStatement ; 28 import java.sql.ResultSet ; 29 import java.sql.SQLException ; 30 import java.sql.SQLWarning ; 31 import java.sql.Types ; 32 33 import org.apache.derby.tools.ij; 34 import org.apache.derby.tools.JDBCDisplayUtil; 35 36 53 54 public class streams { 55 56 private static int pkCount; 57 private static Connection conn; 58 59 public static void main(String [] args) { 60 System.out.println("Test streams starting"); 61 62 try { 63 ij.getPropertyArg(args); 66 conn = ij.startJBMS(); 67 68 conn.setAutoCommit(true); 69 70 setup(); 71 72 conn.setAutoCommit(false); 73 74 doWork(); 75 76 conn.setAutoCommit(true); 77 78 teardown(); 79 80 conn.close(); 81 82 } catch (Throwable e) { 83 System.out.println("FAIL: exception thrown:"); 84 JDBCDisplayUtil.ShowException(System.out,e); 85 } 86 87 System.out.println("Test streams finished"); 88 } 89 90 static void setup() throws SQLException { 91 Statement stmt = conn.createStatement(); 92 stmt.executeUpdate("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.storage.pageSize', '2048')"); 93 94 verifyCount( 95 stmt.executeUpdate("create table t1 (id int, pid int, lvc long varchar, lvb long varchar for bit data)"), 96 0); 97 verifyCount( 98 stmt.executeUpdate("create table t2 (id int, pid int, lvc long varchar, lvb long varchar for bit data)"), 99 0); 100 verifyCount( 101 stmt.executeUpdate("create trigger tr21 after insert on t2 for each statement mode db2sql values 1"), 102 0); 103 verifyCount( 104 stmt.executeUpdate("create table t3 (id int not null primary key, pid int, lvc long varchar, lvb long varchar for bit data, CONSTRAINT FK1 Foreign Key(pid) REFERENCES T3 (id))"), 105 0); 106 verifyCount( 107 stmt.executeUpdate("create table t4 (id int, longcol long varchar)"), 108 0); 109 verifyCount( 110 stmt.executeUpdate("create table t5 (id int, longcol long varchar)"), 111 0); 112 } 113 114 static void teardown() throws SQLException { 115 Statement stmt = conn.createStatement(); 116 117 verifyCount( 118 stmt.executeUpdate("drop table t1"), 119 0); 120 121 verifyCount( 122 stmt.executeUpdate("drop trigger tr21"), 123 0); 124 125 verifyCount( 126 stmt.executeUpdate("drop table t2"), 127 0); 128 129 verifyCount( 130 stmt.executeUpdate("drop table t3"), 131 0); 132 133 verifyCount( 134 stmt.executeUpdate("drop table t4"), 135 0); 136 137 verifyCount( 138 stmt.executeUpdate("drop table t5"), 139 0); 140 141 stmt.close(); 142 143 System.out.println("teardown complete"); 144 } 145 146 static void verifyCount(int count, int expect) throws SQLException { 147 if (count!=expect) { 148 System.out.println("FAIL: Expected "+expect+" got "+count+" rows"); 149 throw new SQLException ("Wrong number of rows returned"); 150 } 151 else 152 System.out.println("PASS: expected and got "+count+ 153 (count == 1? " row":" rows")); 154 } 155 156 private static void doWork() throws Exception { 157 158 Statement s = conn.createStatement(); 159 160 System.out.println("Start testing"); 161 162 PreparedStatement ps = conn.prepareStatement("insert into t1 values(?, ?, ?,?)"); 163 int level1 = insertLongString(ps, 8, true); 164 System.out.println("materialized insert: got reader stack level"); 165 166 ps = conn.prepareStatement("insert into t2 values(?, ?, ?,?)"); 167 int level2 = insertLongString(ps, 8, true); 168 System.out.println("materialized insert (for trigger): got reader stack level"); 169 170 if (level1 != level2) 171 System.out.println("FAILED!! level difference not expected since streams are materialized."); 172 else 173 System.out.println("SUCCEED!! stack level as expected."); 174 175 ps = conn.prepareStatement("insert into t3 values(?, ?, ?,?)"); 176 int level3 = insertLongString(ps, 8, true); 177 System.out.println("self ref foreign key insert(should not materialize): got reader stack level"); 178 if (level3 == level1) 179 System.out.println("SUCCEED!! levels expected."); 180 else 181 System.out.println("FAILED!! should not materialize in this case."); 182 183 conn.rollback(); 184 185 s.executeUpdate("insert into t3 values (1,1,'a',null), (2,2,'b',null), (3,3,'c',null)"); 186 ps = conn.prepareStatement("update t3 set id = ?, lvc = ? where pid = 2"); 187 level1 = insertLongString(ps, 8, false); 188 System.out.println("materialized for multiple row update: got reader stack level"); 189 190 ps = conn.prepareStatement("update t3 set id = ?, lvc = ? where pid = 2 and id = 2"); 191 level2 = insertLongString(ps, 8, false); 192 System.out.println("single row update: got reader stack level"); 193 194 if (level1 != level2) 195 System.out.println("FAILED!! level difference not expected because streams are materialized with fix for bug 5592."); 196 else 197 System.out.println("SUCCEED!! single row update materialized stream."); 198 199 s.executeUpdate("insert into t4 values (1, 'ccccc')"); 200 ps = conn.prepareStatement("insert into t4 values(?, ?)"); 201 insertLongString(ps, 6, false); 202 s.executeUpdate("insert into t4 values (3, 'aaaaabbbbbb')"); 203 s.executeUpdate("insert into t4 values (4, 'bbbbbb')"); 204 insertLongString(ps, 5, false); 205 ResultSet rs = s.executeQuery("select id, cast(longcol as varchar(8192)) lcol from t4 order by lcol"); 206 if (rs.next()) System.out.println("id = "+ rs.getInt(1) + " longcol = " + rs.getString(2)); 208 if (rs.next()) System.out.println("id = "+ rs.getInt(1) + " longcol = " + rs.getString(2)); 210 for (int i = 0; i < 2; i++) 211 { 212 if (rs.next()) 213 { 214 String longcol = rs.getString(2); 215 int collen = longcol.length(); 216 System.out.print("id = "+ rs.getInt(1) + " longcol length = " + collen); 217 System.out.println(" longcol = " + longcol.substring(0, 5) + "..." + 218 longcol.substring(collen - 5, collen)); 219 } 220 } 221 if (rs.next()) System.out.println("id = "+ rs.getInt(1) + " longcol = " + rs.getString(2)); 223 if (rs.next()) 224 System.out.println("FAILED, more rows left"); 225 else 226 System.out.println("number of rows ok"); 227 228 s.executeUpdate("insert into t5 values (1, 'bbbbbb')"); 229 ps = conn.prepareStatement("insert into t5 values(?, ?)"); 230 insertLongString(ps, 5, false); 231 insertLongString(ps, 7, false); 232 s.executeUpdate("insert into t5 values (3, 'aaaaabbbbbba')"); 233 s.executeUpdate("insert into t5 values (4, 'bbbbbbbbb')"); 234 rs = s.executeQuery("select t4.id, t4.longcol, t5.id, cast(t5.longcol as varchar(8192)) lcol from t4, t5 where cast(t4.longcol as varchar(8192)) = cast(t5.longcol as varchar(8192)) order by lcol"); 235 while (rs.next()) 236 { 237 System.out.println("t4 id = " + rs.getInt(1) + " t4 longcol length = " + rs.getString(2).length() 238 + " t5 id = " + rs.getInt(3) + " t5 longcol length = " + rs.getString(4).length()); 239 } 240 241 System.out.println("Start testing long var binary"); 242 conn.rollback(); 243 244 ps = conn.prepareStatement("insert into t1 values(?, ?, ?,?)"); 245 level1 = insertLongBinary(ps, 8); 246 System.out.println("non materialized insert: got reader stack level"); 247 248 ps = conn.prepareStatement("insert into t2 values(?, ?, ?,?)"); 249 level2 = insertLongBinary(ps, 8); 250 System.out.println("materialized insert (for trigger): got reader stack level"); 251 252 if (level1 > level2 + 5) 253 System.out.println("SUCCEED, level difference expected."); 254 else 255 System.out.println("FAILED, check stack level change."); 256 257 ps = conn.prepareStatement("insert into t3 values(?, ?, ?,?)"); 258 level3 = insertLongBinary(ps, 8); 259 System.out.println("self ref foreign key insert(should not materialize): got reader stack level"); 260 if (level3 == level1) 261 System.out.println("SUCCEED!! levels expected."); 262 else 263 System.out.println("FAILED!! should not materialize stream in this case."); 264 265 conn.rollback(); 266 } 267 268 private static int insertLongString(PreparedStatement ps, int kchars, boolean isInsert) throws SQLException 269 { 270 int chars = (kchars * 1024) + 273; 272 273 long start = System.currentTimeMillis(); 274 275 DummyReader dr = new DummyReader(chars); 276 if (isInsert) 277 { 278 ps.setInt(1, pkCount); 279 ps.setInt(2, pkCount++); 280 ps.setCharacterStream(3, dr, chars); 281 ps.setNull(4, Types.VARBINARY); 282 } 283 else 284 { 285 ps.setInt(1, 2); 286 ps.setCharacterStream(2, dr, chars); 287 } 288 289 ps.executeUpdate(); 290 long end = System.currentTimeMillis(); 291 292 System.out.println("setCharacterStream " + chars + " chars"); 293 294 return dr.readerStackLevel; 295 296 } 297 298 private static int insertLongBinary(PreparedStatement ps, int kbytes) throws SQLException { 299 300 int bytes = (kbytes * 1024) + 273; 302 303 long start = System.currentTimeMillis(); 304 ps.setInt(1, pkCount); 305 ps.setInt(2, pkCount++); 306 ps.setNull(3, Types.LONGVARCHAR); 307 DummyBinary db = new DummyBinary(bytes); 308 ps.setBinaryStream(4, db, bytes); 309 310 ps.executeUpdate(); 311 long end = System.currentTimeMillis(); 312 313 System.out.println("setBinaryStream " + bytes + " bytes"); 314 315 return db.readerStackLevel; 316 } 317 } 318 319 class DummyReader extends java.io.Reader { 320 321 private int count; 322 public int readerStackLevel; 323 324 DummyReader(int length) { 325 this.count = length; 326 } 327 328 private void whereAmI() { 329 if (count == 0) 330 { 331 readerStackLevel = -1; 332 try {throw new Throwable ();} catch (Throwable e) { 333 try { 334 readerStackLevel = e.getStackTrace().length; 335 } catch (NoSuchMethodError nme) { 338 DummyOutputStream dos = new DummyOutputStream(); 339 DummyPrintStream dps = new DummyPrintStream(dos); 340 e.printStackTrace(dps); 341 dps.flush(); 342 readerStackLevel = dps.lines; 344 } 346 } 347 } 348 } 349 350 public int read() { 351 if (count == 0) 352 return -1; 353 354 count--; 355 whereAmI(); 356 357 return 'b'; 358 } 359 360 public int read(char[] buf, int offset, int length) { 361 362 if (count == 0) 363 return -1; 364 365 if (length > count) 366 length = count; 367 368 count -= length; 369 whereAmI(); 370 371 java.util.Arrays.fill(buf, offset, offset + length, 'b'); 372 373 return length; 374 } 375 376 public void close(){} 377 } 378 379 class DummyBinary extends java.io.InputStream { 380 381 public int readerStackLevel; 382 int count; 383 byte content = 42; 384 DummyBinary(int length) { 385 this.count = length; 386 } 387 388 private void whereAmI() { 389 if (count == 0) 390 { 391 readerStackLevel = -1; 392 try {throw new Throwable ();} catch (Throwable e) { 393 try { 394 readerStackLevel = e.getStackTrace().length; 395 } catch (NoSuchMethodError nme) { 398 DummyOutputStream dos = new DummyOutputStream(); 399 DummyPrintStream dps = new DummyPrintStream(dos); 400 e.printStackTrace(dps); 401 dps.flush(); 402 readerStackLevel = dps.lines; 404 } 406 } 407 } 408 } 409 410 public int read() { 411 if (count == 0) 412 return -1; 413 414 count--; 415 whereAmI(); 416 return content++; 417 } 418 419 public int read(byte[] buf, int offset, int length) { 420 421 if (count == 0) 422 return -1; 423 424 if (length > count) 425 length = count; 426 427 count -= length; 428 whereAmI(); 429 430 for (int i = 0; i < length; i++) 431 buf[offset + i] = content++; 432 433 return length; 434 } 435 436 public void close(){} 437 } 438 439 440 class DummyOutputStream extends java.io.OutputStream { 441 public void close() {} 442 public void flush() {} 443 public void write(byte[] b) {} 444 public void write(byte[] b, int off, int len) {} 445 public void write(int b) {} 446 } 447 448 class DummyPrintStream extends java.io.PrintStream { 449 public int lines; 450 public DummyPrintStream(DummyOutputStream dos) {super(dos);} 451 public void println() { lines++; } 452 public void println(String x) { lines++; } 453 public void println(Object x) { lines++; } 454 public void println(char[] x) { lines++; } 455 public void println(double x) { lines++; } 456 public void println(float x) { lines++; } 457 public void println(long x) { lines++; } 458 public void println(int x) { lines++; } 459 public void println(char x) { lines++; } 460 public void println(boolean x) { lines++; } 461 } 462 | Popular Tags |