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.
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.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 com.healthmarketscience.jackcess.impl.DatabaseImpl;
/**
* 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
{
/** 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