Permalink
Browse files

Convert some more Database::instance() calls to db::build() form.

  • Loading branch information...
1 parent b9a0e09 commit 96b00d6cfe437e376d5547a10aa8d1cf7def8c13 @bharat bharat committed Nov 27, 2009
@@ -62,12 +62,11 @@ static function photo_menu($menu, $theme) {
}
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;
}
}
@@ -140,10 +140,10 @@ static function get_max_weight() {
// 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;
}
@@ -51,10 +51,16 @@ function add_to_parent($parent) {
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 @@ public function delete() {
$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 @@ function move_to($target) {
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 @@ function move_to($target) {
}
$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;
@@ -285,14 +285,14 @@ public function resize_url($full_uri=false) {
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 @@ public function get_position($child, $where=array()) {
->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;
@@ -170,10 +170,10 @@ static function send_comment_published($comment) {
}
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)
@@ -89,12 +89,12 @@ static function sort_by_name($tag1, $tag2) {
*/
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;

0 comments on commit 96b00d6

Please sign in to comment.