1
0
mirror of https://github.com/danog/ext-pq.git synced 2024-12-02 17:28:35 +01:00
ext-pq/tests/conv001.phpt
Michael Wallner 6e672843c6 fix test
2014-09-11 16:40:46 +02:00

234 lines
3.7 KiB
PHP

--TEST--
converter
--SKIPIF--
<?php
include "_skipif.inc";
if (!pq\Types::DEFINED) {
die("skip pq\\Types::DEFINED == false\n");
}
?>
--FILE--
<?php
echo "Test\n";
include "_setup.inc";
abstract class Converter implements pq\ConverterInterface
{
protected $types;
function __construct(\pq\Types $types) {
$this->types = $types;
}
}
class HStoreConverter extends Converter
{
function convertTypes() {
return [ $this->types["hstore"]->oid ];
}
function convertFromString($string) {
return eval("return [$string];");
}
function convertToString($data) {
$string = "";
foreach ($data as $k => $v) {
if (isset($v)) {
$string .= sprintf("\"%s\"=>\"%s\",", addslashes($k), addslashes($v));
} else {
$string .= sprintf("\"%s\"=>NULL,", addslashes($k));
}
}
return $string;
}
}
class IntVectorConverter extends Converter
{
function convertTypes() {
return [
$this->types["int2vector"]->oid,
$this->types["oidvector"]->oid
];
}
function convertFromString($string) {
return array_map("intval", explode(" ", $string));
}
function convertToString($data) {
return implode(" ", $data);
}
}
class JSONConverter extends Converter
{
function convertTypes() {
return [ $this->types["json"]->oid ];
}
function convertFromString($string) {
return json_decode($string);
}
function convertToString($data) {
return json_encode($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));
$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",
array(
// hstore
array(
"k1" => "v1",
"k2" => "v2",
"k3" => null
),
// vectors
array(
1, 3, 5, 7, 9, 11
),
array(
2345124, 1431341, 1343423
),
// JSON
(object) array(
"int" => 123,
"obj" => (object) array(
"a" => 1,
"b" => 2,
"c" => 3,
),
"str" => "äüö"
),
// arrays
array(array(array(1,2,3))),
array(array("a\"","b}",null)),
array(true,false),
array(1.1,2.2)
),
array(
$t["hstore"]->oid,
$t["int2vector"]->oid,
$t["oidvector"]->oid,
$t["json"]->oid,
$t["_int4"]->oid,
$t["_text"]->oid,
$t["_bool"]->oid,
$t["_float8"]->oid
)
);
var_dump($r->fetchAll());
?>
Done
--EXPECTF--
Test
array(1) {
[0]=>
array(%d) {
[0]=>
array(3) {
["k1"]=>
string(2) "v1"
["k2"]=>
string(2) "v2"
["k3"]=>
NULL
}
[1]=>
array(6) {
[0]=>
int(1)
[1]=>
int(3)
[2]=>
int(5)
[3]=>
int(7)
[4]=>
int(9)
[5]=>
int(11)
}
[2]=>
array(3) {
[0]=>
int(2345124)
[1]=>
int(1431341)
[2]=>
int(1343423)
}
[3]=>
object(stdClass)#%d (3) {
["int"]=>
int(123)
["obj"]=>
object(stdClass)#%d (3) {
["a"]=>
int(1)
["b"]=>
int(2)
["c"]=>
int(3)
}
["str"]=>
string(6) "äüö"
}
[4]=>
array(1) {
[0]=>
array(1) {
[0]=>
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
}
}
[5]=>
array(1) {
[0]=>
array(3) {
[0]=>
string(2) "a""
[1]=>
string(2) "b}"
[2]=>
NULL
}
}
[6]=>
array(2) {
[0]=>
bool(true)
[1]=>
bool(false)
}
[7]=>
array(2) {
[0]=>
float(1.1)
[1]=>
float(2.2)
}
}
}
Done