deno.land / x / pg_mem@2.8.1 / datatypes / datatypes.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
import { IValue, _IType } from '../interfaces-private.ts';import { DataType, CastError, IType, QueryError, nil } from '../interfaces.ts';import { nullIsh } from '../utils.ts';import { Evaluator, Value } from '../evaluator.ts';import { parseArrayLiteral } from 'https://deno.land/x/pgsql_ast_parser@12.0.1/mod.ts';import { parseGeometricLiteral } from 'https://deno.land/x/pgsql_ast_parser@12.0.1/mod.ts';import { bufCompare, bufFromString, bufToString, TBuffer } from '../misc/buffer-deno.ts';import { TypeBase } from './datatype-base.ts';import { BoxType, CircleType, LineType, LsegType, PathType, PointType, PolygonType } from './datatypes-geometric.ts';import { IntervalType } from './t-interval.ts';import { TimeType } from './t-time.ts';import { TimestampType } from './t-timestamp.ts';import { JSONBType } from './t-jsonb.ts';import { RegTypeImpl } from './t-regtype.ts';import { RegClassImpl } from './t-regclass.ts';import { RecordType } from './t-record.ts';import { INetType } from './t-inet.ts';import { buildCtx } from '../parser/context.ts';

class UUIDtype extends TypeBase<string> {

get primary(): DataType { return DataType.uuid; }
doCanCast(to: _IType) { switch (to.primary) { case DataType.text: return true; } return null; }
doCast(value: Evaluator, to: _IType) { switch (to.primary) { case DataType.text: return value; } throw new Error('Unexpected cast error'); }}


