From 028dc4503ceab34121967dc919b822e711863530 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Fri, 20 Sep 2013 14:00:02 +0200 Subject: [PATCH] [#2760] org.jooq.Meta should treat MySQL databases as Schema, not as Catalog --- .../src/main/java/org/jooq/impl/MetaImpl.java | 108 +++++++++++++----- 1 file changed, 79 insertions(+), 29 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/impl/MetaImpl.java b/jOOQ/src/main/java/org/jooq/impl/MetaImpl.java index 219fa27b12..8f59af2369 100644 --- a/jOOQ/src/main/java/org/jooq/impl/MetaImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/MetaImpl.java @@ -42,6 +42,8 @@ package org.jooq.impl; import static java.util.Arrays.asList; import static java.util.Collections.unmodifiableList; +import static org.jooq.SQLDialect.MARIADB; +import static org.jooq.SQLDialect.MYSQL; import static org.jooq.SQLDialect.SQLITE; import static org.jooq.impl.DSL.fieldByName; import static org.jooq.impl.DSL.name; @@ -49,6 +51,7 @@ import static org.jooq.impl.DSL.name; import java.io.Serializable; import java.sql.Connection; import java.sql.DatabaseMetaData; +import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.LinkedHashMap; @@ -122,14 +125,18 @@ class MetaImpl implements Meta, Serializable { @Override public final List getCatalogs() { try { - List result = new ArrayList(); - Result catalogs = create.fetch( - meta().getCatalogs(), - SQLDataType.VARCHAR // TABLE_CATALOG - ); + List result = new ArrayList(); + + // [#2760] MySQL JDBC confuses "catalog" and "schema" + if (!asList(MYSQL, MARIADB).contains(configuration.dialect().family())) { + Result catalogs = create.fetch( + meta().getCatalogs(), + SQLDataType.VARCHAR // TABLE_CATALOG + ); - for (String name : catalogs.getValues(0, String.class)) { - result.add(new MetaCatalog(name)); + for (String name : catalogs.getValues(0, String.class)) { + result.add(new MetaCatalog(name)); + } } // There should always be at least one (empty) catalog in a database @@ -195,16 +202,33 @@ class MetaImpl implements Meta, Serializable { @Override public final List getSchemas() { try { - List result = new ArrayList(); - Result schemas = create.fetch( - meta().getSchemas(), - SQLDataType.VARCHAR, // TABLE_SCHEM - SQLDataType.VARCHAR // TABLE_CATALOG - ); + List result = new ArrayList(); + + if (!asList(MYSQL, MARIADB).contains(configuration.dialect().family())) { + Result schemas = create.fetch( + meta().getSchemas(), + + // [#2681] Work around a flaw in the MySQL JDBC driver + SQLDataType.VARCHAR, // TABLE_SCHEM + SQLDataType.VARCHAR // TABLE_CATALOG + ); - for (String name : schemas.getValues(0, String.class)) { - result.add(new MetaSchema(name)); - } + for (String name : schemas.getValues(0, String.class)) { + result.add(new MetaSchema(name)); + } + } + + // [#2760] MySQL JDBC confuses "catalog" and "schema" + else { + Result schemas = create.fetch( + meta().getCatalogs(), + SQLDataType.VARCHAR // TABLE_CATALOG + ); + + for (String name : schemas.getValues(0, String.class)) { + result.add(new MetaSchema(name)); + } + } // There should always be at least one (empty) schema in a database if (result.isEmpty()) { @@ -260,19 +284,25 @@ class MetaImpl implements Meta, Serializable { /* [/pro] */ } - List> result = new ArrayList>(); + ResultSet rs; + if (!asList(MYSQL, MARIADB).contains(configuration.dialect().family())) { + rs = meta().getTables(null, getName(), "%", types); + } + + // [#2760] MySQL JDBC confuses "catalog" and "schema" + else { + rs = meta().getTables(getName(), null, "%", types); + } + + List> result = new ArrayList>(); Result tables = create.fetch( - meta().getTables(null, getName(), "%", types), + rs, + + // [#2681] Work around a flaw in the MySQL JDBC driver SQLDataType.VARCHAR, // TABLE_CAT SQLDataType.VARCHAR, // TABLE_SCHEM SQLDataType.VARCHAR, // TABLE_NAME - SQLDataType.VARCHAR, // TABLE_TYPE - SQLDataType.VARCHAR, // REMARKS - SQLDataType.VARCHAR, // TYPE_CAT - SQLDataType.VARCHAR, // TYPE_SCHEM - SQLDataType.VARCHAR, // TYPE_NAME - SQLDataType.VARCHAR, // SELF_REFERENCING_COL_NAME - SQLDataType.VARCHAR // REF_GENERATION + SQLDataType.VARCHAR // TABLE_TYPE ); for (Record table : tables) { @@ -327,9 +357,19 @@ class MetaImpl implements Meta, Serializable { } } - private final Result getColumns0(String schema, String table) throws SQLException { + private final Result getColumns0(String schema, String table) throws SQLException { + ResultSet rs; + if (!asList(MYSQL, MARIADB).contains(configuration.dialect().family())) { + rs = meta().getColumns(null, schema, table, "%"); + } + + // [#2760] MySQL JDBC confuses "catalog" and "schema" + else { + rs = meta().getColumns(schema, null, table, "%"); + } + return create.fetch( - meta().getColumns(null, schema, table, "%"), + rs, // Work around a bug in the SQL Server JDBC driver by // coercing data types to the expected types @@ -376,10 +416,20 @@ class MetaImpl implements Meta, Serializable { public final UniqueKey getPrimaryKey() { String schema = getSchema() == null ? null : getSchema().getName(); - try { + try { + ResultSet rs; + if (!asList(MYSQL, MARIADB).contains(configuration.dialect().family())) { + rs = meta().getPrimaryKeys(null, schema, getName()); + } + + // [#2760] MySQL JDBC confuses "catalog" and "schema" + else { + rs = meta().getPrimaryKeys(schema, null, getName()); + } + Result result = create.fetch( - meta().getPrimaryKeys(null, schema, getName()), + rs, String.class, // TABLE_CAT String.class, // TABLE_SCHEM String.class, // TABLE_NAME