use of org.jooq.meta.SchemaDefinition in project jOOQ by jOOQ.
the class H2TableDefinition method getElements2_0.
public List<ColumnDefinition> getElements2_0() {
List<ColumnDefinition> result = new ArrayList<>();
H2Database db = (H2Database) getDatabase();
for (Record record : create().select(COLUMNS.COLUMN_NAME, COLUMNS.ORDINAL_POSITION, // [#2230] [#11733] Translate INTERVAL_TYPE to supported types
nvl(concat(ELEMENT_TYPES.DATA_TYPE, inline(" ARRAY")), when(COLUMNS.INTERVAL_TYPE.like(any(inline("%YEAR%"), inline("%MONTH%"))), inline("INTERVAL YEAR TO MONTH")).when(COLUMNS.INTERVAL_TYPE.like(any(inline("%DAY%"), inline("%HOUR%"), inline("%MINUTE%"), inline("%SECOND%"))), inline("INTERVAL DAY TO SECOND")).else_(Tables.COLUMNS.DATA_TYPE)).as(COLUMNS.TYPE_NAME), nvl(ELEMENT_TYPES.CHARACTER_MAXIMUM_LENGTH, COLUMNS.CHARACTER_MAXIMUM_LENGTH).as(COLUMNS.CHARACTER_MAXIMUM_LENGTH), coalesce(ELEMENT_TYPES.DATETIME_PRECISION, ELEMENT_TYPES.NUMERIC_PRECISION, COLUMNS.DATETIME_PRECISION, COLUMNS.NUMERIC_PRECISION).as(COLUMNS.NUMERIC_PRECISION), nvl(ELEMENT_TYPES.NUMERIC_SCALE, COLUMNS.NUMERIC_SCALE).as(COLUMNS.NUMERIC_SCALE), COLUMNS.IS_NULLABLE, field(Tables.COLUMNS.IS_GENERATED.eq(inline("ALWAYS"))).as(COLUMNS.IS_COMPUTED), Tables.COLUMNS.GENERATION_EXPRESSION, COLUMNS.COLUMN_DEFAULT, COLUMNS.REMARKS, field(Tables.COLUMNS.IS_IDENTITY.eq(inline("YES"))).as(Tables.COLUMNS.IS_IDENTITY), COLUMNS.DOMAIN_SCHEMA, COLUMNS.DOMAIN_NAME).from(COLUMNS).leftJoin(ELEMENT_TYPES).on(Tables.COLUMNS.TABLE_SCHEMA.equal(ELEMENT_TYPES.OBJECT_SCHEMA)).and(Tables.COLUMNS.TABLE_NAME.equal(ELEMENT_TYPES.OBJECT_NAME)).and(Tables.COLUMNS.DTD_IDENTIFIER.equal(ELEMENT_TYPES.COLLECTION_TYPE_IDENTIFIER)).where(COLUMNS.TABLE_SCHEMA.equal(getSchema().getName())).and(COLUMNS.TABLE_NAME.equal(getName())).and(!getDatabase().getIncludeInvisibleColumns() ? condition(COLUMNS.IS_VISIBLE.coerce(BOOLEAN)) : noCondition()).orderBy(COLUMNS.ORDINAL_POSITION)) {
// [#5331] AUTO_INCREMENT (MySQL style)
// [#5331] DEFAULT nextval('sequence') (PostgreSQL style)
// [#6332] [#6339] system-generated defaults shouldn't produce a default clause
boolean isIdentity = record.get(Tables.COLUMNS.IS_IDENTITY, boolean.class) || defaultString(record.get(COLUMNS.COLUMN_DEFAULT)).trim().toLowerCase().startsWith("nextval");
boolean isComputed = record.get(COLUMNS.IS_COMPUTED, boolean.class);
// [#681] Domain name if available
Name userType = record.get(COLUMNS.DOMAIN_NAME) != null ? name(record.get(COLUMNS.DOMAIN_SCHEMA), record.get(COLUMNS.DOMAIN_NAME)) : name(getSchema().getName(), getName() + "_" + record.get(COLUMNS.COLUMN_NAME));
SchemaDefinition typeSchema = record.get(COLUMNS.DOMAIN_NAME) != null ? getDatabase().getSchema(record.get(COLUMNS.DOMAIN_SCHEMA)) : getSchema();
DataTypeDefinition type = new DefaultDataTypeDefinition(getDatabase(), typeSchema == null ? getSchema() : typeSchema, record.get(COLUMNS.TYPE_NAME), record.get(COLUMNS.CHARACTER_MAXIMUM_LENGTH), record.get(COLUMNS.NUMERIC_PRECISION), record.get(COLUMNS.NUMERIC_SCALE), record.get(COLUMNS.IS_NULLABLE, boolean.class), isIdentity || isComputed ? null : record.get(COLUMNS.COLUMN_DEFAULT), userType).generatedAlwaysAs(isComputed ? record.get(Tables.COLUMNS.GENERATION_EXPRESSION) : null);
result.add(new DefaultColumnDefinition(getDatabase().getTable(getSchema(), getName()), record.get(COLUMNS.COLUMN_NAME), result.size() + 1, type, isIdentity, record.get(COLUMNS.REMARKS)));
}
return result;
}
use of org.jooq.meta.SchemaDefinition in project jOOQ by jOOQ.
the class HSQLDBDatabase method getDomains0.
@Override
protected List<DomainDefinition> getDomains0() throws SQLException {
List<DomainDefinition> result = new ArrayList<>();
DomainConstraints dc = DOMAIN_CONSTRAINTS.as("dc");
for (Record record : create().select(dc.domains().DOMAIN_SCHEMA, dc.domains().DOMAIN_NAME, dc.domains().DATA_TYPE, dc.domains().CHARACTER_MAXIMUM_LENGTH, dc.domains().NUMERIC_PRECISION, dc.domains().NUMERIC_SCALE, dc.domains().DOMAIN_DEFAULT, dc.checkConstraints().CHECK_CLAUSE).from(dc).where(dc.domains().DOMAIN_SCHEMA.in(getInputSchemata())).orderBy(dc.domains().DOMAIN_SCHEMA, dc.domains().DOMAIN_NAME)) {
SchemaDefinition schema = getSchema(record.get(dc.domains().DOMAIN_SCHEMA));
DataTypeDefinition baseType = new DefaultDataTypeDefinition(this, schema, record.get(dc.domains().DATA_TYPE), record.get(dc.domains().CHARACTER_MAXIMUM_LENGTH), record.get(dc.domains().NUMERIC_PRECISION), record.get(dc.domains().NUMERIC_SCALE), true, record.get(dc.domains().DOMAIN_DEFAULT));
DefaultDomainDefinition domain = new DefaultDomainDefinition(schema, record.get(dc.domains().DOMAIN_NAME), baseType);
if (!StringUtils.isBlank(record.get(dc.checkConstraints().CHECK_CLAUSE)))
domain.addCheckClause(record.get(dc.checkConstraints().CHECK_CLAUSE));
result.add(domain);
}
return result;
}
use of org.jooq.meta.SchemaDefinition in project jOOQ by jOOQ.
the class HSQLDBDatabase method loadCheckConstraints.
@Override
protected void loadCheckConstraints(DefaultRelations relations) throws SQLException {
CheckConstraints cc = CHECK_CONSTRAINTS.as("cc");
Columns c = COLUMNS.as("c");
// [#2808] [#3019] Workaround for bad handling of JOIN .. USING
Field<String> constraintName = field(name(cc.CONSTRAINT_NAME.getName()), String.class);
for (Record record : create().select(cc.tableConstraints().TABLE_SCHEMA, cc.tableConstraints().TABLE_NAME, constraintName, cc.CHECK_CLAUSE).from(cc).where(cc.tableConstraints().TABLE_SCHEMA.in(getInputSchemata())).and(getIncludeSystemCheckConstraints() ? noCondition() : cc.tableConstraints().CONSTRAINT_NAME.notLike("SYS!_CT!_%", '!').or(cc.CHECK_CLAUSE.notIn(// TODO: Should we ever quote these?
select(c.TABLE_SCHEMA.concat(inline('.')).concat(c.TABLE_NAME).concat(inline('.')).concat(c.COLUMN_NAME).concat(inline(" IS NOT NULL"))).from(c).where(c.TABLE_SCHEMA.eq(cc.tableConstraints().TABLE_SCHEMA)).and(c.TABLE_NAME.eq(cc.tableConstraints().TABLE_NAME)))))) {
SchemaDefinition schema = getSchema(record.get(cc.tableConstraints().TABLE_SCHEMA));
TableDefinition table = getTable(schema, record.get(cc.tableConstraints().TABLE_NAME));
if (table != null) {
relations.addCheckConstraint(table, new DefaultCheckConstraintDefinition(schema, table, record.get(constraintName), record.get(cc.CHECK_CLAUSE)));
}
}
}
use of org.jooq.meta.SchemaDefinition in project jOOQ by jOOQ.
the class HSQLDBDatabase method loadForeignKeys.
@Override
protected void loadForeignKeys(DefaultRelations relations) throws SQLException {
KeyColumnUsage fkKcu = KEY_COLUMN_USAGE.as("fk_kcu");
KeyColumnUsage pkKcu = KEY_COLUMN_USAGE.as("pk_kcu");
Result<?> result = 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()).fetch();
for (Record record : result) {
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 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;
}
Aggregations