-
Notifications
You must be signed in to change notification settings - Fork 454
/
Mysql.ts
83 lines (81 loc) · 2.4 KB
/
Mysql.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import mysql, { RowDataPacket } from "mysql2/promise";
import { ConnectionOptions } from "mysql2";
import { MysqlConnectionParams } from "../../types/integrations/mysql";
import { decryptDataSourceParams } from "../services/datasource";
import { FormatDialect } from "../util/sql";
import SqlIntegration from "./SqlIntegration";
export default class Mysql extends SqlIntegration {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
params: MysqlConnectionParams;
requiresDatabase = false;
requiresSchema = false;
setParams(encryptedParams: string) {
this.params = decryptDataSourceParams<MysqlConnectionParams>(
encryptedParams
);
}
getFormatDialect(): FormatDialect {
return "mysql";
}
getSensitiveParamKeys(): string[] {
return ["password"];
}
async runQuery(sql: string) {
const config: ConnectionOptions = {
host: this.params.host,
port: this.params.port,
user: this.params.user,
password: this.params.password,
database: this.params.database,
};
if (this.params.ssl) {
config["ssl"] = {
ca: this.params.caCert,
cert: this.params.clientCert,
key: this.params.clientKey,
};
}
const conn = await mysql.createConnection(config);
const [rows] = await conn.query(sql);
return rows as RowDataPacket[];
}
dateDiff(startCol: string, endCol: string) {
return `DATEDIFF(${endCol}, ${startCol})`;
}
stddev(col: string) {
return `STDDEV_SAMP(${col})`;
}
addTime(
col: string,
unit: "hour" | "minute",
sign: "+" | "-",
amount: number
): string {
return `DATE_${
sign === "+" ? "ADD" : "SUB"
}(${col}, INTERVAL ${amount} ${unit.toUpperCase()})`;
}
dateTrunc(col: string) {
return `DATE(${col})`;
}
formatDate(col: string): string {
return `DATE_FORMAT(${col}, "%Y-%m-%d")`;
}
formatDateTimeString(col: string): string {
return `DATE_FORMAT(${col}, "%Y-%m-%d %H:%i:%S")`;
}
castToString(col: string): string {
return `cast(${col} as char)`;
}
ensureFloat(col: string): string {
return `CAST(${col} AS DOUBLE)`;
}
getInformationSchemaWhereClause(): string {
if (!this.params.database)
throw new Error(
`No database name provided in MySql connection. Please add a database by editing the connection settings.`
);
return `table_schema IN ('${this.params.database}')`;
}
}