0001: /*
0002: Copyright (C) 2002-2007 MySQL AB
0003:
0004: This program is free software; you can redistribute it and/or modify
0005: it under the terms of version 2 of the GNU General Public License as
0006: published by the Free Software Foundation.
0007:
0008: There are special exceptions to the terms and conditions of the GPL
0009: as it is applied to this software. View the full text of the
0010: exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
0011: software distribution.
0012:
0013: This program is distributed in the hope that it will be useful,
0014: but WITHOUT ANY WARRANTY; without even the implied warranty of
0015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0016: GNU General Public License for more details.
0017:
0018: You should have received a copy of the GNU General Public License
0019: along with this program; if not, write to the Free Software
0020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0021:
0022:
0023:
0024: */
0025: package testsuite.regression;
0026:
0027: import java.sql.Connection;
0028: import java.sql.DatabaseMetaData;
0029: import java.sql.DriverPropertyInfo;
0030: import java.sql.PreparedStatement;
0031: import java.sql.ResultSet;
0032: import java.sql.ResultSetMetaData;
0033: import java.sql.SQLException;
0034: import java.sql.Statement;
0035: import java.sql.Types;
0036: import java.util.HashMap;
0037: import java.util.Properties;
0038:
0039: import testsuite.BaseTestCase;
0040:
0041: import com.mysql.jdbc.Driver;
0042: import com.mysql.jdbc.Field;
0043: import com.mysql.jdbc.NonRegisteringDriver;
0044: import com.mysql.jdbc.SQLError;
0045:
0046: /**
0047: * Regression tests for DatabaseMetaData
0048: *
0049: * @author Mark Matthews
0050: * @version $Id: MetaDataRegressionTest.java,v 1.1.2.1 2005/05/13 18:58:38
0051: * mmatthews Exp $
0052: */
0053: public class MetaDataRegressionTest extends BaseTestCase {
0054: /**
0055: * Creates a new MetaDataRegressionTest.
0056: *
0057: * @param name
0058: * the name of the test
0059: */
0060: public MetaDataRegressionTest(String name) {
0061: super (name);
0062: }
0063:
0064: /**
0065: * Runs all test cases in this test suite
0066: *
0067: * @param args
0068: */
0069: public static void main(String[] args) {
0070: junit.textui.TestRunner.run(MetaDataRegressionTest.class);
0071: }
0072:
0073: /**
0074: * DOCUMENT ME!
0075: *
0076: * @throws Exception
0077: * ...
0078: */
0079: public void testBug2607() throws Exception {
0080: try {
0081: this .stmt.executeUpdate("DROP TABLE IF EXISTS testBug2607");
0082: this .stmt
0083: .executeUpdate("CREATE TABLE testBug2607 (field1 INT PRIMARY KEY)");
0084:
0085: this .rs = this .stmt
0086: .executeQuery("SELECT field1 FROM testBug2607");
0087:
0088: ResultSetMetaData rsmd = this .rs.getMetaData();
0089:
0090: assertTrue(!rsmd.isAutoIncrement(1));
0091: } finally {
0092: this .stmt.executeUpdate("DROP TABLE IF EXISTS testBug2607");
0093: }
0094: }
0095:
0096: /**
0097: * Tests fix for BUG#2852, where RSMD is not returning correct (or matching)
0098: * types for TINYINT and SMALLINT.
0099: *
0100: * @throws Exception
0101: * if the test fails.
0102: */
0103: public void testBug2852() throws Exception {
0104: try {
0105: this .stmt.executeUpdate("DROP TABLE IF EXISTS testBug2852");
0106: this .stmt
0107: .executeUpdate("CREATE TABLE testBug2852 (field1 TINYINT, field2 SMALLINT)");
0108: this .stmt
0109: .executeUpdate("INSERT INTO testBug2852 VALUES (1,1)");
0110:
0111: this .rs = this .stmt
0112: .executeQuery("SELECT * from testBug2852");
0113:
0114: assertTrue(this .rs.next());
0115:
0116: ResultSetMetaData rsmd = this .rs.getMetaData();
0117:
0118: assertTrue(rsmd.getColumnClassName(1).equals(
0119: this .rs.getObject(1).getClass().getName()));
0120: assertTrue("java.lang.Integer".equals(rsmd
0121: .getColumnClassName(1)));
0122:
0123: assertTrue(rsmd.getColumnClassName(2).equals(
0124: this .rs.getObject(2).getClass().getName()));
0125: assertTrue("java.lang.Integer".equals(rsmd
0126: .getColumnClassName(2)));
0127: } finally {
0128: this .stmt.executeUpdate("DROP TABLE IF EXISTS testBug2852");
0129: }
0130: }
0131:
0132: /**
0133: * Tests fix for BUG#2855, where RSMD is not returning correct (or matching)
0134: * types for FLOAT.
0135: *
0136: * @throws Exception
0137: * if the test fails.
0138: */
0139: public void testBug2855() throws Exception {
0140: try {
0141: this .stmt.executeUpdate("DROP TABLE IF EXISTS testBug2855");
0142: this .stmt
0143: .executeUpdate("CREATE TABLE testBug2855 (field1 FLOAT)");
0144: this .stmt
0145: .executeUpdate("INSERT INTO testBug2855 VALUES (1)");
0146:
0147: this .rs = this .stmt
0148: .executeQuery("SELECT * from testBug2855");
0149:
0150: assertTrue(this .rs.next());
0151:
0152: ResultSetMetaData rsmd = this .rs.getMetaData();
0153:
0154: assertTrue(rsmd.getColumnClassName(1).equals(
0155: this .rs.getObject(1).getClass().getName()));
0156: assertTrue("java.lang.Float".equals(rsmd
0157: .getColumnClassName(1)));
0158: } finally {
0159: this .stmt.executeUpdate("DROP TABLE IF EXISTS testBug2855");
0160: }
0161: }
0162:
0163: /**
0164: * Tests fix for BUG#3570 -- inconsistent reporting of column type
0165: *
0166: * @throws Exception
0167: * if an error occurs
0168: */
0169: public void testBug3570() throws Exception {
0170: String createTableQuery = " CREATE TABLE testBug3570(field_tinyint TINYINT"
0171: + ",field_smallint SMALLINT"
0172: + ",field_mediumint MEDIUMINT"
0173: + ",field_int INT"
0174: + ",field_integer INTEGER"
0175: + ",field_bigint BIGINT"
0176: + ",field_real REAL"
0177: + ",field_float FLOAT"
0178: + ",field_decimal DECIMAL"
0179: + ",field_numeric NUMERIC"
0180: + ",field_double DOUBLE"
0181: + ",field_char CHAR(3)"
0182: + ",field_varchar VARCHAR(255)"
0183: + ",field_date DATE"
0184: + ",field_time TIME"
0185: + ",field_year YEAR"
0186: + ",field_timestamp TIMESTAMP"
0187: + ",field_datetime DATETIME"
0188: + ",field_tinyblob TINYBLOB"
0189: + ",field_blob BLOB"
0190: + ",field_mediumblob MEDIUMBLOB"
0191: + ",field_longblob LONGBLOB"
0192: + ",field_tinytext TINYTEXT"
0193: + ",field_text TEXT"
0194: + ",field_mediumtext MEDIUMTEXT"
0195: + ",field_longtext LONGTEXT"
0196: + ",field_enum ENUM('1','2','3')"
0197: + ",field_set SET('1','2','3'))";
0198:
0199: try {
0200: this .stmt.executeUpdate("DROP TABLE IF EXISTS testBug3570");
0201: this .stmt.executeUpdate(createTableQuery);
0202:
0203: ResultSet dbmdRs = this .conn.getMetaData().getColumns(
0204: this .conn.getCatalog(), null, "testBug3570", "%");
0205:
0206: this .rs = this .stmt
0207: .executeQuery("SELECT * FROM testBug3570");
0208:
0209: ResultSetMetaData rsmd = this .rs.getMetaData();
0210:
0211: while (dbmdRs.next()) {
0212: String columnName = dbmdRs.getString(4);
0213: int typeFromGetColumns = dbmdRs.getInt(5);
0214: int typeFromRSMD = rsmd.getColumnType(this .rs
0215: .findColumn(columnName));
0216:
0217: //
0218: // TODO: Server needs to send these types correctly....
0219: //
0220: if (!"field_tinyblob".equals(columnName)
0221: && !"field_tinytext".equals(columnName)) {
0222: assertTrue(columnName
0223: + " -> type from DBMD.getColumns("
0224: + typeFromGetColumns
0225: + ") != type from RSMD.getColumnType("
0226: + typeFromRSMD + ")",
0227: typeFromGetColumns == typeFromRSMD);
0228: }
0229: }
0230: } finally {
0231: this .stmt.executeUpdate("DROP TABLE IF EXISTS testBug3570");
0232: }
0233: }
0234:
0235: /**
0236: * Tests char/varchar bug
0237: *
0238: * @throws Exception
0239: * if any errors occur
0240: */
0241: public void testCharVarchar() throws Exception {
0242: try {
0243: this .stmt.execute("DROP TABLE IF EXISTS charVarCharTest");
0244: this .stmt.execute("CREATE TABLE charVarCharTest ("
0245: + " TableName VARCHAR(64),"
0246: + " FieldName VARCHAR(64),"
0247: + " NextCounter INTEGER);");
0248:
0249: String query = "SELECT TableName, FieldName, NextCounter FROM charVarCharTest";
0250: this .rs = this .stmt.executeQuery(query);
0251:
0252: ResultSetMetaData rsmeta = this .rs.getMetaData();
0253:
0254: assertTrue(rsmeta.getColumnTypeName(1).equalsIgnoreCase(
0255: "VARCHAR"));
0256:
0257: // is "CHAR", expected "VARCHAR"
0258: assertTrue(rsmeta.getColumnType(1) == 12);
0259:
0260: // is 1 (java.sql.Types.CHAR), expected 12 (java.sql.Types.VARCHAR)
0261: } finally {
0262: this .stmt.execute("DROP TABLE IF EXISTS charVarCharTest");
0263: }
0264: }
0265:
0266: /**
0267: * Tests fix for BUG#1673, where DatabaseMetaData.getColumns() is not
0268: * returning correct column ordinal info for non '%' column name patterns.
0269: *
0270: * @throws Exception
0271: * if the test fails for any reason
0272: */
0273: public void testFixForBug1673() throws Exception {
0274: try {
0275: this .stmt.executeUpdate("DROP TABLE IF EXISTS testBug1673");
0276: this .stmt
0277: .executeUpdate("CREATE TABLE testBug1673 (field_1 INT, field_2 INT)");
0278:
0279: DatabaseMetaData dbmd = this .conn.getMetaData();
0280:
0281: int ordinalPosOfCol2Full = 0;
0282:
0283: this .rs = dbmd.getColumns(this .conn.getCatalog(), null,
0284: "testBug1673", null);
0285:
0286: while (this .rs.next()) {
0287: if (this .rs.getString(4).equals("field_2")) {
0288: ordinalPosOfCol2Full = this .rs.getInt(17);
0289: }
0290: }
0291:
0292: int ordinalPosOfCol2Scoped = 0;
0293:
0294: this .rs = dbmd.getColumns(this .conn.getCatalog(), null,
0295: "testBug1673", "field_2");
0296:
0297: while (this .rs.next()) {
0298: if (this .rs.getString(4).equals("field_2")) {
0299: ordinalPosOfCol2Scoped = this .rs.getInt(17);
0300: }
0301: }
0302:
0303: assertTrue(
0304: "Ordinal position in full column list of '"
0305: + ordinalPosOfCol2Full
0306: + "' != ordinal position in pattern search, '"
0307: + ordinalPosOfCol2Scoped + "'.",
0308: (ordinalPosOfCol2Full != 0)
0309: && (ordinalPosOfCol2Scoped != 0)
0310: && (ordinalPosOfCol2Scoped == ordinalPosOfCol2Full));
0311: } finally {
0312: this .stmt.executeUpdate("DROP TABLE IF EXISTS testBug1673");
0313: }
0314: }
0315:
0316: /**
0317: * Tests bug reported by OpenOffice team with getColumns and LONGBLOB
0318: *
0319: * @throws Exception
0320: * if any errors occur
0321: */
0322: public void testGetColumns() throws Exception {
0323: try {
0324: this .stmt
0325: .execute("CREATE TABLE IF NOT EXISTS longblob_regress(field_1 longblob)");
0326:
0327: DatabaseMetaData dbmd = this .conn.getMetaData();
0328: ResultSet dbmdRs = null;
0329:
0330: try {
0331: dbmdRs = dbmd.getColumns("", "", "longblob_regress",
0332: "%");
0333:
0334: while (dbmdRs.next()) {
0335: dbmdRs.getInt(7);
0336: }
0337: } finally {
0338: if (dbmdRs != null) {
0339: try {
0340: dbmdRs.close();
0341: } catch (SQLException ex) {
0342: ;
0343: }
0344: }
0345: }
0346: } finally {
0347: this .stmt.execute("DROP TABLE IF EXISTS longblob_regress");
0348: }
0349: }
0350:
0351: /**
0352: * Tests fix for Bug#
0353: *
0354: * @throws Exception
0355: * if an error occurs
0356: */
0357: public void testGetColumnsBug1099() throws Exception {
0358: try {
0359: this .stmt
0360: .executeUpdate("DROP TABLE IF EXISTS testGetColumnsBug1099");
0361:
0362: DatabaseMetaData dbmd = this .conn.getMetaData();
0363:
0364: this .rs = dbmd.getTypeInfo();
0365:
0366: StringBuffer types = new StringBuffer();
0367:
0368: HashMap alreadyDoneTypes = new HashMap();
0369:
0370: while (this .rs.next()) {
0371: String typeName = this .rs.getString("TYPE_NAME");
0372: String createParams = this .rs
0373: .getString("CREATE_PARAMS");
0374:
0375: if ((typeName.indexOf("BINARY") == -1)
0376: && !typeName.equals("LONG VARCHAR")) {
0377: if (!alreadyDoneTypes.containsKey(typeName)) {
0378: alreadyDoneTypes.put(typeName, null);
0379:
0380: if (types.length() != 0) {
0381: types.append(", \n");
0382: }
0383:
0384: int typeNameLength = typeName.length();
0385: StringBuffer safeTypeName = new StringBuffer(
0386: typeNameLength);
0387:
0388: for (int i = 0; i < typeNameLength; i++) {
0389: char c = typeName.charAt(i);
0390:
0391: if (Character.isWhitespace(c)) {
0392: safeTypeName.append("_");
0393: } else {
0394: safeTypeName.append(c);
0395: }
0396: }
0397:
0398: types.append(safeTypeName.toString());
0399: types.append("Column ");
0400: types.append(typeName);
0401:
0402: if (typeName.indexOf("CHAR") != -1) {
0403: types.append(" (1)");
0404: } else if (typeName.equalsIgnoreCase("enum")
0405: || typeName.equalsIgnoreCase("set")) {
0406: types.append("('a', 'b', 'c')");
0407: }
0408: }
0409: }
0410: }
0411:
0412: this .stmt
0413: .executeUpdate("CREATE TABLE testGetColumnsBug1099("
0414: + types.toString() + ")");
0415:
0416: dbmd.getColumns(null, this .conn.getCatalog(),
0417: "testGetColumnsBug1099", "%");
0418: } finally {
0419: this .stmt
0420: .executeUpdate("DROP TABLE IF EXISTS testGetColumnsBug1099");
0421: }
0422: }
0423:
0424: /**
0425: * Tests whether or not unsigned columns are reported correctly in
0426: * DBMD.getColumns
0427: *
0428: * @throws Exception
0429: */
0430: public void testGetColumnsUnsigned() throws Exception {
0431: try {
0432: this .stmt
0433: .executeUpdate("DROP TABLE IF EXISTS testGetUnsignedCols");
0434: this .stmt
0435: .executeUpdate("CREATE TABLE testGetUnsignedCols (field1 BIGINT, field2 BIGINT UNSIGNED)");
0436:
0437: DatabaseMetaData dbmd = this .conn.getMetaData();
0438:
0439: this .rs = dbmd.getColumns(this .conn.getCatalog(), null,
0440: "testGetUnsignedCols", "%");
0441:
0442: assertTrue(this .rs.next());
0443: // This row doesn't have 'unsigned' attribute
0444: assertTrue(this .rs.next());
0445: assertTrue(this .rs.getString(6).toLowerCase().indexOf(
0446: "unsigned") != -1);
0447: } finally {
0448: this .stmt
0449: .executeUpdate("DROP TABLE IF EXISTS testGetUnsignedCols");
0450: }
0451: }
0452:
0453: /**
0454: * Tests whether bogus parameters break Driver.getPropertyInfo().
0455: *
0456: * @throws Exception
0457: * if an error occurs.
0458: */
0459: public void testGetPropertyInfo() throws Exception {
0460: new Driver().getPropertyInfo("", null);
0461: }
0462:
0463: /**
0464: * Tests whether ResultSetMetaData returns correct info for CHAR/VARCHAR
0465: * columns.
0466: *
0467: * @throws Exception
0468: * if the test fails
0469: */
0470: public void testIsCaseSensitive() throws Exception {
0471: try {
0472: this .stmt
0473: .executeUpdate("DROP TABLE IF EXISTS testIsCaseSensitive");
0474: this .stmt
0475: .executeUpdate("CREATE TABLE testIsCaseSensitive (bin_char CHAR(1) BINARY, bin_varchar VARCHAR(64) BINARY, ci_char CHAR(1), ci_varchar VARCHAR(64))");
0476: this .rs = this .stmt
0477: .executeQuery("SELECT bin_char, bin_varchar, ci_char, ci_varchar FROM testIsCaseSensitive");
0478:
0479: ResultSetMetaData rsmd = this .rs.getMetaData();
0480: assertTrue(rsmd.isCaseSensitive(1));
0481: assertTrue(rsmd.isCaseSensitive(2));
0482: assertTrue(!rsmd.isCaseSensitive(3));
0483: assertTrue(!rsmd.isCaseSensitive(4));
0484: } finally {
0485: this .stmt
0486: .executeUpdate("DROP TABLE IF EXISTS testIsCaseSensitive");
0487: }
0488:
0489: if (versionMeetsMinimum(4, 1)) {
0490: try {
0491: this .stmt
0492: .executeUpdate("DROP TABLE IF EXISTS testIsCaseSensitiveCs");
0493: this .stmt
0494: .executeUpdate("CREATE TABLE testIsCaseSensitiveCs ("
0495: + "bin_char CHAR(1) CHARACTER SET latin1 COLLATE latin1_general_cs,"
0496: + "bin_varchar VARCHAR(64) CHARACTER SET latin1 COLLATE latin1_general_cs,"
0497: + "ci_char CHAR(1) CHARACTER SET latin1 COLLATE latin1_general_ci,"
0498: + "ci_varchar VARCHAR(64) CHARACTER SET latin1 COLLATE latin1_general_ci, "
0499: + "bin_tinytext TINYTEXT CHARACTER SET latin1 COLLATE latin1_general_cs,"
0500: + "bin_text TEXT CHARACTER SET latin1 COLLATE latin1_general_cs,"
0501: + "bin_med_text MEDIUMTEXT CHARACTER SET latin1 COLLATE latin1_general_cs,"
0502: + "bin_long_text LONGTEXT CHARACTER SET latin1 COLLATE latin1_general_cs,"
0503: + "ci_tinytext TINYTEXT CHARACTER SET latin1 COLLATE latin1_general_ci,"
0504: + "ci_text TEXT CHARACTER SET latin1 COLLATE latin1_general_ci,"
0505: + "ci_med_text MEDIUMTEXT CHARACTER SET latin1 COLLATE latin1_general_ci,"
0506: + "ci_long_text LONGTEXT CHARACTER SET latin1 COLLATE latin1_general_ci)");
0507:
0508: this .rs = this .stmt
0509: .executeQuery("SELECT bin_char, bin_varchar, ci_char, ci_varchar, bin_tinytext, bin_text, bin_med_text, bin_long_text, ci_tinytext, ci_text, ci_med_text, ci_long_text FROM testIsCaseSensitiveCs");
0510:
0511: ResultSetMetaData rsmd = this .rs.getMetaData();
0512: assertTrue(rsmd.isCaseSensitive(1));
0513: assertTrue(rsmd.isCaseSensitive(2));
0514: assertTrue(!rsmd.isCaseSensitive(3));
0515: assertTrue(!rsmd.isCaseSensitive(4));
0516:
0517: assertTrue(rsmd.isCaseSensitive(5));
0518: assertTrue(rsmd.isCaseSensitive(6));
0519: assertTrue(rsmd.isCaseSensitive(7));
0520: assertTrue(rsmd.isCaseSensitive(8));
0521:
0522: assertTrue(!rsmd.isCaseSensitive(9));
0523: assertTrue(!rsmd.isCaseSensitive(10));
0524: assertTrue(!rsmd.isCaseSensitive(11));
0525: assertTrue(!rsmd.isCaseSensitive(12));
0526: } finally {
0527: this .stmt
0528: .executeUpdate("DROP TABLE IF EXISTS testIsCaseSensitiveCs");
0529: }
0530: }
0531: }
0532:
0533: /**
0534: * Tests whether or not DatabaseMetaData.getColumns() returns the correct
0535: * java.sql.Types info.
0536: *
0537: * @throws Exception
0538: * if the test fails.
0539: */
0540: public void testLongText() throws Exception {
0541: try {
0542: this .stmt
0543: .executeUpdate("DROP TABLE IF EXISTS testLongText");
0544: this .stmt
0545: .executeUpdate("CREATE TABLE testLongText (field1 LONGTEXT)");
0546:
0547: this .rs = this .conn.getMetaData().getColumns(
0548: this .conn.getCatalog(), null, "testLongText", "%");
0549:
0550: assertTrue(this .rs.next());
0551:
0552: assertTrue(this .rs.getInt("DATA_TYPE") == java.sql.Types.LONGVARCHAR);
0553: } finally {
0554: this .stmt
0555: .executeUpdate("DROP TABLE IF EXISTS testLongText");
0556: }
0557: }
0558:
0559: /**
0560: * Tests for types being returned correctly
0561: *
0562: * @throws Exception
0563: * if an error occurs.
0564: */
0565: public void testTypes() throws Exception {
0566: try {
0567: this .stmt.execute("DROP TABLE IF EXISTS typesRegressTest");
0568: this .stmt.execute("CREATE TABLE typesRegressTest ("
0569: + "varcharField VARCHAR(32),"
0570: + "charField CHAR(2)," + "enumField ENUM('1','2'),"
0571: + "setField SET('1','2','3'),"
0572: + "tinyblobField TINYBLOB,"
0573: + "mediumBlobField MEDIUMBLOB,"
0574: + "longblobField LONGBLOB," + "blobField BLOB)");
0575:
0576: this .rs = this .stmt
0577: .executeQuery("SELECT * from typesRegressTest");
0578:
0579: ResultSetMetaData rsmd = this .rs.getMetaData();
0580:
0581: int numCols = rsmd.getColumnCount();
0582:
0583: for (int i = 0; i < numCols; i++) {
0584: String columnName = rsmd.getColumnName(i + 1);
0585: String columnTypeName = rsmd.getColumnTypeName(i + 1);
0586: System.out
0587: .println(columnName + " -> " + columnTypeName);
0588: }
0589: } finally {
0590: this .stmt.execute("DROP TABLE IF EXISTS typesRegressTest");
0591: }
0592: }
0593:
0594: /**
0595: * Tests fix for BUG#4742, 'DOUBLE' mapped twice in getTypeInfo().
0596: *
0597: * @throws Exception
0598: * if the test fails.
0599: */
0600: public void testBug4742() throws Exception {
0601: HashMap clashMap = new HashMap();
0602:
0603: this .rs = this .conn.getMetaData().getTypeInfo();
0604:
0605: while (this .rs.next()) {
0606: String name = this .rs.getString(1);
0607: assertTrue("Type represented twice in type info, '" + name
0608: + "'.", !clashMap.containsKey(name));
0609: clashMap.put(name, name);
0610: }
0611: }
0612:
0613: /**
0614: * Tests fix for BUG#4138, getColumns() returns incorrect JDBC type for
0615: * unsigned columns.
0616: *
0617: * @throws Exception
0618: * if the test fails.
0619: */
0620: public void testBug4138() throws Exception {
0621: try {
0622: String[] typesToTest = new String[] { "TINYINT",
0623: "SMALLINT", "MEDIUMINT", "INTEGER", "BIGINT",
0624: "FLOAT", "DOUBLE", "DECIMAL" };
0625:
0626: short[] jdbcMapping = new short[] { Types.TINYINT,
0627: Types.SMALLINT, Types.INTEGER, Types.INTEGER,
0628: Types.BIGINT, Types.REAL, Types.DOUBLE,
0629: Types.DECIMAL };
0630:
0631: this .stmt.executeUpdate("DROP TABLE IF EXISTS testBug4138");
0632:
0633: StringBuffer createBuf = new StringBuffer();
0634:
0635: createBuf.append("CREATE TABLE testBug4138 (");
0636:
0637: boolean firstColumn = true;
0638:
0639: for (int i = 0; i < typesToTest.length; i++) {
0640: if (!firstColumn) {
0641: createBuf.append(", ");
0642: } else {
0643: firstColumn = false;
0644: }
0645:
0646: createBuf.append("field");
0647: createBuf.append((i + 1));
0648: createBuf.append(" ");
0649: createBuf.append(typesToTest[i]);
0650: createBuf.append(" UNSIGNED");
0651: }
0652: createBuf.append(")");
0653: this .stmt.executeUpdate(createBuf.toString());
0654:
0655: DatabaseMetaData dbmd = this .conn.getMetaData();
0656: this .rs = dbmd.getColumns(this .conn.getCatalog(), null,
0657: "testBug4138", "field%");
0658:
0659: assertTrue(this .rs.next());
0660:
0661: for (int i = 0; i < typesToTest.length; i++) {
0662: assertTrue(
0663: "JDBC Data Type of "
0664: + this .rs.getShort("DATA_TYPE")
0665: + " for MySQL type '"
0666: + this .rs.getString("TYPE_NAME")
0667: + "' from 'DATA_TYPE' column does not match expected value of "
0668: + jdbcMapping[i] + ".",
0669: jdbcMapping[i] == this .rs.getShort("DATA_TYPE"));
0670: this .rs.next();
0671: }
0672:
0673: this .rs.close();
0674:
0675: StringBuffer queryBuf = new StringBuffer("SELECT ");
0676: firstColumn = true;
0677:
0678: for (int i = 0; i < typesToTest.length; i++) {
0679: if (!firstColumn) {
0680: queryBuf.append(", ");
0681: } else {
0682: firstColumn = false;
0683: }
0684:
0685: queryBuf.append("field");
0686: queryBuf.append((i + 1));
0687: }
0688:
0689: queryBuf.append(" FROM testBug4138");
0690:
0691: this .rs = this .stmt.executeQuery(queryBuf.toString());
0692:
0693: ResultSetMetaData rsmd = this .rs.getMetaData();
0694:
0695: for (int i = 0; i < typesToTest.length; i++) {
0696:
0697: assertTrue(jdbcMapping[i] == rsmd.getColumnType(i + 1));
0698: String desiredTypeName = typesToTest[i] + " unsigned";
0699:
0700: assertTrue(rsmd.getColumnTypeName((i + 1)) + " != "
0701: + desiredTypeName,
0702: desiredTypeName.equalsIgnoreCase(rsmd
0703: .getColumnTypeName(i + 1)));
0704: }
0705: } finally {
0706: this .stmt.executeUpdate("DROP TABLE IF EXISTS testBug4138");
0707: }
0708: }
0709:
0710: /**
0711: * Here for housekeeping only, the test is actually in testBug4138().
0712: *
0713: * @throws Exception
0714: * if the test fails.
0715: */
0716: public void testBug4860() throws Exception {
0717: testBug4138();
0718: }
0719:
0720: /**
0721: * Tests fix for BUG#4880 - RSMD.getPrecision() returns '0' for non-numeric
0722: * types.
0723: *
0724: * Why-oh-why is this not in the spec, nor the api-docs, but in some
0725: * 'optional' book, _and_ it is a variance from both ODBC and the ANSI SQL
0726: * standard :p
0727: *
0728: * (from the CTS testsuite)....
0729: *
0730: * The getPrecision(int colindex) method returns an integer value
0731: * representing the number of decimal digits for number types,maximum length
0732: * in characters for character types,maximum length in bytes for JDBC binary
0733: * datatypes.
0734: *
0735: * (See Section 27.3 of JDBC 2.0 API Reference & Tutorial 2nd edition)
0736: *
0737: * @throws Exception
0738: * if the test fails.
0739: */
0740:
0741: public void testBug4880() throws Exception {
0742: try {
0743: this .stmt.executeUpdate("DROP TABLE IF EXISTS testBug4880");
0744: this .stmt
0745: .executeUpdate("CREATE TABLE testBug4880 (field1 VARCHAR(80), field2 TINYBLOB, field3 BLOB, field4 MEDIUMBLOB, field5 LONGBLOB)");
0746: this .rs = this .stmt
0747: .executeQuery("SELECT field1, field2, field3, field4, field5 FROM testBug4880");
0748: ResultSetMetaData rsmd = this .rs.getMetaData();
0749:
0750: assertEquals(80, rsmd.getPrecision(1));
0751: assertEquals(Types.VARCHAR, rsmd.getColumnType(1));
0752: assertEquals(80, rsmd.getColumnDisplaySize(1));
0753:
0754: assertEquals(255, rsmd.getPrecision(2));
0755: assertEquals(Types.VARBINARY, rsmd.getColumnType(2));
0756: assertTrue("TINYBLOB".equalsIgnoreCase(rsmd
0757: .getColumnTypeName(2)));
0758: assertEquals(255, rsmd.getColumnDisplaySize(2));
0759:
0760: assertEquals(65535, rsmd.getPrecision(3));
0761: assertEquals(Types.LONGVARBINARY, rsmd.getColumnType(3));
0762: assertTrue("BLOB".equalsIgnoreCase(rsmd
0763: .getColumnTypeName(3)));
0764: assertEquals(65535, rsmd.getColumnDisplaySize(3));
0765:
0766: assertEquals(16777215, rsmd.getPrecision(4));
0767: assertEquals(Types.LONGVARBINARY, rsmd.getColumnType(4));
0768: assertTrue("MEDIUMBLOB".equalsIgnoreCase(rsmd
0769: .getColumnTypeName(4)));
0770: assertEquals(16777215, rsmd.getColumnDisplaySize(4));
0771:
0772: if (versionMeetsMinimum(4, 1)) {
0773: // Server doesn't send us enough information to detect LONGBLOB
0774: // type
0775: assertEquals(Integer.MAX_VALUE, rsmd.getPrecision(5));
0776: assertEquals(Types.LONGVARBINARY, rsmd.getColumnType(5));
0777: assertTrue("LONGBLOB".equalsIgnoreCase(rsmd
0778: .getColumnTypeName(5)));
0779: assertEquals(Integer.MAX_VALUE, rsmd
0780: .getColumnDisplaySize(5));
0781: }
0782: } finally {
0783: this .stmt.executeUpdate("DROP TABLE IF EXISTS testBug4880");
0784: }
0785: }
0786:
0787: /**
0788: * Tests fix for BUG#6399, ResultSetMetaData.getDisplaySize() is wrong for
0789: * multi-byte charsets.
0790: *
0791: * @throws Exception
0792: * if the test fails
0793: */
0794: public void testBug6399() throws Exception {
0795: if (versionMeetsMinimum(4, 1)) {
0796: try {
0797: this .stmt
0798: .executeUpdate("DROP TABLE IF EXISTS testBug6399");
0799: this .stmt
0800: .executeUpdate("CREATE TABLE testBug6399 (field1 CHAR(3) CHARACTER SET UTF8, field2 CHAR(3) CHARACTER SET LATIN1, field3 CHAR(3) CHARACTER SET SJIS)");
0801: this .stmt
0802: .executeUpdate("INSERT INTO testBug6399 VALUES ('a', 'a', 'a')");
0803:
0804: this .rs = this .stmt
0805: .executeQuery("SELECT field1, field2, field3 FROM testBug6399");
0806: ResultSetMetaData rsmd = this .rs.getMetaData();
0807:
0808: assertTrue(3 == rsmd.getColumnDisplaySize(1));
0809: assertTrue(3 == rsmd.getColumnDisplaySize(2));
0810: assertTrue(3 == rsmd.getColumnDisplaySize(3));
0811: } finally {
0812: this .stmt
0813: .executeUpdate("DROP TABLE IF EXISTS testBug6399");
0814: }
0815: }
0816: }
0817:
0818: /**
0819: * Tests fix for BUG#7081, DatabaseMetaData.getIndexInfo() ignoring 'unique'
0820: * parameters.
0821: *
0822: * @throws Exception
0823: * if the test fails.
0824: */
0825: public void testBug7081() throws Exception {
0826: String tableName = "testBug7081";
0827:
0828: try {
0829: createTable(tableName, "(field1 INT, INDEX(field1))");
0830:
0831: DatabaseMetaData dbmd = this .conn.getMetaData();
0832: this .rs = dbmd.getIndexInfo(this .conn.getCatalog(), null,
0833: tableName, true, false);
0834: assertTrue(!this .rs.next()); // there should be no rows that meet
0835: // this requirement
0836:
0837: this .rs = dbmd.getIndexInfo(this .conn.getCatalog(), null,
0838: tableName, false, false);
0839: assertTrue(this .rs.next()); // there should be one row that meets
0840: // this requirement
0841: assertTrue(!this .rs.next());
0842:
0843: } finally {
0844: dropTable(tableName);
0845: }
0846: }
0847:
0848: /**
0849: * Tests fix for BUG#7033 - PreparedStatements don't encode Big5 (and other
0850: * multibyte) character sets correctly in static SQL strings.
0851: *
0852: * @throws Exception
0853: * if the test fails.
0854: */
0855: public void testBug7033() throws Exception {
0856: if (false) { // disabled for now
0857: Connection big5Conn = null;
0858: Statement big5Stmt = null;
0859: PreparedStatement big5PrepStmt = null;
0860:
0861: String testString = "\u5957 \u9910";
0862:
0863: try {
0864: Properties props = new Properties();
0865: props.setProperty("useUnicode", "true");
0866: props.setProperty("characterEncoding", "Big5");
0867:
0868: big5Conn = getConnectionWithProps(props);
0869: big5Stmt = big5Conn.createStatement();
0870:
0871: byte[] foobar = testString.getBytes("Big5");
0872: System.out.println(foobar);
0873:
0874: this .rs = big5Stmt
0875: .executeQuery("select 1 as '\u5957 \u9910'");
0876: String retrString = this .rs.getMetaData()
0877: .getColumnName(1);
0878: assertTrue(testString.equals(retrString));
0879:
0880: big5PrepStmt = big5Conn
0881: .prepareStatement("select 1 as '\u5957 \u9910'");
0882: this .rs = big5PrepStmt.executeQuery();
0883: retrString = this .rs.getMetaData().getColumnName(1);
0884: assertTrue(testString.equals(retrString));
0885: } finally {
0886: if (this .rs != null) {
0887: this .rs.close();
0888: this .rs = null;
0889: }
0890:
0891: if (big5Stmt != null) {
0892: big5Stmt.close();
0893:
0894: }
0895:
0896: if (big5PrepStmt != null) {
0897: big5PrepStmt.close();
0898: }
0899:
0900: if (big5Conn != null) {
0901: big5Conn.close();
0902: }
0903: }
0904: }
0905: }
0906:
0907: /**
0908: * Tests fix for Bug#8812, DBMD.getIndexInfo() returning inverted values for
0909: * 'NON_UNIQUE' column.
0910: *
0911: * @throws Exception
0912: * if the test fails.
0913: */
0914: public void testBug8812() throws Exception {
0915: String tableName = "testBug8812";
0916:
0917: try {
0918: createTable(tableName,
0919: "(field1 INT, field2 INT, INDEX(field1), UNIQUE INDEX(field2))");
0920:
0921: DatabaseMetaData dbmd = this .conn.getMetaData();
0922: this .rs = dbmd.getIndexInfo(this .conn.getCatalog(), null,
0923: tableName, true, false);
0924: assertTrue(this .rs.next()); // there should be one row that meets
0925: // this requirement
0926: assertEquals(this .rs.getBoolean("NON_UNIQUE"), false);
0927:
0928: this .rs = dbmd.getIndexInfo(this .conn.getCatalog(), null,
0929: tableName, false, false);
0930: assertTrue(this .rs.next()); // there should be two rows that meets
0931: // this requirement
0932: assertEquals(this .rs.getBoolean("NON_UNIQUE"), false);
0933: assertTrue(this .rs.next());
0934: assertEquals(this .rs.getBoolean("NON_UNIQUE"), true);
0935:
0936: } finally {
0937: dropTable(tableName);
0938: }
0939: }
0940:
0941: /**
0942: * Tests fix for BUG#8800 - supportsMixedCase*Identifiers() returns wrong
0943: * value on servers running on case-sensitive filesystems.
0944: */
0945:
0946: public void testBug8800() throws Exception {
0947: assertEquals(((com.mysql.jdbc.Connection) this .conn)
0948: .lowerCaseTableNames(), !this .conn.getMetaData()
0949: .supportsMixedCaseIdentifiers());
0950: assertEquals(((com.mysql.jdbc.Connection) this .conn)
0951: .lowerCaseTableNames(), !this .conn.getMetaData()
0952: .supportsMixedCaseQuotedIdentifiers());
0953:
0954: }
0955:
0956: /**
0957: * Tests fix for BUG#8792 - DBMD.supportsResultSetConcurrency() not
0958: * returning true for forward-only/read-only result sets (we obviously
0959: * support this).
0960: *
0961: * @throws Exception
0962: * if the test fails.
0963: */
0964: public void testBug8792() throws Exception {
0965: DatabaseMetaData dbmd = this .conn.getMetaData();
0966:
0967: assertTrue(dbmd
0968: .supportsResultSetConcurrency(
0969: ResultSet.TYPE_FORWARD_ONLY,
0970: ResultSet.CONCUR_READ_ONLY));
0971:
0972: assertTrue(dbmd
0973: .supportsResultSetConcurrency(
0974: ResultSet.TYPE_FORWARD_ONLY,
0975: ResultSet.CONCUR_UPDATABLE));
0976:
0977: assertTrue(dbmd.supportsResultSetConcurrency(
0978: ResultSet.TYPE_SCROLL_INSENSITIVE,
0979: ResultSet.CONCUR_READ_ONLY));
0980:
0981: assertTrue(dbmd.supportsResultSetConcurrency(
0982: ResultSet.TYPE_SCROLL_INSENSITIVE,
0983: ResultSet.CONCUR_UPDATABLE));
0984:
0985: assertTrue(!dbmd.supportsResultSetConcurrency(
0986: ResultSet.TYPE_SCROLL_SENSITIVE,
0987: ResultSet.CONCUR_READ_ONLY));
0988:
0989: assertTrue(!dbmd.supportsResultSetConcurrency(
0990: ResultSet.TYPE_SCROLL_SENSITIVE,
0991: ResultSet.CONCUR_UPDATABLE));
0992:
0993: // Check error conditions
0994: try {
0995: dbmd.supportsResultSetConcurrency(
0996: ResultSet.TYPE_FORWARD_ONLY, Integer.MIN_VALUE);
0997: fail("Exception should've been raised for bogus concurrency value");
0998: } catch (SQLException sqlEx) {
0999: assertTrue(SQLError.SQL_STATE_ILLEGAL_ARGUMENT.equals(sqlEx
1000: .getSQLState()));
1001: }
1002:
1003: try {
1004: assertTrue(dbmd.supportsResultSetConcurrency(
1005: ResultSet.TYPE_SCROLL_INSENSITIVE,
1006: Integer.MIN_VALUE));
1007: fail("Exception should've been raised for bogus concurrency value");
1008: } catch (SQLException sqlEx) {
1009: assertTrue(SQLError.SQL_STATE_ILLEGAL_ARGUMENT.equals(sqlEx
1010: .getSQLState()));
1011: }
1012:
1013: try {
1014: assertTrue(dbmd.supportsResultSetConcurrency(
1015: Integer.MIN_VALUE, Integer.MIN_VALUE));
1016: fail("Exception should've been raised for bogus concurrency value");
1017: } catch (SQLException sqlEx) {
1018: assertTrue(SQLError.SQL_STATE_ILLEGAL_ARGUMENT.equals(sqlEx
1019: .getSQLState()));
1020: }
1021: }
1022:
1023: /**
1024: * Tests fix for BUG#8803, 'DATA_TYPE' column from
1025: * DBMD.getBestRowIdentifier() causes ArrayIndexOutOfBoundsException when
1026: * accessed (and in fact, didn't return any value).
1027: *
1028: * @throws Exception
1029: * if the test fails.
1030: */
1031: public void testBug8803() throws Exception {
1032: String tableName = "testBug8803";
1033: createTable(tableName, "(field1 INT NOT NULL PRIMARY KEY)");
1034: DatabaseMetaData metadata = this .conn.getMetaData();
1035: try {
1036: this .rs = metadata.getBestRowIdentifier(this .conn
1037: .getCatalog(), null, tableName,
1038: DatabaseMetaData.bestRowNotPseudo, true);
1039:
1040: assertTrue(this .rs.next());
1041:
1042: this .rs.getInt("DATA_TYPE"); // **** Fails here *****
1043: } finally {
1044: if (this .rs != null) {
1045: this .rs.close();
1046:
1047: this .rs = null;
1048: }
1049: }
1050:
1051: }
1052:
1053: /**
1054: * Tests fix for BUG#9320 - PreparedStatement.getMetaData() inserts blank
1055: * row in database under certain conditions when not using server-side
1056: * prepared statements.
1057: *
1058: * @throws Exception
1059: * if the test fails.
1060: */
1061: public void testBug9320() throws Exception {
1062: createTable("testBug9320", "(field1 int)");
1063:
1064: testAbsenceOfMetadataForQuery("INSERT INTO testBug9320 VALUES (?)");
1065: testAbsenceOfMetadataForQuery("UPDATE testBug9320 SET field1=?");
1066: testAbsenceOfMetadataForQuery("DELETE FROM testBug9320 WHERE field1=?");
1067: }
1068:
1069: /**
1070: * Tests fix for BUG#9778, DBMD.getTables() shouldn't return tables if views
1071: * are asked for, even if the database version doesn't support views.
1072: *
1073: * @throws Exception
1074: * if the test fails.
1075: */
1076: public void testBug9778() throws Exception {
1077: String tableName = "testBug9778";
1078:
1079: try {
1080: createTable(tableName, "(field1 int)");
1081: this .rs = this .conn.getMetaData().getTables(null, null,
1082: tableName, new String[] { "VIEW" });
1083: assertEquals(false, this .rs.next());
1084:
1085: this .rs = this .conn.getMetaData().getTables(null, null,
1086: tableName, new String[] { "TABLE" });
1087: assertEquals(true, this .rs.next());
1088: } finally {
1089: if (this .rs != null) {
1090: this .rs.close();
1091: this .rs = null;
1092: }
1093: }
1094: }
1095:
1096: /**
1097: * Tests fix for BUG#9769 - Should accept null for procedureNamePattern,
1098: * even though it isn't JDBC compliant, for legacy's sake.
1099: *
1100: * @throws Exception
1101: * if the test fails.
1102: */
1103: public void testBug9769() throws Exception {
1104: boolean defaultPatternConfig = ((com.mysql.jdbc.Connection) this .conn)
1105: .getNullNamePatternMatchesAll();
1106:
1107: // We're going to change this in 3.2.x, so make that test here, so we
1108: // catch it.
1109:
1110: if (this .conn.getMetaData().getDriverMajorVersion() == 3
1111: && this .conn.getMetaData().getDriverMinorVersion() >= 2) {
1112: assertEquals(false, defaultPatternConfig);
1113: } else {
1114: assertEquals(true, defaultPatternConfig);
1115: }
1116:
1117: try {
1118: this .conn.getMetaData().getProcedures(
1119: this .conn.getCatalog(), "%", null);
1120:
1121: if (!defaultPatternConfig) {
1122: // we shouldn't have gotten here
1123: fail("Exception should've been thrown");
1124: }
1125: } catch (SQLException sqlEx) {
1126: if (!defaultPatternConfig) {
1127: assertEquals(SQLError.SQL_STATE_ILLEGAL_ARGUMENT, sqlEx
1128: .getSQLState());
1129: } else {
1130: throw sqlEx; // we shouldn't have gotten an exception here
1131: }
1132: }
1133:
1134: // FIXME: TO test for 3.1.9
1135: // getColumns();
1136: // getTablePrivileges();
1137: // getTables();
1138:
1139: }
1140:
1141: /**
1142: * Tests fix for BUG#9917 - Should accept null for catalog in DBMD methods,
1143: * even though it's not JDBC-compliant for legacy's sake.
1144: *
1145: * @throws Exception
1146: * if the test fails.
1147: */
1148: public void testBug9917() throws Exception {
1149: String tableName = "testBug9917";
1150: boolean defaultCatalogConfig = ((com.mysql.jdbc.Connection) this .conn)
1151: .getNullCatalogMeansCurrent();
1152:
1153: // We're going to change this in 3.2.x, so make that test here, so we
1154: // catch it.
1155:
1156: if (this .conn.getMetaData().getDriverMajorVersion() == 3
1157: && this .conn.getMetaData().getDriverMinorVersion() >= 2) {
1158: assertEquals(false, defaultCatalogConfig);
1159: } else {
1160: assertEquals(true, defaultCatalogConfig);
1161: }
1162:
1163: try {
1164: createTable(tableName, "(field1 int)");
1165: String currentCatalog = this .conn.getCatalog();
1166:
1167: try {
1168: this .rs = this .conn.getMetaData().getTables(null, null,
1169: tableName, new String[] { "TABLE" });
1170:
1171: if (!defaultCatalogConfig) {
1172: // we shouldn't have gotten here
1173: fail("Exception should've been thrown");
1174: }
1175:
1176: assertEquals(true, this .rs.next());
1177: assertEquals(currentCatalog, this .rs
1178: .getString("TABLE_CAT"));
1179:
1180: // FIXME: Methods to test for 3.1.9
1181: //
1182: // getBestRowIdentifier()
1183: // getColumns()
1184: // getCrossReference()
1185: // getExportedKeys()
1186: // getImportedKeys()
1187: // getIndexInfo()
1188: // getPrimaryKeys()
1189: // getProcedures()
1190:
1191: } catch (SQLException sqlEx) {
1192: if (!defaultCatalogConfig) {
1193: assertEquals(SQLError.SQL_STATE_ILLEGAL_ARGUMENT,
1194: sqlEx.getSQLState());
1195: } else {
1196: throw sqlEx; // we shouldn't have gotten an exception
1197: // here
1198: }
1199: }
1200:
1201: } finally {
1202: if (this .rs != null) {
1203: this .rs.close();
1204: this .rs = null;
1205: }
1206: }
1207: }
1208:
1209: /**
1210: * Tests fix for BUG#11575 -- DBMD.storesLower/Mixed/UpperIdentifiers()
1211: * reports incorrect values for servers deployed on Windows.
1212: *
1213: * @throws Exception
1214: * if the test fails.
1215: */
1216: public void testBug11575() throws Exception {
1217: DatabaseMetaData dbmd = this .conn.getMetaData();
1218:
1219: if (isServerRunningOnWindows()) {
1220: assertEquals(true, dbmd.storesLowerCaseIdentifiers());
1221: assertEquals(true, dbmd.storesLowerCaseQuotedIdentifiers());
1222: assertEquals(false, dbmd.storesMixedCaseIdentifiers());
1223: assertEquals(false, dbmd.storesMixedCaseQuotedIdentifiers());
1224: assertEquals(false, dbmd.storesUpperCaseIdentifiers());
1225: assertEquals(true, dbmd.storesUpperCaseQuotedIdentifiers());
1226: } else {
1227: assertEquals(false, dbmd.storesLowerCaseIdentifiers());
1228: assertEquals(false, dbmd.storesLowerCaseQuotedIdentifiers());
1229: assertEquals(true, dbmd.storesMixedCaseIdentifiers());
1230: assertEquals(true, dbmd.storesMixedCaseQuotedIdentifiers());
1231: assertEquals(false, dbmd.storesUpperCaseIdentifiers());
1232: assertEquals(true, dbmd.storesUpperCaseQuotedIdentifiers());
1233: }
1234: }
1235:
1236: /**
1237: * Tests fix for BUG#11781, foreign key information that is quoted is parsed
1238: * incorrectly.
1239: */
1240: public void testBug11781() throws Exception {
1241:
1242: if (versionMeetsMinimum(5, 1)) {
1243: if (!versionMeetsMinimum(5, 2)) {
1244: // server bug prevents this test from functioning
1245:
1246: return;
1247: }
1248: }
1249:
1250: createTable(
1251: "`app tab`",
1252: "( C1 int(11) NULL, INDEX NEWINX (C1), INDEX NEWINX2 (C1)) ENGINE = InnoDB CHECKSUM = 0 COMMENT = 'InnoDB free: 3072 kB; (`C1`) REFER`test/app tab`(`C1`)' PACK_KEYS = 0");
1253:
1254: this .stmt
1255: .executeUpdate("ALTER TABLE `app tab` ADD CONSTRAINT APPFK FOREIGN KEY (C1) REFERENCES `app tab` (C1)");
1256:
1257: /*
1258: * this.rs = this.conn.getMetaData().getCrossReference(
1259: * this.conn.getCatalog(), null, "app tab", this.conn.getCatalog(),
1260: * null, "app tab");
1261: */
1262: this .rs = ((com.mysql.jdbc.DatabaseMetaData) this .conn
1263: .getMetaData()).extractForeignKeyFromCreateTable(
1264: this .conn.getCatalog(), "app tab");
1265: assertTrue("must return a row", this .rs.next());
1266:
1267: String catalog = this .conn.getCatalog();
1268:
1269: assertEquals("comment; APPFK(`C1`) REFER `" + catalog
1270: + "`/ `app tab` (`C1`)", this .rs.getString(3));
1271:
1272: this .rs.close();
1273:
1274: this .rs = this .conn.getMetaData().getImportedKeys(
1275: this .conn.getCatalog(), null, "app tab");
1276:
1277: assertTrue(this .rs.next());
1278:
1279: this .rs = this .conn.getMetaData().getExportedKeys(
1280: this .conn.getCatalog(), null, "app tab");
1281:
1282: assertTrue(this .rs.next());
1283: }
1284:
1285: /**
1286: * Tests fix for BUG#12970 - java.sql.Types.OTHER returned for binary and
1287: * varbinary columns.
1288: *
1289: */
1290: public void testBug12970() throws Exception {
1291: if (versionMeetsMinimum(5, 0, 8)) {
1292: String tableName = "testBug12970";
1293:
1294: createTable(tableName,
1295: "(binary_field BINARY(32), varbinary_field VARBINARY(64))");
1296:
1297: try {
1298: this .rs = this .conn.getMetaData().getColumns(
1299: this .conn.getCatalog(), null, tableName, "%");
1300: assertTrue(this .rs.next());
1301: assertEquals(Types.BINARY, this .rs.getInt("DATA_TYPE"));
1302: assertEquals(32, this .rs.getInt("COLUMN_SIZE"));
1303: assertTrue(this .rs.next());
1304: assertEquals(Types.VARBINARY, this .rs
1305: .getInt("DATA_TYPE"));
1306: assertEquals(64, this .rs.getInt("COLUMN_SIZE"));
1307: this .rs.close();
1308:
1309: this .rs = this .stmt
1310: .executeQuery("SELECT binary_field, varbinary_field FROM "
1311: + tableName);
1312: ResultSetMetaData rsmd = this .rs.getMetaData();
1313: assertEquals(Types.BINARY, rsmd.getColumnType(1));
1314: assertEquals(32, rsmd.getPrecision(1));
1315: assertEquals(Types.VARBINARY, rsmd.getColumnType(2));
1316: assertEquals(64, rsmd.getPrecision(2));
1317: this .rs.close();
1318: } finally {
1319: if (this .rs != null) {
1320: this .rs.close();
1321: }
1322: }
1323: }
1324: }
1325:
1326: /**
1327: * Tests fix for BUG#12975 - OpenOffice expects DBMD.supportsIEF() to return
1328: * "true" if foreign keys are supported by the datasource, even though this
1329: * method also covers support for check constraints, which MySQL _doesn't_
1330: * have.
1331: *
1332: * @throws Exception
1333: * if the test fails.
1334: */
1335: public void testBug12975() throws Exception {
1336: assertEquals(false, this .conn.getMetaData()
1337: .supportsIntegrityEnhancementFacility());
1338:
1339: Connection overrideConn = null;
1340:
1341: try {
1342: Properties props = new Properties();
1343:
1344: props.setProperty(
1345: "overrideSupportsIntegrityEnhancementFacility",
1346: "true");
1347:
1348: overrideConn = getConnectionWithProps(props);
1349: assertEquals(true, overrideConn.getMetaData()
1350: .supportsIntegrityEnhancementFacility());
1351: } finally {
1352: if (overrideConn != null) {
1353: overrideConn.close();
1354: }
1355: }
1356: }
1357:
1358: /**
1359: * Tests fix for BUG#13277 - RSMD for generated keys has NPEs when a
1360: * connection is referenced.
1361: *
1362: * @throws Exception
1363: */
1364: public void testBug13277() throws Exception {
1365: if (isRunningOnJdk131()) {
1366: return; // test not valid on JDK-1.3.1
1367: }
1368:
1369: createTable("testBug13277",
1370: "(field1 INT NOT NULL PRIMARY KEY AUTO_INCREMENT, field2 VARCHAR(32))");
1371:
1372: try {
1373: this .stmt
1374: .executeUpdate(
1375: "INSERT INTO testBug13277 (field2) VALUES ('abcdefg')",
1376: Statement.RETURN_GENERATED_KEYS);
1377:
1378: this .rs = this .stmt.getGeneratedKeys();
1379:
1380: ResultSetMetaData rsmd = this .rs.getMetaData();
1381: checkRsmdForBug13277(rsmd);
1382: this .rs.close();
1383:
1384: for (int i = 0; i < 5; i++) {
1385: this .stmt
1386: .addBatch("INSERT INTO testBug13277 (field2) VALUES ('abcdefg')");
1387: }
1388:
1389: this .stmt.executeBatch();
1390:
1391: this .rs = this .stmt.getGeneratedKeys();
1392:
1393: rsmd = this .rs.getMetaData();
1394: checkRsmdForBug13277(rsmd);
1395: this .rs.close();
1396:
1397: this .pstmt = this .conn
1398: .prepareStatement(
1399: "INSERT INTO testBug13277 (field2) VALUES ('abcdefg')",
1400: Statement.RETURN_GENERATED_KEYS);
1401: this .pstmt.executeUpdate();
1402:
1403: this .rs = this .pstmt.getGeneratedKeys();
1404:
1405: rsmd = this .rs.getMetaData();
1406: checkRsmdForBug13277(rsmd);
1407: this .rs.close();
1408:
1409: this .pstmt.addBatch();
1410: this .pstmt.addBatch();
1411:
1412: this .pstmt.executeUpdate();
1413:
1414: this .rs = this .pstmt.getGeneratedKeys();
1415:
1416: rsmd = this .rs.getMetaData();
1417: checkRsmdForBug13277(rsmd);
1418: this .rs.close();
1419:
1420: } finally {
1421: if (this .pstmt != null) {
1422: this .pstmt.close();
1423: this .pstmt = null;
1424: }
1425:
1426: if (this .rs != null) {
1427: this .rs.close();
1428: this .rs = null;
1429: }
1430: }
1431: }
1432:
1433: /**
1434: * Tests BUG13601 (which doesn't seem to be present in 3.1.11, but we'll
1435: * leave it in here for regression's-sake).
1436: *
1437: * @throws Exception
1438: * if the test fails.
1439: */
1440: public void testBug13601() throws Exception {
1441:
1442: if (versionMeetsMinimum(5, 0)) {
1443: createTable("testBug13601",
1444: "(field1 BIGINT NOT NULL, field2 BIT default 0 NOT NULL) ENGINE=MyISAM");
1445:
1446: this .rs = this .stmt
1447: .executeQuery("SELECT field1, field2 FROM testBug13601 WHERE 1=-1");
1448: ResultSetMetaData rsmd = this .rs.getMetaData();
1449: assertEquals(Types.BIT, rsmd.getColumnType(2));
1450: assertEquals(Boolean.class.getName(), rsmd
1451: .getColumnClassName(2));
1452:
1453: this .rs = this .conn
1454: .prepareStatement(
1455: "SELECT field1, field2 FROM testBug13601 WHERE 1=-1")
1456: .executeQuery();
1457: rsmd = this .rs.getMetaData();
1458: assertEquals(Types.BIT, rsmd.getColumnType(2));
1459: assertEquals(Boolean.class.getName(), rsmd
1460: .getColumnClassName(2));
1461:
1462: }
1463: }
1464:
1465: /**
1466: * Tests fix for BUG#14815 - DBMD.getColumns() doesn't return TABLE_NAME
1467: * correctly.
1468: *
1469: * @throws Exception
1470: * if the test fails
1471: */
1472: public void testBug14815() throws Exception {
1473: try {
1474: createTable("testBug14815_1", "(field_1_1 int)");
1475: createTable("testBug14815_2", "(field_2_1 int)");
1476:
1477: boolean lcTableNames = this .conn.getMetaData()
1478: .storesLowerCaseIdentifiers();
1479:
1480: String tableName1 = lcTableNames ? "testbug14815_1"
1481: : "testBug14815_1";
1482: String tableName2 = lcTableNames ? "testbug14815_2"
1483: : "testBug14815_2";
1484:
1485: this .rs = this .conn.getMetaData().getColumns(
1486: this .conn.getCatalog(), null, "testBug14815%", "%");
1487:
1488: assertTrue(this .rs.next());
1489: assertEquals(tableName1, this .rs.getString("TABLE_NAME"));
1490: assertEquals("field_1_1", this .rs.getString("COLUMN_NAME"));
1491:
1492: assertTrue(this .rs.next());
1493: assertEquals(tableName2, this .rs.getString("TABLE_NAME"));
1494: assertEquals("field_2_1", this .rs.getString("COLUMN_NAME"));
1495:
1496: } finally {
1497: if (this .rs != null) {
1498: this .rs.close();
1499: this .rs = null;
1500: }
1501: }
1502: }
1503:
1504: /**
1505: * Tests fix for BUG#15854 - DBMD.getColumns() returns wrong type for BIT.
1506: *
1507: * @throws Exception
1508: * if the test fails.
1509: */
1510: public void testBug15854() throws Exception {
1511: if (versionMeetsMinimum(5, 0)) {
1512: createTable("testBug15854", "(field1 BIT)");
1513: try {
1514: this .rs = this .conn.getMetaData().getColumns(
1515: this .conn.getCatalog(), null, "testBug15854",
1516: "field1");
1517: assertTrue(this .rs.next());
1518: assertEquals(Types.BIT, this .rs.getInt("DATA_TYPE"));
1519: } finally {
1520: if (this .rs != null) {
1521: ResultSet toClose = this .rs;
1522: this .rs = null;
1523: toClose.close();
1524: }
1525: }
1526:
1527: }
1528: }
1529:
1530: /**
1531: * Tests fix for BUG#16277 - Invalid classname returned for
1532: * RSMD.getColumnClassName() for BIGINT type.
1533: *
1534: * @throws Exception
1535: * if the test fails.
1536: */
1537: public void testBug16277() throws Exception {
1538: createTable("testBug16277",
1539: "(field1 BIGINT, field2 BIGINT UNSIGNED)");
1540: ResultSetMetaData rsmd = this .stmt.executeQuery(
1541: "SELECT field1, field2 FROM testBug16277")
1542: .getMetaData();
1543: assertEquals("java.lang.Long", rsmd.getColumnClassName(1));
1544: assertEquals("java.math.BigInteger", rsmd.getColumnClassName(2));
1545: }
1546:
1547: /**
1548: * Tests fix for BUG#18554 - Aliased column names where length of name > 251
1549: * are corrupted.
1550: *
1551: * @throws Exception
1552: * if the test fails.
1553: */
1554: public void testBug18554() throws Exception {
1555: testBug18554(249);
1556: testBug18554(250);
1557: testBug18554(251);
1558: testBug18554(252);
1559: testBug18554(253);
1560: testBug18554(254);
1561: testBug18554(255);
1562: }
1563:
1564: private void testBug18554(int columnNameLength) throws Exception {
1565: StringBuffer buf = new StringBuffer(columnNameLength + 2);
1566:
1567: for (int i = 0; i < columnNameLength; i++) {
1568: buf.append((char) ((Math.random() * 26) + 65));
1569: }
1570:
1571: String colName = buf.toString();
1572: this .rs = this .stmt.executeQuery("select curtime() as `"
1573: + colName + "`");
1574: ResultSetMetaData meta = this .rs.getMetaData();
1575:
1576: assertEquals(colName, meta.getColumnLabel(1));
1577:
1578: }
1579:
1580: private void checkRsmdForBug13277(ResultSetMetaData rsmd)
1581: throws SQLException {
1582: assertEquals(17, rsmd.getColumnDisplaySize(1));
1583:
1584: if (versionMeetsMinimum(4, 1)) {
1585: assertEquals(false, rsmd.isDefinitelyWritable(1));
1586: assertEquals(true, rsmd.isReadOnly(1));
1587: assertEquals(false, rsmd.isWritable(1));
1588: }
1589: }
1590:
1591: public void testSupportsCorrelatedSubqueries() throws Exception {
1592: DatabaseMetaData dbmd = this .conn.getMetaData();
1593:
1594: assertEquals(versionMeetsMinimum(4, 1), dbmd
1595: .supportsCorrelatedSubqueries());
1596: }
1597:
1598: public void testSupportesGroupByUnrelated() throws Exception {
1599: DatabaseMetaData dbmd = this .conn.getMetaData();
1600:
1601: assertEquals(true, dbmd.supportsGroupByUnrelated());
1602: }
1603:
1604: /**
1605: * Tests fix for BUG#21267, ParameterMetaData throws NullPointerException
1606: * when prepared SQL actually has a syntax error
1607: *
1608: * @throws Exception
1609: */
1610: public void testBug21267() throws Exception {
1611: if (isRunningOnJdk131()) {
1612: return; // no parameter metadata on JDK-1.3.1
1613: }
1614:
1615: createTable(
1616: "bug21267",
1617: "(`Col1` int(11) NOT NULL,`Col2` varchar(45) default NULL,`Col3` varchar(45) default NULL,PRIMARY KEY (`Col1`))");
1618:
1619: try {
1620: this .pstmt = this .conn
1621: .prepareStatement("SELECT Col1, Col2,Col4 FROM bug21267 WHERE Col1=?");
1622: this .pstmt.setInt(1, 1);
1623:
1624: java.sql.ParameterMetaData psMeta = this .pstmt
1625: .getParameterMetaData();
1626:
1627: try {
1628: assertEquals(0, psMeta.getParameterType(1));
1629: } catch (SQLException sqlEx) {
1630: assertEquals(SQLError.SQL_STATE_DRIVER_NOT_CAPABLE,
1631: sqlEx.getSQLState());
1632: }
1633:
1634: this .pstmt.close();
1635:
1636: Properties props = new Properties();
1637: props
1638: .setProperty("generateSimpleParameterMetadata",
1639: "true");
1640:
1641: this .pstmt = getConnectionWithProps(props)
1642: .prepareStatement(
1643: "SELECT Col1, Col2,Col4 FROM bug21267 WHERE Col1=?");
1644:
1645: psMeta = this .pstmt.getParameterMetaData();
1646:
1647: assertEquals(Types.VARCHAR, psMeta.getParameterType(1));
1648: } finally {
1649: closeMemberJDBCResources();
1650: }
1651: }
1652:
1653: /**
1654: * Tests fix for BUG#21544 - When using information_schema for metadata,
1655: * COLUMN_SIZE for getColumns() is not clamped to range of
1656: * java.lang.Integer as is the case when not using
1657: * information_schema, thus leading to a truncation exception that
1658: * isn't present when not using information_schema.
1659: *
1660: * @throws Exception if the test fails
1661: */
1662: public void testBug21544() throws Exception {
1663: if (!versionMeetsMinimum(5, 0)) {
1664: return;
1665: }
1666:
1667: createTable("testBug21544",
1668: "(foo_id INT NOT NULL, stuff LONGTEXT"
1669: + ", PRIMARY KEY (foo_id)) TYPE=INNODB");
1670:
1671: Connection infoSchemConn = null;
1672:
1673: Properties props = new Properties();
1674: props.setProperty("useInformationSchema", "true");
1675: props.setProperty("jdbcCompliantTruncation", "false");
1676:
1677: infoSchemConn = getConnectionWithProps(props);
1678:
1679: try {
1680: this .rs = infoSchemConn.getMetaData().getColumns(null,
1681: null, "testBug21544", null);
1682:
1683: while (rs.next()) {
1684: rs.getInt("COLUMN_SIZE");
1685: }
1686: } finally {
1687: if (infoSchemConn != null) {
1688: infoSchemConn.close();
1689: }
1690:
1691: closeMemberJDBCResources();
1692: }
1693: }
1694:
1695: /**
1696: * Tests fix for BUG#22613 - DBMD.getColumns() does not return expected
1697: * COLUMN_SIZE for the SET type (fixed to be consistent with the ODBC driver)
1698: *
1699: * @throws Exception if the test fails
1700: */
1701: public void testBug22613() throws Exception {
1702:
1703: createTable("bug22613",
1704: "( s set('a','bc','def','ghij') default NULL, t enum('a', 'ab', 'cdef'))");
1705:
1706: try {
1707: checkMetadataForBug22613(this .conn);
1708:
1709: if (versionMeetsMinimum(5, 0)) {
1710: Connection infoSchemConn = null;
1711:
1712: try {
1713: Properties props = new Properties();
1714: props.setProperty("useInformationSchema", "true");
1715:
1716: infoSchemConn = getConnectionWithProps(props);
1717:
1718: checkMetadataForBug22613(infoSchemConn);
1719: } finally {
1720: if (infoSchemConn != null) {
1721: infoSchemConn.close();
1722: }
1723: }
1724: }
1725: } finally {
1726: closeMemberJDBCResources();
1727: }
1728: }
1729:
1730: private void checkMetadataForBug22613(Connection c)
1731: throws Exception {
1732: String maxValue = "a,bc,def,ghij";
1733:
1734: try {
1735: DatabaseMetaData meta = c.getMetaData();
1736: this .rs = meta.getColumns(null, this .conn.getCatalog(),
1737: "bug22613", "s");
1738: this .rs.first();
1739:
1740: assertEquals(maxValue.length(), rs.getInt("COLUMN_SIZE"));
1741:
1742: this .rs = meta.getColumns(null, c.getCatalog(), "bug22613",
1743: "t");
1744: this .rs.first();
1745:
1746: assertEquals(4, rs.getInt("COLUMN_SIZE"));
1747: } finally {
1748: closeMemberJDBCResources();
1749: }
1750: }
1751:
1752: /**
1753: * Fix for BUG#22628 - Driver.getPropertyInfo() throws NullPointerException for URL that only specifies
1754: * host and/or port.
1755: *
1756: * @throws Exception if the test fails.
1757: */
1758: public void testBug22628() throws Exception {
1759: DriverPropertyInfo[] dpi = new NonRegisteringDriver()
1760: .getPropertyInfo("jdbc:mysql://bogus:9999",
1761: new Properties());
1762:
1763: boolean foundHost = false;
1764:
1765: for (int i = 0; i < dpi.length; i++) {
1766: if ("bogus:9999".equals(dpi[i].value)) {
1767: foundHost = true;
1768: break;
1769: }
1770: }
1771:
1772: assertTrue(foundHost);
1773: }
1774:
1775: private void testAbsenceOfMetadataForQuery(String query)
1776: throws Exception {
1777: try {
1778: this .pstmt = this .conn.prepareStatement(query);
1779: ResultSetMetaData rsmd = this .pstmt.getMetaData();
1780:
1781: assertNull(rsmd);
1782:
1783: this .pstmt = ((com.mysql.jdbc.Connection) this .conn)
1784: .clientPrepareStatement(query);
1785: rsmd = this .pstmt.getMetaData();
1786:
1787: assertNull(rsmd);
1788: } finally {
1789: if (this .pstmt != null) {
1790: this .pstmt.close();
1791: }
1792: }
1793: }
1794:
1795: public void testRSMDToStringFromDBMD() throws Exception {
1796: try {
1797: this .rs = this .conn.getMetaData().getTypeInfo();
1798:
1799: this .rs.getMetaData().toString(); // used to cause NPE
1800: } finally {
1801: closeMemberJDBCResources();
1802: }
1803: }
1804:
1805: public void testCharacterSetForDBMD() throws Exception {
1806: if (versionMeetsMinimum(4, 0)) {
1807: // server is broken, fixed in 5.2/6.0?
1808:
1809: if (!versionMeetsMinimum(5, 2)) {
1810: return;
1811: }
1812: }
1813:
1814: String quoteChar = this .conn.getMetaData()
1815: .getIdentifierQuoteString();
1816:
1817: String tableName = quoteChar + "\u00e9\u0074\u00e9" + quoteChar;
1818: createTable(tableName, "(field1 int)");
1819: this .rs = this .conn.getMetaData().getTables(
1820: this .conn.getCatalog(), null, tableName,
1821: new String[] { "TABLE" });
1822: assertEquals(true, this .rs.next());
1823: System.out.println(this .rs.getString("TABLE_NAME"));
1824: System.out.println(new String(this .rs.getBytes("TABLE_NAME"),
1825: "UTF-8"));
1826: }
1827:
1828: /**
1829: * Tests fix for BUG#18258 - Nonexistent catalog/database causes SQLException
1830: * to be raised, rather than returning empty result set.
1831: *
1832: * @throws Exception if the test fails.
1833: */
1834: public void testBug18258() throws Exception {
1835: String bogusDatabaseName = "abcdefghijklmnopqrstuvwxyz";
1836: this .conn.getMetaData().getTables(bogusDatabaseName, "%", "%",
1837: new String[] { "TABLE", "VIEW" });
1838: this .conn.getMetaData().getColumns(bogusDatabaseName, "%", "%",
1839: "%");
1840: this .conn.getMetaData().getProcedures(bogusDatabaseName, "%",
1841: "%");
1842: }
1843:
1844: /**
1845: * Tests fix for BUG#23303 - DBMD.getSchemas() doesn't return a TABLE_CATALOG column.
1846: *
1847: * @throws Exception if the test fails.
1848: */
1849: public void testBug23303() throws Exception {
1850: try {
1851: this .rs = this .conn.getMetaData().getSchemas();
1852: this .rs.findColumn("TABLE_CATALOG");
1853: } finally {
1854: closeMemberJDBCResources();
1855: }
1856: }
1857:
1858: /**
1859: * Tests fix for BUG#23304 - DBMD using "show" and DBMD using
1860: * information_schema do not return results consistent with eachother.
1861: *
1862: * (note this fix only addresses the inconsistencies, not the issue that
1863: * the driver is treating schemas differently than some users expect.
1864: *
1865: * We will revisit this behavior when there is full support for schemas
1866: * in MySQL).
1867: *
1868: * @throws Exception
1869: */
1870: public void testBug23304() throws Exception {
1871: if (!versionMeetsMinimum(5, 0)) {
1872: return;
1873: }
1874:
1875: Connection connShow = null;
1876: Connection connInfoSchema = null;
1877:
1878: ResultSet rsShow = null;
1879: ResultSet rsInfoSchema = null;
1880:
1881: try {
1882: Properties noInfoSchemaProps = new Properties();
1883: noInfoSchemaProps.setProperty("useInformationSchema",
1884: "false");
1885:
1886: Properties infoSchemaProps = new Properties();
1887: infoSchemaProps.setProperty("useInformationSchema", "true");
1888: infoSchemaProps.setProperty("dumpQueriesOnException",
1889: "true");
1890:
1891: connShow = getConnectionWithProps(noInfoSchemaProps);
1892: connInfoSchema = getConnectionWithProps(infoSchemaProps);
1893:
1894: DatabaseMetaData dbmdUsingShow = connShow.getMetaData();
1895: DatabaseMetaData dbmdUsingInfoSchema = connInfoSchema
1896: .getMetaData();
1897:
1898: assertNotSame(dbmdUsingShow.getClass(), dbmdUsingInfoSchema
1899: .getClass());
1900:
1901: if (!isRunningOnJdk131()) {
1902: rsShow = dbmdUsingShow.getSchemas();
1903: rsInfoSchema = dbmdUsingInfoSchema.getSchemas();
1904:
1905: compareResultSets(rsShow, rsInfoSchema);
1906: }
1907:
1908: /*
1909: rsShow = dbmdUsingShow.getTables(connShow.getCatalog(), null, "%", new String[] {"TABLE", "VIEW"});
1910: rsInfoSchema = dbmdUsingInfoSchema.getTables(connInfoSchema.getCatalog(), null, "%", new String[] {"TABLE", "VIEW"});
1911:
1912: compareResultSets(rsShow, rsInfoSchema);
1913:
1914: rsShow = dbmdUsingShow.getTables(null, null, "%", new String[] {"TABLE", "VIEW"});
1915: rsInfoSchema = dbmdUsingInfoSchema.getTables(null, null, "%", new String[] {"TABLE", "VIEW"});
1916:
1917: compareResultSets(rsShow, rsInfoSchema);
1918: */
1919:
1920: createTable(
1921: "t_testBug23304",
1922: "(field1 int primary key not null, field2 tinyint, field3 mediumint, field4 mediumint, field5 bigint, field6 float, field7 double, field8 decimal, field9 char(32), field10 varchar(32), field11 blob, field12 mediumblob, field13 longblob, field14 text, field15 mediumtext, field16 longtext, field17 date, field18 time, field19 datetime, field20 timestamp)");
1923:
1924: rsShow = dbmdUsingShow.getColumns(connShow.getCatalog(),
1925: null, "t_testBug23304", "%");
1926: rsInfoSchema = dbmdUsingInfoSchema.getColumns(
1927: connInfoSchema.getCatalog(), null,
1928: "t_testBug23304", "%");
1929:
1930: compareResultSets(rsShow, rsInfoSchema);
1931: } finally {
1932: if (rsShow != null) {
1933: rsShow.close();
1934: }
1935:
1936: if (rsInfoSchema != null) {
1937: rsInfoSchema.close();
1938: }
1939: }
1940: }
1941:
1942: private void compareResultSets(ResultSet expected, ResultSet actual)
1943: throws Exception {
1944: if (expected == null && actual != null) {
1945: fail("Expected null result set, actual was not null.");
1946: } else if (expected != null && actual == null) {
1947: fail("Expected non-null actual result set.");
1948: } else if (expected == null && actual == null) {
1949: return;
1950: }
1951:
1952: expected.last();
1953:
1954: int expectedRows = expected.getRow();
1955:
1956: actual.last();
1957:
1958: int actualRows = actual.getRow();
1959:
1960: assertEquals(expectedRows, actualRows);
1961:
1962: ResultSetMetaData metadataExpected = expected.getMetaData();
1963: ResultSetMetaData metadataActual = actual.getMetaData();
1964:
1965: assertEquals(metadataExpected.getColumnCount(), metadataActual
1966: .getColumnCount());
1967:
1968: for (int i = 0; i < metadataExpected.getColumnCount(); i++) {
1969: assertEquals(metadataExpected.getColumnName(i + 1),
1970: metadataActual.getColumnName(i + 1));
1971: assertEquals(metadataExpected.getColumnType(i + 1),
1972: metadataActual.getColumnType(i + 1));
1973: assertEquals(metadataExpected.getColumnClassName(i + 1),
1974: metadataActual.getColumnClassName(i + 1));
1975: }
1976:
1977: expected.beforeFirst();
1978: actual.beforeFirst();
1979:
1980: StringBuffer messageBuf = null;
1981:
1982: while (expected.next() && actual.next()) {
1983:
1984: if (messageBuf != null) {
1985: messageBuf.append("\n");
1986: }
1987:
1988: for (int i = 0; i < metadataExpected.getColumnCount(); i++) {
1989: if (expected.getObject(i + 1) == null
1990: && actual.getObject(i + 1) == null) {
1991: continue;
1992: }
1993:
1994: if ((expected.getObject(i + 1) == null && actual
1995: .getObject(i + 1) != null)
1996: || (expected.getObject(i + 1) != null && actual
1997: .getObject(i + 1) == null)
1998: || (!expected.getObject(i + 1).equals(
1999: actual.getObject(i + 1)))) {
2000: if ("COLUMN_DEF".equals(metadataExpected
2001: .getColumnName(i + 1))
2002: && (expected.getObject(i + 1) == null && actual
2003: .getString(i + 1).length() == 0)
2004: || (expected.getString(i + 1).length() == 0 && actual
2005: .getObject(i + 1) == null)) {
2006: continue; // known bug with SHOW FULL COLUMNS, and we can't distinguish between null and ''
2007: // for a default
2008: }
2009:
2010: if (messageBuf == null) {
2011: messageBuf = new StringBuffer();
2012: } else {
2013: messageBuf.append("\n");
2014: }
2015:
2016: messageBuf.append("On row " + expected.getRow()
2017: + " ,for column named "
2018: + metadataExpected.getColumnName(i + 1)
2019: + ", expected '"
2020: + expected.getObject(i + 1) + "', found '"
2021: + actual.getObject(i + 1) + "'");
2022:
2023: }
2024: }
2025: }
2026:
2027: if (messageBuf != null) {
2028: fail(messageBuf.toString());
2029: }
2030: }
2031:
2032: /**
2033: * Tests fix for BUG#25624 - Whitespace surrounding storage/size specifiers in stored procedure
2034: * declaration causes NumberFormatException to be thrown when calling stored procedure.
2035: *
2036: * @throws Exception
2037: */
2038: public void testBug25624() throws Exception {
2039: if (!versionMeetsMinimum(5, 0)) {
2040: return;
2041: }
2042:
2043: //
2044: // we changed up the parameters to get coverage of the fixes,
2045: // also note that whitespace _is_ significant in the DDL...
2046: //
2047:
2048: createProcedure("testBug25624",
2049: "(in _par1 decimal( 10 , 2 ) , in _par2 varchar( 4 )) BEGIN select 1; END");
2050:
2051: this .conn.prepareCall("{call testBug25624(?,?)}").close();
2052: }
2053:
2054: /**
2055: * Tests fix for BUG#27867 - Schema objects with identifiers other than
2056: * the connection character aren't retrieved correctly in ResultSetMetadata.
2057: *
2058: * @throws Exception if the test fails.
2059: */
2060: public void testBug27867() throws Exception {
2061: try {
2062: String gbkColumnName = "\u00e4\u00b8\u00ad\u00e6\u2013\u2021\u00e6\u00b5\u2039\u00e8\u00af\u2022";
2063: createTable("ColumnNameEncoding", "(" + "`" + gbkColumnName
2064: + "` varchar(1) default NULL,"
2065: + "`ASCIIColumn` varchar(1) default NULL"
2066: + ")ENGINE=MyISAM DEFAULT CHARSET=utf8");
2067:
2068: this .rs = this .stmt
2069: .executeQuery("SELECT * FROM ColumnNameEncoding");
2070: java.sql.ResultSetMetaData tblMD = this .rs.getMetaData();
2071:
2072: assertEquals(gbkColumnName, tblMD.getColumnName(1));
2073: assertEquals("ASCIIColumn", tblMD.getColumnName(2));
2074: } finally {
2075: closeMemberJDBCResources();
2076: }
2077: }
2078:
2079: /**
2080: * Fixed BUG#27915 - DatabaseMetaData.getColumns() doesn't
2081: * contain SCOPE_* or IS_AUTOINCREMENT columns.
2082: *
2083: * @throws Exception
2084: */
2085: public void testBug27915() throws Exception {
2086: createTable("testBug27915",
2087: "(field1 int not null primary key auto_increment, field2 int)");
2088: DatabaseMetaData dbmd = this .conn.getMetaData();
2089:
2090: try {
2091: this .rs = dbmd.getColumns(this .conn.getCatalog(), null,
2092: "testBug27915", "%");
2093: this .rs.next();
2094:
2095: checkBug27915();
2096:
2097: if (versionMeetsMinimum(5, 0)) {
2098: this .rs = getConnectionWithProps(
2099: "useInformationSchema=true").getMetaData()
2100: .getColumns(this .conn.getCatalog(), null,
2101: "testBug27915", "%");
2102: this .rs.next();
2103:
2104: checkBug27915();
2105: }
2106: } finally {
2107: closeMemberJDBCResources();
2108: }
2109: }
2110:
2111: private void checkBug27915() throws SQLException {
2112: assertNull(this .rs.getString("SCOPE_CATALOG"));
2113: assertNull(this .rs.getString("SCOPE_SCHEMA"));
2114: assertNull(this .rs.getString("SCOPE_TABLE"));
2115: assertNull(this .rs.getString("SOURCE_DATA_TYPE"));
2116: assertEquals("YES", this .rs.getString("IS_AUTOINCREMENT"));
2117:
2118: this .rs.next();
2119:
2120: assertNull(this .rs.getString("SCOPE_CATALOG"));
2121: assertNull(this .rs.getString("SCOPE_SCHEMA"));
2122: assertNull(this .rs.getString("SCOPE_TABLE"));
2123: assertNull(this .rs.getString("SOURCE_DATA_TYPE"));
2124: assertEquals("NO", this .rs.getString("IS_AUTOINCREMENT"));
2125: }
2126: }
|