[#2760] org.jooq.Meta should treat MySQL databases as Schema, not as Catalog

This commit is contained in:
Lukas Eder 2013-09-20 14:00:02 +02:00
parent 232ec19e91
commit 028dc4503c

View File

@ -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<Catalog> getCatalogs() {
try {
List<Catalog> result = new ArrayList<Catalog>();
Result<Record> catalogs = create.fetch(
meta().getCatalogs(),
SQLDataType.VARCHAR // TABLE_CATALOG
);
List<Catalog> result = new ArrayList<Catalog>();
// [#2760] MySQL JDBC confuses "catalog" and "schema"
if (!asList(MYSQL, MARIADB).contains(configuration.dialect().family())) {
Result<Record> 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<Schema> getSchemas() {
try {
List<Schema> result = new ArrayList<Schema>();
Result<Record> schemas = create.fetch(
meta().getSchemas(),
SQLDataType.VARCHAR, // TABLE_SCHEM
SQLDataType.VARCHAR // TABLE_CATALOG
);
List<Schema> result = new ArrayList<Schema>();
if (!asList(MYSQL, MARIADB).contains(configuration.dialect().family())) {
Result<Record> 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<Record> 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<Table<?>> result = new ArrayList<Table<?>>();
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<Table<?>> result = new ArrayList<Table<?>>();
Result<Record> 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<Record> getColumns0(String schema, String table) throws SQLException {
private final Result<Record> 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<Record> 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<Record> result =
create.fetch(
meta().getPrimaryKeys(null, schema, getName()),
rs,
String.class, // TABLE_CAT
String.class, // TABLE_SCHEM
String.class, // TABLE_NAME