[#330] Fixed PostgreSQL regressions

This commit is contained in:
lukaseder 2016-11-01 16:18:58 +01:00
parent 32e4493130
commit 4659e83f09
8 changed files with 132 additions and 96 deletions

View File

@ -42,12 +42,14 @@ package org.jooq.util;
import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableSet;
import static org.jooq.impl.DSL.name;
import static org.jooq.util.AbstractGenerator.Language.JAVA;
import static org.jooq.util.AbstractGenerator.Language.SCALA;
import java.util.HashSet;
import java.util.Set;
import org.jooq.Name;
import org.jooq.SQLDialect;
import org.jooq.exception.SQLDialectNotSupportedException;
import org.jooq.util.AbstractGenerator.Language;
@ -355,7 +357,7 @@ class GenerationUtil {
/**
* Gets the base type for an array type, depending on the RDBMS dialect
*/
static String getArrayBaseType(SQLDialect dialect, String t, String u) {
static Name getArrayBaseType(SQLDialect dialect, String t, Name u) {
// [#4388] TODO: Improve array handling
switch (dialect.family()) {
@ -365,8 +367,10 @@ class GenerationUtil {
case POSTGRES: {
// The convention is to prepend a "_" to a type to get an array type
if (u != null && u.startsWith("_")) {
return u.substring(1);
if (u != null && u.last().startsWith("_")) {
String[] name = u.getName();
name[name.length - 1] = name[name.length - 1].substring(1);
return name(name);
}
// But there are also arrays with a "vector" suffix
@ -376,7 +380,7 @@ class GenerationUtil {
}
case H2: {
return H2DataType.OTHER.getTypeName();
return name(H2DataType.OTHER.getTypeName());
}
@ -387,12 +391,12 @@ class GenerationUtil {
// In HSQLDB 2.2.5, there has been an incompatible INFORMATION_SCHEMA change around the
// ELEMENT_TYPES view. Arrays are now described much more explicitly
if ("ARRAY".equalsIgnoreCase(t)) {
return "OTHER";
return name("OTHER");
}
// This is for backwards compatibility
else {
return t.replace(" ARRAY", "");
return name(t.replace(" ARRAY", ""));
}
}
}

View File

@ -4930,11 +4930,11 @@ public class JavaGenerator extends AbstractGenerator {
// [#4388] TODO: Improve array handling
if (database.isArrayType(type.getType())) {
String baseType = GenerationUtil.getArrayBaseType(db.getDialect(), type.getType(), type.getUserType());
Name baseType = GenerationUtil.getArrayBaseType(db.getDialect(), type.getType(), type.getQualifiedUserType());
return getTypeReference(
db,
type.getSchema(),
baseType,
baseType.last(),
0,
0,
0,
@ -5007,12 +5007,12 @@ public class JavaGenerator extends AbstractGenerator {
else if (db.isArrayType(t)) {
// [#4388] TODO: Improve array handling
String baseType = GenerationUtil.getArrayBaseType(db.getDialect(), t, u == null ? null : u.last());
Name baseType = GenerationUtil.getArrayBaseType(db.getDialect(), t, u);
if (scala)
type = "scala.Array[" + getType(db, schema, baseType, p, s, baseType, javaType, defaultType, udtMode) + "]";
type = "scala.Array[" + getType(db, schema, baseType.last(), p, s, baseType, javaType, defaultType, udtMode) + "]";
else
type = getType(db, schema, baseType, p, s, baseType, javaType, defaultType, udtMode) + "[]";
type = getType(db, schema, baseType.last(), p, s, baseType, javaType, defaultType, udtMode) + "[]";
}
// Check for Oracle-style VARRAY types

View File

@ -597,7 +597,10 @@ public class PostgresDatabase extends AbstractDatabase {
0, // ?
!record.get(d.TYPNOTNULL, boolean.class),
record.get(d.TYPDEFAULT),
record.get(b.TYPNAME, String.class)
name(
record.get(n.NSPNAME),
record.get(b.TYPNAME)
)
);
DefaultDomainDefinition domain = new DefaultDomainDefinition(

View File

@ -44,6 +44,7 @@ package org.jooq.util.postgres;
import static org.jooq.impl.DSL.condition;
import static org.jooq.impl.DSL.field;
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.DSL.name;
import static org.jooq.impl.DSL.not;
import static org.jooq.impl.DSL.nvl;
import static org.jooq.impl.DSL.when;
@ -82,17 +83,17 @@ import org.jooq.util.postgres.pg_catalog.tables.PgType;
*/
public class PostgresMaterializedViewDefinition extends AbstractTableDefinition {
public PostgresMaterializedViewDefinition(SchemaDefinition schema, String name, String comment) {
super(schema, name, comment);
}
public PostgresMaterializedViewDefinition(SchemaDefinition schema, String name, String comment) {
super(schema, name, comment);
}
@Override
public List<ColumnDefinition> getElements0() throws SQLException {
List<ColumnDefinition> result = new ArrayList<ColumnDefinition>();
@Override
public List<ColumnDefinition> getElements0() throws SQLException {
List<ColumnDefinition> result = new ArrayList<ColumnDefinition>();
Columns col = COLUMNS;
PgAttribute a = PG_ATTRIBUTE.as("a");
PgAttrdef ad = PG_ATTRDEF.as("ad");
Columns col = COLUMNS;
PgAttribute a = PG_ATTRIBUTE.as("a");
PgAttrdef ad = PG_ATTRDEF.as("ad");
PgType t = PG_TYPE.as("t");
PgType bt = PG_TYPE.as("bt");
PgClass c = PG_CLASS.as("c");
@ -103,29 +104,29 @@ public class PostgresMaterializedViewDefinition extends AbstractTableDefinition
PgNamespace nco = PG_NAMESPACE.as("nco");
for (Record record : create().select(
field("({0})::information_schema.sql_identifier", col.COLUMN_NAME.getDataType(), a.ATTNAME).as(col.COLUMN_NAME),
field("({0})::information_schema.cardinal_number", col.ORDINAL_POSITION.getDataType(), a.ATTNUM).as(col.ORDINAL_POSITION),
field("({0})::information_schema.character_data", col.DATA_TYPE.getDataType(),
when(t.TYPTYPE.eq(inline("d")),
when(bt.TYPELEM.ne(inline(0L)).and(bt.TYPLEN.eq(inline((short) -1))), inline("ARRAY"))
.when(nbt.NSPNAME.eq(inline("pg_catalog")), field("format_type({0}, NULL::integer)", String.class, t.TYPBASETYPE))
.otherwise(inline("USER-DEFINED")))
.otherwise(
when(t.TYPELEM.ne(inline(0L)).and(t.TYPLEN.eq(inline((short) -1))), inline("ARRAY"))
.when(nt.NSPNAME.eq(inline("pg_catalog")), field("format_type({0}, NULL::integer)", String.class, a.ATTTYPID))
.otherwise(inline("USER-DEFINED")))).as(col.DATA_TYPE),
field("({0})::information_schema.sql_identifier", col.COLUMN_NAME.getDataType(), a.ATTNAME).as(col.COLUMN_NAME),
field("({0})::information_schema.cardinal_number", col.ORDINAL_POSITION.getDataType(), a.ATTNUM).as(col.ORDINAL_POSITION),
field("({0})::information_schema.character_data", col.DATA_TYPE.getDataType(),
when(t.TYPTYPE.eq(inline("d")),
when(bt.TYPELEM.ne(inline(0L)).and(bt.TYPLEN.eq(inline((short) -1))), inline("ARRAY"))
.when(nbt.NSPNAME.eq(inline("pg_catalog")), field("format_type({0}, NULL::integer)", String.class, t.TYPBASETYPE))
.otherwise(inline("USER-DEFINED")))
.otherwise(
when(t.TYPELEM.ne(inline(0L)).and(t.TYPLEN.eq(inline((short) -1))), inline("ARRAY"))
.when(nt.NSPNAME.eq(inline("pg_catalog")), field("format_type({0}, NULL::integer)", String.class, a.ATTTYPID))
.otherwise(inline("USER-DEFINED")))).as(col.DATA_TYPE),
field("(information_schema._pg_char_max_length(information_schema._pg_truetypid(a.*, t.*), information_schema._pg_truetypmod(a.*, t.*)))::information_schema.cardinal_number", col.CHARACTER_MAXIMUM_LENGTH.getDataType()).as(col.CHARACTER_MAXIMUM_LENGTH),
field("(information_schema._pg_numeric_precision(information_schema._pg_truetypid(a.*, t.*), information_schema._pg_truetypmod(a.*, t.*)))::information_schema.cardinal_number", col.NUMERIC_PRECISION.getDataType()).as(col.NUMERIC_PRECISION),
field("(information_schema._pg_numeric_scale(information_schema._pg_truetypid(a.*, t.*), information_schema._pg_truetypmod(a.*, t.*)))::information_schema.cardinal_number", col.NUMERIC_SCALE.getDataType()).as(col.NUMERIC_SCALE),
field("({0})::information_schema.yes_or_no", col.IS_NULLABLE.getDataType(),
when(condition(a.ATTNOTNULL).or(t.TYPTYPE.eq(inline("d")).and(t.TYPNOTNULL)), inline("NO"))
.otherwise(inline("YES"))).as(col.IS_NULLABLE),
field("(pg_get_expr({0}, {1}))::information_schema.character_data", col.COLUMN_DEFAULT.getDataType(), ad.ADBIN, ad.ADRELID).as(col.COLUMN_DEFAULT),
field("({0})::information_schema.sql_identifier", col.UDT_SCHEMA.getDataType(),
nvl(nbt.NSPNAME, nt.NSPNAME)).as(col.UDT_SCHEMA),
field("({0})::information_schema.sql_identifier", col.UDT_NAME.getDataType(),
nvl(bt.TYPNAME, t.TYPNAME)).as(col.UDT_NAME),
PG_DESCRIPTION.DESCRIPTION)
field("({0})::information_schema.yes_or_no", col.IS_NULLABLE.getDataType(),
when(condition(a.ATTNOTNULL).or(t.TYPTYPE.eq(inline("d")).and(t.TYPNOTNULL)), inline("NO"))
.otherwise(inline("YES"))).as(col.IS_NULLABLE),
field("(pg_get_expr({0}, {1}))::information_schema.character_data", col.COLUMN_DEFAULT.getDataType(), ad.ADBIN, ad.ADRELID).as(col.COLUMN_DEFAULT),
field("({0})::information_schema.sql_identifier", col.UDT_SCHEMA.getDataType(),
nvl(nbt.NSPNAME, nt.NSPNAME)).as(col.UDT_SCHEMA),
field("({0})::information_schema.sql_identifier", col.UDT_NAME.getDataType(),
nvl(bt.TYPNAME, t.TYPNAME)).as(col.UDT_NAME),
PG_DESCRIPTION.DESCRIPTION)
.from(a
.leftJoin(ad)
.on(a.ATTRELID.eq(ad.ADRELID))
@ -175,21 +176,24 @@ public class PostgresMaterializedViewDefinition extends AbstractTableDefinition
record.get(COLUMNS.NUMERIC_SCALE),
record.get(COLUMNS.IS_NULLABLE, boolean.class),
record.get(COLUMNS.COLUMN_DEFAULT),
record.get(COLUMNS.UDT_NAME)
name(
record.get(COLUMNS.UDT_SCHEMA),
record.get(COLUMNS.UDT_NAME)
)
);
ColumnDefinition column = new DefaultColumnDefinition(
getDatabase().getTable(getSchema(), getName()),
record.get(COLUMNS.COLUMN_NAME),
record.get(COLUMNS.ORDINAL_POSITION, int.class),
type,
defaultString(record.get(COLUMNS.COLUMN_DEFAULT)).startsWith("nextval"),
record.get(PG_DESCRIPTION.DESCRIPTION)
);
ColumnDefinition column = new DefaultColumnDefinition(
getDatabase().getTable(getSchema(), getName()),
record.get(COLUMNS.COLUMN_NAME),
record.get(COLUMNS.ORDINAL_POSITION, int.class),
type,
defaultString(record.get(COLUMNS.COLUMN_DEFAULT)).startsWith("nextval"),
record.get(PG_DESCRIPTION.DESCRIPTION)
);
result.add(column);
}
result.add(column);
}
return result;
}
return result;
}
}

View File

@ -42,6 +42,7 @@ package org.jooq.util.postgres;
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.DSL.name;
import static org.jooq.util.postgres.information_schema.Tables.PARAMETERS;
import static org.jooq.util.postgres.information_schema.Tables.ROUTINES;
import static org.jooq.util.postgres.pg_catalog.Tables.PG_PROC;
@ -95,7 +96,10 @@ public class PostgresRoutineDefinition extends AbstractRoutineDefinition {
record.get(ROUTINES.NUMERIC_SCALE),
null,
(String) null,
record.get(ROUTINES.TYPE_UDT_NAME)
name(
record.get(ROUTINES.TYPE_UDT_SCHEMA),
record.get(ROUTINES.TYPE_UDT_NAME)
)
);
returnValue = new DefaultParameterDefinition(this, "RETURN_VALUE", -1, type);
@ -119,6 +123,7 @@ public class PostgresRoutineDefinition extends AbstractRoutineDefinition {
PARAMETERS.CHARACTER_MAXIMUM_LENGTH,
PARAMETERS.NUMERIC_PRECISION,
PARAMETERS.NUMERIC_SCALE,
PARAMETERS.UDT_SCHEMA,
PARAMETERS.UDT_NAME,
PARAMETERS.ORDINAL_POSITION,
PARAMETERS.PARAMETER_MODE,
@ -132,17 +137,25 @@ public class PostgresRoutineDefinition extends AbstractRoutineDefinition {
.fetch()) {
String inOut = record.get(PARAMETERS.PARAMETER_MODE);
SchemaDefinition typeSchema = null;
String schemaName = record.get(PARAMETERS.UDT_SCHEMA);
if (schemaName != null)
typeSchema = getDatabase().getSchema(schemaName);
DataTypeDefinition type = new DefaultDataTypeDefinition(
getDatabase(),
getSchema(),
typeSchema,
record.get(PARAMETERS.DATA_TYPE),
record.get(PARAMETERS.CHARACTER_MAXIMUM_LENGTH),
record.get(PARAMETERS.NUMERIC_PRECISION),
record.get(PARAMETERS.NUMERIC_SCALE),
null,
record.get(PARAMETERS.PARAMETER_DEFAULT),
record.get(PARAMETERS.UDT_NAME)
name(
record.get(PARAMETERS.UDT_SCHEMA),
record.get(PARAMETERS.UDT_NAME)
)
);
ParameterDefinition parameter = new DefaultParameterDefinition(

View File

@ -41,6 +41,7 @@
package org.jooq.util.postgres;
import static org.jooq.impl.DSL.name;
import static org.jooq.tools.StringUtils.defaultString;
import static org.jooq.util.postgres.PostgresDSL.oid;
import static org.jooq.util.postgres.information_schema.Tables.COLUMNS;
@ -65,13 +66,13 @@ import org.jooq.util.SchemaDefinition;
*/
public class PostgresTableDefinition extends AbstractTableDefinition {
public PostgresTableDefinition(SchemaDefinition schema, String name, String comment) {
super(schema, name, comment);
}
public PostgresTableDefinition(SchemaDefinition schema, String name, String comment) {
super(schema, name, comment);
}
@Override
public List<ColumnDefinition> getElements0() throws SQLException {
List<ColumnDefinition> result = new ArrayList<ColumnDefinition>();
@Override
public List<ColumnDefinition> getElements0() throws SQLException {
List<ColumnDefinition> result = new ArrayList<ColumnDefinition>();
for (Record record : create().select(
COLUMNS.COLUMN_NAME,
@ -114,21 +115,24 @@ public class PostgresTableDefinition extends AbstractTableDefinition {
record.get(COLUMNS.NUMERIC_SCALE),
record.get(COLUMNS.IS_NULLABLE, boolean.class),
record.get(COLUMNS.COLUMN_DEFAULT),
record.get(COLUMNS.UDT_NAME)
name(
record.get(COLUMNS.UDT_SCHEMA),
record.get(COLUMNS.UDT_NAME)
)
);
ColumnDefinition column = new DefaultColumnDefinition(
getDatabase().getTable(getSchema(), getName()),
record.get(COLUMNS.COLUMN_NAME),
record.get(COLUMNS.ORDINAL_POSITION, int.class),
type,
defaultString(record.get(COLUMNS.COLUMN_DEFAULT)).trim().toLowerCase().startsWith("nextval"),
record.get(PG_DESCRIPTION.DESCRIPTION)
);
ColumnDefinition column = new DefaultColumnDefinition(
getDatabase().getTable(getSchema(), getName()),
record.get(COLUMNS.COLUMN_NAME),
record.get(COLUMNS.ORDINAL_POSITION, int.class),
type,
defaultString(record.get(COLUMNS.COLUMN_DEFAULT)).trim().toLowerCase().startsWith("nextval"),
record.get(PG_DESCRIPTION.DESCRIPTION)
);
result.add(column);
}
result.add(column);
}
return result;
}
return result;
}
}

View File

@ -42,6 +42,7 @@
package org.jooq.util.postgres;
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.DSL.name;
import static org.jooq.impl.DSL.nvl;
import static org.jooq.impl.DSL.partitionBy;
import static org.jooq.impl.DSL.row;
@ -83,15 +84,15 @@ public class PostgresTableValuedFunction extends AbstractTableDefinition {
private final String specificName;
public PostgresTableValuedFunction(SchemaDefinition schema, String name, String specificName, String comment) {
super(schema, name, comment);
super(schema, name, comment);
this.routine = new PostgresRoutineDefinition(schema.getDatabase(), schema.getInputName(), name, specificName);
this.specificName = specificName;
}
this.routine = new PostgresRoutineDefinition(schema.getDatabase(), schema.getInputName(), name, specificName);
this.specificName = specificName;
}
@Override
public List<ColumnDefinition> getElements0() throws SQLException {
List<ColumnDefinition> result = new ArrayList<ColumnDefinition>();
@Override
public List<ColumnDefinition> getElements0() throws SQLException {
List<ColumnDefinition> result = new ArrayList<ColumnDefinition>();
Routines r = ROUTINES;
Parameters p = PARAMETERS;
@ -186,23 +187,26 @@ public class PostgresTableValuedFunction extends AbstractTableDefinition {
record.get(p.NUMERIC_SCALE),
record.get(c.IS_NULLABLE, boolean.class),
record.get(c.COLUMN_DEFAULT),
record.get(p.UDT_NAME)
name(
record.get(p.UDT_SCHEMA),
record.get(p.UDT_NAME)
)
);
ColumnDefinition column = new DefaultColumnDefinition(
getDatabase().getTable(getSchema(), getName()),
record.get(p.PARAMETER_NAME),
record.get(p.ORDINAL_POSITION, int.class),
type,
ColumnDefinition column = new DefaultColumnDefinition(
getDatabase().getTable(getSchema(), getName()),
record.get(p.PARAMETER_NAME),
record.get(p.ORDINAL_POSITION, int.class),
type,
defaultString(record.get(c.COLUMN_DEFAULT)).startsWith("nextval"),
null
);
null
);
result.add(column);
}
result.add(column);
}
return result;
}
return result;
}
@Override
protected List<ParameterDefinition> getParameters0() {

View File

@ -40,6 +40,7 @@
*/
package org.jooq.util.postgres;
import static org.jooq.impl.DSL.name;
import static org.jooq.util.postgres.information_schema.Tables.ATTRIBUTES;
import java.sql.SQLException;
@ -98,7 +99,10 @@ public class PostgresUDTDefinition extends AbstractUDTDefinition {
record.get(ATTRIBUTES.NUMERIC_SCALE),
record.get(ATTRIBUTES.IS_NULLABLE, boolean.class),
record.get(ATTRIBUTES.ATTRIBUTE_DEFAULT),
record.get(ATTRIBUTES.ATTRIBUTE_UDT_NAME)
name(
record.get(ATTRIBUTES.ATTRIBUTE_UDT_SCHEMA),
record.get(ATTRIBUTES.ATTRIBUTE_UDT_NAME)
)
);
AttributeDefinition column = new DefaultAttributeDefinition(