diff options
author | Bharat Mediratta <bharat@menalto.com> | 2009-12-17 21:05:58 -0800 |
---|---|---|
committer | Bharat Mediratta <bharat@menalto.com> | 2009-12-17 21:05:58 -0800 |
commit | 9b75b85e71b47fc953773544c930f5cecf6b34a7 (patch) | |
tree | 0c0f369260a39481790d6b16531350e7034c287f | |
parent | 2aba8c4bffed5fb38689d5ed998c486ab6bde902 (diff) |
Update all database queries such that we can run "php index.php
package" and generate the same SQL as before.
-rw-r--r-- | modules/gallery/controllers/packager.php | 7 | ||||
-rw-r--r-- | modules/gallery/helpers/access.php | 81 |
2 files changed, 56 insertions, 32 deletions
diff --git a/modules/gallery/controllers/packager.php b/modules/gallery/controllers/packager.php index d27adf4d..4569e166 100644 --- a/modules/gallery/controllers/packager.php +++ b/modules/gallery/controllers/packager.php @@ -42,7 +42,7 @@ class Packager_Controller extends Controller { // Drop all tables foreach ($db->list_tables() as $table) { - $db->query("DROP TABLE IF EXISTS `$table`"); + $db->query("DROP TABLE IF EXISTS {{$table}}"); } // Clean out data @@ -88,8 +88,9 @@ class Packager_Controller extends Controller { $db->query("TRUNCATE {sessions}"); $db->query("TRUNCATE {logs}"); $db->query("DELETE FROM {vars} WHERE `module_name` = 'gallery' AND `name` = '_cache'"); - $db->update("users", array("password" => ""), array("id" => 1)); - $db->update("users", array("password" => ""), array("id" => 2)); + + db::build()->update("users", array("password" => ""), array("id" => 1))->execute(); + db::build()->update("users", array("password" => ""), array("id" => 2))->execute(); $dbconfig = Kohana::config('database.default'); $conn = $dbconfig["connection"]; diff --git a/modules/gallery/helpers/access.php b/modules/gallery/helpers/access.php index 0d197597..85410085 100644 --- a/modules/gallery/helpers/access.php +++ b/modules/gallery/helpers/access.php @@ -419,8 +419,8 @@ class access_Core { * @return ORM_Iterator */ private static function _get_all_groups() { - // When we build the gallery package, it's possible that there is no identity provider installed yet. - // This is ok at packaging time, so work around it. + // When we build the gallery package, it's possible that there is no identity provider + // installed yet. This is ok at packaging time, so work around it. if (module::is_active(module::get_var("gallery", "identity_provider", "user"))) { return identity::groups(); } else { @@ -436,11 +436,10 @@ class access_Core { * @return void */ private static function _drop_columns($perm_name, $group) { - $db = Database::instance(); $field = "{$perm_name}_{$group->id}"; $cache_table = $perm_name == "view" ? "items" : "access_caches"; - $db->query("ALTER TABLE {{$cache_table}} DROP `$field`"); - $db->query("ALTER TABLE {access_intents} DROP `$field`"); + Database::instance()->query("ALTER TABLE {{$cache_table}} DROP `$field`"); + Database::instance()->query("ALTER TABLE {access_intents} DROP `$field`"); model_cache::clear(); ORM::factory("access_intent")->clear_cache(); } @@ -453,13 +452,18 @@ class access_Core { * @return void */ private static function _add_columns($perm_name, $group) { - $db = Database::instance(); $field = "{$perm_name}_{$group->id}"; $cache_table = $perm_name == "view" ? "items" : "access_caches"; $not_null = $cache_table == "items" ? "" : "NOT NULL"; - $db->query("ALTER TABLE {{$cache_table}} ADD `$field` BINARY $not_null DEFAULT FALSE"); - $db->query("ALTER TABLE {access_intents} ADD `$field` BINARY DEFAULT NULL"); - $db->update("access_intents", array($field => self::DENY), array("item_id" => 1)); + Database::instance()->query( + "ALTER TABLE {{$cache_table}} ADD `$field` BINARY $not_null DEFAULT FALSE"); + Database::instance()->query( + "ALTER TABLE {access_intents} ADD `$field` BINARY DEFAULT NULL"); + db::build() + ->update("access_intents") + ->set($field, self::DENY) + ->where("item_id", "=", 1) + ->execute(); model_cache::clear(); ORM::factory("access_intent")->clear_cache(); } @@ -476,8 +480,6 @@ class access_Core { */ private static function _update_access_view_cache($group, $item) { $access = ORM::factory("access_intent")->where("item_id", "=", $item->id)->find(); - - $db = Database::instance(); $field = "view_{$group->id}"; // With view permissions, deny values in the parent can override allow values in the child, @@ -506,8 +508,12 @@ class access_Core { // access_caches table will already contain DENY values and we won't be able to overwrite // them according the rule above. So mark every permission below this level as UNKNOWN so // that we can tell which permissions have been changed, and which ones need to be updated. - $db->update("items", array($field => self::UNKNOWN), - array("left_ptr >=" => $item->left_ptr, "right_ptr <=" => $item->right_ptr)); + db::build() + ->update("items") + ->set($field, self::UNKNOWN) + ->where("left_ptr", ">=", $item->left_ptr) + ->where("right_ptr", "<=", $item->right_ptr) + ->execute(); $query = ORM::factory("access_intent") ->select(array("access_intents.$field", "items.left_ptr", "items.right_ptr", "items.id")) @@ -521,20 +527,34 @@ class access_Core { foreach ($query as $row) { if ($row->$field == self::ALLOW) { // Propagate ALLOW for any row that is still UNKNOWN. - $db->update("items", array($field => $row->$field), - array($field => self::UNKNOWN, "left_ptr >=" => $row->left_ptr, "right_ptr <=" => $row->right_ptr)); + db::build() + ->update("items") + ->set($field, $row->$field) + ->where($field, "IS", self::UNKNOWN) // UNKNOWN is NULL so we have to use IS + ->where("left_ptr", ">=", $row->left_ptr) + ->where("right_ptr", "<=", $row->right_ptr) + ->execute(); } else if ($row->$field == self::DENY) { // DENY overwrites everything below it - $db->update("items", array($field => $row->$field), - array("left_ptr >=" => $row->left_ptr, "right_ptr <=" => $row->right_ptr)); + db::build() + ->update("items") + ->set($field, $row->$field) + ->where("left_ptr", ">=", $row->left_ptr) + ->where("right_ptr", "<=", $row->right_ptr) + ->execute(); } } // Finally, if our intent is DEFAULT at this point it means that we were unable to find a // DENY parent in the hierarchy to propagate from. So we'll still have a UNKNOWN values in // the hierarchy, and all of those are safe to change to ALLOW. - $db->update("items", array($field => self::ALLOW), - array($field => self::UNKNOWN, "left_ptr >=" => $item->left_ptr, "right_ptr <=" => $item->right_ptr)); + db::build() + ->update("items") + ->set($field, self::ALLOW) + ->where($field, "IS", self::UNKNOWN) // UNKNOWN is NULL so we have to use IS + ->where("left_ptr", ">=", $item->left_ptr) + ->where("right_ptr", "<=", $item->right_ptr) + ->execute(); } /** @@ -551,7 +571,6 @@ class access_Core { private static function _update_access_non_view_cache($group, $perm_name, $item) { $access = ORM::factory("access_intent")->where("item_id", "=", $item->id)->find(); - $db = Database::instance(); $field = "{$perm_name}_{$group->id}"; // If the item's intent is DEFAULT, then we need to back up the chain to find the nearest @@ -564,7 +583,7 @@ class access_Core { ->join("access_intents", "items.id", "access_intents.item_id") ->where("left_ptr", "<", $item->left_ptr) ->where("right_ptr", ">", $item->right_ptr) - ->where($field, "IS NOT", self::UNKNOWN) + ->where($field, "IS NOT", self::UNKNOWN) // UNKNOWN is NULL so we have to use IS NOT ->order_by("left_ptr", "DESC") ->limit(1) ->find(); @@ -583,14 +602,18 @@ class access_Core { ->where($field, "IS NOT", self::INHERIT) ->order_by("level", "ASC") ->find_all(); - foreach ($query as $row) { - $value = ($row->$field === self::ALLOW) ? "TRUE" : "FALSE"; - $db->query( - "UPDATE {access_caches} SET `$field` = $value " . - "WHERE `item_id` IN " . - " (SELECT `id` FROM {items} " . - " WHERE `left_ptr` >= $row->left_ptr " . - " AND `right_ptr` <= $row->right_ptr)"); + foreach ($query as $row) { + $value = ($row->$field === self::ALLOW) ? true : false; + db::build() + ->update("access_caches") + ->set($field, $value) + ->where("item_id", "IN", + db::build() + ->select("id") + ->from("items") + ->where("left_ptr", ">=", $row->left_ptr) + ->where("right_ptr", "<=", $row->right_ptr)) + ->execute(); } } |