65328c3bb1b630022eac2d8deed75a452cbf0c05
[m6w6/ext-pq] / tests / conv001.phpt
1 --TEST--
2 converter
3 --SKIPIF--
4 <?php
5 include "_skipif.inc";
6 if (!pq\Types::DEFINED) {
7 die("skip pq\\Types::DEFINED == false\n");
8 }
9 ?>
10 --FILE--
11 <?php
12 echo "Test\n";
13
14 include "_setup.inc";
15
16 abstract class Converter implements pq\ConverterInterface
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) {
32 return eval("return [$string];");
33 }
34
35 function convertToString($data) {
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) {
58 return array_map("intval", explode(" ", $string));
59 }
60
61 function convertToString($data) {
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) {
73 return json_decode($string);
74 }
75
76 function convertToString($data) {
77 return json_encode($data);
78 }
79 }
80
81 $c = new pq\Connection(PQ_DSN);
82 $c->exec("CREATE EXTENSION IF NOT EXISTS hstore");
83 $t = new pq\Types($c);
84
85 $c->setConverter(new HStoreConverter($t));
86 $c->setConverter(new IntVectorConverter($t));
87 $c->setConverter(new JSONConverter($t));
88
89 $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",
90 array(
91 // hstore
92 array(
93 "k1" => "v1",
94 "k2" => "v2",
95 "k3" => null
96 ),
97 // vectors
98 array(
99 1, 3, 5, 7, 9, 11
100 ),
101 array(
102 2345124, 1431341, 1343423
103 ),
104 // JSON
105 (object) array(
106 "int" => 123,
107 "obj" => (object) array(
108 "a" => 1,
109 "b" => 2,
110 "c" => 3,
111 ),
112 "str" => "äüö"
113 ),
114 // arrays
115 array(array(array(1,2,3))),
116 array(array("a\"","b}",null)),
117 array(true,false),
118 array(1.1,2.2)
119 ),
120 array(
121 $t["hstore"]->oid,
122 $t["int2vector"]->oid,
123 $t["oidvector"]->oid,
124 $t["json"]->oid,
125 $t["_int4"]->oid,
126 $t["_text"]->oid,
127 $t["_bool"]->oid,
128 $t["_float8"]->oid
129 )
130 );
131
132 var_dump($r->fetchAll());
133
134 ?>
135 Done
136 --EXPECTF--
137 Test
138 array(1) {
139 [0]=>
140 array(%d) {
141 [0]=>
142 array(3) {
143 ["k1"]=>
144 string(2) "v1"
145 ["k2"]=>
146 string(2) "v2"
147 ["k3"]=>
148 NULL
149 }
150 [1]=>
151 array(6) {
152 [0]=>
153 int(1)
154 [1]=>
155 int(3)
156 [2]=>
157 int(5)
158 [3]=>
159 int(7)
160 [4]=>
161 int(9)
162 [5]=>
163 int(11)
164 }
165 [2]=>
166 array(3) {
167 [0]=>
168 int(2345124)
169 [1]=>
170 int(1431341)
171 [2]=>
172 int(1343423)
173 }
174 [3]=>
175 object(stdClass)#%d (3) {
176 ["int"]=>
177 int(123)
178 ["obj"]=>
179 object(stdClass)#%d (3) {
180 ["a"]=>
181 int(1)
182 ["b"]=>
183 int(2)
184 ["c"]=>
185 int(3)
186 }
187 ["str"]=>
188 string(6) "äüö"
189 }
190 [4]=>
191 array(1) {
192 [0]=>
193 array(1) {
194 [0]=>
195 array(3) {
196 [0]=>
197 int(1)
198 [1]=>
199 int(2)
200 [2]=>
201 int(3)
202 }
203 }
204 }
205 [5]=>
206 array(1) {
207 [0]=>
208 array(3) {
209 [0]=>
210 string(2) "a""
211 [1]=>
212 string(2) "b}"
213 [2]=>
214 NULL
215 }
216 }
217 [6]=>
218 array(2) {
219 [0]=>
220 bool(true)
221 [1]=>
222 bool(false)
223 }
224 [7]=>
225 array(2) {
226 [0]=>
227 float(1.1)
228 [1]=>
229 float(2.2)
230 }
231 }
232 }
233 Done