$this->data->add(new Expr("+ $data"));
} else {
$data = $this->row->getTable()->getConnection()->quote($data);
- $this->data->add(new Expr("%s %s"), isset($op) ? $op : "||", $data);
+ $this->data->add(new Expr("%s %s", isset($op) ? $op : "||", $data));
}
$this->dirty = true;
function update(array $where, array $data, $returning = "*") {
$query = new QueryWriter("UPDATE ".$this->conn->quoteName($this->name));
$first = true;
- $params = array();
foreach ($data as $key => $val) {
$query->write($first ? "SET" : ",", $key, "=", $query->param($val));
$first and $first = false;
* @param string $e the expression or a format string followed by arguments
* @param string ...
*/
- function __construct($e) {
+ function __construct($e, $arg = null) {
if (func_num_args() > 1) {
$e = call_user_func_array("sprintf", func_get_args());
}
* @return string
*/
function __toString() {
- return (string) $this->expression . $this->next;
+ $string = $this->expression;
+ if ($this->next) {
+ $string .= " " . $this->next;
+ }
+ return (string) $string;
}
/**
* @return \pq\Result
*/
function exec(\pq\Connection $c) {
- return $c->execParams($this, $this->params, $this->types);
+ return $c->execParams($this, $this->getParams(), $this->getTypes());
}
}
--- /dev/null
+<?php
+
+namespace pq\Gateway;
+
+include __DIR__."/../../../setup.inc";
+
+class CellTest extends \PHPUnit_Framework_TestCase {
+
+ /**
+ * @var \pq\Connection
+ */
+ protected $conn;
+
+ /**
+ * @var \pq\Gateway\Table
+ */
+ protected $table;
+
+ protected function setUp() {
+ $this->conn = new \pq\Connection(PQ_DSN);
+ $this->conn->exec(PQ_TEST_DROP_TABLE);
+ $this->conn->exec(PQ_TEST_CREATE_TABLE);
+ $this->conn->exec(PQ_TEST_CREATE_DATA);
+ $this->table = new Table(PQ_TEST_TABLE_NAME, $this->conn);
+ }
+
+ protected function tearDown() {
+ $this->conn->exec(PQ_TEST_DROP_TABLE);
+ }
+
+ /**
+ * This is very bad test…
+ */
+ public function testBasic() {
+ $row = $this->table->find(null, "id desc", 1)->current();
+ foreach ($row->getData() as $key => $val) {
+ $this->assertEquals($val, (string) $row->$key);
+ $this->assertFalse($row->$key->isExpr());
+ $this->assertFalse($row->$key->isDirty());
+ $this->assertSame($val, $row->$key->get());
+ $row->$key->mod(123);
+ $this->assertNotEquals($val, (string) $row->$key);
+ $this->assertTrue($row->$key->isExpr());
+ $this->assertTrue($row->$key->isDirty());
+ $this->assertNotSame($val, $row->$key->get());
+ $this->assertEquals("$key + 123", (string) $row->$key->get());
+ $row->$key->mod("foobar");
+ $this->assertEquals("$key + 123 || 'foobar'", (string) $row->$key);
+ $row->$key->mod(new \pq\Query\Expr(" - %s()", "now"));
+ $this->assertEquals("$key + 123 || 'foobar' - now()", (string) $row->$key);
+ }
+ }
+}
$this->assertCount(1, $rowset);
$this->assertCount(4, $this->table->find());
}
+
+ public function testCreateFail() {
+ $this->setExpectedException("\\pq\\Exception");
+ $rowset = new Rowset($this->table);
+ $rowset->append(new Row($this->table, array("foo" => "bar"), true));
+ $rowset->create();
+ }
public function testUpdate() {
$rowset = $this->table->find();
});
}
+ public function testUpdateFail() {
+ $this->setExpectedException("pq\\Exception");
+ $rowset = $this->table->find();
+ $rowset->apply(function($row) {
+ $row->data = new \pq\Query\Expr("die");
+ });
+ $rowset->update();
+
+ }
+
public function testDelete() {
$this->table->find()->delete();
$this->assertCount(0, $this->table->find());
}
+ public function testDeleteFail() {
+ $this->setExpectedException("pq\\Exception");
+ $rowset = new Rowset($this->table);
+ $rowset->append(new Row($this->table, array("xx" => 0)))->delete();
+ }
+
public function testJsonSerialize() {
$json = sprintf('[{"id":"1","created":"%s","counter":"-1","number":"-1.1","data":"yesterday"}'
.',{"id":"2","created":"%s","counter":"0","number":"0","data":"today"}'