|
|
|
@ -15,6 +15,7 @@ import { |
|
|
|
} from "./utils" |
|
|
|
import { DatasourcePlus } from "./base/datasourcePlus" |
|
|
|
import dayjs from "dayjs" |
|
|
|
import { FieldTypes } from "../constants" |
|
|
|
const { NUMBER_REGEX } = require("../utilities") |
|
|
|
|
|
|
|
module MySQLModule { |
|
|
|
@ -101,7 +102,7 @@ module MySQLModule { |
|
|
|
} |
|
|
|
// 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()) { |
|
|
|
else if (/^\d/.test(binding) && dayjs(binding).isValid()) { |
|
|
|
bindings[i] = dayjs(binding).toDate() |
|
|
|
} |
|
|
|
} |
|
|
|
@ -151,20 +152,24 @@ module MySQLModule { |
|
|
|
|
|
|
|
async internalQuery( |
|
|
|
query: SqlQuery, |
|
|
|
connect: boolean = true |
|
|
|
opts: { connect?: boolean; disableCoercion?: boolean } = { |
|
|
|
connect: true, |
|
|
|
disableCoercion: false, |
|
|
|
} |
|
|
|
): Promise<any[] | any> { |
|
|
|
try { |
|
|
|
if (connect) { |
|
|
|
if (opts?.connect) { |
|
|
|
await this.connect() |
|
|
|
} |
|
|
|
const baseBindings = query.bindings || [] |
|
|
|
const bindings = opts?.disableCoercion |
|
|
|
? baseBindings |
|
|
|
: bindingTypeCoerce(baseBindings) |
|
|
|
// Node MySQL is callback based, so we must wrap our call in a promise
|
|
|
|
const response = await this.client.query( |
|
|
|
query.sql, |
|
|
|
bindingTypeCoerce(query.bindings || []) |
|
|
|
) |
|
|
|
const response = await this.client.query(query.sql, bindings) |
|
|
|
return response[0] |
|
|
|
} finally { |
|
|
|
if (connect) { |
|
|
|
if (opts?.connect) { |
|
|
|
await this.disconnect() |
|
|
|
} |
|
|
|
} |
|
|
|
@ -179,7 +184,7 @@ module MySQLModule { |
|
|
|
// get the tables first
|
|
|
|
const tablesResp = await this.internalQuery( |
|
|
|
{ sql: "SHOW TABLES;" }, |
|
|
|
false |
|
|
|
{ connect: false } |
|
|
|
) |
|
|
|
const tableNames = tablesResp.map( |
|
|
|
(obj: any) => |
|
|
|
@ -191,7 +196,7 @@ module MySQLModule { |
|
|
|
const schema: TableSchema = {} |
|
|
|
const descResp = await this.internalQuery( |
|
|
|
{ sql: `DESCRIBE \`${tableName}\`;` }, |
|
|
|
false |
|
|
|
{ connect: false } |
|
|
|
) |
|
|
|
for (let column of descResp) { |
|
|
|
const columnName = column.Field |
|
|
|
@ -211,8 +216,8 @@ module MySQLModule { |
|
|
|
schema[columnName] = { |
|
|
|
name: columnName, |
|
|
|
autocolumn: isAuto, |
|
|
|
type: convertSqlType(column.Type), |
|
|
|
constraints, |
|
|
|
...convertSqlType(column.Type), |
|
|
|
} |
|
|
|
} |
|
|
|
if (!tables[tableName]) { |
|
|
|
@ -254,7 +259,8 @@ module MySQLModule { |
|
|
|
async query(json: QueryJson) { |
|
|
|
await this.connect() |
|
|
|
try { |
|
|
|
const queryFn = (query: any) => this.internalQuery(query, false) |
|
|
|
const queryFn = (query: any) => |
|
|
|
this.internalQuery(query, { connect: false, disableCoercion: true }) |
|
|
|
return await this.queryWithReturning(json, queryFn) |
|
|
|
} finally { |
|
|
|
await this.disconnect() |
|
|
|
|