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.
/*
* Copyright (c) 2020, Haiyang Li.
*
* 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.
*/
package com.landawn.abacus.util;
import static com.landawn.abacus.util.IOUtil.DEFAULT_QUEUE_SIZE_FOR_ROW_PARSER;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import com.landawn.abacus.DataSet;
import com.landawn.abacus.exception.UncheckedIOException;
import com.landawn.abacus.exception.UncheckedSQLException;
import com.landawn.abacus.type.Type;
/**
*
* @author Haiyang Li
* @see {@link com.landawn.abacus.condition.ConditionFactory}
* @see {@link com.landawn.abacus.condition.ConditionFactory.CF}
* @see {@link com.landawn.abacus.annotation.ReadOnly}
* @see {@link com.landawn.abacus.annotation.ReadOnlyId}
* @see {@link com.landawn.abacus.annotation.NonUpdatable}
* @see {@link com.landawn.abacus.annotation.Transient}
* @see {@link com.landawn.abacus.annotation.Table}
* @see {@link com.landawn.abacus.annotation.Column}
* @see http://docs.oracle.com/javase/8/docs/api/java/sql/Connection.html
* @see http://docs.oracle.com/javase/8/docs/api/java/sql/Statement.html
* @see http://docs.oracle.com/javase/8/docs/api/java/sql/PreparedStatement.html
* @see http://docs.oracle.com/javase/8/docs/api/java/sql/ResultSet.html
* @since 0.8
*/
public final class JdbcUtils {
private JdbcUtils() {
// singleton.
}
/**
* Imports the data from DataSet to database.
*
* @param dataset
* @param conn
* @param insertSQL the column order in the sql must be consistent with the column order in the DataSet. Here is sample about how to create the sql:
*
* List columnNameList = new ArrayList<>(dataset.columnNameList());
* columnNameList.retainAll(yourSelectColumnNames);
* String sql = RE.insert(columnNameList).into(tableName).sql();
*
* @return
* @throws UncheckedSQLException the unchecked SQL exception
*/
public static int importData(final DataSet dataset, final Connection conn, final String insertSQL) throws UncheckedSQLException {
return importData(dataset, dataset.columnNameList(), conn, insertSQL);
}
/**
* Imports the data from DataSet to database.
*
* @param dataset
* @param selectColumnNames
* @param conn
* @param insertSQL the column order in the sql must be consistent with the column order in the DataSet. Here is sample about how to create the sql:
*
* List columnNameList = new ArrayList<>(dataset.columnNameList());
* columnNameList.retainAll(yourSelectColumnNames);
* String sql = RE.insert(columnNameList).into(tableName).sql();
*
* @return
* @throws UncheckedSQLException the unchecked SQL exception
*/
public static int importData(final DataSet dataset, final Collection selectColumnNames, final Connection conn, final String insertSQL)
throws UncheckedSQLException {
return importData(dataset, selectColumnNames, 0, dataset.size(), conn, insertSQL);
}
/**
* Imports the data from DataSet to database.
*
* @param dataset
* @param selectColumnNames
* @param offset
* @param count
* @param conn
* @param insertSQL the column order in the sql must be consistent with the column order in the DataSet. Here is sample about how to create the sql:
*
* List columnNameList = new ArrayList<>(dataset.columnNameList());
* columnNameList.retainAll(yourSelectColumnNames);
* String sql = RE.insert(columnNameList).into(tableName).sql();
*
* @return
* @throws UncheckedSQLException the unchecked SQL exception
*/
public static int importData(final DataSet dataset, final Collection selectColumnNames, final int offset, final int count, final Connection conn,
final String insertSQL) throws UncheckedSQLException {
return importData(dataset, selectColumnNames, offset, count, conn, insertSQL, 200, 0);
}
/**
* Imports the data from DataSet to database.
*
* @param dataset
* @param selectColumnNames
* @param offset
* @param count
* @param conn
* @param insertSQL the column order in the sql must be consistent with the column order in the DataSet. Here is sample about how to create the sql:
*
* List columnNameList = new ArrayList<>(dataset.columnNameList());
* columnNameList.retainAll(yourSelectColumnNames);
* String sql = RE.insert(columnNameList).into(tableName).sql();
*
* @param batchSize
* @param batchInterval
* @return
* @throws UncheckedSQLException the unchecked SQL exception
*/
public static int importData(final DataSet dataset, final Collection selectColumnNames, final int offset, final int count, final Connection conn,
final String insertSQL, final int batchSize, final int batchInterval) throws UncheckedSQLException {
return importData(dataset, selectColumnNames, offset, count, Fn.alwaysTrue(), conn, insertSQL, batchSize, batchInterval);
}
/**
* Imports the data from DataSet to database.
*
* @param
* @param dataset
* @param selectColumnNames
* @param offset
* @param count
* @param filter
* @param conn
* @param insertSQL the column order in the sql must be consistent with the column order in the DataSet. Here is sample about how to create the sql:
*
* List columnNameList = new ArrayList<>(dataset.columnNameList());
* columnNameList.retainAll(yourSelectColumnNames);
* String sql = RE.insert(columnNameList).into(tableName).sql();
*
* @param batchSize
* @param batchInterval
* @return
* @throws UncheckedSQLException the unchecked SQL exception
* @throws E the e
*/
public static int importData(final DataSet dataset, final Collection selectColumnNames, final int offset, final int count,
final Throwables.Predicate super Object[], E> filter, final Connection conn, final String insertSQL, final int batchSize, final int batchInterval)
throws UncheckedSQLException, E {
PreparedStatement stmt = null;
try {
stmt = JdbcUtil.prepareStatement(conn, insertSQL);
return importData(dataset, selectColumnNames, offset, count, filter, stmt, batchSize, batchInterval);
} catch (SQLException e) {
throw new UncheckedSQLException(e);
} finally {
JdbcUtil.closeQuietly(stmt);
}
}
/**
* Imports the data from DataSet to database.
*
* @param dataset
* @param conn
* @param insertSQL the column order in the sql must be consistent with the column order in the DataSet. Here is sample about how to create the sql:
*
* List columnNameList = new ArrayList<>(dataset.columnNameList());
* columnNameList.retainAll(yourSelectColumnNames);
* String sql = RE.insert(columnNameList).into(tableName).sql();
*
* @param columnTypeMap
* @return
* @throws UncheckedSQLException the unchecked SQL exception
*/
@SuppressWarnings("rawtypes")
public static int importData(final DataSet dataset, final Connection conn, final String insertSQL, final Map columnTypeMap)
throws UncheckedSQLException {
return importData(dataset, 0, dataset.size(), conn, insertSQL, columnTypeMap);
}
/**
* Imports the data from DataSet to database.
*
* @param dataset
* @param offset
* @param count
* @param conn
* @param insertSQL the column order in the sql must be consistent with the column order in the DataSet. Here is sample about how to create the sql:
*
* List columnNameList = new ArrayList<>(dataset.columnNameList());
* columnNameList.retainAll(yourSelectColumnNames);
* String sql = RE.insert(columnNameList).into(tableName).sql();
*
* @param columnTypeMap
* @return
* @throws UncheckedSQLException the unchecked SQL exception
*/
@SuppressWarnings("rawtypes")
public static int importData(final DataSet dataset, final int offset, final int count, final Connection conn, final String insertSQL,
final Map columnTypeMap) throws UncheckedSQLException {
return importData(dataset, offset, count, conn, insertSQL, 200, 0, columnTypeMap);
}
/**
* Imports the data from DataSet to database.
*
* @param dataset
* @param offset
* @param count
* @param conn
* @param insertSQL the column order in the sql must be consistent with the column order in the DataSet. Here is sample about how to create the sql:
*
* List columnNameList = new ArrayList<>(dataset.columnNameList());
* columnNameList.retainAll(yourSelectColumnNames);
* String sql = RE.insert(columnNameList).into(tableName).sql();
*
* @param batchSize
* @param batchInterval
* @param columnTypeMap
* @return
* @throws UncheckedSQLException the unchecked SQL exception
*/
@SuppressWarnings("rawtypes")
public static int importData(final DataSet dataset, final int offset, final int count, final Connection conn, final String insertSQL, final int batchSize,
final int batchInterval, final Map columnTypeMap) throws UncheckedSQLException {
return importData(dataset, offset, count, Fn.alwaysTrue(), conn, insertSQL, batchSize, batchInterval, columnTypeMap);
}
/**
* Imports the data from DataSet to database.
*
* @param
* @param dataset
* @param offset
* @param count
* @param filter
* @param conn
* @param insertSQL the column order in the sql must be consistent with the column order in the DataSet. Here is sample about how to create the sql:
*
* List columnNameList = new ArrayList<>(dataset.columnNameList());
* columnNameList.retainAll(yourSelectColumnNames);
* String sql = RE.insert(columnNameList).into(tableName).sql();
*
* @param batchSize
* @param batchInterval
* @param columnTypeMap
* @return
* @throws UncheckedSQLException the unchecked SQL exception
* @throws E the e
*/
@SuppressWarnings("rawtypes")
public static int importData(final DataSet dataset, final int offset, final int count,
final Throwables.Predicate super Object[], E> filter, final Connection conn, final String insertSQL, final int batchSize, final int batchInterval,
final Map columnTypeMap) throws UncheckedSQLException, E {
PreparedStatement stmt = null;
try {
stmt = JdbcUtil.prepareStatement(conn, insertSQL);
return importData(dataset, offset, count, filter, stmt, batchSize, batchInterval, columnTypeMap);
} catch (SQLException e) {
throw new UncheckedSQLException(e);
} finally {
JdbcUtil.closeQuietly(stmt);
}
}
/**
* Imports the data from DataSet to database.
*
* @param dataset
* @param conn
* @param insertSQL the column order in the sql must be consistent with the column order in the DataSet. Here is sample about how to create the sql:
*
* List columnNameList = new ArrayList<>(dataset.columnNameList());
* columnNameList.retainAll(yourSelectColumnNames);
* String sql = RE.insert(columnNameList).into(tableName).sql();
*
* @param stmtSetter
* @return
* @throws UncheckedSQLException the unchecked SQL exception
*/
public static int importData(final DataSet dataset, final Connection conn, final String insertSQL,
final JdbcUtil.BiParametersSetter super PreparedStatement, ? super Object[]> stmtSetter) throws UncheckedSQLException {
return importData(dataset, 0, dataset.size(), conn, insertSQL, stmtSetter);
}
/**
* Imports the data from DataSet to database.
*
* @param dataset
* @param offset
* @param count
* @param conn
* @param insertSQL the column order in the sql must be consistent with the column order in the DataSet. Here is sample about how to create the sql:
*
* List columnNameList = new ArrayList<>(dataset.columnNameList());
* columnNameList.retainAll(yourSelectColumnNames);
* String sql = RE.insert(columnNameList).into(tableName).sql();
*
* @param stmtSetter
* @return
* @throws UncheckedSQLException the unchecked SQL exception
*/
public static int importData(final DataSet dataset, final int offset, final int count, final Connection conn, final String insertSQL,
final JdbcUtil.BiParametersSetter super PreparedStatement, ? super Object[]> stmtSetter) throws UncheckedSQLException {
return importData(dataset, offset, count, conn, insertSQL, 200, 0, stmtSetter);
}
/**
* Imports the data from DataSet to database.
*
* @param dataset
* @param offset
* @param count
* @param conn
* @param insertSQL the column order in the sql must be consistent with the column order in the DataSet. Here is sample about how to create the sql:
*
* List columnNameList = new ArrayList<>(dataset.columnNameList());
* columnNameList.retainAll(yourSelectColumnNames);
* String sql = RE.insert(columnNameList).into(tableName).sql();
*
* @param batchSize
* @param batchInterval
* @param stmtSetter
* @return
* @throws UncheckedSQLException the unchecked SQL exception
*/
public static int importData(final DataSet dataset, final int offset, final int count, final Connection conn, final String insertSQL, final int batchSize,
final int batchInterval, final JdbcUtil.BiParametersSetter super PreparedStatement, ? super Object[]> stmtSetter) throws UncheckedSQLException {
return importData(dataset, offset, count, Fn.alwaysTrue(), conn, insertSQL, batchSize, batchInterval, stmtSetter);
}
/**
* Imports the data from DataSet to database.
*
* @param
* @param dataset
* @param offset
* @param count
* @param filter
* @param conn
* @param insertSQL the column order in the sql must be consistent with the column order in the DataSet. Here is sample about how to create the sql:
*
* List columnNameList = new ArrayList<>(dataset.columnNameList());
* columnNameList.retainAll(yourSelectColumnNames);
* String sql = RE.insert(columnNameList).into(tableName).sql();
*
* @param batchSize
* @param batchInterval
* @param stmtSetter
* @return
* @throws UncheckedSQLException the unchecked SQL exception
* @throws E the e
*/
public static int importData(final DataSet dataset, final int offset, final int count,
final Throwables.Predicate super Object[], E> filter, final Connection conn, final String insertSQL, final int batchSize, final int batchInterval,
final JdbcUtil.BiParametersSetter super PreparedStatement, ? super Object[]> stmtSetter) throws UncheckedSQLException, E {
PreparedStatement stmt = null;
try {
stmt = JdbcUtil.prepareStatement(conn, insertSQL);
return importData(dataset, offset, count, filter, stmt, batchSize, batchInterval, stmtSetter);
} catch (SQLException e) {
throw new UncheckedSQLException(e);
} finally {
JdbcUtil.closeQuietly(stmt);
}
}
/**
* Imports the data from DataSet to database.
*
* @param dataset
* @param stmt the column order in the sql must be consistent with the column order in the DataSet.
* @return
* @throws UncheckedSQLException the unchecked SQL exception
*/
public static int importData(final DataSet dataset, final PreparedStatement stmt) throws UncheckedSQLException {
return importData(dataset, dataset.columnNameList(), stmt);
}
/**
* Imports the data from DataSet to database.
*
* @param dataset
* @param selectColumnNames
* @param stmt the column order in the sql must be consistent with the column order in the DataSet.
* @return
* @throws UncheckedSQLException the unchecked SQL exception
*/
public static int importData(final DataSet dataset, final Collection selectColumnNames, final PreparedStatement stmt) throws UncheckedSQLException {
return importData(dataset, selectColumnNames, 0, dataset.size(), stmt);
}
/**
* Imports the data from DataSet to database.
*
* @param dataset
* @param selectColumnNames
* @param offset
* @param count
* @param stmt the column order in the sql must be consistent with the column order in the DataSet.
* @return
* @throws UncheckedSQLException the unchecked SQL exception
*/
public static int importData(final DataSet dataset, final Collection selectColumnNames, final int offset, final int count,
final PreparedStatement stmt) throws UncheckedSQLException {
return importData(dataset, selectColumnNames, offset, count, stmt, 200, 0);
}
/**
* Imports the data from DataSet to database.
*
* @param dataset
* @param selectColumnNames
* @param offset
* @param count
* @param stmt the column order in the sql must be consistent with the column order in the DataSet.
* @param batchSize
* @param batchInterval
* @return
* @throws UncheckedSQLException the unchecked SQL exception
*/
public static int importData(final DataSet dataset, final Collection selectColumnNames, final int offset, final int count,
final PreparedStatement stmt, final int batchSize, final int batchInterval) throws UncheckedSQLException {
return importData(dataset, selectColumnNames, offset, count, Fn.alwaysTrue(), stmt, batchSize, batchInterval);
}
/**
* Imports the data from DataSet to database.
*
* @param
* @param dataset
* @param selectColumnNames
* @param offset
* @param count
* @param filter
* @param stmt the column order in the sql must be consistent with the column order in the DataSet.
* @param batchSize
* @param batchInterval
* @return
* @throws UncheckedSQLException the unchecked SQL exception
* @throws E the e
*/
public static int importData(final DataSet dataset, final Collection selectColumnNames, final int offset, final int count,
final Throwables.Predicate super Object[], E> filter, final PreparedStatement stmt, final int batchSize, final int batchInterval)
throws UncheckedSQLException, E {
final Type> objType = N.typeOf(Object.class);
final Map> columnTypeMap = new HashMap<>();
for (String propName : selectColumnNames) {
columnTypeMap.put(propName, objType);
}
return importData(dataset, offset, count, filter, stmt, batchSize, batchInterval, columnTypeMap);
}
/**
* Imports the data from DataSet to database.
*
* @param dataset
* @param stmt the column order in the sql must be consistent with the column order in the DataSet.
* @param columnTypeMap
* @return
* @throws UncheckedSQLException the unchecked SQL exception
*/
@SuppressWarnings("rawtypes")
public static int importData(final DataSet dataset, final PreparedStatement stmt, final Map columnTypeMap)
throws UncheckedSQLException {
return importData(dataset, 0, dataset.size(), stmt, columnTypeMap);
}
/**
* Imports the data from DataSet to database.
*
* @param dataset
* @param offset
* @param count
* @param stmt the column order in the sql must be consistent with the column order in the DataSet.
* @param columnTypeMap
* @return
* @throws UncheckedSQLException the unchecked SQL exception
*/
@SuppressWarnings("rawtypes")
public static int importData(final DataSet dataset, final int offset, final int count, final PreparedStatement stmt,
final Map columnTypeMap) throws UncheckedSQLException {
return importData(dataset, offset, count, stmt, 200, 0, columnTypeMap);
}
/**
* Imports the data from DataSet to database.
*
* @param dataset
* @param offset
* @param count
* @param stmt the column order in the sql must be consistent with the column order in the DataSet.
* @param batchSize
* @param batchInterval
* @param columnTypeMap
* @return
* @throws UncheckedSQLException the unchecked SQL exception
*/
@SuppressWarnings("rawtypes")
public static int importData(final DataSet dataset, final int offset, final int count, final PreparedStatement stmt, final int batchSize,
final int batchInterval, final Map columnTypeMap) throws UncheckedSQLException {
return importData(dataset, offset, count, Fn.alwaysTrue(), stmt, batchSize, batchInterval, columnTypeMap);
}
/**
* Imports the data from DataSet to database.
*
* @param
* @param dataset
* @param offset
* @param count
* @param filter
* @param stmt the column order in the sql must be consistent with the column order in the DataSet.
* @param batchSize
* @param batchInterval
* @param columnTypeMap
* @return
* @throws UncheckedSQLException the unchecked SQL exception
* @throws E the e
*/
@SuppressWarnings("rawtypes")
public static int importData(final DataSet dataset, final int offset, final int count,
final Throwables.Predicate super Object[], E> filter, final PreparedStatement stmt, final int batchSize, final int batchInterval,
final Map columnTypeMap) throws UncheckedSQLException, E {
N.checkArgument(offset >= 0 && count >= 0, "'offset'=%s and 'count'=%s can't be negative", offset, count);
N.checkArgument(batchSize > 0 && batchInterval >= 0, "'batchSize'=%s must be greater than 0 and 'batchInterval'=%s can't be negative", batchSize,
batchInterval);
int result = 0;
try {
final int columnCount = columnTypeMap.size();
final List columnNameList = dataset.columnNameList();
final int[] columnIndexes = new int[columnCount];
final Type