/
Mssql.ts
122 lines (104 loc) · 3.76 KB
/
Mssql.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* @adonisjs/lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/// <reference path="../../adonis-typings/index.ts" />
import { RawBuilder } from '../Database/StaticBuilder/Raw'
import { DialectContract, MssqlConfig, QueryClientContract } from '@ioc:Adonis/Lucid/Database'
export class MssqlDialect implements DialectContract {
public readonly name = 'mssql'
public readonly supportsAdvisoryLocks = false
public readonly supportsViews = false
public readonly supportsTypes = false
public readonly supportsReturningStatement = true
/**
* Reference to the database version. Knex.js fetches the version after
* the first database query, so it will be set to undefined initially
*/
public readonly version = this.client.getReadClient()['context']['client'].version
/**
* The default format for datetime column. The date formats is
* valid for luxon date parsing library
*/
public readonly dateTimeFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZ"
constructor(private client: QueryClientContract, private config: MssqlConfig) {}
/**
* Returns an array of table names
*/
public async getAllTables() {
const tables = await this.client
.query()
.from('information_schema.tables')
.select('table_name as table_name')
.where('table_type', 'BASE TABLE')
.where('table_catalog', new RawBuilder('DB_NAME()'))
.whereNot('table_name', 'like', 'spt_%')
.andWhereNot('table_name', 'MSreplication_options')
.orderBy('table_name', 'asc')
return tables.map(({ table_name }) => table_name)
}
/**
* Truncate mssql table. Disabling foreign key constriants alone is
* not enough for SQL server.
*
* One has to drop all FK constraints and then re-create them, and
* this all is too much work
*/
public async truncate(table: string, _: boolean) {
return this.client.knexQuery().table(table).truncate()
}
/**
* Drop all tables inside the database
*/
public async dropAllTables() {
await this.client.rawQuery(`
DECLARE @sql NVARCHAR(MAX) = N'';
SELECT @sql += 'ALTER TABLE '
+ QUOTENAME(OBJECT_SCHEMA_NAME(parent_object_id)) + '.' + + QUOTENAME(OBJECT_NAME(parent_object_id))
+ ' DROP CONSTRAINT ' + QUOTENAME(name) + ';'
FROM sys.foreign_keys;
EXEC sp_executesql @sql;
`)
const ignoredTables = (this.config.wipe?.ignoreTables || [])
.map((table) => `"${table}"`)
.join(', ')
await this.client.rawQuery(`
EXEC sp_MSforeachtable 'DROP TABLE \\?',
@whereand='AND o.Name NOT IN (${ignoredTables || '""'})'
`)
}
public async getAllViews(): Promise<string[]> {
throw new Error(
'"getAllViews" method not implemented is not implemented for mssql. Create a PR to add the feature'
)
}
public async getAllTypes(): Promise<string[]> {
throw new Error(
'"getAllTypes" method not implemented is not implemented for mssql. Create a PR to add the feature'
)
}
public async dropAllViews(): Promise<void> {
throw new Error(
'"dropAllViews" method not implemented is not implemented for mssql. Create a PR to add the feature'
)
}
public async dropAllTypes(): Promise<void> {
throw new Error(
'"dropAllTypes" method not implemented is not implemented for mssql. Create a PR to add the feature'
)
}
public getAdvisoryLock(): Promise<boolean> {
throw new Error(
'Support for advisory locks is not implemented for mssql. Create a PR to add the feature'
)
}
public releaseAdvisoryLock(): Promise<boolean> {
throw new Error(
'Support for advisory locks is not implemented for mssql. Create a PR to add the feature'
)
}
}