This commit is contained in:
Lukas Eder 2021-05-10 22:59:59 +02:00
parent 2d3c6a8782
commit 059f3fb4a3
6 changed files with 89 additions and 114 deletions

View File

@ -28,15 +28,6 @@
</licenses>
<!-- Workaround for https://issues.apache.org/jira/browse/MPLUGIN-369 -->
<repositories>
<repository>
<id>apache-snapshot-repository</id>
<name>Apache Snapshot Repository</name>
<url>https://repository.apache.org/snapshots/</url>
</repository>
</repositories>
<build>
<plugins>
@ -74,7 +65,7 @@
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-tools-annotations</artifactId>
<version>3.6.1-SNAPSHOT</version>
<version>3.6.1</version>
</dependency>
</dependencies>
</plugin>

View File

@ -38,6 +38,7 @@
package org.jooq.meta.cubrid;
import static org.jooq.Records.toList;
import static org.jooq.impl.DSL.concat;
import static org.jooq.impl.DSL.field;
import static org.jooq.impl.DSL.name;
@ -58,6 +59,7 @@ import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Record3;
import org.jooq.Records;
import org.jooq.Result;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;
@ -135,7 +137,7 @@ public class CUBRIDDatabase extends AbstractDatabase {
.selectDistinct(DB_INDEX.CLASS_NAME)
.from(DB_INDEX)
.where(DB_INDEX.IS_FOREIGN_KEY.isTrue())
.fetch(DB_INDEX.CLASS_NAME)) {
.collect(toList())) {
for (Record record : create().fetch(meta.getImportedKeys(null, null, table))) {
String foreignKeyName =

View File

@ -37,8 +37,7 @@
*/
package org.jooq.meta.h2;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;
import static org.jooq.Records.mapping;
import static org.jooq.impl.DSL.condition;
import static org.jooq.impl.DSL.falseCondition;
import static org.jooq.impl.DSL.field;
@ -73,7 +72,6 @@ import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import org.jooq.DSLContext;
import org.jooq.Field;
@ -398,17 +396,9 @@ public class H2Database extends AbstractDatabase implements ResultQueryDatabase
@Override
protected List<SchemaDefinition> getSchemata0() throws SQLException {
return
create().select(
SCHEMATA.SCHEMA_NAME,
SCHEMATA.REMARKS)
create().select(SCHEMATA.SCHEMA_NAME, SCHEMATA.REMARKS)
.from(SCHEMATA)
.collect(mapping(
r -> new SchemaDefinition(this,
r.get(SCHEMATA.SCHEMA_NAME),
r.get(SCHEMATA.REMARKS)
),
toList()
));
.fetch(mapping((s, r) -> new SchemaDefinition(this, s, r)));
}
@Override
@ -473,12 +463,14 @@ public class H2Database extends AbstractDatabase implements ResultQueryDatabase
protected List<TableDefinition> getTables0() throws SQLException {
List<TableDefinition> result = new ArrayList<>();
for (Record record : create().select(
final /* record */ class R { private final String schema; private final String table; private final TableType type; private final String comment; private final String source; public R(String schema, String table, TableType type, String comment, String source) { this.schema = schema; this.table = table; this.type = type; this.comment = comment; this.source = source; } public String schema() { return schema; } public String table() { return table; } public TableType type() { return type; } public String comment() { return comment; } public String source() { return source; } @Override public boolean equals(Object o) { if (!(o instanceof R)) return false; R other = (R) o; if (!java.util.Objects.equals(this.schema, other.schema)) return false; if (!java.util.Objects.equals(this.table, other.table)) return false; if (!java.util.Objects.equals(this.type, other.type)) return false; if (!java.util.Objects.equals(this.comment, other.comment)) return false; if (!java.util.Objects.equals(this.source, other.source)) return false; return true; } @Override public int hashCode() { return java.util.Objects.hash(this.schema, this.table, this.type, this.comment, this.source); } @Override public String toString() { return new StringBuilder("R[").append("schema=").append(this.schema).append(", table=").append(this.table).append(", type=").append(this.type).append(", comment=").append(this.comment).append(", source=").append(this.source).append("]").toString(); } }
for (R r : create().select(
TABLES.TABLE_SCHEMA,
TABLES.TABLE_NAME,
when(TABLES.TABLE_TYPE.eq(inline("VIEW")), inline(TableType.VIEW.name()))
.when(TABLES.STORAGE_TYPE.like(inline("%TEMPORARY%")), inline(TableType.TEMPORARY.name()))
.else_(inline(TableType.TABLE.name())).as("table_type"),
.else_(inline(TableType.TABLE.name())).convertFrom(TableType::valueOf).as("table_type"),
TABLES.REMARKS,
VIEWS.VIEW_DEFINITION)
.from(TABLES)
@ -488,19 +480,13 @@ public class H2Database extends AbstractDatabase implements ResultQueryDatabase
.where(TABLES.TABLE_SCHEMA.in(getInputSchemata()))
.orderBy(
TABLES.TABLE_SCHEMA,
TABLES.TABLE_NAME)) {
TABLES.TABLE_NAME)
.fetch(mapping(R::new))) {
SchemaDefinition schema = getSchema(record.get(TABLES.TABLE_SCHEMA));
SchemaDefinition schema = getSchema(r.schema);
if (schema != null) {
String name = record.get(TABLES.TABLE_NAME);
String comment = record.get(TABLES.REMARKS);
TableType tableType = record.get("table_type", TableType.class);
String source = record.get(VIEWS.VIEW_DEFINITION);
H2TableDefinition table = new H2TableDefinition(schema, name, comment, tableType, source);
result.add(table);
}
if (schema != null)
result.add(new H2TableDefinition(schema, r.table, r.comment, r.type, r.source));
}
return result;
@ -580,8 +566,10 @@ public class H2Database extends AbstractDatabase implements ResultQueryDatabase
private void getInlineEnums(List<EnumDefinition> result) {
final /* record */ class R { private final String schema; private final String table; private final String column; private final String type; public R(String schema, String table, String column, String type) { this.schema = schema; this.table = table; this.column = column; this.type = type; } public String schema() { return schema; } public String table() { return table; } public String column() { return column; } public String type() { return type; } @Override public boolean equals(Object o) { if (!(o instanceof R)) return false; R other = (R) o; if (!java.util.Objects.equals(this.schema, other.schema)) return false; if (!java.util.Objects.equals(this.table, other.table)) return false; if (!java.util.Objects.equals(this.column, other.column)) return false; if (!java.util.Objects.equals(this.type, other.type)) return false; return true; } @Override public int hashCode() { return java.util.Objects.hash(this.schema, this.table, this.column, this.type); } @Override public String toString() { return new StringBuilder("R[").append("schema=").append(this.schema).append(", table=").append(this.table).append(", column=").append(this.column).append(", type=").append(this.type).append("]").toString(); } }
enumLoop:
for (Record record : create()
for (R r : create()
.select(
COLUMNS.TABLE_SCHEMA,
COLUMNS.TABLE_NAME,
@ -594,22 +582,20 @@ public class H2Database extends AbstractDatabase implements ResultQueryDatabase
.orderBy(
COLUMNS.TABLE_SCHEMA.asc(),
COLUMNS.TABLE_NAME.asc(),
COLUMNS.COLUMN_NAME.asc())) {
COLUMNS.COLUMN_NAME.asc())
.fetch(mapping(R::new))) {
SchemaDefinition schema = getSchema(record.get(COLUMNS.TABLE_SCHEMA));
SchemaDefinition schema = getSchema(r.schema);
if (schema == null)
continue enumLoop;
String table = record.get(COLUMNS.TABLE_NAME);
String column = record.get(COLUMNS.COLUMN_NAME);
String name = table + "_" + column;
String columnType = record.get(COLUMNS.COLUMN_TYPE);
String name = r.table + "_" + r.column;
// [#1237] Don't generate enum classes for columns in MySQL tables
// that are excluded from code generation
TableDefinition tableDefinition = getTable(schema, table);
TableDefinition tableDefinition = getTable(schema, r.table);
if (tableDefinition != null) {
ColumnDefinition columnDefinition = tableDefinition.getColumn(column);
ColumnDefinition columnDefinition = tableDefinition.getColumn(r.column);
if (columnDefinition != null) {
@ -619,7 +605,7 @@ public class H2Database extends AbstractDatabase implements ResultQueryDatabase
DefaultEnumDefinition definition = new DefaultEnumDefinition(schema, name, "");
CSVReader reader = new CSVReader(
new StringReader(columnType.replaceAll("(^enum\\()|(\\).*$)", ""))
new StringReader(r.type.replaceAll("(^enum\\()|(\\).*$)", ""))
,',' // Separator
,'\'' // Quote character
,true // Strict quotes
@ -637,8 +623,10 @@ public class H2Database extends AbstractDatabase implements ResultQueryDatabase
private void getDomainEnums(List<EnumDefinition> result) {
final /* record */ class R { private final String schema; private final String name; private final String sql; public R(String schema, String name, String sql) { this.schema = schema; this.name = name; this.sql = sql; } public String schema() { return schema; } public String name() { return name; } public String sql() { return sql; } @Override public boolean equals(Object o) { if (!(o instanceof R)) return false; R other = (R) o; if (!java.util.Objects.equals(this.schema, other.schema)) return false; if (!java.util.Objects.equals(this.name, other.name)) return false; if (!java.util.Objects.equals(this.sql, other.sql)) return false; return true; } @Override public int hashCode() { return java.util.Objects.hash(this.schema, this.name, this.sql); } @Override public String toString() { return new StringBuilder("R[").append("schema=").append(this.schema).append(", name=").append(this.name).append(", sql=").append(this.sql).append("]").toString(); } }
enumLoop:
for (Record record : create()
for (R r : create()
.select(
DOMAINS.DOMAIN_SCHEMA,
DOMAINS.DOMAIN_NAME,
@ -648,19 +636,17 @@ public class H2Database extends AbstractDatabase implements ResultQueryDatabase
.and(DOMAINS.DOMAIN_SCHEMA.in(getInputSchemata()))
.orderBy(
DOMAINS.DOMAIN_SCHEMA,
DOMAINS.DOMAIN_NAME)) {
DOMAINS.DOMAIN_NAME)
.fetch(mapping(R::new))) {
SchemaDefinition schema = getSchema(record.get(DOMAINS.DOMAIN_SCHEMA));
SchemaDefinition schema = getSchema(r.schema);
if (schema == null)
continue enumLoop;
String name = record.get(DOMAINS.DOMAIN_NAME);
String sql = record.get(DOMAINS.SQL);
DefaultEnumDefinition definition = new DefaultEnumDefinition(schema, name, "");
DefaultEnumDefinition definition = new DefaultEnumDefinition(schema, r.name, "");
CSVReader reader = new CSVReader(
new StringReader(sql.replaceAll("(?i:(^.*as enum\\()|(\\).*$))", ""))
new StringReader(r.sql.replaceAll("(?i:(^.*as enum\\()|(\\).*$))", ""))
,',' // Separator
,'\'' // Quote character
,true // Strict quotes

View File

@ -40,6 +40,7 @@ package org.jooq.meta.hsqldb;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;
import static org.jooq.Records.mapping;
import static org.jooq.impl.DSL.decode;
import static org.jooq.impl.DSL.falseCondition;
import static org.jooq.impl.DSL.field;
@ -78,6 +79,7 @@ import org.jooq.Field;
import org.jooq.Record;
import org.jooq.Record12;
import org.jooq.Record6;
import org.jooq.Records;
import org.jooq.Result;
import org.jooq.ResultQuery;
import org.jooq.SQLDialect;
@ -385,10 +387,7 @@ public class HSQLDBDatabase extends AbstractDatabase implements ResultQueryDatab
return
create().select(SCHEMATA.SCHEMA_NAME)
.from(SCHEMATA)
.collect(mapping(
r -> new SchemaDefinition(this, r.value1(), ""),
toList()
));
.fetch(mapping(s -> new SchemaDefinition(this, s, "")));
}
@Override

View File

@ -41,9 +41,9 @@ package org.jooq.meta.mysql;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;
import static org.jooq.Records.mapping;
import static org.jooq.SQLDialect.MYSQL;
// ...
import static org.jooq.SQLDialect.POSTGRES;
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.DSL.noCondition;
import static org.jooq.impl.DSL.row;
@ -75,6 +75,7 @@ import org.jooq.Record;
import org.jooq.Record12;
import org.jooq.Record5;
import org.jooq.Record6;
import org.jooq.Records;
import org.jooq.Result;
import org.jooq.ResultQuery;
import org.jooq.SQLDialect;
@ -458,55 +459,51 @@ public class MySQLDatabase extends AbstractDatabase implements ResultQueryDataba
protected List<EnumDefinition> getEnums0() throws SQLException {
List<EnumDefinition> result = new ArrayList<>();
Result<Record5<String, String, String, String, String>> records = create()
.select(
COLUMNS.TABLE_SCHEMA,
COLUMNS.COLUMN_COMMENT,
COLUMNS.TABLE_NAME,
COLUMNS.COLUMN_NAME,
COLUMNS.COLUMN_TYPE)
.from(COLUMNS)
.where(
COLUMNS.COLUMN_TYPE.like("enum(%)").and(
COLUMNS.TABLE_SCHEMA.in(workaroundFor5213(getInputSchemata()))))
.orderBy(
COLUMNS.TABLE_SCHEMA.asc(),
COLUMNS.TABLE_NAME.asc(),
COLUMNS.COLUMN_NAME.asc())
.fetch();
final /* record */ class R { private final String schema; private final String table; private final String column; private final String type; private final String comment; public R(String schema, String table, String column, String type, String comment) { this.schema = schema; this.table = table; this.column = column; this.type = type; this.comment = comment; } public String schema() { return schema; } public String table() { return table; } public String column() { return column; } public String type() { return type; } public String comment() { return comment; } @Override public boolean equals(Object o) { if (!(o instanceof R)) return false; R other = (R) o; if (!java.util.Objects.equals(this.schema, other.schema)) return false; if (!java.util.Objects.equals(this.table, other.table)) return false; if (!java.util.Objects.equals(this.column, other.column)) return false; if (!java.util.Objects.equals(this.type, other.type)) return false; if (!java.util.Objects.equals(this.comment, other.comment)) return false; return true; } @Override public int hashCode() { return java.util.Objects.hash(this.schema, this.table, this.column, this.type, this.comment); } @Override public String toString() { return new StringBuilder("R[").append("schema=").append(this.schema).append(", table=").append(this.table).append(", column=").append(this.column).append(", type=").append(this.type).append(", comment=").append(this.comment).append("]").toString(); } }
for (Record record : records) {
SchemaDefinition schema = getSchema(record.get(COLUMNS.TABLE_SCHEMA));
for (R r : create()
.select(
COLUMNS.TABLE_SCHEMA,
COLUMNS.TABLE_NAME,
COLUMNS.COLUMN_NAME,
COLUMNS.COLUMN_TYPE,
COLUMNS.COLUMN_COMMENT)
.from(COLUMNS)
.where(
COLUMNS.COLUMN_TYPE.like("enum(%)").and(
COLUMNS.TABLE_SCHEMA.in(workaroundFor5213(getInputSchemata()))))
.orderBy(
COLUMNS.TABLE_SCHEMA.asc(),
COLUMNS.TABLE_NAME.asc(),
COLUMNS.COLUMN_NAME.asc())
.fetch(mapping(R::new))
) {
SchemaDefinition schema = getSchema(r.schema);
String comment = record.get(COLUMNS.COLUMN_COMMENT);
String table = record.get(COLUMNS.TABLE_NAME);
String column = record.get(COLUMNS.COLUMN_NAME);
String name = table + "_" + column;
String columnType = record.get(COLUMNS.COLUMN_TYPE);
String name = r.table + "_" + r.column;
// [#1237] Don't generate enum classes for columns in MySQL tables
// that are excluded from code generation
TableDefinition tableDefinition = getTable(schema, table);
TableDefinition tableDefinition = getTable(schema, r.table);
if (tableDefinition != null) {
ColumnDefinition columnDefinition = tableDefinition.getColumn(column);
ColumnDefinition columnDefinition = tableDefinition.getColumn(r.column);
if (columnDefinition != null) {
// [#1137] Avoid generating enum classes for enum types that
// are explicitly forced to another type
if (getConfiguredForcedType(columnDefinition, columnDefinition.getType()) == null) {
DefaultEnumDefinition definition = new DefaultEnumDefinition(schema, name, comment);
DefaultEnumDefinition definition = new DefaultEnumDefinition(schema, name, r.comment);
CSVReader reader = new CSVReader(
new StringReader(columnType.replaceAll("(^enum\\()|(\\)$)", ""))
new StringReader(r.type.replaceAll("(^enum\\()|(\\)$)", ""))
,',' // Separator
,'\'' // Quote character
,true // Strict quotes
);
for (String string : reader.next()) {
for (String string : reader.next())
definition.addLiteral(string);
}
result.add(definition);
}

View File

@ -41,6 +41,7 @@ package org.jooq.meta.postgres;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;
import static org.jooq.Records.mapping;
import static org.jooq.Rows.toRowArray;
import static org.jooq.SQLDialect.POSTGRES;
// ...
@ -122,6 +123,7 @@ import org.jooq.Record12;
import org.jooq.Record2;
import org.jooq.Record5;
import org.jooq.Record6;
import org.jooq.Records;
import org.jooq.Result;
import org.jooq.ResultQuery;
import org.jooq.Rows;
@ -740,6 +742,8 @@ public class PostgresDatabase extends AbstractDatabase implements ResultQueryDat
return result;
}
static final /* record */ class Identifier { private final String schema; private final String name; public Identifier(String schema, String name) { this.schema = schema; this.name = name; } public String schema() { return schema; } public String name() { return name; } @Override public boolean equals(Object o) { if (!(o instanceof Identifier)) return false; Identifier other = (Identifier) o; if (!java.util.Objects.equals(this.schema, other.schema)) return false; if (!java.util.Objects.equals(this.name, other.name)) return false; return true; } @Override public int hashCode() { return java.util.Objects.hash(this.schema, this.name); } @Override public String toString() { return new StringBuilder("Identifier[").append("schema=").append(this.schema).append(", name=").append(this.name).append("]").toString(); } }
@Override
protected List<EnumDefinition> getEnums0() throws SQLException {
List<EnumDefinition> result = new ArrayList<>();
@ -750,26 +754,22 @@ public class PostgresDatabase extends AbstractDatabase implements ResultQueryDat
// [#2707] Fetch all enum type names first, in order to be able to
// perform enumlabel::[typname] casts in the subsequent query for
// cross-version compatible enum literal ordering
Result<Record2<String, String>> types = create()
.select(
PG_TYPE.pgNamespace().NSPNAME,
PG_TYPE.TYPNAME)
.from(PG_TYPE)
.where(PG_TYPE.pgNamespace().NSPNAME.in(getInputSchemata()))
.and(oid(PG_TYPE).in(select(PG_ENUM.ENUMTYPID).from(PG_ENUM)))
.orderBy(
PG_TYPE.pgNamespace().NSPNAME,
PG_TYPE.TYPNAME)
.fetch();
for (Record2<String, String> type : types) {
String nspname = type.get(PG_TYPE.pgNamespace().NSPNAME);
String typname = type.get(PG_TYPE.TYPNAME);
for (Identifier type : create()
.select(
PG_TYPE.pgNamespace().NSPNAME,
PG_TYPE.TYPNAME)
.from(PG_TYPE)
.where(PG_TYPE.pgNamespace().NSPNAME.in(getInputSchemata()))
.and(oid(PG_TYPE).in(select(PG_ENUM.ENUMTYPID).from(PG_ENUM)))
.orderBy(
PG_TYPE.pgNamespace().NSPNAME,
PG_TYPE.TYPNAME)
.fetch(mapping(Identifier::new))) {
DefaultEnumDefinition definition = null;
for (String label : enumLabels(nspname, typname)) {
SchemaDefinition schema = getSchema(nspname);
String typeName = String.valueOf(typname);
for (String label : enumLabels(type.schema, type.name)) {
SchemaDefinition schema = getSchema(type.schema);
String typeName = String.valueOf(type.name);
if (definition == null || !definition.getName().equals(typeName)) {
definition = new DefaultEnumDefinition(schema, typeName, null);
@ -894,7 +894,7 @@ public class PostgresDatabase extends AbstractDatabase implements ResultQueryDat
// [#2736] This table is unavailable in Amazon Redshift
if (exists(ATTRIBUTES)) {
for (Record record : create()
for (Identifier udt : create()
.selectDistinct(
ATTRIBUTES.UDT_SCHEMA,
ATTRIBUTES.UDT_NAME)
@ -903,12 +903,12 @@ public class PostgresDatabase extends AbstractDatabase implements ResultQueryDat
.orderBy(
ATTRIBUTES.UDT_SCHEMA,
ATTRIBUTES.UDT_NAME)
.fetch()) {
.fetch(mapping(Identifier::new))) {
SchemaDefinition schema = getSchema(record.get(ATTRIBUTES.UDT_SCHEMA));
String name = record.get(ATTRIBUTES.UDT_NAME);
SchemaDefinition schema = getSchema(udt.schema);
result.add(new PostgresUDTDefinition(schema, name, null));
if (schema != null)
result.add(new PostgresUDTDefinition(schema, udt.name, null));
}
}
@ -1181,6 +1181,6 @@ public class PostgresDatabase extends AbstractDatabase implements ResultQueryDat
.where(PG_ENUM.pgType().pgNamespace().NSPNAME.eq(nspname))
.and(PG_ENUM.pgType().TYPNAME.eq(typname))
.orderBy(orderBy)
.fetch(PG_ENUM.ENUMLABEL);
.collect(Records.toList());
}
}