Skip to content

Commit

Permalink
Fixes #1667 [API] DelegatingCallableStatement failed to invoke setDate
Browse files Browse the repository at this point in the history
  • Loading branch information
ThuF committed Mar 25, 2022
1 parent 9ace641 commit a7d4728
Showing 1 changed file with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ function PreparedStatement(internalStatement) {

this.setDate = function (index, value) {
if (value !== null && value !== undefined) {
var dateInstance = new java.sql.Date(value.getTime());
let date = getDateValue(value);
let dateInstance = new java.sql.Date(date.getTime());
this.native.setDate(index, dateInstance);
} else {
this.setNull(index, SQLTypes.DATE);
Expand Down Expand Up @@ -339,7 +340,8 @@ function PreparedStatement(internalStatement) {

this.setTime = function (index, value) {
if (value !== null && value !== undefined) {
var timeInstance = new java.sql.Time(value.getTime());
let date = getDateValue(value);
let timeInstance = new java.sql.Time(date.getTime());
this.native.setTime(index, timeInstance);
} else {
this.setNull(index, SQLTypes.TIME);
Expand All @@ -348,7 +350,8 @@ function PreparedStatement(internalStatement) {

this.setTimestamp = function (index, value) {
if (value !== null && value !== undefined) {
var timestampInstance = new java.sql.Timestamp(value.getTime());
let date = getDateValue(value);
let timestampInstance = new java.sql.Timestamp(date.getTime());
this.native.setTimestamp(index, timestampInstance);
} else {
this.setNull(index, SQLTypes.TIMESTAMP);
Expand Down Expand Up @@ -635,23 +638,29 @@ function CallableStatement() {

this.setDate = function (parameter, value) {
if (value !== null && value !== undefined) {
this.native.setDate(parameter, value);
let date = getDateValue(value);
let dateInstance = new java.sql.Date(date.getTime());
this.native.setDate(parameter, dateInstance);
} else {
this.setNull(parameter, SQLTypes.DATE);
}
};

this.setTime = function (parameter, value) {
if (value !== null && value !== undefined) {
this.native.setTime(parameter, value);
let date = getDateValue(value);
let timeInstance = new java.sql.Time(date.getTime());
this.native.setTime(parameter, timeInstance);
} else {
this.setNull(parameter, SQLTypes.TIME);
}
};

this.setTimestamp = function (parameter, value) {
if (value !== null && value !== undefined) {
this.native.setTimestamp(parameter, value);
let date = getDateValue(value);
let timestampInstance = new java.sql.Timestamp(date.getTime());
this.native.setTimestamp(parameter, timestampInstance);
} else {
this.setNull(parameter, SQLTypes.TIMESTAMP);
}
Expand Down Expand Up @@ -990,4 +999,15 @@ function createNClobValue(native, value) {
} catch (e) {
throw new Error(`Error occured during creation of 'NClob' value: ${e.message}`);
}
}

function getDateValue(value) {
if (typeof value === "string" && isValidDateString(value)) {
return new Date(value);
}
return value;
}

function isValidDateString(value) {
return (new Date(value) !== "Invalid Date") && !isNaN(new Date(value));
}

0 comments on commit a7d4728

Please sign in to comment.