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());
}
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);
}
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;
}
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;
}
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;
}
Aggregations