use of org.jooq.meta.SchemaDefinition in project jOOQ by jOOQ.
the class MySQLDatabase method getEnums0.
@Override
protected List<EnumDefinition> getEnums0() throws SQLException {
List<EnumDefinition> result = new ArrayList<>();
for (ColumnRecord r : create().select(COLUMNS.TABLE_SCHEMA, COLUMNS.TABLE_NAME, COLUMNS.COLUMN_NAME, COLUMNS.COLUMN_TYPE, COLUMNS.COLUMN_COMMENT).from(COLUMNS).where(COLUMNS.COLUMN_TYPE.like("enum(%)").and(COLUMNS.TABLE_SCHEMA.in(workaroundFor5213(getInputSchemata())))).orderBy(COLUMNS.TABLE_SCHEMA.asc(), COLUMNS.TABLE_NAME.asc(), COLUMNS.COLUMN_NAME.asc()).fetch(mapping(ColumnRecord::new))) {
SchemaDefinition schema = getSchema(r.schema);
String name = r.table + "_" + r.column;
// [#1237] Don't generate enum classes for columns in MySQL tables
// that are excluded from code generation
TableDefinition tableDefinition = getTable(schema, r.table);
if (tableDefinition != null) {
ColumnDefinition columnDefinition = tableDefinition.getColumn(r.column);
if (columnDefinition != null) {
// are explicitly forced to another type
if (getConfiguredForcedType(columnDefinition, columnDefinition.getType()) == null) {
DefaultEnumDefinition definition = new DefaultEnumDefinition(schema, name, r.comment);
CSVReader reader = new CSVReader(new StringReader(r.type.replaceAll("(^enum\\()|(\\)$)", "")), // Separator
',', // Quote character
'\'', // Strict quotes
true);
for (String string : reader.next()) definition.addLiteral(string);
result.add(definition);
}
}
}
}
return result;
}
use of org.jooq.meta.SchemaDefinition 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.SchemaDefinition in project jOOQ by jOOQ.
the class DerbyDatabase method getSequences0.
@Override
protected List<SequenceDefinition> getSequences0() throws SQLException {
List<SequenceDefinition> result = new ArrayList<>();
for (Record record : sequences(getInputSchemata())) {
SchemaDefinition schema = getSchema(record.get(SYSSEQUENCES.sysschemas().SCHEMANAME));
DataTypeDefinition type = new DefaultDataTypeDefinition(this, schema, record.get(SYSSEQUENCES.SEQUENCEDATATYPE));
result.add(new DefaultSequenceDefinition(schema, record.get(SYSSEQUENCES.SEQUENCENAME), type, null, record.get(SYSSEQUENCES.STARTVALUE), record.get(SYSSEQUENCES.INCREMENT), record.get(SYSSEQUENCES.MINIMUMVALUE), record.get(SYSSEQUENCES.MAXIMUMVALUE), record.get(SYSSEQUENCES.CYCLEOPTION, Boolean.class), null));
}
return result;
}
use of org.jooq.meta.SchemaDefinition in project jOOQ by jOOQ.
the class DerbyDatabase method loadForeignKeys.
@Override
protected void loadForeignKeys(DefaultRelations relations) throws SQLException {
Field<String> fkName = field("fc.constraintname", String.class);
Field<String> fkTable = field("ft.tablename", String.class);
Field<String> fkSchema = field("fs.schemaname", String.class);
Field<?> fkDescriptor = field("fg.descriptor");
Field<String> ukName = field("pc.constraintname", String.class);
Field<String> ukTable = field("pt.tablename", String.class);
Field<String> ukSchema = field("ps.schemaname", String.class);
for (Record record : create().select(fkName, fkTable, fkSchema, fkDescriptor, ukName, ukTable, ukSchema).from("sys.sysconstraints fc").join("sys.sysforeignkeys f ").on("f.constraintid = fc.constraintid").join("sys.sysconglomerates fg").on("fg.conglomerateid = f.conglomerateid").join("sys.systables ft").on("ft.tableid = fg.tableid").join("sys.sysschemas fs").on("ft.schemaid = fs.schemaid").join("sys.sysconstraints pc").on("pc.constraintid = f.keyconstraintid").join("sys.systables pt").on("pt.tableid = pc.tableid").join("sys.sysschemas ps").on("ps.schemaid = pt.schemaid").where("cast(fc.type as varchar(32672)) = 'F'").fetch()) {
SchemaDefinition foreignKeySchema = getSchema(record.get(fkSchema));
SchemaDefinition uniqueKeySchema = getSchema(record.get(ukSchema));
String foreignKeyName = record.get(fkName);
String foreignKeyTableName = record.get(fkTable);
List<Integer> foreignKeyIndexes = decode(record.get(fkDescriptor, String.class));
String uniqueKeyName = record.get(ukName);
String uniqueKeyTableName = record.get(ukTable);
TableDefinition foreignKeyTable = getTable(foreignKeySchema, foreignKeyTableName);
TableDefinition uniqueKeyTable = getTable(uniqueKeySchema, uniqueKeyTableName);
if (foreignKeyTable != null && uniqueKeyTable != null)
for (int i = 0; i < foreignKeyIndexes.size(); i++) relations.addForeignKey(foreignKeyName, foreignKeyTable, foreignKeyTable.getColumn(foreignKeyIndexes.get(i)), uniqueKeyName, uniqueKeyTable);
}
}
use of org.jooq.meta.SchemaDefinition in project jOOQ by jOOQ.
the class DerbyDatabase method loadUniqueKeys.
/**
* {@inheritDoc}
*/
@Override
protected void loadUniqueKeys(DefaultRelations relations) throws SQLException {
for (Record record : fetchKeys("U")) {
SchemaDefinition schema = getSchema(record.get(SYSSCHEMAS.SCHEMANAME));
String key = record.get(SYSCONSTRAINTS.CONSTRAINTNAME);
String tableName = record.get(SYSTABLES.TABLENAME);
String descriptor = record.get(SYSCONGLOMERATES.DESCRIPTOR, String.class);
TableDefinition table = getTable(schema, tableName);
if (table != null)
for (int index : decode(descriptor)) relations.addUniqueKey(key, table, table.getColumn(index));
}
}
Aggregations