Search in sources :

Example 76 with Field

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

the class ParserImpl method parseField.

private static final Field<?> parseField(ParserContext ctx, Type type) {
    if (B.is(type)) {
        Field<?> r = parseFieldAnd(ctx);
        Condition c = null;
        while (parseKeywordIf(ctx, "OR")) c = ((c == null) ? condition((Field) r) : c).or((Field) parseFieldAnd(ctx));
        return c == null ? r : field(c);
    } else {
        return parseFieldConcat(ctx, type);
    }
}
Also used : Condition(org.jooq.Condition) TableField(org.jooq.TableField) GroupField(org.jooq.GroupField) Field(org.jooq.Field) SortField(org.jooq.SortField)

Example 77 with Field

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

the class ParserImpl method parseAlterTable.

private static final DDLQuery parseAlterTable(ParserContext ctx) {
    boolean ifExists = parseKeywordIf(ctx, "IF EXISTS");
    Table<?> tableName = parseTableName(ctx);
    parseWhitespaceIf(ctx);
    AlterTableStep s1 = ifExists ? ctx.dsl.alterTableIfExists(tableName) : ctx.dsl.alterTable(tableName);
    switch(ctx.character()) {
        case 'a':
        case 'A':
            if (parseKeywordIf(ctx, "ADD")) {
                ConstraintTypeStep constraint = null;
                if (parseKeywordIf(ctx, "CONSTRAINT"))
                    constraint = constraint(parseIdentifier(ctx));
                if (parseKeywordIf(ctx, "PRIMARY KEY")) {
                    parse(ctx, '(');
                    Field<?>[] fieldNames = parseFieldNames(ctx).toArray(EMPTY_FIELD);
                    parse(ctx, ')');
                    return constraint == null ? s1.add(primaryKey(fieldNames)) : s1.add(constraint.primaryKey(fieldNames));
                } else if (parseKeywordIf(ctx, "UNIQUE")) {
                    parse(ctx, '(');
                    Field<?>[] fieldNames = parseFieldNames(ctx).toArray(EMPTY_FIELD);
                    parse(ctx, ')');
                    return constraint == null ? s1.add(unique(fieldNames)) : s1.add(constraint.unique(fieldNames));
                } else if (parseKeywordIf(ctx, "FOREIGN KEY")) {
                    parse(ctx, '(');
                    Field<?>[] referencing = parseFieldNames(ctx).toArray(EMPTY_FIELD);
                    parse(ctx, ')');
                    parseKeyword(ctx, "REFERENCES");
                    Table<?> referencedTable = parseTableName(ctx);
                    parse(ctx, '(');
                    Field<?>[] referencedFields = parseFieldNames(ctx).toArray(EMPTY_FIELD);
                    parse(ctx, ')');
                    if (referencing.length != referencedFields.length)
                        throw ctx.exception();
                    return constraint == null ? s1.add(foreignKey(referencing).references(referencedTable, referencedFields)) : s1.add(constraint.foreignKey(referencing).references(referencedTable, referencedFields));
                } else if (parseKeywordIf(ctx, "CHECK")) {
                    parse(ctx, '(');
                    Condition condition = parseCondition(ctx);
                    parse(ctx, ')');
                    return constraint == null ? s1.add(check(condition)) : s1.add(constraint.check(condition));
                } else if (constraint != null) {
                    throw ctx.unexpectedToken();
                } else {
                    parseKeywordIf(ctx, "COLUMN");
                    // The below code is taken from CREATE TABLE, with minor modifications as
                    // https://github.com/jOOQ/jOOQ/issues/5317 has not yet been implemented
                    // Once implemented, we might be able to factor out the common logic into
                    // a new parseXXX() method.
                    String fieldName = parseIdentifier(ctx);
                    DataType type = parseDataType(ctx);
                    boolean nullable = false;
                    boolean defaultValue = false;
                    boolean unique = false;
                    for (; ; ) {
                        if (!nullable) {
                            if (parseKeywordIf(ctx, "NULL")) {
                                type = type.nullable(true);
                                nullable = true;
                                continue;
                            } else if (parseKeywordIf(ctx, "NOT NULL")) {
                                type = type.nullable(false);
                                nullable = true;
                                continue;
                            }
                        }
                        if (!defaultValue) {
                            if (parseKeywordIf(ctx, "DEFAULT")) {
                                type = type.defaultValue(parseField(ctx));
                                defaultValue = true;
                                continue;
                            }
                        }
                        if (!unique) {
                            if (parseKeywordIf(ctx, "PRIMARY KEY")) {
                                throw ctx.unexpectedToken();
                            } else if (parseKeywordIf(ctx, "UNIQUE")) {
                                throw ctx.unexpectedToken();
                            }
                        }
                        if (parseKeywordIf(ctx, "CHECK")) {
                            throw ctx.unexpectedToken();
                        }
                        break;
                    }
                    return s1.add(field(name(fieldName), type), type);
                }
            }
            break;
        case 'd':
        case 'D':
            if (parseKeywordIf(ctx, "DROP")) {
                if (parseKeywordIf(ctx, "COLUMN")) {
                    Field<?> field = parseFieldName(ctx);
                    boolean cascade = parseKeywordIf(ctx, "CASCADE");
                    boolean restrict = !cascade && parseKeywordIf(ctx, "RESTRICT");
                    AlterTableDropStep s2 = s1.dropColumn(field);
                    AlterTableFinalStep s3 = cascade ? s2.cascade() : restrict ? s2.restrict() : s2;
                    return s3;
                } else if (parseKeywordIf(ctx, "CONSTRAINT")) {
                    String constraint = parseIdentifier(ctx);
                    return s1.dropConstraint(constraint);
                }
            }
            break;
        case 'r':
        case 'R':
            if (parseKeywordIf(ctx, "RENAME")) {
                if (parseKeywordIf(ctx, "TO")) {
                    String newName = parseIdentifier(ctx);
                    return s1.renameTo(newName);
                } else if (parseKeywordIf(ctx, "COLUMN")) {
                    String oldName = parseIdentifier(ctx);
                    parseKeyword(ctx, "TO");
                    String newName = parseIdentifier(ctx);
                    return s1.renameColumn(oldName).to(newName);
                } else if (parseKeywordIf(ctx, "CONSTRAINT")) {
                    String oldName = parseIdentifier(ctx);
                    parseKeyword(ctx, "TO");
                    String newName = parseIdentifier(ctx);
                    return s1.renameConstraint(oldName).to(newName);
                }
            }
            break;
    }
    throw ctx.unexpectedToken();
}
Also used : ConstraintTypeStep(org.jooq.ConstraintTypeStep) Condition(org.jooq.Condition) TableField(org.jooq.TableField) GroupField(org.jooq.GroupField) Field(org.jooq.Field) SortField(org.jooq.SortField) AlterTableFinalStep(org.jooq.AlterTableFinalStep) AlterTableStep(org.jooq.AlterTableStep) DataType(org.jooq.DataType) AlterTableDropStep(org.jooq.AlterTableDropStep)

