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) 2007 Health Market Science, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
You can contact Health Market Science at [email protected]
or at the following address:
Health Market Science
2700 Horizon Drive
Suite 200
King of Prussia, PA 19406
*/
package com.healthmarketscience.jackcess.util;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.healthmarketscience.jackcess.ColumnBuilder;
import com.healthmarketscience.jackcess.DataType;
import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.Table;
import com.healthmarketscience.jackcess.TableBuilder;
import com.healthmarketscience.jackcess.impl.ByteUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Utility class for importing tables to an Access database from other
* sources. See the {@link Builder} for convenient configuration of the
* import functionality. Note that most scenarios for customizing input data
* can be handled by implementing a custom {@link ImportFilter}.
*
* @author James Ahlborn
* @usage _general_class_
*/
public class ImportUtil
{
private static final Log LOG = LogFactory.getLog(ImportUtil.class);
/** Batch commit size for copying other result sets into this database */
private static final int COPY_TABLE_BATCH_SIZE = 200;
/** the platform line separator */
static final String LINE_SEPARATOR = System.getProperty("line.separator");
private ImportUtil() {}
/**
* Returns a List of Column instances converted from the given
* ResultSetMetaData (this is the same method used by the various {@code
* importResultSet()} methods).
*
* @return a List of Columns
*/
public static List toColumns(ResultSetMetaData md)
throws SQLException
{
List columns = new LinkedList();
for (int i = 1; i <= md.getColumnCount(); i++) {
ColumnBuilder column = new ColumnBuilder(md.getColumnName(i))
.escapeName();
int lengthInUnits = md.getColumnDisplaySize(i);
column.setSQLType(md.getColumnType(i), lengthInUnits);
DataType type = column.getType();
// we check for isTrueVariableLength here to avoid setting the length
// for a NUMERIC column, which pretends to be var-len, even though it
// isn't
if(type.isTrueVariableLength() && !type.isLongValue()) {
column.setLengthInUnits((short)lengthInUnits);
}
if(type.getHasScalePrecision()) {
int scale = md.getScale(i);
int precision = md.getPrecision(i);
if(type.isValidScale(scale)) {
column.setScale((byte)scale);
}
if(type.isValidPrecision(precision)) {
column.setPrecision((byte)precision);
}
}
columns.add(column);
}
return columns;
}
/**
* Copy an existing JDBC ResultSet into a new table in this database.
*
* Equivalent to:
* {@code importResultSet(source, db, name, SimpleImportFilter.INSTANCE);}
*
* @param name Name of the new table to create
* @param source ResultSet to copy from
*
* @return the name of the copied table
*
* @see #importResultSet(ResultSet,Database,String,ImportFilter)
* @see Builder
*/
public static String importResultSet(ResultSet source, Database db,
String name)
throws SQLException, IOException
{
return importResultSet(source, db, name, SimpleImportFilter.INSTANCE);
}
/**
* Copy an existing JDBC ResultSet into a new table in this database.
*
* Equivalent to:
* {@code importResultSet(source, db, name, filter, false);}
*
* @param name Name of the new table to create
* @param source ResultSet to copy from
* @param filter valid import filter
*
* @return the name of the imported table
*
* @see #importResultSet(ResultSet,Database,String,ImportFilter,boolean)
* @see Builder
*/
public static String importResultSet(ResultSet source, Database db,
String name, ImportFilter filter)
throws SQLException, IOException
{
return importResultSet(source, db, name, filter, false);
}
/**
* Copy an existing JDBC ResultSet into a new (or optionally existing) table
* in this database.
*
* @param name Name of the new table to create
* @param source ResultSet to copy from
* @param filter valid import filter
* @param useExistingTable if {@code true} use current table if it already
* exists, otherwise, create new table with unique
* name
*
* @return the name of the imported table
*
* @see Builder
*/
public static String importResultSet(ResultSet source, Database db,
String name, ImportFilter filter,
boolean useExistingTable)
throws SQLException, IOException
{
ResultSetMetaData md = source.getMetaData();
name = TableBuilder.escapeIdentifier(name);
Table table = null;
if(!useExistingTable || ((table = db.getTable(name)) == null)) {
List columns = toColumns(md);
table = createUniqueTable(db, name, columns, md, filter);
}
List