Browse Source
Merge pull request #5215 from Budibase/fix/5817
Fixing MySQL queries with dates
master
Michael Drury
5 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with
11 additions and
3 deletions
-
packages/server/scripts/integrations/mysql/init.sql
-
packages/server/src/integrations/mysql.ts
|
|
|
@ -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'); |
|
|
|
|
|
|
|
@ -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 |
|
|
|
} |
|
|
|
|