-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
184 lines (157 loc) · 4.35 KB
/
index.ts
File metadata and controls
184 lines (157 loc) · 4.35 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import sql from 'sql-template-tag'
/**
* Abstraction over database access: How to execute a query and
* produce the result as an array of rows.
*/
export type Storage = (query: Query) => Array<Row>
export type Query<A = any> = { sql: string, values: unknown[] }
export type Row = Record<string, Value>
export type Value = string | number | null
function exec<A>(storage: Storage, query: Query<A>): Array<A> {
return storage(query) as Array<A>
}
/**
* A best effort description of the functionally relevant aspects of the
* entire database structure. This is intended for use in a testing to
* determine whether two databases' structures are effectively equivalent.
*/
export function getStructure(storage: Storage): Structure {
return exec(storage, pragmaTableListQuery).map((table) => {
const { schema } = table
const indexes: Array<Index> =
exec(storage, pragmaIndexListQuery(table)).map((index) => {
const columns = exec(storage, pragmaIndexInfoQuery({ name: index.name, schema }))
return {
name: index.name,
unique: !!index.unique,
origin: index.origin,
partial: !!index.partial,
columns,
} satisfies Index
})
const columns: Array<Column> =
exec(storage, pragmaTableInfoQuery(table)).map((column) => ({
name: column.name,
type: column.type,
notNull: !!column.notnull,
defaultValue: column.dflt_value,
primaryKey: column.pk,
} satisfies Column))
return {
schema,
name: table.name,
type: table.type,
withoutRowid: !!table.wr,
strict: !!table.strict,
columns,
indexes,
} satisfies Table
})
}
export type Structure = Array<Table>
export type Table = {
schema: string
name: string
type: TableType
// https://sqlite.org/withoutrowid.html
withoutRowid: boolean
// https://sqlite.org/stricttables.html
strict: boolean
columns: Array<Column>
indexes: Array<Index>
}
export type TableType =
'table' // https://sqlite.org/lang_createtable.html
| 'view' // https://sqlite.org/lang_createview.html
| 'shadow' // https://sqlite.org/vtab.html#xshadowname
| 'virtual' // https://sqlite.org/lang_createvtab.html
export type Column = {
name: string
type: string
notNull: boolean
defaultValue: string | null
primaryKey: number
}
export type Index = {
name: string
unique: boolean
origin: IndexOrigin
partial: boolean
columns: Array<IndexColumn>
}
export type IndexOrigin =
'c' // https://sqlite.org/lang_createindex.html
| 'u' // https://sqlite.org/lang_createtable.html#uniqueconst
| 'pk' // https://sqlite.org/lang_createtable.html#primkeyconst
export type IndexColumn = PragmaIndexInfo
/**
* https://sqlite.org/pragma.html#pragma_table_list
*/
const pragmaTableListQuery: Query<PragmaTableList> =
sql`
select "schema", "name", "type", "wr", "strict"
from pragma_table_list()
where "schema" not like 'temp' and "name" not like 'sqlite_%'
order by "schema", "name"
`
type PragmaTableList = {
schema: string
name: string
type: 'table' | 'view' | 'shadow' | 'virtual'
// https://sqlite.org/withoutrowid.html
wr: 0 | 1
// https://sqlite.org/stricttables.html
strict: 0 | 1
}
/**
* https://sqlite.org/pragma.html#pragma_table_info
*/
function pragmaTableInfoQuery(
table: { name: string, schema: string })
: Query<PragmaTableInfo> {
return sql`
select "name", "type", "notnull", "dflt_value", "pk"
from pragma_table_info( ${table.name}, ${table.schema} )
order by "name"
`
}
type PragmaTableInfo = {
name: string
type: string
notnull: 0 | 1
dflt_value: string | null
pk: number
}
/**
* https://sqlite.org/pragma.html#pragma_index_list
*/
function pragmaIndexListQuery(
table: { name: string, schema: string }
): Query<PragmaIndexList> {
return sql`
select "name", "unique", "origin", "partial"
from pragma_index_list( ${table.name}, ${table.schema} )
order by "name"
`
}
type PragmaIndexList = {
name: string
unique: 0 | 1
origin: 'c' | 'u' | 'pk'
partial: 0 | 1
}
/**
* https://sqlite.org/pragma.html#pragma_index_info
*/
function pragmaIndexInfoQuery(
index: { name: string, schema: string }
): Query<PragmaIndexInfo> {
return sql`
select "name" from
pragma_index_info( ${index.name}, ${index.schema} )
order by "seqno"
`
}
type PragmaIndexInfo = {
name: string
}