| 1 21 22 package org.apache.derbyTesting.functionTests.tests.jdbcapi; 23 import java.sql.Connection ; 24 import java.sql.DriverManager ; 25 import java.sql.PreparedStatement ; 26 import java.sql.ResultSetMetaData ; 27 import java.sql.ResultSet ; 28 import java.sql.Statement ; 29 import java.sql.SQLException ; 30 import java.sql.Types ; 31 import java.sql.PreparedStatement ; 32 import java.io.*; 33 34 import org.apache.derby.tools.ij; 35 import org.apache.derby.tools.JDBCDisplayUtil; 36 import org.apache.derbyTesting.functionTests.util.TestUtil; 37 38 43 44 public class maxfieldsize { 45 46 47 public static ResultSet rs; 48 49 static final int START_SECOND_HALF = 5; 50 static final int NUM_EXECUTIONS = 2 * START_SECOND_HALF; 51 52 static final String c1_value="C1XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 53 static final String c2_value="C2XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 54 static final String c3_value="C3XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 55 static final String c4_value="C4XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 56 static final String c5_value="C5XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 57 static final String c6_value="C6XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 58 59 static private boolean isDerbyNet = false; 60 61 public static void main(String [] args) { 62 Connection conn; 63 Statement stmt; 64 65 String [] testObjects = {"table tab1", "table tab2"}; 67 68 69 System.out.println("Test MaxFieldSize starting"); 70 71 isDerbyNet = TestUtil.isNetFramework(); 72 try 73 { 74 ij.getPropertyArg(args); 77 conn = ij.startJBMS(); 78 stmt = conn.createStatement(); 79 TestUtil.cleanUpTest(stmt, testObjects); 80 stmt.execute("create table tab1("+ 82 "c1 char(100) for bit data,"+ 83 "c2 varchar(100) for bit data," + 84 "c3 long varchar for bit data,"+ 85 "c4 char(100),"+ 86 "c5 varchar(100),"+ 87 "c6 long varchar)"); 88 load_data(conn); 90 91 int loop = 0; 93 while (loop < NUM_EXECUTIONS ) 94 { 95 if (loop == START_SECOND_HALF) 96 { 97 stmt.setMaxFieldSize(24); 98 } 99 100 System.out.println("Iteration #: " + loop); 101 System.out.println("Max Field Size = " + stmt.getMaxFieldSize()); 102 rs = stmt.executeQuery("select * from tab1"); 103 while (rs.next()) 104 { 105 for(int i=1 ; i < 7 ; i++) 106 { 107 System.out.println("Column #: " + i); 108 switch (loop % START_SECOND_HALF) 109 { 110 case 0: 111 connectionJdbc20.get_using_object(rs, i); 112 break; 113 114 case 1: 115 if (isDerbyNet) 116 System.out.println("beetle 5350 - JCC returns incorrect result for maxfieldsize()"); 117 connectionJdbc20.get_using_string(rs, i); 118 break; 119 120 case 2: 121 connectionJdbc20.get_using_ascii_stream(rs, i); 122 break; 123 124 case 3: 125 if(i < 4 ) { 127 connectionJdbc20.get_using_binary_stream(rs, i); 128 } 129 else 130 { 131 System.out.println("SKIPPING"); 132 } 133 break; 134 135 case 4: 136 if(i < 4 ) { 138 connectionJdbc20.get_using_bytes(rs, i); 139 } 140 else 141 { 142 System.out.println("SKIPPING"); 143 } 144 break; 145 } 146 } 147 } 148 rs.close(); 149 loop++; 150 } 151 try{ 153 stmt.setMaxFieldSize(-200); 155 } catch (SQLException e) { 156 if ((e.getMessage() != null && 157 e.getMessage().indexOf("Invalid maxFieldSize value") >= 0) 158 || (e.getSQLState() != null && 159 (e.getSQLState().equals("XJ066")))) 160 System.out.println("Expected Exception - Invalid maxFieldsize"); 161 else System.out.println("Unexpected FAILURE at " +e); 162 } 163 stmt.setMaxFieldSize(0); 165 166 System.out.println("Doing external sort"); 169 170 testSort(conn, stmt); 171 172 TestUtil.cleanUpTest(stmt, testObjects); 173 174 stmt.close(); 175 conn.close(); 176 177 } 178 catch (SQLException e) { 179 dumpSQLExceptions(e); 180 e.printStackTrace(); 181 } 182 catch (Throwable e) { 183 System.out.println("FAIL -- unexpected exception: "+e); 184 e.printStackTrace(); 185 } 186 187 System.out.println("Test maxfieldsize finished"); 188 } 189 190 191 static private void load_data(Connection conn) throws Exception { 192 PreparedStatement pstmt = null; 193 194 try{ 195 pstmt = conn.prepareStatement( 196 "insert into tab1 values(?,?,?,?,?,?)"); 197 198 pstmt.setBytes(1, c1_value.getBytes("US-ASCII")); 199 pstmt.setBytes(2, c2_value.getBytes("US-ASCII")); 200 pstmt.setBytes(3, c3_value.getBytes("US-ASCII")); 201 pstmt.setString(4, c4_value); 202 pstmt.setString(5, c5_value); 203 pstmt.setString(6, c6_value); 204 pstmt.execute(); 205 } 206 catch(SQLException e) 207 { 208 dumpSQLExceptions(e); 209 e.printStackTrace(); 210 } 211 catch(Throwable e ) 212 { 213 System.out.println("Fail -- unexpected exception "); 214 e.printStackTrace(); 215 } 216 finally 217 { 218 pstmt.close(); 219 } 220 } 221 222 private static void testSort(Connection conn, Statement stmt) 223 throws SQLException , java.io.UnsupportedEncodingException  224 { 225 PreparedStatement insertPStmt; 226 227 stmt.execute("create table tab2("+ 229 "c0 int, " + 230 "c1 char(100) for bit data,"+ 231 "c2 varchar(100) for bit data," + 232 "c3 long varchar for bit data,"+ 233 "c4 char(100),"+ 234 "c5 varchar(100),"+ 235 "c6 long varchar)"); 236 237 insertPStmt = conn.prepareStatement( 239 "insert into tab2 values (?, ?, ?, ?, ?, ?, ?)"); 240 for (int index = 0; index < 5000; index++) 241 { 242 insertPStmt.setInt(1, index); 243 insertPStmt.setBytes(2, c1_value.getBytes("US-ASCII")); 244 insertPStmt.setBytes(3, c2_value.getBytes("US-ASCII")); 245 insertPStmt.setBytes(4, c3_value.getBytes("US-ASCII")); 246 insertPStmt.setString(5, c4_value); 247 insertPStmt.setString(6, c5_value); 248 insertPStmt.setString(7, c6_value); 249 insertPStmt.executeUpdate(); 250 } 251 252 insertPStmt.close(); 253 254 doSort(stmt); 256 257 stmt.setMaxFieldSize(24); 259 doSort(stmt); 260 } 261 262 private static void doSort(Statement stmt) 263 throws SQLException  264 { 265 System.out.println("Max Field Size = " + stmt.getMaxFieldSize()); 266 267 try 268 { 269 273 rs = stmt.executeQuery("select * from tab2 order by c0 desc"); 274 for (int index = 0; index < 5000; index++) 275 { 276 rs.next(); 277 if (index < 5 || index >= 4995) 278 { 279 System.out.println("Iteration #: " + index); 280 System.out.println("Column #1: " + rs.getInt(1)); 281 System.out.println("Column #2:"); 282 connectionJdbc20.get_using_binary_stream(rs, 2); 283 System.out.println("Column #3:"); 284 connectionJdbc20.get_using_binary_stream(rs, 3); 285 System.out.println("Column #4:"); 286 connectionJdbc20.get_using_binary_stream(rs, 4); 287 System.out.println("Column #5:"); 288 connectionJdbc20.get_using_ascii_stream(rs, 5); 289 System.out.println("Column #6:"); 290 connectionJdbc20.get_using_ascii_stream(rs, 6); 291 System.out.println("Column #7:"); 292 connectionJdbc20.get_using_ascii_stream(rs, 7); 293 } 294 } 295 } 296 catch (SQLException e) 297 { 298 throw e; 299 } 300 catch (Throwable e) { 301 System.out.println("FAIL -- unexpected exception: "+e); 302 e.printStackTrace(); 303 } 304 } 305 306 static private void dumpSQLExceptions (SQLException se) { 307 System.out.println("FAIL -- unexpected exception"); 308 while (se != null) 309 { 310 System.out.println("SQLSTATE("+se.getSQLState()+"): "+se); 311 se = se.getNextException(); 312 } 313 } 314 315 static private void dumpExpectedSQLExceptions (SQLException se) { 316 System.out.println("PASS -- expected exception"); 317 while (se != null) 318 { 319 System.out.println("SQLSTATE("+se.getSQLState()+"): "+se); 320 se = se.getNextException(); 321 } 322 } 323 324 static private void cleanUp(Connection conn) throws SQLException  325 { 326 327 } 328 329 330 } 331 | Popular Tags |