X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-pq;a=blobdiff_plain;f=tests%2Fconv001.phpt;h=546f00ff7fff58236d43aefe9123eb6155caf96d;hp=65328c3bb1b630022eac2d8deed75a452cbf0c05;hb=5ba3107011d4050d5ae5877dae80e382dc228e6a;hpb=6e672843c6d37b9d84f632f173d6bdf8025564b2 diff --git a/tests/conv001.phpt b/tests/conv001.phpt index 65328c3..546f00f 100644 --- a/tests/conv001.phpt +++ b/tests/conv001.phpt @@ -3,17 +3,17 @@ converter --SKIPIF-- +--INI-- +date.timezone=UTC --FILE-- types["hstore"]->oid ]; } - function convertFromString($string) { + function convertFromString($string, $type) { return eval("return [$string];"); } - function convertToString($data) { + function convertToString($data, $type) { $string = ""; foreach ($data as $k => $v) { if (isset($v)) { @@ -54,11 +54,11 @@ class IntVectorConverter extends Converter ]; } - function convertFromString($string) { + function convertFromString($string, $type) { return array_map("intval", explode(" ", $string)); } - function convertToString($data) { + function convertToString($data, $type) { return implode(" ", $data); } } @@ -69,24 +69,75 @@ class JSONConverter extends Converter return [ $this->types["json"]->oid ]; } - function convertFromString($string) { - return json_decode($string); + function convertFromString($string, $type) { + return json_decode($string, true); } - function convertToString($data) { + function convertToString($data, $type) { return json_encode($data); } } +class Point { + public $x; + public $y; + function __construct($x, $y) { + $this->x = $x; + $this->y = $y; + } +} + +class Box { + public $p1; + public $p2; + function __construct(Point $p1, Point $p2) { + $this->p1 = $p1; + $this->p2 = $p2; + } +} + +class BoxConverter extends Converter +{ + function convertTypes() { + return [ $this->types["box"]->oid ]; + } + + function convertToString($box, $type) { + return sprintf("(%F,%F),(%F,%F)", + $box->p1->x, $box->p1->y, + $box->p2->x, $box->p2->y + ); + } + + function convertFromString($data, $type) { + list($p1x, $p1y, $p2x, $p2y) = sscanf($data, "(%f,%f),(%f,%f)"); + return new Box(new Point($p1x, $p1y), new Point($p2x, $p2y)); + } +} + +class Text { + private $data; + function __construct($data) { + $this->data = $data; + } + function __toString() { + return (string) $this->data; + } +} + $c = new pq\Connection(PQ_DSN); $c->exec("CREATE EXTENSION IF NOT EXISTS hstore"); $t = new pq\Types($c); $c->setConverter(new HStoreConverter($t)); $c->setConverter(new IntVectorConverter($t)); -$c->setConverter(new JSONConverter($t)); +if (!(defined("pq\\Types::JSON") && defined("pq\\Result::CONV_JSON"))) { + $c->setConverter(new JSONConverter($t)); +} +$c->setConverter(new BoxConverter($t)); -$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", +$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, ". + "\$11 as dt1, \$12 as dt3, \$13 as dt4, \$14 as dt5, \$15 as dt7, \$16 as dt8, \$17 as txta, \$18 as boxa", array( // hstore array( @@ -115,7 +166,22 @@ $r = $c->execParams("SELECT \$1 as hs, \$2 as iv, \$3 as oids, \$4 as js, \$5 as array(array(array(1,2,3))), array(array("a\"","b}",null)), array(true,false), - array(1.1,2.2) + array(1.1,2.2), + // double + 123.456, + // bool + true, + // datetimes + new pq\Datetime, + new pq\Datetime, + new pq\Datetime, + new pq\Datetime, + new pq\Datetime, + new pq\Datetime, + // text array + [new Text(0), new Text(" or "), new Text(true)], + // box array + [new Box(new Point(1,2), new Point(2,3)), new Box(new Point(3,4), new Point(4,5))], ), array( $t["hstore"]->oid, @@ -125,7 +191,17 @@ $r = $c->execParams("SELECT \$1 as hs, \$2 as iv, \$3 as oids, \$4 as js, \$5 as $t["_int4"]->oid, $t["_text"]->oid, $t["_bool"]->oid, - $t["_float8"]->oid + $t["_float8"]->oid, + $t["float4"]->oid, + $t["bool"]->oid, + $t["date"]->oid, + $t["timestamp"]->oid, + $t["timestamptz"]->oid, + $t["date"]->oid, + $t["timestamp"]->oid, + $t["timestamptz"]->oid, + $t["_text"]->oid, + $t["_box"]->oid ) ); @@ -172,11 +248,11 @@ array(1) { int(1343423) } [3]=> - object(stdClass)#%d (3) { + array(3) { ["int"]=> int(123) ["obj"]=> - object(stdClass)#%d (3) { + array(3) { ["a"]=> int(1) ["b"]=> @@ -228,6 +304,122 @@ array(1) { [1]=> float(2.2) } + [8]=> + float(123.456) + [9]=> + bool(true) + [10]=> + object(pq\DateTime)#%d (4) { + ["format"]=> + string(5) "Y-m-d" + ["date"]=> + string(26) "%d-%d-%d 00:00:00.000000" + ["timezone_type"]=> + int(3) + ["timezone"]=> + string(3) "UTC" + } + [11]=> + object(pq\DateTime)#%d (4) { + ["format"]=> + string(13) "Y-m-d H:i:s.u" + ["date"]=> + string(26) "%d-%d-%d %d:%d:%d.%d" + ["timezone_type"]=> + int(3) + ["timezone"]=> + string(3) "UTC" + } + [12]=> + object(pq\DateTime)#%d (4) { + ["format"]=> + string(14) "Y-m-d H:i:s.uO" + ["date"]=> + string(26) "%d-%d-%d %d:%d:%d.%d" + ["timezone_type"]=> + int(1) + ["timezone"]=> + string(%d) "%s" + } + [13]=> + object(pq\DateTime)#%d (4) { + ["format"]=> + string(5) "Y-m-d" + ["date"]=> + string(26) "%d-%d-%d 00:00:00.000000" + ["timezone_type"]=> + int(3) + ["timezone"]=> + string(3) "UTC" + } + [14]=> + object(pq\DateTime)#%d (4) { + ["format"]=> + string(13) "Y-m-d H:i:s.u" + ["date"]=> + string(26) "%d-%d-%d %d:%d:%d.%d" + ["timezone_type"]=> + int(3) + ["timezone"]=> + string(3) "UTC" + } + [15]=> + object(pq\DateTime)#%d (4) { + ["format"]=> + string(14) "Y-m-d H:i:s.uO" + ["date"]=> + string(26) "%d-%d-%d %d:%d:%d.%d" + ["timezone_type"]=> + int(1) + ["timezone"]=> + string(%d) "%s" + } + [16]=> + array(3) { + [0]=> + string(1) "0" + [1]=> + string(4) " or " + [2]=> + string(1) "1" + } + [17]=> + array(2) { + [0]=> + object(Box)#%d (2) { + ["p1"]=> + object(Point)#%d (2) { + ["x"]=> + float(2) + ["y"]=> + float(3) + } + ["p2"]=> + object(Point)#%d (2) { + ["x"]=> + float(1) + ["y"]=> + float(2) + } + } + [1]=> + object(Box)#%d (2) { + ["p1"]=> + object(Point)#%d (2) { + ["x"]=> + float(4) + ["y"]=> + float(5) + } + ["p2"]=> + object(Point)#%d (2) { + ["x"]=> + float(3) + ["y"]=> + float(4) + } + } + } } } Done