diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2011-11-25 15:06:47 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2011-11-25 15:06:47 +0000 |
commit | 67560e68f22c9745a7fe11876224a170798b05d5 (patch) | |
tree | d2de2a53d1bfdf549b7f104a033c8cef6ea1db35 /src/java | |
parent | 54e66161e13f78312d2a13c7647646dac7032bd5 (diff) | |
download | jackcess-67560e68f22c9745a7fe11876224a170798b05d5.tar.gz jackcess-67560e68f22c9745a7fe11876224a170798b05d5.zip |
Add option to import file without headers to existing table; Add ImportUtil.Builder and ExportUtil.Builder to simplify import/export operations
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@599 f203690c-595d-4dc9-a70b-905162fa7fd2
Diffstat (limited to 'src/java')
-rw-r--r-- | src/java/com/healthmarketscience/jackcess/ExportUtil.java | 108 | ||||
-rw-r--r-- | src/java/com/healthmarketscience/jackcess/ImportUtil.java | 178 |
2 files changed, 282 insertions, 4 deletions
diff --git a/src/java/com/healthmarketscience/jackcess/ExportUtil.java b/src/java/com/healthmarketscience/jackcess/ExportUtil.java index 472d328..ad8d502 100644 --- a/src/java/com/healthmarketscience/jackcess/ExportUtil.java +++ b/src/java/com/healthmarketscience/jackcess/ExportUtil.java @@ -68,6 +68,7 @@ public class ExportUtil { * The directory where the new files will be created * * @see #exportAll(Database,File,String) + * @see Builder */ public static void exportAll(Database db, File dir) throws IOException { @@ -87,6 +88,7 @@ public class ExportUtil { * The file extension of the new files * * @see #exportFile(Database,String,File,boolean,String,char,ExportFilter) + * @see Builder */ public static void exportAll(Database db, File dir, String ext) throws IOException { @@ -111,6 +113,7 @@ public class ExportUtil { * If <code>true</code> the first line contains the column names * * @see #exportFile(Database,String,File,boolean,String,char,ExportFilter) + * @see Builder */ public static void exportAll(Database db, File dir, String ext, boolean header) @@ -142,6 +145,7 @@ public class ExportUtil { * valid export filter * * @see #exportFile(Database,String,File,boolean,String,char,ExportFilter) + * @see Builder */ public static void exportAll(Database db, File dir, String ext, boolean header, String delim, @@ -166,6 +170,7 @@ public class ExportUtil { * New file to create * * @see #exportFile(Database,String,File,boolean,String,char,ExportFilter) + * @see Builder */ public static void exportFile(Database db, String tableName, File f) throws IOException { @@ -194,6 +199,7 @@ public class ExportUtil { * valid export filter * * @see #exportWriter(Database,String,BufferedWriter,boolean,String,char,ExportFilter) + * @see Builder */ public static void exportFile(Database db, String tableName, File f, boolean header, String delim, char quote, @@ -227,6 +233,7 @@ public class ExportUtil { * Writer to export to * * @see #exportWriter(Database,String,BufferedWriter,boolean,String,char,ExportFilter) + * @see Builder */ public static void exportWriter(Database db, String tableName, BufferedWriter out) throws IOException { @@ -254,6 +261,7 @@ public class ExportUtil { * valid export filter * * @see #exportWriter(Cursor,BufferedWriter,boolean,String,char,ExportFilter) + * @see Builder */ public static void exportWriter(Database db, String tableName, BufferedWriter out, boolean header, String delim, @@ -279,6 +287,8 @@ public class ExportUtil { * The quote character * @param filter * valid export filter + * + * @see Builder */ public static void exportWriter(Cursor cursor, BufferedWriter out, boolean header, String delim, @@ -390,4 +400,102 @@ public class ExportUtil { out.write(quote); } + + /** + * Builder which simplifies configuration of an export operation. + */ + public static class Builder + { + private Database _db; + private String _tableName; + private String _ext = DEFAULT_FILE_EXT; + private Cursor _cursor; + private String _delim = DEFAULT_DELIMITER; + private char _quote = DEFAULT_QUOTE_CHAR; + private ExportFilter _filter = SimpleExportFilter.INSTANCE; + private boolean _header; + + public Builder(Database db) { + this(db, null); + } + + public Builder(Database db, String tableName) { + _db = db; + _tableName = tableName; + } + + public Builder(Cursor cursor) { + _cursor = cursor; + } + + public Builder setDatabase(Database db) { + _db = db; + return this; + } + + public Builder setTableName(String tableName) { + _tableName = tableName; + return this; + } + + public Builder setCursor(Cursor cursor) { + _cursor = cursor; + return this; + } + + public Builder setDelimiter(String delim) { + _delim = delim; + return this; + } + + public Builder setQuote(char quote) { + _quote = quote; + return this; + } + + public Builder setFilter(ExportFilter filter) { + _filter = filter; + return this; + } + + public Builder setHeader(boolean header) { + _header = header; + return this; + } + + public Builder setFileNameExtension(String ext) { + _ext = ext; + return this; + } + + /** + * @see ExportUtil#exportAll(Database,File,String,boolean,String,char,ExportFilter) + */ + public void exportAll(File dir) throws IOException { + ExportUtil.exportAll(_db, dir, _ext, _header, _delim, _quote, _filter); + } + + /** + * @see ExportUtil#exportFile(Database,String,File,boolean,String,char,ExportFilter) + */ + public void exportFile(File f) throws IOException { + ExportUtil.exportFile(_db, _tableName, f, _header, _delim, _quote, + _filter); + } + + /** + * @see ExportUtil#exportWriter(Database,String,BufferedWriter,boolean,String,char,ExportFilter) + * @see ExportUtil#exportWriter(Cursor,BufferedWriter,boolean,String,char,ExportFilter) + */ + public void exportWriter(BufferedWriter writer) throws IOException { + if(_cursor != null) { + ExportUtil.exportWriter(_cursor, writer, _header, _delim, + _quote, _filter); + } else { + ExportUtil.exportWriter(_db, _tableName, writer, _header, _delim, + _quote, _filter); + } + } + } + } diff --git a/src/java/com/healthmarketscience/jackcess/ImportUtil.java b/src/java/com/healthmarketscience/jackcess/ImportUtil.java index b647570..571ebfa 100644 --- a/src/java/com/healthmarketscience/jackcess/ImportUtil.java +++ b/src/java/com/healthmarketscience/jackcess/ImportUtil.java @@ -73,6 +73,7 @@ public class ImportUtil * @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) @@ -94,6 +95,7 @@ public class ImportUtil * @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) @@ -114,6 +116,8 @@ public class ImportUtil * name * * @return the name of the imported table + * + * @see Builder */ public static String importResultSet(ResultSet source, Database db, String name, ImportFilter filter, @@ -194,6 +198,7 @@ public class ImportUtil * @return the name of the imported table * * @see #importFile(File,Database,String,String,ImportFilter) + * @see Builder */ public static String importFile(File f, Database db, String name, String delim) @@ -216,6 +221,7 @@ public class ImportUtil * @return the name of the imported table * * @see #importReader(BufferedReader,Database,String,String,ImportFilter) + * @see Builder */ public static String importFile(File f, Database db, String name, String delim, ImportFilter filter) @@ -229,7 +235,7 @@ public class ImportUtil * Copy a delimited text file into a new table in this database. * <p> * Equivalent to: - * {@code importReader(new BufferedReader(new FileReader(f)), db, name, delim, "'", filter, false);} + * {@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 @@ -242,7 +248,8 @@ public class ImportUtil * * @return the name of the imported table * - * @see #importReader(BufferedReader,Database,String,String,ImportFilter) + * @see #importReader(BufferedReader,Database,String,String,ImportFilter,boolean) + * @see Builder */ public static String importFile(File f, Database db, String name, String delim, char quote, @@ -250,11 +257,42 @@ public class ImportUtil boolean useExistingTable) throws IOException { + return importFile(f, db, name, delim, quote, filter, useExistingTable, true); + } + + /** + * Copy a delimited text file into a new table in this database. + * <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. + * @param quote the quote character + * @param filter valid import filter + * @param useExistingTable if {@code true} use current table if it already + * exists, otherwise, create new table with unique + * name + * @param header if {@code false} the first line is not a header row, only + * valid if useExistingTable is {@code true} + * @return the name of the imported table + * + * @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, + ImportFilter filter, + boolean useExistingTable, + boolean header) + throws IOException + { BufferedReader in = null; try { in = new BufferedReader(new FileReader(f)); return importReader(in, db, name, delim, quote, filter, - useExistingTable); + useExistingTable, header); } finally { if (in != null) { try { @@ -279,6 +317,7 @@ public class ImportUtil * @return the name of the imported table * * @see #importReader(BufferedReader,Database,String,String,ImportFilter) + * @see Builder */ public static String importReader(BufferedReader in, Database db, String name, String delim) @@ -301,6 +340,7 @@ public class ImportUtil * @return the name of the imported table * * @see #importReader(BufferedReader,Database,String,String,ImportFilter,boolean) + * @see Builder */ public static String importReader(BufferedReader in, Database db, String name, String delim, @@ -326,6 +366,8 @@ public class ImportUtil * name * * @return the name of the imported table + * + * @see Builder */ public static String importReader(BufferedReader in, Database db, String name, String delim, @@ -340,6 +382,9 @@ public class ImportUtil /** * Copy a delimited text file into a new (or optionally exixsting) table in * this database. + * <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 @@ -351,6 +396,8 @@ public class ImportUtil * name * * @return the name of the imported table + * + * @see Builder */ public static String importReader(BufferedReader in, Database db, String name, String delim, char quote, @@ -358,6 +405,35 @@ public class ImportUtil boolean useExistingTable) throws IOException { + 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. + * @param quote the quote character + * @param filter valid import filter + * @param useExistingTable if {@code true} use current table if it already + * exists, otherwise, create new table with unique + * name + * @param header if {@code false} the first line is not a header row, only + * valid if useExistingTable is {@code true} + * + * @return the name of the imported table + * + * @see Builder + */ + public static String importReader(BufferedReader in, Database db, + String name, String delim, char quote, + ImportFilter filter, + boolean useExistingTable, boolean header) + throws IOException + { String line = in.readLine(); if (line == null || line.trim().length() == 0) { return null; @@ -381,11 +457,23 @@ public class ImportUtil } 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) { Object[] data = splitLine(line, delimPat, quote, in, numColumns); @@ -509,6 +597,88 @@ public class ImportUtil return db.getTable(name); } - + /** + * Builder which simplifies configuration of an import operation. + */ + public static class Builder + { + private Database _db; + private String _tableName; + private String _delim = ExportUtil.DEFAULT_DELIMITER; + private char _quote = ExportUtil.DEFAULT_QUOTE_CHAR; + private ImportFilter _filter = SimpleImportFilter.INSTANCE; + private boolean _useExistingTable; + private boolean _header = true; + + public Builder(Database db) { + this(db, null); + } + + public Builder(Database db, String tableName) { + _db = db; + _tableName = tableName; + } + + public Builder setDatabase(Database db) { + _db = db; + return this; + } + + public Builder setTableName(String tableName) { + _tableName = tableName; + return this; + } + + public Builder setDelimiter(String delim) { + _delim = delim; + return this; + } + + public Builder setQuote(char quote) { + _quote = quote; + return this; + } + + public Builder setFilter(ImportFilter filter) { + _filter = filter; + return this; + } + + public Builder setUseExistingTable(boolean useExistingTable) { + _useExistingTable = useExistingTable; + return this; + } + + public Builder setHeader(boolean header) { + _header = header; + return this; + } + + /** + * @see ImportUtil#importResultSet(ResultSet,Database,String,ImportFilter,boolean) + */ + public String importResultSet(ResultSet source) + throws SQLException, IOException + { + return ImportUtil.importResultSet(source, _db, _tableName, _filter, + _useExistingTable); + } + + /** + * @see ImportUtil#importFile(File,Database,String,String,char,ImportFilter,boolean,boolean) + */ + public String importFile(File f) throws IOException { + return ImportUtil.importFile(f, _db, _tableName, _delim, _quote, _filter, + _useExistingTable, _header); + } + + /** + * @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, + _filter, _useExistingTable, _header); + } + } } |