Example 78 with Field

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

the class TableAlias method init.

/**
     * Register fields for this table alias
     */
@SuppressWarnings({ "rawtypes", "unchecked" })
private final Fields<R> init(String[] fieldAliases) {
    List<Field<?>> result = new ArrayList<Field<?>>();
    Row row = this.alias.wrapped().fieldsRow();
    int size = row.size();
    for (int i = 0; i < size; i++) {
        Field<?> field = row.field(i);
        String name = field.getName();
        if (fieldAliases != null && fieldAliases.length > i) {
            name = fieldAliases[i];
        }
        result.add(new TableFieldImpl(name, field.getDataType(), this, field.getComment(), field.getBinding()));
    }
    return new Fields<R>(result);
}
Also used : TableField(org.jooq.TableField) Field(org.jooq.Field) ArrayList(java.util.ArrayList) Row(org.jooq.Row)

Example 79 with Field

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

the class MergeImpl method getStandardMerge.

// -------------------------------------------------------------------------
// QueryPart API
// -------------------------------------------------------------------------
/**
     * Return a standard MERGE statement emulating the H2-specific syntax
     */
private final QueryPart getStandardMerge() {
    // The SRC for the USING() clause:
    // ------------------------------
    Table<?> src;
    if (upsertSelect != null) {
        List<Field<?>> v = new ArrayList<Field<?>>();
        Row row = upsertSelect.fieldsRow();
        for (int i = 0; i < row.size(); i++) {
            v.add(row.field(i).as("s" + (i + 1)));
        }
        // [#579] TODO: Currently, this syntax may require aliasing
        // on the call-site
        src = DSL.select(v).from(upsertSelect).asTable("src");
    } else {
        List<Field<?>> v = new ArrayList<Field<?>>();
        for (int i = 0; i < getUpsertValues().size(); i++) {
            v.add(getUpsertValues().get(i).as("s" + (i + 1)));
        }
        src = DSL.select(v).asTable("src");
    }
    // The condition for the ON clause:
    // --------------------------------
    Set<Field<?>> onFields = new HashSet<Field<?>>();
    Condition condition = null;
    if (getUpsertKeys().isEmpty()) {
        UniqueKey<?> key = table.getPrimaryKey();
        if (key != null) {
            onFields.addAll(key.getFields());
            for (int i = 0; i < key.getFields().size(); i++) {
                Condition rhs = key.getFields().get(i).equal((Field) src.field(i));
                if (condition == null) {
                    condition = rhs;
                } else {
                    condition = condition.and(rhs);
                }
            }
        } else // This should probably execute an INSERT statement
        {
            throw new IllegalStateException("Cannot omit KEY() clause on a non-Updatable Table");
        }
    } else {
        for (int i = 0; i < getUpsertKeys().size(); i++) {
            int matchIndex = getUpsertFields().indexOf(getUpsertKeys().get(i));
            if (matchIndex == -1) {
                throw new IllegalStateException("Fields in KEY() clause must be part of the fields specified in MERGE INTO table (...)");
            }
            onFields.addAll(getUpsertKeys());
            Condition rhs = getUpsertKeys().get(i).equal((Field) src.field(matchIndex));
            if (condition == null) {
                condition = rhs;
            } else {
                condition = condition.and(rhs);
            }
        }
    }
    // INSERT and UPDATE clauses
    // -------------------------
    Map<Field<?>, Field<?>> update = new LinkedHashMap<Field<?>, Field<?>>();
    Map<Field<?>, Field<?>> insert = new LinkedHashMap<Field<?>, Field<?>>();
    for (int i = 0; i < src.fieldsRow().size(); i++) {
        // Oracle does not allow to update fields from the ON clause
        if (!onFields.contains(getUpsertFields().get(i))) {
            update.put(getUpsertFields().get(i), src.field(i));
        }
        insert.put(getUpsertFields().get(i), src.field(i));
    }
    return DSL.mergeInto(table).using(src).on(condition).whenMatchedThenUpdate().set(update).whenNotMatchedThenInsert().set(insert);
}
Also used : Condition(org.jooq.Condition) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Field(org.jooq.Field) Row(org.jooq.Row) HashSet(java.util.HashSet)

