* @author James Ahlborn
* @usage _general_class_
*/
-public class ImportUtil
+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() {}
/**
{
List<ColumnBuilder> columns = new LinkedList<ColumnBuilder>();
for (int i = 1; i <= md.getColumnCount(); i++) {
- ColumnBuilder column = new ColumnBuilder(md.getColumnName(i))
+ ColumnBuilder column = new ColumnBuilder(md.getColumnLabel(i))
.escapeName();
int lengthInUnits = md.getColumnDisplaySize(i);
column.setSQLType(md.getColumnType(i), lengthInUnits);
* <p>
* 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 importResultSet(source, db, name, SimpleImportFilter.INSTANCE);
}
-
+
/**
* Copy an existing JDBC ResultSet into a new table in this database.
* <p>
* 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 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
* name
*
* @return the name of the imported table
- *
+ *
* @see Builder
*/
public static String importResultSet(ResultSet source, Database db,
return table.getName();
}
-
+
/**
* Copy a delimited text file into a new table in this database.
* <p>
* Equivalent to:
* {@code importFile(f, name, db, delim, SimpleImportFilter.INSTANCE);}
- *
+ *
* @param name Name of the new table to create
* @param f Source file to import
* @param delim Regular expression representing the delimiter string.
* <p>
* Equivalent to:
* {@code importFile(f, name, db, delim, "'", filter, false);}
- *
+ *
* @param name Name of the new table to create
* @param f Source file to import
* @param delim Regular expression representing the delimiter string.
* <p>
* Equivalent to:
* {@code importReader(new BufferedReader(new FileReader(f)), db, name, delim, "'", filter, useExistingTable, true);}
- *
+ *
* @param name Name of the new table to create
* @param f Source file to import
* @param delim Regular expression representing the delimiter string.
* @see #importReader(BufferedReader,Database,String,String,ImportFilter,boolean)
* @see Builder
*/
- public static String importFile(File f, Database db, String name,
- String delim, char quote,
+ public static String importFile(File f, Database db, String name,
+ String delim, char quote,
ImportFilter filter,
boolean useExistingTable)
throws IOException
* <p>
* Equivalent to:
* {@code importReader(new BufferedReader(new FileReader(f)), db, name, delim, "'", filter, useExistingTable, header);}
- *
+ *
* @param name Name of the new table to create
* @param f Source file to import
* @param delim Regular expression representing the delimiter string.
* @see #importReader(BufferedReader,Database,String,String,char,ImportFilter,boolean,boolean)
* @see Builder
*/
- public static String importFile(File f, Database db, String name,
- String delim, char quote,
+ public static String importFile(File f, Database db, String name,
+ String delim, char quote,
ImportFilter filter,
boolean useExistingTable,
boolean header)
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(f));
- return importReader(in, db, name, delim, quote, filter,
+ return importReader(in, db, name, delim, quote, filter,
useExistingTable, header);
} finally {
ByteUtil.closeQuietly(in);
* <p>
* Equivalent to:
* {@code importReader(in, db, name, delim, SimpleImportFilter.INSTANCE);}
- *
+ *
* @param name Name of the new table to create
* @param in Source reader to import
* @param delim Regular expression representing the delimiter string.
* @see #importReader(BufferedReader,Database,String,String,ImportFilter)
* @see Builder
*/
- public static String importReader(BufferedReader in, Database db,
+ public static String importReader(BufferedReader in, Database db,
String name, String delim)
throws IOException
{
return importReader(in, db, name, delim, SimpleImportFilter.INSTANCE);
}
-
+
/**
* Copy a delimited text file into a new table in this database.
* <p>
* Equivalent to:
* {@code importReader(in, db, name, delim, filter, false);}
- *
+ *
* @param name Name of the new table to create
* @param in Source reader to import
* @param delim Regular expression representing the delimiter string.
* @see #importReader(BufferedReader,Database,String,String,ImportFilter,boolean)
* @see Builder
*/
- public static String importReader(BufferedReader in, Database db,
+ public static String importReader(BufferedReader in, Database db,
String name, String delim,
ImportFilter filter)
throws IOException
* <p>
* Equivalent to:
* {@code importReader(in, db, name, delim, '"', filter, false);}
- *
+ *
* @param name Name of the new table to create
* @param in Source reader to import
* @param delim Regular expression representing the delimiter string.
* name
*
* @return the name of the imported table
- *
+ *
* @see Builder
*/
- public static String importReader(BufferedReader in, Database db,
+ public static String importReader(BufferedReader in, Database db,
String name, String delim,
- ImportFilter filter,
+ ImportFilter filter,
boolean useExistingTable)
throws IOException
{
* <p>
* Equivalent to:
* {@code importReader(in, db, name, delim, '"', filter, useExistingTable, true);}
- *
+ *
* @param name Name of the new table to create
* @param in Source reader to import
* @param delim Regular expression representing the delimiter string.
* name
*
* @return the name of the imported table
- *
+ *
* @see Builder
*/
- public static String importReader(BufferedReader in, Database db,
+ public static String importReader(BufferedReader in, Database db,
String name, String delim, char quote,
ImportFilter filter,
boolean useExistingTable)
throws IOException
{
- return importReader(in, db, name, delim, quote, filter, useExistingTable,
+ return importReader(in, db, name, delim, quote, filter, useExistingTable,
true);
}
/**
* Copy a delimited text file into a new (or optionally exixsting) table in
* this database.
- *
+ *
* @param name Name of the new table to create
* @param in Source reader to import
* @param delim Regular expression representing the delimiter string.
* valid if useExistingTable is {@code true}
*
* @return the name of the imported table
- *
+ *
* @see Builder
*/
- public static String importReader(BufferedReader in, Database db,
+ public static String importReader(BufferedReader in, Database db,
String name, String delim, char quote,
ImportFilter filter,
boolean useExistingTable, boolean header)
List<ColumnBuilder> columns = new LinkedList<ColumnBuilder>();
Object[] columnNames = splitLine(line, delimPat, quote, in, 0);
-
+
for (int i = 0; i < columnNames.length; i++) {
columns.add(new ColumnBuilder((String)columnNames[i], DataType.TEXT)
.escapeName()
}
table = createUniqueTable(db, name, columns, null, filter);
-
+
// the first row was a header row
header = true;
}
List<Object[]> rows = new ArrayList<Object[]>(COPY_TABLE_BATCH_SIZE);
int numColumns = table.getColumnCount();
-
+
if(!header) {
// first line is _not_ a header line
Object[] data = splitLine(line, delimPat, quote, in, numColumns);
data = filter.filterRow(data);
if(data != null) {
rows.add(data);
- }
+ }
}
while ((line = in.readLine()) != null)
idx = endIdx + 1;
} else {
-
+
// done
idx = endIdx;
break;
*/
private static Table createUniqueTable(Database db, String name,
List<ColumnBuilder> columns,
- ResultSetMetaData md,
+ ResultSetMetaData md,
ImportFilter filter)
throws IOException, SQLException
{
while(db.getTable(name) != null) {
name = baseName + (counter++);
}
-
+
return new TableBuilder(name)
.addColumns(filter.filterColumns(columns, md))
.toTable(db);
public String importResultSet(ResultSet source)
throws SQLException, IOException
{
- return ImportUtil.importResultSet(source, _db, _tableName, _filter,
+ return ImportUtil.importResultSet(source, _db, _tableName, _filter,
_useExistingTable);
}
* @see ImportUtil#importReader(BufferedReader,Database,String,String,char,ImportFilter,boolean,boolean)
*/
public String importReader(BufferedReader reader) throws IOException {
- return ImportUtil.importReader(reader, _db, _tableName, _delim, _quote,
+ return ImportUtil.importReader(reader, _db, _tableName, _delim, _quote,
_filter, _useExistingTable, _header);
}
}