Skip to content

Commit

Permalink
[#5460] Support for exporting individual tables as well
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaseder committed Aug 5, 2016
1 parent 83c5769 commit 5b87623
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 62 deletions.
22 changes: 22 additions & 0 deletions jOOQ/src/main/java/org/jooq/DSLContext.java
Expand Up @@ -239,6 +239,28 @@ public interface DSLContext extends Scope , AutoCloseable {
*/
InformationSchema informationSchema(Schema... schemas);

/**
* Export a table to the {@link InformationSchema} format.
* <p>
* Exporting a single table will not include any foreign key definitions in
* the exported data.
* <p>
* This allows for serialising schema meta information as XML using JAXB.
* See also {@link Constants#XSD_META} for details.
*/
InformationSchema informationSchema(Table<?> table);

/**
* Export a set of tables to the {@link InformationSchema} format.
* <p>
* Only those foreign keys whose referenced table is also included in the
* export will be exported.
* <p>
* This allows for serialising schema meta information as XML using JAXB.
* See also {@link Constants#XSD_META} for details.
*/
InformationSchema informationSchema(Table<?>... table);

// -------------------------------------------------------------------------
// XXX APIs for creating scope for transactions, mocking, batching, etc.
// -------------------------------------------------------------------------
Expand Down
18 changes: 12 additions & 6 deletions jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java
Expand Up @@ -380,7 +380,7 @@ public Meta meta(InformationSchema schema) {

@Override
public InformationSchema informationSchema(Catalog catalog) {
return informationSchema0(catalog.getSchemas());
return InformationSchemaExport.exportSchemas(configuration(), catalog.getSchemas());
}

@Override
Expand All @@ -390,21 +390,27 @@ public InformationSchema informationSchema(Catalog... catalogs) {
for (Catalog catalog : catalogs)
schemas.addAll(catalog.getSchemas());

return informationSchema0(schemas);
return InformationSchemaExport.exportSchemas(configuration(), schemas);
}

@Override
public InformationSchema informationSchema(Schema schema) {
return informationSchema0(Arrays.asList(schema));
return InformationSchemaExport.exportSchemas(configuration(), Arrays.asList(schema));
}

@Override
public InformationSchema informationSchema(Schema... schemas) {
return informationSchema0(Arrays.asList(schemas));
return InformationSchemaExport.exportSchemas(configuration(), Arrays.asList(schemas));
}

private final InformationSchema informationSchema0(List<Schema> schemas) {
return InformationSchemaExport.export(configuration(), schemas);
@Override
public InformationSchema informationSchema(Table<?> table) {
return InformationSchemaExport.exportTables(configuration(), Arrays.asList(table));
}

@Override
public InformationSchema informationSchema(Table<?>... tables) {
return InformationSchemaExport.exportTables(configuration(), Arrays.asList(tables));
}

// -------------------------------------------------------------------------
Expand Down
140 changes: 84 additions & 56 deletions jOOQ/src/main/java/org/jooq/impl/InformationSchemaExport.java
Expand Up @@ -40,7 +40,9 @@
*/
package org.jooq.impl;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.jooq.Configuration;
import org.jooq.Field;
Expand All @@ -56,90 +58,116 @@
*/
final class InformationSchemaExport {

static final InformationSchema export(Configuration configuration, List<Schema> schemas) {
static final InformationSchema exportTables(Configuration configuration, List<Table<?>> tables) {
InformationSchema result = new InformationSchema();

Set<Schema> schemas = new LinkedHashSet<Schema>();
for (Table<?> t : tables)
schemas.add(t.getSchema());

for (Schema s : schemas)
exportSchema0(result, s);

for (Table<?> t : tables)
exportTable0(configuration, result, t);

return result;
}

static final InformationSchema exportSchemas(Configuration configuration, List<Schema> schemas) {
InformationSchema result = new InformationSchema();

for (Schema s : schemas) {
org.jooq.util.xml.jaxb.Schema is = new org.jooq.util.xml.jaxb.Schema();
exportSchema0(result, s);

for (Table<?> t : s.getTables())
exportTable0(configuration, result, t);

if (!StringUtils.isBlank(s.getCatalog().getName()))
is.setCatalogName(s.getCatalog().getName());
for (Sequence<?> q : s.getSequences())
exportSequences0(configuration, result, q);
}

if (!StringUtils.isBlank(s.getName())) {
is.setSchemaName(s.getName());
result.getSchemata().add(is);
}
return result;
}

for (Table<?> t : s.getTables()) {
org.jooq.util.xml.jaxb.Table it = new org.jooq.util.xml.jaxb.Table();
private static void exportSequences0(Configuration configuration, InformationSchema result, Sequence<?> q) {
org.jooq.util.xml.jaxb.Sequence iq = new org.jooq.util.xml.jaxb.Sequence();

if (!StringUtils.isBlank(t.getCatalog().getName()))
it.setTableCatalog(t.getCatalog().getName());
if (!StringUtils.isBlank(q.getCatalog().getName()))
iq.setSequenceCatalog(q.getCatalog().getName());

if (!StringUtils.isBlank(t.getSchema().getName()))
it.setTableSchema(t.getSchema().getName());
if (!StringUtils.isBlank(q.getSchema().getName()))
iq.setSequenceSchema(q.getSchema().getName());

it.setTableName(t.getName());
result.getTables().add(it);
iq.setSequenceName(q.getName());
iq.setDataType(q.getDataType().getTypeName(configuration));

Field<?>[] fields = t.fields();
for (int i = 0; i < fields.length; i++) {
Field<?> f = fields[i];
Column ic = new Column();
if (q.getDataType().hasLength())
iq.setCharacterMaximumLength(q.getDataType().length());

if (!StringUtils.isBlank(t.getCatalog().getName()))
ic.setTableCatalog(t.getCatalog().getName());
if (q.getDataType().hasPrecision())
iq.setNumericPrecision(q.getDataType().precision());

if (!StringUtils.isBlank(t.getSchema().getName()))
ic.setTableSchema(t.getSchema().getName());
if (q.getDataType().hasScale())
iq.setNumericScale(q.getDataType().scale());

ic.setTableName(t.getName());
ic.setColumnName(t.getName());
ic.setDataType(f.getDataType().getTypeName(configuration));
result.getSequences().add(iq);
}

if (f.getDataType().hasLength())
ic.setCharacterMaximumLength(f.getDataType().length());
private static void exportTable0(Configuration configuration, InformationSchema result, Table<?> t) {
org.jooq.util.xml.jaxb.Table it = new org.jooq.util.xml.jaxb.Table();

if (f.getDataType().hasPrecision())
ic.setNumericPrecision(f.getDataType().precision());
if (!StringUtils.isBlank(t.getCatalog().getName()))
it.setTableCatalog(t.getCatalog().getName());

if (f.getDataType().hasScale())
ic.setNumericScale(f.getDataType().scale());
if (!StringUtils.isBlank(t.getSchema().getName()))
it.setTableSchema(t.getSchema().getName());

ic.setColumnDefault(DSL.using(configuration).render(f.getDataType().defaultValue()));
ic.setIsNullable(f.getDataType().nullable());
ic.setOrdinalPosition(i + 1);
it.setTableName(t.getName());
result.getTables().add(it);

result.getColumns().add(ic);
}
}
Field<?>[] fields = t.fields();
for (int i = 0; i < fields.length; i++) {
Field<?> f = fields[i];
Column ic = new Column();

for (Sequence<?> q : s.getSequences()) {
org.jooq.util.xml.jaxb.Sequence iq = new org.jooq.util.xml.jaxb.Sequence();
if (!StringUtils.isBlank(t.getCatalog().getName()))
ic.setTableCatalog(t.getCatalog().getName());

if (!StringUtils.isBlank(q.getCatalog().getName()))
iq.setSequenceCatalog(q.getCatalog().getName());
if (!StringUtils.isBlank(t.getSchema().getName()))
ic.setTableSchema(t.getSchema().getName());

if (!StringUtils.isBlank(q.getSchema().getName()))
iq.setSequenceSchema(q.getSchema().getName());
ic.setTableName(t.getName());
ic.setColumnName(t.getName());
ic.setDataType(f.getDataType().getTypeName(configuration));

iq.setSequenceName(q.getName());
iq.setDataType(q.getDataType().getTypeName(configuration));
if (f.getDataType().hasLength())
ic.setCharacterMaximumLength(f.getDataType().length());

if (q.getDataType().hasLength())
iq.setCharacterMaximumLength(q.getDataType().length());
if (f.getDataType().hasPrecision())
ic.setNumericPrecision(f.getDataType().precision());

if (q.getDataType().hasPrecision())
iq.setNumericPrecision(q.getDataType().precision());
if (f.getDataType().hasScale())
ic.setNumericScale(f.getDataType().scale());

if (q.getDataType().hasScale())
iq.setNumericScale(q.getDataType().scale());
ic.setColumnDefault(DSL.using(configuration).render(f.getDataType().defaultValue()));
ic.setIsNullable(f.getDataType().nullable());
ic.setOrdinalPosition(i + 1);

result.getSequences().add(iq);
}
result.getColumns().add(ic);
}
}

return result;
private static final void exportSchema0(InformationSchema result, Schema s) {
org.jooq.util.xml.jaxb.Schema is = new org.jooq.util.xml.jaxb.Schema();

if (!StringUtils.isBlank(s.getCatalog().getName()))
is.setCatalogName(s.getCatalog().getName());

if (!StringUtils.isBlank(s.getName())) {
is.setSchemaName(s.getName());
result.getSchemata().add(is);
}
}

private InformationSchemaExport() {}
Expand Down

0 comments on commit 5b87623

Please sign in to comment.