Closed (won't fix)
Project:
Path redirect
Version:
6.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Reporter:
Created:
30 Jan 2009 at 03:10 UTC
Updated:
30 Jan 2009 at 03:24 UTC
(ok, obtuse title, but see the example code)
function path_redirect_save($edit) {
$edit += array(
'type' => variable_get('path_redirect_default_status', 301),
'query' => '',
'fragment' => '',
);
if (!empty($edit['rid'])) {
$return = db_query("UPDATE {path_redirect} SET path = '%s', redirect = '%s', query = '%s', fragment = '%s', type = %d WHERE rid = %d", $edit['path'], $edit['redirect'], $edit['query'], $edit['fragment'], $edit['type'], $edit['rid']);
}
else {
$return = db_query("INSERT INTO {path_redirect} (path, redirect, query, fragment, type) VALUES ('%s', '%s', '%s', '%s', '%s')", $edit['path'], $edit['redirect'], $edit['query'], $edit['fragment'], $edit['type']);
}
return $return;
}
To use that to do an update I need to do a select on the database before I call it.
vs.
function path_redirect_save($edit) {
$edit += array(
'type' => variable_get('path_redirect_default_status', 301),
'query' => '',
'fragment' => '',
);
$return = db_query("UPDATE {path_redirect} SET path = '%s', redirect = '%s', query = '%s', fragment = '%s', type = %d WHERE rid = %d", $edit['path'], $edit['redirect'], $edit['query'], $edit['fragment'], $edit['type'], $edit['rid']);
if (!db_affected_rows()) {
$return = db_query("INSERT INTO {path_redirect} (path, redirect, query, fragment, type) VALUES ('%s', '%s', '%s', '%s', '%s')", $edit['path'], $edit['redirect'], $edit['query'], $edit['fragment'], $edit['type']);
}
return $return;
}
The second version reduces the chances of a race condition and improves the DX. It's a pattern that's very common in core.
Comments
Comment #1
dave reidThe latest 6.x-1.x code actually makes things even better by using drupal_write_record:
So I'm gunna go ahead and mark this as won't fix since there's no "already implemented" status. :) Thanks for the code review though, if you have more suggestions let me know!