1 21 22 package org.apache.derbyTesting.functionTests.tests.lang; 23 24 import java.sql.Connection ; 25 import java.sql.DatabaseMetaData ; 26 import java.sql.ResultSet ; 27 import java.sql.Statement ; 28 import java.sql.SQLException ; 29 30 import org.apache.derby.tools.ij; 31 import org.apache.derby.tools.JDBCDisplayUtil; 32 33 39 public class JitTest { 40 41 public static void main (String args[]) 42 { 43 try { 44 45 ij.getPropertyArg(args); 48 Connection conn = ij.startJBMS(); 49 50 System.out.println("Start JitTest"); 51 testDerby1327BadStartWithForAutoIncColumn(conn); 53 conn.close(); 54 } catch (Exception e) { 55 System.out.println("FAIL -- unexpected exception "+e); 56 JDBCDisplayUtil.ShowException(System.out, e); 57 e.printStackTrace(); 58 } 59 } 60 61 68 public static void testDerby1327BadStartWithForAutoIncColumn(Connection conn) 69 throws Exception 70 { 71 conn.setAutoCommit(false); 72 Statement stmt = null; 73 74 dropAllAppTables(conn); 75 System.out.println("Create tables until we get a wrong Start with value"); 76 stmt = conn.createStatement(); 77 78 int numBadStartWith = 0; 80 String createTableSQL = null; 81 try { 82 for (int i = 0; (i < 200) && (numBadStartWith == 0); i++) 85 { 86 String tableName = "APP.MYTABLE" + i; 87 createTableSQL = "CREATE TABLE " + tableName + 88 " (ROLEID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY ("+ 89 "START WITH 2, INCREMENT BY 1), INSTANCEID INTEGER, STATUS"+ 90 " INTEGER, LOGICAL_STATE INTEGER, LSTATE_TSTAMP TIMESTAMP,"+ 91 " UPDT_TSTAMP TIMESTAMP, TSTAMP TIMESTAMP,"+ 92 " CLALEVEL1_CLALEVEL2_CLALEVEL2ID VARCHAR(255), "+ 93 "CLALEVEL1_CLALEVEL2_CLALEVEL3_CLALEVEL3ID VARCHAR(255))"; 94 95 stmt.executeUpdate(createTableSQL); 96 conn.commit(); 97 numBadStartWith = checkBadStartWithCols(conn,2); 98 if (numBadStartWith > 0) 99 break; 100 } 101 } catch (SQLException se) 102 { 103 System.out.println("Failed on " + createTableSQL); 104 JDBCDisplayUtil.ShowSQLException(System.out,se); 105 106 } 107 108 if (numBadStartWith == 0) 109 { 110 System.out.println("PASS: All 200 tables created without problems"); 111 dropAllAppTables(conn); 112 } 113 stmt.close(); 114 conn.rollback(); 115 } 116 117 118 125 private static int checkBadStartWithCols(Connection conn, int 126 maxautoincrementstart) throws Exception 127 { 128 Statement stmt = conn.createStatement(); 129 ResultSet rs =stmt.executeQuery("select count(autoincrementstart) from"+ 130 " sys.syscolumns c, sys.systables t, sys.sysschemas s WHERE"+ 131 " t.schemaid = s.schemaid and s.schemaname = 'APP' and"+ 132 " autoincrementstart > " + maxautoincrementstart); 133 134 rs.next(); 135 int numBadStartWith = rs.getInt(1); 136 if (numBadStartWith > 0) 137 System.out.println(numBadStartWith + " columns have bad START WITH VALUE"); 138 rs.close(); 139 140 if (numBadStartWith > 0) 141 { 142 rs =stmt.executeQuery("select tablename, columnname,"+ 143 " autoincrementstart from sys.syscolumns c, sys.systables t,"+ 144 " sys.sysschemas s WHERE t.schemaid = s.schemaid and"+ 145 " s.schemaname = 'APP' and autoincrementstart > 2 ORDER"+ 146 " BY tablename"); 147 while (rs.next()) 148 { 149 System.out.println("Unexpected start value: " + 150 rs.getLong(3) + 151 " on column " + rs.getString(1) + 152 "(" + rs.getString(2) + ")"); 153 154 155 } 156 } 157 return numBadStartWith; 158 } 159 160 165 private static void dropAllAppTables(Connection conn) throws SQLException 166 { 167 Statement stmt1 = conn.createStatement(); 168 Statement stmt2 = conn.createStatement(); 169 System.out.println("Drop all tables in APP schema"); 170 ResultSet rs = stmt1.executeQuery("SELECT tablename from sys.systables"+ 171 " t, sys.sysschemas s where t.schemaid = s.schemaid and"+ 172 " s.schemaname = 'APP'"); 173 174 while (rs.next()) 175 { 176 String tableName = rs.getString(1); 177 178 try { 179 stmt2.executeUpdate("DROP TABLE " + tableName); 180 } 181 catch (SQLException se) 182 { 183 System.out.println("Error dropping table:" + tableName); 184 se.printStackTrace(); 185 continue; 186 } 187 } 188 } 189 } 190 | Popular Tags |