[#4218] Add LoaderSourceStep.loadRows(Iterable<? extends Object[]>)
This commit is contained in:
parent
19dbd4ea29
commit
e2765eae96
67
jOOQ/src/main/java/org/jooq/LoaderRowsStep.java
Normal file
67
jOOQ/src/main/java/org/jooq/LoaderRowsStep.java
Normal file
@ -0,0 +1,67 @@
|
||||
/**
|
||||
* Copyright (c) 2009-2015, Data Geekery GmbH (http://www.datageekery.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This work is dual-licensed
|
||||
* - under the Apache Software License 2.0 (the "ASL")
|
||||
* - under the jOOQ License and Maintenance Agreement (the "jOOQ License")
|
||||
* =============================================================================
|
||||
* You may choose which license applies to you:
|
||||
*
|
||||
* - If you're using this work with Open Source databases, you may choose
|
||||
* either ASL or jOOQ License.
|
||||
* - If you're using this work with at least one commercial database, you must
|
||||
* choose jOOQ License
|
||||
*
|
||||
* For more information, please visit http://www.jooq.org/licenses
|
||||
*
|
||||
* Apache Software License 2.0:
|
||||
* -----------------------------------------------------------------------------
|
||||
* 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.
|
||||
*
|
||||
* jOOQ License and Maintenance Agreement:
|
||||
* -----------------------------------------------------------------------------
|
||||
* Data Geekery grants the Customer the non-exclusive, timely limited and
|
||||
* non-transferable license to install and use the Software under the terms of
|
||||
* the jOOQ License and Maintenance Agreement.
|
||||
*
|
||||
* This library is distributed with a LIMITED WARRANTY. See the jOOQ License
|
||||
* and Maintenance Agreement for more details: http://www.jooq.org/licensing
|
||||
*/
|
||||
package org.jooq;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* The <code>Loader</code> API is used for configuring data loads.
|
||||
* <p>
|
||||
* The step in constructing the {@link Loader} object where you can set the
|
||||
* mandatory row loader options.
|
||||
*
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
public interface LoaderRowsStep<R extends TableRecord<R>> {
|
||||
|
||||
/**
|
||||
* Specify the the fields to be loaded into the table in the correct order.
|
||||
*/
|
||||
@Support
|
||||
LoaderListenerStep<R> fields(Field<?>... fields);
|
||||
|
||||
/**
|
||||
* Specify the the fields to be loaded into the table in the correct order.
|
||||
*/
|
||||
@Support
|
||||
LoaderListenerStep<R> fields(Collection<? extends Field<?>> fields);
|
||||
|
||||
}
|
||||
@ -47,6 +47,7 @@ import java.io.Reader;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.CharsetDecoder;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
@ -62,187 +63,202 @@ import org.xml.sax.InputSource;
|
||||
public interface LoaderSourceStep<R extends TableRecord<R>> {
|
||||
|
||||
/**
|
||||
* Load CSV data
|
||||
* Load in-memory data.
|
||||
*/
|
||||
LoaderRowsStep<R> loadRows(Object[]... rows);
|
||||
|
||||
/**
|
||||
* Load in-memory data.
|
||||
*/
|
||||
LoaderRowsStep<R> loadRows(Iterable<? extends Object[]> rows);
|
||||
|
||||
/**
|
||||
* Load in-memory data.
|
||||
*/
|
||||
LoaderRowsStep<R> loadRows(Iterator<? extends Object[]> rows);
|
||||
|
||||
/**
|
||||
* Load CSV data.
|
||||
*/
|
||||
@Support
|
||||
LoaderCSVStep<R> loadCSV(File file) throws FileNotFoundException;
|
||||
|
||||
/**
|
||||
* Load CSV data
|
||||
* Load CSV data.
|
||||
*/
|
||||
@Support
|
||||
LoaderCSVStep<R> loadCSV(File file, String charsetName) throws FileNotFoundException, UnsupportedEncodingException;
|
||||
|
||||
/**
|
||||
* Load CSV data
|
||||
* Load CSV data.
|
||||
*/
|
||||
@Support
|
||||
LoaderCSVStep<R> loadCSV(File file, Charset cs) throws FileNotFoundException;
|
||||
|
||||
/**
|
||||
* Load CSV data
|
||||
* Load CSV data.
|
||||
*/
|
||||
@Support
|
||||
LoaderCSVStep<R> loadCSV(File file, CharsetDecoder dec) throws FileNotFoundException;
|
||||
|
||||
/**
|
||||
* Load CSV data
|
||||
* Load CSV data.
|
||||
*/
|
||||
@Support
|
||||
LoaderCSVStep<R> loadCSV(String data);
|
||||
|
||||
/**
|
||||
* Load CSV data
|
||||
* Load CSV data.
|
||||
*/
|
||||
@Support
|
||||
LoaderCSVStep<R> loadCSV(InputStream stream);
|
||||
|
||||
/**
|
||||
* Load CSV data
|
||||
* Load CSV data.
|
||||
*/
|
||||
@Support
|
||||
LoaderCSVStep<R> loadCSV(InputStream stream, String charsetName) throws UnsupportedEncodingException;
|
||||
|
||||
/**
|
||||
* Load CSV data
|
||||
* Load CSV data.
|
||||
*/
|
||||
@Support
|
||||
LoaderCSVStep<R> loadCSV(InputStream stream, Charset cs);
|
||||
|
||||
/**
|
||||
* Load CSV data
|
||||
* Load CSV data.
|
||||
*/
|
||||
@Support
|
||||
LoaderCSVStep<R> loadCSV(InputStream stream, CharsetDecoder dec);
|
||||
|
||||
/**
|
||||
* Load CSV data
|
||||
* Load CSV data.
|
||||
*/
|
||||
@Support
|
||||
LoaderCSVStep<R> loadCSV(Reader reader);
|
||||
|
||||
/**
|
||||
* Load XML data
|
||||
* Load XML data.
|
||||
*/
|
||||
@Support
|
||||
LoaderXMLStep<R> loadXML(File file) throws FileNotFoundException;
|
||||
|
||||
/**
|
||||
* Load XML data
|
||||
* Load XML data.
|
||||
*/
|
||||
@Support
|
||||
LoaderXMLStep<R> loadXML(File file, String charsetName) throws FileNotFoundException, UnsupportedEncodingException;
|
||||
|
||||
/**
|
||||
* Load XML data
|
||||
* Load XML data.
|
||||
*/
|
||||
@Support
|
||||
LoaderXMLStep<R> loadXML(File file, Charset cs) throws FileNotFoundException;
|
||||
|
||||
/**
|
||||
* Load XML data
|
||||
* Load XML data.
|
||||
*/
|
||||
@Support
|
||||
LoaderXMLStep<R> loadXML(File file, CharsetDecoder dec) throws FileNotFoundException;
|
||||
|
||||
/**
|
||||
* Load XML data
|
||||
* Load XML data.
|
||||
*/
|
||||
@Support
|
||||
LoaderXMLStep<R> loadXML(String data);
|
||||
|
||||
/**
|
||||
* Load XML data
|
||||
* Load XML data.
|
||||
*/
|
||||
@Support
|
||||
LoaderXMLStep<R> loadXML(InputStream stream);
|
||||
|
||||
/**
|
||||
* Load XML data
|
||||
* Load XML data.
|
||||
*/
|
||||
@Support
|
||||
LoaderXMLStep<R> loadXML(InputStream stream, String charsetName) throws UnsupportedEncodingException;
|
||||
|
||||
/**
|
||||
* Load XML data
|
||||
* Load XML data.
|
||||
*/
|
||||
@Support
|
||||
LoaderXMLStep<R> loadXML(InputStream stream, Charset cs);
|
||||
|
||||
/**
|
||||
* Load XML data
|
||||
* Load XML data.
|
||||
*/
|
||||
@Support
|
||||
LoaderXMLStep<R> loadXML(InputStream stream, CharsetDecoder dec);
|
||||
|
||||
/**
|
||||
* Load XML data
|
||||
* Load XML data.
|
||||
*/
|
||||
@Support
|
||||
LoaderXMLStep<R> loadXML(Reader reader);
|
||||
|
||||
/**
|
||||
* Load XML data
|
||||
* Load XML data.
|
||||
*/
|
||||
@Support
|
||||
LoaderXMLStep<R> loadXML(InputSource source);
|
||||
|
||||
/**
|
||||
* Load JSON data
|
||||
* Load JSON data.
|
||||
*/
|
||||
@Support
|
||||
LoaderJSONStep<R> loadJSON(File file) throws FileNotFoundException;
|
||||
|
||||
/**
|
||||
* Load JSON data
|
||||
* Load JSON data.
|
||||
*/
|
||||
@Support
|
||||
LoaderJSONStep<R> loadJSON(File file, String charsetName) throws FileNotFoundException, UnsupportedEncodingException;
|
||||
|
||||
/**
|
||||
* Load JSON data
|
||||
* Load JSON data.
|
||||
*/
|
||||
@Support
|
||||
LoaderJSONStep<R> loadJSON(File file, Charset cs) throws FileNotFoundException;
|
||||
|
||||
/**
|
||||
* Load JSON data
|
||||
* Load JSON data.
|
||||
*/
|
||||
@Support
|
||||
LoaderJSONStep<R> loadJSON(File file, CharsetDecoder dec) throws FileNotFoundException;
|
||||
|
||||
/**
|
||||
* Load JSON data
|
||||
* Load JSON data.
|
||||
*/
|
||||
@Support
|
||||
LoaderJSONStep<R> loadJSON(String data);
|
||||
|
||||
/**
|
||||
* Load JSON data
|
||||
* Load JSON data.
|
||||
*/
|
||||
@Support
|
||||
LoaderJSONStep<R> loadJSON(InputStream stream);
|
||||
|
||||
/**
|
||||
* Load JSON data
|
||||
* Load JSON data.
|
||||
*/
|
||||
@Support
|
||||
LoaderJSONStep<R> loadJSON(InputStream stream, String charsetName) throws UnsupportedEncodingException;
|
||||
|
||||
/**
|
||||
* Load JSON data
|
||||
* Load JSON data.
|
||||
*/
|
||||
@Support
|
||||
LoaderJSONStep<R> loadJSON(InputStream stream, Charset cs);
|
||||
|
||||
/**
|
||||
* Load JSON data
|
||||
* Load JSON data.
|
||||
*/
|
||||
@Support
|
||||
LoaderJSONStep<R> loadJSON(InputStream stream, CharsetDecoder dec);
|
||||
|
||||
/**
|
||||
* Load JSON data
|
||||
* Load JSON data.
|
||||
*/
|
||||
@Support
|
||||
LoaderJSONStep<R> loadJSON(Reader reader);
|
||||
|
||||
@ -54,13 +54,24 @@ class LoaderErrorImpl implements LoaderError {
|
||||
private final String[] row;
|
||||
private final Query query;
|
||||
|
||||
LoaderErrorImpl(DataAccessException exception, String[] row, int rowIndex, Query query) {
|
||||
LoaderErrorImpl(DataAccessException exception, Object[] row, int rowIndex, Query query) {
|
||||
this.exception = exception;
|
||||
this.row = row;
|
||||
this.row = strings(row);
|
||||
this.rowIndex = rowIndex;
|
||||
this.query = query;
|
||||
}
|
||||
|
||||
private static String[] strings(Object[] row) {
|
||||
if (row == null)
|
||||
return null;
|
||||
|
||||
String[] result = new String[row.length];
|
||||
for (int i = 0; i < result.length; i++)
|
||||
result[i] = row[i] == null ? null : row[i].toString();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataAccessException exception() {
|
||||
return exception;
|
||||
|
||||
@ -56,6 +56,7 @@ import java.nio.charset.CharsetDecoder;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
@ -75,6 +76,7 @@ import org.jooq.LoaderJSONOptionsStep;
|
||||
import org.jooq.LoaderJSONStep;
|
||||
import org.jooq.LoaderOptionsStep;
|
||||
import org.jooq.LoaderRowListener;
|
||||
import org.jooq.LoaderRowsStep;
|
||||
import org.jooq.LoaderXMLStep;
|
||||
import org.jooq.SelectQuery;
|
||||
import org.jooq.Table;
|
||||
@ -95,6 +97,7 @@ class LoaderImpl<R extends TableRecord<R>> implements
|
||||
|
||||
// Cascading interface implementations for Loader behaviour
|
||||
LoaderOptionsStep<R>,
|
||||
LoaderRowsStep<R>,
|
||||
LoaderXMLStep<R>,
|
||||
LoaderCSVStep<R>,
|
||||
LoaderCSVOptionsStep<R>,
|
||||
@ -104,58 +107,60 @@ class LoaderImpl<R extends TableRecord<R>> implements
|
||||
|
||||
// Configuration constants
|
||||
// -----------------------
|
||||
private static final int ON_DUPLICATE_KEY_ERROR = 0;
|
||||
private static final int ON_DUPLICATE_KEY_IGNORE = 1;
|
||||
private static final int ON_DUPLICATE_KEY_UPDATE = 2;
|
||||
private static final int ON_DUPLICATE_KEY_ERROR = 0;
|
||||
private static final int ON_DUPLICATE_KEY_IGNORE = 1;
|
||||
private static final int ON_DUPLICATE_KEY_UPDATE = 2;
|
||||
|
||||
private static final int ON_ERROR_ABORT = 0;
|
||||
private static final int ON_ERROR_IGNORE = 1;
|
||||
private static final int ON_ERROR_ABORT = 0;
|
||||
private static final int ON_ERROR_IGNORE = 1;
|
||||
|
||||
private static final int COMMIT_NONE = 0;
|
||||
private static final int COMMIT_AFTER = 1;
|
||||
private static final int COMMIT_ALL = 2;
|
||||
private static final int COMMIT_NONE = 0;
|
||||
private static final int COMMIT_AFTER = 1;
|
||||
private static final int COMMIT_ALL = 2;
|
||||
|
||||
private static final int BATCH_NONE = 0;
|
||||
private static final int BATCH_AFTER = 1;
|
||||
private static final int BATCH_ALL = 2;
|
||||
private static final int BATCH_NONE = 0;
|
||||
private static final int BATCH_AFTER = 1;
|
||||
private static final int BATCH_ALL = 2;
|
||||
|
||||
private static final int BULK_NONE = 0;
|
||||
private static final int BULK_AFTER = 1;
|
||||
private static final int BULK_ALL = 2;
|
||||
private static final int BULK_NONE = 0;
|
||||
private static final int BULK_AFTER = 1;
|
||||
private static final int BULK_ALL = 2;
|
||||
|
||||
private static final int CONTENT_CSV = 0;
|
||||
private static final int CONTENT_XML = 1;
|
||||
private static final int CONTENT_JSON = 2;
|
||||
private static final int CONTENT_CSV = 0;
|
||||
private static final int CONTENT_XML = 1;
|
||||
private static final int CONTENT_JSON = 2;
|
||||
private static final int CONTENT_ROWS = 3;
|
||||
|
||||
// Configuration data
|
||||
// ------------------
|
||||
private final DSLContext create;
|
||||
private final Configuration configuration;
|
||||
private final Table<R> table;
|
||||
private int onDuplicate = ON_DUPLICATE_KEY_ERROR;
|
||||
private int onError = ON_ERROR_ABORT;
|
||||
private int commit = COMMIT_NONE;
|
||||
private int commitAfter = 1;
|
||||
private int batch = BATCH_NONE;
|
||||
private int batchAfter = 1;
|
||||
private int bulk = BULK_NONE;
|
||||
private int bulkAfter = 1;
|
||||
private int content = CONTENT_CSV;
|
||||
private BufferedReader data;
|
||||
private final DSLContext create;
|
||||
private final Configuration configuration;
|
||||
private final Table<R> table;
|
||||
private int onDuplicate = ON_DUPLICATE_KEY_ERROR;
|
||||
private int onError = ON_ERROR_ABORT;
|
||||
private int commit = COMMIT_NONE;
|
||||
private int commitAfter = 1;
|
||||
private int batch = BATCH_NONE;
|
||||
private int batchAfter = 1;
|
||||
private int bulk = BULK_NONE;
|
||||
private int bulkAfter = 1;
|
||||
private int content = CONTENT_CSV;
|
||||
private BufferedReader data;
|
||||
private Iterator<? extends Object[]> rows;
|
||||
|
||||
// CSV configuration data
|
||||
// ----------------------
|
||||
private int ignoreRows = 1;
|
||||
private char quote = CSVParser.DEFAULT_QUOTE_CHARACTER;
|
||||
private char separator = CSVParser.DEFAULT_SEPARATOR;
|
||||
private String nullString = null;
|
||||
private Field<?>[] fields;
|
||||
private boolean[] primaryKey;
|
||||
private int ignoreRows = 1;
|
||||
private char quote = CSVParser.DEFAULT_QUOTE_CHARACTER;
|
||||
private char separator = CSVParser.DEFAULT_SEPARATOR;
|
||||
private String nullString = null;
|
||||
private Field<?>[] fields;
|
||||
private boolean[] primaryKey;
|
||||
|
||||
// Result data
|
||||
// -----------
|
||||
private LoaderRowListener listener;
|
||||
private LoaderContext result = new DefaultLoaderContext();
|
||||
private LoaderContext result = new DefaultLoaderContext();
|
||||
private int ignored;
|
||||
private int processed;
|
||||
private int stored;
|
||||
@ -275,6 +280,23 @@ class LoaderImpl<R extends TableRecord<R>> implements
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final LoaderRowsStep<R> loadRows(Object[]... r) {
|
||||
return loadRows(Arrays.asList(r));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final LoaderRowsStep<R> loadRows(Iterable<? extends Object[]> r) {
|
||||
return loadRows(r.iterator());
|
||||
}
|
||||
|
||||
@Override
|
||||
public final LoaderRowsStep<R> loadRows(Iterator<? extends Object[]> r) {
|
||||
content = CONTENT_ROWS;
|
||||
this.rows = r;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final LoaderImpl<R> loadCSV(File file) throws FileNotFoundException {
|
||||
return loadCSV(new FileReader(file));
|
||||
@ -520,6 +542,9 @@ class LoaderImpl<R extends TableRecord<R>> implements
|
||||
else if (content == CONTENT_JSON) {
|
||||
executeJSON();
|
||||
}
|
||||
else if (content == CONTENT_ROWS) {
|
||||
executeRows();
|
||||
}
|
||||
else {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
@ -573,13 +598,25 @@ class LoaderImpl<R extends TableRecord<R>> implements
|
||||
}
|
||||
}
|
||||
|
||||
private void executeSQL(Iterator<String[]> reader) throws SQLException {
|
||||
String[] row = null;
|
||||
private void executeRows() {
|
||||
try {
|
||||
executeSQL(rows);
|
||||
}
|
||||
|
||||
// SQLExceptions originating from rollbacks or commits are always fatal
|
||||
// They are propagated, and not swallowed
|
||||
catch (SQLException e) {
|
||||
throw Utils.translate(null, e);
|
||||
}
|
||||
}
|
||||
|
||||
private void executeSQL(Iterator<? extends Object[]> iterator) throws SQLException {
|
||||
Object[] row = null;
|
||||
BatchBindStep bind = null;
|
||||
InsertQuery<R> insert = null;
|
||||
|
||||
execution: {
|
||||
rows: while (reader.hasNext() && ((row = reader.next()) != null)) {
|
||||
rows: while (iterator.hasNext() && ((row = iterator.next()) != null)) {
|
||||
try {
|
||||
|
||||
// [#1627] Handle NULL values
|
||||
@ -760,21 +797,21 @@ class LoaderImpl<R extends TableRecord<R>> implements
|
||||
/**
|
||||
* Type-safety...
|
||||
*/
|
||||
private <T> void addValue0(InsertQuery<R> insert, Field<T> field, String row) {
|
||||
private <T> void addValue0(InsertQuery<R> insert, Field<T> field, Object row) {
|
||||
insert.addValue(field, field.getDataType().convert(row));
|
||||
}
|
||||
|
||||
/**
|
||||
* Type-safety...
|
||||
*/
|
||||
private <T> void addValueForUpdate0(InsertQuery<R> insert, Field<T> field, String row) {
|
||||
private <T> void addValueForUpdate0(InsertQuery<R> insert, Field<T> field, Object row) {
|
||||
insert.addValueForUpdate(field, field.getDataType().convert(row));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a type-safe condition
|
||||
*/
|
||||
private <T> Condition getCondition(Field<T> field, String string) {
|
||||
private <T> Condition getCondition(Field<T> field, Object string) {
|
||||
return field.equal(field.getDataType().convert(string));
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user