From 26c461139af6d0dd943a7e50a9444072f36b0628 Mon Sep 17 00:00:00 2001 From: Simon Sessingo Date: Sat, 2 Dec 2017 20:21:34 +0100 Subject: [PATCH] Development - Removed depricated prefix method. - Made table parameter in alias method optional. - Removed .idea folder as it was causing issues. --- .gitignore | 1 + .idea/codeStyleSettings.xml | 9 -- .idea/composerJson.xml | 9 -- .idea/deployment.xml | 43 ------- .idea/inspectionProfiles/Project_Default.xml | 15 --- .idea/markdown-navigator.xml | 72 ------------ .../markdown-navigator/profiles_settings.xml | 3 - .idea/modules.xml | 8 -- .idea/pecee-pixie.iml | 98 ---------------- .idea/php-test-framework.xml | 14 --- .idea/php.xml | 45 ------- .idea/vcs.xml | 6 - README.md | 15 +++ composer.json | 2 +- composer.lock | 110 +++++++++--------- .../QueryBuilder/QueryBuilderHandler.php | 51 ++++---- .../Pecee/Pixie/QueryBuilderBehaviorTest.php | 31 ++++- 17 files changed, 124 insertions(+), 408 deletions(-) delete mode 100644 .idea/codeStyleSettings.xml delete mode 100644 .idea/composerJson.xml delete mode 100644 .idea/deployment.xml delete mode 100644 .idea/inspectionProfiles/Project_Default.xml delete mode 100644 .idea/markdown-navigator.xml delete mode 100644 .idea/markdown-navigator/profiles_settings.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/pecee-pixie.iml delete mode 100644 .idea/php-test-framework.xml delete mode 100644 .idea/php.xml delete mode 100644 .idea/vcs.xml diff --git a/.gitignore b/.gitignore index 7186bab..233f5da 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ composer.phar *~ /devel/ /coverage.clover +.idea/ ### Composer template /vendor/ diff --git a/.idea/codeStyleSettings.xml b/.idea/codeStyleSettings.xml deleted file mode 100644 index c4c9543..0000000 --- a/.idea/codeStyleSettings.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/composerJson.xml b/.idea/composerJson.xml deleted file mode 100644 index 4199499..0000000 --- a/.idea/composerJson.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/deployment.xml b/.idea/deployment.xml deleted file mode 100644 index 9cbabb7..0000000 --- a/.idea/deployment.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml deleted file mode 100644 index 3d2c9cb..0000000 --- a/.idea/inspectionProfiles/Project_Default.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/markdown-navigator.xml b/.idea/markdown-navigator.xml deleted file mode 100644 index e41dd85..0000000 --- a/.idea/markdown-navigator.xml +++ /dev/null @@ -1,72 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/markdown-navigator/profiles_settings.xml b/.idea/markdown-navigator/profiles_settings.xml deleted file mode 100644 index 57927c5..0000000 --- a/.idea/markdown-navigator/profiles_settings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index af9f61b..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/pecee-pixie.iml b/.idea/pecee-pixie.iml deleted file mode 100644 index 0e4c427..0000000 --- a/.idea/pecee-pixie.iml +++ /dev/null @@ -1,98 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/php-test-framework.xml b/.idea/php-test-framework.xml deleted file mode 100644 index 9d2103d..0000000 --- a/.idea/php-test-framework.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/php.xml b/.idea/php.xml deleted file mode 100644 index 51cda52..0000000 --- a/.idea/php.xml +++ /dev/null @@ -1,45 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/README.md b/README.md index d9a0bbd..30d0086 100644 --- a/README.md +++ b/README.md @@ -303,6 +303,21 @@ $result = $queryBuilder ### Select +#### Select from query + +You can easily select items from another query by using + +```php +$subQuery = $queryBuilder->table('person'); +$builder = $queryBuilder->table($queryBuilder->subQuery($subQuery))->where('id', '=', 2); +``` + +Will produce the following output: + +```sql +SELECT * FROM (SELECT * FROM `person`) WHERE `id` = 2 +``` + #### Select single field ```php diff --git a/composer.json b/composer.json index 5aca082..3734d78 100644 --- a/composer.json +++ b/composer.json @@ -38,7 +38,7 @@ "usmanhalalit/viocon": "1.0.1" }, "require-dev": { - "phpunit/phpunit": "^6.4", + "phpunit/phpunit": "^6.0", "mockery/mockery": "^1" }, "autoload": { diff --git a/composer.lock b/composer.lock index e198b6e..d03cd81 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "55c47951e95f4087152d5ea0bd6f2ed4", + "content-hash": "431825fe68c6a6b27cb373d5aa657c4b", "packages": [ { "name": "usmanhalalit/viocon", @@ -57,32 +57,32 @@ "packages-dev": [ { "name": "doctrine/instantiator", - "version": "1.0.5", + "version": "1.1.0", "source": { "type": "git", "url": "https://github.com/doctrine/instantiator.git", - "reference": "8e884e78f9f0eb1329e445619e04456e64d8051d" + "reference": "185b8868aa9bf7159f5f953ed5afb2d7fcdc3bda" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/doctrine/instantiator/zipball/8e884e78f9f0eb1329e445619e04456e64d8051d", - "reference": "8e884e78f9f0eb1329e445619e04456e64d8051d", + "url": "https://api.github.com/repos/doctrine/instantiator/zipball/185b8868aa9bf7159f5f953ed5afb2d7fcdc3bda", + "reference": "185b8868aa9bf7159f5f953ed5afb2d7fcdc3bda", "shasum": "" }, "require": { - "php": ">=5.3,<8.0-DEV" + "php": "^7.1" }, "require-dev": { "athletic/athletic": "~0.1.8", "ext-pdo": "*", "ext-phar": "*", - "phpunit/phpunit": "~4.0", - "squizlabs/php_codesniffer": "~2.0" + "phpunit/phpunit": "^6.2.3", + "squizlabs/php_codesniffer": "^3.0.2" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0.x-dev" + "dev-master": "1.2.x-dev" } }, "autoload": { @@ -107,7 +107,7 @@ "constructor", "instantiate" ], - "time": "2015-06-14T21:17:01+00:00" + "time": "2017-07-22T11:58:36+00:00" }, { "name": "hamcrest/hamcrest-php", @@ -425,29 +425,35 @@ }, { "name": "phpdocumentor/reflection-docblock", - "version": "4.1.1", + "version": "4.2.0", "source": { "type": "git", "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", - "reference": "2d3d238c433cf69caeb4842e97a3223a116f94b2" + "reference": "66465776cfc249844bde6d117abff1d22e06c2da" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/2d3d238c433cf69caeb4842e97a3223a116f94b2", - "reference": "2d3d238c433cf69caeb4842e97a3223a116f94b2", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/66465776cfc249844bde6d117abff1d22e06c2da", + "reference": "66465776cfc249844bde6d117abff1d22e06c2da", "shasum": "" }, "require": { "php": "^7.0", - "phpdocumentor/reflection-common": "^1.0@dev", + "phpdocumentor/reflection-common": "^1.0.0", "phpdocumentor/type-resolver": "^0.4.0", "webmozart/assert": "^1.0" }, "require-dev": { - "mockery/mockery": "^0.9.4", - "phpunit/phpunit": "^4.4" + "doctrine/instantiator": "~1.0.5", + "mockery/mockery": "^1.0", + "phpunit/phpunit": "^6.4" }, "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.x-dev" + } + }, "autoload": { "psr-4": { "phpDocumentor\\Reflection\\": [ @@ -466,7 +472,7 @@ } ], "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", - "time": "2017-08-30T18:51:59+00:00" + "time": "2017-11-27T17:38:31+00:00" }, { "name": "phpdocumentor/type-resolver", @@ -580,16 +586,16 @@ }, { "name": "phpunit/php-code-coverage", - "version": "5.2.3", + "version": "5.2.4", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "8e1d2397d8adf59a3f12b2878a3aaa66d1ab189d" + "reference": "033ec97498cf530cc1be4199264cad568b19be26" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/8e1d2397d8adf59a3f12b2878a3aaa66d1ab189d", - "reference": "8e1d2397d8adf59a3f12b2878a3aaa66d1ab189d", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/033ec97498cf530cc1be4199264cad568b19be26", + "reference": "033ec97498cf530cc1be4199264cad568b19be26", "shasum": "" }, "require": { @@ -598,7 +604,7 @@ "php": "^7.0", "phpunit/php-file-iterator": "^1.4.2", "phpunit/php-text-template": "^1.2.1", - "phpunit/php-token-stream": "^2.0", + "phpunit/php-token-stream": "^2.0.1", "sebastian/code-unit-reverse-lookup": "^1.0.1", "sebastian/environment": "^3.0", "sebastian/version": "^2.0.1", @@ -640,20 +646,20 @@ "testing", "xunit" ], - "time": "2017-11-03T13:47:33+00:00" + "time": "2017-11-27T09:00:30+00:00" }, { "name": "phpunit/php-file-iterator", - "version": "1.4.3", + "version": "1.4.5", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-file-iterator.git", - "reference": "8ebba84e5bd74fc5fdeb916b38749016c7232f93" + "reference": "730b01bc3e867237eaac355e06a36b85dd93a8b4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/8ebba84e5bd74fc5fdeb916b38749016c7232f93", - "reference": "8ebba84e5bd74fc5fdeb916b38749016c7232f93", + "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/730b01bc3e867237eaac355e06a36b85dd93a8b4", + "reference": "730b01bc3e867237eaac355e06a36b85dd93a8b4", "shasum": "" }, "require": { @@ -687,7 +693,7 @@ "filesystem", "iterator" ], - "time": "2017-11-24T15:00:59+00:00" + "time": "2017-11-27T13:52:08+00:00" }, { "name": "phpunit/php-text-template", @@ -781,16 +787,16 @@ }, { "name": "phpunit/php-token-stream", - "version": "2.0.1", + "version": "2.0.2", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-token-stream.git", - "reference": "9a02332089ac48e704c70f6cefed30c224e3c0b0" + "reference": "791198a2c6254db10131eecfe8c06670700904db" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/9a02332089ac48e704c70f6cefed30c224e3c0b0", - "reference": "9a02332089ac48e704c70f6cefed30c224e3c0b0", + "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/791198a2c6254db10131eecfe8c06670700904db", + "reference": "791198a2c6254db10131eecfe8c06670700904db", "shasum": "" }, "require": { @@ -826,20 +832,20 @@ "keywords": [ "tokenizer" ], - "time": "2017-08-20T05:47:52+00:00" + "time": "2017-11-27T05:48:46+00:00" }, { "name": "phpunit/phpunit", - "version": "6.4.4", + "version": "6.5.2", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "562f7dc75d46510a4ed5d16189ae57fbe45a9932" + "reference": "24b708f2fd725bcef1c8153b366043381aa324f2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/562f7dc75d46510a4ed5d16189ae57fbe45a9932", - "reference": "562f7dc75d46510a4ed5d16189ae57fbe45a9932", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/24b708f2fd725bcef1c8153b366043381aa324f2", + "reference": "24b708f2fd725bcef1c8153b366043381aa324f2", "shasum": "" }, "require": { @@ -853,12 +859,12 @@ "phar-io/version": "^1.0", "php": "^7.0", "phpspec/prophecy": "^1.7", - "phpunit/php-code-coverage": "^5.2.2", - "phpunit/php-file-iterator": "^1.4.2", + "phpunit/php-code-coverage": "^5.2.3", + "phpunit/php-file-iterator": "^1.4.3", "phpunit/php-text-template": "^1.2.1", "phpunit/php-timer": "^1.0.9", - "phpunit/phpunit-mock-objects": "^4.0.3", - "sebastian/comparator": "^2.0.2", + "phpunit/phpunit-mock-objects": "^5.0.4", + "sebastian/comparator": "^2.1", "sebastian/diff": "^2.0", "sebastian/environment": "^3.1", "sebastian/exporter": "^3.1", @@ -884,7 +890,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "6.4.x-dev" + "dev-master": "6.5.x-dev" } }, "autoload": { @@ -910,20 +916,20 @@ "testing", "xunit" ], - "time": "2017-11-08T11:26:09+00:00" + "time": "2017-12-02T05:36:24+00:00" }, { "name": "phpunit/phpunit-mock-objects", - "version": "4.0.4", + "version": "5.0.4", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", - "reference": "2f789b59ab89669015ad984afa350c4ec577ade0" + "reference": "16b50f4167e5e85e81ca8a3dd105d0a5fd32009a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/2f789b59ab89669015ad984afa350c4ec577ade0", - "reference": "2f789b59ab89669015ad984afa350c4ec577ade0", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/16b50f4167e5e85e81ca8a3dd105d0a5fd32009a", + "reference": "16b50f4167e5e85e81ca8a3dd105d0a5fd32009a", "shasum": "" }, "require": { @@ -936,7 +942,7 @@ "phpunit/phpunit": "<6.0" }, "require-dev": { - "phpunit/phpunit": "^6.0" + "phpunit/phpunit": "^6.5" }, "suggest": { "ext-soap": "*" @@ -944,7 +950,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "4.0.x-dev" + "dev-master": "5.0.x-dev" } }, "autoload": { @@ -959,7 +965,7 @@ "authors": [ { "name": "Sebastian Bergmann", - "email": "sb@sebastian-bergmann.de", + "email": "sebastian@phpunit.de", "role": "lead" } ], @@ -969,7 +975,7 @@ "mock", "xunit" ], - "time": "2017-08-03T14:08:16+00:00" + "time": "2017-12-02T05:31:19+00:00" }, { "name": "sebastian/code-unit-reverse-lookup", diff --git a/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php b/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php index 0c10971..159b38f 100644 --- a/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php +++ b/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php @@ -254,14 +254,20 @@ protected function aggregate(string $type): int * Add or change table alias * Example: table AS alias * - * @param string $table * @param string $alias + * @param string $table * * @return static */ - public function alias(string $table, string $alias) + public function alias(string $alias, string $table = null) { - $this->statements['aliases'][$this->tablePrefix . $table] = strtolower($alias); + if($table === null && isset($this->statements['tables'][0]) === true) { + $table = $this->statements['tables'][0]; + } else { + $table = $this->tablePrefix . $table; + } + + $this->statements['aliases'][$table] = strtolower($alias); return $this; } @@ -810,7 +816,7 @@ public function orWhereIn($key, $values) * Adds OR WHERE NOT statement to the current query. * * @param string|Raw|\Closure $key - * @param string|array|Raw|\Closure|null $operator + * @param string|null $operator * @param mixed|Raw|\Closure|null $value * * @return static @@ -906,24 +912,6 @@ public function pdo() return $this->pdo; } - /** - * Add or change table alias - * - * Example: table AS alias - * - * @deprecated This method will be removed in the near future, please use QueryBuilderHandler::alias instead. - * @see QueryBuilderHandler::alias - * - * @param string $table - * @param string $alias - * - * @return static - */ - public function prefix($table, $alias) - { - return $this->alias($table, $alias); - } - /** * Performs query. * @@ -1133,9 +1121,9 @@ public function subQuery(QueryBuilderHandler $queryBuilder, $alias = null): Raw * Sets the table that the query is using * * @param string|array $tables Single table or multiple tables as an array or as multiple parameters - * * @throws Exception * @return static + * * ``` * Examples: * - basic usage @@ -1156,9 +1144,10 @@ public function table($tables) } $instance = new static($this->connection); + foreach ($tables as $key => $value) { if (\is_string($key)) { - $instance->alias($key, $value); + $instance->alias($value, $key); $tTables[] = $key; } else { $tTables[] = $value; @@ -1251,7 +1240,7 @@ public function updateOrInsert($data) * Adds WHERE statement to the current query. * * @param string|Raw|\Closure $key - * @param string|Raw|\Closure|null $operator + * @param string|null $operator * @param mixed|Raw|\Closure|null $value * * @return static @@ -1352,11 +1341,11 @@ public function whereNotIn($key, $values) /** * Adds WHERE NOT NULL statement to the current query. * - * @param string $key + * @param string|Raw|\Closure $key * * @return static */ - public function whereNotNull(string $key) + public function whereNotNull($key) { return $this->whereNullHandler($key, 'NOT'); } @@ -1376,16 +1365,16 @@ public function whereNull($key) /** * Handles WHERE NULL statements. * - * @param string $key + * @param string|Raw|\Closure $key * @param string $prefix * @param string $operator * * @return mixed */ - protected function whereNullHandler(string $key, $prefix = '', $operator = '') + protected function whereNullHandler($key, $prefix = '', $operator = '') { $key = $this->adapterInstance->wrapSanitizer($this->addTablePrefix($key)); - - return $this->{$operator . 'Where'}($this->raw("{$key} IS {$prefix} NULL")); + $prefix = ($prefix !== '') ? $prefix . ' ' : $prefix; + return $this->{$operator . 'Where'}($this->raw("$key IS {$prefix}NULL")); } } diff --git a/tests/Pecee/Pixie/QueryBuilderBehaviorTest.php b/tests/Pecee/Pixie/QueryBuilderBehaviorTest.php index 9b93357..70ebdda 100644 --- a/tests/Pecee/Pixie/QueryBuilderBehaviorTest.php +++ b/tests/Pecee/Pixie/QueryBuilderBehaviorTest.php @@ -30,7 +30,7 @@ public function testAlias() { $query = $this->builder ->table(['table1']) - ->alias('table1', 't1') + ->alias('t1') ->join('table2', 'table2.person_id', '=', 'foo2.id') ; @@ -301,7 +301,7 @@ public function testSelectQueryWithNull() ; $this->assertEquals( - "SELECT * FROM `cb_my_table` WHERE `key1` IS NULL OR `key2` IS NULL AND `key3` IS NOT NULL OR `key4` IS NOT NULL OR `key5` = NULL", + "SELECT * FROM `cb_my_table` WHERE `key1` IS NULL OR `key2` IS NULL AND `key3` IS NOT NULL OR `key4` IS NOT NULL OR `key5` = NULL", $query->getQuery()->getRawSql() ); } @@ -339,4 +339,31 @@ public function testUpdateQuery() $this->assertEquals("UPDATE `cb_my_table` SET `key`='Sana',`value`='Amrin' WHERE `value` = 'Sana'" , $builder->getQuery('update', $data)->getRawSql()); } + + public function testFromSubQuery() { + + $subQuery = $this->builder->table('person'); + $builder = $this->builder->table($this->builder->subQuery($subQuery))->where('id', '=', 2); + + $this->assertEquals('SELECT * FROM (SELECT * FROM `cb_person`) WHERE `id` = 2', $builder->getQuery()->getRawSql()); + + } + + public function testTableAlias() { + + $builder = $this->builder->table('persons')->alias('staff'); + + $this->assertEquals('SELECT * FROM `cb_persons` AS `staff`', $builder->getQuery()->getRawSql()); + + } + + public function testWhereNotNullSubQuery() { + $subQuery = $this->builder->table('persons')->alias('staff'); + + $query = $this->builder->whereNull($this->builder->subQuery($subQuery)); + + $this->assertEquals('SELECT * WHERE (SELECT * FROM `cb_persons` AS `staff`) IS NULL', $query->getQuery()->getRawSql()); + + } + }