use of org.jooq.meta.SchemaDefinition in project jOOQ by jOOQ.
the class PostgresTableValuedFunction method getElements0.
@Override
public List<ColumnDefinition> getElements0() throws SQLException {
List<ColumnDefinition> result = new ArrayList<>();
Routines r = ROUTINES;
Parameters p = PARAMETERS;
PgNamespace pg_n = PG_NAMESPACE;
PgProc pg_p = PG_PROC;
Columns c = COLUMNS;
PgType pg_t = PG_TYPE;
Field<Integer> pPrecision = pNumericPrecision(p);
Field<Integer> cPrecision = nvl(c.DATETIME_PRECISION, c.NUMERIC_PRECISION);
Field<Integer> rPrecision = nvl(r.DATETIME_PRECISION, r.NUMERIC_PRECISION);
for (Record record : create().select(p.PARAMETER_NAME, rowNumber().over(partitionBy(p.SPECIFIC_NAME).orderBy(p.ORDINAL_POSITION)).as(p.ORDINAL_POSITION), p.DATA_TYPE, p.CHARACTER_MAXIMUM_LENGTH, pPrecision.as(p.NUMERIC_PRECISION), p.NUMERIC_SCALE, inline("true").as(c.IS_NULLABLE), (((PostgresDatabase) getDatabase()).is94() ? PARAMETERS.PARAMETER_DEFAULT : inline((String) null)).as(c.COLUMN_DEFAULT), p.UDT_SCHEMA, p.UDT_NAME).from(r).join(p).on(row(r.SPECIFIC_CATALOG, r.SPECIFIC_SCHEMA, r.SPECIFIC_NAME).eq(p.SPECIFIC_CATALOG, p.SPECIFIC_SCHEMA, p.SPECIFIC_NAME)).join(pg_n).on(r.SPECIFIC_SCHEMA.eq(pg_n.NSPNAME)).join(pg_p).on(pg_p.PRONAMESPACE.eq(pg_n.OID)).and(pg_p.PRONAME.concat("_").concat(pg_p.OID).eq(r.SPECIFIC_NAME)).where(r.SPECIFIC_NAME.eq(specificName)).and(p.PARAMETER_MODE.ne("IN")).and(pg_p.PRORETSET).unionAll(// from INFORMATION_SCHEMA.TABLES
select(coalesce(c.COLUMN_NAME, getName()).as(c.COLUMN_NAME), coalesce(c.ORDINAL_POSITION, inline(1)).as(c.ORDINAL_POSITION), coalesce(c.DATA_TYPE, r.DATA_TYPE).as(c.DATA_TYPE), coalesce(c.CHARACTER_MAXIMUM_LENGTH, r.CHARACTER_MAXIMUM_LENGTH).as(c.CHARACTER_MAXIMUM_LENGTH), coalesce(cPrecision, rPrecision).as(c.NUMERIC_PRECISION), coalesce(c.NUMERIC_SCALE, r.NUMERIC_SCALE).as(c.NUMERIC_SCALE), coalesce(c.IS_NULLABLE, "true").as(c.IS_NULLABLE), coalesce(c.COLUMN_DEFAULT, inline((String) null)).as(c.COLUMN_DEFAULT), coalesce(c.UDT_SCHEMA, inline((String) null)).as(c.UDT_SCHEMA), coalesce(c.UDT_NAME, r.UDT_NAME, field(select(pg_t.TYPNAME).from(pg_t).where(pg_t.OID.eq(pg_p.PRORETTYPE)))).as(c.UDT_NAME)).from(r).leftOuterJoin(c).on(row(r.TYPE_UDT_CATALOG, r.TYPE_UDT_SCHEMA, r.TYPE_UDT_NAME).eq(c.TABLE_CATALOG, c.TABLE_SCHEMA, c.TABLE_NAME)).join(pg_n).on(r.SPECIFIC_SCHEMA.eq(pg_n.NSPNAME)).join(pg_p).on(pg_p.PRONAMESPACE.eq(pg_n.OID)).and(pg_p.PRONAME.concat("_").concat(pg_p.OID).eq(r.SPECIFIC_NAME)).where(r.SPECIFIC_NAME.eq(specificName)).and(row(r.SPECIFIC_CATALOG, r.SPECIFIC_SCHEMA, r.SPECIFIC_NAME).notIn(select(p.SPECIFIC_CATALOG, p.SPECIFIC_SCHEMA, p.SPECIFIC_NAME).from(p).where(p.PARAMETER_MODE.eq("OUT")))).and(pg_p.PRORETSET)).orderBy(2)) {
SchemaDefinition typeSchema = null;
String schemaName = record.get(p.UDT_SCHEMA);
if (schemaName != null)
typeSchema = getDatabase().getSchema(schemaName);
DataTypeDefinition type = new DefaultDataTypeDefinition(getDatabase(), typeSchema, record.get(p.DATA_TYPE), record.get(p.CHARACTER_MAXIMUM_LENGTH), record.get(p.NUMERIC_PRECISION), record.get(p.NUMERIC_SCALE), record.get(c.IS_NULLABLE, boolean.class), record.get(c.COLUMN_DEFAULT), name(record.get(p.UDT_SCHEMA), record.get(p.UDT_NAME)));
result.add(new DefaultColumnDefinition(getDatabase().getTable(getSchema(), getName()), record.get(p.PARAMETER_NAME), result.size() + 1, type, defaultString(record.get(c.COLUMN_DEFAULT)).startsWith("nextval"), null));
}
return result;
}
use of org.jooq.meta.SchemaDefinition 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.SchemaDefinition in project jOOQ by jOOQ.
the class SQLiteDatabase method getSchemata0.
@Override
protected List<SchemaDefinition> getSchemata0() throws SQLException {
List<SchemaDefinition> result = new ArrayList<>();
result.add(new SchemaDefinition(this, "", ""));
return result;
}
use of org.jooq.meta.SchemaDefinition in project jOOQ by jOOQ.
the class XMLTableDefinition method getElements0.
@Override
protected List<ColumnDefinition> getElements0() throws SQLException {
List<ColumnDefinition> result = new ArrayList<>();
for (Column column : info.getColumns()) {
if (StringUtils.equals(defaultIfNull(table.getTableCatalog(), ""), defaultIfNull(column.getTableCatalog(), "")) && StringUtils.equals(defaultIfNull(table.getTableSchema(), ""), defaultIfNull(column.getTableSchema(), "")) && StringUtils.equals(defaultIfNull(table.getTableName(), ""), defaultIfNull(column.getTableName(), ""))) {
SchemaDefinition schema = getDatabase().getSchema(column.getTableSchema());
DataTypeDefinition type = new DefaultDataTypeDefinition(getDatabase(), schema, column.getDataType(), unbox(column.getCharacterMaximumLength()), unbox(column.getNumericPrecision()), unbox(column.getNumericScale()), column.isIsNullable(), column.getColumnDefault()).generatedAlwaysAs(TRUE.equals(column.isIsGenerated()) ? column.getGenerationExpression() : null).generationOption(TRUE.equals(column.isIsGenerated()) ? "STORED".equalsIgnoreCase(column.getGenerationOption()) ? STORED : "VIRTUAL".equalsIgnoreCase(column.getGenerationOption()) ? VIRTUAL : null : null);
result.add(new DefaultColumnDefinition(this, column.getColumnName(), unbox(column.getOrdinalPosition()), type, column.getIdentityGeneration() != null, TRUE.equals(column.isReadonly()), column.getComment()));
}
}
return result;
}
use of org.jooq.meta.SchemaDefinition 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);
}
}
Aggregations