use of org.jooq.util.ColumnDefinition in project jOOQ by jOOQ.
the class PostgresTableDefinition method getElements0.
@Override
public List<ColumnDefinition> getElements0() throws SQLException {
List<ColumnDefinition> result = new ArrayList<ColumnDefinition>();
for (Record record : create().select(COLUMNS.COLUMN_NAME, COLUMNS.ORDINAL_POSITION, COLUMNS.DATA_TYPE, COLUMNS.CHARACTER_MAXIMUM_LENGTH, COLUMNS.NUMERIC_PRECISION, COLUMNS.NUMERIC_SCALE, COLUMNS.IS_NULLABLE, COLUMNS.COLUMN_DEFAULT, COLUMNS.UDT_SCHEMA, COLUMNS.UDT_NAME, PG_DESCRIPTION.DESCRIPTION).from(COLUMNS).join(PG_NAMESPACE).on(COLUMNS.TABLE_SCHEMA.eq(PG_NAMESPACE.NSPNAME)).join(PG_CLASS).on(PG_CLASS.RELNAME.eq(COLUMNS.TABLE_NAME)).and(PG_CLASS.RELNAMESPACE.eq(oid(PG_NAMESPACE))).leftOuterJoin(PG_DESCRIPTION).on(PG_DESCRIPTION.OBJOID.eq(oid(PG_CLASS))).and(PG_DESCRIPTION.OBJSUBID.eq(COLUMNS.ORDINAL_POSITION)).where(COLUMNS.TABLE_SCHEMA.equal(getSchema().getName())).and(COLUMNS.TABLE_NAME.equal(getName())).orderBy(COLUMNS.ORDINAL_POSITION).fetch()) {
SchemaDefinition typeSchema = null;
String schemaName = record.get(COLUMNS.UDT_SCHEMA);
if (schemaName != null)
typeSchema = getDatabase().getSchema(schemaName);
DataTypeDefinition type = new DefaultDataTypeDefinition(getDatabase(), typeSchema, record.get(COLUMNS.DATA_TYPE), record.get(COLUMNS.CHARACTER_MAXIMUM_LENGTH), record.get(COLUMNS.NUMERIC_PRECISION), record.get(COLUMNS.NUMERIC_SCALE), record.get(COLUMNS.IS_NULLABLE, boolean.class), record.get(COLUMNS.COLUMN_DEFAULT), name(record.get(COLUMNS.UDT_SCHEMA), record.get(COLUMNS.UDT_NAME)));
ColumnDefinition column = new DefaultColumnDefinition(getDatabase().getTable(getSchema(), getName()), record.get(COLUMNS.COLUMN_NAME), record.get(COLUMNS.ORDINAL_POSITION, int.class), type, defaultString(record.get(COLUMNS.COLUMN_DEFAULT)).trim().toLowerCase().startsWith("nextval"), record.get(PG_DESCRIPTION.DESCRIPTION));
result.add(column);
}
return result;
}
use of org.jooq.util.ColumnDefinition in project jOOQ by jOOQ.
the class HSQLDBDatabase method loadForeignKeys.
@Override
protected void loadForeignKeys(DefaultRelations relations) throws SQLException {
Result<?> result = create().select(REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_NAME, REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_SCHEMA, KEY_COLUMN_USAGE.CONSTRAINT_NAME, KEY_COLUMN_USAGE.TABLE_SCHEMA, KEY_COLUMN_USAGE.TABLE_NAME, KEY_COLUMN_USAGE.COLUMN_NAME).from(REFERENTIAL_CONSTRAINTS).join(KEY_COLUMN_USAGE).on(KEY_COLUMN_USAGE.CONSTRAINT_SCHEMA.equal(REFERENTIAL_CONSTRAINTS.CONSTRAINT_SCHEMA)).and(KEY_COLUMN_USAGE.CONSTRAINT_NAME.equal(REFERENTIAL_CONSTRAINTS.CONSTRAINT_NAME)).where(KEY_COLUMN_USAGE.TABLE_SCHEMA.in(getInputSchemata())).orderBy(KEY_COLUMN_USAGE.TABLE_SCHEMA.asc(), KEY_COLUMN_USAGE.TABLE_NAME.asc(), KEY_COLUMN_USAGE.CONSTRAINT_NAME.asc(), KEY_COLUMN_USAGE.ORDINAL_POSITION.asc()).fetch();
for (Record record : result) {
SchemaDefinition foreignKeySchema = getSchema(record.get(KEY_COLUMN_USAGE.TABLE_SCHEMA));
SchemaDefinition uniqueKeySchema = getSchema(record.get(REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_SCHEMA));
String foreignKey = record.get(KEY_COLUMN_USAGE.CONSTRAINT_NAME);
String foreignKeyTable = record.get(KEY_COLUMN_USAGE.TABLE_NAME);
String foreignKeyColumn = record.get(KEY_COLUMN_USAGE.COLUMN_NAME);
String uniqueKey = record.get(REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_NAME);
TableDefinition referencingTable = getTable(foreignKeySchema, foreignKeyTable);
if (referencingTable != null) {
ColumnDefinition referencingColumn = referencingTable.getColumn(foreignKeyColumn);
relations.addForeignKey(foreignKey, uniqueKey, referencingColumn, uniqueKeySchema);
}
}
}
use of org.jooq.util.ColumnDefinition in project jOOQ by jOOQ.
the class JDBCDatabase method loadUniqueKeys.
@Override
protected void loadUniqueKeys(DefaultRelations relations) throws SQLException {
for (Schema schema : getSchemasFromMeta()) {
SchemaDefinition s = getSchema(schema.getName());
if (s != null) {
for (Table<?> table : schema.getTables()) {
TableDefinition t = getTable(s, table.getName());
if (t != null) {
UniqueKey<?> key = table.getPrimaryKey();
if (key != null) {
for (Field<?> field : key.getFields()) {
ColumnDefinition c = t.getColumn(field.getName());
relations.addPrimaryKey("PK_" + key.getTable().getName(), c);
}
}
}
}
}
}
}
use of org.jooq.util.ColumnDefinition in project jOOQ by jOOQ.
the class JDBCTableDefinition method getElements0.
@Override
public List<ColumnDefinition> getElements0() throws SQLException {
List<ColumnDefinition> result = new ArrayList<ColumnDefinition>();
int ordinal = 0;
for (Field<?> field : table.fields()) {
DataTypeDefinition type = new DefaultDataTypeDefinition(getDatabase(), getSchema(), field.getDataType().getTypeName(), field.getDataType().length(), field.getDataType().precision(), field.getDataType().scale(), field.getDataType().nullable(), create().renderInlined(field.getDataType().defaultValue()), (Name) null);
ColumnDefinition column = new DefaultColumnDefinition(getDatabase().getTable(getSchema(), getName()), field.getName(), ordinal, type, false, null);
result.add(column);
ordinal++;
}
return result;
}
use of org.jooq.util.ColumnDefinition in project jOOQ by jOOQ.
the class MySQLTableDefinition method getElements0.
@Override
public List<ColumnDefinition> getElements0() throws SQLException {
List<ColumnDefinition> result = new ArrayList<ColumnDefinition>();
for (Record record : create().select(Columns.ORDINAL_POSITION, Columns.COLUMN_NAME, Columns.COLUMN_COMMENT, Columns.COLUMN_TYPE, Columns.DATA_TYPE, Columns.IS_NULLABLE, Columns.COLUMN_DEFAULT, Columns.CHARACTER_MAXIMUM_LENGTH, Columns.NUMERIC_PRECISION, Columns.NUMERIC_SCALE, Columns.EXTRA).from(COLUMNS).where(TABLE_SCHEMA.equal(getSchema().getName())).and(TABLE_NAME.equal(getName())).orderBy(ORDINAL_POSITION)) {
String dataType = record.get(Columns.DATA_TYPE);
// [#519] Some types have unsigned versions
if (getDatabase().supportsUnsignedTypes()) {
if (asList("tinyint", "smallint", "mediumint", "int", "bigint").contains(dataType.toLowerCase())) {
if (record.get(Columns.COLUMN_TYPE).toLowerCase().contains("unsigned")) {
dataType += "unsigned";
}
}
}
DataTypeDefinition type = new DefaultDataTypeDefinition(getDatabase(), getSchema(), dataType, record.get(Columns.CHARACTER_MAXIMUM_LENGTH), record.get(Columns.NUMERIC_PRECISION), record.get(Columns.NUMERIC_SCALE), record.get(Columns.IS_NULLABLE, boolean.class), record.get(Columns.COLUMN_DEFAULT), name(getSchema().getName(), getName() + "_" + record.get(Columns.COLUMN_NAME)));
ColumnDefinition column = new DefaultColumnDefinition(getDatabase().getTable(getSchema(), getName()), record.get(Columns.COLUMN_NAME), record.get(Columns.ORDINAL_POSITION, int.class), type, "auto_increment".equalsIgnoreCase(record.get(Columns.EXTRA)), record.get(Columns.COLUMN_COMMENT));
result.add(column);
}
return result;
}
Aggregations