Search in sources :

Example 1 with Field

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

the class AbstractRecord method fromArray.

@Override
public final void fromArray(Object[] array, Field<?>... f) {
    Fields accept = new Fields(f);
    int size = fields.size();
    for (int i = 0; i < size && i < array.length; i++) {
        Field field = fields.field(i);
        if (accept.field(field) != null) {
            Tools.setValue(this, field, array[i]);
        }
    }
}
Also used : Field(org.jooq.Field)

Example 2 with Field

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

the class ParserImpl method parseMerge.

private static final Merge<?> parseMerge(ParserContext ctx) {
    parseKeyword(ctx, "MERGE INTO");
    Table<?> target = parseTableName(ctx);
    parseKeyword(ctx, "USING");
    parse(ctx, '(');
    TableLike<?> using = parseSelect(ctx);
    parse(ctx, ')');
    if (parseKeywordIf(ctx, "AS"))
        using = using.asTable(parseIdentifier(ctx));
    parseKeyword(ctx, "ON");
    Condition on = parseCondition(ctx);
    boolean update = false;
    boolean insert = false;
    List<Field<?>> insertColumns = null;
    List<Field<?>> insertValues = null;
    Map<Field<?>, Object> updateSet = null;
    for (; ; ) {
        if (!update && (update = parseKeywordIf(ctx, "WHEN MATCHED THEN UPDATE SET"))) {
            updateSet = parseSetClauseList(ctx);
        } else if (!insert && (insert = parseKeywordIf(ctx, "WHEN NOT MATCHED THEN INSERT"))) {
            parse(ctx, '(');
            insertColumns = Arrays.asList(Tools.fieldsByName(parseIdentifiers(ctx)));
            parse(ctx, ')');
            parseKeyword(ctx, "VALUES");
            parse(ctx, '(');
            insertValues = parseFields(ctx);
            parse(ctx, ')');
            if (insertColumns.size() != insertValues.size())
                throw ctx.exception();
        } else
            break;
    }
    if (!update && !insert)
        throw ctx.exception();
    // TODO support WHERE
    // TODO support multi clause MERGE
    // TODO support DELETE
    MergeMatchedStep<?> s1 = ctx.dsl.mergeInto(target).using(using).on(on);
    MergeNotMatchedStep<?> s2 = update ? s1.whenMatchedThenUpdate().set(updateSet) : s1;
    MergeFinalStep<?> s3 = insert ? s2.whenNotMatchedThenInsert(insertColumns).values(insertValues) : s2;
    return s3;
}
Also used : Condition(org.jooq.Condition) TableField(org.jooq.TableField) GroupField(org.jooq.GroupField) Field(org.jooq.Field) SortField(org.jooq.SortField)

Example 3 with Field

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

the class Fields method field.

@Override
@SuppressWarnings("unchecked")
public final <T> Field<T> field(Field<T> field) {
    if (field == null)
        return null;
    // [#4540] Try finding a match by identity
    for (Field<?> f : fields) if (f == field)
        return (Field<T>) f;
    // [#1802] Try finding an exact match (e.g. exact matching qualified name)
    for (Field<?> f : fields) if (f.equals(field))
        return (Field<T>) f;
    // [#4283] table / column matches are better than only column matches
    Field<?> columnMatch = null;
    Field<?> columnMatch2 = null;
    String tableName = tableName(field);
    String fieldName = field.getName();
    for (Field<?> f : fields) {
        String fName = f.getName();
        if (tableName != null) {
            String tName = tableName(f);
            if (tName != null && tableName.equals(tName) && fName.equals(fieldName))
                return (Field<T>) f;
        }
        // In case no exact match was found, return the first field with matching name
        if (fName.equals(fieldName)) {
            if (columnMatch == null)
                columnMatch = f;
            else
                // [#4476] [#4477] This might be unintentional from a user
                //                 perspective, e.g. when ambiguous ID columns are present.
                // [#5578] Finish the loop, though, as we might have an exact match
                //         despite some ambiguity
                columnMatch2 = f;
        }
    }
    if (columnMatch2 != null)
        if (log.isInfoEnabled())
            log.info("Ambiguous match found for " + fieldName + ". Both " + columnMatch + " and " + columnMatch2 + " match.", new SQLWarning());
    return (Field<T>) columnMatch;
}
Also used : SQLWarning(java.sql.SQLWarning) Field(org.jooq.Field) TableField(org.jooq.TableField)

