Search in sources :

Example 1 with IndexColumnDefinition

use of org.jooq.meta.IndexColumnDefinition in project jOOQ by jOOQ.

the class JavaGenerator method printCreateIndex.

private void printCreateIndex(JavaWriter out, IndexDefinition index) {
    String sortFieldSeparator = "";
    StringBuilder orderFields = new StringBuilder();
    for (IndexColumnDefinition column : index.getIndexColumns()) {
        orderFields.append(sortFieldSeparator);
        orderFields.append(out.ref(getStrategy().getFullJavaIdentifier(column.getColumn()), colRefSegments(null)));
        orderFields.append(column.getSortOrder() == DESC ? ".desc()" : "");
        sortFieldSeparator = ", ";
    }
    if (scala)
        out.print("%s.createIndex(%s.name(\"%s\"), %s, Array[%s [_] ](%s), %s)", Internal.class, DSL.class, escapeString(index.getOutputName()), out.ref(getStrategy().getFullJavaIdentifier(index.getTable()), 2), OrderField.class, orderFields, index.isUnique());
    else if (kotlin)
        out.print("%s.createIndex(%s.name(\"%s\"), %s, arrayOf(%s), %s)", Internal.class, DSL.class, escapeString(index.getOutputName()), out.ref(getStrategy().getFullJavaIdentifier(index.getTable()), 2), orderFields, index.isUnique());
    else
        out.print("%s.createIndex(%s.name(\"%s\"), %s, new %s[] { %s }, %s)", Internal.class, DSL.class, escapeString(index.getOutputName()), out.ref(getStrategy().getFullJavaIdentifier(index.getTable()), 2), OrderField.class, orderFields, index.isUnique());
}
Also used : IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) Internal(org.jooq.impl.Internal) DSL(org.jooq.impl.DSL) OrderField(org.jooq.OrderField)

Example 2 with IndexColumnDefinition

use of org.jooq.meta.IndexColumnDefinition in project jOOQ by jOOQ.

the class JavaGenerator method printTableJPAAnnotation.

