Example #1
0
 /** Export table data
  * @param string
  * @param string
  * @param string
  * @return null prints data
  */
 function dumpData($table, $style, $query)
 {
     global $connection, $jush;
     $max_packet = $jush == "sqlite" ? 0 : 1048576;
     // default, minimum is 1024
     if ($style) {
         if ($_POST["format"] == "sql") {
             if ($style == "TRUNCATE+INSERT") {
                 echo truncate_sql($table) . ";\n";
             }
             $fields = fields($table);
         }
         $result = $connection->query($query, 1);
         // 1 - MYSQLI_USE_RESULT //! enum and set as numbers
         if ($result) {
             $insert = "";
             $buffer = "";
             $keys = array();
             $suffix = "";
             $fetch_function = $table != '' ? 'fetch_assoc' : 'fetch_row';
             while ($row = $result->{$fetch_function}()) {
                 if (!$keys) {
                     $values = array();
                     foreach ($row as $val) {
                         $field = $result->fetch_field();
                         $keys[] = $field->name;
                         $key = idf_escape($field->name);
                         $values[] = "{$key} = VALUES({$key})";
                     }
                     $suffix = ($style == "INSERT+UPDATE" ? "\nON DUPLICATE KEY UPDATE " . implode(", ", $values) : "") . ";\n";
                 }
                 if ($_POST["format"] != "sql") {
                     if ($style == "table") {
                         dump_csv($keys);
                         $style = "INSERT";
                     }
                     dump_csv($row);
                 } else {
                     if (!$insert) {
                         $insert = "INSERT INTO " . table($table) . " (" . implode(", ", array_map('idf_escape', $keys)) . ") VALUES";
                     }
                     foreach ($row as $key => $val) {
                         $field = $fields[$key];
                         $row[$key] = $val !== null ? unconvert_field($field, preg_match('~(^|[^o])int|float|double|decimal~', $field["type"]) && $val != '' ? $val : q($val)) : "NULL";
                     }
                     $s = ($max_packet ? "\n" : " ") . "(" . implode(",\t", $row) . ")";
                     if (!$buffer) {
                         $buffer = $insert . $s;
                     } elseif (strlen($buffer) + 4 + strlen($s) + strlen($suffix) < $max_packet) {
                         // 4 - length specification
                         $buffer .= ",{$s}";
                     } else {
                         echo $buffer . $suffix;
                         $buffer = $insert . $s;
                     }
                 }
             }
             if ($buffer) {
                 echo $buffer . $suffix;
             }
         } elseif ($_POST["format"] == "sql") {
             echo "-- " . str_replace("\n", " ", $connection->error) . "\n";
         }
     }
 }
 /** Export table data
  * @param string
  * @param string
  * @param string
  * @return null prints data
  */
 function dumpData($table, $style, $query)
 {
     global $connection, $jush;
     $max_packet = $jush == "sqlite" ? 0 : 1048576;
     // default, minimum is 1024
     if ($style) {
         if ($_POST["format"] == "sql" && $style == "TRUNCATE+INSERT") {
             echo truncate_sql($table) . ";\n";
         }
         if ($_POST["format"] == "sql") {
             $fields = fields($table);
         }
         $result = $connection->query($query, 1);
         // 1 - MYSQLI_USE_RESULT //! enum and set as numbers
         if ($result) {
             $insert = "";
             $buffer = "";
             while ($row = $result->fetch_assoc()) {
                 if ($_POST["format"] != "sql") {
                     if ($style == "table") {
                         dump_csv(array_keys($row));
                         $style = "INSERT";
                     }
                     dump_csv($row);
                 } else {
                     if (!$insert) {
                         $insert = "INSERT INTO " . table($table) . " (" . implode(", ", array_map('idf_escape', array_keys($row))) . ") VALUES";
                     }
                     foreach ($row as $key => $val) {
                         $row[$key] = isset($val) ? ereg('int|float|double|decimal', $fields[$key]["type"]) ? $val : q($val) : "NULL";
                         //! columns looking like functions
                     }
                     $s = implode(",\t", $row);
                     if ($style == "INSERT+UPDATE") {
                         $set = array();
                         foreach ($row as $key => $val) {
                             $set[] = idf_escape($key) . " = {$val}";
                         }
                         echo "{$insert} ({$s}) ON DUPLICATE KEY UPDATE " . implode(", ", $set) . ";\n";
                     } else {
                         $s = ($max_packet ? "\n" : " ") . "({$s})";
                         if (!$buffer) {
                             $buffer = $insert . $s;
                         } elseif (strlen($buffer) + 4 + strlen($s) < $max_packet) {
                             // 4 - length specification
                             $buffer .= ",{$s}";
                         } else {
                             echo "{$buffer};\n";
                             $buffer = $insert . $s;
                         }
                     }
                 }
             }
             if ($_POST["format"] == "sql" && $style != "INSERT+UPDATE" && $buffer) {
                 $buffer .= ";\n";
                 echo $buffer;
             }
         } elseif ($_POST["format"] == "sql") {
             echo "-- " . str_replace("\n", " ", $connection->error) . "\n";
         }
     }
 }