From 39182cfbc097071a93c14f126fef66606369ad0f Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 1 Apr 2022 17:13:28 +0100 Subject: [PATCH] Fixing issue #5817 - making sure that date strings are correctly parsed into the bindings. --- packages/server/scripts/integrations/mysql/init.sql | 5 +++-- packages/server/src/integrations/mysql.ts | 9 ++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/server/scripts/integrations/mysql/init.sql b/packages/server/scripts/integrations/mysql/init.sql index f37ef0d532..9fa608f42d 100644 --- a/packages/server/scripts/integrations/mysql/init.sql +++ b/packages/server/scripts/integrations/mysql/init.sql @@ -14,6 +14,7 @@ CREATE TABLE Tasks ( TaskID int NOT NULL AUTO_INCREMENT, PersonID INT, TaskName varchar(255), + CreatedAt DATE, PRIMARY KEY (TaskID), CONSTRAINT fkPersons FOREIGN KEY(PersonID) @@ -25,6 +26,6 @@ CREATE TABLE Products ( updated time ); INSERT INTO Persons (FirstName, LastName, Age, Address, City, CreatedAt) VALUES ('Mike', 'Hughes', 28.2, '123 Fake Street', 'Belfast', '2021-01-19 03:14:07'); -INSERT INTO Tasks (PersonID, TaskName) VALUES (1, 'assembling'); -INSERT INTO Tasks (PersonID, TaskName) VALUES (1, 'processing'); +INSERT INTO Tasks (PersonID, TaskName, CreatedAt) VALUES (1, 'assembling', '2020-01-01'); +INSERT INTO Tasks (PersonID, TaskName, CreatedAt) VALUES (2, 'processing', '2019-12-31'); INSERT INTO Products (name, updated) VALUES ('Meat', '11:00:22'), ('Fruit', '10:00:00'); diff --git a/packages/server/src/integrations/mysql.ts b/packages/server/src/integrations/mysql.ts index 6f009bbd4a..cd5dc77f9b 100644 --- a/packages/server/src/integrations/mysql.ts +++ b/packages/server/src/integrations/mysql.ts @@ -14,6 +14,7 @@ import { finaliseExternalTables, } from "./utils" import { DatasourcePlus } from "./base/datasourcePlus" +import dayjs from "dayjs" module MySQLModule { const mysql = require("mysql2/promise") @@ -86,10 +87,16 @@ module MySQLModule { if (typeof binding !== "string") { continue } - const matches = binding.match(/^\d*/g) + const matches = binding.match(/^\d*$/g) + // check if number first if (matches && matches[0] !== "" && !isNaN(Number(matches[0]))) { bindings[i] = parseFloat(binding) } + // if not a number, see if it is a date - important to do in this order as any + // integer will be considered a valid date + else if (dayjs(binding).isValid()) { + bindings[i] = dayjs(binding).toDate() + } } return bindings }