Search in sources :

Example 26 with Record

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

the class FirebirdTableDefinition method getElements0.

@Override
protected List<ColumnDefinition> getElements0() throws SQLException {
    List<ColumnDefinition> result = new ArrayList<ColumnDefinition>();
    Rdb$relationFields r = RDB$RELATION_FIELDS.as("r");
    Rdb$fields f = RDB$FIELDS.as("f");
    // DatabaseMetaData implementation
    for (Record record : create().select(r.RDB$FIELD_NAME.trim(), r.RDB$DESCRIPTION, r.RDB$DEFAULT_VALUE, DSL.bitOr(r.RDB$NULL_FLAG.nvl((short) 0), f.RDB$NULL_FLAG.nvl((short) 0)).as(r.RDB$NULL_FLAG), r.RDB$DEFAULT_SOURCE, r.RDB$FIELD_POSITION, // [#3342] FIELD_LENGTH should be ignored for LOBs
    CHARACTER_LENGTH(f).as("CHARACTER_LENGTH"), f.RDB$FIELD_PRECISION, FIELD_SCALE(f).as("FIELD_SCALE"), FIELD_TYPE(f).as("FIELD_TYPE"), f.RDB$FIELD_SUB_TYPE).from(r).leftOuterJoin(f).on(r.RDB$FIELD_SOURCE.eq(f.RDB$FIELD_NAME)).where(r.RDB$RELATION_NAME.eq(getName())).orderBy(r.RDB$FIELD_POSITION).fetch()) {
        DefaultDataTypeDefinition type = new DefaultDataTypeDefinition(getDatabase(), getSchema(), record.get("FIELD_TYPE", String.class), record.get("CHARACTER_LENGTH", short.class), record.get(f.RDB$FIELD_PRECISION), record.get("FIELD_SCALE", Integer.class), record.get(r.RDB$NULL_FLAG) == 0, record.get(r.RDB$DEFAULT_SOURCE));
        ColumnDefinition column = new DefaultColumnDefinition(getDatabase().getTable(getSchema(), getName()), record.get(r.RDB$FIELD_NAME.trim()), record.get(r.RDB$FIELD_POSITION), type, false, null);
        result.add(column);
    }
    return result;
}
Also used : Rdb$relationFields(org.jooq.util.firebird.rdb.tables.Rdb$relationFields) DefaultDataTypeDefinition(org.jooq.util.DefaultDataTypeDefinition) DefaultColumnDefinition(org.jooq.util.DefaultColumnDefinition) Rdb$fields(org.jooq.util.firebird.rdb.tables.Rdb$fields) ArrayList(java.util.ArrayList) Record(org.jooq.Record) ColumnDefinition(org.jooq.util.ColumnDefinition) DefaultColumnDefinition(org.jooq.util.DefaultColumnDefinition)

Example 27 with Record

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

the class FirebirdTableValuedFunction method getElements0.

