@nongt/visql 0.0.0-stage → 0.3.1
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/LICENSE +21 -0
- package/README.md +916 -2
- package/dist/PipelineEditor-DNZkDSaY.js +10042 -0
- package/dist/calendarDate-CYlr9TvX.js +43 -0
- package/dist/core-api-BDCnbFHY.js +1056 -0
- package/dist/core.js +80 -0
- package/dist/fixtures.js +7 -0
- package/dist/index.js +145 -0
- package/dist/issueLocation-Cw5sttQL.js +3183 -0
- package/dist/mui-date-pickers.js +117 -0
- package/dist/mui.js +510 -0
- package/dist/schema-z95dxLxu.js +839 -0
- package/dist/sql-import.js +1366 -0
- package/dist/style.css +1 -0
- package/dist/tokens-BTaVMSOx.js +95 -0
- package/dist/types/adapters/mui/MuiDatePicker.d.ts +12 -0
- package/dist/types/adapters/mui/MuiDateTimePicker.d.ts +10 -0
- package/dist/types/adapters/mui/MuiListInput.d.ts +9 -0
- package/dist/types/adapters/mui/MuiTimePicker.d.ts +10 -0
- package/dist/types/adapters/mui/QueryBuilder.d.ts +12 -0
- package/dist/types/adapters/mui/components.d.ts +16 -0
- package/dist/types/adapters/mui/index.d.ts +6 -0
- package/dist/types/adapters/mui/muiPickers.d.ts +6 -0
- package/dist/types/adapters/mui/pickerStyles.d.ts +28 -0
- package/dist/types/adapters/mui/theme.d.ts +11 -0
- package/dist/types/core/BetweenOnTextContext.d.ts +11 -0
- package/dist/types/core/DialectContext.d.ts +10 -0
- package/dist/types/core/DrillDownContext.d.ts +22 -0
- package/dist/types/core/ExistsContext.d.ts +29 -0
- package/dist/types/core/FlowDirectionContext.d.ts +8 -0
- package/dist/types/core/FlowOrientationContext.d.ts +6 -0
- package/dist/types/core/FunctionsContext.d.ts +25 -0
- package/dist/types/core/GroupKeysContext.d.ts +18 -0
- package/dist/types/core/MeasureLockContext.d.ts +12 -0
- package/dist/types/core/ReadOnlyContext.d.ts +6 -0
- package/dist/types/core/SchemaContext.d.ts +8 -0
- package/dist/types/core/SubqueryContext.d.ts +16 -0
- package/dist/types/core/UnionBranchContext.d.ts +6 -0
- package/dist/types/core/ValueInputContext.d.ts +24 -0
- package/dist/types/core/absolutePath.d.ts +57 -0
- package/dist/types/core/ast.d.ts +154 -0
- package/dist/types/core/astChildren.d.ts +4 -0
- package/dist/types/core/astEqual.d.ts +4 -0
- package/dist/types/core/astMap.d.ts +21 -0
- package/dist/types/core/calendarDate.d.ts +21 -0
- package/dist/types/core/columnFormats.d.ts +23 -0
- package/dist/types/core/components/BoolPicker.d.ts +8 -0
- package/dist/types/core/components/NumberInput.d.ts +8 -0
- package/dist/types/core/components/ReadOnlyPart.d.ts +10 -0
- package/dist/types/core/components/TextInput.d.ts +8 -0
- package/dist/types/core/components/expression/BinaryView.d.ts +10 -0
- package/dist/types/core/components/expression/ExprSummary.d.ts +12 -0
- package/dist/types/core/components/expression/ExpressionTypeSelector.d.ts +35 -0
- package/dist/types/core/components/expression/ExpressionView.d.ts +30 -0
- package/dist/types/core/components/expression/FieldView.d.ts +15 -0
- package/dist/types/core/components/expression/FormulaPeek.d.ts +14 -0
- package/dist/types/core/components/expression/FunctionArgInput.d.ts +22 -0
- package/dist/types/core/components/expression/FunctionView.d.ts +7 -0
- package/dist/types/core/components/expression/ListView.d.ts +20 -0
- package/dist/types/core/components/expression/LiteralView.d.ts +23 -0
- package/dist/types/core/components/expression/LogicalView.d.ts +9 -0
- package/dist/types/core/components/expression/MeasureMenu.d.ts +21 -0
- package/dist/types/core/components/expression/ParamView.d.ts +13 -0
- package/dist/types/core/components/expression/RelativeDateView.d.ts +15 -0
- package/dist/types/core/components/expression/SubqueryView.d.ts +8 -0
- package/dist/types/core/components/expression/formulaClasses.d.ts +8 -0
- package/dist/types/core/components/flow/CaseBranchFlowNode.d.ts +5 -0
- package/dist/types/core/components/flow/CaseElseFlowNode.d.ts +5 -0
- package/dist/types/core/components/flow/CaseFlowNode.d.ts +5 -0
- package/dist/types/core/components/flow/ConditionFlowNode.d.ts +5 -0
- package/dist/types/core/components/flow/CreateConditionFlowNode.d.ts +14 -0
- package/dist/types/core/components/flow/CreateHavingFlowNode.d.ts +11 -0
- package/dist/types/core/components/flow/DistinctToggle.d.ts +10 -0
- package/dist/types/core/components/flow/ExistsFlowNode.d.ts +9 -0
- package/dist/types/core/components/flow/FromFlowNode.d.ts +110 -0
- package/dist/types/core/components/flow/FunctionArgFlowNode.d.ts +5 -0
- package/dist/types/core/components/flow/FunctionCallFlowNode.d.ts +5 -0
- package/dist/types/core/components/flow/FunctionSortKeys.d.ts +10 -0
- package/dist/types/core/components/flow/GroupBgNode.d.ts +14 -0
- package/dist/types/core/components/flow/GroupByFlowNode.d.ts +29 -0
- package/dist/types/core/components/flow/IfElseFlowNode.d.ts +5 -0
- package/dist/types/core/components/flow/IfFlowNode.d.ts +5 -0
- package/dist/types/core/components/flow/IfThenFlowNode.d.ts +5 -0
- package/dist/types/core/components/flow/LeafConditionFrame.d.ts +19 -0
- package/dist/types/core/components/flow/LogicalFlowNode.d.ts +5 -0
- package/dist/types/core/components/flow/MoveTooltip.d.ts +19 -0
- package/dist/types/core/components/flow/NotFlowNode.d.ts +5 -0
- package/dist/types/core/components/flow/OrderByFlowNode.d.ts +32 -0
- package/dist/types/core/components/flow/SectionLabelFlowNode.d.ts +7 -0
- package/dist/types/core/components/flow/SelectFlowNode.d.ts +49 -0
- package/dist/types/core/components/flow/SharedQueriesFlowNode.d.ts +39 -0
- package/dist/types/core/components/flow/SummaryPill.d.ts +12 -0
- package/dist/types/core/components/flow/WarningNote.d.ts +9 -0
- package/dist/types/core/components/flow/chainSides.d.ts +18 -0
- package/dist/types/core/components/flow/dragContext.d.ts +12 -0
- package/dist/types/core/components/flow/moveTooltipHysteresis.d.ts +18 -0
- package/dist/types/core/components/flow/useRowDrag.d.ts +34 -0
- package/dist/types/core/components/flow/useRowReorderFlip.d.ts +30 -0
- package/dist/types/core/derivedTables.d.ts +98 -0
- package/dist/types/core/distinctSort.d.ts +7 -0
- package/dist/types/core/drillPath.d.ts +11 -0
- package/dist/types/core/exists.d.ts +7 -0
- package/dist/types/core/existsMenu.d.ts +61 -0
- package/dist/types/core/expressionFlow.d.ts +428 -0
- package/dist/types/core/fanOut.d.ts +58 -0
- package/dist/types/core/findParts.d.ts +19 -0
- package/dist/types/core/flowTrees.d.ts +20 -0
- package/dist/types/core/focusKey.d.ts +10 -0
- package/dist/types/core/formula.d.ts +24 -0
- package/dist/types/core/formulaPeek.d.ts +16 -0
- package/dist/types/core/functionRegistry.d.ts +117 -0
- package/dist/types/core/grouping.d.ts +58 -0
- package/dist/types/core/history.d.ts +25 -0
- package/dist/types/core/i18n/defaultMessages.d.ts +367 -0
- package/dist/types/core/i18n/messages.d.ts +273 -0
- package/dist/types/core/i18n/relativeDateWords.d.ts +3 -0
- package/dist/types/core/id.d.ts +5 -0
- package/dist/types/core/issueAnchor.d.ts +29 -0
- package/dist/types/core/issueLocation.d.ts +105 -0
- package/dist/types/core/limits.d.ts +17 -0
- package/dist/types/core/measures.d.ts +51 -0
- package/dist/types/core/params.d.ts +30 -0
- package/dist/types/core/pipelineAnchor.d.ts +118 -0
- package/dist/types/core/pipelineFlow.d.ts +271 -0
- package/dist/types/core/queryAst.d.ts +201 -0
- package/dist/types/core/queryHelpers.d.ts +113 -0
- package/dist/types/core/queryValidation.d.ts +206 -0
- package/dist/types/core/relativeDates.d.ts +11 -0
- package/dist/types/core/resolveNames.d.ts +17 -0
- package/dist/types/core/rowRange.d.ts +3 -0
- package/dist/types/core/schema.d.ts +119 -0
- package/dist/types/core/scopeQuery.d.ts +45 -0
- package/dist/types/core/selectColumns.d.ts +23 -0
- package/dist/types/core/settleQuery.d.ts +11 -0
- package/dist/types/core/sharedQueries.d.ts +66 -0
- package/dist/types/core/sqlGen.d.ts +102 -0
- package/dist/types/core/sqlOffsets.d.ts +5 -0
- package/dist/types/core/sqlSpans.d.ts +33 -0
- package/dist/types/core/storedQuery.d.ts +43 -0
- package/dist/types/core/summarize.d.ts +3 -0
- package/dist/types/core/theme/tokens.d.ts +74 -0
- package/dist/types/core/ui/UIContext.d.ts +6 -0
- package/dist/types/core/ui/index.d.ts +12 -0
- package/dist/types/core/ui/types.d.ts +264 -0
- package/dist/types/core/unionTables.d.ts +121 -0
- package/dist/types/core/useValueTypes.d.ts +12 -0
- package/dist/types/core/valueTypes.d.ts +54 -0
- package/dist/types/core-api.d.ts +20 -0
- package/dist/types/features/queryEditor/ExpressionFlowCanvas.d.ts +10 -0
- package/dist/types/features/queryEditor/FindBox.d.ts +12 -0
- package/dist/types/features/queryEditor/FlowCanvas.d.ts +96 -0
- package/dist/types/features/queryEditor/FocusPulse.d.ts +10 -0
- package/dist/types/features/queryEditor/IssuesButton.d.ts +16 -0
- package/dist/types/features/queryEditor/PipelineEditor.d.ts +210 -0
- package/dist/types/features/queryEditor/QueryOutline.d.ts +16 -0
- package/dist/types/features/queryEditor/ResultsPanel.d.ts +11 -0
- package/dist/types/features/queryEditor/RunButton.d.ts +11 -0
- package/dist/types/features/queryEditor/SubFlowPanel.d.ts +24 -0
- package/dist/types/features/queryEditor/UndoRedoToast.d.ts +6 -0
- package/dist/types/features/queryEditor/canvasPositioning.d.ts +81 -0
- package/dist/types/features/queryEditor/findKeys.d.ts +3 -0
- package/dist/types/features/queryEditor/flowDragUtils.d.ts +16 -0
- package/dist/types/features/queryEditor/focusContext.d.ts +24 -0
- package/dist/types/features/queryEditor/hoveredPart.d.ts +32 -0
- package/dist/types/features/queryEditor/issueFocusParts.d.ts +9 -0
- package/dist/types/features/queryEditor/issueLocationText.d.ts +7 -0
- package/dist/types/features/queryEditor/issueText.d.ts +11 -0
- package/dist/types/features/queryEditor/outlineSections.d.ts +23 -0
- package/dist/types/features/queryEditor/run.d.ts +36 -0
- package/dist/types/features/queryEditor/sectionLabels.d.ts +15 -0
- package/dist/types/features/queryEditor/treeScopeContext.d.ts +14 -0
- package/dist/types/features/queryEditor/useRemeasureOnDirectionChange.d.ts +7 -0
- package/dist/types/features/queryEditor/useRevealNewNodes.d.ts +35 -0
- package/dist/types/features/queryEditor/withTreeScope.d.ts +10 -0
- package/dist/types/fixtures.d.ts +44 -0
- package/dist/types/index.d.ts +13 -0
- package/dist/types/mui-date-pickers.d.ts +3 -0
- package/dist/types/mui.d.ts +2 -0
- package/dist/types/sql-import.d.ts +3 -0
- package/dist/types/sqlImport/context.d.ts +71 -0
- package/dist/types/sqlImport/expressions.d.ts +11 -0
- package/dist/types/sqlImport/importSql.d.ts +28 -0
- package/dist/types/sqlImport/parse.d.ts +6 -0
- package/dist/types/sqlImport/syntax.d.ts +245 -0
- package/dist/types/sqlImport/tokenize.d.ts +26 -0
- package/package.json +164 -3
|
@@ -0,0 +1,1056 @@
|
|
|
1
|
+
var Et = Object.defineProperty;
|
|
2
|
+
var Tt = (t, e, r) => e in t ? Et(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
|
|
3
|
+
var b = (t, e, r) => Tt(t, typeof e != "symbol" ? e + "" : e, r);
|
|
4
|
+
import { B as x, q as d, F as yt, g as F, d as H, t as Q, h as pt, w as k, o as X, H as At, k as mt, b as gt, i as Dt } from "./schema-z95dxLxu.js";
|
|
5
|
+
import { ag as D, aA as V, aB as Nt, aC as Ot, aD as ht, aj as Rt, V as St, ap as It, aE as y, al as bt, a7 as Ct, aF as Lt, aG as W, $ as Mt } from "./issueLocation-Cw5sttQL.js";
|
|
6
|
+
import { s as J } from "./calendarDate-CYlr9TvX.js";
|
|
7
|
+
class wt extends Error {
|
|
8
|
+
constructor(r, n, s) {
|
|
9
|
+
super(`scopeQuery: the query at ${r.length ? r.join(".") : "the top"} reads "${n}" but can't reach "${s}", which the condition names`);
|
|
10
|
+
/** Where the body is, as keys into the query (validateQuery's
|
|
11
|
+
* `pathSegments` style). */
|
|
12
|
+
b(this, "path");
|
|
13
|
+
/** The table it reads that needs scope. */
|
|
14
|
+
b(this, "table");
|
|
15
|
+
this.name = "ScopeError", this.path = r, this.table = n;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
const K = (t) => Object.prototype.toString.call(t) === "[object Object]", Ft = (t) => Array.isArray(t.joins) && K(t.from);
|
|
19
|
+
function C(t, e, r) {
|
|
20
|
+
if (Array.isArray(t)) {
|
|
21
|
+
const a = t.map((o, f) => C(o, [...e, f], r));
|
|
22
|
+
return a.every((o, f) => o === t[f]) ? t : a;
|
|
23
|
+
}
|
|
24
|
+
if (!K(t)) return t;
|
|
25
|
+
let n = !1;
|
|
26
|
+
const s = Object.entries(t).map(([a, o]) => {
|
|
27
|
+
const f = C(o, [...e, a], r);
|
|
28
|
+
return f !== o && (n = !0), [a, f];
|
|
29
|
+
}), i = n ? Object.fromEntries(s) : t;
|
|
30
|
+
return Ft(i) ? r(i, e) : i;
|
|
31
|
+
}
|
|
32
|
+
const z = (t, e) => x(t).find(e.requires);
|
|
33
|
+
function Z(t, e) {
|
|
34
|
+
return t ? F(t, e) ? !0 : t.type === "logical" && t.operator === "AND" && t.operands.some((r) => F(r, e)) : !1;
|
|
35
|
+
}
|
|
36
|
+
function j(t, e, r, n) {
|
|
37
|
+
const s = z(e, r);
|
|
38
|
+
if (!s || Z(e.where, r.condition)) return e;
|
|
39
|
+
const i = e, a = x(i), o = d(i).flatMap(($) => $.alias ? [$.alias] : []), f = V(r.condition).find(($) => !o.includes($) && yt(t, a, $) === void 0);
|
|
40
|
+
if (f) throw new wt(n, s, f);
|
|
41
|
+
const { joins: u } = Nt(
|
|
42
|
+
t,
|
|
43
|
+
{
|
|
44
|
+
id: "",
|
|
45
|
+
from: e.from,
|
|
46
|
+
joins: e.joins,
|
|
47
|
+
select: []
|
|
48
|
+
},
|
|
49
|
+
r.condition
|
|
50
|
+
), { where: c } = e;
|
|
51
|
+
let l = r.condition;
|
|
52
|
+
return (c == null ? void 0 : c.type) === "logical" && c.operator === "AND" ? l = { ...c, operands: [...c.operands, r.condition] } : c && (l = { type: "logical", operator: "AND", operands: [c, r.condition] }), { ...e, joins: u, where: l };
|
|
53
|
+
}
|
|
54
|
+
function le(t, e, r) {
|
|
55
|
+
const n = j(t, e, r, []), s = D(t, e, n), i = C(s, [], (a, o) => o.length === 0 ? a : j(t, a, r, o));
|
|
56
|
+
return D(t, s, i);
|
|
57
|
+
}
|
|
58
|
+
function $e(t, e) {
|
|
59
|
+
const r = [];
|
|
60
|
+
return C(t, [], (n, s) => {
|
|
61
|
+
const i = z(n, e);
|
|
62
|
+
return i && !Z(n.where, e.condition) && r.push({ path: s, table: i }), n;
|
|
63
|
+
}), r;
|
|
64
|
+
}
|
|
65
|
+
const qt = (t) => typeof t == "object" && t !== null && !(t instanceof Date), Y = {
|
|
66
|
+
/** A column: `expr.field('orders', 'status')`. */
|
|
67
|
+
field: (t, e) => ({ type: "field", table: t, column: e }),
|
|
68
|
+
/** A literal value, written into the SQL (as a bound parameter by
|
|
69
|
+
* astToParameterizedSql). `''` is marked as the empty text (see
|
|
70
|
+
* LiteralNode's `empty`), since code means it. */
|
|
71
|
+
value: (t) => t === "" ? { type: "literal", value: t, empty: !0 } : { type: "literal", value: t },
|
|
72
|
+
/** A named value the application supplies when generating the SQL
|
|
73
|
+
* (astToParameterizedSql's `values`). With `relative`, a date relative
|
|
74
|
+
* to when the query runs, which the application computes (issue 140):
|
|
75
|
+
* `expr.param('sale_from', { relative: { unit: 'day', offset: -30 } })`. */
|
|
76
|
+
param: (t, e = {}) => e.relative ? { type: "param", name: t, relative: e.relative } : { type: "param", name: t },
|
|
77
|
+
/** `left <operator> right`. */
|
|
78
|
+
compare: (t, e, r) => ({
|
|
79
|
+
type: "binary",
|
|
80
|
+
operator: e,
|
|
81
|
+
left: t,
|
|
82
|
+
right: r
|
|
83
|
+
}),
|
|
84
|
+
/** `operand IS NULL`. */
|
|
85
|
+
isNull: (t) => ({
|
|
86
|
+
type: "binary",
|
|
87
|
+
operator: "IS NULL",
|
|
88
|
+
left: t
|
|
89
|
+
}),
|
|
90
|
+
/** `operand IS NOT NULL`. */
|
|
91
|
+
isNotNull: (t) => ({
|
|
92
|
+
type: "binary",
|
|
93
|
+
operator: "IS NOT NULL",
|
|
94
|
+
left: t
|
|
95
|
+
}),
|
|
96
|
+
/** `left = right`. */
|
|
97
|
+
eq: (t, e) => ({
|
|
98
|
+
type: "binary",
|
|
99
|
+
operator: "=",
|
|
100
|
+
left: t,
|
|
101
|
+
right: e
|
|
102
|
+
}),
|
|
103
|
+
and: (...t) => ({
|
|
104
|
+
type: "logical",
|
|
105
|
+
operator: "AND",
|
|
106
|
+
operands: t
|
|
107
|
+
}),
|
|
108
|
+
or: (...t) => ({
|
|
109
|
+
type: "logical",
|
|
110
|
+
operator: "OR",
|
|
111
|
+
operands: t
|
|
112
|
+
}),
|
|
113
|
+
not: (t) => ({ type: "not", operand: t }),
|
|
114
|
+
/** A function call: `expr.fn('LOWER', expr.field('users', 'email'))`. */
|
|
115
|
+
fn: (t, ...e) => ({
|
|
116
|
+
type: "function",
|
|
117
|
+
name: t,
|
|
118
|
+
args: e
|
|
119
|
+
}),
|
|
120
|
+
/** Whether the outer row has related rows: `EXISTS (SELECT 1 FROM …
|
|
121
|
+
* WHERE <link> AND <where>)`. `link` matches them to the outer row;
|
|
122
|
+
* `where` names the subquery's own tables only. NOT EXISTS:
|
|
123
|
+
* `expr.not(expr.exists(…))`. */
|
|
124
|
+
exists: (t, e) => ({ type: "exists", query: { joins: [], ...t }, link: e }),
|
|
125
|
+
/** `left IN (…)`: the values (`[1, 2, 3]`, or a list of expressions
|
|
126
|
+
* such as params: `expr.list(…)`), or a sub query (`expr.subquery`).
|
|
127
|
+
* NOT IN: `expr.not(expr.in(…))`. */
|
|
128
|
+
in: (t, e) => ({
|
|
129
|
+
type: "binary",
|
|
130
|
+
operator: "IN",
|
|
131
|
+
left: t,
|
|
132
|
+
right: Array.isArray(e) ? Y.list(...e) : e
|
|
133
|
+
}),
|
|
134
|
+
/** `left BETWEEN low AND high` (issue 169), both bounds included: each a
|
|
135
|
+
* value or an expression (a param, a relative date). NOT BETWEEN:
|
|
136
|
+
* `expr.not(expr.between(…))`. */
|
|
137
|
+
between: (t, e, r) => ({
|
|
138
|
+
type: "binary",
|
|
139
|
+
operator: "BETWEEN",
|
|
140
|
+
left: t,
|
|
141
|
+
right: Y.list(e, r)
|
|
142
|
+
}),
|
|
143
|
+
/** IN's values: each a value (`expr.list(1, 2)`) or an expression
|
|
144
|
+
* (`expr.list(expr.param('a'), expr.param('b'))`). */
|
|
145
|
+
list: (...t) => ({
|
|
146
|
+
type: "list",
|
|
147
|
+
items: t.map((e) => qt(e) ? e : { type: "literal", value: e })
|
|
148
|
+
}),
|
|
149
|
+
/** A query of its own for IN to compare with, selecting `select` — one
|
|
150
|
+
* column. Its tables are its own: it sees nothing of the query around
|
|
151
|
+
* it. `expr.in(expr.field('customer', 'id'), expr.subquery({ from: {
|
|
152
|
+
* table: 'sale_transaction' }, select: expr.field('sale_transaction',
|
|
153
|
+
* 'customer_id') }))`. */
|
|
154
|
+
subquery: ({ select: t, ...e }) => ({
|
|
155
|
+
type: "subquery",
|
|
156
|
+
id: H(),
|
|
157
|
+
query: { joins: [], ...e, select: [{ id: H(), expression: t }] }
|
|
158
|
+
})
|
|
159
|
+
};
|
|
160
|
+
function Ee(t, e, r) {
|
|
161
|
+
return y(t, e, { type: "setBaseTable", table: r });
|
|
162
|
+
}
|
|
163
|
+
function Te(t, e, r, n = {}) {
|
|
164
|
+
const s = { type: "addJoin", table: r, ...n };
|
|
165
|
+
return y(t, e, s);
|
|
166
|
+
}
|
|
167
|
+
function ye(t, e, r) {
|
|
168
|
+
return y(t, e, { type: "removeJoin", joinId: r });
|
|
169
|
+
}
|
|
170
|
+
function Ut(t, e, r, n = {}) {
|
|
171
|
+
const s = Ot(e), i = V(r);
|
|
172
|
+
if (s && i.some((u) => u !== s.alias)) {
|
|
173
|
+
if (i.includes(s.alias))
|
|
174
|
+
throw new Error("addCondition: a condition names both the union and tables of its branches: add it in two parts");
|
|
175
|
+
return s.query.union.forEach((u) => {
|
|
176
|
+
const c = u.from.table, l = new Set(d(u).map(Q)), $ = ht(t, c), p = i.find((A) => !l.has(A) && !$.has(A));
|
|
177
|
+
if (p)
|
|
178
|
+
throw new Error(`addCondition: "${p}" can't be reached from "${c}", one of the union's tables: a condition must reach every branch`);
|
|
179
|
+
}), s.query.union.reduce((u, c, l) => Rt(
|
|
180
|
+
u,
|
|
181
|
+
[St(s.id, l)],
|
|
182
|
+
($) => Ut(t, $, r, n)
|
|
183
|
+
), e);
|
|
184
|
+
}
|
|
185
|
+
const a = n.operator ?? "AND", { where: o } = e;
|
|
186
|
+
let f;
|
|
187
|
+
return o ? o.type === "logical" && o.operator === a ? f = { ...o, operands: [...o.operands, r] } : f = { type: "logical", operator: a, operands: [o, r] } : f = r, D(t, e, It(t, e, f));
|
|
188
|
+
}
|
|
189
|
+
function pe(t, e, r, n) {
|
|
190
|
+
const s = y(t, e, { type: "addSelectItem" }), i = s.select[s.select.length - 1].id, a = y(t, s, {
|
|
191
|
+
type: "setSelectExpression",
|
|
192
|
+
selectId: i,
|
|
193
|
+
expression: r
|
|
194
|
+
}), o = n === void 0 ? a : y(t, a, { type: "setSelectAlias", selectId: i, alias: n });
|
|
195
|
+
return D(t, e, o);
|
|
196
|
+
}
|
|
197
|
+
function Ae(t, e, r) {
|
|
198
|
+
const n = y(t, e, { type: "addGroupByItem" }), { id: s } = n.groupBy[n.groupBy.length - 1], i = y(t, n, {
|
|
199
|
+
type: "setGroupByExpression",
|
|
200
|
+
groupById: s,
|
|
201
|
+
expression: r
|
|
202
|
+
});
|
|
203
|
+
return D(t, e, i);
|
|
204
|
+
}
|
|
205
|
+
function me(t, e, r) {
|
|
206
|
+
return D(t, e, bt(t, e, r));
|
|
207
|
+
}
|
|
208
|
+
function ge(t, e, r, n = "asc") {
|
|
209
|
+
const s = y(t, e, { type: "addOrderByItem" }), { id: i } = s.orderBy[s.orderBy.length - 1], a = y(t, s, {
|
|
210
|
+
type: "setOrderByExpression",
|
|
211
|
+
orderById: i,
|
|
212
|
+
expression: r
|
|
213
|
+
}), o = y(t, a, {
|
|
214
|
+
type: "setOrderByDirection",
|
|
215
|
+
orderById: i,
|
|
216
|
+
direction: n
|
|
217
|
+
});
|
|
218
|
+
return D(t, e, o);
|
|
219
|
+
}
|
|
220
|
+
function De(t, e, r) {
|
|
221
|
+
let n = e;
|
|
222
|
+
return "limit" in r && (n = y(t, n, { type: "setLimit", limit: r.limit })), "offset" in r && (n = y(t, n, { type: "setOffset", offset: r.offset })), n;
|
|
223
|
+
}
|
|
224
|
+
function Ne(t, e, r) {
|
|
225
|
+
return y(t, e, { type: "setDistinct", distinct: r });
|
|
226
|
+
}
|
|
227
|
+
function Oe(t) {
|
|
228
|
+
if (!pt(t)) throw new Error(`query "${t.id}" has no base table yet`);
|
|
229
|
+
return t;
|
|
230
|
+
}
|
|
231
|
+
const q = 4;
|
|
232
|
+
class h extends Error {
|
|
233
|
+
constructor(r, n) {
|
|
234
|
+
super(n);
|
|
235
|
+
b(this, "kind");
|
|
236
|
+
this.name = "QueryFormatError", this.kind = r;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
const I = (t) => typeof t == "object" && t !== null && !Array.isArray(t);
|
|
240
|
+
function vt(t) {
|
|
241
|
+
const e = [];
|
|
242
|
+
let r = 0;
|
|
243
|
+
for (; r < t.length; ) {
|
|
244
|
+
for (; t[r] === " "; ) r += 1;
|
|
245
|
+
if (t[r] === "'") {
|
|
246
|
+
let n = "";
|
|
247
|
+
for (r += 1; r < t.length && !(t[r] === "'" && t[r + 1] !== "'"); )
|
|
248
|
+
n += t[r], r += t[r] === "'" ? 2 : 1;
|
|
249
|
+
e.push(n);
|
|
250
|
+
const s = t.indexOf(",", r);
|
|
251
|
+
r = s === -1 ? t.length : s + 1;
|
|
252
|
+
} else {
|
|
253
|
+
const n = t.indexOf(",", r), s = t.slice(r, n === -1 ? void 0 : n).trim();
|
|
254
|
+
s !== "" && e.push(/^-?\d+(\.\d+)?$/.test(s) ? Number(s) : s), r = n === -1 ? t.length : n + 1;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return e;
|
|
258
|
+
}
|
|
259
|
+
function Bt(t) {
|
|
260
|
+
const e = (r) => {
|
|
261
|
+
if (Array.isArray(r)) return r.map(e);
|
|
262
|
+
if (!I(r)) return r;
|
|
263
|
+
const n = Object.fromEntries(Object.entries(r).map(([a, o]) => [a, e(o)])), { right: s } = n;
|
|
264
|
+
if (n.type !== "binary" || n.operator !== "IN" || !I(s) || s.type === "subquery" || s.type === "list") return n;
|
|
265
|
+
const i = s.type === "literal" && typeof s.value == "string" ? vt(s.value).map((a) => ({ type: "literal", value: a })) : [s];
|
|
266
|
+
return { ...n, right: { type: "list", items: i } };
|
|
267
|
+
};
|
|
268
|
+
return e(t);
|
|
269
|
+
}
|
|
270
|
+
const dt = (t) => t, _t = (t) => t, Ht = [Bt, dt, _t];
|
|
271
|
+
function jt(t, e, r) {
|
|
272
|
+
return r.slice(e - 1).reduce((n, s) => s(n), t);
|
|
273
|
+
}
|
|
274
|
+
function L(t, e) {
|
|
275
|
+
return Array.isArray(t) ? t.map((r) => L(r, e)) : I(t) ? t.type === "literal" ? e(t) : Object.fromEntries(Object.entries(t).map(([r, n]) => [r, L(n, e)])) : t;
|
|
276
|
+
}
|
|
277
|
+
function Yt(t) {
|
|
278
|
+
return t.value instanceof Date ? { ...t, value: t.value.toISOString(), valueType: "date" } : typeof t.value == "bigint" ? { ...t, value: String(t.value), valueType: "bigint" } : t;
|
|
279
|
+
}
|
|
280
|
+
function Pt(t) {
|
|
281
|
+
if (t.valueType === void 0) return t;
|
|
282
|
+
const { valueType: e, ...r } = t, n = String(r.value);
|
|
283
|
+
let s;
|
|
284
|
+
if (e === "date") {
|
|
285
|
+
if (s = new Date(n), Number.isNaN(s.getTime()))
|
|
286
|
+
throw new h("invalid", `not a date: ${JSON.stringify(n)}`);
|
|
287
|
+
} else if (e === "bigint" && /^-?\d+$/.test(n))
|
|
288
|
+
s = BigInt(n);
|
|
289
|
+
else {
|
|
290
|
+
const i = String(e);
|
|
291
|
+
throw new h("invalid", `can't read a ${i} from ${JSON.stringify(n)}`);
|
|
292
|
+
}
|
|
293
|
+
return { ...r, value: s };
|
|
294
|
+
}
|
|
295
|
+
function he(t) {
|
|
296
|
+
return {
|
|
297
|
+
version: q,
|
|
298
|
+
query: L(t, Yt)
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
function Re(t) {
|
|
302
|
+
if (!I(t))
|
|
303
|
+
throw new h("invalid", "a stored query is an object");
|
|
304
|
+
const { version: e } = t;
|
|
305
|
+
if (typeof e != "number" || !Number.isInteger(e) || e < 1)
|
|
306
|
+
throw new h("invalid", "a stored query has a whole-number version from 1");
|
|
307
|
+
if (e > q)
|
|
308
|
+
throw new h(
|
|
309
|
+
"newerVersion",
|
|
310
|
+
`stored in format ${e}; this version reads up to ${q}`
|
|
311
|
+
);
|
|
312
|
+
const r = jt(t.query, e, Ht);
|
|
313
|
+
if (!I(r) || typeof r.id != "string")
|
|
314
|
+
throw new h("invalid", "a stored query has a query with a string id");
|
|
315
|
+
return L(r, Pt);
|
|
316
|
+
}
|
|
317
|
+
function U(t, e) {
|
|
318
|
+
const r = new Map(e == null ? void 0 : e.tableOf), n = new Set(e == null ? void 0 : e.aliases), s = new Set(e == null ? void 0 : e.derived);
|
|
319
|
+
return t.forEach(({ table: i, alias: a }) => {
|
|
320
|
+
if (i === void 0) {
|
|
321
|
+
a && s.add(a);
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
if (r.set(i, i), a) {
|
|
325
|
+
r.set(a, i), n.add(a), s.delete(a);
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
n.delete(i), s.delete(i);
|
|
329
|
+
}), { tableOf: r, aliases: n, derived: s };
|
|
330
|
+
}
|
|
331
|
+
function Gt(t, e, r) {
|
|
332
|
+
const n = t.on && S(t.on, e, r);
|
|
333
|
+
return t.query ? { ...t, on: n, query: At(t.query, (s) => M(s, r)) } : t.shared !== void 0 ? { ...t, on: n } : { ...t, on: n, table: r(t.table) };
|
|
334
|
+
}
|
|
335
|
+
function S(t, e, r) {
|
|
336
|
+
if (t.type === "field") {
|
|
337
|
+
if (!t.table || e.derived.has(t.table)) return t;
|
|
338
|
+
const n = e.tableOf.get(t.table) ?? t.table;
|
|
339
|
+
return {
|
|
340
|
+
...t,
|
|
341
|
+
table: e.aliases.has(t.table) ? t.table : r(n),
|
|
342
|
+
...t.column ? { column: r(n, t.column) } : {}
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
if (t.type === "subquery") return { ...t, query: M(t.query, r) };
|
|
346
|
+
if (t.type === "exists") {
|
|
347
|
+
const { query: n } = t, s = [n.from, ...n.joins], i = U(s), a = U(s, e);
|
|
348
|
+
return {
|
|
349
|
+
...t,
|
|
350
|
+
link: S(t.link, a, r),
|
|
351
|
+
query: {
|
|
352
|
+
...n,
|
|
353
|
+
from: { ...n.from, table: r(n.from.table) },
|
|
354
|
+
joins: n.joins.map((o) => Gt(o, i, r)),
|
|
355
|
+
...n.where ? { where: S(n.where, i, r) } : {}
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
return X(t, (n) => S(n, e, r));
|
|
360
|
+
}
|
|
361
|
+
function M(t, e) {
|
|
362
|
+
const r = U([t.from, ...t.joins]), n = k(
|
|
363
|
+
t,
|
|
364
|
+
(s) => S(s, r, e),
|
|
365
|
+
(s) => M(s, e)
|
|
366
|
+
);
|
|
367
|
+
return {
|
|
368
|
+
...n,
|
|
369
|
+
from: n.from.table === void 0 ? n.from : { ...n.from, table: e(n.from.table) },
|
|
370
|
+
joins: n.joins.map((s) => s.table === void 0 ? s : { ...s, table: e(s.table) })
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
function v(t, e) {
|
|
374
|
+
if (Array.isArray(t)) return t.map((s) => v(s, e));
|
|
375
|
+
if (typeof t != "object" || t === null || t instanceof Date) return t;
|
|
376
|
+
const r = Object.fromEntries(Object.entries(t).map(
|
|
377
|
+
([s, i]) => [s, v(i, e)]
|
|
378
|
+
));
|
|
379
|
+
if (!("from" in r) || !Array.isArray(r.joins)) return r;
|
|
380
|
+
const n = (s) => s.table !== void 0 && !s.alias && e(s.table) !== s.table ? { ...s, alias: s.table } : s;
|
|
381
|
+
return {
|
|
382
|
+
...r,
|
|
383
|
+
from: n(r.from),
|
|
384
|
+
joins: r.joins.map(n)
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
function xt(t, e) {
|
|
388
|
+
return M(v(t, e), e);
|
|
389
|
+
}
|
|
390
|
+
const Qt = /^(\d{2}):(\d{2})(?::(\d{2}))?$/, P = /YYYY|MM|DD|HH|mm|ss/g, O = (t, e = 2) => String(t).padStart(e, "0");
|
|
391
|
+
function tt(t, e) {
|
|
392
|
+
let r;
|
|
393
|
+
const n = typeof t == "string" ? Qt.exec(t.trim()) : null;
|
|
394
|
+
if (n)
|
|
395
|
+
r = { HH: n[1], mm: n[2], ss: n[3] ?? "00" };
|
|
396
|
+
else {
|
|
397
|
+
const a = typeof t == "string" ? Ct(t, "dateTime") : t;
|
|
398
|
+
a instanceof Date && (r = {
|
|
399
|
+
YYYY: O(a.getUTCFullYear(), 4),
|
|
400
|
+
MM: O(a.getUTCMonth() + 1),
|
|
401
|
+
DD: O(a.getUTCDate()),
|
|
402
|
+
HH: O(a.getUTCHours()),
|
|
403
|
+
mm: O(a.getUTCMinutes()),
|
|
404
|
+
ss: O(a.getUTCSeconds())
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
if (!r) return t;
|
|
408
|
+
const s = r;
|
|
409
|
+
return (e.match(P) ?? []).some((a) => s[a] === void 0) ? t : e.replace(P, (a) => s[a]);
|
|
410
|
+
}
|
|
411
|
+
function et(t, e) {
|
|
412
|
+
const r = /* @__PURE__ */ new Map(), n = (s) => {
|
|
413
|
+
const i = /* @__PURE__ */ new Map();
|
|
414
|
+
d(s).forEach((u) => i.set(Q(u), u.table));
|
|
415
|
+
const a = (u) => {
|
|
416
|
+
if ((u == null ? void 0 : u.type) !== "field" || !u.table || !u.column) return;
|
|
417
|
+
const c = i.has(u.table) ? i.get(u.table) : u.table, l = c === void 0 ? void 0 : mt(e, c), $ = l == null ? void 0 : l.columns.find((p) => p.name === u.column);
|
|
418
|
+
return $ == null ? void 0 : $.format;
|
|
419
|
+
}, o = (u, c) => {
|
|
420
|
+
switch (u.type) {
|
|
421
|
+
case "literal":
|
|
422
|
+
return { ...u, value: tt(u.value, c) };
|
|
423
|
+
case "list":
|
|
424
|
+
return { ...u, items: u.items.map((l) => o(l, c)) };
|
|
425
|
+
case "param":
|
|
426
|
+
return r.has(u.name) || r.set(u.name, c), u;
|
|
427
|
+
default:
|
|
428
|
+
return u;
|
|
429
|
+
}
|
|
430
|
+
}, f = (u) => {
|
|
431
|
+
if (u.type === "subquery") return { ...u, query: n(u.query) };
|
|
432
|
+
const c = X(u, f);
|
|
433
|
+
if (c.type !== "binary" || !c.right) return c;
|
|
434
|
+
const l = a(c.left), $ = a(c.right);
|
|
435
|
+
return {
|
|
436
|
+
...c,
|
|
437
|
+
left: $ ? o(c.left, $) : c.left,
|
|
438
|
+
right: l ? o(c.right, l) : c.right
|
|
439
|
+
};
|
|
440
|
+
};
|
|
441
|
+
return k(s, f, (u) => n(u));
|
|
442
|
+
};
|
|
443
|
+
return { query: n(t), params: r };
|
|
444
|
+
}
|
|
445
|
+
const Se = ["mysql", "mssql", "postgres"], Ie = {
|
|
446
|
+
mysql: "MySQL",
|
|
447
|
+
mssql: "SQL Server",
|
|
448
|
+
postgres: "PostgreSQL"
|
|
449
|
+
};
|
|
450
|
+
function m(t, e) {
|
|
451
|
+
const r = t.replace(/[\uE000-\uE003]/g, "");
|
|
452
|
+
switch (e) {
|
|
453
|
+
case "mysql":
|
|
454
|
+
return `\`${r.replace(/`/g, "``")}\``;
|
|
455
|
+
case "mssql":
|
|
456
|
+
return `[${r.replace(/]/g, "]]")}]`;
|
|
457
|
+
case "postgres":
|
|
458
|
+
return `"${r.replace(/"/g, '""')}"`;
|
|
459
|
+
default:
|
|
460
|
+
return `"${r.replace(/"/g, '""')}"`;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
const kt = /^[A-Za-z_][A-Za-z0-9_]*$/, rt = (t) => `${t}`, Xt = /\uE000(\d+)\uE000/g;
|
|
464
|
+
let N;
|
|
465
|
+
function be(t, e) {
|
|
466
|
+
N = { ...t, writing: [] };
|
|
467
|
+
try {
|
|
468
|
+
return e();
|
|
469
|
+
} finally {
|
|
470
|
+
N = void 0;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
function nt(t, e, r) {
|
|
474
|
+
if (typeof t != "object" || t === null || t instanceof Date) return;
|
|
475
|
+
let n;
|
|
476
|
+
return Object.entries(t).some(([s, i]) => {
|
|
477
|
+
const a = [...r, Array.isArray(t) ? Number(s) : s];
|
|
478
|
+
return n = i === e ? a : nt(i, e, a), n !== void 0;
|
|
479
|
+
}), n;
|
|
480
|
+
}
|
|
481
|
+
function T(t, e) {
|
|
482
|
+
if (!N) return e();
|
|
483
|
+
const { writing: r, paths: n } = N, s = r[r.length - 1], i = typeof t == "string" ? [t] : nt(s.value, t, s.path);
|
|
484
|
+
if (!i) return e();
|
|
485
|
+
r.push({ value: t, path: i });
|
|
486
|
+
try {
|
|
487
|
+
return `${n.push(i) - 1}${e()}`;
|
|
488
|
+
} finally {
|
|
489
|
+
r.pop();
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
const Vt = {
|
|
493
|
+
mysql: {
|
|
494
|
+
text: "CHAR",
|
|
495
|
+
integer: "SIGNED",
|
|
496
|
+
number: "DECIMAL(38, 10)",
|
|
497
|
+
date: "DATE",
|
|
498
|
+
dateTime: "DATETIME",
|
|
499
|
+
time: "TIME",
|
|
500
|
+
boolean: "SIGNED"
|
|
501
|
+
},
|
|
502
|
+
mssql: {
|
|
503
|
+
text: "NVARCHAR(MAX)",
|
|
504
|
+
integer: "BIGINT",
|
|
505
|
+
number: "DECIMAL(38, 10)",
|
|
506
|
+
date: "DATE",
|
|
507
|
+
dateTime: "DATETIME2",
|
|
508
|
+
time: "TIME",
|
|
509
|
+
boolean: "BIT"
|
|
510
|
+
},
|
|
511
|
+
postgres: {
|
|
512
|
+
text: "text",
|
|
513
|
+
integer: "bigint",
|
|
514
|
+
number: "numeric",
|
|
515
|
+
date: "date",
|
|
516
|
+
dateTime: "timestamp",
|
|
517
|
+
time: "time",
|
|
518
|
+
boolean: "boolean"
|
|
519
|
+
}
|
|
520
|
+
};
|
|
521
|
+
function Wt(t, e) {
|
|
522
|
+
var n;
|
|
523
|
+
const r = (n = Vt[e]) == null ? void 0 : n[t];
|
|
524
|
+
return r ? e === "postgres" ? `NULL::${r}` : `CAST(NULL AS ${r})` : "NULL";
|
|
525
|
+
}
|
|
526
|
+
function st(t, e, r) {
|
|
527
|
+
if (t == null) return "NULL";
|
|
528
|
+
if (r)
|
|
529
|
+
return r.values.push(t), rt(r.values.length - 1);
|
|
530
|
+
if (typeof t == "boolean")
|
|
531
|
+
return e === "mssql" ? t ? "1" : "0" : t ? "TRUE" : "FALSE";
|
|
532
|
+
if (typeof t == "string") {
|
|
533
|
+
const n = N ? t.replace(/[\uE001-\uE003]/g, "") : t;
|
|
534
|
+
return n === "" ? "''" : `'${n.replace(/'/g, "''")}'`;
|
|
535
|
+
}
|
|
536
|
+
return typeof t == "number" || typeof t == "bigint" ? String(t) : t instanceof Date ? `'${J(t)}'` : String(t);
|
|
537
|
+
}
|
|
538
|
+
function Jt(t, e) {
|
|
539
|
+
switch (t) {
|
|
540
|
+
case "!=":
|
|
541
|
+
return e === "mssql" ? "<>" : "!=";
|
|
542
|
+
case "REGEXP":
|
|
543
|
+
if (e === "postgres") return "~";
|
|
544
|
+
if (e === "mssql") throw new Error("REGEXP isn't available in SQL Server");
|
|
545
|
+
return "REGEXP";
|
|
546
|
+
default:
|
|
547
|
+
return t;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
function g(t) {
|
|
551
|
+
return (t == null ? void 0 : t.type) === "literal" && typeof t.value == "string" && /^[A-Za-z_]+$/.test(t.value) ? t.value.toUpperCase() : "DAY";
|
|
552
|
+
}
|
|
553
|
+
const w = {
|
|
554
|
+
YEAR: "year",
|
|
555
|
+
QUARTER: "quarter",
|
|
556
|
+
MONTH: "month",
|
|
557
|
+
WEEK: "week",
|
|
558
|
+
DAY: "day",
|
|
559
|
+
HOUR: "hour",
|
|
560
|
+
MINUTE: "minute",
|
|
561
|
+
SECOND: "second",
|
|
562
|
+
MILLISECOND: "millisecond"
|
|
563
|
+
}, Kt = {
|
|
564
|
+
// ---- Date arithmetic ----
|
|
565
|
+
DATE_ADD: ([t, e], r, n, s) => {
|
|
566
|
+
var a;
|
|
567
|
+
const i = g(r[2]);
|
|
568
|
+
switch (n) {
|
|
569
|
+
case "mysql":
|
|
570
|
+
return i === "MILLISECOND" ? `DATE_ADD(${t}, INTERVAL (${e}) * 1000 MICROSECOND)` : `DATE_ADD(${t}, INTERVAL ${e} ${i})`;
|
|
571
|
+
case "postgres": {
|
|
572
|
+
const o = w[i] ?? i.toLowerCase();
|
|
573
|
+
return !s && ((a = r[1]) == null ? void 0 : a.type) === "literal" && typeof r[1].value == "number" ? `(${t} + INTERVAL '${r[1].value} ${o}')` : `(${t} + (${e} * INTERVAL '1 ${o}'))`;
|
|
574
|
+
}
|
|
575
|
+
case "mssql":
|
|
576
|
+
return `DATEADD(${i}, ${e}, ${t})`;
|
|
577
|
+
default:
|
|
578
|
+
return `DATE_ADD(${t}, ${e}, ${g(r[2])})`;
|
|
579
|
+
}
|
|
580
|
+
},
|
|
581
|
+
DATE_SUB: ([t, e], r, n, s) => {
|
|
582
|
+
var a;
|
|
583
|
+
const i = g(r[2]);
|
|
584
|
+
switch (n) {
|
|
585
|
+
case "mysql":
|
|
586
|
+
return i === "MILLISECOND" ? `DATE_SUB(${t}, INTERVAL (${e}) * 1000 MICROSECOND)` : `DATE_SUB(${t}, INTERVAL ${e} ${i})`;
|
|
587
|
+
case "postgres": {
|
|
588
|
+
const o = w[i] ?? i.toLowerCase();
|
|
589
|
+
return !s && ((a = r[1]) == null ? void 0 : a.type) === "literal" && typeof r[1].value == "number" ? `(${t} - INTERVAL '${r[1].value} ${o}')` : `(${t} - (${e} * INTERVAL '1 ${o}'))`;
|
|
590
|
+
}
|
|
591
|
+
case "mssql":
|
|
592
|
+
return `DATEADD(${i}, -(${e}), ${t})`;
|
|
593
|
+
default:
|
|
594
|
+
return `DATE_SUB(${t}, ${e}, ${g(r[2])})`;
|
|
595
|
+
}
|
|
596
|
+
},
|
|
597
|
+
// ---- Date diff ----
|
|
598
|
+
DATEDIFF: ([t, e], r, n) => {
|
|
599
|
+
const s = g(r[2]);
|
|
600
|
+
switch (n) {
|
|
601
|
+
case "mysql":
|
|
602
|
+
return `TIMESTAMPDIFF(${s}, ${t}, ${e})`;
|
|
603
|
+
case "mssql":
|
|
604
|
+
return `DATEDIFF(${s}, ${t}, ${e})`;
|
|
605
|
+
case "postgres":
|
|
606
|
+
switch (s) {
|
|
607
|
+
case "YEAR":
|
|
608
|
+
return `EXTRACT(YEAR FROM age(${e}, ${t}))::integer`;
|
|
609
|
+
case "QUARTER": {
|
|
610
|
+
const i = `EXTRACT(YEAR FROM age(${e}, ${t}))`, a = `EXTRACT(MONTH FROM age(${e}, ${t}))`;
|
|
611
|
+
return `FLOOR((${i} * 12 + ${a}) / 3)::integer`;
|
|
612
|
+
}
|
|
613
|
+
case "MONTH": {
|
|
614
|
+
const i = `EXTRACT(YEAR FROM age(${e}, ${t}))`, a = `EXTRACT(MONTH FROM age(${e}, ${t}))`;
|
|
615
|
+
return `(${i} * 12 + ${a})::integer`;
|
|
616
|
+
}
|
|
617
|
+
case "WEEK":
|
|
618
|
+
return `FLOOR((${e}::date - ${t}::date) / 7)`;
|
|
619
|
+
case "DAY":
|
|
620
|
+
return `(${e}::date - ${t}::date)`;
|
|
621
|
+
case "HOUR":
|
|
622
|
+
return `FLOOR(EXTRACT(EPOCH FROM (${e} - ${t})) / 3600)::integer`;
|
|
623
|
+
case "MINUTE":
|
|
624
|
+
return `FLOOR(EXTRACT(EPOCH FROM (${e} - ${t})) / 60)::integer`;
|
|
625
|
+
case "SECOND":
|
|
626
|
+
return `FLOOR(EXTRACT(EPOCH FROM (${e} - ${t})))::integer`;
|
|
627
|
+
case "MILLISECOND":
|
|
628
|
+
return `FLOOR(EXTRACT(EPOCH FROM (${e} - ${t})) * 1000)::integer`;
|
|
629
|
+
default:
|
|
630
|
+
return `/* unknown unit: ${s} */`;
|
|
631
|
+
}
|
|
632
|
+
default:
|
|
633
|
+
return `DATEDIFF(${t}, ${e}, ${s})`;
|
|
634
|
+
}
|
|
635
|
+
},
|
|
636
|
+
// ---- Date extract ----
|
|
637
|
+
EXTRACT: ([t], e, r) => {
|
|
638
|
+
const n = g(e[1]);
|
|
639
|
+
switch (r) {
|
|
640
|
+
case "mysql":
|
|
641
|
+
return n === "MILLISECOND" ? `FLOOR(MICROSECOND(${t}) / 1000)` : `EXTRACT(${n} FROM ${t})`;
|
|
642
|
+
case "postgres":
|
|
643
|
+
return n === "MILLISECOND" ? `EXTRACT(MILLISECONDS FROM ${t})::integer` : `EXTRACT(${n} FROM ${t})`;
|
|
644
|
+
case "mssql":
|
|
645
|
+
return `DATEPART(${n}, ${t})`;
|
|
646
|
+
default:
|
|
647
|
+
return `EXTRACT(${t}, ${n})`;
|
|
648
|
+
}
|
|
649
|
+
},
|
|
650
|
+
// ---- Date truncation ----
|
|
651
|
+
DATE_TRUNC: ([t], e, r) => {
|
|
652
|
+
const n = g(e[1]);
|
|
653
|
+
switch (r) {
|
|
654
|
+
case "mysql":
|
|
655
|
+
switch (n) {
|
|
656
|
+
case "YEAR":
|
|
657
|
+
return `DATE_FORMAT(${t}, '%Y-01-01')`;
|
|
658
|
+
case "QUARTER":
|
|
659
|
+
return `MAKEDATE(YEAR(${t}), 1) + INTERVAL (QUARTER(${t}) - 1) QUARTER`;
|
|
660
|
+
case "MONTH":
|
|
661
|
+
return `DATE_FORMAT(${t}, '%Y-%m-01')`;
|
|
662
|
+
case "WEEK":
|
|
663
|
+
return `DATE_SUB(DATE(${t}), INTERVAL WEEKDAY(${t}) DAY)`;
|
|
664
|
+
case "DAY":
|
|
665
|
+
return `DATE(${t})`;
|
|
666
|
+
case "HOUR":
|
|
667
|
+
return `DATE_FORMAT(${t}, '%Y-%m-%d %H:00:00')`;
|
|
668
|
+
case "MINUTE":
|
|
669
|
+
return `DATE_FORMAT(${t}, '%Y-%m-%d %H:%i:00')`;
|
|
670
|
+
case "SECOND":
|
|
671
|
+
return `DATE_FORMAT(${t}, '%Y-%m-%d %H:%i:%s')`;
|
|
672
|
+
case "MILLISECOND":
|
|
673
|
+
return `DATE_FORMAT(${t}, '%Y-%m-%d %H:%i:%s.000')`;
|
|
674
|
+
default:
|
|
675
|
+
return `/* unknown unit: ${n} */`;
|
|
676
|
+
}
|
|
677
|
+
case "postgres":
|
|
678
|
+
return `DATE_TRUNC('${w[n] ?? n.toLowerCase()}', ${t})`;
|
|
679
|
+
case "mssql":
|
|
680
|
+
switch (n) {
|
|
681
|
+
case "YEAR":
|
|
682
|
+
return `DATEFROMPARTS(YEAR(${t}), 1, 1)`;
|
|
683
|
+
case "QUARTER":
|
|
684
|
+
return `DATEFROMPARTS(YEAR(${t}), (DATEPART(QUARTER, ${t}) - 1) * 3 + 1, 1)`;
|
|
685
|
+
case "MONTH":
|
|
686
|
+
return `DATEFROMPARTS(YEAR(${t}), MONTH(${t}), 1)`;
|
|
687
|
+
case "WEEK": {
|
|
688
|
+
const s = `CAST(${t} AS DATE)`;
|
|
689
|
+
return `DATEADD(DAY, -(DATEDIFF(DAY, 0, ${s}) % 7), ${s})`;
|
|
690
|
+
}
|
|
691
|
+
case "DAY":
|
|
692
|
+
return `CAST(${t} AS DATE)`;
|
|
693
|
+
case "HOUR":
|
|
694
|
+
return `DATEADD(HOUR, DATEDIFF(HOUR, 0, ${t}), 0)`;
|
|
695
|
+
case "MINUTE":
|
|
696
|
+
return `DATEADD(MINUTE, DATEDIFF(MINUTE, 0, ${t}), 0)`;
|
|
697
|
+
case "SECOND":
|
|
698
|
+
return `DATEADD(SECOND, DATEDIFF(SECOND, '2000-01-01', ${t}), '2000-01-01')`;
|
|
699
|
+
case "MILLISECOND": {
|
|
700
|
+
const s = `CAST(${t} AS DATE)`;
|
|
701
|
+
return `DATEADD(MILLISECOND, DATEDIFF(MILLISECOND, ${s}, ${t}), ${s})`;
|
|
702
|
+
}
|
|
703
|
+
default:
|
|
704
|
+
return `/* unknown unit: ${n} */`;
|
|
705
|
+
}
|
|
706
|
+
default:
|
|
707
|
+
return `DATE_TRUNC(${t}, ${n})`;
|
|
708
|
+
}
|
|
709
|
+
},
|
|
710
|
+
// ---- Type cast ----
|
|
711
|
+
/**
|
|
712
|
+
* CAST(expr, type_name) — type_name is stored as a string literal.
|
|
713
|
+
* Renders as CAST(expr AS type_name) for all dialects.
|
|
714
|
+
*/
|
|
715
|
+
CAST: ([t], e) => {
|
|
716
|
+
const r = e[1], n = (r == null ? void 0 : r.type) === "literal" && typeof r.value == "string" && /^[A-Za-z][A-Za-z0-9_ ]*(\(\s*\d+\s*(,\s*\d+\s*)?\))?$/.test(r.value) ? r.value : "??";
|
|
717
|
+
return `CAST(${t} AS ${n})`;
|
|
718
|
+
},
|
|
719
|
+
// ---- String aggregation (issue 137) ----
|
|
720
|
+
// The separator goes into the SQL as a literal even in parameterized SQL:
|
|
721
|
+
// MySQL's SEPARATOR takes nothing else.
|
|
722
|
+
STRING_AGG: ([t = "??"], e, r, n, s, i) => {
|
|
723
|
+
const a = e[1] ?? { type: "literal", value: ", " }, o = a.type === "literal" && typeof a.value == "string" ? st(a.value, r) : "??", f = s.distinct ? "DISTINCT " : "", u = ($) => {
|
|
724
|
+
var p;
|
|
725
|
+
return (p = s.orderBy) != null && p.length ? `ORDER BY ${s.orderBy.map((A) => `${$(A)} ${A.direction === "desc" ? "DESC" : "ASC"}`).join(", ")}` : "";
|
|
726
|
+
}, c = ($) => i($.expression), l = ($) => $ ? ` ${$}` : "";
|
|
727
|
+
switch (r) {
|
|
728
|
+
case "mysql":
|
|
729
|
+
return `GROUP_CONCAT(${f}${t}${l(u(c))} SEPARATOR ${o})`;
|
|
730
|
+
case "postgres": {
|
|
731
|
+
const $ = `CAST(${t} AS TEXT)`, p = u((A) => {
|
|
732
|
+
if (!s.distinct) return c(A);
|
|
733
|
+
if (F(A.expression, e[0])) return $;
|
|
734
|
+
throw new Error("PostgreSQL orders STRING_AGG of different values by the value only");
|
|
735
|
+
});
|
|
736
|
+
return `STRING_AGG(${f}${$}, ${o}${l(p)})`;
|
|
737
|
+
}
|
|
738
|
+
case "mssql": {
|
|
739
|
+
if (s.distinct)
|
|
740
|
+
throw new Error("SQL Server's STRING_AGG can't join only different values");
|
|
741
|
+
const $ = u(c), p = $ ? ` WITHIN GROUP (${$})` : "";
|
|
742
|
+
return `STRING_AGG(CAST(${t} AS NVARCHAR(MAX)), ${o})${p}`;
|
|
743
|
+
}
|
|
744
|
+
default:
|
|
745
|
+
return `STRING_AGG(${f}${t}, ${o}${l(u(c))})`;
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
};
|
|
749
|
+
function R(t) {
|
|
750
|
+
return t.type === "binary" || t.type === "logical" || t.type === "if" || t.type === "case";
|
|
751
|
+
}
|
|
752
|
+
function E(t, e, r, n) {
|
|
753
|
+
return T(t, () => zt(t, e, r, n));
|
|
754
|
+
}
|
|
755
|
+
function G(t, e, r, n) {
|
|
756
|
+
const s = (i) => {
|
|
757
|
+
if (!i) return "??";
|
|
758
|
+
const a = E(i, e, r, n);
|
|
759
|
+
return R(i) ? `(${a})` : a;
|
|
760
|
+
};
|
|
761
|
+
return T(t, () => {
|
|
762
|
+
const [i, a] = t.type === "list" ? t.items : [t];
|
|
763
|
+
return `${s(i)} AND ${s(a)}`;
|
|
764
|
+
});
|
|
765
|
+
}
|
|
766
|
+
function zt(t, e, r, n) {
|
|
767
|
+
var a;
|
|
768
|
+
const s = " ".repeat(r), i = " ".repeat(r + 1);
|
|
769
|
+
switch (t.type) {
|
|
770
|
+
case "literal":
|
|
771
|
+
return t.value === "" && !t.empty ? "??" : t.value === null && t.as ? Wt(t.as, e) : st(t.value, e, n);
|
|
772
|
+
case "param": {
|
|
773
|
+
if (!n) return kt.test(t.name) ? `:${t.name}` : "??";
|
|
774
|
+
if (!n.named.has(t.name))
|
|
775
|
+
throw new Error(`no value for parameter "${t.name}"`);
|
|
776
|
+
return n.values.push(n.named.get(t.name)), rt(n.values.length - 1);
|
|
777
|
+
}
|
|
778
|
+
case "field": {
|
|
779
|
+
if (!t.column) return "??";
|
|
780
|
+
const o = m(t.column, e);
|
|
781
|
+
return t.table ? `${m(t.table, e)}.${o}` : o;
|
|
782
|
+
}
|
|
783
|
+
case "function": {
|
|
784
|
+
const o = t.args.map((l) => E(l, e, r, n)), f = Kt[t.name.toUpperCase()];
|
|
785
|
+
if (f)
|
|
786
|
+
return f(
|
|
787
|
+
o,
|
|
788
|
+
t.args,
|
|
789
|
+
e,
|
|
790
|
+
n !== void 0,
|
|
791
|
+
t,
|
|
792
|
+
(l) => E(l, e, r, n)
|
|
793
|
+
);
|
|
794
|
+
const u = /^[A-Za-z_][A-Za-z0-9_.]*$/.test(t.name) || t.name === "" ? t.name : "??";
|
|
795
|
+
if (o.length === 0 && u.toUpperCase() === "COUNT") return `${u}(*)`;
|
|
796
|
+
const c = t.distinct && o.length > 0 ? "DISTINCT " : "";
|
|
797
|
+
return `${u}(${c}${o.join(", ")})`;
|
|
798
|
+
}
|
|
799
|
+
case "binary": {
|
|
800
|
+
const o = t.operator ? Jt(t.operator, e) : "=", f = E(t.left, e, r, n), u = R(t.left) ? `(${f})` : f;
|
|
801
|
+
if (Mt(t.operator)) return `${u} ${o}`;
|
|
802
|
+
if (!t.right) return `${u} ${o} ??`;
|
|
803
|
+
if (o === "BETWEEN")
|
|
804
|
+
return `${u} BETWEEN ${G(t.right, e, r, n)}`;
|
|
805
|
+
if (o === "IN")
|
|
806
|
+
return t.right.type === "list" ? `${u} IN ${E(t.right, e, r, n)}` : `${u} IN (${E(t.right, e, r, n)})`;
|
|
807
|
+
const c = E(t.right, e, r, n), l = R(t.right) ? `(${c})` : c;
|
|
808
|
+
return `${u} ${o} ${l}`;
|
|
809
|
+
}
|
|
810
|
+
case "logical": {
|
|
811
|
+
if (t.operands.length === 0) return "/* empty */";
|
|
812
|
+
if (t.operands.length === 1)
|
|
813
|
+
return E(t.operands[0], e, r, n);
|
|
814
|
+
const o = `
|
|
815
|
+
${s}${t.operator} `;
|
|
816
|
+
return t.operands.map((f) => {
|
|
817
|
+
if (f.type === "logical") {
|
|
818
|
+
const u = E(f, e, r + 1, n);
|
|
819
|
+
return `(
|
|
820
|
+
${i}${u}
|
|
821
|
+
${s})`;
|
|
822
|
+
}
|
|
823
|
+
return E(f, e, r, n);
|
|
824
|
+
}).join(o);
|
|
825
|
+
}
|
|
826
|
+
case "not": {
|
|
827
|
+
const { operand: o } = t;
|
|
828
|
+
if (o.type === "binary" && o.operator === "IN" && ((a = o.right) == null ? void 0 : a.type) === "subquery") {
|
|
829
|
+
const f = E(o.left, e, r, n);
|
|
830
|
+
return `${R(o.left) ? `(${f})` : f} NOT IN (${E(o.right, e, r, n)})`;
|
|
831
|
+
}
|
|
832
|
+
if (o.type === "binary" && o.operator === "BETWEEN" && o.right) {
|
|
833
|
+
const f = E(o.left, e, r, n);
|
|
834
|
+
return `${R(o.left) ? `(${f})` : f} NOT BETWEEN ${G(o.right, e, r, n)}`;
|
|
835
|
+
}
|
|
836
|
+
if (o.type === "logical" || o.type === "not") {
|
|
837
|
+
const f = E(o, e, r + 1, n);
|
|
838
|
+
return `NOT (
|
|
839
|
+
${i}${f}
|
|
840
|
+
${s})`;
|
|
841
|
+
}
|
|
842
|
+
return `NOT ${E(o, e, r, n)}`;
|
|
843
|
+
}
|
|
844
|
+
case "if": {
|
|
845
|
+
const o = t.condition ? E(t.condition, e, r, n) : "??", f = t.then ? E(t.then, e, r, n) : "??", u = t.else ? E(t.else, e, r, n) : "NULL";
|
|
846
|
+
return e === "mysql" ? `IF(${o}, ${f}, ${u})` : e === "mssql" ? `IIF(${o}, ${f}, ${u})` : `CASE WHEN ${o} THEN ${f} ELSE ${u} END`;
|
|
847
|
+
}
|
|
848
|
+
case "case": {
|
|
849
|
+
const o = t.subject ? ` ${E(t.subject, e, r, n)}` : "", f = t.branches.map((c) => {
|
|
850
|
+
const l = E(c.when, e, r + 1, n), $ = E(c.then, e, r + 1, n);
|
|
851
|
+
return `${i}WHEN ${l} THEN ${$}`;
|
|
852
|
+
}).join(`
|
|
853
|
+
`), u = t.else ? `
|
|
854
|
+
${i}ELSE ${E(t.else, e, r + 1, n)}` : "";
|
|
855
|
+
return `CASE${o}
|
|
856
|
+
${f}${u}
|
|
857
|
+
${s}END`;
|
|
858
|
+
}
|
|
859
|
+
// EXISTS (SELECT 1 FROM … WHERE <link> AND <where>): the subquery written
|
|
860
|
+
// in place, one level in, so its params fall in reading order.
|
|
861
|
+
case "exists": {
|
|
862
|
+
const { query: o, link: f } = t, u = ($) => $.type === "logical" && $.operator === "AND" ? $.operands : [$], c = o.where ? { type: "logical", operator: "AND", operands: [...u(f), ...u(o.where)] } : f;
|
|
863
|
+
return `EXISTS (
|
|
864
|
+
${[
|
|
865
|
+
"SELECT 1",
|
|
866
|
+
// The clauses below write their expressions with formatNode.
|
|
867
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
868
|
+
ct(o.from, e),
|
|
869
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
870
|
+
...o.joins.map(($) => ft($, e, n)),
|
|
871
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
872
|
+
it(c, e, n)
|
|
873
|
+
].join(`
|
|
874
|
+
`).replace(/^/gm, i)}
|
|
875
|
+
${s})`;
|
|
876
|
+
}
|
|
877
|
+
// IN's right side (issue 122): its query, on lines of its own, inside the
|
|
878
|
+
// parentheses IN writes around it.
|
|
879
|
+
// An empty list is unfinished, like a value not filled in.
|
|
880
|
+
case "list":
|
|
881
|
+
return `(${t.items.map((o) => E(o, e, r, n)).join(", ") || "??"})`;
|
|
882
|
+
case "subquery":
|
|
883
|
+
return `
|
|
884
|
+
${_(t.query, e, n).join(`
|
|
885
|
+
`).replace(/^/gm, i)}
|
|
886
|
+
${s}`;
|
|
887
|
+
default:
|
|
888
|
+
return "/* unknown */";
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
function it(t, e, r) {
|
|
892
|
+
return `WHERE
|
|
893
|
+
${E(t, e, 1, r)}`;
|
|
894
|
+
}
|
|
895
|
+
const Zt = {
|
|
896
|
+
inner: "INNER JOIN",
|
|
897
|
+
left: "LEFT JOIN",
|
|
898
|
+
right: "RIGHT JOIN",
|
|
899
|
+
full: "FULL JOIN",
|
|
900
|
+
cross: "CROSS JOIN"
|
|
901
|
+
};
|
|
902
|
+
let ot = /* @__PURE__ */ new Map();
|
|
903
|
+
function at(t, e, r) {
|
|
904
|
+
return t.shared !== void 0 ? m(ot.get(t.shared) ?? "??", e) : t.query ? `(
|
|
905
|
+
${ut(t.query, e, r).replace(/^/gm, " ")}
|
|
906
|
+
)` : m(t.table, e);
|
|
907
|
+
}
|
|
908
|
+
function ut(t, e, r) {
|
|
909
|
+
return gt(t).map((n) => _(n, e, r).join(`
|
|
910
|
+
`)).join(Dt(t) && !t.all ? `
|
|
911
|
+
UNION
|
|
912
|
+
` : `
|
|
913
|
+
UNION ALL
|
|
914
|
+
`);
|
|
915
|
+
}
|
|
916
|
+
function te(t, e, r) {
|
|
917
|
+
return t.length === 0 ? [] : [`WITH
|
|
918
|
+
${t.map((s) => T(s, () => {
|
|
919
|
+
const i = ut(s.query, e, r).replace(/^/gm, " ");
|
|
920
|
+
return ` ${m(s.name, e)} AS (
|
|
921
|
+
${i}
|
|
922
|
+
)`;
|
|
923
|
+
})).join(`,
|
|
924
|
+
`)}`];
|
|
925
|
+
}
|
|
926
|
+
function ct(t, e, r) {
|
|
927
|
+
const n = t.alias ? ` AS ${m(t.alias, e)}` : "";
|
|
928
|
+
return `FROM ${T(t, () => `${at(t, e, r)}${n}`)}`;
|
|
929
|
+
}
|
|
930
|
+
function ft(t, e, r) {
|
|
931
|
+
return T(t, () => {
|
|
932
|
+
const n = at(t, e, r), s = t.alias ? ` AS ${m(t.alias, e)}` : "";
|
|
933
|
+
if (t.joinType === "cross") return `CROSS JOIN ${n}${s}`;
|
|
934
|
+
const i = t.on ? E(t.on, e, 1, r) : "/* unconfigured */";
|
|
935
|
+
return `${Zt[t.joinType]} ${n}${s}
|
|
936
|
+
ON ${i}`;
|
|
937
|
+
});
|
|
938
|
+
}
|
|
939
|
+
function ee(t, e, r, n = "SELECT") {
|
|
940
|
+
if (t.length === 0) return `${n} ${T(t, () => "*")}`;
|
|
941
|
+
const s = t.map((i) => T(i, () => {
|
|
942
|
+
const a = E(i.expression, e, 1, r), o = i.alias ? ` AS ${m(i.alias, e)}` : "";
|
|
943
|
+
return `${a}${o}`;
|
|
944
|
+
}));
|
|
945
|
+
return `${n}
|
|
946
|
+
${s.join(`,
|
|
947
|
+
`)}`;
|
|
948
|
+
}
|
|
949
|
+
function re(t, e, r) {
|
|
950
|
+
return `GROUP BY
|
|
951
|
+
${t.map((s) => T(
|
|
952
|
+
s,
|
|
953
|
+
() => E(s.expression, e, 1, r)
|
|
954
|
+
)).join(`,
|
|
955
|
+
`)}`;
|
|
956
|
+
}
|
|
957
|
+
function ne(t, e, r) {
|
|
958
|
+
return `HAVING
|
|
959
|
+
${E(t, e, 1, r)}`;
|
|
960
|
+
}
|
|
961
|
+
function se(t, e, r, n) {
|
|
962
|
+
return `ORDER BY
|
|
963
|
+
${t.map((i) => T(i, () => `${E(n(i.expression), e, 1, r)} ${i.direction === "desc" ? "DESC" : "ASC"}`)).join(`,
|
|
964
|
+
`)}`;
|
|
965
|
+
}
|
|
966
|
+
const B = (t) => t !== void 0 && Number.isInteger(t) && t >= 0 ? t : void 0;
|
|
967
|
+
function lt(t, e) {
|
|
968
|
+
const r = e.resolveName ? xt(t, e.resolveName) : t;
|
|
969
|
+
return e.defaultLimit !== void 0 && B(r.limit) === void 0 ? { ...r, limit: e.defaultLimit } : r;
|
|
970
|
+
}
|
|
971
|
+
const ie = "18446744073709551615";
|
|
972
|
+
function _(t, e, r, n = `SELECT${t.distinct ? " DISTINCT" : ""}`) {
|
|
973
|
+
const s = [
|
|
974
|
+
ee(t.select, e, r, n),
|
|
975
|
+
ct(t.from, e, r),
|
|
976
|
+
...t.joins.map((a) => ft(a, e, r))
|
|
977
|
+
];
|
|
978
|
+
t.where && s.push(it(t.where, e, r));
|
|
979
|
+
const i = t.groupBy ?? [];
|
|
980
|
+
if (i.length > 0 && s.push(re(i, e, r)), t.having) {
|
|
981
|
+
const a = e === "mysql" ? W(t.having, t) : t.having;
|
|
982
|
+
s.push(ne(a, e, r));
|
|
983
|
+
}
|
|
984
|
+
return s;
|
|
985
|
+
}
|
|
986
|
+
function $t(t, e, r) {
|
|
987
|
+
N && (N.writing = [{ value: t, path: [] }]), ot = new Map((t.shared ?? []).map((c) => [c.id, c.name]));
|
|
988
|
+
const n = te(t.shared ?? [], e, r), s = B(t.limit), i = B(t.offset) || void 0, a = t.orderBy ?? [], o = e === "mssql" && s !== void 0 && i === void 0, f = `SELECT${t.distinct ? " DISTINCT" : ""}${o ? ` ${T("limit", () => `TOP (${s})`)}` : ""}`, u = _(t, e, r, f);
|
|
989
|
+
if (a.length > 0 && u.push(se(a, e, r, (c) => e === "mysql" ? W(c, t) : c)), e === "mssql") {
|
|
990
|
+
if (i !== void 0) {
|
|
991
|
+
a.length === 0 && u.push("ORDER BY (SELECT NULL)");
|
|
992
|
+
const c = s !== void 0 ? ` ${T("limit", () => `FETCH NEXT ${s} ROWS ONLY`)}` : "";
|
|
993
|
+
u.push(`${T("offset", () => `OFFSET ${i} ROWS`)}${c}`);
|
|
994
|
+
}
|
|
995
|
+
} else if (s !== void 0 || i !== void 0) {
|
|
996
|
+
const c = s !== void 0 || e === "mysql" ? T(s !== void 0 ? "limit" : {}, () => `LIMIT ${s ?? ie}`) : "", l = i !== void 0 ? T("offset", () => `OFFSET ${i}`) : "";
|
|
997
|
+
u.push([c, l].filter(Boolean).join(" "));
|
|
998
|
+
}
|
|
999
|
+
return [...n, ...u].join(`
|
|
1000
|
+
`);
|
|
1001
|
+
}
|
|
1002
|
+
function Ce(t, e, r = {}) {
|
|
1003
|
+
const n = r.values ? Lt(t, r.values) : t, s = r.schema ? et(n, r.schema).query : n;
|
|
1004
|
+
return $t(lt(s, r), e);
|
|
1005
|
+
}
|
|
1006
|
+
function oe(t, e) {
|
|
1007
|
+
switch (t) {
|
|
1008
|
+
case "mysql":
|
|
1009
|
+
return "?";
|
|
1010
|
+
case "mssql":
|
|
1011
|
+
return `@p${e}`;
|
|
1012
|
+
case "postgres":
|
|
1013
|
+
return `$${e}`;
|
|
1014
|
+
default:
|
|
1015
|
+
return "?";
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
function Le(t, e, r = {}) {
|
|
1019
|
+
const n = r.schema ? et(t, r.schema) : { query: t, params: /* @__PURE__ */ new Map() }, s = {
|
|
1020
|
+
values: [],
|
|
1021
|
+
named: new Map(Object.entries(r.values ?? {}).map(([u, c]) => {
|
|
1022
|
+
const l = n.params.get(u);
|
|
1023
|
+
return [u, l ? tt(c, l) : c];
|
|
1024
|
+
}))
|
|
1025
|
+
}, i = lt(n.query, r), a = $t(i, e, s), o = [];
|
|
1026
|
+
return { sql: a.replace(Xt, (u, c) => {
|
|
1027
|
+
const l = s.values[Number(c)];
|
|
1028
|
+
return o.push(l instanceof Date ? J(l) : l), oe(e, o.length);
|
|
1029
|
+
}), params: o };
|
|
1030
|
+
}
|
|
1031
|
+
export {
|
|
1032
|
+
Se as D,
|
|
1033
|
+
q as Q,
|
|
1034
|
+
wt as S,
|
|
1035
|
+
Le as a,
|
|
1036
|
+
Ce as b,
|
|
1037
|
+
Ut as c,
|
|
1038
|
+
Ae as d,
|
|
1039
|
+
Te as e,
|
|
1040
|
+
ge as f,
|
|
1041
|
+
pe as g,
|
|
1042
|
+
Y as h,
|
|
1043
|
+
Oe as i,
|
|
1044
|
+
Ee as j,
|
|
1045
|
+
Ne as k,
|
|
1046
|
+
me as l,
|
|
1047
|
+
De as m,
|
|
1048
|
+
h as n,
|
|
1049
|
+
Re as o,
|
|
1050
|
+
he as p,
|
|
1051
|
+
Ie as q,
|
|
1052
|
+
ye as r,
|
|
1053
|
+
le as s,
|
|
1054
|
+
$e as u,
|
|
1055
|
+
be as w
|
|
1056
|
+
};
|