Merge pull request #5215 from Budibase/fix/5817

Fixing MySQL queries with dates
This commit is contained in:
Michael Drury 2022-04-04 10:47:43 +01:00 committed by GitHub
commit a5a5bf753f
2 changed files with 11 additions and 3 deletions

View File

@ -14,6 +14,7 @@ CREATE TABLE Tasks (
TaskID int NOT NULL AUTO_INCREMENT, TaskID int NOT NULL AUTO_INCREMENT,
PersonID INT, PersonID INT,
TaskName varchar(255), TaskName varchar(255),
CreatedAt DATE,
PRIMARY KEY (TaskID), PRIMARY KEY (TaskID),
CONSTRAINT fkPersons CONSTRAINT fkPersons
FOREIGN KEY(PersonID) FOREIGN KEY(PersonID)
@ -25,6 +26,6 @@ CREATE TABLE Products (
updated time 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 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, CreatedAt) VALUES (1, 'assembling', '2020-01-01');
INSERT INTO Tasks (PersonID, TaskName) VALUES (1, 'processing'); 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'); INSERT INTO Products (name, updated) VALUES ('Meat', '11:00:22'), ('Fruit', '10:00:00');

View File

@ -14,6 +14,7 @@ import {
finaliseExternalTables, finaliseExternalTables,
} from "./utils" } from "./utils"
import { DatasourcePlus } from "./base/datasourcePlus" import { DatasourcePlus } from "./base/datasourcePlus"
import dayjs from "dayjs"
module MySQLModule { module MySQLModule {
const mysql = require("mysql2/promise") const mysql = require("mysql2/promise")
@ -86,10 +87,16 @@ module MySQLModule {
if (typeof binding !== "string") { if (typeof binding !== "string") {
continue 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]))) { if (matches && matches[0] !== "" && !isNaN(Number(matches[0]))) {
bindings[i] = parseFloat(binding) 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 return bindings
} }