Index: trunk/globals.php =================================================================== diff -u -r1248 -r1393 --- trunk/globals.php (.../globals.php) (revision 1248) +++ trunk/globals.php (.../globals.php) (revision 1393) @@ -124,24 +124,43 @@ function GetNextResourceId($Increment=1) { - $adodbConnection = &GetADODBConnection(); - $sql = "LOCK TABLES ".GetTablePrefix()."IdGenerator WRITE"; - $adodbConnection->Execute($sql); - $sql = "UPDATE ".GetTablePrefix()."IdGenerator SET lastid=lastid+".$Increment; - $adodbConnection->Execute($sql); - $rs = $adodbConnection->Execute("SELECT lastid FROM ".GetTablePrefix()."IdGenerator"); - $val = $rs->fields["lastid"]; - if(!$rs || $rs->EOF) + global $objModules; + $table_name = GetTablePrefix().'IdGenerator'; + + $db = &GetADODBConnection(); + + // dummy protection: get maximal resource id used actually and fix last_id used + $max_resourceid = 0; + $table_info = $objModules->ExecuteFunction('GetModuleInfo', 'dupe_resourceids'); + $sql_template = 'SELECT MAX(ResourceId) FROM '.GetTablePrefix().'%s'; + + foreach($table_info as $module_name => $module_info) + { + foreach($module_info as $module_sub_info) + { + $sql = sprintf($sql_template,$module_sub_info['Table']); + $tmp_resourceid = $db->GetOne($sql); + if($tmp_resourceid > $max_resourceid) $max_resourceid = $tmp_resourceid; + } + } + + // update lastid to be next resourceid available + $db->Execute('LOCK TABLES '.$table_name.' WRITE'); + $id_diff = $db->GetOne('SELECT '.$max_resourceid.' + 1 - lastid FROM '.$table_name); + if($id_diff) $Increment += $id_diff; + + $sql = 'UPDATE '.$table_name.' SET lastid = lastid + '.$Increment; // set new id in db + $db->Execute($sql); + + $val = $db->GetOne('SELECT lastid FROM '.$table_name); + if($val === false) { - echo $adodbConnection->ErrorMsg(); - $sql = "INSERT INTO ".GetTablePrefix()."IdGenerator (lastid) VALUES ($Increment)"; - $adodbConnection->Execute($sql); - $val = 1; + $db->Execute('INSERT INTO '.$table_name.' (lastid) VALUES ('.$Increment.')'); + $val = $Increment; } - $sql = "UNLOCK TABLES"; - $adodbConnection->Execute($sql); - $val = $val-($Increment-1); - return $val; + $db->Execute('UNLOCK TABLES'); + + return $val - $Increment + $id_diff; // return previous free id (-1) ? } function AddSlash($s)