From c8429046ef9a42b94fe4321155f20c4f29809e60 Mon Sep 17 00:00:00 2001 From: Simon Holywell Date: Wed, 28 Aug 2013 16:33:59 +0100 Subject: [PATCH] Test and document the query logger callback --- README.markdown | 1 + docs/configuration.rst | 24 +++++++++ test/ConfigTest53.php | 109 +++++------------------------------------ 3 files changed, 36 insertions(+), 98 deletions(-) diff --git a/README.markdown b/README.markdown index 2437e300..c06b09c1 100644 --- a/README.markdown +++ b/README.markdown @@ -71,6 +71,7 @@ Changelog * 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) * 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) diff --git a/docs/configuration.rst b/docs/configuration.rst index 75d77f7e..a1c356a0 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -225,6 +225,30 @@ the log. ``ORM::get_last_query()`` returns the most recent query executed. ``ORM::get_query_log()`` returns an array of all queries executed. +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 + + 'widget_id', - 'widget_handle' => 'widget_handle_id', - )); - } - - protected function tearDownIdColumnOverrides() { - ORM::configure('id_column_overrides', array()); - } - - public function testSettingIdColumn() { - ORM::for_table('widget')->find_one(5); - $expected = "SELECT * FROM `widget` WHERE `primary_key` = '5' LIMIT 1"; - $this->assertEquals($expected, ORM::get_last_query()); - } - - public function testSettingIdColumnOverridesOne() { - $this->setUpIdColumnOverrides(); - - ORM::for_table('widget')->find_one(5); - $expected = "SELECT * FROM `widget` WHERE `widget_id` = '5' LIMIT 1"; - $this->assertEquals($expected, ORM::get_last_query()); - - $this->tearDownIdColumnOverrides(); - } - - public function testSettingIdColumnOverridesTwo() { - $this->setUpIdColumnOverrides(); - - ORM::for_table('widget_handle')->find_one(5); - $expected = "SELECT * FROM `widget_handle` WHERE `widget_handle_id` = '5' LIMIT 1"; - $this->assertEquals($expected, ORM::get_last_query()); - - $this->tearDownIdColumnOverrides(); - } - - public function testSettingIdColumnOverridesThree() { - $this->setUpIdColumnOverrides(); + public function testLoggerCallback() { + ORM::configure('logger', function($log_string) { + return $log_string; + }); + $function = ORM::get_config('logger'); + $this->assertTrue(is_callable($function)); - ORM::for_table('widget_nozzle')->find_one(5); - $expected = "SELECT * FROM `widget_nozzle` WHERE `primary_key` = '5' LIMIT 1"; - $this->assertEquals($expected, ORM::get_last_query()); - - $this->tearDownIdColumnOverrides(); - } - - public function testInstanceIdColumnOne() { - $this->setUpIdColumnOverrides(); - - ORM::for_table('widget')->use_id_column('new_id')->find_one(5); - $expected = "SELECT * FROM `widget` WHERE `new_id` = '5' LIMIT 1"; - $this->assertEquals($expected, ORM::get_last_query()); - - $this->tearDownIdColumnOverrides(); - } - - public function testInstanceIdColumnTwo() { - $this->setUpIdColumnOverrides(); - - ORM::for_table('widget_handle')->use_id_column('new_id')->find_one(5); - $expected = "SELECT * FROM `widget_handle` WHERE `new_id` = '5' LIMIT 1"; - $this->assertEquals($expected, ORM::get_last_query()); - - $this->tearDownIdColumnOverrides(); - } - - public function testInstanceIdColumnThree() { - $this->setUpIdColumnOverrides(); - - ORM::for_table('widget_nozzle')->use_id_column('new_id')->find_one(5); - $expected = "SELECT * FROM `widget_nozzle` WHERE `new_id` = '5' LIMIT 1"; - $this->assertEquals($expected, ORM::get_last_query()); - - $this->tearDownIdColumnOverrides(); - } - - public function testGetConfig() { - $this->assertTrue(ORM::get_config('logging')); - ORM::configure('logging', false); - $this->assertFalse(ORM::get_config('logging')); - } + $log_string = "UPDATE `widget` SET `added` = NOW() WHERE `id` = '1'"; + $this->assertEquals($log_string, $function($log_string)); - public function testGetConfigArray() { - $expected = array( - 'connection_string' => 'sqlite::memory:', - 'id_column' => 'primary_key', - 'id_column_overrides' => array(), - 'error_mode' => PDO::ERRMODE_EXCEPTION, - 'username' => null, - 'password' => null, - 'driver_options' => null, - 'identifier_quote_character' => '`', - 'logging' => true, - 'caching' => false, - 'return_result_sets' => false, - ); - $this->assertEquals($expected, ORM::get_config()); + ORM::configure('logger', null); } -} +} \ No newline at end of file