class NullType extends TypeBase<null> {
get primary(): DataType { return DataType.null; }
doCast(value: Evaluator<any>, to: _IType): Evaluator<any> { return new Evaluator(to, null, 'null', null, null); }
doCanCast(to: _IType): boolean { return true; }
doCanConvertImplicit() { return true; }
doEquals(a: any, b: any): boolean { return false; }
doGt(a: any, b: any): boolean { return false; }
doLt(a: any, b: any): boolean { return false; }
doPrefer(type: _IType) { return type; // always prefer notnull types }}
export class DefaultType extends NullType {}
export const integers: ReadonlySet<DataType> = new Set([DataType.integer, DataType.bigint]);export const floats: ReadonlySet<DataType> = new Set([DataType.decimal, DataType.float]);export const numbers: ReadonlySet<DataType> = new Set([...integers, ...floats]);export const numberPriorities = [DataType.integer, DataType.bigint, DataType.decimal, DataType.float] .reduce<Record<DataType, number>>((a, x, i) => ({ ...a, [x]: i }), {} as Record<DataType, number>);
export function isNumeric(t: DataType | IType) { const type = typeof t === 'string' ? t : t.primary; return numbers.has(type);}export function isInteger(t: DataType | IType) { const type = typeof t === 'string' ? t : t.primary; return integers.has(type);}
class NumberType extends TypeBase<number> {
constructor(readonly primary: DataType) { super(); }
doCanConvertImplicit(to: _IType) { switch (to.primary) { case DataType.integer: case DataType.bigint: case DataType.float: case DataType.decimal: case DataType.regtype: case DataType.regclass: return true; default: return false; } }
doPrefer(type: _IType): _IType | null { switch (type.primary) { case DataType.integer: case DataType.bigint: return this; case DataType.float: case DataType.decimal: return type; } return null; }
doCanCast(to: _IType) { switch (to.primary) { case DataType.integer: case DataType.bigint: case DataType.float: case DataType.decimal: case DataType.regtype: case DataType.regclass: return true; case DataType.text: return true; default: return false; } } doCast(value: Evaluator<any>, to: _IType): Evaluator<any> { if (!integers.has(value.type.primary) && integers.has(to.primary)) { return new Evaluator( to , value.id , value.hash , value , (raw, t) => { const got = value.get(raw, t); return typeof got === 'number' ? Math.round(got) : got; } ); } const { schema } = buildCtx(); switch (to.primary) { case DataType.regtype: return value .setType(Types.regtype) .setConversion((int: number) => { const got = schema.getType(int, { nullIfNotFound: true }); if (!got) { throw new CastError(DataType.integer, DataType.regtype); } return got.name; } , intToRegType => ({ intToRegType })); case DataType.regclass: return value .setType(Types.regclass) .setConversion((int: number) => { // === int -> regclass const obj = schema.getObjectByRegOrName(int, { nullIfNotFound: true }); return obj?.reg.classId ?? int; } , intToRegClass => ({ intToRegClass })); case DataType.text: return value .setType(to) .setConversion((int: number) => int.toString() , toTxt => ({ toTxt })); } return value.setType(to); }}


class ByteArrayType extends TypeBase<TBuffer> {
get primary(): DataType { return DataType.bytea; }

doCanCast(to: _IType) { switch (to.primary) { case DataType.text: return true; } return null; }
doCast(value: Evaluator, to: _IType) { switch (to.primary) { case DataType.text: return value .setConversion(raw => bufToString(raw) , toStr => ({ toStr })); } throw new Error('Unexpected cast error'); }
doEquals(a: TBuffer, b: TBuffer): boolean { return bufCompare(a, b) === 0; }
doGt(a: TBuffer, b: TBuffer): boolean { return bufCompare(a, b) > 0; }
doLt(a: TBuffer, b: TBuffer): boolean { return bufCompare(a, b) < 0; }}

class TextType extends TypeBase<string> {
get name(): string { if (this.citext) { return 'citext'; } return this.len ? 'character varying' : 'text'; }
get primary(): DataType { return this.citext ? DataType.citext : DataType.text; }
constructor(readonly len: number | null, private citext?: boolean) { super(); }
doPrefer(to: _IType) { if (to instanceof TextType) { // returns the broader type if (!to.len) { return to; } if (!this.len) { return this; } return to.len > this.len ? to : this; } if (this.canCast(to)) { return to; } return null; }
doCanConvertImplicit(to: _IType): boolean { // text is implicitely convertible to dates switch (to.primary) { case DataType.text: case DataType.bool: case DataType.uuid: case DataType.bytea: return true; } return false; }
doCanCast(to: _IType): boolean | nil { switch (to.primary) { case DataType.text: case DataType.citext: return true; case DataType.text: case DataType.uuid: return true; case DataType.bool: return true; case DataType.array: return this.canCast((to as ArrayType).of); case DataType.bytea: return true; } if (numbers.has(to.primary)) { return true; } if (isGeometric(to.primary)) { return true; } return undefined; }
doCast(value: Evaluator<string>, to: _IType) { switch (to.primary) { case DataType.citext: return value.setType(to); case DataType.bool: return value .setConversion(rawStr => { if (nullIsh(rawStr)) { return null; } if (rawStr === '0') { return false; } else if (rawStr === '1') { return true; } const str = (rawStr as string).toLowerCase(); if ('true'.startsWith(str)) { return true; } else if ('false'.startsWith(str)) { return false; } if ('yes'.startsWith(str)) { return true; } else if ('no'.startsWith(str)) { return false; } throw new CastError(DataType.text, DataType.bool, 'string ' + rawStr); } , toBool => ({ toBool })); case DataType.uuid: return value .setConversion((_rawStr: string) => { let rawStr = _rawStr; if (nullIsh(rawStr)) { return null; } // check schema if (rawStr[0] === '{') { if (rawStr[rawStr.length - 1] !== '}') { throw new CastError(DataType.text, DataType.uuid, 'string: ' + JSON.stringify(_rawStr)); } rawStr = rawStr.substr(1, rawStr.length - 2); } rawStr = rawStr.toLowerCase(); const [full, a, b, c, d, e] = /^([0-9a-f]{8})-?([0-9a-f]{4})-?([0-9a-f]{4})-?([0-9a-f]{4})-?([0-9a-f]{12})$/.exec(rawStr) ?? []; if (!full) { throw new CastError(DataType.text, DataType.uuid, 'string: ' + JSON.stringify(_rawStr)); } return `${a}-${b}-${c}-${d}-${e}`; } , toUuid => ({ toUuid })); case DataType.text: const fromStr = to as TextType; const toStr = to as TextType; if (toStr.len === null || (fromStr.len ?? -1) < toStr.len) { // no need to truncate return value; } return value .setConversion(str => { if (str && str.length > toStr.len!) { throw new QueryError(`value too long for type character varying(${toStr.len})`); } return str; } , truncate => ({ truncate, len: toStr.len }));
case DataType.array: return value .setType(to) .setConversion((str: string) => { const array = parseArrayLiteral(str); (to as ArrayType).convertLiteral(array); return array; } , parseArray => ({ parseArray })); case DataType.bytea: return value .setConversion(str => { return bufFromString(str); } , toBytea => ({ toBytea }));
} if (numbers.has(to.primary)) { const isInt = integers.has(to.primary); return value .setConversion(str => { const val = Number.parseFloat(str); if (!Number.isFinite(val)) { throw new QueryError(`invalid input syntax for ${to.primary}: ${str}`); } if (isInt && Math.floor(val) !== val) { throw new QueryError(`invalid input syntax for ${to.primary}: ${str}`) } return val; } , castNum => ({ castNum, to: to.primary })); } if (isGeometric(to.primary)) { return value .setConversion(str => { const ret = parseGeometricLiteral(str, to.primary as any); return ret; } , castGeo => ({ castGeo, to: to.primary })); } return undefined; }
doEquals(a: string, b: string) { if (this.citext) { return a.localeCompare(b, undefined, { sensitivity: 'accent' }) === 0; }
return super.doEquals(a, b); }}


class BoolType extends TypeBase<boolean> { get primary(): DataType { return DataType.bool; }
doCanCast(to: _IType): boolean | nil { switch (to.primary) { case DataType.text: case DataType.citext: case DataType.bool: case DataType.integer: return true; } return false; }
doCast(value: Evaluator, to: _IType) { switch (to.primary) { case DataType.text: case DataType.citext: return new Evaluator( to , value.id , value.hash! , value , (raw, t) => { const got = value.get(raw, t); return got ? 'true' : 'false'; }); case DataType.bool: return value; case DataType.integer: return new Evaluator( to , value.id , value.hash! , value , (raw, t) => { const got = value.get(raw, t); return got ? 1 : 0; }); } throw new Error('Unexpected cast error'); }}
export class ArrayType extends TypeBase<any[]> {
public static matches(type: IType): type is ArrayType { return type.primary === DataType.array; }
get primary(): DataType { if (this.list) { return DataType.list; } return DataType.array; }
get name(): string { return this.of.name + '[]'; }

constructor(readonly of: _IType, private list: boolean) { super(); }
doCanCast(to: _IType) { if (to instanceof ArrayType) { return this.of.canCast(to.of); } return this.of.canCast(to); }
doCast(value: Evaluator, _to: _IType) { if (_to instanceof ArrayType) {
const to = _to as ArrayType; const valueType = value.type as ArrayType; return new Evaluator( to , value.id , value.hash! , value , (raw, t) => { const arr = value.get(raw, t) as any[]; return arr.map(x => Value.constant(valueType.of, x).cast(to.of).get(raw, t)); }); } if (_to.primary === DataType.text) { return this.toText(_to, value); } return this.toSingleColumn(_to, value); }
toText(to: _IType, value: Evaluator) { const valueType = value.type as ArrayType; const converter = Value.converter(valueType.of, Types.text()); return new Evaluator( to , value.id , value.hash! , value , (raw, t) => { const arr = value.get(raw, t) as any[]; const strs = arr.map(x => converter(x, t)); const data = strs.join(','); return this.list ? '(' + data + ')' : '{' + data + '}'; }, { forceNotConstant: true }); }
toSingleColumn(to: _IType, value: Evaluator) { const valueType = value.type as ArrayType; const converter = Value.converter(valueType.of, to); return new Evaluator( to , value.id , value.hash! , value , (raw, t) => { const arr = value.get(raw, t) as any[]; return converter(arr[0], t); }, { forceNotConstant: true }); }
doEquals(a: any[], b: any[]): boolean { if (a.length !== b.length) { return false; } for (let i = 0; i < a.length; i++) { if (!this.of.equals(a[i], b[i])) { return false; } } return true; }
doGt(a: any[], b: any[]): boolean { const len = Math.min(a.length, b.length); for (let i = 0; i < len; i++) { if (this.of.gt(a[i], b[i])) { return true; } } return a.length > b.length; }
doLt(a: any[], b: any[]): boolean { const len = Math.min(a.length, b.length); for (let i = 0; i < len; i++) { if (this.of.lt(a[i], b[i])) { return true; } } return a.length < b.length; }
convertLiteral(elts: any) { if (nullIsh(elts)) { return; } if (!Array.isArray(elts)) { throw new QueryError('Array depth mismatch: was expecting an array item.'); } if (this.of instanceof ArrayType) { for (let i = 0; i < elts.length; i++) { this.of.convertLiteral(elts[i]); } } else { for (let i = 0; i < elts.length; i++) { if (Array.isArray(elts[i])) { throw new QueryError('Array depth mismatch: was not expecting an array item.'); } elts[i] = Value.text(elts[i]) .cast(this.of) .get(); } } return elts; }}
export interface RecordCol { readonly name: string; readonly type: _IType;}
/** Basic types */export const Types = { [DataType.record]: (columns: RecordCol[]) => new RecordType(columns) as _IType, [DataType.bool]: new BoolType() as _IType, [DataType.text]: (len: number | nil = null) => makeText(len) as _IType, [DataType.citext]: new TextType(null, true), [DataType.timestamp]: (len: number | nil = null) => makeTimestamp(DataType.timestamp, len) as _IType, [DataType.timestamptz]: (len: number | nil = null) => makeTimestamp(DataType.timestamptz, len) as _IType, [DataType.uuid]: new UUIDtype() as _IType, [DataType.date]: new TimestampType(DataType.date) as _IType, [DataType.interval]: new IntervalType() as _IType, [DataType.time]: new TimeType(DataType.time) as _IType, [DataType.timetz]: new TimeType(DataType.timetz) as _IType, [DataType.jsonb]: new JSONBType(DataType.jsonb) as _IType, [DataType.regtype]: new RegTypeImpl() as _IType, [DataType.regclass]: new RegClassImpl() as _IType, [DataType.json]: new JSONBType(DataType.json) as _IType, [DataType.null]: new NullType() as _IType, [DataType.float]: new NumberType(DataType.float) as _IType, [DataType.integer]: new NumberType(DataType.integer) as _IType, [DataType.bigint]: new NumberType(DataType.bigint) as _IType, [DataType.bytea]: new ByteArrayType() as _IType, [DataType.point]: new PointType() as _IType, [DataType.line]: new LineType() as _IType, [DataType.lseg]: new LsegType() as _IType, [DataType.box]: new BoxType() as _IType, [DataType.inet]: new INetType() as _IType, [DataType.path]: new PathType() as _IType, [DataType.polygon]: new PolygonType() as _IType, [DataType.circle]: new CircleType() as _IType, default: new DefaultType() as _IType,}
export const dateTypes: ReadonlySet<DataType> = new Set([ DataType.timestamp , DataType.timestamptz , DataType.date , DataType.time]);
export function isDateType(_type: _IType | DataType) { const t = typeof _type === 'string' ? _type : _type.primary; return dateTypes.has(t);}
export function isGeometric(dt: DataType) { switch (dt) { case DataType.point: case DataType.line: case DataType.lseg: case DataType.box: case DataType.path: case DataType.polygon: case DataType.circle: return true; } return false;}
const texts = new Map<number | null, _IType>();function makeText(len: number | nil = null) { len = len ?? null; let got = texts.get(len); if (!got) { texts.set(len, got = new TextType(len)); } return got;}
const timestamps = new Map<string, _IType>();function makeTimestamp(primary: DataType, len: number | nil = null) { len = len ?? null; const key = primary + '/' + len; let got = timestamps.get(key); if (!got) { timestamps.set(key, got = new TimestampType(primary, len)); } return got;}






export const typeSynonyms: { [key: string]: DataType | { type: DataType; ignoreConfig: boolean } } = { 'varchar': DataType.text, 'char': DataType.text, 'character': DataType.text, 'character varying': DataType.text,
'int': DataType.integer, 'int4': DataType.integer, 'int8': DataType.bigint, 'serial': DataType.integer, 'serial8': DataType.bigint, 'bigserial': DataType.integer, 'smallserial': DataType.integer, 'smallint': DataType.integer, 'bigint': DataType.integer, 'oid': DataType.integer,
'decimal': DataType.float, 'float': DataType.float, 'double precision': DataType.float, 'numeric': { type: DataType.float, ignoreConfig: true }, 'real': DataType.float, 'money': DataType.float,
'timestamp with time zone': DataType.timestamptz, 'timestamp without time zone': DataType.timestamp,
'boolean': DataType.bool,
'time with time zone': DataType.timetz, 'time without time zone': DataType.time,}

/** Finds a common type by implicit conversion */export function reconciliateTypes(values: IValue[], nullIfNoMatch?: false): _IType;export function reconciliateTypes(values: IValue[], nullIfNoMatch: true): _IType | nil;export function reconciliateTypes(values: IValue[], nullIfNoMatch?: boolean): _IType | nilexport function reconciliateTypes(values: IValue[], nullIfNoMatch?: boolean): _IType | nil { // FROM https://www.postgresql.org/docs/current/typeconv-union-case.html
const nonNull = values .filter(x => x.type.primary !== DataType.null);
if (!nonNull.length) { // If all inputs are of type unknown, resolve as type text (the preferred type of the string category). Otherwise, unknown inputs are ignored for the purposes of the remaining rules. return Types.text(); }
// If all inputs are of the same type, and it is not unknown, resolve as that type. const single = new Set(nonNull .map(v => v.type.reg.typeId)); if (single.size === 1) { return nonNull[0].type; }
return reconciliateTypesRaw(nonNull, nullIfNoMatch);}


/** Finds a common type by implicit conversion */function reconciliateTypesRaw(values: IValue[], nullIfNoMatch?: false): _IType;function reconciliateTypesRaw(values: IValue[], nullIfNoMatch: true): _IType | nil;function reconciliateTypesRaw(values: IValue[], nullIfNoMatch?: boolean): _IType | nilfunction reconciliateTypesRaw(values: IValue[], nullIfNoMatch?: boolean): _IType | nil { // find the matching type among non constants const foundType = values .reduce((final, c) => { if (c.type === Types.null) { return final; } const pref = final.prefer(c.type); if (!pref) { throw new CastError(c.type.primary, final.primary, c.id ?? undefined); } return pref; }, Types.null);
// check that all constant literals are matching this. for (const x of values) { if (!x.isConstantLiteral && !x.type.canConvertImplicit(foundType)) { if (nullIfNoMatch) { return null; } throw new CastError(x.type.primary, foundType.primary); } }
return foundType;}
pg_mem

Version Info

Tagged at
4 months ago