@happyvertical/sql 0.112.1 → 0.112.3
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.
- package/AGENT.md +65 -3
- package/README.md +146 -19
- package/dist/aggregate-expression.d.ts +77 -3
- package/dist/aggregate-expression.d.ts.map +1 -1
- package/dist/aggregate.d.ts +3 -2
- package/dist/aggregate.d.ts.map +1 -1
- package/dist/chunks/{duckdb-CpeXdf0i.js → duckdb-DCV4JFWK.js} +23 -52
- package/dist/chunks/duckdb-DCV4JFWK.js.map +1 -0
- package/dist/chunks/{postgres-dialect-DIy5f5YR.js → postgres-dialect-CaNhva-C.js} +28 -328
- package/dist/chunks/postgres-dialect-CaNhva-C.js.map +1 -0
- package/dist/chunks/query-Dz0TZ0pY.js +970 -0
- package/dist/chunks/query-Dz0TZ0pY.js.map +1 -0
- package/dist/chunks/{schema-manager-tfASrb_b.js → schema-manager-psLzQ2yh.js} +471 -18
- package/dist/chunks/schema-manager-psLzQ2yh.js.map +1 -0
- package/dist/chunks/{sqlite-connection-cache-C_7rVisQ.js → sqlite-connection-cache-BycfrYfi.js} +2 -2
- package/dist/chunks/{sqlite-connection-cache-C_7rVisQ.js.map → sqlite-connection-cache-BycfrYfi.js.map} +1 -1
- package/dist/chunks/{sqlite-BdM-nAQD.js → sqlite-nNhv0FMq.js} +22 -22
- package/dist/chunks/sqlite-nNhv0FMq.js.map +1 -0
- package/dist/chunks/{sqlite-native-DngobJtv.js → sqlite-native-CZGNm9zh.js} +40 -37
- package/dist/chunks/sqlite-native-CZGNm9zh.js.map +1 -0
- package/dist/doctor.d.ts.map +1 -1
- package/dist/duckdb.d.ts.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +44 -77
- package/dist/index.js.map +1 -1
- package/dist/json.d.ts.map +1 -1
- package/dist/pglite.js +2 -2
- package/dist/query.d.ts +3 -0
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +3 -3
- package/dist/schema-manager.d.ts.map +1 -1
- package/dist/shared/alter-utils.d.ts +43 -7
- package/dist/shared/alter-utils.d.ts.map +1 -1
- package/dist/shared/duckdb-schema-utils.d.ts.map +1 -1
- package/dist/shared/postgres-dialect.d.ts.map +1 -1
- package/dist/shared/types.d.ts +1 -0
- package/dist/shared/types.d.ts.map +1 -1
- package/dist/shared/utils.d.ts +12 -1
- package/dist/shared/utils.d.ts.map +1 -1
- package/dist/sqlite-native.d.ts.map +1 -1
- package/dist/sqlite.d.ts.map +1 -1
- package/metadata.json +4 -4
- package/package.json +4 -4
- package/dist/chunks/duckdb-CpeXdf0i.js.map +0 -1
- package/dist/chunks/postgres-dialect-DIy5f5YR.js.map +0 -1
- package/dist/chunks/query-i6IIkNhF.js +0 -564
- package/dist/chunks/query-i6IIkNhF.js.map +0 -1
- package/dist/chunks/schema-manager-tfASrb_b.js.map +0 -1
- package/dist/chunks/sqlite-BdM-nAQD.js.map +0 -1
- package/dist/chunks/sqlite-native-DngobJtv.js.map +0 -1
package/AGENT.md
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
<!-- BEGIN AGENT:GENERATED -->
|
|
4
4
|
## Purpose
|
|
5
|
-
Database interface
|
|
5
|
+
Database interface and portable expressions for SQLite, PostgreSQL, and DuckDB
|
|
6
6
|
|
|
7
7
|
## Package Map
|
|
8
8
|
- Package: `@happyvertical/sql`
|
|
9
9
|
- Hierarchy path: `@happyvertical/sdk > packages > sql`
|
|
10
|
-
- Workspace position: `
|
|
10
|
+
- Workspace position: `30 of 34` local packages
|
|
11
11
|
- Internal dependencies: `@happyvertical/utils`
|
|
12
12
|
- Internal dependents: `@happyvertical/jobs`, `@happyvertical/secrets`
|
|
13
13
|
- Knowledge graph files: `AGENT.md`, `metadata.json`, `ecosystem-manifest.json`
|
|
@@ -26,8 +26,70 @@ pnpm --filter @happyvertical/sql clean
|
|
|
26
26
|
- If failures span multiple packages or Turborepo ordering looks wrong, run `pnpm build` and `pnpm typecheck` from the repo root before retrying package-scoped commands.
|
|
27
27
|
|
|
28
28
|
## Ecosystem Relationships
|
|
29
|
-
- Provides: Database interface
|
|
29
|
+
- Provides: Database interface and portable expressions for SQLite, PostgreSQL, and DuckDB
|
|
30
30
|
- Implements: none
|
|
31
31
|
- Requires: @happyvertical/utils, @duckdb/node-api, @electric-sql/pglite, @libsql/client, @russellthehippo/honker-node, @sqliteai/sqlite-vector, pg, sqlite-vss
|
|
32
32
|
- Stability: stable (Primary package surface is described as implemented and production-oriented.)
|
|
33
33
|
<!-- END AGENT:GENERATED -->
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
## Portable scalar expressions
|
|
37
|
+
|
|
38
|
+
Import `renderExpression` and `ExpressionError` from `@happyvertical/sql` or the
|
|
39
|
+
browser-safe `@happyvertical/sql/query`. Render with
|
|
40
|
+
`renderExpression(node, { dialect, mode: 'ddl' | 'query', resolveColumn })`;
|
|
41
|
+
the result contains `sql`, ordered `values`, `resultType`, and per-dialect
|
|
42
|
+
`immutable` flags. Query mode binds string/date values; DDL mode safely inlines
|
|
43
|
+
literals and requires immutability on every supported dialect. The aggregate
|
|
44
|
+
builder uses the same renderer while preserving its existing SQL policy.
|
|
45
|
+
|
|
46
|
+
Persist dates as `{ value: { kind: 'date' | 'datetime', iso } }`. Datetimes need
|
|
47
|
+
an explicit ISO UTC offset and are canonicalized to UTC before timezone-free
|
|
48
|
+
SQL casts. For generated temporal expressions, resolve columns to
|
|
49
|
+
`{ name, type: 'date' | 'datetime' }` only when their schema uses native DATE or
|
|
50
|
+
TIMESTAMP WITHOUT TIME ZONE with UTC wall-clock fields (ISO text on SQLite).
|
|
51
|
+
Unknown or timezone-aware temporal inputs, legacy JavaScript Date literals,
|
|
52
|
+
and session-dependent casts fail generated use with
|
|
53
|
+
`ExpressionError.code === 'NON_IMMUTABLE'`.
|
|
54
|
+
|
|
55
|
+
See [README: portable scalar expressions](README.md#portable-scalar-expressions-and-generated-columns)
|
|
56
|
+
for the new conditional, numeric, text and date node shapes, NULL behavior,
|
|
57
|
+
argument bounds, ASCII case-conversion contract and DuckDB index limitations.
|
|
58
|
+
|
|
59
|
+
## Column identifier quoting
|
|
60
|
+
|
|
61
|
+
CRUD helpers quote columns on every adapter. SQLite emits backticks so absent
|
|
62
|
+
columns fail even when the driver accepts double-quoted string literals.
|
|
63
|
+
PostgreSQL/PGlite bare names keep
|
|
64
|
+
legacy folding (`camelCase` addresses `camelcase`); explicitly quoted names
|
|
65
|
+
such as `'"MixedCase"'` and `'"Full Name"'` preserve physical spelling. WHERE
|
|
66
|
+
and aggregate identifiers support qualified parts (`alias.order`), with dots
|
|
67
|
+
inside delimiters treated literally (`'"foo.bar"'`).
|
|
68
|
+
|
|
69
|
+
Public `buildWhere`, `buildAggregate` and `bucketExpr` preserve ordinary SQL
|
|
70
|
+
text by default, quoting keywords and explicit names. Pass
|
|
71
|
+
`{ quoteIdentifiers: true }` as the fourth argument for quote-all output;
|
|
72
|
+
adapter CRUD uses that policy automatically. `raw()` remains caller-authored
|
|
73
|
+
WHERE SQL. Standalone `renderExpression` retains exact physical-name semantics.
|
|
74
|
+
|
|
75
|
+
DuckDB/JSON `upsert` record/conflict keys retain their existing literal physical
|
|
76
|
+
name contract: spaces, dots, hyphens and quote characters are data, including
|
|
77
|
+
outer quote characters. Strict insert/update and WHERE keys use explicit SQL
|
|
78
|
+
delimiters for these names. See README and `TEST-DESIGN-identifiers.md` for the
|
|
79
|
+
compatibility exceptions, dialect matrix and complete identifier sink audit.
|
|
80
|
+
|
|
81
|
+
Shared index SQL helpers and schema manifests accept trusted SQL terms such as
|
|
82
|
+
`lower(email)` and `created_at DESC` verbatim. Adapter `alterTable.addIndex` uses
|
|
83
|
+
the branch-base plain-name grammar and error; keyword names then quote safely.
|
|
84
|
+
Aggregate ORDER BY retains only the identifier and optional direction, ignoring
|
|
85
|
+
later tokens. Legacy numeric SQL tokens keep literal/ordinal meaning in WHERE
|
|
86
|
+
and aggregates; explicitly quoted numeric names address physical columns.
|
|
87
|
+
Manifest foreign key column strings support comma-separated identifiers, with
|
|
88
|
+
commas inside explicit quotes kept as part of the physical name.
|
|
89
|
+
|
|
90
|
+
DDL/schema identifier names also normalize legacy SQLite backtick, bracket and
|
|
91
|
+
single-quote delimiters. FK list parsing keeps commas inside those names literal;
|
|
92
|
+
malformed names and expressions still reject.
|
|
93
|
+
|
|
94
|
+
Aggregate numeric compatibility applies to operands/ordinals, while SELECT aliases,
|
|
95
|
+
FROM/JOIN names and ORDER BY selected aliases always use identifier rendering.
|
package/README.md
CHANGED
|
@@ -565,25 +565,74 @@ connection.
|
|
|
565
565
|
|
|
566
566
|
### Identifiers
|
|
567
567
|
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
double-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
568
|
+
Every adapter quotes CRUD column identifiers, including insert/upsert column
|
|
569
|
+
lists, conflict targets, SET clauses and WHERE fields. Keyword columns such as
|
|
570
|
+
`order`, `group`, `from`, `select`, `values` and `user` work without special input.
|
|
571
|
+
Table names retain the restricted `[a-zA-Z_][a-zA-Z0-9_]*` contract. Bare CRUD
|
|
572
|
+
column names follow the same pattern; values are always bound parameters.
|
|
573
|
+
|
|
574
|
+
SQLite emits backtick-quoted identifiers, doubling embedded backticks. This
|
|
575
|
+
ensures an absent column fails instead of falling back to a string literal on
|
|
576
|
+
SQLite drivers with double-quoted string support. Input names still use the
|
|
577
|
+
explicit double-quote syntax below; authored raw SQL keeps its driver behavior.
|
|
578
|
+
|
|
579
|
+
PostgreSQL and PGlite preserve PostgreSQL's established unquoted folding:
|
|
580
|
+
`camelCase` addresses the physical column `camelcase`. For an exact mixed-case
|
|
581
|
+
or spaced physical column, use an explicitly double-delimited identifier:
|
|
582
|
+
|
|
583
|
+
```typescript
|
|
584
|
+
await db.insert('items', { order: 1, '"MixedCase"': 'exact', '"Full Name"': 'Ada' });
|
|
585
|
+
await db.update('items', { '"Full Name" like': 'A%' }, { order: 2 });
|
|
586
|
+
```
|
|
587
|
+
|
|
588
|
+
Double an embedded double quote inside a delimited identifier. Malformed
|
|
589
|
+
identifiers, empty names, NUL bytes and non-string identifiers are rejected.
|
|
590
|
+
Record keys represent one column. WHERE and aggregate identifiers can instead
|
|
591
|
+
be qualified: `alias.order` quotes each component, while `"foo.bar"` addresses
|
|
592
|
+
one column whose name contains a literal dot. Delimited names are identifiers,
|
|
593
|
+
never raw expressions; use `raw()` only for developer-authored WHERE SQL.
|
|
594
|
+
|
|
595
|
+
DuckDB and JSON `upsert()` retain their existing literal physical-name record
|
|
596
|
+
and conflict keys, so `Full Name`, `user-id`, `2024_total`, `foo.bar` and embedded
|
|
597
|
+
quotes work directly there. A key containing outer quote characters is also
|
|
598
|
+
literal in these existing upsert paths; it is never silently renamed. For strict
|
|
599
|
+
`insert`/`update` and WHERE inputs, explicitly delimit such names as above.
|
|
600
|
+
JSON catalog loading and writeback preserve the original physical keys.
|
|
601
|
+
|
|
602
|
+
Standalone `buildWhere`, `buildAggregate` and `bucketExpr` preserve ordinary
|
|
603
|
+
identifier SQL output by default and automatically quote keywords and explicit
|
|
604
|
+
delimited identifiers. New builder calls should pass `{ quoteIdentifiers: true }`
|
|
605
|
+
as the fourth argument to quote every identifier component. Adapter CRUD methods
|
|
606
|
+
select this policy automatically. Raw expression keys retain their SQL exactly.
|
|
607
|
+
There is no adapter `orderBy`, aggregate or record `exists` method: execute
|
|
608
|
+
`buildAggregate()` output for ordered/aggregate queries, and use `tableExists()`
|
|
609
|
+
for table existence. Standalone `renderExpression` retains its exact physical
|
|
610
|
+
identifier spelling contract.
|
|
611
|
+
|
|
612
|
+
Index `columns` arrays in shared SQL generation helpers and schema manifests retain their
|
|
613
|
+
caller-authored SQL term contract: expressions such as `lower(email)` and ordered
|
|
614
|
+
terms such as `created_at DESC` pass through exactly. Simple identifiers quote
|
|
615
|
+
according to the builder's dialect policy. Strict CRUD and WHERE keys still
|
|
616
|
+
require identifiers or explicit `raw()` expressions. Direct adapter
|
|
617
|
+
`alterTable.addIndex()` accepts only the original plain column-name grammar
|
|
618
|
+
(`[a-zA-Z_][a-zA-Z0-9_]*`); expressions, ordered SQL, spaced/digit-prefix names and
|
|
619
|
+
explicitly quoted unusual names reject before execution. Plain keyword names
|
|
620
|
+
such as `order` pass validation and are quoted in the generated SQL. Aggregate `orderBy` keeps
|
|
621
|
+
the first identifier and optional ASC/DESC direction; trailing tokens are ignored.
|
|
622
|
+
Legacy numeric literal WHERE keys, aggregate literals and ordinal group/order
|
|
623
|
+
terms retain their SQL meaning; explicitly quoted numeric names address columns.
|
|
624
|
+
SELECT aliases and FROM/JOIN table names always use identifier rendering, even
|
|
625
|
+
when their spelling is numeric. ORDER BY a selected alias also names that alias. Manifest foreign key column strings can contain comma-separated names;
|
|
626
|
+
commas inside explicit quotes belong to one name. DDL/schema names also accept
|
|
627
|
+
legacy SQLite backtick, bracket and single-quote delimiters, normalizing them as
|
|
628
|
+
identifiers while preserving their physical spelling. CRUD/WHERE exact-name
|
|
629
|
+
inputs continue to use explicit double quotes.
|
|
630
|
+
|
|
631
|
+
Known-dialect ALTER helpers quote column and index identifiers. The schema
|
|
632
|
+
manifest manager has no dialect tag and preserves bare ordinary spelling
|
|
633
|
+
(including PostgreSQL folding), quoting keywords and explicitly delimited names.
|
|
634
|
+
Schema DDL, types, defaults/checks, partial-index WHERE expressions, foreign-key
|
|
635
|
+
actions and trigger syntax/bodies remain developer-authored SQL contracts.
|
|
587
636
|
|
|
588
637
|
### WHERE Clause Building
|
|
589
638
|
|
|
@@ -676,6 +725,7 @@ const aggregate = buildAggregate(
|
|
|
676
725
|
},
|
|
677
726
|
1,
|
|
678
727
|
'postgres',
|
|
728
|
+
{ quoteIdentifiers: true },
|
|
679
729
|
);
|
|
680
730
|
|
|
681
731
|
const rows = await db.query(aggregate.sql, aggregate.values);
|
|
@@ -779,6 +829,83 @@ const aggregate = buildAggregate(
|
|
|
779
829
|
`t.region`) now aliases to its last segment. Two select items that resolve to
|
|
780
830
|
the same alias are rejected.
|
|
781
831
|
|
|
832
|
+
### Portable scalar expressions and generated columns
|
|
833
|
+
|
|
834
|
+
`renderExpression` is exported from the root and browser-safe `@happyvertical/sql/query`.
|
|
835
|
+
It uses the same expression renderer as `buildAggregate`, with a quoted-identifier
|
|
836
|
+
policy for standalone expressions and the existing aggregate policy for compatibility.
|
|
837
|
+
|
|
838
|
+
```typescript
|
|
839
|
+
import { renderExpression } from '@happyvertical/sql/query';
|
|
840
|
+
|
|
841
|
+
const rendered = renderExpression(
|
|
842
|
+
{ op: 'round', value: { op: 'mul', left: { col: 'total' }, right: { value: 0.2 } }, digits: { value: 2 } },
|
|
843
|
+
{ dialect: 'postgres', mode: 'ddl', resolveColumn: name => name },
|
|
844
|
+
);
|
|
845
|
+
// { sql, values: [], resultType: 'number', immutable: { sqlite: true, postgres: true, duckdb: true } }
|
|
846
|
+
```
|
|
847
|
+
|
|
848
|
+
`mode: 'query'` binds strings and typed date literals as `$N` parameters and returns
|
|
849
|
+
`values`; finite numbers, booleans and NULL remain inline. `mode: 'ddl'` safely
|
|
850
|
+
inlines literals and requires immutability on **every** dialect. JSON uses DuckDB
|
|
851
|
+
semantics. `resolveColumn` maps a logical column to an identifier string or
|
|
852
|
+
`{ name, type }`. Dots delimit identifier components, each double-quoted; it never
|
|
853
|
+
accepts SQL. For temporal columns, `type: 'date'` means a native SQL DATE and
|
|
854
|
+
`type: 'datetime'` means a native TIMESTAMP WITHOUT TIME ZONE containing UTC
|
|
855
|
+
wall-clock fields (SQLite stores either as ISO text). Supplying this metadata is a
|
|
856
|
+
caller assertion about the actual schema. Unknown, text and timezone-aware
|
|
857
|
+
columns cannot be used in generated temporal operations: PostgreSQL extraction
|
|
858
|
+
from TIMESTAMPTZ can depend on the session timezone.
|
|
859
|
+
|
|
860
|
+
New nodes are closed JSON data (unknown/missing fields are rejected):
|
|
861
|
+
|
|
862
|
+
| Node shape | Result type | NULL and portable semantics |
|
|
863
|
+
| --- | --- | --- |
|
|
864
|
+
| `{ op: 'case', branches: [{ when, then }], else }` | common branch type, otherwise `unknown` | Ordered first true condition wins; false/NULL conditions continue; selected NULL remains NULL. Conditions must be SQL booleans. |
|
|
865
|
+
| `{ op: 'if', condition, then, else }` | common branch type | One-branch CASE; false/NULL selects else. |
|
|
866
|
+
| `{ op: 'round', value, digits }` | number | Strict NULL; halfway cases round away from zero, including negative digits. Integer digits are bounded to -12..12. |
|
|
867
|
+
| `{ op: 'abs' / 'floor' / 'ceil', value }` | number | Strict NULL; ordinary mathematical absolute value, floor and ceiling. |
|
|
868
|
+
| `{ op: 'least' / 'greatest', operands }` | number | At least two numeric operands; **any** NULL yields NULL, including on PostgreSQL. |
|
|
869
|
+
| `{ op: 'mod', left, right }` | number | Fractional remainder with dividend's sign; NULL operand or zero divisor yields NULL. Existing `div` also yields NULL for zero. |
|
|
870
|
+
| `{ op: 'concat', operands }` | text | At least one text operand; concatenation with any NULL yields NULL. |
|
|
871
|
+
| `{ op: 'lower' / 'upper', value }` | text | Strict NULL; ASCII A-Z/a-z conversion only. Non-ASCII characters are preserved identically across dialects. |
|
|
872
|
+
| `{ op: 'trim', value }` | text | Strict NULL; removes ASCII spaces from both ends, preserving tabs/newlines. |
|
|
873
|
+
| `{ op: 'length', value }` | number | Strict NULL; Unicode code-point count, not grapheme count. |
|
|
874
|
+
| `{ op: 'substring', value, start, length }` | text | Strict NULL; positive one-based integer start, nonnegative integer length, both at most 2147483647. Out-of-string start returns empty text. |
|
|
875
|
+
| `{ op: 'year' / 'month' / 'day', value }` | number | Strict NULL; UTC calendar part for typed dates/datetimes. |
|
|
876
|
+
| `{ op: 'daysBetween', later, earlier }` | number | Strict NULL; signed calendar-date difference, discarding time of day. |
|
|
877
|
+
| `{ op: 'addDays', value, days }` | date | Strict NULL; add integer calendar days (-365000..365000), discarding time of day. Canonical YYYY-MM-DD text at the expression boundary; native DATE internally for composition. |
|
|
878
|
+
| `{ value: { kind: 'date' / 'datetime', iso } }` | date / datetime | JSON-safe typed literal; date requires YYYY-MM-DD, datetime requires ISO seconds and explicit Z/offset (up to millisecond precision). Gregorian calendar/leap days are validated. Datetimes are canonicalized to UTC before timezone-free SQL casts. |
|
|
879
|
+
|
|
880
|
+
All function arguments above are expression operands (`{ col }`, `{ value }`, or
|
|
881
|
+
another node). Numeric functions require SQL numeric values, text functions SQL
|
|
882
|
+
text values, and conditional branches compatible SQL types; this renderer does
|
|
883
|
+
not inspect a live schema or emulate dialect-specific implicit conversions.
|
|
884
|
+
Invalid literal integer arguments throw; dynamic NULL/out-of-range/fractional
|
|
885
|
+
integer arguments yield NULL without unsafe casts. An `addDays` result outside
|
|
886
|
+
years 0001..9999 yields NULL before formatting or nested date operations. Legacy
|
|
887
|
+
JavaScript `Date` query literals retain their
|
|
888
|
+
existing casts (including PostgreSQL TIMESTAMPTZ); generated use requires the new
|
|
889
|
+
typed literals. Generated `asText` also requires known number/text/boolean input;
|
|
890
|
+
date-to-text casts and unknown types can depend on session settings. SQL DATE/TIMESTAMP literal projections use native database result
|
|
891
|
+
representations; date-valued formula projections use canonical ISO text.
|
|
892
|
+
|
|
893
|
+
Every new node is immutable on SQLite, PostgreSQL and DuckDB with these input
|
|
894
|
+
type contracts. `case`/`if` report their common result type (`null` when all
|
|
895
|
+
branches are NULL, otherwise `unknown` for mixed types); existing `coalesce`
|
|
896
|
+
propagates types too. `immutable` reports each dialect independently and includes
|
|
897
|
+
children. A nonimmutable child (legacy Date, or a temporal operation on an unknown
|
|
898
|
+
column) makes generated use fail with `ExpressionError.code: 'NON_IMMUTABLE'`.
|
|
899
|
+
Invalid data uses `INVALID_EXPRESSION`; depth >24, >1024 nodes or >262144 cumulative
|
|
900
|
+
SQL characters during rendering uses `EXPRESSION_LIMIT`. SQL expansion guards
|
|
901
|
+
apply before nested portable guards can allocate exponentially large strings.
|
|
902
|
+
|
|
903
|
+
Tests execute each node on SQLite, PGlite and DuckDB and create generated columns
|
|
904
|
+
and expression indexes on SQLite/PostgreSQL. DuckDB supports virtual generated
|
|
905
|
+
columns but rejects indexes on generated columns (constant expressions can instead
|
|
906
|
+
fail its requirement to reference a base column); these index limitations are
|
|
907
|
+
asserted explicitly. This API renders expressions; callers create schema DDL.
|
|
908
|
+
|
|
782
909
|
### Schema Synchronization
|
|
783
910
|
|
|
784
911
|
```typescript
|
|
@@ -8,7 +8,7 @@ import type { SqlAdapterType } from './shared/utils.js';
|
|
|
8
8
|
* bound as parameters (strings, dates). There is no raw-SQL escape hatch.
|
|
9
9
|
*/
|
|
10
10
|
/** A scalar a node may hold. Strings and dates are bound; the rest are inlined. */
|
|
11
|
-
export type AggregateLiteral = string | number | boolean | Date | null;
|
|
11
|
+
export type AggregateLiteral = string | number | boolean | Date | TypedDateLiteral | null;
|
|
12
12
|
export type AggregateArithmeticOp = 'add' | 'sub' | 'mul' | 'div';
|
|
13
13
|
export type AggregateComparisonOp = 'eq' | 'ne' | 'lt' | 'lte' | 'gt' | 'gte';
|
|
14
14
|
export type AggregateOperand =
|
|
@@ -20,12 +20,57 @@ export type AggregateOperand =
|
|
|
20
20
|
| {
|
|
21
21
|
value: AggregateLiteral;
|
|
22
22
|
} | AggregateExpression;
|
|
23
|
-
|
|
23
|
+
/** JSON-safe calendar date or UTC datetime (offsets are canonicalized to UTC). */
|
|
24
|
+
export interface TypedDateLiteral {
|
|
25
|
+
kind: 'date' | 'datetime';
|
|
26
|
+
iso: string;
|
|
27
|
+
}
|
|
28
|
+
export type PortableExpression = {
|
|
29
|
+
op: 'case';
|
|
30
|
+
branches: {
|
|
31
|
+
when: AggregateOperand;
|
|
32
|
+
then: AggregateOperand;
|
|
33
|
+
}[];
|
|
34
|
+
else: AggregateOperand;
|
|
35
|
+
} | {
|
|
36
|
+
op: 'if';
|
|
37
|
+
condition: AggregateOperand;
|
|
38
|
+
then: AggregateOperand;
|
|
39
|
+
else: AggregateOperand;
|
|
40
|
+
} | {
|
|
41
|
+
op: 'round';
|
|
42
|
+
value: AggregateOperand;
|
|
43
|
+
digits: AggregateOperand;
|
|
44
|
+
} | {
|
|
45
|
+
op: 'abs' | 'floor' | 'ceil' | 'lower' | 'upper' | 'trim' | 'length' | 'year' | 'month' | 'day';
|
|
46
|
+
value: AggregateOperand;
|
|
47
|
+
} | {
|
|
48
|
+
op: 'least' | 'greatest' | 'concat';
|
|
49
|
+
operands: AggregateOperand[];
|
|
50
|
+
} | {
|
|
51
|
+
op: 'mod';
|
|
52
|
+
left: AggregateOperand;
|
|
53
|
+
right: AggregateOperand;
|
|
54
|
+
} | {
|
|
55
|
+
op: 'substring';
|
|
56
|
+
value: AggregateOperand;
|
|
57
|
+
start: AggregateOperand;
|
|
58
|
+
length: AggregateOperand;
|
|
59
|
+
} | {
|
|
60
|
+
op: 'daysBetween';
|
|
61
|
+
later: AggregateOperand;
|
|
62
|
+
earlier: AggregateOperand;
|
|
63
|
+
} | {
|
|
64
|
+
op: 'addDays';
|
|
65
|
+
value: AggregateOperand;
|
|
66
|
+
days: AggregateOperand;
|
|
67
|
+
};
|
|
68
|
+
export type AggregateExpression = PortableExpression
|
|
24
69
|
/**
|
|
25
70
|
* `left op right`. `div` yields a fractional result on every dialect (integer
|
|
26
71
|
* operands are not truncated) and NULL, not an error, for a zero divisor.
|
|
27
72
|
*/
|
|
28
|
-
{
|
|
73
|
+
| {
|
|
29
74
|
op: AggregateArithmeticOp;
|
|
30
75
|
left: AggregateOperand;
|
|
31
76
|
right: AggregateOperand;
|
|
@@ -100,6 +145,11 @@ export interface ExpressionContext {
|
|
|
100
145
|
onColumn?: (column: string) => void;
|
|
101
146
|
/** Nodes rendered so far across every expression of one build; capped at {@link MAX_NODES}. */
|
|
102
147
|
nodeCount?: number;
|
|
148
|
+
/** Public rendering policy; omitted by the aggregate builder for compatibility. */
|
|
149
|
+
mode?: 'ddl' | 'query';
|
|
150
|
+
columnType?: (column: string) => ExpressionResultType;
|
|
151
|
+
/** Cumulative rendering work, including SQL duplicated by portable guards. */
|
|
152
|
+
sqlWork?: number;
|
|
103
153
|
}
|
|
104
154
|
/**
|
|
105
155
|
* Render an expression node to SQL, binding its string and date literals into
|
|
@@ -107,4 +157,28 @@ export interface ExpressionContext {
|
|
|
107
157
|
* identifier, non-finite number, or an expression too large to be legitimate.
|
|
108
158
|
*/
|
|
109
159
|
export declare function renderAggregateExpression(node: AggregateOperand, ctx: ExpressionContext): string;
|
|
160
|
+
export type ExpressionResultType = 'number' | 'text' | 'boolean' | 'date' | 'datetime' | 'null' | 'unknown';
|
|
161
|
+
export type ExpressionErrorCode = 'INVALID_EXPRESSION' | 'NON_IMMUTABLE' | 'EXPRESSION_LIMIT';
|
|
162
|
+
/** Coded validation failures suitable for persisted/untrusted expression data. */
|
|
163
|
+
export declare class ExpressionError extends Error {
|
|
164
|
+
readonly code: ExpressionErrorCode;
|
|
165
|
+
constructor(code: ExpressionErrorCode, message: string);
|
|
166
|
+
}
|
|
167
|
+
export interface RenderExpressionOptions {
|
|
168
|
+
dialect: 'sqlite' | 'postgres' | 'duckdb' | 'json';
|
|
169
|
+
mode: 'ddl' | 'query';
|
|
170
|
+
/** Maps a logical column to an identifier, never to SQL. Dots delimit parts. */
|
|
171
|
+
resolveColumn?: (column: string) => string | {
|
|
172
|
+
name: string;
|
|
173
|
+
type: ExpressionResultType;
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
export interface RenderExpressionResult {
|
|
177
|
+
sql: string;
|
|
178
|
+
values: unknown[];
|
|
179
|
+
resultType: ExpressionResultType;
|
|
180
|
+
immutable: Record<'sqlite' | 'postgres' | 'duckdb', boolean>;
|
|
181
|
+
}
|
|
182
|
+
/** Render a closed scalar expression. DDL additionally requires immutability. */
|
|
183
|
+
export declare function renderExpression(node: AggregateOperand, options: RenderExpressionOptions): RenderExpressionResult;
|
|
110
184
|
//# sourceMappingURL=aggregate-expression.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aggregate-expression.d.ts","sourceRoot":"","sources":["../src/aggregate-expression.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"aggregate-expression.d.ts","sourceRoot":"","sources":["../src/aggregate-expression.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAExD;;;;;;;GAOG;AAEH,mFAAmF;AACnF,MAAM,MAAM,gBAAgB,GACxB,MAAM,GACN,MAAM,GACN,OAAO,GACP,IAAI,GACJ,gBAAgB,GAChB,IAAI,CAAC;AAET,MAAM,MAAM,qBAAqB,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,CAAC;AAClE,MAAM,MAAM,qBAAqB,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,KAAK,GAAG,IAAI,GAAG,KAAK,CAAC;AAE9E,MAAM,MAAM,gBAAgB;AAC1B,mFAAmF;AACjF;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE;AACjB,uBAAuB;GACrB;IAAE,KAAK,EAAE,gBAAgB,CAAA;CAAE,GAC3B,mBAAmB,CAAC;AAExB,kFAAkF;AAClF,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,MAAM,GAAG,UAAU,CAAC;IAC1B,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,MAAM,kBAAkB,GAC1B;IACE,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,EAAE;QAAE,IAAI,EAAE,gBAAgB,CAAC;QAAC,IAAI,EAAE,gBAAgB,CAAA;KAAE,EAAE,CAAC;IAC/D,IAAI,EAAE,gBAAgB,CAAC;CACxB,GACD;IACE,EAAE,EAAE,IAAI,CAAC;IACT,SAAS,EAAE,gBAAgB,CAAC;IAC5B,IAAI,EAAE,gBAAgB,CAAC;IACvB,IAAI,EAAE,gBAAgB,CAAC;CACxB,GACD;IAAE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
+
import { type IdentifierOptions } from './shared/alter-utils.js';
|
|
2
3
|
import type { WhereClause } from './shared/types.js';
|
|
3
4
|
import { type SqlAdapterType } from './shared/utils.js';
|
|
4
5
|
export type AggregateTimeBucketUnit = 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year';
|
|
@@ -52,6 +53,6 @@ export interface AggregateBuildResult {
|
|
|
52
53
|
sql: string;
|
|
53
54
|
values: any[];
|
|
54
55
|
}
|
|
55
|
-
export declare function bucketExpr(adapterType: SqlAdapterType | undefined, unit: AggregateTimeBucketUnit, column: string): string;
|
|
56
|
-
export declare function buildAggregate(spec: AggregateSpec, startIndex?: number, adapterType?: SqlAdapterType): AggregateBuildResult;
|
|
56
|
+
export declare function bucketExpr(adapterType: SqlAdapterType | undefined, unit: AggregateTimeBucketUnit, column: string, options?: IdentifierOptions): string;
|
|
57
|
+
export declare function buildAggregate(spec: AggregateSpec, startIndex?: number, adapterType?: SqlAdapterType, options?: IdentifierOptions): AggregateBuildResult;
|
|
57
58
|
//# sourceMappingURL=aggregate.d.ts.map
|
package/dist/aggregate.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aggregate.d.ts","sourceRoot":"","sources":["../src/aggregate.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,aAAa,EAClB,KAAK,gBAAgB,EAGtB,MAAM,2BAA2B,CAAC;AACnC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,EAIL,KAAK,cAAc,EAEpB,MAAM,mBAAmB,CAAC;AAE3B,MAAM,MAAM,uBAAuB,GAC/B,QAAQ,GACR,MAAM,GACN,KAAK,GACL,MAAM,GACN,OAAO,GACP,SAAS,GACT,MAAM,CAAC;AAEX,MAAM,MAAM,iBAAiB,GACzB,OAAO,GACP,KAAK,GACL,KAAK,GACL,KAAK,GACL,KAAK,GACL,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC;AAElB,MAAM,M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AC9B,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB,uEAAuE;IACvE,SAAS,CAAC,EAAE,gBAAgB,CAAC;IAC7B,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAC5B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,oBAAoB;IACnC,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,GAAG,EAAE,CAAC;CACf;AA4CD,wBAAgB,UAAU,CACxB,WAAW,EAAE,cAAc,GAAG,SAAS,EACvC,IAAI,EAAE,uBAAuB,EAC7B,MAAM,EAAE,MAAM,EACd,OAAO,GAAE,iBAAsB,GAC9B,MAAM,CA4BR;AAqRD,wBAAgB,cAAc,CAC5B,IAAI,EAAE,aAAa,EACnB,UAAU,SAAI,EACd,WAAW,CAAC,EAAE,cAAc,EAC5B,OAAO,GAAE,iBAAsB,GAC9B,oBAAoB,CAwItB"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { C as validateColumnNames, E as validateTableName, S as validateColumnName, T as validatePlainColumnName, d as escapeStringLiteral, f as generateAddColumnStatement, g as quoteDuckDBColumnName, i as formatDbError, n as buildAdapterWhere, p as generateCreateIndexStatement, s as resolveInsertColumns, t as DatabaseSchemaManager, u as wrapDatabaseError, w as validateIndexName } from "./schema-manager-psLzQ2yh.js";
|
|
2
|
+
import { a as NestedTransactionError, o as resolveSchemas, s as convertUniqueIndexesToInlineConstraints } from "./query-Dz0TZ0pY.js";
|
|
3
|
+
import { c as createTransactionLock, d as validateDatabaseCacheOptions } from "./postgres-dialect-CaNhva-C.js";
|
|
4
4
|
import { n as createDuckDBResourceCloser, r as throwWithDuckDBCleanup, t as serializeDuckDBStatements } from "./duckdb-statement-queue-BJ6-PQRr.js";
|
|
5
5
|
import { t as redactDatabaseUrl } from "./redact-database-url-CfMDvSdy.js";
|
|
6
6
|
import { DatabaseError } from "@happyvertical/utils";
|
|
@@ -20,7 +20,7 @@ import { tmpdir } from "node:os";
|
|
|
20
20
|
*/
|
|
21
21
|
async function createTablesFromSchemas(connection, schemas) {
|
|
22
22
|
for (const [tableName, schema] of Object.entries(schemas)) try {
|
|
23
|
-
if ((await connection.runAndReadAll(`SELECT name FROM sqlite_master WHERE type='table' AND name='${tableName}'`)).getRowObjects().length > 0) {
|
|
23
|
+
if ((await connection.runAndReadAll(`SELECT name FROM sqlite_master WHERE type='table' AND name='${escapeStringLiteral(tableName)}'`)).getRowObjects().length > 0) {
|
|
24
24
|
console.log(`[duckdb] Table ${tableName} already exists, skipping`);
|
|
25
25
|
continue;
|
|
26
26
|
}
|
|
@@ -206,7 +206,7 @@ async function getDatabase(options = {}) {
|
|
|
206
206
|
}
|
|
207
207
|
}).join(", ")})`;
|
|
208
208
|
}).join(", ");
|
|
209
|
-
const sql = `INSERT INTO ${table} (${keys.join(", ")}) VALUES ${placeholders}`;
|
|
209
|
+
const sql = `INSERT INTO ${table} (${keys.map((key) => quoteDuckDBColumnName(key)).join(", ")}) VALUES ${placeholders}`;
|
|
210
210
|
try {
|
|
211
211
|
await connection.run(sql, values);
|
|
212
212
|
const affected = records.length;
|
|
@@ -234,7 +234,7 @@ async function getDatabase(options = {}) {
|
|
|
234
234
|
*/
|
|
235
235
|
const get = async (table, where) => {
|
|
236
236
|
validateTableName(table);
|
|
237
|
-
const { sql: whereClause, values } =
|
|
237
|
+
const { sql: whereClause, values } = buildAdapterWhere(where, 1, "duckdb");
|
|
238
238
|
const sql = `SELECT * FROM ${table} ${whereClause} LIMIT 1`;
|
|
239
239
|
try {
|
|
240
240
|
const rows = (await connection.runAndReadAll(sql, values)).getRowObjects();
|
|
@@ -258,7 +258,7 @@ async function getDatabase(options = {}) {
|
|
|
258
258
|
*/
|
|
259
259
|
const list = async (table, where) => {
|
|
260
260
|
validateTableName(table);
|
|
261
|
-
const { sql: whereClause, values } =
|
|
261
|
+
const { sql: whereClause, values } = buildAdapterWhere(where, 1, "duckdb");
|
|
262
262
|
const sql = `SELECT * FROM ${table} ${whereClause}`;
|
|
263
263
|
try {
|
|
264
264
|
return convertBigInts((await connection.runAndReadAll(sql, values)).getRowObjects());
|
|
@@ -284,8 +284,8 @@ async function getDatabase(options = {}) {
|
|
|
284
284
|
validateTableName(table);
|
|
285
285
|
const keys = Object.keys(data);
|
|
286
286
|
validateColumnNames(keys);
|
|
287
|
-
const setClause = keys.map((key, idx) => `${key} = $${idx + 1}`).join(", ");
|
|
288
|
-
const { sql: whereClause, values: whereValues } =
|
|
287
|
+
const setClause = keys.map((key, idx) => `${quoteDuckDBColumnName(key)} = $${idx + 1}`).join(", ");
|
|
288
|
+
const { sql: whereClause, values: whereValues } = buildAdapterWhere(where, keys.length + 1, "duckdb");
|
|
289
289
|
const sql = `UPDATE ${table} SET ${setClause} ${whereClause}`;
|
|
290
290
|
const values = [...keys.map((key) => data[key]), ...whereValues];
|
|
291
291
|
try {
|
|
@@ -326,8 +326,8 @@ async function getDatabase(options = {}) {
|
|
|
326
326
|
return `$${paramIdx.value++}`;
|
|
327
327
|
};
|
|
328
328
|
const executeNullAwareDuckDBUpsert = async (table, conflictColumns, data) => {
|
|
329
|
-
const conflictWhere = Object.fromEntries(conflictColumns.map((col) => [col, data[col]]));
|
|
330
|
-
if (!await get(table, conflictWhere)) return insert(table, data);
|
|
329
|
+
const conflictWhere = Object.fromEntries(conflictColumns.map((col) => [quoteDuckDBColumnName(col, true), data[col]]));
|
|
330
|
+
if (!await get(table, conflictWhere)) return insert(table, Object.fromEntries(Object.entries(data).map(([key, value]) => [quoteDuckDBColumnName(key, true), value])));
|
|
331
331
|
const updateKeys = Object.keys(data).filter((key) => !conflictColumns.includes(key));
|
|
332
332
|
if (updateKeys.length === 0) return {
|
|
333
333
|
operation: "upsert",
|
|
@@ -337,9 +337,9 @@ async function getDatabase(options = {}) {
|
|
|
337
337
|
const paramIdx = { value: 1 };
|
|
338
338
|
const assignments = updateKeys.map((key) => {
|
|
339
339
|
const valueExpr = buildDuckDBValueExpression(data[key], values, paramIdx);
|
|
340
|
-
return
|
|
340
|
+
return `${quoteDuckDBColumnName(key, true)} = ${valueExpr}`;
|
|
341
341
|
});
|
|
342
|
-
const { sql: whereClause, values: whereValues } =
|
|
342
|
+
const { sql: whereClause, values: whereValues } = buildAdapterWhere(conflictWhere, paramIdx.value, "duckdb");
|
|
343
343
|
const sql = `UPDATE ${table} SET ${assignments.join(", ")} ${whereClause}`;
|
|
344
344
|
try {
|
|
345
345
|
await connection.run(sql, [...values, ...whereValues]);
|
|
@@ -408,39 +408,10 @@ async function getDatabase(options = {}) {
|
|
|
408
408
|
values.push(value);
|
|
409
409
|
paramIdx++;
|
|
410
410
|
}
|
|
411
|
-
const
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
else if (value === "" && typeof value === "string") {
|
|
416
|
-
updateSetParts.push(`${key} = CAST($${paramIdx} AS TEXT)`);
|
|
417
|
-
values.push(value);
|
|
418
|
-
paramIdx++;
|
|
419
|
-
} else if (value instanceof Date) {
|
|
420
|
-
updateSetParts.push(`${key} = $${paramIdx}`);
|
|
421
|
-
values.push(value.toISOString());
|
|
422
|
-
paramIdx++;
|
|
423
|
-
} else if (Array.isArray(value)) {
|
|
424
|
-
updateSetParts.push(`${key} = CAST($${paramIdx} AS JSON)`);
|
|
425
|
-
values.push(JSON.stringify(value));
|
|
426
|
-
paramIdx++;
|
|
427
|
-
} else if (typeof value === "object" && value !== null && Object.getPrototypeOf(value) === Object.prototype) {
|
|
428
|
-
updateSetParts.push(`${key} = CAST($${paramIdx} AS JSON)`);
|
|
429
|
-
values.push(JSON.stringify(value));
|
|
430
|
-
paramIdx++;
|
|
431
|
-
} else {
|
|
432
|
-
updateSetParts.push(`${key} = $${paramIdx}`);
|
|
433
|
-
values.push(value);
|
|
434
|
-
paramIdx++;
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
const conflict = conflictCols.map((col) => `"${escapeQuotedIdentifier(col)}"`).join(", ");
|
|
438
|
-
const quotedKeys = keys.map((key) => `"${escapeQuotedIdentifier(key)}"`).join(", ");
|
|
439
|
-
const quotedUpdateSetParts = updateKeys.map((key, i) => {
|
|
440
|
-
const part = updateSetParts[i];
|
|
441
|
-
const valueExpr = part.substring(part.indexOf("=") + 1).trim();
|
|
442
|
-
return `"${escapeQuotedIdentifier(key)}" = ${valueExpr}`;
|
|
443
|
-
});
|
|
411
|
+
const updateIndex = { value: paramIdx };
|
|
412
|
+
const quotedUpdateSetParts = updateKeys.map((key) => `${quoteDuckDBColumnName(key, true)} = ${buildDuckDBValueExpression(record[key], values, updateIndex)}`);
|
|
413
|
+
const conflict = conflictCols.map((col) => quoteDuckDBColumnName(col, true)).join(", ");
|
|
414
|
+
const quotedKeys = keys.map((key) => quoteDuckDBColumnName(key, true)).join(", ");
|
|
444
415
|
const conflictAction = quotedUpdateSetParts.length > 0 ? `DO UPDATE SET ${quotedUpdateSetParts.join(", ")}` : "DO NOTHING";
|
|
445
416
|
const sql = `INSERT INTO ${table} (${quotedKeys}) VALUES (${placeholders.join(", ")}) ON CONFLICT(${conflict}) ${conflictAction}`;
|
|
446
417
|
try {
|
|
@@ -492,7 +463,7 @@ async function getDatabase(options = {}) {
|
|
|
492
463
|
const deleteRecords = async (table, where) => {
|
|
493
464
|
validateTableName(table);
|
|
494
465
|
if (Object.keys(where).length === 0) throw new DatabaseError("DELETE requires at least one WHERE condition to prevent accidental deletion of all records", { table });
|
|
495
|
-
const { sql: whereClause, values } =
|
|
466
|
+
const { sql: whereClause, values } = buildAdapterWhere(where, 1, "duckdb");
|
|
496
467
|
const sql = `DELETE FROM ${table} ${whereClause}`;
|
|
497
468
|
try {
|
|
498
469
|
await connection.run(sql, values);
|
|
@@ -525,7 +496,7 @@ async function getDatabase(options = {}) {
|
|
|
525
496
|
const rows = (await connection.runAndReadAll(`SELECT COUNT(*) as count FROM ${table}`)).getRowObjects();
|
|
526
497
|
return Number(rows[0]?.count) || 0;
|
|
527
498
|
}
|
|
528
|
-
const { sql: whereClause, values } =
|
|
499
|
+
const { sql: whereClause, values } = buildAdapterWhere(where, 1, "duckdb");
|
|
529
500
|
const sql = `SELECT COUNT(*) as count FROM ${table} ${whereClause}`;
|
|
530
501
|
const rows = (await connection.runAndReadAll(sql, values)).getRowObjects();
|
|
531
502
|
return Number(rows[0]?.count) || 0;
|
|
@@ -562,7 +533,7 @@ async function getDatabase(options = {}) {
|
|
|
562
533
|
const exportTableToJSON = async (connection, table, dataDir) => {
|
|
563
534
|
validateTableName(table);
|
|
564
535
|
const filePath = join(dataDir, `${table}.json`);
|
|
565
|
-
await connection.run(`COPY (SELECT * FROM ${table}) TO '${filePath}' (FORMAT JSON, ARRAY true)`);
|
|
536
|
+
await connection.run(`COPY (SELECT * FROM ${table}) TO '${escapeStringLiteral(filePath)}' (FORMAT JSON, ARRAY true)`);
|
|
566
537
|
};
|
|
567
538
|
/**
|
|
568
539
|
* Manual export method for 'manual' write strategy
|
|
@@ -941,10 +912,10 @@ async function getDatabase(options = {}) {
|
|
|
941
912
|
addIndex: async (table, index) => {
|
|
942
913
|
validateTableName(table);
|
|
943
914
|
validateIndexName(index.name);
|
|
944
|
-
for (const col of index.columns)
|
|
915
|
+
for (const col of index.columns) validatePlainColumnName(col);
|
|
945
916
|
let sql;
|
|
946
917
|
try {
|
|
947
|
-
sql = generateCreateIndexStatement(table, index);
|
|
918
|
+
sql = generateCreateIndexStatement(table, index, "duckdb");
|
|
948
919
|
await connection.run(sql);
|
|
949
920
|
} catch (e) {
|
|
950
921
|
throw wrapDatabaseError("Failed to create index on table", e, {
|
|
@@ -1093,4 +1064,4 @@ async function getDatabase(options = {}) {
|
|
|
1093
1064
|
//#endregion
|
|
1094
1065
|
export { getDatabase };
|
|
1095
1066
|
|
|
1096
|
-
//# sourceMappingURL=duckdb-
|
|
1067
|
+
//# sourceMappingURL=duckdb-DCV4JFWK.js.map
|