diff options
author | Bharat Mediratta <bharat@menalto.com> | 2009-11-26 21:14:54 -0800 |
---|---|---|
committer | Bharat Mediratta <bharat@menalto.com> | 2009-11-26 21:14:54 -0800 |
commit | 96b00d6cfe437e376d5547a10aa8d1cf7def8c13 (patch) | |
tree | 355f3df10c86f595e1373a53a0746b877b27489b | |
parent | b9a0e0963746420d82ad3107135aa7a9256825be (diff) |
Convert some more Database::instance() calls to db::build() form.
-rw-r--r-- | modules/comment/helpers/comment_event.php | 5 | ||||
-rw-r--r-- | modules/gallery/helpers/item.php | 4 | ||||
-rw-r--r-- | modules/gallery/libraries/ORM_MPTT.php | 90 | ||||
-rw-r--r-- | modules/gallery/models/item.php | 6 | ||||
-rw-r--r-- | modules/notification/helpers/notification.php | 4 | ||||
-rw-r--r-- | modules/tag/helpers/tag.php | 4 |
6 files changed, 69 insertions, 44 deletions
diff --git a/modules/comment/helpers/comment_event.php b/modules/comment/helpers/comment_event.php index cf5d0a60..c90f7663 100644 --- a/modules/comment/helpers/comment_event.php +++ b/modules/comment/helpers/comment_event.php @@ -62,12 +62,11 @@ class comment_event_Core { } static function item_index_data($item, $data) { - foreach (Database::instance() + foreach (db::build() ->select("text") ->from("comments") ->where("item_id", "=", $item->id) - ->get() - ->as_array() as $row) { + ->execute() as $row) { $data[] = $row->text; } } diff --git a/modules/gallery/helpers/item.php b/modules/gallery/helpers/item.php index 7496d368..c3126435 100644 --- a/modules/gallery/helpers/item.php +++ b/modules/gallery/helpers/item.php @@ -140,10 +140,10 @@ class item_Core { // Guard against an empty result when we create the first item. It's unfortunate that we // have to check this every time. // @todo: figure out a better way to bootstrap the weight. - $result = Database::instance() + $result = db::build() ->select("weight")->from("items") ->order_by("weight", "desc")->limit(1) - ->get()->current(); + ->execute()->current(); return ($result ? $result->weight : 0) + 1; } diff --git a/modules/gallery/libraries/ORM_MPTT.php b/modules/gallery/libraries/ORM_MPTT.php index a67f05be..0ec0a848 100644 --- a/modules/gallery/libraries/ORM_MPTT.php +++ b/modules/gallery/libraries/ORM_MPTT.php @@ -51,10 +51,16 @@ class ORM_MPTT_Core extends ORM { try { // Make a hole in the parent for this new item - $this->db->query( - "UPDATE {{$this->table_name}} SET `left_ptr` = `left_ptr` + 2 WHERE `left_ptr` >= {$parent->right_ptr}"); - $this->db->query( - "UPDATE {{$this->table_name}} SET `right_ptr` = `right_ptr` + 2 WHERE `right_ptr` >= {$parent->right_ptr}"); + $this->db_builder + ->update($this->table_name) + ->set("left_ptr", new Database_Expression("`left_ptr` + 2")) + ->where("left_ptr", ">=", $parent->right_ptr) + ->execute(); + $this->db_builder + ->update($this->table_name) + ->set("right_ptr", new Database_Expression("`right_ptr` + 2")) + ->where("right_ptr", ">=", $parent->right_ptr) + ->execute(); $parent->right_ptr += 2; // Insert this item into the hole @@ -92,10 +98,16 @@ class ORM_MPTT_Core extends ORM { $this->lock(); try { - $this->db->query( - "UPDATE {{$this->table_name}} SET `left_ptr` = `left_ptr` - 2 WHERE `left_ptr` > {$this->right_ptr}"); - $this->db->query( - "UPDATE {{$this->table_name}} SET `right_ptr` = `right_ptr` - 2 WHERE `right_ptr` > {$this->right_ptr}"); + $this->db_builder + ->update($this->table_name) + ->set("left_ptr", new Database_Expression("`left_ptr` - 2")) + ->where("left_ptr", ">", $this->right_ptr) + ->execute(); + $this->db_builder + ->update($this->table_name) + ->set("right_ptr", new Database_Expression("`right_ptr` - 2")) + ->where("right_ptr", ">", $this->right_ptr) + ->execute(); } catch (Exception $e) { $this->unlock(); throw $e; @@ -239,23 +251,32 @@ class ORM_MPTT_Core extends ORM { try { if ($level_delta) { // Update the levels for the to-be-moved items - $this->db->query( - "UPDATE {{$this->table_name}} SET `level` = `level` + $level_delta" . - " WHERE `left_ptr` >= $original_left_ptr AND `right_ptr` <= $original_right_ptr"); + $this->db_builder + ->update($this->table_name) + ->set("level", new Database_Expression("`level` + $level_delta")) + ->where("left_ptr", ">=", $original_left_ptr) + ->where("right_ptr", "<=", $original_right_ptr) + ->execute(); } // Make a hole in the target for the move - $target->db->query( - "UPDATE {{$this->table_name}} SET `left_ptr` = `left_ptr` + $size_of_hole" . - " WHERE `left_ptr` >= $target_right_ptr"); - $target->db->query( - "UPDATE {{$this->table_name}} SET `right_ptr` = `right_ptr` + $size_of_hole" . - " WHERE `right_ptr` >= $target_right_ptr"); + $target->db_builder + ->update($this->table_name) + ->set("left_ptr", new Database_Expression("`left_ptr` + $size_of_hole")) + ->where("left_ptr", ">=", $target_right_ptr) + ->execute(); + $target->db_builder + ->update($this->table_name) + ->set("right_ptr", new Database_Expression("`right_ptr` + $size_of_hole")) + ->where("right_ptr", ">=", $target_right_ptr) + ->execute(); // Change the parent. - $this->db->query( - "UPDATE {{$this->table_name}} SET `parent_id` = {$target->id}" . - " WHERE `id` = {$this->id}"); + $this->db_builder + ->update($this->table_name) + ->set("parent_id", $target->id) + ->where("id", "=", $this->id) + ->execute(); // If the source is to the right of the target then we just adjusted its left_ptr and right_ptr above. $left_ptr = $original_left_ptr; @@ -266,20 +287,25 @@ class ORM_MPTT_Core extends ORM { } $new_offset = $target->right_ptr - $left_ptr; - $this->db->query( - "UPDATE {{$this->table_name}}" . - " SET `left_ptr` = `left_ptr` + $new_offset," . - " `right_ptr` = `right_ptr` + $new_offset" . - " WHERE `left_ptr` >= $left_ptr" . - " AND `right_ptr` <= $right_ptr"); + $this->db_builder + ->update($this->table_name) + ->set("left_ptr", new Database_Expression("`left_ptr` + $new_offset")) + ->set("right_ptr", new Database_Expression("`right_ptr` + $new_offset")) + ->where("left_ptr", ">=", $left_ptr) + ->where("right_ptr", "<=", $right_ptr) + ->execute(); // Close the hole in the source's parent after the move - $this->db->query( - "UPDATE {{$this->table_name}} SET `left_ptr` = `left_ptr` - $size_of_hole" . - " WHERE `left_ptr` > $right_ptr"); - $this->db->query( - "UPDATE {{$this->table_name}} SET `right_ptr` = `right_ptr` - $size_of_hole" . - " WHERE `right_ptr` > $right_ptr"); + $this->db_builder + ->update($this->table_name) + ->set("left_ptr", new Database_Expression("`left_ptr` - $size_of_hole")) + ->where("left_ptr", ">", $right_ptr) + ->execute(); + $this->db_builder + ->update($this->table_name) + ->set("right_ptr", new Database_Expression("`right_ptr` - $size_of_hole")) + ->where("right_ptr", ">", $right_ptr) + ->execute(); } catch (Exception $e) { $this->unlock(); throw $e; diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php index 16c57dbc..8a42cc1e 100644 --- a/modules/gallery/models/item.php +++ b/modules/gallery/models/item.php @@ -285,14 +285,14 @@ class Item_Model extends ORM_MPTT { private function _build_relative_caches() { $names = array(); $slugs = array(); - foreach (Database::instance() + foreach (db::build() ->select(array("name", "slug")) ->from("items") ->where("left_ptr", "<=", $this->left_ptr) ->where("right_ptr", ">=", $this->right_ptr) ->where("id", "<>", 1) ->order_by("left_ptr", "ASC") - ->get() as $row) { + ->execute() as $row) { // Don't encode the names segment $names[] = rawurlencode($row->name); $slugs[] = rawurlencode($row->slug); @@ -489,7 +489,7 @@ class Item_Model extends ORM_MPTT { ->where("parent_id", "=", $this->id) ->merge_where($where) ->order_by($order_by) - ->get() as $row) { + ->execute() as $row) { $position++; if ($row->id == $child->id) { break; diff --git a/modules/notification/helpers/notification.php b/modules/notification/helpers/notification.php index 7e935614..31a56c1f 100644 --- a/modules/notification/helpers/notification.php +++ b/modules/notification/helpers/notification.php @@ -170,10 +170,10 @@ class notification { } static function send_pending_notifications() { - foreach (Database::instance() + foreach (db::build() ->select("DISTINCT email") ->from("pending_notifications") - ->get() as $row) { + ->execute() as $row) { $email = $row->email; $result = ORM::factory("pending_notification") ->where("email", "=", $email) diff --git a/modules/tag/helpers/tag.php b/modules/tag/helpers/tag.php index c4c4ba15..3e8a0d20 100644 --- a/modules/tag/helpers/tag.php +++ b/modules/tag/helpers/tag.php @@ -89,12 +89,12 @@ class tag_Core { */ static function item_tags($item) { $tags = array(); - foreach (Database::instance() + foreach (db::build() ->select("name") ->from("tags") ->join("items_tags", "tags.id", "items_tags.tag_id", "left") ->where("items_tags.item_id", "=", $item->id) - ->get() as $row) { + ->execute() as $row) { $tags[] = $row->name; } return $tags; |