Skip to content

Commit

Permalink
[#6307] Added support for generating HSQLDB indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaseder committed Jul 12, 2017
1 parent 59ffa9b commit 770016b
Show file tree
Hide file tree
Showing 20 changed files with 3,200 additions and 2,116 deletions.
2 changes: 1 addition & 1 deletion jOOQ-meta/src/main/java/org/jooq/util/h2/H2Database.java
Expand Up @@ -105,7 +105,7 @@ protected DSLContext create0() {
protected List<IndexDefinition> getIndexes0() throws SQLException { protected List<IndexDefinition> getIndexes0() throws SQLException {
List<IndexDefinition> result = new ArrayList<IndexDefinition>(); List<IndexDefinition> result = new ArrayList<IndexDefinition>();


// Same implementation as in MySQLDatabase // Same implementation as in HSQLDBDatabase and MySQLDatabase
Map<Record, Result<Record>> indexes = create() Map<Record, Result<Record>> indexes = create()
.select( .select(
Indexes.TABLE_SCHEMA, Indexes.TABLE_SCHEMA,
Expand Down
78 changes: 78 additions & 0 deletions jOOQ-meta/src/main/java/org/jooq/util/hsqldb/HSQLDBDatabase.java
Expand Up @@ -45,32 +45,40 @@
import static org.jooq.util.hsqldb.information_schema.Tables.ROUTINES; import static org.jooq.util.hsqldb.information_schema.Tables.ROUTINES;
import static org.jooq.util.hsqldb.information_schema.Tables.SCHEMATA; import static org.jooq.util.hsqldb.information_schema.Tables.SCHEMATA;
import static org.jooq.util.hsqldb.information_schema.Tables.SEQUENCES; import static org.jooq.util.hsqldb.information_schema.Tables.SEQUENCES;
import static org.jooq.util.hsqldb.information_schema.Tables.SYSTEM_INDEXINFO;
import static org.jooq.util.hsqldb.information_schema.Tables.TABLES; import static org.jooq.util.hsqldb.information_schema.Tables.TABLES;
import static org.jooq.util.hsqldb.information_schema.Tables.TABLE_CONSTRAINTS; import static org.jooq.util.hsqldb.information_schema.Tables.TABLE_CONSTRAINTS;


import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


import org.jooq.DSLContext; import org.jooq.DSLContext;
import org.jooq.Field; import org.jooq.Field;
import org.jooq.Record; import org.jooq.Record;
import org.jooq.Record4; import org.jooq.Record4;
import org.jooq.Result; import org.jooq.Result;
import org.jooq.SQLDialect; import org.jooq.SQLDialect;
import org.jooq.SortOrder;
import org.jooq.impl.DSL; import org.jooq.impl.DSL;
import org.jooq.tools.JooqLogger; import org.jooq.tools.JooqLogger;
import org.jooq.util.AbstractDatabase; import org.jooq.util.AbstractDatabase;
import org.jooq.util.AbstractIndexDefinition;
import org.jooq.util.ArrayDefinition; import org.jooq.util.ArrayDefinition;
import org.jooq.util.CatalogDefinition; import org.jooq.util.CatalogDefinition;
import org.jooq.util.ColumnDefinition; import org.jooq.util.ColumnDefinition;
import org.jooq.util.DataTypeDefinition; import org.jooq.util.DataTypeDefinition;
import org.jooq.util.DefaultCheckConstraintDefinition; import org.jooq.util.DefaultCheckConstraintDefinition;
import org.jooq.util.DefaultDataTypeDefinition; import org.jooq.util.DefaultDataTypeDefinition;
import org.jooq.util.DefaultIndexColumnDefinition;
import org.jooq.util.DefaultRelations; import org.jooq.util.DefaultRelations;
import org.jooq.util.DefaultSequenceDefinition; import org.jooq.util.DefaultSequenceDefinition;
import org.jooq.util.DomainDefinition; import org.jooq.util.DomainDefinition;
import org.jooq.util.EnumDefinition; import org.jooq.util.EnumDefinition;
import org.jooq.util.IndexColumnDefinition;
import org.jooq.util.IndexDefinition;
import org.jooq.util.PackageDefinition; import org.jooq.util.PackageDefinition;
import org.jooq.util.RoutineDefinition; import org.jooq.util.RoutineDefinition;
import org.jooq.util.SchemaDefinition; import org.jooq.util.SchemaDefinition;
Expand All @@ -92,6 +100,76 @@ protected DSLContext create0() {
return DSL.using(getConnection(), SQLDialect.HSQLDB); return DSL.using(getConnection(), SQLDialect.HSQLDB);
} }


@Override
protected List<IndexDefinition> getIndexes0() throws SQLException {
List<IndexDefinition> result = new ArrayList<IndexDefinition>();

// Same implementation as in H2Database and MySQLDatabase
Map<Record, Result<Record>> indexes = create()
.select(
SYSTEM_INDEXINFO.TABLE_SCHEM,
SYSTEM_INDEXINFO.TABLE_NAME,
SYSTEM_INDEXINFO.INDEX_NAME,
SYSTEM_INDEXINFO.NON_UNIQUE,
SYSTEM_INDEXINFO.COLUMN_NAME,
SYSTEM_INDEXINFO.ORDINAL_POSITION,
SYSTEM_INDEXINFO.ASC_OR_DESC)
.from(SYSTEM_INDEXINFO)
.where(SYSTEM_INDEXINFO.TABLE_SCHEM.in(getInputSchemata()))
.orderBy(
SYSTEM_INDEXINFO.TABLE_SCHEM,
SYSTEM_INDEXINFO.TABLE_NAME,
SYSTEM_INDEXINFO.INDEX_NAME,
SYSTEM_INDEXINFO.ORDINAL_POSITION)
.fetchGroups(
new Field[] {
SYSTEM_INDEXINFO.TABLE_SCHEM,
SYSTEM_INDEXINFO.TABLE_NAME,
SYSTEM_INDEXINFO.INDEX_NAME,
SYSTEM_INDEXINFO.NON_UNIQUE
},
new Field[] {
SYSTEM_INDEXINFO.COLUMN_NAME,
SYSTEM_INDEXINFO.ORDINAL_POSITION,
SYSTEM_INDEXINFO.ASC_OR_DESC
});

for (Entry<Record, Result<Record>> entry : indexes.entrySet()) {
final Record index = entry.getKey();
final Result<Record> cols = entry.getValue();

final SchemaDefinition tableSchema = getSchema(index.get(SYSTEM_INDEXINFO.TABLE_SCHEM));
final String indexName = index.get(SYSTEM_INDEXINFO.INDEX_NAME);
final String tableName = index.get(SYSTEM_INDEXINFO.TABLE_NAME);
final TableDefinition table = getTable(tableSchema, tableName);
final boolean unique = !index.get(SYSTEM_INDEXINFO.NON_UNIQUE, boolean.class);

if (table != null) {
result.add(new AbstractIndexDefinition(tableSchema, indexName, table, unique) {
List<IndexColumnDefinition> indexColumns = new ArrayList<IndexColumnDefinition>();

{
for (Record column : cols) {
indexColumns.add(new DefaultIndexColumnDefinition(
this,
table.getColumn(column.get(SYSTEM_INDEXINFO.COLUMN_NAME)),
"D".equals(column.get(SYSTEM_INDEXINFO.ASC_OR_DESC)) ? SortOrder.DESC : SortOrder.ASC,
column.get(SYSTEM_INDEXINFO.ORDINAL_POSITION, int.class)
));
}
}

@Override
protected List<IndexColumnDefinition> getIndexColumns0() {
return indexColumns;
}
});
}
}

return result;
}

@Override @Override
protected void loadPrimaryKeys(DefaultRelations relations) throws SQLException { protected void loadPrimaryKeys(DefaultRelations relations) throws SQLException {
for (Record record : fetchKeys("PRIMARY KEY")) { for (Record record : fetchKeys("PRIMARY KEY")) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 770016b

Please sign in to comment.