Skip to content

Commit

Permalink
MDL-15073 delete_records now truncates table if deleting all records,…
Browse files Browse the repository at this point in the history
… fixed some regressions
  • Loading branch information
skodak committed May 31, 2008
1 parent 3efa38a commit b820eb8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
28 changes: 13 additions & 15 deletions lib/accesslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -5175,8 +5175,6 @@ function build_context_path($force=false) {
WHERE temp.id={context}.id";
}

$udelsql = "TRUNCATE TABLE {context_temp}";

// Top level categories
$sql = "UPDATE {context}
SET depth=2, path=" . $DB->sql_concat("'$base/'", 'id') . "
Expand All @@ -5188,13 +5186,13 @@ function build_context_path($force=false) {
$emptyclause";

$DB->execute($sql);
$DB->execute($udelsql);
$DB->delete_records('context_temp');

// Deeper categories - one query per depthlevel
$maxdepth = $DB->get_field_sql("SELECT MAX(depth)
FROM {course_categories}");
for ($n=2; $n<=$maxdepth; $n++) {
$sql = "INSERT INTO {context}_temp (id, path, depth)
$sql = "INSERT INTO {context_temp} (id, path, depth)
SELECT ctx.id, ".$DB->sql_concat('pctx.path', "'/'", 'ctx.id').", $n+1
FROM {context} ctx
JOIN {course_categories} c ON ctx.instanceid=c.id
Expand All @@ -5203,19 +5201,19 @@ function build_context_path($force=false) {
AND pctx.contextlevel=".CONTEXT_COURSECAT."
AND c.depth=$n
AND NOT EXISTS (SELECT 'x'
FROM {context}_temp temp
FROM {context_temp} temp
WHERE temp.id = ctx.id)
$ctxemptyclause";
$DB->execute($sql);

// this is needed after every loop
// MDL-11532
$DB->execute($updatesql);
$DB->execute($udelsql);
$DB->delete_records('context_temp');
}

// Courses -- except sitecourse
$sql = "INSERT INTO {context}_temp (id, path, depth)
$sql = "INSERT INTO {context_temp} (id, path, depth)
SELECT ctx.id, ".$DB->sql_concat('pctx.path', "'/'", 'ctx.id').", pctx.depth+1
FROM {context} ctx
JOIN {course} c ON ctx.instanceid=c.id
Expand All @@ -5224,33 +5222,33 @@ function build_context_path($force=false) {
AND c.id!=".SITEID."
AND pctx.contextlevel=".CONTEXT_COURSECAT."
AND NOT EXISTS (SELECT 'x'
FROM {context}_temp temp
FROM {context_temp} temp
WHERE temp.id = ctx.id)
$ctxemptyclause";
$DB->execute($sql);

$DB->execute($updatesql);
$DB->execute($udelsql);
$DB->delete_records('context_temp');

// Module instances
$sql = "INSERT INTO {context}_temp (id, path, depth)
$sql = "INSERT INTO {context_temp} (id, path, depth)
SELECT ctx.id, ".$DB->sql_concat('pctx.path', "'/'", 'ctx.id').", pctx.depth+1
FROM {context} ctx
JOIN {course_modules} cm ON ctx.instanceid=cm.id
JOIN {context} pctx ON cm.course=pctx.instanceid
WHERE ctx.contextlevel=".CONTEXT_MODULE."
AND pctx.contextlevel=".CONTEXT_COURSE."
AND NOT EXISTS (SELECT 'x'
FROM {context}_temp temp
FROM {context_temp} temp
WHERE temp.id = ctx.id)
$ctxemptyclause";
$DB->execute($sql);

$DB->execute($updatesql);
$DB->execute($udelsql);
$DB->delete_records('context_temp');

// Blocks - non-pinned course-view only
$sql = "INSERT INTO {context}_temp (id, path, depth)
$sql = "INSERT INTO {context_temp} (id, path, depth)
SELECT ctx.id, ".$DB->sql_concat('pctx.path', "'/'", 'ctx.id').", pctx.depth+1
FROM {context} ctx
JOIN {block_instance} bi ON ctx.instanceid = bi.id
Expand All @@ -5259,13 +5257,13 @@ function build_context_path($force=false) {
AND pctx.contextlevel=".CONTEXT_COURSE."
AND bi.pagetype='course-view'
AND NOT EXISTS (SELECT 'x'
FROM {context}_temp temp
FROM {context_temp} temp
WHERE temp.id = ctx.id)
$ctxemptyclause";
$DB->execute($sql);

$DB->execute($updatesql);
$DB->execute($udelsql);
$DB->delete_records('context_temp');

// Blocks - others
$sql = "UPDATE {context}
Expand Down
2 changes: 1 addition & 1 deletion lib/datalib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1519,7 +1519,7 @@ function update_timezone_records($timezones) {
global $DB;

/// Clear out all the old stuff
$DB->execute("TRUNCATE TABLE {timezone}");
$DB->delete_records('timezone');

/// Insert all the new stuff
foreach ($timezones as $timezone) {
Expand Down
6 changes: 5 additions & 1 deletion lib/dml/moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -1032,12 +1032,16 @@ public function record_exists_sql($sql, array $params=null) {

/**
* Delete the records from a table where all the given conditions met.
* If conditions not specified, table is truncated.
*
* @param string $table the table to delete from.
* @param array $conditions optional array $fieldname=>requestedvalue with AND in between
* @return returns success.
*/
public function delete_records($table, array $conditions) {
public function delete_records($table, array $conditions=null) {
if (is_null($conditions)) {
return $this->execute("TRUNCATE TABLE {".$table."}");
}
list($select, $params) = $this->where_clause($conditions);
return $this->delete_records_select($table, $select, $params);
}
Expand Down

0 comments on commit b820eb8

Please sign in to comment.