@Override
protected List<ColumnDefinition> getElements0() throws SQLException {
    List<ColumnDefinition> result = new ArrayList<ColumnDefinition>();
    Rdb$procedureParameters p = RDB$PROCEDURE_PARAMETERS.as("p");
    Rdb$fields f = RDB$FIELDS.as("f");
    // DatabaseMetaData implementation
    for (Record record : create().select(p.RDB$PARAMETER_NUMBER, p.RDB$PARAMETER_NAME.trim(), p.RDB$DESCRIPTION, p.RDB$DEFAULT_VALUE, DSL.bitOr(p.RDB$NULL_FLAG.nvl((short) 0), f.RDB$NULL_FLAG.nvl((short) 0)).as(p.RDB$NULL_FLAG), p.RDB$DEFAULT_SOURCE, // [#3342] FIELD_LENGTH should be ignored for LOBs
    CHARACTER_LENGTH(f).as("CHARACTER_LENGTH"), f.RDB$FIELD_PRECISION, FIELD_SCALE(f).as("FIELD_SCALE"), FIELD_TYPE(f).as("FIELD_TYPE"), f.RDB$FIELD_SUB_TYPE).from(p).leftOuterJoin(f).on(p.RDB$FIELD_SOURCE.eq(f.RDB$FIELD_NAME)).where(p.RDB$PROCEDURE_NAME.eq(getName())).and(p.RDB$PARAMETER_TYPE.eq((short) 1)).orderBy(p.RDB$PARAMETER_NUMBER)) {
        DefaultDataTypeDefinition type = new DefaultDataTypeDefinition(getDatabase(), getSchema(), record.get("FIELD_TYPE", String.class), record.get("CHARACTER_LENGTH", short.class), record.get(f.RDB$FIELD_PRECISION), record.get("FIELD_SCALE", Integer.class), record.get(p.RDB$NULL_FLAG) == 0, record.get(p.RDB$DEFAULT_SOURCE));
        ColumnDefinition column = new DefaultColumnDefinition(getDatabase().getTable(getSchema(), getName()), record.get(p.RDB$PARAMETER_NAME.trim()), record.get(p.RDB$PARAMETER_NUMBER), type, false, null);
        result.add(column);
    }
    return result;
}
Also used : DefaultDataTypeDefinition(org.jooq.util.DefaultDataTypeDefinition) DefaultColumnDefinition(org.jooq.util.DefaultColumnDefinition) Rdb$fields(org.jooq.util.firebird.rdb.tables.Rdb$fields) ArrayList(java.util.ArrayList) Record(org.jooq.Record) Rdb$procedureParameters(org.jooq.util.firebird.rdb.tables.Rdb$procedureParameters) ColumnDefinition(org.jooq.util.ColumnDefinition) DefaultColumnDefinition(org.jooq.util.DefaultColumnDefinition)

Example 28 with Record

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

the class ParserImpl method parseSelect.

// -----------------------------------------------------------------------------------------------------------------
// Statement parsing
// -----------------------------------------------------------------------------------------------------------------
private static final SelectQueryImpl<Record> parseSelect(ParserContext ctx) {
    SelectQueryImpl<Record> result = parseQueryPrimary(ctx);
    CombineOperator combine;
    while ((combine = parseCombineOperatorIf(ctx)) != null) {
        switch(combine) {
            case UNION:
                result = (SelectQueryImpl<Record>) result.union(parseQueryPrimary(ctx));
                break;
            case UNION_ALL:
                result = (SelectQueryImpl<Record>) result.unionAll(parseQueryPrimary(ctx));
                break;
            case EXCEPT:
                result = (SelectQueryImpl<Record>) result.except(parseQueryPrimary(ctx));
                break;
            case EXCEPT_ALL:
                result = (SelectQueryImpl<Record>) result.exceptAll(parseQueryPrimary(ctx));
                break;
            case INTERSECT:
                result = (SelectQueryImpl<Record>) result.intersect(parseQueryPrimary(ctx));
                break;
            case INTERSECT_ALL:
                result = (SelectQueryImpl<Record>) result.intersectAll(parseQueryPrimary(ctx));
                break;
            default:
                ctx.unexpectedToken();
                break;
        }
    }
    if (parseKeywordIf(ctx, "ORDER"))
        if (parseKeywordIf(ctx, "SIBLINGS BY")) {
            result.addOrderBy(parseSortSpecification(ctx));
            result.setOrderBySiblings(true);
        } else if (parseKeywordIf(ctx, "BY"))
            result.addOrderBy(parseSortSpecification(ctx));
        else
            throw ctx.unexpectedToken();
    if (!result.getLimit().isApplicable()) {
        boolean offsetStandard = false;
        boolean offsetPostgres = false;
        if (parseKeywordIf(ctx, "OFFSET")) {
            result.addOffset((int) (long) parseUnsignedInteger(ctx));
            if (parseKeywordIf(ctx, "ROWS") || parseKeywordIf(ctx, "ROW"))
                offsetStandard = true;
            else
                offsetPostgres = true;
        }
        if (!offsetStandard && parseKeywordIf(ctx, "LIMIT")) {
            int limit = (int) (long) parseUnsignedInteger(ctx);
            if (!offsetPostgres && parseIf(ctx, ','))
                result.addLimit(limit, (int) (long) parseUnsignedInteger(ctx));
            else if (!offsetPostgres && parseKeywordIf(ctx, "OFFSET"))
                result.addLimit((int) (long) parseUnsignedInteger(ctx), limit);
            else
                result.addLimit(limit);
        } else if (!offsetPostgres && parseKeywordIf(ctx, "FETCH")) {
            if (!parseKeywordIf(ctx, "FIRST") && !parseKeywordIf(ctx, "NEXT"))
                throw ctx.unexpectedToken();
            result.addLimit((int) (long) parseUnsignedInteger(ctx));
            if (!parseKeywordIf(ctx, "ROWS ONLY") && !parseKeywordIf(ctx, "ROW ONLY"))
                throw ctx.unexpectedToken();
        }
    }
    return result;
}
Also used : Record(org.jooq.Record) Constraint(org.jooq.Constraint) DSL.constraint(org.jooq.impl.DSL.constraint)

