Search in sources :

Example 16 with Field

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

the class ParserImpl method parseFieldCaseIf.

private static final Field<?> parseFieldCaseIf(ParserContext ctx) {
    if (parseKeywordIf(ctx, "CASE")) {
        if (parseKeywordIf(ctx, "WHEN")) {
            CaseConditionStep step = null;
            Field result;
            do {
                Condition condition = parseCondition(ctx);
                parseKeyword(ctx, "THEN");
                Field value = parseField(ctx);
                step = step == null ? when(condition, value) : step.when(condition, value);
            } while (parseKeywordIf(ctx, "WHEN"));
            if (parseKeywordIf(ctx, "ELSE"))
                result = step.otherwise(parseField(ctx));
            else
                result = step;
            parseKeyword(ctx, "END");
            return result;
        } else {
            CaseValueStep init = choose(parseField(ctx));
            CaseWhenStep step = null;
            Field result;
            parseKeyword(ctx, "WHEN");
            do {
                Field when = parseField(ctx);
                parseKeyword(ctx, "THEN");
                Field then = parseField(ctx);
                step = step == null ? init.when(when, then) : step.when(when, then);
            } while (parseKeywordIf(ctx, "WHEN"));
            if (parseKeywordIf(ctx, "ELSE"))
                result = step.otherwise(parseField(ctx));
            else
                result = step;
            parseKeyword(ctx, "END");
            return result;
        }
    }
    return null;
}
Also used : Condition(org.jooq.Condition) TableField(org.jooq.TableField) GroupField(org.jooq.GroupField) Field(org.jooq.Field) SortField(org.jooq.SortField) CaseValueStep(org.jooq.CaseValueStep) CaseWhenStep(org.jooq.CaseWhenStep) CaseConditionStep(org.jooq.CaseConditionStep)

Example 17 with Field

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

the class ParserImpl method parseFieldRoundIf.

private static final Field<?> parseFieldRoundIf(ParserContext ctx) {
    if (parseKeywordIf(ctx, "ROUND")) {
        Field<?> arg1 = null;
        Integer arg2 = null;
        parse(ctx, '(');
        arg1 = parseFieldSum(ctx, N);
        if (parseIf(ctx, ','))
            arg2 = (int) (long) parseUnsignedInteger(ctx);
        parse(ctx, ')');
        return arg2 == null ? round((Field) arg1) : round((Field) arg1, arg2);
    }
    return null;
}
Also used : BigInteger(java.math.BigInteger) TableField(org.jooq.TableField) GroupField(org.jooq.GroupField) Field(org.jooq.Field) SortField(org.jooq.SortField)

Example 18 with Field

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

the class ParserImpl method parseCreateTable.

private static final DDLQuery parseCreateTable(ParserContext ctx) {
    boolean ifNotExists = parseKeywordIf(ctx, "IF NOT EXISTS");
    Table<?> tableName = parseTableName(ctx);
    if (parseKeywordIf(ctx, "AS")) {
        Select<?> select = parseSelect(ctx);
        CreateTableAsStep<Record> s1 = ifNotExists ? ctx.dsl.createTableIfNotExists(tableName) : ctx.dsl.createTable(tableName);
        CreateTableFinalStep s2 = s1.as(select);
        return s2;
    } else {
        List<Field<?>> fields = new ArrayList<Field<?>>();
        List<Constraint> constraints = new ArrayList<Constraint>();
        boolean primary = false;
        boolean noConstraint = true;
        parse(ctx, '(');
        do {
            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((Field) parseField(ctx));
                        defaultValue = true;
                        continue;
                    }
                }
                if (!unique) {
                    if (parseKeywordIf(ctx, "PRIMARY KEY")) {
                        constraints.add(primaryKey(fieldName));
                        primary = true;
                        unique = true;
                        continue;
                    } else if (parseKeywordIf(ctx, "UNIQUE")) {
                        constraints.add(unique(fieldName));
                        unique = true;
                        continue;
                    }
                }
                if (parseKeywordIf(ctx, "CHECK")) {
                    parse(ctx, '(');
                    constraints.add(check(parseCondition(ctx)));
                    parse(ctx, ')');
                    continue;
                }
                break;
            }
            fields.add(field(name(fieldName), type));
        } while (parseIf(ctx, ',') && (noConstraint = !peekKeyword(ctx, "PRIMARY KEY") && !peekKeyword(ctx, "UNIQUE") && !peekKeyword(ctx, "FOREIGN KEY") && !peekKeyword(ctx, "CHECK") && !peekKeyword(ctx, "CONSTRAINT")));
        if (!noConstraint) {
            do {
                ConstraintTypeStep constraint = null;
                if (parseKeywordIf(ctx, "CONSTRAINT"))
                    constraint = constraint(parseIdentifier(ctx));
                if (parseKeywordIf(ctx, "PRIMARY KEY")) {
                    if (primary) {
                        throw ctx.exception();
                    } else {
                        primary = true;
                        parse(ctx, '(');
                        Field<?>[] fieldNames = parseFieldNames(ctx).toArray(EMPTY_FIELD);
                        parse(ctx, ')');
                        constraints.add(constraint == null ? primaryKey(fieldNames) : constraint.primaryKey(fieldNames));
                    }
                } else if (parseKeywordIf(ctx, "UNIQUE")) {
                    parse(ctx, '(');
                    Field<?>[] fieldNames = parseFieldNames(ctx).toArray(EMPTY_FIELD);
                    parse(ctx, ')');
                    constraints.add(constraint == null ? unique(fieldNames) : 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();
                    constraints.add(constraint == null ? foreignKey(referencing).references(referencedTable, referencedFields) : constraint.foreignKey(referencing).references(referencedTable, referencedFields));
                } else if (parseKeywordIf(ctx, "CHECK")) {
                    parse(ctx, '(');
                    Condition condition = parseCondition(ctx);
                    parse(ctx, ')');
                    constraints.add(constraint == null ? check(condition) : constraint.check(condition));
                } else {
                    throw ctx.unexpectedToken();
                }
            } while (parseIf(ctx, ','));
        }
        parse(ctx, ')');
        CreateTableAsStep<Record> s1 = ifNotExists ? ctx.dsl.createTableIfNotExists(tableName) : ctx.dsl.createTable(tableName);
        CreateTableColumnStep s2 = s1.columns(fields);
        CreateTableConstraintStep s3 = constraints.isEmpty() ? s2 : s2.constraints(constraints);
        return s3;
    }
}
Also used : CreateTableFinalStep(org.jooq.CreateTableFinalStep) ConstraintTypeStep(org.jooq.ConstraintTypeStep) Condition(org.jooq.Condition) CreateTableConstraintStep(org.jooq.CreateTableConstraintStep) Constraint(org.jooq.Constraint) ArrayList(java.util.ArrayList) TableField(org.jooq.TableField) GroupField(org.jooq.GroupField) Field(org.jooq.Field) SortField(org.jooq.SortField) CreateTableColumnStep(org.jooq.CreateTableColumnStep) Record(org.jooq.Record)

