Qual: Simplify code

This commit is contained in:
Laurent Destailleur 2010-08-31 21:46:21 +00:00
parent 2ac615a352
commit 0cfe0514d3

View File

@ -133,21 +133,24 @@ function run_sql($sqlfile,$silent=1,$entity='',$usesavepoint=1)
} }
} }
// Ajout ligne si non commentaire // Add line buf to buffer if not a comment
if (! preg_match('/^--/',$buf)) $buffer .= $buf; if (! preg_match('/^--/',$buf))
{
$buffer .= trim($buf);
}
// print $buf.'<br>'; // print $buf.'<br>';
if (preg_match('/;/',$buffer)) if (preg_match('/;/',$buffer))
{ {
// Found new request // Found new request
$arraysql[$i]=trim($buffer); if ($buffer) $arraysql[$i]=$buffer;
$i++; $i++;
$buffer=''; $buffer='';
} }
} }
if ($buffer) $arraysql[$i]=trim($buffer); if ($buffer) $arraysql[$i]=$buffer;
fclose($fp); fclose($fp);
} }
else else
@ -159,42 +162,39 @@ function run_sql($sqlfile,$silent=1,$entity='',$usesavepoint=1)
$listofmaxrowid=array(); // This is a cache table $listofmaxrowid=array(); // This is a cache table
foreach($arraysql as $i => $sql) foreach($arraysql as $i => $sql)
{ {
if ($sql) $newsql=$sql;
// Replace __+MAX_table__ with max of table
while (preg_match('/__\+MAX_([A-Za-z_]+)__/i',$newsql,$reg))
{ {
$newsql=$sql; $table=$reg[1];
if (! isset($listofmaxrowid[$table]))
// Replace __+MAX_table__ with max of table
while (preg_match('/__\+MAX_([A-Za-z_]+)__/i',$newsql,$reg))
{ {
$table=$reg[1]; //var_dump($db);
if (! isset($listofmaxrowid[$table])) $sqlgetrowid='SELECT MAX(rowid) as max from '.$table;
$resql=$db->query($sqlgetrowid);
if ($resql)
{ {
//var_dump($db); $obj=$db->fetch_object($resql);
$sqlgetrowid='SELECT MAX(rowid) as max from '.$table; $listofmaxrowid[$table]=$obj->max;
$resql=$db->query($sqlgetrowid); if (empty($listofmaxrowid[$table])) $listofmaxrowid[$table]=0;
if ($resql) }
{ else
$obj=$db->fetch_object($resql); {
$listofmaxrowid[$table]=$obj->max; dol_syslog('Admin.lib::run_sql Failed to get max rowid for '.$table.' '.$db->lasterror().' sql='.$sqlgetrowid, LOG_ERR);
if (empty($listofmaxrowid[$table])) $listofmaxrowid[$table]=0; if (! $silent) print '<tr><td valign="top" colspan="2">';
} if (! $silent) print '<div class="error">'.$langs->trans("Failed to get max rowid for ".$table)."</div></td>";
else if (! $silent) print '</tr>';
{ $error++;
dol_syslog('Admin.lib::run_sql Failed to get max rowid for '.$table.' '.$db->lasterror().' sql='.$sqlgetrowid, LOG_ERR); break;
if (! $silent) print '<tr><td valign="top" colspan="2">';
if (! $silent) print '<div class="error">'.$langs->trans("Failed to get max rowid for ".$table)."</div></td>";
if (! $silent) print '</tr>';
$error++;
break;
}
} }
$from='__+MAX_'.$table.'__';
$to='+'.$listofmaxrowid[$table];
$newsql=str_replace($from,$to,$newsql);
dol_syslog('Admin.lib::run_sql New Request '.($i+1).' (replacing '.$from.' to '.$to.') sql='.$newsql, LOG_DEBUG);
$arraysql[$i]=$newsql;
} }
$from='__+MAX_'.$table.'__';
$to='+'.$listofmaxrowid[$table];
$newsql=str_replace($from,$to,$newsql);
dol_syslog('Admin.lib::run_sql New Request '.($i+1).' (replacing '.$from.' to '.$to.') sql='.$newsql, LOG_DEBUG);
$arraysql[$i]=$newsql;
} }
} }