From dc87da9c4ec52918e34f2e43eac3014f0f99a8bc Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Sat, 9 Mar 2013 23:15:36 +0100 Subject: [PATCH] flush --- lib/pq/Gateway/Cell.php | 63 ++++------------------- lib/pq/Gateway/Table.php | 57 +++++++++++++++++---- lib/pq/Query/Executor.php | 50 +++++++++++++++++++ lib/pq/Query/Executor/Async.php | 55 ++++++++++++++++++++ lib/pq/Query/ExecutorInterface.php | 30 +++++++++++ lib/pq/Query/Expressible.php | 72 +++++++++++++++++++++++++++ lib/pq/Query/ExpressibleInterface.php | 40 +++++++++++++++ lib/pq/Query/Writer.php | 11 +--- lib/pq/Query/WriterInterface.php | 39 ++++++++++++++- 9 files changed, 343 insertions(+), 74 deletions(-) create mode 100644 lib/pq/Query/Executor.php create mode 100644 lib/pq/Query/Executor/Async.php create mode 100644 lib/pq/Query/ExecutorInterface.php create mode 100644 lib/pq/Query/Expressible.php create mode 100644 lib/pq/Query/ExpressibleInterface.php diff --git a/lib/pq/Gateway/Cell.php b/lib/pq/Gateway/Cell.php index b284b75..efa5fcb 100644 --- a/lib/pq/Gateway/Cell.php +++ b/lib/pq/Gateway/Cell.php @@ -2,9 +2,9 @@ namespace pq\Gateway; -use \pq\Query\Expr; +use \pq\Query\Expressible; -class Cell +class Cell extends Expressible { /** * @var \pq\Gateway\Row @@ -16,11 +16,6 @@ class Cell */ protected $name; - /** - * @var mixed - */ - protected $data; - /** * @var bool */ @@ -33,28 +28,12 @@ class Cell * @param bool $dirty */ function __construct(Row $row, $name, $data, $dirty = false) { + parent::__construct($data); $this->row = $row; $this->name = $name; - $this->data = $data; $this->dirty = $dirty; } - /** - * Get value as string - * @return string - */ - function __toString() { - return (string) $this->data; - } - - /** - * Test whether the value is an unevaluated expression - * @return bool - */ - function isExpr() { - return $this->data instanceof Expr; - } - /** * Check whether the cell has been modified * @return bool @@ -64,46 +43,26 @@ class Cell } /** - * Get value - * @return mixed - */ - function get() { - return $this->data; - } - - /** - * Modify the value in this cell + * Set the value * @param mixed $data - * @param string $op a specific operator * @return \pq\Gateway\Cell */ - function mod($data, $op = null) { - if (!($this->data instanceof Expr)) { - $this->data = new Expr($this->name); - } - - if ($data instanceof Expr) { - $this->data->add($data); - } elseif (!isset($op) && is_numeric($data)) { - $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)); - } - + function set($data) { + parent::set($data); $this->dirty = true; - return $this; } /** - * Set the value in this cell + * Modify the value in this cell * @param mixed $data + * @param string $op a specific operator * @return \pq\Gateway\Cell */ - function set($data) { - $this->data = $data; + function mod($data, $op = null) { + parent::mod($data, $op); $this->dirty = true; return $this; } + } diff --git a/lib/pq/Gateway/Table.php b/lib/pq/Gateway/Table.php index ebab838..2e82a21 100644 --- a/lib/pq/Gateway/Table.php +++ b/lib/pq/Gateway/Table.php @@ -3,6 +3,7 @@ namespace pq\Gateway; use \pq\Query\Writer as QueryWriter; +use \pq\Query\Executor as QueryExecutor; class Table { @@ -27,9 +28,14 @@ class Table protected $rowset = "\\pq\\Gateway\\Rowset"; /** - * @var \pq\Query\Writer + * @var \pq\Query\WriterIterface */ protected $query; + + /** + * @var \pq\Query\ExecutorInterface + */ + protected $exec; /** * @param string $name @@ -73,7 +79,31 @@ class Table * @return \pq\Query\WriterInterface */ function getQueryWriter() { - return $this->query ?: new QueryWriter; + if (!$this->query) { + $this->query = new QueryWriter; + } + return $this->query; + } + + /** + * Set the query executor + * @param \pq\Query\ExecutorInterface $exec + * @return \pq\Gateway\Table + */ + function setQueryExecutor(\pq\Query\ExecutorInterface $exec) { + $this->exec = $exec; + return $this; + } + + /** + * Get the query executor + * @return \pq\Query\ExecutorInterface + */ + function getQueryExecutor() { + if (!$this->exec) { + $this->exec = new QueryExecutor($this->conn); + } + return $this->exec; } /** @@ -92,12 +122,19 @@ class Table /** * Execute the query - * @param \pq\Query\Writer $query + * @param \pq\Query\WriterInterface $query * @return mixed */ protected function execute(QueryWriter $query) { - $result = $query->exec($this->conn); - + return $this->getQueryExecutor()->execute($query, array($this, "onResult")); + } + + /** + * Retreives the result of an executed query + * @param \pq\Result $result + * @return mixed + */ + public function onResult(\pq\Result $result) { if ($result->status != \pq\Result::TUPLES_OK) { return $result; } @@ -111,14 +148,14 @@ class Table return $result; } - + /** * Find rows in the table * @param array $where * @param array|string $order * @param int $limit * @param int $offset - * @return \pq\Result + * @return mixed */ function find(array $where = null, $order = null, $limit = 0, $offset = 0) { $query = $this->getQueryWriter()->reset(); @@ -140,7 +177,7 @@ class Table * Insert a row into the table * @param array $data * @param string $returning - * @return \pq\Result + * @return mixed */ function create(array $data = null, $returning = "*") { $query = $this->getQueryWriter()->reset(); @@ -169,7 +206,7 @@ class Table * @param array $where * @param array $data * @param string $returning - * @retunr \pq\Result + * @retunr mixed */ function update(array $where, array $data, $returning = "*") { $query = $this->getQueryWriter()->reset(); @@ -190,7 +227,7 @@ class Table * Delete rows from the table * @param array $where * @param string $returning - * @return pq\Result + * @return mixed */ function delete(array $where, $returning = null) { $query = $this->getQueryWriter()->reset(); diff --git a/lib/pq/Query/Executor.php b/lib/pq/Query/Executor.php new file mode 100644 index 0000000..a01580a --- /dev/null +++ b/lib/pq/Query/Executor.php @@ -0,0 +1,50 @@ +conn = $conn; + } + + /** + * @inheritdoc + * @return \pq\Connection + */ + function getConnection() { + return $this->conn; + } + + /** + * @inheritdoc + * @param \pq\Connection $conn + * @return \pq\Query\Executor + */ + function setConnection(\pq\Connection $conn) { + $this->conn = $conn; + return $this; + } + + /** + * Execute the query synchronously through \pq\Connection::execParams() + * @param \pq\Query\WriterInterface $query + * @param callable $callback + * @return mixed + */ + function execute(WriterInterface $query, callable $callback) { + return $callback($this->getConnection()->execParams($query, $query->getParams(), $query->getTypes())); + } +} diff --git a/lib/pq/Query/Executor/Async.php b/lib/pq/Query/Executor/Async.php new file mode 100644 index 0000000..d41f53e --- /dev/null +++ b/lib/pq/Query/Executor/Async.php @@ -0,0 +1,55 @@ +conn = $conn; + } + + /** + * Get the connection + * @return \pq\Connection + */ + function getConnection() { + return $this->conn; + } + + /** + * Set the connection + * @param \pq\Connection $conn + * @return \pq\Query\Executor\Async + */ + function setConnection(\pq\Connection $conn) { + $this->conn = $conn; + return $this; + } + + /** + * Execute the query asynchronously through \pq\Connection::execParamsAsync() + * @param \pq\Query\WriterInterface $query + * @param callable $callback + * @return \React\Promise\DeferredPromise + */ + function execute(WriterInterface $query, callable $callback) { + $deferred = new Deferred; // FIXME + $this->getConnection()->execParamsAsync($query, $query->getParams(), $query->getTypes(), + array($deferred->resolver(), "resolve")); + return $deferred->then($callback); + } +} diff --git a/lib/pq/Query/ExecutorInterface.php b/lib/pq/Query/ExecutorInterface.php new file mode 100644 index 0000000..141ac23 --- /dev/null +++ b/lib/pq/Query/ExecutorInterface.php @@ -0,0 +1,30 @@ +data = $data; + } + + /** + * Get value as string + * @return string + */ + function __toString() { + return (string) $this->data; + } + + /** + * Test whether the value is an unevaluated expression + * @return bool + */ + function isExpr() { + return $this->data instanceof Expr; + } + + /** + * Get value + * @return mixed + */ + function get() { + return $this->data; + } + + /** + * Set the value + * @param mixed $data + * @return \pq\Query\Expressible + */ + function set($data) { + $this->data = $data; + return $this; + } + + /** + * Modify the data + * @param mixed $data + * @param string $op a specific operator + * @return \pq\Query\Expressible + */ + function mod($data, $op = null) { + if (!($this->data instanceof Expr)) { + $this->data = new Expr($this->name); + } + + if ($data instanceof Expr) { + $this->data->add($data); + } elseif (!isset($op) && is_numeric($data)) { + $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)); + } + + return $this; + } +} \ No newline at end of file diff --git a/lib/pq/Query/ExpressibleInterface.php b/lib/pq/Query/ExpressibleInterface.php new file mode 100644 index 0000000..1009583 --- /dev/null +++ b/lib/pq/Query/ExpressibleInterface.php @@ -0,0 +1,40 @@ +get(); } if ($param instanceof Expr) { @@ -132,13 +132,4 @@ class Writer implements WriterInterface } return $this; } - - /** - * Execute the query through \pq\Connection::execParams($this, $this->params, $this->types) - * @param \pq\Connection $c - * @return \pq\Result - */ - function exec(\pq\Connection $c) { - return $c->execParams($this, $this->getParams(), $this->getTypes()); - } } diff --git a/lib/pq/Query/WriterInterface.php b/lib/pq/Query/WriterInterface.php index 32e133b..885d4ca 100644 --- a/lib/pq/Query/WriterInterface.php +++ b/lib/pq/Query/WriterInterface.php @@ -2,14 +2,49 @@ namespace pq\Query; +/** + * A query writer which supports easily constructing queries for \pq\Connection::execParams() + */ interface WriterInterface { + /** + * Returns the plain constructed query as string + * @return string + */ function __toString(); + + /** + * Returns a list of parameters as array + * @return array + */ function getParams(); + + /** + * Returns a list any types associated with the params + */ function getTypes(); + + /** + * Reset the state of the query writer + */ + function reset(); + + /** + * Write plain SQL to the query + * @param mixed $arg variable list of arguments, arrays will be imploded to a comm separated list + */ function write(/*...*/); + + /** + * Remember the parameter with any associated type and return $N to be written to the query string + * @param mixed $param a literal parameter, a \pq\Gateway\Table\Cell or a \pq\Query\Expr + * @param int $type the oid of the type of the param + */ function param($param, $type = null); + + /** + * An array of AND/OR criteria + * @param array $criteria + */ function criteria(array $criteria); - function reset(); - function exec(\pq\Connection $c); } -- 2.30.2