PageRenderTime 33ms CodeModel.GetById 3ms RepoModel.GetById 0ms app.codeStats 0ms

/EQEmuJSM/mysql-connector-java-5.1.13/src/testsuite/simple/MetadataTest.java

http://cubbers-eqemu-utils.googlecode.com/
Java | 830 lines | 619 code | 98 blank | 113 comment | 73 complexity | 584f39b17ab5f86fce1c152c53426cb7 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0
  1. /*
  2. Copyright 2002-2004 MySQL AB, 2008 Sun Microsystems
  3. All rights reserved. Use is subject to license terms.
  4. The MySQL Connector/J is licensed under the terms of the GPL,
  5. like most MySQL Connectors. There are special exceptions to the
  6. terms and conditions of the GPL as it is applied to this software,
  7. see the FLOSS License Exception available on mysql.com.
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation; version 2 of the
  11. License.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19. 02110-1301 USA
  20. */
  21. package testsuite.simple;
  22. import java.sql.Connection;
  23. import java.sql.DatabaseMetaData;
  24. import java.sql.ResultSet;
  25. import java.sql.ResultSetMetaData;
  26. import java.sql.SQLException;
  27. import java.sql.Statement;
  28. import java.sql.Types;
  29. import java.util.HashSet;
  30. import java.util.List;
  31. import java.util.Properties;
  32. import java.util.Set;
  33. import testsuite.BaseTestCase;
  34. import com.mysql.jdbc.StringUtils;
  35. /**
  36. * Tests DatabaseMetaData methods.
  37. *
  38. * @author Mark Matthews
  39. * @version $Id$
  40. */
  41. public class MetadataTest extends BaseTestCase {
  42. // ~ Constructors
  43. // -----------------------------------------------------------
  44. /**
  45. * Creates a new MetadataTest object.
  46. *
  47. * @param name
  48. * DOCUMENT ME!
  49. */
  50. public MetadataTest(String name) {
  51. super(name);
  52. }
  53. // ~ Methods
  54. // ----------------------------------------------------------------
  55. /**
  56. * Runs all test cases in this test suite
  57. *
  58. * @param args
  59. */
  60. public static void main(String[] args) {
  61. junit.textui.TestRunner.run(MetadataTest.class);
  62. }
  63. /**
  64. * DOCUMENT ME!
  65. *
  66. * @throws Exception
  67. * DOCUMENT ME!
  68. */
  69. public void setUp() throws Exception {
  70. super.setUp();
  71. createTestTable();
  72. }
  73. /**
  74. * DOCUMENT ME!
  75. *
  76. * @throws SQLException
  77. * DOCUMENT ME!
  78. */
  79. public void testForeignKeys() throws SQLException {
  80. try {
  81. DatabaseMetaData dbmd = this.conn.getMetaData();
  82. this.rs = dbmd.getImportedKeys(null, null, "child");
  83. while (this.rs.next()) {
  84. String pkColumnName = this.rs.getString("PKCOLUMN_NAME");
  85. String fkColumnName = this.rs.getString("FKCOLUMN_NAME");
  86. assertTrue("Primary Key not returned correctly ('"
  87. + pkColumnName + "' != 'parent_id')", pkColumnName
  88. .equalsIgnoreCase("parent_id"));
  89. assertTrue("Foreign Key not returned correctly ('"
  90. + fkColumnName + "' != 'parent_id_fk')", fkColumnName
  91. .equalsIgnoreCase("parent_id_fk"));
  92. }
  93. this.rs.close();
  94. this.rs = dbmd.getExportedKeys(null, null, "parent");
  95. while (this.rs.next()) {
  96. String pkColumnName = this.rs.getString("PKCOLUMN_NAME");
  97. String fkColumnName = this.rs.getString("FKCOLUMN_NAME");
  98. String fkTableName = this.rs.getString("FKTABLE_NAME");
  99. assertTrue("Primary Key not returned correctly ('"
  100. + pkColumnName + "' != 'parent_id')", pkColumnName
  101. .equalsIgnoreCase("parent_id"));
  102. assertTrue(
  103. "Foreign Key table not returned correctly for getExportedKeys ('"
  104. + fkTableName + "' != 'child')", fkTableName
  105. .equalsIgnoreCase("child"));
  106. assertTrue(
  107. "Foreign Key not returned correctly for getExportedKeys ('"
  108. + fkColumnName + "' != 'parent_id_fk')",
  109. fkColumnName.equalsIgnoreCase("parent_id_fk"));
  110. }
  111. this.rs.close();
  112. this.rs = dbmd.getCrossReference(null, null, "cpd_foreign_3", null,
  113. null, "cpd_foreign_4");
  114. assertTrue(this.rs.next());
  115. String pkColumnName = this.rs.getString("PKCOLUMN_NAME");
  116. String pkTableName = this.rs.getString("PKTABLE_NAME");
  117. String fkColumnName = this.rs.getString("FKCOLUMN_NAME");
  118. String fkTableName = this.rs.getString("FKTABLE_NAME");
  119. String deleteAction = cascadeOptionToString(this.rs
  120. .getInt("DELETE_RULE"));
  121. String updateAction = cascadeOptionToString(this.rs
  122. .getInt("UPDATE_RULE"));
  123. assertEquals(pkColumnName, "cpd_foreign_1_id");
  124. assertEquals(pkTableName, "cpd_foreign_3");
  125. assertEquals(fkColumnName, "cpd_foreign_1_id");
  126. assertEquals(fkTableName, "cpd_foreign_4");
  127. assertEquals(deleteAction, "NO ACTION");
  128. assertEquals(updateAction, "CASCADE");
  129. this.rs.close();
  130. this.rs = null;
  131. } finally {
  132. if (this.rs != null) {
  133. this.rs.close();
  134. this.rs = null;
  135. }
  136. }
  137. }
  138. /**
  139. * DOCUMENT ME!
  140. *
  141. * @throws SQLException
  142. * DOCUMENT ME!
  143. */
  144. public void testGetPrimaryKeys() throws SQLException {
  145. try {
  146. DatabaseMetaData dbmd = this.conn.getMetaData();
  147. this.rs = dbmd.getPrimaryKeys(this.conn.getCatalog(), "",
  148. "multikey");
  149. short[] keySeqs = new short[4];
  150. String[] columnNames = new String[4];
  151. int i = 0;
  152. while (this.rs.next()) {
  153. this.rs.getString("TABLE_NAME");
  154. columnNames[i] = this.rs.getString("COLUMN_NAME");
  155. this.rs.getString("PK_NAME");
  156. keySeqs[i] = this.rs.getShort("KEY_SEQ");
  157. i++;
  158. }
  159. if ((keySeqs[0] != 3) && (keySeqs[1] != 2) && (keySeqs[2] != 4)
  160. && (keySeqs[4] != 1)) {
  161. fail("Keys returned in wrong order");
  162. }
  163. } finally {
  164. if (this.rs != null) {
  165. try {
  166. this.rs.close();
  167. } catch (SQLException sqlEx) {
  168. /* ignore */
  169. }
  170. }
  171. }
  172. }
  173. private static String cascadeOptionToString(int option) {
  174. switch (option) {
  175. case DatabaseMetaData.importedKeyCascade:
  176. return "CASCADE";
  177. case DatabaseMetaData.importedKeySetNull:
  178. return "SET NULL";
  179. case DatabaseMetaData.importedKeyRestrict:
  180. return "RESTRICT";
  181. case DatabaseMetaData.importedKeyNoAction:
  182. return "NO ACTION";
  183. }
  184. return "SET DEFAULT";
  185. }
  186. private void createTestTable() throws SQLException {
  187. this.stmt.executeUpdate("DROP TABLE IF EXISTS child");
  188. this.stmt.executeUpdate("DROP TABLE IF EXISTS parent");
  189. this.stmt.executeUpdate("DROP TABLE IF EXISTS multikey");
  190. this.stmt.executeUpdate("DROP TABLE IF EXISTS cpd_foreign_4");
  191. this.stmt.executeUpdate("DROP TABLE IF EXISTS cpd_foreign_3");
  192. this.stmt.executeUpdate("DROP TABLE IF EXISTS cpd_foreign_2");
  193. this.stmt.executeUpdate("DROP TABLE IF EXISTS cpd_foreign_1");
  194. this.stmt.executeUpdate("DROP TABLE IF EXISTS fktable2");
  195. this.stmt.executeUpdate("DROP TABLE IF EXISTS fktable1");
  196. createTable("parent", "(parent_id INT NOT NULL, PRIMARY KEY (parent_id))", "INNODB");
  197. createTable("child", "(child_id INT, parent_id_fk INT, INDEX par_ind (parent_id_fk), "
  198. + "FOREIGN KEY (parent_id_fk) REFERENCES parent(parent_id)) ", "INNODB");
  199. createTable("multikey", "(d INT NOT NULL, b INT NOT NULL, a INT NOT NULL, c INT NOT NULL, PRIMARY KEY (d, b, a, c))");
  200. // Test compound foreign keys
  201. createTable("cpd_foreign_1", "("
  202. + "id int(8) not null auto_increment primary key,"
  203. + "name varchar(255) not null unique," + "key (id)"
  204. + ")", "InnoDB");
  205. createTable("cpd_foreign_2", "("
  206. + "id int(8) not null auto_increment primary key,"
  207. + "key (id)," + "name varchar(255)" + ") ", "InnoDB");
  208. createTable("cpd_foreign_3", "("
  209. + "cpd_foreign_1_id int(8) not null,"
  210. + "cpd_foreign_2_id int(8) not null,"
  211. + "key(cpd_foreign_1_id),"
  212. + "key(cpd_foreign_2_id),"
  213. + "primary key (cpd_foreign_1_id, cpd_foreign_2_id),"
  214. + "foreign key (cpd_foreign_1_id) references cpd_foreign_1(id),"
  215. + "foreign key (cpd_foreign_2_id) references cpd_foreign_2(id)"
  216. + ") ", "InnoDB");
  217. createTable("cpd_foreign_4", "("
  218. + "cpd_foreign_1_id int(8) not null,"
  219. + "cpd_foreign_2_id int(8) not null,"
  220. + "key(cpd_foreign_1_id),"
  221. + "key(cpd_foreign_2_id),"
  222. + "primary key (cpd_foreign_1_id, cpd_foreign_2_id),"
  223. + "foreign key (cpd_foreign_1_id, cpd_foreign_2_id) "
  224. + "references cpd_foreign_3(cpd_foreign_1_id, cpd_foreign_2_id) "
  225. + "ON DELETE RESTRICT ON UPDATE CASCADE"
  226. + ") ", "InnoDB");
  227. createTable("fktable1", "(TYPE_ID int not null, TYPE_DESC varchar(32), primary key(TYPE_ID))", "InnoDB");
  228. createTable("fktable2", "(KEY_ID int not null, COF_NAME varchar(32), PRICE float, TYPE_ID int, primary key(KEY_ID), "
  229. + "index(TYPE_ID), foreign key(TYPE_ID) references fktable1(TYPE_ID)) ", "InnoDB");
  230. }
  231. /**
  232. * Tests the implementation of metadata for views.
  233. *
  234. * This test automatically detects whether or not the server it is running
  235. * against supports the creation of views.
  236. *
  237. * @throws SQLException
  238. * if the test fails.
  239. */
  240. public void testViewMetaData() throws SQLException {
  241. try {
  242. this.rs = this.conn.getMetaData().getTableTypes();
  243. while (this.rs.next()) {
  244. if ("VIEW".equalsIgnoreCase(this.rs.getString(1))) {
  245. this.stmt
  246. .executeUpdate("DROP VIEW IF EXISTS vTestViewMetaData");
  247. this.stmt
  248. .executeUpdate("DROP TABLE IF EXISTS testViewMetaData");
  249. this.stmt
  250. .executeUpdate("CREATE TABLE testViewMetaData (field1 INT)");
  251. this.stmt
  252. .executeUpdate("CREATE VIEW vTestViewMetaData AS SELECT field1 FROM testViewMetaData");
  253. ResultSet tablesRs = null;
  254. try {
  255. tablesRs = this.conn.getMetaData().getTables(
  256. this.conn.getCatalog(), null, "%ViewMetaData",
  257. new String[] { "TABLE", "VIEW" });
  258. assertTrue(tablesRs.next());
  259. assertTrue("testViewMetaData".equalsIgnoreCase(tablesRs
  260. .getString(3)));
  261. assertTrue(tablesRs.next());
  262. assertTrue("vTestViewMetaData"
  263. .equalsIgnoreCase(tablesRs.getString(3)));
  264. } finally {
  265. if (tablesRs != null) {
  266. tablesRs.close();
  267. }
  268. }
  269. try {
  270. tablesRs = this.conn.getMetaData().getTables(
  271. this.conn.getCatalog(), null, "%ViewMetaData",
  272. new String[] { "TABLE" });
  273. assertTrue(tablesRs.next());
  274. assertTrue("testViewMetaData".equalsIgnoreCase(tablesRs
  275. .getString(3)));
  276. assertTrue(!tablesRs.next());
  277. } finally {
  278. if (tablesRs != null) {
  279. tablesRs.close();
  280. }
  281. }
  282. break;
  283. }
  284. }
  285. } finally {
  286. if (this.rs != null) {
  287. this.rs.close();
  288. }
  289. }
  290. }
  291. /**
  292. * Tests detection of read-only fields when the server is 4.1.0 or newer.
  293. *
  294. * @throws Exception
  295. * if the test fails.
  296. */
  297. public void testRSMDIsReadOnly() throws Exception {
  298. try {
  299. this.rs = this.stmt.executeQuery("SELECT 1");
  300. ResultSetMetaData rsmd = this.rs.getMetaData();
  301. if (versionMeetsMinimum(4, 1)) {
  302. assertTrue(rsmd.isReadOnly(1));
  303. try {
  304. this.stmt
  305. .executeUpdate("DROP TABLE IF EXISTS testRSMDIsReadOnly");
  306. this.stmt
  307. .executeUpdate("CREATE TABLE testRSMDIsReadOnly (field1 INT)");
  308. this.stmt
  309. .executeUpdate("INSERT INTO testRSMDIsReadOnly VALUES (1)");
  310. this.rs = this.stmt
  311. .executeQuery("SELECT 1, field1 + 1, field1 FROM testRSMDIsReadOnly");
  312. rsmd = this.rs.getMetaData();
  313. assertTrue(rsmd.isReadOnly(1));
  314. assertTrue(rsmd.isReadOnly(2));
  315. assertTrue(!rsmd.isReadOnly(3));
  316. } finally {
  317. this.stmt
  318. .executeUpdate("DROP TABLE IF EXISTS testRSMDIsReadOnly");
  319. }
  320. } else {
  321. assertTrue(rsmd.isReadOnly(1) == false);
  322. }
  323. } finally {
  324. if (this.rs != null) {
  325. this.rs.close();
  326. }
  327. }
  328. }
  329. public void testBitType() throws Exception {
  330. if (versionMeetsMinimum(5, 0, 3)) {
  331. try {
  332. this.stmt.executeUpdate("DROP TABLE IF EXISTS testBitType");
  333. this.stmt
  334. .executeUpdate("CREATE TABLE testBitType (field1 BIT, field2 BIT, field3 BIT)");
  335. this.stmt
  336. .executeUpdate("INSERT INTO testBitType VALUES (1, 0, NULL)");
  337. this.rs = this.stmt
  338. .executeQuery("SELECT field1, field2, field3 FROM testBitType");
  339. this.rs.next();
  340. assertTrue(((Boolean) this.rs.getObject(1)).booleanValue());
  341. assertTrue(!((Boolean) this.rs.getObject(2)).booleanValue());
  342. assertEquals(this.rs.getObject(3), null);
  343. System.out.println(this.rs.getObject(1) + ", "
  344. + this.rs.getObject(2) + ", " + this.rs.getObject(3));
  345. this.rs = this.conn.prepareStatement(
  346. "SELECT field1, field2, field3 FROM testBitType")
  347. .executeQuery();
  348. this.rs.next();
  349. assertTrue(((Boolean) this.rs.getObject(1)).booleanValue());
  350. assertTrue(!((Boolean) this.rs.getObject(2)).booleanValue());
  351. assertEquals(this.rs.getObject(3), null);
  352. byte[] asBytesTrue = this.rs.getBytes(1);
  353. byte[] asBytesFalse = this.rs.getBytes(2);
  354. byte[] asBytesNull = this.rs.getBytes(3);
  355. assertEquals(asBytesTrue[0], 1);
  356. assertEquals(asBytesFalse[0], 0);
  357. assertEquals(asBytesNull, null);
  358. this.stmt.executeUpdate("DROP TABLE IF EXISTS testBitField");
  359. this.stmt
  360. .executeUpdate("CREATE TABLE testBitField(field1 BIT(9))");
  361. this.rs = this.stmt
  362. .executeQuery("SELECT field1 FROM testBitField");
  363. System.out.println(this.rs.getMetaData().getColumnClassName(1));
  364. } finally {
  365. this.stmt.executeUpdate("DROP TABLE IF EXISTS testBitType");
  366. }
  367. }
  368. }
  369. public void testSupportsSelectForUpdate() throws Exception {
  370. boolean supportsForUpdate = this.conn.getMetaData()
  371. .supportsSelectForUpdate();
  372. if (this.versionMeetsMinimum(4, 0)) {
  373. assertTrue(supportsForUpdate);
  374. } else {
  375. assertTrue(!supportsForUpdate);
  376. }
  377. }
  378. public void testTinyint1IsBit() throws Exception {
  379. String tableName = "testTinyint1IsBit";
  380. // Can't use 'BIT' or boolean
  381. createTable(tableName, "(field1 TINYINT(1))");
  382. this.stmt.executeUpdate("INSERT INTO " + tableName + " VALUES (1)");
  383. Properties props = new Properties();
  384. props.setProperty("tinyint1IsBit", "true");
  385. props.setProperty("transformedBitIsBoolean", "true");
  386. Connection boolConn = getConnectionWithProps(props);
  387. this.rs = boolConn.createStatement().executeQuery(
  388. "SELECT field1 FROM " + tableName);
  389. checkBitOrBooleanType(false);
  390. this.rs = boolConn.prepareStatement("SELECT field1 FROM " + tableName)
  391. .executeQuery();
  392. checkBitOrBooleanType(false);
  393. this.rs = boolConn.getMetaData().getColumns(boolConn.getCatalog(),
  394. null, tableName, "field1");
  395. assertTrue(this.rs.next());
  396. if (versionMeetsMinimum(4, 1)) {
  397. assertEquals(Types.BOOLEAN, this.rs.getInt("DATA_TYPE"));
  398. } else {
  399. assertEquals(Types.BIT, this.rs.getInt("DATA_TYPE"));
  400. }
  401. if (versionMeetsMinimum(4, 1)) {
  402. assertEquals("BOOLEAN", this.rs.getString("TYPE_NAME"));
  403. } else {
  404. assertEquals("BIT", this.rs.getString("TYPE_NAME"));
  405. }
  406. props.clear();
  407. props.setProperty("transformedBitIsBoolean", "false");
  408. props.setProperty("tinyint1IsBit", "true");
  409. Connection bitConn = getConnectionWithProps(props);
  410. this.rs = bitConn.createStatement().executeQuery(
  411. "SELECT field1 FROM " + tableName);
  412. checkBitOrBooleanType(true);
  413. this.rs = bitConn.prepareStatement("SELECT field1 FROM " + tableName)
  414. .executeQuery();
  415. checkBitOrBooleanType(true);
  416. this.rs = bitConn.getMetaData().getColumns(boolConn.getCatalog(), null,
  417. tableName, "field1");
  418. assertTrue(this.rs.next());
  419. assertEquals(Types.BIT, this.rs.getInt("DATA_TYPE"));
  420. assertEquals("BIT", this.rs.getString("TYPE_NAME"));
  421. }
  422. private void checkBitOrBooleanType(boolean usingBit) throws SQLException {
  423. assertTrue(this.rs.next());
  424. assertEquals("java.lang.Boolean", this.rs.getObject(1).getClass()
  425. .getName());
  426. if (!usingBit) {
  427. if (versionMeetsMinimum(4, 1)) {
  428. assertEquals(Types.BOOLEAN, this.rs.getMetaData()
  429. .getColumnType(1));
  430. } else {
  431. assertEquals(Types.BIT, this.rs.getMetaData().getColumnType(1));
  432. }
  433. } else {
  434. assertEquals(Types.BIT, this.rs.getMetaData().getColumnType(1));
  435. }
  436. assertEquals("java.lang.Boolean", this.rs.getMetaData()
  437. .getColumnClassName(1));
  438. }
  439. /**
  440. * Tests the implementation of Information Schema for primary keys.
  441. */
  442. public void testGetPrimaryKeysUsingInfoShcema() throws Exception {
  443. if (versionMeetsMinimum(5, 0, 7)) {
  444. this.stmt.executeUpdate("DROP TABLE IF EXISTS t1");
  445. this.stmt.executeUpdate("CREATE TABLE t1 (c1 int(1) primary key)");
  446. Properties props = new Properties();
  447. props.put("useInformationSchema", "true");
  448. Connection conn1 = null;
  449. try {
  450. conn1 = getConnectionWithProps(props);
  451. DatabaseMetaData metaData = conn1.getMetaData();
  452. this.rs = metaData.getPrimaryKeys(null, null, "t1");
  453. this.rs.next();
  454. assertEquals("t1", this.rs.getString("TABLE_NAME"));
  455. assertEquals("c1", this.rs.getString("COLUMN_NAME"));
  456. } finally {
  457. if (conn1 != null) {
  458. conn1.close();
  459. }
  460. }
  461. }
  462. }
  463. /**
  464. * Tests the implementation of Information Schema for index info.
  465. */
  466. public void testGetIndexInfoUsingInfoSchema() throws Exception {
  467. if (versionMeetsMinimum(5, 0, 7)) {
  468. this.stmt.executeUpdate("DROP TABLE IF EXISTS t1");
  469. this.stmt.executeUpdate("CREATE TABLE t1 (c1 int(1))");
  470. this.stmt.executeUpdate("CREATE INDEX index1 ON t1 (c1)");
  471. Connection conn1 = null;
  472. try {
  473. conn1 = getConnectionWithProps("useInformationSchema=true");
  474. DatabaseMetaData metaData = conn1.getMetaData();
  475. this.rs = metaData.getIndexInfo(conn1.getCatalog(), null, "t1", false, true);
  476. this.rs.next();
  477. assertEquals("t1", this.rs.getString("TABLE_NAME"));
  478. assertEquals("c1", this.rs.getString("COLUMN_NAME"));
  479. assertEquals("1", this.rs.getString("NON_UNIQUE"));
  480. assertEquals("index1", this.rs.getString("INDEX_NAME"));
  481. } finally {
  482. if (conn1 != null) {
  483. conn1.close();
  484. }
  485. }
  486. }
  487. }
  488. /**
  489. * Tests the implementation of Information Schema for columns.
  490. */
  491. public void testGetColumnsUsingInfoSchema() throws Exception {
  492. if (versionMeetsMinimum(5, 0, 7)) {
  493. this.stmt.executeUpdate("DROP TABLE IF EXISTS t1");
  494. this.stmt.executeUpdate("CREATE TABLE t1 (c1 char(1))");
  495. Properties props = new Properties();
  496. props.put("useInformationSchema", "true");
  497. Connection conn1 = null;
  498. try {
  499. conn1 = getConnectionWithProps(props);
  500. DatabaseMetaData metaData = conn1.getMetaData();
  501. this.rs = metaData.getColumns(null, null, "t1", null);
  502. this.rs.next();
  503. assertEquals("t1", this.rs.getString("TABLE_NAME"));
  504. assertEquals("c1", this.rs.getString("COLUMN_NAME"));
  505. assertEquals("CHAR", this.rs.getString("TYPE_NAME"));
  506. assertEquals("1", this.rs.getString("COLUMN_SIZE"));
  507. } finally {
  508. if (conn1 != null) {
  509. conn1.close();
  510. }
  511. }
  512. }
  513. }
  514. /**
  515. * Tests the implementation of Information Schema for tables.
  516. */
  517. public void testGetTablesUsingInfoSchema() throws Exception {
  518. if (versionMeetsMinimum(5, 0, 7)) {
  519. this.stmt.executeUpdate("DROP TABLE IF EXISTS `t1-1`");
  520. this.stmt.executeUpdate("CREATE TABLE `t1-1` (c1 char(1))");
  521. this.stmt.executeUpdate("DROP TABLE IF EXISTS `t1-2`");
  522. this.stmt.executeUpdate("CREATE TABLE `t1-2` (c1 char(1))");
  523. this.stmt.executeUpdate("DROP TABLE IF EXISTS `t2`");
  524. this.stmt.executeUpdate("CREATE TABLE `t2` (c1 char(1))");
  525. Set tableNames = new HashSet();
  526. tableNames.add("t1-1");
  527. tableNames.add("t1-2");
  528. Properties props = new Properties();
  529. props.put("useInformationSchema", "true");
  530. Connection conn1 = null;
  531. try {
  532. conn1 = getConnectionWithProps(props);
  533. DatabaseMetaData metaData = conn1.getMetaData();
  534. // pattern matching for table name
  535. this.rs = metaData.getTables(null, null, "t1-_", null);
  536. while (this.rs.next()) {
  537. assertTrue(tableNames.remove(this.rs.getString("TABLE_NAME")));
  538. }
  539. assertTrue(tableNames.isEmpty());
  540. } finally {
  541. if (conn1 != null) {
  542. conn1.close();
  543. }
  544. }
  545. }
  546. }
  547. /**
  548. * Tests the implementation of Information Schema for column privileges.
  549. */
  550. public void testGetColumnPrivilegesUsingInfoSchema() throws Exception {
  551. String dontRunPropertyName = "com.mysql.jdbc.testsuite.cantGrant";
  552. if (!runTestIfSysPropDefined(dontRunPropertyName)) {
  553. if (versionMeetsMinimum(5, 0, 7)) {
  554. Properties props = new Properties();
  555. props.put("useInformationSchema", "true");
  556. Connection conn1 = null;
  557. Statement stmt1 = null;
  558. String userHostQuoted = null;
  559. boolean grantFailed = true;
  560. try {
  561. conn1 = getConnectionWithProps(props);
  562. stmt1 = conn1.createStatement();
  563. stmt1.executeUpdate("DROP TABLE IF EXISTS t1");
  564. stmt1.executeUpdate("CREATE TABLE t1 (c1 int)");
  565. this.rs = stmt1.executeQuery("SELECT USER()");
  566. this.rs.next();
  567. String user = this.rs.getString(1);
  568. List userHost = StringUtils.split(user, "@", false);
  569. userHostQuoted = "'" + userHost.get(0) + "'@'" + userHost.get(1) + "'";
  570. try {
  571. stmt1.executeUpdate("GRANT update (c1) on t1 to " + userHostQuoted);
  572. grantFailed = false;
  573. } catch (SQLException sqlEx) {
  574. logDebug("This testcase needs to be run with a URL that allows the user to issue GRANTs "
  575. + " in the current database. You can skip this test by setting the system property \""
  576. + dontRunPropertyName + "\".");
  577. grantFailed = true;
  578. }
  579. if (!grantFailed) {
  580. DatabaseMetaData metaData = conn1.getMetaData();
  581. this.rs = metaData.getColumnPrivileges(null, null, "t1", null);
  582. this.rs.next();
  583. assertEquals("t1", this.rs.getString("TABLE_NAME"));
  584. assertEquals("c1", this.rs.getString("COLUMN_NAME"));
  585. assertEquals(userHostQuoted, this.rs.getString("GRANTEE"));
  586. assertEquals("UPDATE", this.rs.getString("PRIVILEGE"));
  587. }
  588. } finally {
  589. if (stmt1 != null) {
  590. stmt1.executeUpdate("DROP TABLE IF EXISTS t1");
  591. if (!grantFailed) {
  592. stmt1.executeUpdate("REVOKE UPDATE (c1) ON t1 FROM " + userHostQuoted);
  593. }
  594. stmt1.close();
  595. }
  596. if (conn1 != null) {
  597. conn1.close();
  598. }
  599. }
  600. }
  601. }
  602. }
  603. /**
  604. * Tests the implementation of Information Schema for description
  605. * of stored procedures available in a catalog.
  606. */
  607. public void testGetProceduresUsingInfoSchema() throws Exception {
  608. if (versionMeetsMinimum(5, 0, 7)) {
  609. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS sp1");
  610. this.stmt.executeUpdate("CREATE PROCEDURE sp1()\n BEGIN\n" + "SELECT 1;" + "end\n");
  611. Properties props = new Properties();
  612. props.put("useInformationSchema", "true");
  613. Connection conn1 = null;
  614. try {
  615. conn1 = getConnectionWithProps(props);
  616. DatabaseMetaData metaData = conn1.getMetaData();
  617. this.rs = metaData.getProcedures(null, null, "sp1");
  618. this.rs.next();
  619. assertEquals("sp1", this.rs.getString("PROCEDURE_NAME"));
  620. assertEquals("1", this.rs.getString("PROCEDURE_TYPE"));
  621. } finally {
  622. if (conn1 != null) {
  623. conn1.close();
  624. }
  625. this.stmt.executeUpdate("DROP PROCEDURE sp1");
  626. }
  627. }
  628. }
  629. /**
  630. * Tests the implementation of Information Schema for foreign key.
  631. */
  632. public void testGetCrossReferenceUsingInfoSchema() throws Exception {
  633. if (versionMeetsMinimum(5, 0, 7)) {
  634. this.stmt.executeUpdate("DROP TABLE IF EXISTS child");
  635. this.stmt.executeUpdate("DROP TABLE If EXISTS parent");
  636. this.stmt.executeUpdate("CREATE TABLE parent(id INT NOT NULL, "
  637. + "PRIMARY KEY (id)) ENGINE=INNODB");
  638. this.stmt.executeUpdate("CREATE TABLE child(id INT, parent_id INT, "
  639. + "FOREIGN KEY (parent_id) REFERENCES parent(id) ON DELETE SET NULL) ENGINE=INNODB");
  640. Properties props = new Properties();
  641. props.put("useInformationSchema", "true");
  642. Connection conn1 = null;
  643. try {
  644. conn1 = getConnectionWithProps(props);
  645. DatabaseMetaData metaData = conn1.getMetaData();
  646. this.rs = metaData.getCrossReference(null, null, "parent", null, null, "child");
  647. this.rs.next();
  648. assertEquals("parent", this.rs.getString("PKTABLE_NAME"));
  649. assertEquals("id", this.rs.getString("PKCOLUMN_NAME"));
  650. assertEquals("child", this.rs.getString("FKTABLE_NAME"));
  651. assertEquals("parent_id", this.rs.getString("FKCOLUMN_NAME"));
  652. } finally {
  653. this.stmt.executeUpdate("DROP TABLE IF EXISTS child");
  654. this.stmt.executeUpdate("DROP TABLE If EXISTS parent");
  655. if (conn1 != null) {
  656. conn1.close();
  657. }
  658. }
  659. }
  660. }
  661. /**
  662. * Tests the implementation of Information Schema for foreign key.
  663. */
  664. public void testGetExportedKeysUsingInfoSchema() throws Exception {
  665. if (versionMeetsMinimum(5, 0, 7)) {
  666. this.stmt.executeUpdate("DROP TABLE IF EXISTS child");
  667. this.stmt.executeUpdate("DROP TABLE If EXISTS parent");
  668. this.stmt.executeUpdate("CREATE TABLE parent(id INT NOT NULL, "
  669. + "PRIMARY KEY (id)) ENGINE=INNODB");
  670. this.stmt.executeUpdate("CREATE TABLE child(id INT, parent_id INT, "
  671. + "FOREIGN KEY (parent_id) REFERENCES parent(id) ON DELETE SET NULL) ENGINE=INNODB");
  672. Properties props = new Properties();
  673. props.put("useInformationSchema", "true");
  674. Connection conn1 = null;
  675. try {
  676. conn1 = getConnectionWithProps(props);
  677. DatabaseMetaData metaData = conn1.getMetaData();
  678. this.rs = metaData.getExportedKeys(null, null, "parent");
  679. this.rs.next();
  680. assertEquals("parent", this.rs.getString("PKTABLE_NAME"));
  681. assertEquals("id", this.rs.getString("PKCOLUMN_NAME"));
  682. assertEquals("child", this.rs.getString("FKTABLE_NAME"));
  683. assertEquals("parent_id", this.rs.getString("FKCOLUMN_NAME"));
  684. } finally {
  685. this.stmt.executeUpdate("DROP TABLE IF EXISTS child");
  686. this.stmt.executeUpdate("DROP TABLE If EXISTS parent");
  687. if (conn1 != null) {
  688. conn1.close();
  689. }
  690. }
  691. }
  692. }
  693. /**
  694. * Tests the implementation of Information Schema for foreign key.
  695. */
  696. public void testGetImportedKeysUsingInfoSchema() throws Exception {
  697. if (versionMeetsMinimum(5, 0, 7)) {
  698. this.stmt.executeUpdate("DROP TABLE IF EXISTS child");
  699. this.stmt.executeUpdate("DROP TABLE If EXISTS parent");
  700. this.stmt.executeUpdate("CREATE TABLE parent(id INT NOT NULL, "
  701. + "PRIMARY KEY (id)) ENGINE=INNODB");
  702. this.stmt.executeUpdate("CREATE TABLE child(id INT, parent_id INT, "
  703. + "FOREIGN KEY (parent_id) REFERENCES parent(id) ON DELETE SET NULL) ENGINE=INNODB");
  704. Properties props = new Properties();
  705. props.put("useInformationSchema", "true");
  706. Connection conn1 = null;
  707. try {
  708. conn1 = getConnectionWithProps(props);
  709. DatabaseMetaData metaData = conn1.getMetaData();
  710. this.rs = metaData.getImportedKeys(null, null, "child");
  711. this.rs.next();
  712. assertEquals("parent", this.rs.getString("PKTABLE_NAME"));
  713. assertEquals("id", this.rs.getString("PKCOLUMN_NAME"));
  714. assertEquals("child", this.rs.getString("FKTABLE_NAME"));
  715. assertEquals("parent_id", this.rs.getString("FKCOLUMN_NAME"));
  716. } finally {
  717. this.stmt.executeUpdate("DROP TABLE IF EXISTS child");
  718. this.stmt.executeUpdate("DROP TABLE If EXISTS parent");
  719. if (conn1 != null) {
  720. conn1.close();
  721. }
  722. }
  723. }
  724. }
  725. }