From 141c2f1066710f20fced09a84e66ce9ed73dcdd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Pude=C5=82ek?= Date: Fri, 1 Sep 2017 18:07:21 +0200 Subject: [PATCH 1/5] fix test directory --- phpunit.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index 71da1eb..164567e 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -12,7 +12,7 @@ syntaxCheck="false"> - tests/Pixie/ + tests/Pecee/Pixie/ - \ No newline at end of file + From b32243cf2e99787bbb52775e2a166d300e8fa76c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Pude=C5=82ek?= Date: Fri, 1 Sep 2017 18:07:53 +0200 Subject: [PATCH 2/5] make library STABLE --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 7143909..6c19420 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "sqlite" ], "license": "MIT", - "minimum-stability": "dev", + "minimum-stability": "stable", "authors": [ { "name": "Simon Sessingø", @@ -35,7 +35,7 @@ ], "require": { "php": ">=5.4.0", - "usmanhalalit/viocon": "1.0.*@dev" + "usmanhalalit/viocon": "1.0.1" }, "require-dev": { "phpunit/phpunit": "^4.8", From e31297ea8b6181e4b73ff44a88aafae78e04faee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Pude=C5=82ek?= Date: Fri, 1 Sep 2017 20:07:05 +0200 Subject: [PATCH 3/5] fix phpdoc comment --- src/Pecee/Pixie/Connection.php | 15 +- .../Pixie/ConnectionAdapters/BaseAdapter.php | 6 + src/Pecee/Pixie/ConnectionAdapters/Mysql.php | 6 + src/Pecee/Pixie/ConnectionAdapters/Pgsql.php | 7 +- src/Pecee/Pixie/ConnectionAdapters/Sqlite.php | 7 +- src/Pecee/Pixie/EventHandler.php | 31 ++- src/Pecee/Pixie/Exception.php | 5 + .../QueryBuilder/Adapters/BaseAdapter.php | 69 +++-- .../Pixie/QueryBuilder/Adapters/Mysql.php | 5 + .../Pixie/QueryBuilder/Adapters/Pgsql.php | 5 + .../Pixie/QueryBuilder/Adapters/Sqlite.php | 6 + src/Pecee/Pixie/QueryBuilder/JoinBuilder.php | 18 +- .../Pixie/QueryBuilder/NestedCriteria.php | 17 +- .../QueryBuilder/QueryBuilderHandler.php | 262 +++++++++++------- src/Pecee/Pixie/QueryBuilder/QueryObject.php | 23 +- src/Pecee/Pixie/QueryBuilder/Raw.php | 8 + src/Pecee/Pixie/QueryBuilder/Transaction.php | 5 + .../QueryBuilder/TransactionHaltException.php | 5 + 18 files changed, 341 insertions(+), 159 deletions(-) diff --git a/src/Pecee/Pixie/Connection.php b/src/Pecee/Pixie/Connection.php index f517c7a..cdf7654 100644 --- a/src/Pecee/Pixie/Connection.php +++ b/src/Pecee/Pixie/Connection.php @@ -1,9 +1,15 @@ getStatements(); - $tables = isset($statements['tables']) ? $statements['tables'] : []; + $tables = isset($statements['tables']) ? $statements['tables'] : []; // Events added with :any will be fired in case of any table, // we are adding :any as a fake table at the beginning. @@ -105,7 +116,7 @@ public function fireEvents($queryBuilder, $event) unset($handlerParams[1]); // we do not need $event // Add to fired list $this->firedEvents[] = $eventId; - $result = call_user_func_array($action, $handlerParams); + $result = call_user_func_array($action, $handlerParams); if ($result !== null) { return $result; } @@ -114,4 +125,4 @@ public function fireEvents($queryBuilder, $event) return null; } -} \ No newline at end of file +} diff --git a/src/Pecee/Pixie/Exception.php b/src/Pecee/Pixie/Exception.php index b9f29bd..de6650f 100644 --- a/src/Pecee/Pixie/Exception.php +++ b/src/Pecee/Pixie/Exception.php @@ -1,6 +1,11 @@ connection = $connection; - $this->container = $this->connection->getContainer(); + $this->container = $this->connection->getContainer(); } /** @@ -43,7 +56,7 @@ public function select($statements) // From $fromEnabled = false; - $tables = ''; + $tables = ''; if (isset($statements['tables'])) { $tables = []; @@ -61,7 +74,7 @@ public function select($statements) $tables[] = $t; } - $tables = join(',', $tables); + $tables = join(',', $tables); $fromEnabled = true; } // Select @@ -88,7 +101,7 @@ public function select($statements) } // Limit and offset - $limit = isset($statements['limit']) ? 'LIMIT ' . $statements['limit'] : ''; + $limit = isset($statements['limit']) ? 'LIMIT ' . $statements['limit'] : ''; $offset = isset($statements['offset']) ? 'OFFSET ' . $statements['offset'] : ''; // Having @@ -144,8 +157,8 @@ public function criteriaOnly($statements, $bindValues = true) /** * Build a generic insert/ignore/replace query * - * @param array $statements - * @param array $data + * @param array $statements + * @param array $data * @param string $type * * @return array @@ -162,7 +175,7 @@ private function doInsert($statements, array $data, $type) if ($value instanceof Raw) { $values[] = (string)$value; } else { - $values[] = '?'; + $values[] = '?'; $bindings[] = $value; } } @@ -181,7 +194,7 @@ private function doInsert($statements, array $data, $type) } list($updateStatement, $updateBindings) = $this->getUpdateStatement($statements['onduplicate']); $sqlArray[] = 'ON DUPLICATE KEY UPDATE ' . $updateStatement; - $bindings = array_merge($bindings, $updateBindings); + $bindings = array_merge($bindings, $updateBindings); } $sql = $this->concatenateQuery($sqlArray); @@ -240,14 +253,14 @@ public function replace($statements, array $data) */ private function getUpdateStatement($data) { - $bindings = []; + $bindings = []; $statement = ''; foreach ($data as $key => $value) { if ($value instanceof Raw) { $statement .= $this->wrapSanitizer($key) . '=' . $value . ','; } else { - $statement .= $this->wrapSanitizer($key) . '=?,'; + $statement .= $this->wrapSanitizer($key) . '=?,'; $bindings[] = $value; } } @@ -314,7 +327,7 @@ public function delete($statements) list($whereCriteria, $whereBindings) = $this->buildCriteriaWithType($statements, 'wheres', 'WHERE'); $sqlArray = ['DELETE FROM', $this->wrapSanitizer($table), $whereCriteria]; - $sql = $this->concatenateQuery($sqlArray); + $sql = $this->concatenateQuery($sqlArray); $bindings = $whereBindings; return compact('sql', 'bindings'); @@ -326,7 +339,7 @@ public function delete($statements) * * @param array $pieces * @param $glue - * @param bool $wrapSanitizer + * @param bool $wrapSanitizer * * @return string */ @@ -369,7 +382,8 @@ protected function concatenateQuery(array $pieces) * Build generic criteria string and bindings from statements, like "a = b and c = ?" * * @param array $statements - * @param bool $bindValues + * @param bool $bindValues + * * @throws Exception * * @return array @@ -379,7 +393,7 @@ protected function buildCriteria($statements, $bindValues = true) $criteria = ''; $bindings = []; foreach ($statements as $statement) { - $key = $this->wrapSanitizer($statement['key']); + $key = $this->wrapSanitizer($statement['key']); $value = $statement['value']; if ($value === null && $key instanceof \Closure) { @@ -403,7 +417,7 @@ protected function buildCriteria($statements, $bindValues = true) $bindings = array_merge($bindings, $queryObject->getBindings()); // Append the sql we get from the nestedCriteria object $criteria .= $statement['joiner'] . ' (' . $queryObject->getSql() . ') '; - } elseif (is_array($value)) { + } else if (is_array($value)) { // where_in or between like query $criteria .= $statement['joiner'] . ' ' . $key . ' ' . $statement['operator']; @@ -414,14 +428,14 @@ protected function buildCriteria($statements, $bindValues = true) $valuePlaceholder = ''; foreach ($statement['value'] as $subValue) { $valuePlaceholder .= '?, '; - $bindings[] = $subValue; + $bindings[] = $subValue; } $valuePlaceholder = trim($valuePlaceholder, ', '); - $criteria .= ' (' . $valuePlaceholder . ') '; + $criteria .= ' (' . $valuePlaceholder . ') '; } - } elseif ($value instanceof Raw) { + } else if ($value instanceof Raw) { $criteria .= "{$statement['joiner']} {$key} {$statement['operator']} $value "; } else { // Usual where like criteria @@ -430,13 +444,13 @@ protected function buildCriteria($statements, $bindValues = true) // Specially for joins // We are not binding values, lets sanitize then - $value = $this->wrapSanitizer($value); + $value = $this->wrapSanitizer($value); $criteria .= $statement['joiner'] . ' ' . $key . ' ' . $statement['operator'] . ' ' . $value . ' '; - } elseif ($statement['key'] instanceof Raw) { + } else if ($statement['key'] instanceof Raw) { if ($statement['operator'] !== null) { - $criteria .= "{$statement['joiner']} {$key} {$statement['operator']} ? "; - $bindings = array_merge($bindings, $statement['key']->getBindings()); + $criteria .= "{$statement['joiner']} {$key} {$statement['operator']} ? "; + $bindings = array_merge($bindings, $statement['key']->getBindings()); $bindings[] = $value; } else { $criteria .= $statement['joiner'] . ' ' . $key . ' '; @@ -447,8 +461,8 @@ protected function buildCriteria($statements, $bindValues = true) // For wheres $valuePlaceholder = '?'; - $bindings[] = $value; - $criteria .= $statement['joiner'] . ' ' . $key . ' ' . $statement['operator'] . ' ' . $valuePlaceholder . ' '; + $bindings[] = $value; + $criteria .= $statement['joiner'] . ' ' . $key . ' ' . $statement['operator'] . ' ' . $valuePlaceholder . ' '; } } } @@ -471,7 +485,7 @@ public function wrapSanitizer($value) // Its a raw query, just cast as string, object has __toString() if ($value instanceof Raw) { return (string)$value; - } elseif ($value instanceof \Closure) { + } else if ($value instanceof \Closure) { return $value; } @@ -518,6 +532,7 @@ protected function buildCriteriaWithType($statements, $key, $type, $bindValues = * Build join string * * @param $statements + * * @throws Exception * @return string */ @@ -556,4 +571,4 @@ protected function buildJoin($statements) return $sql; } -} \ No newline at end of file +} diff --git a/src/Pecee/Pixie/QueryBuilder/Adapters/Mysql.php b/src/Pecee/Pixie/QueryBuilder/Adapters/Mysql.php index 251321b..81ef40c 100644 --- a/src/Pecee/Pixie/QueryBuilder/Adapters/Mysql.php +++ b/src/Pecee/Pixie/QueryBuilder/Adapters/Mysql.php @@ -1,6 +1,11 @@ addTablePrefix($key); - $value = $this->addTablePrefix($value); + $key = $this->addTablePrefix($key); + $value = $this->addTablePrefix($value); $this->statements['criteria'][] = compact('key', 'operator', 'value', 'joiner'); return $this; diff --git a/src/Pecee/Pixie/QueryBuilder/NestedCriteria.php b/src/Pecee/Pixie/QueryBuilder/NestedCriteria.php index aa68d10..57184f8 100644 --- a/src/Pecee/Pixie/QueryBuilder/NestedCriteria.php +++ b/src/Pecee/Pixie/QueryBuilder/NestedCriteria.php @@ -1,18 +1,25 @@ addTablePrefix($key); + $key = $this->addTablePrefix($key); $this->statements['criteria'][] = compact('key', 'operator', 'value', 'joiner'); return $this; diff --git a/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php b/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php index b14c1ac..bbddf2b 100644 --- a/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php +++ b/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php @@ -6,6 +6,11 @@ use Pecee\Pixie\Connection; use Pecee\Pixie\Exception; +/** + * Class QueryBuilderHandler + * + * @package Pecee\Pixie\QueryBuilder + */ class QueryBuilderHandler { @@ -50,6 +55,14 @@ class QueryBuilderHandler * @var array */ protected $fetchParameters = [\PDO::FETCH_OBJ]; + /** + * @var string + */ + protected $adapter; + /** + * @var array + */ + protected $adapterConfig; /** * @param \Pecee\Pixie\Connection|null $connection @@ -62,10 +75,10 @@ public function __construct(Connection $connection = null) throw new Exception('No database connection found.', 1); } - $this->connection = $connection; - $this->container = $this->connection->getContainer(); - $this->pdo = $this->connection->getPdoInstance(); - $this->adapter = $this->connection->getAdapter(); + $this->connection = $connection; + $this->container = $this->connection->getContainer(); + $this->pdo = $this->connection->getPdoInstance(); + $this->adapter = $this->connection->getAdapter(); $this->adapterConfig = $this->connection->getAdapterConfig(); if (isset($this->adapterConfig['prefix'])) { @@ -84,10 +97,9 @@ public function __construct(Connection $connection = null) /** * Set the fetch mode * - * @param string $mode * @return static */ - public function setFetchMode($mode) + public function setFetchMode() { $this->fetchParameters = func_get_args(); @@ -97,8 +109,9 @@ public function setFetchMode($mode) /** * Fetch query results as object of specified type * - * @param $className - * @param array $constructorArgs + * @param string $className + * @param array $constructorArgs + * * @return QueryBuilderHandler */ public function asObject($className, array $constructorArgs = []) @@ -108,6 +121,7 @@ public function asObject($className, array $constructorArgs = []) /** * @param \Pecee\Pixie\Connection|null $connection + * * @throws \Pecee\Pixie\Exception * @return static */ @@ -122,7 +136,7 @@ public function newQuery(Connection $connection = null) /** * @param string $sql - * @param array $bindings + * @param array $bindings * * @return static */ @@ -139,6 +153,7 @@ public function query($sql, array $bindings = []) * * @param string $table * @param string $alias + * * @return QueryBuilderHandler */ public function alias($table, $alias) @@ -153,9 +168,11 @@ public function alias($table, $alias) * Example: table AS alias * * @deprecated This method will be removed in the near future, please use QueryBuilderHandler::alias instead. - * @see QueryBuilderHandler::alias + * @see QueryBuilderHandler::alias + * * @param string $table * @param string $alias + * * @return QueryBuilderHandler */ public function prefix($table, $alias) @@ -164,14 +181,17 @@ public function prefix($table, $alias) } /** - * @param $sql - * @param array $bindings + * @param string $sql + * @param array $bindings * * @return array PDOStatement and execution time as float */ public function statement($sql, $bindings = []) { - $start = microtime(true); + /** + * @var $start float + */ + $start = microtime(true); $pdoStatement = $this->pdo->prepare($sql); foreach ($bindings as $key => $value) { $pdoStatement->bindValue( @@ -187,12 +207,17 @@ public function statement($sql, $bindings = []) /** * Get all rows + * * @throws Exception - * @return \stdClass|array|null + * @return \stdClass[] */ public function get() { - $queryObject = null; + /** + * @var $executionTime float + * @var $start float + */ + $queryObject = null; $executionTime = 0; if ($this->pdoStatement === null) { $queryObject = $this->getQuery('select'); @@ -204,8 +229,8 @@ public function get() $start = microtime(true); $this->fireEvents('before-select', $queryObject); - $result = call_user_func_array([$this->pdoStatement, 'fetchAll'], $this->fetchParameters); - $executionTime += microtime(true) - $start; + $result = call_user_func_array([$this->pdoStatement, 'fetchAll'], $this->fetchParameters); + $executionTime += microtime(true) - $start; $this->pdoStatement = null; $this->fireEvents('after-select', $queryObject, $result, $executionTime); @@ -214,6 +239,7 @@ public function get() /** * Get first row + * * @throws Exception * @return \stdClass|null */ @@ -222,14 +248,14 @@ public function first() $this->limit(1); $result = $this->get(); - return null === $result ? null : $result[0]; + return empty($result) ? null : $result[0]; } /** - * @param $value - * @param string $fieldName - * @throws Exception - * @return null|\stdClass + * @param string $fieldName + * @param string|int|float $value + * + * @return \stdClass[] */ public function findAll($fieldName, $value) { @@ -239,8 +265,9 @@ public function findAll($fieldName, $value) } /** - * @param $value - * @param string $fieldName + * @param string|int|float $value + * @param string $fieldName + * * @throws Exception * @return null|\stdClass */ @@ -253,6 +280,7 @@ public function find($value, $fieldName = 'id') /** * Get count of rows + * * @throws Exception * @return int */ @@ -263,14 +291,15 @@ public function count() unset($this->statements['orderBys'], $this->statements['limit'], $this->statements['offset']); - $count = $this->aggregate('count'); + $count = $this->aggregate('count'); $this->statements = $originalStatements; return $count; } /** - * @param $type + * @param string $type + * * @throws Exception * @return int */ @@ -280,7 +309,7 @@ protected function aggregate($type) $mainSelects = isset($this->statements['selects']) ? $this->statements['selects'] : null; // Replace select with a scalar value like `count` $this->statements['selects'] = [$this->raw($type . '(*) as field')]; - $row = $this->get(); + $row = $this->get(); // Set the select as it was if ($mainSelects) { @@ -302,7 +331,7 @@ protected function aggregate($type) } /** - * @param string $type + * @param string $type * @param array|bool $dataToBePassed * * @return QueryObject @@ -325,7 +354,8 @@ public function getQuery($type = 'select', $dataToBePassed = []) /** * @param QueryBuilderHandler $queryBuilder - * @param null $alias + * @param string|null $alias + * * @throws Exception * @return Raw */ @@ -340,8 +370,9 @@ public function subQuery(QueryBuilderHandler $queryBuilder, $alias = null) } /** - * @param array $data + * @param array $data * @param string $type + * * @throws Exception * @return array|string */ @@ -377,7 +408,8 @@ private function doInsert($data, $type) } /** - * @param $data + * @param array $data + * * @throws Exception * @return array|string */ @@ -387,7 +419,8 @@ public function insert($data) } /** - * @param $data + * @param array $data + * * @throws Exception * @return array|string */ @@ -397,7 +430,8 @@ public function insertIgnore($data) } /** - * @param $data + * @param array $data + * * @throws Exception * @return array|string */ @@ -407,12 +441,17 @@ public function replace($data) } /** - * @param string $data + * @param array $data + * * @throws Exception - * @return static + * @return \PDOStatement */ public function update($data) { + /** + * @var $response \PDOStatement + * @var $executionTime float + */ $queryObject = $this->getQuery('update', $data); $this->fireEvents('before-update', $queryObject); @@ -424,9 +463,9 @@ public function update($data) } /** - * @param $data - * @throws Exception - * @return array|string + * @param array $data + * + * @return array|\PDOStatement|string */ public function updateOrInsert($data) { @@ -439,6 +478,7 @@ public function updateOrInsert($data) /** * @param string $data + * * @return static */ public function onDuplicateKeyUpdate($data) @@ -449,10 +489,14 @@ public function onDuplicateKeyUpdate($data) } /** - * @throws Exception + * @return \PDOStatement */ public function delete() { + /** + * @var $response \PDOStatement + * @var $executionTime float + */ $queryObject = $this->getQuery('delete'); $this->fireEvents('before-delete', $queryObject); @@ -464,7 +508,8 @@ public function delete() } /** - * @param $tables array|string Single table or multiple tables as an array or as multiple parameters + * @param $tables string|string[] Single table or multiple tables as an array or as multiple parameters + * * @throws Exception * @return static */ @@ -477,14 +522,14 @@ public function table($tables) } $instance = new static($this->connection); - $tables = $this->addTablePrefix($tables, false); + $tables = $this->addTablePrefix($tables, false); $instance->addStatement('tables', $tables); return $instance; } /** - * @param array|string $tables + * @param string|string[] $tables * * @return static */ @@ -501,12 +546,13 @@ public function from($tables) } /** - * @param array|string $fields + * @param string|string[] $fields,... + * * @return static */ public function select($fields) { - if (is_array($fields) === false) { + if (!is_array($fields)) { $fields = func_get_args(); } @@ -518,6 +564,7 @@ public function select($fields) /** * @param array|string $fields + * * @return static */ public function selectDistinct($fields) @@ -543,22 +590,22 @@ public function groupBy($field) /** * @param string|array $fields - * @param string $defaultDirection + * @param string $defaultDirection * * @return static */ public function orderBy($fields, $defaultDirection = 'ASC') { - if (is_array($fields) === false) { + if (!is_array($fields)) { $fields = [$fields]; } foreach ((array)$fields as $key => $value) { $field = $key; - $type = $value; + $type = $value; if (is_int($key)) { $field = $value; - $type = $defaultDirection; + $type = $defaultDirection; } if (!$field instanceof Raw) { $field = $this->addTablePrefix($field); @@ -571,6 +618,7 @@ public function orderBy($fields, $defaultDirection = 'ASC') /** * @param int $limit + * * @return static */ public function limit($limit) @@ -582,6 +630,7 @@ public function limit($limit) /** * @param int $offset + * * @return static $this */ public function offset($offset) @@ -592,23 +641,23 @@ public function offset($offset) } /** - * @param string $key + * @param string $key * @param string|mixed $operator * @param string|mixed $value - * @param string $joiner + * @param string $joiner * * @return static */ public function having($key, $operator, $value, $joiner = 'AND') { - $key = $this->addTablePrefix($key); + $key = $this->addTablePrefix($key); $this->statements['havings'][] = compact('key', 'operator', 'value', 'joiner'); return $this; } /** - * @param string $key + * @param string $key * @param string|mixed $operator * @param string|mixed $value * @@ -620,7 +669,7 @@ public function orHaving($key, $operator, $value) } /** - * @param string $key + * @param string $key * @param string|mixed $operator * @param string|mixed $value * @@ -630,7 +679,7 @@ public function where($key, $operator = null, $value = null) { // If two params are given then assume operator is = if (func_num_args() === 2) { - $value = $operator; + $value = $operator; $operator = '='; } @@ -642,7 +691,7 @@ public function where($key, $operator = null, $value = null) } /** - * @param string $key + * @param string $key * @param string|mixed $operator * @param string|mixed $value * @@ -652,7 +701,7 @@ public function orWhere($key, $operator = null, $value = null) { // If two params are given then assume operator is = if (func_num_args() === 2) { - $value = $operator; + $value = $operator; $operator = '='; } @@ -660,7 +709,7 @@ public function orWhere($key, $operator = null, $value = null) } /** - * @param string $key + * @param string $key * @param string|mixed $operator * @param string|mixed $value * @@ -670,7 +719,7 @@ public function whereNot($key, $operator = null, $value = null) { // If two params are given then assume operator is = if (func_num_args() === 2) { - $value = $operator; + $value = $operator; $operator = '='; } @@ -678,7 +727,7 @@ public function whereNot($key, $operator = null, $value = null) } /** - * @param string $key + * @param string $key * @param string|mixed $operator * @param string|mixed $value * @@ -688,7 +737,7 @@ public function orWhereNot($key, $operator = null, $value = null) { // If two params are given then assume operator is = if (func_num_args() === 2) { - $value = $operator; + $value = $operator; $operator = '='; } @@ -696,8 +745,8 @@ public function orWhereNot($key, $operator = null, $value = null) } /** - * @param string $key - * @param array $values + * @param string $key + * @param string[]|int[]|float[] $values * * @return static */ @@ -707,8 +756,8 @@ public function whereIn($key, $values) } /** - * @param string $key - * @param array $values + * @param string $key + * @param string[]|integer[]|float[] $values * * @return static */ @@ -718,8 +767,8 @@ public function whereNotIn($key, $values) } /** - * @param string $key - * @param array $values + * @param string $key + * @param string[]|integer[]|float[] $values * * @return static */ @@ -729,8 +778,8 @@ public function orWhereIn($key, $values) } /** - * @param string $key - * @param array $values + * @param string $key + * @param string[]|integer[]|float[] $values * * @return static */ @@ -740,9 +789,9 @@ public function orWhereNotIn($key, $values) } /** - * @param string $key - * @param string $valueFrom - * @param string $valueTo + * @param string $key + * @param string|integer|float $valueFrom + * @param string|integer|float $valueTo * * @return static */ @@ -752,9 +801,9 @@ public function whereBetween($key, $valueFrom, $valueTo) } /** - * @param string $key - * @param string $valueFrom - * @param string $valueTo + * @param string $key + * @param string|integer|float $valueFrom + * @param string|integer|float $valueTo * * @return static */ @@ -764,7 +813,8 @@ public function orWhereBetween($key, $valueFrom, $valueTo) } /** - * @param $key + * @param string $key + * * @return QueryBuilderHandler */ public function whereNull($key) @@ -773,7 +823,8 @@ public function whereNull($key) } /** - * @param $key + * @param string $key + * * @return QueryBuilderHandler */ public function whereNotNull($key) @@ -782,7 +833,8 @@ public function whereNotNull($key) } /** - * @param $key + * @param string $key + * * @return QueryBuilderHandler */ public function orWhereNull($key) @@ -791,7 +843,8 @@ public function orWhereNull($key) } /** - * @param $key + * @param string $key + * * @return QueryBuilderHandler */ public function orWhereNotNull($key) @@ -799,6 +852,13 @@ public function orWhereNotNull($key) return $this->whereNullHandler($key, 'NOT', 'or'); } + /** + * @param string $key + * @param string $prefix + * @param string $operator + * + * @return mixed + */ protected function whereNullHandler($key, $prefix = '', $operator = '') { $key = $this->adapterInstance->wrapSanitizer($this->addTablePrefix($key)); @@ -807,11 +867,11 @@ protected function whereNullHandler($key, $prefix = '', $operator = '') } /** - * @param string $table - * @param string $key + * @param string $table + * @param string $key * @param string|null $operator * @param string|null $value - * @param string $type + * @param string $type * * @return static */ @@ -841,6 +901,7 @@ public function join($table, $key, $operator = null, $value = null, $type = 'inn * Runs a transaction * * @param \Closure $callback + * * @return static */ public function transaction(\Closure $callback) @@ -872,8 +933,8 @@ public function transaction(\Closure $callback) } /** - * @param string $table - * @param string $key + * @param string $table + * @param string $key * @param string|null $operator * @param string|null $value * @@ -885,8 +946,8 @@ public function leftJoin($table, $key, $operator = null, $value = null) } /** - * @param string $table - * @param string $key + * @param string $table + * @param string $key * @param string|null $operator * @param string|null $value * @@ -898,8 +959,8 @@ public function rightJoin($table, $key, $operator = null, $value = null) } /** - * @param string $table - * @param string $key + * @param string $table + * @param string $key * @param string|mixed $operator * @param string|mixed $value * @@ -914,7 +975,7 @@ public function innerJoin($table, $key, $operator = null, $value = null) * Add a raw query * * @param string $value - * @param array $bindings + * @param array $bindings * * @return Raw */ @@ -954,16 +1015,16 @@ public function getConnection() } /** - * @param string $key - * @param string|mixed $operator - * @param string|mixed $value - * @param string $joiner + * @param string $key + * @param string|mixed $operator + * @param string|integer|float $value + * @param string $joiner * * @return static */ protected function whereHandler($key, $operator = null, $value = null, $joiner = 'AND') { - $key = $this->addTablePrefix($key); + $key = $this->addTablePrefix($key); $this->statements['wheres'][] = compact('key', 'operator', 'value', 'joiner'); return $this; @@ -972,7 +1033,7 @@ protected function whereHandler($key, $operator = null, $value = null, $joiner = /** * Add table prefix (if given) on given string. * - * @param $values + * @param $values * @param bool $tableFieldMix If we have mixes of field and table names with a "." * * @return array|string @@ -1021,7 +1082,7 @@ public function addTablePrefix($values, $tableFieldMix = true) /** * @param string $key - * @param mixed $value + * @param mixed $value */ protected function addStatement($key, $value) { @@ -1033,7 +1094,7 @@ protected function addStatement($key, $value) } /** - * @param string $event + * @param string $event * @param string|null $table * * @return \Closure|null @@ -1044,9 +1105,9 @@ public function getEvent($event, $table = null) } /** - * @param string $event + * @param string $event * @param string|null $table - * @param \Closure $action + * @param \Closure $action * * @return void */ @@ -1056,7 +1117,7 @@ public function registerEvent($event, $table = null, \Closure $action) } /** - * @param string $event + * @param string $event * @param string|null $table * * @return void @@ -1068,6 +1129,7 @@ public function removeEvent($event, $table = null) /** * @param $event + * * @return mixed */ public function fireEvents($event) @@ -1085,4 +1147,4 @@ public function getStatements() { return $this->statements; } -} \ No newline at end of file +} diff --git a/src/Pecee/Pixie/QueryBuilder/QueryObject.php b/src/Pecee/Pixie/QueryBuilder/QueryObject.php index 2bfc27e..6874e94 100644 --- a/src/Pecee/Pixie/QueryBuilder/QueryObject.php +++ b/src/Pecee/Pixie/QueryBuilder/QueryObject.php @@ -1,6 +1,12 @@ sql = (string)$sql; + $this->sql = (string)$sql; $this->bindings = $bindings; - $this->pdo = $pdo; + $this->pdo = $pdo; } /** @@ -59,14 +72,14 @@ public function getRawSql() * * Reference: http://stackoverflow.com/a/1376838/656489 * - * @param string $query The sql query with parameter placeholders - * @param array $params The array of substitution parameters + * @param string $query The sql query with parameter placeholders + * @param array $params The array of substitution parameters * * @return string The interpolated query */ protected function interpolateQuery($query, $params) { - $keys = []; + $keys = []; $values = $params; # build a regular expression for each parameter diff --git a/src/Pecee/Pixie/QueryBuilder/Raw.php b/src/Pecee/Pixie/QueryBuilder/Raw.php index 0ed5116..94f97cd 100644 --- a/src/Pecee/Pixie/QueryBuilder/Raw.php +++ b/src/Pecee/Pixie/QueryBuilder/Raw.php @@ -1,6 +1,11 @@ bindings = (array)$bindings; } + /** + * @return array + */ public function getBindings() { return $this->bindings; diff --git a/src/Pecee/Pixie/QueryBuilder/Transaction.php b/src/Pecee/Pixie/QueryBuilder/Transaction.php index eef75d7..f0d04fe 100644 --- a/src/Pecee/Pixie/QueryBuilder/Transaction.php +++ b/src/Pecee/Pixie/QueryBuilder/Transaction.php @@ -1,6 +1,11 @@ Date: Fri, 1 Sep 2017 20:53:18 +0200 Subject: [PATCH 4/5] check loaded library (mysql, pgsql, sqlite) --- src/Pecee/Pixie/ConnectionAdapters/Exception.php | 13 +++++++++++++ src/Pecee/Pixie/ConnectionAdapters/Mysql.php | 7 ++++++- src/Pecee/Pixie/ConnectionAdapters/Pgsql.php | 6 ++++++ src/Pecee/Pixie/ConnectionAdapters/Sqlite.php | 8 +++++++- 4 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 src/Pecee/Pixie/ConnectionAdapters/Exception.php diff --git a/src/Pecee/Pixie/ConnectionAdapters/Exception.php b/src/Pecee/Pixie/ConnectionAdapters/Exception.php new file mode 100644 index 0000000..d688594 --- /dev/null +++ b/src/Pecee/Pixie/ConnectionAdapters/Exception.php @@ -0,0 +1,13 @@ +container->build( From 500db3e868af6b83f3e032c472c589b6ceba6892 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Fri, 24 Nov 2017 20:45:36 +0100 Subject: [PATCH 5/5] Fixed issues related to groupBy functionality in combination with Raw and Closures. --- .../Pixie/QueryBuilder/Adapters/BaseAdapter.php | 6 +++++- .../Pixie/QueryBuilder/QueryBuilderHandler.php | 15 ++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Pecee/Pixie/QueryBuilder/Adapters/BaseAdapter.php b/src/Pecee/Pixie/QueryBuilder/Adapters/BaseAdapter.php index 90f37ae..880fb85 100644 --- a/src/Pecee/Pixie/QueryBuilder/Adapters/BaseAdapter.php +++ b/src/Pecee/Pixie/QueryBuilder/Adapters/BaseAdapter.php @@ -1,4 +1,5 @@ buildCriteriaWithType($statements, 'wheres', 'WHERE'); + // Group bys $groupBys = ''; if (isset($statements['groupBys']) && $groupBys = $this->arrayStr($statements['groupBys'], ', ')) { @@ -471,7 +473,9 @@ public function wrapSanitizer($value) // Its a raw query, just cast as string, object has __toString() if ($value instanceof Raw) { return (string)$value; - } elseif ($value instanceof \Closure) { + } + + if ($value instanceof \Closure) { return $value; } diff --git a/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php b/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php index 0dfaf6d..c0b9fbf 100644 --- a/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php +++ b/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php @@ -22,7 +22,9 @@ class QueryBuilderHandler /** * @var array */ - protected $statements = []; + protected $statements = [ + 'groupBys' => array() + ]; /** * @var \PDO @@ -535,8 +537,15 @@ public function selectDistinct($fields) */ public function groupBy($field) { - $field = $this->addTablePrefix($field); - $this->addStatement('groupBys', $field); + if (!$field instanceof Raw) { + $field = $this->addTablePrefix($field); + } + + if(is_array($field) === true) { + $this->statements['groupBys'] = array_merge($this->statements['groupBys'], $field); + } else { + $this->statements['groupBys'][] = $field; + } return $this; }