@remix-run/data-table-sqlite 0.0.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,487 @@
1
+ import { getTableName, getTablePrimaryKey } from '@remix-run/data-table'
2
+ import type { DataManipulationOperation, Predicate, SqlStatement } from '@remix-run/data-table'
3
+ import {
4
+ collectColumns as collectColumnsHelper,
5
+ normalizeJoinType as normalizeJoinTypeHelper,
6
+ quotePath as quotePathHelper,
7
+ } from '@remix-run/data-table/sql-helpers'
8
+
9
+ type JoinClause = Extract<DataManipulationOperation, { kind: 'select' }>['joins'][number]
10
+ type UpsertOperation = Extract<DataManipulationOperation, { kind: 'upsert' }>
11
+ type OperationTable = Extract<DataManipulationOperation, { kind: 'select' }>['table']
12
+
13
+ type CompileContext = {
14
+ values: unknown[]
15
+ }
16
+
17
+ export function compileSqliteOperation(operation: DataManipulationOperation): SqlStatement {
18
+ if (operation.kind === 'raw') {
19
+ return {
20
+ text: operation.sql.text,
21
+ values: [...operation.sql.values],
22
+ }
23
+ }
24
+
25
+ let context: CompileContext = { values: [] }
26
+
27
+ if (operation.kind === 'select') {
28
+ let selection = '*'
29
+
30
+ if (operation.select !== '*') {
31
+ selection = operation.select
32
+ .map((field) => quotePath(field.column) + ' as ' + quoteIdentifier(field.alias))
33
+ .join(', ')
34
+ }
35
+
36
+ return {
37
+ text:
38
+ 'select ' +
39
+ (operation.distinct ? 'distinct ' : '') +
40
+ selection +
41
+ compileFromClause(operation.table, operation.joins, context) +
42
+ compileWhereClause(operation.where, context) +
43
+ compileGroupByClause(operation.groupBy) +
44
+ compileHavingClause(operation.having, context) +
45
+ compileOrderByClause(operation.orderBy) +
46
+ compileLimitClause(operation.limit) +
47
+ compileOffsetClause(operation.offset),
48
+ values: context.values,
49
+ }
50
+ }
51
+
52
+ if (operation.kind === 'count' || operation.kind === 'exists') {
53
+ let inner =
54
+ 'select 1' +
55
+ compileFromClause(operation.table, operation.joins, context) +
56
+ compileWhereClause(operation.where, context) +
57
+ compileGroupByClause(operation.groupBy) +
58
+ compileHavingClause(operation.having, context)
59
+
60
+ return {
61
+ text:
62
+ 'select count(*) as ' +
63
+ quoteIdentifier('count') +
64
+ ' from (' +
65
+ inner +
66
+ ') as ' +
67
+ quoteIdentifier('__dt_count'),
68
+ values: context.values,
69
+ }
70
+ }
71
+
72
+ if (operation.kind === 'insert') {
73
+ return compileInsertOperation(operation.table, operation.values, operation.returning, context)
74
+ }
75
+
76
+ if (operation.kind === 'insertMany') {
77
+ return compileInsertManyOperation(
78
+ operation.table,
79
+ operation.values,
80
+ operation.returning,
81
+ context,
82
+ )
83
+ }
84
+
85
+ if (operation.kind === 'update') {
86
+ let columns = Object.keys(operation.changes)
87
+
88
+ return {
89
+ text:
90
+ 'update ' +
91
+ quotePath(getTableName(operation.table)) +
92
+ ' set ' +
93
+ columns
94
+ .map(
95
+ (column) => quotePath(column) + ' = ' + pushValue(context, operation.changes[column]),
96
+ )
97
+ .join(', ') +
98
+ compileWhereClause(operation.where, context) +
99
+ compileReturningClause(operation.returning),
100
+ values: context.values,
101
+ }
102
+ }
103
+
104
+ if (operation.kind === 'delete') {
105
+ return {
106
+ text:
107
+ 'delete from ' +
108
+ quotePath(getTableName(operation.table)) +
109
+ compileWhereClause(operation.where, context) +
110
+ compileReturningClause(operation.returning),
111
+ values: context.values,
112
+ }
113
+ }
114
+
115
+ if (operation.kind === 'upsert') {
116
+ return compileUpsertOperation(operation, context)
117
+ }
118
+
119
+ throw new Error('Unsupported operation kind')
120
+ }
121
+
122
+ function compileInsertOperation(
123
+ table: OperationTable,
124
+ values: Record<string, unknown>,
125
+ returning: '*' | string[] | undefined,
126
+ context: CompileContext,
127
+ ): SqlStatement {
128
+ let columns = Object.keys(values)
129
+
130
+ if (columns.length === 0) {
131
+ return {
132
+ text:
133
+ 'insert into ' +
134
+ quotePath(getTableName(table)) +
135
+ ' default values' +
136
+ compileReturningClause(returning),
137
+ values: context.values,
138
+ }
139
+ }
140
+
141
+ return {
142
+ text:
143
+ 'insert into ' +
144
+ quotePath(getTableName(table)) +
145
+ ' (' +
146
+ columns.map((column) => quotePath(column)).join(', ') +
147
+ ') values (' +
148
+ columns.map((column) => pushValue(context, values[column])).join(', ') +
149
+ ')' +
150
+ compileReturningClause(returning),
151
+ values: context.values,
152
+ }
153
+ }
154
+
155
+ function compileInsertManyOperation(
156
+ table: OperationTable,
157
+ rows: Record<string, unknown>[],
158
+ returning: '*' | string[] | undefined,
159
+ context: CompileContext,
160
+ ): SqlStatement {
161
+ if (rows.length === 0) {
162
+ return {
163
+ text: 'select 0 where 1 = 0',
164
+ values: context.values,
165
+ }
166
+ }
167
+
168
+ let columns = collectColumns(rows)
169
+
170
+ if (columns.length === 0) {
171
+ return {
172
+ text:
173
+ 'insert into ' +
174
+ quotePath(getTableName(table)) +
175
+ ' default values' +
176
+ compileReturningClause(returning),
177
+ values: context.values,
178
+ }
179
+ }
180
+
181
+ return {
182
+ text:
183
+ 'insert into ' +
184
+ quotePath(getTableName(table)) +
185
+ ' (' +
186
+ columns.map((column) => quotePath(column)).join(', ') +
187
+ ') values ' +
188
+ rows
189
+ .map(
190
+ (row) =>
191
+ '(' +
192
+ columns
193
+ .map((column) => {
194
+ let value = Object.prototype.hasOwnProperty.call(row, column) ? row[column] : null
195
+ return pushValue(context, value)
196
+ })
197
+ .join(', ') +
198
+ ')',
199
+ )
200
+ .join(', ') +
201
+ compileReturningClause(returning),
202
+ values: context.values,
203
+ }
204
+ }
205
+
206
+ function compileUpsertOperation(operation: UpsertOperation, context: CompileContext): SqlStatement {
207
+ let insertColumns = Object.keys(operation.values)
208
+ let conflictTarget = operation.conflictTarget ?? [...getTablePrimaryKey(operation.table)]
209
+
210
+ if (insertColumns.length === 0) {
211
+ throw new Error('upsert requires at least one value')
212
+ }
213
+
214
+ let updateValues = operation.update ?? operation.values
215
+ let updateColumns = Object.keys(updateValues)
216
+
217
+ let conflictClause = ''
218
+
219
+ if (updateColumns.length === 0) {
220
+ conflictClause =
221
+ ' on conflict (' +
222
+ conflictTarget.map((column: string) => quotePath(column)).join(', ') +
223
+ ') do nothing'
224
+ } else {
225
+ conflictClause =
226
+ ' on conflict (' +
227
+ conflictTarget.map((column: string) => quotePath(column)).join(', ') +
228
+ ') do update set ' +
229
+ updateColumns
230
+ .map((column) => quotePath(column) + ' = ' + pushValue(context, updateValues[column]))
231
+ .join(', ')
232
+ }
233
+
234
+ return {
235
+ text:
236
+ 'insert into ' +
237
+ quotePath(getTableName(operation.table)) +
238
+ ' (' +
239
+ insertColumns.map((column) => quotePath(column)).join(', ') +
240
+ ') values (' +
241
+ insertColumns.map((column) => pushValue(context, operation.values[column])).join(', ') +
242
+ ')' +
243
+ conflictClause +
244
+ compileReturningClause(operation.returning),
245
+ values: context.values,
246
+ }
247
+ }
248
+
249
+ function compileFromClause(
250
+ table: OperationTable,
251
+ joins: JoinClause[],
252
+ context: CompileContext,
253
+ ): string {
254
+ let output = ' from ' + quotePath(getTableName(table))
255
+
256
+ for (let join of joins) {
257
+ output +=
258
+ ' ' +
259
+ normalizeJoinType(join.type) +
260
+ ' join ' +
261
+ quotePath(getTableName(join.table)) +
262
+ ' on ' +
263
+ compilePredicate(join.on, context)
264
+ }
265
+
266
+ return output
267
+ }
268
+
269
+ function compileWhereClause(predicates: Predicate[], context: CompileContext): string {
270
+ if (predicates.length === 0) {
271
+ return ''
272
+ }
273
+
274
+ return (
275
+ ' where ' +
276
+ predicates.map((predicate) => '(' + compilePredicate(predicate, context) + ')').join(' and ')
277
+ )
278
+ }
279
+
280
+ function compileGroupByClause(columns: string[]): string {
281
+ if (columns.length === 0) {
282
+ return ''
283
+ }
284
+
285
+ return ' group by ' + columns.map((column) => quotePath(column)).join(', ')
286
+ }
287
+
288
+ function compileHavingClause(predicates: Predicate[], context: CompileContext): string {
289
+ if (predicates.length === 0) {
290
+ return ''
291
+ }
292
+
293
+ return (
294
+ ' having ' +
295
+ predicates.map((predicate) => '(' + compilePredicate(predicate, context) + ')').join(' and ')
296
+ )
297
+ }
298
+
299
+ function compileOrderByClause(orderBy: { column: string; direction: 'asc' | 'desc' }[]): string {
300
+ if (orderBy.length === 0) {
301
+ return ''
302
+ }
303
+
304
+ return (
305
+ ' order by ' +
306
+ orderBy
307
+ .map((clause) => quotePath(clause.column) + ' ' + clause.direction.toUpperCase())
308
+ .join(', ')
309
+ )
310
+ }
311
+
312
+ function compileLimitClause(limit: number | undefined): string {
313
+ if (limit === undefined) {
314
+ return ''
315
+ }
316
+
317
+ return ' limit ' + String(limit)
318
+ }
319
+
320
+ function compileOffsetClause(offset: number | undefined): string {
321
+ if (offset === undefined) {
322
+ return ''
323
+ }
324
+
325
+ return ' offset ' + String(offset)
326
+ }
327
+
328
+ function compileReturningClause(returning: '*' | string[] | undefined): string {
329
+ if (!returning) {
330
+ return ''
331
+ }
332
+
333
+ if (returning === '*') {
334
+ return ' returning *'
335
+ }
336
+
337
+ return ' returning ' + returning.map((column) => quotePath(column)).join(', ')
338
+ }
339
+
340
+ function compilePredicate(predicate: Predicate, context: CompileContext): string {
341
+ if (predicate.type === 'comparison') {
342
+ let column = quotePath(predicate.column)
343
+
344
+ if (predicate.operator === 'eq') {
345
+ if (
346
+ predicate.valueType === 'value' &&
347
+ (predicate.value === null || predicate.value === undefined)
348
+ ) {
349
+ return column + ' is null'
350
+ }
351
+
352
+ let comparisonValue = compileComparisonValue(predicate, context)
353
+ return column + ' = ' + comparisonValue
354
+ }
355
+
356
+ if (predicate.operator === 'ne') {
357
+ if (
358
+ predicate.valueType === 'value' &&
359
+ (predicate.value === null || predicate.value === undefined)
360
+ ) {
361
+ return column + ' is not null'
362
+ }
363
+
364
+ let comparisonValue = compileComparisonValue(predicate, context)
365
+ return column + ' <> ' + comparisonValue
366
+ }
367
+
368
+ if (predicate.operator === 'gt') {
369
+ let comparisonValue = compileComparisonValue(predicate, context)
370
+ return column + ' > ' + comparisonValue
371
+ }
372
+
373
+ if (predicate.operator === 'gte') {
374
+ let comparisonValue = compileComparisonValue(predicate, context)
375
+ return column + ' >= ' + comparisonValue
376
+ }
377
+
378
+ if (predicate.operator === 'lt') {
379
+ let comparisonValue = compileComparisonValue(predicate, context)
380
+ return column + ' < ' + comparisonValue
381
+ }
382
+
383
+ if (predicate.operator === 'lte') {
384
+ let comparisonValue = compileComparisonValue(predicate, context)
385
+ return column + ' <= ' + comparisonValue
386
+ }
387
+
388
+ if (predicate.operator === 'in' || predicate.operator === 'notIn') {
389
+ let values = Array.isArray(predicate.value) ? predicate.value : []
390
+
391
+ if (values.length === 0) {
392
+ return predicate.operator === 'in' ? '1 = 0' : '1 = 1'
393
+ }
394
+
395
+ let keyword = predicate.operator === 'in' ? 'in' : 'not in'
396
+
397
+ return (
398
+ column +
399
+ ' ' +
400
+ keyword +
401
+ ' (' +
402
+ values.map((value) => pushValue(context, value)).join(', ') +
403
+ ')'
404
+ )
405
+ }
406
+
407
+ if (predicate.operator === 'like') {
408
+ let comparisonValue = compileComparisonValue(predicate, context)
409
+ return column + ' like ' + comparisonValue
410
+ }
411
+
412
+ if (predicate.operator === 'ilike') {
413
+ let comparisonValue = compileComparisonValue(predicate, context)
414
+ return 'lower(' + column + ') like lower(' + comparisonValue + ')'
415
+ }
416
+ }
417
+
418
+ if (predicate.type === 'between') {
419
+ return (
420
+ quotePath(predicate.column) +
421
+ ' between ' +
422
+ pushValue(context, predicate.lower) +
423
+ ' and ' +
424
+ pushValue(context, predicate.upper)
425
+ )
426
+ }
427
+
428
+ if (predicate.type === 'null') {
429
+ return (
430
+ quotePath(predicate.column) + (predicate.operator === 'isNull' ? ' is null' : ' is not null')
431
+ )
432
+ }
433
+
434
+ if (predicate.type === 'logical') {
435
+ if (predicate.predicates.length === 0) {
436
+ return predicate.operator === 'and' ? '1 = 1' : '1 = 0'
437
+ }
438
+
439
+ let joiner = predicate.operator === 'and' ? ' and ' : ' or '
440
+
441
+ return predicate.predicates
442
+ .map((child) => '(' + compilePredicate(child, context) + ')')
443
+ .join(joiner)
444
+ }
445
+
446
+ throw new Error('Unsupported predicate')
447
+ }
448
+
449
+ function compileComparisonValue(
450
+ predicate: Extract<Predicate, { type: 'comparison' }>,
451
+ context: CompileContext,
452
+ ): string {
453
+ if (predicate.valueType === 'column') {
454
+ return quotePath(predicate.value)
455
+ }
456
+
457
+ return pushValue(context, predicate.value)
458
+ }
459
+
460
+ function normalizeJoinType(type: string): string {
461
+ return normalizeJoinTypeHelper(type)
462
+ }
463
+
464
+ function quoteIdentifier(value: string): string {
465
+ return '"' + value.replace(/"/g, '""') + '"'
466
+ }
467
+
468
+ function quotePath(path: string): string {
469
+ return quotePathHelper(path, quoteIdentifier)
470
+ }
471
+
472
+ function pushValue(context: CompileContext, value: unknown): string {
473
+ context.values.push(normalizeBoundValue(value))
474
+ return '?'
475
+ }
476
+
477
+ function normalizeBoundValue(value: unknown): unknown {
478
+ if (typeof value === 'boolean') {
479
+ return value ? 1 : 0
480
+ }
481
+
482
+ return value
483
+ }
484
+
485
+ function collectColumns(rows: Record<string, unknown>[]): string[] {
486
+ return collectColumnsHelper(rows)
487
+ }