use of org.jooq.meta.SchemaDefinition in project jOOQ by jOOQ.
the class H2Database method loadForeignKeys2_0.
private void loadForeignKeys2_0(DefaultRelations relations) {
KeyColumnUsage fkKcu = KEY_COLUMN_USAGE.as("fk_kcu");
KeyColumnUsage pkKcu = KEY_COLUMN_USAGE.as("pk_kcu");
for (Record record : create().select(REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_NAME, REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_SCHEMA, TABLE_CONSTRAINTS.TABLE_NAME, fkKcu.CONSTRAINT_NAME, fkKcu.TABLE_SCHEMA, fkKcu.TABLE_NAME, fkKcu.COLUMN_NAME, pkKcu.COLUMN_NAME).from(REFERENTIAL_CONSTRAINTS).join(fkKcu).on(fkKcu.CONSTRAINT_SCHEMA.equal(REFERENTIAL_CONSTRAINTS.CONSTRAINT_SCHEMA)).and(fkKcu.CONSTRAINT_NAME.equal(REFERENTIAL_CONSTRAINTS.CONSTRAINT_NAME)).join(TABLE_CONSTRAINTS).on(TABLE_CONSTRAINTS.CONSTRAINT_SCHEMA.eq(REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_SCHEMA)).and(TABLE_CONSTRAINTS.CONSTRAINT_NAME.eq(REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_NAME)).join(pkKcu).on(pkKcu.CONSTRAINT_SCHEMA.eq(TABLE_CONSTRAINTS.CONSTRAINT_SCHEMA)).and(pkKcu.CONSTRAINT_NAME.eq(TABLE_CONSTRAINTS.CONSTRAINT_NAME)).and(pkKcu.ORDINAL_POSITION.eq(fkKcu.POSITION_IN_UNIQUE_CONSTRAINT)).where(fkKcu.TABLE_SCHEMA.in(getInputSchemata())).orderBy(fkKcu.TABLE_SCHEMA.asc(), fkKcu.TABLE_NAME.asc(), fkKcu.CONSTRAINT_NAME.asc(), fkKcu.ORDINAL_POSITION.asc())) {
SchemaDefinition foreignKeySchema = getSchema(record.get(fkKcu.TABLE_SCHEMA));
SchemaDefinition uniqueKeySchema = getSchema(record.get(REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_SCHEMA));
String foreignKey = record.get(fkKcu.CONSTRAINT_NAME);
String foreignKeyTableName = record.get(fkKcu.TABLE_NAME);
String foreignKeyColumn = record.get(fkKcu.COLUMN_NAME);
String uniqueKey = record.get(REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_NAME);
String uniqueKeyTableName = record.get(TABLE_CONSTRAINTS.TABLE_NAME);
String uniqueKeyColumn = record.get(pkKcu.COLUMN_NAME);
TableDefinition foreignKeyTable = getTable(foreignKeySchema, foreignKeyTableName);
TableDefinition uniqueKeyTable = getTable(uniqueKeySchema, uniqueKeyTableName);
if (foreignKeyTable != null && uniqueKeyTable != null)
relations.addForeignKey(foreignKey, foreignKeyTable, foreignKeyTable.getColumn(foreignKeyColumn), uniqueKey, uniqueKeyTable, uniqueKeyTable.getColumn(uniqueKeyColumn), true);
}
}
use of org.jooq.meta.SchemaDefinition in project jOOQ by jOOQ.
the class H2Database method getDomains1_4.
private List<DomainDefinition> getDomains1_4() {
List<DomainDefinition> result = new ArrayList<>();
for (Record record : create().select(DOMAINS.DOMAIN_SCHEMA, DOMAINS.DOMAIN_NAME, DOMAINS.TYPE_NAME, DOMAINS.PRECISION, DOMAINS.SCALE, DOMAINS.IS_NULLABLE, DOMAINS.COLUMN_DEFAULT, DOMAINS.CHECK_CONSTRAINT).from(DOMAINS).where(DOMAINS.DOMAIN_SCHEMA.in(getInputSchemata())).and(DOMAINS.TYPE_NAME.ne(inline("ENUM"))).orderBy(DOMAINS.DOMAIN_SCHEMA, DOMAINS.DOMAIN_NAME)) {
// [#7644] [#11721] H2 puts DATETIME_PRECISION in NUMERIC_SCALE column
boolean isTimestamp = record.get(DOMAINS.TYPE_NAME).trim().toLowerCase().startsWith("timestamp");
SchemaDefinition schema = getSchema(record.get(DOMAINS.DOMAIN_SCHEMA));
DataTypeDefinition baseType = new DefaultDataTypeDefinition(this, schema, record.get(DOMAINS.TYPE_NAME), record.get(DOMAINS.PRECISION), isTimestamp ? record.get(DOMAINS.SCALE) : record.get(DOMAINS.PRECISION), isTimestamp ? 0 : record.get(DOMAINS.SCALE), !record.get(DOMAINS.IS_NULLABLE, boolean.class), record.get(DOMAINS.COLUMN_DEFAULT));
DefaultDomainDefinition domain = new DefaultDomainDefinition(schema, record.get(DOMAINS.DOMAIN_NAME), baseType);
if (!StringUtils.isBlank(record.get(DOMAINS.CHECK_CONSTRAINT)))
domain.addCheckClause(record.get(DOMAINS.CHECK_CONSTRAINT));
result.add(domain);
}
return result;
}
use of org.jooq.meta.SchemaDefinition 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.SchemaDefinition in project jOOQ by jOOQ.
the class H2Database method loadForeignKeys1_4.
private void loadForeignKeys1_4(DefaultRelations relations) {
for (Record record : create().select(CROSS_REFERENCES.FK_NAME, CROSS_REFERENCES.FKTABLE_NAME, CROSS_REFERENCES.FKTABLE_SCHEMA, CROSS_REFERENCES.FKCOLUMN_NAME, CROSS_REFERENCES.PKCOLUMN_NAME, CROSS_REFERENCES.referencedConstraint().CONSTRAINT_NAME, CROSS_REFERENCES.referencedConstraint().TABLE_NAME, CROSS_REFERENCES.referencedConstraint().CONSTRAINT_SCHEMA).from(CROSS_REFERENCES).where(CROSS_REFERENCES.FKTABLE_SCHEMA.in(getInputSchemata())).and(CROSS_REFERENCES.referencedConstraint().CONSTRAINT_TYPE.in("PRIMARY KEY", "PRIMARY_KEY", "UNIQUE")).orderBy(CROSS_REFERENCES.FKTABLE_SCHEMA.asc(), CROSS_REFERENCES.FK_NAME.asc(), CROSS_REFERENCES.ORDINAL_POSITION.asc()).fetch()) {
SchemaDefinition foreignKeySchema = getSchema(record.get(CROSS_REFERENCES.FKTABLE_SCHEMA));
SchemaDefinition uniqueKeySchema = getSchema(record.get(CROSS_REFERENCES.referencedConstraint().CONSTRAINT_SCHEMA));
if (foreignKeySchema != null && uniqueKeySchema != null) {
String foreignKey = record.get(CROSS_REFERENCES.FK_NAME);
String foreignKeyTableName = record.get(CROSS_REFERENCES.FKTABLE_NAME);
String foreignKeyColumn = record.get(CROSS_REFERENCES.FKCOLUMN_NAME);
String uniqueKey = record.get(CROSS_REFERENCES.referencedConstraint().CONSTRAINT_NAME);
String uniqueKeyTableName = record.get(CROSS_REFERENCES.referencedConstraint().TABLE_NAME);
String uniqueKeyColumn = record.get(CROSS_REFERENCES.PKCOLUMN_NAME);
TableDefinition foreignKeyTable = getTable(foreignKeySchema, foreignKeyTableName);
TableDefinition uniqueKeyTable = getTable(uniqueKeySchema, uniqueKeyTableName);
if (foreignKeyTable != null && uniqueKeyTable != null)
relations.addForeignKey(foreignKey, foreignKeyTable, foreignKeyTable.getColumn(foreignKeyColumn), uniqueKey, uniqueKeyTable, uniqueKeyTable.getColumn(uniqueKeyColumn), true);
}
}
}
use of org.jooq.meta.SchemaDefinition in project jOOQ by jOOQ.
the class H2Database method getDomainEnums2_0.
private void getDomainEnums2_0(List<EnumDefinition> result) {
// TODO: Re-generate the H2 schema and use generated code to run this query
create().resultQuery(("" + "select object_schema, object_name, array_agg(ev.value_name order by ev.VALUE_ORDINAL)\n" + "from INFORMATION_SCHEMA.ENUM_VALUES ev\n" + "where ev.OBJECT_TYPE = 'DOMAIN'\n" + "group by object_schema, object_name\n" + "order by 1, 2\n" + "")).coerce(COLUMNS.TABLE_SCHEMA, COLUMNS.TABLE_NAME, arrayAgg(COLUMNS.COLUMN_NAME)).forEach(r -> {
SchemaDefinition schema = getSchema(r.value1());
if (schema == null)
return;
DefaultEnumDefinition definition = new DefaultEnumDefinition(schema, r.value2(), "");
for (String string : r.value3()) definition.addLiteral(string);
result.add(definition);
});
}
Aggregations