Search in sources :

Example 1 with PgIndex

use of org.jooq.meta.postgres.pg_catalog.tables.PgIndex 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;
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) ArrayList(java.util.ArrayList) SortOrder(org.jooq.SortOrder) PgConstraint(org.jooq.meta.postgres.pg_catalog.tables.PgConstraint) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) ColumnDefinition(org.jooq.meta.ColumnDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) BigInteger(java.math.BigInteger) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) AbstractIndexDefinition(org.jooq.meta.AbstractIndexDefinition) IndexDefinition(org.jooq.meta.IndexDefinition) PgConstraint(org.jooq.meta.postgres.pg_catalog.tables.PgConstraint) PgIndex(org.jooq.meta.postgres.pg_catalog.tables.PgIndex) AbstractIndexDefinition(org.jooq.meta.AbstractIndexDefinition) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) PgClass(org.jooq.meta.postgres.pg_catalog.tables.PgClass) TableDefinition(org.jooq.meta.TableDefinition) Record6(org.jooq.Record6) Records.intoList(org.jooq.Records.intoList) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList)

Aggregations

BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 List (java.util.List)1 Collectors.toList (java.util.stream.Collectors.toList)1 Record6 (org.jooq.Record6)1 Records.intoList (org.jooq.Records.intoList)1 SortOrder (org.jooq.SortOrder)1 AbstractIndexDefinition (org.jooq.meta.AbstractIndexDefinition)1 ColumnDefinition (org.jooq.meta.ColumnDefinition)1 DefaultIndexColumnDefinition (org.jooq.meta.DefaultIndexColumnDefinition)1 IndexColumnDefinition (org.jooq.meta.IndexColumnDefinition)1 IndexDefinition (org.jooq.meta.IndexDefinition)1 SchemaDefinition (org.jooq.meta.SchemaDefinition)1 TableDefinition (org.jooq.meta.TableDefinition)1 PgClass (org.jooq.meta.postgres.pg_catalog.tables.PgClass)1 PgConstraint (org.jooq.meta.postgres.pg_catalog.tables.PgConstraint)1 PgIndex (org.jooq.meta.postgres.pg_catalog.tables.PgIndex)1