diff --git a/.gitignore b/.gitignore index c8aa69a4d..f6c86fa35 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ Thumbs.db /cache/*/* /lock/* /.vscode/settings.json +/vendor/**/.git diff --git a/classes/db.php b/classes/db.php index a760d4402..a30ffad31 100755 --- a/classes/db.php +++ b/classes/db.php @@ -1,27 +1,38 @@ prepare(...) etc - public function pdo_connect() { - + public static function get_dsn() { $db_port = Config::get(Config::DB_PORT) ? ';port=' . Config::get(Config::DB_PORT) : ''; $db_host = Config::get(Config::DB_HOST) ? ';host=' . Config::get(Config::DB_HOST) : ''; + return Config::get(Config::DB_TYPE) . ':dbname=' . Config::get(Config::DB_NAME) . $db_host . $db_port; + } + + // this really shouldn't be used unless a separate PDO connection is needed + // normal usage is Db::pdo()->prepare(...) etc + public function pdo_connect() : PDO { + try { - $pdo = new PDO(Config::get(Config::DB_TYPE) . ':dbname=' . Config::get(Config::DB_NAME) . $db_host . $db_port, + $pdo = new PDO(self::get_dsn(), Config::get(Config::DB_USER), Config::get(Config::DB_PASS)); } catch (Exception $e) { @@ -49,7 +60,7 @@ class Db return $pdo; } - public static function instance() { + public static function instance() : Db { if (self::$instance == null) self::$instance = new self(); @@ -60,7 +71,7 @@ class Db if (self::$instance == null) self::$instance = new self(); - if (!self::$instance->pdo) { + if (empty(self::$instance->pdo)) { self::$instance->pdo = self::$instance->pdo_connect(); } diff --git a/classes/pref/users.php b/classes/pref/users.php index 111cabdca..bf95886ad 100644 --- a/classes/pref/users.php +++ b/classes/pref/users.php @@ -14,9 +14,9 @@ class Pref_Users extends Handler_Administrative { $sth = $this->pdo->prepare("SELECT id, login, access_level, email FROM ttrss_users WHERE id = ?"); $sth->execute([$id]); - if ($row = $sth->fetch(PDO::FETCH_ASSOC)) { + if ($user = $sth->fetch(PDO::FETCH_ASSOC)) { print json_encode([ - "user" => $row, + "user" => $user, "access_level_names" => $access_level_names ]); } @@ -106,21 +106,22 @@ class Pref_Users extends Handler_Administrative { } function editSave() { - $login = clean($_REQUEST["login"]); - $uid = (int) clean($_REQUEST["id"]); - $access_level = (int) clean($_REQUEST["access_level"]); - $email = clean($_REQUEST["email"]); + $id = (int)$_REQUEST['id']; $password = clean($_REQUEST["password"]); + $user = ORM::for_table('ttrss_users')->find_one($id); - // no blank usernames - if (!$login) return; + if ($user) { + $login = clean($_REQUEST["login"]); - // forbid renaming admin - if ($uid == 1) $login = "admin"; + if ($id == 1) $login = "admin"; + if (!$login) return; - $sth = $this->pdo->prepare("UPDATE ttrss_users SET login = LOWER(?), - access_level = ?, email = ?, otp_enabled = false WHERE id = ?"); - $sth->execute([$login, $access_level, $email, $uid]); + $user->login = $login; + $user->access_level = (int) clean($_REQUEST["access_level"]); + $user->email = clean($_REQUEST["email"]); + + $user->save(); + } if ($password) { UserHelper::reset_password($uid, false, $password); @@ -194,11 +195,10 @@ class Pref_Users extends Handler_Administrative { $sort = "login"; } - $sort = $this->_validate_field($sort, - ["login", "access_level", "created", "num_feeds", "created", "last_login"], "login"); + if (!in_array($sort, ["login", "access_level", "created", "num_feeds", "created", "last_login"])) + $sort = "login"; if ($sort != "login") $sort = "$sort DESC"; - ?>
@@ -253,32 +253,28 @@ class Pref_Users extends Handler_Administrative { pdo->prepare("SELECT - tu.id, - login,access_level,email, - ".SUBSTRING_FOR_DATE."(last_login,1,16) as last_login, - ".SUBSTRING_FOR_DATE."(created,1,16) as created, - (SELECT COUNT(id) FROM ttrss_feeds WHERE owner_uid = tu.id) AS num_feeds - FROM - ttrss_users tu - WHERE - (:search = '' OR login LIKE :search) AND tu.id > 0 - ORDER BY $sort"); - $sth->execute([":search" => $user_search ? "%$user_search%" : ""]); + $users = ORM::for_table('ttrss_users') + ->table_alias('u') + ->left_outer_join("ttrss_feeds", ["owner_uid", "=", "u.id"], 'f') + ->select_expr('u.*,COUNT(f.id) AS num_feeds') + ->where_like("login", $user_search ? "%$user_search%" : "%") + ->order_by_expr($sort) + ->group_by_expr('u.id') + ->find_many(); - while ($row = $sth->fetch()) { ?> + foreach ($users as $user) { ?> - + - person - - - - + person + + + + @@ -288,11 +284,4 @@ class Pref_Users extends Handler_Administrative { =5.2.0" + }, + "require-dev": { + "ext-pdo_sqlite": "*", + "phpunit/phpunit": "^4.8" + }, + "type": "library", + "autoload": { + "classmap": [ + "idiorm.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-2-Clause", + "BSD-3-Clause", + "BSD-4-Clause" + ], + "authors": [ + { + "name": "Jamie Matthews", + "email": "jamie.matthews@gmail.com", + "homepage": "http://j4mie.org", + "role": "Developer" + }, + { + "name": "Simon Holywell", + "email": "treffynnon@php.net", + "homepage": "http://simonholywell.com", + "role": "Maintainer" + }, + { + "name": "Durham Hale", + "email": "me@durhamhale.com", + "homepage": "http://durhamhale.com", + "role": "Maintainer" + } + ], + "description": "A lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5", + "homepage": "http://j4mie.github.com/idiormandparis", + "keywords": [ + "idiorm", + "orm", + "query builder" + ], + "support": { + "issues": "https://github.com/j4mie/idiorm/issues", + "source": "https://github.com/j4mie/idiorm" + }, + "time": "2020-04-29T00:37:09+00:00" + }, { "name": "mervick/material-design-icons", "version": "2.2.0", diff --git a/vendor/composer/InstalledVersions.php b/vendor/composer/InstalledVersions.php index 584bd5959..e17d3b0a7 100644 --- a/vendor/composer/InstalledVersions.php +++ b/vendor/composer/InstalledVersions.php @@ -30,7 +30,8 @@ private static $installed = array ( 'aliases' => array ( ), - 'reference' => 'bada1601fc231ecde54a528a545611429d60af21', + 'reference' => 'f96abd2b52b9de5eac3651594ca32c6a4023c3e3', + 'dev-requirement' => true, 'name' => '__root__', ), 'versions' => @@ -42,7 +43,8 @@ private static $installed = array ( 'aliases' => array ( ), - 'reference' => 'bada1601fc231ecde54a528a545611429d60af21', + 'reference' => 'f96abd2b52b9de5eac3651594ca32c6a4023c3e3', + 'dev-requirement' => false, ), 'beberlei/assert' => array ( @@ -52,6 +54,7 @@ private static $installed = array ( array ( ), 'reference' => 'd63a6943fc4fd1a2aedb65994e3548715105abcf', + 'dev-requirement' => false, ), 'chillerlan/php-qrcode' => array ( @@ -61,6 +64,7 @@ private static $installed = array ( array ( ), 'reference' => 'd8bf297e6843a53aeaa8f3285ce04fc349d133d6', + 'dev-requirement' => false, ), 'chillerlan/php-settings-container' => array ( @@ -70,6 +74,17 @@ private static $installed = array ( array ( ), 'reference' => 'b9b0431dffd74102ee92348a63b4c33fc8ba639b', + 'dev-requirement' => false, + ), + 'j4mie/idiorm' => + array ( + 'pretty_version' => 'v1.5.7', + 'version' => '1.5.7.0', + 'aliases' => + array ( + ), + 'reference' => 'd23f97053ef5d0b988a02c6a71eb5c6118b2f5b4', + 'dev-requirement' => false, ), 'mervick/material-design-icons' => array ( @@ -79,6 +94,7 @@ private static $installed = array ( array ( ), 'reference' => '635435c8d3df3a6da3241648caf8a65d1c07cc1a', + 'dev-requirement' => false, ), 'paragonie/constant_time_encoding' => array ( @@ -88,6 +104,7 @@ private static $installed = array ( array ( ), 'reference' => 'f34c2b11eb9d2c9318e13540a1dbc2a3afbd939c', + 'dev-requirement' => false, ), 'spomky-labs/otphp' => array ( @@ -97,6 +114,7 @@ private static $installed = array ( array ( ), 'reference' => 'f44cce5a9db4b8da410215d992110482c931232f', + 'dev-requirement' => false, ), 'thecodingmachine/safe' => array ( @@ -106,6 +124,7 @@ private static $installed = array ( array ( ), 'reference' => 'a8ab0876305a4cdaef31b2350fcb9811b5608dbc', + 'dev-requirement' => false, ), ), ); @@ -125,7 +144,6 @@ foreach (self::getInstalled() as $installed) { $packages[] = array_keys($installed['versions']); } - if (1 === \count($packages)) { return $packages[0]; } @@ -141,11 +159,12 @@ return array_keys(array_flip(\call_user_func_array('array_merge', $packages))); -public static function isInstalled($packageName) + +public static function isInstalled($packageName, $includeDevRequirements = true) { foreach (self::getInstalled() as $installed) { if (isset($installed['versions'][$packageName])) { -return true; +return $includeDevRequirements || empty($installed['versions'][$packageName]['dev-requirement']); } } @@ -164,7 +183,6 @@ return false; - public static function satisfies(VersionParser $parser, $packageName, $constraint) { $constraint = $parser->parseConstraints($constraint); diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index b26f1b13b..429869e46 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -7,4 +7,9 @@ $baseDir = dirname($vendorDir); return array( 'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php', + 'IdiormMethodMissingException' => $vendorDir . '/j4mie/idiorm/idiorm.php', + 'IdiormResultSet' => $vendorDir . '/j4mie/idiorm/idiorm.php', + 'IdiormString' => $vendorDir . '/j4mie/idiorm/idiorm.php', + 'IdiormStringException' => $vendorDir . '/j4mie/idiorm/idiorm.php', + 'ORM' => $vendorDir . '/j4mie/idiorm/idiorm.php', ); diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index ac3554d3f..6f1c238b1 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -154,6 +154,11 @@ class ComposerStaticInit19fc2ff1c0f9a92279c7979386bb2056 public static $classMap = array ( 'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php', + 'IdiormMethodMissingException' => __DIR__ . '/..' . '/j4mie/idiorm/idiorm.php', + 'IdiormResultSet' => __DIR__ . '/..' . '/j4mie/idiorm/idiorm.php', + 'IdiormString' => __DIR__ . '/..' . '/j4mie/idiorm/idiorm.php', + 'IdiormStringException' => __DIR__ . '/..' . '/j4mie/idiorm/idiorm.php', + 'ORM' => __DIR__ . '/..' . '/j4mie/idiorm/idiorm.php', ); public static function getInitializer(ClassLoader $loader) diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json index e8e1eb8d1..7de696894 100644 --- a/vendor/composer/installed.json +++ b/vendor/composer/installed.json @@ -204,6 +204,75 @@ }, "install-path": "../chillerlan/php-settings-container" }, + { + "name": "j4mie/idiorm", + "version": "v1.5.7", + "version_normalized": "1.5.7.0", + "source": { + "type": "git", + "url": "https://github.com/j4mie/idiorm.git", + "reference": "d23f97053ef5d0b988a02c6a71eb5c6118b2f5b4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/j4mie/idiorm/zipball/d23f97053ef5d0b988a02c6a71eb5c6118b2f5b4", + "reference": "d23f97053ef5d0b988a02c6a71eb5c6118b2f5b4", + "shasum": "" + }, + "require": { + "php": ">=5.2.0" + }, + "require-dev": { + "ext-pdo_sqlite": "*", + "phpunit/phpunit": "^4.8" + }, + "time": "2020-04-29T00:37:09+00:00", + "type": "library", + "installation-source": "dist", + "autoload": { + "classmap": [ + "idiorm.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-2-Clause", + "BSD-3-Clause", + "BSD-4-Clause" + ], + "authors": [ + { + "name": "Jamie Matthews", + "email": "jamie.matthews@gmail.com", + "homepage": "http://j4mie.org", + "role": "Developer" + }, + { + "name": "Simon Holywell", + "email": "treffynnon@php.net", + "homepage": "http://simonholywell.com", + "role": "Maintainer" + }, + { + "name": "Durham Hale", + "email": "me@durhamhale.com", + "homepage": "http://durhamhale.com", + "role": "Maintainer" + } + ], + "description": "A lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5", + "homepage": "http://j4mie.github.com/idiormandparis", + "keywords": [ + "idiorm", + "orm", + "query builder" + ], + "support": { + "issues": "https://github.com/j4mie/idiorm/issues", + "source": "https://github.com/j4mie/idiorm" + }, + "install-path": "../j4mie/idiorm" + }, { "name": "mervick/material-design-icons", "version": "2.2.0", diff --git a/vendor/composer/installed.php b/vendor/composer/installed.php index 018e89903..59d40508a 100644 --- a/vendor/composer/installed.php +++ b/vendor/composer/installed.php @@ -6,7 +6,8 @@ 'aliases' => array ( ), - 'reference' => 'bada1601fc231ecde54a528a545611429d60af21', + 'reference' => 'f96abd2b52b9de5eac3651594ca32c6a4023c3e3', + 'dev-requirement' => true, 'name' => '__root__', ), 'versions' => @@ -18,7 +19,8 @@ 'aliases' => array ( ), - 'reference' => 'bada1601fc231ecde54a528a545611429d60af21', + 'reference' => 'f96abd2b52b9de5eac3651594ca32c6a4023c3e3', + 'dev-requirement' => false, ), 'beberlei/assert' => array ( @@ -28,6 +30,7 @@ array ( ), 'reference' => 'd63a6943fc4fd1a2aedb65994e3548715105abcf', + 'dev-requirement' => false, ), 'chillerlan/php-qrcode' => array ( @@ -37,6 +40,7 @@ array ( ), 'reference' => 'd8bf297e6843a53aeaa8f3285ce04fc349d133d6', + 'dev-requirement' => false, ), 'chillerlan/php-settings-container' => array ( @@ -46,6 +50,17 @@ array ( ), 'reference' => 'b9b0431dffd74102ee92348a63b4c33fc8ba639b', + 'dev-requirement' => false, + ), + 'j4mie/idiorm' => + array ( + 'pretty_version' => 'v1.5.7', + 'version' => '1.5.7.0', + 'aliases' => + array ( + ), + 'reference' => 'd23f97053ef5d0b988a02c6a71eb5c6118b2f5b4', + 'dev-requirement' => false, ), 'mervick/material-design-icons' => array ( @@ -55,6 +70,7 @@ array ( ), 'reference' => '635435c8d3df3a6da3241648caf8a65d1c07cc1a', + 'dev-requirement' => false, ), 'paragonie/constant_time_encoding' => array ( @@ -64,6 +80,7 @@ array ( ), 'reference' => 'f34c2b11eb9d2c9318e13540a1dbc2a3afbd939c', + 'dev-requirement' => false, ), 'spomky-labs/otphp' => array ( @@ -73,6 +90,7 @@ array ( ), 'reference' => 'f44cce5a9db4b8da410215d992110482c931232f', + 'dev-requirement' => false, ), 'thecodingmachine/safe' => array ( @@ -82,6 +100,7 @@ array ( ), 'reference' => 'a8ab0876305a4cdaef31b2350fcb9811b5608dbc', + 'dev-requirement' => false, ), ), ); diff --git a/vendor/j4mie/idiorm/CONTRIBUTING.md b/vendor/j4mie/idiorm/CONTRIBUTING.md new file mode 100644 index 000000000..914737217 --- /dev/null +++ b/vendor/j4mie/idiorm/CONTRIBUTING.md @@ -0,0 +1,16 @@ +--- +### Feature complete + +Idiorm is now considered to be feature complete as of version 1.5.0. Whilst it will continue to be maintained with bug fixes there will be no further new features added. + +**Please do not submit feature requests or pull requests adding new features as they will be closed without ceremony.** + +--- + +When making a pull request please include the following aspects: + +- Update the changelog in the README.markdown file to include details of the pull request +- If the documentation in the README or Sphinx docs needs to be amended please do so in the pull request +- Include unit tests for any changes - if it is a bug include at least one regression test + + diff --git a/vendor/j4mie/idiorm/README.markdown b/vendor/j4mie/idiorm/README.markdown new file mode 100644 index 000000000..eebb1a2d0 --- /dev/null +++ b/vendor/j4mie/idiorm/README.markdown @@ -0,0 +1,239 @@ +Idiorm +====== + +[![Build Status](https://travis-ci.org/j4mie/idiorm.png?branch=master)](https://travis-ci.org/j4mie/idiorm) [![Latest Stable Version](https://poser.pugx.org/j4mie/idiorm/v/stable.png)](https://packagist.org/packages/j4mie/idiorm) [![Total Downloads](https://poser.pugx.org/j4mie/idiorm/downloads.png)](https://packagist.org/packages/j4mie/idiorm) [![Code Climate](https://codeclimate.com/github/j4mie/idiorm/badges/gpa.svg)](https://codeclimate.com/github/j4mie/idiorm) + +[http://j4mie.github.com/idiormandparis/](http://j4mie.github.com/idiormandparis/) + +--- +### Feature/API complete + +Idiorm is now considered to be feature complete as of version 1.5.0. Whilst it will continue to be maintained with bug fixes there will be no further new features added from this point on. This means that if a pull request makes breaking changes to the API or requires anything other than a patch version bump of the library then it will not be merged. + +**Please do not submit feature requests or API breaking changes as they will be closed without ceremony.** + +--- + +A lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5 and above. + +Tested on PHP 5.2.0+ - may work on earlier versions with PDO and the correct database drivers. + +Released under a [BSD license](http://en.wikipedia.org/wiki/BSD_licenses). + +**See Also: [Paris](http://github.com/j4mie/paris), an Active Record implementation built on top of Idiorm.** + +Features +-------- + +* Makes simple queries and simple CRUD operations completely painless. +* Gets out of the way when more complex SQL is required. +* Built on top of [PDO](http://php.net/pdo). +* Uses [prepared statements](http://uk.php.net/manual/en/pdo.prepared-statements.php) throughout to protect against [SQL injection](http://en.wikipedia.org/wiki/SQL_injection) attacks. +* Requires no model classes, no XML configuration and no code generation: works out of the box, given only a connection string. +* Consists of one main class called `ORM`. Additional classes are prefixed with `Idiorm`. Minimal global namespace pollution. +* Database agnostic. Currently supports SQLite, MySQL, Firebird and PostgreSQL. May support others, please give it a try! +* Supports collections of models with method chaining to filter or apply actions to multiple results at once. +* Multiple connections supported +* PSR-1 compliant methods (any method can be called in camelCase instead of underscores eg. `find_many()` becomes `findMany()`) - you'll need PHP 5.3+ + +Documentation +------------- + +The documentation is hosted on Read the Docs: [idiorm.rtfd.org](http://idiorm.rtfd.org) + +### Building the Docs ### + +You will need to install [Sphinx](http://sphinx-doc.org/) and then in the docs folder run: + + make html + +The documentation will now be in docs/_build/html/index.html + +Let's See Some Code +------------------- + +```php +$user = ORM::for_table('user') + ->where_equal('username', 'j4mie') + ->find_one(); + +$user->first_name = 'Jamie'; +$user->save(); + +$tweets = ORM::for_table('tweet') + ->select('tweet.*') + ->join('user', array( + 'user.id', '=', 'tweet.user_id' + )) + ->where_equal('user.username', 'j4mie') + ->find_many(); + +foreach ($tweets as $tweet) { + echo $tweet->text; +} +``` + +Tests +----- + +Tests are written with PHPUnit and be run through composer + + composer test + +To make testing on PHP 5.2 (Idiorm maintains support back to this version of PHP) there +is a Docker setup in `./test/docker_for_php52` - check the readme in there for more. + +Changelog +--------- + +#### 1.5.7 - released 2020-04-29 + +* Fix argument order in call to join() [[CatalinFrancu](https://github.com/CatalinFrancu)] - [issue #357](https://github.com/j4mie/idiorm/pull/357) + +#### 1.5.6 - released 2018-05-31 + +* Assign `null` to `self::$_db` on `reset_db()` to ensure PDO closes the connections [[bleakgadfly](https://github.com/bleakgadfly)] - [issue #338](https://github.com/j4mie/idiorm/issues/338) + +#### 1.5.5 - released 2018-01-05 + +* Add a docker setup for testing with PHP 5.2 (uses PHPUnit 3.6.12, which is the last version released compatible with PHP 5.2) [[Treffynnon](https://github.com/treffynnon)] + +#### 1.5.4 - released 2018-01-04 + +* Reset Idiorm state when a cached result is returned [[fayland](https://github.com/fayland) (and [Treffynnon](https://github.com/treffynnon))] - [issue #319](https://github.com/j4mie/idiorm/issues/319) +* Fix travis builds for PHP 5.2+ (adding 7.0 and 7.1) and document support for newer PHP versions [[Treffynnon](https://github.com/treffynnon)] +* Correct PHPDoc comments for `selectMany()` [[kawausokun](https://github.com/kawausokun)] - [issue #325](github.com/j4mie/idiorm/issues/325) +* Add pdo_sqlite to the composer require-dev dependencies [[qyanu](https://github.com/qyanu)] - [issue #328](github.com/j4mie/idiorm/issues/328) + +#### 1.5.3 - released 2017-03-21 + +* Document the `raw_execute()` method and add a note for `get_db()` in the querying documentation - [[Treffynnon](https://github.com/treffynnon)] + +#### 1.5.2 - released 2016-12-14 + +* Fix autoincremented compound keys inserts [[lrlopez](https://github.com/lrlopez)] - [issue #233](https://github.com/j4mie/idiorm/issues/233) and [pull #235](https://github.com/j4mie/idiorm/pull/235) +* Add @method tags for magic methods [[stellis](https://github.com/stellis)] - [issue #237](https://github.com/j4mie/idiorm/issues/237) +* Ensure `is_dirty()` returns correctly when fed null or an empty string [[tentwofour](https://github.com/tentwofour)] - [issue #268](https://github.com/j4mie/idiorm/issues/268) +* Adding Code Climate badge to the readme file [[e3betht](https://github.com/e3betht)] - [issue #260](https://github.com/j4mie/idiorm/issues/260) +* Typo in navigation [[leongersen](https://github.com/leongersen)] - [issue #257](https://github.com/j4mie/idiorm/issues/257) +* Support named placeholders logging and test [[m92o](https://github.com/m92o)] - [issue #223](https://github.com/j4mie/idiorm/issues/223) +* `having_id_is()` reference undefined variable `$value` [[Treffynnon](https://github.com/treffynnon)] - [issue #224](https://github.com/j4mie/idiorm/issues/224) +* Documentation fix - ORM query output for `where_any_is()` [[uovidiu](https://github.com/uovidiu)] - [issue #306](https://github.com/j4mie/idiorm/issues/306) +* Code style fix preventing nested loops from using the same variable names [[mkkeck](https://github.com/mkkeck)] - [issue #301](https://github.com/j4mie/idiorm/issues/301) +* Document shortcomings of the built in query logger [[Treffynnon](https://github.com/treffynnon)] - [issue #307](https://github.com/j4mie/idiorm/issues/307) +* Add phpunit to dev dependencies, add `composer test` script shortcut and fix PDO mock in test bootstrap [[Treffynnon](https://github.com/treffynnon)] +* New test for multiple raw where clauses [[Treffynnon](https://github.com/treffynnon)] - [issue #236](https://github.com/j4mie/idiorm/issues/236) +* Remove PHP 5.2 from travis-ci containers to test against (**note** Idiorm still supports PHP 5.2 despite this) [[Treffynnon](https://github.com/treffynnon)] + +#### 1.5.1 - released 2014-06-23 + +* Binding of named parameters was broken [[cainmi](https://github.com/cainmi)] - [issue #221](https://github.com/j4mie/idiorm/pull/221) + +#### 1.5.0 - released 2014-06-22 + +* Multiple OR'ed conditions support [[lrlopez](https://github.com/lrlopez)] - [issue #201](https://github.com/j4mie/idiorm/issues/201) +* `where_id_in()` for selecting multiple records by primary key [[lrlopez](https://github.com/lrlopez)] - [issue #202](https://github.com/j4mie/idiorm/issues/202) +* Add compound primary key support [[lrlopez](https://github.com/lrlopez)] - [issue #171](https://github.com/j4mie/idiorm/issues/171) +* Add a RAW JOIN source to the query [[moiseevigor](https://github.com/moiseevigor)] - [issue #163](https://github.com/j4mie/idiorm/issues/163) +* offsetExists() should return true for null values, resolves [#181](https://github.com/j4mie/idiorm/issues/181) [[cainmi](https://github.com/cainmi)] - [issue #214](https://github.com/j4mie/idiorm/pull/214) +* Custom cache callback functions [[peter-mw](https://github.com/peter-mw)] - [issue #216](https://github.com/j4mie/idiorm/pull/216) +* Restrict null primary keys on update/delete, resolves [#203](https://github.com/j4mie/idiorm/issues/203) [[cainmi](https://github.com/cainmi)] - [issue #205](https://github.com/j4mie/idiorm/issues/205) +* Ensure parameters treated by type correctly [[charsleysa](https://github.com/charsleysa)] & [[SneakyBobito](https://github.com/SneakyBobito)] - [issue #206](https://github.com/j4mie/idiorm/issues/206) & [issue #208](https://github.com/j4mie/idiorm/issues/208) +* Reduce the type casting on aggregate functions to allow characters [[herroffizier](https://github.com/herroffizier)] - [issue #150](https://github.com/j4mie/idiorm/issues/150) +* Prevent invalid method calls from triggering infinite recursion [[michaelward82](https://github.com/michaelward82)] - [issue #152](https://github.com/j4mie/idiorm/issues/152) +* Add time to query logging - adds query time parameter to external logger callback function [[AgelxNash](https://github.com/AgelxNash)] - [issue #180](https://github.com/j4mie/idiorm/issues/180) +* Changed database array access to ensure it's always properly setup [[falmp](https://github.com/falmp)] - [issue #159](https://github.com/j4mie/idiorm/issues/159) +* Allow unsetting the db (`ORM::set_db(null)`) to make the test work again [[borrel](https://github.com/borrel)] - [issue #160](https://github.com/j4mie/idiorm/issues/160) +* Correct [issue #176](https://github.com/j4mie/idiorm/issues/176): Ensure database setup before building select [[kendru](https://github.com/kendru)] - [issue #197](https://github.com/j4mie/idiorm/issues/197) +* Add HHVM to travis-ci build matrix [[ptarjan](https://github.com/ptarjan)] - [issue #168](https://github.com/j4mie/idiorm/issues/168) +* Improve where statement precendence documentation [[thomasahle](https://github.com/thomasahle)] - [issue #190](https://github.com/j4mie/idiorm/issues/190) +* Improve testing checks [[charsleysa](https://github.com/charsleysa)] - [issue #173](https://github.com/j4mie/idiorm/issues/173) + +#### 1.4.1 - released 2013-12-12 + +**Patch update to remove a broken pull request** - may have consequences for users of 1.4.0 that exploited the "`find_many()` now returns an associative array with the databases primary ID as the array keys" change that was merged in 1.4.0. + +* Back out pull request/issue [#133](https://github.com/j4mie/idiorm/pull/133) as it breaks backwards compatibility in previously unexpected ways (see [#162](https://github.com/j4mie/idiorm/pull/162), [#156](https://github.com/j4mie/idiorm/issues/156) and [#133](https://github.com/j4mie/idiorm/pull/133#issuecomment-29063108)) - sorry for merging this change into Idiorm - closes [issue 156](https://github.com/j4mie/idiorm/issues/156) + +#### 1.4.0 - released 2013-09-05 + +* `find_many()` now returns an associative array with the databases primary ID as the array keys [[Surt](https://github.com/Surt)] - [issue #133](https://github.com/j4mie/idiorm/issues/133) +* Calls to `set()` and `set_expr()` return `$this` allowing them to be chained [[Surt](https://github.com/Surt)] +* Add PSR-1 compliant camelCase method calls to Idiorm (PHP 5.3+ required) [[crhayes](https://github.com/crhayes)] - [issue #108](https://github.com/j4mie/idiorm/issues/108) +* Add static method `get_config()` to access current configuration [[javierd](https://github.com/mikejestes)] - [issue #141](https://github.com/j4mie/idiorm/issues/141) +* Add logging callback functionality [[lalop](https://github.com/lalop)] - [issue #130](https://github.com/j4mie/idiorm/issues/130) +* Add support for MS SQL ``TOP`` limit style (automatically used for PDO drivers: sqlsrv, dblib and mssql) [[numkem](https://github.com/numkem)] - [issue #116](https://github.com/j4mie/idiorm/issues/116) +* Uses table aliases in `WHERE` clauses [[vicvicvic](https://github.com/vicvicvic)] - [issue #140](https://github.com/j4mie/idiorm/issues/140) +* Ignore result columns when calling an aggregate function [[tassoevan](https://github.com/tassoevan)] - [issue #120](https://github.com/j4mie/idiorm/issues/120) +* Improve documentation [[bruston](https://github.com/bruston)] - [issue #111](https://github.com/j4mie/idiorm/issues/111) +* Improve PHPDoc on `get_db()` [[mailopl](https://github.com/mailopl)] - [issue #106](https://github.com/j4mie/idiorm/issues/106) +* Improve documentation [[sjparsons](https://github.com/sjparsons)] - [issue #103](https://github.com/j4mie/idiorm/issues/103) +* Make tests/bootstrap.php HHVM compatible [[JoelMarcey](https://github.com/JoelMarcey)] - [issue #143](https://github.com/j4mie/idiorm/issues/143) +* Fix docblock [[ulrikjohansson](https://github.com/ulrikjohansson)] - [issue #147](https://github.com/j4mie/idiorm/issues/147) +* Fix incorrect variable name in querying documentation [[fridde](https://github.com/fridde)] - [issue #146](https://github.com/j4mie/idiorm/issues/146) + +#### 1.3.0 - released 2013-01-31 + +* Documentation moved to [idiorm.rtfd.org](http://idiorm.rtfd.org) and now built using [Sphinx](http://sphinx-doc.org/) +* Add support for multiple database connections - closes [issue #15](https://github.com/j4mie/idiorm/issues/15) [[tag](https://github.com/tag)] +* Add in raw_execute - closes [issue #40](https://github.com/j4mie/idiorm/issues/40) [[tag](https://github.com/tag)] +* Add `get_last_statement()` - closes [issue #84](https://github.com/j4mie/idiorm/issues/84) [[tag](https://github.com/tag)] +* Add HAVING clause functionality - closes [issue #50](https://github.com/j4mie/idiorm/issues/50) +* Add `is_new` method - closes [issue #85](https://github.com/j4mie/idiorm/issues/85) +* Add `ArrayAccess` support to the model instances allowing property access via `$model['field']` as well as `$model->field` - [issue #51](https://github.com/j4mie/idiorm/issues/51) +* Add a result set object for collections of models that can support method chains to filter or apply actions to multiple results at once - issue [#51](https://github.com/j4mie/idiorm/issues/51) and [#22](https://github.com/j4mie/idiorm/issues/22) +* Add support for [Firebird](http://www.firebirdsql.org) with `ROWS` and `TO` result set limiting and identifier quoting [[mapner](https://github.com/mapner)] - [issue #98](https://github.com/j4mie/idiorm/issues/98) +* Fix last insert ID for PostgreSQL using RETURNING - closes issues [#62](https://github.com/j4mie/idiorm/issues/62) and [#89](https://github.com/j4mie/idiorm/issues/89) [[laacz](https://github.com/laacz)] +* Reset Idiorm after performing a query to allow for calling `count()` and then `find_many()` [[fayland](https://github.com/fayland)] - [issue #97](https://github.com/j4mie/idiorm/issues/97) +* Change Composer to use a classmap so that autoloading is better supported [[javierd](https://github.com/javiervd)] - [issue #96](https://github.com/j4mie/idiorm/issues/96) +* Add query logging to `delete_many` [[tag](https://github.com/tag)] +* Fix when using `set_expr` alone it doesn't trigger query creation - closes [issue #90](https://github.com/j4mie/idiorm/issues/90) +* Escape quote symbols in "_quote_identifier_part" - close [issue #74](https://github.com/j4mie/idiorm/issues/74) +* Fix issue with aggregate functions always returning `int` when is `float` sometimes required - closes [issue #92](https://github.com/j4mie/idiorm/issues/92) +* Move testing into PHPUnit to unify method testing and query generation testing + +#### 1.2.3 - released 2012-11-28 + +* Fix [issue #78](https://github.com/j4mie/idiorm/issues/78) - remove use of PHP 5.3 static call + +#### 1.2.2 - released 2012-11-15 + +* Fix bug where input parameters were sent as part-indexed, part associative + +#### 1.2.1 - released 2012-11-15 + +* Fix minor bug caused by IdiormStringException not extending Exception + +#### 1.2.0 - released 2012-11-14 + +* Setup composer for installation via packagist (j4mie/idiorm) +* Add `order_by_expr` method [[sandermarechal](http://github.com/sandermarechal)] +* Add support for raw queries without parameters argument [[sandermarechal](http://github.com/sandermarechal)] +* Add support to set multiple properties at once by passing an associative array to `set` method [[sandermarechal](http://github.com/sandermarechal)] +* Allow an associative array to be passed to `configure` method [[jordanlev](http://github.com/jordanlev)] +* Patch to allow empty Paris models to be saved ([[j4mie/paris](http://github.com/j4mie/paris)]) - [issue #58](https://github.com/j4mie/idiorm/issues/58) +* Add `select_many` and `select_many_expr` - closing issues [#49](https://github.com/j4mie/idiorm/issues/49) and [#69](https://github.com/j4mie/idiorm/issues/69) +* Add support for `MIN`, `AVG`, `MAX` and `SUM` - closes [issue #16](https://github.com/j4mie/idiorm/issues/16) +* Add `group_by_expr` - closes [issue #24](https://github.com/j4mie/idiorm/issues/24) +* Add `set_expr` to allow database expressions to be set as ORM properties - closes issues [#59](https://github.com/j4mie/idiorm/issues/59) and [#43](https://github.com/j4mie/idiorm/issues/43) [[brianherbert](https://github.com/brianherbert)] +* Prevent ambiguous column names when joining tables - [issue #66](https://github.com/j4mie/idiorm/issues/66) [[hellogerard](https://github.com/hellogerard)] +* Add `delete_many` method [[CBeerta](https://github.com/CBeerta)] +* Allow unsetting of ORM parameters [[CBeerta](https://github.com/CBeerta)] +* Add `find_array` to get the records as associative arrays [[Surt](https://github.com/Surt)] - closes [issue #17](https://github.com/j4mie/idiorm/issues/17) +* Fix bug in `_log_query` with `?` and `%` supplied in raw where statements etc. - closes [issue #57](https://github.com/j4mie/idiorm/issues/57) [[ridgerunner](https://github.com/ridgerunner)] + +#### 1.1.1 - released 2011-01-30 + +* Fix bug in quoting column wildcard. j4mie/paris#12 +* Small documentation improvements + +#### 1.1.0 - released 2011-01-24 + +* Add `is_dirty` method +* Add basic query caching +* Add `distinct` method +* Add `group_by` method + +#### 1.0.0 - released 2010-12-01 + +* Initial release diff --git a/vendor/j4mie/idiorm/composer.json b/vendor/j4mie/idiorm/composer.json new file mode 100644 index 000000000..8e19087ba --- /dev/null +++ b/vendor/j4mie/idiorm/composer.json @@ -0,0 +1,49 @@ +{ + "name": "j4mie/idiorm", + "type": "library", + "description": "A lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5", + "keywords": ["idiorm", "orm", "query builder"], + "homepage": "http://j4mie.github.com/idiormandparis", + "support": { + "issues": "https://github.com/j4mie/idiorm/issues", + "source": "https://github.com/j4mie/idiorm" + }, + "authors": [ + { + "name": "Jamie Matthews", + "email": "jamie.matthews@gmail.com", + "homepage": "http://j4mie.org", + "role": "Developer" + }, + { + "name": "Simon Holywell", + "email": "treffynnon@php.net", + "homepage": "http://simonholywell.com", + "role": "Maintainer" + }, + { + "name": "Durham Hale", + "email": "me@durhamhale.com", + "homepage": "http://durhamhale.com", + "role": "Maintainer" + } + ], + "scripts": { + "test": "phpunit -c ./phpunit.xml" + }, + "require-dev": { + "phpunit/phpunit": "^4.8", + "ext-pdo_sqlite": "*" + }, + "license": [ + "BSD-2-Clause", + "BSD-3-Clause", + "BSD-4-Clause" + ], + "require": { + "php": ">=5.2.0" + }, + "autoload": { + "classmap": ["idiorm.php"] + } +} diff --git a/vendor/j4mie/idiorm/demo.php b/vendor/j4mie/idiorm/demo.php new file mode 100644 index 000000000..771f48a2e --- /dev/null +++ b/vendor/j4mie/idiorm/demo.php @@ -0,0 +1,81 @@ +exec(" + CREATE TABLE IF NOT EXISTS contact ( + id INTEGER PRIMARY KEY, + name TEXT, + email TEXT + );" + ); + + // Handle POST submission + if (!empty($_POST)) { + + // Create a new contact object + $contact = ORM::for_table('contact')->create(); + + // SHOULD BE MORE ERROR CHECKING HERE! + + // Set the properties of the object + $contact->name = $_POST['name']; + $contact->email = $_POST['email']; + + // Save the object to the database + $contact->save(); + + // Redirect to self. + header('Location: ' . basename(__FILE__)); + exit; + } + + // Get a list of all contacts from the database + $count = ORM::for_table('contact')->count(); + $contact_list = ORM::for_table('contact')->find_many(); +?> + + + + Idiorm Demo + + + + +

Idiorm Demo

+ +

Contact List ( contacts)

+ + +
+

Add Contact

+

+

+ +
+ + diff --git a/vendor/j4mie/idiorm/docs/Makefile b/vendor/j4mie/idiorm/docs/Makefile new file mode 100644 index 000000000..4dbea877c --- /dev/null +++ b/vendor/j4mie/idiorm/docs/Makefile @@ -0,0 +1,153 @@ +# Makefile for Sphinx documentation +# + +# You can set these variables from the command line. +SPHINXOPTS = +SPHINXBUILD = sphinx-build +PAPER = +BUILDDIR = _build + +# Internal variables. +PAPEROPT_a4 = -D latex_paper_size=a4 +PAPEROPT_letter = -D latex_paper_size=letter +ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . +# the i18n builder cannot share the environment and doctrees with the others +I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . + +.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext + +help: + @echo "Please use \`make ' where is one of" + @echo " html to make standalone HTML files" + @echo " dirhtml to make HTML files named index.html in directories" + @echo " singlehtml to make a single large HTML file" + @echo " pickle to make pickle files" + @echo " json to make JSON files" + @echo " htmlhelp to make HTML files and a HTML help project" + @echo " qthelp to make HTML files and a qthelp project" + @echo " devhelp to make HTML files and a Devhelp project" + @echo " epub to make an epub" + @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" + @echo " latexpdf to make LaTeX files and run them through pdflatex" + @echo " text to make text files" + @echo " man to make manual pages" + @echo " texinfo to make Texinfo files" + @echo " info to make Texinfo files and run them through makeinfo" + @echo " gettext to make PO message catalogs" + @echo " changes to make an overview of all changed/added/deprecated items" + @echo " linkcheck to check all external links for integrity" + @echo " doctest to run all doctests embedded in the documentation (if enabled)" + +clean: + -rm -rf $(BUILDDIR)/* + +html: + $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." + +dirhtml: + $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." + +singlehtml: + $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml + @echo + @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." + +pickle: + $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle + @echo + @echo "Build finished; now you can process the pickle files." + +json: + $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json + @echo + @echo "Build finished; now you can process the JSON files." + +htmlhelp: + $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp + @echo + @echo "Build finished; now you can run HTML Help Workshop with the" \ + ".hhp project file in $(BUILDDIR)/htmlhelp." + +qthelp: + $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp + @echo + @echo "Build finished; now you can run "qcollectiongenerator" with the" \ + ".qhcp project file in $(BUILDDIR)/qthelp, like this:" + @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/Idiorm.qhcp" + @echo "To view the help file:" + @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/Idiorm.qhc" + +devhelp: + $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp + @echo + @echo "Build finished." + @echo "To view the help file:" + @echo "# mkdir -p $$HOME/.local/share/devhelp/Idiorm" + @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/Idiorm" + @echo "# devhelp" + +epub: + $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub + @echo + @echo "Build finished. The epub file is in $(BUILDDIR)/epub." + +latex: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo + @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." + @echo "Run \`make' in that directory to run these through (pdf)latex" \ + "(use \`make latexpdf' here to do that automatically)." + +latexpdf: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through pdflatex..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +text: + $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text + @echo + @echo "Build finished. The text files are in $(BUILDDIR)/text." + +man: + $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man + @echo + @echo "Build finished. The manual pages are in $(BUILDDIR)/man." + +texinfo: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo + @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." + @echo "Run \`make' in that directory to run these through makeinfo" \ + "(use \`make info' here to do that automatically)." + +info: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo "Running Texinfo files through makeinfo..." + make -C $(BUILDDIR)/texinfo info + @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." + +gettext: + $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale + @echo + @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." + +changes: + $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes + @echo + @echo "The overview file is in $(BUILDDIR)/changes." + +linkcheck: + $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck + @echo + @echo "Link check complete; look for any errors in the above output " \ + "or in $(BUILDDIR)/linkcheck/output.txt." + +doctest: + $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest + @echo "Testing of doctests in the sources finished, look at the " \ + "results in $(BUILDDIR)/doctest/output.txt." diff --git a/vendor/j4mie/idiorm/docs/conf.py b/vendor/j4mie/idiorm/docs/conf.py new file mode 100644 index 000000000..87e17925f --- /dev/null +++ b/vendor/j4mie/idiorm/docs/conf.py @@ -0,0 +1,242 @@ +# -*- coding: utf-8 -*- +# +# Idiorm documentation build configuration file, created by +# sphinx-quickstart on Wed Nov 28 15:39:16 2012. +# +# This file is execfile()d with the current directory set to its containing dir. +# +# Note that not all possible configuration values are present in this +# autogenerated file. +# +# All configuration values have a default; values that are commented out +# serve to show the default. + +import sys, os + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +#sys.path.insert(0, os.path.abspath('.')) + +# -- General configuration ----------------------------------------------------- + +# If your documentation needs a minimal Sphinx version, state it here. +#needs_sphinx = '1.0' + +# Add any Sphinx extension module names here, as strings. They can be extensions +# coming with Sphinx (named 'sphinx.ext.*') or your custom ones. +extensions = [] + +# Add any paths that contain templates here, relative to this directory. +templates_path = ['_templates'] + +# The suffix of source filenames. +source_suffix = '.rst' + +# The encoding of source files. +#source_encoding = 'utf-8-sig' + +# The master toctree document. +master_doc = 'index' + +# General information about the project. +project = u'Idiorm' +copyright = u'2014, Jamie Matthews and Simon Holywell' + +# The version info for the project you're documenting, acts as replacement for +# |version| and |release|, also used in various other places throughout the +# built documents. +# +# The short X.Y version. +version = '' +# The full version, including alpha/beta/rc tags. +release = '' + +# The language for content autogenerated by Sphinx. Refer to documentation +# for a list of supported languages. +#language = None + +# There are two options for replacing |today|: either, you set today to some +# non-false value, then it is used: +#today = '' +# Else, today_fmt is used as the format for a strftime call. +#today_fmt = '%B %d, %Y' + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +exclude_patterns = ['_build'] + +# The reST default role (used for this markup: `text`) to use for all documents. +#default_role = None + +# If true, '()' will be appended to :func: etc. cross-reference text. +#add_function_parentheses = True + +# If true, the current module name will be prepended to all description +# unit titles (such as .. function::). +#add_module_names = True + +# If true, sectionauthor and moduleauthor directives will be shown in the +# output. They are ignored by default. +#show_authors = False + +# The name of the Pygments (syntax highlighting) style to use. +pygments_style = 'sphinx' + +# A list of ignored prefixes for module index sorting. +#modindex_common_prefix = [] + + +# -- Options for HTML output --------------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +html_theme = 'default' + +# Theme options are theme-specific and customize the look and feel of a theme +# further. For a list of options available for each theme, see the +# documentation. +#html_theme_options = {} + +# Add any paths that contain custom themes here, relative to this directory. +#html_theme_path = [] + +# The name for this set of Sphinx documents. If None, it defaults to +# " v documentation". +#html_title = None + +# A shorter title for the navigation bar. Default is the same as html_title. +#html_short_title = None + +# The name of an image file (relative to this directory) to place at the top +# of the sidebar. +#html_logo = None + +# The name of an image file (within the static path) to use as favicon of the +# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 +# pixels large. +#html_favicon = None + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ['_static'] + +# If not '', a 'Last updated on:' timestamp is inserted at every page bottom, +# using the given strftime format. +#html_last_updated_fmt = '%b %d, %Y' + +# If true, SmartyPants will be used to convert quotes and dashes to +# typographically correct entities. +#html_use_smartypants = True + +# Custom sidebar templates, maps document names to template names. +#html_sidebars = {} + +# Additional templates that should be rendered to pages, maps page names to +# template names. +#html_additional_pages = {} + +# If false, no module index is generated. +#html_domain_indices = True + +# If false, no index is generated. +#html_use_index = True + +# If true, the index is split into individual pages for each letter. +#html_split_index = False + +# If true, links to the reST sources are added to the pages. +#html_show_sourcelink = True + +# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. +#html_show_sphinx = True + +# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. +#html_show_copyright = True + +# If true, an OpenSearch description file will be output, and all pages will +# contain a tag referring to it. The value of this option must be the +# base URL from which the finished HTML is served. +#html_use_opensearch = '' + +# This is the file name suffix for HTML files (e.g. ".xhtml"). +#html_file_suffix = None + +# Output file base name for HTML help builder. +htmlhelp_basename = 'Idiormdoc' + + +# -- Options for LaTeX output -------------------------------------------------- + +latex_elements = { +# The paper size ('letterpaper' or 'a4paper'). +#'papersize': 'letterpaper', + +# The font size ('10pt', '11pt' or '12pt'). +#'pointsize': '10pt', + +# Additional stuff for the LaTeX preamble. +#'preamble': '', +} + +# Grouping the document tree into LaTeX files. List of tuples +# (source start file, target name, title, author, documentclass [howto/manual]). +latex_documents = [ + ('index', 'Idiorm.tex', u'Idiorm Documentation', + u'Jamie Matthews and Simon Holywell', 'manual'), +] + +# The name of an image file (relative to this directory) to place at the top of +# the title page. +#latex_logo = None + +# For "manual" documents, if this is true, then toplevel headings are parts, +# not chapters. +#latex_use_parts = False + +# If true, show page references after internal links. +#latex_show_pagerefs = False + +# If true, show URL addresses after external links. +#latex_show_urls = False + +# Documents to append as an appendix to all manuals. +#latex_appendices = [] + +# If false, no module index is generated. +#latex_domain_indices = True + + +# -- Options for manual page output -------------------------------------------- + +# One entry per manual page. List of tuples +# (source start file, name, description, authors, manual section). +man_pages = [ + ('index', 'idiorm', u'Idiorm Documentation', + [u'Jamie Matthews and Simon Holywell'], 1) +] + +# If true, show URL addresses after external links. +#man_show_urls = False + + +# -- Options for Texinfo output ------------------------------------------------ + +# Grouping the document tree into Texinfo files. List of tuples +# (source start file, target name, title, author, +# dir menu entry, description, category) +texinfo_documents = [ + ('index', 'Idiorm', u'Idiorm Documentation', + u'Jamie Matthews and Simon Holywell', 'Idiorm', 'One line description of project.', + 'Miscellaneous'), +] + +# Documents to append as an appendix to all manuals. +#texinfo_appendices = [] + +# If false, no module index is generated. +#texinfo_domain_indices = True + +# How to display URL addresses: 'footnote', 'no', or 'inline'. +#texinfo_show_urls = 'footnote' diff --git a/vendor/j4mie/idiorm/docs/configuration.rst b/vendor/j4mie/idiorm/docs/configuration.rst new file mode 100644 index 000000000..fb90dfa0f --- /dev/null +++ b/vendor/j4mie/idiorm/docs/configuration.rst @@ -0,0 +1,378 @@ +Configuration +============= + +The first thing you need to know about Idiorm is that *you don’t need to +define any model classes to use it*. With almost every other ORM, the +first thing to do is set up your models and map them to database tables +(through configuration variables, XML files or similar). With Idiorm, +you can start using the ORM straight away. + +Setup +~~~~~ + +First, ``require`` the Idiorm source file: + +.. code-block:: php + + 'value_for_setting_1', + 'setting_name_2' => 'value_for_setting_2', + 'etc' => 'etc' + )); + +Use the ``get_config`` method to read current settings. + +.. code-block:: php + + 'mysql:host=localhost;dbname=my_database', + 'username' => 'database_user', + 'password' => 'top_secret' + )); + +Result sets +^^^^^^^^^^^ + +Setting: ``return_result_sets`` + +Collections of results can be returned as an array (default) or as a result set. +See the `find_result_set()` documentation for more information. + +.. code-block:: php + + 'SET NAMES utf8')); + +PDO Error Mode +^^^^^^^^^^^^^^ + +Setting: ``error_mode`` + +This can be used to set the ``PDO::ATTR_ERRMODE`` setting on the +database connection class used by Idiorm. It should be passed one of the +class constants defined by PDO. For example: + +.. code-block:: php + + 'person_id', + 'role' => 'role_id', + )); + +As with ``id_column`` setting, you can specify a compound primary key +using an array. + +Limit clause style +^^^^^^^^^^^^^^^^^^ + +Setting: ``limit_clause_style`` + +You can specify the limit clause style in the configuration. This is to facilitate +a MS SQL style limit clause that uses the ``TOP`` syntax. + +Acceptable values are ``ORM::LIMIT_STYLE_TOP_N`` and ``ORM::LIMIT_STYLE_LIMIT``. + +.. note:: + + If the PDO driver you are using is one of sqlsrv, dblib or mssql then Idiorm + will automatically select the ``ORM::LIMIT_STYLE_TOP_N`` for you unless you + override the setting. + +Query logging +^^^^^^^^^^^^^ + +Setting: ``logging`` + +Idiorm can log all queries it executes. To enable query logging, set the +``logging`` option to ``true`` (it is ``false`` by default). + +When query logging is enabled, you can use two static methods to access +the log. ``ORM::get_last_query()`` returns the most recent query +executed. ``ORM::get_query_log()`` returns an array of all queries +executed. + +.. note:: + + The code that does the query log is an approximation of that provided by PDO/the + database (see the Idiorm source code for detail). The actual query isn't even available + to idiorm to log as the database/PDO handles the binding outside of idiorm's reach and + doesn't pass it back. + + This means that you might come across some inconsistencies between what is logged and + what is actually run. In these case you'll need to look at the query log provided by + your database vendor (eg. MySQL). + +Query logger +^^^^^^^^^^^^ + +Setting: ``logger`` + +.. note:: + + You must enable ``logging`` for this setting to have any effect. + +It is possible to supply a ``callable`` to this configuration setting, which will +be executed for every query that idiorm executes. In PHP a ``callable`` is anything +that can be executed as if it were a function. Most commonly this will take the +form of a anonymous function. + +This setting is useful if you wish to log queries with an external library as it +allows you too whatever you would like from inside the callback function. + +.. code-block:: php + + find_one(5); + + // Using default connection, explicitly + $person = ORM::for_table('person', ORM::DEFAULT_CONNECTION)->find_one(5); + + // Using named connection + $person = ORM::for_table('different_person', 'remote')->find_one(5); + + + +Supported Methods +^^^^^^^^^^^^^^^^^ +In each of these cases, the ``$connection_name`` parameter is optional, and is +an arbitrary key identifying the named connection. + +* ``ORM::configure($key, $value, $connection_name)`` +* ``ORM::for_table($table_name, $connection_name)`` +* ``ORM::set_db($pdo, $connection_name)`` +* ``ORM::get_db($connection_name)`` +* ``ORM::raw_execute($query, $parameters, $connection_name)`` +* ``ORM::get_last_query($connection_name)`` +* ``ORM::get_query_log($connection_name)`` + +Of these methods, only ``ORM::get_last_query($connection_name)`` does *not* +fallback to the default connection when no connection name is passed. +Instead, passing no connection name (or ``null``) returns the most recent +query on *any* connection. + +.. code-block:: php + + find_one(5); + + // Using named connection + $person = ORM::for_table('different_person', 'remote')->find_one(5); + + // Last query on *any* connection + ORM::get_last_query(); // returns query on 'different_person' using 'remote' + + // returns query on 'person' using default by passing in the connection name + ORM::get_last_query(ORM::DEFAULT_CONNECTION); + +Notes +~~~~~ +* **There is no support for joins across connections** +* Multiple connections do not share configuration settings. This means if + one connection has logging set to ``true`` and the other does not, only + queries from the logged connection will be available via + ``ORM::get_last_query()`` and ``ORM::get_query_log()``. +* A new method has been added, ``ORM::get_connection_names()``, which returns + an array of connection names. +* Caching *should* work with multiple connections (remember to turn caching + on for each connection), but the unit tests are not robust. Please report + any errors. + diff --git a/vendor/j4mie/idiorm/docs/index.rst b/vendor/j4mie/idiorm/docs/index.rst new file mode 100644 index 000000000..d4e4bb97b --- /dev/null +++ b/vendor/j4mie/idiorm/docs/index.rst @@ -0,0 +1,29 @@ +.. Idiorm documentation master file, created by + sphinx-quickstart on Wed Nov 28 15:39:16 2012. + You can adapt this file completely to your liking, but it should at least + contain the root `toctree` directive. + +Welcome to Idiorm's documentation! +================================== + +Contents: + +.. toctree:: + :maxdepth: 2 + + philosophy + installation + configuration + querying + models + transactions + connections + + +Indices and tables +================== + +* :ref:`genindex` +* :ref:`modindex` +* :ref:`search` + diff --git a/vendor/j4mie/idiorm/docs/installation.rst b/vendor/j4mie/idiorm/docs/installation.rst new file mode 100644 index 000000000..255ea62a8 --- /dev/null +++ b/vendor/j4mie/idiorm/docs/installation.rst @@ -0,0 +1,19 @@ +Installation +============ + +Packagist +~~~~~~~~~ + +This library is available through Packagist with the vendor and package +identifier of ``j4mie/idiorm`` + +Please see the `Packagist documentation`_ for further information. + +Download +~~~~~~~~ + +You can clone the git repository, download idiorm.php or a release tag +and then drop the idiorm.php file in the vendors/3rd party/libs +directory of your project. + +.. _Packagist documentation: http://packagist.org/ \ No newline at end of file diff --git a/vendor/j4mie/idiorm/docs/make.bat b/vendor/j4mie/idiorm/docs/make.bat new file mode 100644 index 000000000..c09073569 --- /dev/null +++ b/vendor/j4mie/idiorm/docs/make.bat @@ -0,0 +1,190 @@ +@ECHO OFF + +REM Command file for Sphinx documentation + +if "%SPHINXBUILD%" == "" ( + set SPHINXBUILD=sphinx-build +) +set BUILDDIR=_build +set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . +set I18NSPHINXOPTS=%SPHINXOPTS% . +if NOT "%PAPER%" == "" ( + set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% + set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% +) + +if "%1" == "" goto help + +if "%1" == "help" ( + :help + echo.Please use `make ^` where ^ is one of + echo. html to make standalone HTML files + echo. dirhtml to make HTML files named index.html in directories + echo. singlehtml to make a single large HTML file + echo. pickle to make pickle files + echo. json to make JSON files + echo. htmlhelp to make HTML files and a HTML help project + echo. qthelp to make HTML files and a qthelp project + echo. devhelp to make HTML files and a Devhelp project + echo. epub to make an epub + echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter + echo. text to make text files + echo. man to make manual pages + echo. texinfo to make Texinfo files + echo. gettext to make PO message catalogs + echo. changes to make an overview over all changed/added/deprecated items + echo. linkcheck to check all external links for integrity + echo. doctest to run all doctests embedded in the documentation if enabled + goto end +) + +if "%1" == "clean" ( + for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i + del /q /s %BUILDDIR%\* + goto end +) + +if "%1" == "html" ( + %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The HTML pages are in %BUILDDIR%/html. + goto end +) + +if "%1" == "dirhtml" ( + %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. + goto end +) + +if "%1" == "singlehtml" ( + %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. + goto end +) + +if "%1" == "pickle" ( + %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can process the pickle files. + goto end +) + +if "%1" == "json" ( + %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can process the JSON files. + goto end +) + +if "%1" == "htmlhelp" ( + %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can run HTML Help Workshop with the ^ +.hhp project file in %BUILDDIR%/htmlhelp. + goto end +) + +if "%1" == "qthelp" ( + %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can run "qcollectiongenerator" with the ^ +.qhcp project file in %BUILDDIR%/qthelp, like this: + echo.^> qcollectiongenerator %BUILDDIR%\qthelp\Idiorm.qhcp + echo.To view the help file: + echo.^> assistant -collectionFile %BUILDDIR%\qthelp\Idiorm.ghc + goto end +) + +if "%1" == "devhelp" ( + %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. + goto end +) + +if "%1" == "epub" ( + %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The epub file is in %BUILDDIR%/epub. + goto end +) + +if "%1" == "latex" ( + %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. + goto end +) + +if "%1" == "text" ( + %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The text files are in %BUILDDIR%/text. + goto end +) + +if "%1" == "man" ( + %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The manual pages are in %BUILDDIR%/man. + goto end +) + +if "%1" == "texinfo" ( + %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. + goto end +) + +if "%1" == "gettext" ( + %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The message catalogs are in %BUILDDIR%/locale. + goto end +) + +if "%1" == "changes" ( + %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes + if errorlevel 1 exit /b 1 + echo. + echo.The overview file is in %BUILDDIR%/changes. + goto end +) + +if "%1" == "linkcheck" ( + %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck + if errorlevel 1 exit /b 1 + echo. + echo.Link check complete; look for any errors in the above output ^ +or in %BUILDDIR%/linkcheck/output.txt. + goto end +) + +if "%1" == "doctest" ( + %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest + if errorlevel 1 exit /b 1 + echo. + echo.Testing of doctests in the sources finished, look at the ^ +results in %BUILDDIR%/doctest/output.txt. + goto end +) + +:end diff --git a/vendor/j4mie/idiorm/docs/models.rst b/vendor/j4mie/idiorm/docs/models.rst new file mode 100644 index 000000000..d72e27d7d --- /dev/null +++ b/vendor/j4mie/idiorm/docs/models.rst @@ -0,0 +1,161 @@ +Models +====== + +Getting data from objects +~~~~~~~~~~~~~~~~~~~~~~~~~ + +Once you've got a set of records (objects) back from a query, you can +access properties on those objects (the values stored in the columns in +its corresponding table) in two ways: by using the ``get`` method, or +simply by accessing the property on the object directly: + +.. code-block:: php + + find_one(5); + + // The following two forms are equivalent + $name = $person->get('name'); + $name = $person->name; + +You can also get the all the data wrapped by an ORM instance using the +``as_array`` method. This will return an associative array mapping +column names (keys) to their values. + +The ``as_array`` method takes column names as optional arguments. If one +or more of these arguments is supplied, only matching column names will +be returned. + +.. code-block:: php + + create(); + + $person->first_name = 'Fred'; + $person->surname = 'Bloggs'; + $person->age = 50; + + // Returns array('first_name' => 'Fred', 'surname' => 'Bloggs', 'age' => 50) + $data = $person->as_array(); + + // Returns array('first_name' => 'Fred', 'age' => 50) + $data = $person->as_array('first_name', 'age'); + +Updating records +~~~~~~~~~~~~~~~~ + +To update the database, change one or more of the properties of the +object, then call the ``save`` method to commit the changes to the +database. Again, you can change the values of the object's properties +either by using the ``set`` method or by setting the value of the +property directly. By using the ``set`` method it is also possible to +update multiple properties at once, by passing in an associative array: + +.. code-block:: php + + find_one(5); + + // The following two forms are equivalent + $person->set('name', 'Bob Smith'); + $person->age = 20; + + // This is equivalent to the above two assignments + $person->set(array( + 'name' => 'Bob Smith', + 'age' => 20 + )); + + // Syncronise the object with the database + $person->save(); + +Properties containing expressions +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +It is possible to set properties on the model that contain database +expressions using the ``set_expr`` method. + +.. code-block:: php + + find_one(5); + $person->set('name', 'Bob Smith'); + $person->age = 20; + $person->set_expr('updated', 'NOW()'); + $person->save(); + +The ``updated`` column's value will be inserted into query in its raw +form therefore allowing the database to execute any functions referenced +- such as ``NOW()`` in this case. + +Creating new records +~~~~~~~~~~~~~~~~~~~~ + +To add a new record, you need to first create an "empty" object +instance. You then set values on the object as normal, and save it. + +.. code-block:: php + + create(); + + $person->name = 'Joe Bloggs'; + $person->age = 40; + + $person->save(); + +After the object has been saved, you can call its ``id()`` method to +find the autogenerated primary key value that the database assigned to +it. + +Properties containing expressions +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +It is possible to set properties on the model that contain database +expressions using the ``set_expr`` method. + +.. code-block:: php + + create(); + $person->set('name', 'Bob Smith'); + $person->age = 20; + $person->set_expr('added', 'NOW()'); + $person->save(); + +The ``added`` column's value will be inserted into query in its raw form +therefore allowing the database to execute any functions referenced - +such as ``NOW()`` in this case. + +Checking whether a property has been modified +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +To check whether a property has been changed since the object was +created (or last saved), call the ``is_dirty`` method: + +.. code-block:: php + + is_dirty('name'); // Returns true or false + +Deleting records +~~~~~~~~~~~~~~~~ + +To delete an object from the database, simply call its ``delete`` +method. + +.. code-block:: php + + find_one(5); + $person->delete(); + +To delete more than one object from the database, build a query: + +.. code-block:: php + + where_equal('zipcode', 55555) + ->delete_many(); + diff --git a/vendor/j4mie/idiorm/docs/philosophy.rst b/vendor/j4mie/idiorm/docs/philosophy.rst new file mode 100644 index 000000000..a11d4210f --- /dev/null +++ b/vendor/j4mie/idiorm/docs/philosophy.rst @@ -0,0 +1,34 @@ +Philosophy +========== + +The `Pareto Principle`_ states that *roughly 80% of the effects come +from 20% of the causes.* In software development terms, this could be +translated into something along the lines of *80% of the results come +from 20% of the complexity*. In other words, you can get pretty far by +being pretty stupid. + +**Idiorm is deliberately simple**. Where other ORMs consist of dozens of +classes with complex inheritance hierarchies, Idiorm has only one class, +``ORM``, which functions as both a fluent ``SELECT`` query API and a +simple CRUD model class. If my hunch is correct, this should be quite +enough for many real-world applications. Let’s face it: most of us +aren’t building Facebook. We’re working on small-to-medium-sized +projects, where the emphasis is on simplicity and rapid development +rather than infinite flexibility and features. + +You might think of **Idiorm** as a *micro-ORM*. It could, perhaps, be +“the tie to go along with `Slim`_\ ’s tux” (to borrow a turn of phrase +from `DocumentCloud`_). Or it could be an effective bit of spring +cleaning for one of those horrendous SQL-littered legacy PHP apps you +have to support. + +**Idiorm** might also provide a good base upon which to build +higher-level, more complex database abstractions. For example, `Paris`_ +is an implementation of the `Active Record pattern`_ built on top of +Idiorm. + +.. _Pareto Principle: http://en.wikipedia.org/wiki/Pareto_principle +.. _Slim: http://github.com/codeguy/slim/ +.. _DocumentCloud: http://github.com/documentcloud/underscore +.. _Paris: http://github.com/j4mie/paris +.. _Active Record pattern: http://martinfowler.com/eaaCatalog/activeRecord.html \ No newline at end of file diff --git a/vendor/j4mie/idiorm/docs/querying.rst b/vendor/j4mie/idiorm/docs/querying.rst new file mode 100644 index 000000000..1ea5a2549 --- /dev/null +++ b/vendor/j4mie/idiorm/docs/querying.rst @@ -0,0 +1,896 @@ +Querying +======== + +Idiorm provides a `*fluent +interface* `_ to enable +simple queries to be built without writing a single character of SQL. If +you've used `jQuery `_ at all, you'll be familiar +with the concept of a fluent interface. It just means that you can +*chain* method calls together, one after another. This can make your +code more readable, as the method calls strung together in order can +start to look a bit like a sentence. + +All Idiorm queries start with a call to the ``for_table`` static method +on the ORM class. This tells the ORM which table to use when making the +query. + +*Note that this method **does not** escape its query parameter and so +the table name should **not** be passed directly from user input.* + +Method calls which add filters and constraints to your query are then +strung together. Finally, the chain is finished by calling either +``find_one()`` or ``find_many()``, which executes the query and returns +the result. + +Let's start with a simple example. Say we have a table called ``person`` +which contains the columns ``id`` (the primary key of the record - +Idiorm assumes the primary key column is called ``id`` but this is +configurable, see below), ``name``, ``age`` and ``gender``. + +A note on PSR-1 and camelCase +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +All the methods detailed in the documentation can also be called in a PSR-1 way: +underscores (_) become camelCase. Here follows an example of one query chain +being converted to a PSR-1 compliant style. + +.. code-block:: php + + where('name', 'Fred Bloggs')->find_one(); + + // PSR-1 compliant style + $person = ORM::forTable('person')->where('name', 'Fred Bloggs')->findOne(); + +As you can see any method can be changed from the documented underscore (_) format +to that of a camelCase method name. + +.. note:: + + In the background the PSR-1 compliant style uses the `__call()` and + `__callStatic()` magic methods to map the camelCase method name you supply + to the original underscore method name. It then uses `call_user_func_array()` + to apply the arguments to the method. If this minimal overhead is too great + then you can simply revert to using the underscore methods to avoid it. In + general this will not be a bottle neck in any application however and should + be considered a micro-optimisation. + + As `__callStatic()` was added in PHP 5.3.0 you will need at least that version + of PHP to use this feature in any meaningful way. + +Single records +^^^^^^^^^^^^^^ + +Any method chain that ends in ``find_one()`` will return either a +*single* instance of the ORM class representing the database row you +requested, or ``false`` if no matching record was found. + +To find a single record where the ``name`` column has the value "Fred +Bloggs": + +.. code-block:: php + + where('name', 'Fred Bloggs')->find_one(); + +This roughly translates into the following SQL: +``SELECT * FROM person WHERE name = "Fred Bloggs"`` + +To find a single record by ID, you can pass the ID directly to the +``find_one`` method: + +.. code-block:: php + + find_one(5); + +If you are using a compound primary key, you can find the records +using an array as the parameter: + +.. code-block:: php + + find_one(array( + 'user_id' => 34, + 'role_id' => 10 + )); + + +Multiple records +^^^^^^^^^^^^^^^^ + +.. note:: + + It is recommended that you use results sets over arrays - see `As a result set` + below. + +Any method chain that ends in ``find_many()`` will return an *array* of +ORM class instances, one for each row matched by your query. If no rows +were found, an empty array will be returned. + +To find all records in the table: + +.. code-block:: php + + find_many(); + +To find all records where the ``gender`` is ``female``: + +.. code-block:: php + + where('gender', 'female')->find_many(); + +As a result set +''''''''''''''' + +.. note:: + + There is a configuration setting ``return_result_sets`` that will cause + ``find_many()`` to return result sets by default. It is recommended that you + turn this setting on: + + :: + + ORM::configure('return_result_sets', true); + +You can also find many records as a result set instead of an array of Idiorm +instances. This gives you the advantage that you can run batch operations on a +set of results. + +So for example instead of running this: + +.. code-block:: php + + find_many(); + foreach ($people as $person) { + $person->age = 50; + $person->save(); + } + +You can simply do this instead: + +.. code-block:: php + + find_result_set() + ->set('age', 50) + ->save(); + +To do this substitute any call to ``find_many()`` with +``find_result_set()``. + +A result set will also behave like an array so you can `count()` it and `foreach` +over it just like an array. + +.. code-block:: php + + find_result_set() as $record) { + echo $record->name; + } + +.. code-block:: php + + find_result_set()); + +.. note:: + + For deleting many records it is recommended that you use `delete_many()` as it + is more efficient than calling `delete()` on a result set. + +As an associative array +''''''''''''''''''''''' + +You can also find many records as an associative array instead of Idiorm +instances. To do this substitute any call to ``find_many()`` with +``find_array()``. + +.. code-block:: php + + where('gender', 'female')->find_array(); + +This is useful if you need to serialise the the query output into a +format like JSON and you do not need the ability to update the returned +records. + +Counting results +^^^^^^^^^^^^^^^^ + +To return a count of the number of rows that would be returned by a +query, call the ``count()`` method. + +.. code-block:: php + + count(); + +Filtering results +^^^^^^^^^^^^^^^^^ + +Idiorm provides a family of methods to extract only records which +satisfy some condition or conditions. These methods may be called +multiple times to build up your query, and Idiorm's fluent interface +allows method calls to be *chained* to create readable and +simple-to-understand queries. + +*Caveats* +''''''''' + +Only a subset of the available conditions supported by SQL are available +when using Idiorm. Additionally, all the ``WHERE`` clauses will be +``AND``\ ed together when the query is run. Support for ``OR``\ ing +``WHERE`` clauses is not currently present. + +These limits are deliberate: these are by far the most commonly used +criteria, and by avoiding support for very complex queries, the Idiorm +codebase can remain small and simple. + +Some support for more complex conditions and queries is provided by the +``where_raw`` and ``raw_query`` methods (see below). If you find +yourself regularly requiring more functionality than Idiorm can provide, +it may be time to consider using a more full-featured ORM. + +Equality: ``where``, ``where_equal``, ``where_not_equal`` +''''''''''''''''''''''''''''''''''''''''''''''''''''''''' + +By default, calling ``where`` with two parameters (the column name and +the value) will combine them using an equals operator (``=``). For +example, calling ``where('name', 'Fred')`` will result in the clause +``WHERE name = "Fred"``. + +If your coding style favours clarity over brevity, you may prefer to use +the ``where_equal`` method: this is identical to ``where``. + +The ``where_not_equal`` method adds a ``WHERE column != "value"`` clause +to your query. + +You can specify multiple columns and their values in the same call. In this +case you should pass an associative array as the first parameter. The array +notation uses keys as column names. + +.. code-block:: php + + where(array( + 'name' => 'Fred', + 'age' => 20 + )) + ->find_many(); + + // Creates SQL: + SELECT * FROM `person` WHERE `name` = "Fred" AND `age` = "20"; + +Shortcut: ``where_id_is`` +''''''''''''''''''''''''' + +This is a simple helper method to query the table by primary key. +Respects the ID column specified in the config. If you are using a compound +primary key, you must pass an array where the key is the column name. Columns +that don't belong to the key will be ignored. + +Shortcut: ``where_id_in`` +''''''''''''''''''''''''' + +This helper method is similar to ``where_id_is`, but it expects an array of +primary keys to be selected. It is compound primary keys aware. + +Less than / greater than: ``where_lt``, ``where_gt``, ``where_lte``, ``where_gte`` +'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' + +There are four methods available for inequalities: + +- Less than: + ``$people = ORM::for_table('person')->where_lt('age', 10)->find_many();`` +- Greater than: + ``$people = ORM::for_table('person')->where_gt('age', 5)->find_many();`` +- Less than or equal: + ``$people = ORM::for_table('person')->where_lte('age', 10)->find_many();`` +- Greater than or equal: + ``$people = ORM::for_table('person')->where_gte('age', 5)->find_many();`` + +String comparision: ``where_like`` and ``where_not_like`` +''''''''''''''''''''''''''''''''''''''''''''''''''''''''' + +To add a ``WHERE ... LIKE`` clause, use: + +.. code-block:: php + + where_like('name', '%fred%')->find_many(); + +Similarly, to add a ``WHERE ... NOT LIKE`` clause, use: + +.. code-block:: php + + where_not_like('name', '%bob%')->find_many(); + +Multiple OR'ed conditions +''''''''''''''''''''''''' + +You can add simple OR'ed conditions to the same WHERE clause using ``where_any_is``. You +should specify multiple conditions using an array of items. Each item will be an +associative array that contains a multiple conditions. + +.. code-block:: php + + where_any_is(array( + array('name' => 'Joe', 'age' => 10), + array('name' => 'Fred', 'age' => 20))) + ->find_many(); + + // Creates SQL: + SELECT * FROM `widget` WHERE (( `name` = 'Joe' AND `age` = '10' ) OR ( `name` = 'Fred' AND `age` = '20' )); + +By default, it uses the equal operator for every column, but it can be overriden for any +column using a second parameter: + +.. code-block:: php + + where_any_is(array( + array('name' => 'Joe', 'age' => 10), + array('name' => 'Fred', 'age' => 20)), array('age' => '>')) + ->find_many(); + + // Creates SQL: + SELECT * FROM `widget` WHERE (( `name` = 'Joe' AND `age` = '10' ) OR ( `name` = 'Fred' AND `age` > '20' )); + +If you want to set the default operator for all the columns, just pass it as the second parameter: + +.. code-block:: php + + where_any_is(array( + array('score' => '5', 'age' => 10), + array('score' => '15', 'age' => 20)), '>') + ->find_many(); + + // Creates SQL: + SELECT * FROM `widget` WHERE (( `score` > '5' AND `age` > '10' ) OR ( `score` > '15' AND `age` > '20' )); + +Set membership: ``where_in`` and ``where_not_in`` +''''''''''''''''''''''''''''''''''''''''''''''''' + +To add a ``WHERE ... IN ()`` or ``WHERE ... NOT IN ()`` clause, use the +``where_in`` and ``where_not_in`` methods respectively. + +Both methods accept two arguments. The first is the column name to +compare against. The second is an *array* of possible values. As all the +``where_`` methods, you can specify multiple columns using an associative +*array* as the only parameter. + +.. code-block:: php + + where_in('name', array('Fred', 'Joe', 'John'))->find_many(); + +Working with ``NULL`` values: ``where_null`` and ``where_not_null`` +''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' + +To add a ``WHERE column IS NULL`` or ``WHERE column IS NOT NULL`` +clause, use the ``where_null`` and ``where_not_null`` methods +respectively. Both methods accept a single parameter: the column name to +test. + +Raw WHERE clauses +''''''''''''''''' + +If you require a more complex query, you can use the ``where_raw`` +method to specify the SQL fragment for the WHERE clause exactly. This +method takes two arguments: the string to add to the query, and an +(optional) array of parameters which will be bound to the string. If +parameters are supplied, the string should contain question mark +characters (``?``) to represent the values to be bound, and the +parameter array should contain the values to be substituted into the +string in the correct order. + +This method may be used in a method chain alongside other ``where_*`` +methods as well as methods such as ``offset``, ``limit`` and +``order_by_*``. The contents of the string you supply will be connected +with preceding and following WHERE clauses with AND. + +.. code-block:: php + + where('name', 'Fred') + ->where_raw('(`age` = ? OR `age` = ?)', array(20, 25)) + ->order_by_asc('name') + ->find_many(); + + // Creates SQL: + SELECT * FROM `person` WHERE `name` = "Fred" AND (`age` = 20 OR `age` = 25) ORDER BY `name` ASC; + +.. note:: + + You must wrap your expression in parentheses when using any of ``ALL``, + ``ANY``, ``BETWEEN``, ``IN``, ``LIKE``, ``OR`` and ``SOME``. Otherwise + the precedence of ``AND`` will bind stronger and in the above example + you would effectively get ``WHERE (`name` = "Fred" AND `age` = 20) OR `age` = 25`` + +Note that this method only supports "question mark placeholder" syntax, +and NOT "named placeholder" syntax. This is because PDO does not allow +queries that contain a mixture of placeholder types. Also, you should +ensure that the number of question mark placeholders in the string +exactly matches the number of elements in the array. + +If you require yet more flexibility, you can manually specify the entire +query. See *Raw queries* below. + +Limits and offsets +'''''''''''''''''' + +*Note that these methods **do not** escape their query parameters and so +these should **not** be passed directly from user input.* + +The ``limit`` and ``offset`` methods map pretty closely to their SQL +equivalents. + +.. code-block:: php + + where('gender', 'female')->limit(5)->offset(10)->find_many(); + +Ordering +'''''''' + +*Note that these methods **do not** escape their query parameters and so +these should **not** be passed directly from user input.* + +Two methods are provided to add ``ORDER BY`` clauses to your query. +These are ``order_by_desc`` and ``order_by_asc``, each of which takes a +column name to sort by. The column names will be quoted. + +.. code-block:: php + + order_by_asc('gender')->order_by_desc('name')->find_many(); + +If you want to order by something other than a column name, then use the +``order_by_expr`` method to add an unquoted SQL expression as an +``ORDER BY`` clause. + +.. code-block:: php + + order_by_expr('SOUNDEX(`name`)')->find_many(); + +Grouping +^^^^^^^^ + +*Note that this method **does not** escape it query parameter and so +this should **not** by passed directly from user input.* + +To add a ``GROUP BY`` clause to your query, call the ``group_by`` +method, passing in the column name. You can call this method multiple +times to add further columns. + +.. code-block:: php + + where('gender', 'female')->group_by('name')->find_many(); + +It is also possible to ``GROUP BY`` a database expression: + +.. code-block:: php + + where('gender', 'female')->group_by_expr("FROM_UNIXTIME(`time`, '%Y-%m')")->find_many(); + +Having +^^^^^^ + +When using aggregate functions in combination with a ``GROUP BY`` you can use +``HAVING`` to filter based on those values. + +``HAVING`` works in exactly the same way as all of the ``where*`` functions in Idiorm. +Substitute ``where_`` for ``having_`` to make use of these functions. + +For example: + +.. code-block:: php + + group_by('name')->having_not_like('name', '%bob%')->find_many(); + +Result columns +^^^^^^^^^^^^^^ + +By default, all columns in the ``SELECT`` statement are returned from +your query. That is, calling: + +.. code-block:: php + + find_many(); + +Will result in the query: + +.. code-block:: php + + `_ to specify +many columns at once. + +.. code-block:: php + + select('name')->select('age')->find_many(); + +Will result in the query: + +.. code-block:: php + + select('name', 'person_name')->find_many(); + +Will result in the query: + +.. code-block:: php + + select('person.name', 'person_name')->find_many(); + +Will result in the query: + +.. code-block:: php + + `_ +to specify many expressions at once. + +.. code-block:: php + + select_expr('COUNT(*)', 'count')->find_many(); + +Will result in the query: + +.. code-block:: php + + select_many('name', 'age')->find_many(); + +Will result in the query: + +.. code-block:: php + + select_many(array('first_name' => 'name'), 'age', 'height')->find_many(); + +Will result in the query: + +.. code-block:: php + + 'column', 'column2', 'alias2' => 'column3'), 'column4', 'column5') + select_many('column', 'column2', 'column3') + select_many(array('column', 'column2', 'column3'), 'column4', 'column5') + +All the select methods can also be chained with each other so you could +do the following to get a neat select query including an expression: + +.. code-block:: php + + select_many('name', 'age', 'height')->select_expr('NOW()', 'timestamp')->find_many(); + +Will result in the query: + +.. code-block:: php + + distinct()->select('name')->find_many(); + +This will result in the query: + +.. code-block:: php + + join('person_profile', array('person.id', '=', 'person_profile.person_id'))->find_many(); + +It is also possible to specify the condition as a string, which will be +inserted as-is into the query. However, in this case the column names +will **not** be escaped, and so this method should be used with caution. + +.. code-block:: php + + join('person_profile', 'person.id = person_profile.person_id')->find_many(); + +The ``join`` methods also take an optional third parameter, which is an +``alias`` for the table in the query. This is useful if you wish to join +the table to *itself* to create a hierarchical structure. In this case, +it is best combined with the ``table_alias`` method, which will add an +alias to the *main* table associated with the ORM, and the ``select`` +method to control which columns get returned. + +.. code-block:: php + + table_alias('p1') + ->select('p1.*') + ->select('p2.name', 'parent_name') + ->join('person', array('p1.parent', '=', 'p2.id'), 'p2') + ->find_many(); + +Raw JOIN clauses +''''''''''''''''' + +If you need to construct a more complex query, you can use the ``raw_join`` +method to specify the SQL fragment for the JOIN clause exactly. This +method takes four required arguments: the string to add to the query, +the conditions is as an *array* containing three components: +the first column, the operator, and the second column, the table alias and +(optional) the parameters array. If parameters are supplied, +the string should contain question mark characters (``?``) to represent +the values to be bound, and the parameter array should contain the values +to be substituted into the string in the correct order. + +This method may be used in a method chain alongside other ``*_join`` +methods as well as methods such as ``offset``, ``limit`` and +``order_by_*``. The contents of the string you supply will be connected +with preceding and following JOIN clauses. + +.. code-block:: php + + raw_join( + 'JOIN (SELECT * FROM role WHERE role.name = ?)', + array('person.role_id', '=', 'role.id'), + 'role', + array('role' => 'janitor')) + ->order_by_asc('person.name') + ->find_many(); + + // Creates SQL: + SELECT * FROM `person` JOIN (SELECT * FROM role WHERE role.name = 'janitor') `role` ON `person`.`role_id` = `role`.`id` ORDER BY `person`.`name` ASC + +Note that this method only supports "question mark placeholder" syntax, +and NOT "named placeholder" syntax. This is because PDO does not allow +queries that contain a mixture of placeholder types. Also, you should +ensure that the number of question mark placeholders in the string +exactly matches the number of elements in the array. + +If you require yet more flexibility, you can manually specify the entire +query. See *Raw queries* below. + + +Aggregate functions +^^^^^^^^^^^^^^^^^^^ + +There is support for ``MIN``, ``AVG``, ``MAX`` and ``SUM`` in addition +to ``COUNT`` (documented earlier). + +To return a minimum value of column, call the ``min()`` method. + +.. code-block:: php + + min('height'); + +The other functions (``AVG``, ``MAX`` and ``SUM``) work in exactly the +same manner. Supply a column name to perform the aggregate function on +and it will return an integer. + +Raw queries +^^^^^^^^^^^ + +If you need to perform more complex queries, you can completely specify +the query to execute by using the ``raw_query`` method. This method +takes a string and optionally an array of parameters. The string can +contain placeholders, either in question mark or named placeholder +syntax, which will be used to bind the parameters to the query. + +.. code-block:: php + + raw_query('SELECT p.* FROM person p JOIN role r ON p.role_id = r.id WHERE r.name = :role', array('role' => 'janitor'))->find_many(); + +The ORM class instance(s) returned will contain data for all the columns +returned by the query. Note that you still must call ``for_table`` to +bind the instances to a particular table, even though there is nothing +to stop you from specifying a completely different table in the query. +This is because if you wish to later called ``save``, the ORM will need +to know which table to update. + +.. note:: + + Using ``raw_query`` is advanced and possibly dangerous, and + Idiorm does not make any attempt to protect you from making errors when + using this method. If you find yourself calling ``raw_query`` often, you + may have misunderstood the purpose of using an ORM, or your application + may be too complex for Idiorm. Consider using a more full-featured + database abstraction system. + +Raw SQL execution using PDO +''''''''''''''''''''''''''' + +.. warning:: + + By using this function you're dropping down to PHPs PDO directly. Idiorm + does not make any attempt to protect you from making errors when using this + method. + + You're essentially just using Idiorm to manage the connection and configuration + when you implement ``raw_execute()``. + +It can be handy, in some instances, to make use of the PDO instance underneath +Idiorm to make advanced queries. These can be things like dropping a table from +the database that Idiorm doesn't support and will not support in the future. These +are operations that fall outside the 80/20 philosophy of Idiorm. That said there is +a lot of interest in this function and quite a lot of support requests related to +it. + +This method directly maps to `PDOStatement::execute()`_ underneath so please +familiarise yourself with it's documentation. + +Dropping tables +~~~~~~~~~~~~~~~ + +This can be done very simply using ``raw_execute()``. + +.. code-block:: php + + fetch(PDO::FETCH_ASSOC)) { + var_dump($row); + } + +It is also worth noting that ``$statement`` is a ``PDOStatement`` instance so calling +its ``fetch()`` method is the same as if you had called against PDO without Idiorm. + +Getting the PDO instance +'''''''''''''''''''''''' + +.. warning:: + + By using this function you're dropping down to PHPs PDO directly. Idiorm + does not make any attempt to protect you from making errors when using this + method. + + You're essentially just using Idiorm to manage the connection and configuration + when you implement against ``get_db()``. + +If none of the preceeding methods suit your purposes then you can also get direct +access to the PDO instance underneath Idiorm using ``ORM::get_db()``. This will +return a configured instance of `PDO`_. + +.. code-block:: php + + query('SHOW TABLES') as $row) { + var_dump($row); + } + +.. _PDOStatement::execute(): https://secure.php.net/manual/en/pdostatement.execute.php +.. _PDO: https://secure.php.net/manual/en/class.pdo.php diff --git a/vendor/j4mie/idiorm/docs/transactions.rst b/vendor/j4mie/idiorm/docs/transactions.rst new file mode 100644 index 000000000..23e6e3178 --- /dev/null +++ b/vendor/j4mie/idiorm/docs/transactions.rst @@ -0,0 +1,21 @@ +Transactions +============ + +Idiorm doesn’t supply any extra methods to deal with transactions, but +it’s very easy to use PDO’s built-in methods: + +.. code-block:: php + + beginTransaction(); + + // Commit a transaction + ORM::get_db()->commit(); + + // Roll back a transaction + ORM::get_db()->rollBack(); + +For more details, see `the PDO documentation on Transactions`_. + +.. _the PDO documentation on Transactions: https://secure.php.net/manual/en/pdo.transactions.php \ No newline at end of file diff --git a/vendor/j4mie/idiorm/idiorm.php b/vendor/j4mie/idiorm/idiorm.php new file mode 100644 index 000000000..2b1f38ca0 --- /dev/null +++ b/vendor/j4mie/idiorm/idiorm.php @@ -0,0 +1,2539 @@ + 'sqlite::memory:', + 'id_column' => 'id', + 'id_column_overrides' => array(), + 'error_mode' => PDO::ERRMODE_EXCEPTION, + 'username' => null, + 'password' => null, + 'driver_options' => null, + 'identifier_quote_character' => null, // if this is null, will be autodetected + 'limit_clause_style' => null, // if this is null, will be autodetected + 'logging' => false, + 'logger' => null, + 'caching' => false, + 'caching_auto_clear' => false, + 'return_result_sets' => false, + ); + + // Map of configuration settings + protected static $_config = array(); + + // Map of database connections, instances of the PDO class + protected static $_db = array(); + + // Last query run, only populated if logging is enabled + protected static $_last_query; + + // Log of all queries run, mapped by connection key, only populated if logging is enabled + protected static $_query_log = array(); + + // Query cache, only used if query caching is enabled + protected static $_query_cache = array(); + + // Reference to previously used PDOStatement object to enable low-level access, if needed + protected static $_last_statement = null; + + // --------------------------- // + // --- INSTANCE PROPERTIES --- // + // --------------------------- // + + // Key name of the connections in self::$_db used by this instance + protected $_connection_name; + + // The name of the table the current ORM instance is associated with + protected $_table_name; + + // Alias for the table to be used in SELECT queries + protected $_table_alias = null; + + // Values to be bound to the query + protected $_values = array(); + + // Columns to select in the result + protected $_result_columns = array('*'); + + // Are we using the default result column or have these been manually changed? + protected $_using_default_result_columns = true; + + // Join sources + protected $_join_sources = array(); + + // Should the query include a DISTINCT keyword? + protected $_distinct = false; + + // Is this a raw query? + protected $_is_raw_query = false; + + // The raw query + protected $_raw_query = ''; + + // The raw query parameters + protected $_raw_parameters = array(); + + // Array of WHERE clauses + protected $_where_conditions = array(); + + // LIMIT + protected $_limit = null; + + // OFFSET + protected $_offset = null; + + // ORDER BY + protected $_order_by = array(); + + // GROUP BY + protected $_group_by = array(); + + // HAVING + protected $_having_conditions = array(); + + // The data for a hydrated instance of the class + protected $_data = array(); + + // Fields that have been modified during the + // lifetime of the object + protected $_dirty_fields = array(); + + // Fields that are to be inserted in the DB raw + protected $_expr_fields = array(); + + // Is this a new object (has create() been called)? + protected $_is_new = false; + + // Name of the column to use as the primary key for + // this instance only. Overrides the config settings. + protected $_instance_id_column = null; + + // ---------------------- // + // --- STATIC METHODS --- // + // ---------------------- // + + /** + * Pass configuration settings to the class in the form of + * key/value pairs. As a shortcut, if the second argument + * is omitted and the key is a string, the setting is + * assumed to be the DSN string used by PDO to connect + * to the database (often, this will be the only configuration + * required to use Idiorm). If you have more than one setting + * you wish to configure, another shortcut is to pass an array + * of settings (and omit the second argument). + * @param string|array $key + * @param mixed $value + * @param string $connection_name Which connection to use + */ + public static function configure($key, $value = null, $connection_name = self::DEFAULT_CONNECTION) { + self::_setup_db_config($connection_name); //ensures at least default config is set + + if (is_array($key)) { + // Shortcut: If only one array argument is passed, + // assume it's an array of configuration settings + foreach ($key as $conf_key => $conf_value) { + self::configure($conf_key, $conf_value, $connection_name); + } + } else { + if (is_null($value)) { + // Shortcut: If only one string argument is passed, + // assume it's a connection string + $value = $key; + $key = 'connection_string'; + } + self::$_config[$connection_name][$key] = $value; + } + } + + /** + * Retrieve configuration options by key, or as whole array. + * @param string $key + * @param string $connection_name Which connection to use + */ + public static function get_config($key = null, $connection_name = self::DEFAULT_CONNECTION) { + if ($key) { + return self::$_config[$connection_name][$key]; + } else { + return self::$_config[$connection_name]; + } + } + + /** + * Delete all configs in _config array. + */ + public static function reset_config() { + self::$_config = array(); + } + + /** + * Despite its slightly odd name, this is actually the factory + * method used to acquire instances of the class. It is named + * this way for the sake of a readable interface, ie + * ORM::for_table('table_name')->find_one()-> etc. As such, + * this will normally be the first method called in a chain. + * @param string $table_name + * @param string $connection_name Which connection to use + * @return ORM + */ + public static function for_table($table_name, $connection_name = self::DEFAULT_CONNECTION) { + self::_setup_db($connection_name); + return new self($table_name, array(), $connection_name); + } + + /** + * Set up the database connection used by the class + * @param string $connection_name Which connection to use + */ + protected static function _setup_db($connection_name = self::DEFAULT_CONNECTION) { + if (!array_key_exists($connection_name, self::$_db) || + !is_object(self::$_db[$connection_name])) { + self::_setup_db_config($connection_name); + + $db = new PDO( + self::$_config[$connection_name]['connection_string'], + self::$_config[$connection_name]['username'], + self::$_config[$connection_name]['password'], + self::$_config[$connection_name]['driver_options'] + ); + + $db->setAttribute(PDO::ATTR_ERRMODE, self::$_config[$connection_name]['error_mode']); + self::set_db($db, $connection_name); + } + } + + /** + * Ensures configuration (multiple connections) is at least set to default. + * @param string $connection_name Which connection to use + */ + protected static function _setup_db_config($connection_name) { + if (!array_key_exists($connection_name, self::$_config)) { + self::$_config[$connection_name] = self::$_default_config; + } + } + + /** + * Set the PDO object used by Idiorm to communicate with the database. + * This is public in case the ORM should use a ready-instantiated + * PDO object as its database connection. Accepts an optional string key + * to identify the connection if multiple connections are used. + * @param PDO $db + * @param string $connection_name Which connection to use + */ + public static function set_db($db, $connection_name = self::DEFAULT_CONNECTION) { + self::_setup_db_config($connection_name); + self::$_db[$connection_name] = $db; + if(!is_null(self::$_db[$connection_name])) { + self::_setup_identifier_quote_character($connection_name); + self::_setup_limit_clause_style($connection_name); + } + } + + /** + * Close and delete all registered PDO objects in _db array. + */ + public static function reset_db() { + self::$_db = null; + + self::$_db = array(); + } + + /** + * Detect and initialise the character used to quote identifiers + * (table names, column names etc). If this has been specified + * manually using ORM::configure('identifier_quote_character', 'some-char'), + * this will do nothing. + * @param string $connection_name Which connection to use + */ + protected static function _setup_identifier_quote_character($connection_name) { + if (is_null(self::$_config[$connection_name]['identifier_quote_character'])) { + self::$_config[$connection_name]['identifier_quote_character'] = + self::_detect_identifier_quote_character($connection_name); + } + } + + /** + * Detect and initialise the limit clause style ("SELECT TOP 5" / + * "... LIMIT 5"). If this has been specified manually using + * ORM::configure('limit_clause_style', 'top'), this will do nothing. + * @param string $connection_name Which connection to use + */ + public static function _setup_limit_clause_style($connection_name) { + if (is_null(self::$_config[$connection_name]['limit_clause_style'])) { + self::$_config[$connection_name]['limit_clause_style'] = + self::_detect_limit_clause_style($connection_name); + } + } + + /** + * Return the correct character used to quote identifiers (table + * names, column names etc) by looking at the driver being used by PDO. + * @param string $connection_name Which connection to use + * @return string + */ + protected static function _detect_identifier_quote_character($connection_name) { + switch(self::get_db($connection_name)->getAttribute(PDO::ATTR_DRIVER_NAME)) { + case 'pgsql': + case 'sqlsrv': + case 'dblib': + case 'mssql': + case 'sybase': + case 'firebird': + return '"'; + case 'mysql': + case 'sqlite': + case 'sqlite2': + default: + return '`'; + } + } + + /** + * Returns a constant after determining the appropriate limit clause + * style + * @param string $connection_name Which connection to use + * @return string Limit clause style keyword/constant + */ + protected static function _detect_limit_clause_style($connection_name) { + switch(self::get_db($connection_name)->getAttribute(PDO::ATTR_DRIVER_NAME)) { + case 'sqlsrv': + case 'dblib': + case 'mssql': + return ORM::LIMIT_STYLE_TOP_N; + default: + return ORM::LIMIT_STYLE_LIMIT; + } + } + + /** + * Returns the PDO instance used by the the ORM to communicate with + * the database. This can be called if any low-level DB access is + * required outside the class. If multiple connections are used, + * accepts an optional key name for the connection. + * @param string $connection_name Which connection to use + * @return PDO + */ + public static function get_db($connection_name = self::DEFAULT_CONNECTION) { + self::_setup_db($connection_name); // required in case this is called before Idiorm is instantiated + return self::$_db[$connection_name]; + } + + /** + * Executes a raw query as a wrapper for PDOStatement::execute. + * Useful for queries that can't be accomplished through Idiorm, + * particularly those using engine-specific features. + * @example raw_execute('SELECT `name`, AVG(`order`) FROM `customer` GROUP BY `name` HAVING AVG(`order`) > 10') + * @example raw_execute('INSERT OR REPLACE INTO `widget` (`id`, `name`) SELECT `id`, `name` FROM `other_table`') + * @param string $query The raw SQL query + * @param array $parameters Optional bound parameters + * @param string $connection_name Which connection to use + * @return bool Success + */ + public static function raw_execute($query, $parameters = array(), $connection_name = self::DEFAULT_CONNECTION) { + self::_setup_db($connection_name); + return self::_execute($query, $parameters, $connection_name); + } + + /** + * Returns the PDOStatement instance last used by any connection wrapped by the ORM. + * Useful for access to PDOStatement::rowCount() or error information + * @return PDOStatement + */ + public static function get_last_statement() { + return self::$_last_statement; + } + + /** + * Internal helper method for executing statments. Logs queries, and + * stores statement object in ::_last_statment, accessible publicly + * through ::get_last_statement() + * @param string $query + * @param array $parameters An array of parameters to be bound in to the query + * @param string $connection_name Which connection to use + * @return bool Response of PDOStatement::execute() + */ + protected static function _execute($query, $parameters = array(), $connection_name = self::DEFAULT_CONNECTION) { + $statement = self::get_db($connection_name)->prepare($query); + self::$_last_statement = $statement; + $time = microtime(true); + + foreach ($parameters as $key => &$param) { + if (is_null($param)) { + $type = PDO::PARAM_NULL; + } else if (is_bool($param)) { + $type = PDO::PARAM_BOOL; + } else if (is_int($param)) { + $type = PDO::PARAM_INT; + } else { + $type = PDO::PARAM_STR; + } + + $statement->bindParam(is_int($key) ? ++$key : $key, $param, $type); + } + + $q = $statement->execute(); + self::_log_query($query, $parameters, $connection_name, (microtime(true)-$time)); + + return $q; + } + + /** + * Add a query to the internal query log. Only works if the + * 'logging' config option is set to true. + * + * This works by manually binding the parameters to the query - the + * query isn't executed like this (PDO normally passes the query and + * parameters to the database which takes care of the binding) but + * doing it this way makes the logged queries more readable. + * @param string $query + * @param array $parameters An array of parameters to be bound in to the query + * @param string $connection_name Which connection to use + * @param float $query_time Query time + * @return bool + */ + protected static function _log_query($query, $parameters, $connection_name, $query_time) { + // If logging is not enabled, do nothing + if (!self::$_config[$connection_name]['logging']) { + return false; + } + + if (!isset(self::$_query_log[$connection_name])) { + self::$_query_log[$connection_name] = array(); + } + + if (empty($parameters)) { + $bound_query = $query; + } else { + // Escape the parameters + $parameters = array_map(array(self::get_db($connection_name), 'quote'), $parameters); + + if (array_values($parameters) === $parameters) { + // ? placeholders + // Avoid %format collision for vsprintf + $query = str_replace("%", "%%", $query); + + // Replace placeholders in the query for vsprintf + if(false !== strpos($query, "'") || false !== strpos($query, '"')) { + $query = IdiormString::str_replace_outside_quotes("?", "%s", $query); + } else { + $query = str_replace("?", "%s", $query); + } + + // Replace the question marks in the query with the parameters + $bound_query = vsprintf($query, $parameters); + } else { + // named placeholders + foreach ($parameters as $key => $val) { + $query = str_replace($key, $val, $query); + } + $bound_query = $query; + } + } + + self::$_last_query = $bound_query; + self::$_query_log[$connection_name][] = $bound_query; + + + if(is_callable(self::$_config[$connection_name]['logger'])){ + $logger = self::$_config[$connection_name]['logger']; + $logger($bound_query, $query_time); + } + + return true; + } + + /** + * Get the last query executed. Only works if the + * 'logging' config option is set to true. Otherwise + * this will return null. Returns last query from all connections if + * no connection_name is specified + * @param null|string $connection_name Which connection to use + * @return string + */ + public static function get_last_query($connection_name = null) { + if ($connection_name === null) { + return self::$_last_query; + } + if (!isset(self::$_query_log[$connection_name])) { + return ''; + } + + return end(self::$_query_log[$connection_name]); + } + + /** + * Get an array containing all the queries run on a + * specified connection up to now. + * Only works if the 'logging' config option is + * set to true. Otherwise, returned array will be empty. + * @param string $connection_name Which connection to use + */ + public static function get_query_log($connection_name = self::DEFAULT_CONNECTION) { + if (isset(self::$_query_log[$connection_name])) { + return self::$_query_log[$connection_name]; + } + return array(); + } + + /** + * Get a list of the available connection names + * @return array + */ + public static function get_connection_names() { + return array_keys(self::$_db); + } + + // ------------------------ // + // --- INSTANCE METHODS --- // + // ------------------------ // + + /** + * "Private" constructor; shouldn't be called directly. + * Use the ORM::for_table factory method instead. + */ + protected function __construct($table_name, $data = array(), $connection_name = self::DEFAULT_CONNECTION) { + $this->_table_name = $table_name; + $this->_data = $data; + + $this->_connection_name = $connection_name; + self::_setup_db_config($connection_name); + } + + /** + * Create a new, empty instance of the class. Used + * to add a new row to your database. May optionally + * be passed an associative array of data to populate + * the instance. If so, all fields will be flagged as + * dirty so all will be saved to the database when + * save() is called. + */ + public function create($data=null) { + $this->_is_new = true; + if (!is_null($data)) { + return $this->hydrate($data)->force_all_dirty(); + } + return $this; + } + + /** + * Specify the ID column to use for this instance or array of instances only. + * This overrides the id_column and id_column_overrides settings. + * + * This is mostly useful for libraries built on top of Idiorm, and will + * not normally be used in manually built queries. If you don't know why + * you would want to use this, you should probably just ignore it. + */ + public function use_id_column($id_column) { + $this->_instance_id_column = $id_column; + return $this; + } + + /** + * Create an ORM instance from the given row (an associative + * array of data fetched from the database) + */ + protected function _create_instance_from_row($row) { + $instance = self::for_table($this->_table_name, $this->_connection_name); + $instance->use_id_column($this->_instance_id_column); + $instance->hydrate($row); + return $instance; + } + + /** + * Tell the ORM that you are expecting a single result + * back from your query, and execute it. Will return + * a single instance of the ORM class, or false if no + * rows were returned. + * As a shortcut, you may supply an ID as a parameter + * to this method. This will perform a primary key + * lookup on the table. + */ + public function find_one($id=null) { + if (!is_null($id)) { + $this->where_id_is($id); + } + $this->limit(1); + $rows = $this->_run(); + + if (empty($rows)) { + return false; + } + + return $this->_create_instance_from_row($rows[0]); + } + + /** + * Tell the ORM that you are expecting multiple results + * from your query, and execute it. Will return an array + * of instances of the ORM class, or an empty array if + * no rows were returned. + * @return array|\IdiormResultSet + */ + public function find_many() { + if(self::$_config[$this->_connection_name]['return_result_sets']) { + return $this->find_result_set(); + } + return $this->_find_many(); + } + + /** + * Tell the ORM that you are expecting multiple results + * from your query, and execute it. Will return an array + * of instances of the ORM class, or an empty array if + * no rows were returned. + * @return array + */ + protected function _find_many() { + $rows = $this->_run(); + return array_map(array($this, '_create_instance_from_row'), $rows); + } + + /** + * Tell the ORM that you are expecting multiple results + * from your query, and execute it. Will return a result set object + * containing instances of the ORM class. + * @return \IdiormResultSet + */ + public function find_result_set() { + return new IdiormResultSet($this->_find_many()); + } + + /** + * Tell the ORM that you are expecting multiple results + * from your query, and execute it. Will return an array, + * or an empty array if no rows were returned. + * @return array + */ + public function find_array() { + return $this->_run(); + } + + /** + * Tell the ORM that you wish to execute a COUNT query. + * Will return an integer representing the number of + * rows returned. + */ + public function count($column = '*') { + return $this->_call_aggregate_db_function(__FUNCTION__, $column); + } + + /** + * Tell the ORM that you wish to execute a MAX query. + * Will return the max value of the choosen column. + */ + public function max($column) { + return $this->_call_aggregate_db_function(__FUNCTION__, $column); + } + + /** + * Tell the ORM that you wish to execute a MIN query. + * Will return the min value of the choosen column. + */ + public function min($column) { + return $this->_call_aggregate_db_function(__FUNCTION__, $column); + } + + /** + * Tell the ORM that you wish to execute a AVG query. + * Will return the average value of the choosen column. + */ + public function avg($column) { + return $this->_call_aggregate_db_function(__FUNCTION__, $column); + } + + /** + * Tell the ORM that you wish to execute a SUM query. + * Will return the sum of the choosen column. + */ + public function sum($column) { + return $this->_call_aggregate_db_function(__FUNCTION__, $column); + } + + /** + * Execute an aggregate query on the current connection. + * @param string $sql_function The aggregate function to call eg. MIN, COUNT, etc + * @param string $column The column to execute the aggregate query against + * @return int + */ + protected function _call_aggregate_db_function($sql_function, $column) { + $alias = strtolower($sql_function); + $sql_function = strtoupper($sql_function); + if('*' != $column) { + $column = $this->_quote_identifier($column); + } + $result_columns = $this->_result_columns; + $this->_result_columns = array(); + $this->select_expr("$sql_function($column)", $alias); + $result = $this->find_one(); + $this->_result_columns = $result_columns; + + $return_value = 0; + if($result !== false && isset($result->$alias)) { + if (!is_numeric($result->$alias)) { + $return_value = $result->$alias; + } + elseif((int) $result->$alias == (float) $result->$alias) { + $return_value = (int) $result->$alias; + } else { + $return_value = (float) $result->$alias; + } + } + return $return_value; + } + + /** + * This method can be called to hydrate (populate) this + * instance of the class from an associative array of data. + * This will usually be called only from inside the class, + * but it's public in case you need to call it directly. + */ + public function hydrate($data=array()) { + $this->_data = $data; + return $this; + } + + /** + * Force the ORM to flag all the fields in the $data array + * as "dirty" and therefore update them when save() is called. + */ + public function force_all_dirty() { + $this->_dirty_fields = $this->_data; + return $this; + } + + /** + * Perform a raw query. The query can contain placeholders in + * either named or question mark style. If placeholders are + * used, the parameters should be an array of values which will + * be bound to the placeholders in the query. If this method + * is called, all other query building methods will be ignored. + */ + public function raw_query($query, $parameters = array()) { + $this->_is_raw_query = true; + $this->_raw_query = $query; + $this->_raw_parameters = $parameters; + return $this; + } + + /** + * Add an alias for the main table to be used in SELECT queries + */ + public function table_alias($alias) { + $this->_table_alias = $alias; + return $this; + } + + /** + * Internal method to add an unquoted expression to the set + * of columns returned by the SELECT query. The second optional + * argument is the alias to return the expression as. + */ + protected function _add_result_column($expr, $alias=null) { + if (!is_null($alias)) { + $expr .= " AS " . $this->_quote_identifier($alias); + } + + if ($this->_using_default_result_columns) { + $this->_result_columns = array($expr); + $this->_using_default_result_columns = false; + } else { + $this->_result_columns[] = $expr; + } + return $this; + } + + /** + * Counts the number of columns that belong to the primary + * key and their value is null. + */ + public function count_null_id_columns() { + if (is_array($this->_get_id_column_name())) { + return count(array_filter($this->id(), 'is_null')); + } else { + return is_null($this->id()) ? 1 : 0; + } + } + + /** + * Add a column to the list of columns returned by the SELECT + * query. This defaults to '*'. The second optional argument is + * the alias to return the column as. + */ + public function select($column, $alias=null) { + $column = $this->_quote_identifier($column); + return $this->_add_result_column($column, $alias); + } + + /** + * Add an unquoted expression to the list of columns returned + * by the SELECT query. The second optional argument is + * the alias to return the column as. + */ + public function select_expr($expr, $alias=null) { + return $this->_add_result_column($expr, $alias); + } + + /** + * Add columns to the list of columns returned by the SELECT + * query. This defaults to '*'. Many columns can be supplied + * as either an array or as a list of parameters to the method. + * + * Note that the alias must not be numeric - if you want a + * numeric alias then prepend it with some alpha chars. eg. a1 + * + * @example select_many(array('alias' => 'column', 'column2', 'alias2' => 'column3'), 'column4', 'column5'); + * @example select_many('column', 'column2', 'column3'); + * @example select_many(array('column', 'column2', 'column3'), 'column4', 'column5'); + * + * @return \ORM + */ + public function select_many() { + $columns = func_get_args(); + if(!empty($columns)) { + $columns = $this->_normalise_select_many_columns($columns); + foreach($columns as $alias => $column) { + if(is_numeric($alias)) { + $alias = null; + } + $this->select($column, $alias); + } + } + return $this; + } + + /** + * Add an unquoted expression to the list of columns returned + * by the SELECT query. Many columns can be supplied as either + * an array or as a list of parameters to the method. + * + * Note that the alias must not be numeric - if you want a + * numeric alias then prepend it with some alpha chars. eg. a1 + * + * @example select_many_expr(array('alias' => 'column', 'column2', 'alias2' => 'column3'), 'column4', 'column5') + * @example select_many_expr('column', 'column2', 'column3') + * @example select_many_expr(array('column', 'column2', 'column3'), 'column4', 'column5') + * + * @return \ORM + */ + public function select_many_expr() { + $columns = func_get_args(); + if(!empty($columns)) { + $columns = $this->_normalise_select_many_columns($columns); + foreach($columns as $alias => $column) { + if(is_numeric($alias)) { + $alias = null; + } + $this->select_expr($column, $alias); + } + } + return $this; + } + + /** + * Take a column specification for the select many methods and convert it + * into a normalised array of columns and aliases. + * + * It is designed to turn the following styles into a normalised array: + * + * array(array('alias' => 'column', 'column2', 'alias2' => 'column3'), 'column4', 'column5')) + * + * @param array $columns + * @return array + */ + protected function _normalise_select_many_columns($columns) { + $return = array(); + foreach($columns as $column) { + if(is_array($column)) { + foreach($column as $key => $value) { + if(!is_numeric($key)) { + $return[$key] = $value; + } else { + $return[] = $value; + } + } + } else { + $return[] = $column; + } + } + return $return; + } + + /** + * Add a DISTINCT keyword before the list of columns in the SELECT query + */ + public function distinct() { + $this->_distinct = true; + return $this; + } + + /** + * Internal method to add a JOIN source to the query. + * + * The join_operator should be one of INNER, LEFT OUTER, CROSS etc - this + * will be prepended to JOIN. + * + * The table should be the name of the table to join to. + * + * The constraint may be either a string or an array with three elements. If it + * is a string, it will be compiled into the query as-is, with no escaping. The + * recommended way to supply the constraint is as an array with three elements: + * + * first_column, operator, second_column + * + * Example: array('user.id', '=', 'profile.user_id') + * + * will compile to + * + * ON `user`.`id` = `profile`.`user_id` + * + * The final (optional) argument specifies an alias for the joined table. + */ + protected function _add_join_source($join_operator, $table, $constraint, $table_alias=null) { + + $join_operator = trim("{$join_operator} JOIN"); + + $table = $this->_quote_identifier($table); + + // Add table alias if present + if (!is_null($table_alias)) { + $table_alias = $this->_quote_identifier($table_alias); + $table .= " {$table_alias}"; + } + + // Build the constraint + if (is_array($constraint)) { + list($first_column, $operator, $second_column) = $constraint; + $first_column = $this->_quote_identifier($first_column); + $second_column = $this->_quote_identifier($second_column); + $constraint = "{$first_column} {$operator} {$second_column}"; + } + + $this->_join_sources[] = "{$join_operator} {$table} ON {$constraint}"; + return $this; + } + + /** + * Add a RAW JOIN source to the query + */ + public function raw_join($table, $constraint, $table_alias, $parameters = array()) { + // Add table alias if present + if (!is_null($table_alias)) { + $table_alias = $this->_quote_identifier($table_alias); + $table .= " {$table_alias}"; + } + + $this->_values = array_merge($this->_values, $parameters); + + // Build the constraint + if (is_array($constraint)) { + list($first_column, $operator, $second_column) = $constraint; + $first_column = $this->_quote_identifier($first_column); + $second_column = $this->_quote_identifier($second_column); + $constraint = "{$first_column} {$operator} {$second_column}"; + } + + $this->_join_sources[] = "{$table} ON {$constraint}"; + return $this; + } + + /** + * Add a simple JOIN source to the query + */ + public function join($table, $constraint, $table_alias=null) { + return $this->_add_join_source("", $table, $constraint, $table_alias); + } + + /** + * Add an INNER JOIN souce to the query + */ + public function inner_join($table, $constraint, $table_alias=null) { + return $this->_add_join_source("INNER", $table, $constraint, $table_alias); + } + + /** + * Add a LEFT OUTER JOIN souce to the query + */ + public function left_outer_join($table, $constraint, $table_alias=null) { + return $this->_add_join_source("LEFT OUTER", $table, $constraint, $table_alias); + } + + /** + * Add an RIGHT OUTER JOIN souce to the query + */ + public function right_outer_join($table, $constraint, $table_alias=null) { + return $this->_add_join_source("RIGHT OUTER", $table, $constraint, $table_alias); + } + + /** + * Add an FULL OUTER JOIN souce to the query + */ + public function full_outer_join($table, $constraint, $table_alias=null) { + return $this->_add_join_source("FULL OUTER", $table, $constraint, $table_alias); + } + + /** + * Internal method to add a HAVING condition to the query + */ + protected function _add_having($fragment, $values=array()) { + return $this->_add_condition('having', $fragment, $values); + } + + /** + * Internal method to add a HAVING condition to the query + */ + protected function _add_simple_having($column_name, $separator, $value) { + return $this->_add_simple_condition('having', $column_name, $separator, $value); + } + + /** + * Internal method to add a HAVING clause with multiple values (like IN and NOT IN) + */ + public function _add_having_placeholder($column_name, $separator, $values) { + if (!is_array($column_name)) { + $data = array($column_name => $values); + } else { + $data = $column_name; + } + $result = $this; + foreach ($data as $key => $val) { + $column = $result->_quote_identifier($key); + $placeholders = $result->_create_placeholders($val); + $result = $result->_add_having("{$column} {$separator} ({$placeholders})", $val); + } + return $result; + } + + /** + * Internal method to add a HAVING clause with no parameters(like IS NULL and IS NOT NULL) + */ + public function _add_having_no_value($column_name, $operator) { + $conditions = (is_array($column_name)) ? $column_name : array($column_name); + $result = $this; + foreach($conditions as $column) { + $column = $this->_quote_identifier($column); + $result = $result->_add_having("{$column} {$operator}"); + } + return $result; + } + + /** + * Internal method to add a WHERE condition to the query + */ + protected function _add_where($fragment, $values=array()) { + return $this->_add_condition('where', $fragment, $values); + } + + /** + * Internal method to add a WHERE condition to the query + */ + protected function _add_simple_where($column_name, $separator, $value) { + return $this->_add_simple_condition('where', $column_name, $separator, $value); + } + + /** + * Add a WHERE clause with multiple values (like IN and NOT IN) + */ + public function _add_where_placeholder($column_name, $separator, $values) { + if (!is_array($column_name)) { + $data = array($column_name => $values); + } else { + $data = $column_name; + } + $result = $this; + foreach ($data as $key => $val) { + $column = $result->_quote_identifier($key); + $placeholders = $result->_create_placeholders($val); + $result = $result->_add_where("{$column} {$separator} ({$placeholders})", $val); + } + return $result; + } + + /** + * Add a WHERE clause with no parameters(like IS NULL and IS NOT NULL) + */ + public function _add_where_no_value($column_name, $operator) { + $conditions = (is_array($column_name)) ? $column_name : array($column_name); + $result = $this; + foreach($conditions as $column) { + $column = $this->_quote_identifier($column); + $result = $result->_add_where("{$column} {$operator}"); + } + return $result; + } + + /** + * Internal method to add a HAVING or WHERE condition to the query + */ + protected function _add_condition($type, $fragment, $values=array()) { + $conditions_class_property_name = "_{$type}_conditions"; + if (!is_array($values)) { + $values = array($values); + } + array_push($this->$conditions_class_property_name, array( + self::CONDITION_FRAGMENT => $fragment, + self::CONDITION_VALUES => $values, + )); + return $this; + } + + /** + * Helper method to compile a simple COLUMN SEPARATOR VALUE + * style HAVING or WHERE condition into a string and value ready to + * be passed to the _add_condition method. Avoids duplication + * of the call to _quote_identifier + * + * If column_name is an associative array, it will add a condition for each column + */ + protected function _add_simple_condition($type, $column_name, $separator, $value) { + $multiple = is_array($column_name) ? $column_name : array($column_name => $value); + $result = $this; + + foreach($multiple as $key => $val) { + // Add the table name in case of ambiguous columns + if (count($result->_join_sources) > 0 && strpos($key, '.') === false) { + $table = $result->_table_name; + if (!is_null($result->_table_alias)) { + $table = $result->_table_alias; + } + + $key = "{$table}.{$key}"; + } + $key = $result->_quote_identifier($key); + $result = $result->_add_condition($type, "{$key} {$separator} ?", $val); + } + return $result; + } + + /** + * Return a string containing the given number of question marks, + * separated by commas. Eg "?, ?, ?" + */ + protected function _create_placeholders($fields) { + if(!empty($fields)) { + $db_fields = array(); + foreach($fields as $key => $value) { + // Process expression fields directly into the query + if(array_key_exists($key, $this->_expr_fields)) { + $db_fields[] = $value; + } else { + $db_fields[] = '?'; + } + } + return implode(', ', $db_fields); + } + } + + /** + * Helper method that filters a column/value array returning only those + * columns that belong to a compound primary key. + * + * If the key contains a column that does not exist in the given array, + * a null value will be returned for it. + */ + protected function _get_compound_id_column_values($value) { + $filtered = array(); + foreach($this->_get_id_column_name() as $key) { + $filtered[$key] = isset($value[$key]) ? $value[$key] : null; + } + return $filtered; + } + + /** + * Helper method that filters an array containing compound column/value + * arrays. + */ + protected function _get_compound_id_column_values_array($values) { + $filtered = array(); + foreach($values as $value) { + $filtered[] = $this->_get_compound_id_column_values($value); + } + return $filtered; + } + + /** + * Add a WHERE column = value clause to your query. Each time + * this is called in the chain, an additional WHERE will be + * added, and these will be ANDed together when the final query + * is built. + * + * If you use an array in $column_name, a new clause will be + * added for each element. In this case, $value is ignored. + */ + public function where($column_name, $value=null) { + return $this->where_equal($column_name, $value); + } + + /** + * More explicitly named version of for the where() method. + * Can be used if preferred. + */ + public function where_equal($column_name, $value=null) { + return $this->_add_simple_where($column_name, '=', $value); + } + + /** + * Add a WHERE column != value clause to your query. + */ + public function where_not_equal($column_name, $value=null) { + return $this->_add_simple_where($column_name, '!=', $value); + } + + /** + * Special method to query the table by its primary key + * + * If primary key is compound, only the columns that + * belong to they key will be used for the query + */ + public function where_id_is($id) { + return (is_array($this->_get_id_column_name())) ? + $this->where($this->_get_compound_id_column_values($id), null) : + $this->where($this->_get_id_column_name(), $id); + } + + /** + * Allows adding a WHERE clause that matches any of the conditions + * specified in the array. Each element in the associative array will + * be a different condition, where the key will be the column name. + * + * By default, an equal operator will be used against all columns, but + * it can be overriden for any or every column using the second parameter. + * + * Each condition will be ORed together when added to the final query. + */ + public function where_any_is($values, $operator='=') { + $data = array(); + $query = array("(("); + $first = true; + foreach ($values as $value) { + if ($first) { + $first = false; + } else { + $query[] = ") OR ("; + } + $firstsub = true; + foreach($value as $key => $item) { + $op = is_string($operator) ? $operator : (isset($operator[$key]) ? $operator[$key] : '='); + if ($firstsub) { + $firstsub = false; + } else { + $query[] = "AND"; + } + $query[] = $this->_quote_identifier($key); + $data[] = $item; + $query[] = $op . " ?"; + } + } + $query[] = "))"; + return $this->where_raw(join(' ', $query), $data); + } + + /** + * Similar to where_id_is() but allowing multiple primary keys. + * + * If primary key is compound, only the columns that + * belong to they key will be used for the query + */ + public function where_id_in($ids) { + return (is_array($this->_get_id_column_name())) ? + $this->where_any_is($this->_get_compound_id_column_values_array($ids)) : + $this->where_in($this->_get_id_column_name(), $ids); + } + + /** + * Add a WHERE ... LIKE clause to your query. + */ + public function where_like($column_name, $value=null) { + return $this->_add_simple_where($column_name, 'LIKE', $value); + } + + /** + * Add where WHERE ... NOT LIKE clause to your query. + */ + public function where_not_like($column_name, $value=null) { + return $this->_add_simple_where($column_name, 'NOT LIKE', $value); + } + + /** + * Add a WHERE ... > clause to your query + */ + public function where_gt($column_name, $value=null) { + return $this->_add_simple_where($column_name, '>', $value); + } + + /** + * Add a WHERE ... < clause to your query + */ + public function where_lt($column_name, $value=null) { + return $this->_add_simple_where($column_name, '<', $value); + } + + /** + * Add a WHERE ... >= clause to your query + */ + public function where_gte($column_name, $value=null) { + return $this->_add_simple_where($column_name, '>=', $value); + } + + /** + * Add a WHERE ... <= clause to your query + */ + public function where_lte($column_name, $value=null) { + return $this->_add_simple_where($column_name, '<=', $value); + } + + /** + * Add a WHERE ... IN clause to your query + */ + public function where_in($column_name, $values) { + return $this->_add_where_placeholder($column_name, 'IN', $values); + } + + /** + * Add a WHERE ... NOT IN clause to your query + */ + public function where_not_in($column_name, $values) { + return $this->_add_where_placeholder($column_name, 'NOT IN', $values); + } + + /** + * Add a WHERE column IS NULL clause to your query + */ + public function where_null($column_name) { + return $this->_add_where_no_value($column_name, "IS NULL"); + } + + /** + * Add a WHERE column IS NOT NULL clause to your query + */ + public function where_not_null($column_name) { + return $this->_add_where_no_value($column_name, "IS NOT NULL"); + } + + /** + * Add a raw WHERE clause to the query. The clause should + * contain question mark placeholders, which will be bound + * to the parameters supplied in the second argument. + */ + public function where_raw($clause, $parameters=array()) { + return $this->_add_where($clause, $parameters); + } + + /** + * Add a LIMIT to the query + */ + public function limit($limit) { + $this->_limit = $limit; + return $this; + } + + /** + * Add an OFFSET to the query + */ + public function offset($offset) { + $this->_offset = $offset; + return $this; + } + + /** + * Add an ORDER BY clause to the query + */ + protected function _add_order_by($column_name, $ordering) { + $column_name = $this->_quote_identifier($column_name); + $this->_order_by[] = "{$column_name} {$ordering}"; + return $this; + } + + /** + * Add an ORDER BY column DESC clause + */ + public function order_by_desc($column_name) { + return $this->_add_order_by($column_name, 'DESC'); + } + + /** + * Add an ORDER BY column ASC clause + */ + public function order_by_asc($column_name) { + return $this->_add_order_by($column_name, 'ASC'); + } + + /** + * Add an unquoted expression as an ORDER BY clause + */ + public function order_by_expr($clause) { + $this->_order_by[] = $clause; + return $this; + } + + /** + * Add a column to the list of columns to GROUP BY + */ + public function group_by($column_name) { + $column_name = $this->_quote_identifier($column_name); + $this->_group_by[] = $column_name; + return $this; + } + + /** + * Add an unquoted expression to the list of columns to GROUP BY + */ + public function group_by_expr($expr) { + $this->_group_by[] = $expr; + return $this; + } + + /** + * Add a HAVING column = value clause to your query. Each time + * this is called in the chain, an additional HAVING will be + * added, and these will be ANDed together when the final query + * is built. + * + * If you use an array in $column_name, a new clause will be + * added for each element. In this case, $value is ignored. + */ + public function having($column_name, $value=null) { + return $this->having_equal($column_name, $value); + } + + /** + * More explicitly named version of for the having() method. + * Can be used if preferred. + */ + public function having_equal($column_name, $value=null) { + return $this->_add_simple_having($column_name, '=', $value); + } + + /** + * Add a HAVING column != value clause to your query. + */ + public function having_not_equal($column_name, $value=null) { + return $this->_add_simple_having($column_name, '!=', $value); + } + + /** + * Special method to query the table by its primary key. + * + * If primary key is compound, only the columns that + * belong to they key will be used for the query + */ + public function having_id_is($id) { + return (is_array($this->_get_id_column_name())) ? + $this->having($this->_get_compound_id_column_values($id), null) : + $this->having($this->_get_id_column_name(), $id); + } + + /** + * Add a HAVING ... LIKE clause to your query. + */ + public function having_like($column_name, $value=null) { + return $this->_add_simple_having($column_name, 'LIKE', $value); + } + + /** + * Add where HAVING ... NOT LIKE clause to your query. + */ + public function having_not_like($column_name, $value=null) { + return $this->_add_simple_having($column_name, 'NOT LIKE', $value); + } + + /** + * Add a HAVING ... > clause to your query + */ + public function having_gt($column_name, $value=null) { + return $this->_add_simple_having($column_name, '>', $value); + } + + /** + * Add a HAVING ... < clause to your query + */ + public function having_lt($column_name, $value=null) { + return $this->_add_simple_having($column_name, '<', $value); + } + + /** + * Add a HAVING ... >= clause to your query + */ + public function having_gte($column_name, $value=null) { + return $this->_add_simple_having($column_name, '>=', $value); + } + + /** + * Add a HAVING ... <= clause to your query + */ + public function having_lte($column_name, $value=null) { + return $this->_add_simple_having($column_name, '<=', $value); + } + + /** + * Add a HAVING ... IN clause to your query + */ + public function having_in($column_name, $values=null) { + return $this->_add_having_placeholder($column_name, 'IN', $values); + } + + /** + * Add a HAVING ... NOT IN clause to your query + */ + public function having_not_in($column_name, $values=null) { + return $this->_add_having_placeholder($column_name, 'NOT IN', $values); + } + + /** + * Add a HAVING column IS NULL clause to your query + */ + public function having_null($column_name) { + return $this->_add_having_no_value($column_name, 'IS NULL'); + } + + /** + * Add a HAVING column IS NOT NULL clause to your query + */ + public function having_not_null($column_name) { + return $this->_add_having_no_value($column_name, 'IS NOT NULL'); + } + + /** + * Add a raw HAVING clause to the query. The clause should + * contain question mark placeholders, which will be bound + * to the parameters supplied in the second argument. + */ + public function having_raw($clause, $parameters=array()) { + return $this->_add_having($clause, $parameters); + } + + /** + * Build a SELECT statement based on the clauses that have + * been passed to this instance by chaining method calls. + */ + protected function _build_select() { + // If the query is raw, just set the $this->_values to be + // the raw query parameters and return the raw query + if ($this->_is_raw_query) { + $this->_values = $this->_raw_parameters; + return $this->_raw_query; + } + + // Build and return the full SELECT statement by concatenating + // the results of calling each separate builder method. + return $this->_join_if_not_empty(" ", array( + $this->_build_select_start(), + $this->_build_join(), + $this->_build_where(), + $this->_build_group_by(), + $this->_build_having(), + $this->_build_order_by(), + $this->_build_limit(), + $this->_build_offset(), + )); + } + + /** + * Build the start of the SELECT statement + */ + protected function _build_select_start() { + $fragment = 'SELECT '; + $result_columns = join(', ', $this->_result_columns); + + if (!is_null($this->_limit) && + self::$_config[$this->_connection_name]['limit_clause_style'] === ORM::LIMIT_STYLE_TOP_N) { + $fragment .= "TOP {$this->_limit} "; + } + + if ($this->_distinct) { + $result_columns = 'DISTINCT ' . $result_columns; + } + + $fragment .= "{$result_columns} FROM " . $this->_quote_identifier($this->_table_name); + + if (!is_null($this->_table_alias)) { + $fragment .= " " . $this->_quote_identifier($this->_table_alias); + } + return $fragment; + } + + /** + * Build the JOIN sources + */ + protected function _build_join() { + if (count($this->_join_sources) === 0) { + return ''; + } + + return join(" ", $this->_join_sources); + } + + /** + * Build the WHERE clause(s) + */ + protected function _build_where() { + return $this->_build_conditions('where'); + } + + /** + * Build the HAVING clause(s) + */ + protected function _build_having() { + return $this->_build_conditions('having'); + } + + /** + * Build GROUP BY + */ + protected function _build_group_by() { + if (count($this->_group_by) === 0) { + return ''; + } + return "GROUP BY " . join(", ", $this->_group_by); + } + + /** + * Build a WHERE or HAVING clause + * @param string $type + * @return string + */ + protected function _build_conditions($type) { + $conditions_class_property_name = "_{$type}_conditions"; + // If there are no clauses, return empty string + if (count($this->$conditions_class_property_name) === 0) { + return ''; + } + + $conditions = array(); + foreach ($this->$conditions_class_property_name as $condition) { + $conditions[] = $condition[self::CONDITION_FRAGMENT]; + $this->_values = array_merge($this->_values, $condition[self::CONDITION_VALUES]); + } + + return strtoupper($type) . " " . join(" AND ", $conditions); + } + + /** + * Build ORDER BY + */ + protected function _build_order_by() { + if (count($this->_order_by) === 0) { + return ''; + } + return "ORDER BY " . join(", ", $this->_order_by); + } + + /** + * Build LIMIT + */ + protected function _build_limit() { + $fragment = ''; + if (!is_null($this->_limit) && + self::$_config[$this->_connection_name]['limit_clause_style'] == ORM::LIMIT_STYLE_LIMIT) { + if (self::get_db($this->_connection_name)->getAttribute(PDO::ATTR_DRIVER_NAME) == 'firebird') { + $fragment = 'ROWS'; + } else { + $fragment = 'LIMIT'; + } + $fragment .= " {$this->_limit}"; + } + return $fragment; + } + + /** + * Build OFFSET + */ + protected function _build_offset() { + if (!is_null($this->_offset)) { + $clause = 'OFFSET'; + if (self::get_db($this->_connection_name)->getAttribute(PDO::ATTR_DRIVER_NAME) == 'firebird') { + $clause = 'TO'; + } + return "$clause " . $this->_offset; + } + return ''; + } + + /** + * Wrapper around PHP's join function which + * only adds the pieces if they are not empty. + */ + protected function _join_if_not_empty($glue, $pieces) { + $filtered_pieces = array(); + foreach ($pieces as $piece) { + if (is_string($piece)) { + $piece = trim($piece); + } + if (!empty($piece)) { + $filtered_pieces[] = $piece; + } + } + return join($glue, $filtered_pieces); + } + + /** + * Quote a string that is used as an identifier + * (table names, column names etc). This method can + * also deal with dot-separated identifiers eg table.column + */ + protected function _quote_one_identifier($identifier) { + $parts = explode('.', $identifier); + $parts = array_map(array($this, '_quote_identifier_part'), $parts); + return join('.', $parts); + } + + /** + * Quote a string that is used as an identifier + * (table names, column names etc) or an array containing + * multiple identifiers. This method can also deal with + * dot-separated identifiers eg table.column + */ + protected function _quote_identifier($identifier) { + if (is_array($identifier)) { + $result = array_map(array($this, '_quote_one_identifier'), $identifier); + return join(', ', $result); + } else { + return $this->_quote_one_identifier($identifier); + } + } + + /** + * This method performs the actual quoting of a single + * part of an identifier, using the identifier quote + * character specified in the config (or autodetected). + */ + protected function _quote_identifier_part($part) { + if ($part === '*') { + return $part; + } + + $quote_character = self::$_config[$this->_connection_name]['identifier_quote_character']; + // double up any identifier quotes to escape them + return $quote_character . + str_replace($quote_character, + $quote_character . $quote_character, + $part + ) . $quote_character; + } + + /** + * Create a cache key for the given query and parameters. + */ + protected static function _create_cache_key($query, $parameters, $table_name = null, $connection_name = self::DEFAULT_CONNECTION) { + if(isset(self::$_config[$connection_name]['create_cache_key']) and is_callable(self::$_config[$connection_name]['create_cache_key'])){ + return call_user_func_array(self::$_config[$connection_name]['create_cache_key'], array($query, $parameters, $table_name, $connection_name)); + } + $parameter_string = join(',', $parameters); + $key = $query . ':' . $parameter_string; + return sha1($key); + } + + /** + * Check the query cache for the given cache key. If a value + * is cached for the key, return the value. Otherwise, return false. + */ + protected static function _check_query_cache($cache_key, $table_name = null, $connection_name = self::DEFAULT_CONNECTION) { + if(isset(self::$_config[$connection_name]['check_query_cache']) and is_callable(self::$_config[$connection_name]['check_query_cache'])){ + return call_user_func_array(self::$_config[$connection_name]['check_query_cache'], array($cache_key, $table_name, $connection_name)); + } elseif (isset(self::$_query_cache[$connection_name][$cache_key])) { + return self::$_query_cache[$connection_name][$cache_key]; + } + return false; + } + + /** + * Clear the query cache + */ + public static function clear_cache($table_name = null, $connection_name = self::DEFAULT_CONNECTION) { + self::$_query_cache = array(); + if(isset(self::$_config[$connection_name]['clear_cache']) and is_callable(self::$_config[$connection_name]['clear_cache'])){ + return call_user_func_array(self::$_config[$connection_name]['clear_cache'], array($table_name, $connection_name)); + } + } + + /** + * Add the given value to the query cache. + */ + protected static function _cache_query_result($cache_key, $value, $table_name = null, $connection_name = self::DEFAULT_CONNECTION) { + if(isset(self::$_config[$connection_name]['cache_query_result']) and is_callable(self::$_config[$connection_name]['cache_query_result'])){ + return call_user_func_array(self::$_config[$connection_name]['cache_query_result'], array($cache_key, $value, $table_name, $connection_name)); + } elseif (!isset(self::$_query_cache[$connection_name])) { + self::$_query_cache[$connection_name] = array(); + } + self::$_query_cache[$connection_name][$cache_key] = $value; + } + + /** + * Execute the SELECT query that has been built up by chaining methods + * on this class. Return an array of rows as associative arrays. + */ + protected function _run() { + $query = $this->_build_select(); + $caching_enabled = self::$_config[$this->_connection_name]['caching']; + + if ($caching_enabled) { + $cache_key = self::_create_cache_key($query, $this->_values, $this->_table_name, $this->_connection_name); + $cached_result = self::_check_query_cache($cache_key, $this->_table_name, $this->_connection_name); + + if ($cached_result !== false) { + $this->_reset_idiorm_state(); + return $cached_result; + } + } + + self::_execute($query, $this->_values, $this->_connection_name); + $statement = self::get_last_statement(); + + $rows = array(); + while ($row = $statement->fetch(PDO::FETCH_ASSOC)) { + $rows[] = $row; + } + + if ($caching_enabled) { + self::_cache_query_result($cache_key, $rows, $this->_table_name, $this->_connection_name); + } + + $this->_reset_idiorm_state(); + return $rows; + } + + /** + * Reset the Idiorm instance state + */ + private function _reset_idiorm_state() { + $this->_values = array(); + $this->_result_columns = array('*'); + $this->_using_default_result_columns = true; + } + + /** + * Return the raw data wrapped by this ORM + * instance as an associative array. Column + * names may optionally be supplied as arguments, + * if so, only those keys will be returned. + */ + public function as_array() { + if (func_num_args() === 0) { + return $this->_data; + } + $args = func_get_args(); + return array_intersect_key($this->_data, array_flip($args)); + } + + /** + * Return the value of a property of this object (database row) + * or null if not present. + * + * If a column-names array is passed, it will return a associative array + * with the value of each column or null if it is not present. + */ + public function get($key) { + if (is_array($key)) { + $result = array(); + foreach($key as $column) { + $result[$column] = isset($this->_data[$column]) ? $this->_data[$column] : null; + } + return $result; + } else { + return isset($this->_data[$key]) ? $this->_data[$key] : null; + } + } + + /** + * Return the name of the column in the database table which contains + * the primary key ID of the row. + */ + protected function _get_id_column_name() { + if (!is_null($this->_instance_id_column)) { + return $this->_instance_id_column; + } + if (isset(self::$_config[$this->_connection_name]['id_column_overrides'][$this->_table_name])) { + return self::$_config[$this->_connection_name]['id_column_overrides'][$this->_table_name]; + } + return self::$_config[$this->_connection_name]['id_column']; + } + + /** + * Get the primary key ID of this object. + */ + public function id($disallow_null = false) { + $id = $this->get($this->_get_id_column_name()); + + if ($disallow_null) { + if (is_array($id)) { + foreach ($id as $id_part) { + if ($id_part === null) { + throw new Exception('Primary key ID contains null value(s)'); + } + } + } else if ($id === null) { + throw new Exception('Primary key ID missing from row or is null'); + } + } + + return $id; + } + + /** + * Set a property to a particular value on this object. + * To set multiple properties at once, pass an associative array + * as the first parameter and leave out the second parameter. + * Flags the properties as 'dirty' so they will be saved to the + * database when save() is called. + */ + public function set($key, $value = null) { + return $this->_set_orm_property($key, $value); + } + + /** + * Set a property to a particular value on this object. + * To set multiple properties at once, pass an associative array + * as the first parameter and leave out the second parameter. + * Flags the properties as 'dirty' so they will be saved to the + * database when save() is called. + * @param string|array $key + * @param string|null $value + */ + public function set_expr($key, $value = null) { + return $this->_set_orm_property($key, $value, true); + } + + /** + * Set a property on the ORM object. + * @param string|array $key + * @param string|null $value + * @param bool $raw Whether this value should be treated as raw or not + */ + protected function _set_orm_property($key, $value = null, $expr = false) { + if (!is_array($key)) { + $key = array($key => $value); + } + foreach ($key as $field => $value) { + $this->_data[$field] = $value; + $this->_dirty_fields[$field] = $value; + if (false === $expr and isset($this->_expr_fields[$field])) { + unset($this->_expr_fields[$field]); + } else if (true === $expr) { + $this->_expr_fields[$field] = true; + } + } + return $this; + } + + /** + * Check whether the given field has been changed since this + * object was saved. + */ + public function is_dirty($key) { + return array_key_exists($key, $this->_dirty_fields); + } + + /** + * Check whether the model was the result of a call to create() or not + * @return bool + */ + public function is_new() { + return $this->_is_new; + } + + /** + * Save any fields which have been modified on this object + * to the database. + */ + public function save() { + $query = array(); + + // remove any expression fields as they are already baked into the query + $values = array_values(array_diff_key($this->_dirty_fields, $this->_expr_fields)); + + if (!$this->_is_new) { // UPDATE + // If there are no dirty values, do nothing + if (empty($values) && empty($this->_expr_fields)) { + return true; + } + $query = $this->_build_update(); + $id = $this->id(true); + if (is_array($id)) { + $values = array_merge($values, array_values($id)); + } else { + $values[] = $id; + } + } else { // INSERT + $query = $this->_build_insert(); + } + + $success = self::_execute($query, $values, $this->_connection_name); + $caching_auto_clear_enabled = self::$_config[$this->_connection_name]['caching_auto_clear']; + if($caching_auto_clear_enabled){ + self::clear_cache($this->_table_name, $this->_connection_name); + } + // If we've just inserted a new record, set the ID of this object + if ($this->_is_new) { + $this->_is_new = false; + if ($this->count_null_id_columns() != 0) { + $db = self::get_db($this->_connection_name); + if($db->getAttribute(PDO::ATTR_DRIVER_NAME) == 'pgsql') { + // it may return several columns if a compound primary + // key is used + $row = self::get_last_statement()->fetch(PDO::FETCH_ASSOC); + foreach($row as $key => $value) { + $this->_data[$key] = $value; + } + } else { + $column = $this->_get_id_column_name(); + // if the primary key is compound, assign the last inserted id + // to the first column + if (is_array($column)) { + $column = reset($column); + } + $this->_data[$column] = $db->lastInsertId(); + } + } + } + + $this->_dirty_fields = $this->_expr_fields = array(); + return $success; + } + + /** + * Add a WHERE clause for every column that belongs to the primary key + */ + public function _add_id_column_conditions(&$query) { + $query[] = "WHERE"; + $keys = is_array($this->_get_id_column_name()) ? $this->_get_id_column_name() : array( $this->_get_id_column_name() ); + $first = true; + foreach($keys as $key) { + if ($first) { + $first = false; + } + else { + $query[] = "AND"; + } + $query[] = $this->_quote_identifier($key); + $query[] = "= ?"; + } + } + + /** + * Build an UPDATE query + */ + protected function _build_update() { + $query = array(); + $query[] = "UPDATE {$this->_quote_identifier($this->_table_name)} SET"; + + $field_list = array(); + foreach ($this->_dirty_fields as $key => $value) { + if(!array_key_exists($key, $this->_expr_fields)) { + $value = '?'; + } + $field_list[] = "{$this->_quote_identifier($key)} = $value"; + } + $query[] = join(", ", $field_list); + $this->_add_id_column_conditions($query); + return join(" ", $query); + } + + /** + * Build an INSERT query + */ + protected function _build_insert() { + $query[] = "INSERT INTO"; + $query[] = $this->_quote_identifier($this->_table_name); + $field_list = array_map(array($this, '_quote_identifier'), array_keys($this->_dirty_fields)); + $query[] = "(" . join(", ", $field_list) . ")"; + $query[] = "VALUES"; + + $placeholders = $this->_create_placeholders($this->_dirty_fields); + $query[] = "({$placeholders})"; + + if (self::get_db($this->_connection_name)->getAttribute(PDO::ATTR_DRIVER_NAME) == 'pgsql') { + $query[] = 'RETURNING ' . $this->_quote_identifier($this->_get_id_column_name()); + } + + return join(" ", $query); + } + + /** + * Delete this record from the database + */ + public function delete() { + $query = array( + "DELETE FROM", + $this->_quote_identifier($this->_table_name) + ); + $this->_add_id_column_conditions($query); + return self::_execute(join(" ", $query), is_array($this->id(true)) ? array_values($this->id(true)) : array($this->id(true)), $this->_connection_name); + } + + /** + * Delete many records from the database + */ + public function delete_many() { + // Build and return the full DELETE statement by concatenating + // the results of calling each separate builder method. + $query = $this->_join_if_not_empty(" ", array( + "DELETE FROM", + $this->_quote_identifier($this->_table_name), + $this->_build_where(), + )); + + return self::_execute($query, $this->_values, $this->_connection_name); + } + + // --------------------- // + // --- ArrayAccess --- // + // --------------------- // + + public function offsetExists($key) { + return array_key_exists($key, $this->_data); + } + + public function offsetGet($key) { + return $this->get($key); + } + + public function offsetSet($key, $value) { + if(is_null($key)) { + throw new InvalidArgumentException('You must specify a key/array index.'); + } + $this->set($key, $value); + } + + public function offsetUnset($key) { + unset($this->_data[$key]); + unset($this->_dirty_fields[$key]); + } + + // --------------------- // + // --- MAGIC METHODS --- // + // --------------------- // + public function __get($key) { + return $this->offsetGet($key); + } + + public function __set($key, $value) { + $this->offsetSet($key, $value); + } + + public function __unset($key) { + $this->offsetUnset($key); + } + + + public function __isset($key) { + return $this->offsetExists($key); + } + + /** + * Magic method to capture calls to undefined class methods. + * In this case we are attempting to convert camel case formatted + * methods into underscore formatted methods. + * + * This allows us to call ORM methods using camel case and remain + * backwards compatible. + * + * @param string $name + * @param array $arguments + * @return ORM + */ + public function __call($name, $arguments) + { + $method = strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $name)); + + if (method_exists($this, $method)) { + return call_user_func_array(array($this, $method), $arguments); + } else { + throw new IdiormMethodMissingException("Method $name() does not exist in class " . get_class($this)); + } + } + + /** + * Magic method to capture calls to undefined static class methods. + * In this case we are attempting to convert camel case formatted + * methods into underscore formatted methods. + * + * This allows us to call ORM methods using camel case and remain + * backwards compatible. + * + * @param string $name + * @param array $arguments + * @return ORM + */ + public static function __callStatic($name, $arguments) + { + $method = strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $name)); + + return call_user_func_array(array('ORM', $method), $arguments); + } + } + + /** + * A class to handle str_replace operations that involve quoted strings + * @example IdiormString::str_replace_outside_quotes('?', '%s', 'columnA = "Hello?" AND columnB = ?'); + * @example IdiormString::value('columnA = "Hello?" AND columnB = ?')->replace_outside_quotes('?', '%s'); + * @author Jeff Roberson + * @author Simon Holywell + * @link http://stackoverflow.com/a/13370709/461813 StackOverflow answer + */ + class IdiormString { + protected $subject; + protected $search; + protected $replace; + + /** + * Get an easy to use instance of the class + * @param string $subject + * @return \self + */ + public static function value($subject) { + return new self($subject); + } + + /** + * Shortcut method: Replace all occurrences of the search string with the replacement + * string where they appear outside quotes. + * @param string $search + * @param string $replace + * @param string $subject + * @return string + */ + public static function str_replace_outside_quotes($search, $replace, $subject) { + return self::value($subject)->replace_outside_quotes($search, $replace); + } + + /** + * Set the base string object + * @param string $subject + */ + public function __construct($subject) { + $this->subject = (string) $subject; + } + + /** + * Replace all occurrences of the search string with the replacement + * string where they appear outside quotes + * @param string $search + * @param string $replace + * @return string + */ + public function replace_outside_quotes($search, $replace) { + $this->search = $search; + $this->replace = $replace; + return $this->_str_replace_outside_quotes(); + } + + /** + * Validate an input string and perform a replace on all ocurrences + * of $this->search with $this->replace + * @author Jeff Roberson + * @link http://stackoverflow.com/a/13370709/461813 StackOverflow answer + * @return string + */ + protected function _str_replace_outside_quotes(){ + $re_valid = '/ + # Validate string having embedded quoted substrings. + ^ # Anchor to start of string. + (?: # Zero or more string chunks. + "[^"\\\\]*(?:\\\\.[^"\\\\]*)*" # Either a double quoted chunk, + | \'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\' # or a single quoted chunk, + | [^\'"\\\\]+ # or an unquoted chunk (no escapes). + )* # Zero or more string chunks. + \z # Anchor to end of string. + /sx'; + if (!preg_match($re_valid, $this->subject)) { + throw new IdiormStringException("Subject string is not valid in the replace_outside_quotes context."); + } + $re_parse = '/ + # Match one chunk of a valid string having embedded quoted substrings. + ( # Either $1: Quoted chunk. + "[^"\\\\]*(?:\\\\.[^"\\\\]*)*" # Either a double quoted chunk, + | \'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\' # or a single quoted chunk. + ) # End $1: Quoted chunk. + | ([^\'"\\\\]+) # or $2: an unquoted chunk (no escapes). + /sx'; + return preg_replace_callback($re_parse, array($this, '_str_replace_outside_quotes_cb'), $this->subject); + } + + /** + * Process each matching chunk from preg_replace_callback replacing + * each occurrence of $this->search with $this->replace + * @author Jeff Roberson + * @link http://stackoverflow.com/a/13370709/461813 StackOverflow answer + * @param array $matches + * @return string + */ + protected function _str_replace_outside_quotes_cb($matches) { + // Return quoted string chunks (in group $1) unaltered. + if ($matches[1]) return $matches[1]; + // Process only unquoted chunks (in group $2). + return preg_replace('/'. preg_quote($this->search, '/') .'/', + $this->replace, $matches[2]); + } + } + + /** + * A result set class for working with collections of model instances + * @author Simon Holywell + * @method null setResults(array $results) + * @method array getResults() + */ + class IdiormResultSet implements Countable, IteratorAggregate, ArrayAccess, Serializable { + /** + * The current result set as an array + * @var array + */ + protected $_results = array(); + + /** + * Optionally set the contents of the result set by passing in array + * @param array $results + */ + public function __construct(array $results = array()) { + $this->set_results($results); + } + + /** + * Set the contents of the result set by passing in array + * @param array $results + */ + public function set_results(array $results) { + $this->_results = $results; + } + + /** + * Get the current result set as an array + * @return array + */ + public function get_results() { + return $this->_results; + } + + /** + * Get the current result set as an array + * @return array + */ + public function as_array() { + return $this->get_results(); + } + + /** + * Get the number of records in the result set + * @return int + */ + public function count() { + return count($this->_results); + } + + /** + * Get an iterator for this object. In this case it supports foreaching + * over the result set. + * @return \ArrayIterator + */ + public function getIterator() { + return new ArrayIterator($this->_results); + } + + /** + * ArrayAccess + * @param int|string $offset + * @return bool + */ + public function offsetExists($offset) { + return isset($this->_results[$offset]); + } + + /** + * ArrayAccess + * @param int|string $offset + * @return mixed + */ + public function offsetGet($offset) { + return $this->_results[$offset]; + } + + /** + * ArrayAccess + * @param int|string $offset + * @param mixed $value + */ + public function offsetSet($offset, $value) { + $this->_results[$offset] = $value; + } + + /** + * ArrayAccess + * @param int|string $offset + */ + public function offsetUnset($offset) { + unset($this->_results[$offset]); + } + + /** + * Serializable + * @return string + */ + public function serialize() { + return serialize($this->_results); + } + + /** + * Serializable + * @param string $serialized + * @return array + */ + public function unserialize($serialized) { + return unserialize($serialized); + } + + /** + * Call a method on all models in a result set. This allows for method + * chaining such as setting a property on all models in a result set or + * any other batch operation across models. + * @example ORM::for_table('Widget')->find_many()->set('field', 'value')->save(); + * @param string $method + * @param array $params + * @return \IdiormResultSet + */ + public function __call($method, $params = array()) { + foreach($this->_results as $model) { + if (method_exists($model, $method)) { + call_user_func_array(array($model, $method), $params); + } else { + throw new IdiormMethodMissingException("Method $method() does not exist in class " . get_class($this)); + } + } + return $this; + } + } + + /** + * A placeholder for exceptions eminating from the IdiormString class + */ + class IdiormStringException extends Exception {} + + class IdiormMethodMissingException extends Exception {}