[#6307] Add code generation support for indexes (including Oracle impl)
This commit is contained in:
parent
43c13ce1d7
commit
d9a917f4de
@ -54,6 +54,7 @@ abstract class AbstractGenerator implements Generator {
|
||||
private static final JooqLogger log = JooqLogger.getLogger(AbstractGenerator.class);
|
||||
|
||||
boolean generateDeprecated = true;
|
||||
boolean generateIndexes = true;
|
||||
boolean generateRelations = true;
|
||||
boolean generateInstanceFields = true;
|
||||
boolean generateGeneratedAnnotation = true;
|
||||
@ -174,6 +175,16 @@ abstract class AbstractGenerator implements Generator {
|
||||
this.generateDeprecated = generateDeprecated;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean generateIndexes() {
|
||||
return generateIndexes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setGenerateIndexes(boolean generateIndexes) {
|
||||
this.generateIndexes = generateIndexes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean generateRelations() {
|
||||
|
||||
|
||||
@ -418,6 +418,7 @@ public class GenerationTool {
|
||||
database.setIncludeExcludeColumns(TRUE.equals(d.isIncludeExcludeColumns()));
|
||||
database.setIncludeForeignKeys(!FALSE.equals(d.isIncludeForeignKeys()));
|
||||
database.setIncludePackages(!FALSE.equals(d.isIncludePackages()));
|
||||
database.setIncludeIndexes(!FALSE.equals(d.isIncludeIndexes()));
|
||||
database.setIncludePrimaryKeys(!FALSE.equals(d.isIncludePrimaryKeys()));
|
||||
database.setIncludeRoutines(!FALSE.equals(d.isIncludeRoutines()));
|
||||
database.setIncludeSequences(!FALSE.equals(d.isIncludeSequences()));
|
||||
@ -517,6 +518,8 @@ public class GenerationTool {
|
||||
// [#1394] The <generate/> element should be optional
|
||||
if (g.getGenerate() == null)
|
||||
g.setGenerate(new Generate());
|
||||
if (g.getGenerate().isIndexes() != null)
|
||||
generator.setGenerateIndexes(g.getGenerate().isIndexes());
|
||||
if (g.getGenerate().isRelations() != null)
|
||||
generator.setGenerateRelations(g.getGenerate().isRelations());
|
||||
if (g.getGenerate().isDeprecated() != null)
|
||||
|
||||
@ -69,6 +69,16 @@ public interface Generator {
|
||||
*/
|
||||
void setGenerateDeprecated(boolean generateDeprecated);
|
||||
|
||||
/**
|
||||
* Whether indexes should be generated.
|
||||
*/
|
||||
boolean generateIndexes();
|
||||
|
||||
/**
|
||||
* Whether indexes should be generated.
|
||||
*/
|
||||
void setGenerateIndexes(boolean generateIndexes);
|
||||
|
||||
/**
|
||||
* Whether foreign key relations should be resolved
|
||||
*/
|
||||
|
||||
@ -38,6 +38,7 @@ package org.jooq.util;
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.jooq.SQLDialect.MYSQL;
|
||||
import static org.jooq.SQLDialect.POSTGRES;
|
||||
import static org.jooq.SortOrder.DESC;
|
||||
import static org.jooq.impl.DSL.name;
|
||||
import static org.jooq.tools.StringUtils.defaultIfBlank;
|
||||
import static org.jooq.tools.StringUtils.defaultString;
|
||||
@ -80,6 +81,7 @@ import org.jooq.EnumType;
|
||||
import org.jooq.Field;
|
||||
import org.jooq.ForeignKey;
|
||||
import org.jooq.Identity;
|
||||
import org.jooq.Index;
|
||||
// ...
|
||||
import org.jooq.Name;
|
||||
import org.jooq.Package;
|
||||
@ -89,6 +91,7 @@ import org.jooq.Result;
|
||||
import org.jooq.Row;
|
||||
import org.jooq.Schema;
|
||||
import org.jooq.Sequence;
|
||||
import org.jooq.SortField;
|
||||
import org.jooq.Table;
|
||||
import org.jooq.TableField;
|
||||
import org.jooq.UDT;
|
||||
@ -262,6 +265,7 @@ public class JavaGenerator extends AbstractGenerator {
|
||||
+ ((!generateInterfaces && generateImmutableInterfaces) ? " (forced to true because of <immutableInterfaces/>)" : ""));
|
||||
log.info(" immutable interfaces", generateInterfaces());
|
||||
log.info(" daos", generateDaos());
|
||||
log.info(" indexes", generateIndexes());
|
||||
log.info(" relations", generateRelations()
|
||||
+ ((!generateRelations && generateTables) ? " (forced to true because of <tables/>)" :
|
||||
((!generateRelations && generateDaos) ? " (forced to true because of <daos/>)" : "")));
|
||||
@ -428,7 +432,7 @@ public class JavaGenerator extends AbstractGenerator {
|
||||
generateTableReferences(schema);
|
||||
}
|
||||
|
||||
if (generateRelations() && database.getTables(schema).size() > 0) {
|
||||
if ((generateRelations() || generateIndexes()) && database.getTables(schema).size() > 0) {
|
||||
generateRelations(schema);
|
||||
}
|
||||
|
||||
@ -623,7 +627,7 @@ public class JavaGenerator extends AbstractGenerator {
|
||||
JavaWriter out = newJavaWriter(new File(getFile(schema).getParentFile(), "Keys.java"));
|
||||
printPackage(out, schema);
|
||||
printClassJavadoc(out,
|
||||
"A class modelling foreign key relationships between tables of the <code>" + schema.getOutputName() + "</code> schema");
|
||||
"A class modelling foreign key relationships, indexes, and constraints of tables of the <code>" + schema.getOutputName() + "</code> schema");
|
||||
printClassAnnotations(out, schema);
|
||||
|
||||
if (scala)
|
||||
@ -631,99 +635,142 @@ public class JavaGenerator extends AbstractGenerator {
|
||||
else
|
||||
out.println("public class Keys {");
|
||||
|
||||
out.tab(1).header("IDENTITY definitions");
|
||||
out.println();
|
||||
|
||||
List<IndexDefinition> allIndexes = new ArrayList<IndexDefinition>();
|
||||
List<IdentityDefinition> allIdentities = new ArrayList<IdentityDefinition>();
|
||||
List<UniqueKeyDefinition> allUniqueKeys = new ArrayList<UniqueKeyDefinition>();
|
||||
List<ForeignKeyDefinition> allForeignKeys = new ArrayList<ForeignKeyDefinition>();
|
||||
|
||||
for (TableDefinition table : database.getTables(schema)) {
|
||||
try {
|
||||
IdentityDefinition identity = table.getIdentity();
|
||||
if (generateIndexes()) {
|
||||
out.tab(1).header("INDEX definitions");
|
||||
out.println();
|
||||
|
||||
if (identity != null) {
|
||||
final String identityType = out.ref(getStrategy().getFullJavaClassName(identity.getColumn().getContainer(), Mode.RECORD));
|
||||
final String columnType = out.ref(getJavaType(identity.getColumn().getType()));
|
||||
final String identityId = getStrategy().getJavaIdentifier(identity.getColumn().getContainer());
|
||||
final int block = allIdentities.size() / INITIALISER_SIZE;
|
||||
for (TableDefinition table : database.getTables(schema)) {
|
||||
try {
|
||||
List<IndexDefinition> indexes = table.getIndexes();
|
||||
|
||||
if (scala)
|
||||
out.tab(1).println("val IDENTITY_%s = Identities%s.IDENTITY_%s",
|
||||
identityId, block, identityId);
|
||||
else
|
||||
out.tab(1).println("public static final %s<%s, %s> IDENTITY_%s = Identities%s.IDENTITY_%s;",
|
||||
Identity.class, identityType, columnType, identityId, block, identityId);
|
||||
for (IndexDefinition index : indexes) {
|
||||
final String keyId = getStrategy().getJavaIdentifier(index);
|
||||
final int block = allIndexes.size() / INITIALISER_SIZE;
|
||||
|
||||
allIdentities.add(identity);
|
||||
if (scala)
|
||||
out.tab(1).println("val %s = Indexes%s.%s", keyId, block, keyId);
|
||||
else
|
||||
out.tab(1).println("public static final %s %s = Indexes%s.%s;", Index.class, keyId, block, keyId);
|
||||
|
||||
allIndexes.add(index);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("Error while generating table " + table, e);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("Error while generating table " + table, e);
|
||||
}
|
||||
}
|
||||
|
||||
// Unique keys
|
||||
out.tab(1).header("UNIQUE and PRIMARY KEY definitions");
|
||||
out.println();
|
||||
if (generateRelations()) {
|
||||
out.tab(1).header("IDENTITY definitions");
|
||||
out.println();
|
||||
|
||||
for (TableDefinition table : database.getTables(schema)) {
|
||||
try {
|
||||
List<UniqueKeyDefinition> uniqueKeys = table.getUniqueKeys();
|
||||
|
||||
for (UniqueKeyDefinition uniqueKey : uniqueKeys) {
|
||||
final String keyType = out.ref(getStrategy().getFullJavaClassName(uniqueKey.getTable(), Mode.RECORD));
|
||||
final String keyId = getStrategy().getJavaIdentifier(uniqueKey);
|
||||
final int block = allUniqueKeys.size() / INITIALISER_SIZE;
|
||||
for (TableDefinition table : database.getTables(schema)) {
|
||||
try {
|
||||
IdentityDefinition identity = table.getIdentity();
|
||||
|
||||
if (scala)
|
||||
out.tab(1).println("val %s = UniqueKeys%s.%s", keyId, block, keyId);
|
||||
else
|
||||
out.tab(1).println("public static final %s<%s> %s = UniqueKeys%s.%s;", UniqueKey.class, keyType, keyId, block, keyId);
|
||||
if (identity != null) {
|
||||
final String identityType = out.ref(getStrategy().getFullJavaClassName(identity.getColumn().getContainer(), Mode.RECORD));
|
||||
final String columnType = out.ref(getJavaType(identity.getColumn().getType()));
|
||||
final String identityId = getStrategy().getJavaIdentifier(identity.getColumn().getContainer());
|
||||
final int block = allIdentities.size() / INITIALISER_SIZE;
|
||||
|
||||
allUniqueKeys.add(uniqueKey);
|
||||
if (scala)
|
||||
out.tab(1).println("val IDENTITY_%s = Identities%s.IDENTITY_%s",
|
||||
identityId, block, identityId);
|
||||
else
|
||||
out.tab(1).println("public static final %s<%s, %s> IDENTITY_%s = Identities%s.IDENTITY_%s;",
|
||||
Identity.class, identityType, columnType, identityId, block, identityId);
|
||||
|
||||
allIdentities.add(identity);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("Error while generating table " + table, e);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("Error while generating table " + table, e);
|
||||
}
|
||||
}
|
||||
|
||||
// Foreign keys
|
||||
out.tab(1).header("FOREIGN KEY definitions");
|
||||
out.println();
|
||||
// Unique keys
|
||||
out.tab(1).header("UNIQUE and PRIMARY KEY definitions");
|
||||
out.println();
|
||||
|
||||
for (TableDefinition table : database.getTables(schema)) {
|
||||
try {
|
||||
List<ForeignKeyDefinition> foreignKeys = table.getForeignKeys();
|
||||
for (TableDefinition table : database.getTables(schema)) {
|
||||
try {
|
||||
List<UniqueKeyDefinition> uniqueKeys = table.getUniqueKeys();
|
||||
|
||||
for (ForeignKeyDefinition foreignKey : foreignKeys) {
|
||||
final String keyType = out.ref(getStrategy().getFullJavaClassName(foreignKey.getKeyTable(), Mode.RECORD));
|
||||
final String referencedType = out.ref(getStrategy().getFullJavaClassName(foreignKey.getReferencedTable(), Mode.RECORD));
|
||||
final String keyId = getStrategy().getJavaIdentifier(foreignKey);
|
||||
final int block = allForeignKeys.size() / INITIALISER_SIZE;
|
||||
for (UniqueKeyDefinition uniqueKey : uniqueKeys) {
|
||||
final String keyType = out.ref(getStrategy().getFullJavaClassName(uniqueKey.getTable(), Mode.RECORD));
|
||||
final String keyId = getStrategy().getJavaIdentifier(uniqueKey);
|
||||
final int block = allUniqueKeys.size() / INITIALISER_SIZE;
|
||||
|
||||
if (scala)
|
||||
out.tab(1).println("val %s = ForeignKeys%s.%s", keyId, block, keyId);
|
||||
else
|
||||
out.tab(1).println("public static final %s<%s, %s> %s = ForeignKeys%s.%s;", ForeignKey.class, keyType, referencedType, keyId, block, keyId);
|
||||
if (scala)
|
||||
out.tab(1).println("val %s = UniqueKeys%s.%s", keyId, block, keyId);
|
||||
else
|
||||
out.tab(1).println("public static final %s<%s> %s = UniqueKeys%s.%s;", UniqueKey.class, keyType, keyId, block, keyId);
|
||||
|
||||
allForeignKeys.add(foreignKey);
|
||||
allUniqueKeys.add(uniqueKey);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("Error while generating table " + table, e);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("Error while generating reference " + table, e);
|
||||
|
||||
// Foreign keys
|
||||
out.tab(1).header("FOREIGN KEY definitions");
|
||||
out.println();
|
||||
|
||||
for (TableDefinition table : database.getTables(schema)) {
|
||||
try {
|
||||
List<ForeignKeyDefinition> foreignKeys = table.getForeignKeys();
|
||||
|
||||
for (ForeignKeyDefinition foreignKey : foreignKeys) {
|
||||
final String keyType = out.ref(getStrategy().getFullJavaClassName(foreignKey.getKeyTable(), Mode.RECORD));
|
||||
final String referencedType = out.ref(getStrategy().getFullJavaClassName(foreignKey.getReferencedTable(), Mode.RECORD));
|
||||
final String keyId = getStrategy().getJavaIdentifier(foreignKey);
|
||||
final int block = allForeignKeys.size() / INITIALISER_SIZE;
|
||||
|
||||
if (scala)
|
||||
out.tab(1).println("val %s = ForeignKeys%s.%s", keyId, block, keyId);
|
||||
else
|
||||
out.tab(1).println("public static final %s<%s, %s> %s = ForeignKeys%s.%s;", ForeignKey.class, keyType, referencedType, keyId, block, keyId);
|
||||
|
||||
allForeignKeys.add(foreignKey);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("Error while generating reference " + table, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// [#1459] Print nested classes for actual static field initialisations
|
||||
// keeping top-level initialiser small
|
||||
int indexCounter = 0;
|
||||
int identityCounter = 0;
|
||||
int uniqueKeyCounter = 0;
|
||||
int foreignKeyCounter = 0;
|
||||
|
||||
out.tab(1).header("[#1459] distribute members to avoid static initialisers > 64kb");
|
||||
|
||||
// Indexes
|
||||
// -------
|
||||
|
||||
for (IndexDefinition index : allIndexes) {
|
||||
printIndex(out, indexCounter, index);
|
||||
indexCounter++;
|
||||
}
|
||||
|
||||
if (indexCounter > 0) {
|
||||
out.tab(1).println("}");
|
||||
}
|
||||
|
||||
// Identities
|
||||
// ----------
|
||||
|
||||
@ -766,6 +813,58 @@ public class JavaGenerator extends AbstractGenerator {
|
||||
watch.splitInfo("Keys generated");
|
||||
}
|
||||
|
||||
protected void printIndex(JavaWriter out, int indexCounter, IndexDefinition index) {
|
||||
final int block = indexCounter / INITIALISER_SIZE;
|
||||
|
||||
// Print new nested class
|
||||
if (indexCounter % INITIALISER_SIZE == 0) {
|
||||
if (indexCounter > 0) {
|
||||
out.tab(1).println("}");
|
||||
}
|
||||
|
||||
out.println();
|
||||
|
||||
if (scala)
|
||||
out.tab(1).println("private object Indexes%s extends %s {", block, AbstractKeys.class);
|
||||
else
|
||||
out.tab(1).println("private static class Indexes%s extends %s {", block, AbstractKeys.class);
|
||||
}
|
||||
|
||||
// (Name name, Table<?> table, SortField<?>[] sortFields, boolean unique)
|
||||
String sortFieldSeparator = "";
|
||||
StringBuilder sortFields = new StringBuilder();
|
||||
|
||||
for (IndexColumnDefinition column : index.getIndexColumns()) {
|
||||
sortFields.append(sortFieldSeparator);
|
||||
sortFields.append(out.ref(getStrategy().getFullJavaIdentifier(column.getColumn()), 3));
|
||||
sortFields.append(column.getSortOrder() == DESC ? ".desc()" : ".asc()");
|
||||
|
||||
sortFieldSeparator = ", ";
|
||||
}
|
||||
|
||||
if (scala)
|
||||
out.tab(2).println("val %s : %s = %s.createIndex(\"%s\", %s, Array[%s](%s), %s)",
|
||||
getStrategy().getJavaIdentifier(index),
|
||||
Index.class,
|
||||
AbstractKeys.class,
|
||||
escapeString(index.getOutputName()),
|
||||
out.ref(getStrategy().getFullJavaIdentifier(index.getTable()), 2),
|
||||
SortField.class,
|
||||
sortFields,
|
||||
index.isUnique()
|
||||
);
|
||||
else
|
||||
out.tab(2).println("public static %s %s = createIndex(\"%s\", %s, new %s[] { %s }, %s);",
|
||||
Index.class,
|
||||
getStrategy().getJavaIdentifier(index),
|
||||
escapeString(index.getOutputName()),
|
||||
out.ref(getStrategy().getFullJavaIdentifier(index.getTable()), 2),
|
||||
SortField.class,
|
||||
sortFields,
|
||||
index.isUnique()
|
||||
);
|
||||
}
|
||||
|
||||
protected void printIdentity(JavaWriter out, int identityCounter, IdentityDefinition identity) {
|
||||
final int block = identityCounter / INITIALISER_SIZE;
|
||||
|
||||
@ -785,13 +884,13 @@ public class JavaGenerator extends AbstractGenerator {
|
||||
|
||||
if (scala)
|
||||
out.tab(2).println("val %s : %s[%s, %s] = %s.createIdentity(%s, %s)",
|
||||
getStrategy().getJavaIdentifier(identity),
|
||||
Identity.class,
|
||||
out.ref(getStrategy().getFullJavaClassName(identity.getTable(), Mode.RECORD)),
|
||||
out.ref(getJavaType(identity.getColumn().getType())),
|
||||
AbstractKeys.class,
|
||||
out.ref(getStrategy().getFullJavaIdentifier(identity.getColumn().getContainer()), 2),
|
||||
out.ref(getStrategy().getFullJavaIdentifier(identity.getColumn()), colRefSegments(identity.getColumn())));
|
||||
getStrategy().getJavaIdentifier(identity),
|
||||
Identity.class,
|
||||
out.ref(getStrategy().getFullJavaClassName(identity.getTable(), Mode.RECORD)),
|
||||
out.ref(getJavaType(identity.getColumn().getType())),
|
||||
AbstractKeys.class,
|
||||
out.ref(getStrategy().getFullJavaIdentifier(identity.getColumn().getContainer()), 2),
|
||||
out.ref(getStrategy().getFullJavaIdentifier(identity.getColumn()), colRefSegments(identity.getColumn())));
|
||||
else
|
||||
out.tab(2).println("public static %s<%s, %s> %s = createIdentity(%s, %s);",
|
||||
Identity.class,
|
||||
@ -821,13 +920,13 @@ public class JavaGenerator extends AbstractGenerator {
|
||||
|
||||
if (scala)
|
||||
out.tab(2).println("val %s : %s[%s] = %s.createUniqueKey(%s, \"%s\", [[%s]])",
|
||||
getStrategy().getJavaIdentifier(uniqueKey),
|
||||
UniqueKey.class,
|
||||
out.ref(getStrategy().getFullJavaClassName(uniqueKey.getTable(), Mode.RECORD)),
|
||||
AbstractKeys.class,
|
||||
out.ref(getStrategy().getFullJavaIdentifier(uniqueKey.getTable()), 2),
|
||||
escapeString(uniqueKey.getOutputName()),
|
||||
out.ref(getStrategy().getFullJavaIdentifiers(uniqueKey.getKeyColumns()), colRefSegments(null)));
|
||||
getStrategy().getJavaIdentifier(uniqueKey),
|
||||
UniqueKey.class,
|
||||
out.ref(getStrategy().getFullJavaClassName(uniqueKey.getTable(), Mode.RECORD)),
|
||||
AbstractKeys.class,
|
||||
out.ref(getStrategy().getFullJavaIdentifier(uniqueKey.getTable()), 2),
|
||||
escapeString(uniqueKey.getOutputName()),
|
||||
out.ref(getStrategy().getFullJavaIdentifiers(uniqueKey.getKeyColumns()), colRefSegments(null)));
|
||||
else
|
||||
out.tab(2).println("public static final %s<%s> %s = createUniqueKey(%s, \"%s\", [[%s]]);",
|
||||
UniqueKey.class,
|
||||
@ -857,15 +956,15 @@ public class JavaGenerator extends AbstractGenerator {
|
||||
|
||||
if (scala)
|
||||
out.tab(2).println("val %s : %s[%s, %s] = %s.createForeignKey(%s, %s, \"%s\", [[%s]])",
|
||||
getStrategy().getJavaIdentifier(foreignKey),
|
||||
ForeignKey.class,
|
||||
out.ref(getStrategy().getFullJavaClassName(foreignKey.getKeyTable(), Mode.RECORD)),
|
||||
out.ref(getStrategy().getFullJavaClassName(foreignKey.getReferencedTable(), Mode.RECORD)),
|
||||
AbstractKeys.class,
|
||||
out.ref(getStrategy().getFullJavaIdentifier(foreignKey.getReferencedKey()), 2),
|
||||
out.ref(getStrategy().getFullJavaIdentifier(foreignKey.getKeyTable()), 2),
|
||||
escapeString(foreignKey.getOutputName()),
|
||||
out.ref(getStrategy().getFullJavaIdentifiers(foreignKey.getKeyColumns()), colRefSegments(null)));
|
||||
getStrategy().getJavaIdentifier(foreignKey),
|
||||
ForeignKey.class,
|
||||
out.ref(getStrategy().getFullJavaClassName(foreignKey.getKeyTable(), Mode.RECORD)),
|
||||
out.ref(getStrategy().getFullJavaClassName(foreignKey.getReferencedTable(), Mode.RECORD)),
|
||||
AbstractKeys.class,
|
||||
out.ref(getStrategy().getFullJavaIdentifier(foreignKey.getReferencedKey()), 2),
|
||||
out.ref(getStrategy().getFullJavaIdentifier(foreignKey.getKeyTable()), 2),
|
||||
escapeString(foreignKey.getOutputName()),
|
||||
out.ref(getStrategy().getFullJavaIdentifiers(foreignKey.getKeyColumns()), colRefSegments(null)));
|
||||
else
|
||||
out.tab(2).println("public static final %s<%s, %s> %s = createForeignKey(%s, %s, \"%s\", [[%s]]);",
|
||||
ForeignKey.class,
|
||||
|
||||
@ -111,6 +111,7 @@ public abstract class AbstractDatabase implements Database {
|
||||
private boolean includePackages = true;
|
||||
private boolean includeUDTs = true;
|
||||
private boolean includeSequences = true;
|
||||
private boolean includeIndexes = true;
|
||||
private boolean includePrimaryKeys = true;
|
||||
private boolean includeUniqueKeys = true;
|
||||
private boolean includeForeignKeys = true;
|
||||
@ -142,6 +143,7 @@ public abstract class AbstractDatabase implements Database {
|
||||
private List<SchemaDefinition> schemata;
|
||||
private List<SequenceDefinition> sequences;
|
||||
private List<IdentityDefinition> identities;
|
||||
private List<IndexDefinition> indexes;
|
||||
private List<UniqueKeyDefinition> uniqueKeys;
|
||||
private List<ForeignKeyDefinition> foreignKeys;
|
||||
private List<CheckConstraintDefinition> checkConstraints;
|
||||
@ -158,6 +160,8 @@ public abstract class AbstractDatabase implements Database {
|
||||
|
||||
private transient Map<SchemaDefinition, List<SequenceDefinition>> sequencesBySchema;
|
||||
private transient Map<SchemaDefinition, List<IdentityDefinition>> identitiesBySchema;
|
||||
private transient Map<SchemaDefinition, List<IndexDefinition>> indexesBySchema;
|
||||
private transient Map<TableDefinition, List<IndexDefinition>> indexesByTable;
|
||||
private transient Map<SchemaDefinition, List<UniqueKeyDefinition>> uniqueKeysBySchema;
|
||||
private transient Map<SchemaDefinition, List<ForeignKeyDefinition>> foreignKeysBySchema;
|
||||
private transient Map<SchemaDefinition, List<CheckConstraintDefinition>> checkConstraintsBySchema;
|
||||
@ -731,6 +735,16 @@ public abstract class AbstractDatabase implements Database {
|
||||
this.includeSequences = includeSequences;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void setIncludeIndexes(boolean includeIndexes) {
|
||||
this.includeIndexes = includeIndexes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean getIncludeIndexes() {
|
||||
return includeIndexes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean getIncludePrimaryKeys() {
|
||||
return includePrimaryKeys;
|
||||
@ -1484,6 +1498,59 @@ public abstract class AbstractDatabase implements Database {
|
||||
return relations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<IndexDefinition> getIndexes(SchemaDefinition schema) {
|
||||
if (indexes == null) {
|
||||
indexes = new ArrayList<IndexDefinition>();
|
||||
|
||||
if (getIncludeIndexes()) {
|
||||
try {
|
||||
List<IndexDefinition> r = getIndexes0();
|
||||
|
||||
indexes = sort(filterExcludeInclude(r));
|
||||
log.info("Indexes fetched", fetchedSize(r, indexes));
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("Error while fetching indexes", e);
|
||||
}
|
||||
}
|
||||
else
|
||||
log.info("Indexes excluded");
|
||||
}
|
||||
|
||||
if (indexesBySchema == null)
|
||||
indexesBySchema = new LinkedHashMap<SchemaDefinition, List<IndexDefinition>>();
|
||||
|
||||
List<IndexDefinition> result = filterSchema(indexes, schema, indexesBySchema);
|
||||
|
||||
if (indexesByTable == null)
|
||||
indexesByTable = new HashMap<TableDefinition, List<IndexDefinition>>();
|
||||
|
||||
for (IndexDefinition index : result) {
|
||||
List<IndexDefinition> list = indexesByTable.get(index.getTable());
|
||||
|
||||
if (list == null) {
|
||||
list = new ArrayList<IndexDefinition>();
|
||||
indexesByTable.put(index.getTable(), list);
|
||||
}
|
||||
|
||||
list.add(index);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<IndexDefinition> getIndexes(TableDefinition table) {
|
||||
|
||||
// Lazy initialise indexes
|
||||
if (indexesByTable == null)
|
||||
getIndexes(table.getSchema());
|
||||
|
||||
List<IndexDefinition> list = indexesByTable.get(table);
|
||||
return list == null ? Collections.emptyList() : list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<RoutineDefinition> getRoutines(SchemaDefinition schema) {
|
||||
if (routines == null) {
|
||||
@ -1800,6 +1867,13 @@ public abstract class AbstractDatabase implements Database {
|
||||
*/
|
||||
protected abstract DSLContext create0();
|
||||
|
||||
/**
|
||||
* Retrieve ALL indexes from the database
|
||||
*/
|
||||
protected List<IndexDefinition> getIndexes0() throws SQLException {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve primary keys and store them to relations
|
||||
*/
|
||||
|
||||
@ -79,6 +79,11 @@ implements TableDefinition {
|
||||
return primaryKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<IndexDefinition> getIndexes() {
|
||||
return getDatabase().getIndexes(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<UniqueKeyDefinition> getUniqueKeys() {
|
||||
return getDatabase().getRelations().getUniqueKeys(this);
|
||||
|
||||
@ -98,6 +98,16 @@ public interface Database {
|
||||
*/
|
||||
List<IdentityDefinition> getIdentities(SchemaDefinition schema);
|
||||
|
||||
/**
|
||||
* The indexes contained in this database.
|
||||
*/
|
||||
List<IndexDefinition> getIndexes(SchemaDefinition schema);
|
||||
|
||||
/**
|
||||
* The indexes contained in this database.
|
||||
*/
|
||||
List<IndexDefinition> getIndexes(TableDefinition schema);
|
||||
|
||||
/**
|
||||
* The unique keys contained in this database.
|
||||
*/
|
||||
@ -398,6 +408,16 @@ public interface Database {
|
||||
*/
|
||||
boolean getIncludePrimaryKeys();
|
||||
|
||||
/**
|
||||
* whether indexes should be included.
|
||||
*/
|
||||
void setIncludeIndexes(boolean includeIndexes);
|
||||
|
||||
/**
|
||||
* whether indexes should be included.
|
||||
*/
|
||||
boolean getIncludeIndexes();
|
||||
|
||||
/**
|
||||
* whether sequences should be included.
|
||||
*/
|
||||
|
||||
@ -67,6 +67,11 @@ public interface TableDefinition extends Definition {
|
||||
*/
|
||||
ColumnDefinition getColumn(int columnIndex);
|
||||
|
||||
/**
|
||||
* Get the indexes for this table.
|
||||
*/
|
||||
List<IndexDefinition> getIndexes();
|
||||
|
||||
/**
|
||||
* Get the primary key for this table.
|
||||
*/
|
||||
|
||||
@ -65,6 +65,8 @@ public class Database implements Serializable
|
||||
@XmlElement(defaultValue = "true")
|
||||
protected Boolean includeSequences = true;
|
||||
@XmlElement(defaultValue = "true")
|
||||
protected Boolean includeIndexes = true;
|
||||
@XmlElement(defaultValue = "true")
|
||||
protected Boolean includePrimaryKeys = true;
|
||||
@XmlElement(defaultValue = "true")
|
||||
protected Boolean includeUniqueKeys = true;
|
||||
@ -435,6 +437,30 @@ public class Database implements Serializable
|
||||
this.includeSequences = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* This flag indicates whether indexes should be included in output produced by this database
|
||||
*
|
||||
* @return
|
||||
* possible object is
|
||||
* {@link Boolean }
|
||||
*
|
||||
*/
|
||||
public Boolean isIncludeIndexes() {
|
||||
return includeIndexes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the includeIndexes property.
|
||||
*
|
||||
* @param value
|
||||
* allowed object is
|
||||
* {@link Boolean }
|
||||
*
|
||||
*/
|
||||
public void setIncludeIndexes(Boolean value) {
|
||||
this.includeIndexes = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* This flag indicates whether primary keys should be included in output produced by this database
|
||||
*
|
||||
@ -1161,6 +1187,11 @@ public class Database implements Serializable
|
||||
return this;
|
||||
}
|
||||
|
||||
public Database withIncludeIndexes(Boolean value) {
|
||||
setIncludeIndexes(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Database withIncludePrimaryKeys(Boolean value) {
|
||||
setIncludePrimaryKeys(value);
|
||||
return this;
|
||||
|
||||
@ -35,6 +35,8 @@ public class Generate implements Serializable
|
||||
|
||||
private final static long serialVersionUID = 31000L;
|
||||
@XmlElement(defaultValue = "true")
|
||||
protected Boolean indexes = true;
|
||||
@XmlElement(defaultValue = "true")
|
||||
protected Boolean relations = true;
|
||||
@XmlElement(defaultValue = "true")
|
||||
protected Boolean deprecated = true;
|
||||
@ -110,6 +112,30 @@ public class Generate implements Serializable
|
||||
@XmlElement(defaultValue = "false")
|
||||
protected Boolean javaTimeTypes = false;
|
||||
|
||||
/**
|
||||
* Generate index information.
|
||||
*
|
||||
* @return
|
||||
* possible object is
|
||||
* {@link Boolean }
|
||||
*
|
||||
*/
|
||||
public Boolean isIndexes() {
|
||||
return indexes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the indexes property.
|
||||
*
|
||||
* @param value
|
||||
* allowed object is
|
||||
* {@link Boolean }
|
||||
*
|
||||
*/
|
||||
public void setIndexes(Boolean value) {
|
||||
this.indexes = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Primary key / foreign key relations should be generated and used.
|
||||
* This is a prerequisite for various advanced features
|
||||
@ -1026,6 +1052,11 @@ public class Generate implements Serializable
|
||||
this.javaTimeTypes = value;
|
||||
}
|
||||
|
||||
public Generate withIndexes(Boolean value) {
|
||||
setIndexes(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Generate withRelations(Boolean value) {
|
||||
setRelations(value);
|
||||
return this;
|
||||
|
||||
@ -456,6 +456,10 @@ Excludes match before includes, i.e. excludes have a higher priority.]]></jxb:ja
|
||||
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[This flag indicates whether sequences should be included in output produced by this database]]></jxb:javadoc></jxb:property></appinfo></annotation>
|
||||
</element>
|
||||
|
||||
<element name="includeIndexes" type="boolean" default="true" minOccurs="0" maxOccurs="1">
|
||||
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[This flag indicates whether indexes should be included in output produced by this database]]></jxb:javadoc></jxb:property></appinfo></annotation>
|
||||
</element>
|
||||
|
||||
<element name="includePrimaryKeys" type="boolean" default="true" minOccurs="0" maxOccurs="1">
|
||||
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[This flag indicates whether primary keys should be included in output produced by this database]]></jxb:javadoc></jxb:property></appinfo></annotation>
|
||||
</element>
|
||||
@ -816,6 +820,10 @@ type. If provided, both "expression" and "types" must match.]]></jxb:javadoc></j
|
||||
<complexType name="Generate">
|
||||
<annotation><appinfo><jxb:class><jxb:javadoc><![CDATA[Options strictly related to generated code.]]></jxb:javadoc></jxb:class></appinfo></annotation>
|
||||
<all>
|
||||
<element name="indexes" type="boolean" default="true" minOccurs="0" maxOccurs="1">
|
||||
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[Generate index information.]]></jxb:javadoc></jxb:property></appinfo></annotation>
|
||||
</element>
|
||||
|
||||
<element name="relations" type="boolean" default="true" minOccurs="0" maxOccurs="1">
|
||||
<annotation><appinfo><jxb:property><jxb:javadoc><![CDATA[Primary key / foreign key relations should be generated and used.
|
||||
This is a prerequisite for various advanced features]]></jxb:javadoc></jxb:property></appinfo></annotation>
|
||||
|
||||
@ -36,29 +36,38 @@ package org.jooq.impl;
|
||||
|
||||
import org.jooq.ForeignKey;
|
||||
import org.jooq.Identity;
|
||||
import org.jooq.Index;
|
||||
import org.jooq.Record;
|
||||
import org.jooq.SortField;
|
||||
import org.jooq.Table;
|
||||
import org.jooq.TableField;
|
||||
import org.jooq.UniqueKey;
|
||||
|
||||
/**
|
||||
* A base class for generated static references
|
||||
* A base class for generated static references.
|
||||
* <p>
|
||||
* This type is for JOOQ INTERNAL USE only. Do not reference directly
|
||||
* This type is for JOOQ INTERNAL USE only. Do not reference directly.
|
||||
*
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
public abstract class AbstractKeys {
|
||||
|
||||
/**
|
||||
* Factory method for identities
|
||||
* Factory method for indexes.
|
||||
*/
|
||||
protected static Index createIndex(String name, Table<?> table, SortField<?>[] sortFields, boolean unique) {
|
||||
return new IndexImpl(DSL.name(name), table, sortFields, null, unique);
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method for identities.
|
||||
*/
|
||||
protected static <R extends Record, T> Identity<R, T> createIdentity(Table<R> table, TableField<R, T> field) {
|
||||
return new IdentityImpl<R, T>(table, field);
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method for unique keys
|
||||
* Factory method for unique keys.
|
||||
*/
|
||||
|
||||
@SafeVarargs
|
||||
@ -68,7 +77,7 @@ public abstract class AbstractKeys {
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method for unique keys
|
||||
* Factory method for unique keys.
|
||||
*/
|
||||
|
||||
@SafeVarargs
|
||||
@ -78,7 +87,7 @@ public abstract class AbstractKeys {
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method for foreign keys
|
||||
* Factory method for foreign keys.
|
||||
*/
|
||||
|
||||
@SafeVarargs
|
||||
@ -88,7 +97,7 @@ public abstract class AbstractKeys {
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method for foreign keys
|
||||
* Factory method for foreign keys.
|
||||
*/
|
||||
|
||||
@SafeVarargs
|
||||
|
||||
Loading…
Reference in New Issue
Block a user