deno.land / x / pg_mem@2.8.1 / execution / select.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import { _IStatementExecutor, _Transaction, StatementResult, _IStatement, _ISelection, NotSupported, QueryError, asSelectable, nil, OnStatementExecuted, _ISchema } from '../interfaces-private.ts';import { WithStatementBinding, SelectStatement, SelectFromUnion, WithStatement, ValuesStatement, SelectFromStatement, QNameMapped, Name, SelectedColumn, Expr, OrderByStatement } from 'https://deno.land/x/pgsql_ast_parser@12.0.1/mod.ts';import { Deletion } from './records-mutations/deletion.ts';import { Update } from './records-mutations/update.ts';import { Insert } from './records-mutations/insert.ts';import { ValuesTable } from '../schema/values-table.ts';import { ignore, suggestColumnName, notNil, modifyIfNecessary } from '../utils.ts';import { JoinSelection } from '../transforms/join.ts';import { cleanResults } from './clean-results.ts';import { MutationDataSourceBase } from './records-mutations/mutation-base.ts';import { locOf } from './exec-utils.ts';import { buildCtx, withBindingScope } from '../parser/context.ts';import { buildValue } from '../parser/expression-builder.ts';import { ArrayType } from '../datatypes/index.ts';import { RecordType } from '../datatypes/t-record.ts';import { FunctionCallTable } from '../schema/function-call-table.ts';



export function buildValues(p: ValuesStatement, acceptDefault?: boolean): _ISelection { const ret = new ValuesTable('', p.values, null, acceptDefault); return ret.selection;}

function buildWithable(p: WithStatementBinding): _ISelection { switch (p.type) { case 'select': case 'union': case 'union all': case 'with': case 'with recursive': case 'values': return buildSelect(p); case 'delete': return new Deletion(p); case 'update': return new Update(p); case 'insert': return new Insert(p); default: throw NotSupported.never(p); }}
export function buildSelect(p: SelectStatement): _ISelection { switch (p.type) { case 'union': case 'union all': return buildUnion(p); case 'with': return buildWith(p, false); case 'select': return buildRawSelect(p); case 'values': return buildValues(p); case 'with recursive': throw new NotSupported('recursirve with statements not implemented by pg-mem'); default: throw NotSupported.never(p); }}

