Merge pull request #1974 from Budibase/fix/sql-input-issues

Fixing issues seen with numbers/dates in MySQL
This commit is contained in:
Michael Drury 2021-07-12 13:07:31 +01:00 committed by GitHub
commit e922e45eaf
4 changed files with 33 additions and 5 deletions

View File

@ -2,6 +2,8 @@ CREATE DATABASE IF NOT EXISTS main;
USE main;
CREATE TABLE Persons (
PersonID int NOT NULL AUTO_INCREMENT,
CreatedAt datetime,
Age float,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
@ -17,6 +19,6 @@ CREATE TABLE Tasks (
FOREIGN KEY(PersonID)
REFERENCES Persons(PersonID)
);
INSERT INTO Persons (FirstName, LastName, Address, City) VALUES ('Mike', 'Hughes', '123 Fake Street', 'Belfast');
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');

View File

@ -8,9 +8,24 @@ import {
Operation,
RelationshipsJson,
} from "../../definitions/datasource"
import { isIsoDateString } from "../utils"
type KnexQuery = Knex.QueryBuilder | Knex
function parseBody(body: any) {
for (let [key, value] of Object.entries(body)) {
if (typeof value !== "string") {
continue
}
if (isIsoDateString(value)) {
body[key] = new Date(value)
} else if (!isNaN(parseFloat(value))) {
body[key] = parseFloat(value)
}
}
return body
}
// right now we only do filters on the specific table being queried
function addFilters(
tableName: string,
@ -119,11 +134,12 @@ function buildCreate(
): KnexQuery {
const { endpoint, body } = json
let query: KnexQuery = knex(endpoint.entityId)
const parsedBody = parseBody(body)
// mysql can't use returning
if (opts.disableReturning) {
return query.insert(body)
return query.insert(parsedBody)
} else {
return query.insert(body).returning("*")
return query.insert(parsedBody).returning("*")
}
}
@ -173,12 +189,13 @@ function buildUpdate(
): KnexQuery {
const { endpoint, body, filters } = json
let query: KnexQuery = knex(endpoint.entityId)
const parsedBody = parseBody(body)
query = addFilters(endpoint.entityId, query, filters)
// mysql can't use returning
if (opts.disableReturning) {
return query.update(body)
return query.update(parsedBody)
} else {
return query.update(body).returning("*")
return query.update(parsedBody).returning("*")
}
}

View File

@ -29,6 +29,7 @@ module MySQLModule {
blob: FieldTypes.LONGFORM,
enum: FieldTypes.STRING,
varchar: FieldTypes.STRING,
float: FieldTypes.NUMBER,
int: FieldTypes.NUMBER,
numeric: FieldTypes.NUMBER,
bigint: FieldTypes.NUMBER,

View File

@ -68,3 +68,11 @@ export function isSQL(datasource: Datasource): boolean {
const SQL = [SourceNames.POSTGRES, SourceNames.SQL_SERVER, SourceNames.MYSQL]
return SQL.indexOf(datasource.source) !== -1
}
export function isIsoDateString(str: string) {
if (!/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(str)) {
return false
}
let d = new Date(str)
return d.toISOString() === str
}