16 abstract class Converter implements pq\Converter
20 function __construct(\pq\Types $types) {
21 $this->types = $types;
25 class HStoreConverter extends Converter
27 function convertTypes() {
28 return [ $this->types["hstore"]->oid ];
31 function convertFromString($string, $type) {
32 return eval("return [$string];");
35 function convertToString($data, $type) {
37 foreach ($data as $k => $v) {
39 $string .= sprintf("\"%s\"=>\"%s\",", addslashes($k), addslashes($v));
41 $string .= sprintf("\"%s\"=>NULL,", addslashes($k));
48 class IntVectorConverter extends Converter
50 function convertTypes() {
52 $this->types["int2vector"]->oid,
53 $this->types["oidvector"]->oid
57 function convertFromString($string, $type) {
58 return array_map("intval", explode(" ", $string));
61 function convertToString($data, $type) {
62 return implode(" ", $data);
66 class JSONConverter extends Converter
68 function convertTypes() {
69 return [ $this->types["json"]->oid ];
72 function convertFromString($string, $type) {
73 return json_decode($string, true);
76 function convertToString($data, $type) {
77 return json_encode($data);
84 function __construct($x, $y) {
93 function __construct(Point $p1, Point $p2) {
99 class BoxConverter extends Converter
101 function convertTypes() {
102 return [ $this->types["box"]->oid ];
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
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));
120 function __construct($data) {
123 function __toString() {
124 return (string) $this->data;
128 $c = new pq\Connection(PQ_DSN);
129 $c->exec("CREATE EXTENSION IF NOT EXISTS hstore");
130 $t = new pq\Types($c);
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));
137 $c->setConverter(new BoxConverter($t));
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",
153 2345124, 1431341, 1343423
158 "obj" => (object) array(
166 array(array(array(1,2,3))),
167 array(array("a\"","b}",null)),
184 [new Text(0), new Text(" or "), new Text(true)],
186 [new Box(new Point(1,2), new Point(2,3)), new Box(new Point(3,4), new Point(4,5))],
190 $t["int2vector"]->oid,
191 $t["oidvector"]->oid,
201 $t["timestamp"]->oid,
202 $t["timestamptz"]->oid,
205 $t["timestamp"]->oid,
206 $t["timestamptz"]->oid,
212 var_dump($r->fetchAll());
316 object(pq\DateTime)#%d (4) {
320 string(26) "%d-%d-%d 00:00:00.000000"
327 object(pq\DateTime)#%d (4) {
329 string(11) "Y-m-d H:i:s"
331 string(26) "%d-%d-%d %d:%d:%d.000000"
338 object(pq\DateTime)#%d (4) {
340 string(13) "Y-m-d H:i:s.u"
342 string(26) "%d-%d-%d %d:%d:%d.000000"
349 object(pq\DateTime)#%d (4) {
351 string(14) "Y-m-d H:i:s.uO"
353 string(26) "%d-%d-%d %d:%d:%d.000000"
360 object(pq\DateTime)#%d (4) {
364 string(26) "%d-%d-%d 00:00:00.000000"
371 object(pq\DateTime)#%d (4) {
373 string(11) "Y-m-d H:i:s"
375 string(26) "%d-%d-%d %d:%d:%d.000000"
382 object(pq\DateTime)#%d (4) {
384 string(13) "Y-m-d H:i:s.u"
386 string(26) "%d-%d-%d %d:%d:%d.000000"
393 object(pq\DateTime)#%d (4) {
395 string(14) "Y-m-d H:i:s.uO"
397 string(26) "%d-%d-%d %d:%d:%d.000000"
417 object(Point)#%d (2) {
424 object(Point)#%d (2) {
434 object(Point)#%d (2) {
441 object(Point)#%d (2) {