Example 29 with Record

use of org.jooq.Record 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 30 with Record

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

the class ResultImpl method formatHTML.

@Override
public final void formatHTML(Writer writer) {
    try {
        writer.append("<table>");
        writer.append("<thead>");
        writer.append("<tr>");
        for (Field<?> field : fields.fields) {
            writer.append("<th>");
            writer.append(escapeXML(field.getName()));
            writer.append("</th>");
        }
        writer.append("</tr>");
        writer.append("</thead>");
        writer.append("<tbody>");
        for (Record record : this) {
            writer.append("<tr>");
            for (int index = 0; index < fields.fields.length; index++) {
                writer.append("<td>");
                writer.append(escapeXML(format0(record.getValue(index), false, true)));
                writer.append("</td>");
            }
            writer.append("</tr>");
        }
        writer.append("</tbody>");
        writer.append("</table>");
        writer.flush();
    } catch (java.io.IOException e) {
        throw new IOException("Exception while writing HTML", e);
    }
}
Also used : TableRecord(org.jooq.TableRecord) UpdatableRecord(org.jooq.UpdatableRecord) Record(org.jooq.Record) IOException(org.jooq.exception.IOException)

Aggregations

Record (org.jooq.Record)91 ArrayList (java.util.ArrayList)40 SchemaDefinition (org.jooq.util.SchemaDefinition)36 TableDefinition (org.jooq.util.TableDefinition)32 ColumnDefinition (org.jooq.util.ColumnDefinition)23 DefaultDataTypeDefinition (org.jooq.util.DefaultDataTypeDefinition)22 DataTypeDefinition (org.jooq.util.DataTypeDefinition)19 DefaultColumnDefinition (org.jooq.util.DefaultColumnDefinition)11 TableField (org.jooq.TableField)9 UpdatableRecord (org.jooq.UpdatableRecord)9 TableRecord (org.jooq.TableRecord)7 Connection (java.sql.Connection)5 StringUtils.defaultString (org.jooq.tools.StringUtils.defaultString)5 DefaultParameterDefinition (org.jooq.util.DefaultParameterDefinition)5 DefaultSequenceDefinition (org.jooq.util.DefaultSequenceDefinition)5 ParameterDefinition (org.jooq.util.ParameterDefinition)5 SequenceDefinition (org.jooq.util.SequenceDefinition)5 Schema (org.jooq.Schema)4 IOException (org.jooq.exception.IOException)4 PgNamespace (org.jooq.util.postgres.pg_catalog.tables.PgNamespace)4