package com.microsoft.sqlserver.jdbc;
import java.text.MessageFormat;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
abstract class SQLServerBulkRecord implements ISQLServerBulkRecord {
private static final long serialVersionUID = -170992637946357449L;
protected class ColumnMetadata {
String columnName;
int columnType;
int precision;
int scale;
DateTimeFormatter dateTimeFormatter = null;
ColumnMetadata(String name, int type, int precision, int scale, DateTimeFormatter dateTimeFormatter) {
columnName = name;
columnType = type;
this.precision = precision;
this.scale = scale;
this.dateTimeFormatter = dateTimeFormatter;
}
}
protected String[] columnNames = null;
protected Map<Integer, ColumnMetadata> columnMetadata;
protected DateTimeFormatter dateTimeFormatter = null;
protected DateTimeFormatter timeFormatter = null;
protected String loggerPackageName = "com.microsoft.jdbc.SQLServerBulkRecord";
protected static java.util.logging.Logger loggerExternal = java.util.logging.Logger
.getLogger("com.microsoft.jdbc.SQLServerBulkRecord");
@Override
public void addColumnMetadata(int positionInSource, String name, int jdbcType, int precision, int scale,
DateTimeFormatter dateTimeFormatter) throws SQLServerException {
addColumnMetadataInternal(positionInSource, name, jdbcType, precision, scale, dateTimeFormatter);
}
@Override
public void addColumnMetadata(int positionInSource, String name, int jdbcType, int precision,
int scale) throws SQLServerException {
addColumnMetadataInternal(positionInSource, name, jdbcType, precision, scale, null);
}
void addColumnMetadataInternal(int positionInSource, String name, int jdbcType, int precision, int scale,
DateTimeFormatter dateTimeFormatter) throws SQLServerException {}
@Override
public void setTimestampWithTimezoneFormat(String dateTimeFormat) {
loggerExternal.entering(loggerPackageName, "setTimestampWithTimezoneFormat", dateTimeFormat);
this.dateTimeFormatter = DateTimeFormatter.ofPattern(dateTimeFormat);
loggerExternal.exiting(loggerPackageName, "setTimestampWithTimezoneFormat");
}
@Override
public void setTimestampWithTimezoneFormat(DateTimeFormatter dateTimeFormatter) {
if (loggerExternal.isLoggable(java.util.logging.Level.FINER)) {
loggerExternal.entering(loggerPackageName, "setTimestampWithTimezoneFormat",
new Object[] {dateTimeFormatter});
}
this.dateTimeFormatter = dateTimeFormatter;
loggerExternal.exiting(loggerPackageName, "setTimestampWithTimezoneFormat");
}
@Override
public void setTimeWithTimezoneFormat(String timeFormat) {
loggerExternal.entering(loggerPackageName, "setTimeWithTimezoneFormat", timeFormat);
this.timeFormatter = DateTimeFormatter.ofPattern(timeFormat);
loggerExternal.exiting(loggerPackageName, "setTimeWithTimezoneFormat");
}
@Override
public void setTimeWithTimezoneFormat(DateTimeFormatter dateTimeFormatter) {
if (loggerExternal.isLoggable(java.util.logging.Level.FINER)) {
loggerExternal.entering(loggerPackageName, "setTimeWithTimezoneFormat", new Object[] {dateTimeFormatter});
}
this.timeFormatter = dateTimeFormatter;
loggerExternal.exiting(loggerPackageName, "setTimeWithTimezoneFormat");
}
protected void throwInvalidArgument(String argument) throws SQLServerException {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_invalidArgument"));
Object[] msgArgs = {argument};
SQLServerException.makeFromDriverError(null, null, form.format(msgArgs), null, false);
}
protected void checkDuplicateColumnName(int positionInTable, String colName) throws SQLServerException {
if (null != colName && colName.trim().length() != 0) {
for (Entry<Integer, ColumnMetadata> entry : columnMetadata.entrySet()) {
if (null != entry && entry.getKey() != positionInTable) {
if (null != entry.getValue() && colName.trim().equalsIgnoreCase(entry.getValue().columnName)) {
throw new SQLServerException(SQLServerException.getErrString("R_BulkDataDuplicateColumn"),
null);
}
}
}
}
}
@Override
public DateTimeFormatter getColumnDateTimeFormatter(int column) {
return columnMetadata.get(column).dateTimeFormatter;
}
@Override
public Set<Integer> getColumnOrdinals() {
return columnMetadata.keySet();
}
@Override
public String getColumnName(int column) {
return columnMetadata.get(column).columnName;
}
@Override
public int getColumnType(int column) {
return columnMetadata.get(column).columnType;
}
@Override
public int getPrecision(int column) {
return columnMetadata.get(column).precision;
}
@Override
public int getScale(int column) {
return columnMetadata.get(column).scale;
}
@Override
public boolean isAutoIncrement(int column) {
return false;
}
}