Search in sources :

Example 1 with IOException

use of org.jooq.exception.IOException 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)

Example 2 with IOException

use of org.jooq.exception.IOException in project jOOQ by jOOQ.

the class ResultImpl method formatInsert.

@Override
public final void formatInsert(Writer writer, Table<?> table, Field<?>... f) {
    DSLContext ctx = DSL.using(configuration());
    try {
        for (R record : this) {
            writer.append(ctx.renderInlined(insertInto(table, f).values(record.intoArray())));
            writer.append(";\n");
        }
        writer.flush();
    } catch (java.io.IOException e) {
        throw new IOException("Exception while writing INSERTs", e);
    }
}
Also used : DSLContext(org.jooq.DSLContext) IOException(org.jooq.exception.IOException)

Example 3 with IOException

use of org.jooq.exception.IOException in project jOOQ by jOOQ.

the class ResultImpl method formatJSON.

@Override
public final void formatJSON(Writer writer, JSONFormat format) {
    try {
        String separator;
        int recordLevel = format.header() ? 2 : 1;
        if (format.header()) {
            if (format.format())
                writer.append('{').append(format.newline()).append(format.indentString(1)).append("\"fields\": [");
            else
                writer.append("{\"fields\":[");
            separator = "";
            for (Field<?> field : fields.fields) {
                writer.append(separator);
                if (format.format())
                    writer.append(format.newline()).append(format.indentString(2));
                writer.append('{');
                if (format.format())
                    writer.append(format.newline()).append(format.indentString(3));
                if (field instanceof TableField) {
                    Table<?> table = ((TableField<?, ?>) field).getTable();
                    if (table != null) {
                        Schema schema = table.getSchema();
                        if (schema != null) {
                            writer.append("\"schema\":");
                            if (format.format())
                                writer.append(' ');
                            JSONValue.writeJSONString(schema.getName(), writer);
                            writer.append(',');
                            if (format.format())
                                writer.append(format.newline()).append(format.indentString(3));
                        }
                        writer.append("\"table\":");
                        if (format.format())
                            writer.append(' ');
                        JSONValue.writeJSONString(table.getName(), writer);
                        writer.append(',');
                        if (format.format())
                            writer.append(format.newline()).append(format.indentString(3));
                    }
                }
                writer.append("\"name\":");
                if (format.format())
                    writer.append(' ');
                JSONValue.writeJSONString(field.getName(), writer);
                writer.append(',');
                if (format.format())
                    writer.append(format.newline()).append(format.indentString(3));
                writer.append("\"type\":");
                if (format.format())
                    writer.append(' ');
                JSONValue.writeJSONString(field.getDataType().getTypeName().toUpperCase(), writer);
                if (format.format())
                    writer.append(format.newline()).append(format.indentString(2));
                writer.append('}');
                separator = ",";
            }
            if (format.format())
                writer.append(format.newline()).append(format.indentString(1)).append("],").append(format.newline()).append(format.indentString(1)).append("\"records\": ");
            else
                writer.append("],\"records\":");
        }
        writer.append('[');
        separator = "";
        switch(format.recordFormat()) {
            case ARRAY:
                for (Record record : this) {
                    writer.append(separator);
                    if (format.format())
                        writer.append(format.newline());
                    formatJSONArray0(record, fields, format, recordLevel, writer);
                    separator = ",";
                }
                break;
            case OBJECT:
                for (Record record : this) {
                    writer.append(separator);
                    if (format.format())
                        writer.append(format.newline());
                    formatJSONMap0(record, fields, format, recordLevel, writer);
                    separator = ",";
                }
                break;
            default:
                throw new IllegalArgumentException("Format not supported: " + format);
        }
        if (format.format()) {
            writer.append(format.newline());
            if (format.header())
                writer.append(format.indentString(1));
        }
        writer.append(']');
        if (format.header())
            writer.append(format.newline()).append('}');
        writer.flush();
    } catch (java.io.IOException e) {
        throw new IOException("Exception while writing JSON", e);
    }
}
Also used : Schema(org.jooq.Schema) TableRecord(org.jooq.TableRecord) UpdatableRecord(org.jooq.UpdatableRecord) Record(org.jooq.Record) IOException(org.jooq.exception.IOException) TableField(org.jooq.TableField)

