-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathindex.ts
More file actions
111 lines (98 loc) · 3 KB
/
Copy pathindex.ts
File metadata and controls
111 lines (98 loc) · 3 KB
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
import type {
CompiledQuery,
Dialect,
Driver,
LimitNode,
OffsetNode,
OperationNode,
QueryResult,
SelectQueryNode,
} from 'kysely'
import type { connections } from '~/drizzle'
import { MssqlQueryCompiler as DefaultMssqlQueryCompiler, DummyDriver, MssqlAdapter } from 'kysely'
import { logSql } from '../../sql'
function isSelectQueryNode(node: OperationNode): node is SelectQueryNode {
return node.kind === 'SelectQueryNode'
}
class MssqlQueryCompiler extends DefaultMssqlQueryCompiler {
protected override visitOffset(node: OffsetNode) {
if (this.parentNode != null && isSelectQueryNode(this.parentNode) && this.parentNode.limit != null)
return // will be handle when visitLimit
this.append(' OFFSET ')
this.visitNode(node.offset)
this.append(' ROWS ')
}
protected override visitLimit(node: LimitNode): void {
if (this.parentNode != null && isSelectQueryNode(this.parentNode)) {
if (this.parentNode.offset != null) {
this.append(' OFFSET ')
this.visitNode(this.parentNode.offset.offset)
this.append(' ROWS ')
}
else {
this.append(' OFFSET 0 ROWS ')
}
}
this.append(' FETCH NEXT ')
this.visitNode(node.limit)
this.append(' ROWS ONLY ')
}
}
function execute(connection: typeof connections.$inferSelect, compiledQuery: CompiledQuery) {
if (!window.electron) {
throw new Error('Electron is not available')
}
const promise = window.electron.query.mssql({
connectionString: connection.connectionString,
sql: compiledQuery.sql,
values: compiledQuery.parameters as unknown[],
})
logSql(connection, promise, {
sql: compiledQuery.sql,
values: compiledQuery.parameters as unknown[],
})
return promise
}
function createDriver(connection: typeof connections.$inferSelect) {
return {
async init() {},
async acquireConnection() {
return {
executeQuery: async <R>(compiledQuery: CompiledQuery): Promise<QueryResult<R>> => {
const { result } = await execute(connection, compiledQuery)
return {
rows: result as R[],
}
},
streamQuery() {
throw new Error('Not implemented')
},
}
},
async beginTransaction() {},
async commitTransaction() {},
async rollbackTransaction() {},
async releaseConnection() {},
async destroy() {},
} satisfies Driver
}
export function mssqlDialect(connection: typeof connections.$inferSelect) {
return {
createDriver: () => createDriver(connection),
createQueryCompiler: () => new MssqlQueryCompiler(),
createAdapter: () => new MssqlAdapter(),
createIntrospector: () => {
throw new Error('Not implemented')
},
} satisfies Dialect
}
export function mssqlColdDialect() {
return {
createDriver: () => new DummyDriver(),
createQueryCompiler: () => new MssqlQueryCompiler(),
createAdapter: () => new MssqlAdapter(),
createIntrospector: () => {
throw new Error('Not implemented')
},
} satisfies Dialect
}