com.landawn.abacus.util.CSVUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-android-se-jdk7 Show documentation
Show all versions of abacus-android-se-jdk7 Show documentation
A general programming library in Java
/*
* Copyright (c) 2015, 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 java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.landawn.abacus.DataSet;
import com.landawn.abacus.core.RowDataSet;
import com.landawn.abacus.dataChannel.DataChannel;
import com.landawn.abacus.dataChannel.ResultSetChannel;
import com.landawn.abacus.dataChannel.StatementDataChannel;
import com.landawn.abacus.exception.AbacusException;
import com.landawn.abacus.exception.UncheckedIOException;
import com.landawn.abacus.exception.UncheckedSQLException;
import com.landawn.abacus.parser.JSONParser;
import com.landawn.abacus.parser.JSONSerializationConfig;
import com.landawn.abacus.parser.JSONSerializationConfig.JSC;
import com.landawn.abacus.parser.ParserFactory;
import com.landawn.abacus.parser.ParserUtil;
import com.landawn.abacus.parser.ParserUtil.EntityInfo;
import com.landawn.abacus.parser.ParserUtil.PropInfo;
import com.landawn.abacus.type.Type;
import com.landawn.abacus.util.Try.BiConsumer;
import com.landawn.abacus.util.function.Predicate;
/**
*
* @since 0.8
*
* @author Haiyang Li
*
*/
public final class CSVUtil {
private static final JSONParser jsonParser = ParserFactory.createJSONParser();
public static DataSet loadCSV(final File csvFile) {
return loadCSV(csvFile, (Collection) null);
}
public static DataSet loadCSV(final File csvFile, final Collection selectColumnNames) {
return loadCSV(csvFile, selectColumnNames, 0, Long.MAX_VALUE);
}
public static DataSet loadCSV(final File csvFile, final Collection selectColumnNames, final long offset, final long count) {
return loadCSV(csvFile, selectColumnNames, offset, count, null);
}
/**
* Load the data from CSV.
*
* @param csvFile
* @param selectColumnNames
* @param offset
* @param count
* @param filter
* @return
*/
public static DataSet loadCSV(final File csvFile, final Collection selectColumnNames, final long offset, final long count,
final Predicate filter) {
InputStream csvInputStream = null;
try {
csvInputStream = new FileInputStream(csvFile);
return loadCSV(csvInputStream, selectColumnNames, offset, count, filter);
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
IOUtil.closeQuietly(csvInputStream);
}
}
public static DataSet loadCSV(final InputStream csvInputStream) {
return loadCSV(csvInputStream, (Collection) null);
}
public static DataSet loadCSV(final InputStream csvInputStream, final Collection selectColumnNames) {
return loadCSV(csvInputStream, selectColumnNames, 0, Long.MAX_VALUE);
}
public static DataSet loadCSV(final InputStream csvInputStream, final Collection selectColumnNames, final long offset, final long count) {
return loadCSV(csvInputStream, selectColumnNames, offset, count, null);
}
/**
* Load the data from CSV.
*
* @param csvInputStream
* @param selectColumnNames
* @param offset
* @param count
* @param filter
* @return
*/
public static DataSet loadCSV(final InputStream csvInputStream, final Collection selectColumnNames, final long offset, final long count,
final Predicate filter) {
final Reader csvReader = new InputStreamReader(csvInputStream);
return loadCSV(csvReader, selectColumnNames, offset, count, filter);
}
public static DataSet loadCSV(final Reader csvReader) {
return loadCSV(csvReader, (Collection) null);
}
public static DataSet loadCSV(final Reader csvReader, final Collection selectColumnNames) {
return loadCSV(csvReader, selectColumnNames, 0, Long.MAX_VALUE);
}
public static DataSet loadCSV(final Reader csvReader, final Collection selectColumnNames, long offset, long count) {
return loadCSV(csvReader, selectColumnNames, offset, count, null);
}
/**
* Load the data from CSV.
*
* @param csvReader
* @param selectColumnNames
* @param offset
* @param count
* @param filter
* @return
*/
public static DataSet loadCSV(final Reader csvReader, final Collection selectColumnNames, long offset, long count,
final Predicate filter) {
N.checkArgument(offset >= 0 && count >= 0, "'offset'=%s and 'count'=%s can't be negative", offset, count);
final BufferedReader br = csvReader instanceof BufferedReader ? (BufferedReader) csvReader : ObjectFactory.createBufferedReader(csvReader);
try {
List tmp = new ArrayList<>();
String line = br.readLine();
jsonParser.readString(tmp, line);
final String[] titles = tmp.toArray(new String[tmp.size()]);
final int columnCount = titles.length;
final Type>[] columnTypes = new Type>[columnCount];
final List columnNameList = new ArrayList<>(selectColumnNames == null ? columnCount : selectColumnNames.size());
final List> columnList = new ArrayList<>(selectColumnNames == null ? columnCount : selectColumnNames.size());
final Set selectPropNameSet = selectColumnNames == null ? null : new HashSet<>(selectColumnNames);
for (int i = 0; i < columnCount; i++) {
if (selectPropNameSet == null || selectPropNameSet.remove(titles[i])) {
columnNameList.add(titles[i]);
columnList.add(new ArrayList<>());
columnTypes[i] = N.typeOf(String.class);
}
}
if (selectPropNameSet != null && selectPropNameSet.size() > 0) {
throw new AbacusException(selectPropNameSet + " are not included in titles: " + N.toString(titles));
}
final String[] strs = new String[titles.length];
while (offset-- > 0 && br.readLine() != null) {
}
while (count > 0 && (line = br.readLine()) != null) {
jsonParser.readString(strs, line);
if (filter != null && filter.test(strs) == false) {
continue;
}
for (int i = 0, columnIndex = 0; i < columnCount; i++) {
if (columnTypes[i] != null) {
columnList.get(columnIndex++).add(strs[i]);
}
}
count--;
}
return new RowDataSet(columnNameList, columnList);
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
if (br != csvReader) {
ObjectFactory.recycle(br);
}
}
}
public static DataSet loadCSV(final Class> entityClass, final File csvFile) {
return loadCSV(entityClass, csvFile, null);
}
public static DataSet loadCSV(final Class> entityClass, final File csvFile, final Collection selectColumnNames) {
return loadCSV(entityClass, csvFile, selectColumnNames, 0, Long.MAX_VALUE);
}
public static DataSet loadCSV(final Class> entityClass, final File csvFile, final Collection selectColumnNames, final long offset,
final long count) {
return loadCSV(entityClass, csvFile, selectColumnNames, offset, count, null);
}
/**
* Load the data from CSV.
*
* @param entityClass
* @param csvFile
* @param selectColumnNames
* @param offset
* @param count
* @param filter
* @return
*/
public static DataSet loadCSV(final Class> entityClass, final File csvFile, final Collection selectColumnNames, final long offset,
final long count, final Predicate filter) {
InputStream csvInputStream = null;
try {
csvInputStream = new FileInputStream(csvFile);
return loadCSV(entityClass, csvInputStream, selectColumnNames, offset, count, filter);
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
IOUtil.closeQuietly(csvInputStream);
}
}
public static DataSet loadCSV(final Class> entityClass, final InputStream csvInputStream) {
return loadCSV(entityClass, csvInputStream, null);
}
public static DataSet loadCSV(final Class> entityClass, final InputStream csvInputStream, final Collection selectColumnNames) {
return loadCSV(entityClass, csvInputStream, selectColumnNames, 0, Long.MAX_VALUE);
}
public static DataSet loadCSV(final Class> entityClass, final InputStream csvInputStream, final Collection selectColumnNames, final long offset,
final long count) {
return loadCSV(entityClass, csvInputStream, selectColumnNames, offset, count, null);
}
/**
* Load the data from CSV.
*
* @param entityClass
* @param csvInputStream
* @param selectColumnNames
* @param offset
* @param count
* @param filter
* @return
*/
public static DataSet loadCSV(final Class> entityClass, final InputStream csvInputStream, final Collection selectColumnNames, final long offset,
final long count, final Predicate filter) {
final Reader csvReader = new InputStreamReader(csvInputStream);
return loadCSV(entityClass, csvReader, selectColumnNames, offset, count, filter);
}
public static DataSet loadCSV(final Class> entityClass, final Reader csvReader) {
return loadCSV(entityClass, csvReader, null);
}
public static DataSet loadCSV(final Class> entityClass, final Reader csvReader, final Collection selectColumnNames) {
return loadCSV(entityClass, csvReader, selectColumnNames, 0, Long.MAX_VALUE);
}
public static DataSet loadCSV(final Class> entityClass, final Reader csvReader, final Collection selectColumnNames, long offset, long count) {
return loadCSV(entityClass, csvReader, selectColumnNames, offset, count, null);
}
/**
* Load the data from CSV.
*
* @param entityClass
* @param csvReader
* @param selectColumnNames
* @param offset
* @param count
* @param filter
* @return
*/
public static DataSet loadCSV(final Class> entityClass, final Reader csvReader, final Collection selectColumnNames, long offset, long count,
final Predicate filter) {
N.checkArgument(offset >= 0 && count >= 0, "'offset'=%s and 'count'=%s can't be negative", offset, count);
final BufferedReader br = csvReader instanceof BufferedReader ? (BufferedReader) csvReader : ObjectFactory.createBufferedReader(csvReader);
final EntityInfo entityInfo = ParserUtil.getEntityInfo(entityClass);
try {
List tmp = new ArrayList<>();
String line = br.readLine();
jsonParser.readString(tmp, line);
final String[] titles = tmp.toArray(new String[tmp.size()]);
final int columnCount = titles.length;
final Type>[] columnTypes = new Type>[columnCount];
final List columnNameList = new ArrayList<>(selectColumnNames == null ? columnCount : selectColumnNames.size());
final List> columnList = new ArrayList<>(selectColumnNames == null ? columnCount : selectColumnNames.size());
final Set selectPropNameSet = selectColumnNames == null ? null : new HashSet<>(selectColumnNames);
for (int i = 0; i < columnCount; i++) {
if (selectPropNameSet == null || selectPropNameSet.remove(titles[i])) {
PropInfo propInfo = entityInfo.getPropInfo(titles[i]);
if (propInfo == null && selectPropNameSet != null) {
throw new AbacusException(titles[i] + " is not defined in entity class: " + ClassUtil.getCanonicalClassName(entityClass));
}
if (propInfo != null) {
columnTypes[i] = propInfo.type;
columnNameList.add(titles[i]);
columnList.add(new ArrayList<>());
}
}
}
if (selectPropNameSet != null && selectPropNameSet.size() > 0) {
throw new AbacusException(selectColumnNames + " are not included in titles: " + N.toString(titles));
}
final String[] strs = new String[titles.length];
while (offset-- > 0 && br.readLine() != null) {
}
while (count > 0 && (line = br.readLine()) != null) {
jsonParser.readString(strs, line);
if (filter != null && filter.test(strs) == false) {
continue;
}
for (int i = 0, columnIndex = 0; i < columnCount; i++) {
if (columnTypes[i] != null) {
columnList.get(columnIndex++).add(columnTypes[i].valueOf(strs[i]));
}
}
count--;
}
return new RowDataSet(columnNameList, columnList);
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
if (br != csvReader) {
ObjectFactory.recycle(br);
}
}
}
@SuppressWarnings("rawtypes")
public static DataSet loadCSV(final File csvFile, final Map columnTypeMap) {
return loadCSV(csvFile, 0, Long.MAX_VALUE, columnTypeMap);
}
@SuppressWarnings("rawtypes")
public static DataSet loadCSV(final File csvFile, final long offset, final long count, final Map columnTypeMap) {
return loadCSV(csvFile, offset, count, null, columnTypeMap);
}
/**
* Load the data from CSV.
* @param csvFile
* @param offset
* @param count
* @param filter
* @param columnTypeMap
*
* @return
*/
@SuppressWarnings("rawtypes")
public static DataSet loadCSV(final File csvFile, final long offset, final long count, final Predicate filter,
final Map columnTypeMap) {
InputStream csvInputStream = null;
try {
csvInputStream = new FileInputStream(csvFile);
return loadCSV(csvInputStream, offset, count, filter, columnTypeMap);
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
IOUtil.closeQuietly(csvInputStream);
}
}
@SuppressWarnings("rawtypes")
public static DataSet loadCSV(final InputStream csvInputStream, final Map columnTypeMap) {
return loadCSV(csvInputStream, 0, Long.MAX_VALUE, columnTypeMap);
}
@SuppressWarnings("rawtypes")
public static DataSet loadCSV(final InputStream csvInputStream, final long offset, final long count, final Map columnTypeMap) {
return loadCSV(csvInputStream, offset, count, null, columnTypeMap);
}
/**
* Load the data from CSV.
* @param csvInputStream
* @param offset
* @param count
* @param filter
* @param columnTypeMap
*
* @return
*/
@SuppressWarnings("rawtypes")
public static DataSet loadCSV(final InputStream csvInputStream, final long offset, final long count, final Predicate filter,
final Map columnTypeMap) {
final Reader csvReader = new InputStreamReader(csvInputStream);
return loadCSV(csvReader, offset, count, filter, columnTypeMap);
}
@SuppressWarnings("rawtypes")
public static DataSet loadCSV(final Reader csvReader, final Map columnTypeMap) {
return loadCSV(csvReader, 0, Long.MAX_VALUE, columnTypeMap);
}
@SuppressWarnings("rawtypes")
public static DataSet loadCSV(final Reader csvReader, long offset, long count, final Map columnTypeMap) {
return loadCSV(csvReader, offset, count, null, columnTypeMap);
}
/**
* Load the data from CSV.
* @param csvReader
* @param offset
* @param count
* @param filter
* @param columnTypeMap
*
* @return
*/
@SuppressWarnings("rawtypes")
public static DataSet loadCSV(final Reader csvReader, long offset, long count, final Predicate filter,
final Map columnTypeMap) {
N.checkArgument(offset >= 0 && count >= 0, "'offset'=%s and 'count'=%s can't be negative", offset, count);
if (N.isNullOrEmpty(columnTypeMap)) {
throw new IllegalArgumentException("columnTypeMap can't be null or empty");
}
final BufferedReader br = csvReader instanceof BufferedReader ? (BufferedReader) csvReader : ObjectFactory.createBufferedReader(csvReader);
try {
List tmp = new ArrayList<>();
String line = br.readLine();
jsonParser.readString(tmp, line);
final String[] titles = tmp.toArray(new String[tmp.size()]);
final int columnCount = titles.length;
final Type>[] columnTypes = new Type>[columnCount];
final List columnNameList = new ArrayList<>(columnTypeMap.size());
final List> columnList = new ArrayList<>(columnTypeMap.size());
for (int i = 0; i < columnCount; i++) {
if (columnTypeMap.containsKey(titles[i])) {
columnTypes[i] = columnTypeMap.get(titles[i]);
columnNameList.add(titles[i]);
columnList.add(new ArrayList<>());
}
}
if (columnNameList.size() != columnTypeMap.size()) {
final List keys = new ArrayList<>(columnTypeMap.keySet());
keys.removeAll(columnNameList);
throw new AbacusException(keys + " are not included in titles: " + N.toString(titles));
}
final String[] strs = new String[titles.length];
while (offset-- > 0 && br.readLine() != null) {
}
while (count > 0 && (line = br.readLine()) != null) {
jsonParser.readString(strs, line);
if (filter != null && filter.test(strs) == false) {
continue;
}
for (int i = 0, columnIndex = 0; i < columnCount; i++) {
if (columnTypes[i] != null) {
columnList.get(columnIndex++).add(columnTypes[i].valueOf(strs[i]));
}
}
count--;
}
return new RowDataSet(columnNameList, columnList);
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
if (br != csvReader) {
ObjectFactory.recycle(br);
}
}
}
@SuppressWarnings("rawtypes")
public static DataSet loadCSV(final File csvFile, final List extends Type> columnTypeList) {
return loadCSV(csvFile, 0, Long.MAX_VALUE, columnTypeList);
}
@SuppressWarnings("rawtypes")
public static DataSet loadCSV(final File csvFile, final long offset, final long count, final List extends Type> columnTypeList) {
return loadCSV(csvFile, offset, count, null, columnTypeList);
}
/**
* Load the data from CSV.
* @param csvFile
* @param offset
* @param count
* @param filter
* @param columnTypeList set the column type to null to skip the column in CSV.
*
* @return
*/
@SuppressWarnings("rawtypes")
public static DataSet loadCSV(final File csvFile, final long offset, final long count, final Predicate filter,
final List extends Type> columnTypeList) {
InputStream csvInputStream = null;
try {
csvInputStream = new FileInputStream(csvFile);
return loadCSV(csvInputStream, offset, count, filter, columnTypeList);
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
IOUtil.closeQuietly(csvInputStream);
}
}
@SuppressWarnings("rawtypes")
public static DataSet loadCSV(final InputStream csvInputStream, final List extends Type> columnTypeList) {
return loadCSV(csvInputStream, 0, Long.MAX_VALUE, columnTypeList);
}
@SuppressWarnings("rawtypes")
public static DataSet loadCSV(final InputStream csvInputStream, final long offset, final long count, final List extends Type> columnTypeList) {
return loadCSV(csvInputStream, offset, count, null, columnTypeList);
}
/**
* Load the data from CSV.
* @param csvInputStream
* @param offset
* @param count
* @param filter
* @param columnTypeList set the column type to null to skip the column in CSV.
*
* @return
*/
@SuppressWarnings("rawtypes")
public static DataSet loadCSV(final InputStream csvInputStream, final long offset, final long count, final Predicate filter,
final List extends Type> columnTypeList) {
final Reader csvReader = new InputStreamReader(csvInputStream);
return loadCSV(csvReader, offset, count, filter, columnTypeList);
}
@SuppressWarnings("rawtypes")
public static DataSet loadCSV(final Reader csvReader, final List extends Type> columnTypeList) {
return loadCSV(csvReader, 0, Long.MAX_VALUE, columnTypeList);
}
@SuppressWarnings("rawtypes")
public static DataSet loadCSV(final Reader csvReader, long offset, long count, final List extends Type> columnTypeList) {
return loadCSV(csvReader, offset, count, null, columnTypeList);
}
/**
* Load the data from CSV.
* @param csvReader
* @param offset
* @param count
* @param filter
* @param columnTypeList set the column type to null to skip the column in CSV.
*
* @return
*/
@SuppressWarnings("rawtypes")
public static DataSet loadCSV(final Reader csvReader, long offset, long count, final Predicate filter,
final List extends Type> columnTypeList) {
N.checkArgument(offset >= 0 && count >= 0, "'offset'=%s and 'count'=%s can't be negative", offset, count);
if (N.isNullOrEmpty(columnTypeList)) {
throw new IllegalArgumentException("columnTypeList can't be null or empty");
}
final BufferedReader br = csvReader instanceof BufferedReader ? (BufferedReader) csvReader : ObjectFactory.createBufferedReader(csvReader);
final Type>[] columnTypes = columnTypeList.toArray(new Type[columnTypeList.size()]);
try {
List tmp = new ArrayList<>();
String line = br.readLine();
jsonParser.readString(tmp, line);
final String[] titles = tmp.toArray(new String[tmp.size()]);
final int columnCount = titles.length;
final List columnNameList = new ArrayList<>(columnCount);
final List> columnList = new ArrayList<>();
for (int i = 0; i < columnCount; i++) {
if (columnTypes[i] != null) {
columnNameList.add(titles[i]);
columnList.add(new ArrayList<>());
}
}
final String[] strs = new String[titles.length];
while (offset-- > 0 && br.readLine() != null) {
}
while (count > 0 && (line = br.readLine()) != null) {
jsonParser.readString(strs, line);
if (filter != null && filter.test(strs) == false) {
continue;
}
for (int i = 0, columnIndex = 0; i < columnCount; i++) {
if (columnTypes[i] != null) {
columnList.get(columnIndex++).add(columnTypes[i].valueOf(strs[i]));
}
}
count--;
}
return new RowDataSet(columnNameList, columnList);
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
if (br != csvReader) {
ObjectFactory.recycle(br);
}
}
}
/**
* Exports the data from database to CVS. Title will be added at the first line and columns will be quoted.
*
* @param out
* @param conn
* @param querySQL
* @return
*/
public static long exportCSV(final File out, final Connection conn, final String querySQL) {
return exportCSV(out, conn, querySQL, 0, Long.MAX_VALUE, true, true);
}
/**
* Exports the data from database to CVS.
*
* @param out
* @param conn
* @param querySQL
* @param offset
* @param count
* @param writeTitle
* @param quoted
* @return
*/
public static long exportCSV(final File out, final Connection conn, final String querySQL, final long offset, final long count, final boolean writeTitle,
final boolean quoted) {
return exportCSV(out, conn, querySQL, null, offset, count, writeTitle, quoted);
}
/**
* Exports the data from database to CVS.
*
* @param out
* @param conn
* @param querySQL
* @param selectColumnNames
* @param offset
* @param count
* @param writeTitle
* @param quoted
* @return
*/
public static long exportCSV(final File out, final Connection conn, final String querySQL, final Collection selectColumnNames, final long offset,
final long count, final boolean writeTitle, final boolean quoted) {
final NamedSQL namedSQL = NamedSQL.parse(querySQL);
PreparedStatement stmt = null;
try {
stmt = conn.prepareStatement(namedSQL.getPureSQL(), ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
stmt.setFetchSize(200);
return exportCSV(out, stmt, selectColumnNames, offset, count, writeTitle, quoted);
} catch (SQLException e) {
throw new UncheckedSQLException(e);
} finally {
JdbcUtil.closeQuietly(stmt);
}
}
/**
* Exports the data from database to CVS. Title will be added at the first line and columns will be quoted.
*
* @param out
* @param stmt
* @return
*/
@SuppressWarnings("unchecked")
public static long exportCSV(final File out, final PreparedStatement stmt) {
return exportCSV(out, stmt, 0, Long.MAX_VALUE, true, true);
}
/**
* Exports the data from database to CVS.
*
* @param out
* @param stmt
* @param offset
* @param count
* @param writeTitle
* @param quoted
* @return
*/
public static long exportCSV(final File out, final PreparedStatement stmt, final long offset, final long count, final boolean writeTitle,
final boolean quoted) {
return exportCSV(out, stmt, null, offset, count, writeTitle, quoted);
}
/**
* Exports the data from database to CVS.
*
* @param out
* @param stmt
* @param selectColumnNames
* @param offset
* @param count
* @param writeTitle
* @param quoted
* @return
*/
public static long exportCSV(final File out, final PreparedStatement stmt, final Collection selectColumnNames, final long offset, final long count,
final boolean writeTitle, final boolean quoted) {
ResultSet rs = null;
try {
rs = stmt.executeQuery();
// rs.setFetchSize(200);
return exportCSV(out, rs, selectColumnNames, offset, count, writeTitle, quoted);
} catch (SQLException e) {
throw new UncheckedSQLException(e);
} finally {
JdbcUtil.closeQuietly(rs);
}
}
/**
* Exports the data from database to CVS. Title will be added at the first line and columns will be quoted.
*
* @param out
* @param rs
* @return
*/
public static long exportCSV(final File out, final ResultSet rs) {
return exportCSV(out, rs, 0, Long.MAX_VALUE, true, true);
}
/**
* Exports the data from database to CVS.
*
* @param out
* @param rs
* @param offset
* @param count
* @param writeTitle
* @param quoted
* @return
*/
public static long exportCSV(final File out, final ResultSet rs, final long offset, final long count, final boolean writeTitle, final boolean quoted) {
return exportCSV(out, rs, null, offset, count, writeTitle, quoted);
}
/**
*
* @param out
* @param rs
* @param selectColumnNames
* @param offset
* @param count
* @param writeTitle
* @param quoted
* @return
*/
public static long exportCSV(final File out, final ResultSet rs, final Collection selectColumnNames, final long offset, final long count,
final boolean writeTitle, final boolean quoted) {
OutputStream os = null;
try {
if (!out.exists()) {
out.createNewFile();
}
os = new FileOutputStream(out);
long result = exportCSV(os, rs, selectColumnNames, offset, count, writeTitle, quoted);
os.flush();
return result;
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
IOUtil.close(os);
}
}
/**
* Exports the data from database to CVS. Title will be added at the first line and columns will be quoted.
*
* @param out
* @param rs
* @return
*/
public static long exportCSV(final OutputStream out, final ResultSet rs) {
return exportCSV(out, rs, 0, Long.MAX_VALUE, true, true);
}
/**
* Exports the data from database to CVS.
*
* @param out
* @param rs
* @param offset
* @param count
* @param writeTitle
* @param quoted
* @return
*/
public static long exportCSV(final OutputStream out, final ResultSet rs, final long offset, final long count, final boolean writeTitle,
final boolean quoted) {
return exportCSV(out, rs, null, offset, count, writeTitle, quoted);
}
/**
* Exports the data from database to CVS.
*
* @param out
* @param rs
* @param selectColumnNames
* @param offset
* @param count
* @param writeTitle
* @param quoted
* @return
*/
public static long exportCSV(final OutputStream out, final ResultSet rs, final Collection selectColumnNames, final long offset, final long count,
final boolean writeTitle, final boolean quoted) {
Writer writer = null;
try {
writer = new OutputStreamWriter(out);
long result = exportCSV(writer, rs, selectColumnNames, offset, count, writeTitle, quoted);
writer.flush();
return result;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
/**
* Exports the data from database to CVS. Title will be added at the first line and columns will be quoted.
*
* @param out
* @param rs
* @return
*/
public static long exportCSV(final Writer out, final ResultSet rs) {
return exportCSV(out, rs, 0, Long.MAX_VALUE, true, true);
}
/**
* Exports the data from database to CVS.
*
* @param out
* @param rs
* @param offset
* @param count
* @param writeTitle
* @param quoted
* @return
*/
public static long exportCSV(final Writer out, final ResultSet rs, final long offset, final long count, final boolean writeTitle, final boolean quoted) {
return exportCSV(out, rs, null, offset, count, writeTitle, quoted);
}
/**
* Exports the data from database to CVS.
*
* @param out
* @param rs
* @param selectColumnNames
* @param offset
* @param count
* @param writeTitle
* @param quoted
* @return
*/
public static long exportCSV(final Writer out, final ResultSet rs, final Collection selectColumnNames, long offset, final long count,
final boolean writeTitle, final boolean quoted) {
N.checkArgument(offset >= 0 && count >= 0, "'offset'=%s and 'count'=%s can't be negative", offset, count);
final JSONSerializationConfig config = JSC.create();
config.setDateTimeFormat(DateTimeFormat.ISO_8601_TIMESTAMP);
if (quoted) {
config.setCharQuotation(D._QUOTATION_D);
config.setStringQuotation(D._QUOTATION_D);
} else {
config.setCharQuotation((char) 0);
config.setStringQuotation((char) 0);
}
long result = 0;
final Type