protected void printTableJPAAnnotation(JavaWriter out, TableDefinition table) {
    SchemaDefinition schema = table.getSchema();
    int indent = out.indent();
    if (generateJPAAnnotations()) {
        // Since JPA 1.0
        out.println("@%s", out.ref("jakarta.persistence.Entity"));
        // Since JPA 1.0
        out.println("@%s(", out.ref("jakarta.persistence.Table"));
        out.print("name = \"", out.ref("jakarta.persistence.Table"));
        out.print(escapeString(table.getName()));
        out.print("\"");
        if (!schema.isDefaultSchema()) {
            out.println(",");
            out.print("schema = \"");
            out.print(escapeString(schema.getOutputName()));
            out.print("\"");
        }
        List<UniqueKeyDefinition> keys = table.getUniqueKeys();
        if (!keys.isEmpty()) {
            out.println(",");
            out.print("uniqueConstraints = ");
            out.println(scala ? "Array(" : kotlin ? "[" : "{");
            for (int i = 0; i < keys.size(); i++) {
                UniqueKeyDefinition uk = keys.get(i);
                out.print(scala ? "new " : kotlin ? "" : "@").print(out.ref("jakarta.persistence.UniqueConstraint")).print("(");
                if (!StringUtils.isBlank(uk.getOutputName()))
                    out.print("name = \"" + escapeString(uk.getOutputName()) + "\", ");
                out.print("columnNames = ").print(scala ? "Array(" : kotlin ? "[ " : "{ ");
                List<ColumnDefinition> columns = uk.getKeyColumns();
                for (int j = 0; j < columns.size(); j++) {
                    out.print(j > 0 ? ", " : "");
                    out.print("\"");
                    out.print(escapeString(columns.get(j).getName()));
                    out.print("\"");
                }
                out.print(scala ? ")" : kotlin ? " ]" : " }").print(")").println(i < keys.size() - 1 ? "," : "");
            }
            out.print(scala ? ")" : kotlin ? "]" : "}");
        }
        if (StringUtils.isBlank(generateJPAVersion()) || "2.1".compareTo(generateJPAVersion()) <= 0) {
            List<IndexDefinition> indexes = table.getIndexes();
            if (!indexes.isEmpty()) {
                out.println(",");
                out.print("indexes = ").println(scala ? "Array(" : kotlin ? "[" : "{");
                for (int i = 0; i < indexes.size(); i++) {
                    IndexDefinition index = indexes.get(i);
                    out.print(scala ? "new " : kotlin ? "" : "@");
                    out.print(out.ref("jakarta.persistence.Index"));
                    out.print("(name = \"").print(escapeString(index.getOutputName())).print("\"");
                    if (index.isUnique())
                        out.print(", unique = true");
                    out.print(", columnList = \"");
                    List<IndexColumnDefinition> columns = index.getIndexColumns();
                    for (int j = 0; j < columns.size(); j++) {
                        IndexColumnDefinition column = columns.get(j);
                        if (j > 0)
                            out.print(", ");
                        out.print(escapeString(column.getOutputName()));
                        if (column.getSortOrder() == SortOrder.ASC)
                            out.print(" ASC");
                        else if (column.getSortOrder() == SortOrder.DESC)
                            out.print(" DESC");
                    }
                    out.print("\")").println(i < indexes.size() - 1 ? "," : "");
                }
                out.print(scala ? ")" : kotlin ? "]" : "}");
            }
        }
        out.println();
        out.println(")");
    }
    // [#10196] The above logic triggers an indent level of -1, incorrectly
    out.indent(indent);
}
Also used : IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) SchemaDefinition(org.jooq.meta.SchemaDefinition) UniqueKeyDefinition(org.jooq.meta.UniqueKeyDefinition) IndexDefinition(org.jooq.meta.IndexDefinition) ColumnDefinition(org.jooq.meta.ColumnDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) EmbeddableColumnDefinition(org.jooq.meta.EmbeddableColumnDefinition)

Example 3 with IndexColumnDefinition

use of org.jooq.meta.IndexColumnDefinition in project jOOQ by jOOQ.

the class HSQLDBDatabase method getIndexes0.

@Override
protected List<IndexDefinition> getIndexes0() throws SQLException {
    List<IndexDefinition> result = new ArrayList<>();
    // 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())).and(getIncludeSystemIndexes() ? noCondition() : SYSTEM_INDEXINFO.INDEX_NAME.notLike("SYS!_IDX!_%", '!')).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 });
    indexLoop: 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));
        if (tableSchema == null)
            continue indexLoop;
        final String indexName = index.get(SYSTEM_INDEXINFO.INDEX_NAME);
        final String tableName = index.get(SYSTEM_INDEXINFO.TABLE_NAME);
        final TableDefinition table = getTable(tableSchema, tableName);
        if (table == null)
            continue indexLoop;
        final boolean unique = !index.get(SYSTEM_INDEXINFO.NON_UNIQUE, boolean.class);
        // [#6310] [#6620] Function-based indexes are not yet supported
        for (Record column : cols) if (table.getColumn(column.get(SYSTEM_INDEXINFO.COLUMN_NAME)) == null)
            continue indexLoop;
        result.add(new AbstractIndexDefinition(tableSchema, indexName, table, unique) {

            List<IndexColumnDefinition> indexColumns = new ArrayList<>();

            {
                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;
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) ArrayList(java.util.ArrayList) Result(org.jooq.Result) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) Entry(java.util.Map.Entry) AbstractIndexDefinition(org.jooq.meta.AbstractIndexDefinition) IndexDefinition(org.jooq.meta.IndexDefinition) AbstractIndexDefinition(org.jooq.meta.AbstractIndexDefinition) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record) List(java.util.List) ArrayList(java.util.ArrayList)

Example 4 with IndexColumnDefinition

use of org.jooq.meta.IndexColumnDefinition in project jOOQ by jOOQ.

the class MySQLDatabase method getIndexes0.

