deno.land / x / pg_mem@2.8.1 / datatypes / t-jsonb.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
import { DataType, nil, _IType } from '../interfaces-private.ts';import { TypeBase } from './datatype-base.ts';import { Evaluator } from '../evaluator.ts';import { deepCompare, deepEqual, errorMessage } from '../utils.ts';import { Types } from './datatypes.ts';import { JSON_NIL } from '../execution/clean-results.ts';import { QueryError } from '../interfaces.ts';import { jsonStringify as stringify} from 'https://deno.land/x/stable_stringify@v0.2.1/jsonStringify.ts';
export class JSONBType extends TypeBase<any> {

constructor(readonly primary: DataType) { super(); }
doCanCast(_to: _IType): boolean | nil { switch (_to.primary) { case DataType.text: case DataType.json: case DataType.jsonb: case DataType.float: case DataType.bool: case DataType.integer: return true; } return null; }
doCast(a: Evaluator, to: _IType): Evaluator { switch (to.primary) { case DataType.text: return a .setType(Types.text()) .setConversion(json => stringify(this.toResult(json)) , toJsonB => ({ toJsonB })) .cast(to) as Evaluator; // <== might need truncation case DataType.jsonb: case DataType.json: return a.setType(to); case DataType.float: case DataType.integer: const isInt = to.primary === DataType.integer; return a .setType(to) .setConversion(json => { if (json === JSON_NIL) { throw new QueryError('cannot cast jsonb null to type ' + (isInt ? 'integer' : 'double precision'), '22023'); } if (typeof json !== 'number') { throw new QueryError('cannot cast jsonb string to type ' + (isInt ? 'integer' : 'double precision'), '22023'); } return isInt ? Math.round(json) : json; }, toFloat => ({ toFloat })); case DataType.bool: return a .setType(to) .setConversion(json => { if (typeof json !== 'boolean') { throw new QueryError('cannot cast jsonb string to type boolean', '22023'); } return json; }, toFloat => ({ toFloat })); default: return a.setType(to); }
}


doCanBuildFrom(from: _IType) { switch (from.primary) { case DataType.text: return true; } return false; }
doBuildFrom(value: Evaluator, from: _IType): Evaluator<Date> | nil { switch (from.primary) { case DataType.text: return value .setConversion(raw => { try { return JSON.parse(raw, (_, x) => x ?? JSON_NIL) ?? JSON_NIL } catch (e) { throw new QueryError({ error: `invalid input syntax for type json`, details: errorMessage(e), code: '22P02', }); } } , toJsonb => ({ toJsonb })); } return null; }


doEquals(a: any, b: any): boolean { return deepEqual(this.toResult(a), this.toResult(b), false); }
doGt(a: any, b: any): boolean { return deepCompare(this.toResult(a), this.toResult(b)) > 0; }
doLt(a: any, b: any): boolean { return deepCompare(this.toResult(a), this.toResult(b)) < 0; }
toResult(result: any): any { return result === JSON_NIL ? null : result; }
}
pg_mem

Version Info

Tagged at
4 months ago