Permalink
Browse files

Update all database queries such that we can run "php index.php

package" and generate the same SQL as before.
  • Loading branch information...
1 parent 2aba8c4 commit 9b75b85e71b47fc953773544c930f5cecf6b34a7 @bharat bharat committed Dec 18, 2009
Showing with 56 additions and 32 deletions.
  1. +4 −3 modules/gallery/controllers/packager.php
  2. +52 −29 modules/gallery/helpers/access.php
@@ -42,7 +42,7 @@ private function _reset() {
// 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 @@ private function _dump_database() {
$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"];
@@ -419,8 +419,8 @@ static function csrf_form_field() {
* @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 @@ private static function _get_all_groups() {
* @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 @@ private static function _drop_columns($perm_name, $group) {
* @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 @@ private static function _add_columns($perm_name, $group) {
*/
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 @@ private static function _update_access_view_cache($group, $item) {
// 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 @@ private static function _update_access_view_cache($group, $item) {
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 @@ private static function _update_access_view_cache($group, $item) {
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 @@ private static function _update_access_non_view_cache($group, $perm_name, $item)
->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 @@ private static function _update_access_non_view_cache($group, $perm_name, $item)
->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();
}
}

0 comments on commit 9b75b85

Please sign in to comment.