Repository navigation
Expand file tree
/
Copy pathsql-syntax.ts
More file actions
81 lines (76 loc) · 3.92 KB
/
Copy pathsql-syntax.ts
File metadata and controls
81 lines (76 loc) · 3.92 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
// Syntax of the dialect being compiled for.
//
// Identifiers and literals are written while a query's callbacks run, deep
// inside expression code that has no dialect argument to read. `compile`
// installs the dialect's syntax in its fiber for the duration of the compile instead,
// the same way `withSubqueryCompiler` hands down the subquery renderer. A leaf
// module so the fragment renderer can read it without importing the builder.
import { Context, type DateTime } from "effect"
import { fiberLocal } from "./fiber-local"
export interface SqlSyntax {
/** The dialect's name (`clickhouse`, `postgres`, …), for the few choices expression code makes per dialect. */
readonly dialect: string
/** False when the dialect has no ILIKE operator. */
readonly ilike?: boolean
/**
* True when LIKE has no default escape character, so `\` in a pattern is
* literal unless the statement says `ESCAPE '\'` (SQLite). Patterns use `\`
* to escape `%`, `_` and `\` on every dialect.
*/
readonly likeEscape?: boolean
/**
* What decides LIKE's case rules. `sensitive` (the default): `LIKE` is
* case-sensitive. `ascii-insensitive` (SQLite): LIKE folds ASCII, so a
* case-sensitive text match is a GLOB. `collation` (MySQL): LIKE follows a
* collation that may ignore case and accents, so a case-sensitive text match
* compares bytes (`CAST(x AS BINARY) LIKE …`). `contains` and friends write
* `lower()` on both sides for an insensitive match where there is no ILIKE.
*/
readonly likeCase?: "sensitive" | "ascii-insensitive" | "collation"
/**
* True when the database parses some decimal literals to a neighbouring
* double (SQLite), so a fraction must be bound rather than written as text.
*/
readonly inexactNumberText?: boolean
/**
* The integer column types (`integer`, `bigint`, …) that compare with a
* fraction numerically (SQLite's type affinity, MySQL's numeric comparison),
* so `integer > 9.75` writes the fraction instead of refusing it through the
* column's whole-number codec.
*/
readonly fractionComparableIntegers?: ReadonlyArray<string>
/**
* The CAST a value compared with a column of SQL type `sqlType` needs, or
* `undefined` for none: MySQL compares a JSON column with a bound string as
* a JSON string, so the value is `CAST(? AS JSON)`.
*/
readonly comparisonCast?: (sqlType: string) => string | undefined
/** Text concatenation of SQL operands, where it is not `||` (MySQL, whose `||` is OR). */
readonly concat?: (parts: ReadonlyArray<string>) => string
/** One identifier (a column, alias, table or schema name), quoted as needed. */
readonly quoteIdent: (name: string) => string
/**
* True when a table name that is not a dotted path of plain identifiers is
* written as it is given, not quoted: ClickHouse, where an external table's
* name is its FROM target, which may be a table function (`numbers(10)`), a
* subquery, or a name the caller has quoted (`` `my-db`.events ``).
*/
readonly rawTableNames?: boolean
/** A string as a quoted literal. */
readonly quoteString: (value: string) => string
/** An encoded wire value (string, number, boolean, null, arrays and records
* of those) as a literal. Throws for a value it cannot write. */
readonly literal: (value: unknown, context: string) => string
/** A point in time compared against an expression with no declared type. */
readonly dateTimeLiteral: (value: DateTime.Utc) => string
}
// Kept in the compiling fiber's context (see `fiber-local.ts`). Save/restore
// makes nested compilations independent, including when a callback throws.
const current = fiberLocal(
Context.Reference<SqlSyntax | undefined>("@maple-dev/effect-orm/internal/SqlSyntax", { defaultValue: () => undefined }),
)
export function withSqlSyntax<A>(syntax: SqlSyntax, body: () => A): A {
return current.with(syntax, body)
}
/** The syntax installed by the enclosing compile, if there is one. */
export const activeSqlSyntax = (): SqlSyntax | undefined => current.get()