-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
sequence.ts
204 lines (171 loc) · 5.6 KB
/
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
import { AlterSequenceChange, CreateSequenceOptions } from 'https://deno.land/x/pgsql_ast_parser@3.1.0/mod.ts';
import { combineSubs, ignore } 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';
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, private db: _ISchema) {
this.reg = db._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) {
t.set(this.symbol, this.start);
}
return this;
case 'set schema':
if (opts.newSchema === this.db.name) {
return this;
}
throw new NotSupported('Sequence schema change');
case 'rename':
const to = opts.newName.toLowerCase();
this.db._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<number>(this.symbol);
if (v === undefined) {
v = this.start;
} else {
v += this.inc;
}
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}"`);
}
t.set(this.symbol, value);
}
currentValue(t: _Transaction): number {
const v = t.get<number>(this.symbol);
if (v === undefined) {
throw new QueryError(`currval of sequence "${this.name}" is not yet defined in this session`);
}
return v;
}
private alterOpts(t: _Transaction, opts: CreateSequenceOptions) {
if (opts.as) {
ignore(opts.as);
this.cfg.dataType = this.db.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.db.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.db._reg_unregister(this);
}
}