update version header
[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 dt3, \$13 as dt4, \$14 as dt5, \$15 as dt7, \$16 as dt8, \$17 as txta, \$18 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 // text array
182 [new Text(0), new Text(" or "), new Text(true)],
183 // box array
184 [new Box(new Point(1,2), new Point(2,3)), new Box(new Point(3,4), new Point(4,5))],
185 ),
186 array(
187 $t["hstore"]->oid,
188 $t["int2vector"]->oid,
189 $t["oidvector"]->oid,
190 $t["json"]->oid,
191 $t["_int4"]->oid,
192 $t["_text"]->oid,
193 $t["_bool"]->oid,
194 $t["_float8"]->oid,
195 $t["float4"]->oid,
196 $t["bool"]->oid,
197 $t["date"]->oid,
198 $t["timestamp"]->oid,
199 $t["timestamptz"]->oid,
200 $t["date"]->oid,
201 $t["timestamp"]->oid,
202 $t["timestamptz"]->oid,
203 $t["_text"]->oid,
204 $t["_box"]->oid
205 )
206 );
207
208 var_dump($r->fetchAll());
209
210 ?>
211 Done
212 --EXPECTF--
213 Test
214 array(1) {
215 [0]=>
216 array(%d) {
217 [0]=>
218 array(3) {
219 ["k1"]=>
220 string(2) "v1"
221 ["k2"]=>
222 string(2) "v2"
223 ["k3"]=>
224 NULL
225 }
226 [1]=>
227 array(6) {
228 [0]=>
229 int(1)
230 [1]=>
231 int(3)
232 [2]=>
233 int(5)
234 [3]=>
235 int(7)
236 [4]=>
237 int(9)
238 [5]=>
239 int(11)
240 }
241 [2]=>
242 array(3) {
243 [0]=>
244 int(2345124)
245 [1]=>
246 int(1431341)
247 [2]=>
248 int(1343423)
249 }
250 [3]=>
251 array(3) {
252 ["int"]=>
253 int(123)
254 ["obj"]=>
255 array(3) {
256 ["a"]=>
257 int(1)
258 ["b"]=>
259 int(2)
260 ["c"]=>
261 int(3)
262 }
263 ["str"]=>
264 string(6) "äüö"
265 }
266 [4]=>
267 array(1) {
268 [0]=>
269 array(1) {
270 [0]=>
271 array(3) {
272 [0]=>
273 int(1)
274 [1]=>
275 int(2)
276 [2]=>
277 int(3)
278 }
279 }
280 }
281 [5]=>
282 array(1) {
283 [0]=>
284 array(3) {
285 [0]=>
286 string(2) "a""
287 [1]=>
288 string(2) "b}"
289 [2]=>
290 NULL
291 }
292 }
293 [6]=>
294 array(2) {
295 [0]=>
296 bool(true)
297 [1]=>
298 bool(false)
299 }
300 [7]=>
301 array(2) {
302 [0]=>
303 float(1.1)
304 [1]=>
305 float(2.2)
306 }
307 [8]=>
308 float(123.456)
309 [9]=>
310 bool(true)
311 [10]=>
312 object(pq\DateTime)#%d (4) {
313 ["format"]=>
314 string(5) "Y-m-d"
315 ["date"]=>
316 string(26) "%d-%d-%d 00:00:00.000000"
317 ["timezone_type"]=>
318 int(3)
319 ["timezone"]=>
320 string(3) "UTC"
321 }
322 [11]=>
323 object(pq\DateTime)#%d (4) {
324 ["format"]=>
325 string(13) "Y-m-d H:i:s.u"
326 ["date"]=>
327 string(26) "%d-%d-%d %d:%d:%d.%d"
328 ["timezone_type"]=>
329 int(3)
330 ["timezone"]=>
331 string(3) "UTC"
332 }
333 [12]=>
334 object(pq\DateTime)#%d (4) {
335 ["format"]=>
336 string(14) "Y-m-d H:i:s.uO"
337 ["date"]=>
338 string(26) "%d-%d-%d %d:%d:%d.%d"
339 ["timezone_type"]=>
340 int(1)
341 ["timezone"]=>
342 string(%d) "%s"
343 }
344 [13]=>
345 object(pq\DateTime)#%d (4) {
346 ["format"]=>
347 string(5) "Y-m-d"
348 ["date"]=>
349 string(26) "%d-%d-%d 00:00:00.000000"
350 ["timezone_type"]=>
351 int(3)
352 ["timezone"]=>
353 string(3) "UTC"
354 }
355 [14]=>
356 object(pq\DateTime)#%d (4) {
357 ["format"]=>
358 string(13) "Y-m-d H:i:s.u"
359 ["date"]=>
360 string(26) "%d-%d-%d %d:%d:%d.%d"
361 ["timezone_type"]=>
362 int(3)
363 ["timezone"]=>
364 string(3) "UTC"
365 }
366 [15]=>
367 object(pq\DateTime)#%d (4) {
368 ["format"]=>
369 string(14) "Y-m-d H:i:s.uO"
370 ["date"]=>
371 string(26) "%d-%d-%d %d:%d:%d.%d"
372 ["timezone_type"]=>
373 int(1)
374 ["timezone"]=>
375 string(%d) "%s"
376 }
377 [16]=>
378 array(3) {
379 [0]=>
380 string(1) "0"
381 [1]=>
382 string(4) " or "
383 [2]=>
384 string(1) "1"
385 }
386 [17]=>
387 array(2) {
388 [0]=>
389 object(Box)#%d (2) {
390 ["p1"]=>
391 object(Point)#%d (2) {
392 ["x"]=>
393 float(2)
394 ["y"]=>
395 float(3)
396 }
397 ["p2"]=>
398 object(Point)#%d (2) {
399 ["x"]=>
400 float(1)
401 ["y"]=>
402 float(2)
403 }
404 }
405 [1]=>
406 object(Box)#%d (2) {
407 ["p1"]=>
408 object(Point)#%d (2) {
409 ["x"]=>
410 float(4)
411 ["y"]=>
412 float(5)
413 }
414 ["p2"]=>
415 object(Point)#%d (2) {
416 ["x"]=>
417 float(3)
418 ["y"]=>
419 float(4)
420 }
421 }
422 }
423 }
424 }
425 Done