From 39561b4a949e72857f3286a2390149f48688fa4c Mon Sep 17 00:00:00 2001 From: lilgreenbird Date: Thu, 22 Dec 2022 11:03:54 -0800 Subject: [PATCH] cleanup (#2012) --- .../microsoft/sqlserver/jdbc/Parameter.java | 41 +++++++++---------- .../sqlserver/jdbc/SQLServerConnection.java | 13 +----- 2 files changed, 21 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/Parameter.java b/src/main/java/com/microsoft/sqlserver/jdbc/Parameter.java index 19886fd31..2e0c33103 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/Parameter.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/Parameter.java @@ -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 @@ -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(); } diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java index 275b7fd0d..b74aac552 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java @@ -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()); @@ -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;