use of org.jooq.meta.TableDefinition in project jOOQ by jOOQ.
the class HSQLDBDatabase method loadPrimaryKeys.
@Override
protected void loadPrimaryKeys(DefaultRelations relations) throws SQLException {
for (Record record : primaryKeys(getInputSchemata())) {
SchemaDefinition schema = getSchema(record.get(KEY_COLUMN_USAGE.TABLE_SCHEMA));
String key = record.get(KEY_COLUMN_USAGE.CONSTRAINT_NAME);
String tableName = record.get(KEY_COLUMN_USAGE.TABLE_NAME);
String columnName = record.get(KEY_COLUMN_USAGE.COLUMN_NAME);
TableDefinition table = getTable(schema, tableName);
if (table != null)
relations.addPrimaryKey(key, table, table.getColumn(columnName));
}
}
use of org.jooq.meta.TableDefinition in project jOOQ by jOOQ.
the class PostgresDatabase method loadUniqueKeys.
@Override
protected void loadUniqueKeys(DefaultRelations relations) throws SQLException {
for (Record record : uniqueKeys(getInputSchemata())) {
SchemaDefinition schema = getSchema(record.get(KEY_COLUMN_USAGE.TABLE_SCHEMA));
String key = record.get(KEY_COLUMN_USAGE.CONSTRAINT_NAME);
String tableName = record.get(KEY_COLUMN_USAGE.TABLE_NAME);
String columnName = record.get(KEY_COLUMN_USAGE.COLUMN_NAME);
TableDefinition table = getTable(schema, tableName);
if (table != null)
relations.addUniqueKey(key, table, table.getColumn(columnName));
}
}
use of org.jooq.meta.TableDefinition in project jOOQ by jOOQ.
the class SQLiteDatabase method loadCheckConstraints.
@Override
protected void loadCheckConstraints(DefaultRelations r) throws SQLException {
DSLContext ctx = create().configuration().deriveSettings(s -> s.withInterpreterDelayForeignKeyDeclarations(true)).dsl();
SchemaDefinition schema = getSchemata().get(0);
for (Record record : ctx.select(SQLiteMaster.TBL_NAME, SQLiteMaster.SQL).from(SQLITE_MASTER).where(SQLiteMaster.SQL.likeIgnoreCase("%CHECK%")).orderBy(SQLiteMaster.TBL_NAME)) {
TableDefinition table = getTable(schema, record.get(SQLiteMaster.TBL_NAME));
if (table != null) {
String sql = record.get(SQLiteMaster.SQL);
try {
Query query = ctx.parser().parseQuery(sql);
for (Table<?> t : ctx.meta(query).getTables(table.getName())) {
for (Check<?> check : t.getChecks()) {
r.addCheckConstraint(table, new DefaultCheckConstraintDefinition(schema, table, check.getName(), ctx.renderInlined(check.condition()), check.enforced()));
}
}
} catch (ParserException e) {
log.info("Cannot parse SQL: " + sql, e);
} catch (DataDefinitionException e) {
log.info("Cannot interpret SQL: " + sql, e);
}
}
}
}
use of org.jooq.meta.TableDefinition in project jOOQ by jOOQ.
the class MySQLDatabase method loadForeignKeys.
@Override
protected void loadForeignKeys(DefaultRelations relations) throws SQLException {
for (Record record : create().select(REFERENTIAL_CONSTRAINTS.CONSTRAINT_SCHEMA, REFERENTIAL_CONSTRAINTS.CONSTRAINT_NAME, REFERENTIAL_CONSTRAINTS.TABLE_NAME, REFERENTIAL_CONSTRAINTS.REFERENCED_TABLE_NAME, REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_NAME, REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_SCHEMA, KEY_COLUMN_USAGE.COLUMN_NAME).from(REFERENTIAL_CONSTRAINTS).join(KEY_COLUMN_USAGE).on(REFERENTIAL_CONSTRAINTS.CONSTRAINT_SCHEMA.equal(KEY_COLUMN_USAGE.CONSTRAINT_SCHEMA)).and(REFERENTIAL_CONSTRAINTS.CONSTRAINT_NAME.equal(KEY_COLUMN_USAGE.CONSTRAINT_NAME)).and(REFERENTIAL_CONSTRAINTS.TABLE_NAME.equal(KEY_COLUMN_USAGE.TABLE_NAME)).where(REFERENTIAL_CONSTRAINTS.CONSTRAINT_SCHEMA.in(workaroundFor5213(getInputSchemata()))).orderBy(KEY_COLUMN_USAGE.CONSTRAINT_SCHEMA.asc(), KEY_COLUMN_USAGE.CONSTRAINT_NAME.asc(), KEY_COLUMN_USAGE.ORDINAL_POSITION.asc()).fetch()) {
SchemaDefinition foreignKeySchema = getSchema(record.get(REFERENTIAL_CONSTRAINTS.CONSTRAINT_SCHEMA));
SchemaDefinition uniqueKeySchema = getSchema(record.get(REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_SCHEMA));
String foreignKey = record.get(REFERENTIAL_CONSTRAINTS.CONSTRAINT_NAME);
String foreignKeyColumn = record.get(KEY_COLUMN_USAGE.COLUMN_NAME);
String foreignKeyTableName = record.get(REFERENTIAL_CONSTRAINTS.TABLE_NAME);
String uniqueKey = record.get(REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_NAME);
String uniqueKeyTableName = record.get(REFERENTIAL_CONSTRAINTS.REFERENCED_TABLE_NAME);
TableDefinition foreignKeyTable = getTable(foreignKeySchema, foreignKeyTableName);
TableDefinition uniqueKeyTable = getTable(uniqueKeySchema, uniqueKeyTableName);
if (foreignKeyTable != null)
relations.addForeignKey(foreignKey, foreignKeyTable, foreignKeyTable.getColumn(foreignKeyColumn), getKeyName(uniqueKeyTableName, uniqueKey), uniqueKeyTable);
}
}
use of org.jooq.meta.TableDefinition 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;
}
Aggregations