Refactor
This commit is contained in:
parent
057761fdd9
commit
20f71fadd3
|
@ -93,6 +93,21 @@ const SCHEMA: Integration = {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const defaultTypeCasting = function (field: any, next: any) {
|
||||||
|
if (
|
||||||
|
field.type == "DATETIME" ||
|
||||||
|
field.type === "DATE" ||
|
||||||
|
field.type === "TIMESTAMP" ||
|
||||||
|
field.type === "LONGLONG"
|
||||||
|
) {
|
||||||
|
return field.string()
|
||||||
|
}
|
||||||
|
if (field.type === "BIT" && field.length === 1) {
|
||||||
|
return field.buffer()?.[0]
|
||||||
|
}
|
||||||
|
return next()
|
||||||
|
}
|
||||||
|
|
||||||
export function bindingTypeCoerce(bindings: any[]) {
|
export function bindingTypeCoerce(bindings: any[]) {
|
||||||
for (let i = 0; i < bindings.length; i++) {
|
for (let i = 0; i < bindings.length; i++) {
|
||||||
const binding = bindings[i]
|
const binding = bindings[i]
|
||||||
|
@ -147,24 +162,9 @@ class MySQLIntegration extends Sql implements DatasourcePlus {
|
||||||
delete config.rejectUnauthorized
|
delete config.rejectUnauthorized
|
||||||
this.config = {
|
this.config = {
|
||||||
...config,
|
...config,
|
||||||
|
typeCast: defaultTypeCasting,
|
||||||
multipleStatements: true,
|
multipleStatements: true,
|
||||||
}
|
}
|
||||||
if (!this.config.typeCast) {
|
|
||||||
this.config.typeCast = function (field: any, next: any) {
|
|
||||||
if (
|
|
||||||
field.type == "DATETIME" ||
|
|
||||||
field.type === "DATE" ||
|
|
||||||
field.type === "TIMESTAMP" ||
|
|
||||||
field.type === "LONGLONG"
|
|
||||||
) {
|
|
||||||
return field.string()
|
|
||||||
}
|
|
||||||
if (field.type === "BIT" && field.length === 1) {
|
|
||||||
return field.buffer()?.[0]
|
|
||||||
}
|
|
||||||
return next()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async testConnection() {
|
async testConnection() {
|
||||||
|
@ -196,6 +196,37 @@ class MySQLIntegration extends Sql implements DatasourcePlus {
|
||||||
return `concat(${parts.join(", ")})`
|
return `concat(${parts.join(", ")})`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defineTypeCastingFromSchema(schema: {
|
||||||
|
[key: string]: { name: string; type: string }
|
||||||
|
}): void {
|
||||||
|
if (!schema) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
this.config.typeCast = function (field: any, next: any) {
|
||||||
|
if (schema[field.name]?.name === field.name) {
|
||||||
|
if (["LONGLONG", "NEWDECIMAL", "DECIMAL"].includes(field.type)) {
|
||||||
|
if (schema[field.name]?.type === "number") {
|
||||||
|
const value = field.string()
|
||||||
|
return value ? Number(value) : null
|
||||||
|
} else {
|
||||||
|
return field.string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
field.type == "DATETIME" ||
|
||||||
|
field.type === "DATE" ||
|
||||||
|
field.type === "TIMESTAMP"
|
||||||
|
) {
|
||||||
|
return field.string()
|
||||||
|
}
|
||||||
|
if (field.type === "BIT" && field.length === 1) {
|
||||||
|
return field.buffer()?.[0]
|
||||||
|
}
|
||||||
|
return next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async connect() {
|
async connect() {
|
||||||
this.client = await mysql.createConnection(this.config)
|
this.client = await mysql.createConnection(this.config)
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,34 +70,11 @@ class QueryRunner {
|
||||||
datasourceClone.config.authConfigs = updatedConfigs
|
datasourceClone.config.authConfigs = updatedConfigs
|
||||||
}
|
}
|
||||||
|
|
||||||
if (datasource.source === SourceName.MYSQL && schema) {
|
|
||||||
datasourceClone.config.typeCast = function (field: any, next: any) {
|
|
||||||
if (schema[field.name]?.name === field.name) {
|
|
||||||
if (["LONGLONG", "NEWDECIMAL", "DECIMAL"].includes(field.type)) {
|
|
||||||
if (schema[field.name]?.type === "number") {
|
|
||||||
const value = field.string()
|
|
||||||
return value ? Number(value) : null
|
|
||||||
} else {
|
|
||||||
return field.string()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (
|
|
||||||
field.type == "DATETIME" ||
|
|
||||||
field.type === "DATE" ||
|
|
||||||
field.type === "TIMESTAMP"
|
|
||||||
) {
|
|
||||||
return field.string()
|
|
||||||
}
|
|
||||||
if (field.type === "BIT" && field.length === 1) {
|
|
||||||
return field.buffer()?.[0]
|
|
||||||
}
|
|
||||||
return next()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const integration = new Integration(datasourceClone.config)
|
const integration = new Integration(datasourceClone.config)
|
||||||
|
|
||||||
|
// define the type casting from the schema
|
||||||
|
integration.defineTypeCastingFromSchema?.(schema)
|
||||||
|
|
||||||
// pre-query, make sure datasource variables are added to parameters
|
// pre-query, make sure datasource variables are added to parameters
|
||||||
const parameters = await this.addDatasourceVariables()
|
const parameters = await this.addDatasourceVariables()
|
||||||
|
|
||||||
|
|
|
@ -166,6 +166,12 @@ export interface IntegrationBase {
|
||||||
delete?(query: any): Promise<any[] | any>
|
delete?(query: any): Promise<any[] | any>
|
||||||
testConnection?(): Promise<ConnectionInfo>
|
testConnection?(): Promise<ConnectionInfo>
|
||||||
getExternalSchema?(): Promise<string>
|
getExternalSchema?(): Promise<string>
|
||||||
|
defineTypeCastingFromSchema?(schema: {
|
||||||
|
[key: string]: {
|
||||||
|
name: string
|
||||||
|
type: string
|
||||||
|
}
|
||||||
|
}): void
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DatasourcePlus extends IntegrationBase {
|
export interface DatasourcePlus extends IntegrationBase {
|
||||||
|
|
Loading…
Reference in New Issue