Package com.microsoft.sqlserver.jdbc
Class SQLServerBulkCSVFileRecord
- java.lang.Object
-
- com.microsoft.sqlserver.jdbc.SQLServerBulkCSVFileRecord
-
- All Implemented Interfaces:
ISQLServerBulkData
,ISQLServerBulkRecord
,Serializable
,AutoCloseable
public class SQLServerBulkCSVFileRecord extends Object implements AutoCloseable
Provides a simple implementation of the ISQLServerBulkRecord interface that can be used to read in the basic Java data types from a delimited file where each line represents a row of data.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected Map<Integer,com.microsoft.sqlserver.jdbc.SQLServerBulkRecord.ColumnMetadata>
columnMetadata
Metadata to represent the columns in the batch/file.protected String[]
columnNames
Contains all the column names if firstLineIsColumnNames is trueprotected DateTimeFormatter
dateTimeFormatter
Contains the format that java.sql.Types.TIMESTAMP_WITH_TIMEZONE data should be read in as.protected DateTimeFormatter
timeFormatter
Contains the format that java.sql.Types.TIME_WITH_TIMEZONE data should be read in as.
-
Constructor Summary
Constructors Constructor Description SQLServerBulkCSVFileRecord(InputStream fileToParse, String encoding, String delimiter, boolean firstLineIsColumnNames)
Constructs a SQLServerBulkCSVFileRecord to parse data from a delimited file with the given encoding.SQLServerBulkCSVFileRecord(String fileToParse, boolean firstLineIsColumnNames)
Constructs a SQLServerBulkCSVFileRecord to parse data from a CSV file with the default encoding.SQLServerBulkCSVFileRecord(String fileToParse, String encoding, boolean firstLineIsColumnNames)
Constructs a SQLServerBulkCSVFileRecord to parse data from a CSV file with the given encoding.SQLServerBulkCSVFileRecord(String fileToParse, String encoding, String delimiter, boolean firstLineIsColumnNames)
Constructs a simple reader to parse data from a delimited file with the given encoding.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addColumnMetadata(int positionInSource, String name, int jdbcType, int precision, int scale)
Adds metadata for the given column in the file.void
addColumnMetadata(int positionInSource, String name, int jdbcType, int precision, int scale, DateTimeFormatter dateTimeFormatter)
Adds metadata for the given column in the file.void
close()
Releases any resources associated with the file reader.DateTimeFormatter
getColumnDateTimeFormatter(int column)
Returns thedateTimeFormatter
for the given column.String
getColumnName(int column)
Returns the name of the given column.Set<Integer>
getColumnOrdinals()
Returns the ordinals for each of the columns represented in this data record.int
getColumnType(int column)
Returns the JDBC data type of the given column.int
getPrecision(int column)
Returns the precision for the given column.Object[]
getRowData()
Returns the data for the current row as an array of Objects.int
getScale(int column)
Returns the scale for the given column.boolean
isAutoIncrement(int column)
Returns whether the column represents an identity column.boolean
isEscapeColumnDelimitersCSV()
Returns whether the rules to escape delimiters are used.boolean
next()
Advances to the next data row.void
setEscapeColumnDelimitersCSV(boolean escapeDelimiters)
When set to true, the following rules will be used to parse CSV files: Each field may or may not be enclosed in double quotes.void
setTimestampWithTimezoneFormat(String dateTimeFormat)
Sets the format for reading in dates from the file.void
setTimestampWithTimezoneFormat(DateTimeFormatter dateTimeFormatter)
Sets the format for reading in dates from the file.void
setTimeWithTimezoneFormat(String timeFormat)
Sets the format for reading in dates from the file.void
setTimeWithTimezoneFormat(DateTimeFormatter dateTimeFormatter)
Sets the format for reading in dates from the file.
-
-
-
Field Detail
-
columnNames
protected String[] columnNames
Contains all the column names if firstLineIsColumnNames is true
-
columnMetadata
protected transient Map<Integer,com.microsoft.sqlserver.jdbc.SQLServerBulkRecord.ColumnMetadata> columnMetadata
Metadata to represent the columns in the batch/file. Each column should be mapped to its corresponding position within the parameter (from position 1 and onwards)
-
dateTimeFormatter
protected transient DateTimeFormatter dateTimeFormatter
Contains the format that java.sql.Types.TIMESTAMP_WITH_TIMEZONE data should be read in as.
-
timeFormatter
protected transient DateTimeFormatter timeFormatter
Contains the format that java.sql.Types.TIME_WITH_TIMEZONE data should be read in as.
-
-
Constructor Detail
-
SQLServerBulkCSVFileRecord
public SQLServerBulkCSVFileRecord(String fileToParse, String encoding, String delimiter, boolean firstLineIsColumnNames) throws SQLServerException
Constructs a simple reader to parse data from a delimited file with the given encoding.- Parameters:
fileToParse
- File to parse data from.encoding
- Charset encoding to use for reading the file, or NULL for the default encoding.delimiter
- Delimiter to used to separate each column. Regex characters must be escaped with double backslashes.firstLineIsColumnNames
- True if the first line of the file should be parsed as column names; false otherwise- Throws:
SQLServerException
- If the arguments are invalid, there are any errors in reading the file, or the file is empty
-
SQLServerBulkCSVFileRecord
public SQLServerBulkCSVFileRecord(InputStream fileToParse, String encoding, String delimiter, boolean firstLineIsColumnNames) throws SQLServerException
Constructs a SQLServerBulkCSVFileRecord to parse data from a delimited file with the given encoding.- Parameters:
fileToParse
- InputStream to parse data fromencoding
- Charset encoding to use for reading the file, or NULL for the default encoding.delimiter
- Delimiter to used to separate each column. Regex characters must be escaped with double backslashes.firstLineIsColumnNames
- True if the first line of the file should be parsed as column names; false otherwise- Throws:
SQLServerException
- If the arguments are invalid, there are any errors in reading the file, or the file is empty
-
SQLServerBulkCSVFileRecord
public SQLServerBulkCSVFileRecord(String fileToParse, String encoding, boolean firstLineIsColumnNames) throws SQLServerException
Constructs a SQLServerBulkCSVFileRecord to parse data from a CSV file with the given encoding.- Parameters:
fileToParse
- File to parse data fromencoding
- Charset encoding to use for reading the file.firstLineIsColumnNames
- True if the first line of the file should be parsed as column names; false otherwise- Throws:
SQLServerException
- If the arguments are invalid, there are any errors in reading the file, or the file is empty
-
SQLServerBulkCSVFileRecord
public SQLServerBulkCSVFileRecord(String fileToParse, boolean firstLineIsColumnNames) throws SQLServerException
Constructs a SQLServerBulkCSVFileRecord to parse data from a CSV file with the default encoding.- Parameters:
fileToParse
- File to parse data fromfirstLineIsColumnNames
- True if the first line of the file should be parsed as column names; false otherwise- Throws:
SQLServerException
- If the arguments are invalid, there are any errors in reading the file, or the file is empty
-
-
Method Detail
-
close
public void close() throws SQLServerException
Releases any resources associated with the file reader.- Specified by:
close
in interfaceAutoCloseable
- Throws:
SQLServerException
- when an error occurs
-
getRowData
public Object[] getRowData() throws SQLServerException
Description copied from interface:ISQLServerBulkData
Returns the data for the current row as an array of Objects. Each Object must match the Java language Type that is used to represent the indicated JDBC data type for the given column. For more information, see 'Understanding the JDBC Driver Data Types' for the appropriate mappings.- Specified by:
getRowData
in interfaceISQLServerBulkData
- Returns:
- The data for the row.
- Throws:
SQLServerException
- If there are any errors in obtaining the data.
-
next
public boolean next() throws SQLServerException
Description copied from interface:ISQLServerBulkData
Advances to the next data row.- Specified by:
next
in interfaceISQLServerBulkData
- Returns:
- True if rows are available; false if there are no more rows
- Throws:
SQLServerException
- If there are any errors in advancing to the next row.
-
isEscapeColumnDelimitersCSV
public boolean isEscapeColumnDelimitersCSV()
Returns whether the rules to escape delimiters are used.- Returns:
- true if the rules are used, false otherwise.
-
setEscapeColumnDelimitersCSV
public void setEscapeColumnDelimitersCSV(boolean escapeDelimiters)
When set to true, the following rules will be used to parse CSV files: Each field may or may not be enclosed in double quotes. If fields are not enclosed with double quotes, then double quotes may not appear inside the fields. Fields containing double quotes, and delimiters should be enclosed in double quotes. If double-quotes are used to enclose fields, then a double-quote appearing inside a field must be escaped by preceding it with another double quote. Spaces are considered part of a field. Spaces before and after enclosing double quotes are ignored.- Parameters:
escapeDelimiters
- true if the rules above to be used.
-
addColumnMetadata
public void addColumnMetadata(int positionInSource, String name, int jdbcType, int precision, int scale, DateTimeFormatter dateTimeFormatter) throws SQLServerException
Description copied from interface:ISQLServerBulkRecord
Adds metadata for the given column in the file.- Specified by:
addColumnMetadata
in interfaceISQLServerBulkRecord
- Parameters:
positionInSource
- Indicates which column the metadata is for. Columns start at 1.name
- Name for the column (optional if only using column ordinal in a mapping for SQLServerBulkCopy operation)jdbcType
- JDBC data type of the columnprecision
- Precision for the column (ignored for the appropriate data types)scale
- Scale for the column (ignored for the appropriate data types)dateTimeFormatter
- format to parse data that is sent- Throws:
SQLServerException
- when an error occurs
-
addColumnMetadata
public void addColumnMetadata(int positionInSource, String name, int jdbcType, int precision, int scale) throws SQLServerException
Description copied from interface:ISQLServerBulkRecord
Adds metadata for the given column in the file.- Specified by:
addColumnMetadata
in interfaceISQLServerBulkRecord
- Parameters:
positionInSource
- Indicates which column the metadata is for. Columns start at 1.name
- Name for the column (optional if only using column ordinal in a mapping for SQLServerBulkCopy operation)jdbcType
- JDBC data type of the columnprecision
- Precision for the column (ignored for the appropriate data types)scale
- Scale for the column (ignored for the appropriate data types)- Throws:
SQLServerException
- when an error occurs
-
setTimestampWithTimezoneFormat
public void setTimestampWithTimezoneFormat(String dateTimeFormat)
Description copied from interface:ISQLServerBulkRecord
Sets the format for reading in dates from the file.- Specified by:
setTimestampWithTimezoneFormat
in interfaceISQLServerBulkRecord
- Parameters:
dateTimeFormat
- format to parse data sent as java.sql.Types.TIMESTAMP_WITH_TIMEZONE
-
setTimestampWithTimezoneFormat
public void setTimestampWithTimezoneFormat(DateTimeFormatter dateTimeFormatter)
Description copied from interface:ISQLServerBulkRecord
Sets the format for reading in dates from the file.- Specified by:
setTimestampWithTimezoneFormat
in interfaceISQLServerBulkRecord
- Parameters:
dateTimeFormatter
- format to parse data sent as java.sql.Types.TIMESTAMP_WITH_TIMEZONE
-
setTimeWithTimezoneFormat
public void setTimeWithTimezoneFormat(String timeFormat)
Description copied from interface:ISQLServerBulkRecord
Sets the format for reading in dates from the file.- Specified by:
setTimeWithTimezoneFormat
in interfaceISQLServerBulkRecord
- Parameters:
timeFormat
- format to parse data sent as java.sql.Types.TIME_WITH_TIMEZONE
-
setTimeWithTimezoneFormat
public void setTimeWithTimezoneFormat(DateTimeFormatter dateTimeFormatter)
Description copied from interface:ISQLServerBulkRecord
Sets the format for reading in dates from the file.- Specified by:
setTimeWithTimezoneFormat
in interfaceISQLServerBulkRecord
- Parameters:
dateTimeFormatter
- format to parse data sent as java.sql.Types.TIME_WITH_TIMEZONE
-
getColumnDateTimeFormatter
public DateTimeFormatter getColumnDateTimeFormatter(int column)
Description copied from interface:ISQLServerBulkRecord
Returns thedateTimeFormatter
for the given column.- Specified by:
getColumnDateTimeFormatter
in interfaceISQLServerBulkRecord
- Parameters:
column
- Column ordinal- Returns:
- dateTimeFormatter
-
getColumnOrdinals
public Set<Integer> getColumnOrdinals()
Description copied from interface:ISQLServerBulkData
Returns the ordinals for each of the columns represented in this data record.- Specified by:
getColumnOrdinals
in interfaceISQLServerBulkData
- Returns:
- Set of ordinals for the columns.
-
getColumnName
public String getColumnName(int column)
Description copied from interface:ISQLServerBulkData
Returns the name of the given column.- Specified by:
getColumnName
in interfaceISQLServerBulkData
- Parameters:
column
- Column ordinal- Returns:
- Name of the column
-
getColumnType
public int getColumnType(int column)
Description copied from interface:ISQLServerBulkData
Returns the JDBC data type of the given column.- Specified by:
getColumnType
in interfaceISQLServerBulkData
- Parameters:
column
- Column ordinal- Returns:
- JDBC data type of the column
-
getPrecision
public int getPrecision(int column)
Description copied from interface:ISQLServerBulkData
Returns the precision for the given column.- Specified by:
getPrecision
in interfaceISQLServerBulkData
- Parameters:
column
- Column ordinal- Returns:
- Precision of the column
-
getScale
public int getScale(int column)
Description copied from interface:ISQLServerBulkData
Returns the scale for the given column.- Specified by:
getScale
in interfaceISQLServerBulkData
- Parameters:
column
- Column ordinal- Returns:
- Scale of the column
-
isAutoIncrement
public boolean isAutoIncrement(int column)
Description copied from interface:ISQLServerBulkRecord
Returns whether the column represents an identity column.- Specified by:
isAutoIncrement
in interfaceISQLServerBulkRecord
- Parameters:
column
- Column ordinal- Returns:
- True if the column is an identity column; false otherwise.
-
-