function buildUnion(p: SelectFromUnion): _ISelection { const left = buildSelect(p.left); const right = buildSelect(p.right); const ret = left.union(right); if (p.type === 'union all') { return ret; } return ret.distinct();}
export function buildWith(p: WithStatement, topLevel: boolean): _ISelection { return withBindingScope(() => { const { setTempBinding } = buildCtx(); // declare temp bindings for (const { alias, statement } of p.bind) { const prepared = (topLevel ? buildWithable(statement) : buildSelect(checkReadonlyWithable(statement))) .setAlias(alias.name); setTempBinding(alias.name, prepared); } return buildSelect(checkReadonlyWithable(p.in)); })}

function buildRawSelectSubject(p: SelectFromStatement): _ISelection | nil { // compute data source let sel: _ISelection | undefined = undefined; for (const from of p.from ?? []) { // find what to select let newT: _ISelection; switch (from.type) { case 'table': newT = getSelectable(from.name); break; case 'statement': newT = mapColumns(from.alias , buildSelect(from.statement) , from.columnNames , true) .setAlias(from.alias); break; case 'call': const fnName = from.alias?.name ?? from.function?.name; const fromValue = buildValue(from); if (ArrayType.matches(fromValue.type) && RecordType.matches(fromValue.type.of)) { // if the function returns an array of records (= "a table"), then lets use it as a table const cols = fromValue.type.of.columns; newT = new FunctionCallTable(cols, fromValue); } else { // if the function returns a single value, then lets transform this into a table // nb: the function call will be re-built in here, but its OK (coz' of build cache) newT = new ValuesTable(fnName, [[from]], [fnName]) .setAlias(from.alias?.name ?? suggestColumnName(from) ?? ''); } break; default: throw NotSupported.never(from); }
if (!sel) { // first table to be selected sel = newT; continue; }
switch (from.join?.type) { case 'INNER JOIN': sel = new JoinSelection(sel, newT, from.join!, true); break; case 'LEFT JOIN': sel = new JoinSelection(sel, newT, from.join!, false); break; case 'RIGHT JOIN': sel = new JoinSelection(newT, sel, from.join!, false); break; case null: case undefined: // cross join (equivalent to INNER JOIN ON TRUE) sel = new JoinSelection(sel, newT, { type: 'INNER JOIN', on: { type: 'boolean', value: true } }, true); break; default: throw new NotSupported('Join type not supported ' + (from.join?.type ?? '<no join specified>')); } } return sel;}

function buildRawSelect(p: SelectFromStatement): _ISelection { const distinct = !p.distinct || p.distinct === 'all' ? null : p.distinct;
// ignore "for update" clause (not useful in non-concurrent environements) ignore(p.for);
let sel = buildRawSelectSubject(p);

// filter & select sel = sel ?? buildCtx().schema.dualTable.selection; sel = sel.filter(p.where);
// postgres helps users: you can use group-by & order-by on aliases. // ... but you cant use aliases in a computation (only in simple order by statements) // this hack reproduces this behaviour const aliases = new Map(notNil(p.columns?.filter(c => !!c.alias?.name)).map(c => [c.alias!.name, c.expr])); const orderBy = modifyIfNecessary(p.orderBy ?? [], o => { const by = o.by.type === 'ref' && !o.by.table && aliases.get(o.by.name); return by ? { ...o, by } : null; });

if (p.groupBy) { const groupBy = modifyIfNecessary(p.groupBy ?? [], o => { const group = o.type === 'ref' && !o.table && !sel?.getColumn(o.name, true) && aliases.get(o.name); return group || null; }); sel = sel.groupBy(groupBy); }
// order selection sel = sel.orderBy(orderBy);
// when not grouping by, distinct is handled before // selection => can distinct on non selected values if (!p.groupBy && Array.isArray(p.distinct)) { sel = sel.distinct(p.distinct); }
// select columns sel = sel.select(p.columns!);

// when grouping by, distinct is handled after selection // => can distinct on key, or selected if (p.groupBy && Array.isArray(p.distinct)) { sel = sel.distinct(p.distinct); }
// handle 'distinct' on result set if (distinct === 'distinct') { sel = sel.distinct(); }
if (p.limit) { sel = sel.limit(p.limit); } return sel;}
function getSelectable(name: QNameMapped): _ISelection { const { schema, getTempBinding } = buildCtx(); const temp = !name.schema && getTempBinding(name.name);
let ret = temp || asSelectable(schema.getObject(name)).selection; ret = mapColumns(name.name, ret, name.columnNames, false);
if (name.alias) { ret = ret.setAlias(name.alias); } return ret;}
function mapColumns(tableName: string, sel: _ISelection, columnNames: Name[] | nil, appendNonMapped: boolean) { if (!columnNames?.length) { return sel; } if (columnNames.length > sel.columns.length) { throw new QueryError(`table "${tableName}" has ${sel.columns.length} columns available but ${columnNames.length} columns specified`, '42P10') }
const mapped = new Set<string>(columnNames.map(x => x.name)); const cols = sel.columns.map<SelectedColumn>((col, i) => ({ expr: { type: 'ref', name: col.id!, }, // when realiasing table columns, columns which have not been mapped // must not be removed // see ut "can map column names" alias: columnNames[i] ?? { name: mapped.has(sel.columns[i].id!) ? `${sel.columns[i].id!}1` : sel.columns[i].id!, }, }));
return sel.select( cols )}

export class SelectExec implements _IStatementExecutor { readonly selection: _ISelection;
constructor(private statement: _IStatement, private p: WithStatementBinding) { // a bit of a special case for top level withs. this.selection = p.type === 'with' ? buildWith(p, true) : buildWithable(p); }
get schema() { return this.statement.schema; }


execute(t: _Transaction): StatementResult { const rows = cleanResults([...this.selection.enumerate(t)]); let unnamedFields = 0; const nextDefaultFieldName = () => { const unnamedField = `column${unnamedFields || ''}`; unnamedFields += 1; return unnamedField; } return { result: { rows, rowCount: t.getTransient(MutationDataSourceBase.affectedRows) ?? rows.length, command: this.p.type.toUpperCase(), fields: this.selection.columns.map( c => ({ name: c.id ?? nextDefaultFieldName(), type: c.type.primary, [TYPE_SYMBOL]: c.type, }) ), location: locOf(this.p), }, state: t, }; }}
export const TYPE_SYMBOL = Symbol('type');

function checkReadonlyWithable(st: WithStatementBinding) { switch (st.type) { case 'delete': case 'insert': case 'update': throw new NotSupported(`"WITH" nested statement with query type '${st.type}'`); } return st;}
pg_mem

Version Info

Tagged at
4 months ago