Skip to content

Commit

Permalink
[#5437] Add support for sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaseder committed Jul 25, 2016
1 parent 53e44fa commit 18d7071
Showing 1 changed file with 84 additions and 27 deletions.
111 changes: 84 additions & 27 deletions jOOQ/src/main/java/org/jooq/impl/InformationSchemaMetaImpl.java
Expand Up @@ -56,6 +56,7 @@
import org.jooq.Meta;
import org.jooq.Record;
import org.jooq.Schema;
import org.jooq.Sequence;
import org.jooq.Table;
import org.jooq.UniqueKey;
import org.jooq.exception.SQLDialectNotSupportedException;
Expand All @@ -67,15 +68,17 @@
*/
final class InformationSchemaMetaImpl implements Meta {

private final Configuration configuration;
private final List<Catalog> catalogs;
private final List<Schema> schemas;
private final Map<String, Schema> schemasByName;
private final Map<Catalog, List<Schema>> schemasPerCatalog;
private final List<Table<?>> tables;
private final Map<String, Table<?>> tablesByName;
private final Map<Schema, List<Table<?>>> tablesPerSchema;
private final List<UniqueKey<?>> primaryKeys;
private final Configuration configuration;
private final List<Catalog> catalogs;
private final List<Schema> schemas;
private final Map<String, Schema> schemasByName;
private final Map<Catalog, List<Schema>> schemasPerCatalog;
private final List<Table<?>> tables;
private final Map<String, Table<?>> tablesByName;
private final Map<Schema, List<Table<?>>> tablesPerSchema;
private final List<Sequence<?>> sequences;
private final Map<Schema, List<Sequence<?>>> sequencesPerSchema;
private final List<UniqueKey<?>> primaryKeys;

InformationSchemaMetaImpl(Configuration configuration, InformationSchema schema) {
this.configuration = configuration;
Expand All @@ -86,6 +89,8 @@ final class InformationSchemaMetaImpl implements Meta {
this.tables = new ArrayList<Table<?>>();
this.tablesByName = new HashMap<String, Table<?>>();
this.tablesPerSchema = new HashMap<Schema, List<Table<?>>>();
this.sequences = new ArrayList<Sequence<?>>();
this.sequencesPerSchema = new HashMap<Schema, List<Sequence<?>>>();
this.primaryKeys = new ArrayList<UniqueKey<?>>();

init(schema);
Expand All @@ -111,6 +116,23 @@ private final void init(InformationSchema meta) {
tablesByName.put(xt.getTableName(), it);
}

for (org.jooq.util.xml.jaxb.Sequence xs : meta.getSequences()) {
String typeName = xs.getDataType();
int length = xs.getCharacterMaximumLength() == null ? 0 : xs.getCharacterMaximumLength();
int precision = xs.getNumericPrecision() == null ? 0 : xs.getNumericPrecision();
int scale = xs.getNumericScale() == null ? 0 : xs.getNumericScale();
boolean nullable = true;

@SuppressWarnings({ "rawtypes", "unchecked" })
InformationSchemaSequence is = new InformationSchemaSequence(
xs.getSequenceName(),
schemasByName.get(xs.getSequenceSchema()),
type(typeName, length, precision, scale, nullable)
);

sequences.add(is);
}

List<Column> columns = new ArrayList<Column>(meta.getColumns());
Collections.sort(columns, new Comparator<Column>() {
@Override
Expand All @@ -136,25 +158,12 @@ public int compare(Column o1, Column o2) {
int scale = xc.getNumericScale() == null ? 0 : xc.getNumericScale();
boolean nullable = xc.isIsNullable() == null ? true : xc.isIsNullable();

if (precision == 0)
precision = xc.getCharacterMaximumLength() == null ? 0 : xc.getCharacterMaximumLength();

// TODO: Exception handling should be moved inside SQLDataType
DataType<?> type = null;
try {
type = DefaultDataType.getDataType(configuration.family(), typeName);
type = type.nullable(nullable);

if (length != 0)
type = type.length(length);
else if (precision != 0 || scale != 0)
type = type.precision(precision, scale);
}
catch (SQLDialectNotSupportedException e) {
type = SQLDataType.OTHER;
}

AbstractTable.createField(xc.getColumnName(), type, tablesByName.get(xc.getTableName()));
AbstractTable.createField(
xc.getColumnName(),
type(typeName, length, precision, scale, nullable),
tablesByName.get(xc.getTableName())
);
}

// Initialise indexes
Expand All @@ -181,6 +190,37 @@ else if (precision != 0 || scale != 0)

list.add(t);
}

for (Sequence<?> q : sequences) {
Schema s = q.getSchema();
List<Sequence<?>> list = sequencesPerSchema.get(s);

if (list == null) {
list = new ArrayList<Sequence<?>>();
sequencesPerSchema.put(s, list);
}

list.add(q);
}
}

private final DataType<?> type(String typeName, int length, int precision, int scale, boolean nullable) {
DataType<?> type = null;

try {
type = DefaultDataType.getDataType(configuration.family(), typeName);
type = type.nullable(nullable);

if (length != 0)
type = type.length(length);
else if (precision != 0 || scale != 0)
type = type.precision(precision, scale);
}
catch (SQLDialectNotSupportedException e) {
type = SQLDataType.OTHER;
}

return type;
}

@Override
Expand Down Expand Up @@ -235,6 +275,11 @@ public InformationSchemaSchema(String name, Catalog catalog) {
public final List<Table<?>> getTables() {
return unmodifiableList(tablesPerSchema.get(this));
}

@Override
public final List<Sequence<?>> getSequences() {
return unmodifiableList(sequencesPerSchema.get(this));
}
}

private final class InformationSchemaTable extends TableImpl<Record> {
Expand Down Expand Up @@ -263,4 +308,16 @@ public List<UniqueKey<Record>> getKeys() {
return super.getReferences();
}
}

private final class InformationSchemaSequence<N extends Number> extends SequenceImpl<N> {

/**
* Generated UID
*/
private static final long serialVersionUID = -1246697252597049756L;

InformationSchemaSequence(String name, Schema schema, DataType<N> type) {
super(name, schema, type);
}
}
}

0 comments on commit 18d7071

Please sign in to comment.