diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..d3b6bcb --- /dev/null +++ b/.editorconfig @@ -0,0 +1,14 @@ +# http://editorconfig.org +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 2 +indent_style = space +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +max_line_length = 0 +trim_trailing_whitespace = false diff --git a/README.md b/README.md index 42fce9d..7688679 100644 --- a/README.md +++ b/README.md @@ -79,18 +79,18 @@ Figure 4. Data Manipulation ```php $settings = [ - 'user_name' => 'Chris' - 'user_email' => 'myemail@mail.com' + 'user_name' => 'Chris' + 'user_email' => 'myemail@mail.com' ]; -$filter[] = ['user_id=%s', 1]; +$filter[] = ['user_id=%s', 1]; // inserts row into 'user' table $database->insertRow('user', $settings); // updates rows in 'user' table where user_id is $database->updateRows('user', $settings, $filter); // delete rows in 'user' table where user_id is 1 -$database->deleteRows('user', $filter); +$database->deleteRows('user', $filter); ``` Inserting data is pretty trivial. We included 2 ways to insert data. Like getRow(), there's no need to worry about bound data because Storm will do this for you. Figure 4 shows the 2 kind of inserts mentioned. @@ -157,17 +157,17 @@ A better way to build complex queries is with using the search object. An overvi ```php $database - ->search('user') - ->setColumns('*') - ->innerJoinOn('group', 'group_owner=user_id') - ->leftJoinUsing('friends', 'user_id') - ->filterByUserName('Chris') - ->addFilter("user_last LIKE '%s%%'", 'Brown') - ->sortByUserId('ASC') - ->addSort('user_last', 'DESC') - ->setRange(25) - ->setStart(75) - ->getRows(); + ->search('user') + ->setColumns('*') + ->innerJoinOn('group', 'group_owner=user_id') + ->leftJoinUsing('friends', 'user_id') + ->filterByUserName('Chris') + ->addFilter("user_last LIKE '%s%%'", 'Brown') + ->sortByUserId('ASC') + ->addSort('user_last', 'DESC') + ->setRange(25) + ->setStart(75) + ->getRows(); ``` In the figure above there's a few methods being powered with magic, but we'll just start going down the line. First off, to instantiate the search object you simply need to call `search()` passing the name of the table as the argument. Secondly we call `setColumns()`. This call is optional, but if used, can either accept an array of columns or an argument separated list of columns, ie. `setColumns('user_id', 'user_name')`. Next, `innerJoinOn()` is the new way we accept joins. There are eight methods dedicated to different kinds of joins. @@ -243,9 +243,9 @@ $collection->setUserName('Chris'); $collection->setAnyThing() //collections are iterable -foreach($collection as $model) { - echo $model->getUserName().' '; - echo $model['user_email']; +foreach($collection as $model) { + echo $model->getUserName().' '; + echo $model['user_email']; } //access as array @@ -280,7 +280,7 @@ $collection->count(); $collection->add(['post_title' => 'Hi']); //returns a table array (no objects) -$collection->get(); +$collection->get(); ``` ---- @@ -317,12 +317,12 @@ A common example is when you have an array table that comprises of joined data. ```php $row = [ - 'user_id' => 1, - 'user_name' => 'Chris', - 'user_email' => 'my@email.com', - 'post_user' => 1, - 'post_title' => 'My Post', - 'post_detail' => 'This is my new article' + 'user_id' => 1, + 'user_name' => 'Chris', + 'user_email' => 'my@email.com', + 'post_user' => 1, + 'post_title' => 'My Post', + 'post_detail' => 'This is my new article' ]; $database->model($row)->save('user')->save('post'); @@ -342,29 +342,29 @@ So a common scenario would be retrieving data, manipulating the results and send ```php //load database $database - //search user table - ->search('user') - //WHERE user_gender = $_get['gender'] - ->filterByUserGender($_GET['gender']) - //ORDER BY user_id - ->sortByUserId('ASC') - //LIMIT 75, 25 - ->setStart(75)->setRange(25) - //get a collection object - ->getCollection() - //sets all users to active - ->setUserActive(1) - //Set a new column post_title - ->setPostTitle('A '.$_GET['gender'].'\'s Post') - //Set a new column post_detail - ->setPostDetail('Content is King') - //Copy the contents of user_id to a new column post_user - ->copy('user_id', 'post_user') - //Set a new column post_created - ->setPostCreated(time()) - ->formatTime('post_created', 'Y-m-d H:i:s') - //save to user table - ->save('user') - //save to post table - ->save('post'); + //search user table + ->search('user') + //WHERE user_gender = $_get['gender'] + ->filterByUserGender($_GET['gender']) + //ORDER BY user_id + ->sortByUserId('ASC') + //LIMIT 75, 25 + ->setStart(75)->setRange(25) + //get a collection object + ->getCollection() + //sets all users to active + ->setUserActive(1) + //Set a new column post_title + ->setPostTitle('A '.$_GET['gender'].'\'s Post') + //Set a new column post_detail + ->setPostDetail('Content is King') + //Copy the contents of user_id to a new column post_user + ->copy('user_id', 'post_user') + //Set a new column post_created + ->setPostCreated(time()) + ->formatTime('post_created', 'Y-m-d H:i:s') + //save to user table + ->save('user') + //save to post table + ->save('post'); ``` diff --git a/composer.json b/composer.json index c4ee504..d4f92f9 100644 --- a/composer.json +++ b/composer.json @@ -1,34 +1,34 @@ { - "name": "cradlephp/storm", - "type": "library", - "description": "A Search Collection Model Kind of SQL Client", - "homepage": "https://cradlephp.github.io/", - "keywords": [ - "orm", - "cradle", - "cradlephp", - "mysql", - "postgres", - "sqlite" - ], - "license": "MIT", - "authors": [ - { - "name": "Christian Blanquera", - "email": "cblanquera@openovate.com" - } - ], - "require-dev": { - "phpunit/phpunit": "7.0.2", - "squizlabs/php_codesniffer": "3.2.3", - "php-coveralls/php-coveralls": "~2.1.0" - }, - "require": { - "cradlephp/components": "~2.2.2" - }, - "autoload": { - "psr-4": { - "Cradle\\Storm\\": "src/" - } + "name": "cradlephp/storm", + "type": "library", + "description": "A Search Collection Model Kind of SQL Client", + "homepage": "https://cradlephp.github.io/", + "keywords": [ + "orm", + "cradle", + "cradlephp", + "mysql", + "postgres", + "sqlite" + ], + "license": "MIT", + "authors": [ + { + "name": "Christian Blanquera", + "email": "cblanquera@openovate.com" } + ], + "require-dev": { + "phpunit/phpunit": "7.0.2", + "squizlabs/php_codesniffer": "3.2.3", + "php-coveralls/php-coveralls": "~2.1.0" + }, + "require": { + "cradlephp/components": "~2.3.0" + }, + "autoload": { + "psr-4": { + "Cradle\\Storm\\": "src/" + } + } } diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..57922a1 --- /dev/null +++ b/composer.lock @@ -0,0 +1,2952 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "9858d566eb3329f41170a6f8cd93a459", + "packages": [ + { + "name": "cradlephp/components", + "version": "2.3.0", + "source": { + "type": "git", + "url": "https://github.com/CradlePHP/components.git", + "reference": "67e9e7e46be2b4bcb6a6a796cec3c44247a6fa2a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/CradlePHP/components/zipball/67e9e7e46be2b4bcb6a6a796cec3c44247a6fa2a", + "reference": "67e9e7e46be2b4bcb6a6a796cec3c44247a6fa2a", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "require-dev": { + "php-coveralls/php-coveralls": "~2.1.0", + "phpunit/phpunit": "7.0.2", + "squizlabs/php_codesniffer": "3.2.3" + }, + "type": "library", + "autoload": { + "psr-4": { + "Cradle\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Christian Blanquera", + "email": "cblanquera@openovate.com" + } + ], + "description": "Common PHP7 Patterns and Constructs", + "homepage": "https://cradlephp.github.io/", + "keywords": [ + "cradle", + "cradlephp", + "library", + "php70", + "php71" + ], + "time": "2020-05-13T19:28:37+00:00" + } + ], + "packages-dev": [ + { + "name": "doctrine/instantiator", + "version": "1.3.1", + "source": { + "type": "git", + "url": "https://github.com/doctrine/instantiator.git", + "reference": "f350df0268e904597e3bd9c4685c53e0e333feea" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/instantiator/zipball/f350df0268e904597e3bd9c4685c53e0e333feea", + "reference": "f350df0268e904597e3bd9c4685c53e0e333feea", + "shasum": "" + }, + "require": { + "php": "^7.1 || ^8.0" + }, + "require-dev": { + "doctrine/coding-standard": "^6.0", + "ext-pdo": "*", + "ext-phar": "*", + "phpbench/phpbench": "^0.13", + "phpstan/phpstan-phpunit": "^0.11", + "phpstan/phpstan-shim": "^0.11", + "phpunit/phpunit": "^7.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.2.x-dev" + } + }, + "autoload": { + "psr-4": { + "Doctrine\\Instantiator\\": "src/Doctrine/Instantiator/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Marco Pivetta", + "email": "ocramius@gmail.com", + "homepage": "http://ocramius.github.com/" + } + ], + "description": "A small, lightweight utility to instantiate objects in PHP without invoking their constructors", + "homepage": "https://www.doctrine-project.org/projects/instantiator.html", + "keywords": [ + "constructor", + "instantiate" + ], + "time": "2020-05-29T17:27:14+00:00" + }, + { + "name": "guzzlehttp/guzzle", + "version": "6.5.5", + "source": { + "type": "git", + "url": "https://github.com/guzzle/guzzle.git", + "reference": "9d4290de1cfd701f38099ef7e183b64b4b7b0c5e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/guzzle/zipball/9d4290de1cfd701f38099ef7e183b64b4b7b0c5e", + "reference": "9d4290de1cfd701f38099ef7e183b64b4b7b0c5e", + "shasum": "" + }, + "require": { + "ext-json": "*", + "guzzlehttp/promises": "^1.0", + "guzzlehttp/psr7": "^1.6.1", + "php": ">=5.5", + "symfony/polyfill-intl-idn": "^1.17.0" + }, + "require-dev": { + "ext-curl": "*", + "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.4 || ^7.0", + "psr/log": "^1.1" + }, + "suggest": { + "psr/log": "Required for using the Log middleware" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "6.5-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle is a PHP HTTP client library", + "homepage": "http://guzzlephp.org/", + "keywords": [ + "client", + "curl", + "framework", + "http", + "http client", + "rest", + "web service" + ], + "time": "2020-06-16T21:01:06+00:00" + }, + { + "name": "guzzlehttp/promises", + "version": "v1.3.1", + "source": { + "type": "git", + "url": "https://github.com/guzzle/promises.git", + "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/promises/zipball/a59da6cf61d80060647ff4d3eb2c03a2bc694646", + "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646", + "shasum": "" + }, + "require": { + "php": ">=5.5.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Promise\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle promises library", + "keywords": [ + "promise" + ], + "time": "2016-12-20T10:07:11+00:00" + }, + { + "name": "guzzlehttp/psr7", + "version": "1.6.1", + "source": { + "type": "git", + "url": "https://github.com/guzzle/psr7.git", + "reference": "239400de7a173fe9901b9ac7c06497751f00727a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/psr7/zipball/239400de7a173fe9901b9ac7c06497751f00727a", + "reference": "239400de7a173fe9901b9ac7c06497751f00727a", + "shasum": "" + }, + "require": { + "php": ">=5.4.0", + "psr/http-message": "~1.0", + "ralouphie/getallheaders": "^2.0.5 || ^3.0.0" + }, + "provide": { + "psr/http-message-implementation": "1.0" + }, + "require-dev": { + "ext-zlib": "*", + "phpunit/phpunit": "~4.8.36 || ^5.7.27 || ^6.5.8" + }, + "suggest": { + "zendframework/zend-httphandlerrunner": "Emit PSR-7 responses" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.6-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Psr7\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + }, + { + "name": "Tobias Schultze", + "homepage": "https://github.com/Tobion" + } + ], + "description": "PSR-7 message implementation that also provides common utility methods", + "keywords": [ + "http", + "message", + "psr-7", + "request", + "response", + "stream", + "uri", + "url" + ], + "time": "2019-07-01T23:21:34+00:00" + }, + { + "name": "myclabs/deep-copy", + "version": "1.10.1", + "source": { + "type": "git", + "url": "https://github.com/myclabs/DeepCopy.git", + "reference": "969b211f9a51aa1f6c01d1d2aef56d3bd91598e5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/969b211f9a51aa1f6c01d1d2aef56d3bd91598e5", + "reference": "969b211f9a51aa1f6c01d1d2aef56d3bd91598e5", + "shasum": "" + }, + "require": { + "php": "^7.1 || ^8.0" + }, + "replace": { + "myclabs/deep-copy": "self.version" + }, + "require-dev": { + "doctrine/collections": "^1.0", + "doctrine/common": "^2.6", + "phpunit/phpunit": "^7.1" + }, + "type": "library", + "autoload": { + "psr-4": { + "DeepCopy\\": "src/DeepCopy/" + }, + "files": [ + "src/DeepCopy/deep_copy.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Create deep copies (clones) of your objects", + "keywords": [ + "clone", + "copy", + "duplicate", + "object", + "object graph" + ], + "time": "2020-06-29T13:22:24+00:00" + }, + { + "name": "paragonie/random_compat", + "version": "v9.99.99", + "source": { + "type": "git", + "url": "https://github.com/paragonie/random_compat.git", + "reference": "84b4dfb120c6f9b4ff7b3685f9b8f1aa365a0c95" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/paragonie/random_compat/zipball/84b4dfb120c6f9b4ff7b3685f9b8f1aa365a0c95", + "reference": "84b4dfb120c6f9b4ff7b3685f9b8f1aa365a0c95", + "shasum": "" + }, + "require": { + "php": "^7" + }, + "require-dev": { + "phpunit/phpunit": "4.*|5.*", + "vimeo/psalm": "^1" + }, + "suggest": { + "ext-libsodium": "Provides a modern crypto API that can be used to generate random bytes." + }, + "type": "library", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Paragon Initiative Enterprises", + "email": "security@paragonie.com", + "homepage": "https://paragonie.com" + } + ], + "description": "PHP 5.x polyfill for random_bytes() and random_int() from PHP 7", + "keywords": [ + "csprng", + "polyfill", + "pseudorandom", + "random" + ], + "time": "2018-07-02T15:55:56+00:00" + }, + { + "name": "phar-io/manifest", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/phar-io/manifest.git", + "reference": "2df402786ab5368a0169091f61a7c1e0eb6852d0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phar-io/manifest/zipball/2df402786ab5368a0169091f61a7c1e0eb6852d0", + "reference": "2df402786ab5368a0169091f61a7c1e0eb6852d0", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-phar": "*", + "phar-io/version": "^1.0.1", + "php": "^5.6 || ^7.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Arne Blankerts", + "email": "arne@blankerts.de", + "role": "Developer" + }, + { + "name": "Sebastian Heuer", + "email": "sebastian@phpeople.de", + "role": "Developer" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "Developer" + } + ], + "description": "Component for reading phar.io manifest information from a PHP Archive (PHAR)", + "time": "2017-03-05T18:14:27+00:00" + }, + { + "name": "phar-io/version", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/phar-io/version.git", + "reference": "a70c0ced4be299a63d32fa96d9281d03e94041df" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phar-io/version/zipball/a70c0ced4be299a63d32fa96d9281d03e94041df", + "reference": "a70c0ced4be299a63d32fa96d9281d03e94041df", + "shasum": "" + }, + "require": { + "php": "^5.6 || ^7.0" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Arne Blankerts", + "email": "arne@blankerts.de", + "role": "Developer" + }, + { + "name": "Sebastian Heuer", + "email": "sebastian@phpeople.de", + "role": "Developer" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "Developer" + } + ], + "description": "Library for handling version information and constraints", + "time": "2017-03-05T17:38:23+00:00" + }, + { + "name": "php-coveralls/php-coveralls", + "version": "v2.1.0", + "source": { + "type": "git", + "url": "https://github.com/php-coveralls/php-coveralls.git", + "reference": "3b00c229726f892bfdadeaf01ea430ffd04a939d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-coveralls/php-coveralls/zipball/3b00c229726f892bfdadeaf01ea430ffd04a939d", + "reference": "3b00c229726f892bfdadeaf01ea430ffd04a939d", + "shasum": "" + }, + "require": { + "ext-json": "*", + "ext-simplexml": "*", + "guzzlehttp/guzzle": "^6.0", + "php": "^5.5 || ^7.0", + "psr/log": "^1.0", + "symfony/config": "^2.1 || ^3.0 || ^4.0", + "symfony/console": "^2.1 || ^3.0 || ^4.0", + "symfony/stopwatch": "^2.0 || ^3.0 || ^4.0", + "symfony/yaml": "^2.0 || ^3.0 || ^4.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.8.35 || ^5.4.3 || ^6.0" + }, + "suggest": { + "symfony/http-kernel": "Allows Symfony integration" + }, + "bin": [ + "bin/php-coveralls" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.1-dev" + } + }, + "autoload": { + "psr-4": { + "PhpCoveralls\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Kitamura Satoshi", + "email": "with.no.parachute@gmail.com", + "homepage": "https://www.facebook.com/satooshi.jp", + "role": "Original creator" + }, + { + "name": "Takashi Matsuo", + "email": "tmatsuo@google.com" + }, + { + "name": "Google Inc" + }, + { + "name": "Dariusz Ruminski", + "email": "dariusz.ruminski@gmail.com", + "homepage": "https://github.com/keradus" + }, + { + "name": "Contributors", + "homepage": "https://github.com/php-coveralls/php-coveralls/graphs/contributors" + } + ], + "description": "PHP client library for Coveralls API", + "homepage": "https://github.com/php-coveralls/php-coveralls", + "keywords": [ + "ci", + "coverage", + "github", + "test" + ], + "time": "2018-05-22T23:11:08+00:00" + }, + { + "name": "phpdocumentor/reflection-common", + "version": "2.1.0", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/ReflectionCommon.git", + "reference": "6568f4687e5b41b054365f9ae03fcb1ed5f2069b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionCommon/zipball/6568f4687e5b41b054365f9ae03fcb1ed5f2069b", + "reference": "6568f4687e5b41b054365f9ae03fcb1ed5f2069b", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.x-dev" + } + }, + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jaap van Otterdijk", + "email": "opensource@ijaap.nl" + } + ], + "description": "Common reflection classes used by phpdocumentor to reflect the code structure", + "homepage": "http://www.phpdoc.org", + "keywords": [ + "FQSEN", + "phpDocumentor", + "phpdoc", + "reflection", + "static analysis" + ], + "time": "2020-04-27T09:25:28+00:00" + }, + { + "name": "phpdocumentor/reflection-docblock", + "version": "4.3.4", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", + "reference": "da3fd972d6bafd628114f7e7e036f45944b62e9c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/da3fd972d6bafd628114f7e7e036f45944b62e9c", + "reference": "da3fd972d6bafd628114f7e7e036f45944b62e9c", + "shasum": "" + }, + "require": { + "php": "^7.0", + "phpdocumentor/reflection-common": "^1.0.0 || ^2.0.0", + "phpdocumentor/type-resolver": "~0.4 || ^1.0.0", + "webmozart/assert": "^1.0" + }, + "require-dev": { + "doctrine/instantiator": "^1.0.5", + "mockery/mockery": "^1.0", + "phpdocumentor/type-resolver": "0.4.*", + "phpunit/phpunit": "^6.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.x-dev" + } + }, + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": [ + "src/" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mike van Riel", + "email": "me@mikevanriel.com" + } + ], + "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", + "time": "2019-12-28T18:55:12+00:00" + }, + { + "name": "phpdocumentor/type-resolver", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/TypeResolver.git", + "reference": "2e32a6d48972b2c1976ed5d8967145b6cec4a4a9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/2e32a6d48972b2c1976ed5d8967145b6cec4a4a9", + "reference": "2e32a6d48972b2c1976ed5d8967145b6cec4a4a9", + "shasum": "" + }, + "require": { + "php": "^7.1", + "phpdocumentor/reflection-common": "^2.0" + }, + "require-dev": { + "ext-tokenizer": "^7.1", + "mockery/mockery": "~1", + "phpunit/phpunit": "^7.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.x-dev" + } + }, + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mike van Riel", + "email": "me@mikevanriel.com" + } + ], + "description": "A PSR-5 based resolver of Class names, Types and Structural Element Names", + "time": "2019-08-22T18:11:29+00:00" + }, + { + "name": "phpspec/prophecy", + "version": "v1.10.3", + "source": { + "type": "git", + "url": "https://github.com/phpspec/prophecy.git", + "reference": "451c3cd1418cf640de218914901e51b064abb093" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpspec/prophecy/zipball/451c3cd1418cf640de218914901e51b064abb093", + "reference": "451c3cd1418cf640de218914901e51b064abb093", + "shasum": "" + }, + "require": { + "doctrine/instantiator": "^1.0.2", + "php": "^5.3|^7.0", + "phpdocumentor/reflection-docblock": "^2.0|^3.0.2|^4.0|^5.0", + "sebastian/comparator": "^1.2.3|^2.0|^3.0|^4.0", + "sebastian/recursion-context": "^1.0|^2.0|^3.0|^4.0" + }, + "require-dev": { + "phpspec/phpspec": "^2.5 || ^3.2", + "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.5 || ^7.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.10.x-dev" + } + }, + "autoload": { + "psr-4": { + "Prophecy\\": "src/Prophecy" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Konstantin Kudryashov", + "email": "ever.zet@gmail.com", + "homepage": "http://everzet.com" + }, + { + "name": "Marcello Duarte", + "email": "marcello.duarte@gmail.com" + } + ], + "description": "Highly opinionated mocking framework for PHP 5.3+", + "homepage": "https://github.com/phpspec/prophecy", + "keywords": [ + "Double", + "Dummy", + "fake", + "mock", + "spy", + "stub" + ], + "time": "2020-03-05T15:02:03+00:00" + }, + { + "name": "phpunit/php-code-coverage", + "version": "6.0.5", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-code-coverage.git", + "reference": "4cab20a326d14de7575a8e235c70d879b569a57a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/4cab20a326d14de7575a8e235c70d879b569a57a", + "reference": "4cab20a326d14de7575a8e235c70d879b569a57a", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-xmlwriter": "*", + "php": "^7.1", + "phpunit/php-file-iterator": "^1.4.2", + "phpunit/php-text-template": "^1.2.1", + "phpunit/php-token-stream": "^3.0", + "sebastian/code-unit-reverse-lookup": "^1.0.1", + "sebastian/environment": "^3.1", + "sebastian/version": "^2.0.1", + "theseer/tokenizer": "^1.1" + }, + "require-dev": { + "phpunit/phpunit": "^7.0" + }, + "suggest": { + "ext-xdebug": "^2.6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "6.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Library that provides collection, processing, and rendering functionality for PHP code coverage information.", + "homepage": "https://github.com/sebastianbergmann/php-code-coverage", + "keywords": [ + "coverage", + "testing", + "xunit" + ], + "time": "2018-05-28T11:49:20+00:00" + }, + { + "name": "phpunit/php-file-iterator", + "version": "1.4.5", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-file-iterator.git", + "reference": "730b01bc3e867237eaac355e06a36b85dd93a8b4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/730b01bc3e867237eaac355e06a36b85dd93a8b4", + "reference": "730b01bc3e867237eaac355e06a36b85dd93a8b4", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "FilterIterator implementation that filters files based on a list of suffixes.", + "homepage": "https://github.com/sebastianbergmann/php-file-iterator/", + "keywords": [ + "filesystem", + "iterator" + ], + "time": "2017-11-27T13:52:08+00:00" + }, + { + "name": "phpunit/php-text-template", + "version": "1.2.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-text-template.git", + "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/31f8b717e51d9a2afca6c9f046f5d69fc27c8686", + "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Simple template engine.", + "homepage": "https://github.com/sebastianbergmann/php-text-template/", + "keywords": [ + "template" + ], + "time": "2015-06-21T13:50:34+00:00" + }, + { + "name": "phpunit/php-timer", + "version": "2.1.2", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-timer.git", + "reference": "1038454804406b0b5f5f520358e78c1c2f71501e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/1038454804406b0b5f5f520358e78c1c2f71501e", + "reference": "1038454804406b0b5f5f520358e78c1c2f71501e", + "shasum": "" + }, + "require": { + "php": "^7.1" + }, + "require-dev": { + "phpunit/phpunit": "^7.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.1-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Utility class for timing", + "homepage": "https://github.com/sebastianbergmann/php-timer/", + "keywords": [ + "timer" + ], + "time": "2019-06-07T04:22:29+00:00" + }, + { + "name": "phpunit/php-token-stream", + "version": "3.1.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-token-stream.git", + "reference": "995192df77f63a59e47f025390d2d1fdf8f425ff" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/995192df77f63a59e47f025390d2d1fdf8f425ff", + "reference": "995192df77f63a59e47f025390d2d1fdf8f425ff", + "shasum": "" + }, + "require": { + "ext-tokenizer": "*", + "php": "^7.1" + }, + "require-dev": { + "phpunit/phpunit": "^7.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.1-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Wrapper around PHP's tokenizer extension.", + "homepage": "https://github.com/sebastianbergmann/php-token-stream/", + "keywords": [ + "tokenizer" + ], + "time": "2019-09-17T06:23:10+00:00" + }, + { + "name": "phpunit/phpunit", + "version": "7.0.2", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/phpunit.git", + "reference": "e2f8aa21bc54b6ba218bdd4f9e0dac1e9bc3b4e9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/e2f8aa21bc54b6ba218bdd4f9e0dac1e9bc3b4e9", + "reference": "e2f8aa21bc54b6ba218bdd4f9e0dac1e9bc3b4e9", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-json": "*", + "ext-libxml": "*", + "ext-mbstring": "*", + "ext-xml": "*", + "myclabs/deep-copy": "^1.6.1", + "phar-io/manifest": "^1.0.1", + "phar-io/version": "^1.0", + "php": "^7.1", + "phpspec/prophecy": "^1.7", + "phpunit/php-code-coverage": "^6.0", + "phpunit/php-file-iterator": "^1.4.3", + "phpunit/php-text-template": "^1.2.1", + "phpunit/php-timer": "^2.0", + "phpunit/phpunit-mock-objects": "^6.0", + "sebastian/comparator": "^2.1", + "sebastian/diff": "^3.0", + "sebastian/environment": "^3.1", + "sebastian/exporter": "^3.1", + "sebastian/global-state": "^2.0", + "sebastian/object-enumerator": "^3.0.3", + "sebastian/resource-operations": "^1.0", + "sebastian/version": "^2.0.1" + }, + "require-dev": { + "ext-pdo": "*" + }, + "suggest": { + "ext-xdebug": "*", + "phpunit/php-invoker": "^2.0" + }, + "bin": [ + "phpunit" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "7.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "The PHP Unit Testing framework.", + "homepage": "https://phpunit.de/", + "keywords": [ + "phpunit", + "testing", + "xunit" + ], + "time": "2018-02-26T07:03:12+00:00" + }, + { + "name": "phpunit/phpunit-mock-objects", + "version": "6.1.2", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", + "reference": "f9756fd4f43f014cb2dca98deeaaa8ce5500a36e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/f9756fd4f43f014cb2dca98deeaaa8ce5500a36e", + "reference": "f9756fd4f43f014cb2dca98deeaaa8ce5500a36e", + "shasum": "" + }, + "require": { + "doctrine/instantiator": "^1.0.5", + "php": "^7.1", + "phpunit/php-text-template": "^1.2.1", + "sebastian/exporter": "^3.1" + }, + "require-dev": { + "phpunit/phpunit": "^7.0" + }, + "suggest": { + "ext-soap": "*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "6.1-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Mock Object library for PHPUnit", + "homepage": "https://github.com/sebastianbergmann/phpunit-mock-objects/", + "keywords": [ + "mock", + "xunit" + ], + "abandoned": true, + "time": "2018-05-29T13:54:20+00:00" + }, + { + "name": "psr/container", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/container.git", + "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/container/zipball/b7ce3b176482dbbc1245ebf52b181af44c2cf55f", + "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Container\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common Container Interface (PHP FIG PSR-11)", + "homepage": "https://github.com/php-fig/container", + "keywords": [ + "PSR-11", + "container", + "container-interface", + "container-interop", + "psr" + ], + "time": "2017-02-14T16:28:37+00:00" + }, + { + "name": "psr/http-message", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-message.git", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Message\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for HTTP messages", + "homepage": "https://github.com/php-fig/http-message", + "keywords": [ + "http", + "http-message", + "psr", + "psr-7", + "request", + "response" + ], + "time": "2016-08-06T14:39:51+00:00" + }, + { + "name": "psr/log", + "version": "1.1.3", + "source": { + "type": "git", + "url": "https://github.com/php-fig/log.git", + "reference": "0f73288fd15629204f9d42b7055f72dacbe811fc" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/log/zipball/0f73288fd15629204f9d42b7055f72dacbe811fc", + "reference": "0f73288fd15629204f9d42b7055f72dacbe811fc", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.1.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Log\\": "Psr/Log/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for logging libraries", + "homepage": "https://github.com/php-fig/log", + "keywords": [ + "log", + "psr", + "psr-3" + ], + "time": "2020-03-23T09:12:05+00:00" + }, + { + "name": "ralouphie/getallheaders", + "version": "3.0.3", + "source": { + "type": "git", + "url": "https://github.com/ralouphie/getallheaders.git", + "reference": "120b605dfeb996808c31b6477290a714d356e822" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/ralouphie/getallheaders/zipball/120b605dfeb996808c31b6477290a714d356e822", + "reference": "120b605dfeb996808c31b6477290a714d356e822", + "shasum": "" + }, + "require": { + "php": ">=5.6" + }, + "require-dev": { + "php-coveralls/php-coveralls": "^2.1", + "phpunit/phpunit": "^5 || ^6.5" + }, + "type": "library", + "autoload": { + "files": [ + "src/getallheaders.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ralph Khattar", + "email": "ralph.khattar@gmail.com" + } + ], + "description": "A polyfill for getallheaders.", + "time": "2019-03-08T08:55:37+00:00" + }, + { + "name": "sebastian/code-unit-reverse-lookup", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/code-unit-reverse-lookup.git", + "reference": "4419fcdb5eabb9caa61a27c7a1db532a6b55dd18" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/code-unit-reverse-lookup/zipball/4419fcdb5eabb9caa61a27c7a1db532a6b55dd18", + "reference": "4419fcdb5eabb9caa61a27c7a1db532a6b55dd18", + "shasum": "" + }, + "require": { + "php": "^5.6 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^5.7 || ^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Looks up which function or method a line of code belongs to", + "homepage": "https://github.com/sebastianbergmann/code-unit-reverse-lookup/", + "time": "2017-03-04T06:30:41+00:00" + }, + { + "name": "sebastian/comparator", + "version": "2.1.3", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/comparator.git", + "reference": "34369daee48eafb2651bea869b4b15d75ccc35f9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/34369daee48eafb2651bea869b4b15d75ccc35f9", + "reference": "34369daee48eafb2651bea869b4b15d75ccc35f9", + "shasum": "" + }, + "require": { + "php": "^7.0", + "sebastian/diff": "^2.0 || ^3.0", + "sebastian/exporter": "^3.1" + }, + "require-dev": { + "phpunit/phpunit": "^6.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.1.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Volker Dusch", + "email": "github@wallbash.com" + }, + { + "name": "Bernhard Schussek", + "email": "bschussek@2bepublished.at" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides the functionality to compare PHP values for equality", + "homepage": "https://github.com/sebastianbergmann/comparator", + "keywords": [ + "comparator", + "compare", + "equality" + ], + "time": "2018-02-01T13:46:46+00:00" + }, + { + "name": "sebastian/diff", + "version": "3.0.2", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/diff.git", + "reference": "720fcc7e9b5cf384ea68d9d930d480907a0c1a29" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/720fcc7e9b5cf384ea68d9d930d480907a0c1a29", + "reference": "720fcc7e9b5cf384ea68d9d930d480907a0c1a29", + "shasum": "" + }, + "require": { + "php": "^7.1" + }, + "require-dev": { + "phpunit/phpunit": "^7.5 || ^8.0", + "symfony/process": "^2 || ^3.3 || ^4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Kore Nordmann", + "email": "mail@kore-nordmann.de" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Diff implementation", + "homepage": "https://github.com/sebastianbergmann/diff", + "keywords": [ + "diff", + "udiff", + "unidiff", + "unified diff" + ], + "time": "2019-02-04T06:01:07+00:00" + }, + { + "name": "sebastian/environment", + "version": "3.1.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/environment.git", + "reference": "cd0871b3975fb7fc44d11314fd1ee20925fce4f5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/cd0871b3975fb7fc44d11314fd1ee20925fce4f5", + "reference": "cd0871b3975fb7fc44d11314fd1ee20925fce4f5", + "shasum": "" + }, + "require": { + "php": "^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.1.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides functionality to handle HHVM/PHP environments", + "homepage": "http://www.github.com/sebastianbergmann/environment", + "keywords": [ + "Xdebug", + "environment", + "hhvm" + ], + "time": "2017-07-01T08:51:00+00:00" + }, + { + "name": "sebastian/exporter", + "version": "3.1.2", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/exporter.git", + "reference": "68609e1261d215ea5b21b7987539cbfbe156ec3e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/68609e1261d215ea5b21b7987539cbfbe156ec3e", + "reference": "68609e1261d215ea5b21b7987539cbfbe156ec3e", + "shasum": "" + }, + "require": { + "php": "^7.0", + "sebastian/recursion-context": "^3.0" + }, + "require-dev": { + "ext-mbstring": "*", + "phpunit/phpunit": "^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.1.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + }, + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Volker Dusch", + "email": "github@wallbash.com" + }, + { + "name": "Adam Harvey", + "email": "aharvey@php.net" + }, + { + "name": "Bernhard Schussek", + "email": "bschussek@gmail.com" + } + ], + "description": "Provides the functionality to export PHP variables for visualization", + "homepage": "http://www.github.com/sebastianbergmann/exporter", + "keywords": [ + "export", + "exporter" + ], + "time": "2019-09-14T09:02:43+00:00" + }, + { + "name": "sebastian/global-state", + "version": "2.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/global-state.git", + "reference": "e8ba02eed7bbbb9e59e43dedd3dddeff4a56b0c4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/e8ba02eed7bbbb9e59e43dedd3dddeff4a56b0c4", + "reference": "e8ba02eed7bbbb9e59e43dedd3dddeff4a56b0c4", + "shasum": "" + }, + "require": { + "php": "^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.0" + }, + "suggest": { + "ext-uopz": "*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Snapshotting of global state", + "homepage": "http://www.github.com/sebastianbergmann/global-state", + "keywords": [ + "global state" + ], + "time": "2017-04-27T15:39:26+00:00" + }, + { + "name": "sebastian/object-enumerator", + "version": "3.0.3", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/object-enumerator.git", + "reference": "7cfd9e65d11ffb5af41198476395774d4c8a84c5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/object-enumerator/zipball/7cfd9e65d11ffb5af41198476395774d4c8a84c5", + "reference": "7cfd9e65d11ffb5af41198476395774d4c8a84c5", + "shasum": "" + }, + "require": { + "php": "^7.0", + "sebastian/object-reflector": "^1.1.1", + "sebastian/recursion-context": "^3.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Traverses array structures and object graphs to enumerate all referenced objects", + "homepage": "https://github.com/sebastianbergmann/object-enumerator/", + "time": "2017-08-03T12:35:26+00:00" + }, + { + "name": "sebastian/object-reflector", + "version": "1.1.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/object-reflector.git", + "reference": "773f97c67f28de00d397be301821b06708fca0be" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/object-reflector/zipball/773f97c67f28de00d397be301821b06708fca0be", + "reference": "773f97c67f28de00d397be301821b06708fca0be", + "shasum": "" + }, + "require": { + "php": "^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.1-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Allows reflection of object attributes, including inherited and non-public ones", + "homepage": "https://github.com/sebastianbergmann/object-reflector/", + "time": "2017-03-29T09:07:27+00:00" + }, + { + "name": "sebastian/recursion-context", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/recursion-context.git", + "reference": "5b0cd723502bac3b006cbf3dbf7a1e3fcefe4fa8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/5b0cd723502bac3b006cbf3dbf7a1e3fcefe4fa8", + "reference": "5b0cd723502bac3b006cbf3dbf7a1e3fcefe4fa8", + "shasum": "" + }, + "require": { + "php": "^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + }, + { + "name": "Adam Harvey", + "email": "aharvey@php.net" + } + ], + "description": "Provides functionality to recursively process PHP variables", + "homepage": "http://www.github.com/sebastianbergmann/recursion-context", + "time": "2017-03-03T06:23:57+00:00" + }, + { + "name": "sebastian/resource-operations", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/resource-operations.git", + "reference": "ce990bb21759f94aeafd30209e8cfcdfa8bc3f52" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/resource-operations/zipball/ce990bb21759f94aeafd30209e8cfcdfa8bc3f52", + "reference": "ce990bb21759f94aeafd30209e8cfcdfa8bc3f52", + "shasum": "" + }, + "require": { + "php": ">=5.6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides a list of PHP built-in functions that operate on resources", + "homepage": "https://www.github.com/sebastianbergmann/resource-operations", + "time": "2015-07-28T20:34:47+00:00" + }, + { + "name": "sebastian/version", + "version": "2.0.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/version.git", + "reference": "99732be0ddb3361e16ad77b68ba41efc8e979019" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/99732be0ddb3361e16ad77b68ba41efc8e979019", + "reference": "99732be0ddb3361e16ad77b68ba41efc8e979019", + "shasum": "" + }, + "require": { + "php": ">=5.6" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Library that helps with managing the version number of Git-hosted PHP projects", + "homepage": "https://github.com/sebastianbergmann/version", + "time": "2016-10-03T07:35:21+00:00" + }, + { + "name": "squizlabs/php_codesniffer", + "version": "3.2.3", + "source": { + "type": "git", + "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", + "reference": "4842476c434e375f9d3182ff7b89059583aa8b27" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/4842476c434e375f9d3182ff7b89059583aa8b27", + "reference": "4842476c434e375f9d3182ff7b89059583aa8b27", + "shasum": "" + }, + "require": { + "ext-simplexml": "*", + "ext-tokenizer": "*", + "ext-xmlwriter": "*", + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" + }, + "bin": [ + "bin/phpcs", + "bin/phpcbf" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Greg Sherwood", + "role": "lead" + } + ], + "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", + "homepage": "http://www.squizlabs.com/php-codesniffer", + "keywords": [ + "phpcs", + "standards" + ], + "time": "2018-02-20T21:35:23+00:00" + }, + { + "name": "symfony/config", + "version": "v4.4.10", + "source": { + "type": "git", + "url": "https://github.com/symfony/config.git", + "reference": "395f6e09e1dc6ac9c1a5eea3b7f44f7a820a5504" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/config/zipball/395f6e09e1dc6ac9c1a5eea3b7f44f7a820a5504", + "reference": "395f6e09e1dc6ac9c1a5eea3b7f44f7a820a5504", + "shasum": "" + }, + "require": { + "php": ">=7.1.3", + "symfony/filesystem": "^3.4|^4.0|^5.0", + "symfony/polyfill-ctype": "~1.8" + }, + "conflict": { + "symfony/finder": "<3.4" + }, + "require-dev": { + "symfony/event-dispatcher": "^3.4|^4.0|^5.0", + "symfony/finder": "^3.4|^4.0|^5.0", + "symfony/messenger": "^4.1|^5.0", + "symfony/service-contracts": "^1.1|^2", + "symfony/yaml": "^3.4|^4.0|^5.0" + }, + "suggest": { + "symfony/yaml": "To use the yaml reference dumper" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.4-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Config\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Config Component", + "homepage": "https://symfony.com", + "time": "2020-05-23T09:11:46+00:00" + }, + { + "name": "symfony/console", + "version": "v4.4.10", + "source": { + "type": "git", + "url": "https://github.com/symfony/console.git", + "reference": "326b064d804043005526f5a0494cfb49edb59bb0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/console/zipball/326b064d804043005526f5a0494cfb49edb59bb0", + "reference": "326b064d804043005526f5a0494cfb49edb59bb0", + "shasum": "" + }, + "require": { + "php": ">=7.1.3", + "symfony/polyfill-mbstring": "~1.0", + "symfony/polyfill-php73": "^1.8", + "symfony/polyfill-php80": "^1.15", + "symfony/service-contracts": "^1.1|^2" + }, + "conflict": { + "symfony/dependency-injection": "<3.4", + "symfony/event-dispatcher": "<4.3|>=5", + "symfony/lock": "<4.4", + "symfony/process": "<3.3" + }, + "provide": { + "psr/log-implementation": "1.0" + }, + "require-dev": { + "psr/log": "~1.0", + "symfony/config": "^3.4|^4.0|^5.0", + "symfony/dependency-injection": "^3.4|^4.0|^5.0", + "symfony/event-dispatcher": "^4.3", + "symfony/lock": "^4.4|^5.0", + "symfony/process": "^3.4|^4.0|^5.0", + "symfony/var-dumper": "^4.3|^5.0" + }, + "suggest": { + "psr/log": "For using the console logger", + "symfony/event-dispatcher": "", + "symfony/lock": "", + "symfony/process": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.4-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Console\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Console Component", + "homepage": "https://symfony.com", + "time": "2020-05-30T20:06:45+00:00" + }, + { + "name": "symfony/filesystem", + "version": "v4.4.10", + "source": { + "type": "git", + "url": "https://github.com/symfony/filesystem.git", + "reference": "b27f491309db5757816db672b256ea2e03677d30" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/filesystem/zipball/b27f491309db5757816db672b256ea2e03677d30", + "reference": "b27f491309db5757816db672b256ea2e03677d30", + "shasum": "" + }, + "require": { + "php": ">=7.1.3", + "symfony/polyfill-ctype": "~1.8" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.4-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Filesystem\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Filesystem Component", + "homepage": "https://symfony.com", + "time": "2020-05-30T18:50:54+00:00" + }, + { + "name": "symfony/polyfill-ctype", + "version": "v1.18.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-ctype.git", + "reference": "1c302646f6efc070cd46856e600e5e0684d6b454" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/1c302646f6efc070cd46856e600e5e0684d6b454", + "reference": "1c302646f6efc070cd46856e600e5e0684d6b454", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "suggest": { + "ext-ctype": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.18-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Ctype\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Gert de Pagter", + "email": "BackEndTea@gmail.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for ctype functions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "ctype", + "polyfill", + "portable" + ], + "time": "2020-07-14T12:35:20+00:00" + }, + { + "name": "symfony/polyfill-intl-idn", + "version": "v1.18.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-intl-idn.git", + "reference": "bc6549d068d0160e0f10f7a5a23c7d1406b95ebe" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-intl-idn/zipball/bc6549d068d0160e0f10f7a5a23c7d1406b95ebe", + "reference": "bc6549d068d0160e0f10f7a5a23c7d1406b95ebe", + "shasum": "" + }, + "require": { + "php": ">=5.3.3", + "symfony/polyfill-intl-normalizer": "^1.10", + "symfony/polyfill-php70": "^1.10", + "symfony/polyfill-php72": "^1.10" + }, + "suggest": { + "ext-intl": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.18-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Intl\\Idn\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Laurent Bassin", + "email": "laurent@bassin.info" + }, + { + "name": "Trevor Rowbotham", + "email": "trevor.rowbotham@pm.me" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "idn", + "intl", + "polyfill", + "portable", + "shim" + ], + "time": "2020-07-14T12:35:20+00:00" + }, + { + "name": "symfony/polyfill-intl-normalizer", + "version": "v1.18.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-intl-normalizer.git", + "reference": "37078a8dd4a2a1e9ab0231af7c6cb671b2ed5a7e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-intl-normalizer/zipball/37078a8dd4a2a1e9ab0231af7c6cb671b2ed5a7e", + "reference": "37078a8dd4a2a1e9ab0231af7c6cb671b2ed5a7e", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "suggest": { + "ext-intl": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.18-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Intl\\Normalizer\\": "" + }, + "files": [ + "bootstrap.php" + ], + "classmap": [ + "Resources/stubs" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for intl's Normalizer class and related functions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "intl", + "normalizer", + "polyfill", + "portable", + "shim" + ], + "time": "2020-07-14T12:35:20+00:00" + }, + { + "name": "symfony/polyfill-mbstring", + "version": "v1.18.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-mbstring.git", + "reference": "a6977d63bf9a0ad4c65cd352709e230876f9904a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/a6977d63bf9a0ad4c65cd352709e230876f9904a", + "reference": "a6977d63bf9a0ad4c65cd352709e230876f9904a", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "suggest": { + "ext-mbstring": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.18-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Mbstring\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for the Mbstring extension", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "mbstring", + "polyfill", + "portable", + "shim" + ], + "time": "2020-07-14T12:35:20+00:00" + }, + { + "name": "symfony/polyfill-php70", + "version": "v1.18.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-php70.git", + "reference": "0dd93f2c578bdc9c72697eaa5f1dd25644e618d3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-php70/zipball/0dd93f2c578bdc9c72697eaa5f1dd25644e618d3", + "reference": "0dd93f2c578bdc9c72697eaa5f1dd25644e618d3", + "shasum": "" + }, + "require": { + "paragonie/random_compat": "~1.0|~2.0|~9.99", + "php": ">=5.3.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.18-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Php70\\": "" + }, + "files": [ + "bootstrap.php" + ], + "classmap": [ + "Resources/stubs" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill backporting some PHP 7.0+ features to lower PHP versions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "polyfill", + "portable", + "shim" + ], + "time": "2020-07-14T12:35:20+00:00" + }, + { + "name": "symfony/polyfill-php72", + "version": "v1.18.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-php72.git", + "reference": "639447d008615574653fb3bc60d1986d7172eaae" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-php72/zipball/639447d008615574653fb3bc60d1986d7172eaae", + "reference": "639447d008615574653fb3bc60d1986d7172eaae", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.18-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Php72\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "polyfill", + "portable", + "shim" + ], + "time": "2020-07-14T12:35:20+00:00" + }, + { + "name": "symfony/polyfill-php73", + "version": "v1.18.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-php73.git", + "reference": "fffa1a52a023e782cdcc221d781fe1ec8f87fcca" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-php73/zipball/fffa1a52a023e782cdcc221d781fe1ec8f87fcca", + "reference": "fffa1a52a023e782cdcc221d781fe1ec8f87fcca", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.18-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Php73\\": "" + }, + "files": [ + "bootstrap.php" + ], + "classmap": [ + "Resources/stubs" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "polyfill", + "portable", + "shim" + ], + "time": "2020-07-14T12:35:20+00:00" + }, + { + "name": "symfony/polyfill-php80", + "version": "v1.18.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-php80.git", + "reference": "d87d5766cbf48d72388a9f6b85f280c8ad51f981" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/d87d5766cbf48d72388a9f6b85f280c8ad51f981", + "reference": "d87d5766cbf48d72388a9f6b85f280c8ad51f981", + "shasum": "" + }, + "require": { + "php": ">=7.0.8" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.18-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Php80\\": "" + }, + "files": [ + "bootstrap.php" + ], + "classmap": [ + "Resources/stubs" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ion Bazan", + "email": "ion.bazan@gmail.com" + }, + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "polyfill", + "portable", + "shim" + ], + "time": "2020-07-14T12:35:20+00:00" + }, + { + "name": "symfony/service-contracts", + "version": "v1.1.9", + "source": { + "type": "git", + "url": "https://github.com/symfony/service-contracts.git", + "reference": "b776d18b303a39f56c63747bcb977ad4b27aca26" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/service-contracts/zipball/b776d18b303a39f56c63747bcb977ad4b27aca26", + "reference": "b776d18b303a39f56c63747bcb977ad4b27aca26", + "shasum": "" + }, + "require": { + "php": ">=7.1.3", + "psr/container": "^1.0" + }, + "suggest": { + "symfony/service-implementation": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.1-dev" + }, + "thanks": { + "name": "symfony/contracts", + "url": "https://github.com/symfony/contracts" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Contracts\\Service\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Generic abstractions related to writing services", + "homepage": "https://symfony.com", + "keywords": [ + "abstractions", + "contracts", + "decoupling", + "interfaces", + "interoperability", + "standards" + ], + "time": "2020-07-06T13:19:58+00:00" + }, + { + "name": "symfony/stopwatch", + "version": "v4.4.10", + "source": { + "type": "git", + "url": "https://github.com/symfony/stopwatch.git", + "reference": "f51fb90df1154a7f75987198a9689e28f91e6a50" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/stopwatch/zipball/f51fb90df1154a7f75987198a9689e28f91e6a50", + "reference": "f51fb90df1154a7f75987198a9689e28f91e6a50", + "shasum": "" + }, + "require": { + "php": ">=7.1.3", + "symfony/service-contracts": "^1.0|^2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.4-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Stopwatch\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Stopwatch Component", + "homepage": "https://symfony.com", + "time": "2020-05-20T08:37:50+00:00" + }, + { + "name": "symfony/yaml", + "version": "v4.4.10", + "source": { + "type": "git", + "url": "https://github.com/symfony/yaml.git", + "reference": "c2d2cc66e892322cfcc03f8f12f8340dbd7a3f8a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/yaml/zipball/c2d2cc66e892322cfcc03f8f12f8340dbd7a3f8a", + "reference": "c2d2cc66e892322cfcc03f8f12f8340dbd7a3f8a", + "shasum": "" + }, + "require": { + "php": ">=7.1.3", + "symfony/polyfill-ctype": "~1.8" + }, + "conflict": { + "symfony/console": "<3.4" + }, + "require-dev": { + "symfony/console": "^3.4|^4.0|^5.0" + }, + "suggest": { + "symfony/console": "For validating YAML files using the lint command" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.4-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Yaml\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Yaml Component", + "homepage": "https://symfony.com", + "time": "2020-05-20T08:37:50+00:00" + }, + { + "name": "theseer/tokenizer", + "version": "1.1.3", + "source": { + "type": "git", + "url": "https://github.com/theseer/tokenizer.git", + "reference": "11336f6f84e16a720dae9d8e6ed5019efa85a0f9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/theseer/tokenizer/zipball/11336f6f84e16a720dae9d8e6ed5019efa85a0f9", + "reference": "11336f6f84e16a720dae9d8e6ed5019efa85a0f9", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-tokenizer": "*", + "ext-xmlwriter": "*", + "php": "^7.0" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Arne Blankerts", + "email": "arne@blankerts.de", + "role": "Developer" + } + ], + "description": "A small library for converting tokenized PHP source code into XML and potentially other formats", + "time": "2019-06-13T22:48:21+00:00" + }, + { + "name": "webmozart/assert", + "version": "1.9.1", + "source": { + "type": "git", + "url": "https://github.com/webmozart/assert.git", + "reference": "bafc69caeb4d49c39fd0779086c03a3738cbb389" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/webmozart/assert/zipball/bafc69caeb4d49c39fd0779086c03a3738cbb389", + "reference": "bafc69caeb4d49c39fd0779086c03a3738cbb389", + "shasum": "" + }, + "require": { + "php": "^5.3.3 || ^7.0 || ^8.0", + "symfony/polyfill-ctype": "^1.8" + }, + "conflict": { + "phpstan/phpstan": "<0.12.20", + "vimeo/psalm": "<3.9.1" + }, + "require-dev": { + "phpunit/phpunit": "^4.8.36 || ^7.5.13" + }, + "type": "library", + "autoload": { + "psr-4": { + "Webmozart\\Assert\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Bernhard Schussek", + "email": "bschussek@gmail.com" + } + ], + "description": "Assertions to validate method input/output with nice error messages.", + "keywords": [ + "assert", + "check", + "validate" + ], + "time": "2020-07-08T17:02:28+00:00" + } + ], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] +} diff --git a/phpcs.xml b/phpcs.xml index 607100f..7e20a00 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -1,11 +1,11 @@ - The coding standard for PHP_CodeSniffer itself. + The coding standard for PHP_CodeSniffer itself. - src + src - - + + - + \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml index 6ab6983..11595ec 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,57 +1,63 @@ - - test/AbstractQuery.php - test/AbstractSql.php - test/Collection.php - test/Model.php - test/Search.php - test/QueryDelete.php - test/QueryInsert.php - test/QuerySelect.php - test/QueryUpdate.php - test/SqlException.php - test/MySql/QueryAlter.php - test/MySql/QueryCreate.php - test/MySql/QuerySubSelect.php - test/MySql/QueryUtility.php - test/PostGreSql/QueryAlter.php - test/PostGreSql/QueryCreate.php - test/PostGreSql/QueryDelete.php - test/PostGreSql/QueryInsert.php - test/PostGreSql/QuerySelect.php - test/PostGreSql/QueryUpdate.php - test/PostGreSql/QueryUtility.php - test/Sqlite/QueryAlter.php - test/Sqlite/QueryCreate.php - test/Sqlite/QueryUtility.php - test/MySql.php - test/PostGreSql.php - test/Sqlite.php + test/SqlFactory.php + test/SqlException.php + + + test/Query/AbstractQuery.php + test/Query/Delete.php + test/Query/Insert.php + test/Query/Select.php + test/Query/Update.php + test/Query/MySql/Alter.php + test/Query/MySql/Create.php + test/Query/MySql/Utility.php + test/Query/PostGreSql/Alter.php + test/Query/PostGreSql/Create.php + test/Query/PostGreSql/Delete.php + test/Query/PostGreSql/Insert.php + test/Query/PostGreSql/Select.php + test/Query/PostGreSql/Update.php + test/Query/PostGreSql/Utility.php + test/Query/Sqlite/Alter.php + test/Query/Sqlite/Create.php + test/Query/Sqlite/Utility.php + + + test/Mapper/AbstractMapper.php + test/Mapper/Model.php + test/Mapper/Search.php + test/Mapper/Collection.php + test/Mapper/Update.php + test/Mapper/Remove.php + + + test/Engine/AbstractEngine.php + test/Engine/MySql.php + test/Engine/PostGreSql.php + test/Engine/Sqlite.php - - - ./src - - ./vendor - ./test - src/SqlInterface.php - - + + ./src + + ./vendor + ./test + + diff --git a/src/AbstractQuery.php b/src/AbstractQuery.php deleted file mode 100644 index ea49c12..0000000 --- a/src/AbstractQuery.php +++ /dev/null @@ -1,46 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm; - -/** - * Generates select query string syntax - * - * @vendor Cradle - * @package Sql - * @author Christian Blanquera - * @standard PSR-2 - */ -abstract class AbstractQuery -{ - /** - * @var string $table most queries deal with tables - */ - protected $table = null; - - /** - * Transform class to string using - * getQuery - * - * @return string - */ - public function __toString() - { - return $this->getQuery(); - } - - /** - * Returns the string version of the query - * - * @param bool - * - * @return string - */ - abstract public function getQuery(); -} diff --git a/src/AbstractSql.php b/src/AbstractSql.php deleted file mode 100644 index aa14927..0000000 --- a/src/AbstractSql.php +++ /dev/null @@ -1,654 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm; - -use StdClass; -use Closure; -use PDO; -use ReflectionClass; - -use Cradle\Helper\InstanceTrait; -use Cradle\Helper\LoopTrait; -use Cradle\Helper\ConditionalTrait; - -use Cradle\Profiler\InspectorTrait; -use Cradle\Profiler\LoggerTrait; - -use Cradle\Resolver\StateTrait; - -/** - * Abstractly defines a layout of available methods to - * connect to and query a database. This class also lays out - * query building methods that auto renders a valid query - * the specific database will understand without actually - * needing to know the query language. - * - * @vendor Cradle - * @package Sql - * @author Christian Blanquera - * @standard PSR-2 - */ -abstract class AbstractSql -{ - use InstanceTrait, - LoopTrait, - ConditionalTrait, - InspectorTrait, - LoggerTrait, - StateTrait - { - StateTrait::__callResolver as __call; - } - - /** - * @const int INSTANCE Flag that designates multiton when using ::i() - */ - const INSTANCE = 0; - - /** - * @const string FIRST The first index in getQueries - */ - const FIRST = 'first'; - - /** - * @const string LAST The last index in getQueries - */ - const LAST = 'last'; - - /** - * @var [RESOURCE] $connection PDO resource - */ - protected $connection = null; - - /** - * @var array $binds Bound data from the current query - */ - protected $binds = []; - - /** - * Connects to the database - * - * @param PDO|array $options The connection options - * - * @return AbstractSQL - */ - abstract public function connect($options = []); - - /** - * Binds a value and returns the bound key - * - * @param *string|array|number|null $value What to bind - * - * @return string - */ - public function bind($value) - { - if (is_array($value)) { - foreach ($value as $i => $item) { - $value[$i] = $this->bind($item); - } - - return '('.implode(",", $value).')'; - } else if (is_int($value) || ctype_digit($value)) { - if (strpos($value, '0') !== 0) { - return $value; - } - } - - $name = ':bind'.count($this->binds).'bind'; - $this->binds[$name] = $value; - return $name; - } - - /** - * Returns collection - * - * @param array $data Initial collection data - * - * @return Collection - */ - public function collection(array $data = []) - { - return $this - ->resolve(Collection::class) - ->setDatabase($this) - ->set($data); - } - - /** - * Removes rows that match a filter - * - * @param *string|null $table The table name - * @param array|string $filters Filters to test against - * - * @return AbstractSql - */ - public function deleteRows($table, $filters = null) - { - $query = $this->getDeleteQuery($table); - - //array('post_id=%s AND post_title IN %s', 123, array('asd')); - if (is_array($filters)) { - //can be array of arrays - if (is_array($filters[0])) { - foreach ($filters as $i => $filter) { - if (is_array($filters)) { - $format = array_shift($filter); - - //reindex filters - $filter = array_values($filter); - - //bind filters - foreach ($filter as $i => $value) { - $filter[$i] = $this->bind($value); - } - - //combine - $query->where(vsprintf($format, $filter)); - } - } - } else { - $format = array_shift($filters); - - //reindex filters - $filters = array_values($filters); - - //bind filters - foreach ($filters as $i => $value) { - $filters[$i] = $this->bind($value); - } - - //combine - $query->where(vsprintf($format, $filters)); - } - } else { - $query->where($filters); - } - - //run the query - $this->query($query, $this->getBinds()); - - return $this; - } - - /** - * Returns all the bound values of this query - * - * @return array - */ - public function getBinds() - { - return $this->binds; - } - - /** - * Returns the connection object - * if no connection has been made - * it will attempt to make it - * - * @return resource PDO connection resource - */ - public function getConnection() - { - if (!$this->connection) { - $this->connect(); - } - - return $this->connection; - } - - /** - * Returns the delete query builder - * - * @param *string|null $table The table name - * - * @return QueryDelete - */ - public function getDeleteQuery($table = null) - { - return $this->resolve(QueryDelete::class, $table); - } - - /** - * Returns the insert query builder - * - * @param string|null $table Name of table - * - * @return QueryInsert - */ - public function getInsertQuery($table = null) - { - return $this->resolve(QueryInsert::class, $table); - } - - /** - * Returns the last inserted id - * - * @param string|null $column A particular column name - * - * @return int the id - */ - public function getLastInsertedId($column = null) - { - if (is_string($column)) { - return $this->getConnection()->lastInsertId($column); - } - - return $this->getConnection()->lastInsertId(); - } - - /** - * Returns a model given the column name and the value - * - * @param *string $table Table name - * @param *string $name Column name - * @param *scalar|null $value Column value - * - * @return Model|null - */ - public function getModel($table, $name, $value) - { - //get the row - $result = $this->getRow($table, $name, $value); - - if (is_null($result)) { - return null; - } - - return $this->model()->setTable($table)->set($result); - } - - /** - * Returns a 1 row result given the column name and the value - * - * @param *string $table Table name - * @param *string $name Column name - * @param *scalar|null $value Column value - * - * @return array|null - */ - public function getRow($table, $name, $value) - { - //make the query - $query = $this - ->getSelectQuery() - ->from($table) - ->where($name.' = '.$this->bind($value)) - ->limit(0, 1); - - //get the results - $results = $this->query($query, $this->getBinds()); - - //if we have results - if (isset($results[0])) { - //return it - return $results[0]; - } - - return null; - } - - /** - * Returns the select query builder - * - * @param string|array $select Column list - * - * @return QuerySelect - */ - public function getSelectQuery($select = '*') - { - return $this->resolve(QuerySelect::class, $select); - } - - /** - * Returns the update query builder - * - * @param string|null $table Name of table - * - * @return QueryUpdate - */ - public function getUpdateQuery($table = null) - { - return $this->resolve(QueryUpdate::class, $table); - } - - /** - * Inserts data into a table and returns the ID - * - * @param *string $table Table name - * @param *array $setting Key/value array matching table columns - * @param bool|array $bind Whether to compute with binded variables - * - * @return AbstractSql - */ - public function insertRow($table, array $settings, $bind = true) - { - //build insert query - $query = $this->getInsertQuery($table); - - //foreach settings - foreach ($settings as $key => $value) { - //if value is not a vulnerability - if (is_null($value) || is_bool($value)) { - //just add it to the query - $query->set($key, $value); - continue; - } - - //if bind is true or is an array and we want to bind it - if ($bind === true || (is_array($bind) && in_array($key, $bind))) { - //bind the value - $value = $this->bind($value); - } - - //add it to the query - $query->set($key, $value); - } - - //run the query - $this->query($query, $this->getBinds()); - - return $this; - } - - /** - * Inserts multiple rows into a table - * - * @param *string $table Table name - * @param array $setting Key/value 2D array matching table columns - * @param bool|array $bind Whether to compute with binded variables - * - * @return AbstractSql - */ - public function insertRows($table, array $settings, $bind = true) - { - //build insert query - $query = $this->getInsertQuery($table); - - //this is an array of arrays - foreach ($settings as $index => $setting) { - //for each column - foreach ($setting as $key => $value) { - //if value is not a vulnerability - if (is_null($value) || is_bool($value)) { - //just add it to the query - $query->set($key, $value, $index); - continue; - } - - //if bind is true or is an array and we want to bind it - if ($bind === true || (is_array($bind) && in_array($key, $bind))) { - //bind the value - $value = $this->bind($value); - } - - //add it to the query - $query->set($key, $value, $index); - } - } - - //run the query - $this->query($query, $this->getBinds()); - - return $this; - } - - /** - * Adaptor used to force a connection to the handler - * - * @param PDO $connection - * - * @return AbstractSQL - */ - public static function loadPDO(PDO $connection) - { - $reflection = new ReflectionClass(static::class); - $instance = $reflection->newInstanceWithoutConstructor(); - return $instance->connect($connection); - } - - /** - * Returns model - * - * @param array $data The initial data to set - * - * @return Model - */ - public function model(array $data = []) - { - return $this->resolve(Model::class, $data)->setDatabase($this); - } - - /** - * Queries the database - * - * @param *string $query The query to ran - * @param array $binds List of binded values - * @param callable|null $fetch Whether to fetch all the rows - * - * @return array - */ - public function query($query, array $binds = [], $fetch = null) - { - $request = new StdClass(); - - $request->query = $query; - $request->binds = $binds; - - $connection = $this->getConnection(); - $query = (string) $request->query; - $stmt = $connection->prepare($query); - - //bind some more values - foreach ($request->binds as $key => $value) { - $stmt->bindValue($key, $value); - } - - //PDO Execute - if (!$stmt->execute()) { - $error = $stmt->errorInfo(); - - //unpack binds for the report - foreach ($binds as $key => $value) { - $query = str_replace($key, "'$value'", $query); - } - - //throw Exception - throw SqlException::forQueryError($query, $error[2]); - } - - //clear binds - $this->binds = []; - - if (!is_callable($fetch)) { - $results = $stmt->fetchAll(PDO::FETCH_ASSOC); - - //log query - $this->log([ - 'query' => $query, - 'binds' => $binds, - 'results' => $results - ]); - - return $results; - } - - if ($fetch instanceof Closure) { - $fetch = $fetch->bindTo($this, get_class($this)); - } - - while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { - if (call_user_func($fetch, $row, $this) === false) { - break; - } - } - - return $this; - } - - /** - * Returns search - * - * @param string|null $table Table name - * - * @return Search - */ - public function search($table = null) - { - $search = $this->resolve(Search::class, $this); - - if ($table) { - $search->setTable($table); - } - - return $search; - } - - /** - * Sets all the bound values of this query - * - * @param *array $binds key/values to bind - * - * @return AbstractSql - */ - public function setBinds(array $binds) - { - $this->binds = $binds; - return $this; - } - - /** - * Sets only 1 row given the column name and the value - * - * @param *string $table Table name - * @param *string $name Column name - * @param *scalar|null $value Column value - * @param *array $setting Key/value array matching table columns - * - * @return AbstractSql - */ - public function setRow($table, $name, $value, array $setting) - { - //first check to see if the row exists - $row = $this->getRow($table, $name, $value); - - if (!$row) { - //we need to insert - $setting[$name] = $value; - return $this->insertRow($table, $setting); - } - - //we need to update this row - return $this->updateRows($table, $setting, [$name.'=%s', $value]); - } - - /** - * Sets up a transaction call - * - * @param *callable $callback - * - * @return AbstractSql - */ - public function transaction($callback) - { - $connection = $this->getConnection(); - $connection->beginTransaction(); - - if ($callback instanceof Closure) { - $callback = $callback->bindTo($this, get_class($this)); - } - - if (call_user_func($callback, $this) === false) { - $connection->rollBack(); - } else { - $connection->commit(); - } - - return $this; - } - - /** - * Updates rows that match a filter given the update settings - * - * @param *string $table Table name - * @param *array $setting Key/value array matching table columns - * @param array|string $filters Filters to test against - * @param bool|array $bind Whether to compute with binded variables - * - * @return AbstractSql - */ - public function updateRows($table, array $settings, $filters = null, $bind = true) - { - //build the query - $query = $this->getUpdateQuery($table); - - //foreach settings - foreach ($settings as $key => $value) { - //if value is not a vulnerability - if (is_null($value) || is_bool($value)) { - //just add it to the query - $query->set($key, $value); - continue; - } - - //if bind is true or is an array and we want to bind it - if ($bind === true || (is_array($bind) && in_array($key, $bind))) { - //bind the value - $value = $this->bind($value); - } - - //add it to the query - $query->set($key, $value); - } - - //array('post_id=%s AND post_title IN %s', 123, array('asd')); - if (is_array($filters)) { - //can be array of arrays - if (is_array($filters[0])) { - foreach ($filters as $i => $filter) { - if (is_array($filters)) { - $format = array_shift($filter); - - //reindex filters - $filter = array_values($filter); - - //bind filters - foreach ($filter as $i => $value) { - $filter[$i] = $this->bind($value); - } - - //combine - $query->where(vsprintf($format, $filter)); - } - } - } else { - $format = array_shift($filters); - - //reindex filters - $filters = array_values($filters); - - //bind filters - foreach ($filters as $i => $value) { - $filters[$i] = $this->bind($value); - } - - //combine - $query->where(vsprintf($format, $filters)); - } - } else { - $query->where($filters); - } - - //run the query - $this->query($query, $this->getBinds()); - - return $this; - } -} diff --git a/src/Collection.php b/src/Collection.php deleted file mode 100644 index f2c8d70..0000000 --- a/src/Collection.php +++ /dev/null @@ -1,107 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm; - -use Cradle\Data\Model\ModelInterface; -use Cradle\Data\Collection\CollectionInterface; - -use Cradle\Data\Model as DataModel; -use Cradle\Data\Collection as DataCollection; - -/** - * Sql Collection handler - * - * @vendor Cradle - * @package Sql - * @author Christian Blanquera - * @standard PSR-2 - */ -class Collection extends DataCollection -{ - /** - * @var SqlInterface|null $database The database resource - */ - protected $database = null; - - /** - * @var string|null $table Default table name - */ - protected $table = null; - - /** - * Returns the entire data - * - * @param array $row - * - * @return Model - */ - public function getModel(array $row = []): ModelInterface - { - $model = $this->resolve(Model::class, $row); - - if (!is_null($this->database)) { - $model->setDatabase($this->database); - } - - if (!is_null($this->table)) { - $model->setTable($this->table); - } - - return $model; - } - - /** - * Sets the default database - * - * @param SqlInterface $database Database object instance - * - * @return Collection - */ - public function setDatabase(SqlInterface $database) - { - $this->database = $database; - - //for each row - foreach ($this->data as $row) { - if (!is_object($row) || !method_exists($row, __FUNCTION__)) { - continue; - } - - //let the row handle this - $row->setDatabase($database); - } - - return $this; - } - - /** - * Sets the default database - * - * @param string $table The name of the table - * - * @return Collection - */ - public function setTable($table) - { - $this->table = $table; - - //for each row - foreach ($this->data as $row) { - if (!is_object($row) || !method_exists($row, __FUNCTION__)) { - continue; - } - - //let the row handle this - $row->setTable($table); - } - - return $this; - } -} diff --git a/src/Engine/AbstractEngine.php b/src/Engine/AbstractEngine.php new file mode 100644 index 0000000..4191ab8 --- /dev/null +++ b/src/Engine/AbstractEngine.php @@ -0,0 +1,712 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Engine; + +use StdClass; +use Closure; +use PDO; +use ReflectionClass; + +use Cradle\Resolver\StateTrait; + +use Cradle\Storm\SqlException; + +use Cradle\Storm\Mapper\Model; +use Cradle\Storm\Mapper\Collection; +use Cradle\Storm\Mapper\Search; +use Cradle\Storm\Mapper\Remove; +use Cradle\Storm\Mapper\Update; + +use Cradle\Storm\Query\QueryInterface; +use Cradle\Storm\Query\Delete as QueryDelete; +use Cradle\Storm\Query\Insert as QueryInsert; +use Cradle\Storm\Query\Select as QuerySelect; +use Cradle\Storm\Query\Update as QueryUpdate; + +/** + * Abstractly defines a layout of available methods to + * connect to and query a database. This class also lays out + * query building methods that auto renders a valid query + * the specific database will understand without actually + * needing to know the query language. + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +abstract class AbstractEngine +{ + use StateTrait + { + StateTrait::__callResolver as __call; + } + + /** + * @const int INSTANCE Flag that designates multiton when using ::i() + */ + const INSTANCE = 0; + + /** + * @const string FIRST The first index in getQueries + */ + const FIRST = 'first'; + + /** + * @const string LAST The last index in getQueries + */ + const LAST = 'last'; + + /** + * @var [RESOURCE] $connection PDO resource + */ + protected $connection = null; + + /** + * @var array $binds Bound data from the current query + */ + protected $binds = []; + + /** + * Connects to the database + * + * @param PDO|array $options The connection options + * + * @return EngineInterface + */ + abstract public function connect($options = []): EngineInterface; + + /** + * Binds a value and returns the bound key + * + * @param *string|array|number|null $value What to bind + * + * @return string + */ + public function bind($value): string + { + if (is_array($value)) { + foreach ($value as $i => $item) { + $value[$i] = $this->bind($item); + } + + return sprintf('(%s)', implode(',', $value)); + } else if (is_int($value) || ctype_digit($value)) { + if (strpos($value, '0') !== 0) { + return $value; + } + } + + $name = ':bind'.count($this->binds).'bind'; + $this->binds[$name] = $value; + return $name; + } + + /** + * Returns collection + * + * @param array $data Initial collection data + * + * @return Collection + */ + public function collection(array $data = []): Collection + { + return $this + ->resolve(Collection::class) + ->setDatabase($this) + ->set($data); + } + + /** + * Removes rows that match a filter + * + * @param ?string $table The table name + * @param array|string $filters Filters to test against + * + * @return EngineInterface + */ + public function deleteRows(?string $table = null, $filters = null): EngineInterface + { + $query = $this->getDeleteQuery($table); + + //array('post_id=%s AND post_title IN %s', 123, array('asd')); + if (is_array($filters)) { + //can be array of arrays + if (is_array($filters[0])) { + foreach ($filters as $i => $filter) { + if (is_array($filters)) { + $format = array_shift($filter); + + //reindex filters + $filter = array_values($filter); + + //bind filters + foreach ($filter as $i => $value) { + $filter[$i] = $this->bind($value); + } + + //combine + $query->where(vsprintf($format, $filter)); + } + } + } else { + $format = array_shift($filters); + + //reindex filters + $filters = array_values($filters); + + //bind filters + foreach ($filters as $i => $value) { + $filters[$i] = $this->bind($value); + } + + //combine + $query->where(vsprintf($format, $filters)); + } + } else { + $query->where($filters); + } + + //run the query + $this->query($query, $this->getBinds()); + + return $this; + } + + /** + * Returns all the bound values of this query + * + * @return array + */ + public function getBinds(): array + { + return $this->binds; + } + + /** + * Returns the connection object + * if no connection has been made + * it will attempt to make it + * + * @return resource PDO connection resource + */ + public function getConnection(): PDO + { + if (!$this->connection) { + $this->connect(); + } + + return $this->connection; + } + + /** + * Returns the delete query builder + * + * @param ?string $table The table name + * + * @return QueryInterface + */ + public function getDeleteQuery(?string $table = null): QueryInterface + { + return $this->resolve(QueryDelete::class, $table); + } + + /** + * Returns the insert query builder + * + * @param ?string $table Name of table + * + * @return QueryInterface + */ + public function getInsertQuery(?string $table = null): QueryInterface + { + return $this->resolve(QueryInsert::class, $table); + } + + /** + * Returns the last inserted id + * + * @param ?string $column A particular column name + * + * @return int the id + */ + public function getLastInsertedId(?string $column = null): int + { + if (is_string($column)) { + return $this->getConnection()->lastInsertId($column); + } + + return $this->getConnection()->lastInsertId(); + } + + /** + * Returns a model given the column name and the value + * + * @param *string $table Table name + * @param *string $name Column name + * @param ?scalar $value Column value + * + * @return ?Model + */ + public function getModel(string $table, string $name, $value): ?Model + { + //scalar check + if ($value && !is_scalar($value)) { + return null; + } + + //get the row + $result = $this->getRow($table, $name, $value); + + if (is_null($result)) { + return null; + } + + return $this->model()->setTable($table)->set($result); + } + + /** + * Returns a 1 row result given the column name and the value + * + * @param *string $table Table name + * @param *string $name Column name + * @param ?scalar $value Column value + * + * @return ?array + */ + public function getRow(string $table, string $name, $value): ?array + { + //scalar check + if ($value && !is_scalar($value)) { + return null; + } + + //make the query + $query = $this + ->getSelectQuery() + ->from($table) + ->where(sprintf('%s = %s', $name, $this->bind($value))) + ->limit(0, 1); + + //get the results + $results = $this->query($query, $this->getBinds()); + + //if we have results + if (isset($results[0])) { + //return it + return $results[0]; + } + + return null; + } + + /** + * Returns the select query builder + * + * @param string|array $select Column list + * + * @return QueryInterface + */ + public function getSelectQuery($select = '*'): QueryInterface + { + return $this->resolve(QuerySelect::class, $select); + } + + /** + * Returns the update query builder + * + * @param ?string $table Name of table + * + * @return QueryInterface + */ + public function getUpdateQuery(?string $table = null): QueryInterface + { + return $this->resolve(QueryUpdate::class, $table); + } + + /** + * Inserts data into a table and returns the ID + * + * @param *string $table Table name + * @param *array $setting Key/value array matching table columns + * @param bool|array $bind Whether to compute with binded variables + * + * @return EngineInterface + */ + public function insertRow(string $table, array $settings, $bind = true): EngineInterface + { + //build insert query + $query = $this->getInsertQuery($table); + + //foreach settings + foreach ($settings as $key => $value) { + //if value is not a vulnerability + if (is_null($value) || is_bool($value)) { + //just add it to the query + $query->set($key, $value); + continue; + } + + //if bind is true or is an array and we want to bind it + if ($bind === true || (is_array($bind) && in_array($key, $bind))) { + //bind the value + $value = $this->bind($value); + } + + //add it to the query + $query->set($key, $value); + } + + //run the query + $this->query($query, $this->getBinds()); + + return $this; + } + + /** + * Inserts multiple rows into a table + * + * @param *string $table Table name + * @param array $setting Key/value 2D array matching table columns + * @param bool|array $bind Whether to compute with binded variables + * + * @return AbstractSql + */ + public function insertRows(string $table, array $settings, $bind = true): EngineInterface + { + //build insert query + $query = $this->getInsertQuery($table); + + //this is an array of arrays + foreach ($settings as $index => $setting) { + //for each column + foreach ($setting as $key => $value) { + //if value is not a vulnerability + if (is_null($value) || is_bool($value)) { + //just add it to the query + $query->set($key, $value, $index); + continue; + } + + //if bind is true or is an array and we want to bind it + if ($bind === true || (is_array($bind) && in_array($key, $bind))) { + //bind the value + $value = $this->bind($value); + } + + //add it to the query + $query->set($key, $value, $index); + } + } + + //run the query + $this->query($query, $this->getBinds()); + + return $this; + } + + /** + * Adaptor used to force a connection to the handler + * + * @param *PDO $connection + * + * @return EngineInterface + */ + public static function loadPDO(PDO $connection): EngineInterface + { + $reflection = new ReflectionClass(static::class); + $instance = $reflection->newInstanceWithoutConstructor(); + return $instance->connect($connection); + } + + /** + * Returns model + * + * @param array $data The initial data to set + * + * @return Model + */ + public function model(array $data = []): Model + { + return $this->resolve(Model::class, $data)->setDatabase($this); + } + + /** + * Queries the database + * + * @param *string|QueryInterface $query The query to ran + * @param array $binds List of binded values + * @param ?callable $fetch Whether to fetch all the rows + * + * @return array|EngineInterface + */ + public function query($query, array $binds = [], ?callable $fetch = null) + { + $request = new StdClass(); + + $request->query = $query; + $request->binds = $binds; + + $connection = $this->getConnection(); + $query = (string) $request->query; + $stmt = $connection->prepare($query); + + //bind some more values + foreach ($request->binds as $key => $value) { + $stmt->bindValue($key, $value); + } + + //PDO Execute + if (!$stmt->execute()) { + $error = $stmt->errorInfo(); + + //unpack binds for the report + foreach ($binds as $key => $value) { + $query = str_replace($key, "'$value'", $query); + } + + //throw Exception + throw SqlException::forQueryError($query, $error[2]); + } + + //clear binds + $this->binds = []; + + if (!is_callable($fetch)) { + $results = $stmt->fetchAll(PDO::FETCH_ASSOC); + + //log query + $this->log([ + 'query' => $query, + 'binds' => $binds, + 'results' => $results + ]); + + return $results; + } + + if ($fetch instanceof Closure) { + $fetch = $fetch->bindTo($this, get_class($this)); + } + + while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { + if (call_user_func($fetch, $row, $this) === false) { + break; + } + } + + return $this; + } + + /** + * Returns update + * + * @param ?string $table Table name + * + * @return Remove + */ + public function remove(?string $table = null): Remove + { + $remove = $this->resolve(Remove::class, $this); + + if ($table) { + $remove->setTable($table); + } + + return $remove; + } + + /** + * Returns search + * + * @param ?string $table Table name + * + * @return Search + */ + public function search(?string $table = null): Search + { + $search = $this->resolve(Search::class, $this); + + if ($table) { + $search->setTable($table); + } + + return $search; + } + + /** + * Sets all the bound values of this query + * + * @param *array $binds key/values to bind + * + * @return EngineInterface + */ + public function setBinds(array $binds): EngineInterface + { + $this->binds = $binds; + return $this; + } + + /** + * Sets only 1 row given the column name and the value + * + * @param *string $table Table name + * @param *string $name Column name + * @param ?scalar $value Column value + * @param *array $setting Key/value array matching table columns + * + * @return EngineInterface + */ + public function setRow( + string $table, + string $name, + $value, + array $setting + ): EngineInterface + { + //first check to see if the row exists + $row = $this->getRow($table, $name, $value); + + if (!$row) { + //we need to insert + $setting[$name] = $value; + return $this->insertRow($table, $setting); + } + + //we need to update this row + return $this->updateRows($table, $setting, [$name.'=%s', $value]); + } + + /** + * Sets up a transaction call + * + * @param *callable $callback + * + * @return EngineInterface + */ + public function transaction(callable $callback): EngineInterface + { + $connection = $this->getConnection(); + $connection->beginTransaction(); + + if ($callback instanceof Closure) { + $callback = $callback->bindTo($this, get_class($this)); + } + + if (call_user_func($callback, $this) === false) { + $connection->rollBack(); + } else { + $connection->commit(); + } + + return $this; + } + + /** + * Returns update + * + * @param ?string $table Table name + * + * @return Update + */ + public function update(?string $table = null): Update + { + $update = $this->resolve(Update::class, $this); + + if ($table) { + $update->setTable($table); + } + + return $update; + } + + /** + * Updates rows that match a filter given the update settings + * + * @param *string $table Table name + * @param *array $setting Key/value array matching table columns + * @param array|string|null $filters Filters to test against + * @param bool|array $bind Whether to compute with binded variables + * + * @return EngineInterface + */ + public function updateRows( + string $table, + array $settings, + $filters = null, + $bind = true + ): EngineInterface + { + //build the query + $query = $this->getUpdateQuery($table); + + //foreach settings + foreach ($settings as $key => $value) { + //if value is not a vulnerability + if (is_null($value) || is_bool($value)) { + //just add it to the query + $query->set($key, $value); + continue; + } + + //if bind is true or is an array and we want to bind it + if ($bind === true || (is_array($bind) && in_array($key, $bind))) { + //bind the value + $value = $this->bind($value); + } + + //add it to the query + $query->set($key, $value); + } + + //array('post_id=%s AND post_title IN %s', 123, array('asd')); + if (is_array($filters)) { + //can be array of arrays + if (is_array($filters[0])) { + foreach ($filters as $i => $filter) { + if (is_array($filters)) { + $format = array_shift($filter); + + //reindex filters + $filter = array_values($filter); + + //bind filters + foreach ($filter as $i => $value) { + $filter[$i] = $this->bind($value); + } + + //combine + $query->where(vsprintf($format, $filter)); + } + } + } else { + $format = array_shift($filters); + + //reindex filters + $filters = array_values($filters); + + //bind filters + foreach ($filters as $i => $value) { + $filters[$i] = $this->bind($value); + } + + //combine + $query->where(vsprintf($format, $filters)); + } + } else { + $query->where($filters); + } + + //run the query + $this->query($query, $this->getBinds()); + + return $this; + } +} diff --git a/src/Engine/EngineInterface.php b/src/Engine/EngineInterface.php new file mode 100644 index 0000000..91a6a9e --- /dev/null +++ b/src/Engine/EngineInterface.php @@ -0,0 +1,210 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Engine; + +use PDO; + +use Cradle\Storm\Mapper\Model; +use Cradle\Storm\Mapper\Collection; +use Cradle\Storm\Mapper\Search; +use Cradle\Storm\Mapper\Remove; +use Cradle\Storm\Mapper\Update; + +/** + * Abstractly defines a layout of available methods to + * connect to and query a database. This class also lays out + * query building methods that auto renders a valid query + * the specific database will understand without actually + * needing to know the query language. + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +interface EngineInterface +{ + + /** + * Connects to the database + * + * @param PDO|array $options The connection options + * + * @return EngineInterface + */ + public function connect($options = []): EngineInterface; + + /** + * Binds a value and returns the bound key + * + * @param *string|array|number|null $value What to bind + * + * @return string + */ + public function bind($value): string; + + /** + * Returns collection + * + * @param array $data Initial collection data + * + * @return Collection + */ + public function collection(array $data = []): Collection; + + /** + * Removes rows that match a filter + * + * @param *string $table The table name + * @param array|string $filters Filters to test against + * + * @return EngineInterface + */ + public function deleteRows(string $table, $filters = null): EngineInterface; + + /** + * Returns all the bound values of this query + * + * @return array + */ + public function getBinds(): array; + + /** + * Returns the connection object + * if no connection has been made + * it will attempt to make it + * + * @return resource PDO connection resource + */ + public function getConnection(): PDO; + + /** + * Returns the last inserted id + * + * @param ?string $column A particular column name + * + * @return int the id + */ + public function getLastInsertedId(?string $column = null): int; + + /** + * Returns a 1 row result given the column name and the value + * + * @param *string $table Table name + * @param *string $name Column name + * @param ?scalar $value Column value + * + * @return ?array + */ + public function getRow(string $table, string $name, $value): ?array; + + /** + * Inserts data into a table and returns the ID + * + * @param *string $table Table name + * @param *array $setting Key/value array matching table columns + * @param bool|array $bind Whether to compute with binded variables + * + * @return EngineInterface + */ + public function insertRow( + string $table, + array $settings, + $bind = true + ): EngineInterface; + + /** + * Inserts multiple rows into a table + * + * @param *string $table Table name + * @param array $setting Key/value 2D array matching table columns + * @param bool|array $bind Whether to compute with binded variables + * + * @return EngineInterface + */ + public function insertRows( + string $table, + array $settings, + $bind = true + ): EngineInterface; + + /** + * Returns model + * + * @param array $data The initial data to set + * + * @return Model + */ + public function model(array $data = []): Model; + + /** + * Queries the database + * + * @param *string|QueryInterface $query The query to ran + * @param array $binds List of binded values + * @param ?callable $fetch Whether to fetch all the rows + * + * @return array|EngineInterface + */ + public function query($query, array $binds = [], ?callable $fetch = null); + + /** + * Returns update + * + * @param ?string $table Table name + * + * @return Remove + */ + public function remove(?string $table = null): Remove; + + /** + * Returns search + * + * @param ?string $table Table name + * + * @return Search + */ + public function search(?string $table = null): Search; + + /** + * Sets all the bound values of this query + * + * @param *array $binds key/values to bind + * + * @return EngineInterface + */ + public function setBinds(array $binds); + + /** + * Returns update + * + * @param ?string $table Table name + * + * @return Update + */ + public function update(?string $table = null): Update; + + /** + * Updates rows that match a filter given the update settings + * + * @param *string $table Table name + * @param *array $setting Key/value array matching table columns + * @param array|string $filters Filters to test against + * @param bool|array $bind Whether to compute with binded variables + * + * @return EngineInterface + */ + public function updateRows( + string $table, + array $settings, + $filters = null, + $bind = true + ): EngineInterface; +} diff --git a/src/Engine/MySql.php b/src/Engine/MySql.php new file mode 100644 index 0000000..2dfc173 --- /dev/null +++ b/src/Engine/MySql.php @@ -0,0 +1,307 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Engine; + +use PDO; + +use Cradle\Helper\InstanceTrait; +use Cradle\Helper\LoopTrait; +use Cradle\Helper\ConditionalTrait; + +use Cradle\Profiler\InspectorTrait; +use Cradle\Profiler\LoggerTrait; + +use Cradle\Storm\Query\QueryInterface; +use Cradle\Storm\Query\Select as QuerySelect; + +use Cradle\Storm\Query\MySql\Alter; +use Cradle\Storm\Query\MySql\Create; +use Cradle\Storm\Query\MySql\Utility; + +/** + * Abstractly defines a layout of available methods to + * connect to and query a MySql database. This class also + * lays out query building methods that auto renders a + * valid query the specific database will understand without + * actually needing to know the query language. Extending + * all Sql classes, comes coupled with loosely defined + * searching, collections and models. + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +class MySql extends AbstractEngine implements EngineInterface +{ + use InstanceTrait, + LoopTrait, + ConditionalTrait, + InspectorTrait, + LoggerTrait; + + /** + * @var string $host Database host + */ + protected $host = 'localhost'; + + /** + * @var ?string $name Database name + */ + protected $name = null; + + /** + * @var ?string $user Database user name + */ + protected $user = null; + + /** + * @var ?string $pass Database password + */ + protected $pass = null; + + /** + * Construct: Store connection information + * + * @param *string $host Database host + * @param ?string $name Database name + * @param ?string $user Database user name + * @param ?string $pass Database password + * @param ?number $port Database port + */ + public function __construct( + string $host, + ?string $name = null, + ?string $user = null, + ?string $pass = null, + ?int $port = null + ) + { + $this->host = $host; + $this->name = $name; + $this->user = $user; + $this->pass = $pass; + $this->port = $port; + } + + /** + * Connects to the database + * + * @param PDO|array $options the connection options + * + * @return EngineInterface + */ + public function connect($options = []): EngineInterface + { + if ($options instanceof PDO) { + $this->connection = $options; + return $this; + } + + if (!is_array($options)) { + $options = array(); + } + + $host = $port = null; + + if (!is_null($this->host)) { + $host = 'host='.$this->host.';'; + if (!is_null($this->port)) { + $port = 'port='.$this->port.';'; + } + } + + $connection = 'mysql:'.$host.$port.'dbname='.$this->name; + + $this->connection = new PDO($connection, $this->user, $this->pass, $options); + + return $this; + } + + /** + * Returns the alter query builder + * + * @param ?string $table The table name + * + * @return QueryInterface + */ + public function getAlterQuery(?string $table = null): QueryInterface + { + return $this->resolve(Alter::class, $table); + } + + /** + * Returns the columns and attributes given the table name + * + * @param *string $table The name of the table + * @param ?array|string $filters Where filters + * + * @return array + */ + public function getColumns(string $table, $filters = null): array + { + $query = $this->getUtilityQuery(); + + if (is_array($filters)) { + foreach ($filters as $i => $filter) { + //array('post_id=%s AND post_title IN %s', 123, array('asd')); + $format = array_shift($filter); + $filter = $this->bind($filter); + $filters[$i] = vsprintf($format, $filter); + } + } + + $query->showColumns($table, $filters); + return $this->query($query, $this->getBinds()); + } + + /** + * Returns the create query builder + * + * @param ?string $table The table name + * + * @return QueryInterface + */ + public function getCreateQuery(?string $table = null): QueryInterface + { + return $this->resolve(Create::class, $table); + } + + /** + * Peturns the primary key name given the table + * + * @param *string $table Table name + * + * @return string + */ + public function getPrimaryKey(string $table): string + { + $query = $this->getUtilityQuery(); + $results = $this->getColumns($table, "`Key` = 'PRI'"); + return isset($results[0]['Field']) ? $results[0]['Field'] : null; + } + + /** + * Returns a listing of tables in the DB + * + * @param ?string $like The like pattern + * + * @return array + */ + public function getTables(?string $like = null): array + { + $query = $this->getUtilityQuery(); + $like = $like ? $this->bind($like) : null; + $results = $this->query($query->showTables($like), $this->getBinds()); + + $newResults = []; + foreach ($results as $result) { + foreach ($result as $key => $value) { + $newResults[] = $value; + break; + } + } + + return $newResults; + } + + /** + * Returns the whole enitre schema and rows + * of the current table + * + * @param *string $table Name of table + * + * @return string + */ + public function getTableSchema(string $table): string + { + $backup = []; + //get the schema + $schema = $this->getColumns($table); + if (count($schema)) { + //lets rebuild this schema + $query = $this->getCreateQuery()->setTable($table); + + foreach ($schema as $field) { + //first try to parse what we can from each field + $fieldTypeArray = explode(' ', $field['Type']); + $typeArray = explode('(', $fieldTypeArray[0]); + + $type = $typeArray[0]; + + $length = null; + if (isset($typeArray[1])) { + $length = str_replace(')', '', $typeArray[1]); + } + + $attribute = isset($fieldTypeArray[1]) ? $fieldTypeArray[1] : null; + + $null = strtolower($field['Null']) == 'no' ? false : true; + + $increment = strtolower($field['Extra']) == 'auto_increment' ? true : false; + + //lets now add a field to our schema class + $query->addField($field['Field'], [ + 'type' => $type, + 'length' => $length, + 'attribute' => $attribute, + 'null' => $null, + 'default' => $field['Default'], + 'auto_increment' => $increment + ]); + + //set keys where found + switch ($field['Key']) { + case 'PRI': + $query->addPrimaryKey($field['Field']); + break; + case 'UNI': + $query->addUniqueKey($field['Field'], [$field['Field']]); + break; + case 'MUL': + $query->addKey($field['Field'], [$field['Field']]); + break; + } + } + + //store the query but dont run it + $backup[] = $query; + } + + //get the rows + $rows = $this->query($this->getSelectQuery()->from($table)->getQuery()); + + if (count($rows)) { + //lets build an insert query + $query = $this->getInsertQuery($table); + + foreach ($rows as $index => $row) { + foreach ($row as $key => $value) { + $query->set($key, $value, $index); + } + } + + //store the query but dont run it + $backup[] = $query->getQuery(true); + } + + return implode("\n\n", $backup); + } + + /** + * Returns the alter query builder + * + * @return QueryInterface + */ + public function getUtilityQuery(): QueryInterface + { + return $this->resolve(Utility::class); + } +} diff --git a/src/Engine/PostGreSql.php b/src/Engine/PostGreSql.php new file mode 100644 index 0000000..ba1fc0d --- /dev/null +++ b/src/Engine/PostGreSql.php @@ -0,0 +1,391 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Engine; + +use PDO; + +use Cradle\Helper\InstanceTrait; +use Cradle\Helper\LoopTrait; +use Cradle\Helper\ConditionalTrait; + +use Cradle\Profiler\InspectorTrait; +use Cradle\Profiler\LoggerTrait; + +use Cradle\Storm\Query\QueryInterface; + +use Cradle\Storm\Query\PostGreSql\Delete; +use Cradle\Storm\Query\PostGreSql\Insert; +use Cradle\Storm\Query\PostGreSql\Select; +use Cradle\Storm\Query\PostGreSql\Update; +use Cradle\Storm\Query\PostGreSql\Alter; +use Cradle\Storm\Query\PostGreSql\Create; +use Cradle\Storm\Query\PostGreSql\Utility; + +/** + * Abstractly defines a layout of available methods to + * connect to and query a PostGreSql database. This class also + * lays out query building methods that auto renders a + * valid query the specific database will understand without + * actually needing to know the query language. Extending + * all Sql classes, comes coupled with loosely defined + * searching, collections and models. + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +class PostGreSql extends AbstractEngine implements EngineInterface +{ + use InstanceTrait, + LoopTrait, + ConditionalTrait, + InspectorTrait, + LoggerTrait; + + /** + * @var string $host Database host + */ + protected $host = 'localhost'; + + /** + * @var string $port Database port + */ + protected $port = '5432'; + + /** + * @var ?string $name Database name + */ + protected $name = null; + + /** + * @var ?string $user Database user name + */ + protected $user = null; + + /** + * @var ?string $pass Database password + */ + protected $pass = null; + + /** + * Construct: Store connection information + * + * @param *string $host Database host + * @param ?string $name Database name + * @param ?string $user Database user name + * @param ?string $pass Database password + * @param ?number $port Database port + */ + public function __construct( + string $host, + ?string $name = null, + ?string $user = null, + ?string $pass = null, + ?int $port = null + ) + { + $this->host = $host; + $this->name = $name; + $this->user = $user; + $this->pass = $pass; + $this->port = $port; + } + + /** + * Connects to the database + * + * @param PDO|array $options the connection options + * + * @return EngineInterface + */ + public function connect($options = []): EngineInterface + { + if ($options instanceof PDO) { + $this->connection = $options; + return $this; + } + + if (!is_array($options)) { + $options = array(); + } + + $host = $port = null; + + if (!is_null($this->host)) { + $host = 'host='.$this->host.';'; + if (!is_null($this->port)) { + $port = 'port='.$this->port.';'; + } + } + + $connection = 'pgsql:'.$host.$port.'dbname='.$this->name; + + $this->connection = new PDO($connection, $this->user, $this->pass, $options); + + return $this; + } + + /** + * Returns the alter query builder + * + * @param ?string $table The table name + * + * @return QueryInterface + */ + public function getAlterQuery(?string $table = null): QueryInterface + { + return $this->resolve(Alter::class, $table); + } + + /** + * Query for showing all columns of a table + * + * @param *string $table The name of the table + * @param ?string $schema Name of schema + * + * @return array + */ + public function getColumns(string $table, ?string $schema = null): array + { + $select = [ + 'columns.table_schema', + 'columns.column_name', + 'columns.ordinal_position', + 'columns.column_default', + 'columns.is_nullable', + 'columns.data_type', + 'columns.character_maximum_length', + 'columns.character_octet_length', + 'pg_class2.relname AS index_type' + ]; + + $where = [ + "pg_attribute.attrelid = pg_class1.oid AND pg_class1.relname='".$table."'", + 'columns.column_name = pg_attribute.attname AND columns.table_name=pg_class1.relname', + 'pg_class1.oid = pg_index.indrelid AND pg_attribute.attnum = ANY(pg_index.indkey)', + 'pg_class2.oid = pg_index.indexrelid' + ]; + + if ($schema) { + $where[1] .= ' AND columns.table_schema="'.$schema.'"'; + } + + $query = $this + ->getSelectQuery($select) + ->from('pg_attribute') + ->innerJoin('pg_class AS pg_class1', $where[0], false) + ->innerJoin('information_schema.COLUMNS AS columns', $where[1], false) + ->leftJoin('pg_index', $where[2], false) + ->leftJoin('pg_class AS pg_class2', $where[3], false) + ->getQuery(); + + $results = $this->query($query); + + $columns = []; + foreach ($results as $column) { + $key = null; + if (strpos($column['index_type'], '_pkey') !== false) { + $key = 'PRI'; + } else if (strpos($column['index_type'], '_key') !== false) { + $key = 'UNI'; + } + + $columns[] = [ + 'Field' => $column['column_name'], + 'Type' => $column['data_type'], + 'Default' => $column['column_default'], + 'Null' => $column['is_nullable'], + 'Key' => $key + ]; + } + + return $columns; + } + + /** + * Returns the create query builder + * + * @param ?string $table The table name + * + * @return QueryInterface + */ + public function getCreateQuery(?string $table = null): QueryInterface + { + return $this->resolve(Create::class, $table); + } + + /** + * Returns the delete query builder + * + * @param ?string $table The table name + * + * @return QueryInterface + */ + public function getDeleteQuery(?string $table = null): QueryInterface + { + return $this->resolve(Delete::class, $table); + } + + /** + * Query for showing all columns of a table + * + * @param *string $table the name of the table + * @param ?string $schema if from a particular schema + * + * @return array + */ + public function getIndexes(string $table, ?string $schema = null): array + { + $select = [ + 'columns.column_name', + 'pg_class2.relname AS index_type' + ]; + + $where = [ + "pg_attribute.attrelid = pg_class1.oid AND pg_class1.relname='".$table."'", + 'columns.column_name = pg_attribute.attname AND columns.table_name=pg_class1.relname', + 'pg_class1.oid = pg_index.indrelid AND pg_attribute.attnum = ANY(pg_index.indkey)', + 'pg_class2.oid = pg_index.indexrelid' + ]; + + if ($schema) { + $where[1] .= ' AND columns.table_schema="'.$schema.'"'; + } + + $query = $this + ->getSelectQuery($select) + ->from('pg_attribute') + ->innerJoin('pg_class AS pg_class1', $where[0], false) + ->innerJoin('information_schema.COLUMNS AS columns', $where[1], false) + ->innerJoin('pg_index', $where[2], false) + ->innerJoin('pg_class AS pg_class2', $where[3], false) + ->getQuery(); + + return $this->query($query); + } + + /** + * Returns the insert query builder + * + * @param ?string $table The table name + * + * @return QueryInterface + */ + public function getInsertQuery(?string $table = null): QueryInterface + { + return $this->resolve(Insert::class, $table); + } + + /** + * Query for showing all columns of a table + * + * @param *string $table the name of the table + * @param ?string $schema if from a particular schema + * + * @return array + */ + public function getPrimary(string $table, ?string $schema = null) + { + $select = ['columns.column_name']; + + $where = [ + "pg_attribute.attrelid = pg_class1.oid AND pg_class1.relname='".$table."'", + 'columns.column_name = pg_attribute.attname AND columns.table_name=pg_class1.relname', + 'pg_class1.oid = pg_index.indrelid AND pg_attribute.attnum = ANY(pg_index.indkey)', + 'pg_class2.oid = pg_index.indexrelid']; + + if ($schema) { + $where[1] .= ' AND columns.table_schema="'.$schema.'"'; + } + + $query = $this + ->getSelectQuery($select) + ->from('pg_attribute') + ->innerJoin('pg_class AS pg_class1', $where[0], false) + ->innerJoin('information_schema.COLUMNS AS columns', $where[1], false) + ->innerJoin('pg_index', $where[2], false) + ->innerJoin('pg_class AS pg_class2', $where[3], false) + ->where('pg_class2.relname LIKE \'%_pkey\'') + ->getQuery(); + + return $this->query($query); + } + + /** + * Returns the select query builder + * + * @param *string|array $select Column list + * + * @return QueryInterface + */ + public function getSelectQuery($select = '*'): QueryInterface + { + return $this->resolve(Select::class, $select); + } + + /** + * Returns a listing of tables in the DB + * + * @return array + */ + public function getTables(): array + { + $query = $this + ->getSelectQuery('tablename') + ->from('pg_tables') + ->where("tablename NOT LIKE 'pg\\_%'") + ->where("tablename NOT LIKE 'sql\\_%'") + ->getQuery(); + + return $this->query($query); + } + + /** + * Returns the update query builder + * + * @param ?string $table Name of table + * + * @return QueryInterface + */ + public function getUpdateQuery(?string $table = null): QueryInterface + { + return $this->resolve(Update::class, $table); + } + + /** + * Set schema search paths + * + * @param *string $schema Schema name + * + * @return EngineInterface + */ + public function setSchema(string $schema): EngineInterface + { + $schema = func_get_args(); + + $schema = "'".implode("','", $schema)."'"; + + $query = $this->getUtilityQuery()->setSchema($schema); + $this->query($query); + + return $this; + } + + /** + * Returns the alter query builder + * + * @return QueryInterface + */ + public function getUtilityQuery(): QueryInterface + { + return $this->resolve(Utility::class); + } +} diff --git a/src/Engine/Sqlite.php b/src/Engine/Sqlite.php new file mode 100644 index 0000000..1a22e44 --- /dev/null +++ b/src/Engine/Sqlite.php @@ -0,0 +1,221 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Engine; + +use PDO; + +use Cradle\Helper\InstanceTrait; +use Cradle\Helper\LoopTrait; +use Cradle\Helper\ConditionalTrait; + +use Cradle\Profiler\InspectorTrait; +use Cradle\Profiler\LoggerTrait; + +use Cradle\Storm\Query\QueryInterface; + +use Cradle\Storm\Query\Sqlite\Alter; +use Cradle\Storm\Query\Sqlite\Create; +use Cradle\Storm\Query\Sqlite\Utility; + +/** + * Abstractly defines a layout of available methods to + * connect to and query a Sqlite database. This class also + * lays out query building methods that auto renders a + * valid query the specific database will understand without + * actually needing to know the query language. Extending + * all Sql classes, comes coupled with loosely defined + * searching, collections and models. + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +class Sqlite extends AbstractEngine implements EngineInterface +{ + use InstanceTrait, + LoopTrait, + ConditionalTrait, + InspectorTrait, + LoggerTrait; + + /** + * @var string $path Sqlite file path + */ + protected $path = null; + + /** + * Construct: Store connection information + * + * @param *string $path Sqlite file path + */ + public function __construct($path) + { + $this->path = $path; + } + + /** + * Connects to the database + * + * @param PDO|array $options the connection options + * + * @return EngineInterface + */ + public function connect($options = []): EngineInterface + { + if ($options instanceof PDO) { + $this->connection = $options; + return $this; + } + + if (!is_array($options)) { + $options = array(); + } + + $this->connection = new PDO('sqlite:'.$this->path); + $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + return $this; + } + + /** + * Returns the alter query builder + * + * @param ?string $table The table name + * + * @return QueryInterface + */ + public function getAlterQuery(?string $table = null): QueryInterface + { + return $this->resolve(Alter::class, $table); + } + + /** + * Returns the columns and attributes given the table name + * + * @param *string $table The name of the table + * + * @return array + */ + public function getColumns(string $table): array + { + $query = $this->getUtilityQuery()->showColumns($table); + + $results = $this->query($query, $this->getBinds()); + + $columns = []; + foreach ($results as $column) { + $key = null; + if ($column['pk'] == 1) { + $key = 'PRI'; + } + + $columns[] = [ + 'Field' => $column['name'], + 'Type' => $column['type'], + 'Default' => $column['dflt_value'], + 'Null' => $column['notnull'] != 1, + 'Key' => $key + ]; + } + + return $columns; + } + + /** + * Returns the create query builder + * + * @param *string $name Name of table + * + * @return QueryInterface + */ + public function getCreateQuery(?string $table = null): QueryInterface + { + return $this->resolve(Create::class, $table); + } + + /** + * Peturns the primary key name given the table + * + * @param *string $table The table name + * + * @return string + */ + public function getPrimaryKey(string $table): string + { + $results = $this->getColumns($table, "`Key` = 'PRI'"); + return isset($results[0]['Field']) ? $results[0]['Field'] : null; + } + + /** + * Returns a listing of tables in the DB + * + * @param ?string $like The like pattern + * + * @return array + */ + public function getTables(?string $like = null): array + { + $query = $this->getUtilityQuery(); + $like = $like ? $this->bind($like) : null; + return $this->query($query->showTables($like), $this->getBinds()); + } + + /** + * Inserts multiple rows into a table + * + * @param *string $table Table name + * @param *array $setting Key/value 2D array matching table columns + * @param bool|array $bind Whether to compute with binded variables + * + * @return EngineInterface + */ + public function insertRows( + string $table, + array $settings, + $bind = true + ): EngineInterface + { + //this is an array of arrays + foreach ($settings as $index => $setting) { + //Sqlite no available multi insert + //there's work arounds, but no performance gain + $this->insertRow($table, $setting, $bind); + } + + return $this; + } + + /** + * Returns the select query builder + * + * @param string|array $select Column list + * + * @return QueryInterface + */ + public function getSelectQuery($select = '*'): QueryInterface + { + if ($select === '*') { + $select = 'ROWID,*'; + } + + return parent::getSelectQuery($select); + } + + /** + * Returns the alter query builder + * + * @return QueryInterface + */ + public function getUtilityQuery(): QueryInterface + { + return $this->resolve(Utility::class); + } +} diff --git a/src/Mapper/AbstractMapper.php b/src/Mapper/AbstractMapper.php new file mode 100644 index 0000000..ae8dc3d --- /dev/null +++ b/src/Mapper/AbstractMapper.php @@ -0,0 +1,84 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Mapper; + +use Cradle\Storm\Query\QueryInterface; +use Cradle\Storm\Engine\EngineInterface; + +/** + * AbstractFilter + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +abstract class AbstractMapper +{ + /** + * @var EngineInterface $database = null Database object + */ + protected $database = null; + + /** + * @var ?string $table Table name + */ + protected $table = null; + + /** + * Construct: Store database + * + * @param *EngineInterface $database Database object + */ + public function __construct(EngineInterface $database) + { + $this->database = $database; + } + + /** + * Builds query based on the data given + * + * @return string + */ + abstract public function getQuery(): QueryInterface; + + /** + * Queries the database + * + * @param ?callable $fetch Whether to fetch all the rows + * + * @return array|MapperInterface + */ + public function query(?callable $fetch = null) + { + $query = $this->getQuery(); + + $rows = $this->database->query($query, $this->database->getBinds(), $fetch); + + if (!$callback) { + return $rows; + } + + return $this; + } + + /** + * Sets Table Name + * + * @param *string $table Table class name + * + * @return AbstractFilter + */ + public function setTable(string $table): MapperInterface + { + $this->table = $table; + return $this; + } +} diff --git a/src/Mapper/Collection.php b/src/Mapper/Collection.php new file mode 100644 index 0000000..6278be6 --- /dev/null +++ b/src/Mapper/Collection.php @@ -0,0 +1,109 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Mapper; + +use Cradle\Data\Model\ModelInterface; +use Cradle\Data\Collection\CollectionInterface; + +use Cradle\Data\Model as DataModel; +use Cradle\Data\Collection as DataCollection; + +use Cradle\Storm\Engine\EngineInterface; + +/** + * Sql Collection handler + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +class Collection extends DataCollection +{ + /** + * @var EngineInterface $database = null The database resource + */ + protected $database = null; + + /** + * @var ?string $table Default table name + */ + protected $table = null; + + /** + * Returns the entire data + * + * @param array $row + * + * @return Model + */ + public function getModel(array $row = []): ModelInterface + { + $model = $this->resolve(Model::class, $row); + + if (!is_null($this->database)) { + $model->setDatabase($this->database); + } + + if (!is_null($this->table)) { + $model->setTable($this->table); + } + + return $model; + } + + /** + * Sets the default database + * + * @param *EngineInterface $database Database object instance + * + * @return Collection + */ + public function setDatabase(EngineInterface $database): Collection + { + $this->database = $database; + + //for each row + foreach ($this->data as $row) { + if (!is_object($row) || !method_exists($row, __FUNCTION__)) { + continue; + } + + //let the row handle this + $row->setDatabase($database); + } + + return $this; + } + + /** + * Sets the default database + * + * @param string $table The name of the table + * + * @return Collection + */ + public function setTable(string $table): Collection + { + $this->table = $table; + + //for each row + foreach ($this->data as $row) { + if (!is_object($row) || !method_exists($row, __FUNCTION__)) { + continue; + } + + //let the row handle this + $row->setTable($table); + } + + return $this; + } +} diff --git a/src/Mapper/MapperInterface.php b/src/Mapper/MapperInterface.php new file mode 100644 index 0000000..11cb522 --- /dev/null +++ b/src/Mapper/MapperInterface.php @@ -0,0 +1,48 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Mapper; + +use Cradle\Storm\Query\QueryInterface; + +/** + * AbstractFilter + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +interface MapperInterface +{ + /** + * Builds query based on the data given + * + * @return string + */ + public function getQuery(): QueryInterface; + + /** + * Queries the database + * + * @param ?callable $fetch Whether to fetch all the rows + * + * @return array|MapperInterface + */ + public function query(?callable $fetch = null); + + /** + * Sets Table Name + * + * @param *string $table Table class name + * + * @return MapperInterface + */ + public function setTable(string $table): MapperInterface; +} diff --git a/src/Mapper/Model.php b/src/Mapper/Model.php new file mode 100644 index 0000000..cf131f3 --- /dev/null +++ b/src/Mapper/Model.php @@ -0,0 +1,455 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Mapper; + +use Cradle\Data\Model as DataModel; + +use Cradle\Storm\SqlException; +use Cradle\Storm\Engine\EngineInterface; + +/** + * Sql Model + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +class Model extends DataModel +{ + /** + * @const string COLUMNS The name of the columns + */ + const COLUMNS = 'columns'; + + /** + * @const string PRIMARY Primary keyword + */ + const PRIMARY = 'primary'; + + /** + * @const string DATETIME Default datetime format + */ + const DATETIME = 'Y-m-d h:i:s'; + + /** + * @const string DATE Default date format + */ + const DATE = 'Y-m-d'; + + /** + * @const string TIME Default time format + */ + const TIME = 'h:i:s'; + + /** + * @const string TIMESTAMP Default timestamp format + */ + const TIMESTAMP = 'U'; + + /** + * @var ?string $table Table name + */ + protected $table = null; + + /** + * @var EngineInterface $database = null Sql database object + */ + protected $database = null; + + /** + * @var array $meta Stored table meta data + */ + protected static $meta = []; + + /** + * Useful method for formating a time column. + * + * @param *string $column Column name + * @param string $format datetime format + * + * @return Model + */ + public function formatTime(string $column, $format = self::DATETIME) + { + //if the column isn't set + if (!isset($this->data[$column])) { + //do nothing more + return $this; + } + + if (is_numeric($this->data[$column])) { + $this->data[$column] = (int) $this->data[$column]; + } + + //if this is column is a string + if (is_string($this->data[$column])) { + //make into time + $this->data[$column] = strtotime($this->data[$column]); + } + + //if this column is not an integer + if (!is_int($this->data[$column])) { + //do nothing more + return $this; + } + + //set it + $this->data[$column] = date($format, $this->data[$column]); + + return $this; + } + + /** + * Inserts model to database + * + * @param ?string $table Table name + * @param EngineInterface $database = null Dabase object + * + * @return Model + */ + public function insert( + ?string $table = null, + EngineInterface $database = null + ): Model + { + //if no table + if (is_null($table)) { + //if no default table either + if (!$this->table) { + //throw error + throw SqlException::forTableNotSet(); + } + + $table = $this->table; + } + + //if no database + if (is_null($database)) { + //and no default database + if (!$this->database) { + throw SqlException::forDatabaseNotSet(); + } + + $database = $this->database; + } + + //get the meta data, the valid column values and whether is primary is set + $meta = $this->getMeta($table, $database); + $data = $this->getValidColumns(array_keys($meta[self::COLUMNS])); + + //we insert it + $database->insertRow($table, $data); + + //only if we have 1 primary key + if (count($meta[self::PRIMARY]) == 1) { + //set the primary key + $this->data[$meta[self::PRIMARY][0]] = $database->getLastInsertedId(); + } + + return $this; + } + + /** + * Removes model from database + * + * @param ?string $table Table name + * @param ?EngineInterface $database Dabase object + * @param string|array|null $primary The primary column if you know it + * + * @return Model + */ + public function remove( + ?string $table = null, + EngineInterface $database = null, + $primary = null + ): Model + { + //if no table + if (is_null($table)) { + //if no default table either + if (!$this->table) { + //throw error + throw SqlException::forTableNotSet(); + } + + $table = $this->table; + } + + //if no database + if (is_null($database)) { + //and no default database + if (!$this->database) { + throw SqlException::forDatabaseNotSet(); + } + + $database = $this->database; + } + + //get the meta data and valid columns + $meta = $this->getMeta($table, $database); + $data = $this->getValidColumns(array_keys($meta[self::COLUMNS])); + + if (is_null($primary)) { + $primary = $meta[self::PRIMARY]; + } + + if (is_string($primary)) { + $primary = [$primary]; + } + + $filter = []; + //for each primary key + foreach ($primary as $column) { + //if the primary is not set + if (!isset($data[$column])) { + //we can't do a remove + //do nothing more + return $this; + } + + //add the condition to the filter + $filter[] = [$column.'=%s', $data[$column]]; + } + + //we delete it + $database->deleteRows($table, $filter); + + return $this; + } + + /** + * Inserts or updates model to database + * + * @param ?string $table Table name + * @param ?EngineInterface $database Dabase object + * @param string|array|null $primary The primary column if you know it + * + * @return Model + */ + public function save( + ?string $table = null, + EngineInterface $database = null, + $primary = null + ): Model + { + //if no table + if (is_null($table)) { + //if no default table either + if (!$this->table) { + //throw error + throw SqlException::forTableNotSet(); + } + + $table = $this->table; + } + + //if no database + if (is_null($database)) { + //and no default database + if (!$this->database) { + throw SqlException::forDatabaseNotSet(); + } + + $database = $this->database; + } + + //get the meta data, the valid column values and whether is primary is set + $meta = $this->getMeta($table, $database); + + if (is_null($primary)) { + $primary = $meta[self::PRIMARY]; + } + + if (is_string($primary)) { + $primary = [$primary]; + } + + $primarySet = $this->isPrimarySet($primary); + + //if no primary meta or primary values are not set + if (empty($primary) || !$primarySet) { + return $this->insert($table, $database); + } + + return $this->update($table, $database, $primary); + } + + /** + * Sets the default database + * + * @param *EngineInterface $database A database object + * + * @return Model + */ + public function setDatabase(EngineInterface $database): Model + { + $this->database = $database; + return $this; + } + + /** + * Sets the default database + * + * @param string $table Table name + * + * @return Model + */ + public function setTable(string $table): Model + { + $this->table = $table; + return $this; + } + + /** + * Updates model to database + * + * @param ?string $table Table name + * @param ?EngineInterface $database Dabase object + * @param string|array|null $primary The primary column if you know it + * + * @return Model + */ + public function update( + ?string $table = null, + EngineInterface $database = null, + $primary = null + ): Model + { + //if no table + if (is_null($table)) { + //if no default table either + if (!$this->table) { + //throw error + throw SqlException::forTableNotSet(); + } + + $table = $this->table; + } + + //if no database + if (is_null($database)) { + //and no default database + if (!$this->database) { + throw SqlException::forDatabaseNotSet(); + } + + $database = $this->database; + } + + //get the meta data, the valid column values and whether is primary is set + $meta = $this->getMeta($table, $database); + $data = $this->getValidColumns(array_keys($meta[self::COLUMNS])); + + //update original data + $this->original = $this->data; + + //from here it means that this table has primary + //columns and all primary values are set + + if (is_null($primary)) { + $primary = $meta[self::PRIMARY]; + } + + if (is_string($primary)) { + $primary = [$primary]; + } + + $filter = []; + //for each primary key + foreach ($primary as $column) { + //add the condition to the filter + $filter[] = [$column.'=%s', $data[$column]]; + } + + //we update it + $database->updateRows($table, $data, $filter); + + return $this; + } + + /** + * Checks to see if we have a primary value/s set + * + * @param *array $primary List of primary columns + * + * @return bool + */ + protected function isPrimarySet(array $primary): bool + { + foreach ($primary as $column) { + if (is_null($this[$column])) { + return false; + } + } + + return true; + } + + /** + * Returns the table meta data + * + * @param *string $table Table name + * @param *EngineInterface $database Database object + * + * @return array + */ + protected function getMeta(string $table, EngineInterface $database): array + { + $uid = spl_object_hash($database); + if (isset(self::$meta[$uid][$table])) { + return self::$meta[$uid][$table]; + } + + $columns = $database->getColumns($table); + + $meta = []; + foreach ($columns as $i => $column) { + $meta[self::COLUMNS][$column['Field']] = [ + 'type' => $column['Type'], + 'key' => $column['Key'], + 'default' => $column['Default'], + 'empty' => $column['Null'] == 'YES' + ]; + + if ($column['Key'] == 'PRI') { + $meta[self::PRIMARY][] = $column['Field']; + } + } + + self::$meta[$uid][$table] = $meta; + + return $meta; + } + + /** + * Returns only the valid data given + * the partiular table + * + * @param array $columns An unsorted list of possible columns + * + * @return array + */ + protected function getValidColumns(array $columns): array + { + $valid = []; + $dataColumns = array_keys($this->data); + + foreach ($columns as $column) { + if (!in_array($column, $dataColumns)) { + continue; + } + + $valid[$column] = $this->data[$column]; + } + + return $valid; + } +} diff --git a/src/Mapper/Remove.php b/src/Mapper/Remove.php new file mode 100644 index 0000000..203b240 --- /dev/null +++ b/src/Mapper/Remove.php @@ -0,0 +1,390 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Mapper; + +use Cradle\Storm\Query\QueryInterface; +use Cradle\Storm\SqlException; + +use Cradle\Event\EventTrait; + +use Cradle\Helper\InstanceTrait; +use Cradle\Helper\LoopTrait; +use Cradle\Helper\ConditionalTrait; + +use Cradle\Profiler\InspectorTrait; +use Cradle\Profiler\LoggerTrait; + +use Cradle\Resolver\StateTrait; +use Cradle\Resolver\ResolverException; + +/** + * AbstractFilter + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +class Remove extends AbstractMapper implements MapperInterface +{ + use EventTrait, + InstanceTrait, + LoopTrait, + ConditionalTrait, + InspectorTrait, + LoggerTrait, + StateTrait; + + /** + * @const string LEFT Join type + */ + const LEFT = 'LEFT'; + + /** + * @const string RIGHT Join type + */ + const RIGHT = 'RIGHT'; + + /** + * @const string INNER Join type + */ + const INNER = 'INNER'; + + /** + * @const string OUTER Join type + */ + const OUTER = 'OUTER'; + + /** + * @var array $join List of relational joins + */ + protected $joins = []; + + /** + * @var array $filter List of filters + */ + protected $filters = []; + + /** + * Magical processing of sortBy + * and filterBy Methods + * + * @param *string $name Name of method + * @param *array $args Arguments to pass + * + * @return mixed + */ + public function __call(string $name, array $args) + { + //if method starts with filterBy + if (strpos($name, 'filterBy') === 0) { + //ex. filterByUserName('Chris', '-') + //choose separator + $separator = '_'; + if (isset($args[1]) && is_scalar($args[1])) { + $separator = (string) $args[1]; + } + + //transform method to column name + $key = substr($name, 8); + $key = preg_replace("/([A-Z0-9])/", $separator."$1", $key); + $key = substr($key, strlen($separator)); + $key = strtolower($key); + + //if arg isn't set + if (!isset($args[0])) { + //default is null + $args[0] = null; + } + + //generate key + if (is_array($args[0])) { + $key = $key.' IN %s'; + } else { + $key = $key.'=%s'; + } + + //add it to the search filter + $this->addFilter($key, $args[0]); + + return $this; + } + + try { + return $this->__callResolver($name, $args); + } catch (ResolverException $e) { + throw new SqlException($e->getMessage()); + } + } + + /** + * Adds filter + * + * @param *string $where sprintf format + * @param scalar[,scalar..] $binds sprintf values + * + * @return MapperInterface + */ + public function addFilter(string $where, ...$binds): MapperInterface + { + $this->filters[] = [ + 'where' => $where, + 'binds' => $binds + ]; + + return $this; + } + + /** + * Builds query based on the data given + * + * @return string + */ + public function getQuery(): QueryInterface { + $query = $this->database->getDeleteQuery()->setTable($this->table); + + foreach ($this->joins as $join) { + $where = $join['where']; + if (!empty($join['binds'])) { + foreach ($join['binds'] as $i => $value) { + $join['binds'][$i] = $this->database->bind($value); + } + + $where = vsprintf($where, $join['binds']); + } + + $query->join($join['type'], $join['table'], $where, $join['using']); + } + + foreach ($this->filters as $i => $filter) { + //array('post_id=%s AND post_title IN %s', 123, array('asd')); + $where = $filter['where']; + if (!empty($filter['binds'])) { + foreach ($filter['binds'] as $i => $value) { + $filter['binds'][$i] = $this->database->bind($value); + } + + $where = vsprintf($where, $filter['binds']); + } + + $query->where($where); + } + + return $query; + } + + /** + * Adds Inner Join On + * + * @param *string $table Table name + * @param *string $where Table name + * @param scalar[,scalar..] $binds sprintf values + * + * @return MapperInterface + */ + public function innerJoinOn( + string $table, + string $where, + ...$binds + ): MapperInterface + { + $this->joins[] = [ + 'type' => static::INNER, + 'table' => $table, + 'where' => $where, + 'binds' => $binds, + 'using' => false + ]; + + return $this; + } + + /** + * Adds Inner Join Using + * + * @param *string $table Table name + * @param *string $where Table name + * @param scalar[,scalar..] $binds sprintf values + * + * @return MapperInterface + */ + public function innerJoinUsing( + string $table, + string $where, + ...$binds + ): MapperInterface + { + $this->joins[] = [ + 'type' => static::INNER, + 'table' => $table, + 'where' => $where, + 'binds' => $binds, + 'using' => true + ]; + + return $this; + } + + /** + * Adds Left Join On + * + * @param *string $table Table name + * @param *string $where Table name + * @param scalar[,scalar..] $binds sprintf values + * + * @return MapperInterface + */ + public function leftJoinOn( + string $table, + string $where, + ...$binds + ): MapperInterface + { + $this->joins[] = [ + 'type' => static::LEFT, + 'table' => $table, + 'where' => $where, + 'binds' => $binds, + 'using' => false + ]; + + return $this; + } + + /** + * Adds Left Join Using + * + * @param *string $table Table name + * @param *string $where Table name + * @param scalar[,scalar..] $binds sprintf values + * + * @return MapperInterface + */ + public function leftJoinUsing( + string $table, + string $where, + ...$binds + ): MapperInterface + { + $this->joins[] = [ + 'type' => static::LEFT, + 'table' => $table, + 'where' => $where, + 'binds' => $binds, + 'using' => true + ]; + + return $this; + } + + /** + * Adds Outer Join On + * + * @param *string $table Table name + * @param *string $where Table name + * @param scalar[,scalar..] $binds sprintf values + * + * @return MapperInterface + */ + public function outerJoinOn( + string $table, + string $where, + ...$binds + ): MapperInterface + { + $this->joins[] = [ + 'type' => static::OUTER, + 'table' => $table, + 'where' => $where, + 'binds' => $binds, + 'using' => false + ]; + + return $this; + } + + /** + * Adds Outer Join USing + * + * @param *string $table Table name + * @param *string $where Table name + * @param scalar[,scalar..] $binds sprintf values + * + * @return MapperInterface + */ + public function outerJoinUsing( + string $table, + string $where, + ...$binds + ): MapperInterface + { + $this->joins[] = [ + 'type' => static::OUTER, + 'table' => $table, + 'where' => $where, + 'binds' => $binds, + 'using' => true + ]; + + return $this; + } + + /** + * Adds Right Join On + * + * @param *string $table Table name + * @param *string $where Table name + * @param scalar[,scalar..] $binds sprintf values + * + * @return MapperInterface + */ + public function rightJoinOn( + string $table, + string $where, + ...$binds + ): MapperInterface + { + $this->joins[] = [ + 'type' => static::RIGHT, + 'table' => $table, + 'where' => $where, + 'binds' => $binds, + 'using' => false + ]; + + return $this; + } + + /** + * Adds Right Join Using + * + * @param *string $table Table name + * @param *string $where Table name + * @param scalar[,scalar..] $binds sprintf values + * + * @return MapperInterface + */ + public function rightJoinUsing( + string $table, + string $where, + ...$binds + ): MapperInterface + { + $this->joins[] = [ + 'type' => static::RIGHT, + 'table' => $table, + 'where' => $where, + 'binds' => $binds, + 'using' => true + ]; + + return $this; + } +} diff --git a/src/Mapper/Search.php b/src/Mapper/Search.php new file mode 100644 index 0000000..1412810 --- /dev/null +++ b/src/Mapper/Search.php @@ -0,0 +1,454 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Mapper; + +use Cradle\Storm\Engine\EngineInterface; +use Cradle\Storm\Query\QueryInterface; +use Cradle\Storm\SqlException; + +use Cradle\Resolver\ResolverException; + +/** + * Sql Search + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +class Search extends Remove +{ + /** + * @const string ASC Sort direction + */ + const ASC = 'ASC'; + + /** + * @const string DESC Sort direction + */ + const DESC = 'DESC'; + + /** + * @var EngineInterface $database = null Database object + */ + protected $database = null; + + /** + * @var array $columns List of columns + */ + protected $columns = []; + + /** + * @var array $sort List of orders and directions + */ + protected $sort = []; + + /** + * @var array $group List of "group bys" + */ + protected $group = []; + + /** + * @var array $having List of "havings" + */ + protected $having = []; + + /** + * @var array $start Pagination start + */ + protected $start = 0; + + /** + * @var array $range Pagination range + */ + protected $range = 0; + + /** + * Construct: Store database + * + * @param *EngineInterface $database Database object + */ + public function __construct(EngineInterface $database) + { + $this->database = $database; + } + + /** + * Magical processing of sortBy + * and filterBy Methods + * + * @param *string $name Name of method + * @param *array $args Arguments to pass + * + * @return mixed + */ + public function __call(string $name, array $args) + { + //if method starts with filterBy + if (strpos($name, 'filterBy') === 0) { + //ex. filterByUserName('Chris', '-') + //choose separator + $separator = '_'; + if (isset($args[1]) && is_scalar($args[1])) { + $separator = (string) $args[1]; + } + + //transform method to column name + $key = substr($name, 8); + $key = preg_replace("/([A-Z0-9])/", $separator."$1", $key); + $key = substr($key, strlen($separator)); + $key = strtolower($key); + + //if arg isn't set + if (!isset($args[0])) { + //default is null + $args[0] = null; + } + + //generate key + if (is_array($args[0])) { + $key = $key.' IN %s'; + } else { + $key = $key.'=%s'; + } + + //add it to the search filter + $this->addFilter($key, $args[0]); + + return $this; + } + + //if method starts with sortBy + if (strpos($name, 'sortBy') === 0) { + //ex. sortByUserName('Chris', '-') + //determine separator + $separator = '_'; + if (isset($args[1]) && is_scalar($args[1])) { + $separator = (string) $args[1]; + } + + //transform method to column name + $key = substr($name, 6); + $key = preg_replace("/([A-Z0-9])/", $separator."$1", $key); + $key = substr($key, strlen($separator)); + $key = strtolower($key); + + //if arg isn't set + if (!isset($args[0])) { + //default is null + $args[0] = null; + } + + //add it to the search sort + $this->addSort($key, $args[0]); + + return $this; + } + + try { + return $this->__callResolver($name, $args); + } catch (ResolverException $e) { + throw new SqlException($e->getMessage()); + } + } + + /** + * Adds sort + * + * @param *string $column Column name + * @param string $order ASC or DESC + * + * @return MapperInterface + */ + public function addSort(string $column, ?string $order = self::ASC): MapperInterface + { + if ($order != self::DESC) { + $order = self::ASC; + } + + $this->sort[$column] = $order; + + return $this; + } + + /** + * Returns the results in a collection + * + * @return Collection + */ + public function getCollection(): Collection + { + $collection = $this + ->resolve(Collection::class) + ->setDatabase($this->database); + + if ($this->table) { + $collection->setTable($this->table); + } + + return $collection->set($this->getRows()); + } + + /** + * Returns the one result in a model + * + * @param int $index Row index to return + * + * @return Model + */ + public function getModel(int $index = 0): Model + { + return $this->getCollection()->offsetGet($index); + } + + /** + * Builds query based on the data given + * + * @return QueryInterface + */ + public function getQuery(): QueryInterface + { + $query = $this->database->getSelectQuery(); + + if ($this->table) { + $query->from($this->table); + } + + foreach ($this->joins as $join) { + $where = $join['where']; + if (!empty($join['binds'])) { + foreach ($join['binds'] as $i => $value) { + $join['binds'][$i] = $this->database->bind($value); + } + + $where = vsprintf($where, $join['binds']); + } + + $query->join($join['type'], $join['table'], $where, $join['using']); + } + + foreach ($this->filters as $i => $filter) { + //array('post_id=%s AND post_title IN %s', 123, array('asd')); + $where = $filter['where']; + if (!empty($filter['binds'])) { + foreach ($filter['binds'] as $i => $value) { + $filter['binds'][$i] = $this->database->bind($value); + } + + $where = vsprintf($where, $filter['binds']); + } + + $query->where($where); + } + + return $query; + } + + /** + * Returns the one result + * + * @param ?int $index Row index to return + * @param ?string $column Specific column to return + * + * @return ?array + */ + public function getRow(?int $index = 0, ?string $column = null): ?array + { + $rows = $this->getRows(); + + if (!is_null($column) && isset($rows[$index][$column])) { + return $rows[$index][$column]; + } else if (is_null($column) && isset($rows[$index])) { + return $rows[$index]; + } + + return null; + } + + /** + * Returns the array rows + * + * @param ?callable $fetch + * + * @return array|MapperInterface + */ + public function getRows(?callable $fetch = null) + { + return $this->query($fetch); + } + + /** + * Returns the total results + * + * @return int + */ + public function getTotal(): int + { + $query = $this->getQuery()->select('COUNT(*) as total'); + + $rows = $this->database->query($query, $this->database->getBinds()); + + if (!isset($rows[0]['total'])) { + return 0; + } + + return $rows[0]['total']; + } + + /** + * Group by clause + * + * @param string $group Column name + * + * @return MapperInterface + */ + public function groupBy(string $group): MapperInterface + { + if (is_string($group)) { + $group = [$group]; + } + + $this->group = $group; + return $this; + } + + /** + * Having clause + * + * @param string $having Column name + * + * @return MapperInterface + */ + public function having(string $having): MapperInterface + { + if (is_string($having)) { + $having = [$having]; + } + + $this->having = $having; + return $this; + } + + /** + * Queries the database + * + * @param ?callable $fetch Whether to fetch all the rows + * + * @return array|MapperInterface + */ + public function query(?callable $fetch = null) + { + $query = $this->getQuery(); + + if (!empty($this->columns)) { + $query->select(implode(', ', $this->columns)); + } + + foreach ($this->sort as $key => $value) { + $query->sortBy($key, $value); + } + + if ($this->range) { + $query->limit($this->start, $this->range); + } + + if (!empty($this->group)) { + $query->groupBy($this->group); + } + + if (!empty($this->having)) { + $query->having($this->having); + } + + $rows = $this->database->query($query, $this->database->getBinds(), $fetch); + + if (!$fetch) { + return $rows; + } + + return $this; + } + + /** + * Sets Columns + * + * @param string[,string..]|array $columns List of table columns + * + * @return MapperInterface + */ + public function setColumns($columns): MapperInterface + { + if (!is_array($columns)) { + $columns = func_get_args(); + } + + $this->columns = $columns; + + return $this; + } + + /** + * Sets the pagination page + * + * @param int $page Pagination page + * + * @return MapperInterface + */ + public function setPage(int $page): MapperInterface + { + if ($page < 1) { + $page = 1; + } + + if ($this->range == 0) { + $this->setRange(25); + } + + $this->start = ($page - 1) * $this->range; + + return $this; + } + + /** + * Sets the pagination range + * + * @param int $range Pagination range + * + * @return MapperInterface + */ + public function setRange(int $range): MapperInterface + { + if ($range < 0) { + $range = 25; + } + + $this->range = $range; + + return $this; + } + + /** + * Sets the pagination start + * + * @param int $start Pagination start + * + * @return MapperInterface + */ + public function setStart(int $start): MapperInterface + { + if ($start < 0) { + $start = 0; + } + + $this->start = $start; + + return $this; + } +} diff --git a/src/Mapper/Update.php b/src/Mapper/Update.php new file mode 100644 index 0000000..2fce99b --- /dev/null +++ b/src/Mapper/Update.php @@ -0,0 +1,115 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Mapper; + +use Cradle\Storm\Query\QueryInterface; + +use Cradle\Event\EventTrait; + +use Cradle\Helper\InstanceTrait; +use Cradle\Helper\LoopTrait; +use Cradle\Helper\ConditionalTrait; + +use Cradle\Profiler\InspectorTrait; +use Cradle\Profiler\LoggerTrait; + +use Cradle\Resolver\StateTrait; +use Cradle\Resolver\ResolverException; + +/** + * Sql Search + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +class Update extends Remove +{ + use EventTrait, + InstanceTrait, + LoopTrait, + ConditionalTrait, + InspectorTrait, + LoggerTrait, + StateTrait; + + /** + * @var array $settings List of settings + */ + protected $settings = []; + + /** + * Builds query based on the data given + * + * @return string + */ + public function getQuery(): QueryInterface + { + $query = $this->database->getUpdateQuery()->setTable($this->table); + + foreach ($this->joins as $join) { + $where = $join['where']; + if (!empty($join['binds'])) { + foreach ($join['binds'] as $i => $value) { + $join['binds'][$i] = $this->database->bind($value); + } + + $where = vsprintf($where, $join['binds']); + } + + $query->join($join['type'], $join['table'], $where, $join['using']); + } + + foreach ($this->filters as $i => $filter) { + //array('post_id=%s AND post_title IN %s', 123, array('asd')); + $where = $filter['where']; + if (!empty($filter['binds'])) { + foreach ($filter['binds'] as $i => $value) { + $filter['binds'][$i] = $this->database->bind($value); + } + + $where = vsprintf($where, $filter['binds']); + } + + $query->where($where); + } + + foreach ($settings as $key => $setting) { + $value = $setting['value']; + if ($setting['bind']) { + $value = $this->database->bind($value); + } + + $query->set($key, $value); + } + + return $query; + } + + /** + * Adds Right Join Using + * + * @param *string $key column name + * @param *string $value + * @param bool $bind Whhether to bind the value + * + * @return MapperInterface + */ + public function set(string $key, string $value, bool $bind = true): MapperInterface + { + $this->settings[$key] = [ + 'value' => $value, + 'bind' => $bind + ]; + + return $this; + } +} diff --git a/src/Model.php b/src/Model.php deleted file mode 100644 index eb535b0..0000000 --- a/src/Model.php +++ /dev/null @@ -1,446 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm; - -use Cradle\Data\Model as DataModel; - -/** - * Sql Model - * - * @vendor Cradle - * @package Sql - * @author Christian Blanquera - * @standard PSR-2 - */ -class Model extends DataModel -{ - /** - * @const string COLUMNS The name of the columns - */ - const COLUMNS = 'columns'; - - /** - * @const string PRIMARY Primary keyword - */ - const PRIMARY = 'primary'; - - /** - * @const string DATETIME Default datetime format - */ - const DATETIME = 'Y-m-d h:i:s'; - - /** - * @const string DATE Default date format - */ - const DATE = 'Y-m-d'; - - /** - * @const string TIME Default time format - */ - const TIME = 'h:i:s'; - - /** - * @const string TIMESTAMP Default timestamp format - */ - const TIMESTAMP = 'U'; - - /** - * @var string|null $table Table name - */ - protected $table = null; - - /** - * @var SqlInterface|null $database Sql database object - */ - protected $database = null; - - /** - * @var array $meta Stored table meta data - */ - protected static $meta = []; - - /** - * Useful method for formating a time column. - * - * @param *string $column Column name - * @param string $format datetime format - * - * @return Model - */ - public function formatTime($column, $format = self::DATETIME) - { - //if the column isn't set - if (!isset($this->data[$column])) { - //do nothing more - return $this; - } - - if (is_numeric($this->data[$column])) { - $this->data[$column] = (int) $this->data[$column]; - } - - //if this is column is a string - if (is_string($this->data[$column])) { - //make into time - $this->data[$column] = strtotime($this->data[$column]); - } - - //if this column is not an integer - if (!is_int($this->data[$column])) { - //do nothing more - return $this; - } - - //set it - $this->data[$column] = date($format, $this->data[$column]); - - return $this; - } - - /** - * Inserts model to database - * - * @param string|null $table Table name - * @param SqlInterface $database Dabase object - * - * @return Model - */ - public function insert($table = null, SqlInterface $database = null) - { - //if no table - if (is_null($table)) { - //if no default table either - if (!$this->table) { - //throw error - throw SqlException::forTableNotSet(); - } - - $table = $this->table; - } - - //if no database - if (is_null($database)) { - //and no default database - if (!$this->database) { - throw SqlException::forDatabaseNotSet(); - } - - $database = $this->database; - } - - //get the meta data, the valid column values and whether is primary is set - $meta = $this->getMeta($table, $database); - $data = $this->getValidColumns(array_keys($meta[self::COLUMNS])); - - //we insert it - $database->insertRow($table, $data); - - //only if we have 1 primary key - if (count($meta[self::PRIMARY]) == 1) { - //set the primary key - $this->data[$meta[self::PRIMARY][0]] = $database->getLastInsertedId(); - } - - return $this; - } - - /** - * Removes model from database - * - * @param string|null $table Table name - * @param SqlInterface|null $database Dabase object - * @param string|array|null $primary The primary column if you know it - * - * @return Model - */ - public function remove( - $table = null, - SqlInterface $database = null, - $primary = null - ) { - //if no table - if (is_null($table)) { - //if no default table either - if (!$this->table) { - //throw error - throw SqlException::forTableNotSet(); - } - - $table = $this->table; - } - - //if no database - if (is_null($database)) { - //and no default database - if (!$this->database) { - throw SqlException::forDatabaseNotSet(); - } - - $database = $this->database; - } - - //get the meta data and valid columns - $meta = $this->getMeta($table, $database); - $data = $this->getValidColumns(array_keys($meta[self::COLUMNS])); - - if (is_null($primary)) { - $primary = $meta[self::PRIMARY]; - } - - if (is_string($primary)) { - $primary = [$primary]; - } - - $filter = []; - //for each primary key - foreach ($primary as $column) { - //if the primary is not set - if (!isset($data[$column])) { - //we can't do a remove - //do nothing more - return $this; - } - - //add the condition to the filter - $filter[] = [$column.'=%s', $data[$column]]; - } - - //we delete it - $database->deleteRows($table, $filter); - - return $this; - } - - /** - * Inserts or updates model to database - * - * @param string|null $table Table name - * @param SqlInterface|null $database Dabase object - * @param string|array|null $primary The primary column if you know it - * - * @return Model - */ - public function save( - $table = null, - SqlInterface $database = null, - $primary = null - ) { - //if no table - if (is_null($table)) { - //if no default table either - if (!$this->table) { - //throw error - throw SqlException::forTableNotSet(); - } - - $table = $this->table; - } - - //if no database - if (is_null($database)) { - //and no default database - if (!$this->database) { - throw SqlException::forDatabaseNotSet(); - } - - $database = $this->database; - } - - //get the meta data, the valid column values and whether is primary is set - $meta = $this->getMeta($table, $database); - - if (is_null($primary)) { - $primary = $meta[self::PRIMARY]; - } - - if (is_string($primary)) { - $primary = [$primary]; - } - - $primarySet = $this->isPrimarySet($primary); - - //if no primary meta or primary values are not set - if (empty($primary) || !$primarySet) { - return $this->insert($table, $database); - } - - return $this->update($table, $database, $primary); - } - - /** - * Sets the default database - * - * @param SqlInterface $database A database object - * - * @return Model - */ - public function setDatabase(SqlInterface $database) - { - $this->database = $database; - return $this; - } - - /** - * Sets the default database - * - * @param string $table Table name - * - * @return Model - */ - public function setTable($table) - { - $this->table = $table; - return $this; - } - - /** - * Updates model to database - * - * @param string|null $table Table name - * @param SqlInterface|null $database Dabase object - * @param string|array|null $primary The primary column if you know it - * - * @return Model - */ - public function update( - $table = null, - SqlInterface $database = null, - $primary = null - ) { - //if no table - if (is_null($table)) { - //if no default table either - if (!$this->table) { - //throw error - throw SqlException::forTableNotSet(); - } - - $table = $this->table; - } - - //if no database - if (is_null($database)) { - //and no default database - if (!$this->database) { - throw SqlException::forDatabaseNotSet(); - } - - $database = $this->database; - } - - //get the meta data, the valid column values and whether is primary is set - $meta = $this->getMeta($table, $database); - $data = $this->getValidColumns(array_keys($meta[self::COLUMNS])); - - //update original data - $this->original = $this->data; - - //from here it means that this table has primary - //columns and all primary values are set - - if (is_null($primary)) { - $primary = $meta[self::PRIMARY]; - } - - if (is_string($primary)) { - $primary = [$primary]; - } - - $filter = []; - //for each primary key - foreach ($primary as $column) { - //add the condition to the filter - $filter[] = [$column.'=%s', $data[$column]]; - } - - //we update it - $database->updateRows($table, $data, $filter); - - return $this; - } - - /** - * Checks to see if we have a primary value/s set - * - * @param array $primary List of primary columns - * - * @return bool - */ - protected function isPrimarySet(array $primary) - { - foreach ($primary as $column) { - if (is_null($this[$column])) { - return false; - } - } - - return true; - } - - /** - * Returns the table meta data - * - * @param string|null $table Table name - * @param SqlInterface|null $database Database object - * - * @return array - */ - protected function getMeta($table, SqlInterface $database) - { - $uid = spl_object_hash($database); - if (isset(self::$meta[$uid][$table])) { - return self::$meta[$uid][$table]; - } - - $columns = $database->getColumns($table); - - $meta = []; - foreach ($columns as $i => $column) { - $meta[self::COLUMNS][$column['Field']] = [ - 'type' => $column['Type'], - 'key' => $column['Key'], - 'default' => $column['Default'], - 'empty' => $column['Null'] == 'YES' - ]; - - if ($column['Key'] == 'PRI') { - $meta[self::PRIMARY][] = $column['Field']; - } - } - - self::$meta[$uid][$table] = $meta; - - return $meta; - } - - /** - * Returns only the valid data given - * the partiular table - * - * @param array $columns An unsorted list of possible columns - * - * @return array - */ - protected function getValidColumns($columns) - { - $valid = []; - $dataColumns = array_keys($this->data); - - foreach ($columns as $column) { - if (!in_array($column, $dataColumns)) { - continue; - } - - $valid[$column] = $this->data[$column]; - } - - return $valid; - } -} diff --git a/src/MySql.php b/src/MySql.php deleted file mode 100644 index 2dbfb31..0000000 --- a/src/MySql.php +++ /dev/null @@ -1,299 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm; - -use PDO; - -use Cradle\Storm\MySql\QueryAlter; -use Cradle\Storm\MySql\QueryCreate; -use Cradle\Storm\MySql\QuerySubSelect; -use Cradle\Storm\MySql\QueryUtility; - -/** - * Abstractly defines a layout of available methods to - * connect to and query a MySql database. This class also - * lays out query building methods that auto renders a - * valid query the specific database will understand without - * actually needing to know the query language. Extending - * all Sql classes, comes coupled with loosely defined - * searching, collections and models. - * - * @vendor Cradle - * @package Sql - * @author Christian Blanquera - * @standard PSR-2 - */ -class MySql extends AbstractSql implements SqlInterface -{ - /** - * @var string $host Database host - */ - protected $host = 'localhost'; - - /** - * @var string|null $name Database name - */ - protected $name = null; - - /** - * @var string|null $user Database user name - */ - protected $user = null; - - /** - * @var string|null $pass Database password - */ - protected $pass = null; - - /** - * Construct: Store connection information - * - * @param *string $host Database host - * @param *string|null $name Database name - * @param *string|null $user Database user name - * @param string|null $pass Database password - * @param number|null $port Database port - */ - public function __construct($host, $name, $user, $pass = null, $port = null) - { - $this->host = $host; - $this->name = $name; - $this->user = $user; - $this->pass = $pass; - $this->port = $port; - } - - /** - * Connects to the database - * - * @param PDO|array $options the connection options - * - * @return MySql - */ - public function connect($options = []) - { - if ($options instanceof PDO) { - $this->connection = $options; - return $this; - } - - if (!is_array($options)) { - $options = array(); - } - - $host = $port = null; - - if (!is_null($this->host)) { - $host = 'host='.$this->host.';'; - if (!is_null($this->port)) { - $port = 'port='.$this->port.';'; - } - } - - $connection = 'mysql:'.$host.$port.'dbname='.$this->name; - - $this->connection = new PDO($connection, $this->user, $this->pass, $options); - - return $this; - } - - /** - * Returns the alter query builder - * - * @param *string $name Name of table - * - * @return QueryAlter - */ - public function getAlterQuery($name = null) - { - return $this->resolve(QueryAlter::class, $name); - } - - /** - * Returns the columns and attributes given the table name - * - * @param string $table The name of the table - * @param array $filters Where filters - * - * @return array|false - */ - public function getColumns($table, $filters = null) - { - $query = $this->getUtilityQuery(); - - if (is_array($filters)) { - foreach ($filters as $i => $filter) { - //array('post_id=%s AND post_title IN %s', 123, array('asd')); - $format = array_shift($filter); - $filter = $this->bind($filter); - $filters[$i] = vsprintf($format, $filter); - } - } - - $query->showColumns($table, $filters); - return $this->query($query, $this->getBinds()); - } - - /** - * Returns the create query builder - * - * @param *string $name Name of table - * - * @return QueryCreate - */ - public function getCreateQuery($name = null) - { - return $this->resolve(QueryCreate::class, $name); - } - - /** - * Peturns the primary key name given the table - * - * @param string $table Table name - * - * @return string - */ - public function getPrimaryKey($table) - { - $query = $this->getUtilityQuery(); - $results = $this->getColumns($table, "`Key` = 'PRI'"); - return isset($results[0]['Field']) ? $results[0]['Field'] : null; - } - - /** - * Returns the Subselect query builder - * - * @param string $parentQuery The parent query - * @param string $select List of columns - * - * @return QuerySubSelect - */ - public function getSubSelectQuery($parentQuery, $select = '*') - { - return $this->resolve(QuerySubSelect::class, $parentQuery, $select); - } - - /** - * Returns a listing of tables in the DB - * - * @param string|null $like The like pattern - * - * @return array|false - */ - public function getTables($like = null) - { - $query = $this->getUtilityQuery(); - $like = $like ? $this->bind($like) : null; - $results = $this->query($query->showTables($like), $this->getBinds()); - $newResults = []; - - foreach ($results as $result) { - foreach ($result as $key => $value) { - $newResults[] = $value; - break; - } - } - - return $newResults; - } - - /** - * Returns the whole enitre schema and rows - * of the current table - * - * @param *string $table Name of table - * - * @return string - */ - public function getTableSchema($table) - { - $backup = []; - //get the schema - $schema = $this->getColumns($table); - if (count($schema)) { - //lets rebuild this schema - $query = $this->getCreateQuery()->setName($table); - - foreach ($schema as $field) { - //first try to parse what we can from each field - $fieldTypeArray = explode(' ', $field['Type']); - $typeArray = explode('(', $fieldTypeArray[0]); - - $type = $typeArray[0]; - - $length = null; - if (isset($typeArray[1])) { - $length = str_replace(')', '', $typeArray[1]); - } - - $attribute = isset($fieldTypeArray[1]) ? $fieldTypeArray[1] : null; - - $null = strtolower($field['Null']) == 'no' ? false : true; - - $increment = strtolower($field['Extra']) == 'auto_increment' ? true : false; - - //lets now add a field to our schema class - $query->addField($field['Field'], [ - 'type' => $type, - 'length' => $length, - 'attribute' => $attribute, - 'null' => $null, - 'default' => $field['Default'], - 'auto_increment' => $increment - ]); - - //set keys where found - switch ($field['Key']) { - case 'PRI': - $query->addPrimaryKey($field['Field']); - break; - case 'UNI': - $query->addUniqueKey($field['Field'], [$field['Field']]); - break; - case 'MUL': - $query->addKey($field['Field'], [$field['Field']]); - break; - } - } - - //store the query but dont run it - $backup[] = $query; - } - - //get the rows - $rows = $this->query($this->getSelectQuery()->from($table)->getQuery()); - - if (count($rows)) { - //lets build an insert query - $query = $this->getInsertQuery($table); - - foreach ($rows as $index => $row) { - foreach ($row as $key => $value) { - $query->set($key, $value, $index); - } - } - - //store the query but dont run it - $backup[] = $query->getQuery(true); - } - - return implode("\n\n", $backup); - } - - /** - * Returns the alter query builder - * - * @return QueryUtility - */ - public function getUtilityQuery() - { - return $this->resolve(QueryUtility::class); - } -} diff --git a/src/MySql/QueryAlter.php b/src/MySql/QueryAlter.php deleted file mode 100644 index b187748..0000000 --- a/src/MySql/QueryAlter.php +++ /dev/null @@ -1,344 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm\MySql; - -use Cradle\Storm\AbstractQuery; - -/** - * Generates alter query string syntax - * - * @vendor Cradle - * @package Sql - * @author Christian Blanquera - * @standard PSR-2 - */ -class QueryAlter extends AbstractQuery -{ - /** - * @var string|null $name Name of table - */ - protected $name = null; - - /** - * @var array $changeFields List of fields to change - */ - protected $changeFields = []; - - /** - * @var array $addFields List of fields to add - */ - protected $addFields = []; - - /** - * @var array $removeFields List of fields to remove - */ - protected $removeFields = []; - - /** - * @var array $addKeys List of keys to add - */ - protected $addKeys = []; - - /** - * @var array $removeKeys List of keys to remove - */ - protected $removeKeys = []; - - /** - * @var array $addUniqueKeys List of unique keys to add - */ - protected $addUniqueKeys = []; - - /** - * @var array $removeUniqueKeys List of unique keys to remove - */ - protected $removeUniqueKeys = []; - - /** - * @var array $addPrimaryKeys List of primary keys to add - */ - protected $addPrimaryKeys = []; - - /** - * @var array $removePrimaryKeys List of primary keys to remove - */ - protected $removePrimaryKeys = []; - - /** - * Construct: Set the table, if any - * - * @param string|null $name Table name - */ - public function __construct($name = null) - { - if (is_string($name)) { - $this->setName($name); - } - } - - /** - * Adds a field in the table - * - * @param *string $name Column name - * @param *array $attributes Column attributes - * - * @return QueryAlter - */ - public function addField($name, array $attributes) - { - $this->addFields[$name] = $attributes; - return $this; - } - - /** - * Adds an index key - * - * @param *string $name Name of key - * - * @return QueryAlter - */ - public function addKey($name) - { - $this->addKeys[] = '`'.$name.'`'; - return $this; - } - - /** - * Adds a primary key - * - * @param *string $name Name of key - * - * @return QueryAlter - */ - public function addPrimaryKey($name) - { - $this->addPrimaryKeys[] = '`'.$name.'`'; - return $this; - } - - /** - * Adds a unique key - * - * @param *string $name Name of key - * - * @return QueryAlter - */ - public function addUniqueKey($name) - { - $this->addUniqueKeys[] = '`'.$name.'`'; - return $this; - } - - /** - * Changes attributes of the table given - * the field name - * - * @param *string $name Column name - * @param *array $attributes Column attributes - * - * @return QueryAlter - */ - public function changeField($name, array $attributes) - { - $this->changeFields[$name] = $attributes; - return $this; - } - - /** - * Returns the string version of the query - * - * @param bool $unbind Whether to unbind variables - * - * @return string - */ - public function getQuery($unbind = false) - { - $fields = []; - $table = '`'.$this->name.'`'; - - foreach ($this->removeFields as $name) { - $fields[] = 'DROP `'.$name.'`'; - } - - foreach ($this->addFields as $name => $attr) { - $field = ['ADD `'.$name.'`']; - if (isset($attr['type'])) { - $field[] = isset($attr['length']) ? - $attr['type'] . '('.$attr['length'].')' : - $attr['type']; - } - - if (isset($attr['attribute'])) { - $field[] = $attr['attribute']; - } - - if (isset($attr['null'])) { - if ($attr['null'] == false) { - $field[] = 'NOT NULL'; - } else { - $field[] = 'DEFAULT NULL'; - } - } - - if (isset($attr['default'])&& $attr['default'] !== false) { - if (!isset($attr['null']) || $attr['null'] == false) { - if (is_string($attr['default'])) { - $field[] = 'DEFAULT \''.$attr['default'] . '\''; - } else if (is_numeric($attr['default'])) { - $field[] = 'DEFAULT '.$attr['default']; - } - } - } - - if (isset($attr['auto_increment']) && $attr['auto_increment'] == true) { - $field[] = 'auto_increment'; - } - - $fields[] = implode(' ', $field); - } - - foreach ($this->changeFields as $name => $attr) { - $field = ['CHANGE `'.$name.'` `'.$name.'`']; - - if (isset($attr['name'])) { - $field = ['CHANGE `'.$name.'` `'.$attr['name'].'`']; - } - - if (isset($attr['type'])) { - $field[] = isset($attr['length']) ? $attr['type'] . '('.$attr['length'].')' : $attr['type']; - } - - if (isset($attr['attribute'])) { - $field[] = $attr['attribute']; - } - - if (isset($attr['null'])) { - if ($attr['null'] == false) { - $field[] = 'NOT NULL'; - } else { - $field[] = 'DEFAULT NULL'; - } - } - - if (isset($attr['default'])&& $attr['default'] !== false) { - if (!isset($attr['null']) || $attr['null'] == false) { - if (is_string($attr['default'])) { - $field[] = 'DEFAULT \''.$attr['default'] . '\''; - } else if (is_numeric($attr['default'])) { - $field[] = 'DEFAULT '.$attr['default']; - } - } - } - - if (isset($attr['auto_increment']) && $attr['auto_increment'] == true) { - $field[] = 'auto_increment'; - } - - $fields[] = implode(' ', $field); - } - - foreach ($this->removeKeys as $key) { - $fields[] = 'DROP INDEX `'.$key.'`'; - } - - if (!empty($this->addKeys)) { - $fields[] = 'ADD INDEX ('.implode(', ', $this->addKeys).')'; - } - - foreach ($this->removeUniqueKeys as $key) { - $fields[] = 'DROP INDEX `'.$key.'`'; - } - - if (!empty($this->addUniqueKeys)) { - $fields[] = 'ADD UNIQUE ('.implode(', ', $this->addUniqueKeys).')'; - } - - foreach ($this->removePrimaryKeys as $key) { - $fields[] = 'DROP PRIMARY KEY `'.$key.'`'; - } - - if (!empty($this->addPrimaryKeys)) { - $fields[] = 'ADD PRIMARY KEY ('.implode(', ', $this->addPrimaryKeys).')'; - } - - $fields = implode(", \n", $fields); - - return sprintf( - 'ALTER TABLE %s %s;', - $table, - $fields - ); - } - - /** - * Removes a field - * - * @param *string $name Column name - * - * @return QueryAlter - */ - public function removeField($name) - { - $this->removeFields[] = $name; - return $this; - } - - /** - * Removes an index key - * - * @param *string $name Name of key - * - * @return QueryAlter - */ - public function removeKey($name) - { - $this->removeKeys[] = $name; - return $this; - } - - /** - * Removes a primary key - * - * @param *string $name Name of key - * - * @return QueryAlter - */ - public function removePrimaryKey($name) - { - $this->removePrimaryKeys[] = $name; - return $this; - } - - /** - * Removes a unique key - * - * @param *string $name Name of key - * - * @return QueryAlter - */ - public function removeUniqueKey($name) - { - $this->removeUniqueKeys[] = $name; - return $this; - } - - /** - * Sets the name of the table you wish to create - * - * @param *string $name Table name - * - * @return QueryAlter - */ - public function setName($name) - { - $this->name = $name; - return $this; - } -} diff --git a/src/MySql/QueryCreate.php b/src/MySql/QueryCreate.php deleted file mode 100644 index 5d845ab..0000000 --- a/src/MySql/QueryCreate.php +++ /dev/null @@ -1,277 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm\MySql; - -use Cradle\Storm\AbstractQuery; - -/** - * Generates create table query string syntax - * - * @vendor Cradle - * @package Sql - * @author Christian Blanquera - * @standard PSR-2 - */ -class QueryCreate extends AbstractQuery -{ - /** - * @var string|null $name Name of table - */ - protected $name = null; - - /** - * @var string|null $comments Table comments - */ - protected $comments = null; - - /** - * @var array $fields List of fields - */ - protected $fields = []; - - /** - * @var array $keys List of key indexes - */ - protected $keys = []; - - /** - * @var array $uniqueKeys List of unique keys - */ - protected $uniqueKeys = []; - - /** - * @var array $primaryKeys List of primary keys - */ - protected $primaryKeys = []; - - /** - * Construct: Set the table, if any - * - * @param string|null $name Name of table - */ - public function __construct($name = null) - { - if (is_string($name)) { - $this->setName($name); - } - } - - /** - * Adds a field in the table - * - * @param *string $name Column name - * @param *array $attributes Column attributes - * - * @return Create - */ - public function addField($name, array $attributes) - { - $this->fields[$name] = $attributes; - return $this; - } - - /** - * Adds an index key - * - * @param *string $name Name of key - * @param *array $fields List of key fields - * - * @return Create - */ - public function addKey($name, array $fields) - { - $this->keys[$name] = $fields; - return $this; - } - - /** - * Adds a primary key - * - * @param *string $name Name of key - * - * @return Create - */ - public function addPrimaryKey($name) - { - $this->primaryKeys[] = $name; - return $this; - } - - /** - * Adds a unique key - * - * @param *string $name Name of key - * @param *array $fields List of key fields - * - * @return Create - */ - public function addUniqueKey($name, array $fields) - { - $this->uniqueKeys[$name] = $fields; - return $this; - } - - /** - * Returns the string version of the query - * - * @param bool $unbind Whether to unbind variables - * - * @return string - */ - public function getQuery($unbind = false) - { - $table = '`'.$this->name.'`'; - - $fields = []; - foreach ($this->fields as $name => $attr) { - $field = ['`'.$name.'`']; - if (isset($attr['type'])) { - $field[] = isset($attr['length']) && $attr['length'] ? - $attr['type'] . '('.$attr['length'].')' : - $attr['type']; - } - - if (isset($attr['attribute'])) { - $field[] = $attr['attribute']; - } - - if (isset($attr['null'])) { - if ($attr['null'] == false) { - $field[] = 'NOT NULL'; - } else { - $field[] = 'DEFAULT NULL'; - } - } - - if (isset($attr['default'])&& $attr['default'] !== false) { - if (!isset($attr['null']) || $attr['null'] == false) { - if (is_string($attr['default'])) { - $field[] = 'DEFAULT \''.$attr['default'] . '\''; - } else if (is_numeric($attr['default'])) { - $field[] = 'DEFAULT '.$attr['default']; - } - } - } - - if (isset($attr['auto_increment']) && $attr['auto_increment'] == true) { - $field[] = 'auto_increment'; - } - - $fields[] = implode(' ', $field); - } - - $fields = !empty($fields) ? implode(', ', $fields) : ''; - - $primary = !empty($this->primaryKeys) ? - ', PRIMARY KEY (`'.implode('`, `', $this->primaryKeys).'`)' : - ''; - - $uniques = []; - foreach ($this->uniqueKeys as $key => $value) { - $uniques[] = 'UNIQUE KEY `'. $key .'` (`'.implode('`, `', $value).'`)'; - } - - $uniques = !empty($uniques) ? ', ' . implode(", \n", $uniques) : ''; - - $keys = []; - foreach ($this->keys as $key => $value) { - $keys[] = 'KEY `'. $key .'` (`'.implode('`, `', $value).'`)'; - } - - $keys = !empty($keys) ? ', ' . implode(", \n", $keys) : ''; - - return sprintf( - 'CREATE TABLE %s (%s%s%s%s);', - $table, - $fields, - $primary, - $uniques, - $keys - ); - } - - /** - * Sets comments - * - * @param *string $comments Table comments - * - * @return Create - */ - public function setComments($comments) - { - $this->comments = $comments; - return $this; - } - - /** - * Sets a list of fields to the table - * - * @param *array $fields List of fields - * - * @return Create - */ - public function setFields(array $fields) - { - $this->fields = $fields; - return $this; - } - - /** - * Sets a list of keys to the table - * - * @param *array $keys List of keys - * - * @return Create - */ - public function setKeys(array $keys) - { - $this->keys = $keys; - return $this; - } - - /** - * Sets the name of the table you wish to create - * - * @param *string $name Table name - * - * @return Create - */ - public function setName($name) - { - $this->name = $name; - return $this; - } - - /** - * Sets a list of primary keys to the table - * - * @param *array $primaryKeys List of primary keys - * - * @return Create - */ - public function setPrimaryKeys(array $primaryKeys) - { - $this->primaryKeys = $primaryKeys; - return $this; - } - - /** - * Sets a list of unique keys to the table - * - * @param *array $uniqueKeys List of unique keys - * - * @return Create - */ - public function setUniqueKeys(array $uniqueKeys) - { - $this->uniqueKeys = $uniqueKeys; - return $this; - } -} diff --git a/src/MySql/QuerySubSelect.php b/src/MySql/QuerySubSelect.php deleted file mode 100644 index 4cc055a..0000000 --- a/src/MySql/QuerySubSelect.php +++ /dev/null @@ -1,63 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm\MySql; - -use Cradle\Storm\QuerySelect; - -/** - * Generates subselect query string syntax - * - * @vendor Cradle - * @package Sql - * @author Christian Blanquera - * @standard PSR-2 - */ -class QuerySubSelect -{ - /** - * @var SqlQuerySelect $parentQuery - */ - protected $parentQuery; - - /** - * Construct: Set Parent Query and Column - * - * @param QuerySelect|null $parentQuery Main select query - * @param string $select List of columns - */ - public function __construct(QuerySelect $parentQuery = null, $select = '*') - { - $this->setParentQuery($parentQuery); - $this->select = is_array($select) ? implode(', ', $select) : $select; - } - - /** - * Returns the string version of the query - * - * @return string - */ - public function getQuery() - { - return '('.substr($this->parentQuery->getQuery(), 0, -1).')'; - } - - /** - * Sets the parent Query - * - * @param $parentQuery Main select query - * - * @return QuerySubSelect - */ - public function setParentQuery(QuerySelect $parentQuery) - { - $this->parentQuery = $parentQuery; - return $this; - } -} diff --git a/src/MySql/QueryUtility.php b/src/MySql/QueryUtility.php deleted file mode 100644 index 8926249..0000000 --- a/src/MySql/QueryUtility.php +++ /dev/null @@ -1,107 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm\MySql; - -use Cradle\Storm\AbstractQuery; - -/** - * Generates utility query strings - * - * @vendor Cradle - * @package Sql - * @author Christian Blanquera - * @standard PSR-2 - */ -class QueryUtility extends AbstractQuery -{ - /** - * @var string|null $query The query string - */ - protected $query = null; - - /** - * Query for dropping a table - * - * @param *string $table The name of the table - * - * @return QueryUtility - */ - public function dropTable($table) - { - $this->query = 'DROP TABLE `' . $table .'`'; - return $this; - } - - /** - * Returns the string version of the query - * - * @return string - */ - public function getQuery() - { - return $this->query.';'; - } - - /** - * Query for renaming a table - * - * @param *string $table The name of the table - * @param *string $name The new name of the table - * - * @return QueryUtility - */ - public function renameTable($table, $name) - { - $this->query = 'RENAME TABLE `' . $table . '` TO `' . $name . '`'; - return $this; - } - - /** - * Query for showing all columns of a table - * - * @param *string $table The name of the table - * @param *string|null $where Filter/s - * - * @return QueryUtility - */ - public function showColumns($table, $where = null) - { - $where = $where ? ' WHERE '.$where : null; - $this->query = 'SHOW FULL COLUMNS FROM `' . $table .'`' . $where; - return $this; - } - - /** - * Query for showing all tables - * - * @param string|null $like The like pattern - * - * @return QueryUtility - */ - public function showTables($like = null) - { - $like = $like ? ' LIKE '.$like : null; - $this->query = 'SHOW TABLES'.$like; - return $this; - } - - /** - * Query for truncating a table - * - * @param *string $table The name of the table - * - * @return QueryUtility - */ - public function truncate($table) - { - $this->query = 'TRUNCATE `' . $table .'`'; - return $this; - } -} diff --git a/src/PostGreSql.php b/src/PostGreSql.php deleted file mode 100644 index 6967182..0000000 --- a/src/PostGreSql.php +++ /dev/null @@ -1,371 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm; - -use PDO; - -use Cradle\Storm\PostGreSql\QueryDelete as PostGreSqlQueryDelete; -use Cradle\Storm\PostGreSql\QueryInsert as PostGreSqlQueryInsert; -use Cradle\Storm\PostGreSql\QuerySelect as PostGreSqlQuerySelect; -use Cradle\Storm\PostGreSql\QueryUpdate as PostGreSqlQueryUpdate; -use Cradle\Storm\PostGreSql\QueryAlter; -use Cradle\Storm\PostGreSql\QueryCreate; -use Cradle\Storm\PostGreSql\QuerySubSelect; -use Cradle\Storm\PostGreSql\QueryUtility; - -/** - * Abstractly defines a layout of available methods to - * connect to and query a PostGreSql database. This class also - * lays out query building methods that auto renders a - * valid query the specific database will understand without - * actually needing to know the query language. Extending - * all Sql classes, comes coupled with loosely defined - * searching, collections and models. - * - * @vendor Cradle - * @package PostGreSql - * @author Christian Blanquera - * @standard PSR-2 - */ -class PostGreSql extends AbstractSql implements SqlInterface -{ - /** - * @var string $host Database host - */ - protected $host = 'localhost'; - - /** - * @var string $port Database port - */ - protected $port = '5432'; - - /** - * @var string|null $name Database name - */ - protected $name = null; - - /** - * @var string|null $user Database user name - */ - protected $user = null; - - /** - * @var string|null $pass Database password - */ - protected $pass = null; - - /** - * Construct: Store connection information - * - * @param *string $host Database host - * @param *string|null $name Database name - * @param *string|null $user Database user name - * @param string|null $pass Database password - * @param number|null $port Database port - */ - public function __construct($host, $name, $user, $pass = null, $port = null) - { - $this->host = $host; - $this->name = $name; - $this->user = $user; - $this->pass = $pass; - $this->port = $port; - } - - /** - * Connects to the database - * - * @param PDO|array $options the connection options - * - * @return PostGreSql - */ - public function connect($options = []) - { - if ($options instanceof PDO) { - $this->connection = $options; - return $this; - } - - if (!is_array($options)) { - $options = array(); - } - - $host = $port = null; - - if (!is_null($this->host)) { - $host = 'host='.$this->host.';'; - if (!is_null($this->port)) { - $port = 'port='.$this->port.';'; - } - } - - $connection = 'pgsql:'.$host.$port.'dbname='.$this->name; - - $this->connection = new PDO($connection, $this->user, $this->pass, $options); - - return $this; - } - - /** - * Returns the alter query builder - * - * @param *string $name Name of table - * - * @return QueryAlter - */ - public function getAlterQuery($name = null) - { - return $this->resolve(QueryAlter::class, $name); - } - - /** - * Query for showing all columns of a table - * - * @param *string $table The name of the table - * @param array $filters Where filters - * - * @return array - */ - public function getColumns($table, $schema = null) - { - $select = [ - 'columns.table_schema', - 'columns.column_name', - 'columns.ordinal_position', - 'columns.column_default', - 'columns.is_nullable', - 'columns.data_type', - 'columns.character_maximum_length', - 'columns.character_octet_length', - 'pg_class2.relname AS index_type' - ]; - - $where = [ - "pg_attribute.attrelid = pg_class1.oid AND pg_class1.relname='".$table."'", - 'columns.column_name = pg_attribute.attname AND columns.table_name=pg_class1.relname', - 'pg_class1.oid = pg_index.indrelid AND pg_attribute.attnum = ANY(pg_index.indkey)', - 'pg_class2.oid = pg_index.indexrelid' - ]; - - if ($schema) { - $where[1] .= ' AND columns.table_schema="'.$schema.'"'; - } - - $query = $this - ->getSelectQuery($select) - ->from('pg_attribute') - ->innerJoin('pg_class AS pg_class1', $where[0], false) - ->innerJoin('information_schema.COLUMNS AS columns', $where[1], false) - ->leftJoin('pg_index', $where[2], false) - ->leftJoin('pg_class AS pg_class2', $where[3], false) - ->getQuery(); - - $results = $this->query($query); - - $columns = []; - foreach ($results as $column) { - $key = null; - if (strpos($column['index_type'], '_pkey') !== false) { - $key = 'PRI'; - } else if (strpos($column['index_type'], '_key') !== false) { - $key = 'UNI'; - } - - $columns[] = [ - 'Field' => $column['column_name'], - 'Type' => $column['data_type'], - 'Default' => $column['column_default'], - 'Null' => $column['is_nullable'], - 'Key' => $key - ]; - } - - return $columns; - } - - /** - * Returns the create query builder - * - * @param *string $name Name of table - * - * @return QueryCreate - */ - public function getCreateQuery($name = null) - { - return $this->resolve(QueryCreate::class, $name); - } - - /** - * Returns the delete query builder - * - * @param *string|null $table The table name - * - * @return QueryDelete - */ - public function getDeleteQuery($table = null) - { - return $this->resolve(PostGreSqlQueryDelete::class, $table); - } - - /** - * Query for showing all columns of a table - * - * @param *string $table the name of the table - * @param string|null $schema if from a particular schema - * - * @return array - */ - public function getIndexes($table, $schema = null) - { - $select = [ - 'columns.column_name', - 'pg_class2.relname AS index_type' - ]; - - $where = [ - "pg_attribute.attrelid = pg_class1.oid AND pg_class1.relname='".$table."'", - 'columns.column_name = pg_attribute.attname AND columns.table_name=pg_class1.relname', - 'pg_class1.oid = pg_index.indrelid AND pg_attribute.attnum = ANY(pg_index.indkey)', - 'pg_class2.oid = pg_index.indexrelid' - ]; - - if ($schema) { - $where[1] .= ' AND columns.table_schema="'.$schema.'"'; - } - - $query = $this - ->getSelectQuery($select) - ->from('pg_attribute') - ->innerJoin('pg_class AS pg_class1', $where[0], false) - ->innerJoin('information_schema.COLUMNS AS columns', $where[1], false) - ->innerJoin('pg_index', $where[2], false) - ->innerJoin('pg_class AS pg_class2', $where[3], false) - ->getQuery(); - - return $this->query($query); - } - - /** - * Returns the insert query builder - * - * @param string|null $table Name of table - * - * @return QueryInsert - */ - public function getInsertQuery($table = null) - { - return $this->resolve(PostGreSqlQueryInsert::class, $table); - } - - /** - * Query for showing all columns of a table - * - * @param *string $table the name of the table - * @param string|null $schema if from a particular schema - * - * @return array - */ - public function getPrimary($table, $schema = null) - { - $select = ['columns.column_name']; - - $where = [ - "pg_attribute.attrelid = pg_class1.oid AND pg_class1.relname='".$table."'", - 'columns.column_name = pg_attribute.attname AND columns.table_name=pg_class1.relname', - 'pg_class1.oid = pg_index.indrelid AND pg_attribute.attnum = ANY(pg_index.indkey)', - 'pg_class2.oid = pg_index.indexrelid']; - - if ($schema) { - $where[1] .= ' AND columns.table_schema="'.$schema.'"'; - } - - $query = $this - ->getSelectQuery($select) - ->from('pg_attribute') - ->innerJoin('pg_class AS pg_class1', $where[0], false) - ->innerJoin('information_schema.COLUMNS AS columns', $where[1], false) - ->innerJoin('pg_index', $where[2], false) - ->innerJoin('pg_class AS pg_class2', $where[3], false) - ->where('pg_class2.relname LIKE \'%_pkey\'') - ->getQuery(); - - return $this->query($query); - } - - /** - * Returns the select query builder - * - * @param string|array $select Column list - * - * @return QuerySelect - */ - public function getSelectQuery($select = '*') - { - return $this->resolve(PostGreSqlQuerySelect::class, $select); - } - - /** - * Returns a listing of tables in the DB - * - * @return array|false - */ - public function getTables() - { - $query = $this - ->getSelectQuery('tablename') - ->from('pg_tables') - ->where("tablename NOT LIKE 'pg\\_%'") - ->where("tablename NOT LIKE 'sql\\_%'") - ->getQuery(); - - return $this->query($query); - } - - /** - * Returns the update query builder - * - * @param string|null $table Name of table - * - * @return QueryUpdate - */ - public function getUpdateQuery($table = null) - { - return $this->resolve(PostGreSqlQueryUpdate::class, $table); - } - - /** - * Set schema search paths - * - * @param string $schema Schema name - * - * @return Index - */ - public function setSchema($schema) - { - $schema = func_get_args(); - - $schema = "'".implode("','", $schema)."'"; - - $query = $this->getUtilityQuery()->setSchema($schema); - $this->query($query); - - return $this; - } - - /** - * Returns the alter query builder - * - * @return QueryUtility - */ - public function getUtilityQuery() - { - return $this->resolve(QueryUtility::class); - } -} diff --git a/src/PostGreSql/QueryAlter.php b/src/PostGreSql/QueryAlter.php deleted file mode 100644 index 2f541cc..0000000 --- a/src/PostGreSql/QueryAlter.php +++ /dev/null @@ -1,262 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm\PostGreSql; - -use Cradle\Storm\AbstractQuery; - -/** - * Generates alter query string syntax - * - * @vendor Cradle - * @package Sql - * @author Christian Blanquera - * @standard PSR-2 - */ -class QueryAlter extends AbstractQuery -{ - /** - * @var string|null $name Name of table - */ - protected $name = null; - - /** - * @var array $changeFields List of fields to change - */ - protected $changeFields = []; - - /** - * @var array $addFields List of fields to add - */ - protected $addFields = []; - - /** - * @var array $removeFields List of fields to remove - */ - protected $removeFields = []; - - /** - * @var array $addPrimaryKeys List of primary keys to add - */ - protected $addPrimaryKeys = []; - - /** - * @var array $removePrimaryKeys List of primary keys to remove - */ - protected $removePrimaryKeys = []; - - /** - * Construct: set table name, if given - * - * @param string|null $name Table name - */ - public function __construct($name = null) - { - if (is_string($name)) { - $this->setName($name); - } - } - - /** - * Adds a field in the table - * - * @param *string $name Column name - * @param *array $attributes Column attributes - * - * @return QueryAlter - */ - public function addField($name, array $attributes) - { - $this->addFields[$name] = $attributes; - return $this; - } - - /** - * Adds a primary key - * - * @param *string $name Name of key - * - * @return QueryAlter - */ - public function addPrimaryKey($name) - { - $this->addPrimaryKeys[] = '"'.$name.'"'; - return $this; - } - - /** - * Changes attributes of the table given - * the field name - * - * @param *string $name Column name - * @param *array $attributes Column attributes - * - * @return QueryAlter - */ - public function changeField($name, array $attributes) - { - $this->changeFields[$name] = $attributes; - return $this; - } - - /** - * Returns the string version of the query - * - * @param bool $unbind Whether to unbind variables - * - * @return string - */ - public function getQuery($unbind = false) - { - $fields = []; - $table = '"'.$this->name.'"'; - - foreach ($this->removeFields as $name) { - $fields[] = 'DROP COLUMN "'.$name.'"'; - } - - foreach ($this->addFields as $name => $attr) { - $field = ['ADD "'.$name.'"']; - if (isset($attr['type'])) { - $field[] = isset($attr['length']) ? $attr['type'] - . '('.$attr['length'].')' : - $attr['type']; - - if (isset($attr['list']) && $attr['list']) { - $field[count($field)-1].='[]'; - } - } - - if (isset($attr['attribute'])) { - $field[] = $attr['attribute']; - } - - if (isset($attr['unique']) && $attr['unique']) { - $field[] = 'UNIQUE'; - } - - if (isset($attr['null'])) { - if ($attr['null'] == false) { - $field[] = 'NOT NULL'; - } else { - $field[] = 'DEFAULT NULL'; - } - } - - if (isset($attr['default'])&& $attr['default'] !== false) { - if (!isset($attr['null']) || $attr['null'] == false) { - if (is_string($attr['default'])) { - $field[] = 'DEFAULT \''.$attr['default'] . '\''; - } else if (is_numeric($attr['default'])) { - $field[] = 'DEFAULT '.$attr['default']; - } - } - } - - $fields[] = implode(' ', $field); - } - - foreach ($this->changeFields as $name => $attr) { - $field = ['ALTER COLUMN "'.$name.'"']; - - if (isset($attr['name'])) { - $field = ['CHANGE "'.$name.'" "'.$attr['name'].'"']; - } - - if (isset($attr['type'])) { - $field[] = isset($attr['length']) ? - $attr['type'] . '('.$attr['length'].')' : - $attr['type']; - - if (isset($attr['list']) && $attr['list']) { - $field[count($field)-1].='[]'; - } - } - - if (isset($attr['attribute'])) { - $field[] = $attr['attribute']; - } - - if (isset($attr['unique']) && $attr['unique']) { - $field[] = 'UNIQUE'; - } - - if (isset($attr['null'])) { - if ($attr['null'] == false) { - $field[] = 'NOT NULL'; - } else { - $field[] = 'DEFAULT NULL'; - } - } - - if (isset($attr['default'])&& $attr['default'] !== false) { - if (!isset($attr['null']) || $attr['null'] == false) { - if (is_string($attr['default'])) { - $field[] = 'DEFAULT \''.$attr['default'] . '\''; - } else if (is_numeric($attr['default'])) { - $field[] = 'DEFAULT '.$attr['default']; - } - } - } - - $fields[] = implode(' ', $field); - } - - foreach ($this->removePrimaryKeys as $key) { - $fields[] = 'DROP PRIMARY KEY "'.$key.'"'; - } - - if (!empty($this->addPrimaryKeys)) { - $fields[] = 'ADD PRIMARY KEY ('.implode(', ', $this->addPrimaryKeys).')'; - } - - $fields = implode(", \n", $fields); - - return sprintf('ALTER TABLE %s %s;', $table, $fields); - } - - /** - * Removes a field - * - * @param *string $name Name of field - * - * @return QueryAlter - */ - public function removeField($name) - { - $this->removeFields[] = $name; - return $this; - } - - /** - * Removes a primary key - * - * @param *string $name Name of key - * - * @return QueryAlter - */ - public function removePrimaryKey($name) - { - $this->removePrimaryKeys[] = $name; - return $this; - } - - /** - * Sets the name of the table you wish to create - * - * @param *string $name Name of table - * - * @return QueryAlter - */ - public function setName($name) - { - $this->name = $name; - return $this; - } -} diff --git a/src/PostGreSql/QueryCreate.php b/src/PostGreSql/QueryCreate.php deleted file mode 100644 index a6f96ea..0000000 --- a/src/PostGreSql/QueryCreate.php +++ /dev/null @@ -1,196 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm\PostGreSql; - -use Cradle\Storm\AbstractQuery; - -/** - * Generates create table query string syntax - * - * @vendor Cradle - * @package Sql - * @author Christian Blanquera - * @standard PSR-2 - */ -class QueryCreate extends AbstractQuery -{ -/** - * @var string|null $name Name of table - */ - protected $name = null; - - /** - * @var array $fields List of fields - */ - protected $fields = []; - - /** - * @var array $primaryKeys List of primary keys - */ - protected $primaryKeys = []; - - /** - * @var array $oids Whether to use OIDs - */ - protected $oids = false; - - /** - * Construct: set table name, if given - * - * @param string|null $name Name of table - */ - public function __construct($name = null) - { - if (is_string($name)) { - $this->setName($name); - } - } - - /** - * Adds a field in the table - * - * @param *string $name Column name - * @param *array $attributes Column attributes - * - * @return QueryCreate - */ - public function addField($name, array $attributes) - { - $this->fields[$name] = $attributes; - return $this; - } - - /** - * Adds a primary key - * - * @param *string $name Name of key - * - * @return QueryCreate - */ - public function addPrimaryKey($name) - { - $this->primaryKeys[] = $name; - return $this; - } - - /** - * Returns the string version of the query - * - * @param bool $unbind Whether to unbind variables - * - * @return string - */ - public function getQuery($unbind = false) - { - $table = '"'.$this->name.'"'; - - $fields = []; - foreach ($this->fields as $name => $attr) { - $field = ['"'.$name.'"']; - if (isset($attr['type'])) { - $field[] = isset($attr['length']) ? - $attr['type'] . '('.$attr['length'].')' : - $attr['type']; - - if (isset($attr['list']) && $attr['list']) { - $field[count($field)-1].='[]'; - } - } - - if (isset($attr['attribute'])) { - $field[] = $attr['attribute']; - } - - if (isset($attr['unique']) && $attr['unique']) { - $field[] = 'UNIQUE'; - } - - if (isset($attr['null'])) { - if ($attr['null'] == false) { - $field[] = 'NOT NULL'; - } else { - $field[] = 'DEFAULT NULL'; - } - } - - if (isset($attr['default'])&& $attr['default'] !== false) { - if (!isset($attr['null']) || $attr['null'] == false) { - if (is_string($attr['default'])) { - $field[] = 'DEFAULT \''.$attr['default'] . '\''; - } else if (is_numeric($attr['default'])) { - $field[] = 'DEFAULT '.$attr['default']; - } - } - } - - $fields[] = implode(' ', $field); - } - - $oids = $this->oids ? 'WITH OIDS': null; - $fields = !empty($fields) ? implode(', ', $fields) : ''; - $primary = !empty($this->primaryKeys) ? - ', PRIMARY KEY ("'.implode('", ""', $this->primaryKeys).'")' : - ''; - - return sprintf('CREATE TABLE %s (%s%s) %s;', $table, $fields, $primary, $oids); - } - - /** - * Sets a list of fields to the table - * - * @param array $fields List of fields - * - * @return QueryCreate - */ - public function setFields(array $fields) - { - $this->fields = $fields; - return $this; - } - - /** - * Sets the name of the table you wish to create - * - * @param *string $name Table name - * - * @return QueryCreate - */ - public function setName($name) - { - $this->name = $name; - return $this; - } - - /** - * Sets a list of primary keys to the table - * - * @param *array $primaryKeys List of primary keys - * - * @return QueryCreate - */ - public function setPrimaryKeys(array $primaryKeys) - { - $this->primaryKeys = $primaryKeys; - return $this; - } - - /** - * Specifying if query should add the OIDs as columns - * - * @param bool $oids true or false - * - * @return QueryCreate - */ - public function withOids($oids) - { - $this->oids = $oids; - return $this; - } -} diff --git a/src/PostGreSql/QueryDelete.php b/src/PostGreSql/QueryDelete.php deleted file mode 100644 index 3b8e01f..0000000 --- a/src/PostGreSql/QueryDelete.php +++ /dev/null @@ -1,55 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm\PostGreSql; - -use Cradle\Storm\QueryDelete as SqlQueryDelete; - -/** - * Generates delete query string syntax - * - * @vendor Cradle - * @package Sql - * @author Christian Blanquera - * @standard PSR-2 - */ -class QueryDelete extends SqlQueryDelete -{ - /** - * @var array $table Table name - */ - protected $table = null; - - /** - * @var array $where List of filters - */ - protected $where = []; - - /** - * Construct: set table name, if given - * - * @param string|null $table The initial name of the table - */ - public function __construct($table = null) - { - if (is_string($table)) { - $this->setTable($table); - } - } - - /** - * Returns the string version of the query - * - * @return string - */ - public function getQuery() - { - return 'DELETE FROM "'. $this->table . '" WHERE '. implode(' AND ', $this->where).';'; - } -} diff --git a/src/PostGreSql/QueryInsert.php b/src/PostGreSql/QueryInsert.php deleted file mode 100644 index 11d062b..0000000 --- a/src/PostGreSql/QueryInsert.php +++ /dev/null @@ -1,67 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm\PostGreSql; - -use Cradle\Storm\QueryInsert as SqlQueryInsert; - -/** - * Generates insert query string syntax - * - * @vendor Cradle - * @package Sql - * @author Christian Blanquera - * @standard PSR-2 - */ -class QueryInsert extends SqlQueryInsert -{ - /** - * Returns the string version of the query - * - * @return string - */ - public function getQuery() - { - $multiValList = []; - foreach ($this->setVal as $val) { - $multiValList[] = '('.implode(', ', $val).')'; - } - - return 'INSERT INTO "'. $this->table - . '" ("'.implode('", "', $this->setKey).'") VALUES ' - . implode(", \n", $multiValList).';'; - } - - /** - * Set clause that assigns a given field name to a given value. - * You can also use this to add multiple rows in one call - * - * @param *string $key The column name - * @param *scalar|null $value The column value - * @param int $index For what row is this for? - * - * @return this - * @notes loads a set into registry - */ - public function set($key, $value, $index = 0) - { - if (!in_array($key, $this->setKey)) { - $this->setKey[] = $key; - } - - if (is_null($value)) { - $value = 'NULL'; - } else if (is_bool($value)) { - $value = $value ? 'TRUE' : 'FALSE'; - } - - $this->setVal[$index][] = $value; - return $this; - } -} diff --git a/src/PostGreSql/QuerySelect.php b/src/PostGreSql/QuerySelect.php deleted file mode 100644 index 8dc42e1..0000000 --- a/src/PostGreSql/QuerySelect.php +++ /dev/null @@ -1,50 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm\PostGreSql; - -use Cradle\Storm\QuerySelect as SqlQuerySelect; - -/** - * Generates select query string syntax - * - * @vendor Cradle - * @package Sql - * @author Christian Blanquera - * @standard PSR-2 - */ -class QuerySelect extends SqlQuerySelect -{ - /** - * Returns the string version of the query - * - * @return string - */ - public function getQuery() - { - $joins = empty($this->joins) ? '' : implode(' ', $this->joins); - $where = empty($this->where) ? '' : 'WHERE '.implode(' AND ', $this->where); - $sort = empty($this->sortBy) ? '' : 'ORDER BY '.implode(', ', $this->sortBy); - $limit = is_null($this->page) ? '' : 'LIMIT ' . $this->length .' OFFSET ' .$this->page; - $group = empty($this->group) ? '' : 'GROUP BY ' . implode(', ', $this->group); - - $query = sprintf( - 'SELECT %s FROM %s %s %s %s %s %s;', - $this->select, - $this->from, - $joins, - $where, - $group, - $sort, - $limit - ); - - return str_replace(' ', ' ', $query); - } -} diff --git a/src/PostGreSql/QueryUpdate.php b/src/PostGreSql/QueryUpdate.php deleted file mode 100644 index e3809d7..0000000 --- a/src/PostGreSql/QueryUpdate.php +++ /dev/null @@ -1,67 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm\PostGreSql; - -use Cradle\Storm\QueryDelete as SqlQueryDelete; - -/** - * Generates update query string syntax - * - * @vendor Cradle - * @package Sql - * @author Christian Blanquera - * @standard PSR-2 - */ -class QueryUpdate extends SqlQueryDelete -{ - /** - * @var array $set List of key/values - */ - protected $set = []; - - /** - * Returns the string version of the query - * - * @return string - */ - public function getQuery() - { - $set = []; - foreach ($this->set as $key => $value) { - $set[] = '"'.$key.'" = '.$value; - } - - return 'UPDATE '. $this->table - . ' SET ' . implode(', ', $set) - . ' WHERE '. implode(' AND ', $this->where).';'; - } - - /** - * Set clause that assigns a given field name to a given value. - * - * @param *string $key The column name - * @param *scalar|null $value The column value - * - * @return this - * @notes loads a set into registry - */ - public function set($key, $value) - { - if (is_null($value)) { - $value = 'NULL'; - } else if (is_bool($value)) { - $value = $value ? 'TRUE' : 'FALSE'; - } - - $this->set[$key] = $value; - - return $this; - } -} diff --git a/src/PostGreSql/QueryUtility.php b/src/PostGreSql/QueryUtility.php deleted file mode 100644 index 591a30a..0000000 --- a/src/PostGreSql/QueryUtility.php +++ /dev/null @@ -1,91 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm\PostGreSql; - -use Cradle\Storm\AbstractQuery; - -/** - * Generates utility query strings - * - * @vendor Cradle - * @package Sql - * @author Christian Blanquera - * @standard PSR-2 - */ -class QueryUtility extends AbstractQuery -{ - /** - * @var string|null $query The query string - */ - protected $query = null; - - /** - * Query for dropping a table - * - * @param *string $table The name of the table - * - * @return QueryUtility - */ - public function dropTable($table) - { - $this->query = 'DROP TABLE "' . $table .'"'; - return $this; - } - - /** - * Returns the string version of the query - * - * @return string - */ - public function getQuery() - { - return $this->query.';'; - } - - /** - * Query for renaming a table - * - * @param *string $table The name of the table - * @param *string $name The new name of the table - * - * @return QueryUtility - */ - public function renameTable($table, $name) - { - $this->query = 'RENAME TABLE "' . $table . '" TO "' . $name . '"'; - return $this; - } - - /** - * Specify the schema - * - * @param *string $schema The schema name - * - * @return QueryUtility - */ - public function setSchema($schema) - { - $this->query = 'SET search_path TO '.$schema; - return $this; - } - - /** - * Query for truncating a table - * - * @param *string $table The name of the table - * - * @return QueryUtility - */ - public function truncate($table) - { - $this->query = 'TRUNCATE "' . $table .'"'; - return $this; - } -} diff --git a/src/Query/AbstractQuery.php b/src/Query/AbstractQuery.php new file mode 100644 index 0000000..7c1ccf9 --- /dev/null +++ b/src/Query/AbstractQuery.php @@ -0,0 +1,56 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Query; + +/** + * Generates select query string syntax + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +abstract class AbstractQuery +{ + /** + * @var string $table most queries deal with tables + */ + protected $table = null; + + /** + * Transform class to string using getQuery + * + * @return string + */ + public function __toString(): string + { + return $this->getQuery(); + } + + /** + * Returns the string version of the query + * + * @return string + */ + abstract public function getQuery(): string; + + /** + * Set the table name in which you want to delete from + * + * @param string $table The table name + * + * @return QueryInterface + */ + public function setTable(string $table): QueryInterface + { + $this->table = $table; + return $this; + } +} diff --git a/src/Query/Delete.php b/src/Query/Delete.php new file mode 100644 index 0000000..0524165 --- /dev/null +++ b/src/Query/Delete.php @@ -0,0 +1,110 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Query; + +/** + * Generates delete query string syntax + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +class Delete extends AbstractQuery implements QueryInterface +{ + /** + * @var array|null $joins List of relatoinal joins + */ + protected $joins = []; + + /** + * @var array $where List of filters + */ + protected $where = []; + + /** + * Construct: Set the table, if any + * + * @param ?string $table The initial name of the table + */ + public function __construct(?string $table = null) + { + if (is_string($table)) { + $this->setTable($table); + } + } + + /** + * Returns the string version of the query + * + * @return string + */ + public function getQuery(): string + { + $joins = ''; + if (!empty($this->joins)) { + $joins = implode(' ', $this->joins); + } + + $where = ''; + if (!empty($this->joins)) { + $where = sprintf('WHERE %s', implode(' AND ', $this->where)); + } + + $query = sprintf('DELETE FROM %s %s %s;', $this->table, $joins, $where); + + return str_replace(' ', ' ', $query); + } + + /** + * Allows you to add joins of different types + * to the query + * + * @param *string $type Join type + * @param *string $table Table name to join + * @param *string $where Filter/s + * @param bool $using Whether to use "using" syntax (as opposed to "on") + * + * @return QueryInterface + */ + public function join( + string $type, + string $table, + string $where, + bool $using = true + ): QueryInterface + { + $linkage = sprintf('ON (%s)', $where); + if ($using) { + $linkage = sprintf('USING (%s)', $where); + } + + $this->joins[] = sprintf('%s JOIN %s %s', $type, $table, $linkage); + return $this; + } + + /** + * Where clause + * + * @param array|string $where The where clause + * + * @return QueryInterface + */ + public function where($where): QueryInterface + { + if (is_string($where)) { + $where = [$where]; + } + + $this->where = array_merge($this->where, $where); + + return $this; + } +} diff --git a/src/Query/Insert.php b/src/Query/Insert.php new file mode 100644 index 0000000..0869efb --- /dev/null +++ b/src/Query/Insert.php @@ -0,0 +1,89 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Query; + +/** + * Generates insert query string syntax + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera cblanquera@openovate.com + * @standard PSR-2 + */ +class Insert extends AbstractQuery implements QueryInterface +{ + /** + * @var array $setKey List of keys + */ + protected $setKey = []; + + /** + * @var array $setVal List of values + */ + protected $setVal = []; + + /** + * Set the table, if any + * + * @param ?string $table Table name + */ + public function __construct(?string $table = null) + { + if (is_string($table)) { + $this->setTable($table); + } + } + + /** + * Returns the string version of the query + * + * @return string + */ + public function getQuery(): string + { + $multiValList = []; + foreach ($this->setVal as $val) { + $multiValList[] = sprintf('(%s)', implode(', ', $val)); + } + + return sprintf( + 'INSERT INTO %s (%s) VALUES %s;', + $this->table, + implode(', ', $this->setKey), + implode(", \n", $multiValList) + ); + } + + /** + * Set clause that assigns a given field name to a given value. + * You can also use this to add multiple rows in one call + * + * @param *string $key The column name + * @param ?scalar $value The column value + * @param int $index For what row is this for? + * + * @return QueryInterface + */ + public function set(string $key, $value, int $index = 0): QueryInterface + { + if (!in_array($key, $this->setKey)) { + $this->setKey[] = $key; + } + + if (is_null($value)) { + $value = 'null'; + } else if (is_bool($value)) { + $value = $value ? 1 : 0; + } + + $this->setVal[$index][] = $value; + return $this; + } +} diff --git a/src/Query/MySql/Alter.php b/src/Query/MySql/Alter.php new file mode 100644 index 0000000..33a15bf --- /dev/null +++ b/src/Query/MySql/Alter.php @@ -0,0 +1,327 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Query\MySql; + +use Cradle\Storm\Query\QueryInterface; +use Cradle\Storm\Query\AbstractQuery; + +/** + * Generates alter query string syntax + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +class Alter extends AbstractQuery implements QueryInterface +{ + /** + * @var array $changeFields List of fields to change + */ + protected $changeFields = []; + + /** + * @var array $addFields List of fields to add + */ + protected $addFields = []; + + /** + * @var array $removeFields List of fields to remove + */ + protected $removeFields = []; + + /** + * @var array $addKeys List of keys to add + */ + protected $addKeys = []; + + /** + * @var array $removeKeys List of keys to remove + */ + protected $removeKeys = []; + + /** + * @var array $addUniqueKeys List of unique keys to add + */ + protected $addUniqueKeys = []; + + /** + * @var array $removeUniqueKeys List of unique keys to remove + */ + protected $removeUniqueKeys = []; + + /** + * @var array $addPrimaryKeys List of primary keys to add + */ + protected $addPrimaryKeys = []; + + /** + * @var array $removePrimaryKeys List of primary keys to remove + */ + protected $removePrimaryKeys = []; + + /** + * Construct: set table name, if given + * + * @param ?string $table Table name + */ + public function __construct(?string $table = null) + { + if (is_string($table)) { + $this->setTable($table); + } + } + + /** + * Adds a field in the table + * + * @param *string $name Column name + * @param *array $attributes Column attributes + * + * @return QueryInterface + */ + public function addField(string $name, array $attributes): QueryInterface + { + $this->addFields[$name] = $attributes; + return $this; + } + + /** + * Adds an index key + * + * @param *string $name Name of key + * + * @return QueryInterface + */ + public function addKey(string $name): QueryInterface + { + $this->addKeys[] = '`'.$name.'`'; + return $this; + } + + /** + * Adds a primary key + * + * @param *string $name Name of key + * + * @return QueryInterface + */ + public function addPrimaryKey(string $name): QueryInterface + { + $this->addPrimaryKeys[] = '`'.$name.'`'; + return $this; + } + + /** + * Adds a unique key + * + * @param *string $name Name of key + * + * @return QueryInterface + */ + public function addUniqueKey(string $name): QueryInterface + { + $this->addUniqueKeys[] = '`'.$name.'`'; + return $this; + } + + /** + * Changes attributes of the table given + * the field name + * + * @param *string $name Column name + * @param *array $attributes Column attributes + * + * @return QueryInterface + */ + public function changeField(string $name, array $attributes): QueryInterface + { + $this->changeFields[$name] = $attributes; + return $this; + } + + /** + * Returns the string version of the query + * + * @param bool $unbind Whether to unbind variables + * + * @return string + */ + public function getQuery(bool $unbind = false): string + { + $fields = []; + $table = '`'.$this->table.'`'; + + foreach ($this->removeFields as $name) { + $fields[] = 'DROP `'.$name.'`'; + } + + foreach ($this->addFields as $name => $attr) { + $field = ['ADD `'.$name.'`']; + if (isset($attr['type'])) { + $field[] = isset($attr['length']) ? + $attr['type'] . '('.$attr['length'].')' : + $attr['type']; + } + + if (isset($attr['attribute'])) { + $field[] = $attr['attribute']; + } + + if (isset($attr['null'])) { + if ($attr['null'] == false) { + $field[] = 'NOT NULL'; + } else { + $field[] = 'DEFAULT NULL'; + } + } + + if (isset($attr['default'])&& $attr['default'] !== false) { + if (!isset($attr['null']) || $attr['null'] == false) { + if (is_string($attr['default'])) { + $field[] = 'DEFAULT \''.$attr['default'] . '\''; + } else if (is_numeric($attr['default'])) { + $field[] = 'DEFAULT '.$attr['default']; + } + } + } + + if (isset($attr['auto_increment']) && $attr['auto_increment'] == true) { + $field[] = 'auto_increment'; + } + + $fields[] = implode(' ', $field); + } + + foreach ($this->changeFields as $name => $attr) { + $field = ['CHANGE `'.$name.'` `'.$name.'`']; + + if (isset($attr['name'])) { + $field = ['CHANGE `'.$name.'` `'.$attr['name'].'`']; + } + + if (isset($attr['type'])) { + $field[] = isset($attr['length']) ? $attr['type'] . '('.$attr['length'].')' : $attr['type']; + } + + if (isset($attr['attribute'])) { + $field[] = $attr['attribute']; + } + + if (isset($attr['null'])) { + if ($attr['null'] == false) { + $field[] = 'NOT NULL'; + } else { + $field[] = 'DEFAULT NULL'; + } + } + + if (isset($attr['default'])&& $attr['default'] !== false) { + if (!isset($attr['null']) || $attr['null'] == false) { + if (is_string($attr['default'])) { + $field[] = 'DEFAULT \''.$attr['default'] . '\''; + } else if (is_numeric($attr['default'])) { + $field[] = 'DEFAULT '.$attr['default']; + } + } + } + + if (isset($attr['auto_increment']) && $attr['auto_increment'] == true) { + $field[] = 'auto_increment'; + } + + $fields[] = implode(' ', $field); + } + + foreach ($this->removeKeys as $key) { + $fields[] = 'DROP INDEX `'.$key.'`'; + } + + if (!empty($this->addKeys)) { + $fields[] = 'ADD INDEX ('.implode(', ', $this->addKeys).')'; + } + + foreach ($this->removeUniqueKeys as $key) { + $fields[] = 'DROP INDEX `'.$key.'`'; + } + + if (!empty($this->addUniqueKeys)) { + $fields[] = 'ADD UNIQUE ('.implode(', ', $this->addUniqueKeys).')'; + } + + foreach ($this->removePrimaryKeys as $key) { + $fields[] = 'DROP PRIMARY KEY `'.$key.'`'; + } + + if (!empty($this->addPrimaryKeys)) { + $fields[] = 'ADD PRIMARY KEY ('.implode(', ', $this->addPrimaryKeys).')'; + } + + $fields = implode(", \n", $fields); + + return sprintf( + 'ALTER TABLE %s %s;', + $table, + $fields + ); + } + + /** + * Removes a field + * + * @param *string $name Column name + * + * @return QueryInterface + */ + public function removeField(string $name): QueryInterface + { + $this->removeFields[] = $name; + return $this; + } + + /** + * Removes an index key + * + * @param *string $name Name of key + * + * @return QueryInterface + */ + public function removeKey(string $name): QueryInterface + { + $this->removeKeys[] = $name; + return $this; + } + + /** + * Removes a primary key + * + * @param *string $name Name of key + * + * @return QueryInterface + */ + public function removePrimaryKey(string $name): QueryInterface + { + $this->removePrimaryKeys[] = $name; + return $this; + } + + /** + * Removes a unique key + * + * @param *string $name Name of key + * + * @return QueryInterface + */ + public function removeUniqueKey(string $name): QueryInterface + { + $this->removeUniqueKeys[] = $name; + return $this; + } +} diff --git a/src/Query/MySql/Create.php b/src/Query/MySql/Create.php new file mode 100644 index 0000000..ff04e14 --- /dev/null +++ b/src/Query/MySql/Create.php @@ -0,0 +1,258 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Query\MySql; + +use Cradle\Storm\Query\QueryInterface; +use Cradle\Storm\Query\AbstractQuery; + +/** + * Generates create table query string syntax + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +class Create extends AbstractQuery implements QueryInterface +{ + /** + * @var ?string $comments Table comments + */ + protected $comments = null; + + /** + * @var array $fields List of fields + */ + protected $fields = []; + + /** + * @var array $keys List of key indexes + */ + protected $keys = []; + + /** + * @var array $uniqueKeys List of unique keys + */ + protected $uniqueKeys = []; + + /** + * @var array $primaryKeys List of primary keys + */ + protected $primaryKeys = []; + + /** + * Construct: set table name, if given + * + * @param ?string $table Table name + */ + public function __construct(?string $table = null) + { + if (is_string($table)) { + $this->setTable($table); + } + } + + /** + * Adds a field in the table + * + * @param *string $name Column name + * @param *array $attributes Column attributes + * + * @return QueryInterface + */ + public function addField(string $name, array $attributes): QueryInterface + { + $this->fields[$name] = $attributes; + return $this; + } + + /** + * Adds an index key + * + * @param *string $name Name of key + * @param *array $fields List of key fields + * + * @return QueryInterface + */ + public function addKey(string $name, array $fields): QueryInterface + { + $this->keys[$name] = $fields; + return $this; + } + + /** + * Adds a primary key + * + * @param *string $name Name of key + * + * @return QueryInterface + */ + public function addPrimaryKey(string $name): QueryInterface + { + $this->primaryKeys[] = $name; + return $this; + } + + /** + * Adds a unique key + * + * @param *string $name Name of key + * @param *array $fields List of key fields + * + * @return QueryInterface + */ + public function addUniqueKey(string $name, array $fields): QueryInterface + { + $this->uniqueKeys[$name] = $fields; + return $this; + } + + /** + * Returns the string version of the query + * + * @return string + */ + public function getQuery(): string + { + $table = '`'.$this->table.'`'; + + $fields = []; + foreach ($this->fields as $name => $attr) { + $field = ['`'.$name.'`']; + if (isset($attr['type'])) { + $field[] = isset($attr['length']) && $attr['length'] ? + $attr['type'] . '('.$attr['length'].')' : + $attr['type']; + } + + if (isset($attr['attribute'])) { + $field[] = $attr['attribute']; + } + + if (isset($attr['null'])) { + if ($attr['null'] == false) { + $field[] = 'NOT NULL'; + } else { + $field[] = 'DEFAULT NULL'; + } + } + + if (isset($attr['default'])&& $attr['default'] !== false) { + if (!isset($attr['null']) || $attr['null'] == false) { + if (is_string($attr['default'])) { + $field[] = 'DEFAULT \''.$attr['default'] . '\''; + } else if (is_numeric($attr['default'])) { + $field[] = 'DEFAULT '.$attr['default']; + } + } + } + + if (isset($attr['auto_increment']) && $attr['auto_increment'] == true) { + $field[] = 'auto_increment'; + } + + $fields[] = implode(' ', $field); + } + + $fields = !empty($fields) ? implode(', ', $fields) : ''; + + $primary = !empty($this->primaryKeys) ? + ', PRIMARY KEY (`'.implode('`, `', $this->primaryKeys).'`)' : + ''; + + $uniques = []; + foreach ($this->uniqueKeys as $key => $value) { + $uniques[] = 'UNIQUE KEY `'. $key .'` (`'.implode('`, `', $value).'`)'; + } + + $uniques = !empty($uniques) ? ', ' . implode(", \n", $uniques) : ''; + + $keys = []; + foreach ($this->keys as $key => $value) { + $keys[] = 'KEY `'. $key .'` (`'.implode('`, `', $value).'`)'; + } + + $keys = !empty($keys) ? ', ' . implode(", \n", $keys) : ''; + + return sprintf( + 'CREATE TABLE %s (%s%s%s%s);', + $table, + $fields, + $primary, + $uniques, + $keys + ); + } + + /** + * Sets comments + * + * @param *string $comments Table comments + * + * @return QueryInterface + */ + public function setComments(string $comments): QueryInterface + { + $this->comments = $comments; + return $this; + } + + /** + * Sets a list of fields to the table + * + * @param *array $fields List of fields + * + * @return QueryInterface + */ + public function setFields(array $fields): QueryInterface + { + $this->fields = $fields; + return $this; + } + + /** + * Sets a list of keys to the table + * + * @param *array $keys List of keys + * + * @return QueryInterface + */ + public function setKeys(array $keys): QueryInterface + { + $this->keys = $keys; + return $this; + } + + /** + * Sets a list of primary keys to the table + * + * @param *array $primaryKeys List of primary keys + * + * @return QueryInterface + */ + public function setPrimaryKeys(array $primaryKeys): QueryInterface + { + $this->primaryKeys = $primaryKeys; + return $this; + } + + /** + * Sets a list of unique keys to the table + * + * @param *array $uniqueKeys List of unique keys + * + * @return QueryInterface + */ + public function setUniqueKeys(array $uniqueKeys): QueryInterface + { + $this->uniqueKeys = $uniqueKeys; + return $this; + } +} diff --git a/src/Query/MySql/Utility.php b/src/Query/MySql/Utility.php new file mode 100644 index 0000000..8141ac4 --- /dev/null +++ b/src/Query/MySql/Utility.php @@ -0,0 +1,114 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Query\MySql; + +use Cradle\Storm\Query\QueryInterface; +use Cradle\Storm\Query\AbstractQuery; + +/** + * Generates utility query strings + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +class Utility extends AbstractQuery implements QueryInterface +{ + /** + * @var ?string $query The query string + */ + protected $query = null; + + /** + * Query for dropping a table + * + * @param *string $table The name of the table + * + * @return QueryInterface + */ + public function dropTable(string $table): QueryInterface + { + $this->query = sprintf('DROP TABLE `%s`', $table); + return $this; + } + + /** + * Returns the string version of the query + * + * @return string + */ + public function getQuery(): string + { + return sprintf('%s;', $this->query); + } + + /** + * Query for renaming a table + * + * @param *string $table The name of the table + * @param *string $name The new name of the table + * + * @return QueryInterface + */ + public function renameTable(string $table, string $name): QueryInterface + { + $this->query = sprintf('RENAME TABLE `%s` TO `%s`', $table, $name); + return $this; + } + + /** + * Query for showing all columns of a table + * + * @param *string $table The name of the table + * @param ?string $where Filter/s + * + * @return QueryInterface + */ + public function showColumns(string $table, ?string $where = null): QueryInterface + { + if ($where) { + $where = sprintf('WHERE %s', $where); + } + + $this->query = sprintf('SHOW FULL COLUMNS FROM `%s` %s', $table, $where); + return $this; + } + + /** + * Query for showing all tables + * + * @param ?string $like The like pattern + * + * @return QueryInterface + */ + public function showTables(?string $like = null): QueryInterface + { + if ($like) { + $like = sprintf('LIKE %s', $like); + } + + $this->query = sprintf('SHOW TABLES %s', $like); + return $this; + } + + /** + * Query for truncating a table + * + * @param *string $table The name of the table + * + * @return QueryInterface + */ + public function truncate(string $table): QueryInterface + { + $this->query = sprintf('TRUNCATE `%s`', $table); + return $this; + } +} diff --git a/src/Query/PostGreSql/Alter.php b/src/Query/PostGreSql/Alter.php new file mode 100644 index 0000000..64fe7cf --- /dev/null +++ b/src/Query/PostGreSql/Alter.php @@ -0,0 +1,243 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Query\PostGreSql; + +use Cradle\Storm\Query\QueryInterface; +use Cradle\Storm\Query\AbstractQuery; + +/** + * Generates alter query string syntax + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +class Alter extends AbstractQuery implements QueryInterface +{ + /** + * @var array $changeFields List of fields to change + */ + protected $changeFields = []; + + /** + * @var array $addFields List of fields to add + */ + protected $addFields = []; + + /** + * @var array $removeFields List of fields to remove + */ + protected $removeFields = []; + + /** + * @var array $addPrimaryKeys List of primary keys to add + */ + protected $addPrimaryKeys = []; + + /** + * @var array $removePrimaryKeys List of primary keys to remove + */ + protected $removePrimaryKeys = []; + + /** + * Construct: set table name, if given + * + * @param ?string $table Table name + */ + public function __construct(?string $table = null) + { + if (is_string($table)) { + $this->setTable($table); + } + } + + /** + * Adds a field in the table + * + * @param *string $name Column name + * @param *array $attributes Column attributes + * + * @return QueryAlter + */ + public function addField($name, array $attributes) + { + $this->addFields[$name] = $attributes; + return $this; + } + + /** + * Adds a primary key + * + * @param *string $name Name of key + * + * @return QueryAlter + */ + public function addPrimaryKey($name) + { + $this->addPrimaryKeys[] = '"'.$name.'"'; + return $this; + } + + /** + * Changes attributes of the table given + * the field name + * + * @param *string $name Column name + * @param *array $attributes Column attributes + * + * @return QueryAlter + */ + public function changeField($name, array $attributes) + { + $this->changeFields[$name] = $attributes; + return $this; + } + + /** + * Returns the string version of the query + * + * @return string + */ + public function getQuery(): string + { + $fields = []; + $table = '"'.$this->table.'"'; + + foreach ($this->removeFields as $name) { + $fields[] = 'DROP COLUMN "'.$name.'"'; + } + + foreach ($this->addFields as $name => $attr) { + $field = ['ADD "'.$name.'"']; + if (isset($attr['type'])) { + $field[] = isset($attr['length']) ? $attr['type'] + . '('.$attr['length'].')' : + $attr['type']; + + if (isset($attr['list']) && $attr['list']) { + $field[count($field)-1].='[]'; + } + } + + if (isset($attr['attribute'])) { + $field[] = $attr['attribute']; + } + + if (isset($attr['unique']) && $attr['unique']) { + $field[] = 'UNIQUE'; + } + + if (isset($attr['null'])) { + if ($attr['null'] == false) { + $field[] = 'NOT NULL'; + } else { + $field[] = 'DEFAULT NULL'; + } + } + + if (isset($attr['default'])&& $attr['default'] !== false) { + if (!isset($attr['null']) || $attr['null'] == false) { + if (is_string($attr['default'])) { + $field[] = 'DEFAULT \''.$attr['default'] . '\''; + } else if (is_numeric($attr['default'])) { + $field[] = 'DEFAULT '.$attr['default']; + } + } + } + + $fields[] = implode(' ', $field); + } + + foreach ($this->changeFields as $name => $attr) { + $field = ['ALTER COLUMN "'.$name.'"']; + + if (isset($attr['name'])) { + $field = ['CHANGE "'.$name.'" "'.$attr['name'].'"']; + } + + if (isset($attr['type'])) { + $field[] = isset($attr['length']) ? + $attr['type'] . '('.$attr['length'].')' : + $attr['type']; + + if (isset($attr['list']) && $attr['list']) { + $field[count($field)-1].='[]'; + } + } + + if (isset($attr['attribute'])) { + $field[] = $attr['attribute']; + } + + if (isset($attr['unique']) && $attr['unique']) { + $field[] = 'UNIQUE'; + } + + if (isset($attr['null'])) { + if ($attr['null'] == false) { + $field[] = 'NOT NULL'; + } else { + $field[] = 'DEFAULT NULL'; + } + } + + if (isset($attr['default'])&& $attr['default'] !== false) { + if (!isset($attr['null']) || $attr['null'] == false) { + if (is_string($attr['default'])) { + $field[] = 'DEFAULT \''.$attr['default'] . '\''; + } else if (is_numeric($attr['default'])) { + $field[] = 'DEFAULT '.$attr['default']; + } + } + } + + $fields[] = implode(' ', $field); + } + + foreach ($this->removePrimaryKeys as $key) { + $fields[] = 'DROP PRIMARY KEY "'.$key.'"'; + } + + if (!empty($this->addPrimaryKeys)) { + $fields[] = 'ADD PRIMARY KEY ('.implode(', ', $this->addPrimaryKeys).')'; + } + + $fields = implode(", \n", $fields); + + return sprintf('ALTER TABLE %s %s;', $table, $fields); + } + + /** + * Removes a field + * + * @param *string $name Name of field + * + * @return QueryAlter + */ + public function removeField($name) + { + $this->removeFields[] = $name; + return $this; + } + + /** + * Removes a primary key + * + * @param *string $name Name of key + * + * @return QueryAlter + */ + public function removePrimaryKey($name) + { + $this->removePrimaryKeys[] = $name; + return $this; + } +} diff --git a/src/Query/PostGreSql/Create.php b/src/Query/PostGreSql/Create.php new file mode 100644 index 0000000..b9c14af --- /dev/null +++ b/src/Query/PostGreSql/Create.php @@ -0,0 +1,177 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Query\PostGreSql; + +use Cradle\Storm\Query\QueryInterface; +use Cradle\Storm\Query\AbstractQuery; + +/** + * Generates create table query string syntax + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +class Create extends AbstractQuery implements QueryInterface +{ + /** + * @var array $fields List of fields + */ + protected $fields = []; + + /** + * @var array $primaryKeys List of primary keys + */ + protected $primaryKeys = []; + + /** + * @var array $oids Whether to use OIDs + */ + protected $oids = false; + + /** + * Construct: set table name, if given + * + * @param ?string $table Table name + */ + public function __construct(?string $table = null) + { + if (is_string($table)) { + $this->setTable($table); + } + } + + /** + * Adds a field in the table + * + * @param *string $name Column name + * @param *array $attributes Column attributes + * + * @return QueryCreate + */ + public function addField($name, array $attributes) + { + $this->fields[$name] = $attributes; + return $this; + } + + /** + * Adds a primary key + * + * @param *string $name Name of key + * + * @return QueryCreate + */ + public function addPrimaryKey($name) + { + $this->primaryKeys[] = $name; + return $this; + } + + /** + * Returns the string version of the query + * + * @return string + */ + public function getQuery(): string + { + $table = '"'.$this->name.'"'; + + $fields = []; + foreach ($this->fields as $name => $attr) { + $field = ['"'.$name.'"']; + if (isset($attr['type'])) { + $field[] = isset($attr['length']) ? + $attr['type'] . '('.$attr['length'].')' : + $attr['type']; + + if (isset($attr['list']) && $attr['list']) { + $field[count($field)-1].='[]'; + } + } + + if (isset($attr['attribute'])) { + $field[] = $attr['attribute']; + } + + if (isset($attr['unique']) && $attr['unique']) { + $field[] = 'UNIQUE'; + } + + if (isset($attr['null'])) { + if ($attr['null'] == false) { + $field[] = 'NOT NULL'; + } else { + $field[] = 'DEFAULT NULL'; + } + } + + if (isset($attr['default'])&& $attr['default'] !== false) { + if (!isset($attr['null']) || $attr['null'] == false) { + if (is_string($attr['default'])) { + $field[] = 'DEFAULT \''.$attr['default'] . '\''; + } else if (is_numeric($attr['default'])) { + $field[] = 'DEFAULT '.$attr['default']; + } + } + } + + $fields[] = implode(' ', $field); + } + + $oids = $this->oids ? 'WITH OIDS': null; + $fields = !empty($fields) ? implode(', ', $fields) : ''; + $primary = !empty($this->primaryKeys) ? + ', PRIMARY KEY ("'.implode('", ""', $this->primaryKeys).'")' : + ''; + + return sprintf('CREATE TABLE %s (%s%s) %s;', $table, $fields, $primary, $oids); + } + + /** + * Sets a list of fields to the table + * + * @param array $fields List of fields + * + * @return QueryCreate + */ + public function setFields(array $fields) + { + $this->fields = $fields; + return $this; + } + + /** + * Sets a list of primary keys to the table + * + * @param *array $primaryKeys List of primary keys + * + * @return QueryCreate + */ + public function setPrimaryKeys(array $primaryKeys) + { + $this->primaryKeys = $primaryKeys; + return $this; + } + + /** + * Specifying if query should add the OIDs as columns + * + * @param bool $oids true or false + * + * @return QueryCreate + */ + public function withOids($oids) + { + $this->oids = $oids; + return $this; + } +} diff --git a/src/Query/PostGreSql/Delete.php b/src/Query/PostGreSql/Delete.php new file mode 100644 index 0000000..1a7325a --- /dev/null +++ b/src/Query/PostGreSql/Delete.php @@ -0,0 +1,59 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Query\PostGreSql; + +use Cradle\Storm\Query\Delete as QueryDelete; + +/** + * Generates delete query string syntax + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +class Delete extends QueryDelete +{ + /** + * @var array $table Table name + */ + protected $table = null; + + /** + * @var array $where List of filters + */ + protected $where = []; + + /** + * Construct: set table name, if given + * + * @param ?string $table Table name + */ + public function __construct(?string $table = null) + { + if (is_string($table)) { + $this->setTable($table); + } + } + + /** + * Returns the string version of the query + * + * @return string + */ + public function getQuery(): string + { + return sprintf( + 'DELETE FROM "%s" WHERE %s;', + $this->table, + implode(' AND ', $this->where) + ); + } +} diff --git a/src/Query/PostGreSql/Insert.php b/src/Query/PostGreSql/Insert.php new file mode 100644 index 0000000..1b1c128 --- /dev/null +++ b/src/Query/PostGreSql/Insert.php @@ -0,0 +1,70 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Query\PostGreSql; + +use Cradle\Storm\Query\QueryInterface; +use Cradle\Storm\Query\Insert as QueryInsert; + +/** + * Generates insert query string syntax + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +class Insert extends QueryInsert +{ + /** + * Returns the string version of the query + * + * @return string + */ + public function getQuery(): string + { + $multiValList = []; + foreach ($this->setVal as $val) { + $multiValList[] = sprintf('(%s)', implode(', ', $val)); + } + + return sprintf( + 'INSERT INTO "%s" ("%s") VALUES %s;', + $this->table, + implode('", "', $this->setKey), + implode(", \n", $multiValList) + ); + } + + /** + * Set clause that assigns a given field name to a given value. + * You can also use this to add multiple rows in one call + * + * @param *string $key The column name + * @param ?scalar $value The column value + * @param int $index For what row is this for? + * + * @return QueryInterface + */ + public function set(string $key, $value, int $index = 0): QueryInterface + { + if (!in_array($key, $this->setKey)) { + $this->setKey[] = $key; + } + + if (is_null($value)) { + $value = 'NULL'; + } else if (is_bool($value)) { + $value = $value ? 'TRUE' : 'FALSE'; + } + + $this->setVal[$index][] = $value; + return $this; + } +} diff --git a/src/Query/PostGreSql/Select.php b/src/Query/PostGreSql/Select.php new file mode 100644 index 0000000..470367c --- /dev/null +++ b/src/Query/PostGreSql/Select.php @@ -0,0 +1,75 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Query\PostGreSql; + +use Cradle\Storm\Query\Select as QuerySelect; + +/** + * Generates select query string syntax + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +class Select extends QuerySelect +{ + /** + * Returns the string version of the query + * + * @return string + */ + public function getQuery(): string + { + $joins = ''; + if (!empty($this->joins)) { + $joins = implode(' ', $this->joins); + } + + $where = ''; + if (!empty($this->joins)) { + $where = sprintf('WHERE %s', implode(' AND ', $this->where)); + } + + $sort = ''; + if (!empty($this->sortBy)) { + $sort = sprintf('ORDER BY %s', implode(', ', $this->sortBy)); + } + + $limit = ''; + if (!is_null($this->page) && $this->length) { + $limit = sprintf('LIMIT %s OFFSET %s', $this->length, $this->page); + } + + $group = ''; + if (!empty($this->group)) { + $group = sprintf('GROUP BY %s', implode(', ', $this->group)); + } + + $having = ''; + if (!empty($this->having)) { + $having = sprintf('HAVING %s', implode(', ', $this->having)); + } + + $query = sprintf( + 'SELECT %s FROM %s %s %s %s %s %s %s;', + $this->select, + $this->from, + $joins, + $where, + $group, + $having, + $sort, + $limit + ); + + return str_replace(' ', ' ', $query); + } +} diff --git a/src/Query/PostGreSql/Update.php b/src/Query/PostGreSql/Update.php new file mode 100644 index 0000000..44e5ea0 --- /dev/null +++ b/src/Query/PostGreSql/Update.php @@ -0,0 +1,70 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Query\PostGreSql; + +use Cradle\Storm\Query\QueryInterface; +use Cradle\Storm\Query\Delete as QueryDelete; + +/** + * Generates update query string syntax + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +class Update extends QueryDelete +{ + /** + * @var array $set List of key/values + */ + protected $set = []; + + /** + * Returns the string version of the query + * + * @return string + */ + public function getQuery(): string + { + $set = []; + foreach ($this->set as $key => $value) { + $set[] = sprintf('"%s" = %s', $key, $value); + } + + return sprintf( + 'UPDATE %s SET %s WHERE %s;', + $this->table, + implode(', ', $set), + implode(' AND ', $this->where) + ); + } + + /** + * Set clause that assigns a given field name to a given value. + * + * @param *string $key The column name + * @param ?scalar $value The column value + * + * @return QueryInterface + */ + public function set(string $key, $value): QueryInterface + { + if (is_null($value)) { + $value = 'NULL'; + } else if (is_bool($value)) { + $value = $value ? 'TRUE' : 'FALSE'; + } + + $this->set[$key] = $value; + + return $this; + } +} diff --git a/src/Query/PostGreSql/Utility.php b/src/Query/PostGreSql/Utility.php new file mode 100644 index 0000000..721c856 --- /dev/null +++ b/src/Query/PostGreSql/Utility.php @@ -0,0 +1,92 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Query\PostGreSql; + +use Cradle\Storm\Query\QueryInterface; +use Cradle\Storm\Query\AbstractQuery; + +/** + * Generates utility query strings + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +class Utility extends AbstractQuery implements QueryInterface +{ + /** + * @var ?string $query The query string + */ + protected $query = null; + + /** + * Query for dropping a table + * + * @param *string $table The name of the table + * + * @return QueryInterface + */ + public function dropTable(string $table): QueryInterface + { + $this->query = sprintf('DROP TABLE "%s"', $table); + return $this; + } + + /** + * Returns the string version of the query + * + * @return string + */ + public function getQuery(): string + { + return sprintf('%s;', $this->query); + } + + /** + * Query for renaming a table + * + * @param *string $table The name of the table + * @param *string $name The new name of the table + * + * @return QueryInterface + */ + public function renameTable(string $table, string $name): QueryInterface + { + $this->query = sprintf('RENAME TABLE "%s" TO "%s"', $table, $name); + return $this; + } + + /** + * Specify the schema + * + * @param *string $schema The schema name + * + * @return QueryInterface + */ + public function setSchema(string $schema): QueryInterface + { + $this->query = sprintf('SET search_path TO %s', $schema); + return $this; + } + + /** + * Query for truncating a table + * + * @param *string $table The name of the table + * + * @return QueryInterface + */ + public function truncate(string $table): QueryInterface + { + $this->query = sprintf('TRUNCATE "%s"', $table); + return $this; + } +} diff --git a/src/Query/QueryInterface.php b/src/Query/QueryInterface.php new file mode 100644 index 0000000..79e217d --- /dev/null +++ b/src/Query/QueryInterface.php @@ -0,0 +1,44 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Query; + +/** + * Generates select query string syntax + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +interface QueryInterface +{ + /** + * Transform class to string using getQuery + * + * @return string + */ + public function __toString(): string; + + /** + * Returns the string version of the query + * + * @return string + */ + public function getQuery(): string; + + /** + * Set the table name in which you want to delete from + * + * @param string $table The table name + * + * @return QueryInterface + */ + public function setTable(string $table): QueryInterface; +} diff --git a/src/Query/Select.php b/src/Query/Select.php new file mode 100644 index 0000000..6ba801b --- /dev/null +++ b/src/Query/Select.php @@ -0,0 +1,205 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Query; + +/** + * Generates select query string syntax + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +class Select extends Delete implements QueryInterface +{ + /** + * @var ?string $select List of columns + */ + protected $select = null; + + /** + * @var array $sortBy List of order and directions + */ + protected $sortBy = []; + + /** + * @var array $group List of "group bys" + */ + protected $group = []; + + /** + * @var int|null $page Pagination start + */ + protected $page = null; + + /** + * @var int|null $length Pagination range + */ + protected $length = null; + + /** + * Construct: Set the columns, if any + * + * @param string|array $select Column names + */ + public function __construct($select = '*') + { + $this->select($select); + } + + /** + * From clause + * + * @param *string $from Main table + * + * @return QueryInterface + */ + public function from(string $from): QueryInterface + { + $this->table = $from; + return $this; + } + + /** + * Returns the string version of the query + * + * @return string + */ + public function getQuery(): string + { + $joins = ''; + if (!empty($this->joins)) { + $joins = ' ' . implode(' ', $this->joins); + } + + $where = ''; + if (!empty($this->where)) { + $where = sprintf(' WHERE %s', implode(' AND ', $this->where)); + } + + $sort = ''; + if (!empty($this->sortBy)) { + $sort = sprintf(' ORDER BY %s', implode(', ', $this->sortBy)); + } + + $limit = ''; + if (!is_null($this->page) && $this->length) { + $limit = sprintf(' LIMIT %s, %s', $this->page, $this->length); + } + + $group = ''; + if (!empty($this->group)) { + $group = sprintf(' GROUP BY %s', implode(', ', $this->group)); + } + + $having = ''; + if (!empty($this->having)) { + $having = sprintf(' HAVING %s', implode(', ', $this->having)); + } + + $query = sprintf( + 'SELECT %s FROM %s%s%s%s%s%s%s', + $this->select, + $this->table, + $joins, + $where, + $group, + $having, + $sort, + $limit + ); + + return trim($query) . ';'; + } + + /** + * Group by clause + * + * @param *string|array $group List of "group bys" + * + * @return QueryInterface + */ + public function groupBy($group): QueryInterface + { + if (is_string($group)) { + $group = [$group]; + } + + $this->group = $group; + return $this; + } + + /** + * Having clause + * + * @param string $having Column name + * + * @return QueryInterface + */ + public function having(string $having): QueryInterface + { + if (is_string($having)) { + $having = [$having]; + } + + $this->having = $having; + return $this; + } + + /** + * Limit clause + * + * @param *string|int $page Pagination start + * @param *string|int $length Pagination range + * + * @return QueryInterface + */ + public function limit(int $page, int $length): QueryInterface + { + $this->page = $page; + $this->length = $length; + + return $this; + } + + /** + * Select clause + * + * @param string|array $select Select columns + * + * @return QueryInterface + */ + public function select($select = '*'): QueryInterface + { + //if select is an array + if (is_array($select)) { + //transform into a string + $select = implode(', ', $select); + } + + $this->select = $select; + + return $this; + } + + /** + * Order by clause + * + * @param *string $field Column name + * @param string $order Direction + * + * @return QueryInterface + */ + public function sortBy(string $field, string $order = 'ASC'): QueryInterface + { + $this->sortBy[] = sprintf('%s %s', $field, $order); + return $this; + } +} diff --git a/src/Query/Sqlite/Alter.php b/src/Query/Sqlite/Alter.php new file mode 100644 index 0000000..6c5a553 --- /dev/null +++ b/src/Query/Sqlite/Alter.php @@ -0,0 +1,287 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Query\Sqlite; + +use Cradle\Storm\Query\QueryInterface; +use Cradle\Storm\Query\AbstractQuery; + +/** + * Generates alter query string syntax + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +class Alter extends AbstractQuery implements QueryInterface +{ + /** + * @var array $changeFields List of fields to change + */ + protected $changeFields = []; + + /** + * @var array $addFields List of fields to add + */ + protected $addFields = []; + + /** + * @var array $removeFields List of fields to remove + */ + protected $removeFields = []; + + /** + * @var array $addKeys List of keys to add + */ + protected $addKeys = []; + + /** + * @var array $removeKeys List of keys to remove + */ + protected $removeKeys = []; + + /** + * @var array $addUniqueKeys List of unique keys to add + */ + protected $addUniqueKeys = []; + + /** + * @var array $removeUniqueKeys List of unique keys to remove + */ + protected $removeUniqueKeys = []; + + /** + * @var array $addPrimaryKeys List of primary keys to add + */ + protected $addPrimaryKeys = []; + + /** + * @var array $removePrimaryKeys List of primary keys to remove + */ + protected $removePrimaryKeys = []; + + /** + * Construct: set table name, if given + * + * @param ?string $table Table name + */ + public function __construct(?string $table = null) + { + if (is_string($table)) { + $this->setTable($table); + } + } + + /** + * Adds a field in the table + * + * @param *string $name Column name + * @param *array $attributes Column attributes + * + * @return QueryInterface + */ + public function addField(string $name, array $attributes): QueryInterface + { + $this->addFields[$name] = $attributes; + return $this; + } + + /** + * Adds an index key + * + * @param *string $name Name of key + * @param *string $table Name of key + * @param *string $key Name of key + * + * @return QueryInterface + */ + public function addForeignKey(string $name, string $table, string $key): QueryInterface + { + $this->addKeys[$name] = [$table, $key]; + return $this; + } + + /** + * Adds a unique key + * + * @param *string $name Name of key + * + * @return QueryInterface + */ + public function addUniqueKey(string $name): QueryInterface + { + $this->addUniqueKeys[] = '"'.$name.'"'; + return $this; + } + + /** + * Changes attributes of the table given + * the field name + * + * @param *string $name Column name + * @param *array $attributes Column attributes + * + * @return QueryInterface + */ + public function changeField(string $name, array $attributes): QueryInterface + { + $this->changeFields[$name] = $attributes; + return $this; + } + + /** + * Returns the string version of the query + * + * @return string + */ + public function getQuery(): string + { + $fields = []; + $table = '"'.$this->table.'"'; + + foreach ($this->removeFields as $name) { + $fields[] = 'DROP "'.$name.'"'; + } + + foreach ($this->addFields as $name => $attr) { + $field = ['ADD "'.$name.'"']; + if (isset($attr['type'])) { + $field[] = isset($attr['length']) ? + $attr['type'] . '('.$attr['length'].')' : + $attr['type']; + } + + if (isset($attr['attribute'])) { + $field[] = $attr['attribute']; + } + + if (isset($attr['null'])) { + if ($attr['null'] == false) { + $field[] = 'NOT NULL'; + } else { + $field[] = 'DEFAULT NULL'; + } + } + + if (isset($attr['default'])&& $attr['default'] !== false) { + if (!isset($attr['null']) || $attr['null'] == false) { + if (is_string($attr['default'])) { + $field[] = 'DEFAULT \''.$attr['default'] . '\''; + } else if (is_numeric($attr['default'])) { + $field[] = 'DEFAULT '.$attr['default']; + } + } + } + + $fields[] = implode(' ', $field); + } + + foreach ($this->changeFields as $name => $attr) { + $field = ['CHANGE "'.$name.'" "'.$name.'"']; + + if (isset($attr['name'])) { + $field = ['CHANGE "'.$name.'" "'.$attr['name'].'"']; + } + + if (isset($attr['type'])) { + $field[] = isset($attr['length']) ? + $attr['type'] . '('.$attr['length'].')' : + $attr['type']; + } + + if (isset($attr['attribute'])) { + $field[] = $attr['attribute']; + } + + if (isset($attr['null'])) { + if ($attr['null'] == false) { + $field[] = 'NOT NULL'; + } else { + $field[] = 'DEFAULT NULL'; + } + } + + if (isset($attr['default'])&& $attr['default'] !== false) { + if (!isset($attr['null']) || $attr['null'] == false) { + if (is_string($attr['default'])) { + $field[] = 'DEFAULT \''.$attr['default'] . '\''; + } else if (is_numeric($attr['default'])) { + $field[] = 'DEFAULT '.$attr['default']; + } + } + } + + $fields[] = implode(' ', $field); + } + + foreach ($this->removeKeys as $key) { + $fields[] = 'DROP FOREIGN KEY "'.$key.'"'; + } + + foreach ($this->addKeys as $key => $value) { + $fields[] = 'ADD FOREIGN KEY "'. $key .'" REFERENCES '.$value[0].'('.$value[1].')'; + } + + foreach ($this->removeUniqueKeys as $key) { + $fields[] = 'DROP UNIQUE "'.$key.'"'; + } + + if (!empty($this->addUniqueKeys)) { + $fields[] = 'ADD UNIQUE ('.implode(', ', $this->addUniqueKeys).')'; + } + + $fields = implode(", \n", $fields); + + return sprintf( + 'ALTER TABLE %s %s;', + $table, + $fields + ); + } + + /** + * Removes a field + * + * @param *string $name Name of field + * + * @return QueryInterface + */ + public function removeField(string $name): QueryInterface + { + $this->removeFields[] = $name; + return $this; + } + + /** + * Removes an index key + * + * @param *string $name Name of key + * + * @return QueryInterface + */ + public function removeForeignKey(string $name): QueryInterface + { + $this->removeKeys[] = $name; + return $this; + } + + /** + * Removes a unique key + * + * @param *string $name Name of key + * + * @return QueryInterface + */ + public function removeUniqueKey(string $name): QueryInterface + { + $this->removeUniqueKeys[] = $name; + return $this; + } +} diff --git a/src/Query/Sqlite/Create.php b/src/Query/Sqlite/Create.php new file mode 100644 index 0000000..01010ed --- /dev/null +++ b/src/Query/Sqlite/Create.php @@ -0,0 +1,228 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Query\Sqlite; + +use Cradle\Storm\Query\QueryInterface; +use Cradle\Storm\Query\AbstractQuery; + +/** + * Generates create table query string syntax + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +class Create extends AbstractQuery implements QueryInterface +{ + /** + * @var ?string $comments Table comments + */ + protected $comments = null; + + /** + * @var array $fields List of fields + */ + protected $fields = []; + + /** + * @var array $keys List of key indexes + */ + protected $keys = []; + + /** + * @var array $uniqueKeys List of unique keys + */ + protected $uniqueKeys = []; + + /** + * @var array $primaryKeys List of primary keys + */ + protected $primaryKeys = []; + + /** + * Construct: set table name, if given + * + * @param ?string $table Table name + */ + public function __construct(?string $table = null) + { + if (is_string($table)) { + $this->setTable($table); + } + } + + /** + * Adds a field in the table + * + * @param *string $name Column name + * @param *array $attributes Column attributes + * + * @return QueryInterface + */ + public function addField(string $name, array $attributes): QueryInterface + { + $this->fields[$name] = $attributes; + return $this; + } + + /** + * Adds an index key + * + * @param *string $name Name of column + * @param *string $table Name of foreign table + * @param *string $key Name of key + * + * @return QueryInterface + */ + public function addForeignKey(string $name, string $table, string $key): QueryInterface + { + $this->keys[$name] = [$table, $key]; + return $this; + } + + /** + * Adds a unique key + * + * @param *string $name Name of key + * @param *array $fields List of key fields + * + * @return QueryInterface + */ + public function addUniqueKey(string $name, array $fields): QueryInterface + { + $this->uniqueKeys[$name] = $fields; + return $this; + } + + /** + * Returns the string version of the query + * + * @return string + */ + public function getQuery(): string + { + $table = '"'.$this->name.'"'; + + $fields = []; + foreach ($this->fields as $name => $attr) { + $field = ['"'.$name.'"']; + if (isset($attr['type'])) { + $field[] = isset($attr['length']) ? + $attr['type'] . '('.$attr['length'].')' : + $attr['type']; + } + + if (isset($attr['primary'])) { + $field[] = 'PRIMARY KEY'; + } + + if (isset($attr['attribute'])) { + $field[] = $attr['attribute']; + } + + if (isset($attr['null'])) { + if ($attr['null'] == false) { + $field[] = 'NOT NULL'; + } else { + $field[] = 'DEFAULT NULL'; + } + } + + if (isset($attr['default'])&& $attr['default'] !== false) { + if (!isset($attr['null']) || $attr['null'] == false) { + if (is_string($attr['default'])) { + $field[] = 'DEFAULT \''.$attr['default'] . '\''; + } else if (is_numeric($attr['default'])) { + $field[] = 'DEFAULT '.$attr['default']; + } + } + } + + $fields[] = implode(' ', $field); + } + + $fields = !empty($fields) ? implode(', ', $fields) : ''; + + $uniques = []; + foreach ($this->uniqueKeys as $key => $value) { + $uniques[] = 'UNIQUE "'. $key .'" ("'.implode('", "', $value).'")'; + } + + $uniques = !empty($uniques) ? ', ' . implode(", \n", $uniques) : ''; + + $keys = []; + foreach ($this->keys as $key => $value) { + $keys[] = 'FOREIGN KEY "'. $key .'" REFERENCES '.$value[0].'('.$value[1].')'; + } + + $keys = !empty($keys) ? ', ' . implode(", \n", $keys) : ''; + + return sprintf( + 'CREATE TABLE %s (%s%s%s);', + $table, + $fields, + $uniques, + $keys + ); + } + + /** + * Sets comments + * + * @param *string $comments Table comments + * + * @return QueryInterface + */ + public function setComments(string $comments): QueryInterface + { + $this->comments = $comments; + return $this; + } + + /** + * Sets a list of fields to the table + * + * @param *array $fields List of fields + * + * @return QueryInterface + */ + public function setFields(array $fields): QueryInterface + { + $this->fields = $fields; + return $this; + } + + /** + * Sets a list of keys to the table + * + * @param *array $keys A list of foreign keys + * + * @return QueryInterface + */ + public function setForiegnKeys(array $keys): QueryInterface + { + $this->keys = $keys; + return $this; + } + + /** + * Sets a list of unique keys to the table + * + * @param *array $uniqueKeys List of unique keys + * + * @return QueryInterface + */ + public function setUniqueKeys(array $uniqueKeys): QueryInterface + { + $this->uniqueKeys = $uniqueKeys; + return $this; + } +} diff --git a/src/Query/Sqlite/Utility.php b/src/Query/Sqlite/Utility.php new file mode 100644 index 0000000..ef5fc05 --- /dev/null +++ b/src/Query/Sqlite/Utility.php @@ -0,0 +1,103 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Query\Sqlite; + +use Cradle\Storm\Query\QueryInterface; +use Cradle\Storm\Query\AbstractQuery; + +/** + * Generates utility query strings + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +class Utility extends AbstractQuery implements QueryInterface +{ + /** + * @var ?string $query The query string + */ + protected $query = null; + + /** + * Query for dropping a table + * + * @param *string $table The name of the table + * + * @return QueryInterface + */ + public function dropTable(string $table): QueryInterface + { + $this->query = sprintf('DROP TABLE "%s"', $table); + return $this; + } + + /** + * Returns the string version of the query + * + * @return string + */ + public function getQuery(): string + { + return sprintf('%s;', $this->query); + } + + /** + * Query for renaming a table + * + * @param *string $table The name of the table + * @param *string $name The new name of the table + * + * @return QueryInterface + */ + public function renameTable(string $table, string $name): QueryInterface + { + $this->query = sprintf('RENAME TABLE "%s" TO "%s"', $table, $name); + return $this; + } + + /** + * Query for showing all columns of a table + * + * @param *string $table The name of the table + * + * @return QueryInterface + */ + public function showColumns(string $table): QueryInterface + { + $this->query = sprintf('PRAGMA table_info(%s)', $table); + return $this; + } + + /** + * Query for showing all tables + * + * @return QueryInterface + */ + public function showTables(): QueryInterface + { + $this->query = "SELECT * FROM sqlite_master WHERE type='table'"; + return $this; + } + + /** + * Query for truncating a table + * + * @param *string $table The name of the table + * + * @return QueryInterface + */ + public function truncate(string $table): QueryInterface + { + $this->query = sprintf('TRUNCATE "%s"', $table); + return $this; + } +} diff --git a/src/Query/Update.php b/src/Query/Update.php new file mode 100644 index 0000000..0546ab0 --- /dev/null +++ b/src/Query/Update.php @@ -0,0 +1,80 @@ + +/** + * This file is part of the Cradle PHP Library. + * (c) 2016-2018 Openovate Labs + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +namespace Cradle\Storm\Query; + +/** + * Generates update query string syntax + * + * @vendor Cradle + * @package Storm + * @author Christian Blanquera + * @standard PSR-2 + */ +class Update extends Delete implements QueryInterface +{ + /** + * @var array $set List of key/values + */ + protected $set = []; + + /** + * Returns the string version of the query + * + * @return string + */ + public function getQuery(): string + { + $joins = ''; + if (!empty($this->joins)) { + $joins = implode(' ', $this->joins); + } + + $where = ''; + if (!empty($this->joins)) { + $where = sprintf('WHERE %s', implode(' AND ', $this->where)); + } + + $set = []; + foreach ($this->set as $key => $value) { + $set[] = "{$key} = {$value}"; + } + + $query = sprintf( + 'UPDATE %s %s SET %s %s;', + $this->table, + $joins, + implode(', ', $set), + $where + ); + + return str_replace(' ', ' ', $query); + } + + /** + * Set clause that assigns a given field name to a given value. + * + * @param *string $key The column name + * @param ?scalar $value The column value + * + * @return QueryInterface + */ + public function set(string $key, $value): QueryInterface + { + if (is_null($value)) { + $value = 'null'; + } else if (is_bool($value)) { + $value = $value ? 1 : 0; + } + + $this->set[$key] = $value; + + return $this; + } +} diff --git a/src/QueryDelete.php b/src/QueryDelete.php deleted file mode 100644 index e331f03..0000000 --- a/src/QueryDelete.php +++ /dev/null @@ -1,86 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm; - -/** - * Generates delete query string syntax - * - * @vendor Cradle - * @package Sql - * @author Christian Blanquera - * @standard PSR-2 - */ -class QueryDelete extends AbstractQuery -{ - /** - * @var array $table Table name - */ - protected $table = null; - - /** - * @var array $where List of filters - */ - protected $where = []; - - /** - * Construct: Set the table, if any - * - * @param string|null $table The initial name of the table - */ - public function __construct($table = null) - { - if (is_string($table)) { - $this->setTable($table); - } - } - - /** - * Returns the string version of the query - * - * @return string - */ - public function getQuery() - { - return 'DELETE FROM ' - .$this->table.' WHERE ' - .implode(' AND ', $this->where).';'; - } - - /** - * Set the table name in which you want to delete from - * - * @param string|null $table The initial name of the table - * - * @return Delete - */ - public function setTable($table) - { - $this->table = $table; - return $this; - } - - /** - * Where clause - * - * @param array|string $where The where clause - * - * @return Delete - */ - public function where($where) - { - if (is_string($where)) { - $where = [$where]; - } - - $this->where = array_merge($this->where, $where); - - return $this; - } -} diff --git a/src/QueryInsert.php b/src/QueryInsert.php deleted file mode 100644 index 3e2641b..0000000 --- a/src/QueryInsert.php +++ /dev/null @@ -1,100 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm; - -/** - * Generates insert query string syntax - * - * @vendor Cradle - * @package Sql - * @author Christian Blanquera cblanquera@openovate.com - * @standard PSR-2 - */ -class QueryInsert extends AbstractQuery -{ - /** - * @var array $setKey List of keys - */ - protected $setKey = []; - - /** - * @var array $setVal List of values - */ - protected $setVal = []; - - /** - * Set the table, if any - * - * @param string|null $table Table name - */ - public function __construct($table = null) - { - if (is_string($table)) { - $this->setTable($table); - } - } - - - /** - * Returns the string version of the query - * - * @return string - */ - public function getQuery() - { - $multiValList = []; - foreach ($this->setVal as $val) { - $multiValList[] = '('.implode(', ', $val).')'; - } - - return 'INSERT INTO ' - . $this->table . ' ('.implode(', ', $this->setKey) - . ') VALUES ' . implode(", \n", $multiValList).';'; - } - - /** - * Set clause that assigns a given field name to a given value. - * You can also use this to add multiple rows in one call - * - * @param *string $key The column name - * @param *scalar|null $value The column value - * @param int $index For what row is this for? - * - * @return Insert - */ - public function set($key, $value, $index = 0) - { - if (!in_array($key, $this->setKey)) { - $this->setKey[] = $key; - } - - if (is_null($value)) { - $value = 'null'; - } else if (is_bool($value)) { - $value = $value ? 1 : 0; - } - - $this->setVal[$index][] = $value; - return $this; - } - - /** - * Set the table name in which you want to delete from - * - * @param string $table The table name - * - * @return Insert - */ - public function setTable($table) - { - $this->table = $table; - return $this; - } -} diff --git a/src/QuerySelect.php b/src/QuerySelect.php deleted file mode 100644 index b63618d..0000000 --- a/src/QuerySelect.php +++ /dev/null @@ -1,291 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm; - -/** - * Generates select query string syntax - * - * @vendor Cradle - * @package Sql - * @author Christian Blanquera - * @standard PSR-2 - */ -class QuerySelect extends AbstractQuery -{ - /** - * @var string|null $select List of columns - */ - protected $select = null; - - /** - * @var string|null $from Main table - */ - protected $from = null; - - /** - * @var array|null $joins List of relatoinal joins - */ - protected $joins = null; - - /** - * @var array $where List of filters - */ - protected $where = []; - - /** - * @var array $sortBy List of order and directions - */ - protected $sortBy = []; - - /** - * @var array $group List of "group bys" - */ - protected $group = []; - - /** - * @var int|null $page Pagination start - */ - protected $page = null; - - /** - * @var int|null $length Pagination range - */ - protected $length = null; - - /** - * Construct: Set the columns, if any - * - * @param string|null $select Column names - */ - public function __construct($select = '*') - { - $this->select($select); - } - - /** - * From clause - * - * @param *string $from Main table - * - * @return Select - */ - public function from($from) - { - $this->from = $from; - return $this; - } - - /** - * Returns the string version of the query - * - * @return string - */ - public function getQuery() - { - $joins = empty($this->joins) ? '' : implode(' ', $this->joins); - $where = empty($this->where) ? '' : 'WHERE '.implode(' AND ', $this->where); - $sort = empty($this->sortBy) ? '' : 'ORDER BY '.implode(', ', $this->sortBy); - $limit = is_null($this->page) ? '' : 'LIMIT ' . $this->page .',' .$this->length; - $group = empty($this->group) ? '' : 'GROUP BY ' . implode(', ', $this->group); - $having = empty($this->having) ? '' : 'HAVING ' . implode(', ', $this->having); - - $query = sprintf( - 'SELECT %s FROM %s %s %s %s %s %s %s;', - $this->select, - $this->from, - $joins, - $where, - $group, - $having, - $sort, - $limit - ); - - return str_replace(' ', ' ', $query); - } - - /** - * Group by clause - * - * @param *string|array $group List of "group bys" - * - * @return Select - */ - public function groupBy($group) - { - if (is_string($group)) { - $group = [$group]; - } - - $this->group = $group; - return $this; - } - - /** - * Having clause - * - * @param string $having Column name - * - * @return Search - */ - public function having($having) - { - if (is_string($having)) { - $having = [$having]; - } - - $this->having = $having; - return $this; - } - - /** - * Inner join clause - * - * @param *string $table Table name to join - * @param *string $where Filter/s - * @param bool $using Whether to use "using" syntax (as opposed to "on") - * - * @return Select - */ - public function innerJoin($table, $where, $using = true) - { - return $this->join('INNER', $table, $where, $using); - } - - /** - * Allows you to add joins of different types - * to the query - * - * @param *string $type Join type - * @param *string $table Table name to join - * @param *string $where Filter/s - * @param bool $using Whether to use "using" syntax (as opposed to "on") - * - * @return Select - */ - public function join($type, $table, $where, $using = true) - { - $linkage = $using ? 'USING ('.$where.')' : ' ON ('.$where.')'; - $this->joins[] = $type.' JOIN ' . $table . ' ' . $linkage; - - return $this; - } - - /** - * Left join clause - * - * @param *string $table Table name to join - * @param *string $where Filter/s - * @param bool $using Whether to use "using" syntax (as opposed to "on") - * - * @return Select - */ - public function leftJoin($table, $where, $using = true) - { - return $this->join('LEFT', $table, $where, $using); - } - - /** - * Limit clause - * - * @param *string|int $page Pagination start - * @param *string|int $length Pagination range - * - * @return Select - */ - public function limit($page, $length) - { - $this->page = $page; - $this->length = $length; - - return $this; - } - - /** - * Outer join clause - * - * @param *string $table Table name to join - * @param *string $where Filter/s - * @param bool $using Whether to use "using" syntax (as opposed to "on") - * - * @return Select - */ - public function outerJoin($table, $where, $using = true) - { - return $this->join('OUTER', $table, $where, $using); - } - - /** - * Right join clause - * - * @param *string $table Table name to join - * @param *string $where Filter/s - * @param bool $using Whether to use "using" syntax (as opposed to "on") - * - * @return Select - */ - public function rightJoin($table, $where, $using = true) - { - return $this->join('RIGHT', $table, $where, $using); - } - - /** - * Select clause - * - * @param string $select Select columns - * - * @return Select - */ - public function select($select = '*') - { - //if select is an array - if (is_array($select)) { - //transform into a string - $select = implode(', ', $select); - } - - $this->select = $select; - - return $this; - } - - /** - * Order by clause - * - * @param *string $field Column name - * @param string $order Direction - * - * @return Select - */ - public function sortBy($field, $order = 'ASC') - { - $this->sortBy[] = $field . ' ' . $order; - - return $this; - } - - /** - * Where clause - * - * @param array|string Filter/s - * - * @return Select - */ - public function where($where) - { - if (is_string($where)) { - $where = [$where]; - } - - $this->where = array_merge($this->where, $where); - - return $this; - } -} diff --git a/src/QueryUpdate.php b/src/QueryUpdate.php deleted file mode 100644 index 974eb61..0000000 --- a/src/QueryUpdate.php +++ /dev/null @@ -1,64 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm; - -/** - * Generates update query string syntax - * - * @vendor Cradle - * @package Sql - * @author Christian Blanquera - * @standard PSR-2 - */ -class QueryUpdate extends QueryDelete -{ - /** - * @var array $set List of key/values - */ - protected $set = []; - - /** - * Returns the string version of the query - * - * @return string - */ - public function getQuery() - { - $set = []; - foreach ($this->set as $key => $value) { - $set[] = "{$key} = {$value}"; - } - - return 'UPDATE '. $this->table - . ' SET ' . implode(', ', $set) - . ' WHERE '. implode(' AND ', $this->where).';'; - } - - /** - * Set clause that assigns a given field name to a given value. - * - * @param *string $key The column name - * @param *scalar|null $value The column value - * - * @return Update - */ - public function set($key, $value) - { - if (is_null($value)) { - $value = 'null'; - } else if (is_bool($value)) { - $value = $value ? 1 : 0; - } - - $this->set[$key] = $value; - - return $this; - } -} diff --git a/src/Search.php b/src/Search.php deleted file mode 100644 index 6e32906..0000000 --- a/src/Search.php +++ /dev/null @@ -1,658 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm; - -use Cradle\Event\EventTrait; - -use Cradle\Helper\InstanceTrait; -use Cradle\Helper\LoopTrait; -use Cradle\Helper\ConditionalTrait; - -use Cradle\Profiler\InspectorTrait; -use Cradle\Profiler\LoggerTrait; - -use Cradle\Resolver\StateTrait; -use Cradle\Resolver\ResolverException; - -/** - * Sql Search - * - * @vendor Cradle - * @package Sql - * @author Christian Blanquera - * @standard PSR-2 - */ -class Search -{ - use EventTrait, - InstanceTrait, - LoopTrait, - ConditionalTrait, - InspectorTrait, - LoggerTrait, - StateTrait; - - /** - * @const string LEFT Join type - */ - const LEFT = 'LEFT'; - - /** - * @const string RIGHT Join type - */ - const RIGHT = 'RIGHT'; - - /** - * @const string INNER Join type - */ - const INNER = 'INNER'; - - /** - * @const string OUTER Join type - */ - const OUTER = 'OUTER'; - - /** - * @const string ASC Sort direction - */ - const ASC = 'ASC'; - - /** - * @const string DESC Sort direction - */ - const DESC = 'DESC'; - - /** - * @var SqlInterface|null $database Database object - */ - protected $database = null; - - /** - * @var string|null $table Table name - */ - protected $table = null; - - /** - * @var array $columns List of columns - */ - protected $columns = []; - - /** - * @var array $join List of relational joins - */ - protected $join = []; - - /** - * @var array $filter List of filters - */ - protected $filter = []; - - /** - * @var array $sort List of orders and directions - */ - protected $sort = []; - - /** - * @var array $group List of "group bys" - */ - protected $group = []; - - /** - * @var array $start Pagination start - */ - protected $start = 0; - - /** - * @var array $range Pagination range - */ - protected $range = 0; - - /** - * Magical processing of sortBy - * and filterBy Methods - * - * @param *string $name Name of method - * @param *array $args Arguments to pass - * - * @return mixed - */ - public function __call($name, $args) - { - //if method starts with filterBy - if (strpos($name, 'filterBy') === 0) { - //ex. filterByUserName('Chris', '-') - //choose separator - $separator = '_'; - if (isset($args[1]) && is_scalar($args[1])) { - $separator = (string) $args[1]; - } - - //transform method to column name - $key = substr($name, 8); - $key = preg_replace("/([A-Z0-9])/", $separator."$1", $key); - $key = substr($key, strlen($separator)); - $key = strtolower($key); - - //if arg isn't set - if (!isset($args[0])) { - //default is null - $args[0] = null; - } - - //generate key - if (is_array($args[0])) { - $key = $key.' IN %s'; - } else { - $key = $key.'=%s'; - } - - //add it to the search filter - $this->addFilter($key, $args[0]); - - return $this; - } - - //if method starts with sortBy - if (strpos($name, 'sortBy') === 0) { - //ex. sortByUserName('Chris', '-') - //determine separator - $separator = '_'; - if (isset($args[1]) && is_scalar($args[1])) { - $separator = (string) $args[1]; - } - - //transform method to column name - $key = substr($name, 6); - $key = preg_replace("/([A-Z0-9])/", $separator."$1", $key); - $key = substr($key, strlen($separator)); - $key = strtolower($key); - - //if arg isn't set - if (!isset($args[0])) { - //default is null - $args[0] = null; - } - - //add it to the search sort - $this->addSort($key, $args[0]); - - return $this; - } - - try { - return $this->__callResolver($name, $args); - } catch (ResolverException $e) { - throw new SqlException($e->getMessage()); - } - } - - /** - * Construct: Store database - * - * @param SqlInterface $database Database object - */ - public function __construct(SqlInterface $database) - { - $this->database = $database; - } - - /** - * Adds filter - * - * @param *string sprintf format - * @param string[,string..] sprintf values - * - * @return Search - */ - public function addFilter() - { - $this->filter[] = func_get_args(); - - return $this; - } - - /** - * Adds sort - * - * @param *string $column Column name - * @param string $order ASC or DESC - * - * @return Search - */ - public function addSort($column, $order = self::ASC) - { - if ($order != self::DESC) { - $order = self::ASC; - } - - $this->sort[$column] = $order; - - return $this; - } - - /** - * Returns the results in a collection - * - * @return Collection - */ - public function getCollection() - { - return $this->resolve(Collection::class) - ->setDatabase($this->database) - ->setTable($this->table) - ->set($this->getRows()); - } - - /** - * Returns the one result in a model - * - * @param int $index Row index to return - * - * @return Model - */ - public function getModel($index = 0) - { - return $this->getCollection()->offsetGet($index); - } - - /** - * Builds query based on the data given - * - * @return string - */ - public function getQuery() - { - $query = $this->database->getSelectQuery()->from($this->table); - - foreach ($this->join as $join) { - if (!is_array($join[2])) { - $join[2] = [$join[2]]; - } - - $where = array_shift($join[2]); - if (!empty($join[2])) { - foreach ($join[2] as $i => $value) { - $join[2][$i] = $this->database->bind($value); - } - - $where = vsprintf($where, $join[2]); - } - - $query->join($join[0], $join[1], $where, $join[3]); - } - - foreach ($this->filter as $i => $filter) { - //array('post_id=%s AND post_title IN %s', 123, array('asd')); - $where = array_shift($filter); - if (!empty($filter)) { - foreach ($filter as $i => $value) { - $filter[$i] = $this->database->bind($value); - } - - $where = vsprintf($where, $filter); - } - - $query->where($where); - } - - return $query; - } - - /** - * Returns the one result - * - * @param int $index Row index to return - * @param string|null $column Specific column to return - * - * @return array|null - */ - public function getRow($index = 0, $column = null) - { - if (is_string($index)) { - $column = $index; - $index = 0; - } - - $rows = $this->getRows(); - - if (!is_null($column) && isset($rows[$index][$column])) { - return $rows[$index][$column]; - } else if (is_null($column) && isset($rows[$index])) { - return $rows[$index]; - } - - return null; - } - - /** - * Returns the array rows - * - * @param callable|null $callback - * - * @return array - */ - public function getRows($callback = null) - { - $query = $this->getQuery(); - - if (!empty($this->columns)) { - $query->select(implode(', ', $this->columns)); - } - - foreach ($this->sort as $key => $value) { - $query->sortBy($key, $value); - } - - if ($this->range) { - $query->limit($this->start, $this->range); - } - - if (!empty($this->group)) { - $query->groupBy($this->group); - } - - if (!empty($this->having)) { - $query->having($this->having); - } - - $rows = $this->database->query($query, $this->database->getBinds(), $callback); - - if (!$callback) { - return $rows; - } - - return $this; - } - - /** - * Returns the total results - * - * @return int - */ - public function getTotal() - { - $query = $this->getQuery()->select('COUNT(*) as total'); - - $rows = $this->database->query($query, $this->database->getBinds()); - - if (!isset($rows[0]['total'])) { - return 0; - } - - return $rows[0]['total']; - } - - /** - * Group by clause - * - * @param string $group Column name - * - * @return Search - */ - public function groupBy($group) - { - if (is_string($group)) { - $group = [$group]; - } - - $this->group = $group; - return $this; - } - - /** - * Having clause - * - * @param string $having Column name - * - * @return Search - */ - public function having($having) - { - if (is_string($having)) { - $having = [$having]; - } - - $this->having = $having; - return $this; - } - - /** - * Adds Inner Join On - * - * @param *string $table Table name - * @param *string[,string..] $where Filter/s - * - * @return Search - */ - public function innerJoinOn($table, $where) - { - $where = func_get_args(); - $table = array_shift($where); - - $this->join[] = [self::INNER, $table, $where, false]; - - return $this; - } - - /** - * Adds Inner Join Using - * - * @param *string $table Table name - * @param *string[,string..] $where Filter/s - * - * @return Search - */ - public function innerJoinUsing($table, $where) - { - $where = func_get_args(); - $table = array_shift($where); - - $this->join[] = [self::INNER, $table, $where, true]; - - return $this; - } - - /** - * Adds Left Join On - * - * @param *string $table Table name - * @param *string[,string..] $where Filter/s - * - * @return Search - */ - public function leftJoinOn($table, $where) - { - $where = func_get_args(); - $table = array_shift($where); - - $this->join[] = [self::LEFT, $table, $where, false]; - - return $this; - } - - /** - * Adds Left Join Using - * - * @param *string $table Table name - * @param *string[,string..] $where Filter/s - * - * @return Search - */ - public function leftJoinUsing($table, $where) - { - $where = func_get_args(); - $table = array_shift($where); - - $this->join[] = [self::LEFT, $table, $where, true]; - - return $this; - } - - /** - * Adds Outer Join On - * - * @param *string $table Table name - * @param *string[,string..] $where Filter/s - * - * @return Search - */ - public function outerJoinOn($table, $where) - { - $where = func_get_args(); - $table = array_shift($where); - - $this->join[] = [self::OUTER, $table, $where, false]; - - return $this; - } - - /** - * Adds Outer Join USing - * - * @param *string $table Table name - * @param *string[,string..] $where Filter/s - * - * @return Search - */ - public function outerJoinUsing($table, $where) - { - $where = func_get_args(); - $table = array_shift($where); - - $this->join[] = [self::OUTER, $table, $where, true]; - - return $this; - } - - /** - * Adds Right Join On - * - * @param *string $table Table name - * @param *string[,string..] $where Filter/s - * - * @return Search - */ - public function rightJoinOn($table, $where) - { - $where = func_get_args(); - $table = array_shift($where); - - $this->join[] = [self::RIGHT, $table, $where, false]; - - return $this; - } - - /** - * Adds Right Join Using - * - * @param *string $table Table name - * @param *string[,string..] $where Filter/s - * - * @return Search - */ - public function rightJoinUsing($table, $where) - { - $where = func_get_args(); - $table = array_shift($where); - - $this->join[] = [self::RIGHT, $table, $where, true]; - - return $this; - } - - /** - * Sets Columns - * - * @param string[,string..]|array $columns List of table columns - * - * @return Search - */ - public function setColumns($columns) - { - if (!is_array($columns)) { - $columns = func_get_args(); - } - - $this->columns = $columns; - - return $this; - } - - /** - * Sets the pagination page - * - * @param int $page Pagination page - * - * @return Search - */ - public function setPage($page) - { - if ($page < 1) { - $page = 1; - } - - if ($this->range == 0) { - $this->setRange(25); - } - - $this->start = ($page - 1) * $this->range; - - return $this; - } - - /** - * Sets the pagination range - * - * @param int $range Pagination range - * - * @return Search - */ - public function setRange($range) - { - if ($range < 0) { - $range = 25; - } - - $this->range = $range; - - return $this; - } - - /** - * Sets the pagination start - * - * @param int $start Pagination start - * - * @return Search - */ - public function setStart($start) - { - if ($start < 0) { - $start = 0; - } - - $this->start = $start; - - return $this; - } - - /** - * Sets Table - * - * @param string $table Table class name - * - * @return Search - */ - public function setTable($table) - { - $this->table = $table; - return $this; - } -} diff --git a/src/SqlException.php b/src/SqlException.php index df92d6d..c87d2d1 100644 --- a/src/SqlException.php +++ b/src/SqlException.php @@ -14,75 +14,75 @@ /** * SQL exceptions * - * @package Cradle + * @package Storm * @category Sql * @author Christian Blanquera * @standard PSR-2 */ class SqlException extends Exception { - /** - * @const string QUERY_ERROR Error template - */ - const QUERY_ERROR = '%s Query: %s'; + /** + * @const string QUERY_ERROR Error template + */ + const QUERY_ERROR = '%s Query: %s'; - /** - * @const string TABLE_NOT_SET Error template - */ - const TABLE_NOT_SET = 'No default table set or was passed.'; + /** + * @const string TABLE_NOT_SET Error template + */ + const TABLE_NOT_SET = 'No default table set or was passed.'; - /** - * @const string DATABASE_NOT_SET Error template - */ - const DATABASE_NOT_SET = 'No default database set or was passed.'; + /** + * @const string DATABASE_NOT_SET Error template + */ + const DATABASE_NOT_SET = 'No default database set or was passed.'; - /** - * @const string UNKNOWN_PDO Error template - */ - const UNKNOWN_PDO = 'Could not match an SQL handler with %s'; + /** + * @const string UNKNOWN_PDO Error template + */ + const UNKNOWN_PDO = 'Could not match an SQL handler with %s'; - /** - * Create a new exception for query errors - * - * @param *string $query - * @param *string $error - * - * @return SqlException - */ - public static function forQueryError($query, $error) - { - return new static(sprintf(static::QUERY_ERROR, $query, $error)); - } + /** + * Create a new exception for query errors + * + * @param *string $query + * @param *string $error + * + * @return SqlException + */ + public static function forQueryError($query, $error) + { + return new static(sprintf(static::QUERY_ERROR, $query, $error)); + } - /** - * Create a new exception for table not set - * - * @return SqlException - */ - public static function forTableNotSet() - { - return new static(static::TABLE_NOT_SET); - } + /** + * Create a new exception for table not set + * + * @return SqlException + */ + public static function forTableNotSet() + { + return new static(static::TABLE_NOT_SET); + } - /** - * Create a new exception for database not set - * - * @return SqlException - */ - public static function forDatabaseNotSet() - { - return new static(static::DATABASE_NOT_SET); - } + /** + * Create a new exception for database not set + * + * @return SqlException + */ + public static function forDatabaseNotSet() + { + return new static(static::DATABASE_NOT_SET); + } - /** - * Create a new exception for unknown PDO - * - * @param *string $name - * - * @return SqlException - */ - public static function forUnknownPDO($name) - { - return new static(sprintf(static::UNKNOWN_PDO, $name)); - } + /** + * Create a new exception for unknown PDO + * + * @param *string $name + * + * @return SqlException + */ + public static function forUnknownPDO($name) + { + return new static(sprintf(static::UNKNOWN_PDO, $name)); + } } diff --git a/src/SqlFactory.php b/src/SqlFactory.php index d860d5c..5544b83 100644 --- a/src/SqlFactory.php +++ b/src/SqlFactory.php @@ -11,29 +11,33 @@ use PDO; +use Cradle\Storm\Engine\MySql; +use Cradle\Storm\Engine\PostGreSql; +use Cradle\Storm\Engine\Sqlite; + /** * Auto loads up the right handler given the PDO connection * - * @package Cradle + * @package Storm * @category Sql * @author Christian Blanquera * @standard PSR-2 */ class SqlFactory { - public static function load(PDO $connection) - { - $name = $connection->getAttribute(PDO::ATTR_DRIVER_NAME); - - switch ($name) { - case 'mysql': - return MySql::loadPDO($connection); - case 'pgsql': - return PostGreSql::loadPDO($connection); - case 'sqlite': - return Sqlite::loadPDO($connection); - default: - throw SqlException::forUnknownPDO($name); - } + public static function load(PDO $connection) + { + $name = $connection->getAttribute(PDO::ATTR_DRIVER_NAME); + + switch ($name) { + case 'mysql': + return MySql::loadPDO($connection); + case 'pgsql': + return PostGreSql::loadPDO($connection); + case 'sqlite': + return Sqlite::loadPDO($connection); + default: + throw SqlException::forUnknownPDO($name); } + } } diff --git a/src/SqlInterface.php b/src/SqlInterface.php deleted file mode 100644 index 3beaa20..0000000 --- a/src/SqlInterface.php +++ /dev/null @@ -1,170 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm; - -/** - * Abstractly defines a layout of available methods to - * connect to and query a database. This class also lays out - * query building methods that auto renders a valid query - * the specific database will understand without actually - * needing to know the query language. - * - * @vendor Cradle - * @package Sql - * @author Christian Blanquera - * @standard PSR-2 - */ -interface SqlInterface -{ - - /** - * Connects to the database - * - * @param PDO|array $options The connection options - * - * @return SqlInterface - */ - public function connect($options = []); - - /** - * Binds a value and returns the bound key - * - * @param *string|array|number|null $value What to bind - * - * @return string - */ - public function bind($value); - - /** - * Returns collection - * - * @param array $data Initial collection data - * - * @return Collection - */ - public function collection(array $data = []); - - /** - * Removes rows that match a filter - * - * @param *string|null $table The table name - * @param array|string $filters Filters to test against - * - * @return SqlInterface - */ - public function deleteRows($table, $filters = null); - - /** - * Returns all the bound values of this query - * - * @return array - */ - public function getBinds(); - - /** - * Returns the connection object - * if no connection has been made - * it will attempt to make it - * - * @return resource PDO connection resource - */ - public function getConnection(); - - /** - * Returns the last inserted id - * - * @param string|null $column A particular column name - * - * @return int the id - */ - public function getLastInsertedId($column = null); - - /** - * Returns a 1 row result given the column name and the value - * - * @param *string $table Table name - * @param *string $name Column name - * @param *scalar|null $value Column value - * - * @return array|null - */ - public function getRow($table, $name, $value); - - /** - * Inserts data into a table and returns the ID - * - * @param *string $table Table name - * @param *array $setting Key/value array matching table columns - * @param bool|array $bind Whether to compute with binded variables - * - * @return SqlInterface - */ - public function insertRow($table, array $settings, $bind = true); - - /** - * Inserts multiple rows into a table - * - * @param *string $table Table name - * @param array $setting Key/value 2D array matching table columns - * @param bool|array $bind Whether to compute with binded variables - * - * @return SqlInterface - */ - public function insertRows($table, array $settings, $bind = true); - - /** - * Returns model - * - * @param array $data The initial data to set - * - * @return Model - */ - public function model(array $data = []); - - /** - * Queries the database - * - * @param *string $query The query to ran - * @param array $binds List of binded values - * - * @return array - */ - public function query($query, array $binds = []); - - /** - * Returns search - * - * @param string|null $table Table name - * - * @return Search - */ - public function search($table = null); - - /** - * Sets all the bound values of this query - * - * @param *array $binds key/values to bind - * - * @return SqlInterface - */ - public function setBinds(array $binds); - - /** - * Updates rows that match a filter given the update settings - * - * @param *string $table Table name - * @param *array $setting Key/value array matching table columns - * @param array|string $filters Filters to test against - * @param bool|array $bind Whether to compute with binded variables - * - * @return SqlInterface - */ - public function updateRows($table, array $settings, $filters = null, $bind = true); -} diff --git a/src/Sqlite.php b/src/Sqlite.php deleted file mode 100644 index 21f5994..0000000 --- a/src/Sqlite.php +++ /dev/null @@ -1,198 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm; - -use PDO; - -use Cradle\Storm\Sqlite\QueryAlter; -use Cradle\Storm\Sqlite\QueryCreate; -use Cradle\Storm\Sqlite\QueryUtility; - -/** - * Abstractly defines a layout of available methods to - * connect to and query a Sqlite database. This class also - * lays out query building methods that auto renders a - * valid query the specific database will understand without - * actually needing to know the query language. Extending - * all Sql classes, comes coupled with loosely defined - * searching, collections and models. - * - * @vendor Cradle - * @package Sqlite - * @author Christian Blanquera - * @standard PSR-2 - */ -class Sqlite extends AbstractSql implements SqlInterface -{ - /** - * @var string $path Sqlite file path - */ - protected $path = null; - - /** - * Construct: Store connection information - * - * @param *string $path Sqlite file path - */ - public function __construct($path) - { - $this->path = $path; - } - - /** - * Connects to the database - * - * @param PDO|array $options The connection options - * - * @return Sqlite - */ - public function connect($options = []) - { - if ($options instanceof PDO) { - $this->connection = $options; - return $this; - } - - if (!is_array($options)) { - $options = array(); - } - - $this->connection = new PDO('sqlite:'.$this->path); - $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - - return $this; - } - - /** - * Returns the alter query builder - * - * @param *string $name Name of table - * - * @return QueryAlter - */ - public function getAlterQuery($name = null) - { - return $this->resolve(QueryAlter::class, $name); - } - - /** - * Returns the columns and attributes given the table name - * - * @param *string $table The name of the table - * - * @return array|false - */ - public function getColumns($table) - { - $query = $this->getUtilityQuery()->showColumns($table); - - $results = $this->query($query, $this->getBinds()); - - $columns = []; - foreach ($results as $column) { - $key = null; - if ($column['pk'] == 1) { - $key = 'PRI'; - } - - $columns[] = [ - 'Field' => $column['name'], - 'Type' => $column['type'], - 'Default' => $column['dflt_value'], - 'Null' => $column['notnull'] != 1, - 'Key' => $key - ]; - } - - return $columns; - } - - /** - * Returns the create query builder - * - * @param *string $name Name of table - * - * @return QueryCreate - */ - public function getCreateQuery($name = null) - { - return $this->resolve(QueryCreate::class, $name); - } - - /** - * Peturns the primary key name given the table - * - * @param *string $table The table name - * - * @return string - */ - public function getPrimaryKey($table) - { - $results = $this->getColumns($table, "`Key` = 'PRI'"); - return isset($results[0]['Field']) ? $results[0]['Field'] : null; - } - - /** - * Returns a listing of tables in the DB - * - * @param string|null $like The like pattern - * - * @return array|false - */ - public function getTables($like = null) - { - $query = $this->getUtilityQuery(); - $like = $like ? $this->bind($like) : null; - return $this->query($query->showTables($like), $this->getBinds()); - } - - /** - * Inserts multiple rows into a table - * - * @param *string $table Table name - * @param array $setting Key/value 2D array matching table columns - * @param bool|array $bind Whether to compute with binded variables - * - * @return Sqlite - */ - public function insertRows($table, array $settings, $bind = true) - { - //this is an array of arrays - foreach ($settings as $index => $setting) { - //Sqlite no available multi insert - //there's work arounds, but no performance gain - $this->insertRow($table, $setting, $bind); - } - - return $this; - } - - /** - * Returns the select query builder - * - * @param string|array $select Column list - * - * @return Sqlite - */ - public function getSelectQuery($select = 'ROWID,*') - { - return parent::getSelectQuery($select); - } - - /** - * Returns the alter query builder - * - * @return QueryUtility - */ - public function getUtilityQuery() - { - return $this->resolve(QueryUtility::class); - } -} diff --git a/src/Sqlite/QueryAlter.php b/src/Sqlite/QueryAlter.php deleted file mode 100644 index 85d4c03..0000000 --- a/src/Sqlite/QueryAlter.php +++ /dev/null @@ -1,304 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm\Sqlite; - -use Cradle\Storm\AbstractQuery; - -/** - * Generates alter query string syntax - * - * @vendor Cradle - * @package Sql - * @author Christian Blanquera - * @standard PSR-2 - */ -class QueryAlter extends AbstractQuery -{ - /** - * @var string|null $name Name of table - */ - protected $name = null; - - /** - * @var array $changeFields List of fields to change - */ - protected $changeFields = []; - - /** - * @var array $addFields List of fields to add - */ - protected $addFields = []; - - /** - * @var array $removeFields List of fields to remove - */ - protected $removeFields = []; - - /** - * @var array $addKeys List of keys to add - */ - protected $addKeys = []; - - /** - * @var array $removeKeys List of keys to remove - */ - protected $removeKeys = []; - - /** - * @var array $addUniqueKeys List of unique keys to add - */ - protected $addUniqueKeys = []; - - /** - * @var array $removeUniqueKeys List of unique keys to remove - */ - protected $removeUniqueKeys = []; - - /** - * @var array $addPrimaryKeys List of primary keys to add - */ - protected $addPrimaryKeys = []; - - /** - * @var array $removePrimaryKeys List of primary keys to remove - */ - protected $removePrimaryKeys = []; - - /** - * Construct: Set the table, if any - * - * @param string|null - */ - public function __construct($name = null) - { - if (is_string($name)) { - $this->setName($name); - } - } - - /** - * Adds a field in the table - * - * @param *string $name Column name - * @param *array $attributes Column attributes - * - * @return QueryAlter - */ - public function addField($name, array $attributes) - { - $this->addFields[$name] = $attributes; - return $this; - } - - /** - * Adds an index key - * - * @param *string $name Name of key - * - * @return QueryAlter - */ - public function addForeignKey($name, $table, $key) - { - $this->addKeys[$name] = [$table, $key]; - return $this; - } - - /** - * Adds a unique key - * - * @param *string $name Name of key - * - * @return QueryAlter - */ - public function addUniqueKey($name) - { - $this->addUniqueKeys[] = '"'.$name.'"'; - return $this; - } - - /** - * Changes attributes of the table given - * the field name - * - * @param *string $name Column name - * @param *array $attributes Column attributes - * - * @return QueryAlter - */ - public function changeField($name, array $attributes) - { - $this->changeFields[$name] = $attributes; - return $this; - } - - /** - * Returns the string version of the query - * - * @param bool - * - * @return string - */ - public function getQuery($unbind = false) - { - $fields = []; - $table = '"'.$this->name.'"'; - - foreach ($this->removeFields as $name) { - $fields[] = 'DROP "'.$name.'"'; - } - - foreach ($this->addFields as $name => $attr) { - $field = ['ADD "'.$name.'"']; - if (isset($attr['type'])) { - $field[] = isset($attr['length']) ? - $attr['type'] . '('.$attr['length'].')' : - $attr['type']; - } - - if (isset($attr['attribute'])) { - $field[] = $attr['attribute']; - } - - if (isset($attr['null'])) { - if ($attr['null'] == false) { - $field[] = 'NOT NULL'; - } else { - $field[] = 'DEFAULT NULL'; - } - } - - if (isset($attr['default'])&& $attr['default'] !== false) { - if (!isset($attr['null']) || $attr['null'] == false) { - if (is_string($attr['default'])) { - $field[] = 'DEFAULT \''.$attr['default'] . '\''; - } else if (is_numeric($attr['default'])) { - $field[] = 'DEFAULT '.$attr['default']; - } - } - } - - $fields[] = implode(' ', $field); - } - - foreach ($this->changeFields as $name => $attr) { - $field = ['CHANGE "'.$name.'" "'.$name.'"']; - - if (isset($attr['name'])) { - $field = ['CHANGE "'.$name.'" "'.$attr['name'].'"']; - } - - if (isset($attr['type'])) { - $field[] = isset($attr['length']) ? - $attr['type'] . '('.$attr['length'].')' : - $attr['type']; - } - - if (isset($attr['attribute'])) { - $field[] = $attr['attribute']; - } - - if (isset($attr['null'])) { - if ($attr['null'] == false) { - $field[] = 'NOT NULL'; - } else { - $field[] = 'DEFAULT NULL'; - } - } - - if (isset($attr['default'])&& $attr['default'] !== false) { - if (!isset($attr['null']) || $attr['null'] == false) { - if (is_string($attr['default'])) { - $field[] = 'DEFAULT \''.$attr['default'] . '\''; - } else if (is_numeric($attr['default'])) { - $field[] = 'DEFAULT '.$attr['default']; - } - } - } - - $fields[] = implode(' ', $field); - } - - foreach ($this->removeKeys as $key) { - $fields[] = 'DROP FOREIGN KEY "'.$key.'"'; - } - - foreach ($this->addKeys as $key => $value) { - $fields[] = 'ADD FOREIGN KEY "'. $key .'" REFERENCES '.$value[0].'('.$value[1].')'; - } - - foreach ($this->removeUniqueKeys as $key) { - $fields[] = 'DROP UNIQUE "'.$key.'"'; - } - - if (!empty($this->addUniqueKeys)) { - $fields[] = 'ADD UNIQUE ('.implode(', ', $this->addUniqueKeys).')'; - } - - $fields = implode(", \n", $fields); - - return sprintf( - 'ALTER TABLE %s %s;', - $table, - $fields - ); - } - - /** - * Removes a field - * - * @param *string $name Name of field - * - * @return QueryAlter - */ - public function removeField($name) - { - $this->removeFields[] = $name; - return $this; - } - - /** - * Removes an index key - * - * @param *string $name Name of key - * - * @return QueryAlter - */ - public function removeForeignKey($name) - { - $this->removeKeys[] = $name; - return $this; - } - - /** - * Removes a unique key - * - * @param *string $name Name of key - * - * @return QueryAlter - */ - public function removeUniqueKey($name) - { - $this->removeUniqueKeys[] = $name; - return $this; - } - - /** - * Sets the name of the table you wish to create - * - * @param *string $name Name of table - * - * @return QueryAlter - */ - public function setName($name) - { - $this->name = $name; - return $this; - } -} diff --git a/src/Sqlite/QueryCreate.php b/src/Sqlite/QueryCreate.php deleted file mode 100644 index bd92287..0000000 --- a/src/Sqlite/QueryCreate.php +++ /dev/null @@ -1,247 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm\Sqlite; - -use Cradle\Storm\AbstractQuery; - -/** - * Generates create table query string syntax - * - * @vendor Cradle - * @package Sql - * @author Christian Blanquera - * @standard PSR-2 - */ -class QueryCreate extends AbstractQuery -{ - /** - * @var string|null $name Name of table - */ - protected $name = null; - - /** - * @var string|null $comments Table comments - */ - protected $comments = null; - - /** - * @var array $fields List of fields - */ - protected $fields = []; - - /** - * @var array $keys List of key indexes - */ - protected $keys = []; - - /** - * @var array $uniqueKeys List of unique keys - */ - protected $uniqueKeys = []; - - /** - * @var array $primaryKeys List of primary keys - */ - protected $primaryKeys = []; - - /** - * Construct: Set the table, if any - * - * @param string|null $name Name of table - */ - public function __construct($name = null) - { - if (is_string($name)) { - $this->setName($name); - } - } - - /** - * Adds a field in the table - * - * @param *string $name Column name - * @param *array $attributes Column attributes - * - * @return QueryCreate - */ - public function addField($name, array $attributes) - { - $this->fields[$name] = $attributes; - return $this; - } - - /** - * Adds an index key - * - * @param *string $name Name of column - * @param *string $table Name of foreign table - * @param *string $key Name of key - * - * @return QueryCreate - */ - public function addForeignKey($name, $table, $key) - { - $this->keys[$name] = [$table, $key]; - return $this; - } - - /** - * Adds a unique key - * - * @param *string $name Name of key - * @param *array $fields List of key fields - * - * @return QueryCreate - */ - public function addUniqueKey($name, array $fields) - { - $this->uniqueKeys[$name] = $fields; - return $this; - } - - /** - * Returns the string version of the query - * - * @param bool $unbind Whether to unbind variables - * - * @return string - */ - public function getQuery($unbind = false) - { - $table = '"'.$this->name.'"'; - - $fields = []; - foreach ($this->fields as $name => $attr) { - $field = ['"'.$name.'"']; - if (isset($attr['type'])) { - $field[] = isset($attr['length']) ? - $attr['type'] . '('.$attr['length'].')' : - $attr['type']; - } - - if (isset($attr['primary'])) { - $field[] = 'PRIMARY KEY'; - } - - if (isset($attr['attribute'])) { - $field[] = $attr['attribute']; - } - - if (isset($attr['null'])) { - if ($attr['null'] == false) { - $field[] = 'NOT NULL'; - } else { - $field[] = 'DEFAULT NULL'; - } - } - - if (isset($attr['default'])&& $attr['default'] !== false) { - if (!isset($attr['null']) || $attr['null'] == false) { - if (is_string($attr['default'])) { - $field[] = 'DEFAULT \''.$attr['default'] . '\''; - } else if (is_numeric($attr['default'])) { - $field[] = 'DEFAULT '.$attr['default']; - } - } - } - - $fields[] = implode(' ', $field); - } - - $fields = !empty($fields) ? implode(', ', $fields) : ''; - - $uniques = []; - foreach ($this->uniqueKeys as $key => $value) { - $uniques[] = 'UNIQUE "'. $key .'" ("'.implode('", "', $value).'")'; - } - - $uniques = !empty($uniques) ? ', ' . implode(", \n", $uniques) : ''; - - $keys = []; - foreach ($this->keys as $key => $value) { - $keys[] = 'FOREIGN KEY "'. $key .'" REFERENCES '.$value[0].'('.$value[1].')'; - } - - $keys = !empty($keys) ? ', ' . implode(", \n", $keys) : ''; - - return sprintf( - 'CREATE TABLE %s (%s%s%s);', - $table, - $fields, - $uniques, - $keys - ); - } - - /** - * Sets comments - * - * @param *string $comments Table comments - * - * @return QueryCreate - */ - public function setComments($comments) - { - $this->comments = $comments; - return $this; - } - - /** - * Sets a list of fields to the table - * - * @param *array $fields List of fields - * - * @return QueryCreate - */ - public function setFields(array $fields) - { - $this->fields = $fields; - return $this; - } - - /** - * Sets a list of keys to the table - * - * @param array $keys A list of foreign keys - * - * @return QueryCreate - */ - public function setForiegnKeys(array $keys) - { - $this->keys = $keys; - return $this; - } - - /** - * Sets the name of the table you wish to create - * - * @param *string $name Table name - * - * @return QueryCreate - */ - public function setName($name) - { - $this->name = $name; - return $this; - } - - /** - * Sets a list of unique keys to the table - * - * @param *array $uniqueKeys List of unique keys - * - * @return QueryCreate - */ - public function setUniqueKeys(array $uniqueKeys) - { - $this->uniqueKeys = $uniqueKeys; - return $this; - } -} diff --git a/src/Sqlite/QueryUtility.php b/src/Sqlite/QueryUtility.php deleted file mode 100644 index 6ee8a8d..0000000 --- a/src/Sqlite/QueryUtility.php +++ /dev/null @@ -1,104 +0,0 @@ - -/** - * This file is part of the Cradle PHP Library. - * (c) 2016-2018 Openovate Labs - * - * Copyright and license information can be found at LICENSE.txt - * distributed with this package. - */ - -namespace Cradle\Storm\Sqlite; - -use Cradle\Storm\AbstractQuery; - -/** - * Generates utility query strings - * - * @vendor Cradle - * @package Sql - * @author Christian Blanquera - * @standard PSR-2 - */ -class QueryUtility extends AbstractQuery -{ - /** - * @var string|null $query The query string - */ - protected $query = null; - - /** - * Query for dropping a table - * - * @param *string $table The name of the table - * - * @return QueryUtility - */ - public function dropTable($table) - { - $this->query = 'DROP TABLE "' . $table .'"'; - return $this; - } - - /** - * Returns the string version of the query - * - * @return string - */ - public function getQuery() - { - return $this->query.';'; - } - - /** - * Query for renaming a table - * - * @param *string $table The name of the table - * @param *string $name The new name of the table - * - * @return QueryUtility - */ - public function renameTable($table, $name) - { - $this->query = 'RENAME TABLE "' . $table . '" TO "' . $name . '"'; - return $this; - } - - /** - * Query for showing all columns of a table - * - * @param *string $table The name of the table - * - * @return QueryUtility - */ - public function showColumns($table) - { - $this->query = 'PRAGMA table_info('.$table.')'; - return $this; - } - - /** - * Query for showing all tables - * - * @param string|null $like The like pattern - * - * @return QueryUtility - */ - public function showTables() - { - $this->query = 'SELECT * FROM sqlite_master WHERE type=\'table\''; - return $this; - } - - /** - * Query for truncating a table - * - * @param *string $table The name of the table - * - * @return QueryUtility - */ - public function truncate($table) - { - $this->query = 'TRUNCATE "' . $table .'"'; - return $this; - } -} diff --git a/test/AbstractQuery.php b/test/AbstractQuery.php deleted file mode 100644 index fd45827..0000000 --- a/test/AbstractQuery.php +++ /dev/null @@ -1,51 +0,0 @@ -object = new AbstractQueryStub; - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\AbstractQuery::__toString - */ - public function test__toString() - { - $this->assertEquals('foobar', $this->object->__toString()); - } -} - -if(!class_exists('Cradle\Storm\AbstractQueryStub')) { - class AbstractQueryStub extends AbstractQuery - { - public function getQuery() - { - return 'foobar'; - } - } -} diff --git a/test/AbstractSql.php b/test/AbstractSql.php deleted file mode 100644 index ea3bd98..0000000 --- a/test/AbstractSql.php +++ /dev/null @@ -1,590 +0,0 @@ -object = new AbstractSqlStub; - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\AbstractSql::bind - */ - public function testBind() - { - $this->assertEquals(':bind0bind', $this->object->bind('foobar')); - $this->assertEquals('(:bind1bind,:bind2bind)', $this->object->bind(array('foo','bar'))); - $this->assertEquals(1, $this->object->bind(1)); - } - - /** - * @covers Cradle\Storm\AbstractSql::collection - */ - public function testCollection() - { - $collection = $this->object->collection(); - $this->assertInstanceOf('Cradle\Storm\Collection', $collection); - } - - /** - * @covers Cradle\Storm\AbstractSql::deleteRows - */ - public function testDeleteRows() - { - $instance = $this->object->deleteRows('foobar', 'foo=bar'); - $this->assertInstanceOf('Cradle\Storm\AbstractSqlStub', $instance); - - $instance = $this->object->deleteRows('foobar', array('foo=%s', 'bar')); - $this->assertInstanceOf('Cradle\Storm\AbstractSqlStub', $instance); - - $instance = $this->object->deleteRows('foobar', array(array('foo=%s', 'bar'))); - $this->assertInstanceOf('Cradle\Storm\AbstractSqlStub', $instance); - } - - /** - * @covers Cradle\Storm\AbstractSql::getBinds - */ - public function testGetBinds() - { - $this->assertEquals(':bind0bind', $this->object->bind('foo')); - $this->assertEquals(':bind1bind', $this->object->bind('bar')); - - $binds = $this->object->getBinds(); - - $this->assertEquals('foo', $binds[':bind0bind']); - $this->assertEquals('bar', $binds[':bind1bind']); - } - - /** - * @covers Cradle\Storm\AbstractSql::getConnection - */ - public function testGetConnection() - { - $actual = $this->object->getConnection(); - $this->assertEquals('foobar', $actual); - } - - /** - * @covers Cradle\Storm\AbstractSql::getDeleteQuery - */ - public function testGetDeleteQuery() - { - $actual = $this->object->getDeleteQuery('foobar'); - $this->assertInstanceOf('Cradle\Storm\QueryDelete', $actual); - } - - /** - * @covers Cradle\Storm\AbstractSql::getInsertQuery - */ - public function testGetInsertQuery() - { - $actual = $this->object->getInsertQuery('foobar'); - $this->assertInstanceOf('Cradle\Storm\QueryInsert', $actual); - } - - /** - * @covers Cradle\Storm\AbstractSql::getLastInsertedId - */ - public function testGetLastInsertedId() - { - $actual = $this->object->getLastInsertedId(); - $this->assertEquals(123, $actual); - } - - /** - * @covers Cradle\Storm\AbstractSql::getModel - */ - public function testGetModel() - { - $model = $this->object->getModel('foobar', 'foo_id', 3); - $this->assertInstanceOf('Cradle\Storm\Model', $model); - } - - /** - * @covers Cradle\Storm\AbstractSql::getRow - */ - public function testGetRow() - { - $actual = $this->object->getRow('foobar', 'foo_id', 3); - $this->assertEquals('SELECT * FROM foobar WHERE foo_id = 3 LIMIT 0,1;', $actual['query']); - } - - /** - * @covers Cradle\Storm\AbstractSql::getSelectQuery - */ - public function testGetSelectQuery() - { - $actual = $this->object->getSelectQuery('foobar'); - $this->assertInstanceOf('Cradle\Storm\QuerySelect', $actual); - } - - /** - * @covers Cradle\Storm\AbstractSql::getUpdateQuery - */ - public function testGetUpdateQuery() - { - $actual = $this->object->getUpdateQuery('foobar'); - $this->assertInstanceOf('Cradle\Storm\QueryUpdate', $actual); - } - - /** - * @covers Cradle\Storm\AbstractSql::insertRow - */ - public function testInsertRow() - { - $instance = $this->object->insertRow('foobar', array( - 'foo' => 'bar', - 'bar' => null - )); - - $this->assertInstanceOf('Cradle\Storm\AbstractSqlStub', $instance); - } - - /** - * @covers Cradle\Storm\AbstractSql::insertRows - */ - public function testInsertRows() - { - $instance = $this->object->insertRows('foobar', array( - array( - 'foo' => 'bar', - 'bar' => 'foo' - ), - array( - 'foo' => 'bar', - 'bar' => 'foo' - ) - )); - - $this->assertInstanceOf('Cradle\Storm\AbstractSqlStub', $instance); - } - - /** - * @covers Cradle\Storm\AbstractSql::model - */ - public function testModel() - { - $collection = $this->object->model(); - $this->assertInstanceOf('Cradle\Storm\Model', $collection); - } - - /** - * @covers Cradle\Storm\AbstractSql::query - */ - public function testQuery() - { - $actual = $this->object->query('foobar', array('foo', 'bar')); - $this->assertEquals('foobar', $actual[0]['query']); - } - - /** - * @covers Cradle\Storm\AbstractSql::search - */ - public function testSearch() - { - $collection = $this->object->search('foobar'); - $this->assertInstanceOf('Cradle\Storm\Search', $collection); - } - - /** - * @covers Cradle\Storm\AbstractSql::setBinds - */ - public function testSetBinds() - { - $instance = $this->object->setBinds(array( - 'foo' => 'bar', - 'bar' => 'foo' - )); - - $this->assertInstanceOf('Cradle\Storm\AbstractSqlStub', $instance); - } - - /** - * @covers Cradle\Storm\AbstractSql::setRow - */ - public function testSetRow() - { - $instance = $this->object->setRow('foobar', 'foo_id', 3, array( - 'foo' => 'bar', - 'bar' => 'foo' - )); - - $this->assertInstanceOf('Cradle\Storm\AbstractSqlStub', $instance); - } - - /** - * @covers Cradle\Storm\AbstractSql::updateRows - */ - public function testUpdateRows() - { - $instance = $this->object->updateRows('foobar', array( - 'foo' => 'bar', - 'bar' => 'foo' - ), 'foo=bar'); - - $this->assertInstanceOf('Cradle\Storm\AbstractSqlStub', $instance); - } - - /** - * @covers Cradle\Storm\AbstractSql::i - */ - public function testI() - { - $instance1 = AbstractSqlStub::i(); - $this->assertInstanceOf('Cradle\Storm\AbstractSqlStub', $instance1); - - $instance2 = AbstractSqlStub::i(); - $this->assertTrue($instance1 !== $instance2); - } - - /** - * @covers Cradle\Storm\AbstractSql::loop - */ - public function testLoop() - { - $self = $this; - $this->object->loop(function($i) use ($self) { - $self->assertInstanceOf('Cradle\Storm\AbstractSqlStub', $this); - - if ($i == 2) { - return false; - } - }); - } - - /** - * @covers Cradle\Storm\AbstractSql::when - */ - public function testWhen() - { - $self = $this; - $test = 'Good'; - $this->object->when(function() use ($self) { - $self->assertInstanceOf('Cradle\Storm\AbstractSqlStub', $this); - return false; - }, function() use ($self, &$test) { - $self->assertInstanceOf('Cradle\Storm\AbstractSqlStub', $this); - $test = 'Bad'; - }); - } - - /** - * @covers Cradle\Storm\AbstractSql::getInspectorHandler - */ - public function testGetInspectorHandler() - { - $instance = $this->object->getInspectorHandler(); - $this->assertInstanceOf('Cradle\Profiler\InspectorHandler', $instance); - } - - /** - * @covers Cradle\Storm\AbstractSql::inspect - */ - public function testInspect() - { - ob_start(); - $this->object->inspect('foobar'); - $contents = ob_get_contents(); - ob_end_clean(); - - $this->assertEquals( - '
INSPECTING Variable:
foobar
', - $contents - ); - } - - /** - * @covers Cradle\Storm\AbstractSql::setInspectorHandler - */ - public function testSetInspectorHandler() - { - $instance = $this->object->setInspectorHandler(new InspectorHandler); - $this->assertInstanceOf('Cradle\Storm\AbstractSqlStub', $instance); - } - - /** - * @covers Cradle\Storm\AbstractSql::addLogger - */ - public function testAddLogger() - { - $instance = $this->object->addLogger(function() {}); - $this->assertInstanceOf('Cradle\Storm\AbstractSqlStub', $instance); - } - - /** - * @covers Cradle\Storm\AbstractSql::log - */ - public function testLog() - { - $trigger = new StdClass(); - $trigger->success = null; - $this->object->addLogger(function($trigger) { - $trigger->success = true; - }) - ->log($trigger); - - - $this->assertTrue($trigger->success); - } - - /** - * @covers Cradle\Storm\AbstractSql::loadState - */ - public function testLoadState() - { - $state1 = new AbstractSqlStub(); - $state2 = new AbstractSqlStub(); - - $state1->saveState('state1'); - $state2->saveState('state2'); - - $this->assertTrue($state2 === $state1->loadState('state2')); - $this->assertTrue($state1 === $state2->loadState('state1')); - } - - /** - * @covers Cradle\Storm\AbstractSql::saveState - */ - public function testSaveState() - { - $state1 = new AbstractSqlStub(); - $state2 = new AbstractSqlStub(); - - $state1->saveState('state1'); - $state2->saveState('state2'); - - $this->assertTrue($state2 === $state1->loadState('state2')); - $this->assertTrue($state1 === $state2->loadState('state1')); - } - - /** - * @covers Cradle\Storm\AbstractSql::__call - * @todo Implement test__call(). - */ - public function test__call() - { - $actual = $this->object->addResolver(ResolverCallStub::class, function() {}); - $this->assertInstanceOf('Cradle\Storm\AbstractSqlStub', $actual); - } - - /** - * @covers Cradle\Storm\AbstractSql::__callResolver - * @todo Implement test__callResolver(). - */ - public function test__callResolver() - { - $actual = $this->object->addResolver(ResolverCallStub::class, function() {}); - $this->assertInstanceOf('Cradle\Storm\AbstractSqlStub', $actual); - } - - /** - * @covers Cradle\Storm\AbstractSql::addResolver - */ - public function testAddResolver() - { - $actual = $this->object->addResolver(ResolverCallStub::class, function() {}); - $this->assertInstanceOf('Cradle\Storm\AbstractSqlStub', $actual); - } - - /** - * @covers Cradle\Storm\AbstractSql::getResolverHandler - */ - public function testGetResolverHandler() - { - $actual = $this->object->getResolverHandler(); - $this->assertInstanceOf('Cradle\Resolver\ResolverHandler', $actual); - } - - /** - * @covers Cradle\Storm\AbstractSql::resolve - */ - public function testResolve() - { - $actual = $this->object->addResolver( - ResolverCallStub::class, - function() { - return new ResolverAddStub(); - } - ) - ->resolve(ResolverCallStub::class) - ->foo('bar'); - - $this->assertEquals('barfoo', $actual); - } - - /** - * @covers Cradle\Storm\AbstractSql::resolveShared - */ - public function testResolveShared() - { - $actual = $this - ->object - ->resolveShared(ResolverSharedStub::class) - ->reset() - ->foo('bar'); - - $this->assertEquals('barfoo', $actual); - - $actual = $this - ->object - ->resolveShared(ResolverSharedStub::class) - ->foo('bar'); - - $this->assertEquals('barbar', $actual); - } - - /** - * @covers Cradle\Storm\AbstractSql::resolveStatic - */ - public function testResolveStatic() - { - $actual = $this - ->object - ->resolveStatic( - ResolverStaticStub::class, - 'foo', - 'bar' - ); - - $this->assertEquals('barfoo', $actual); - } - - /** - * @covers Cradle\Storm\AbstractSql::setResolverHandler - */ - public function testSetResolverHandler() - { - $actual = $this->object->setResolverHandler(new ResolverHandler); - $this->assertInstanceOf('Cradle\Storm\AbstractSqlStub', $actual); - } -} - -if(!class_exists('Cradle\Storm\AbstractSqlStub')) { - class AbstractSqlStub extends AbstractSql implements SqlInterface - { - public function connect($options = []) - { - $this->connection = 'foobar'; - return $this; - } - - public function getLastInsertedId($column = null) - { - return 123; - } - - public function query($query, array $binds = [], $fetch = null) - { - return array(array( - 'total' => 123, - 'query' => (string) $query, - 'binds' => $binds - )); - } - - public function getColumns() - { - return array( - array( - 'Field' => 'foobar_id', - 'Type' => 'int', - 'Key' => 'PRI', - 'Default' => null, - 'Null' => 1 - ), - array( - 'Field' => 'foobar_title', - 'Type' => 'vachar', - 'Key' => null, - 'Default' => null, - 'Null' => 1 - ), - array( - 'Field' => 'foobar_date', - 'Type' => 'datetime', - 'Key' => null, - 'Default' => null, - 'Null' => 1 - ) - ); - } - } -} - -if(!class_exists('Cradle\Storm\ResolverCallStub')) { - class ResolverCallStub - { - public function foo($string) - { - return $string . 'foo'; - } - } -} - -if(!class_exists('Cradle\Storm\ResolverAddStub')) { - class ResolverAddStub - { - public function foo($string) - { - return $string . 'foo'; - } - } -} - -if(!class_exists('Cradle\Storm\ResolverSharedStub')) { - class ResolverSharedStub - { - public $name = 'foo'; - - public function foo($string) - { - $name = $this->name; - $this->name = $string; - return $string . $name; - } - - public function reset() - { - $this->name = 'foo'; - return $this; - } - } -} - -if(!class_exists('Cradle\Storm\ResolverStaticStub')) { - class ResolverStaticStub - { - public static function foo($string) - { - return $string . 'foo'; - } - } -} diff --git a/test/Collection.php b/test/Collection.php deleted file mode 100644 index 9f947e9..0000000 --- a/test/Collection.php +++ /dev/null @@ -1,85 +0,0 @@ -object = new Collection; - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\Collection::getModel - */ - public function testGetModel() - { - $instance = $this->object->getModel(); - $this->assertInstanceOf('Cradle\Storm\Model', $instance); - } - - /** - * @covers Cradle\Storm\Collection::setDatabase - */ - public function testSetDatabase() - { - $instance = $this->object->setDatabase(new AbstractSqlStub); - $this->assertInstanceOf('Cradle\Storm\Collection', $instance); - } - - /** - * @covers Cradle\Storm\Collection::setTable - * @todo Implement testSetTable(). - */ - public function testSetTable() - { - $instance = $this->object->setTable('foobar'); - $this->assertInstanceOf('Cradle\Storm\Collection', $instance); - } -} - -if(!class_exists('Cradle\Storm\AbstractSqlStub')) { - class AbstractSqlStub extends AbstractSql implements SqlInterface - { - public function connect($options = []) - { - $this->connection = 'foobar'; - return $this; - } - - public function getLastInsertedId($column = null) - { - return 123; - } - - public function query($query, array $binds = []) - { - return array(array( - 'query' => (string) $query, - 'binds' => $binds - )); - } - } -} diff --git a/test/Engine/AbstractEngine.php b/test/Engine/AbstractEngine.php new file mode 100644 index 0000000..64fffad --- /dev/null +++ b/test/Engine/AbstractEngine.php @@ -0,0 +1,488 @@ +object = new AbstractEngineStub; + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::bind + */ + public function testBind() + { + $this->assertEquals(':bind0bind', $this->object->bind('foobar')); + $this->assertEquals('(:bind1bind,:bind2bind)', $this->object->bind(array('foo','bar'))); + $this->assertEquals(1, $this->object->bind(1)); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::collection + */ + public function testCollection() + { + $collection = $this->object->collection(); + $this->assertInstanceOf('Cradle\Storm\Mapper\Collection', $collection); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::deleteRows + */ + public function testDeleteRows() + { + $instance = $this->object->deleteRows('foobar', 'foo=bar'); + $this->assertInstanceOf('Cradle\Storm\Engine\AbstractEngineStub', $instance); + + $instance = $this->object->deleteRows('foobar', array('foo=%s', 'bar')); + $this->assertInstanceOf('Cradle\Storm\Engine\AbstractEngineStub', $instance); + + $instance = $this->object->deleteRows('foobar', array(array('foo=%s', 'bar'))); + $this->assertInstanceOf('Cradle\Storm\Engine\AbstractEngineStub', $instance); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::getBinds + */ + public function testGetBinds() + { + $this->assertEquals(':bind0bind', $this->object->bind('foo')); + $this->assertEquals(':bind1bind', $this->object->bind('bar')); + + $binds = $this->object->getBinds(); + + $this->assertEquals('foo', $binds[':bind0bind']); + $this->assertEquals('bar', $binds[':bind1bind']); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::getConnection + */ + public function testGetConnection() + { + $actual = $this->object->getConnection(); + $this->assertInstanceOf('PDO', $actual); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::getDeleteQuery + */ + public function testGetDeleteQuery() + { + $actual = $this->object->getDeleteQuery('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\Delete', $actual); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::getInsertQuery + */ + public function testGetInsertQuery() + { + $actual = $this->object->getInsertQuery('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\Insert', $actual); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::getLastInsertedId + */ + public function testGetLastInsertedId() + { + $actual = $this->object->getLastInsertedId(); + $this->assertEquals(123, $actual); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::getModel + */ + public function testGetModel() + { + $model = $this->object->getModel('foobar', 'foo_id', 3); + $this->assertInstanceOf('Cradle\Storm\Mapper\Model', $model); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::getRow + */ + public function testGetRow() + { + $actual = $this->object->getRow('foobar', 'foo_id', 3); + $this->assertEquals('SELECT * FROM foobar WHERE foo_id = 3 LIMIT 0, 1;', $actual['query']); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::getSelectQuery + */ + public function testGetSelectQuery() + { + $actual = $this->object->getSelectQuery('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\Select', $actual); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::getUpdateQuery + */ + public function testGetUpdateQuery() + { + $actual = $this->object->getUpdateQuery('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\Update', $actual); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::insertRow + */ + public function testInsertRow() + { + $instance = $this->object->insertRow('foobar', array( + 'foo' => 'bar', + 'bar' => null + )); + + $this->assertInstanceOf('Cradle\Storm\Engine\AbstractEngineStub', $instance); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::insertRows + */ + public function testInsertRows() + { + $instance = $this->object->insertRows('foobar', array( + array( + 'foo' => 'bar', + 'bar' => 'foo' + ), + array( + 'foo' => 'bar', + 'bar' => 'foo' + ) + )); + + $this->assertInstanceOf('Cradle\Storm\Engine\AbstractEngineStub', $instance); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::model + */ + public function testModel() + { + $collection = $this->object->model(); + $this->assertInstanceOf('Cradle\Storm\Mapper\Model', $collection); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::query + */ + public function testQuery() + { + $actual = $this->object->query('foobar', array('foo', 'bar')); + $this->assertEquals('foobar', $actual[0]['query']); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::search + */ + public function testSearch() + { + $collection = $this->object->search('foobar'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Search', $collection); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::setBinds + */ + public function testSetBinds() + { + $instance = $this->object->setBinds(array( + 'foo' => 'bar', + 'bar' => 'foo' + )); + + $this->assertInstanceOf('Cradle\Storm\Engine\AbstractEngineStub', $instance); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::setRow + */ + public function testSetRow() + { + $instance = $this->object->setRow('foobar', 'foo_id', 3, array( + 'foo' => 'bar', + 'bar' => 'foo' + )); + + $this->assertInstanceOf('Cradle\Storm\Engine\AbstractEngineStub', $instance); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::updateRows + */ + public function testUpdateRows() + { + $instance = $this->object->updateRows('foobar', array( + 'foo' => 'bar', + 'bar' => 'foo' + ), 'foo=bar'); + + $this->assertInstanceOf('Cradle\Storm\Engine\AbstractEngineStub', $instance); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::loadState + */ + public function testLoadState() + { + $state1 = new AbstractEngineStub(); + $state2 = new AbstractEngineStub(); + + $state1->saveState('state1'); + $state2->saveState('state2'); + + $this->assertTrue($state2 === $state1->loadState('state2')); + $this->assertTrue($state1 === $state2->loadState('state1')); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::saveState + */ + public function testSaveState() + { + $state1 = new AbstractEngineStub(); + $state2 = new AbstractEngineStub(); + + $state1->saveState('state1'); + $state2->saveState('state2'); + + $this->assertTrue($state2 === $state1->loadState('state2')); + $this->assertTrue($state1 === $state2->loadState('state1')); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::__call + * @todo Implement test__call(). + */ + public function test__call() + { + $actual = $this->object->addResolver(ResolverCallStub::class, function() {}); + $this->assertInstanceOf('Cradle\Storm\Engine\AbstractEngineStub', $actual); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::__callResolver + * @todo Implement test__callResolver(). + */ + public function test__callResolver() + { + $actual = $this->object->addResolver(ResolverCallStub::class, function() {}); + $this->assertInstanceOf('Cradle\Storm\Engine\AbstractEngineStub', $actual); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::addResolver + */ + public function testAddResolver() + { + $actual = $this->object->addResolver(ResolverCallStub::class, function() {}); + $this->assertInstanceOf('Cradle\Storm\Engine\AbstractEngineStub', $actual); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::getResolverHandler + */ + public function testGetResolverHandler() + { + $actual = $this->object->getResolverHandler(); + $this->assertInstanceOf('Cradle\Resolver\ResolverHandler', $actual); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::resolve + */ + public function testResolve() + { + $actual = $this->object->addResolver( + ResolverCallStub::class, + function() { + return new ResolverAddStub(); + } + ) + ->resolve(ResolverCallStub::class) + ->foo('bar'); + + $this->assertEquals('barfoo', $actual); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::resolveShared + */ + public function testResolveShared() + { + $actual = $this + ->object + ->resolveShared(ResolverSharedStub::class) + ->reset() + ->foo('bar'); + + $this->assertEquals('barfoo', $actual); + + $actual = $this + ->object + ->resolveShared(ResolverSharedStub::class) + ->foo('bar'); + + $this->assertEquals('barbar', $actual); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::resolveStatic + */ + public function testResolveStatic() + { + $actual = $this + ->object + ->resolveStatic( + ResolverStaticStub::class, + 'foo', + 'bar' + ); + + $this->assertEquals('barfoo', $actual); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::setResolverHandler + */ + public function testSetResolverHandler() + { + $actual = $this->object->setResolverHandler(new ResolverHandler); + $this->assertInstanceOf('Cradle\Storm\Engine\AbstractEngineStub', $actual); + } +} + +if(!class_exists('Cradle\Storm\Engine\AbstractEngineStub')) { + class AbstractEngineStub extends AbstractEngine implements EngineInterface + { + public function connect($options = []): EngineInterface + { + $this->connection = new \PDO('sqlite:'.dirname(__DIR__).'/assets/sqlite.db', 'root', '');; + return $this; + } + + public function getLastInsertedId(?string $column = null): int + { + return 123; + } + + public function query($query, array $binds = [], ?callable $fetch = null) + { + return array(array( + 'total' => 123, + 'query' => (string) $query, + 'binds' => $binds + )); + } + + public function getColumns() + { + return array( + array( + 'Field' => 'foobar_id', + 'Type' => 'int', + 'Key' => 'PRI', + 'Default' => null, + 'Null' => 1 + ), + array( + 'Field' => 'foobar_title', + 'Type' => 'vachar', + 'Key' => null, + 'Default' => null, + 'Null' => 1 + ), + array( + 'Field' => 'foobar_date', + 'Type' => 'datetime', + 'Key' => null, + 'Default' => null, + 'Null' => 1 + ) + ); + } + } +} + +if(!class_exists('Cradle\Storm\Engine\ResolverCallStub')) { + class ResolverCallStub + { + public function foo($string) + { + return $string . 'foo'; + } + } +} + +if(!class_exists('Cradle\Storm\Engine\ResolverAddStub')) { + class ResolverAddStub + { + public function foo($string) + { + return $string . 'foo'; + } + } +} + +if(!class_exists('Cradle\Storm\Engine\ResolverSharedStub')) { + class ResolverSharedStub + { + public $name = 'foo'; + + public function foo($string) + { + $name = $this->name; + $this->name = $string; + return $string . $name; + } + + public function reset() + { + $this->name = 'foo'; + return $this; + } + } +} + +if(!class_exists('Cradle\Storm\Engine\ResolverStaticStub')) { + class ResolverStaticStub + { + public static function foo($string) + { + return $string . 'foo'; + } + } +} diff --git a/test/Engine/MySql.php b/test/Engine/MySql.php new file mode 100644 index 0000000..1f165bf --- /dev/null +++ b/test/Engine/MySql.php @@ -0,0 +1,312 @@ +object = SqlFactory::load(include(dirname(__DIR__).'/assets/mysql.php')); + $schema = file_get_contents(dirname(__DIR__).'/assets/mysql-schema.sql'); + $this->object->query($schema); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\Engine\MySql::__construct + */ + public function test__construct() + { + $actual = $this->object->__construct('foo', 'foo', 'foo'); + $this->assertNull($actual); + } + + /** + * @covers Cradle\Storm\Engine\MySql::connect + */ + public function testConnect() + { + $instance = $this->object->connect(include(dirname(__DIR__).'/assets/mysql.php')); + $this->assertInstanceOf('Cradle\Storm\Engine\MySql', $instance); + + $this->object->__construct('127.0.0.1', 'testing_db', 'root'); + $instance = $this->object->connect(); + $this->assertInstanceOf('Cradle\Storm\Engine\MySql', $instance); + } + + /** + * @covers Cradle\Storm\Engine\MySql::getAlterQuery + */ + public function testGetAlterQuery() + { + $instance = $this->object->getAlterQuery('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Alter', $instance); + } + + /** + * @covers Cradle\Storm\Engine\MySql::getColumns + */ + public function testGetColumns() + { + $actual = $this->object->getColumns('address'); + $this->assertTrue(is_array($actual)); + + //$actual = $this->object->getColumns('address', array(array("Field LIKE 'address_%'"))); + //$this->assertTrue(is_array($actual)); + } + + /** + * @covers Cradle\Storm\Engine\MySql::getCreateQuery + */ + public function testGetCreateQuery() + { + $instance = $this->object->getCreateQuery('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Create', $instance); + } + + /** + * @covers Cradle\Storm\Engine\MySql::getPrimaryKey + */ + public function testGetPrimaryKey() + { + $actual = $this->object->getPrimaryKey('address'); + $this->assertEquals('address_id', $actual); + } + + /** + * @covers Cradle\Storm\Engine\MySql::getTables + */ + public function testGetTables() + { + $actual = $this->object->getTables(); + $this->assertTrue(is_array($actual)); + } + + /** + * @covers Cradle\Storm\Engine\MySql::getTableSchema + */ + public function testGetTableSchema() + { + $this->object->insertRow('address', array( + 'address_label' => 'Foo Bar', + 'address_street' => 'foobar', + 'address_city' => 'foobar', + 'address_country' => 'foobar', + 'address_postal' => 'foobar', + 'address_created' => date('Y-m-d H:i:s'), + 'address_updated' => date('Y-m-d H:i:s') + )); + $actual = $this->object->getTableSchema('address'); + $this->assertContains('CREATE TABLE `address`', $actual); + } + + /** + * @covers Cradle\Storm\Engine\MySql::getUtilityQuery + */ + public function testGetUtilityQuery() + { + $instance = $this->object->getUtilityQuery(); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Utility', $instance); + } + + /** + * @covers Cradle\Storm\AbstractSql::query + * @covers Cradle\Storm\Search::getRows + */ + public function testQuery() + { + $test = $this; + $triggered = false; + $instance = $this->object->query('SELECT * FROM address', array(), function($row) use ($test, &$triggered) { + $triggered = true; + $test->assertInstanceOf('Cradle\Storm\Engine\MySql', $this); + $test->assertEquals($row['address_label'], 'Foo Bar'); + return false; + }); + + $this->assertInstanceOf('Cradle\Storm\Engine\MySql', $instance); + $this->assertTrue($triggered); + + $row = $this->object->search('address')->getRow(); + $this->assertEquals($row['address_label'], 'Foo Bar'); + + $triggered = false; + $instance = $this->object->search('address')->getRows(function($row) use ($test, &$triggered) { + $triggered = true; + $test->assertInstanceOf('Cradle\Storm\Engine\MySql', $this); + $test->assertEquals($row['address_label'], 'Foo Bar'); + return false; + }); + + $this->assertInstanceOf('Cradle\Storm\Mapper\Search', $instance); + $this->assertTrue($triggered); + } + + /** + * @covers Cradle\Storm\AbstractSql::transaction + * @covers Cradle\Storm\Search::getTotal + */ + public function testTransaction() + { + $test = $this; + $triggered = false; + + $total = $this->object->search('address')->getTotal(); + + $this->object->transaction(function() use ($test, &$triggered) { + $triggered = true; + $test->assertInstanceOf('Cradle\Storm\Engine\MySql', $this); + + $this->insertRow('address', array( + 'address_label' => 'Foo Bar', + 'address_street' => 'foobar', + 'address_city' => 'foobar', + 'address_country' => 'foobar', + 'address_postal' => 'foobar', + 'address_created' => date('Y-m-d H:i:s'), + 'address_updated' => date('Y-m-d H:i:s') + )); + }); + + $this->assertTrue($triggered); + $this->assertEquals($total + 1, $this->object->search('address')->getTotal()); + + $triggered = false; + $this->object->transaction(function() use ($test, &$triggered) { + $triggered = true; + $test->assertInstanceOf('Cradle\Storm\Engine\MySql', $this); + + $this->insertRow('address', array( + 'address_label' => 'Foo Bar', + 'address_street' => 'foobar', + 'address_city' => 'foobar', + 'address_country' => 'foobar', + 'address_postal' => 'foobar', + 'address_created' => date('Y-m-d H:i:s'), + 'address_updated' => date('Y-m-d H:i:s') + )); + + return false; + }); + + $this->assertTrue($triggered); + $this->assertEquals($total + 1, $this->object->search('address')->getTotal()); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::loop + */ + public function testLoop() + { + $self = $this; + $this->object->loop(function($i) use ($self) { + $self->assertInstanceOf('Cradle\Storm\Engine\MySql', $this); + + if ($i == 2) { + return false; + } + }); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::when + */ + public function testWhen() + { + $self = $this; + $test = 'Good'; + $this->object->when(function() use ($self) { + $self->assertInstanceOf('Cradle\Storm\Engine\MySql', $this); + return false; + }, function() use ($self, &$test) { + $self->assertInstanceOf('Cradle\Storm\Engine\MySql', $this); + $test = 'Bad'; + }); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::getInspectorHandler + */ + public function testGetInspectorHandler() + { + $instance = $this->object->getInspectorHandler(); + $this->assertInstanceOf('Cradle\Profiler\InspectorHandler', $instance); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::inspect + */ + public function testInspect() + { + ob_start(); + $this->object->inspect('foobar'); + $contents = ob_get_contents(); + ob_end_clean(); + + $this->assertEquals( + '
INSPECTING Variable:
foobar
', + $contents + ); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::setInspectorHandler + */ + public function testSetInspectorHandler() + { + $instance = $this->object->setInspectorHandler(new InspectorHandler); + $this->assertInstanceOf('Cradle\Storm\Engine\MySql', $instance); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::addLogger + */ + public function testAddLogger() + { + $instance = $this->object->addLogger(function() {}); + $this->assertInstanceOf('Cradle\Storm\Engine\MySql', $instance); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::log + */ + public function testLog() + { + $trigger = new StdClass(); + $trigger->success = null; + $this->object->addLogger(function($trigger) { + $trigger->success = true; + }) + ->log($trigger); + + + $this->assertTrue($trigger->success); + } +} diff --git a/test/Engine/PostGreSql.php b/test/Engine/PostGreSql.php new file mode 100644 index 0000000..82a155c --- /dev/null +++ b/test/Engine/PostGreSql.php @@ -0,0 +1,248 @@ +object = SqlFactory::load(include(dirname(__DIR__).'/assets/pgsql.php')); + $schema = file_get_contents(dirname(__DIR__).'/assets/pgsql-schema.sql'); + $this->object->query($schema); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\Engine\PostGreSql::connect + */ + public function testConnect() + { + $instance = $this->object->connect(include(__DIR__.'/assets/pgsql.php')); + $this->assertInstanceOf('Cradle\Storm\Engine\PostGreSql', $instance); + } + + /** + * @covers Cradle\Storm\Engine\PostGreSql::getAlterQuery + */ + public function testGetAlterQuery() + { + $instance = $this->object->getAlterQuery('foobar'); + $this->assertInstanceOf('Cradle\Storm\Engine\PostGreSql\QueryAlter', $instance); + } + + /** + * @covers Cradle\Storm\Engine\PostGreSql::getColumns + */ + public function testGetColumns() + { + $actual = $this->object->getColumns('address'); + $this->assertTrue(is_array($actual)); + } + + /** + * @covers Cradle\Storm\Engine\PostGreSql::getCreateQuery + */ + public function testGetCreateQuery() + { + $instance = $this->object->getCreateQuery('foobar'); + $this->assertInstanceOf('Cradle\Storm\Engine\PostGreSql\QueryCreate', $instance); + } + + /** + * @covers Cradle\Storm\Engine\PostGreSql::getDeleteQuery + */ + public function testGetDeleteQuery() + { + $instance = $this->object->getDeleteQuery('foobar'); + $this->assertInstanceOf('Cradle\Storm\Engine\PostGreSql\QueryDelete', $instance); + } + + /** + * @covers Cradle\Storm\Engine\PostGreSql::getIndexes + */ + public function testGetIndexes() + { + $actual = $this->object->getIndexes('address'); + $this->assertTrue(is_array($actual)); + } + + /** + * @covers Cradle\Storm\Engine\PostGreSql::getInsertQuery + */ + public function testGetInsertQuery() + { + $instance = $this->object->getInsertQuery('foobar'); + $this->assertInstanceOf('Cradle\Storm\Engine\PostGreSql\QueryInsert', $instance); + } + + /** + * @covers Cradle\Storm\Engine\PostGreSql::getPrimary + */ + public function testGetPrimary() + { + $actual = $this->object->getPrimary('address'); + $this->assertTrue(is_array($actual)); + } + + /** + * @covers Cradle\Storm\Engine\PostGreSql::getSelectQuery + */ + public function testGetSelectQuery() + { + $instance = $this->object->getSelectQuery('foobar'); + $this->assertInstanceOf('Cradle\Storm\Engine\PostGreSql\QuerySelect', $instance); + } + + /** + * @covers Cradle\Storm\Engine\PostGreSql::getTables + */ + public function testGetTables() + { + $actual = $this->object->getTables(); + $this->assertEquals('address', $actual[0]['tablename']); + } + + /** + * @covers Cradle\Storm\Engine\PostGreSql::getUpdateQuery + */ + public function testGetUpdateQuery() + { + $instance = $this->object->getUpdateQuery('foobar'); + $this->assertInstanceOf('Cradle\Storm\Engine\PostGreSql\QueryUpdate', $instance); + } + + /** + * @covers Cradle\Storm\Engine\PostGreSql::setSchema + */ + public function testSetSchema() + { + $instance = $this->object->setSchema('public'); + $this->assertInstanceOf('Cradle\Storm\Engine\PostGreSql', $instance); + } + + /** + * @covers Cradle\Storm\Engine\PostGreSql::getUtilityQuery + */ + public function testGetUtilityQuery() + { + $instance = $this->object->getUtilityQuery(); + $this->assertInstanceOf('Cradle\Storm\Engine\PostGreSql\QueryUtility', $instance); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::loop + */ + public function testLoop() + { + $self = $this; + $this->object->loop(function($i) use ($self) { + $self->assertInstanceOf('Cradle\Storm\Engine\PostGreSql', $this); + + if ($i == 2) { + return false; + } + }); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::when + */ + public function testWhen() + { + $self = $this; + $test = 'Good'; + $this->object->when(function() use ($self) { + $self->assertInstanceOf('Cradle\Storm\Engine\PostGreSql', $this); + return false; + }, function() use ($self, &$test) { + $self->assertInstanceOf('Cradle\Storm\Engine\PostGreSql', $this); + $test = 'Bad'; + }); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::getInspectorHandler + */ + public function testGetInspectorHandler() + { + $instance = $this->object->getInspectorHandler(); + $this->assertInstanceOf('Cradle\Profiler\PostGreSql', $instance); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::inspect + */ + public function testInspect() + { + ob_start(); + $this->object->inspect('foobar'); + $contents = ob_get_contents(); + ob_end_clean(); + + $this->assertEquals( + '
INSPECTING Variable:
foobar
', + $contents + ); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::setInspectorHandler + */ + public function testSetInspectorHandler() + { + $instance = $this->object->setInspectorHandler(new InspectorHandler); + $this->assertInstanceOf('Cradle\Storm\Engine\PostGreSql', $instance); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::addLogger + */ + public function testAddLogger() + { + $instance = $this->object->addLogger(function() {}); + $this->assertInstanceOf('Cradle\Storm\Engine\PostGreSql', $instance); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::log + */ + public function testLog() + { + $trigger = new StdClass(); + $trigger->success = null; + $this->object->addLogger(function($trigger) { + $trigger->success = true; + }) + ->log($trigger); + + + $this->assertTrue($trigger->success); + } +} diff --git a/test/Engine/Sqlite.php b/test/Engine/Sqlite.php new file mode 100644 index 0000000..427db20 --- /dev/null +++ b/test/Engine/Sqlite.php @@ -0,0 +1,229 @@ +object = SqlFactory::load($connection); + $connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\Engine\Sqlite::connect + */ + public function testConnect() + { + $instance = $this->object->connect(include(dirname(__DIR__).'/assets/sqlite.php')); + + $this->assertInstanceOf('Cradle\Storm\Engine\Sqlite', $instance); + } + + /** + * @covers Cradle\Storm\Engine\Sqlite::getAlterQuery + */ + public function testGetAlterQuery() + { + $instance = $this->object->getAlterQuery('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\Sqlite\Alter', $instance); + } + + /** + * @covers Cradle\Storm\Engine\Sqlite::getColumns + */ + public function testGetColumns() + { + $actual = $this->object->getColumns('unit_post'); + $this->assertTrue(is_array($actual)); + } + + /** + * @covers Cradle\Storm\Engine\Sqlite::getCreateQuery + */ + public function testGetCreateQuery() + { + $instance = $this->object->getCreateQuery('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\Sqlite\Create', $instance); + } + + /** + * @covers Cradle\Storm\Engine\Sqlite::getPrimaryKey + */ + public function testGetPrimaryKey() + { + $actual = $this->object->getPrimaryKey('unit_post'); + $this->assertEquals('post_id', $actual); + } + + /** + * @covers Cradle\Storm\Engine\Sqlite::getTables + */ + public function testGetTables() + { + $actual = $this->object->getTables(); + $this->assertEquals('unit_post', $actual[0]['name']); + } + + /** + * @covers Cradle\Storm\Engine\Sqlite::insertRows + */ + public function testInsertRows() + { + $instance = $this->object->insertRows('unit_post', array( + array( + 'post_slug' => 'unit-test-2-'.md5(uniqid()), + 'post_title' => 'Unit Test 2', + 'post_detail' => 'Unit Test Detail 2', + 'post_published' => date('Y-m-d'), + 'post_created' => date('Y-m-d H:i:s'), + 'post_updated' => date('Y-m-d H:i:s')), + array( + 'post_slug' => 'unit-test-3-'.md5(uniqid()), + 'post_title' => 'Unit Test 3', + 'post_detail' => 'Unit Test Detail 3', + 'post_published' => date('Y-m-d'), + 'post_created' => date('Y-m-d H:i:s'), + 'post_updated' => date('Y-m-d H:i:s')) + )); + + $this->assertInstanceOf('Cradle\Storm\Engine\Sqlite', $instance); + } + + /** + * @covers Cradle\Storm\Engine\Sqlite::getSelectQuery + */ + public function testGetSelectQuery() + { + $instance = $this->object->getSelectQuery(); + $this->assertInstanceOf('Cradle\Storm\Query\Select', $instance); + } + + /** + * @covers Cradle\Storm\Engine\Sqlite::getUtilityQuery + */ + public function testGetUtilityQuery() + { + $instance = $this->object->getUtilityQuery(); + $this->assertInstanceOf('Cradle\Storm\Query\Sqlite\Utility', $instance); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::loop + */ + public function testLoop() + { + $self = $this; + $this->object->loop(function($i) use ($self) { + $self->assertInstanceOf('Cradle\Storm\Engine\Sqlite', $this); + + if ($i == 2) { + return false; + } + }); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::when + */ + public function testWhen() + { + $self = $this; + $test = 'Good'; + $this->object->when(function() use ($self) { + $self->assertInstanceOf('Cradle\Storm\Engine\Sqlite', $this); + return false; + }, function() use ($self, &$test) { + $self->assertInstanceOf('Cradle\Storm\Engine\Sqlite', $this); + $test = 'Bad'; + }); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::getInspectorHandler + */ + public function testGetInspectorHandler() + { + $instance = $this->object->getInspectorHandler(); + $this->assertInstanceOf('Cradle\Profiler\InspectorHandler', $instance); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::inspect + */ + public function testInspect() + { + ob_start(); + $this->object->inspect('foobar'); + $contents = ob_get_contents(); + ob_end_clean(); + + $this->assertEquals( + '
INSPECTING Variable:
foobar
', + $contents + ); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::setInspectorHandler + */ + public function testSetInspectorHandler() + { + $instance = $this->object->setInspectorHandler(new InspectorHandler); + $this->assertInstanceOf('Cradle\Storm\Engine\Sqlite', $instance); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::addLogger + */ + public function testAddLogger() + { + $instance = $this->object->addLogger(function() {}); + $this->assertInstanceOf('Cradle\Storm\Engine\Sqlite', $instance); + } + + /** + * @covers Cradle\Storm\Engine\AbstractEngine::log + */ + public function testLog() + { + $trigger = new StdClass(); + $trigger->success = null; + $this->object->addLogger(function($trigger) { + $trigger->success = true; + }) + ->log($trigger); + + + $this->assertTrue($trigger->success); + } +} diff --git a/test/Mapper/Collection.php b/test/Mapper/Collection.php new file mode 100644 index 0000000..109fc2d --- /dev/null +++ b/test/Mapper/Collection.php @@ -0,0 +1,60 @@ +object = new Collection; + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\Mapper\Collection::getModel + */ + public function testGetModel() + { + $instance = $this->object->getModel(); + $this->assertInstanceOf('Cradle\Storm\Mapper\Model', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Collection::setDatabase + */ + public function testSetDatabase() + { + $instance = $this->object->setDatabase(new AbstractEngineStub); + $this->assertInstanceOf('Cradle\Storm\Mapper\Collection', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Collection::setTable + */ + public function testSetTable() + { + $instance = $this->object->setTable('foobar'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Collection', $instance); + } +} diff --git a/test/Mapper/Model.php b/test/Mapper/Model.php new file mode 100644 index 0000000..4ec1fe9 --- /dev/null +++ b/test/Mapper/Model.php @@ -0,0 +1,375 @@ +object = new Model(); + $this->object + ->setDatabase(new AbstractEngineStub) + ->setFoobarTitle('Foo Bar 1') + ->setFoobarDate('January 12, 2015') + ->setFooDate(1234567890); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\Mapper\Model::formatTime + */ + public function testFormatTime() + { + $actual = $this->object->formatTime('foobar_date'); + $this->assertEquals('2015-01-12 12:00:00', $actual->getFoobarDate()); + $actual = $this->object->formatTime('foo_date'); + $this->assertEquals('2009-02-13 11:31:30', $actual->getFooDate()); + $actual = $this->object->formatTime('foo_title'); + $this->assertEquals('Foo Bar 1', $actual->getFoobarTitle()); + } + + /** + * @covers Cradle\Storm\Mapper\Model::insert + * @covers Cradle\Storm\Mapper\Model::getMeta + * @covers Cradle\Storm\Mapper\Model::getValidColumns + */ + public function testInsert() + { + $instance = $this->object->insert('foo'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Model', $instance); + $this->assertEquals(123, $this->object->getFoobarId()); + + $instance = $this->object->setTable('foo')->insert(); + $this->assertInstanceOf('Cradle\Storm\Mapper\Model', $instance); + $this->assertEquals(123, $this->object->getFoobarId()); + + $triggered = false; + try { + $this->object = new Model(); + $this->object + ->setFoobarTitle('Foo Bar 1') + ->setFoobarDate('January 12, 2015') + ->setFooDate(1234567890) + ->insert(); + } catch(SqlException $e) { + $triggered = true; + } + + $this->assertTrue($triggered); + + $triggered = false; + try { + $this->object = new Model(); + $this->object + ->setFoobarTitle('Foo Bar 1') + ->setFoobarDate('January 12, 2015') + ->setFooDate(1234567890) + ->insert('foo'); + } catch(SqlException $e) { + $triggered = true; + } + + $this->assertTrue($triggered); + + $triggered = false; + try { + $this->object = new Model(); + $this->object + ->setFoobarTitle('Foo Bar 1') + ->setFoobarDate('January 12, 2015') + ->setFooDate(1234567890) + ->insert('foo', new AbstractEngineStub); + } catch(SqlException $e) { + $triggered = true; + } + + $this->assertFalse($triggered); + } + + /** + * @covers Cradle\Storm\Mapper\Model::remove + * @covers Cradle\Storm\Mapper\Model::getMeta + * @covers Cradle\Storm\Mapper\Model::getValidColumns + */ + public function testRemove() + { + $instance = $this->object->setFoobarId(321)->remove('foo'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Model', $instance); + + $instance = $this->object->setFoobarId(321)->setTable('foo')->remove(); + $this->assertInstanceOf('Cradle\Storm\Mapper\Model', $instance); + + $triggered = false; + try { + $this->object = new Model(); + $this->object + ->setFoobarId(321) + ->setFoobarTitle('Foo Bar 1') + ->setFoobarDate('January 12, 2015') + ->setFooDate(1234567890) + ->remove(); + } catch(SqlException $e) { + $triggered = true; + } + + $this->assertTrue($triggered); + + $triggered = false; + try { + $this->object = new Model(); + $this->object + ->setFoobarId(321) + ->setFoobarTitle('Foo Bar 1') + ->setFoobarDate('January 12, 2015') + ->setFooDate(1234567890) + ->remove('foo'); + } catch(SqlException $e) { + $triggered = true; + } + + $this->assertTrue($triggered); + + $triggered = false; + try { + $this->object = new Model(); + $this->object + ->setFoobarId(321) + ->setFoobarTitle('Foo Bar 1') + ->setFoobarDate('January 12, 2015') + ->setFooDate(1234567890) + ->remove('foo', new AbstractEngineStub, 'foobar_id'); + } catch(SqlException $e) { + $triggered = true; + } + + $this->assertFalse($triggered); + } + + /** + * @covers Cradle\Storm\Mapper\Model::save + * @covers Cradle\Storm\Mapper\Model::isPrimarySet + */ + public function testSave() + { + $instance = $this->object->save('foo'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Model', $instance); + + $instance = $this->object->setTable('foo')->save(); + $this->assertInstanceOf('Cradle\Storm\Mapper\Model', $instance); + + $triggered = false; + try { + $this->object = new Model(); + $this->object + ->setFoobarTitle('Foo Bar 1') + ->setFoobarDate('January 12, 2015') + ->setFooDate(1234567890) + ->save(); + } catch(SqlException $e) { + $triggered = true; + } + + $this->assertTrue($triggered); + + $triggered = false; + try { + $this->object = new Model(); + $this->object + ->setFoobarTitle('Foo Bar 1') + ->setFoobarDate('January 12, 2015') + ->setFooDate(1234567890) + ->save('foo'); + } catch(SqlException $e) { + $triggered = true; + } + + $this->assertTrue($triggered); + + $triggered = false; + try { + $this->object = new Model(); + $this->object + ->setFoobarId(321) + ->setFoobarTitle('Foo Bar 1') + ->setFoobarDate('January 12, 2015') + ->setFooDate(1234567890) + ->save('foo', new AbstractEngineStub, 'foobar_id'); + } catch(SqlException $e) { + $triggered = true; + } + + $this->assertFalse($triggered); + } + + /** + * @covers Cradle\Storm\Mapper\Model::setDatabase + */ + public function testSetDatabase() + { + $instance = $this->object->setDatabase(new AbstractEngineStub); + $this->assertInstanceOf('Cradle\Storm\Mapper\Model', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Model::setTable + */ + public function testSetTable() + { + $instance = $this->object->setTable('foo'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Model', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Model::update + * @covers Cradle\Storm\Mapper\Model::getMeta + * @covers Cradle\Storm\Mapper\Model::getValidColumns + */ + public function testUpdate() + { + $instance = $this->object->setFoobarId(321)->update('foo'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Model', $instance); + + $instance = $this->object->setFoobarId(321)->setTable('foo')->update(); + $this->assertInstanceOf('Cradle\Storm\Mapper\Model', $instance); + + $triggered = false; + try { + $this->object = new Model(); + $this->object + ->setFoobarTitle('Foo Bar 1') + ->setFoobarDate('January 12, 2015') + ->setFooDate(1234567890) + ->update(); + } catch(SqlException $e) { + $triggered = true; + } + + $this->assertTrue($triggered); + + $triggered = false; + try { + $this->object = new Model(); + $this->object + ->setFoobarTitle('Foo Bar 1') + ->setFoobarDate('January 12, 2015') + ->setFooDate(1234567890) + ->update('foo'); + } catch(SqlException $e) { + $triggered = true; + } + + $this->assertTrue($triggered); + + $triggered = false; + try { + $this->object = new Model(); + $this->object + ->setFoobarId(321) + ->setFoobarTitle('Foo Bar 1') + ->setFoobarDate('January 12, 2015') + ->setFooDate(1234567890) + ->update('foo', new AbstractEngineStub, 'foobar_id'); + } catch(SqlException $e) { + $triggered = true; + } + + $this->assertFalse($triggered); + } +} + +if(!class_exists('Cradle\Storm\Mapper\AbstractEngineStub')) { + class AbstractEngineStub extends AbstractEngine implements EngineInterface + { + use InstanceTrait, + LoopTrait, + ConditionalTrait, + InspectorTrait, + LoggerTrait, + StateTrait + { + StateTrait::__callResolver as __call; + } + + public function connect($options = []): EngineInterface + { + $this->connection = 'foobar'; + return $this; + } + + public function getLastInsertedId(?string $column = null): int + { + return 123; + } + + public function query($query, array $binds = [], ?callable $fetch = null) + { + return array(array( + 'total' => 123, + 'query' => (string) $query, + 'binds' => $binds + )); + } + + public function getColumns() + { + return array( + array( + 'Field' => 'foobar_id', + 'Type' => 'int', + 'Key' => 'PRI', + 'Default' => null, + 'Null' => 1 + ), + array( + 'Field' => 'foobar_title', + 'Type' => 'vachar', + 'Key' => null, + 'Default' => null, + 'Null' => 1 + ), + array( + 'Field' => 'foobar_date', + 'Type' => 'datetime', + 'Key' => null, + 'Default' => null, + 'Null' => 1 + ) + ); + } + } +} diff --git a/test/Mapper/Remove.php b/test/Mapper/Remove.php new file mode 100644 index 0000000..eed4b7f --- /dev/null +++ b/test/Mapper/Remove.php @@ -0,0 +1,556 @@ +object = new Search(new AbstractEngineStub); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\Mapper\Remove::__construct + */ + public function test__construct() + { + $actual = $this->object->__construct(new AbstractEngineStub); + + $this->assertNull($actual); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::__call + */ + public function test__call() + { + $instance = $this->object->filterByFoo('bar', '_'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Remove', $instance); + $instance = $this->object->filterByFoo(); + $this->assertInstanceOf('Cradle\Storm\Mapper\Remove', $instance); + $instance = $this->object->filterByFoo([1, 2, 3]); + $this->assertInstanceOf('Cradle\Storm\Mapper\Remove', $instance); + + $instance = $this->object->sortByFoo('ASC', '_'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Remove', $instance); + $instance = $this->object->sortByFoo(); + $this->assertInstanceOf('Cradle\Storm\Mapper\Remove', $instance); + + $trigger = false; + try { + $this->object->foobar(); + } catch(SqlException $e) { + $trigger = true; + } + + $this->assertTrue($trigger); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::addFilter + */ + public function testAddFilter() + { + $instance = $this->object->addFilter('foo=1'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Remove', $instance); + + $instance = $this->object->addFilter('foo=%s', 1); + $this->assertInstanceOf('Cradle\Storm\Mapper\Remove', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::innerJoinOn + */ + public function testInnerJoinOn() + { + $instance = $this->object->innerJoinOn('bar', 'bar_id=foo_id'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Remove', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::innerJoinUsing + */ + public function testInnerJoinUsing() + { + $instance = $this->object->innerJoinUsing('bar', 'bar_id=foo_id'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Remove', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::leftJoinOn + */ + public function testLeftJoinOn() + { + $instance = $this->object->leftJoinOn('bar', 'bar_id=foo_id'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Remove', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::leftJoinUsing + */ + public function testLeftJoinUsing() + { + $instance = $this->object->leftJoinUsing('bar', 'bar_id=foo_id'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Remove', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::outerJoinOn + */ + public function testOuterJoinOn() + { + $instance = $this->object->outerJoinOn('bar', 'bar_id=foo_id'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Remove', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::outerJoinUsing + */ + public function testOuterJoinUsing() + { + $instance = $this->object->outerJoinUsing('bar', 'bar_id=foo_id'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Remove', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::rightJoinOn + */ + public function testRightJoinOn() + { + $instance = $this->object->rightJoinOn('bar', 'bar_id=foo_id'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Remove', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::rightJoinUsing + */ + public function testRightJoinUsing() + { + $instance = $this->object->rightJoinUsing('bar', 'bar_id=foo_id'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Remove', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::setTable + */ + public function testSetTable() + { + $instance = $this->object->setTable('foo'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Remove', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::getEventHandler + */ + public function testGetEventHandler() + { + $instance = $this->object->getEventHandler(); + $this->assertInstanceOf('Cradle\Event\EventHandler', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::on + */ + public function testOn() + { + $trigger = new StdClass(); + $trigger->success = null; + + $callback = function() use ($trigger) { + $trigger->success = true; + }; + + $instance = $this + ->object + ->on('foobar', $callback) + ->trigger('foobar'); + + $this->assertInstanceOf('Cradle\Storm\Mapper\Remove', $instance); + $this->assertTrue($trigger->success); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::setEventHandler + */ + public function testSetEventHandler() + { + $instance = $this->object->setEventHandler(new EventHandler); + $this->assertInstanceOf('Cradle\Storm\Mapper\Remove', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::trigger + */ + public function testTrigger() + { + $trigger = new StdClass(); + $trigger->success = null; + + $callback = function() use ($trigger) { + $trigger->success = true; + }; + + $instance = $this + ->object + ->on('foobar', $callback) + ->trigger('foobar'); + + $this->assertInstanceOf('Cradle\Storm\Mapper\Remove', $instance); + $this->assertTrue($trigger->success); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::i + */ + public function testI() + { + $instance1 = Search::i(new AbstractEngineStub); + $this->assertInstanceOf('Cradle\Storm\Mapper\Remove', $instance1); + + $instance2 = Search::i(new AbstractEngineStub); + $this->assertTrue($instance1 !== $instance2); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::loop + */ + public function testLoop() + { + $self = $this; + $this->object->loop(function($i) use ($self) { + $self->assertInstanceOf('Cradle\Storm\Mapper\Remove', $this); + + if ($i == 2) { + return false; + } + }); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::when + */ + public function testWhen() + { + $self = $this; + $test = 'Good'; + $this->object->when(function() use ($self) { + $self->assertInstanceOf('Cradle\Storm\Mapper\Remove', $this); + return false; + }, function() use ($self, &$test) { + $self->assertInstanceOf('Cradle\Storm\Mapper\Remove', $this); + $test = 'Bad'; + }); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::getInspectorHandler + */ + public function testGetInspectorHandler() + { + $instance = $this->object->getInspectorHandler(); + $this->assertInstanceOf('Cradle\Profiler\InspectorHandler', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::inspect + */ + public function testInspect() + { + ob_start(); + $this->object->inspect('foobar'); + $contents = ob_get_contents(); + ob_end_clean(); + + $this->assertEquals( + '
INSPECTING Variable:
foobar
', + $contents + ); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::setInspectorHandler + */ + public function testSetInspectorHandler() + { + $instance = $this->object->setInspectorHandler(new InspectorHandler); + $this->assertInstanceOf('Cradle\Storm\Mapper\Remove', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::addLogger + */ + public function testAddLogger() + { + $instance = $this->object->addLogger(function() {}); + $this->assertInstanceOf('Cradle\Storm\Mapper\Remove', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::log + */ + public function testLog() + { + $trigger = new StdClass(); + $trigger->success = null; + $this->object->addLogger(function($trigger) { + $trigger->success = true; + }) + ->log($trigger); + + + $this->assertTrue($trigger->success); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::loadState + */ + public function testLoadState() + { + $state1 = new Search(new AbstractEngineStub); + $state2 = new Search(new AbstractEngineStub); + + $state1->saveState('state1'); + $state2->saveState('state2'); + + $this->assertTrue($state2 === $state1->loadState('state2')); + $this->assertTrue($state1 === $state2->loadState('state1')); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::saveState + */ + public function testSaveState() + { + $state1 = new Search(new AbstractEngineStub); + $state2 = new Search(new AbstractEngineStub); + + $state1->saveState('state1'); + $state2->saveState('state2'); + + $this->assertTrue($state2 === $state1->loadState('state2')); + $this->assertTrue($state1 === $state2->loadState('state1')); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::__callResolver + */ + public function test__callResolver() + { + $actual = $this->object->addResolver(ResolverCallStub::class, function() {}); + $this->assertInstanceOf('Cradle\Storm\Mapper\Remove', $actual); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::addResolver + */ + public function testAddResolver() + { + $actual = $this->object->addResolver(ResolverCallStub::class, function() {}); + $this->assertInstanceOf('Cradle\Storm\Mapper\Remove', $actual); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::getResolverHandler + */ + public function testGetResolverHandler() + { + $actual = $this->object->getResolverHandler(); + $this->assertInstanceOf('Cradle\Resolver\ResolverHandler', $actual); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::resolve + */ + public function testResolve() + { + $actual = $this->object->addResolver( + ResolverCallStub::class, + function() { + return new ResolverAddStub(); + } + ) + ->resolve(ResolverCallStub::class) + ->foo('bar'); + + $this->assertEquals('barfoo', $actual); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::resolveShared + */ + public function testResolveShared() + { + $actual = $this + ->object + ->resolveShared(ResolverSharedStub::class) + ->reset() + ->foo('bar'); + + $this->assertEquals('barfoo', $actual); + + $actual = $this + ->object + ->resolveShared(ResolverSharedStub::class) + ->foo('bar'); + + $this->assertEquals('barbar', $actual); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::resolveStatic + */ + public function testResolveStatic() + { + $actual = $this + ->object + ->resolveStatic( + ResolverStaticStub::class, + 'foo', + 'bar' + ); + + $this->assertEquals('barfoo', $actual); + } + + /** + * @covers Cradle\Storm\Mapper\Remove::setResolverHandler + */ + public function testSetResolverHandler() + { + $actual = $this->object->setResolverHandler(new ResolverHandler); + $this->assertInstanceOf('Cradle\Storm\Mapper\Remove', $actual); + } +} + +if(!class_exists('Cradle\Storm\Mapper\AbstractEngineStub')) { + class AbstractEngineStub extends AbstractEngine implements EngineInterface + { + public function connect($options = []): EngineInterface + { + $this->connection = 'foobar'; + return $this; + } + + public function getLastInsertedId(?string $column = null): int + { + return 123; + } + + public function query($query, array $binds = [], ?callable $fetch = null) + { + return array(array( + 'total' => 123, + 'query' => (string) $query, + 'binds' => $binds + )); + } + + public function getColumns() + { + return array( + array( + 'Field' => 'foobar_id', + 'Type' => 'int', + 'Key' => 'PRI', + 'Default' => null, + 'Null' => 1 + ), + array( + 'Field' => 'foobar_title', + 'Type' => 'vachar', + 'Key' => null, + 'Default' => null, + 'Null' => 1 + ), + array( + 'Field' => 'foobar_date', + 'Type' => 'datetime', + 'Key' => null, + 'Default' => null, + 'Null' => 1 + ) + ); + } + } +} + +if(!class_exists('Cradle\Storm\Mapper\ResolverCallStub')) { + class ResolverCallStub + { + public function foo($string) + { + return $string . 'foo'; + } + } +} + +if(!class_exists('Cradle\Storm\Mapper\ResolverAddStub')) { + class ResolverAddStub + { + public function foo($string) + { + return $string . 'foo'; + } + } +} + +if(!class_exists('Cradle\Storm\Mapper\ResolverSharedStub')) { + class ResolverSharedStub + { + public $name = 'foo'; + + public function foo($string) + { + $name = $this->name; + $this->name = $string; + return $string . $name; + } + + public function reset() + { + $this->name = 'foo'; + return $this; + } + } +} + +if(!class_exists('Cradle\Storm\Mapper\ResolverStaticStub')) { + class ResolverStaticStub + { + public static function foo($string) + { + return $string . 'foo'; + } + } +} diff --git a/test/Mapper/Search.php b/test/Mapper/Search.php new file mode 100644 index 0000000..72a446b --- /dev/null +++ b/test/Mapper/Search.php @@ -0,0 +1,254 @@ +object = new Search(new AbstractEngineStub); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\Mapper\Search::__construct + */ + public function test__construct() + { + $actual = $this->object->__construct(new AbstractEngineStub); + + $this->assertNull($actual); + } + + /** + * @covers Cradle\Storm\Mapper\Search::__call + */ + public function test__call() + { + $instance = $this->object->filterByFoo('bar', '_'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Search', $instance); + $instance = $this->object->filterByFoo(); + $this->assertInstanceOf('Cradle\Storm\Mapper\Search', $instance); + $instance = $this->object->filterByFoo([1, 2, 3]); + $this->assertInstanceOf('Cradle\Storm\Mapper\Search', $instance); + + $instance = $this->object->sortByFoo('ASC', '_'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Search', $instance); + $instance = $this->object->sortByFoo(); + $this->assertInstanceOf('Cradle\Storm\Mapper\Search', $instance); + + $trigger = false; + try { + $this->object->foobar(); + } catch(SqlException $e) { + $trigger = true; + } + + $this->assertTrue($trigger); + } + + /** + * @covers Cradle\Storm\Mapper\Search::addSort + */ + public function testAddSort() + { + $instance = $this->object->addSort('bar', 'ASC'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Search', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Search::getCollection + */ + public function testGetCollection() + { + $instance = $this->object->getCollection(); + $this->assertInstanceOf('Cradle\Storm\Mapper\Collection', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Search::getModel + */ + public function testGetModel() + { + $instance = $this->object->getModel(); + $this->assertInstanceOf('Cradle\Storm\Mapper\Model', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Search::getRow + */ + public function testGetRow() + { + $actual = $this->object->getRow(); + $this->assertEquals('SELECT * FROM;', $actual['query']); + + $actual = $this->object->getRow(0, 'foobar'); + $this->assertNull($actual['query']); + } + + /** + * @covers Cradle\Storm\Mapper\Search::getRows + */ + public function testGetRows() + { + $this->object->groupBy('foo'); + $this->object->setRange(4); + $this->object->addSort('bar', 'ASC'); + $actual = $this->object->getRows(); + $this->assertEquals('SELECT * FROM GROUP BY foo ORDER BY bar ASC LIMIT 0, 4;', $actual[0]['query']); + } + + /** + * @covers Cradle\Storm\Mapper\Search::getTotal + */ + public function testGetTotal() + { + $actual = $this->object->getTotal(); + $this->assertEquals(123, $actual); + } + + /** + * @covers Cradle\Storm\Mapper\Search::groupBy + */ + public function testGroupBy() + { + $instance = $this->object->groupBy('foo'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Search', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Search::having + */ + public function testHaving() + { + $instance = $this->object->having('foo'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Search', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Search::setColumns + */ + public function testSetColumns() + { + $instance = $this->object->setColumns('foo', 'bar'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Search', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Search::setPage + */ + public function testSetPage() + { + $instance = $this->object->setPage(-4); + $this->assertInstanceOf('Cradle\Storm\Mapper\Search', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Search::setRange + */ + public function testSetRange() + { + $instance = $this->object->setRange(-4); + $this->assertInstanceOf('Cradle\Storm\Mapper\Search', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Search::setStart + */ + public function testSetStart() + { + $instance = $this->object->setStart(4); + $this->assertInstanceOf('Cradle\Storm\Mapper\Search', $instance); + } + + /** + * @covers Cradle\Storm\Mapper\Search::setTable + */ + public function testSetTable() + { + $instance = $this->object->setTable('foo'); + $this->assertInstanceOf('Cradle\Storm\Mapper\Search', $instance); + } +} + +if(!class_exists('Cradle\Storm\Mapper\AbstractEngineStub')) { + class AbstractEngineStub extends AbstractEngine implements EngineInterface + { + public function connect($options = []): EngineInterface + { + $this->connection = 'foobar'; + return $this; + } + + public function getLastInsertedId(?string $column = null): int + { + return 123; + } + + public function query($query, array $binds = [], ?callable $fetch = null) + { + return array(array( + 'total' => 123, + 'query' => (string) $query, + 'binds' => $binds + )); + } + + public function getColumns() + { + return array( + array( + 'Field' => 'foobar_id', + 'Type' => 'int', + 'Key' => 'PRI', + 'Default' => null, + 'Null' => 1 + ), + array( + 'Field' => 'foobar_title', + 'Type' => 'vachar', + 'Key' => null, + 'Default' => null, + 'Null' => 1 + ), + array( + 'Field' => 'foobar_date', + 'Type' => 'datetime', + 'Key' => null, + 'Default' => null, + 'Null' => 1 + ) + ); + } + } +} diff --git a/test/Model.php b/test/Model.php deleted file mode 100644 index 274220b..0000000 --- a/test/Model.php +++ /dev/null @@ -1,351 +0,0 @@ -object = new Model(); - $this->object - ->setDatabase(new AbstractSqlStub) - ->setFoobarTitle('Foo Bar 1') - ->setFoobarDate('January 12, 2015') - ->setFooDate(1234567890); - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\Model::formatTime - */ - public function testFormatTime() - { - $actual = $this->object->formatTime('foobar_date'); - $this->assertEquals('2015-01-12 12:00:00', $actual->getFoobarDate()); - $actual = $this->object->formatTime('foo_date'); - $this->assertEquals('2009-02-13 11:31:30', $actual->getFooDate()); - $actual = $this->object->formatTime('foo_title'); - $this->assertEquals('Foo Bar 1', $actual->getFoobarTitle()); - } - - /** - * @covers Cradle\Storm\Model::insert - * @covers Cradle\Storm\Model::getMeta - * @covers Cradle\Storm\Model::getValidColumns - */ - public function testInsert() - { - $instance = $this->object->insert('foo'); - $this->assertInstanceOf('Cradle\Storm\Model', $instance); - $this->assertEquals(123, $this->object->getFoobarId()); - - $instance = $this->object->setTable('foo')->insert(); - $this->assertInstanceOf('Cradle\Storm\Model', $instance); - $this->assertEquals(123, $this->object->getFoobarId()); - - $triggered = false; - try { - $this->object = new Model(); - $this->object - ->setFoobarTitle('Foo Bar 1') - ->setFoobarDate('January 12, 2015') - ->setFooDate(1234567890) - ->insert(); - } catch(SqlException $e) { - $triggered = true; - } - - $this->assertTrue($triggered); - - $triggered = false; - try { - $this->object = new Model(); - $this->object - ->setFoobarTitle('Foo Bar 1') - ->setFoobarDate('January 12, 2015') - ->setFooDate(1234567890) - ->insert('foo'); - } catch(SqlException $e) { - $triggered = true; - } - - $this->assertTrue($triggered); - - $triggered = false; - try { - $this->object = new Model(); - $this->object - ->setFoobarTitle('Foo Bar 1') - ->setFoobarDate('January 12, 2015') - ->setFooDate(1234567890) - ->insert('foo', new AbstractSqlStub); - } catch(SqlException $e) { - $triggered = true; - } - - $this->assertFalse($triggered); - } - - /** - * @covers Cradle\Storm\Model::remove - * @covers Cradle\Storm\Model::getMeta - * @covers Cradle\Storm\Model::getValidColumns - */ - public function testRemove() - { - $instance = $this->object->setFoobarId(321)->remove('foo'); - $this->assertInstanceOf('Cradle\Storm\Model', $instance); - - $instance = $this->object->setFoobarId(321)->setTable('foo')->remove(); - $this->assertInstanceOf('Cradle\Storm\Model', $instance); - - $triggered = false; - try { - $this->object = new Model(); - $this->object - ->setFoobarId(321) - ->setFoobarTitle('Foo Bar 1') - ->setFoobarDate('January 12, 2015') - ->setFooDate(1234567890) - ->remove(); - } catch(SqlException $e) { - $triggered = true; - } - - $this->assertTrue($triggered); - - $triggered = false; - try { - $this->object = new Model(); - $this->object - ->setFoobarId(321) - ->setFoobarTitle('Foo Bar 1') - ->setFoobarDate('January 12, 2015') - ->setFooDate(1234567890) - ->remove('foo'); - } catch(SqlException $e) { - $triggered = true; - } - - $this->assertTrue($triggered); - - $triggered = false; - try { - $this->object = new Model(); - $this->object - ->setFoobarId(321) - ->setFoobarTitle('Foo Bar 1') - ->setFoobarDate('January 12, 2015') - ->setFooDate(1234567890) - ->remove('foo', new AbstractSqlStub, 'foobar_id'); - } catch(SqlException $e) { - $triggered = true; - } - - $this->assertFalse($triggered); - } - - /** - * @covers Cradle\Storm\Model::save - * @covers Cradle\Storm\Model::isPrimarySet - */ - public function testSave() - { - $instance = $this->object->save('foo'); - $this->assertInstanceOf('Cradle\Storm\Model', $instance); - - $instance = $this->object->setTable('foo')->save(); - $this->assertInstanceOf('Cradle\Storm\Model', $instance); - - $triggered = false; - try { - $this->object = new Model(); - $this->object - ->setFoobarTitle('Foo Bar 1') - ->setFoobarDate('January 12, 2015') - ->setFooDate(1234567890) - ->save(); - } catch(SqlException $e) { - $triggered = true; - } - - $this->assertTrue($triggered); - - $triggered = false; - try { - $this->object = new Model(); - $this->object - ->setFoobarTitle('Foo Bar 1') - ->setFoobarDate('January 12, 2015') - ->setFooDate(1234567890) - ->save('foo'); - } catch(SqlException $e) { - $triggered = true; - } - - $this->assertTrue($triggered); - - $triggered = false; - try { - $this->object = new Model(); - $this->object - ->setFoobarId(321) - ->setFoobarTitle('Foo Bar 1') - ->setFoobarDate('January 12, 2015') - ->setFooDate(1234567890) - ->save('foo', new AbstractSqlStub, 'foobar_id'); - } catch(SqlException $e) { - $triggered = true; - } - - $this->assertFalse($triggered); - } - - /** - * @covers Cradle\Storm\Model::setDatabase - */ - public function testSetDatabase() - { - $instance = $this->object->setDatabase(new AbstractSqlStub); - $this->assertInstanceOf('Cradle\Storm\Model', $instance); - } - - /** - * @covers Cradle\Storm\Model::setTable - */ - public function testSetTable() - { - $instance = $this->object->setTable('foo'); - $this->assertInstanceOf('Cradle\Storm\Model', $instance); - } - - /** - * @covers Cradle\Storm\Model::update - * @covers Cradle\Storm\Model::getMeta - * @covers Cradle\Storm\Model::getValidColumns - */ - public function testUpdate() - { - $instance = $this->object->setFoobarId(321)->update('foo'); - $this->assertInstanceOf('Cradle\Storm\Model', $instance); - - $instance = $this->object->setFoobarId(321)->setTable('foo')->update(); - $this->assertInstanceOf('Cradle\Storm\Model', $instance); - - $triggered = false; - try { - $this->object = new Model(); - $this->object - ->setFoobarTitle('Foo Bar 1') - ->setFoobarDate('January 12, 2015') - ->setFooDate(1234567890) - ->update(); - } catch(SqlException $e) { - $triggered = true; - } - - $this->assertTrue($triggered); - - $triggered = false; - try { - $this->object = new Model(); - $this->object - ->setFoobarTitle('Foo Bar 1') - ->setFoobarDate('January 12, 2015') - ->setFooDate(1234567890) - ->update('foo'); - } catch(SqlException $e) { - $triggered = true; - } - - $this->assertTrue($triggered); - - $triggered = false; - try { - $this->object = new Model(); - $this->object - ->setFoobarId(321) - ->setFoobarTitle('Foo Bar 1') - ->setFoobarDate('January 12, 2015') - ->setFooDate(1234567890) - ->update('foo', new AbstractSqlStub, 'foobar_id'); - } catch(SqlException $e) { - $triggered = true; - } - - $this->assertFalse($triggered); - } -} - -if(!class_exists('Cradle\Storm\AbstractSqlStub')) { - class AbstractSqlStub extends AbstractSql implements SqlInterface - { - public function connect($options = []) - { - $this->connection = 'foobar'; - return $this; - } - - public function getLastInsertedId($column = null) - { - return 123; - } - - public function query($query, array $binds = []) - { - return array(array( - 'total' => 123, - 'query' => (string) $query, - 'binds' => $binds - )); - } - - public function getColumns() - { - return array( - array( - 'Field' => 'foobar_id', - 'Type' => 'int', - 'Key' => 'PRI', - 'Default' => null, - 'Null' => 1 - ), - array( - 'Field' => 'foobar_title', - 'Type' => 'vachar', - 'Key' => null, - 'Default' => null, - 'Null' => 1 - ), - array( - 'Field' => 'foobar_date', - 'Type' => 'datetime', - 'Key' => null, - 'Default' => null, - 'Null' => 1 - ) - ); - } - } -} diff --git a/test/MySql.php b/test/MySql.php deleted file mode 100644 index ac89c00..0000000 --- a/test/MySql.php +++ /dev/null @@ -1,225 +0,0 @@ -object = SqlFactory::load(include(__DIR__.'/assets/mysql.php')); - $schema = file_get_contents(__DIR__.'/assets/mysql-schema.sql'); - $this->object->query($schema); - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\MySql::__construct - */ - public function test__construct() - { - $actual = $this->object->__construct('foo', 'foo', 'foo'); - $this->assertNull($actual); - } - - /** - * @covers Cradle\Storm\MySql::connect - */ - public function testConnect() - { - $instance = $this->object->connect(include(__DIR__.'/assets/mysql.php')); - $this->assertInstanceOf('Cradle\Storm\MySql', $instance); - - $this->object->__construct('127.0.0.1', 'testing_db', 'root'); - $instance = $this->object->connect(); - $this->assertInstanceOf('Cradle\Storm\MySql', $instance); - } - - /** - * @covers Cradle\Storm\MySql::getAlterQuery - */ - public function testGetAlterQuery() - { - $instance = $this->object->getAlterQuery('foobar'); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryAlter', $instance); - } - - /** - * @covers Cradle\Storm\MySql::getColumns - */ - public function testGetColumns() - { - $actual = $this->object->getColumns('address'); - $this->assertTrue(is_array($actual)); - - //$actual = $this->object->getColumns('address', array(array("Field LIKE 'address_%'"))); - //$this->assertTrue(is_array($actual)); - } - - /** - * @covers Cradle\Storm\MySql::getCreateQuery - */ - public function testGetCreateQuery() - { - $instance = $this->object->getCreateQuery('foobar'); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryCreate', $instance); - } - - /** - * @covers Cradle\Storm\MySql::getPrimaryKey - */ - public function testGetPrimaryKey() - { - $actual = $this->object->getPrimaryKey('address'); - $this->assertEquals('address_id', $actual); - } - - /** - * @covers Cradle\Storm\MySql::getSubSelectQuery - */ - public function testGetSubSelectQuery() - { - $instance = $this->object->getSubSelectQuery(new QuerySelect); - $this->assertInstanceOf('Cradle\Storm\MySql\QuerySubSelect', $instance); - } - - /** - * @covers Cradle\Storm\MySql::getTables - */ - public function testGetTables() - { - $actual = $this->object->getTables(); - $this->assertTrue(is_array($actual)); - } - - /** - * @covers Cradle\Storm\MySql::getTableSchema - */ - public function testGetTableSchema() - { - $this->object->insertRow('address', array( - 'address_label' => 'Foo Bar', - 'address_street' => 'foobar', - 'address_city' => 'foobar', - 'address_country' => 'foobar', - 'address_postal' => 'foobar', - 'address_created' => date('Y-m-d H:i:s'), - 'address_updated' => date('Y-m-d H:i:s') - )); - $actual = $this->object->getTableSchema('address'); - $this->assertContains('CREATE TABLE `address`', $actual); - } - - /** - * @covers Cradle\Storm\MySql::getUtilityQuery - */ - public function testGetUtilityQuery() - { - $instance = $this->object->getUtilityQuery(); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryUtility', $instance); - } - - /** - * @covers Cradle\Storm\AbstractSql::query - * @covers Cradle\Storm\Search::getRows - */ - public function testQuery() - { - $test = $this; - $triggered = false; - $instance = $this->object->query('SELECT * FROM address', array(), function($row) use ($test, &$triggered) { - $triggered = true; - $test->assertInstanceOf('Cradle\Storm\MySql', $this); - $test->assertEquals($row['address_label'], 'Foo Bar'); - return false; - }); - - $this->assertInstanceOf('Cradle\Storm\MySql', $instance); - $this->assertTrue($triggered); - - $row = $this->object->search('address')->getRow(); - $this->assertEquals($row['address_label'], 'Foo Bar'); - - $triggered = false; - $instance = $this->object->search('address')->getRows(function($row) use ($test, &$triggered) { - $triggered = true; - $test->assertInstanceOf('Cradle\Storm\MySql', $this); - $test->assertEquals($row['address_label'], 'Foo Bar'); - return false; - }); - - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - $this->assertTrue($triggered); - } - - /** - * @covers Cradle\Storm\AbstractSql::transaction - * @covers Cradle\Storm\Search::getTotal - */ - public function testTransaction() - { - $test = $this; - $triggered = false; - - $total = $this->object->search('address')->getTotal(); - - $this->object->transaction(function() use ($test, &$triggered) { - $triggered = true; - $test->assertInstanceOf('Cradle\Storm\MySql', $this); - - $this->insertRow('address', array( - 'address_label' => 'Foo Bar', - 'address_street' => 'foobar', - 'address_city' => 'foobar', - 'address_country' => 'foobar', - 'address_postal' => 'foobar', - 'address_created' => date('Y-m-d H:i:s'), - 'address_updated' => date('Y-m-d H:i:s') - )); - }); - - $this->assertTrue($triggered); - $this->assertEquals($total + 1, $this->object->search('address')->getTotal()); - - $triggered = false; - $this->object->transaction(function() use ($test, &$triggered) { - $triggered = true; - $test->assertInstanceOf('Cradle\Storm\MySql', $this); - - $this->insertRow('address', array( - 'address_label' => 'Foo Bar', - 'address_street' => 'foobar', - 'address_city' => 'foobar', - 'address_country' => 'foobar', - 'address_postal' => 'foobar', - 'address_created' => date('Y-m-d H:i:s'), - 'address_updated' => date('Y-m-d H:i:s') - )); - - return false; - }); - - $this->assertTrue($triggered); - $this->assertEquals($total + 1, $this->object->search('address')->getTotal()); - } -} diff --git a/test/MySql/QueryAlter.php b/test/MySql/QueryAlter.php deleted file mode 100644 index 8076ccf..0000000 --- a/test/MySql/QueryAlter.php +++ /dev/null @@ -1,172 +0,0 @@ -object = new QueryAlter('foobar'); - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\MySql\QueryAlter::__construct - */ - public function test__construct() - { - $actual = $this->object->__construct('foobar'); - - $this->assertNull($actual); - } - - /** - * @covers Cradle\Storm\MySql\QueryAlter::addField - */ - public function testAddField() - { - $instance = $this->object->addField('foobar', array()); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryAlter', $instance); - } - - /** - * @covers Cradle\Storm\MySql\QueryAlter::addKey - */ - public function testAddKey() - { - $instance = $this->object->addKey('foobar'); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryAlter', $instance); - } - - /** - * @covers Cradle\Storm\MySql\QueryAlter::addPrimaryKey - */ - public function testAddPrimaryKey() - { - $instance = $this->object->addPrimaryKey('foobar'); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryAlter', $instance); - } - - /** - * @covers Cradle\Storm\MySql\QueryAlter::addUniqueKey - */ - public function testAddUniqueKey() - { - $instance = $this->object->addUniqueKey('foobar', array()); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryAlter', $instance); - } - - /** - * @covers Cradle\Storm\MySql\QueryAlter::changeField - */ - public function testChangeField() - { - $instance = $this->object->changeField('foobar', array()); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryAlter', $instance); - } - - /** - * @covers Cradle\Storm\MySql\QueryAlter::getQuery - */ - public function testGetQuery() - { - $this->object->addField('foobar', array( - 'type' => 'varchar', - 'default' => 'something', - 'null' => true, - 'attribute' => 'unsigned', - 'length' => 255 - )); - - $this->object->changeField('foobar', array( - 'type' => 'varchar', - 'default' => 'something', - 'null' => true, - 'attribute' => 'unsigned', - 'length' => 255 - )); - - $this->object->addPrimaryKey('foobar'); - $this->object->addUniqueKey('foobar', array()); - $this->object->addKey('foobar'); - $this->object->removeField('foobar'); - $this->object->removeKey('foobar'); - $this->object->addPrimaryKey('foobar'); - $this->object->removeUniqueKey('foobar'); - $actual = $this->object->getQuery(); - $this->assertEquals('ALTER TABLE `foobar` DROP `foobar`, -ADD `foobar` varchar(255) unsigned DEFAULT NULL, -CHANGE `foobar` `foobar` varchar(255) unsigned DEFAULT NULL, -DROP INDEX `foobar`, -ADD INDEX (`foobar`), -DROP INDEX `foobar`, -ADD UNIQUE (`foobar`), -ADD PRIMARY KEY (`foobar`, `foobar`);', $actual); - } - - /** - * @covers Cradle\Storm\MySql\QueryAlter::removeField - */ - public function testRemoveField() - { - $instance = $this->object->removeField('foobar'); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryAlter', $instance); - } - - /** - * @covers Cradle\Storm\MySql\QueryAlter::removeKey - */ - public function testRemoveKey() - { - $instance = $this->object->removeKey('foobar'); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryAlter', $instance); - } - - /** - * @covers Cradle\Storm\MySql\QueryAlter::removePrimaryKey - */ - public function testRemovePrimaryKey() - { - $instance = $this->object->removePrimaryKey('foobar'); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryAlter', $instance); - } - - /** - * @covers Cradle\Storm\MySql\QueryAlter::removeUniqueKey - */ - public function testRemoveUniqueKey() - { - $instance = $this->object->removeUniqueKey('foobar'); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryAlter', $instance); - } - - /** - * @covers Cradle\Storm\MySql\QueryAlter::setName - */ - public function testSetName() - { - $instance = $this->object->setName('foobar'); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryAlter', $instance); - } -} diff --git a/test/MySql/QueryCreate.php b/test/MySql/QueryCreate.php deleted file mode 100644 index 42f006d..0000000 --- a/test/MySql/QueryCreate.php +++ /dev/null @@ -1,153 +0,0 @@ -object = new QueryCreate('foobar'); - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\MySql\QueryCreate::__construct - */ - public function test__construct() - { - $actual = $this->object->__construct('foobar'); - - $this->assertNull($actual); - } - - /** - * @covers Cradle\Storm\MySql\QueryCreate::addField - */ - public function testAddField() - { - $instance = $this->object->addField('foobar', array()); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryCreate', $instance); - } - - /** - * @covers Cradle\Storm\MySql\QueryCreate::addKey - */ - public function testAddKey() - { - $instance = $this->object->addKey('foobar', array()); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryCreate', $instance); - } - - /** - * @covers Cradle\Storm\MySql\QueryCreate::addPrimaryKey - */ - public function testAddPrimaryKey() - { - $instance = $this->object->addPrimaryKey('foobar'); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryCreate', $instance); - } - - /** - * @covers Cradle\Storm\MySql\QueryCreate::addUniqueKey - */ - public function testAddUniqueKey() - { - $instance = $this->object->addUniqueKey('foobar', array()); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryCreate', $instance); - } - - /** - * @covers Cradle\Storm\MySql\QueryCreate::getQuery - */ - public function testGetQuery() - { - $this->object->addField('foobar', array( - 'type' => 'varchar', - 'default' => 'something', - 'null' => true, - 'attribute' => 'unsigned', - 'length' => 255 - )); - $this->object->addKey('foobar', array('foobar')); - $this->object->addPrimaryKey('foobar'); - $this->object->addUniqueKey('foobar', array('foobar')); - $this->object->setComments('foobar'); - $actual = $this->object->getQuery(); - $this->assertEquals('CREATE TABLE `foobar` (`foobar` varchar(255) unsigned DEFAULT NULL, PRIMARY KEY (`foobar`), UNIQUE KEY `foobar` (`foobar`), KEY `foobar` (`foobar`));', $actual); - } - - /** - * @covers Cradle\Storm\MySql\QueryCreate::setComments - */ - public function testSetComments() - { - $instance = $this->object->setComments('foobar'); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryCreate', $instance); - } - - /** - * @covers Cradle\Storm\MySql\QueryCreate::setFields - */ - public function testSetFields() - { - $instance = $this->object->setFields(array('foobar')); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryCreate', $instance); - } - - /** - * @covers Cradle\Storm\MySql\QueryCreate::setKeys - */ - public function testSetKeys() - { - $instance = $this->object->setKeys(array('foobar')); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryCreate', $instance); - } - - /** - * @covers Cradle\Storm\MySql\QueryCreate::setName - */ - public function testSetName() - { - $instance = $this->object->setName('foobar'); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryCreate', $instance); - } - - /** - * @covers Cradle\Storm\MySql\QueryCreate::setPrimaryKeys - */ - public function testSetPrimaryKeys() - { - $instance = $this->object->setPrimaryKeys(array('foobar')); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryCreate', $instance); - } - - /** - * @covers Cradle\Storm\MySql\QueryCreate::setUniqueKeys - */ - public function testSetUniqueKeys() - { - $instance = $this->object->setUniqueKeys(array('foobar')); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryCreate', $instance); - } -} diff --git a/test/MySql/QuerySubSelect.php b/test/MySql/QuerySubSelect.php deleted file mode 100644 index bb1a5dc..0000000 --- a/test/MySql/QuerySubSelect.php +++ /dev/null @@ -1,61 +0,0 @@ -object = new QuerySubSelect(new QuerySelect); - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\MySql\QuerySubSelect::__construct - */ - public function test__construct() - { - $actual = $this->object->__construct(new QuerySelect); - $this->assertNull($actual); - } - - /** - * @covers Cradle\Storm\MySql\QuerySubSelect::getQuery - */ - public function testGetQuery() - { - $actual = $this->object->getQuery(); - $this->assertEquals('(SELECT * FROM )', $actual); - } - - /** - * @covers Cradle\Storm\MySql\QuerySubSelect::setParentQuery - */ - public function testSetParentQuery() - { - $instance = $this->object->setParentQuery(new QuerySelect); - $this->assertInstanceOf('Cradle\Storm\MySql\QuerySubSelect', $instance); - } -} diff --git a/test/MySql/QueryUtility.php b/test/MySql/QueryUtility.php deleted file mode 100644 index f0635b3..0000000 --- a/test/MySql/QueryUtility.php +++ /dev/null @@ -1,87 +0,0 @@ -object = new QueryUtility; - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\MySql\QueryUtility::dropTable - */ - public function testDropTable() - { - $instance = $this->object->dropTable('foobar'); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryUtility', $instance); - } - - /** - * @covers Cradle\Storm\MySql\QueryUtility::getQuery - */ - public function testGetQuery() - { - $actual = $this->object->getQuery(); - $this->assertEquals(';', $actual); - } - - /** - * @covers Cradle\Storm\MySql\QueryUtility::renameTable - */ - public function testRenameTable() - { - $instance = $this->object->renameTable('foo', 'bar'); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryUtility', $instance); - } - - /** - * @covers Cradle\Storm\MySql\QueryUtility::showColumns - */ - public function testShowColumns() - { - $instance = $this->object->showColumns('foobar'); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryUtility', $instance); - } - - /** - * @covers Cradle\Storm\MySql\QueryUtility::showTables - */ - public function testShowTables() - { - $instance = $this->object->showTables('foobar'); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryUtility', $instance); - } - - /** - * @covers Cradle\Storm\MySql\QueryUtility::truncate - */ - public function testTruncate() - { - $instance = $this->object->truncate('foobar'); - $this->assertInstanceOf('Cradle\Storm\MySql\QueryUtility', $instance); - } -} diff --git a/test/PostGreSql.php b/test/PostGreSql.php deleted file mode 100644 index ecdb3da..0000000 --- a/test/PostGreSql.php +++ /dev/null @@ -1,152 +0,0 @@ -object = SqlFactory::load(include(__DIR__.'/assets/pgsql.php')); - $schema = file_get_contents(__DIR__.'/assets/pgsql-schema.sql'); - $this->object->query($schema); - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\PostGreSql::connect - */ - public function testConnect() - { - $instance = $this->object->connect(include(__DIR__.'/assets/pgsql.php')); - $this->assertInstanceOf('Cradle\Storm\PostGreSql', $instance); - } - - /** - * @covers Cradle\Storm\PostGreSql::getAlterQuery - */ - public function testGetAlterQuery() - { - $instance = $this->object->getAlterQuery('foobar'); - $this->assertInstanceOf('Cradle\Storm\PostGreSql\QueryAlter', $instance); - } - - /** - * @covers Cradle\Storm\PostGreSql::getColumns - */ - public function testGetColumns() - { - $actual = $this->object->getColumns('address'); - $this->assertTrue(is_array($actual)); - } - - /** - * @covers Cradle\Storm\PostGreSql::getCreateQuery - */ - public function testGetCreateQuery() - { - $instance = $this->object->getCreateQuery('foobar'); - $this->assertInstanceOf('Cradle\Storm\PostGreSql\QueryCreate', $instance); - } - - /** - * @covers Cradle\Storm\PostGreSql::getDeleteQuery - */ - public function testGetDeleteQuery() - { - $instance = $this->object->getDeleteQuery('foobar'); - $this->assertInstanceOf('Cradle\Storm\PostGreSql\QueryDelete', $instance); - } - - /** - * @covers Cradle\Storm\PostGreSql::getIndexes - */ - public function testGetIndexes() - { - $actual = $this->object->getIndexes('address'); - $this->assertTrue(is_array($actual)); - } - - /** - * @covers Cradle\Storm\PostGreSql::getInsertQuery - */ - public function testGetInsertQuery() - { - $instance = $this->object->getInsertQuery('foobar'); - $this->assertInstanceOf('Cradle\Storm\PostGreSql\QueryInsert', $instance); - } - - /** - * @covers Cradle\Storm\PostGreSql::getPrimary - */ - public function testGetPrimary() - { - $actual = $this->object->getPrimary('address'); - $this->assertTrue(is_array($actual)); - } - - /** - * @covers Cradle\Storm\PostGreSql::getSelectQuery - */ - public function testGetSelectQuery() - { - $instance = $this->object->getSelectQuery('foobar'); - $this->assertInstanceOf('Cradle\Storm\PostGreSql\QuerySelect', $instance); - } - - /** - * @covers Cradle\Storm\PostGreSql::getTables - */ - public function testGetTables() - { - $actual = $this->object->getTables(); - $this->assertEquals('address', $actual[0]['tablename']); - } - - /** - * @covers Cradle\Storm\PostGreSql::getUpdateQuery - */ - public function testGetUpdateQuery() - { - $instance = $this->object->getUpdateQuery('foobar'); - $this->assertInstanceOf('Cradle\Storm\PostGreSql\QueryUpdate', $instance); - } - - /** - * @covers Cradle\Storm\PostGreSql::setSchema - */ - public function testSetSchema() - { - $instance = $this->object->setSchema('public'); - $this->assertInstanceOf('Cradle\Storm\PostGreSql', $instance); - } - - /** - * @covers Cradle\Storm\PostGreSql::getUtilityQuery - */ - public function testGetUtilityQuery() - { - $instance = $this->object->getUtilityQuery(); - $this->assertInstanceOf('Cradle\Storm\PostGreSql\QueryUtility', $instance); - } -} diff --git a/test/PostGreSql/QueryAlter.php b/test/PostGreSql/QueryAlter.php deleted file mode 100644 index 54468fc..0000000 --- a/test/PostGreSql/QueryAlter.php +++ /dev/null @@ -1,144 +0,0 @@ -object = new QueryAlter('foobar'); - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryAlter::__construct - */ - public function test__construct() - { - $actual = $this->object->__construct('foobar'); - - $this->assertNull($actual); - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryAlter::addField - */ - public function testAddField() - { - $instance = $this->object->addField('foobar', array()); - $this->assertInstanceOf('Cradle\Storm\PostGreSql\QueryAlter', $instance); - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryAlter::addPrimaryKey - */ - public function testAddPrimaryKey() - { - $instance = $this->object->addPrimaryKey('foobar'); - $this->assertInstanceOf('Cradle\Storm\PostGreSql\QueryAlter', $instance); - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryAlter::changeField - */ - public function testChangeField() - { - $instance = $this->object->changeField('foobar', array()); - $this->assertInstanceOf('Cradle\Storm\PostGreSql\QueryAlter', $instance); - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryAlter::getQuery - */ - public function testGetQuery() - { - - $actual = $this->object->getQuery(); - $this->assertEquals('ALTER TABLE "foobar" ;', $actual); - - $actual = $this->object - ->addPrimaryKey('foobar') - ->changeField('foobar', array()) - ->removeField('foobar') - ->removePrimaryKey('foobar') - ->setName('foobar') - ->getQuery(); - - $this->assertEquals('ALTER TABLE "foobar" DROP COLUMN "foobar", -ALTER COLUMN "foobar", -DROP PRIMARY KEY "foobar", -ADD PRIMARY KEY ("foobar");', $actual); - - $this->object->addField('foobar', array( - 'type' => 'varchar', - 'default' => 'something', - 'null' => true, - 'attribute' => 'unsigned', - 'length' => 255 - )); - - $this->object->changeField('foobar', array( - 'type' => 'varchar', - 'default' => 'something', - 'null' => true, - 'attribute' => 'unsigned', - 'length' => 255 - )); - - $actual = $this->object->getQuery(); - $this->assertEquals('ALTER TABLE "foobar" DROP COLUMN "foobar", -ADD "foobar" varchar(255) unsigned DEFAULT NULL, -ALTER COLUMN "foobar" varchar(255) unsigned DEFAULT NULL, -DROP PRIMARY KEY "foobar", -ADD PRIMARY KEY ("foobar");', $actual); - - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryAlter::removeField - */ - public function testRemoveField() - { - $instance = $this->object->removeField('foobar'); - $this->assertInstanceOf('Cradle\Storm\PostGreSql\QueryAlter', $instance); - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryAlter::removePrimaryKey - */ - public function testRemovePrimaryKey() - { - $instance = $this->object->removePrimaryKey('foobar'); - $this->assertInstanceOf('Cradle\Storm\PostGreSql\QueryAlter', $instance); - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryAlter::setName - */ - public function testSetName() - { - $instance = $this->object->setName('foobar'); - $this->assertInstanceOf('Cradle\Storm\PostGreSql\QueryAlter', $instance); - } -} diff --git a/test/PostGreSql/QueryCreate.php b/test/PostGreSql/QueryCreate.php deleted file mode 100644 index ae4df8b..0000000 --- a/test/PostGreSql/QueryCreate.php +++ /dev/null @@ -1,105 +0,0 @@ -object = new QueryCreate('foobar'); - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryCreate::addField - */ - public function testAddField() - { - $instance = $this->object->addField('foobar', array()); - $this->assertInstanceOf('Cradle\Storm\PostGreSql\QueryCreate', $instance); - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryCreate::addPrimaryKey - */ - public function testAddPrimaryKey() - { - $instance = $this->object->addPrimaryKey('foobar', array()); - $this->assertInstanceOf('Cradle\Storm\PostGreSql\QueryCreate', $instance); - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryCreate::getQuery - */ - public function testGetQuery() - { - $this->object->addField('foobar', array( - 'type' => 'varchar', - 'default' => 'something', - 'null' => true, - 'attribute' => 'unsigned', - 'length' => 255 - )); - $this->object->addPrimaryKey('foobar'); - $this->object->withOids(123); - $actual = $this->object->getQuery(); - $this->assertEquals('CREATE TABLE "foobar" ("foobar" varchar(255) unsigned DEFAULT NULL, PRIMARY KEY ("foobar")) WITH OIDS;', $actual); - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryCreate::setFields - */ - public function testSetFields() - { - $instance = $this->object->setFields(array('foobar')); - $this->assertInstanceOf('Cradle\Storm\PostGreSql\QueryCreate', $instance); - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryCreate::setName - */ - public function testSetName() - { - $instance = $this->object->setName('foobar'); - $this->assertInstanceOf('Cradle\Storm\PostGreSql\QueryCreate', $instance); - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryCreate::setPrimaryKeys - */ - public function testSetPrimaryKeys() - { - $instance = $this->object->setPrimaryKeys(array('foobar')); - $this->assertInstanceOf('Cradle\Storm\PostGreSql\QueryCreate', $instance); - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryCreate::withOids - */ - public function testWithOids() - { - $instance = $this->object->withOids(123); - $this->assertInstanceOf('Cradle\Storm\PostGreSql\QueryCreate', $instance); - } -} diff --git a/test/PostGreSql/QueryDelete.php b/test/PostGreSql/QueryDelete.php deleted file mode 100644 index 2eea8a5..0000000 --- a/test/PostGreSql/QueryDelete.php +++ /dev/null @@ -1,52 +0,0 @@ -object = new QueryDelete('foobar'); - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryDelete::__construct - */ - public function test__construct() - { - $this->object->__construct('foobar'); - $actual = $this->object->getQuery(); - $this->assertEquals('DELETE FROM "foobar" WHERE ;', $actual); - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryDelete::getQuery - */ - public function testGetQuery() - { - $actual = $this->object->getQuery(); - $this->assertEquals('DELETE FROM "foobar" WHERE ;', $actual); - } -} diff --git a/test/PostGreSql/QueryInsert.php b/test/PostGreSql/QueryInsert.php deleted file mode 100644 index 7fc4c45..0000000 --- a/test/PostGreSql/QueryInsert.php +++ /dev/null @@ -1,51 +0,0 @@ -object = new QueryInsert; - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryInsert::getQuery - */ - public function testGetQuery() - { - $actual = $this->object->getQuery(); - $this->assertEquals('INSERT INTO "" ("") VALUES ;', $actual); - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryInsert::set - */ - public function testSet() - { - $instance = $this->object->set('foo', 'bar'); - $this->assertInstanceOf('Cradle\Storm\PostGreSql\QueryInsert', $instance); - } -} diff --git a/test/PostGreSql/QuerySelect.php b/test/PostGreSql/QuerySelect.php deleted file mode 100644 index 1141c89..0000000 --- a/test/PostGreSql/QuerySelect.php +++ /dev/null @@ -1,42 +0,0 @@ -object = new QuerySelect; - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\PostGreSql\QuerySelect::getQuery - */ - public function testGetQuery() - { - $actual = $this->object->getQuery(); - $this->assertEquals('SELECT * FROM ;', $actual); - } -} diff --git a/test/PostGreSql/QueryUpdate.php b/test/PostGreSql/QueryUpdate.php deleted file mode 100644 index 087cb7d..0000000 --- a/test/PostGreSql/QueryUpdate.php +++ /dev/null @@ -1,51 +0,0 @@ -object = new QueryUpdate; - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryUpdate::getQuery - */ - public function testGetQuery() - { - $actual = $this->object->getQuery(); - $this->assertEquals('UPDATE SET WHERE ;', $actual); - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryUpdate::set - */ - public function testSet() - { - $instance = $this->object->set('foo', 'bar'); - $this->assertInstanceOf('Cradle\Storm\PostGreSql\QueryUpdate', $instance); - } -} diff --git a/test/PostGreSql/QueryUtility.php b/test/PostGreSql/QueryUtility.php deleted file mode 100644 index 4c3c7d6..0000000 --- a/test/PostGreSql/QueryUtility.php +++ /dev/null @@ -1,78 +0,0 @@ -object = new QueryUtility; - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryUtility::dropTable - */ - public function testDropTable() - { - $instance = $this->object->dropTable('foobar'); - $this->assertInstanceOf('Cradle\Storm\PostGreSql\QueryUtility', $instance); - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryUtility::getQuery - */ - public function testGetQuery() - { - $actual = $this->object->getQuery(); - $this->assertEquals(';', $actual); - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryUtility::renameTable - */ - public function testRenameTable() - { - $instance = $this->object->renameTable('foo', 'bar'); - $this->assertInstanceOf('Cradle\Storm\PostGreSql\QueryUtility', $instance); - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryUtility::setSchema - */ - public function testSetSchema() - { - $instance = $this->object->setSchema('foobar'); - $this->assertInstanceOf('Cradle\Storm\PostGreSql\QueryUtility', $instance); - } - - /** - * @covers Cradle\Storm\PostGreSql\QueryUtility::truncate - */ - public function testTruncate() - { - $instance = $this->object->truncate('foobar'); - $this->assertInstanceOf('Cradle\Storm\PostGreSql\QueryUtility', $instance); - } -} diff --git a/test/Query/AbstractQuery.php b/test/Query/AbstractQuery.php new file mode 100644 index 0000000..5888fe6 --- /dev/null +++ b/test/Query/AbstractQuery.php @@ -0,0 +1,51 @@ +object = new AbstractQueryStub; + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\AbstractQuery::__toString + */ + public function test__toString() + { + $this->assertEquals('foobar', $this->object->__toString()); + } +} + +if(!class_exists('Cradle\Storm\AbstractQueryStub')) { + class AbstractQueryStub extends AbstractQuery + { + public function getQuery(): string + { + return 'foobar'; + } + } +} diff --git a/test/Query/Delete.php b/test/Query/Delete.php new file mode 100644 index 0000000..6835040 --- /dev/null +++ b/test/Query/Delete.php @@ -0,0 +1,60 @@ +object = new Delete('foobar'); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\Query\Delete::getQuery + */ + public function testGetQuery() + { + $actual = $this->object->getQuery(); + $this->assertEquals('DELETE FROM foobar ;', $actual); + } + + /** + * @covers Cradle\Storm\Query\Delete::setTable + */ + public function testSetTable() + { + $instance = $this->object->setTable('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\Delete', $instance); + } + + /** + * @covers Cradle\Storm\Query\Delete::where + */ + public function testWhere() + { + $instance = $this->object->where('foo=bar'); + $this->assertInstanceOf('Cradle\Storm\Query\Delete', $instance); + } +} diff --git a/test/Query/Insert.php b/test/Query/Insert.php new file mode 100644 index 0000000..49ba9fd --- /dev/null +++ b/test/Query/Insert.php @@ -0,0 +1,60 @@ +object = new Insert('foobar'); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\Query\Insert::getQuery + */ + public function testGetQuery() + { + $actual = $this->object->getQuery(); + $this->assertEquals('INSERT INTO foobar () VALUES ;', $actual); + } + + /** + * @covers Cradle\Storm\Query\Insert::set + */ + public function testSet() + { + $instance = $this->object->set('foo', 'bar'); + $this->assertInstanceOf('Cradle\Storm\Query\Insert', $instance); + } + + /** + * @covers Cradle\Storm\Query\Insert::setTable + */ + public function testSetTable() + { + $instance = $this->object->setTable('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\Insert', $instance); + } +} diff --git a/test/Query/MySql/Alter.php b/test/Query/MySql/Alter.php new file mode 100644 index 0000000..76cd1eb --- /dev/null +++ b/test/Query/MySql/Alter.php @@ -0,0 +1,172 @@ +object = new Alter('foobar'); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\Query\MySql\Alter::__construct + */ + public function test__construct() + { + $actual = $this->object->__construct('foobar'); + + $this->assertNull($actual); + } + + /** + * @covers Cradle\Storm\Query\MySql\Alter::addField + */ + public function testAddField() + { + $instance = $this->object->addField('foobar', array()); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Alter', $instance); + } + + /** + * @covers Cradle\Storm\Query\MySql\Alter::addKey + */ + public function testAddKey() + { + $instance = $this->object->addKey('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Alter', $instance); + } + + /** + * @covers Cradle\Storm\Query\MySql\Alter::addPrimaryKey + */ + public function testAddPrimaryKey() + { + $instance = $this->object->addPrimaryKey('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Alter', $instance); + } + + /** + * @covers Cradle\Storm\Query\MySql\Alter::addUniqueKey + */ + public function testAddUniqueKey() + { + $instance = $this->object->addUniqueKey('foobar', array()); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Alter', $instance); + } + + /** + * @covers Cradle\Storm\Query\MySql\Alter::changeField + */ + public function testChangeField() + { + $instance = $this->object->changeField('foobar', array()); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Alter', $instance); + } + + /** + * @covers Cradle\Storm\Query\MySql\Alter::getQuery + */ + public function testGetQuery() + { + $this->object->addField('foobar', array( + 'type' => 'varchar', + 'default' => 'something', + 'null' => true, + 'attribute' => 'unsigned', + 'length' => 255 + )); + + $this->object->changeField('foobar', array( + 'type' => 'varchar', + 'default' => 'something', + 'null' => true, + 'attribute' => 'unsigned', + 'length' => 255 + )); + + $this->object->addPrimaryKey('foobar'); + $this->object->addUniqueKey('foobar', array()); + $this->object->addKey('foobar'); + $this->object->removeField('foobar'); + $this->object->removeKey('foobar'); + $this->object->addPrimaryKey('foobar'); + $this->object->removeUniqueKey('foobar'); + $actual = $this->object->getQuery(); + $this->assertEquals('ALTER TABLE `foobar` DROP `foobar`, +ADD `foobar` varchar(255) unsigned DEFAULT NULL, +CHANGE `foobar` `foobar` varchar(255) unsigned DEFAULT NULL, +DROP INDEX `foobar`, +ADD INDEX (`foobar`), +DROP INDEX `foobar`, +ADD UNIQUE (`foobar`), +ADD PRIMARY KEY (`foobar`, `foobar`);', $actual); + } + + /** + * @covers Cradle\Storm\Query\MySql\Alter::removeField + */ + public function testRemoveField() + { + $instance = $this->object->removeField('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Alter', $instance); + } + + /** + * @covers Cradle\Storm\Query\MySql\Alter::removeKey + */ + public function testRemoveKey() + { + $instance = $this->object->removeKey('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Alter', $instance); + } + + /** + * @covers Cradle\Storm\Query\MySql\Alter::removePrimaryKey + */ + public function testRemovePrimaryKey() + { + $instance = $this->object->removePrimaryKey('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Alter', $instance); + } + + /** + * @covers Cradle\Storm\Query\MySql\Alter::removeUniqueKey + */ + public function testRemoveUniqueKey() + { + $instance = $this->object->removeUniqueKey('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Alter', $instance); + } + + /** + * @covers Cradle\Storm\Query\MySql\Alter::setName + */ + public function testSetName() + { + $instance = $this->object->setName('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Alter', $instance); + } +} diff --git a/test/Query/MySql/Create.php b/test/Query/MySql/Create.php new file mode 100644 index 0000000..63cc824 --- /dev/null +++ b/test/Query/MySql/Create.php @@ -0,0 +1,153 @@ +object = new Create('foobar'); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\Query\MySql\Create::__construct + */ + public function test__construct() + { + $actual = $this->object->__construct('foobar'); + + $this->assertNull($actual); + } + + /** + * @covers Cradle\Storm\Query\MySql\Create::addField + */ + public function testAddField() + { + $instance = $this->object->addField('foobar', array()); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Create', $instance); + } + + /** + * @covers Cradle\Storm\Query\MySql\Create::addKey + */ + public function testAddKey() + { + $instance = $this->object->addKey('foobar', array()); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Create', $instance); + } + + /** + * @covers Cradle\Storm\Query\MySql\Create::addPrimaryKey + */ + public function testAddPrimaryKey() + { + $instance = $this->object->addPrimaryKey('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Create', $instance); + } + + /** + * @covers Cradle\Storm\Query\MySql\Create::addUniqueKey + */ + public function testAddUniqueKey() + { + $instance = $this->object->addUniqueKey('foobar', array()); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Create', $instance); + } + + /** + * @covers Cradle\Storm\Query\MySql\Create::getQuery + */ + public function testGetQuery() + { + $this->object->addField('foobar', array( + 'type' => 'varchar', + 'default' => 'something', + 'null' => true, + 'attribute' => 'unsigned', + 'length' => 255 + )); + $this->object->addKey('foobar', array('foobar')); + $this->object->addPrimaryKey('foobar'); + $this->object->addUniqueKey('foobar', array('foobar')); + $this->object->setComments('foobar'); + $actual = $this->object->getQuery(); + $this->assertEquals('CREATE TABLE `foobar` (`foobar` varchar(255) unsigned DEFAULT NULL, PRIMARY KEY (`foobar`), UNIQUE KEY `foobar` (`foobar`), KEY `foobar` (`foobar`));', $actual); + } + + /** + * @covers Cradle\Storm\Query\MySql\Create::setComments + */ + public function testSetComments() + { + $instance = $this->object->setComments('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Create', $instance); + } + + /** + * @covers Cradle\Storm\Query\MySql\Create::setFields + */ + public function testSetFields() + { + $instance = $this->object->setFields(array('foobar')); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Create', $instance); + } + + /** + * @covers Cradle\Storm\Query\MySql\Create::setKeys + */ + public function testSetKeys() + { + $instance = $this->object->setKeys(array('foobar')); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Create', $instance); + } + + /** + * @covers Cradle\Storm\Query\MySql\Create::setName + */ + public function testSetName() + { + $instance = $this->object->setName('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Create', $instance); + } + + /** + * @covers Cradle\Storm\Query\MySql\Create::setPrimaryKeys + */ + public function testSetPrimaryKeys() + { + $instance = $this->object->setPrimaryKeys(array('foobar')); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Create', $instance); + } + + /** + * @covers Cradle\Storm\Query\MySql\Create::setUniqueKeys + */ + public function testSetUniqueKeys() + { + $instance = $this->object->setUniqueKeys(array('foobar')); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Create', $instance); + } +} diff --git a/test/Query/MySql/Utility.php b/test/Query/MySql/Utility.php new file mode 100644 index 0000000..2655695 --- /dev/null +++ b/test/Query/MySql/Utility.php @@ -0,0 +1,87 @@ +object = new Utility; + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\Query\MySql\Utility::dropTable + */ + public function testDropTable() + { + $instance = $this->object->dropTable('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Utility', $instance); + } + + /** + * @covers Cradle\Storm\Query\MySql\Utility::getQuery + */ + public function testGetQuery() + { + $actual = $this->object->getQuery(); + $this->assertEquals(';', $actual); + } + + /** + * @covers Cradle\Storm\Query\MySql\Utility::renameTable + */ + public function testRenameTable() + { + $instance = $this->object->renameTable('foo', 'bar'); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Utility', $instance); + } + + /** + * @covers Cradle\Storm\Query\MySql\Utility::showColumns + */ + public function testShowColumns() + { + $instance = $this->object->showColumns('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Utility', $instance); + } + + /** + * @covers Cradle\Storm\Query\MySql\Utility::showTables + */ + public function testShowTables() + { + $instance = $this->object->showTables('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Utility', $instance); + } + + /** + * @covers Cradle\Storm\Query\MySql\Utility::truncate + */ + public function testTruncate() + { + $instance = $this->object->truncate('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\MySql\Utility', $instance); + } +} diff --git a/test/Query/PostGreSql/Alter.php b/test/Query/PostGreSql/Alter.php new file mode 100644 index 0000000..0aac689 --- /dev/null +++ b/test/Query/PostGreSql/Alter.php @@ -0,0 +1,144 @@ +object = new Alter('foobar'); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Alter::__construct + */ + public function test__construct() + { + $actual = $this->object->__construct('foobar'); + + $this->assertNull($actual); + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Alter::addField + */ + public function testAddField() + { + $instance = $this->object->addField('foobar', array()); + $this->assertInstanceOf('Cradle\Storm\Query\PostGreSql\Alter', $instance); + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Alter::addPrimaryKey + */ + public function testAddPrimaryKey() + { + $instance = $this->object->addPrimaryKey('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\PostGreSql\Alter', $instance); + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Alter::changeField + */ + public function testChangeField() + { + $instance = $this->object->changeField('foobar', array()); + $this->assertInstanceOf('Cradle\Storm\Query\PostGreSql\Alter', $instance); + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Alter::getQuery + */ + public function testGetQuery() + { + + $actual = $this->object->getQuery(); + $this->assertEquals('ALTER TABLE "foobar" ;', $actual); + + $actual = $this->object + ->addPrimaryKey('foobar') + ->changeField('foobar', array()) + ->removeField('foobar') + ->removePrimaryKey('foobar') + ->setName('foobar') + ->getQuery(); + + $this->assertEquals('ALTER TABLE "foobar" DROP COLUMN "foobar", +ALTER COLUMN "foobar", +DROP PRIMARY KEY "foobar", +ADD PRIMARY KEY ("foobar");', $actual); + + $this->object->addField('foobar', array( + 'type' => 'varchar', + 'default' => 'something', + 'null' => true, + 'attribute' => 'unsigned', + 'length' => 255 + )); + + $this->object->changeField('foobar', array( + 'type' => 'varchar', + 'default' => 'something', + 'null' => true, + 'attribute' => 'unsigned', + 'length' => 255 + )); + + $actual = $this->object->getQuery(); + $this->assertEquals('ALTER TABLE "foobar" DROP COLUMN "foobar", +ADD "foobar" varchar(255) unsigned DEFAULT NULL, +ALTER COLUMN "foobar" varchar(255) unsigned DEFAULT NULL, +DROP PRIMARY KEY "foobar", +ADD PRIMARY KEY ("foobar");', $actual); + + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Alter::removeField + */ + public function testRemoveField() + { + $instance = $this->object->removeField('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\PostGreSql\Alter', $instance); + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Alter::removePrimaryKey + */ + public function testRemovePrimaryKey() + { + $instance = $this->object->removePrimaryKey('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\PostGreSql\Alter', $instance); + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Alter::setName + */ + public function testSetName() + { + $instance = $this->object->setName('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\PostGreSql\Alter', $instance); + } +} diff --git a/test/Query/PostGreSql/Create.php b/test/Query/PostGreSql/Create.php new file mode 100644 index 0000000..cacf14b --- /dev/null +++ b/test/Query/PostGreSql/Create.php @@ -0,0 +1,105 @@ +object = new Create('foobar'); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Create::addField + */ + public function testAddField() + { + $instance = $this->object->addField('foobar', array()); + $this->assertInstanceOf('Cradle\Storm\Query\PostGreSql\Create', $instance); + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Create::addPrimaryKey + */ + public function testAddPrimaryKey() + { + $instance = $this->object->addPrimaryKey('foobar', array()); + $this->assertInstanceOf('Cradle\Storm\Query\PostGreSql\Create', $instance); + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Create::getQuery + */ + public function testGetQuery() + { + $this->object->addField('foobar', array( + 'type' => 'varchar', + 'default' => 'something', + 'null' => true, + 'attribute' => 'unsigned', + 'length' => 255 + )); + $this->object->addPrimaryKey('foobar'); + $this->object->withOids(123); + $actual = $this->object->getQuery(); + $this->assertEquals('CREATE TABLE "foobar" ("foobar" varchar(255) unsigned DEFAULT NULL, PRIMARY KEY ("foobar")) WITH OIDS;', $actual); + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Create::setFields + */ + public function testSetFields() + { + $instance = $this->object->setFields(array('foobar')); + $this->assertInstanceOf('Cradle\Storm\Query\PostGreSql\Create', $instance); + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Create::setName + */ + public function testSetName() + { + $instance = $this->object->setName('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\PostGreSql\Create', $instance); + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Create::setPrimaryKeys + */ + public function testSetPrimaryKeys() + { + $instance = $this->object->setPrimaryKeys(array('foobar')); + $this->assertInstanceOf('Cradle\Storm\Query\PostGreSql\Create', $instance); + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Create::withOids + */ + public function testWithOids() + { + $instance = $this->object->withOids(123); + $this->assertInstanceOf('Cradle\Storm\Query\PostGreSql\Create', $instance); + } +} diff --git a/test/Query/PostGreSql/Delete.php b/test/Query/PostGreSql/Delete.php new file mode 100644 index 0000000..a029e95 --- /dev/null +++ b/test/Query/PostGreSql/Delete.php @@ -0,0 +1,52 @@ +object = new Delete('foobar'); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Delete::__construct + */ + public function test__construct() + { + $this->object->__construct('foobar'); + $actual = $this->object->getQuery(); + $this->assertEquals('DELETE FROM "foobar" WHERE ;', $actual); + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Delete::getQuery + */ + public function testGetQuery() + { + $actual = $this->object->getQuery(); + $this->assertEquals('DELETE FROM "foobar" WHERE ;', $actual); + } +} diff --git a/test/Query/PostGreSql/Insert.php b/test/Query/PostGreSql/Insert.php new file mode 100644 index 0000000..d915983 --- /dev/null +++ b/test/Query/PostGreSql/Insert.php @@ -0,0 +1,51 @@ +object = new Insert; + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Insert::getQuery + */ + public function testGetQuery() + { + $actual = $this->object->getQuery(); + $this->assertEquals('INSERT INTO "" ("") VALUES ;', $actual); + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Insert::set + */ + public function testSet() + { + $instance = $this->object->set('foo', 'bar'); + $this->assertInstanceOf('Cradle\Storm\Query\PostGreSql\Insert', $instance); + } +} diff --git a/test/Query/PostGreSql/Select.php b/test/Query/PostGreSql/Select.php new file mode 100644 index 0000000..a3babd1 --- /dev/null +++ b/test/Query/PostGreSql/Select.php @@ -0,0 +1,42 @@ +object = new Select; + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Select::getQuery + */ + public function testGetQuery() + { + $actual = $this->object->getQuery(); + $this->assertEquals('SELECT * FROM ;', $actual); + } +} diff --git a/test/Query/PostGreSql/Update.php b/test/Query/PostGreSql/Update.php new file mode 100644 index 0000000..c34463b --- /dev/null +++ b/test/Query/PostGreSql/Update.php @@ -0,0 +1,51 @@ +object = new Update; + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Update::getQuery + */ + public function testGetQuery() + { + $actual = $this->object->getQuery(); + $this->assertEquals('UPDATE SET WHERE ;', $actual); + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Update::set + */ + public function testSet() + { + $instance = $this->object->set('foo', 'bar'); + $this->assertInstanceOf('Cradle\Storm\Query\PostGreSql\Update', $instance); + } +} diff --git a/test/Query/PostGreSql/Utility.php b/test/Query/PostGreSql/Utility.php new file mode 100644 index 0000000..0a82616 --- /dev/null +++ b/test/Query/PostGreSql/Utility.php @@ -0,0 +1,78 @@ +object = new Utility; + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Utility::dropTable + */ + public function testDropTable() + { + $instance = $this->object->dropTable('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\PostGreSql\Utility', $instance); + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Utility::getQuery + */ + public function testGetQuery() + { + $actual = $this->object->getQuery(); + $this->assertEquals(';', $actual); + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Utility::renameTable + */ + public function testRenameTable() + { + $instance = $this->object->renameTable('foo', 'bar'); + $this->assertInstanceOf('Cradle\Storm\Query\PostGreSql\Utility', $instance); + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Utility::setSchema + */ + public function testSetSchema() + { + $instance = $this->object->setSchema('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\PostGreSql\Utility', $instance); + } + + /** + * @covers Cradle\Storm\Query\PostGreSql\Utility::truncate + */ + public function testTruncate() + { + $instance = $this->object->truncate('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\PostGreSql\Utility', $instance); + } +} diff --git a/test/Query/Select.php b/test/Query/Select.php new file mode 100644 index 0000000..791549f --- /dev/null +++ b/test/Query/Select.php @@ -0,0 +1,114 @@ +object = new Select; + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\Query\Select::from + */ + public function testFrom() + { + $instance = $this->object->from('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\Select', $instance); + } + + /** + * @covers Cradle\Storm\Query\Select::getQuery + */ + public function testGetQuery() + { + $actual = $this->object->getQuery(); + $this->assertEquals('SELECT * FROM;', $actual); + } + + /** + * @covers Cradle\Storm\Query\Select::groupBy + */ + public function testGroupBy() + { + $instance = $this->object->groupBy('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\Select', $instance); + } + + /** + * @covers Cradle\Storm\Query\Select::having + */ + public function testHaving() + { + $instance = $this->object->having('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\Select', $instance); + } + + /** + * @covers Cradle\Storm\Query\Select::join + */ + public function testJoin() + { + $instance = $this->object->join('INNER', 'foobar', 'foo=bar'); + $this->assertInstanceOf('Cradle\Storm\Query\Select', $instance); + } + + /** + * @covers Cradle\Storm\Query\Select::limit + */ + public function testLimit() + { + $instance = $this->object->limit(0, 1); + $this->assertInstanceOf('Cradle\Storm\Query\Select', $instance); + } + + /** + * @covers Cradle\Storm\Query\Select::select + */ + public function testSelect() + { + $instance = $this->object->select('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\Select', $instance); + } + + /** + * @covers Cradle\Storm\Query\Select::sortBy + */ + public function testSortBy() + { + $instance = $this->object->sortBy('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\Select', $instance); + } + + /** + * @covers Cradle\Storm\Query\Select::where + */ + public function testWhere() + { + $instance = $this->object->where('foo=bar'); + $this->assertInstanceOf('Cradle\Storm\Query\Select', $instance); + } +} diff --git a/test/Query/Sqlite/Alter.php b/test/Query/Sqlite/Alter.php new file mode 100644 index 0000000..1300a0b --- /dev/null +++ b/test/Query/Sqlite/Alter.php @@ -0,0 +1,153 @@ +object = new Alter('foobar'); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Alter::__construct + */ + public function test__construct() + { + $actual = $this->object->__construct('foobar'); + + $this->assertNull($actual); + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Alter::addField + */ + public function testAddField() + { + $instance = $this->object->addField('foobar', array()); + $this->assertInstanceOf('Cradle\Storm\Query\Sqlite\Alter', $instance); + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Alter::addForeignKey + */ + public function testAddForeignKey() + { + $instance = $this->object->addForeignKey('foobar', 'foo', 'bar'); + $this->assertInstanceOf('Cradle\Storm\Query\Sqlite\Alter', $instance); + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Alter::addUniqueKey + */ + public function testAddUniqueKey() + { + $instance = $this->object->addUniqueKey('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\Sqlite\Alter', $instance); + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Alter::changeField + */ + public function testChangeField() + { + $instance = $this->object->changeField('foobar', array()); + $this->assertInstanceOf('Cradle\Storm\Query\Sqlite\Alter', $instance); + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Alter::getQuery + */ + public function testGetQuery() + { + $this->object->addField('foobar', array( + 'type' => 'varchar', + 'default' => 'something', + 'null' => true, + 'attribute' => 'unsigned', + 'length' => 255 + )); + + $this->object->changeField('foobar', array( + 'type' => 'varchar', + 'default' => 'something', + 'null' => true, + 'attribute' => 'unsigned', + 'length' => 255 + )); + + $this->object->addUniqueKey('foobar'); + $this->object->addForeignKey('foobar', 'foo', 'bar'); + $this->object->removeField('foobar'); + $this->object->removeForeignKey('foobar'); + $this->object->removeUniqueKey('foobar'); + + $actual = $this->object->getQuery(); + $this->assertEquals('ALTER TABLE "foobar" DROP "foobar", +ADD "foobar" varchar(255) unsigned DEFAULT NULL, +CHANGE "foobar" "foobar" varchar(255) unsigned DEFAULT NULL, +DROP FOREIGN KEY "foobar", +ADD FOREIGN KEY "foobar" REFERENCES foo(bar), +DROP UNIQUE "foobar", +ADD UNIQUE ("foobar");', $actual); + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Alter::removeField + * @todo Implement testRemoveField(). + */ + public function testRemoveField() + { + $instance = $this->object->removeField('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\Sqlite\Alter', $instance); + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Alter::removeForeignKey + */ + public function testRemoveForeignKey() + { + $instance = $this->object->removeForeignKey('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\Sqlite\Alter', $instance); + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Alter::removeUniqueKey + */ + public function testRemoveUniqueKey() + { + $instance = $this->object->removeUniqueKey('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\Sqlite\Alter', $instance); + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Alter::setName + */ + public function testSetName() + { + $instance = $this->object->setName('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\Sqlite\Alter', $instance); + } +} diff --git a/test/Query/Sqlite/Create.php b/test/Query/Sqlite/Create.php new file mode 100644 index 0000000..f0bf824 --- /dev/null +++ b/test/Query/Sqlite/Create.php @@ -0,0 +1,137 @@ +object = new Create('foobar'); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Create::__construct + */ + public function test__construct() + { + $actual = $this->object->__construct('foobar'); + + $this->assertNull($actual); + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Create::addField + */ + public function testAddField() + { + $instance = $this->object->addField('foobar', array()); + $this->assertInstanceOf('Cradle\Storm\Query\Sqlite\Create', $instance); + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Create::addForeignKey + */ + public function testAddForeignKey() + { + $instance = $this->object->addForeignKey('foobar', 'foo', 'bar'); + $this->assertInstanceOf('Cradle\Storm\Query\Sqlite\Create', $instance); + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Create::addUniqueKey + * @todo Implement testAddUniqueKey(). + */ + public function testAddUniqueKey() + { + $instance = $this->object->addUniqueKey('foobar', array()); + $this->assertInstanceOf('Cradle\Storm\Query\Sqlite\Create', $instance); + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Create::getQuery + */ + public function testGetQuery() + { + $this->object->addField('foobar', array( + 'type' => 'varchar', + 'default' => 'something', + 'null' => true, + 'attribute' => 'unsigned', + 'length' => 255 + )); + $this->object->addForeignKey('foobar', 'foo', 'bar'); + $this->object->addUniqueKey('foobar', array('foobar')); + $this->object->setComments('foobar'); + $actual = $this->object->getQuery(); + $this->assertEquals('CREATE TABLE "foobar" ("foobar" varchar(255) ' + . 'unsigned DEFAULT NULL, UNIQUE "foobar" ("foobar"), FOREIGN KEY ' + . '"foobar" REFERENCES foo(bar));', $actual); + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Create::setComments + */ + public function testSetComments() + { + $instance = $this->object->setComments('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\Sqlite\Create', $instance); + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Create::setFields + */ + public function testSetFields() + { + $instance = $this->object->setFields(array('foobar')); + $this->assertInstanceOf('Cradle\Storm\Query\Sqlite\Create', $instance); + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Create::setForiegnKeys + */ + public function testSetForiegnKeys() + { + $instance = $this->object->setForiegnKeys(array('foobar')); + $this->assertInstanceOf('Cradle\Storm\Query\Sqlite\Create', $instance); + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Create::setName + */ + public function testSetName() + { + $instance = $this->object->setName('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\Sqlite\Create', $instance); + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Create::setUniqueKeys + */ + public function testSetUniqueKeys() + { + $instance = $this->object->setUniqueKeys(array('foobar')); + $this->assertInstanceOf('Cradle\Storm\Query\Sqlite\Create', $instance); + } +} diff --git a/test/Query/Sqlite/Utility.php b/test/Query/Sqlite/Utility.php new file mode 100644 index 0000000..2100694 --- /dev/null +++ b/test/Query/Sqlite/Utility.php @@ -0,0 +1,87 @@ +object = new Utility; + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Utility::dropTable + */ + public function testDropTable() + { + $instance = $this->object->dropTable('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\Sqlite\Utility', $instance); + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Utility::getQuery + */ + public function testGetQuery() + { + $actual = $this->object->getQuery(); + $this->assertEquals(';', $actual); + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Utility::renameTable + */ + public function testRenameTable() + { + $instance = $this->object->renameTable('foo', 'bar'); + $this->assertInstanceOf('Cradle\Storm\Query\Sqlite\Utility', $instance); + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Utility::showColumns + */ + public function testShowColumns() + { + $instance = $this->object->showColumns('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\Sqlite\Utility', $instance); + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Utility::showTables + */ + public function testShowTables() + { + $instance = $this->object->showTables(); + $this->assertInstanceOf('Cradle\Storm\Query\Sqlite\Utility', $instance); + } + + /** + * @covers Cradle\Storm\Query\Sqlite\Utility::truncate + */ + public function testTruncate() + { + $instance = $this->object->truncate('foobar'); + $this->assertInstanceOf('Cradle\Storm\Query\Sqlite\Utility', $instance); + } +} diff --git a/test/Query/Update.php b/test/Query/Update.php new file mode 100644 index 0000000..50868c4 --- /dev/null +++ b/test/Query/Update.php @@ -0,0 +1,51 @@ +object = new Update('foobar'); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers Cradle\Storm\Query\Update::getQuery + */ + public function testGetQuery() + { + $actual = $this->object->getQuery(); + $this->assertEquals('UPDATE foobar SET ;', $actual); + } + + /** + * @covers Cradle\Storm\Query\Update::set + */ + public function testSet() + { + $instance = $this->object->set('foo', 'bar'); + $this->assertInstanceOf('Cradle\Storm\Query\Update', $instance); + } +} diff --git a/test/QueryDelete.php b/test/QueryDelete.php deleted file mode 100644 index 715cad9..0000000 --- a/test/QueryDelete.php +++ /dev/null @@ -1,60 +0,0 @@ -object = new QueryDelete('foobar'); - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\QueryDelete::getQuery - */ - public function testGetQuery() - { - $actual = $this->object->getQuery(); - $this->assertEquals('DELETE FROM foobar WHERE ;', $actual); - } - - /** - * @covers Cradle\Storm\QueryDelete::setTable - */ - public function testSetTable() - { - $instance = $this->object->setTable('foobar'); - $this->assertInstanceOf('Cradle\Storm\QueryDelete', $instance); - } - - /** - * @covers Cradle\Storm\QueryDelete::where - */ - public function testWhere() - { - $instance = $this->object->where('foo=bar'); - $this->assertInstanceOf('Cradle\Storm\QueryDelete', $instance); - } -} diff --git a/test/QueryInsert.php b/test/QueryInsert.php deleted file mode 100644 index cbd6162..0000000 --- a/test/QueryInsert.php +++ /dev/null @@ -1,60 +0,0 @@ -object = new QueryInsert('foobar'); - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\QueryInsert::getQuery - */ - public function testGetQuery() - { - $actual = $this->object->getQuery(); - $this->assertEquals('INSERT INTO foobar () VALUES ;', $actual); - } - - /** - * @covers Cradle\Storm\QueryInsert::set - */ - public function testSet() - { - $instance = $this->object->set('foo', 'bar'); - $this->assertInstanceOf('Cradle\Storm\QueryInsert', $instance); - } - - /** - * @covers Cradle\Storm\QueryInsert::setTable - */ - public function testSetTable() - { - $instance = $this->object->setTable('foobar'); - $this->assertInstanceOf('Cradle\Storm\QueryInsert', $instance); - } -} diff --git a/test/QuerySelect.php b/test/QuerySelect.php deleted file mode 100644 index 6e3d20d..0000000 --- a/test/QuerySelect.php +++ /dev/null @@ -1,150 +0,0 @@ -object = new QuerySelect; - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\QuerySelect::from - */ - public function testFrom() - { - $instance = $this->object->from('foobar'); - $this->assertInstanceOf('Cradle\Storm\QuerySelect', $instance); - } - - /** - * @covers Cradle\Storm\QuerySelect::getQuery - */ - public function testGetQuery() - { - $actual = $this->object->getQuery(); - $this->assertEquals('SELECT * FROM ;', $actual); - } - - /** - * @covers Cradle\Storm\QuerySelect::groupBy - */ - public function testGroupBy() - { - $instance = $this->object->groupBy('foobar'); - $this->assertInstanceOf('Cradle\Storm\QuerySelect', $instance); - } - - /** - * @covers Cradle\Storm\QuerySelect::having - */ - public function testHaving() - { - $instance = $this->object->having('foobar'); - $this->assertInstanceOf('Cradle\Storm\QuerySelect', $instance); - } - - /** - * @covers Cradle\Storm\QuerySelect::innerJoin - */ - public function testInnerJoin() - { - $instance = $this->object->innerJoin('foobar', 'foo=bar'); - $this->assertInstanceOf('Cradle\Storm\QuerySelect', $instance); - } - - /** - * @covers Cradle\Storm\QuerySelect::join - */ - public function testJoin() - { - $instance = $this->object->join('INNER', 'foobar', 'foo=bar'); - $this->assertInstanceOf('Cradle\Storm\QuerySelect', $instance); - } - - /** - * @covers Cradle\Storm\QuerySelect::leftJoin - */ - public function testLeftJoin() - { - $instance = $this->object->leftJoin('foobar', 'foo=bar'); - $this->assertInstanceOf('Cradle\Storm\QuerySelect', $instance); - } - - /** - * @covers Cradle\Storm\QuerySelect::limit - */ - public function testLimit() - { - $instance = $this->object->limit(0, 1); - $this->assertInstanceOf('Cradle\Storm\QuerySelect', $instance); - } - - /** - * @covers Cradle\Storm\QuerySelect::outerJoin - */ - public function testOuterJoin() - { - $instance = $this->object->outerJoin('foobar', 'foo=bar'); - $this->assertInstanceOf('Cradle\Storm\QuerySelect', $instance); - } - - /** - * @covers Cradle\Storm\QuerySelect::rightJoin - */ - public function testRightJoin() - { - $instance = $this->object->rightJoin('foobar', 'foo=bar'); - $this->assertInstanceOf('Cradle\Storm\QuerySelect', $instance); - } - - /** - * @covers Cradle\Storm\QuerySelect::select - */ - public function testSelect() - { - $instance = $this->object->select('foobar'); - $this->assertInstanceOf('Cradle\Storm\QuerySelect', $instance); - } - - /** - * @covers Cradle\Storm\QuerySelect::sortBy - */ - public function testSortBy() - { - $instance = $this->object->sortBy('foobar'); - $this->assertInstanceOf('Cradle\Storm\QuerySelect', $instance); - } - - /** - * @covers Cradle\Storm\QuerySelect::where - */ - public function testWhere() - { - $instance = $this->object->where('foo=bar'); - $this->assertInstanceOf('Cradle\Storm\QuerySelect', $instance); - } -} diff --git a/test/QueryUpdate.php b/test/QueryUpdate.php deleted file mode 100644 index 61e9bfa..0000000 --- a/test/QueryUpdate.php +++ /dev/null @@ -1,51 +0,0 @@ -object = new QueryUpdate('foobar'); - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\QueryUpdate::getQuery - */ - public function testGetQuery() - { - $actual = $this->object->getQuery(); - $this->assertEquals('UPDATE foobar SET WHERE ;', $actual); - } - - /** - * @covers Cradle\Storm\QueryUpdate::set - */ - public function testSet() - { - $instance = $this->object->set('foo', 'bar'); - $this->assertInstanceOf('Cradle\Storm\QueryUpdate', $instance); - } -} diff --git a/test/Search.php b/test/Search.php deleted file mode 100644 index 376e091..0000000 --- a/test/Search.php +++ /dev/null @@ -1,666 +0,0 @@ -object = new Search(new AbstractSqlStub); - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\Search::__construct - */ - public function test__construct() - { - $actual = $this->object->__construct(new AbstractSqlStub); - - $this->assertNull($actual); - } - - /** - * @covers Cradle\Storm\Search::__call - */ - public function test__call() - { - $instance = $this->object->filterByFoo('bar', '_'); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - $instance = $this->object->filterByFoo(); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - $instance = $this->object->filterByFoo([1, 2, 3]); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - - $instance = $this->object->sortByFoo('ASC', '_'); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - $instance = $this->object->sortByFoo(); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - - $trigger = false; - try { - $this->object->foobar(); - } catch(SqlException $e) { - $trigger = true; - } - - $this->assertTrue($trigger); - } - - /** - * @covers Cradle\Storm\Search::addFilter - */ - public function testAddFilter() - { - $instance = $this->object->addFilter('foo=1'); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - - $instance = $this->object->addFilter('foo=%s', 1); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - } - - /** - * @covers Cradle\Storm\Search::addSort - */ - public function testAddSort() - { - $instance = $this->object->addSort('bar', 'ASC'); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - } - - /** - * @covers Cradle\Storm\Search::getCollection - */ - public function testGetCollection() - { - $instance = $this->object->getCollection(); - $this->assertInstanceOf('Cradle\Storm\Collection', $instance); - } - - /** - * @covers Cradle\Storm\Search::getModel - */ - public function testGetModel() - { - $instance = $this->object->getModel(); - $this->assertInstanceOf('Cradle\Storm\Model', $instance); - } - - /** - * @covers Cradle\Storm\Search::getRow - */ - public function testGetRow() - { - $actual = $this->object->getRow(); - $this->assertEquals('SELECT * FROM ;', $actual['query']); - - $actual = $this->object->getRow('foobar'); - $this->assertNull($actual['query']); - } - - /** - * @covers Cradle\Storm\Search::getRows - */ - public function testGetRows() - { - $this->object->groupBy('foo'); - $this->object->setRange(4); - $this->object->addSort('bar', 'ASC'); - $actual = $this->object->getRows(); - $this->assertEquals('SELECT * FROM GROUP BY foo ORDER BY bar ASC LIMIT 0,4;', $actual[0]['query']); - } - - /** - * @covers Cradle\Storm\Search::getTotal - */ - public function testGetTotal() - { - $actual = $this->object->getTotal(); - $this->assertEquals(123, $actual); - } - - /** - * @covers Cradle\Storm\Search::groupBy - */ - public function testGroupBy() - { - $instance = $this->object->groupBy('foo'); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - } - - /** - * @covers Cradle\Storm\Search::having - */ - public function testHaving() - { - $instance = $this->object->having('foo'); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - } - - /** - * @covers Cradle\Storm\Search::innerJoinOn - */ - public function testInnerJoinOn() - { - $instance = $this->object->innerJoinOn('bar', 'bar_id=foo_id'); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - } - - /** - * @covers Cradle\Storm\Search::innerJoinUsing - */ - public function testInnerJoinUsing() - { - $instance = $this->object->innerJoinUsing('bar', 'bar_id=foo_id'); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - } - - /** - * @covers Cradle\Storm\Search::leftJoinOn - */ - public function testLeftJoinOn() - { - $instance = $this->object->leftJoinOn('bar', 'bar_id=foo_id'); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - } - - /** - * @covers Cradle\Storm\Search::leftJoinUsing - */ - public function testLeftJoinUsing() - { - $instance = $this->object->leftJoinUsing('bar', 'bar_id=foo_id'); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - } - - /** - * @covers Cradle\Storm\Search::outerJoinOn - */ - public function testOuterJoinOn() - { - $instance = $this->object->outerJoinOn('bar', 'bar_id=foo_id'); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - } - - /** - * @covers Cradle\Storm\Search::outerJoinUsing - */ - public function testOuterJoinUsing() - { - $instance = $this->object->outerJoinUsing('bar', 'bar_id=foo_id'); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - } - - /** - * @covers Cradle\Storm\Search::rightJoinOn - */ - public function testRightJoinOn() - { - $instance = $this->object->rightJoinOn('bar', 'bar_id=foo_id'); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - } - - /** - * @covers Cradle\Storm\Search::rightJoinUsing - */ - public function testRightJoinUsing() - { - $instance = $this->object->rightJoinUsing('bar', 'bar_id=foo_id'); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - } - - /** - * @covers Cradle\Storm\Search::setColumns - */ - public function testSetColumns() - { - $instance = $this->object->setColumns('foo', 'bar'); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - } - - /** - * @covers Cradle\Storm\Search::setPage - */ - public function testSetPage() - { - $instance = $this->object->setPage(-4); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - } - - /** - * @covers Cradle\Storm\Search::setRange - */ - public function testSetRange() - { - $instance = $this->object->setRange(-4); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - } - - /** - * @covers Cradle\Storm\Search::setStart - */ - public function testSetStart() - { - $instance = $this->object->setStart(4); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - } - - /** - * @covers Cradle\Storm\Search::setTable - */ - public function testSetTable() - { - $instance = $this->object->setTable('foo'); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - } - - /** - * @covers Cradle\Storm\Search::getEventHandler - */ - public function testGetEventHandler() - { - $instance = $this->object->getEventHandler(); - $this->assertInstanceOf('Cradle\Event\EventHandler', $instance); - } - - /** - * @covers Cradle\Storm\Search::on - */ - public function testOn() - { - $trigger = new StdClass(); - $trigger->success = null; - - $callback = function() use ($trigger) { - $trigger->success = true; - }; - - $instance = $this - ->object - ->on('foobar', $callback) - ->trigger('foobar'); - - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - $this->assertTrue($trigger->success); - } - - /** - * @covers Cradle\Storm\Search::setEventHandler - */ - public function testSetEventHandler() - { - $instance = $this->object->setEventHandler(new EventHandler); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - } - - /** - * @covers Cradle\Storm\Search::trigger - */ - public function testTrigger() - { - $trigger = new StdClass(); - $trigger->success = null; - - $callback = function() use ($trigger) { - $trigger->success = true; - }; - - $instance = $this - ->object - ->on('foobar', $callback) - ->trigger('foobar'); - - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - $this->assertTrue($trigger->success); - } - - /** - * @covers Cradle\Storm\Search::i - */ - public function testI() - { - $instance1 = Search::i(new AbstractSqlStub); - $this->assertInstanceOf('Cradle\Storm\Search', $instance1); - - $instance2 = Search::i(new AbstractSqlStub); - $this->assertTrue($instance1 !== $instance2); - } - - /** - * @covers Cradle\Storm\Search::loop - */ - public function testLoop() - { - $self = $this; - $this->object->loop(function($i) use ($self) { - $self->assertInstanceOf('Cradle\Storm\Search', $this); - - if ($i == 2) { - return false; - } - }); - } - - /** - * @covers Cradle\Storm\Search::when - */ - public function testWhen() - { - $self = $this; - $test = 'Good'; - $this->object->when(function() use ($self) { - $self->assertInstanceOf('Cradle\Storm\Search', $this); - return false; - }, function() use ($self, &$test) { - $self->assertInstanceOf('Cradle\Storm\Search', $this); - $test = 'Bad'; - }); - } - - /** - * @covers Cradle\Storm\Search::getInspectorHandler - */ - public function testGetInspectorHandler() - { - $instance = $this->object->getInspectorHandler(); - $this->assertInstanceOf('Cradle\Profiler\InspectorHandler', $instance); - } - - /** - * @covers Cradle\Storm\Search::inspect - */ - public function testInspect() - { - ob_start(); - $this->object->inspect('foobar'); - $contents = ob_get_contents(); - ob_end_clean(); - - $this->assertEquals( - '
INSPECTING Variable:
foobar
', - $contents - ); - } - - /** - * @covers Cradle\Storm\Search::setInspectorHandler - */ - public function testSetInspectorHandler() - { - $instance = $this->object->setInspectorHandler(new InspectorHandler); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - } - - /** - * @covers Cradle\Storm\Search::addLogger - */ - public function testAddLogger() - { - $instance = $this->object->addLogger(function() {}); - $this->assertInstanceOf('Cradle\Storm\Search', $instance); - } - - /** - * @covers Cradle\Storm\Search::log - */ - public function testLog() - { - $trigger = new StdClass(); - $trigger->success = null; - $this->object->addLogger(function($trigger) { - $trigger->success = true; - }) - ->log($trigger); - - - $this->assertTrue($trigger->success); - } - - /** - * @covers Cradle\Storm\Search::loadState - */ - public function testLoadState() - { - $state1 = new Search(new AbstractSqlStub); - $state2 = new Search(new AbstractSqlStub); - - $state1->saveState('state1'); - $state2->saveState('state2'); - - $this->assertTrue($state2 === $state1->loadState('state2')); - $this->assertTrue($state1 === $state2->loadState('state1')); - } - - /** - * @covers Cradle\Storm\Search::saveState - */ - public function testSaveState() - { - $state1 = new Search(new AbstractSqlStub); - $state2 = new Search(new AbstractSqlStub); - - $state1->saveState('state1'); - $state2->saveState('state2'); - - $this->assertTrue($state2 === $state1->loadState('state2')); - $this->assertTrue($state1 === $state2->loadState('state1')); - } - - /** - * @covers Cradle\Storm\Search::__callResolver - */ - public function test__callResolver() - { - $actual = $this->object->addResolver(ResolverCallStub::class, function() {}); - $this->assertInstanceOf('Cradle\Storm\Search', $actual); - } - - /** - * @covers Cradle\Storm\Search::addResolver - */ - public function testAddResolver() - { - $actual = $this->object->addResolver(ResolverCallStub::class, function() {}); - $this->assertInstanceOf('Cradle\Storm\Search', $actual); - } - - /** - * @covers Cradle\Storm\Search::getResolverHandler - */ - public function testGetResolverHandler() - { - $actual = $this->object->getResolverHandler(); - $this->assertInstanceOf('Cradle\Resolver\ResolverHandler', $actual); - } - - /** - * @covers Cradle\Storm\Search::resolve - */ - public function testResolve() - { - $actual = $this->object->addResolver( - ResolverCallStub::class, - function() { - return new ResolverAddStub(); - } - ) - ->resolve(ResolverCallStub::class) - ->foo('bar'); - - $this->assertEquals('barfoo', $actual); - } - - /** - * @covers Cradle\Storm\Search::resolveShared - */ - public function testResolveShared() - { - $actual = $this - ->object - ->resolveShared(ResolverSharedStub::class) - ->reset() - ->foo('bar'); - - $this->assertEquals('barfoo', $actual); - - $actual = $this - ->object - ->resolveShared(ResolverSharedStub::class) - ->foo('bar'); - - $this->assertEquals('barbar', $actual); - } - - /** - * @covers Cradle\Storm\Search::resolveStatic - */ - public function testResolveStatic() - { - $actual = $this - ->object - ->resolveStatic( - ResolverStaticStub::class, - 'foo', - 'bar' - ); - - $this->assertEquals('barfoo', $actual); - } - - /** - * @covers Cradle\Storm\Search::setResolverHandler - */ - public function testSetResolverHandler() - { - $actual = $this->object->setResolverHandler(new ResolverHandler); - $this->assertInstanceOf('Cradle\Storm\Search', $actual); - } -} - -if(!class_exists('Cradle\Storm\AbstractSqlStub')) { - class AbstractSqlStub extends AbstractSql implements SqlInterface - { - public function connect($options = []) - { - $this->connection = 'foobar'; - return $this; - } - - public function getLastInsertedId($column = null) - { - return 123; - } - - public function query($query, array $binds = []) - { - return array(array( - 'total' => 123, - 'query' => (string) $query, - 'binds' => $binds - )); - } - - public function getColumns() - { - return array( - array( - 'Field' => 'foobar_id', - 'Type' => 'int', - 'Key' => 'PRI', - 'Default' => null, - 'Null' => 1 - ), - array( - 'Field' => 'foobar_title', - 'Type' => 'vachar', - 'Key' => null, - 'Default' => null, - 'Null' => 1 - ), - array( - 'Field' => 'foobar_date', - 'Type' => 'datetime', - 'Key' => null, - 'Default' => null, - 'Null' => 1 - ) - ); - } - } -} - -if(!class_exists('Cradle\Storm\ResolverCallStub')) { - class ResolverCallStub - { - public function foo($string) - { - return $string . 'foo'; - } - } -} - -if(!class_exists('Cradle\Storm\ResolverAddStub')) { - class ResolverAddStub - { - public function foo($string) - { - return $string . 'foo'; - } - } -} - -if(!class_exists('Cradle\Storm\ResolverSharedStub')) { - class ResolverSharedStub - { - public $name = 'foo'; - - public function foo($string) - { - $name = $this->name; - $this->name = $string; - return $string . $name; - } - - public function reset() - { - $this->name = 'foo'; - return $this; - } - } -} - -if(!class_exists('Cradle\Storm\ResolverStaticStub')) { - class ResolverStaticStub - { - public static function foo($string) - { - return $string . 'foo'; - } - } -} diff --git a/test/SqlException.php b/test/SqlException.php index d1254cf..f8020a3 100644 --- a/test/SqlException.php +++ b/test/SqlException.php @@ -9,33 +9,33 @@ */ class Cradle_Storm_SqlException_Test extends TestCase { - /** - * @var SqlException - */ - protected $object; + /** + * @var SqlException + */ + protected $object; - /** - * Sets up the fixture, for example, opens a network connection. - * This method is called before a test is executed. - */ - protected function setUp() - { - $this->object = new SqlException; - } + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + */ + protected function setUp() + { + $this->object = new SqlException; + } - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } - /** - * @covers Cradle\Storm\SqlException::forQueryError - */ - public function testForQueryError() - { + /** + * @covers Cradle\Storm\SqlException::forQueryError + */ + public function testForQueryError() + { $message = null; try { throw SqlException::forQueryError('foo', 'bar'); @@ -44,13 +44,13 @@ public function testForQueryError() } $this->assertEquals('foo Query: bar', $message); - } + } - /** - * @covers Cradle\Storm\SqlException::forTableNotSet - */ - public function testForTableNotSet() - { + /** + * @covers Cradle\Storm\SqlException::forTableNotSet + */ + public function testForTableNotSet() + { $message = null; try { throw SqlException::forTableNotSet(); @@ -59,13 +59,13 @@ public function testForTableNotSet() } $this->assertEquals('No default table set or was passed.', $message); - } + } - /** - * @covers Cradle\Storm\SqlException::forDatabaseNotSet - */ - public function testForDatabaseNotSet() - { + /** + * @covers Cradle\Storm\SqlException::forDatabaseNotSet + */ + public function testForDatabaseNotSet() + { $message = null; try { throw SqlException::forDatabaseNotSet(); @@ -74,13 +74,13 @@ public function testForDatabaseNotSet() } $this->assertEquals('No default database set or was passed.', $message); - } + } - /** - * @covers Cradle\Storm\SqlException::forUnknownPDO - */ - public function testForUnknownPDO() - { + /** + * @covers Cradle\Storm\SqlException::forUnknownPDO + */ + public function testForUnknownPDO() + { $message = null; try { throw SqlException::forUnknownPDO('foo'); @@ -89,5 +89,5 @@ public function testForUnknownPDO() } $this->assertEquals('Could not match an SQL handler with foo', $message); - } + } } diff --git a/test/SqlFactory.php b/test/SqlFactory.php index 31851b0..947b4a1 100644 --- a/test/SqlFactory.php +++ b/test/SqlFactory.php @@ -1,4 +1,4 @@ -assertInstanceOf('Cradle\Storm\MySql', $mysql); - - $pgsql = SqlFactory::load(include(__DIR__.'/assets/pgsql.php')); - - $this->assertInstanceOf('Cradle\Storm\PostGreSql', $pgsql); - - $sqlite = SqlFactory::load(include(__DIR__.'/assets/sqlite.php')); - - $this->assertInstanceOf('Cradle\Storm\Sqlite', $sqlite); - } + /** + * @covers Cradle\Storm\SqlFactory::load + */ + public function testLoad() + { + $mysql = SqlFactory::load(include(__DIR__.'/assets/mysql.php')); + $this->assertInstanceOf('Cradle\Storm\Engine\MySql', $mysql); + + //$pgsql = SqlFactory::load(include(__DIR__.'/assets/pgsql.php')); + //$this->assertInstanceOf('Cradle\Storm\Engine\PostGreSql', $pgsql); + + $sqlite = SqlFactory::load(include(__DIR__.'/assets/sqlite.php')); + $this->assertInstanceOf('Cradle\Storm\Engine\Sqlite', $sqlite); + } } diff --git a/test/Sqlite.php b/test/Sqlite.php deleted file mode 100644 index 065eaa2..0000000 --- a/test/Sqlite.php +++ /dev/null @@ -1,134 +0,0 @@ -object = SqlFactory::load($connection); - $connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\Sqlite::connect - */ - public function testConnect() - { - $instance = $this->object->connect(include(__DIR__.'/assets/sqlite.php')); - - $this->assertInstanceOf('Cradle\Storm\Sqlite', $instance); - } - - /** - * @covers Cradle\Storm\Sqlite::getAlterQuery - */ - public function testGetAlterQuery() - { - $instance = $this->object->getAlterQuery('foobar'); - $this->assertInstanceOf('Cradle\Storm\Sqlite\QueryAlter', $instance); - } - - /** - * @covers Cradle\Storm\Sqlite::getColumns - */ - public function testGetColumns() - { - $actual = $this->object->getColumns('unit_post'); - $this->assertTrue(is_array($actual)); - } - - /** - * @covers Cradle\Storm\Sqlite::getCreateQuery - */ - public function testGetCreateQuery() - { - $instance = $this->object->getCreateQuery('foobar'); - $this->assertInstanceOf('Cradle\Storm\Sqlite\QueryCreate', $instance); - } - - /** - * @covers Cradle\Storm\Sqlite::getPrimaryKey - */ - public function testGetPrimaryKey() - { - $actual = $this->object->getPrimaryKey('unit_post'); - $this->assertEquals('post_id', $actual); - } - - /** - * @covers Cradle\Storm\Sqlite::getTables - */ - public function testGetTables() - { - $actual = $this->object->getTables(); - $this->assertEquals('unit_post', $actual[0]['name']); - } - - /** - * @covers Cradle\Storm\Sqlite::insertRows - */ - public function testInsertRows() - { - $instance = $this->object->insertRows('unit_post', array( - array( - 'post_slug' => 'unit-test-2-'.md5(uniqid()), - 'post_title' => 'Unit Test 2', - 'post_detail' => 'Unit Test Detail 2', - 'post_published' => date('Y-m-d'), - 'post_created' => date('Y-m-d H:i:s'), - 'post_updated' => date('Y-m-d H:i:s')), - array( - 'post_slug' => 'unit-test-3-'.md5(uniqid()), - 'post_title' => 'Unit Test 3', - 'post_detail' => 'Unit Test Detail 3', - 'post_published' => date('Y-m-d'), - 'post_created' => date('Y-m-d H:i:s'), - 'post_updated' => date('Y-m-d H:i:s')) - )); - - $this->assertInstanceOf('Cradle\Storm\Sqlite', $instance); - } - - /** - * @covers Cradle\Storm\Sqlite::getSelectQuery - */ - public function testGetSelectQuery() - { - $instance = $this->object->getSelectQuery(); - $this->assertInstanceOf('Cradle\Storm\QuerySelect', $instance); - } - - /** - * @covers Cradle\Storm\Sqlite::getUtilityQuery - */ - public function testGetUtilityQuery() - { - $instance = $this->object->getUtilityQuery(); - $this->assertInstanceOf('Cradle\Storm\Sqlite\QueryUtility', $instance); - } -} diff --git a/test/Sqlite/QueryAlter.php b/test/Sqlite/QueryAlter.php deleted file mode 100644 index e7f806f..0000000 --- a/test/Sqlite/QueryAlter.php +++ /dev/null @@ -1,153 +0,0 @@ -object = new QueryAlter('foobar'); - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\Sqlite\QueryAlter::__construct - */ - public function test__construct() - { - $actual = $this->object->__construct('foobar'); - - $this->assertNull($actual); - } - - /** - * @covers Cradle\Storm\Sqlite\QueryAlter::addField - */ - public function testAddField() - { - $instance = $this->object->addField('foobar', array()); - $this->assertInstanceOf('Cradle\Storm\Sqlite\QueryAlter', $instance); - } - - /** - * @covers Cradle\Storm\Sqlite\QueryAlter::addForeignKey - */ - public function testAddForeignKey() - { - $instance = $this->object->addForeignKey('foobar', 'foo', 'bar'); - $this->assertInstanceOf('Cradle\Storm\Sqlite\QueryAlter', $instance); - } - - /** - * @covers Cradle\Storm\Sqlite\QueryAlter::addUniqueKey - */ - public function testAddUniqueKey() - { - $instance = $this->object->addUniqueKey('foobar'); - $this->assertInstanceOf('Cradle\Storm\Sqlite\QueryAlter', $instance); - } - - /** - * @covers Cradle\Storm\Sqlite\QueryAlter::changeField - */ - public function testChangeField() - { - $instance = $this->object->changeField('foobar', array()); - $this->assertInstanceOf('Cradle\Storm\Sqlite\QueryAlter', $instance); - } - - /** - * @covers Cradle\Storm\Sqlite\QueryAlter::getQuery - */ - public function testGetQuery() - { - $this->object->addField('foobar', array( - 'type' => 'varchar', - 'default' => 'something', - 'null' => true, - 'attribute' => 'unsigned', - 'length' => 255 - )); - - $this->object->changeField('foobar', array( - 'type' => 'varchar', - 'default' => 'something', - 'null' => true, - 'attribute' => 'unsigned', - 'length' => 255 - )); - - $this->object->addUniqueKey('foobar'); - $this->object->addForeignKey('foobar', 'foo', 'bar'); - $this->object->removeField('foobar'); - $this->object->removeForeignKey('foobar'); - $this->object->removeUniqueKey('foobar'); - - $actual = $this->object->getQuery(); - $this->assertEquals('ALTER TABLE "foobar" DROP "foobar", -ADD "foobar" varchar(255) unsigned DEFAULT NULL, -CHANGE "foobar" "foobar" varchar(255) unsigned DEFAULT NULL, -DROP FOREIGN KEY "foobar", -ADD FOREIGN KEY "foobar" REFERENCES foo(bar), -DROP UNIQUE "foobar", -ADD UNIQUE ("foobar");', $actual); - } - - /** - * @covers Cradle\Storm\Sqlite\QueryAlter::removeField - * @todo Implement testRemoveField(). - */ - public function testRemoveField() - { - $instance = $this->object->removeField('foobar'); - $this->assertInstanceOf('Cradle\Storm\Sqlite\QueryAlter', $instance); - } - - /** - * @covers Cradle\Storm\Sqlite\QueryAlter::removeForeignKey - */ - public function testRemoveForeignKey() - { - $instance = $this->object->removeForeignKey('foobar'); - $this->assertInstanceOf('Cradle\Storm\Sqlite\QueryAlter', $instance); - } - - /** - * @covers Cradle\Storm\Sqlite\QueryAlter::removeUniqueKey - */ - public function testRemoveUniqueKey() - { - $instance = $this->object->removeUniqueKey('foobar'); - $this->assertInstanceOf('Cradle\Storm\Sqlite\QueryAlter', $instance); - } - - /** - * @covers Cradle\Storm\Sqlite\QueryAlter::setName - */ - public function testSetName() - { - $instance = $this->object->setName('foobar'); - $this->assertInstanceOf('Cradle\Storm\Sqlite\QueryAlter', $instance); - } -} diff --git a/test/Sqlite/QueryCreate.php b/test/Sqlite/QueryCreate.php deleted file mode 100644 index c19caf9..0000000 --- a/test/Sqlite/QueryCreate.php +++ /dev/null @@ -1,137 +0,0 @@ -object = new QueryCreate('foobar'); - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\Sqlite\QueryCreate::__construct - */ - public function test__construct() - { - $actual = $this->object->__construct('foobar'); - - $this->assertNull($actual); - } - - /** - * @covers Cradle\Storm\Sqlite\QueryCreate::addField - */ - public function testAddField() - { - $instance = $this->object->addField('foobar', array()); - $this->assertInstanceOf('Cradle\Storm\Sqlite\QueryCreate', $instance); - } - - /** - * @covers Cradle\Storm\Sqlite\QueryCreate::addForeignKey - */ - public function testAddForeignKey() - { - $instance = $this->object->addForeignKey('foobar', 'foo', 'bar'); - $this->assertInstanceOf('Cradle\Storm\Sqlite\QueryCreate', $instance); - } - - /** - * @covers Cradle\Storm\Sqlite\QueryCreate::addUniqueKey - * @todo Implement testAddUniqueKey(). - */ - public function testAddUniqueKey() - { - $instance = $this->object->addUniqueKey('foobar', array()); - $this->assertInstanceOf('Cradle\Storm\Sqlite\QueryCreate', $instance); - } - - /** - * @covers Cradle\Storm\Sqlite\QueryCreate::getQuery - */ - public function testGetQuery() - { - $this->object->addField('foobar', array( - 'type' => 'varchar', - 'default' => 'something', - 'null' => true, - 'attribute' => 'unsigned', - 'length' => 255 - )); - $this->object->addForeignKey('foobar', 'foo', 'bar'); - $this->object->addUniqueKey('foobar', array('foobar')); - $this->object->setComments('foobar'); - $actual = $this->object->getQuery(); - $this->assertEquals('CREATE TABLE "foobar" ("foobar" varchar(255) ' - . 'unsigned DEFAULT NULL, UNIQUE "foobar" ("foobar"), FOREIGN KEY ' - . '"foobar" REFERENCES foo(bar));', $actual); - } - - /** - * @covers Cradle\Storm\Sqlite\QueryCreate::setComments - */ - public function testSetComments() - { - $instance = $this->object->setComments('foobar'); - $this->assertInstanceOf('Cradle\Storm\Sqlite\QueryCreate', $instance); - } - - /** - * @covers Cradle\Storm\Sqlite\QueryCreate::setFields - */ - public function testSetFields() - { - $instance = $this->object->setFields(array('foobar')); - $this->assertInstanceOf('Cradle\Storm\Sqlite\QueryCreate', $instance); - } - - /** - * @covers Cradle\Storm\Sqlite\QueryCreate::setForiegnKeys - */ - public function testSetForiegnKeys() - { - $instance = $this->object->setForiegnKeys(array('foobar')); - $this->assertInstanceOf('Cradle\Storm\Sqlite\QueryCreate', $instance); - } - - /** - * @covers Cradle\Storm\Sqlite\QueryCreate::setName - */ - public function testSetName() - { - $instance = $this->object->setName('foobar'); - $this->assertInstanceOf('Cradle\Storm\Sqlite\QueryCreate', $instance); - } - - /** - * @covers Cradle\Storm\Sqlite\QueryCreate::setUniqueKeys - */ - public function testSetUniqueKeys() - { - $instance = $this->object->setUniqueKeys(array('foobar')); - $this->assertInstanceOf('Cradle\Storm\Sqlite\QueryCreate', $instance); - } -} diff --git a/test/Sqlite/QueryUtility.php b/test/Sqlite/QueryUtility.php deleted file mode 100644 index d270e4e..0000000 --- a/test/Sqlite/QueryUtility.php +++ /dev/null @@ -1,87 +0,0 @@ -object = new QueryUtility; - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers Cradle\Storm\Sqlite\QueryUtility::dropTable - */ - public function testDropTable() - { - $instance = $this->object->dropTable('foobar'); - $this->assertInstanceOf('Cradle\Storm\Sqlite\QueryUtility', $instance); - } - - /** - * @covers Cradle\Storm\Sqlite\QueryUtility::getQuery - */ - public function testGetQuery() - { - $actual = $this->object->getQuery(); - $this->assertEquals(';', $actual); - } - - /** - * @covers Cradle\Storm\Sqlite\QueryUtility::renameTable - */ - public function testRenameTable() - { - $instance = $this->object->renameTable('foo', 'bar'); - $this->assertInstanceOf('Cradle\Storm\Sqlite\QueryUtility', $instance); - } - - /** - * @covers Cradle\Storm\Sqlite\QueryUtility::showColumns - */ - public function testShowColumns() - { - $instance = $this->object->showColumns('foobar'); - $this->assertInstanceOf('Cradle\Storm\Sqlite\QueryUtility', $instance); - } - - /** - * @covers Cradle\Storm\Sqlite\QueryUtility::showTables - */ - public function testShowTables() - { - $instance = $this->object->showTables(); - $this->assertInstanceOf('Cradle\Storm\Sqlite\QueryUtility', $instance); - } - - /** - * @covers Cradle\Storm\Sqlite\QueryUtility::truncate - */ - public function testTruncate() - { - $instance = $this->object->truncate('foobar'); - $this->assertInstanceOf('Cradle\Storm\Sqlite\QueryUtility', $instance); - } -} diff --git a/test/assets/mysql.php b/test/assets/mysql.php index 8936b58..7ed93a6 100755 --- a/test/assets/mysql.php +++ b/test/assets/mysql.php @@ -1,3 +1,3 @@ -return new PDO('mysql:host=127.0.0.1;dbname=testing_db', 'root', ''); \ No newline at end of file +return new PDO('mysql:host=127.0.0.1;dbname=testing_db', 'root', ''); diff --git a/test/assets/sqlite.db b/test/assets/sqlite.db old mode 100644 new mode 100755 index 401f948..b8541a6 Binary files a/test/assets/sqlite.db and b/test/assets/sqlite.db differ diff --git a/test/bootstrap.php b/test/bootstrap.php index 50f902b..ba18151 100644 --- a/test/bootstrap.php +++ b/test/bootstrap.php @@ -7,7 +7,7 @@ * distributed with this package. */ if(file_exists(__DIR__.'/../../../autoload.php')) { - require_once __DIR__.'/../../../autoload.php'; + require_once __DIR__.'/../../../autoload.php'; } else { - require_once __DIR__.'/../vendor/autoload.php'; + require_once __DIR__.'/../vendor/autoload.php'; } \ No newline at end of file