use of org.jooq.meta.IndexDefinition in project jOOQ by jOOQ.
the class PostgresDatabase method getIndexes0.
@Override
protected List<IndexDefinition> getIndexes0() throws SQLException {
List<IndexDefinition> result = new ArrayList<>();
PgIndex i = PG_INDEX.as("i");
PgClass trel = PG_CLASS.as("trel");
PgConstraint c = PG_CONSTRAINT.as("c");
indexLoop: for (Record6<String, String, String, Boolean, String[], Integer[]> record : create().select(trel.pgNamespace().NSPNAME, trel.RELNAME, i.indexClass().RELNAME, i.INDISUNIQUE, array(select(field("pg_get_indexdef({0}, k + 1, true)", String.class, i.INDEXRELID)).from("generate_subscripts({0}, 1) as k", i.INDKEY).orderBy(field("k"))).as("columns"), field("{0}::int[]", Integer[].class, i.INDOPTION).as("asc_or_desc")).from(i).join(trel).on(trel.OID.eq(i.INDRELID)).where(trel.pgNamespace().NSPNAME.in(getInputSchemata())).and(getIncludeSystemIndexes() ? noCondition() : row(trel.pgNamespace().NSPNAME, i.indexClass().RELNAME).notIn(select(c.pgNamespace().NSPNAME, c.CONNAME).from(c))).orderBy(1, 2, 3)) {
final SchemaDefinition tableSchema = getSchema(record.get(trel.pgNamespace().NSPNAME));
if (tableSchema == null)
continue indexLoop;
final String indexName = record.get(i.indexClass().RELNAME);
final String tableName = record.get(trel.RELNAME);
final String[] columns = record.value5();
final Integer[] options = record.value6();
final TableDefinition table = getTable(tableSchema, tableName);
if (table == null)
continue indexLoop;
final boolean unique = record.get(i.INDISUNIQUE);
for (int k = 0; k < columns.length; k++) // the column expression, because it might be quoted
if (table.getColumn(columns[k]) == null && table.getColumn(columns[k] = tryParseColumnName(columns[k])) == null)
continue indexLoop;
else // columns without options
if (k >= options.length)
continue indexLoop;
result.add(new AbstractIndexDefinition(tableSchema, indexName, table, unique) {
List<IndexColumnDefinition> indexColumns = new ArrayList<>();
{
for (int ordinal = 0; ordinal < columns.length; ordinal++) {
ColumnDefinition column = table.getColumn(columns[ordinal]);
// [#6307] Some background info on this bitwise operation here:
// https://stackoverflow.com/a/18128104/521799
SortOrder order = (options[ordinal] & 1) == 1 ? SortOrder.DESC : SortOrder.ASC;
indexColumns.add(new DefaultIndexColumnDefinition(this, column, order, ordinal + 1));
}
}
@Override
protected List<IndexColumnDefinition> getIndexColumns0() {
return indexColumns;
}
});
}
return result;
}
use of org.jooq.meta.IndexDefinition in project jOOQ by jOOQ.
the class FirebirdDatabase method getIndexes0.
@Override
protected List<IndexDefinition> getIndexes0() throws SQLException {
final List<IndexDefinition> result = new ArrayList<>();
final Rdb$relationConstraints c = RDB$RELATION_CONSTRAINTS.as("c");
final Rdb$indices i = RDB$INDICES.as("i");
final Rdb$indexSegments s = RDB$INDEX_SEGMENTS.as("s");
Map<Record, Result<Record>> indexes = create().select(s.rdb$indices().RDB$RELATION_NAME.trim().as(i.RDB$RELATION_NAME), s.rdb$indices().RDB$INDEX_NAME.trim().as(i.RDB$INDEX_NAME), s.rdb$indices().RDB$UNIQUE_FLAG, s.RDB$FIELD_NAME.trim().as(s.RDB$FIELD_NAME), s.RDB$FIELD_POSITION).from(s).where(s.rdb$indices().RDB$INDEX_NAME.notIn(select(c.RDB$CONSTRAINT_NAME).from(c))).orderBy(s.rdb$indices().RDB$RELATION_NAME, s.rdb$indices().RDB$INDEX_NAME, s.RDB$FIELD_POSITION).fetchGroups(new Field[] { i.RDB$RELATION_NAME, i.RDB$INDEX_NAME, i.RDB$UNIQUE_FLAG }, new Field[] { s.RDB$FIELD_NAME, s.RDB$FIELD_POSITION });
indexLoop: for (Entry<Record, Result<Record>> entry : indexes.entrySet()) {
final Record index = entry.getKey();
final Result<Record> columns = entry.getValue();
final SchemaDefinition schema = getSchemata().get(0);
final String indexName = index.get(i.RDB$INDEX_NAME);
final String tableName = index.get(i.RDB$RELATION_NAME);
final TableDefinition table = getTable(schema, tableName);
if (table == null)
continue indexLoop;
final boolean unique = index.get(i.RDB$UNIQUE_FLAG, boolean.class);
// [#6310] [#6620] Function-based indexes are not yet supported
for (Record column : columns) if (table.getColumn(column.get(s.RDB$FIELD_NAME)) == null)
continue indexLoop;
result.add(new AbstractIndexDefinition(schema, indexName, table, unique) {
List<IndexColumnDefinition> indexColumns = new ArrayList<>();
{
for (Record column : columns) {
indexColumns.add(new DefaultIndexColumnDefinition(this, table.getColumn(column.get(s.RDB$FIELD_NAME)), SortOrder.ASC, column.get(s.RDB$FIELD_POSITION, int.class)));
}
}
@Override
protected List<IndexColumnDefinition> getIndexColumns0() {
return indexColumns;
}
});
}
return result;
}
use of org.jooq.meta.IndexDefinition in project jOOQ by jOOQ.
the class H2Database method getIndexes0.
@Override
protected List<IndexDefinition> getIndexes0() throws SQLException {
List<IndexDefinition> result = new ArrayList<>();
// Same implementation as in HSQLDBDatabase and MySQLDatabase
Map<Record, Result<Record>> indexes = indexes().fetchGroups(new Field[] { INDEXES.TABLE_SCHEMA, INDEXES.TABLE_NAME, INDEXES.INDEX_NAME, INDEXES.NON_UNIQUE }, new Field[] { INDEXES.COLUMN_NAME, INDEXES.ORDINAL_POSITION });
indexLoop: for (Entry<Record, Result<Record>> entry : indexes.entrySet()) {
final Record index = entry.getKey();
final Result<Record> columns = entry.getValue();
final SchemaDefinition tableSchema = getSchema(index.get(INDEXES.TABLE_SCHEMA));
if (tableSchema == null)
continue indexLoop;
final String indexName = index.get(INDEXES.INDEX_NAME);
final String tableName = index.get(INDEXES.TABLE_NAME);
final TableDefinition table = getTable(tableSchema, tableName);
if (table == null)
continue indexLoop;
final boolean unique = !index.get(INDEXES.NON_UNIQUE, boolean.class);
// [#6310] [#6620] Function-based indexes are not yet supported
for (Record column : columns) if (table.getColumn(column.get(INDEXES.COLUMN_NAME)) == null)
continue indexLoop;
result.add(new AbstractIndexDefinition(tableSchema, indexName, table, unique) {
List<IndexColumnDefinition> indexColumns = new ArrayList<>();
{
for (Record column : columns) {
indexColumns.add(new DefaultIndexColumnDefinition(this, table.getColumn(column.get(INDEXES.COLUMN_NAME)), SortOrder.ASC, column.get(INDEXES.ORDINAL_POSITION, int.class)));
}
}
@Override
protected List<IndexColumnDefinition> getIndexColumns0() {
return indexColumns;
}
});
}
return result;
}
use of org.jooq.meta.IndexDefinition in project jOOQ by jOOQ.
the class SQLiteDatabase method getIndexes0.
@Override
protected List<IndexDefinition> getIndexes0() throws SQLException {
final List<IndexDefinition> result = new ArrayList<>();
final Field<String> fIndexName = field("il.name", String.class).as("index_name");
final Field<Boolean> fUnique = field("il.\"unique\"", boolean.class).as("unique");
final Field<Integer> fSeqno = field("ii.seqno", int.class).add(one()).as("seqno");
final Field<String> fColumnName = field("ii.name", String.class).as("column_name");
Map<Record, Result<Record>> indexes = create().select(SQLiteMaster.NAME, fIndexName, fUnique, fSeqno, fColumnName).from(SQLITE_MASTER, table("pragma_index_list({0})", SQLiteMaster.NAME).as("il"), table("pragma_index_info(il.name)").as("ii")).where(SQLiteMaster.TYPE.eq(inline("table"))).and(getIncludeSystemIndexes() ? noCondition() : field("il.origin", VARCHAR).notIn(inline("pk"), inline("u"))).orderBy(1, 2, 4).fetchGroups(new Field[] { SQLiteMaster.NAME, fIndexName, fUnique }, new Field[] { fColumnName, fSeqno });
indexLoop: for (Entry<Record, Result<Record>> entry : indexes.entrySet()) {
final Record index = entry.getKey();
final Result<Record> columns = entry.getValue();
final SchemaDefinition tableSchema = getSchemata().get(0);
if (tableSchema == null)
continue indexLoop;
final String indexName = index.get(fIndexName);
final String tableName = index.get(SQLiteMaster.NAME);
final TableDefinition table = getTable(tableSchema, tableName);
if (table == null)
continue indexLoop;
final boolean unique = index.get(fUnique);
// [#6310] [#6620] Function-based indexes are not yet supported
for (Record column : columns) if (table.getColumn(column.get(fColumnName)) == null)
continue indexLoop;
result.add(new AbstractIndexDefinition(tableSchema, indexName, table, unique) {
List<IndexColumnDefinition> indexColumns = new ArrayList<>();
{
for (Record column : columns) {
indexColumns.add(new DefaultIndexColumnDefinition(this, table.getColumn(column.get(fColumnName)), SortOrder.ASC, column.get(fSeqno, int.class)));
}
}
@Override
protected List<IndexColumnDefinition> getIndexColumns0() {
return indexColumns;
}
});
}
return result;
}
use of org.jooq.meta.IndexDefinition in project jOOQ by jOOQ.
the class JavaGenerator method generateIndexes.
protected void generateIndexes(SchemaDefinition schema) {
log.info("Generating Indexes");
if (database.getIndexes(schema).isEmpty()) {
log.info("Skipping empty indexes");
return;
}
JavaWriter out = newJavaWriter(getStrategy().getGlobalReferencesFile(schema, IndexDefinition.class));
out.refConflicts(getStrategy().getJavaIdentifiers(database.getIndexes(schema)));
printGlobalReferencesPackage(out, schema, IndexDefinition.class);
if (!kotlin) {
printClassJavadoc(out, "A class modelling indexes of tables in " + schemaNameOrDefault(schema) + ".");
printClassAnnotations(out, schema, Mode.DEFAULT);
}
final String referencesClassName = getStrategy().getGlobalReferencesJavaClassName(schema, IndexDefinition.class);
if (scala)
out.println("%sobject %s {", visibility(), referencesClassName);
else if (kotlin) {
} else
out.println("%sclass %s {", visibility(), referencesClassName);
// [#1459] [#10554] [#10653] Distribute keys to nested classes only if necessary
boolean distributeIndexes = database.getIndexes(schema).size() > maxMembersPerInitialiser();
List<IndexDefinition> allIndexes = new ArrayList<>();
out.header("INDEX definitions");
out.println();
for (IndexDefinition index : database.getIndexes(schema)) {
try {
final String keyId = getStrategy().getJavaIdentifier(index);
final int block = allIndexes.size() / maxMembersPerInitialiser();
if (distributeIndexes)
if (scala)
out.println("%sval %s = Indexes%s.%s", visibility(), keyId, block, keyId);
else if (kotlin)
out.println("%sval %s: %s = Indexes%s.%s", visibility(), keyId, Index.class, block, keyId);
else
out.println("%sstatic final %s %s = Indexes%s.%s;", visibility(), Index.class, keyId, block, keyId);
else
printIndex(out, -1, index, distributeIndexes);
allIndexes.add(index);
} catch (Exception e) {
log.error("Error while generating index " + index, e);
}
}
// [#1459] [#10554] [#10653] Print nested classes for actual static field initialisations
// keeping top-level initialiser small
int indexCounter = 0;
if (distributeIndexes) {
out.header("[#1459] [#10554] [#10653] distribute members to avoid static initialisers > 64kb");
for (IndexDefinition index : allIndexes) {
printIndex(out, indexCounter, index, distributeIndexes);
indexCounter++;
}
if (indexCounter > 0)
out.println("}");
}
generateIndexesClassFooter(schema, out);
if (!kotlin)
out.println("}");
closeJavaWriter(out);
watch.splitInfo("Indexes generated");
}
Aggregations