Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address review comments in PR1590 #2012

Merged
merged 1 commit into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 19 additions & 22 deletions src/main/java/com/microsoft/sqlserver/jdbc/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,6 @@ private void setTypeDefinition(DTV dtv) {
}
}


/**
* Generates the SQL datatype to use for Java date-based values. This
* setting can be controlled by setting the "datetimeParameterType" connection
Expand All @@ -905,31 +904,29 @@ String getDatetimeDataType(SQLServerConnection con, Integer scale) {
String datatype;

if (con.isKatmaiOrLater()) {
switch (con.getDatetimeParameterType()){
case "datetime2":
datatype = SSType.DATETIME2.toString();
if (scale != null){
datatype += "(" + scale + ")";
}
return datatype;
case "datetimeoffset":
datatype = SSType.DATETIMEOFFSET.toString();
if (scale != null){
datatype += "(" + scale + ")";
}
return datatype;
case "datetime":
default:
return SSType.DATETIME.toString();

String paramType = con.getDatetimeParameterType();
if (paramType.equalsIgnoreCase(DatetimeType.DATETIME2.toString())) {
datatype = SSType.DATETIME2.toString();
if (scale != null) {
datatype += "(" + scale + ")";
}
return datatype;
} else if (paramType.equalsIgnoreCase(DatetimeType.DATETIMEOFFSET.toString())) {
datatype = SSType.DATETIMEOFFSET.toString();
if (scale != null) {
datatype += "(" + scale + ")";
}
return datatype;
} else {
return SSType.DATETIME.toString();
}
}

/*
For older versions of SQL server and if for some reason the datetimeParameterType
connection property cannot be determined, we fall back to the "datetime"
format.
*/
* For older versions of SQL server and if for some reason the datetimeParameterType
* connection property cannot be determined, we fall back to the "datetime"
* format.
*/
return SSType.DATETIME.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2349,7 +2349,7 @@ Connection connectInternal(Properties propsIn,
if (null == sPropValue) {
sPropValue = SQLServerDriverStringProperty.DATETIME_DATATYPE.getDefaultValue();
}

datetimeParameterType = DatetimeType.valueOfString(sPropValue);
activeConnectionProperties.setProperty(sPropKey, datetimeParameterType.toString());

Expand Down Expand Up @@ -6908,22 +6908,13 @@ public void setSendTimeAsDatetime(boolean sendTimeAsDateTimeValue) {
@Override
public void setDatetimeParameterType(String datetimeParameterTypeValue) throws SQLServerException {
// cast the value to lowercase, so the case in the connection string does not matter
if (datetimeParameterTypeValue != null){
if (datetimeParameterTypeValue != null) {
datetimeParameterTypeValue = datetimeParameterTypeValue.toLowerCase();
}

if (!isValidDatetimeParameterType(datetimeParameterTypeValue)){
String errorMessage = "The timestamp encoding value (i.e. " + datetimeParameterTypeValue.toString() + ") must be: datetime, datetime2 or datetimeoffset.";
SQLServerException newe = new SQLServerException(errorMessage, null);
throw newe;
}
datetimeParameterType = DatetimeType.valueOfString(datetimeParameterTypeValue);
}

private boolean isValidDatetimeParameterType(String datetimeParameterTypeValue) {
return (datetimeParameterTypeValue.equals("datetime") || datetimeParameterTypeValue.equals("datetime2") || datetimeParameterTypeValue.equals("datetimeoffset"));
}

@Override
public void setUseFmtOnly(boolean useFmtOnly) {
this.useFmtOnly = useFmtOnly;
Expand Down