deno.land / x / pg_mem@2.8.1 / schema / sequence.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
import { AlterSequenceChange, CreateSequenceOptions } from 'https://deno.land/x/pgsql_ast_parser@12.0.1/mod.ts';import { combineSubs, ignore, nullIsh } from '../utils.ts';import { NotSupported, asTable, _ISchema, _ISequence, _IType, _Transaction, RegClass, Reg } from '../interfaces-private.ts';import { ISubscription, nil, QueryError } from '../interfaces.ts';import { Types } from '../datatypes/index.ts';
interface SeqData { currval: number | undefined; nextval: number;}
export class Sequence implements _ISequence {
get type(): 'sequence' { return 'sequence'; }
private symbol = Symbol(); private owner?: ISubscription; private cfg: { start?: number; max?: number; min?: number; cycle?: boolean; inc?: number; dataType?: _IType; } = {};
readonly reg: Reg;

get cycle() { return this.cfg.cycle ?? false; }
get dataType() { return this.cfg.dataType ?? Types.integer; }
get inc() { return this.cfg.inc ?? 1; }

constructor(public name: string, readonly ownerSchema: _ISchema) { this.reg = ownerSchema._reg_register(this); }

get start() { return this.cfg.start ?? (this.inc > 0 ? this.min : this.max); }
get max() { return this.cfg.max ?? (this.inc > 0 ? Number.MAX_SAFE_INTEGER - 1 : -1); }
get min() { return this.cfg.min ?? (this.inc > 0 ? 1 : Number.MIN_SAFE_INTEGER + 1); }
alter(t: _Transaction, opts: CreateSequenceOptions | AlterSequenceChange | nil): this { if (!opts) { return this; } const oldCfg = { ...this.cfg }; try { if (!('type' in opts)) { return this.alterOpts(t, opts); } switch (opts.type) { case 'set options': this.alterOpts(t, opts); if (opts.restart === true || typeof opts.restart === 'number') { if (typeof opts.restart === 'number') { if (opts.restart < this.min) { throw new QueryError(`RESTART value (${opts.restart}) cannot be less than MINVALUE (${this.min})`, '22023'); } this.cfg.start = opts.restart; } const data: SeqData = { currval: t.get<SeqData>(this.symbol)?.currval, nextval: this.start, } t.set(this.symbol, data); } return this; case 'set schema': if (opts.newSchema.name === this.ownerSchema.name) { return this; } throw new NotSupported('Sequence schema change'); case 'rename': const to = opts.newName.name.toLowerCase(); this.ownerSchema._reg_rename(this, this.name, to); this.name = to; return this; case 'owner to': // todo: implement sequence owners ? ...ignored to support pg_dump exports. ignore(opts); return this; default: throw NotSupported.never(opts); } } catch (e) { this.cfg = oldCfg; throw e; } }
nextValue(t: _Transaction): number { let v = t.get<SeqData>(this.symbol)?.nextval; if (v === undefined) { v = this.start; } this.setValue(t, v); return v; }
setValue(t: _Transaction, value: number) { if (value > this.max) { throw new QueryError(`reached maximum value of sequence "${this.name}"`); } if (value < this.min) { throw new QueryError(`reached minimum value of sequence "${this.name}"`); } const data: SeqData = { currval: value, nextval: value + this.inc, }; t.set(this.symbol, data); }
restart(t: _Transaction) { t.delete(this.symbol); }
currentValue(t: _Transaction): number { const v = t.get<SeqData>(this.symbol)?.currval; if (v === undefined) { throw new QueryError(`currval of sequence "${this.name}" is not yet defined in this session`, '55000'); } return v; }

private alterOpts(t: _Transaction, opts: CreateSequenceOptions) { if (opts.as) { ignore(opts.as); this.cfg.dataType = this.ownerSchema.getType(opts.as); } ignore(opts.cache); if (opts.cycle) { this.cfg.cycle = opts.cycle === 'cycle'; }
if (typeof opts.incrementBy === 'number') { this.cfg.inc = opts.incrementBy; }
if (typeof opts.maxValue === 'number') { this.cfg.max = opts.maxValue; } else if (opts.maxValue) { this.cfg.max = undefined; }
if (typeof opts.minValue === 'number') { this.cfg.min = opts.minValue; } else if (opts.maxValue) { this.cfg.min = undefined; }
if (typeof opts.startWith === 'number') { this.cfg.start = opts.startWith; }

if (opts.ownedBy === 'none') { this.owner?.unsubscribe(); } else if (opts.ownedBy) { this.owner?.unsubscribe();
const tbl = asTable(this.ownerSchema.getObject({ name: opts.ownedBy.table, schema: opts.ownedBy.schema }));
const owner = tbl.getColumnRef(opts.ownedBy.column);
this.owner = combineSubs( owner.onDrop(dt => this.drop(dt)), tbl.onDrop(dt => this.drop(dt)), ); }
// === validate if (this.max < this.min) { throw new QueryError('Invalid squeuence min-max'); }
if (!this.inc) { throw new QueryError('Invalid increment'); }
if (this.start > this.max || this.start < this.min) { throw new QueryError('Invalid sequence starting value'); } return this; }
drop(t: _Transaction) { this.owner?.unsubscribe(); this.owner = undefined; t.delete(this.symbol); this.ownerSchema._reg_unregister(this); }}
pg_mem

Version Info

Tagged at
4 months ago