deno.land / x / pg_mem@2.8.1 / datatypes / datatypes-geometric.ts

datatypes-geometric.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
import { DataType, QueryError } from '../interfaces.ts';import { _IType } from '../interfaces-private.ts';import { Box, Circle, Line, Path, Point, Polygon, Segment } from 'https://deno.land/x/pgsql_ast_parser@12.0.1/mod.ts';import { Evaluator } from '../evaluator.ts';import { TypeBase } from './datatype-base.ts';
export function pointToStr(p: Point) { return `(${p.x},${p.y})`;}
export function pointEq(a: Point, b: Point) { return a.x === b.x && a.y === b.y;}
export class PointType extends TypeBase<Point> { get primary(): DataType { return DataType.point; } get name() { return 'point'; } doCanCast(t: _IType) { return t.primary === DataType.text; }
doCast(value: Evaluator<Point>, to: _IType) { if (to.primary !== DataType.text) { throw new QueryError(`Invalid cast to: ` + to.primary); } return value .setConversion((p: Point) => { return pointToStr(p); } , pointToTxt => ({ pointToTxt })); }
doEquals(a: Point, b: Point) { return pointEq(a, b); }
doGt(a: Point, b: Point) { if (a.x !== b.x) { return a.x > b.x; } return a.y > b.y; }
doLt(a: Point, b: Point) { if (a.x !== b.x) { return a.x < b.x; } return a.y < b.y; }}


export class LineType extends TypeBase<Line> {
get primary(): DataType { return DataType.line; } get name() { return 'line'; } doCanCast(t: _IType) { return t.primary === DataType.text; }
doCast(value: Evaluator<Line>, to: _IType) { if (to.primary !== DataType.text) { throw new QueryError(`Invalid cast to: ` + to.primary); } return value .setConversion((l: Line) => { return `{${l.a},${l.b},${l.c}}`; } , lineToTxt => ({ lineToTxt })); }
doEquals(a: Line, b: Line) { return a.a === b.a && a.b === b.b && a.c === b.c; }}
export class LsegType extends TypeBase<Segment> {
get primary(): DataType { return DataType.lseg; } get name() { return 'lseg'; } doCanCast(t: _IType) { return t.primary === DataType.text; }
doCast(value: Evaluator<Segment>, to: _IType) { if (to.primary !== DataType.text) { throw new QueryError(`Invalid cast to: ` + to.primary); } return value .setConversion(([a, b]: Segment) => { return `[${pointToStr(a)},${pointToStr(b)}]`; } , SegmentToTxt => ({ SegmentToTxt })); }
doEquals([as, ae]: Segment, [bs, be]: Segment) { return pointEq(as, bs) && pointEq(ae, be); }}
export class BoxType extends TypeBase<Box> {
get primary(): DataType { return DataType.box; } get name() { return 'box'; } doCanCast(t: _IType) { return t.primary === DataType.text; }
doCast(value: Evaluator<Box>, to: _IType) { if (to.primary !== DataType.text) { throw new QueryError(`Invalid cast to: ` + to.primary); } return value .setConversion(([a, b]: Box) => { return `${pointToStr(a)},${pointToStr(b)}`; } , BoxToTxt => ({ BoxToTxt })); }
doEquals([as, ae]: Box, [bs, be]: Box) { return pointEq(as, bs) && pointEq(ae, be); }}
export class PathType extends TypeBase<Path> {
get primary(): DataType { return DataType.path; } get name() { return 'path'; } doCanCast(t: _IType) { return t.primary === DataType.text; }
doCast(value: Evaluator<Path>, to: _IType) { if (to.primary !== DataType.text) { throw new QueryError(`Invalid cast to: ` + to.primary); } return value .setConversion((p: Path) => { const vals = p.path.map(pointToStr).join(','); return p.closed ? '(' + vals + ')' : '[' + vals + ']'; } , PathToTxt => ({ PathToTxt })); }
doEquals(a: Path, b: Path) { // Yup, you read that right ... // Try it... path equality always returns true (???) return true; // return !!a.closed === !!b.closed // && a.path.length === b.path.length // && a.path.every((x, i) => pointEq(x, b.path[i])); }}
export class PolygonType extends TypeBase<Polygon> {
get primary(): DataType { return DataType.polygon; } get name() { return 'polygon'; } doCanCast(t: _IType) { return t.primary === DataType.text; }
doCast(value: Evaluator<Polygon>, to: _IType) { if (to.primary !== DataType.text) { throw new QueryError(`Invalid cast to: ` + to.primary); } return value .setConversion((p: Polygon) => { const vals = p.map(pointToStr).join(','); return '(' + vals + ')'; } , PolygonToTxt => ({ PolygonToTxt })); }
doEquals(a: Polygon, b: Polygon) { return a.length === b.length && a.every((x, i) => pointEq(x, b[i])); }}

export class CircleType extends TypeBase<Circle> {
get primary(): DataType { return DataType.circle; } get name() { return 'circle'; } doCanCast(t: _IType) { return t.primary === DataType.text; }
doCast(value: Evaluator<Circle>, to: _IType) { if (to.primary !== DataType.text) { throw new QueryError(`Invalid cast to: ` + to.primary); } return value .setConversion((p: Circle) => { return `<${pointToStr(p.c)},${p.r}>` } , CircleToTxt => ({ CircleToTxt })); }
doEquals(a: Circle, b: Circle) { return pointEq(a.c, b.c) && a.r === b.r; }}
pg_mem

Version Info

Tagged at
4 months ago