@Override
protected List<IndexDefinition> getIndexes0() throws SQLException {
    List<IndexDefinition> result = new ArrayList<>();
    // Same implementation as in H2Database and HSQLDBDatabase
    Map<Record, Result<Record>> indexes = create().selectDistinct(STATISTICS.TABLE_SCHEMA, STATISTICS.TABLE_NAME, STATISTICS.INDEX_NAME, STATISTICS.NON_UNIQUE, STATISTICS.COLUMN_NAME, STATISTICS.SEQ_IN_INDEX).from(STATISTICS).where(STATISTICS.TABLE_SCHEMA.in(workaroundFor5213(getInputSchemata()))).and(getIncludeSystemIndexes() ? noCondition() : row(STATISTICS.INDEX_SCHEMA, STATISTICS.TABLE_NAME, STATISTICS.INDEX_NAME).notIn(select(TABLE_CONSTRAINTS.CONSTRAINT_SCHEMA, TABLE_CONSTRAINTS.TABLE_NAME, TABLE_CONSTRAINTS.CONSTRAINT_NAME).from(TABLE_CONSTRAINTS))).orderBy(STATISTICS.TABLE_SCHEMA, STATISTICS.TABLE_NAME, STATISTICS.INDEX_NAME, STATISTICS.SEQ_IN_INDEX).fetchGroups(new Field[] { STATISTICS.TABLE_SCHEMA, STATISTICS.TABLE_NAME, STATISTICS.INDEX_NAME, STATISTICS.NON_UNIQUE }, new Field[] { STATISTICS.COLUMN_NAME, STATISTICS.SEQ_IN_INDEX });
    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(STATISTICS.TABLE_SCHEMA));
        if (tableSchema == null)
            continue indexLoop;
        final String indexName = index.get(STATISTICS.INDEX_NAME);
        final String tableName = index.get(STATISTICS.TABLE_NAME);
        final TableDefinition table = getTable(tableSchema, tableName);
        if (table == null)
            continue indexLoop;
        final boolean unique = !index.get(STATISTICS.NON_UNIQUE, boolean.class);
        // [#6310] [#6620] Function-based indexes are not yet supported
        for (Record column : columns) if (table.getColumn(column.get(STATISTICS.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(STATISTICS.COLUMN_NAME)), SortOrder.ASC, column.get(STATISTICS.SEQ_IN_INDEX, int.class)));
                }
            }

            @Override
            protected List<IndexColumnDefinition> getIndexColumns0() {
                return indexColumns;
            }
        });
    }
    return result;
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) ArrayList(java.util.ArrayList) Result(org.jooq.Result) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) Entry(java.util.Map.Entry) AbstractIndexDefinition(org.jooq.meta.AbstractIndexDefinition) IndexDefinition(org.jooq.meta.IndexDefinition) AbstractIndexDefinition(org.jooq.meta.AbstractIndexDefinition) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList)

Example 5 with IndexColumnDefinition

use of org.jooq.meta.IndexColumnDefinition in project jOOQ by jOOQ.

the class XMLDatabase method getIndexes0.

