PHP-7.4 compat
[m6w6/ext-pq] / tests / conv001.phpt
1 --TEST--
2 converter
3 --SKIPIF--
4 <?php
5 include "_skipif.inc";
6 _ext("json");
7 ?>
8 --INI--
9 date.timezone=UTC
10 --FILE--
11 <?php
12 echo "Test\n";
13
14 include "_setup.inc";
15
16 abstract class Converter implements pq\Converter
17 {
18 protected $types;
19
20 function __construct(\pq\Types $types) {
21 $this->types = $types;
22 }
23 }
24
25 class HStoreConverter extends Converter
26 {
27 function convertTypes() {
28 return [ $this->types["hstore"]->oid ];
29 }
30
31 function convertFromString($string, $type) {
32 return eval("return [$string];");
33 }
34
35 function convertToString($data, $type) {
36 $string = "";
37 foreach ($data as $k => $v) {
38 if (isset($v)) {
39 $string .= sprintf("\"%s\"=>\"%s\",", addslashes($k), addslashes($v));
40 } else {
41 $string .= sprintf("\"%s\"=>NULL,", addslashes($k));
42 }
43 }
44 return $string;
45 }
46 }
47
48 class IntVectorConverter extends Converter
49 {
50 function convertTypes() {
51 return [
52 $this->types["int2vector"]->oid,
53 $this->types["oidvector"]->oid
54 ];
55 }
56
57 function convertFromString($string, $type) {
58 return array_map("intval", explode(" ", $string));
59 }
60
61 function convertToString($data, $type) {
62 return implode(" ", $data);
63 }
64 }
65
66 class JSONConverter extends Converter
67 {
68 function convertTypes() {
69 return [ $this->types["json"]->oid ];
70 }
71
72 function convertFromString($string, $type) {
73 return json_decode($string, true);
74 }
75
76 function convertToString($data, $type) {
77 return json_encode($data);
78 }
79 }
80
81 class Point {
82 public $x;
83 public $y;
84 function __construct($x, $y) {
85 $this->x = $x;
86 $this->y = $y;
87 }
88 }
89
90 class Box {
91 public $p1;
92 public $p2;
93 function __construct(Point $p1, Point $p2) {
94 $this->p1 = $p1;
95 $this->p2 = $p2;
96 }
97 }
98
99 class BoxConverter extends Converter
100 {
101 function convertTypes() {
102 return [ $this->types["box"]->oid ];
103 }
104
105 function convertToString($box, $type) {
106 return sprintf("(%F,%F),(%F,%F)",
107 $box->p1->x, $box->p1->y,
108 $box->p2->x, $box->p2->y
109 );
110 }
111
112 function convertFromString($data, $type) {
113 list($p1x, $p1y, $p2x, $p2y) = sscanf($data, "(%f,%f),(%f,%f)");
114 return new Box(new Point($p1x, $p1y), new Point($p2x, $p2y));
115 }
116 }
117
118 class Text {
119 private $data;
120 function __construct($data) {
121 $this->data = $data;
122 }
123 function __toString() {
124 return (string) $this->data;
125 }
126 }
127
128 $c = new pq\Connection(PQ_DSN);
129 $c->exec("CREATE EXTENSION IF NOT EXISTS hstore");
130 $t = new pq\Types($c);
131
132 $c->setConverter(new HStoreConverter($t));
133 $c->setConverter(new IntVectorConverter($t));
134 if (!(defined("pq\\Types::JSON") && defined("pq\\Result::CONV_JSON"))) {
135 $c->setConverter(new JSONConverter($t));
136 }
137 $c->setConverter(new BoxConverter($t));
138
139 $r = $c->execParams("SELECT \$1 as hs, \$2 as iv, \$3 as oids, \$4 as js, \$5 as ia, \$6 as ta, \$7 as ba, \$8 as da, \$9 as dbl, \$10 as bln, ".
140 "\$11 as dt1, \$12 as dt2, \$13 as dt3, \$14 as dt4, \$15 as dt5, \$16 as dt6, \$17 as dt7, \$18 as dt8, \$19 as txta, \$20 as boxa",
141 array(
142 // hstore
143 array(
144 "k1" => "v1",
145 "k2" => "v2",
146 "k3" => null
147 ),
148 // vectors
149 array(
150 1, 3, 5, 7, 9, 11
151 ),
152 array(
153 2345124, 1431341, 1343423
154 ),
155 // JSON
156 (object) array(
157 "int" => 123,
158 "obj" => (object) array(
159 "a" => 1,
160 "b" => 2,
161 "c" => 3,
162 ),
163 "str" => "äüö"
164 ),
165 // arrays
166 array(array(array(1,2,3))),
167 array(array("a\"","b}",null)),
168 array(true,false),
169 array(1.1,2.2),
170 // double
171 123.456,
172 // bool
173 true,
174 // datetimes
175 new pq\Datetime,
176 new pq\Datetime,
177 new pq\Datetime,
178 new pq\Datetime,
179 new pq\Datetime,
180 new pq\Datetime,
181 new pq\Datetime,
182 new pq\Datetime,
183 // text array
184 [new Text(0), new Text(" or "), new Text(true)],
185 // box array
186 [new Box(new Point(1,2), new Point(2,3)), new Box(new Point(3,4), new Point(4,5))],
187 ),
188 array(
189 $t["hstore"]->oid,
190 $t["int2vector"]->oid,
191 $t["oidvector"]->oid,
192 $t["json"]->oid,
193 $t["_int4"]->oid,
194 $t["_text"]->oid,
195 $t["_bool"]->oid,
196 $t["_float8"]->oid,
197 $t["float4"]->oid,
198 $t["bool"]->oid,
199 $t["date"]->oid,
200 $t["abstime"]->oid,
201 $t["timestamp"]->oid,
202 $t["timestamptz"]->oid,
203 $t["date"]->oid,
204 $t["abstime"]->oid,
205 $t["timestamp"]->oid,
206 $t["timestamptz"]->oid,
207 $t["_text"]->oid,
208 $t["_box"]->oid
209 )
210 );
211
212 var_dump($r->fetchAll());
213
214 ?>
215 Done
216 --EXPECTF--
217 Test
218 array(1) {
219 [0]=>
220 array(%d) {
221 [0]=>
222 array(3) {
223 ["k1"]=>
224 string(2) "v1"
225 ["k2"]=>
226 string(2) "v2"
227 ["k3"]=>
228 NULL
229 }
230 [1]=>
231 array(6) {
232 [0]=>
233 int(1)
234 [1]=>
235 int(3)
236 [2]=>
237 int(5)
238 [3]=>
239 int(7)
240 [4]=>
241 int(9)
242 [5]=>
243 int(11)
244 }
245 [2]=>
246 array(3) {
247 [0]=>
248 int(2345124)
249 [1]=>
250 int(1431341)
251 [2]=>
252 int(1343423)
253 }
254 [3]=>
255 array(3) {
256 ["int"]=>
257 int(123)
258 ["obj"]=>
259 array(3) {
260 ["a"]=>
261 int(1)
262 ["b"]=>
263 int(2)
264 ["c"]=>
265 int(3)
266 }
267 ["str"]=>
268 string(6) "äüö"
269 }
270 [4]=>
271 array(1) {
272 [0]=>
273 array(1) {
274 [0]=>
275 array(3) {
276 [0]=>
277 int(1)
278 [1]=>
279 int(2)
280 [2]=>
281 int(3)
282 }
283 }
284 }
285 [5]=>
286 array(1) {
287 [0]=>
288 array(3) {
289 [0]=>
290 string(2) "a""
291 [1]=>
292 string(2) "b}"
293 [2]=>
294 NULL
295 }
296 }
297 [6]=>
298 array(2) {
299 [0]=>
300 bool(true)
301 [1]=>
302 bool(false)
303 }
304 [7]=>
305 array(2) {
306 [0]=>
307 float(1.1)
308 [1]=>
309 float(2.2)
310 }
311 [8]=>
312 float(123.456)
313 [9]=>
314 bool(true)
315 [10]=>
316 object(pq\DateTime)#%d (4) {
317 ["format"]=>
318 string(5) "Y-m-d"
319 ["date"]=>
320 string(26) "%d-%d-%d 00:00:00.000000"
321 ["timezone_type"]=>
322 int(3)
323 ["timezone"]=>
324 string(3) "UTC"
325 }
326 [11]=>
327 object(pq\DateTime)#%d (4) {
328 ["format"]=>
329 string(11) "Y-m-d H:i:s"
330 ["date"]=>
331 string(26) "%d-%d-%d %d:%d:%d.%d"
332 ["timezone_type"]=>
333 int(1)
334 ["timezone"]=>
335 string(%d) "%s"
336 }
337 [12]=>
338 object(pq\DateTime)#%d (4) {
339 ["format"]=>
340 string(13) "Y-m-d H:i:s.u"
341 ["date"]=>
342 string(26) "%d-%d-%d %d:%d:%d.%d"
343 ["timezone_type"]=>
344 int(3)
345 ["timezone"]=>
346 string(3) "UTC"
347 }
348 [13]=>
349 object(pq\DateTime)#%d (4) {
350 ["format"]=>
351 string(14) "Y-m-d H:i:s.uO"
352 ["date"]=>
353 string(26) "%d-%d-%d %d:%d:%d.%d"
354 ["timezone_type"]=>
355 int(1)
356 ["timezone"]=>
357 string(%d) "%s"
358 }
359 [14]=>
360 object(pq\DateTime)#%d (4) {
361 ["format"]=>
362 string(5) "Y-m-d"
363 ["date"]=>
364 string(26) "%d-%d-%d 00:00:00.000000"
365 ["timezone_type"]=>
366 int(3)
367 ["timezone"]=>
368 string(3) "UTC"
369 }
370 [15]=>
371 object(pq\DateTime)#%d (4) {
372 ["format"]=>
373 string(11) "Y-m-d H:i:s"
374 ["date"]=>
375 string(26) "%d-%d-%d %d:%d:%d.%d"
376 ["timezone_type"]=>
377 int(1)
378 ["timezone"]=>
379 string(%d) "%s"
380 }
381 [16]=>
382 object(pq\DateTime)#%d (4) {
383 ["format"]=>
384 string(13) "Y-m-d H:i:s.u"
385 ["date"]=>
386 string(26) "%d-%d-%d %d:%d:%d.%d"
387 ["timezone_type"]=>
388 int(3)
389 ["timezone"]=>
390 string(3) "UTC"
391 }
392 [17]=>
393 object(pq\DateTime)#%d (4) {
394 ["format"]=>
395 string(14) "Y-m-d H:i:s.uO"
396 ["date"]=>
397 string(26) "%d-%d-%d %d:%d:%d.%d"
398 ["timezone_type"]=>
399 int(1)
400 ["timezone"]=>
401 string(%d) "%s"
402 }
403 [18]=>
404 array(3) {
405 [0]=>
406 string(1) "0"
407 [1]=>
408 string(4) " or "
409 [2]=>
410 string(1) "1"
411 }
412 [19]=>
413 array(2) {
414 [0]=>
415 object(Box)#%d (2) {
416 ["p1"]=>
417 object(Point)#%d (2) {
418 ["x"]=>
419 float(2)
420 ["y"]=>
421 float(3)
422 }
423 ["p2"]=>
424 object(Point)#%d (2) {
425 ["x"]=>
426 float(1)
427 ["y"]=>
428 float(2)
429 }
430 }
431 [1]=>
432 object(Box)#%d (2) {
433 ["p1"]=>
434 object(Point)#%d (2) {
435 ["x"]=>
436 float(4)
437 ["y"]=>
438 float(5)
439 }
440 ["p2"]=>
441 object(Point)#%d (2) {
442 ["x"]=>
443 float(3)
444 ["y"]=>
445 float(4)
446 }
447 }
448 }
449 }
450 }
451 Done