Search in sources :

Example 11 with Field

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

the class CreateViewImpl method accept0.

private final void accept0(Context<?> ctx) {
    // [#3835] SQLite doesn't like renaming columns at the view level
    boolean rename = fields != null && fields.length > 0;
    boolean renameSupported = ctx.family() != SQLITE;
    // [#4806] CREATE VIEW doesn't accept parameters in most databases
    ParamType paramType = ctx.paramType();
    ctx.start(CREATE_VIEW_NAME).keyword("create view").sql(' ');
    if (ifNotExists && supportsIfNotExists(ctx))
        ctx.keyword("if not exists").sql(' ');
    ctx.visit(view);
    if (rename && renameSupported) {
        boolean qualify = ctx.qualify();
        ctx.sql('(').qualify(false).visit(new QueryPartList<Field<?>>(fields)).qualify(qualify).sql(')');
    }
    ctx.end(CREATE_VIEW_NAME).formatSeparator().keyword("as").formatSeparator().start(CREATE_VIEW_AS).paramType(INLINED).visit(rename && !renameSupported ? selectFrom(table(select).as("t", Tools.fieldNames(fields))) : select).paramType(paramType).end(CREATE_VIEW_AS);
}
Also used : Field(org.jooq.Field) ParamType(org.jooq.conf.ParamType)

Example 12 with Field

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

the class ParserImpl method parseFieldAnd.

private static final Field<?> parseFieldAnd(ParserContext ctx) {
    Field<?> r = parseFieldCondition(ctx);
    Condition c = null;
    while (parseKeywordIf(ctx, "AND")) c = ((c == null) ? condition((Field) r) : c).and((Field) parseFieldCondition(ctx));
    return c == null ? r : field(c);
}
Also used : Condition(org.jooq.Condition) TableField(org.jooq.TableField) GroupField(org.jooq.GroupField) Field(org.jooq.Field) SortField(org.jooq.SortField)

Example 13 with Field

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

the class TableRecordImpl method storeInsert0.

final int storeInsert0(Field<?>[] storeFields) {
    DSLContext create = create();
    InsertQuery<R> insert = create.insertQuery(getTable());
    addChangedValues(storeFields, insert);
    // Don't store records if no value was set by client code
    if (!insert.isExecutable()) {
        if (log.isDebugEnabled())
            log.debug("Query is not executable", insert);
        return 0;
    }
    // [#1596] Set timestamp and/or version columns to appropriate values
    BigInteger version = addRecordVersion(insert);
    Timestamp timestamp = addRecordTimestamp(insert);
    // [#814] Refresh identity and/or main unique key values
    // [#1002] Consider also identity columns of non-updatable records
    // [#1537] Avoid refreshing identity columns on batch inserts
    Collection<Field<?>> key = setReturningIfNeeded(insert);
    int result = insert.execute();
    if (result > 0) {
        for (Field<?> storeField : storeFields) changed(storeField, false);
        // [#1596] If insert was successful, update timestamp and/or version columns
        setRecordVersionAndTimestamp(version, timestamp);
        // [#1859] If an insert was successful try fetching the generated values.
        getReturningIfNeeded(insert, key);
        fetched = true;
    }
    return result;
}
Also used : Field(org.jooq.Field) TableField(org.jooq.TableField) DSLContext(org.jooq.DSLContext) BigInteger(java.math.BigInteger) Timestamp(java.sql.Timestamp)

Example 14 with Field

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

the class UpdatableRecordImpl method storeUpdate0.

private final int storeUpdate0(Field<?>[] storeFields, TableField<R, ?>[] keys) {
    UpdateQuery<R> update = create().updateQuery(getTable());
    addChangedValues(storeFields, update);
    Tools.addConditions(update, this, keys);
    // Don't store records if no value was set by client code
    if (!update.isExecutable()) {
        if (log.isDebugEnabled())
            log.debug("Query is not executable", update);
        return 0;
    }
    // [#1596] Set timestamp and/or version columns to appropriate values
    BigInteger version = addRecordVersion(update);
    Timestamp timestamp = addRecordTimestamp(update);
    if (isExecuteWithOptimisticLocking()) {
        // [#1596] Add additional conditions for version and/or timestamp columns
        if (isTimestampOrVersionAvailable()) {
            addConditionForVersionAndTimestamp(update);
        } else // [#5384] Do this only if the exclusion flag for unversioned records is off
        if (isExecuteWithOptimisticLockingIncludeUnversioned()) {
            checkIfChanged(keys);
        }
    }
    // [#1596] Check if the record was really changed in the database
    // [#1859] Specify the returning clause if needed
    Collection<Field<?>> key = setReturningIfNeeded(update);
    int result = update.execute();
    checkIfChanged(result, version, timestamp);
    if (result > 0) {
        for (Field<?> storeField : storeFields) changed(storeField, false);
        // [#1859] If an update was successful try fetching the generated
        getReturningIfNeeded(update, key);
    }
    return result;
}
Also used : Field(org.jooq.Field) TableField(org.jooq.TableField) BigInteger(java.math.BigInteger) Timestamp(java.sql.Timestamp)

Example 15 with Field

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

the class ParserImpl method parseBooleanPrimary.

private static final Condition parseBooleanPrimary(ParserContext ctx) {
    if (parseIf(ctx, '(')) {
        Condition result = parseCondition(ctx);
        parse(ctx, ')');
        return result;
    }
    TruthValue truth = parseTruthValueIf(ctx);
    if (truth != null) {
        Comparator comp = parseComparatorIf(ctx);
        switch(truth) {
            case TRUE:
                return comp == null ? condition(true) : inline(true).compare(comp, (Field<Boolean>) parseField(ctx));
            case FALSE:
                return comp == null ? condition(false) : inline(false).compare(comp, (Field<Boolean>) parseField(ctx));
            case NULL:
                return comp == null ? condition((Boolean) null) : inline((Boolean) null).compare(comp, (Field<Boolean>) parseField(ctx));
            default:
                throw ctx.exception();
        }
    }
    return parsePredicate(ctx);
}
Also used : Condition(org.jooq.Condition) TableField(org.jooq.TableField) GroupField(org.jooq.GroupField) Field(org.jooq.Field) SortField(org.jooq.SortField) Comparator(org.jooq.Comparator)

Aggregations

Field (org.jooq.Field)38 TableField (org.jooq.TableField)22 SortField (org.jooq.SortField)15 Condition (org.jooq.Condition)14 GroupField (org.jooq.GroupField)14 ArrayList (java.util.ArrayList)8 DSLContext (org.jooq.DSLContext)5 BigInteger (java.math.BigInteger)4 Comparator (org.jooq.Comparator)3 Record (org.jooq.Record)3 SQLException (java.sql.SQLException)2 Timestamp (java.sql.Timestamp)2 List (java.util.List)2 ConstraintTypeStep (org.jooq.ConstraintTypeStep)2 Name (org.jooq.Name)2 Row (org.jooq.Row)2 Table (org.jooq.Table)2 UDTField (org.jooq.UDTField)2 DataAccessException (org.jooq.exception.DataAccessException)2 ResultSetMetaData (java.sql.ResultSetMetaData)1