~maria-captains/mariadb-java-client/trunk

« back to all changes in this revision

Viewing changes to src/test/java/org/mariadb/jdbc/DatabaseMetadataTest.java

  • Committer: Vladislav Vaintroub
  • Date: 2013-06-14 12:58:19 UTC
  • Revision ID: wlad@montyprogram.com-20130614125819-5xslh40scs14dmy1
CONJ-35 : provide alternative implementation for DatabaseMetadata.getImportedKeys() that does not use information schema, but parses the output of "show create table" instead. 

The reason for it is to speed up  DatabaseMetadata.getImportedKeys() call , which can get very slow  in certain scenarios involving hibernate

Show diffs side-by-side

added added

removed removed

Lines of Context:
77
77
      assertEquals(rs.getInt("DATA_TYPE"), java.sql.Types.INTEGER);
78
78
      assertEquals(rs.getString("TYPE_NAME"), "int");
79
79
    }
80
 
    
 
80
 
 
81
 
 
82
     /** Same as getImportedKeys, with one foreign key in a table in another catalog */
 
83
     @Test
 
84
     public void getImportedKeys() throws Exception{
 
85
           Statement st  = connection.createStatement();
 
86
 
 
87
           st.execute("DROP TABLE IF EXISTS product_order");
 
88
           st.execute("DROP TABLE IF EXISTS t1.product ");
 
89
           st.execute("DROP TABLE IF EXISTS `cus``tomer`");
 
90
           st.execute("DROP DATABASE IF EXISTS test1");
 
91
 
 
92
           st.execute("CREATE DATABASE IF NOT EXISTS t1");
 
93
 
 
94
           st.execute("CREATE TABLE t1.product (\n" +
 
95
                   "    category INT NOT NULL, id INT NOT NULL,\n" +
 
96
                   "    price DECIMAL,\n" +
 
97
                   "    PRIMARY KEY(category, id)\n" +
 
98
                   ")   ENGINE=INNODB");
 
99
 
 
100
           st.execute("CREATE TABLE `cus``tomer` (\n" +
 
101
                   "    id INT NOT NULL,\n" +
 
102
                   "    PRIMARY KEY (id)\n" +
 
103
                   ")   ENGINE=INNODB");
 
104
 
 
105
           st.execute("CREATE TABLE product_order (\n" +
 
106
                   "    no INT NOT NULL AUTO_INCREMENT,\n" +
 
107
                   "    product_category INT NOT NULL,\n" +
 
108
                   "    product_id INT NOT NULL,\n" +
 
109
                   "    customer_id INT NOT NULL,\n" +
 
110
                   "\n" +
 
111
                   "    PRIMARY KEY(no),\n" +
 
112
                   "    INDEX (product_category, product_id),\n" +
 
113
                   "    INDEX (customer_id),\n" +
 
114
                   "\n" +
 
115
                   "    FOREIGN KEY (product_category, product_id)\n" +
 
116
                   "      REFERENCES t1.product(category, id)\n" +
 
117
                   "      ON UPDATE CASCADE ON DELETE RESTRICT,\n" +
 
118
                   "\n" +
 
119
                   "    FOREIGN KEY (customer_id)\n" +
 
120
                   "      REFERENCES `cus``tomer`(id)\n" +
 
121
                   ")   ENGINE=INNODB;"
 
122
                   )   ;
 
123
 
 
124
 
 
125
           /*
 
126
            Test that I_S implementation is equivalent to parsing "show create table" .
 
127
             Get result sets using either method and compare (ignore minor differences INT vs SMALLINT
 
128
           */
 
129
           ResultSet rs1 = ((MySQLDatabaseMetaData)connection.getMetaData()).getImportedKeysUsingShowCreateTable("test", null, "product_order");
 
130
           ResultSet rs2 = ((MySQLDatabaseMetaData)connection.getMetaData()).getImportedKeysUsingInformationSchema("test", null, "product_order");
 
131
           assertEquals(rs1.getMetaData().getColumnCount(), rs2.getMetaData().getColumnCount());
 
132
 
 
133
 
 
134
           while(rs1.next()) {
 
135
               assertTrue(rs2.next());
 
136
               for (int i = 1; i <= rs1.getMetaData().getColumnCount(); i++) {
 
137
                   Object s1 = rs1.getObject(i);
 
138
                   Object s2 = rs2.getObject(i);
 
139
                   if (s1 instanceof  Number && s2 instanceof Number) {
 
140
                       assertEquals(((Number)s1).intValue(), ((Number)s2).intValue());
 
141
                   } else {
 
142
                       if (s1 != null && s2 != null && !s1.equals(s2)) {
 
143
                          System.out.println("s1= " + s1 + "," + "s2 = " + s2) ;
 
144
                       }
 
145
                       assertEquals(s1,s2);
 
146
                   }
 
147
               }
 
148
           }
 
149
 
 
150
           /* Also compare metadata */
 
151
           ResultSetMetaData md1 =  rs1.getMetaData();
 
152
           ResultSetMetaData md2 =  rs2.getMetaData();
 
153
           for (int i = 1; i <= md1.getColumnCount(); i++) {
 
154
               assertEquals(md1.getColumnLabel(i),md2.getColumnLabel(i));
 
155
           }
 
156
       }
 
157
 
 
158
    @Test
 
159
    public void ttt() throws Exception {
 
160
        ResultSet rs = connection.getMetaData().getImportedKeys("test",null,"product_order");
 
161
        int k=0;
 
162
        while(rs.next()) {
 
163
            k++;
 
164
            System.out.println( "" + k +   " ===================");
 
165
            for (int i = 1 ; i <= rs.getMetaData().getColumnCount();i++) {
 
166
                System.out.println(rs.getMetaData().getColumnLabel(i) + " = " + rs.getObject(i));
 
167
            }
 
168
        }
 
169
    }
81
170
    @Test
82
171
    public void exportedKeysTest() throws SQLException {
83
172
        Statement stmt = connection.createStatement();