1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | exports.MysqlDialect = void 0;
|
12 |
|
13 | const Raw_1 = require("../Database/StaticBuilder/Raw");
|
14 | class MysqlDialect {
|
15 | constructor(client) {
|
16 | Object.defineProperty(this, "client", {
|
17 | enumerable: true,
|
18 | configurable: true,
|
19 | writable: true,
|
20 | value: client
|
21 | });
|
22 | Object.defineProperty(this, "name", {
|
23 | enumerable: true,
|
24 | configurable: true,
|
25 | writable: true,
|
26 | value: 'mysql'
|
27 | });
|
28 | Object.defineProperty(this, "supportsAdvisoryLocks", {
|
29 | enumerable: true,
|
30 | configurable: true,
|
31 | writable: true,
|
32 | value: true
|
33 | });
|
34 | |
35 |
|
36 |
|
37 |
|
38 | Object.defineProperty(this, "version", {
|
39 | enumerable: true,
|
40 | configurable: true,
|
41 | writable: true,
|
42 | value: this.client.getReadClient()['context']['client'].version
|
43 | });
|
44 | |
45 |
|
46 |
|
47 |
|
48 | Object.defineProperty(this, "dateTimeFormat", {
|
49 | enumerable: true,
|
50 | configurable: true,
|
51 | writable: true,
|
52 | value: 'yyyy-MM-dd HH:mm:ss'
|
53 | });
|
54 | }
|
55 | |
56 |
|
57 |
|
58 | async truncate(table, cascade = false) {
|
59 | if (!cascade) {
|
60 | return this.client.knexQuery().table(table).truncate();
|
61 | }
|
62 | |
63 |
|
64 |
|
65 | const trx = await this.client.transaction();
|
66 | try {
|
67 | await trx.rawQuery('SET FOREIGN_KEY_CHECKS=0;');
|
68 | await trx.knexQuery().table(table).truncate();
|
69 | await trx.rawQuery('SET FOREIGN_KEY_CHECKS=1;');
|
70 | await trx.commit();
|
71 | }
|
72 | catch (error) {
|
73 | await trx.rollback();
|
74 | throw error;
|
75 | }
|
76 | }
|
77 | |
78 |
|
79 |
|
80 | async getAllTables() {
|
81 | const tables = await this.client
|
82 | .query()
|
83 | .from('information_schema.tables')
|
84 | .select('table_name as table_name')
|
85 | .where('TABLE_TYPE', 'BASE TABLE')
|
86 | .where('table_schema', new Raw_1.RawBuilder('database()'))
|
87 | .orderBy('table_name', 'asc');
|
88 | return tables.map(({ table_name }) => table_name);
|
89 | }
|
90 | |
91 |
|
92 |
|
93 | async dropAllTables() {
|
94 | const tables = await this.getAllTables();
|
95 | |
96 |
|
97 |
|
98 | const trx = await this.client.transaction();
|
99 | try {
|
100 | await trx.rawQuery('SET FOREIGN_KEY_CHECKS=0;');
|
101 | await trx.rawQuery(`DROP table ${tables.join(',')};`);
|
102 | await trx.rawQuery('SET FOREIGN_KEY_CHECKS=1;');
|
103 | await trx.commit();
|
104 | }
|
105 | catch (error) {
|
106 | await trx.rollback();
|
107 | throw error;
|
108 | }
|
109 | }
|
110 | |
111 |
|
112 |
|
113 |
|
114 | async getAdvisoryLock(key, timeout = 0) {
|
115 | const response = await this.client.rawQuery(`SELECT GET_LOCK('${key}', ${timeout}) as lock_status;`);
|
116 | return response[0] && response[0][0] && response[0][0].lock_status === 1;
|
117 | }
|
118 | |
119 |
|
120 |
|
121 | async releaseAdvisoryLock(key) {
|
122 | const response = await this.client.rawQuery(`SELECT RELEASE_LOCK('${key}') as lock_status;`);
|
123 | return response[0] && response[0][0] && response[0][0].lock_status === 1;
|
124 | }
|
125 | }
|
126 | exports.MysqlDialect = MysqlDialect;
|