Example 4 with Field

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

the class Function method toSQLArguments1.

final void toSQLArguments1(Context<?> ctx, QueryPartList<QueryPart> args) {
    if (distinct) {
        ctx.keyword("distinct");
        // [#2883] PostgreSQL can use the DISTINCT keyword with formal row value expressions.
        if (ctx.family() == POSTGRES && args.size() > 1) {
            ctx.sql('(');
        } else {
            ctx.sql(' ');
        }
    }
    if (!args.isEmpty()) {
        if (filter == null || HSQLDB == ctx.family() || POSTGRES_9_4.precedes(ctx.dialect())) {
            ctx.visit(args);
        } else {
            QueryPartList<Field<?>> expressions = new QueryPartList<Field<?>>();
            for (QueryPart argument : args) expressions.add(DSL.when(filter, argument == ASTERISK ? one() : argument));
            ctx.visit(expressions);
        }
    }
    if (distinct)
        if (ctx.family() == POSTGRES && args.size() > 1)
            ctx.sql(')');
    if (ignoreNulls) {
        ctx.sql(' ').keyword("ignore nulls");
    } else if (respectNulls) {
        ctx.sql(' ').keyword("respect nulls");
    }
}
Also used : Field(org.jooq.Field) SortField(org.jooq.SortField) QueryPart(org.jooq.QueryPart)

Example 5 with Field

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

the class ConstraintImpl method accept.

@Override
public final void accept(Context<?> ctx) {
    if (ctx.data(DATA_CONSTRAINT_REFERENCE) != null) {
        if (name == null)
            throw new DataAccessException("Cannot ALTER or DROP CONSTRAINT without name");
        ctx.visit(name);
    } else {
        boolean qualify = ctx.qualify();
        if (name != null)
            ctx.keyword("constraint").sql(' ').visit(name).formatIndentStart().formatSeparator();
        if (unique != null) {
            ctx.keyword("unique").sql(" (").qualify(false).visit(new QueryPartList<Field<?>>(unique)).qualify(qualify).sql(')');
        } else if (primaryKey != null) {
            ctx.keyword("primary key").sql(" (").qualify(false).visit(new QueryPartList<Field<?>>(primaryKey)).qualify(qualify).sql(')');
        } else if (foreignKey != null) {
            ctx.keyword("foreign key").sql(" (").qualify(false).visit(new QueryPartList<Field<?>>(foreignKey)).qualify(qualify).sql(')').formatSeparator().keyword("references").sql(' ').visit(referencesTable).sql(" (").qualify(false).visit(new QueryPartList<Field<?>>(references)).qualify(qualify).sql(')');
            if (onDelete != null)
                ctx.sql(' ').keyword("on delete").sql(' ').keyword(onDelete.sql);
            if (onUpdate != null)
                ctx.sql(' ').keyword("on update").sql(' ').keyword(onUpdate.sql);
        } else if (check != null) {
            ctx.keyword("check").sql(" (").qualify(false).visit(check).qualify(qualify).sql(')');
        }
        ctx.formatIndentEnd();
    }
}
Also used : Field(org.jooq.Field) DataAccessException(org.jooq.exception.DataAccessException)

Aggregations

Field (org.jooq.Field)135 TableField (org.jooq.TableField)92 SortField (org.jooq.SortField)74 GroupField (org.jooq.GroupField)66 SelectField (org.jooq.SelectField)56 Condition (org.jooq.Condition)54 Record (org.jooq.Record)39 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