Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.jooq.impl.ResultImpl Maven / Gradle / Ivy
/*
* Copyright (c) 2009-2016, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static org.jooq.impl.DSL.insertInto;
import static org.jooq.impl.DSL.name;
import static org.jooq.impl.DSL.table;
import static org.jooq.impl.Tools.indexOrFail;
import static org.jooq.tools.StringUtils.abbreviate;
import static org.jooq.tools.StringUtils.leftPad;
import static org.jooq.tools.StringUtils.rightPad;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Array;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import javax.xml.bind.DatatypeConverter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.jooq.AttachableInternal;
import org.jooq.CSVFormat;
import org.jooq.Configuration;
import org.jooq.Converter;
import org.jooq.DSLContext;
import org.jooq.DataType;
import org.jooq.EnumType;
import org.jooq.Field;
import org.jooq.ForeignKey;
import org.jooq.JSONFormat;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.Record1;
import org.jooq.Record10;
import org.jooq.Record11;
import org.jooq.Record12;
import org.jooq.Record13;
import org.jooq.Record14;
import org.jooq.Record15;
import org.jooq.Record16;
import org.jooq.Record17;
import org.jooq.Record18;
import org.jooq.Record19;
import org.jooq.Record2;
import org.jooq.Record20;
import org.jooq.Record21;
import org.jooq.Record22;
import org.jooq.Record3;
import org.jooq.Record4;
import org.jooq.Record5;
import org.jooq.Record6;
import org.jooq.Record7;
import org.jooq.Record8;
import org.jooq.Record9;
import org.jooq.RecordHandler;
import org.jooq.RecordMapper;
import org.jooq.RecordType;
import org.jooq.Result;
import org.jooq.Row;
import org.jooq.Schema;
import org.jooq.Table;
import org.jooq.TableField;
import org.jooq.TableRecord;
import org.jooq.UpdatableRecord;
import org.jooq.exception.IOException;
import org.jooq.exception.InvalidResultException;
import org.jooq.tools.Convert;
import org.jooq.tools.StringUtils;
import org.jooq.tools.jdbc.MockResultSet;
import org.jooq.tools.json.JSONArray;
import org.jooq.tools.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
/**
* @author Lukas Eder
* @author Ivan Dugic
*/
@SuppressWarnings({"rawtypes", "unchecked" })
final class ResultImpl implements Result, AttachableInternal {
/**
* Generated UID
*/
private static final long serialVersionUID = 6416154375799578362L;
private Configuration configuration;
private final Fields fields;
private final List records;
ResultImpl(Configuration configuration, Collection extends Field>> fields) {
this(configuration, new Fields(fields));
}
ResultImpl(Configuration configuration, Field>... fields) {
this(configuration, new Fields(fields));
}
ResultImpl(Configuration configuration, Fields fields) {
this.configuration = configuration;
this.fields = fields;
this.records = new ArrayList();
}
// -------------------------------------------------------------------------
// XXX: Attachable API
// -------------------------------------------------------------------------
@Override
public final void attach(Configuration c) {
this.configuration = c;
for (R record : records)
if (record != null)
record.attach(c);
}
@Override
public final void detach() {
attach(null);
}
@Override
public final Configuration configuration() {
return configuration;
}
// -------------------------------------------------------------------------
// XXX: Result API
// -------------------------------------------------------------------------
@Override
public final RecordType recordType() {
return fields;
}
@Override
public final Row fieldsRow() {
return new RowImpl(fields);
}
@Override
public final Field field(Field field) {
return fields.field(field);
}
@Override
public final Field> field(String name) {
return fields.field(name);
}
@Override
public final Field field(String name, Class type) {
return fields.field(name, type);
}
@Override
public final Field field(String name, DataType dataType) {
return fields.field(name, dataType);
}
@Override
public final Field> field(Name name) {
return fields.field(name);
}
@Override
public final Field field(Name name, Class type) {
return fields.field(name, type);
}
@Override
public final Field field(Name name, DataType dataType) {
return fields.field(name, dataType);
}
@Override
public final Field> field(int index) {
return fields.field(index);
}
@Override
public final Field field(int index, Class type) {
return fields.field(index, type);
}
@Override
public final Field field(int index, DataType dataType) {
return fields.field(index, dataType);
}
@Override
public final Field>[] fields() {
return fields.fields().clone();
}
@Override
public final Field>[] fields(Field>... f) {
return fields.fields(f);
}
@Override
public final Field>[] fields(int... indexes) {
return fields.fields(indexes);
}
@Override
public final Field>[] fields(String... names) {
return fields.fields(names);
}
@Override
public final Field>[] fields(Name... names) {
return fields.fields(names);
}
@Override
public final boolean isEmpty() {
return records.isEmpty();
}
@Override
public final boolean isNotEmpty() {
return !records.isEmpty();
}
@Override
public final T getValue(int index, Field field) {
return get(index).get(field);
}
@Override
@Deprecated
public final T getValue(int index, Field field, T defaultValue) {
return get(index).getValue(field, defaultValue);
}
@Override
public final Object getValue(int index, int fieldIndex) {
return get(index).get(fieldIndex);
}
@Override
@Deprecated
public final Object getValue(int index, int fieldIndex, Object defaultValue) {
return get(index).getValue(fieldIndex, defaultValue);
}
@Override
public final Object getValue(int index, String fieldName) {
return get(index).get(fieldName);
}
@Override
@Deprecated
public final Object getValue(int index, String fieldName, Object defaultValue) {
return get(index).getValue(fieldName, defaultValue);
}
@Override
public final List getValues(Field field) {
return (List) getValues(indexOrFail(fieldsRow(), field));
}
@Override
public final List getValues(Field> field, Class extends T> type) {
return Convert.convert(getValues(field), type);
}
@Override
public final List getValues(Field field, Converter super T, ? extends U> converter) {
return Convert.convert(getValues(field), converter);
}
@Override
public final List> getValues(int fieldIndex) {
List result = new ArrayList(size());
for (R record : this) {
result.add(record.get(fieldIndex));
}
return result;
}
@Override
public final List getValues(int fieldIndex, Class extends T> type) {
return Convert.convert(getValues(fieldIndex), type);
}
@Override
public final List getValues(int fieldIndex, Converter, ? extends U> converter) {
return Convert.convert(getValues(fieldIndex), converter);
}
@Override
public final List> getValues(String fieldName) {
return getValues(field(fieldName));
}
@Override
public final List getValues(String fieldName, Class extends T> type) {
return Convert.convert(getValues(fieldName), type);
}
@Override
public final List getValues(String fieldName, Converter, ? extends U> converter) {
return Convert.convert(getValues(fieldName), converter);
}
@Override
public final List> getValues(Name fieldName) {
return getValues(field(fieldName));
}
@Override
public final List getValues(Name fieldName, Class extends T> type) {
return Convert.convert(getValues(fieldName), type);
}
@Override
public final List getValues(Name fieldName, Converter, ? extends U> converter) {
return Convert.convert(getValues(fieldName), converter);
}
final void addRecord(R record) {
records.add(record);
}
@Override
public final String format() {
StringWriter writer = new StringWriter();
format(writer);
return writer.toString();
}
@Override
public final void format(OutputStream stream) {
format(new OutputStreamWriter(stream));
}
@Override
public final void format(Writer writer) {
format(writer, 50);
}
@Override
public final String format(int maxRecords) {
StringWriter writer = new StringWriter();
format(writer, maxRecords);
return writer.toString();
}
@Override
public final void format(OutputStream stream, int maxRecords) {
format(new OutputStreamWriter(stream), maxRecords);
}
@Override
public final void format(Writer writer, int maxRecords) {
try {
final int COL_MIN_WIDTH = 4;
final int COL_MAX_WIDTH = 50;
// Numeric columns have greater max width because values are aligned
final int NUM_COL_MAX_WIDTH = 100;
// The max number of records that will be considered for formatting purposes
final int MAX_RECORDS = min(50, maxRecords);
// Get max decimal places for numeric type columns
final int[] decimalPlaces = new int[fields.fields.length];
final int[] widths = new int[fields.fields.length];
for (int index = 0; index < fields.fields.length; index++) {
if (Number.class.isAssignableFrom(fields.fields[index].getType())) {
List decimalPlacesList = new ArrayList();
// Initialize
decimalPlacesList.add(0);
// Collect all decimal places for the column values
String value;
for (int i = 0; i < min(MAX_RECORDS, size()); i++) {
value = format0(getValue(i, index), get(i).changed(index), true);
decimalPlacesList.add(getDecimalPlaces(value));
}
// Find max
decimalPlaces[index] = Collections.max(decimalPlacesList);
}
}
// Get max column widths
int colMaxWidth;
for (int index = 0; index < fields.fields.length; index++) {
// Is number column?
boolean isNumCol = Number.class.isAssignableFrom(fields.fields[index].getType());
colMaxWidth = isNumCol ? NUM_COL_MAX_WIDTH : COL_MAX_WIDTH;
// Collect all widths for the column
List widthList = new ArrayList();
// Add column name width first
widthList.add(min(colMaxWidth, max(COL_MIN_WIDTH, fields.fields[index].getName().length())));
// Add column values width
String value;
for (int i = 0; i < min(MAX_RECORDS, size()); i++) {
value = format0(getValue(i, index), get(i).changed(index), true);
// Align number values before width is calculated
if (isNumCol) {
value = alignNumberValue(decimalPlaces[index], value);
}
widthList.add(min(colMaxWidth, value.length()));
}
// Find max
widths[index] = Collections.max(widthList);
}
// Begin the writing
// ---------------------------------------------------------------------
// Write top line
writer.append("+");
for (int index = 0; index < fields.fields.length; index++) {
writer.append(rightPad("", widths[index], "-"));
writer.append("+");
}
// Write headers
writer.append("\n|");
for (int index = 0; index < fields.fields.length; index++) {
String padded;
if (Number.class.isAssignableFrom(fields.fields[index].getType())) {
padded = leftPad(fields.fields[index].getName(), widths[index]);
}
else {
padded = rightPad(fields.fields[index].getName(), widths[index]);
}
writer.append(abbreviate(padded, widths[index]));
writer.append("|");
}
// Write separator
writer.append("\n+");
for (int index = 0; index < fields.fields.length; index++) {
writer.append(rightPad("", widths[index], "-"));
writer.append("+");
}
// Write columns
for (int i = 0; i < min(maxRecords, size()); i++) {
writer.append("\n|");
for (int index = 0; index < fields.fields.length; index++) {
String value = format0(getValue(i, index), get(i).changed(index), true).replace("\n", "{lf}").replace("\r", "{cr}");
String padded;
if (Number.class.isAssignableFrom(fields.fields[index].getType())) {
// Align number value before left pad
value = alignNumberValue(decimalPlaces[index], value);
// Left pad
padded = leftPad(value, widths[index]);
}
else {
// Right pad
padded = rightPad(value, widths[index]);
}
writer.append(abbreviate(padded, widths[index]));
writer.append("|");
}
}
// Write bottom line
if (size() > 0) {
writer.append("\n+");
for (int index = 0; index < fields.fields.length; index++) {
writer.append(rightPad("", widths[index], "-"));
writer.append("+");
}
}
// Write truncation message, if applicable
if (maxRecords < size()) {
writer.append("\n|...");
writer.append("" + (size() - maxRecords));
writer.append(" record(s) truncated...");
}
writer.flush();
}
catch (java.io.IOException e) {
throw new IOException("Exception while writing TEXT", e);
}
}
private static final String alignNumberValue(Integer columnDecimalPlaces, String value) {
if (!"{null}".equals(value) && columnDecimalPlaces != 0) {
int decimalPlaces = getDecimalPlaces(value);
int rightPadSize = value.length() + columnDecimalPlaces - decimalPlaces;
if (decimalPlaces == 0) {
// If integer value, add one for decimal point
value = rightPad(value, rightPadSize + 1);
}
else {
value = rightPad(value, rightPadSize);
}
}
return value;
}
private static final int getDecimalPlaces(String value) {
int decimalPlaces = 0;
int dotIndex = value.indexOf(".");
if (dotIndex != -1) {
decimalPlaces = value.length() - dotIndex - 1;
}
return decimalPlaces;
}
@Override
public final String formatHTML() {
StringWriter writer = new StringWriter();
formatHTML(writer);
return writer.toString();
}
@Override
public final void formatHTML(OutputStream stream) {
formatHTML(new OutputStreamWriter(stream));
}
@Override
public final void formatHTML(Writer writer) {
try {
writer.append("");
writer.append("");
writer.append("");
for (Field> field : fields.fields) {
writer.append("");
writer.append(escapeXML(field.getName()));
writer.append(" ");
}
writer.append(" ");
writer.append(" ");
writer.append("");
for (Record record : this) {
writer.append("");
for (int index = 0; index < fields.fields.length; index++) {
writer.append("");
writer.append(escapeXML(format0(record.getValue(index), false, true)));
writer.append(" ");
}
writer.append(" ");
}
writer.append(" ");
writer.append("
");
writer.flush();
}
catch (java.io.IOException e) {
throw new IOException("Exception while writing HTML", e);
}
}
@Override
public final String formatCSV() {
return formatCSV(true);
}
@Override
public final String formatCSV(boolean header) {
StringWriter writer = new StringWriter();
formatCSV(writer, header);
return writer.toString();
}
@Override
public final void formatCSV(OutputStream stream) {
formatCSV(stream, true);
}
@Override
public final void formatCSV(OutputStream stream, boolean header) {
formatCSV(new OutputStreamWriter(stream), header);
}
@Override
public final void formatCSV(Writer writer) {
formatCSV(writer, true);
}
@Override
public final void formatCSV(Writer writer, boolean header) {
formatCSV(writer, header, ',', "\"\"");
}
@Override
public final String formatCSV(char delimiter) {
return formatCSV(true, delimiter);
}
@Override
public final String formatCSV(boolean header, char delimiter) {
StringWriter writer = new StringWriter();
formatCSV(writer, delimiter);
return writer.toString();
}
@Override
public final void formatCSV(OutputStream stream, char delimiter) {
formatCSV(stream, true, delimiter);
}
@Override
public final void formatCSV(OutputStream stream, boolean header, char delimiter) {
formatCSV(new OutputStreamWriter(stream), delimiter);
}
@Override
public final void formatCSV(Writer writer, char delimiter) {
formatCSV(writer, true, delimiter);
}
@Override
public final void formatCSV(Writer writer, boolean header, char delimiter) {
formatCSV(writer, header, delimiter, "\"\"");
}
@Override
public final String formatCSV(char delimiter, String nullString) {
return formatCSV(true, delimiter, nullString);
}
@Override
public final String formatCSV(boolean header, char delimiter, String nullString) {
StringWriter writer = new StringWriter();
formatCSV(writer, header, delimiter, nullString);
return writer.toString();
}
@Override
public final String formatCSV(CSVFormat format) {
StringWriter writer = new StringWriter();
formatCSV(writer, format);
return writer.toString();
}
@Override
public final void formatCSV(OutputStream stream, char delimiter, String nullString) {
formatCSV(stream, true, delimiter, nullString);
}
@Override
public final void formatCSV(OutputStream stream, boolean header, char delimiter, String nullString) {
formatCSV(new OutputStreamWriter(stream), header, delimiter, nullString);
}
@Override
public final void formatCSV(OutputStream stream, CSVFormat format) {
formatCSV(new OutputStreamWriter(stream), format);
}
@Override
public final void formatCSV(Writer writer, char delimiter, String nullString) {
formatCSV(writer, true, delimiter, nullString);
}
@Override
public final void formatCSV(Writer writer, boolean header, char delimiter, String nullString) {
formatCSV(writer, new CSVFormat().header(header).delimiter(delimiter).nullString(nullString));
}
@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);
}
}
private final String formatCSV0(Object value, CSVFormat format) {
// [#4746] Escape null and empty strings
if (value == null)
return format.nullString();
if ("".equals(value.toString()))
return format.emptyString();
String result = format0(value, false, false);
switch (format.quote()) {
case NEVER:
return result;
case SPECIAL_CHARACTERS:
if (!StringUtils.containsAny(result, ',', ';', '\t', '"', '\n', '\r', '\'', '\\'))
return result;
// no break
case ALWAYS:
default:
return format.quoteString()
+ result.replace("\\", "\\\\")
.replace(format.quoteString(), format.quoteString() + format.quoteString())
+ format.quoteString();
}
}
private final Object formatJSON0(Object value) {
if (value instanceof byte[])
return DatatypeConverter.printBase64Binary((byte[]) value);
return value;
}
/**
* @param value The value to be formatted
* @param visual Whether the formatted output is to be consumed visually
* (HTML, TEXT) or by a machine (CSV, JSON, XML)
*/
private static final String format0(Object value, boolean changed, boolean visual) {
String formatted = changed && visual ? "*" : "";
if (value == null) {
formatted += visual ? "{null}" : null;
}
else if (value.getClass() == byte[].class) {
formatted += DatatypeConverter.printBase64Binary((byte[]) value);
}
else if (value.getClass().isArray()) {
formatted += Arrays.toString((Object[]) value);
}
else if (value instanceof EnumType) {
formatted += ((EnumType) value).getLiteral();
}
else if (value instanceof Record) {
formatted += ((Record) value).valuesRow().toString();
}
// [#5238] Oracle DATE is really a TIMESTAMP(0)...
else if (value instanceof Date) {
String date = value.toString();
if (Date.valueOf(date).equals(value))
formatted += date;
else
formatted += new Timestamp(((Date) value).getTime());
}
else {
formatted += value.toString();
}
return formatted;
}
@Override
public final String formatJSON() {
StringWriter writer = new StringWriter();
formatJSON(writer);
return writer.toString();
}
@Override
public final String formatJSON(JSONFormat format) {
StringWriter writer = new StringWriter();
formatJSON(writer, format);
return writer.toString();
}
@Override
public final void formatJSON(OutputStream stream) {
formatJSON(new OutputStreamWriter(stream));
}
@Override
public final void formatJSON(OutputStream stream, JSONFormat format) {
formatJSON(new OutputStreamWriter(stream), format);
}
@Override
public final void formatJSON(Writer writer) {
formatJSON(writer, new JSONFormat());
}
@Override
public final void formatJSON(Writer writer, JSONFormat format) {
try {
List> f = null;
List r = new ArrayList();
if (format.header()) {
f = new ArrayList>();
for (Field> field : fields.fields) {
Map fieldMap = new LinkedHashMap();
if (field instanceof TableField) {
Table> table = ((TableField, ?>) field).getTable();
if (table != null) {
Schema schema = table.getSchema();
if (schema != null)
fieldMap.put("schema", schema.getName());
fieldMap.put("table", table.getName());
}
}
fieldMap.put("name", field.getName());
fieldMap.put("type", field.getDataType().getTypeName().toUpperCase());
f.add(fieldMap);
}
}
switch (format.recordFormat()) {
case ARRAY:
for (Record record : this) {
List list = new ArrayList();
for (int index = 0; index < fields.fields.length; index++)
list.add(formatJSON0(record.get(index)));
r.add(list);
}
break;
case OBJECT:
for (Record record : this) {
Map map = new LinkedHashMap();
for (int index = 0; index < fields.fields.length; index++)
map.put(record.field(index).getName(), formatJSON0(record.get(index)));
r.add(map);
}
break;
default:
throw new IllegalArgumentException("Format not supported: " + format);
}
if (f == null) {
writer.append(JSONArray.toJSONString(r));
}
else {
Map> map = new LinkedHashMap>();
map.put("fields", f);
map.put("records", r);
writer.append(JSONObject.toJSONString(map));
}
writer.flush();
}
catch (java.io.IOException e) {
throw new IOException("Exception while writing JSON", e);
}
}
@Override
public final String formatXML() {
StringWriter writer = new StringWriter();
formatXML(writer);
return writer.toString();
}
@Override
public final void formatXML(OutputStream stream) {
formatXML(new OutputStreamWriter(stream));
}
@Override
public final void formatXML(Writer writer) {
try {
writer.append("");
writer.append("");
for (Field> field : fields.fields) {
writer.append(" 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(" ");
writer.append("");
for (Record record : this) {
writer.append("");
for (int index = 0; index < fields.fields.length; index++) {
Object value = record.get(index);
writer.append(" ");
}
else {
writer.append(">");
writer.append(escapeXML(format0(value, false, false)));
writer.append("");
}
}
writer.append(" ");
}
writer.append(" ");
writer.append(" ");
writer.flush();
}
catch (java.io.IOException e) {
throw new IOException("Exception while writing XML", e);
}
}
@Override
public final String formatInsert() {
StringWriter writer = new StringWriter();
formatInsert(writer);
return writer.toString();
}
@Override
public final void formatInsert(OutputStream stream) {
formatInsert(new OutputStreamWriter(stream));
}
@Override
public final void formatInsert(Writer writer) {
Table> table = null;
if (records.size() > 0 && records.get(0) instanceof TableRecord)
table = ((TableRecord>) records.get(0)).getTable();
if (table == null)
table = table(name("UNKNOWN_TABLE"));
formatInsert(writer, table, fields());
}
@Override
public final String formatInsert(Table> table, Field>... f) {
StringWriter writer = new StringWriter();
formatInsert(writer, table, f);
return writer.toString();
}
@Override
public final void formatInsert(OutputStream stream, Table> table, Field>... f) {
formatInsert(new OutputStreamWriter(stream), table, f);
}
@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);
}
}
@Override
public final Document intoXML() {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
Element eResult = document.createElement("result");
eResult.setAttribute("xmlns", "http://www.jooq.org/xsd/jooq-export-3.7.0.xsd");
document.appendChild(eResult);
Element eFields = document.createElement("fields");
eResult.appendChild(eFields);
for (Field> field : fields.fields) {
Element eField = document.createElement("field");
if (field instanceof TableField, ?>) {
Table> table = ((TableField) field).getTable();
if (table != null) {
Schema schema = table.getSchema();
if (schema != null) {
eField.setAttribute("schema", schema.getName());
}
eField.setAttribute("table", table.getName());
}
}
eField.setAttribute("name", field.getName());
eField.setAttribute("type", field.getDataType().getTypeName().toUpperCase());
eFields.appendChild(eField);
}
Element eRecords = document.createElement("records");
eResult.appendChild(eRecords);
for (Record record : this) {
Element eRecord = document.createElement("record");
eRecords.appendChild(eRecord);
for (int index = 0; index < fields.fields.length; index++) {
Field> field = fields.fields[index];
Object value = record.get(index);
Element eValue = document.createElement("value");
eValue.setAttribute("field", field.getName());
eRecord.appendChild(eValue);
if (value != null) {
eValue.setTextContent(format0(value, false, false));
}
}
}
return document;
}
catch (ParserConfigurationException ignore) {
throw new RuntimeException(ignore);
}
}
@Override
public final H intoXML(H handler) throws SAXException {
Attributes empty = new AttributesImpl();
handler.startDocument();
handler.startPrefixMapping("", "http://www.jooq.org/xsd/jooq-export-3.7.0.xsd");
handler.startElement("", "", "result", empty);
handler.startElement("", "", "fields", empty);
for (Field> field : fields.fields) {
AttributesImpl attrs = new AttributesImpl();
if (field instanceof TableField, ?>) {
Table> table = ((TableField) field).getTable();
if (table != null) {
Schema schema = table.getSchema();
if (schema != null) {
attrs.addAttribute("", "", "schema", "CDATA", schema.getName());
}
attrs.addAttribute("", "", "table", "CDATA", table.getName());
}
}
attrs.addAttribute("", "", "name", "CDATA", field.getName());
attrs.addAttribute("", "", "type", "CDATA", field.getDataType().getTypeName().toUpperCase());
handler.startElement("", "", "field", attrs);
handler.endElement("", "", "field");
}
handler.endElement("", "", "fields");
handler.startElement("", "", "records", empty);
for (Record record : this) {
handler.startElement("", "", "record", empty);
for (int index = 0; index < fields.fields.length; index++) {
Field> field = fields.fields[index];
Object value = record.get(index);
AttributesImpl attrs = new AttributesImpl();
attrs.addAttribute("", "", "field", "CDATA", field.getName());
handler.startElement("", "", "value", attrs);
if (value != null) {
char[] chars = format0(value, false, false).toCharArray();
handler.characters(chars, 0, chars.length);
}
handler.endElement("", "", "value");
}
handler.endElement("", "", "record");
}
handler.endElement("", "", "records");
handler.endPrefixMapping("");
handler.endDocument();
return handler;
}
private final String escapeXML(String string) {
return StringUtils.replaceEach(string,
new String[] { "\"", "'", "<", ">", "&" },
new String[] { """, "'", "<", ">", "&"});
}
@Override
public final List> intoMaps() {
List> list = new ArrayList>();
for (R record : this) {
list.add(record.intoMap());
}
return list;
}
@Override
public final Map intoMap(Field key) {
return intoMap0(indexOrFail(fieldsRow(), key));
}
@Override
public final Map, R> intoMap(int keyFieldIndex) {
return intoMap0(keyFieldIndex);
}
@Override
public final Map, R> intoMap(String keyFieldName) {
return intoMap(field(keyFieldName));
}
@Override
public final Map, R> intoMap(Name keyFieldName) {
return intoMap(field(keyFieldName));
}
private final Map intoMap0(int keyFieldIndex) {
Map map = new LinkedHashMap();
for (R record : this)
if (map.put((K) record.get(keyFieldIndex), record) != null)
throw new InvalidResultException("Key " + keyFieldIndex + " is not unique in Result for " + this);
return map;
}
@Override
public final Map intoMap(Field key, Field value) {
int kIndex = indexOrFail(fieldsRow(), key);
int vIndex = indexOrFail(fieldsRow(), value);
return intoMap0(kIndex, vIndex);
}
@Override
public final Map, ?> intoMap(int keyFieldIndex, int valueFieldIndex) {
return intoMap0(keyFieldIndex, valueFieldIndex);
}
@Override
public final Map, ?> intoMap(String keyFieldName, String valueFieldName) {
return intoMap(field(keyFieldName), field(valueFieldName));
}
@Override
public final Map, ?> intoMap(Name keyFieldName, Name valueFieldName) {
return intoMap(field(keyFieldName), field(valueFieldName));
}
private final Map intoMap0(int kIndex, int vIndex) {
Map map = new LinkedHashMap();
for (R record : this)
if (map.put((K) record.get(kIndex), (V) record.get(vIndex)) != null)
throw new InvalidResultException("Key " + record.get(kIndex) + " is not unique in Result for " + this);
return map;
}
@Override
public final Map intoMap(int[] keyFieldIndexes) {
return intoMap(fields(keyFieldIndexes));
}
@Override
public final Map intoMap(String[] keyFieldNames) {
return intoMap(fields(keyFieldNames));
}
@Override
public final Map intoMap(Name[] keyFieldNames) {
return intoMap(fields(keyFieldNames));
}
@Override
public final Map intoMap(Field>[] keys) {
if (keys == null) {
keys = new Field[0];
}
Map map = new LinkedHashMap();
for (R record : this) {
RecordImpl key = new RecordImpl(keys);
for (Field> field : keys) {
Tools.copyValue(key, field, record, field);
}
if (map.put(key, record) != null) {
throw new InvalidResultException("Key list " + Arrays.asList(keys) + " is not unique in Result for " + this);
}
}
return map;
}
@Override
public final Map, E> intoMap(int[] keyFieldIndexes, Class extends E> type) {
return intoMap(fields(keyFieldIndexes), type);
}
@Override
public final Map, E> intoMap(String[] keyFieldNames, Class extends E> type) {
return intoMap(fields(keyFieldNames), type);
}
@Override
public final Map, E> intoMap(Name[] keyFieldNames, Class extends E> type) {
return intoMap(fields(keyFieldNames), type);
}
@Override
public final Map, E> intoMap(Field>[] keys, Class extends E> type) {
return intoMap(keys, Tools.configuration(this).recordMapperProvider().provide(fields, type));
}
@Override
public final Map, E> intoMap(int[] keyFieldIndexes, RecordMapper super R, E> mapper) {
return intoMap(fields(keyFieldIndexes), mapper);
}
@Override
public final Map, E> intoMap(String[] keyFieldNames, RecordMapper super R, E> mapper) {
return intoMap(fields(keyFieldNames), mapper);
}
@Override
public final Map, E> intoMap(Name[] keyFieldNames, RecordMapper super R, E> mapper) {
return intoMap(fields(keyFieldNames), mapper);
}
@Override
public final Map, E> intoMap(Field>[] keys, RecordMapper super R, E> mapper) {
if (keys == null) {
keys = new Field[0];
}
Map, E> map = new LinkedHashMap, E>();
for (R record : this) {
List keyValueList = new ArrayList();
for (Field> key : keys) {
keyValueList.add(record.get(key));
}
if (map.put(keyValueList, mapper.map(record)) != null) {
throw new InvalidResultException("Key list " + keyValueList + " is not unique in Result for " + this);
}
}
return map;
}
@Override
public final Map intoMap(Class extends K> keyType) {
return intoMap(Tools.configuration(this).recordMapperProvider().provide(fields, keyType));
}
@Override
public final Map intoMap(Class extends K> keyType, Class extends V> valueType) {
return intoMap(
Tools.configuration(this).recordMapperProvider().provide(fields, keyType),
Tools.configuration(this).recordMapperProvider().provide(fields, valueType)
);
}
@Override
public final Map intoMap(Class extends K> keyType, RecordMapper super R, V> valueMapper) {
return intoMap(Tools.configuration(this).recordMapperProvider().provide(fields, keyType), valueMapper);
}
@Override
public final Map intoMap(RecordMapper super R, K> keyMapper) {
Map map = new LinkedHashMap();
for (R record : this) {
K key = keyMapper.map(record);
if (map.put(key, record) != null)
throw new InvalidResultException("Key list " + key + " is not unique in Result for " + this);
}
return map;
}
@Override
public final Map intoMap(RecordMapper super R, K> keyMapper, Class valueType) {
return intoMap(keyMapper, Tools.configuration(this).recordMapperProvider().provide(fields, valueType));
}
@Override
public final Map intoMap(RecordMapper super R, K> keyMapper, RecordMapper super R, V> valueMapper) {
Map map = new LinkedHashMap();
for (R record : this) {
K key = keyMapper.map(record);
V value = valueMapper.map(record);
if (map.put(key, value) != null)
throw new InvalidResultException("Key list " + key + " is not unique in Result for " + this);
}
return map;
}
@Override
public final Map intoMap(Table table) {
Map map = new LinkedHashMap();
for (R record : this) {
S key = record.into(table);
if (map.put(key, record) != null)
throw new InvalidResultException("Key list " + key + " is not unique in Result for " + this);
}
return map;
}
@Override
public final Map intoMap(Table table, Class extends E> type) {
return intoMap(table, Tools.configuration(this).recordMapperProvider().provide(fields, type));
}
@Override
public final Map intoMap(Table table, RecordMapper super R, E> mapper) {
Map map = new LinkedHashMap();
for (R record : this) {
S key = record.into(table);
if (map.put(key, mapper.map(record)) != null)
throw new InvalidResultException("Key list " + key + " is not unique in Result for " + this);
}
return map;
}
@Override
public final Map, E> intoMap(int keyFieldIndex, Class extends E> type) {
return intoMap(keyFieldIndex, Tools.configuration(this).recordMapperProvider().provide(fields, type));
}
@Override
public final Map, E> intoMap(String keyFieldName, Class extends E> type) {
return intoMap(keyFieldName, Tools.configuration(this).recordMapperProvider().provide(fields, type));
}
@Override
public final Map, E> intoMap(Name keyFieldName, Class extends E> type) {
return intoMap(keyFieldName, Tools.configuration(this).recordMapperProvider().provide(fields, type));
}
@Override
public final Map intoMap(Field key, Class extends E> type) {
return intoMap(key, Tools.configuration(this).recordMapperProvider().provide(fields, type));
}
@Override
public final Map, E> intoMap(int keyFieldIndex, RecordMapper super R, E> mapper) {
return intoMap0(keyFieldIndex, mapper);
}
@Override
public final Map, E> intoMap(String keyFieldName, RecordMapper super R, E> mapper) {
return intoMap(field(keyFieldName), mapper);
}
@Override
public final Map, E> intoMap(Name keyFieldName, RecordMapper super R, E> mapper) {
return intoMap(field(keyFieldName), mapper);
}
@Override
public final Map intoMap(Field key, RecordMapper super R, E> mapper) {
return intoMap0(indexOrFail(fieldsRow(), key), mapper);
}
private final Map intoMap0(int keyFieldIndex, RecordMapper super R, E> mapper) {
Map map = new LinkedHashMap();
for (R record : this)
if (map.put((K) record.get(keyFieldIndex), mapper.map(record)) != null)
throw new InvalidResultException("Key " + keyFieldIndex + " is not unique in Result for " + this);
return map;
}
@Override
public final Map> intoGroups(Field key) {
return intoGroups0(indexOrFail(fieldsRow(), key));
}
@Override
public final Map, Result> intoGroups(int keyFieldIndex) {
return intoGroups0(keyFieldIndex);
}
@Override
public final Map, Result> intoGroups(String keyFieldName) {
return intoGroups(field(keyFieldName));
}
@Override
public final Map, Result> intoGroups(Name keyFieldName) {
return intoGroups(field(keyFieldName));
}
private final Map> intoGroups0(int keyFieldIndex) {
Map> map = new LinkedHashMap>();
for (R record : this) {
K val = (K) record.get(keyFieldIndex);
Result result = map.get(val);
if (result == null) {
result = new ResultImpl(configuration, fields);
map.put(val, result);
}
result.add(record);
}
return map;
}
@Override
public final Map> intoGroups(Field key, Field value) {
int kIndex = indexOrFail(fieldsRow(), key);
int vIndex = indexOrFail(fieldsRow(), value);
return intoGroups0(kIndex, vIndex);
}
@Override
public final Map, List>> intoGroups(int keyFieldIndex, int valueFieldIndex) {
return (Map) intoGroups0(keyFieldIndex, valueFieldIndex);
}
@Override
public final Map, List>> intoGroups(String keyFieldName, String valueFieldName) {
return (Map) intoGroups(field(keyFieldName), field(valueFieldName));
}
@Override
public final Map, List>> intoGroups(Name keyFieldName, Name valueFieldName) {
return (Map) intoGroups(field(keyFieldName), field(valueFieldName));
}
private final Map> intoGroups0(int kIndex, int vIndex) {
Map> map = new LinkedHashMap>();
for (R record : this) {
K k = (K) record.get(kIndex);
V v = (V) record.get(vIndex);
List result = map.get(k);
if (result == null) {
result = new ArrayList();
map.put(k, result);
}
result.add(v);
}
return map;
}
@Override
public final Map, List> intoGroups(int keyFieldIndex, Class extends E> type) {
return intoGroups(keyFieldIndex, Tools.configuration(this).recordMapperProvider().provide(fields, type));
}
@Override
public final Map, List> intoGroups(String keyFieldName, Class extends E> type) {
return intoGroups(keyFieldName, Tools.configuration(this).recordMapperProvider().provide(fields, type));
}
@Override
public final Map, List> intoGroups(Name keyFieldName, Class extends E> type) {
return intoGroups(keyFieldName, Tools.configuration(this).recordMapperProvider().provide(fields, type));
}
@Override
public final Map> intoGroups(Field key, Class extends E> type) {
return intoGroups(key, Tools.configuration(this).recordMapperProvider().provide(fields, type));
}
@Override
public final Map> intoGroups(Field key, RecordMapper super R, E> mapper) {
return intoGroups0(indexOrFail(fieldsRow(), key), mapper);
}
@Override
public final Map, List> intoGroups(int keyFieldIndex, RecordMapper super R, E> mapper) {
return intoGroups0(keyFieldIndex, mapper);
}
@Override
public final Map, List> intoGroups(String keyFieldName, RecordMapper super R, E> mapper) {
return intoGroups(field(keyFieldName), mapper);
}
@Override
public final Map, List> intoGroups(Name keyFieldName, RecordMapper super R, E> mapper) {
return intoGroups(field(keyFieldName), mapper);
}
private final Map> intoGroups0(int keyFieldIndex, RecordMapper super R, E> mapper) {
Map> map = new LinkedHashMap>();
for (R record : this) {
K keyVal = (K) record.get(keyFieldIndex);
List list = map.get(keyVal);
if (list == null) {
list = new ArrayList();
map.put(keyVal, list);
}
list.add(mapper.map(record));
}
return map;
}
@Override
public final Map> intoGroups(int[] keyFieldIndexes) {
return intoGroups(fields(keyFieldIndexes));
}
@Override
public final Map> intoGroups(String[] keyFieldNames) {
return intoGroups(fields(keyFieldNames));
}
@Override
public final Map> intoGroups(Name[] keyFieldNames) {
return intoGroups(fields(keyFieldNames));
}
@Override
public final Map> intoGroups(Field>[] keys) {
if (keys == null) {
keys = new Field[0];
}
Map> map = new LinkedHashMap>();
for (R record : this) {
RecordImpl key = new RecordImpl(keys);
for (Field> field : keys) {
Tools.copyValue(key, field, record, field);
}
Result result = map.get(key);
if (result == null) {
result = new ResultImpl(configuration(), this.fields);
map.put(key, result);
}
result.add(record);
}
return map;
}
@Override
public final Map> intoGroups(int[] keyFieldIndexes, Class extends E> type) {
return intoGroups(keyFieldIndexes, Tools.configuration(this).recordMapperProvider().provide(fields, type));
}
@Override
public final Map> intoGroups(String[] keyFieldNames, Class extends E> type) {
return intoGroups(keyFieldNames, Tools.configuration(this).recordMapperProvider().provide(fields, type));
}
@Override
public final Map> intoGroups(Name[] keyFieldNames, Class extends E> type) {
return intoGroups(keyFieldNames, Tools.configuration(this).recordMapperProvider().provide(fields, type));
}
@Override
public final Map> intoGroups(Field>[] keys, Class extends E> type) {
return intoGroups(keys, Tools.configuration(this).recordMapperProvider().provide(fields, type));
}
@Override
public final Map> intoGroups(int[] keyFieldIndexes, RecordMapper super R, E> mapper) {
return intoGroups(fields(keyFieldIndexes), mapper);
}
@Override
public final Map> intoGroups(String[] keyFieldNames, RecordMapper super R, E> mapper) {
return intoGroups(fields(keyFieldNames), mapper);
}
@Override
public final Map> intoGroups(Name[] keyFieldNames, RecordMapper super R, E> mapper) {
return intoGroups(fields(keyFieldNames), mapper);
}
@Override
public final Map> intoGroups(Field>[] keys, RecordMapper super R, E> mapper) {
if (keys == null) {
keys = new Field[0];
}
Map> map = new LinkedHashMap>();
for (R record : this) {
RecordImpl key = new RecordImpl(keys);
for (Field> field : keys) {
Tools.copyValue(key, field, record, field);
}
List list = map.get(key);
if (list == null) {
list = new ArrayList();
map.put(key, list);
}
list.add(mapper.map(record));
}
return map;
}
@Override
public final Map> intoGroups(Class extends K> keyType) {
return intoGroups(Tools.configuration(this).recordMapperProvider().provide(fields, keyType));
}
@Override
public final Map> intoGroups(Class extends K> keyType, Class extends V> valueType) {
return intoGroups(
Tools.configuration(this).recordMapperProvider().provide(fields, keyType),
Tools.configuration(this).recordMapperProvider().provide(fields, valueType)
);
}
@Override
public final Map> intoGroups(Class extends K> keyType, RecordMapper super R, V> valueMapper) {
return intoGroups(Tools.configuration(this).recordMapperProvider().provide(fields, keyType), valueMapper);
}
@Override
public final Map> intoGroups(RecordMapper super R, K> keyMapper) {
Map> map = new LinkedHashMap>();
for (R record : this) {
K key = keyMapper.map(record);
Result result = map.get(key);
if (result == null) {
result = new ResultImpl(configuration(), fields());
map.put(key, result);
}
result.add(record);
}
return map;
}
@Override
public final Map> intoGroups(RecordMapper super R, K> keyMapper, Class