deno.land / x / pg_mem@2.8.1 / datatypes / datatype-base.ts

datatype-base.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
import { Evaluator } from '../evaluator.ts';import { CastError, DataType, IValue, nil, Reg, TR, _ISchema, _IType, _RelationBase, _Transaction } from '../interfaces-private.ts';import { ArrayType } from './datatypes.ts';import { isType, nullIsh } from '../utils.ts';import objectHash from 'https://deno.land/x/object_hash@2.0.3.1/mod.ts';import { QueryError } from '../interfaces.ts';
let regCnt = 0;
export function regGen(): Reg { return { classId: ++regCnt, typeId: ++regCnt, };}
export abstract class TypeBase<TRaw = any> implements _IType<TRaw>, _RelationBase { get [isType.TAG]() { return true; }
readonly reg: Reg;
get type(): 'type' { return 'type'; }
constructor() { this.reg = regGen(); }
private _asArray?: _IType<TRaw[]>; private _asList?: _IType<TRaw[]>;
abstract primary: DataType; get name(): string { return this.primary; }

/** Compute a custom unicty hash for a non null value */ doGetHash?(value: TRaw): string | number;
/** Can be casted to */ doCanCast?(to: _IType<TRaw>): boolean | nil;
/** Can be built to from (inverse of doCanCast()) */ doCanBuildFrom?(from: _IType): boolean | nil;
/** * @see this.prefer() doc */ doPrefer?(type: _IType<TRaw>): _IType | null;
/** * @see this.canConvertImplicit() doc */ doCanConvertImplicit?(to: _IType<TRaw>): boolean;
/** Perform conversion from this type to given type */ doCast?(value: Evaluator<TRaw>, to: _IType<TRaw>): Evaluator<any> | nil;
/** Perform conversion given type to this type (inverse of doCast()) */ doBuildFrom?(value: Evaluator, from: _IType): Evaluator<TRaw> | nil;
doEquals(a: TRaw, b: TRaw): boolean { return a === b; }
doGt(a: TRaw, b: TRaw): boolean { return a > b; }
doLt(a: TRaw, b: TRaw): boolean { return a < b; } toString(): string { throw new Error('Method not implemented.'); }
equals(a: TRaw, b: TRaw): boolean | null { if (nullIsh(a) || nullIsh(b)) { return null; } return this.doEquals(a, b); }
gt(a: TRaw, b: TRaw): boolean | null { if (nullIsh(a) || nullIsh(b)) { return null; } return this.doGt(a, b); } lt(a: TRaw, b: TRaw): boolean | null { if (nullIsh(a) || nullIsh(b)) { return null; } return this.doLt(a, b); }
ge(a: TRaw, b: TRaw): boolean | null { return this.gt(a, b) || this.equals(a, b); }
le(a: TRaw, b: TRaw): boolean | null { return this.lt(a, b) || this.equals(a, b); }
/** * When performing 'a+b', will be given 'b' type, * this returns the prefered resulting type, or null if they are not compatible */ prefer(to: _IType<TRaw>): _IType | nil { if (to === this) { return this; } if (this.doPrefer) { const ret = this.doPrefer(to); if (ret) { return ret; } } return (to as TypeBase).doPrefer?.(this); }
/** * Can constant literals be converted implicitely * (without a cast... i.e. you can use both values as different values of a case expression, for instance) **/ canConvertImplicit(to: _IType<TRaw>): boolean | nil { if (to === this) { return true; } return this.doCanConvertImplicit?.(to); }
/** Can be explicitely casted to */ canCast(to: _IType<TRaw>): boolean | nil { if (to === this) { return true; }
// ask the target type if it know how to build itself from this if ((to as TypeBase).doCanBuildFrom?.(this)) { return true; }
// asks this type if it knows how to convert itself to target if (this.doCanConvertImplicit?.(to) || this.doCanCast?.(to)) { return true; }
return false; }
/** Perform cast */ cast(_a: IValue<TRaw>, _to: _IType<any>): IValue<any> { return this._convert(_a, _to, (a, to) => { if (!this.doCanCast?.(to) || !this.doCast) { throw new CastError(this, to); } return this.doCast(a, to); }) }
/** Perform implicit conversion */ convertImplicit(_a: IValue<TRaw>, _to: _IType<any>): IValue<any> { return this._convert(_a, _to, (a, to) => { if (!this.doCanConvertImplicit?.(to) || !this.doCast) { throw new CastError(this, to); } return this.doCast(a, to); }) }
private _convert(a: IValue<TRaw>, _to: _IType<any>, perform: (a: Evaluator, to: _IType) => Evaluator<any> | nil): IValue<any> { const to = _to as TypeBase; if (to === this) { return a; } if (!(a instanceof Evaluator)) { throw new CastError(this, to); }
let converted: Evaluator | nil; if (to.doCanBuildFrom?.(this)) { if (!to.doBuildFrom) { throw new CastError(this, to); } converted = to.doBuildFrom(a, this); } else { converted = perform(a, to); }
if (!converted) { throw new CastError(this, to); } return converted.setType(to); }
asArray(): _IType<TRaw[]> { if (this._asArray) { return this._asArray; } return this._asArray = new ArrayType(this, false); }
asList(): _IType<TRaw[]> { if (this._asList) { return this._asList; } return this._asList = new ArrayType(this, true); }
hash(value: any): string | number | null { if (nullIsh(value)) { return null; } if (this.doGetHash) { return this.doGetHash(value); } if (typeof value === 'number') { return value; } return objectHash(value); }
drop(t: _Transaction): void { throw new QueryError('drop type not implemented'); }
}
pg_mem

Version Info

Tagged at
4 months ago