Merge pull request #1974 from Budibase/fix/sql-input-issues
Fixing issues seen with numbers/dates in MySQL
This commit is contained in:
commit
e922e45eaf
4 changed files with 33 additions and 5 deletions
|
@ -2,6 +2,8 @@ CREATE DATABASE IF NOT EXISTS main;
|
||||||
USE main;
|
USE main;
|
||||||
CREATE TABLE Persons (
|
CREATE TABLE Persons (
|
||||||
PersonID int NOT NULL AUTO_INCREMENT,
|
PersonID int NOT NULL AUTO_INCREMENT,
|
||||||
|
CreatedAt datetime,
|
||||||
|
Age float,
|
||||||
LastName varchar(255),
|
LastName varchar(255),
|
||||||
FirstName varchar(255),
|
FirstName varchar(255),
|
||||||
Address varchar(255),
|
Address varchar(255),
|
||||||
|
@ -17,6 +19,6 @@ CREATE TABLE Tasks (
|
||||||
FOREIGN KEY(PersonID)
|
FOREIGN KEY(PersonID)
|
||||||
REFERENCES Persons(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, 'assembling');
|
||||||
INSERT INTO Tasks (PersonID, TaskName) VALUES (1, 'processing');
|
INSERT INTO Tasks (PersonID, TaskName) VALUES (1, 'processing');
|
||||||
|
|
|
@ -8,9 +8,24 @@ import {
|
||||||
Operation,
|
Operation,
|
||||||
RelationshipsJson,
|
RelationshipsJson,
|
||||||
} from "../../definitions/datasource"
|
} from "../../definitions/datasource"
|
||||||
|
import { isIsoDateString } from "../utils"
|
||||||
|
|
||||||
type KnexQuery = Knex.QueryBuilder | Knex
|
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
|
// right now we only do filters on the specific table being queried
|
||||||
function addFilters(
|
function addFilters(
|
||||||
tableName: string,
|
tableName: string,
|
||||||
|
@ -119,11 +134,12 @@ function buildCreate(
|
||||||
): KnexQuery {
|
): KnexQuery {
|
||||||
const { endpoint, body } = json
|
const { endpoint, body } = json
|
||||||
let query: KnexQuery = knex(endpoint.entityId)
|
let query: KnexQuery = knex(endpoint.entityId)
|
||||||
|
const parsedBody = parseBody(body)
|
||||||
// mysql can't use returning
|
// mysql can't use returning
|
||||||
if (opts.disableReturning) {
|
if (opts.disableReturning) {
|
||||||
return query.insert(body)
|
return query.insert(parsedBody)
|
||||||
} else {
|
} else {
|
||||||
return query.insert(body).returning("*")
|
return query.insert(parsedBody).returning("*")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,12 +189,13 @@ function buildUpdate(
|
||||||
): KnexQuery {
|
): KnexQuery {
|
||||||
const { endpoint, body, filters } = json
|
const { endpoint, body, filters } = json
|
||||||
let query: KnexQuery = knex(endpoint.entityId)
|
let query: KnexQuery = knex(endpoint.entityId)
|
||||||
|
const parsedBody = parseBody(body)
|
||||||
query = addFilters(endpoint.entityId, query, filters)
|
query = addFilters(endpoint.entityId, query, filters)
|
||||||
// mysql can't use returning
|
// mysql can't use returning
|
||||||
if (opts.disableReturning) {
|
if (opts.disableReturning) {
|
||||||
return query.update(body)
|
return query.update(parsedBody)
|
||||||
} else {
|
} else {
|
||||||
return query.update(body).returning("*")
|
return query.update(parsedBody).returning("*")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,6 +29,7 @@ module MySQLModule {
|
||||||
blob: FieldTypes.LONGFORM,
|
blob: FieldTypes.LONGFORM,
|
||||||
enum: FieldTypes.STRING,
|
enum: FieldTypes.STRING,
|
||||||
varchar: FieldTypes.STRING,
|
varchar: FieldTypes.STRING,
|
||||||
|
float: FieldTypes.NUMBER,
|
||||||
int: FieldTypes.NUMBER,
|
int: FieldTypes.NUMBER,
|
||||||
numeric: FieldTypes.NUMBER,
|
numeric: FieldTypes.NUMBER,
|
||||||
bigint: FieldTypes.NUMBER,
|
bigint: FieldTypes.NUMBER,
|
||||||
|
|
|
@ -68,3 +68,11 @@ export function isSQL(datasource: Datasource): boolean {
|
||||||
const SQL = [SourceNames.POSTGRES, SourceNames.SQL_SERVER, SourceNames.MYSQL]
|
const SQL = [SourceNames.POSTGRES, SourceNames.SQL_SERVER, SourceNames.MYSQL]
|
||||||
return SQL.indexOf(datasource.source) !== -1
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue