Merged stable branch up to 2.3.10
[fa-stable.git] / admin / db / maintenance_db.inc
index 4e7d56ccdc443e14f3f8d3d20837c0e2304d352c..ce8e550619a22ee44fb388c06a54a1b04eac3d22 100644 (file)
@@ -18,6 +18,8 @@
  * @param $strSortType String containing either asc or desc [default to asc]
  * @desc Naturally sorts an array using by the column $strSortBy
  */
+define('EXPORT_MAX_INSERT', 50000);
+
 function array_natsort($aryData, $strIndex, $strSortBy, $strSortType=false)
 {
    //    if the parameters are invalid
@@ -178,7 +180,7 @@ function update_extensions($extensions) {
        }
 
        // update per company files
-       $cnt = count($db_connections);
+       $cnt = max(1, count($db_connections));
        for($i = 0; $i < $cnt; $i++) 
        {
                $newexts = $extensions;
@@ -253,7 +255,9 @@ function write_lang()
 
 function db_import($filename, $connection, $force=true)
 {
-       global $db, $go_debug;
+       global $db, $go_debug, $sql_trail;
+
+       $sql_trail = false;
 
        $allowed_commands = array(
                "create"  => 'table_queries', 
@@ -557,12 +561,12 @@ function db_export($conn, $filename, $zip='no', $comment='', $no_default=false)
 //                     if ($auto_incr[$tablename])
 //                             $out.=" AUTO_INCREMENT=".$auto_incr[$tablename];
                        $out.=" ;";
-                       $out.="\n\n\n";
+                       $out.="\n\n";
 
                        // export data
                        if (!$error)
                        {
-                               $out.="### Data of table `".$tablename."` ###\n\n";
+                               $out.="### Data of table `".$tablename."` ###\n";
 
                                // check if field types are NULL or NOT NULL
                                $res3 = db_query("SHOW COLUMNS FROM `" . $tablename . "`");
@@ -575,18 +579,40 @@ function db_export($conn, $filename, $zip='no', $comment='', $no_default=false)
                                }
 
                                $res2 = db_query("SELECT * FROM `" . $tablename . "`");
+                               $maxinsert = 0;
+                               $insert = '';
                                for ($j = 0; $j < db_num_rows($res2); $j++)
                                {
-                                       $out .= "INSERT INTO `" . $tablename . "` VALUES (";
                                        $row2 = db_fetch_row($res2);
-                                       // run through each field
+                                       $values = '(';
                                        for ($k = 0; $k < $nf = db_num_fields($res2); $k++)
                                        {
-                                               $out .= db_escape($row2[$k], $field_null[$k]);
+                                               $values .= db_escape($row2[$k], $field_null[$k]);
                                                if ($k < ($nf - 1))
-                                                       $out .= ", ";
+                                                       $values .= ', ';
+                                       }
+                                       $values .= ')';
+                                       $len = strlen($values);
+                                       if ($maxinsert < $len+1)
+                                       {
+                                               $maxinsert = EXPORT_MAX_INSERT;
+                                               if ($insert)
+                                               {
+                                                       $out .= $insert .';'; // flush insert query
+                                                       $insert = '';
+                                               }
                                        }
-                                       $out .= ");\n";
+
+                                       if ($insert == '')
+                                       {
+                                               $insert = "\nINSERT INTO `" . $tablename . "` VALUES\n";
+                                               $maxinsert -= strlen($insert);
+                                       } else {
+                                               $insert .= ",\n";
+                                       }
+
+                                       $maxinsert -= $len;
+                                       $insert .= $values;
 
                                        // if saving is successful, then empty $out, else set error flag
                                        if (strlen($out) > $max_size && $zip != "zip")
@@ -597,7 +623,8 @@ function db_export($conn, $filename, $zip='no', $comment='', $no_default=false)
                                                        $error = true;
                                        }
                                }
-
+                               if ($insert)
+                                       $out .= $insert. ';';
                        // an error occurred! Try to delete file and return error status
                        }
                        elseif ($error)