Skip to content

Commit

Permalink
cleanup (#2012)
Browse files Browse the repository at this point in the history
  • Loading branch information
lilgreenbird authored Dec 22, 2022
1 parent 45c606f commit 39561b4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 33 deletions.
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

0 comments on commit 39561b4

Please sign in to comment.