7340c49eb6aea5d86e7370cfa23a5be179086560
[m6w6/ext-pq] / tests / conv001.phpt
1 --TEST--
2 converter
3 --SKIPIF--
4 <?php
5 include "_skipif.inc";
6 ?>
7 --FILE--
8 <?php
9 echo "Test\n";
10
11 include "_setup.inc";
12
13 abstract class Converter implements pq\ConverterInterface
14 {
15 protected $types;
16
17 function __construct(\pq\Types $types) {
18 $this->types = $types;
19 }
20 }
21
22 class HStoreConverter extends Converter
23 {
24 function convertTypes() {
25 return [ $this->types["hstore"]->oid ];
26 }
27
28 function convertFromString($string) {
29 return eval("return [$string];");
30 }
31
32 function convertToString($data) {
33 $string = "";
34 foreach ($data as $k => $v) {
35 if (isset($v)) {
36 $string .= sprintf("\"%s\"=>\"%s\",", addslashes($k), addslashes($v));
37 } else {
38 $string .= sprintf("\"%s\"=>NULL,", addslashes($k));
39 }
40 }
41 return $string;
42 }
43 }
44
45 class IntVectorConverter extends Converter
46 {
47 function convertTypes() {
48 return [
49 $this->types["int2vector"]->oid,
50 $this->types["oidvector"]->oid
51 ];
52 }
53
54 function convertFromString($string) {
55 return array_map("intval", explode(" ", $string));
56 }
57
58 function convertToString($data) {
59 return implode(" ", $data);
60 }
61 }
62
63 class JSONConverter extends Converter
64 {
65 function convertTypes() {
66 return [ $this->types["json"]->oid ];
67 }
68
69 function convertFromString($string) {
70 return json_decode($string);
71 }
72
73 function convertToString($data) {
74 return json_encode($data);
75 }
76 }
77
78 $c = new pq\Connection(PQ_DSN);
79 $c->exec("CREATE EXTENSION IF NOT EXISTS hstore");
80 $t = new pq\Types($c);
81
82 $c->setConverter(new HStoreConverter($t));
83 $c->setConverter(new IntVectorConverter($t));
84 $c->setConverter(new JSONConverter($t));
85
86 $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",
87 array(
88 // hstore
89 array(
90 "k1" => "v1",
91 "k2" => "v2",
92 "k3" => null
93 ),
94 // vectors
95 array(
96 1, 3, 5, 7, 9, 11
97 ),
98 array(
99 2345124, 1431341, 1343423
100 ),
101 // JSON
102 (object) array(
103 "int" => 123,
104 "obj" => (object) array(
105 "a" => 1,
106 "b" => 2,
107 "c" => 3,
108 ),
109 "str" => "äüö"
110 ),
111 // arrays
112 array(array(array(1,2,3))),
113 array(array("a\"","b}",null)),
114 array(true,false),
115 array(1.1,2.2)
116 ),
117 array(
118 $t["hstore"]->oid,
119 $t["int2vector"]->oid,
120 $t["oidvector"]->oid,
121 $t["json"]->oid,
122 $t["_int4"]->oid,
123 $t["_text"]->oid,
124 $t["_bool"]->oid,
125 $t["_float8"]->oid
126 )
127 );
128
129 var_dump($r->fetchAll());
130
131 ?>
132 Done
133 --EXPECTF--
134 Test
135 array(1) {
136 [0]=>
137 array(%d) {
138 [0]=>
139 array(3) {
140 ["k1"]=>
141 string(2) "v1"
142 ["k2"]=>
143 string(2) "v2"
144 ["k3"]=>
145 NULL
146 }
147 [1]=>
148 array(6) {
149 [0]=>
150 int(1)
151 [1]=>
152 int(3)
153 [2]=>
154 int(5)
155 [3]=>
156 int(7)
157 [4]=>
158 int(9)
159 [5]=>
160 int(11)
161 }
162 [2]=>
163 array(3) {
164 [0]=>
165 int(2345124)
166 [1]=>
167 int(1431341)
168 [2]=>
169 int(1343423)
170 }
171 [3]=>
172 object(stdClass)#%d (3) {
173 ["int"]=>
174 int(123)
175 ["obj"]=>
176 object(stdClass)#%d (3) {
177 ["a"]=>
178 int(1)
179 ["b"]=>
180 int(2)
181 ["c"]=>
182 int(3)
183 }
184 ["str"]=>
185 string(6) "äüö"
186 }
187 [4]=>
188 array(1) {
189 [0]=>
190 array(1) {
191 [0]=>
192 array(3) {
193 [0]=>
194 int(1)
195 [1]=>
196 int(2)
197 [2]=>
198 int(3)
199 }
200 }
201 }
202 [5]=>
203 array(1) {
204 [0]=>
205 array(3) {
206 [0]=>
207 string(2) "a""
208 [1]=>
209 string(2) "b}"
210 [2]=>
211 NULL
212 }
213 }
214 [6]=>
215 array(2) {
216 [0]=>
217 bool(true)
218 [1]=>
219 bool(false)
220 }
221 [7]=>
222 array(2) {
223 [0]=>
224 float(1.1)
225 [1]=>
226 float(2.2)
227 }
228 }
229 }
230 Done