1 30 31 32 package org.hsqldb.test; 33 34 import java.sql.Connection ; 35 import java.sql.DatabaseMetaData ; 36 import java.sql.PreparedStatement ; 37 import java.sql.ResultSet ; 38 import java.sql.ResultSetMetaData ; 39 import java.util.ArrayList ; 40 import java.util.Iterator ; 41 import java.util.List ; 42 43 import junit.framework.TestCase; 44 import junit.framework.TestResult; 45 46 public class TestDatabaseMetaData extends TestBase { 47 48 public TestDatabaseMetaData(String name) { 49 super(name); 50 } 51 52 public void test() throws Exception { 53 54 Connection conn = newConnection(); 55 PreparedStatement pstmt; 56 int updateCount; 57 58 try { 59 pstmt = conn.prepareStatement( 60 "SET PROPERTY \"sql.enforce_strict_size\" true"); 61 62 pstmt.executeUpdate(); 63 pstmt.close(); 64 65 pstmt = conn.prepareStatement("DROP TABLE t1 IF EXISTS"); 66 67 pstmt.executeUpdate(); 68 pstmt.close(); 69 70 pstmt = conn.prepareStatement( 71 "CREATE TABLE t1 (cha CHARACTER, dec DECIMAL, doub DOUBLE, lon BIGINT, \"IN\" INTEGER, sma SMALLINT, tin TINYINT, " 72 + "dat DATE DEFAULT CURRENT_DATE, tim TIME DEFAULT CURRENT_TIME, timest TIMESTAMP DEFAULT CURRENT_TIMESTAMP );"); 73 updateCount = pstmt.executeUpdate(); 74 75 assertTrue("expected update count of zero", updateCount == 0); 76 77 pstmt = conn.prepareStatement("CREATE INDEX t1 ON t1 (cha );"); 78 updateCount = pstmt.executeUpdate(); 79 pstmt = conn.prepareStatement("DROP TABLE t2 IF EXISTS"); 80 updateCount = pstmt.executeUpdate(); 81 pstmt = conn.prepareStatement( 82 "CREATE TABLE t2 (cha CHARACTER, dec DECIMAL, doub DOUBLE, lon BIGINT, \"IN\" INTEGER, sma SMALLINT, tin TINYINT, " 83 + "dat DATE DEFAULT CURRENT_DATE, tim TIME DEFAULT CURRENT_TIME, timest TIMESTAMP DEFAULT CURRENT_TIMESTAMP );"); 84 updateCount = pstmt.executeUpdate(); 85 pstmt = conn.prepareStatement("CREATE INDEX t2 ON t2 (cha );"); 86 updateCount = pstmt.executeUpdate(); 87 88 DatabaseMetaData dbmd = conn.getMetaData(); 89 ResultSet rsp = dbmd.getTablePrivileges(null, null, "T1"); 90 91 while (rsp.next()) { 92 System.out.println("Table: " + rsp.getString(3) + " priv: " 93 + rsp.getString(6)); 94 } 95 96 rsp = dbmd.getIndexInfo(null, null, "T1", false, false); 97 98 while (rsp.next()) { 99 System.out.println("Table: " + rsp.getString(3) 100 + " IndexName: " + rsp.getString(6)); 101 } 102 103 rsp = dbmd.getIndexInfo(null, null, "T2", false, false); 104 105 while (rsp.next()) { 106 System.out.println("Table: " + rsp.getString(3) 107 + " IndexName: " + rsp.getString(6)); 108 } 109 110 pstmt = conn.prepareStatement("DROP INDEX t2 ON t2;"); 111 updateCount = pstmt.executeUpdate(); 112 rsp = dbmd.getIndexInfo(null, null, "T2", false, false); 113 114 assertTrue("expected getIndexInfo returns empty resultset", 115 rsp.next() == false); 116 117 ResultSet rs = dbmd.getTables(null, null, "T1", 118 new String []{ "TABLE" }); 119 ArrayList tablesarr = new ArrayList (); 120 int i; 121 122 for (i = 0; rs.next(); i++) { 123 String tempstr = 124 rs.getString("TABLE_NAME").trim().toLowerCase(); 125 126 tablesarr.add(tempstr); 127 } 128 129 rs.close(); 130 assertTrue("expected table t1 count of 1", i == 1); 131 132 Iterator it = tablesarr.iterator(); 133 134 for (; it.hasNext(); ) { 135 136 String tablename = ((String ) it.next()).trim(); 138 List collist = new ArrayList (30); 139 140 rs = dbmd.getColumns(null, null, tablename.toUpperCase(), 141 null); 142 143 for (i = 0; rs.next(); i++) { 144 collist.add( 145 rs.getString("COLUMN_NAME").trim().toLowerCase()); 146 } 147 148 rs.close(); 149 } 150 151 pstmt = conn.prepareStatement("DROP TABLE t_1 IF EXISTS"); 152 153 pstmt.executeUpdate(); 154 pstmt.close(); 155 156 pstmt = conn.prepareStatement( 157 "CREATE TABLE t_1 (cha CHARACTER(10), dec DECIMAL(10,2), doub DOUBLE, lon BIGINT, \"IN\" INTEGER, sma SMALLINT, tin TINYINT, " 158 + "dat DATE DEFAULT CURRENT_DATE, tim TIME DEFAULT CURRENT_TIME, timest TIMESTAMP DEFAULT CURRENT_TIMESTAMP, bool BOOLEAN );"); 159 updateCount = pstmt.executeUpdate(); 160 161 assertTrue("expected update count of zero", updateCount == 0); 162 163 rs = dbmd.getTables(null, null, "T\\_1", new String []{ "TABLE" }); 164 165 for (i = 0; rs.next(); i++) { 166 String tempstr = 167 rs.getString("TABLE_NAME").trim().toLowerCase(); 168 169 tablesarr.add(tempstr); 170 } 171 172 rs.close(); 173 assertTrue("expected table t_1 count of 1", i == 1); 174 175 dbmd.getPrimaryKeys(null, null, "T_1"); 177 dbmd.getImportedKeys(null, null, "T_1"); 178 dbmd.getCrossReference(null, null, "T_1", null, null, "T_1"); 179 180 pstmt = conn.prepareStatement( 182 "INSERT INTO T_1 (cha, dec, doub) VALUES ('name', 10.23, 0)"); 183 184 pstmt.executeUpdate(); 185 pstmt.close(); 186 187 pstmt = conn.prepareStatement("SELECT * FROM T_1"); 188 rs = pstmt.executeQuery(); 189 190 ResultSetMetaData md = rs.getMetaData(); 191 int x = md.getColumnDisplaySize(1); 192 int y = md.getColumnDisplaySize(2); 193 int b = md.getPrecision(2); 194 int c = md.getScale(1); 195 int d = md.getScale(2); 196 String e = md.getColumnClassName(10); 197 boolean testresult = (x == 10) && (y == 13) && (b == 10) 198 && (c == 0) && (d == 2) 199 && e.equals("java.sql.Timestamp"); 200 201 assertTrue("wrong result metadata", testresult); 202 203 e = md.getColumnClassName(11); 204 testresult = e.equals("java.lang.Boolean"); 205 206 assertTrue("wrong result metadata", testresult); 207 pstmt.close(); 208 conn.close(); 209 } catch (Exception e) { 210 assertTrue("unable to prepare or execute DDL", false); 211 } finally { 212 conn.close(); 213 } 214 } 215 216 public static void main(String [] args) throws Exception { 217 218 TestResult result; 219 TestCase test; 220 java.util.Enumeration failures; 221 int count; 222 223 result = new TestResult(); 224 test = new TestDatabaseMetaData("test"); 225 226 test.run(result); 227 228 count = result.failureCount(); 229 230 System.out.println("TestDatabaseMetaData failure count: " + count); 231 232 failures = result.failures(); 233 234 while (failures.hasMoreElements()) { 235 System.out.println(failures.nextElement()); 236 } 237 } 238 } 239 | Popular Tags |