use of org.jooq.meta.IndexColumnDefinition 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.IndexColumnDefinition 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.IndexColumnDefinition 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.IndexColumnDefinition 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.IndexColumnDefinition in project jOOQ by jOOQ.
the class XMLGenerator method generate0.
@Override
public void generate0(Database db) {
logDatabaseParameters(db);
log.info("");
logGenerationRemarks(db);
log.info("");
log.info("----------------------------------------------------------");
TextWriter out = new TextWriter(getStrategy().getFile("information_schema.xml"), targetEncoding);
log.info("");
log.info("Generating XML", out.file().getName());
log.info("==========================================================");
InformationSchema is = new InformationSchema();
boolean hasNonDefaultCatalogs = false;
for (CatalogDefinition c : db.getCatalogs()) {
if (!StringUtils.isBlank(c.getName())) {
hasNonDefaultCatalogs = true;
break;
}
}
for (CatalogDefinition c : db.getCatalogs()) {
String catalogName = c.getOutputName();
if (hasNonDefaultCatalogs)
is.getCatalogs().add(new Catalog().withCatalogName(catalogName).withComment(generateCommentsOnCatalogs() ? c.getComment() : null));
for (SchemaDefinition s : c.getSchemata()) {
String schemaName = s.getOutputName();
Schema schema = new Schema();
schema.setCatalogName(catalogName);
schema.setSchemaName(schemaName);
if (generateCommentsOnSchemas())
schema.setComment(s.getComment());
is.getSchemata().add(schema);
for (TableDefinition t : s.getTables()) {
String tableName = t.getOutputName();
Table table = new Table();
table.setTableCatalog(catalogName);
table.setTableSchema(schemaName);
table.setTableName(tableName);
table.setTableType(t.isView() ? TableType.VIEW : t.isTemporary() ? TableType.GLOBAL_TEMPORARY : TableType.BASE_TABLE);
if (generateCommentsOnTables())
table.setComment(t.getComment());
is.getTables().add(table);
if (t.isView()) {
View view = new View();
view.setTableCatalog(catalogName);
view.setTableSchema(schemaName);
view.setTableName(tableName);
if (generateSourcesOnViews())
view.setViewDefinition(t.getSource());
is.getViews().add(view);
}
for (ColumnDefinition co : t.getColumns()) {
String columnName = co.getOutputName();
DataTypeDefinition type = co.getType();
Column column = new Column();
column.setTableCatalog(catalogName);
column.setTableSchema(schemaName);
column.setTableName(tableName);
column.setColumnName(columnName);
if (generateCommentsOnColumns())
column.setComment(co.getComment());
column.setCharacterMaximumLength(type.getLength());
column.setColumnDefault(type.getDefaultValue());
column.setDataType(type.getType());
if (co.isIdentity())
column.setIdentityGeneration("YES");
column.setIsNullable(type.isNullable());
column.setNumericPrecision(type.getPrecision());
column.setNumericScale(type.getScale());
column.setOrdinalPosition(co.getPosition());
column.setReadonly(co.isReadonly());
if (type.isComputed()) {
column.setIsGenerated(type.isComputed());
column.setGenerationExpression(type.getGeneratedAlwaysAs());
column.setGenerationOption(type.getGenerationOption() == VIRTUAL ? "VIRTUAL" : type.getGenerationOption() == STORED ? "STORED" : null);
}
is.getColumns().add(column);
}
}
for (IndexDefinition i : db.getIndexes(s)) {
String indexName = i.getOutputName();
TableDefinition table = i.getTable();
List<IndexColumnDefinition> columns = i.getIndexColumns();
Index index = new Index();
index.setIndexCatalog(catalogName);
index.setIndexSchema(schemaName);
index.setIndexName(indexName);
if (generateCommentsOnKeys())
index.setComment(i.getComment());
index.setTableCatalog(table.getCatalog().getOutputName());
index.setTableSchema(table.getSchema().getOutputName());
index.setTableName(table.getOutputName());
index.setIsUnique(i.isUnique());
is.getIndexes().add(index);
for (int j = 0; j < columns.size(); j++) {
IndexColumnDefinition indexColumn = columns.get(j);
ColumnDefinition column = indexColumn.getColumn();
IndexColumnUsage ic = new IndexColumnUsage();
ic.setIndexCatalog(catalogName);
ic.setIndexSchema(schemaName);
ic.setIndexName(indexName);
ic.setColumnName(column.getOutputName());
ic.setOrdinalPosition(j + 1);
ic.setIsDescending(indexColumn.getSortOrder() == SortOrder.DESC);
ic.setTableCatalog(table.getCatalog().getOutputName());
ic.setTableSchema(table.getSchema().getOutputName());
ic.setTableName(table.getOutputName());
is.getIndexColumnUsages().add(ic);
}
}
for (UniqueKeyDefinition u : db.getKeys(s)) {
String constraintName = u.getOutputName();
TableDefinition table = u.getTable();
List<ColumnDefinition> columns = u.getKeyColumns();
TableConstraint tc = new TableConstraint();
tc.setConstraintCatalog(catalogName);
tc.setConstraintSchema(schemaName);
tc.setConstraintName(constraintName);
tc.setConstraintType(u.isPrimaryKey() ? PRIMARY_KEY : UNIQUE);
if (generateCommentsOnKeys())
tc.setComment(u.getComment());
tc.setTableCatalog(table.getCatalog().getOutputName());
tc.setTableSchema(table.getSchema().getOutputName());
tc.setTableName(table.getOutputName());
tc.setEnforced(u.enforced());
is.getTableConstraints().add(tc);
for (int i = 0; i < columns.size(); i++) {
ColumnDefinition column = columns.get(i);
KeyColumnUsage kc = new KeyColumnUsage();
kc.setConstraintCatalog(catalogName);
kc.setConstraintSchema(schemaName);
kc.setConstraintName(constraintName);
kc.setColumnName(column.getOutputName());
kc.setOrdinalPosition(i + 1);
kc.setTableCatalog(table.getCatalog().getOutputName());
kc.setTableSchema(table.getSchema().getOutputName());
kc.setTableName(table.getOutputName());
is.getKeyColumnUsages().add(kc);
}
}
for (ForeignKeyDefinition f : db.getForeignKeys(s)) {
String constraintName = f.getOutputName();
UniqueKeyDefinition referenced = f.getReferencedKey();
TableDefinition table = f.getKeyTable();
List<ColumnDefinition> columns = f.getKeyColumns();
TableConstraint tc = new TableConstraint();
tc.setConstraintCatalog(catalogName);
tc.setConstraintSchema(schemaName);
tc.setConstraintName(constraintName);
tc.setConstraintType(FOREIGN_KEY);
if (generateCommentsOnKeys())
tc.setComment(f.getComment());
tc.setTableCatalog(table.getCatalog().getOutputName());
tc.setTableSchema(table.getSchema().getOutputName());
tc.setTableName(table.getOutputName());
tc.setEnforced(f.enforced());
ReferentialConstraint rc = new ReferentialConstraint();
rc.setConstraintCatalog(catalogName);
rc.setConstraintSchema(schemaName);
rc.setConstraintName(constraintName);
rc.setUniqueConstraintCatalog(referenced.getCatalog().getOutputName());
rc.setUniqueConstraintSchema(referenced.getSchema().getOutputName());
rc.setUniqueConstraintName(referenced.getOutputName());
is.getTableConstraints().add(tc);
is.getReferentialConstraints().add(rc);
for (int i = 0; i < columns.size(); i++) {
ColumnDefinition column = columns.get(i);
KeyColumnUsage kc = new KeyColumnUsage();
kc.setConstraintCatalog(catalogName);
kc.setConstraintSchema(schemaName);
kc.setConstraintName(constraintName);
kc.setColumnName(column.getOutputName());
kc.setOrdinalPosition(i + 1);
kc.setTableCatalog(table.getCatalog().getOutputName());
kc.setTableSchema(table.getSchema().getOutputName());
kc.setTableName(table.getOutputName());
is.getKeyColumnUsages().add(kc);
}
}
for (CheckConstraintDefinition ch : db.getCheckConstraints(s)) {
String constraintName = ch.getOutputName();
TableDefinition table = ch.getTable();
TableConstraint tc = new TableConstraint();
tc.setConstraintCatalog(catalogName);
tc.setConstraintSchema(schemaName);
tc.setConstraintName(constraintName);
tc.setConstraintType(CHECK);
if (generateCommentsOnKeys())
tc.setComment(ch.getComment());
tc.setTableCatalog(table.getCatalog().getOutputName());
tc.setTableSchema(table.getSchema().getOutputName());
tc.setTableName(table.getOutputName());
tc.setEnforced(ch.enforced());
is.getTableConstraints().add(tc);
CheckConstraint cc = new CheckConstraint();
cc.setConstraintCatalog(catalogName);
cc.setConstraintSchema(schemaName);
cc.setConstraintName(constraintName);
cc.setCheckClause(ch.getCheckClause());
is.getCheckConstraints().add(cc);
}
for (SequenceDefinition se : db.getSequences(s)) {
String sequenceName = se.getOutputName();
DataTypeDefinition type = se.getType();
Sequence sequence = new Sequence();
sequence.setSequenceCatalog(catalogName);
sequence.setSequenceSchema(schemaName);
sequence.setSequenceName(sequenceName);
if (generateCommentsOnSequences())
sequence.setComment(se.getComment());
sequence.setCharacterMaximumLength(type.getLength());
sequence.setDataType(type.getType());
sequence.setNumericPrecision(type.getPrecision());
sequence.setNumericScale(type.getScale());
sequence.setStartValue(Convert.convert(se.getStartWith(), BigInteger.class));
sequence.setIncrement(Convert.convert(se.getIncrementBy(), BigInteger.class));
sequence.setMinimumValue(Convert.convert(se.getMinvalue(), BigInteger.class));
sequence.setMaximumValue(Convert.convert(se.getMaxvalue(), BigInteger.class));
sequence.setCycleOption(se.getCycle());
sequence.setCache(Convert.convert(se.getCache(), BigInteger.class));
is.getSequences().add(sequence);
}
for (PackageDefinition pkg : db.getPackages(s)) for (RoutineDefinition r : pkg.getRoutines()) exportRoutine(is, r, catalogName, schemaName);
for (RoutineDefinition r : db.getRoutines(s)) exportRoutine(is, r, catalogName, schemaName);
}
}
StringWriter writer = new StringWriter();
MiniJAXB.marshal(is, writer);
out.print(writer.toString());
out.close();
}
Aggregations