-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite3.ts
More file actions
209 lines (199 loc) · 5.06 KB
/
sqlite3.ts
File metadata and controls
209 lines (199 loc) · 5.06 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import * as Joi from "joi";
import * as cnst from ".././const";
// defintions based on sqlite 3 datatypes
// https://www.sqlite.org/datatype3.html
export const joiBase = (type: string) => {
switch (type) {
case "int":
case "integer":
case "tinyint":
case "smallint":
case "mediumint":
case "bigint":
case "unsigned big int":
case "int2":
case "int8":
return Joi.number().integer();
// character(n) -- ignore. use default
// varchar(n) -- ignore. use default
// varying character(n) -- ignore. use default
// nchar(n) -- ignore. use default
// native character(n) -- ignore. use default
// nvarchar(n) -- ignore. use default
case "text":
case "clob":
return Joi.string();
case "blob":
case "":
return Joi.string();
case "real":
case "double":
case "double precision":
case "float":
return Joi.number();
case "numeric":
// decimal(n1, n2) -- check for starts with
case "boolean":
case "date":
case "datetime":
return Joi.number();
default:
const match = type.match(cnst.REGEX_CHAR);
if (match) {
return Joi.string().max(Number(match.groups.len));
}
if (type.startsWith("decimal")) {
return Joi.number();
}
return Joi.string();
}
};
export const toSchemaScalar = (type: string) => {
switch (type) {
case "int":
case "integer":
case "tinyint":
case "smallint":
case "mediumint":
case "bigint":
case "unsigned big int":
case "int2":
case "int8":
return "Float";
// character(n) -- ignore. use default
// varchar(n) -- ignore. use default
// varying character(n) -- ignore. use default
// nchar(n) -- ignore. use default
// native character(n) -- ignore. use default
// nvarchar(n) -- ignore. use default
case "text":
case "clob":
return "String";
case "blob":
case "":
return "String";
case "real":
case "double":
case "double precision":
case "float":
return "Float";
case "numeric":
// decimal(n1, n2) -- check for starts with
case "boolean":
case "date":
case "datetime":
return "Float";
default:
// this doesn't matter here as the schema cannot enforce
// const match = type.match(cnst.REGEX_CHAR);
// if (match) {
// return Joi.string().length(Number(match.groups.len));
// }
if (type.startsWith("decimal")) {
return "Float";
}
return "String";
}
};
export const toProtoScalar = (type: string) => {
switch (type) {
case "int":
case "integer":
case "tinyint":
case "smallint":
case "mediumint":
case "bigint":
case "unsigned big int":
case "int2":
case "int8":
return "uint32";
// character(n) -- ignore. use default
// varchar(n) -- ignore. use default
// varying character(n) -- ignore. use default
// nchar(n) -- ignore. use default
// native character(n) -- ignore. use default
// nvarchar(n) -- ignore. use default
case "text":
case "clob":
return "string";
case "blob":
case "":
return "string";
case "real":
case "double":
case "double precision":
case "float":
return "uint32";
case "numeric":
// decimal(n1, n2) -- check for starts with
case "boolean":
case "date":
case "datetime":
return "uint32";
default:
// this doesn't matter here as the schema cannot enforce
// const match = type.match(cnst.REGEX_CHAR);
// if (match) {
// return Joi.string().length(Number(match.groups.len));
// }
if (type.startsWith("decimal")) {
return "uint32";
}
return "string";
}
};
export const dialect = ({ migrationTable }) => {
const dbSurveyQuery = `
SELECT
'' resource_schema,
m.type resource_type,
m.name resource_name,
p.cid resource_column_id,
p.name resource_column_name,
p."notnull" "notnull",
lower(p.type) type,
p.pk primarykey,
CASE unique_idx.origin
WHEN 'u'
THEN 1
ELSE 0
END uniquekey
FROM
sqlite_master AS m
JOIN
pragma_table_info(m.name) AS p
left join (
SELECT
m.name resource_name,
ii.name resource_column_name,
il."unique" uniquekey,
il.origin
FROM
sqlite_master AS m,
pragma_index_list(m.name) AS il,
pragma_index_info(il.name) AS ii
WHERE
m.type = 'table'
and il.origin = 'u'
and il."unique" = 1
) unique_idx
on
m.name = unique_idx.resource_name
and
p.name =unique_idx.resource_column_name
where
m.type in ('table', 'view')
and m.name not in ('${migrationTable}', '${migrationTable}_lock')
ORDER BY
m.name,
p.name;
`;
const versionQuery = `select sqlite_version() as db_version;`;
return {
dbSurveyQuery,
versionQuery,
joiBase,
toSchemaScalar,
toProtoScalar,
};
};