Repository navigation
Expand file tree
/
Copy pathdialect.ts
More file actions
88 lines (83 loc) · 3.21 KB
/
Copy pathdialect.ts
File metadata and controls
88 lines (83 loc) · 3.21 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
import { DateTime } from "effect"
import { noTransactions, type Dialect } from "../ch/dialect"
import { QueryBuilderError } from "../ch/errors"
import { fail } from "../ch/failure"
import { PARAM_MARKER_PREFIX } from "../ch/param"
import { toSeconds } from "../sql/text-timestamp"
import { SqliteTimestampLiteral, sqliteTimestampLiteral, sqliteTimestampText } from "./timestamp"
const hex = (value: Uint8Array): string =>
Array.from(value, (byte) => byte.toString(16).padStart(2, "0")).join("")
/**
* Hex text keeps the compiler's reserved param marker out of SQL text. U+0000
* is refused: SQLite stores it, but its drivers end text there, so the value
* would read back cut short.
*/
const quoteString = (value: string): string =>
value.includes("\0")
? fail(new QueryBuilderError({ code: "InvalidLiteral", message: "a string literal contains U+0000, which SQLite drivers end text at; the value would read back cut short" }), "''")
: value.includes(PARAM_MARKER_PREFIX)
? `CAST(X'${hex(new TextEncoder().encode(value))}' AS TEXT)`
: `'${value.replace(/'/g, "''")}'`
const literal = (value: unknown, context: string): string => {
if (value === null) return "NULL"
if (typeof value === "string") return quoteString(value)
if (typeof value === "boolean") return value ? "1" : "0"
if (typeof value === "number" && Number.isFinite(value)) return String(value)
if (value instanceof Uint8Array) return `X'${hex(value)}'`
return fail(new QueryBuilderError({ code: "InvalidLiteral", message: `${context}: cannot write this value as a SQLite literal` }), "NULL")
}
/** SQLite with positional bindings and transactions supplied by the caller's SqlClient. */
export const sqliteDialect: Dialect<"sqlite"> = {
name: "sqlite",
functions: "sqlite",
ilike: false,
likeEscape: true,
likeCase: "ascii-insensitive",
inexactNumberText: true,
fractionComparableIntegers: ["integer"],
quoteIdent: (name) => `"${name.replace(/"/g, '""')}"`,
quoteString,
literal,
dateTimeLiteral: (value) => {
try {
return quoteString(sqliteTimestampText(DateTime.toEpochMillis(value)))
} catch (error) {
return fail(new QueryBuilderError({ code: "InvalidLiteral", message: (error as Error).message }), "NULL")
}
},
params: { _tag: "bind", placeholder: () => "?", reuse: false, maxParameters: 32766, nulInText: false },
clauses: {
format: false,
derivedTableAlias: true,
groupByAlias: false,
parenthesizeUnionBranches: false,
wrapUnionBranches: true,
limitForOffset: "-1",
returning: true,
onConflict: true,
onConflictConstraint: false,
insertSelectNeedsWhere: true,
defaultInValues: false,
distinctOn: false,
updateFrom: true,
},
paramCodecs: {
dateTime: SqliteTimestampLiteral,
dateTimeSeconds: sqliteTimestampLiteral(toSeconds),
},
transactions: {
support: "full",
savepoints: true,
isolationLevels: ["serializable"],
accessModes: false,
deferrable: false,
setTransaction: () => undefined,
},
}
/** D1 executes SQLite SQL, limits each statement to 100 bindings, and has no interactive transactions. */
export const d1Dialect: Dialect<"sqlite"> = {
...sqliteDialect,
name: "d1",
params: { ...sqliteDialect.params, _tag: "bind", placeholder: () => "?", reuse: false, maxParameters: 100 },
transactions: noTransactions,
}