Example 80 with Field

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

the class ParserImpl method parseUpdate.

private static final Update<?> parseUpdate(ParserContext ctx) {
    parseKeyword(ctx, "UPDATE");
    Table<?> tableName = parseTableName(ctx);
    parseKeyword(ctx, "SET");
    // TODO Row value expression updates
    Map<Field<?>, Object> map = parseSetClauseList(ctx);
    // TODO support FROM
    Condition condition = parseKeywordIf(ctx, "WHERE") ? parseCondition(ctx) : null;
    // TODO support RETURNING
    return condition == null ? ctx.dsl.update(tableName).set(map) : ctx.dsl.update(tableName).set(map).where(condition);
}
Also used : Condition(org.jooq.Condition) TableField(org.jooq.TableField) GroupField(org.jooq.GroupField) Field(org.jooq.Field) SortField(org.jooq.SortField)

Aggregations

Field (org.jooq.Field)146 TableField (org.jooq.TableField)93 SortField (org.jooq.SortField)74 GroupField (org.jooq.GroupField)66 SelectField (org.jooq.SelectField)56 Condition (org.jooq.Condition)54 Record (org.jooq.Record)43 ArrayList (java.util.ArrayList)38 Name (org.jooq.Name)31 List (java.util.List)28 Constraint (org.jooq.Constraint)28 Table (org.jooq.Table)28 SQLDialect (org.jooq.SQLDialect)27 Set (java.util.Set)26 Collection (java.util.Collection)25 Context (org.jooq.Context)25 Map (java.util.Map)24 Configuration (org.jooq.Configuration)24 DSLContext (org.jooq.DSLContext)24 Row (org.jooq.Row)24