Example 4 with IOException

use of org.jooq.exception.IOException in project jOOQ by jOOQ.

the class ResultImpl method formatCSV.

@Override
public final void formatCSV(Writer writer, CSVFormat format) {
    try {
        if (format.header()) {
            String sep1 = "";
            for (Field<?> field : fields.fields) {
                writer.append(sep1);
                writer.append(formatCSV0(field.getName(), format));
                sep1 = format.delimiter();
            }
            writer.append(format.newline());
        }
        for (Record record : this) {
            String sep2 = "";
            for (int index = 0; index < fields.fields.length; index++) {
                writer.append(sep2);
                writer.append(formatCSV0(record.getValue(index), format));
                sep2 = format.delimiter();
            }
            writer.append(format.newline());
        }
        writer.flush();
    } catch (java.io.IOException e) {
        throw new IOException("Exception while writing CSV", e);
    }
}
Also used : TableRecord(org.jooq.TableRecord) UpdatableRecord(org.jooq.UpdatableRecord) Record(org.jooq.Record) IOException(org.jooq.exception.IOException)

Example 5 with IOException

use of org.jooq.exception.IOException in project jOOQ by jOOQ.

the class ResultImpl method formatXML.

@Override
public final void formatXML(Writer writer, XMLFormat format) {
    String newline = format.newline();
    int recordLevel = format.header() ? 2 : 1;
    try {
        writer.append("<result");
        if (format.xmlns())
            writer.append(" xmlns=\"" + Constants.NS_EXPORT + "\"");
        writer.append(">");
        if (format.header()) {
            writer.append(newline).append(format.indentString(1)).append("<fields>");
            for (Field<?> field : fields.fields) {
                writer.append(newline).append(format.indentString(2)).append("<field");
                if (field instanceof TableField) {
                    Table<?> table = ((TableField<?, ?>) field).getTable();
                    if (table != null) {
                        Schema schema = table.getSchema();
                        if (schema != null) {
                            writer.append(" schema=\"");
                            writer.append(escapeXML(schema.getName()));
                            writer.append("\"");
                        }
                        writer.append(" table=\"");
                        writer.append(escapeXML(table.getName()));
                        writer.append("\"");
                    }
                }
                writer.append(" name=\"");
                writer.append(escapeXML(field.getName()));
                writer.append("\"");
                writer.append(" type=\"");
                writer.append(field.getDataType().getTypeName().toUpperCase());
                writer.append("\"/>");
            }
            writer.append(newline).append(format.indentString(1)).append("</fields>");
            writer.append(newline).append(format.indentString(1)).append("<records>");
        }
        for (Record record : this) {
            writer.append(newline).append(format.indentString(recordLevel));
            formatXMLRecord(writer, format, recordLevel, record, fields);
        }
        if (format.header())
            writer.append(newline).append(format.indentString(1)).append("</records>");
        writer.append(newline).append("</result>");
        writer.flush();
    } catch (java.io.IOException e) {
        throw new IOException("Exception while writing XML", e);
    }
}
Also used : Schema(org.jooq.Schema) TableRecord(org.jooq.TableRecord) UpdatableRecord(org.jooq.UpdatableRecord) Record(org.jooq.Record) IOException(org.jooq.exception.IOException) TableField(org.jooq.TableField)

Aggregations

IOException (org.jooq.exception.IOException)6 Record (org.jooq.Record)4 TableRecord (org.jooq.TableRecord)4 UpdatableRecord (org.jooq.UpdatableRecord)4 Schema (org.jooq.Schema)2 TableField (org.jooq.TableField)2 ArrayList (java.util.ArrayList)1 DSLContext (org.jooq.DSLContext)1