deno.land / x / pg_mem@2.8.1 / datatypes / t-record.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
import { DataType, getId, nil, QueryError, _IType, _ISelection, _Transaction, setId } from '../interfaces-private.ts';import { TypeBase } from './datatype-base.ts';import { RecordCol } from './datatypes.ts';import { Evaluator } from '../evaluator.ts';
export class RecordType extends TypeBase<any> {

public static matches(type: _IType): type is RecordType { return type.primary === DataType.record; }
constructor(readonly columns: readonly RecordCol[]) { super(); }
get primary(): DataType { return DataType.record; }
doEquals(a: any, b: any): boolean { return getId(a) === getId(b); }
public static from(selection: _ISelection): RecordType { const recordCols = selection.columns .filter(c => !!c.id) .map<RecordCol>(x => ({ name: x.id!, type: x.type })); return new RecordType(recordCols); }
/** Build a function that will transform a record of this type to a record of the target type */ transformItemFrom(source: _ISelection): ((raw: any, t: _Transaction, execId: string) => any) | null { if (source.columns.length !== this.columns.length) { return null; } const setters: ((oldItem: any, newItem: any, t: _Transaction) => any)[] = []; for (let i = 0; i < this.columns.length; i++) { const to = this.columns[i]; const from = source.columns[i]; if (!from.type.canConvertImplicit(to.type)) { return null; } const casted = from.cast(to.type); setters.push((old, neu, t) => neu[to.name] = casted.get(old, t)); }
return (raw: any, t, execId) => { const ret = {}; // alter the items id, so each execution will have a different id setId(ret, execId + getId(raw)); for (const s of setters) { s(raw, ret, t); } return ret; }; }
doCanCast(to: _IType): boolean | nil { // lets say that any type can cast to a record with no columns // this is a hack ... see row_to_json() UT return to instanceof RecordType && !to.columns.length; }
doCast(value: Evaluator<any>, to: _IType): Evaluator<any> | nil { return value; }}
pg_mem

Version Info

Tagged at
4 months ago