From 54be15191b983e72b4643f3559303b35b7c48795 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Thu, 26 Nov 2009 18:47:40 -0800 Subject: Overload Database_Builder to add merge_where() which takes predefined where clauses and adds them to the existing query. Update all existing queries that take an additional where clause to use it. --- .../libraries/MY_Database_Builder.php | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 modules/kohana23_compat/libraries/MY_Database_Builder.php (limited to 'modules/kohana23_compat/libraries/MY_Database_Builder.php') diff --git a/modules/kohana23_compat/libraries/MY_Database_Builder.php b/modules/kohana23_compat/libraries/MY_Database_Builder.php new file mode 100644 index 00000000..974f9c6d --- /dev/null +++ b/modules/kohana23_compat/libraries/MY_Database_Builder.php @@ -0,0 +1,31 @@ +where($tuple[0], $tuple[1], $tuple[2]); + } + return $this; + } +} \ No newline at end of file -- cgit v1.2.3 From cc4d7c672c863185bab5d654222e205d6517e98a Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 21 Dec 2009 15:40:28 -0800 Subject: Update database tests for K24. Use a mock database that we load through the framework so that we're properly testing the Database_Builder, it's a lot cleaner than what we had before. --- modules/gallery/tests/Database_Test.php | 105 +++++++++++++-------- .../libraries/MY_Database_Builder.php | 4 + 2 files changed, 72 insertions(+), 37 deletions(-) (limited to 'modules/kohana23_compat/libraries/MY_Database_Builder.php') diff --git a/modules/gallery/tests/Database_Test.php b/modules/gallery/tests/Database_Test.php index 4259961e..6aa186e5 100644 --- a/modules/gallery/tests/Database_Test.php +++ b/modules/gallery/tests/Database_Test.php @@ -18,17 +18,27 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class Database_Test extends Unit_Test_Case { + function setup() { + $config = Kohana_Config::instance(); + $config->set("database.mock.connection.type", "mock"); + $config->set("database.mock.cache", false); + $config->set("database.mock.table_prefix", "g_"); + } + function simple_where_test() { - $sql = Database_Builder_For_Test::instance() + $sql = db::build("mock") + ->select("some_column") + ->from("some_table") ->where("a", "=", 1) ->where("b", "=", 2) ->compile(); $sql = str_replace("\n", " ", $sql); - $this->assert_same("SELECT * WHERE `a` = 1 AND `b` = 2", $sql); + $this->assert_same("SELECT [some_column] FROM [some_table] WHERE [a] = [1] AND [b] = [2]", $sql); } function compound_where_test() { - $sql = Database_Builder_For_Test::instance() + $sql = db::build("mock") + ->select() ->where("outer1", "=", 1) ->and_open() ->where("inner1", "=", 1) @@ -38,12 +48,13 @@ class Database_Test extends Unit_Test_Case { ->compile(); $sql = str_replace("\n", " ", $sql); $this->assert_same( - "SELECT * WHERE `outer1` = 1 AND (`inner1` = 1 OR `inner2` = 2) AND `outer2` = 2", + "SELECT [*] WHERE [outer1] = [1] AND ([inner1] = [1] OR [inner2] = [2]) AND [outer2] = [2]", $sql); } function group_first_test() { - $sql = Database_Builder_For_Test::instance() + $sql = db::build("mock") + ->select() ->and_open() ->where("inner1", "=", 1) ->or_where("inner2", "=", 2) @@ -53,12 +64,13 @@ class Database_Test extends Unit_Test_Case { ->compile(); $sql = str_replace("\n", " ", $sql); $this->assert_same( - "SELECT * WHERE (`inner1` = 1 OR `inner2` = 2) AND `outer1` = 1 AND `outer2` = 2", + "SELECT [*] WHERE ([inner1] = [1] OR [inner2] = [2]) AND [outer1] = [1] AND [outer2] = [2]", $sql); } function where_array_test() { - $sql = Database_Builder_For_Test::instance() + $sql = db::build("mock") + ->select() ->where("outer1", "=", 1) ->and_open() ->where("inner1", "=", 1) @@ -68,32 +80,33 @@ class Database_Test extends Unit_Test_Case { ->compile(); $sql = str_replace("\n", " ", $sql); $this->assert_same( - "SELECT * WHERE `outer1` = 1 AND (`inner1` = 1 OR `inner2` = 2 OR `inner3` = 3)", + "SELECT [*] WHERE [outer1] = [1] AND ([inner1] = [1] OR [inner2] = [2] OR [inner3] = [3])", $sql); } function notlike_test() { - $sql = Database_Builder_For_Test::instance() + $sql = db::build("mock") + ->select() ->where("outer1", "=", 1) ->or_open() - ->where("inner1", "NOT LIKE", 1) + ->where("inner1", "NOT LIKE", "%1%") ->close() ->compile(); $sql = str_replace("\n", " ", $sql); $this->assert_same( - "SELECT * WHERE `outer1` = 1 OR ( `inner1` NOT LIKE '%1%')", + "SELECT [*] WHERE [outer1] = [1] OR ([inner1] NOT LIKE [%1%])", $sql); } function prefix_replacement_test() { - $db = Database::instance(); - $converted = $db->add_table_prefixes("CREATE TABLE IF NOT EXISTS {test_tables} ( + $db = Database::instance("mock"); + $converted = $db->add_table_prefixes("CREATE TABLE IF NOT EXISTS {test} ( `id` int(9) NOT NULL auto_increment, `name` varchar(32) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY(`name`)) ENGINE=InnoDB DEFAULT CHARSET=utf8"); - $expected = "CREATE TABLE IF NOT EXISTS g3test_test_tables ( + $expected = "CREATE TABLE IF NOT EXISTS g_test ( `id` int(9) NOT NULL auto_increment, `name` varchar(32) NOT NULL, PRIMARY KEY (`id`), @@ -101,16 +114,16 @@ class Database_Test extends Unit_Test_Case { ENGINE=InnoDB DEFAULT CHARSET=utf8"; $this->assert_same($expected, $converted); - $sql = "UPDATE {test_tables} SET `name` = '{test string}' " . + $sql = "UPDATE {test} SET `name` = '{test string}' " . "WHERE `item_id` IN " . - " (SELECT `id` FROM {items} " . + " (SELECT `id` FROM {test} " . " WHERE `left_ptr` >= 1 " . " AND `right_ptr` <= 6)"; $sql = $db->add_table_prefixes($sql); - $expected = "UPDATE g3test_test_tables SET `name` = '{test string}' " . + $expected = "UPDATE g_test SET `name` = '{test string}' " . "WHERE `item_id` IN " . - " (SELECT `id` FROM g3test_items " . + " (SELECT `id` FROM g_test " . " WHERE `left_ptr` >= 1 " . " AND `right_ptr` <= 6)"; @@ -118,34 +131,52 @@ class Database_Test extends Unit_Test_Case { } function prefix_no_replacement_test() { - $update = Database_Builder_For_Test::instance() + $sql = db::build("mock") ->from("test_tables") ->where("1", "=", "1") ->set(array("name" => "Test Name")) - ->update(); + ->update() + ->compile(); + $sql = str_replace("\n", " ", $sql); + $this->assert_same("UPDATE [test_tables] SET [name] = [Test Name] WHERE [1] = [1]", $sql); + } +} - $expected = "UPDATE `g3test_test_tables` SET `name` = 'Test Name' WHERE 1 = 1"; +class Database_Mock extends Database { + public function connect() { + } - $this->assert_same($expected, $update); + public function disconnect() { } -} -class Database_Builder_For_Test extends Database_Builder { - static function instance() { - $db = new Database_Builder_For_Test(); - $db->_table_names["{items}"] = "g3test_items"; - $db->config["table_prefix"] = "g3test_"; - return $db; + public function set_charset($charset) { } - public function query($sql = '') { - if (!empty($sql)) { - $sql = $this->add_table_prefixes($sql); - } - return $sql; + public function query_execute($sql) { } - public function compile() { - return parent::compile(); + public function escape($val) { } -} + + public function list_constraints($table) { + } + + public function list_fields($table) { + } + + public function list_tables() { + return array("test"); + } + + public function quote_column($val) { + return "[$val]"; + } + + public function quote_table($val) { + return "[$val]"; + } + + public function quote($val) { + return "[$val]"; + } +} \ No newline at end of file diff --git a/modules/kohana23_compat/libraries/MY_Database_Builder.php b/modules/kohana23_compat/libraries/MY_Database_Builder.php index 974f9c6d..54a10860 100644 --- a/modules/kohana23_compat/libraries/MY_Database_Builder.php +++ b/modules/kohana23_compat/libraries/MY_Database_Builder.php @@ -28,4 +28,8 @@ class Database_Builder extends Database_Builder_Core { } return $this; } + + public function compile() { + return parent::compile(); + } } \ No newline at end of file -- cgit v1.2.3 From 0650109d4bb5442cd77fcda61745dab67a632836 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Tue, 22 Dec 2009 13:50:52 -0800 Subject: Add merge_or_where() to MY_Datatabase_Builder and use that instead of or_where() for compatibility and convenience. Caught by failing unit tests. --- modules/gallery/helpers/item.php | 4 ++-- modules/kohana23_compat/libraries/MY_Database_Builder.php | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) (limited to 'modules/kohana23_compat/libraries/MY_Database_Builder.php') diff --git a/modules/gallery/helpers/item.php b/modules/gallery/helpers/item.php index b7be23cd..f6181f8a 100644 --- a/modules/gallery/helpers/item.php +++ b/modules/gallery/helpers/item.php @@ -155,12 +155,12 @@ class item_Core { $view_restrictions = array(); if (!identity::active_user()->admin) { foreach (identity::group_ids_for_active_user() as $id) { - $view_restrictions["items.view_$id"] = access::ALLOW; + $view_restrictions[] = array("items.view_$id", "=", access::ALLOW); } } if (count($view_restrictions)) { - $model->and_open()->or_where($view_restrictions)->close(); + $model->and_open()->merge_or_where($view_restrictions)->close(); } return $model; diff --git a/modules/kohana23_compat/libraries/MY_Database_Builder.php b/modules/kohana23_compat/libraries/MY_Database_Builder.php index 54a10860..c82b6ac4 100644 --- a/modules/kohana23_compat/libraries/MY_Database_Builder.php +++ b/modules/kohana23_compat/libraries/MY_Database_Builder.php @@ -29,6 +29,17 @@ class Database_Builder extends Database_Builder_Core { return $this; } + /** + * Merge in a series of where clause tuples and call or_where() on each one. + * @chainable + */ + public function merge_or_where($tuples) { + foreach ($tuples as $tuple) { + $this->or_where($tuple[0], $tuple[1], $tuple[2]); + } + return $this; + } + public function compile() { return parent::compile(); } -- cgit v1.2.3