Search in sources :

Example 26 with Table

use of org.jooq.Table in project jOOQ by jOOQ.

the class InformationSchemaExport method exportTables.

static final InformationSchema exportTables(Configuration configuration, List<Table<?>> tables) {
    InformationSchema result = new InformationSchema();
    Set<Catalog> includedCatalogs = new LinkedHashSet<>();
    Set<Schema> includedSchemas = new LinkedHashSet<>();
    Set<Table<?>> includedTables = new LinkedHashSet<>(tables);
    for (Table<?> t : tables) if (t.getSchema() != null)
        includedSchemas.add(t.getSchema());
    for (Schema s : includedSchemas) if (s.getCatalog() != null)
        includedCatalogs.add(s.getCatalog());
    for (Catalog c : includedCatalogs) exportCatalog0(result, c);
    for (Schema s : includedSchemas) exportSchema0(result, s);
    for (Table<?> t : tables) exportTable0(configuration, result, t, includedTables);
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Table(org.jooq.Table) InformationSchema(org.jooq.util.xml.jaxb.InformationSchema) Schema(org.jooq.Schema) InformationSchema(org.jooq.util.xml.jaxb.InformationSchema) Catalog(org.jooq.Catalog)

Example 27 with Table

use of org.jooq.Table in project jOOQ by jOOQ.

the class InformationSchemaExport method exportSchemas.

static final InformationSchema exportSchemas(Configuration configuration, List<Schema> schemas) {
    InformationSchema result = new InformationSchema();
    Set<Catalog> includedCatalogs = new LinkedHashSet<>();
    Set<Table<?>> includedTables = new LinkedHashSet<>();
    for (Schema s : schemas) {
        if (s.getCatalog() != null)
            includedCatalogs.add(s.getCatalog());
        includedTables.addAll(s.getTables());
    }
    for (Catalog c : includedCatalogs) exportCatalog0(result, c);
    for (Schema s : schemas) {
        exportSchema0(result, s);
        for (Domain<?> d : s.getDomains()) exportDomain0(configuration, result, d);
        for (Table<?> t : s.getTables()) exportTable0(configuration, result, t, includedTables);
        for (Sequence<?> q : s.getSequences()) exportSequence0(configuration, result, q);
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Table(org.jooq.Table) InformationSchema(org.jooq.util.xml.jaxb.InformationSchema) Schema(org.jooq.Schema) InformationSchema(org.jooq.util.xml.jaxb.InformationSchema) Catalog(org.jooq.Catalog)

Example 28 with Table

use of org.jooq.Table in project jOOQ by jOOQ.

the class DAOImpl method records.

private /* non-final */
List<R> records(Collection<P> objects, boolean forUpdate) {
    List<R> result = new ArrayList<>(objects.size());
    Field<?>[] pk = pk();
    DSLContext ctx;
    // [#7731] Create a Record -> POJO mapping to allow for reusing the below
    // derived Configuration for improved reflection caching.
    IdentityHashMap<R, Object> mapping = !FALSE.equals(settings().isReturnRecordToPojo()) ? new IdentityHashMap<>() : null;
    // are copied back to the relevant POJO using the RecordListener SPI
    if (mapping != null) {
        Consumer<? super RecordContext> end = c -> {
            Record record = c.record();
            // TODO: [#2536] Use mapper()
            if (record != null)
                record.into(mapping.get(record));
        };
        ctx = configuration().deriveAppending(onStoreEnd(end).onInsertEnd(end).onUpdateEnd(end).onDeleteEnd(end)).dsl();
    } else
        ctx = ctx();
    for (P object : objects) {
        R record = ctx.newRecord(table, object);
        if (mapping != null)
            mapping.put(record, object);
        if (forUpdate && pk != null)
            for (Field<?> field : pk) record.changed(field, false);
        Tools.resetChangedOnNotNull(record);
        result.add(record);
    }
    return result;
}
Also used : UniqueKey(org.jooq.UniqueKey) Arrays(java.util.Arrays) RecordMapper(org.jooq.RecordMapper) RecordListener.onStoreEnd(org.jooq.RecordListener.onStoreEnd) Table(org.jooq.Table) DAO(org.jooq.DAO) DSL.row(org.jooq.impl.DSL.row) RecordContext(org.jooq.RecordContext) Condition(org.jooq.Condition) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) DSLContext(org.jooq.DSLContext) SQLDialect(org.jooq.SQLDialect) RecordListenerProvider(org.jooq.RecordListenerProvider) UpdatableRecord(org.jooq.UpdatableRecord) FALSE(java.lang.Boolean.FALSE) Record(org.jooq.Record) IdentityHashMap(java.util.IdentityHashMap) Collection(java.util.Collection) Settings(org.jooq.conf.Settings) Field(org.jooq.Field) Consumer(java.util.function.Consumer) Configuration(org.jooq.Configuration) List(java.util.List) TableField(org.jooq.TableField) DSL.noCondition(org.jooq.impl.DSL.noCondition) EMPTY_RECORD(org.jooq.impl.Tools.EMPTY_RECORD) Optional(java.util.Optional) TRUE(java.lang.Boolean.TRUE) ArrayList(java.util.ArrayList) DSLContext(org.jooq.DSLContext) Field(org.jooq.Field) TableField(org.jooq.TableField) UpdatableRecord(org.jooq.UpdatableRecord) Record(org.jooq.Record)

Example 29 with Table

use of org.jooq.Table in project jOOQ by jOOQ.

the class UpdateQueryImpl method accept0.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
final void accept0(Context<?> ctx) {
    boolean declareTables = ctx.declareTables();
    ctx.start(UPDATE_UPDATE).visit(K_UPDATE).sql(' ').declareTables(true).visit(table(ctx)).declareTables(declareTables).end(UPDATE_UPDATE);
    ctx.formatSeparator().start(UPDATE_SET).visit(K_SET).separatorRequired(true);
    // A multi-row update was specified
    if (multiRow != null) {
        // [#6884] This syntax can be emulated trivially, if the RHS is not a SELECT subquery
        if (multiValue != null && !SUPPORT_RVE_SET.contains(ctx.dialect())) {
            FieldMapForUpdate map = new FieldMapForUpdate(table(), UPDATE_SET_ASSIGNMENT);
            for (int i = 0; i < multiRow.size(); i++) {
                Field<?> k = multiRow.field(i);
                Field<?> v = multiValue.field(i);
                map.put(k, Tools.field(v, k));
            }
            ctx.formatIndentStart().formatSeparator().visit(map).formatIndentEnd();
        } else {
            Row row = removeReadonly(ctx, multiRow);
            ctx.start(UPDATE_SET_ASSIGNMENT).formatIndentStart().formatSeparator().qualify(false, c -> c.visit(row)).sql(" = ");
            // right hand side of a SET clause
            if (multiValue != null) {
                // single-degree rows. Let's just always render it, here.
                if (REQUIRE_RVE_ROW_CLAUSE.contains(ctx.dialect()))
                    ctx.visit(K_ROW).sql(" ");
                ctx.visit(removeReadonly(ctx, multiRow, multiValue));
            } else // Subselects or subselect emulations of row value expressions
            {
                Select<?> select;
                if (multiValue != null)
                    select = select(removeReadonly(ctx, multiRow, multiValue).fields());
                else
                    select = multiSelect;
                visitSubquery(ctx, select, false, false, false);
            }
            ctx.formatIndentEnd().end(UPDATE_SET_ASSIGNMENT);
        }
    } else // A regular (non-multi-row) update was specified
    {
        ctx.formatIndentStart().formatSeparator().visit(updateMap).formatIndentEnd();
    }
    ctx.end(UPDATE_SET);
    switch(ctx.family()) {
        default:
            acceptFrom(ctx);
            break;
    }
    if (limit != null && NO_SUPPORT_LIMIT.contains(ctx.dialect()) || !orderBy.isEmpty() && NO_SUPPORT_ORDER_BY_LIMIT.contains(ctx.dialect())) {
        Field<?>[] keyFields = table().getKeys().isEmpty() ? new Field[] { table().rowid() } : (table().getPrimaryKey() != null ? table().getPrimaryKey() : table().getKeys().get(0)).getFieldsArray();
        ctx.start(UPDATE_WHERE).formatSeparator().visit(K_WHERE).sql(' ');
        if (keyFields.length == 1)
            ctx.visit(keyFields[0].in(select((Field) keyFields[0]).from(table()).where(getWhere()).orderBy(orderBy).limit(limit)));
        else
            ctx.visit(row(keyFields).in(select(keyFields).from(table()).where(getWhere()).orderBy(orderBy).limit(limit)));
        ctx.end(UPDATE_WHERE);
    } else {
        ctx.start(UPDATE_WHERE);
        if (hasWhere())
            ctx.formatSeparator().visit(K_WHERE).sql(' ').visit(getWhere());
        ctx.end(UPDATE_WHERE);
        if (!orderBy.isEmpty())
            ctx.formatSeparator().visit(K_ORDER_BY).sql(' ').visit(orderBy);
        if (limit != null)
            ctx.formatSeparator().visit(K_LIMIT).sql(' ').visit(limit);
    }
    ctx.start(UPDATE_RETURNING);
    toSQLReturning(ctx);
    ctx.end(UPDATE_RETURNING);
}
Also used : THROW(org.jooq.conf.WriteIfReadonly.THROW) Arrays(java.util.Arrays) POSTGRES(org.jooq.SQLDialect.POSTGRES) Condition(org.jooq.Condition) DSL.trueCondition(org.jooq.impl.DSL.trueCondition) Record20(org.jooq.Record20) Record4(org.jooq.Record4) Record21(org.jooq.Record21) Record5(org.jooq.Record5) Record2(org.jooq.Record2) Record22(org.jooq.Record22) K_UPDATE(org.jooq.impl.Keywords.K_UPDATE) Record3(org.jooq.Record3) Record1(org.jooq.Record1) Tools.unqualified(org.jooq.impl.Tools.unqualified) Map(java.util.Map) Tools.visitSubquery(org.jooq.impl.Tools.visitSubquery) Record8(org.jooq.Record8) SQLDialect(org.jooq.SQLDialect) Scope(org.jooq.Scope) Record9(org.jooq.Record9) Record6(org.jooq.Record6) Select(org.jooq.Select) Record7(org.jooq.Record7) SettingsTools.getExecuteUpdateWithoutWhere(org.jooq.conf.SettingsTools.getExecuteUpdateWithoutWhere) Set(java.util.Set) K_ROW(org.jooq.impl.Keywords.K_ROW) Row17(org.jooq.Row17) DERBY(org.jooq.SQLDialect.DERBY) Row16(org.jooq.Row16) UPDATE_UPDATE(org.jooq.Clause.UPDATE_UPDATE) Row15(org.jooq.Row15) Row14(org.jooq.Row14) Row19(org.jooq.Row19) Row18(org.jooq.Row18) DSL.row(org.jooq.impl.DSL.row) K_ORDER_BY(org.jooq.impl.Keywords.K_ORDER_BY) SQLITE(org.jooq.SQLDialect.SQLITE) Row20(org.jooq.Row20) UPDATE(org.jooq.Clause.UPDATE) Row22(org.jooq.Row22) Row21(org.jooq.Row21) Record(org.jooq.Record) K_FROM(org.jooq.impl.Keywords.K_FROM) FIREBIRD(org.jooq.SQLDialect.FIREBIRD) K_WHERE(org.jooq.impl.Keywords.K_WHERE) IGNITE(org.jooq.SQLDialect.IGNITE) YUGABYTEDB(org.jooq.SQLDialect.YUGABYTEDB) Tools.fieldName(org.jooq.impl.Tools.fieldName) Row13(org.jooq.Row13) Row12(org.jooq.Row12) Row11(org.jooq.Row11) Row10(org.jooq.Row10) RowN(org.jooq.RowN) Row(org.jooq.Row) Tools.map(org.jooq.impl.Tools.map) UNotYetImplemented(org.jooq.impl.QOM.UNotYetImplemented) K_LIMIT(org.jooq.impl.Keywords.K_LIMIT) Table(org.jooq.Table) Operator(org.jooq.Operator) UpdateQuery(org.jooq.UpdateQuery) Clause(org.jooq.Clause) DataTypeException(org.jooq.exception.DataTypeException) DSL.name(org.jooq.impl.DSL.name) Row1(org.jooq.Row1) Row2(org.jooq.Row2) Row3(org.jooq.Row3) Collection(java.util.Collection) Row4(org.jooq.Row4) Row5(org.jooq.Row5) Row6(org.jooq.Row6) Field(org.jooq.Field) Row7(org.jooq.Row7) DSL.select(org.jooq.impl.DSL.select) Row8(org.jooq.Row8) CUBRID(org.jooq.SQLDialect.CUBRID) Row9(org.jooq.Row9) HSQLDB(org.jooq.SQLDialect.HSQLDB) List(java.util.List) Context(org.jooq.Context) K_SET(org.jooq.impl.Keywords.K_SET) UPDATE_WHERE(org.jooq.Clause.UPDATE_WHERE) UPDATE_RETURNING(org.jooq.Clause.UPDATE_RETURNING) UPDATE_SET_ASSIGNMENT(org.jooq.Clause.UPDATE_SET_ASSIGNMENT) UPDATE_FROM(org.jooq.Clause.UPDATE_FROM) TableLike(org.jooq.TableLike) FieldMapForUpdate.removeReadonly(org.jooq.impl.FieldMapForUpdate.removeReadonly) Record17(org.jooq.Record17) Record18(org.jooq.Record18) Record19(org.jooq.Record19) Record13(org.jooq.Record13) Record14(org.jooq.Record14) Record15(org.jooq.Record15) Record16(org.jooq.Record16) Configuration(org.jooq.Configuration) Record10(org.jooq.Record10) Record11(org.jooq.Record11) Record12(org.jooq.Record12) H2(org.jooq.SQLDialect.H2) OrderField(org.jooq.OrderField) UPDATE_SET(org.jooq.Clause.UPDATE_SET) Field(org.jooq.Field) OrderField(org.jooq.OrderField) Row(org.jooq.Row)

Example 30 with Table

use of org.jooq.Table in project jOOQ by jOOQ.

the class Alias method acceptDeclareAliasStandard.

private final void acceptDeclareAliasStandard(Context<?> context) {
    if (wrapped instanceof TableImpl)
        context.scopeMarkStart(wrapping);
    SQLDialect dialect = context.dialect();
    SQLDialect family = context.family();
    boolean emulatedDerivedColumnList = false;
    // Hence, wrap the table reference in a subselect
    if (fieldAliases != null && (SUPPORT_DERIVED_COLUMN_NAMES_SPECIAL1.contains(dialect)) && (wrapped instanceof TableImpl || wrapped instanceof CommonTableExpressionImpl)) {
        visitSubquery(context, select(asterisk()).from(((Table<?>) wrapped).as(alias)), true, false, false);
    } else // results using UNION ALL
    if (fieldAliases != null && (emulatedDerivedColumnList || SUPPORT_DERIVED_COLUMN_NAMES_SPECIAL2.contains(dialect))) {
        emulatedDerivedColumnList = true;
        if (wrapped instanceof Values && NO_SUPPORT_VALUES.contains(dialect)) {
            context.data(DATA_SELECT_ALIASES, fieldAliases, t -> toSQLWrapped(t));
        } else {
            // [#3156] Do not SELECT * from derived tables to prevent ambiguously defined columns
            // in those derived tables
            Select<?> wrappedAsSelect = wrapped instanceof Select ? (Select<?>) wrapped : wrapped instanceof DerivedTable ? ((DerivedTable<?>) wrapped).query() : select(asterisk()).from(((Table<?>) wrapped).as(alias));
            List<Field<?>> select = wrappedAsSelect.getSelect();
            if (emulatedDerivedColumnList) {
                SelectFieldList<Field<?>> fields = new SelectFieldList<>();
                for (int i = 0; i < fieldAliases.length; i++) {
                    switch(family) {
                        default:
                            fields.add(field("null").as(fieldAliases[i]));
                            break;
                    }
                }
                visitSubquery(context, select(fields).where(falseCondition()).unionAll(wrappedAsSelect), true, false, false);
            }
        }
    } else
        // The default behaviour
        toSQLWrapped(context);
    // [#291] some aliases cause trouble, if they are not explicitly marked using "as"
    toSQLAs(context);
    context.sql(' ').qualify(false, c -> c.visit(alias));
    // [#1801] Add field aliases to the table alias, if applicable
    if (fieldAliases != null && !emulatedDerivedColumnList) {
        toSQLDerivedColumnList(context);
    } else {
        // TODO: Is this still needed?
        switch(family) {
            case HSQLDB:
            case POSTGRES:
            case YUGABYTEDB:
                {
                    // The javac compiler doesn't like casting of generics
                    Object o = wrapped;
                    if (context.declareTables() && o instanceof ArrayTable)
                        context.sql('(').visit(wrap(((ArrayTable) o).fields()).qualify(false)).sql(')');
                    break;
                }
        }
    }
    if (wrapped instanceof TableImpl)
        context.scopeMarkEnd(wrapping);
}
Also used : QueryPartListView.wrap(org.jooq.impl.QueryPartListView.wrap) Tools.map(org.jooq.impl.Tools.map) UEmpty(org.jooq.impl.QOM.UEmpty) FIELD(org.jooq.Clause.FIELD) DATA_AS_REQUIRED(org.jooq.impl.Tools.BooleanDataKey.DATA_AS_REQUIRED) DSL.field(org.jooq.impl.DSL.field) Table(org.jooq.Table) POSTGRES(org.jooq.SQLDialect.POSTGRES) DATA_UNALIAS_ALIASED_EXPRESSIONS(org.jooq.impl.Tools.BooleanDataKey.DATA_UNALIAS_ALIASED_EXPRESSIONS) DATA_WRAP_DERIVED_TABLES_IN_PARENTHESES(org.jooq.impl.Tools.BooleanDataKey.DATA_WRAP_DERIVED_TABLES_IN_PARENTHESES) DATA_SELECT_ALIASES(org.jooq.impl.Tools.DataKey.DATA_SELECT_ALIASES) Clause(org.jooq.Clause) Tools.visitSubquery(org.jooq.impl.Tools.visitSubquery) FIELD_REFERENCE(org.jooq.Clause.FIELD_REFERENCE) SQLDialect(org.jooq.SQLDialect) Quoted(org.jooq.Name.Quoted) Select(org.jooq.Select) Predicate(java.util.function.Predicate) Name(org.jooq.Name) DSL.falseCondition(org.jooq.impl.DSL.falseCondition) Set(java.util.Set) Field(org.jooq.Field) DSL.select(org.jooq.impl.DSL.select) CUBRID(org.jooq.SQLDialect.CUBRID) QueryPart(org.jooq.QueryPart) HSQLDB(org.jooq.SQLDialect.HSQLDB) List(java.util.List) TABLE(org.jooq.Clause.TABLE) K_AS(org.jooq.impl.Keywords.K_AS) Context(org.jooq.Context) Tools.fieldNames(org.jooq.impl.Tools.fieldNames) TRUE(java.lang.Boolean.TRUE) DERBY(org.jooq.SQLDialect.DERBY) EMPTY_NAME(org.jooq.impl.Tools.EMPTY_NAME) HashSet(java.util.HashSet) SQLITE(org.jooq.SQLDialect.SQLITE) TABLE_REFERENCE(org.jooq.Clause.TABLE_REFERENCE) MYSQL(org.jooq.SQLDialect.MYSQL) TABLE_ALIAS(org.jooq.Clause.TABLE_ALIAS) Record(org.jooq.Record) DSL.asterisk(org.jooq.impl.DSL.asterisk) FIELD_ALIAS(org.jooq.Clause.FIELD_ALIAS) FIREBIRD(org.jooq.SQLDialect.FIREBIRD) RenderOptionalKeyword(org.jooq.conf.RenderOptionalKeyword) IGNITE(org.jooq.SQLDialect.IGNITE) YUGABYTEDB(org.jooq.SQLDialect.YUGABYTEDB) Tools.combine(org.jooq.impl.Tools.combine) NO_SUPPORT_VALUES(org.jooq.impl.Values.NO_SUPPORT_VALUES) H2(org.jooq.SQLDialect.H2) MARIADB(org.jooq.SQLDialect.MARIADB) Table(org.jooq.Table) SQLDialect(org.jooq.SQLDialect) Select(org.jooq.Select) List(java.util.List)

Aggregations

Table (org.jooq.Table)31 Field (org.jooq.Field)20 List (java.util.List)19 Record (org.jooq.Record)19 ArrayList (java.util.ArrayList)17 Set (java.util.Set)16 SQLDialect (org.jooq.SQLDialect)16 Map (java.util.Map)15 Collection (java.util.Collection)14 Condition (org.jooq.Condition)14 Select (org.jooq.Select)14 Configuration (org.jooq.Configuration)13 Name (org.jooq.Name)13 TableField (org.jooq.TableField)13 Arrays (java.util.Arrays)12 DSL.name (org.jooq.impl.DSL.name)12 Schema (org.jooq.Schema)11 StringUtils (org.jooq.tools.StringUtils)11 Catalog (org.jooq.Catalog)10 Row (org.jooq.Row)10