From 00b061b3c13b38e2e472d1256b5f89eacdcbea01 Mon Sep 17 00:00:00 2001 From: Vincenzo Ciaccio Date: Fri, 6 Apr 2018 16:28:44 +0100 Subject: [PATCH 1/5] small fix on wrong link in docs --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8de32d7..0028acc 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ Library on [Packagist](https://packagist.org/packages/usmanhalalit/pixie). - [Connection](#connection) - [Alias](#alias) - [Multiple Connection](#alias) - - [SQLite and PostgreSQL Config Sample](sqlite-and-postgresql-config-sample) + - [SQLite and PostgreSQL Config Sample](#sqlite-and-postgresql-config-sample) - [Query](#query) - [**Select**](#select) - [Get Easily](#get-easily) From 15ffa369d1dab58a40ed1ab3a6c95ae439e4f5ef Mon Sep 17 00:00:00 2001 From: Vincenzo Ciaccio Date: Wed, 11 Apr 2018 13:45:27 +0100 Subject: [PATCH 2/5] usmanhalalit/pixie#185 this would fix the issues on fallback fetchMode if set --- .../QueryBuilder/QueryBuilderHandler.php | 40 ++++++++++++------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/src/Pixie/QueryBuilder/QueryBuilderHandler.php b/src/Pixie/QueryBuilder/QueryBuilderHandler.php index 134fba0..61037c1 100644 --- a/src/Pixie/QueryBuilder/QueryBuilderHandler.php +++ b/src/Pixie/QueryBuilder/QueryBuilderHandler.php @@ -47,14 +47,15 @@ class QueryBuilderHandler * * @var array */ - protected $fetchParameters = array(\PDO::FETCH_OBJ); + protected $fetchParameters = array(PDO::FETCH_OBJ); /** * @param null|\Pixie\Connection $connection * - * @throws \Pixie\Exception + * @param int $fetchMode + * @throws Exception */ - public function __construct(Connection $connection = null) + public function __construct(Connection $connection = null, $fetchMode = PDO::FETCH_OBJ) { if (is_null($connection)) { if (!$connection = Connection::getStoredConnection()) { @@ -68,6 +69,8 @@ public function __construct(Connection $connection = null) $this->adapter = $this->connection->getAdapter(); $this->adapterConfig = $this->connection->getAdapterConfig(); + $this->setFetchMode($fetchMode); + if (isset($this->adapterConfig['prefix'])) { $this->tablePrefix = $this->adapterConfig['prefix']; } @@ -107,8 +110,9 @@ public function asObject($className, $constructorArgs = array()) /** * @param null|\Pixie\Connection $connection - * - * @return static + * @param int $fetchMode + * @return QueryBuilderHandler + * @throws Exception */ public function newQuery(Connection $connection = null) { @@ -116,7 +120,7 @@ public function newQuery(Connection $connection = null) $connection = $this->connection; } - return new static($connection); + return new static($connection, $this->getFetchMode()); } /** @@ -259,9 +263,9 @@ protected function aggregate($type) } if (is_array($row[0])) { - return (int) $row[0]['field']; + return (int)$row[0]['field']; } elseif (is_object($row[0])) { - return (int) $row[0]->field; + return (int)$row[0]->field; } return 0; @@ -269,7 +273,7 @@ protected function aggregate($type) /** * @param string $type - * @param array $dataToBePassed + * @param array $dataToBePassed * * @return mixed * @throws Exception @@ -291,7 +295,7 @@ public function getQuery($type = 'select', $dataToBePassed = array()) /** * @param QueryBuilderHandler $queryBuilder - * @param null $alias + * @param null $alias * * @return Raw */ @@ -453,7 +457,7 @@ public function table($tables) $tables = func_get_args(); } - $instance = new static($this->connection); + $instance = new static($this->connection, $this->getFetchMode()); $tables = $this->addTablePrefix($tables, false); $instance->addStatement('tables', $tables); return $instance; @@ -791,7 +795,7 @@ public function join($table, $key, $operator = null, $value = null, $type = 'inn // Build a new JoinBuilder class, keep it by reference so any changes made // in the closure should reflect here $joinBuilder = $this->container->build('\\Pixie\\QueryBuilder\\JoinBuilder', array($this->connection)); - $joinBuilder = & $joinBuilder; + $joinBuilder = &$joinBuilder; // Call the closure with our new joinBuilder object $key($joinBuilder); $table = $this->addTablePrefix($table, false); @@ -1012,7 +1016,7 @@ public function getEvent($event, $table = ':any') /** * @param $event - * @param string $table + * @param string $table * @param callable $action * * @return void @@ -1030,7 +1034,7 @@ public function registerEvent($event, $table, \Closure $action) /** * @param $event - * @param string $table + * @param string $table * * @return void */ @@ -1061,4 +1065,12 @@ public function getStatements() { return $this->statements; } + + /** + * @return int will return PDO Fetch mode + */ + private function getFetchMode() + { + return !empty($this->fetchParameters) ? current($this->fetchParameters) : PDO::FETCH_OBJ; + } } From 26dc85215dc459234e39aa614a942104a7c5c570 Mon Sep 17 00:00:00 2001 From: Vincenzo Ciaccio Date: Wed, 11 Apr 2018 14:28:10 +0100 Subject: [PATCH 3/5] usmanhalalit/pixie#185 adding required changes on the PR --- src/Pixie/QueryBuilder/QueryBuilderHandler.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Pixie/QueryBuilder/QueryBuilderHandler.php b/src/Pixie/QueryBuilder/QueryBuilderHandler.php index 61037c1..954ebf2 100644 --- a/src/Pixie/QueryBuilder/QueryBuilderHandler.php +++ b/src/Pixie/QueryBuilder/QueryBuilderHandler.php @@ -47,13 +47,13 @@ class QueryBuilderHandler * * @var array */ - protected $fetchParameters = array(PDO::FETCH_OBJ); + protected $fetchParameters = array(\PDO::FETCH_OBJ); /** * @param null|\Pixie\Connection $connection * * @param int $fetchMode - * @throws Exception + * @throws \Pixie\Exception */ public function __construct(Connection $connection = null, $fetchMode = PDO::FETCH_OBJ) { @@ -110,7 +110,6 @@ public function asObject($className, $constructorArgs = array()) /** * @param null|\Pixie\Connection $connection - * @param int $fetchMode * @return QueryBuilderHandler * @throws Exception */ @@ -160,7 +159,8 @@ public function statement($sql, $bindings = array()) /** * Get all rows * - * @return \stdClass|null + * @return \stdClass|array + * @throws Exception */ public function get() { @@ -444,10 +444,10 @@ public function delete() } /** - * @param $tables Single table or multiple tables as an array or as - * multiple parameters + * @param string|array $tables Single table or array of tables * - * @return static + * @return QueryBuilderHandler + * @throws Exception */ public function table($tables) { @@ -1071,6 +1071,7 @@ public function getStatements() */ private function getFetchMode() { - return !empty($this->fetchParameters) ? current($this->fetchParameters) : PDO::FETCH_OBJ; + return !empty($this->fetchParameters) ? + current($this->fetchParameters) : PDO::FETCH_OBJ; } } From 8b35426f04c3eac62f6abb4a2a7a91b96a39fc07 Mon Sep 17 00:00:00 2001 From: Vincenzo Ciaccio Date: Wed, 11 Apr 2018 14:45:51 +0100 Subject: [PATCH 4/5] usmanhalalit/pixie#185 adding regression tests --- src/Pixie/QueryBuilder/QueryBuilderHandler.php | 10 +++++----- tests/Pixie/QueryBuilderBehaviorTest.php | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/Pixie/QueryBuilder/QueryBuilderHandler.php b/src/Pixie/QueryBuilder/QueryBuilderHandler.php index 954ebf2..f7fd976 100644 --- a/src/Pixie/QueryBuilder/QueryBuilderHandler.php +++ b/src/Pixie/QueryBuilder/QueryBuilderHandler.php @@ -23,7 +23,7 @@ class QueryBuilderHandler protected $statements = array(); /** - * @var \PDO + * @var PDO */ protected $pdo; @@ -47,13 +47,13 @@ class QueryBuilderHandler * * @var array */ - protected $fetchParameters = array(\PDO::FETCH_OBJ); + protected $fetchParameters = array(PDO::FETCH_OBJ); /** * @param null|\Pixie\Connection $connection * * @param int $fetchMode - * @throws \Pixie\Exception + * @throws Exception */ public function __construct(Connection $connection = null, $fetchMode = PDO::FETCH_OBJ) { @@ -105,7 +105,7 @@ public function setFetchMode($mode) */ public function asObject($className, $constructorArgs = array()) { - return $this->setFetchMode(\PDO::FETCH_CLASS, $className, $constructorArgs); + return $this->setFetchMode(PDO::FETCH_CLASS, $className, $constructorArgs); } /** @@ -1069,7 +1069,7 @@ public function getStatements() /** * @return int will return PDO Fetch mode */ - private function getFetchMode() + public function getFetchMode() { return !empty($this->fetchParameters) ? current($this->fetchParameters) : PDO::FETCH_OBJ; diff --git a/tests/Pixie/QueryBuilderBehaviorTest.php b/tests/Pixie/QueryBuilderBehaviorTest.php index 40030d9..d71335b 100644 --- a/tests/Pixie/QueryBuilderBehaviorTest.php +++ b/tests/Pixie/QueryBuilderBehaviorTest.php @@ -1,6 +1,6 @@ getQuery()->getRawSql() ); } + + public function testYouCanSetFetchModeFromConstructorAsOptionalParameter() + { + $selectedFetchMode = \PDO::FETCH_ASSOC; + $builder = new QueryBuilderHandler($this->mockConnection, $selectedFetchMode); + $this->assertEquals($selectedFetchMode, $builder->getFetchMode()); + } + + public function testFetchModeSelectedWillBeMaintainedBetweenInstances(){ + $selectedFetchMode = \PDO::FETCH_ASSOC; + $builder = new QueryBuilderHandler($this->mockConnection, $selectedFetchMode); + $newBuilder = $builder->table('stuff'); + + $this->assertEquals($selectedFetchMode, $builder->getFetchMode()); + } } From 271e73268664fa789a9940f05db5e3a9ba2ba257 Mon Sep 17 00:00:00 2001 From: Vincenzo Ciaccio Date: Wed, 11 Apr 2018 14:47:40 +0100 Subject: [PATCH 5/5] usmanhalalit/pixie#185 fix on regression test --- tests/Pixie/QueryBuilderBehaviorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Pixie/QueryBuilderBehaviorTest.php b/tests/Pixie/QueryBuilderBehaviorTest.php index d71335b..e1c3a88 100644 --- a/tests/Pixie/QueryBuilderBehaviorTest.php +++ b/tests/Pixie/QueryBuilderBehaviorTest.php @@ -315,6 +315,6 @@ public function testFetchModeSelectedWillBeMaintainedBetweenInstances(){ $builder = new QueryBuilderHandler($this->mockConnection, $selectedFetchMode); $newBuilder = $builder->table('stuff'); - $this->assertEquals($selectedFetchMode, $builder->getFetchMode()); + $this->assertEquals($selectedFetchMode, $newBuilder->getFetchMode()); } }