@Override
protected List<IndexDefinition> getIndexes0() throws SQLException {
    List<IndexDefinition> result = new ArrayList<>();
    final Map<Name, SortedSet<IndexColumnUsage>> indexColumnUsage = new HashMap<>();
    for (IndexColumnUsage ic : info().getIndexColumnUsages()) {
        Name name = name(ic.getIndexCatalog(), ic.getIndexSchema(), ic.getTableName(), ic.getIndexName());
        SortedSet<IndexColumnUsage> list = indexColumnUsage.computeIfAbsent(name, k -> new TreeSet<>((o1, o2) -> {
            int r = 0;
            r = defaultIfNull(o1.getIndexCatalog(), "").compareTo(defaultIfNull(o2.getIndexCatalog(), ""));
            if (r != 0)
                return r;
            r = defaultIfNull(o1.getIndexSchema(), "").compareTo(defaultIfNull(o2.getIndexSchema(), ""));
            if (r != 0)
                return r;
            r = defaultIfNull(o1.getTableName(), "").compareTo(defaultIfNull(o2.getTableName(), ""));
            if (r != 0)
                return r;
            r = defaultIfNull(o1.getIndexName(), "").compareTo(defaultIfNull(o2.getIndexName(), ""));
            if (r != 0)
                return r;
            return Integer.compare(o1.getOrdinalPosition(), o2.getOrdinalPosition());
        }));
        list.add(ic);
    }
    indexLoop: for (Index i : info().getIndexes()) {
        if (getInputSchemata().contains(i.getTableSchema())) {
            final SchemaDefinition schema = getSchema(i.getTableSchema());
            final TableDefinition table = getTable(schema, i.getTableName());
            if (table == null)
                continue indexLoop;
            final Name name = name(i.getIndexCatalog(), i.getIndexSchema(), i.getTableName(), i.getIndexName());
            IndexDefinition index = new AbstractIndexDefinition(schema, i.getIndexName(), table, Boolean.TRUE.equals(i.isIsUnique()), i.getComment()) {

                private final List<IndexColumnDefinition> indexColumns;

                {
                    indexColumns = new ArrayList<>();
                    SortedSet<IndexColumnUsage> list = indexColumnUsage.get(name);
                    if (list != null)
                        for (IndexColumnUsage ic : list) {
                            ColumnDefinition column = table.getColumn(ic.getColumnName());
                            if (column != null)
                                indexColumns.add(new DefaultIndexColumnDefinition(this, column, Boolean.TRUE.equals(ic.isIsDescending()) ? SortOrder.DESC : SortOrder.ASC, ic.getOrdinalPosition()));
                            else
                                log.error(String.format("Column %s not found in table %s.", ic.getColumnName(), table));
                        }
                    else
                        log.error(String.format("No columns found for index %s.", name));
                }

                @Override
                protected List<IndexColumnDefinition> getIndexColumns0() {
                    return indexColumns;
                }
            };
            result.add(index);
        }
    }
    return result;
}
Also used : Transformer(javax.xml.transform.Transformer) DSL(org.jooq.impl.DSL) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) SortedSet(java.util.SortedSet) StringUtils.isBlank(org.jooq.tools.StringUtils.isBlank) XMLInputFactory(javax.xml.stream.XMLInputFactory) StreamResult(javax.xml.transform.stream.StreamResult) MiniJAXB(org.jooq.util.jaxb.tools.MiniJAXB) DefaultSequenceDefinition(org.jooq.meta.DefaultSequenceDefinition) Table(org.jooq.util.xml.jaxb.Table) DataTypeDefinition(org.jooq.meta.DataTypeDefinition) ColumnDefinition(org.jooq.meta.ColumnDefinition) RoutineDefinition(org.jooq.meta.RoutineDefinition) PackageDefinition(org.jooq.meta.PackageDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) IOException(org.jooq.exception.IOException) InformationSchema(org.jooq.util.xml.jaxb.InformationSchema) XMLStreamReader(javax.xml.stream.XMLStreamReader) Map(java.util.Map) XMLStreamException(javax.xml.stream.XMLStreamException) DSLContext(org.jooq.DSLContext) SQLDialect(org.jooq.SQLDialect) SortOrder(org.jooq.SortOrder) DSL.name(org.jooq.impl.DSL.name) JooqLogger(org.jooq.tools.JooqLogger) Routine(org.jooq.util.xml.jaxb.Routine) View(org.jooq.util.xml.jaxb.View) Name(org.jooq.Name) CheckConstraint(org.jooq.util.xml.jaxb.CheckConstraint) Set(java.util.Set) Constants(org.jooq.Constants) UNIQUE(org.jooq.util.xml.jaxb.TableConstraintType.UNIQUE) Reader(java.io.Reader) Schema(org.jooq.util.xml.jaxb.Schema) TableConstraintType(org.jooq.util.xml.jaxb.TableConstraintType) PRIMARY_KEY(org.jooq.util.xml.jaxb.TableConstraintType.PRIMARY_KEY) JDBCUtils(org.jooq.tools.jdbc.JDBCUtils) FilePattern(org.jooq.FilePattern) TableType(org.jooq.TableOptions.TableType) DomainDefinition(org.jooq.meta.DomainDefinition) List(java.util.List) StringUtils.defaultIfNull(org.jooq.tools.StringUtils.defaultIfNull) IndexColumnUsage(org.jooq.util.xml.jaxb.IndexColumnUsage) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DefaultCheckConstraintDefinition(org.jooq.meta.DefaultCheckConstraintDefinition) AbstractIndexDefinition(org.jooq.meta.AbstractIndexDefinition) DefaultRelations(org.jooq.meta.DefaultRelations) SequenceDefinition(org.jooq.meta.SequenceDefinition) TransformerException(javax.xml.transform.TransformerException) StreamSource(javax.xml.transform.stream.StreamSource) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) TableDefinition(org.jooq.meta.TableDefinition) HashMap(java.util.HashMap) Sequence(org.jooq.util.xml.jaxb.Sequence) IndexDefinition(org.jooq.meta.IndexDefinition) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) SQLException(java.sql.SQLException) ArrayDefinition(org.jooq.meta.ArrayDefinition) UDTDefinition(org.jooq.meta.UDTDefinition) KeyColumnUsage(org.jooq.util.xml.jaxb.KeyColumnUsage) FALSE(java.lang.Boolean.FALSE) StringUtils.defaultIfBlank(org.jooq.tools.StringUtils.defaultIfBlank) ReferentialConstraint(org.jooq.util.xml.jaxb.ReferentialConstraint) StringWriter(java.io.StringWriter) FileInputStream(java.io.FileInputStream) StringUtils(org.jooq.tools.StringUtils) File(java.io.File) Index(org.jooq.util.xml.jaxb.Index) Sort(org.jooq.FilePattern.Sort) StringReader(java.io.StringReader) SchemaDefinition(org.jooq.meta.SchemaDefinition) TableConstraint(org.jooq.util.xml.jaxb.TableConstraint) EnumDefinition(org.jooq.meta.EnumDefinition) TransformerFactory(javax.xml.transform.TransformerFactory) Collections(java.util.Collections) AbstractDatabase(org.jooq.meta.AbstractDatabase) CatalogDefinition(org.jooq.meta.CatalogDefinition) InputStream(java.io.InputStream) SchemaDefinition(org.jooq.meta.SchemaDefinition) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Index(org.jooq.util.xml.jaxb.Index) SortedSet(java.util.SortedSet) IndexColumnUsage(org.jooq.util.xml.jaxb.IndexColumnUsage) Name(org.jooq.Name) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) ColumnDefinition(org.jooq.meta.ColumnDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) AbstractIndexDefinition(org.jooq.meta.AbstractIndexDefinition) IndexDefinition(org.jooq.meta.IndexDefinition) AbstractIndexDefinition(org.jooq.meta.AbstractIndexDefinition) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) TableDefinition(org.jooq.meta.TableDefinition) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

IndexColumnDefinition (org.jooq.meta.IndexColumnDefinition)10 IndexDefinition (org.jooq.meta.IndexDefinition)9 SchemaDefinition (org.jooq.meta.SchemaDefinition)9 TableDefinition (org.jooq.meta.TableDefinition)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 AbstractIndexDefinition (org.jooq.meta.AbstractIndexDefinition)7 DefaultIndexColumnDefinition (org.jooq.meta.DefaultIndexColumnDefinition)7 Entry (java.util.Map.Entry)5 Record (org.jooq.Record)5 Result (org.jooq.Result)5 Arrays.asList (java.util.Arrays.asList)4 ColumnDefinition (org.jooq.meta.ColumnDefinition)4 StringWriter (java.io.StringWriter)2 BigInteger (java.math.BigInteger)2 Collectors.toList (java.util.stream.Collectors.toList)2 DSL (org.jooq.impl.DSL)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1