mirror of
https://github.com/moodle/moodle.git
synced 2025-01-17 21:49:15 +01:00
Merged from MOODLE_14_STABLE - insert_record() - major efficiency improvements for Postgres databases on insert. Also fixed many calls to insert_record() which discard the returned record id to not ask for the record id.
This commit is contained in:
parent
cd1f1e0d68
commit
a9a9bdba55
@ -184,7 +184,7 @@
|
||||
|
||||
|
||||
} else if (strcmp ($result, "INVALID") == 0) { // ERROR
|
||||
insert_record("enrol_paypal", $data);
|
||||
insert_record("enrol_paypal", $data, false);
|
||||
email_paypal_error_to_admin("Received an invalid payment notification!! (Fake payment?)", $data);
|
||||
}
|
||||
}
|
||||
|
@ -228,7 +228,7 @@ function algebra_filter ($courseid, $text) {
|
||||
$texcache->md5key = $md5;
|
||||
$texcache->rawtext = addslashes($texexp);
|
||||
$texcache->timemodified = time();
|
||||
insert_record("cache_filters",$texcache);
|
||||
insert_record("cache_filters",$texcache, false);
|
||||
$text = str_replace( $matches[0][$i], string_file_picture_algebra($filename, $texexp, '', '', $align), $text);
|
||||
} else {
|
||||
$text = str_replace( $matches[0][$i],"<b>Undetermined error:</b> ",$text);
|
||||
|
@ -140,7 +140,7 @@ function tex_filter ($courseid, $text) {
|
||||
$texcache->md5key = $md5;
|
||||
$texcache->rawtext = addslashes($texexp);
|
||||
$texcache->timemodified = time();
|
||||
insert_record("cache_filters",$texcache);
|
||||
insert_record("cache_filters",$texcache, false);
|
||||
}
|
||||
$filename = $md5 . ".gif";
|
||||
$text = str_replace( $matches[0][$i], string_file_picture_tex($filename, $texexp, '', '', $align), $text);
|
||||
|
@ -771,6 +771,13 @@ function get_records_sql($sql) {
|
||||
foreach ($records as $key => $record) {
|
||||
$objects[$key] = (object) $record;
|
||||
}
|
||||
// log performance info
|
||||
if ($rs->RecordCount() > 100
|
||||
&& !empty($CFG->perfdebug)
|
||||
&& function_exists('memory_get_usage')) {
|
||||
error_log("get_records_sql() in $_SERVER[REQUEST_URI]. Fetched $rs->RecordCount() records. Memory allocated: "
|
||||
. memory_get_usage());
|
||||
}
|
||||
return $objects;
|
||||
} else {
|
||||
return false;
|
||||
@ -1069,6 +1076,19 @@ function insert_record($table, $dataobject, $returnid=true, $primarykey='id') {
|
||||
|
||||
unset($dataobject->id);
|
||||
|
||||
/// Postgres doesn't have the concept of primary key built in
|
||||
/// and will return the OID which isn't what we want.
|
||||
/// The efficient and transaction-safe strategy is to
|
||||
/// move the sequence forward first, and make the insert
|
||||
/// with an explicit id.
|
||||
if ( !isset($dataobject->{$primarykey})
|
||||
&& $CFG->dbtype === 'postgres7'
|
||||
&& $returnid == true ) {
|
||||
if ($nextval = get_field_sql("SELECT NEXTVAL('{$CFG->prefix}{$table}_{$primarykey}_seq')")) {
|
||||
$dataobject->{$primarykey} = $nextval;
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the correct SQL from adoDB
|
||||
if (!$insertSQL = $db->GetInsertSQL($rs, (array)$dataobject, true)) {
|
||||
return false;
|
||||
@ -1087,20 +1107,32 @@ function insert_record($table, $dataobject, $returnid=true, $primarykey='id') {
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Find the return ID of the newly inserted record
|
||||
switch ($CFG->dbtype) {
|
||||
case 'postgres7': // Just loves to be special
|
||||
$oid = $db->Insert_ID();
|
||||
if ($rs = $db->Execute('SELECT '. $primarykey .' FROM '. $CFG->prefix . $table .' WHERE oid = '. $oid)) {
|
||||
if ($rs->RecordCount() == 1) {
|
||||
return (integer) $rs->fields[0];
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
default:
|
||||
return (int)$db->Insert_ID(); // Should work on most databases, but not all!
|
||||
/// We already know the record PK
|
||||
/// if it's been passed explicitly,
|
||||
/// or if we've retrieved it from a
|
||||
/// sequence (Postgres).
|
||||
if (isset($dataobject->{$primarykey})) {
|
||||
return $dataobject->{$primarykey};
|
||||
}
|
||||
|
||||
/// This only gets triggered with non-Postgres databases
|
||||
/// however we have some postgres fallback in case we failed
|
||||
/// to find the sequence.
|
||||
$id = $db->Insert_ID();
|
||||
|
||||
if ($CFG->dbtype === 'postgres7') {
|
||||
// try to get the primary key based on id
|
||||
if ( ($rs = $db->Execute('SELECT '. $primarykey .' FROM '. $CFG->prefix . $table .' WHERE oid = '. $id))
|
||||
&& ($rs->RecordCount() == 1) ) {
|
||||
trigger_error("Retrieved $primarykey from oid on table $table because we could not find the sequence.");
|
||||
return (integer)$rs->fields[0];
|
||||
}
|
||||
trigger_error('Failed to retrieve primary key after insert: SELECT '. $primarykey .' FROM '. $CFG->prefix . $table .' WHERE oid = '. $id);
|
||||
return false;
|
||||
}
|
||||
|
||||
return (integer)$id;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -1837,6 +1837,11 @@ function print_footer($course=NULL, $usercourse=NULL) {
|
||||
/// Include the actual footer file
|
||||
|
||||
include ($CFG->themedir.$CFG->theme.'/footer.html');
|
||||
|
||||
if ($CFG->perfdebug && function_exists('memory_get_usage')) {
|
||||
error_log("Page $_SERVER[REQUEST_URI] completed. Memory allocated: "
|
||||
. memory_get_usage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -326,7 +326,7 @@ class ChatDaemon {
|
||||
$msg->timestamp = time();
|
||||
|
||||
// Commit to DB
|
||||
insert_record('chat_messages', $msg);
|
||||
insert_record('chat_messages', $msg, false);
|
||||
|
||||
// OK, now push it out to all users
|
||||
$this->message_broadcast($msg, $this->sets_info[$sessionid]['user']);
|
||||
@ -420,7 +420,7 @@ class ChatDaemon {
|
||||
$msg->message = addslashes($msg->message);
|
||||
|
||||
// Commit to DB
|
||||
insert_record('chat_messages', $msg);
|
||||
insert_record('chat_messages', $msg, false);
|
||||
|
||||
// Undo the hack
|
||||
$msg->message = $origmsg;
|
||||
@ -530,7 +530,7 @@ class ChatDaemon {
|
||||
$msg->message = 'enter';
|
||||
$msg->timestamp = time();
|
||||
|
||||
insert_record('chat_messages', $msg);
|
||||
insert_record('chat_messages', $msg, false);
|
||||
$this->message_broadcast($msg, $this->sets_info[$sessionid]['user']);
|
||||
|
||||
return true;
|
||||
@ -735,7 +735,7 @@ class ChatDaemon {
|
||||
$msg->timestamp = time();
|
||||
|
||||
$this->trace('User has disconnected, destroying uid '.$info['userid'].' with SID '.$sessionid, E_USER_WARNING);
|
||||
insert_record('chat_messages', $msg);
|
||||
insert_record('chat_messages', $msg, false);
|
||||
|
||||
// *************************** IMPORTANT
|
||||
//
|
||||
|
@ -186,7 +186,7 @@ if ( $confirm ) {
|
||||
foreach ($form->categories as $category) {
|
||||
if ( $category > 0 ) {
|
||||
$newcategory->categoryid = $category;
|
||||
insert_record("glossary_entries_categories",$newcategory);
|
||||
insert_record("glossary_entries_categories",$newcategory, false);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
@ -200,7 +200,7 @@ if ( $confirm ) {
|
||||
unset($newalias);
|
||||
$newalias->entryid = $e;
|
||||
$newalias->alias = $alias;
|
||||
insert_record("glossary_alias",$newalias);
|
||||
insert_record("glossary_alias",$newalias, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user