deno.land / x / pg_mem@2.8.1 / schema / pg-catalog / binary-operators.ts

binary-operators.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
import { _ISchema, QueryError } from '../../interfaces-private.ts';import { numbers, isInteger, dateTypes, Types, numberPriorities } from '../../datatypes/index.ts';import { intervalToSec, queryJson } from '../../utils.ts';import moment from 'https://deno.land/x/momentjs@2.29.1-deno/mod.ts';
export function registerCommonOperators(schema: _ISchema) { registerNumericOperators(schema); registerDatetimeOperators(schema); registerJsonOperators(schema); registerTextOperators(schema);}
function* numberPairs() { for (const a of numbers) { for (const b of numbers) { yield [a, b, numberPriorities[a] < numberPriorities[b] ? b : a] as const; } }}
function registerNumericOperators(schema: _ISchema) { // ======= "+ - * /" on numeric types ======= for (const [left, right, returns] of numberPairs()) { schema.registerOperator({ operator: '+', commutative: true, left, right, returns, implementation: (a, b) => a + b, }); } for (const [left, right, returns] of numberPairs()) { schema.registerOperator({ operator: '-', commutative: true, left, right, returns, implementation: (a, b) => a - b, }); }
for (const [left, right, returns] of numberPairs()) { schema.registerOperator({ operator: '*', commutative: true, left, right, returns, implementation: (a, b) => a * b, }); }
for (const [left, right, returns] of numberPairs()) { schema.registerOperator({ operator: '/', commutative: false, left, right, returns, implementation: isInteger(returns) ? ((a, b) => Math.trunc(a / b)) : ((a, b) => a / b), }); }}

function registerDatetimeOperators(schema: _ISchema) { // ======= date/time "+ -" timestamp ======= for (const dt of dateTypes) { for (const [operator, f] of [['+', 1], ['-', -1]] as const) { schema.registerOperator({ operator, commutative: operator === '+', left: dt, right: Types.interval, returns: dt, implementation: (a, b) => moment(a).add(f * intervalToSec(b), 'seconds').toDate(), }); } }
// ==== date "+ -" integer (add days.. only works on dates, not timestamps) for (const [operator, f] of [['+', 1], ['-', -1]] as const) { schema.registerOperator({ operator, commutative: operator === '+', left: Types.date, right: Types.integer, returns: Types.date, implementation: (a, b) => moment(a).add(f * b, 'days').toDate(), }); }}


function registerJsonOperators(schema: _ISchema) { // ======= "json @> json" query operator schema.registerOperator({ operator: '@>', left: Types.jsonb, right: Types.jsonb, returns: Types.bool, implementation: (a, b) => queryJson(b, a), });
// ======= "json - text" (remove key) schema.registerOperator({ operator: '-', left: Types.jsonb, right: Types.text(), returns: Types.jsonb, implementation: (a, b) => { if (Array.isArray(a)) { return a.filter(x => x !== b); } if (typeof a === 'object') { const ret = { ...a }; delete ret[b]; return ret; } throw new QueryError('cannot delete from scalar', '22023'); }, });
// ======= "json - int" (remove index) schema.registerOperator({ operator: '-', left: Types.jsonb, right: Types.integer, returns: Types.jsonb, implementation: (a, b) => { if (Array.isArray(a)) { const ret = [...a]; ret.splice(b, 1); return ret; } if (typeof a === 'object') { throw new QueryError('cannot delete from object using integer index', '22023'); } throw new QueryError('cannot delete from scalar', '22023'); }, })}

function registerTextOperators(schema: _ISchema) { // ======== "text || text" (concat text operator) schema.registerOperator({ operator: '||', left: Types.text(), right: Types.text(), returns: Types.text(), implementation: (a, b) => a + b, })}
pg_mem

Version Info

Tagged at
a year ago