Example 19 with Field

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

the class RecordCondition method accept.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void accept(Context<?> ctx) {
    ConditionProviderImpl condition = new ConditionProviderImpl();
    int size = record.size();
    for (int i = 0; i < size; i++) {
        Object value = record.get(i);
        if (value != null) {
            Field f1 = record.field(i);
            Field f2 = DSL.val(value, f1.getDataType());
            condition.addConditions(f1.eq(f2));
        }
    }
    ctx.visit(condition);
}
Also used : Field(org.jooq.Field)

Example 20 with Field

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

the class JavaGenerator method printConvenienceMethodFunctionAsField.

protected void printConvenienceMethodFunctionAsField(JavaWriter out, RoutineDefinition function, boolean parametersAsField) {
    // [#281] - Java can't handle more than 255 method parameters
    if (function.getInParameters().size() > 254) {
        log.warn("Too many parameters", "Function " + function + " has more than 254 in parameters. Skipping generation of convenience method.");
        return;
    }
    // meaning
    if (parametersAsField && function.getInParameters().isEmpty()) {
        return;
    }
    final String className = out.ref(getStrategy().getFullJavaClassName(function));
    final String localVar = disambiguateJavaMemberName(function.getInParameters(), "f");
    out.tab(1).javadoc("Get <code>%s</code> as a field.", function.getQualifiedOutputName());
    if (scala)
        out.tab(1).print("def %s(", getStrategy().getJavaMethodName(function, Mode.DEFAULT));
    else
        out.tab(1).print("public static %s<%s> %s(", function.isAggregate() ? AggregateFunction.class : Field.class, out.ref(getJavaType(function.getReturnType())), getStrategy().getJavaMethodName(function, Mode.DEFAULT));
    String separator = "";
    for (ParameterDefinition parameter : function.getInParameters()) {
        out.print(separator);
        if (scala) {
            out.print("%s : ", getStrategy().getJavaMemberName(parameter));
            if (parametersAsField) {
                out.print("%s[%s]", Field.class, refExtendsNumberType(out, parameter.getType()));
            } else {
                out.print(refNumberType(out, parameter.getType()));
            }
        } else {
            if (parametersAsField) {
                out.print("%s<%s>", Field.class, refExtendsNumberType(out, parameter.getType()));
            } else {
                out.print(refNumberType(out, parameter.getType()));
            }
            out.print(" %s", getStrategy().getJavaMemberName(parameter));
        }
        separator = ", ";
    }
    if (scala) {
        out.println(") : %s[%s] = {", function.isAggregate() ? AggregateFunction.class : Field.class, out.ref(getJavaType(function.getReturnType())));
        out.tab(2).println("val %s = new %s", localVar, className);
    } else {
        out.println(") {");
        out.tab(2).println("%s %s = new %s();", className, localVar, className);
    }
    for (ParameterDefinition parameter : function.getInParameters()) {
        final String paramSetter = getStrategy().getJavaSetterName(parameter, Mode.DEFAULT);
        final String paramMember = getStrategy().getJavaMemberName(parameter);
        if (scala)
            out.tab(2).println("%s.%s(%s)", localVar, paramSetter, paramMember);
        else
            out.tab(2).println("%s.%s(%s);", localVar, paramSetter, paramMember);
    }
    out.println();
    if (scala)
        out.tab(2).println("return %s.as%s", localVar, function.isAggregate() ? "AggregateFunction" : "Field");
    else
        out.tab(2).println("return %s.as%s();", localVar, function.isAggregate() ? "AggregateFunction" : "Field");
    out.tab(1).println("}");
}
Also used : Field(org.jooq.Field) TableField(org.jooq.TableField) UDTField(org.jooq.UDTField) AggregateFunction(org.jooq.AggregateFunction) StringUtils.defaultString(org.jooq.tools.StringUtils.defaultString)

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