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 dt3, \$13 as dt4, \$14 as dt5, \$15 as dt7, \$16 as dt8, \$17 as txta, \$18 as boxa",
153 2345124, 1431341, 1343423
158 "obj" => (object) array(
166 array(array(array(1,2,3))),
167 array(array("a\"","b}",null)),
182 [new Text(0), new Text(" or "), new Text(true)],
184 [new Box(new Point(1,2), new Point(2,3)), new Box(new Point(3,4), new Point(4,5))],
188 $t["int2vector"]->oid,
189 $t["oidvector"]->oid,
198 $t["timestamp"]->oid,
199 $t["timestamptz"]->oid,
201 $t["timestamp"]->oid,
202 $t["timestamptz"]->oid,
208 var_dump($r->fetchAll());
312 object(pq\DateTime)#%d (4) {
316 string(26) "%d-%d-%d 00:00:00.000000"
323 object(pq\DateTime)#%d (4) {
325 string(13) "Y-m-d H:i:s.u"
327 string(26) "%d-%d-%d %d:%d:%d.%d"
334 object(pq\DateTime)#%d (4) {
336 string(14) "Y-m-d H:i:s.uO"
338 string(26) "%d-%d-%d %d:%d:%d.%d"
345 object(pq\DateTime)#%d (4) {
349 string(26) "%d-%d-%d 00:00:00.000000"
356 object(pq\DateTime)#%d (4) {
358 string(13) "Y-m-d H:i:s.u"
360 string(26) "%d-%d-%d %d:%d:%d.%d"
367 object(pq\DateTime)#%d (4) {
369 string(14) "Y-m-d H:i:s.uO"
371 string(26) "%d-%d-%d %d:%d:%d.%d"
391 object(Point)#%d (2) {
398 object(Point)#%d (2) {
408 object(Point)#%d (2) {
415 object(Point)#%d (2) {