Class DefaultTableLoader
- All Implemented Interfaces:
AutoCloseable
The input streams are built up of comma separated values, where strings are quoted (and to include a quote in a string, it must be specified twice). NULL values should specify the string NULL
only. All other values should respect their SQL representation type for the table in columns question.
The string quotation type may be modified using
invalid @link
#setStringPattern(Pattern)
Pattern
may the one of the following:invalid @link
#quotedStringPattern
invalid @link
#escapedQuotedStringPattern
- Author:
- Christopher Mindus
Field Summary
Modifier and TypeFieldDescriptionprotected final String[]
The column names.protected final Connection
The connection.static final Pattern
Matches a date as YYYY-MM-DD or YYYY/MM/DD where YYYY is 4 digits between 1000 and 2999, MM is two digits between 01 and 12, DD is two digits between 01 and 31.static final Pattern
Pattern for date/time/timestamp separator characters '-/:.' and whitespace: '[-/:.\s]+'.static final int
The default batch count of transactions before a batch-commit is done when inserting multiple rows.protected final PreparedStatement
The prepared statement.static final Pattern
The "NULL" string pattern with whitespace ignored around it, case insensitive.protected final CSVParser
The CSV parser.static final Pattern
Matches a valid 24-hour time from 00:00:00-23:59:59 in hh:mm:ss or hh.mm.ss formats: '([01]?\d|2[0-3])[:.]([0-5]\d)[:.]([0-5]\d)', and allows time as e.g.static final Pattern
Matches a timestamp as "YYYY-MM-DD hh:mm:ss.fff" or "YYYY/MM/DD hh.mm.ss.fffffffff" where YYYY is 4 digits between 1000 and 2999, MM is one or two digits between 01 and 12, DD is one or two digits between 01 and 31.Constructor Summary
ConstructorDescriptionDefaultTableLoader
(InputStream in, Charset charSet, Connection conn, String tableName, boolean clearBefore, String... columns) Creates the table loader.DefaultTableLoader
(Reader reader, Connection conn, String tableName, boolean clearBefore, String... columns) Creates the table loader.Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the input stream or reader and the prepared SQL statement for row insertion: both are attempted to be closed, and if the input stream or reader throws an IOException the SQL statement will be closed.createParser
(BufferedReader reader) Creates the CSV parser.protected void
processLine
(CSVRecord rec) Retrieves and sets all the column values in the prepared statement using theCSVRecord
.void
Processes the reader.void
processReader
(int batchCount) Processes the reader.
Field Details
nullPattern
The "NULL" string pattern with whitespace ignored around it, case insensitive.timePattern
Matches a valid 24-hour time from 00:00:00-23:59:59 in hh:mm:ss or hh.mm.ss formats: '([01]?\d|2[0-3])[:.]([0-5]\d)[:.]([0-5]\d)', and allows time as e.g. 10:12:12, 13.12.59, 13:56:00 or 8.14.00.datePattern
Matches a date as YYYY-MM-DD or YYYY/MM/DD where YYYY is 4 digits between 1000 and 2999, MM is two digits between 01 and 12, DD is two digits between 01 and 31. '[12]\d{3}[-/](0[1-9]|1[0-2])[-/](0[1-9]|[12]\d|3[01])'.timestampPattern
Matches a timestamp as "YYYY-MM-DD hh:mm:ss.fff" or "YYYY/MM/DD hh.mm.ss.fffffffff" where YYYY is 4 digits between 1000 and 2999, MM is one or two digits between 01 and 12, DD is one or two digits between 01 and 31. The time part is a valid 24-hour time from 00:00:00-23:59:59 in hh:mm:ss or hh.mm.ss formats with fractional nanoseconds in 'fffffffff' that may be omitted. hh can be a single digit, but mm and nn must be two digits. '[12]\d{3}[-/](0?[1-9]|1[0-2])[-/](0?[1-9]|[12]\d|3[01])\s+([01]?\d|2[0-3])[:.]([0-5]\d)[:.]([0-5]\d)(\.[0-9]{1,9})?'.dateTimeSeparatorPattern
Pattern for date/time/timestamp separator characters '-/:.' and whitespace: '[-/:.\s]+'.DEFAULT_BATCH_COUNT
public static final int DEFAULT_BATCH_COUNTThe default batch count of transactions before a batch-commit is done when inserting multiple rows.- See Also:
parser
The CSV parser.insertStatement
The prepared statement.conn
The connection.columns
The column names.
Constructor Details
DefaultTableLoader
public DefaultTableLoader(InputStream in, Charset charSet, Connection conn, String tableName, boolean clearBefore, String... columns) throws SQLException, IOException Creates the table loader.- Parameters:
in
- The input stream.charSet
- The character set to use.conn
- The JDBC connection: the connection is never closed by this class. It is up to the called to close it. A finalcommit()
will be performed however.tableName
- The table name.clearBefore
- Flag to clear the table of all rows prior to inserting the data. Please note that this flag sometime cannot betrue
if there are constraints on e.g. other tables.columns
- The columns.- Throws:
NullPointerException
- If any parameter isnull
.IllegalArgumentException
- If thecolumns
are not specified.SQLException
- For SQL errors.IOException
- For I/O errors.
DefaultTableLoader
public DefaultTableLoader(Reader reader, Connection conn, String tableName, boolean clearBefore, String... columns) throws SQLException, IOException Creates the table loader.- Parameters:
reader
- The reader.conn
- The JDBC connection: the connection is never closed by this class. It is up to the called to close it. A finalcommit()
will be performed however.tableName
- The table name.clearBefore
- Flag to clear the table of all rows prior to inserting the data. Please note that this flag sometime cannot betrue
if there are constraints on e.g. other tables.columns
- The field names, leave empty in case the data contains all fields in correct ordering, thus indicies can be used instead of the names.- Throws:
NullPointerException
- If any parameter isnull
.IllegalArgumentException
- If thecolumns
are not specified.SQLException
- For SQL errors.IOException
- For I/O errors.
Method Details
createParser
Creates the CSV parser.- Parameters:
reader
- The buffered reader.- Returns:
- The CSV parser, the default is
new CSVParser(in,CSVFormat.ORACLE)
. - Throws:
IOException
- For I/O errors.
close
Closes the input stream or reader and the prepared SQL statement for row insertion: both are attempted to be closed, and if the input stream or reader throws an IOException the SQL statement will be closed. If that one throws an SQLException, the potential IOException will be added as a suppressed exception. Otherwise the potential IOException is thrown.- Specified by:
close
in interfaceAutoCloseable
- Throws:
IOException
- For I/O errors.SQLException
- For SQL errors.
processReader
Processes the reader.A subclass can override this method to perform specialized reader handling. The default is to read all lines until
EOF
and call{@link #processLine(line)}
for each line, skipping empty lines. Each line produces a new row in the table, then the prepared insert statement's parameters are cleared (before next row is added).- Throws:
IOException
- For stream reader I/O error.SQLException
- For SQL errors.
processReader
Processes the reader.A subclass can override this method to perform specialized reader handling. The default is to read all lines until
EOF
and call{@link #processLine(line)}
for each line, skipping empty lines. Each line produces a new row in the table, then the prepared insert statement's parameters are cleared (before next row is added).- Throws:
IOException
- For stream reader I/O error.SQLException
- For SQL errors.
processLine
Retrieves and sets all the column values in the prepared statement using theCSVRecord
.Subclasses can override this method to provide the line scanning to set the columns with its data using the indicies of the columns.
- Parameters:
rec
- The CSV record.- Throws:
IOException
- For stream reader I/O error.SQLException
- For SQL errors.