示例#1
0
 public static function sqlDumpTable($table, $extendIns, $sizeLimit, $useHex = true, $startFrom = 0, $currentSize = 0)
 {
     $offset = self::OFFSET;
     $tableDump = "";
     $command = Ibos::app()->db->createCommand();
     $tableFields = $command->setText("SHOW FULL COLUMNS FROM `{$table}`")->queryAll();
     if (!$tableFields) {
         $useHex = false;
     }
     if (!in_array($table, self::getExceptTables())) {
         $tableDumped = 0;
         $numRows = $offset;
         $firstField = $tableFields[0];
         if ($extendIns == "0") {
             while ($currentSize + strlen($tableDump) + 500 < $sizeLimit * 1000 && $numRows == $offset) {
                 if ($firstField["Extra"] == "auto_increment") {
                     $selectSql = "SELECT * FROM `{$table}` WHERE `{$firstField["Field"]}` > {$startFrom} ORDER BY `{$firstField["Field"]}` LIMIT {$offset}";
                 } else {
                     $selectSql = "SELECT * FROM `{$table}` LIMIT {$startFrom}, {$offset}";
                 }
                 $tableDumped = 1;
                 $numRows = $command->setText($selectSql)->execute();
                 $rows = $command->queryAll();
                 foreach ($rows as $row) {
                     $comma = $t = "";
                     $index = 0;
                     foreach ($row as $value) {
                         $t .= $comma . ($useHex && !empty($value) && (StringUtil::strExists($tableFields[$index]["Type"], "char") || StringUtil::strExists($tableFields[$index]["Type"], "text")) ? "0x" . bin2hex($value) : "'" . addslashes($value) . "'");
                         $comma = ",";
                         $index++;
                     }
                     if (strlen($t) + $currentSize + strlen($tableDump) + 500 < $sizeLimit * 1000) {
                         if ($firstField["Extra"] == "auto_increment") {
                             $startFrom = array_shift($row);
                         } else {
                             $startFrom++;
                         }
                         $tableDump .= "INSERT INTO `{$table}` VALUES ( {$t});\n";
                     } else {
                         self::$complete = false;
                         break 2;
                     }
                 }
             }
         } else {
             while ($currentSize + strlen($tableDump) + 500 < $sizeLimit * 1000 && $numRows == $offset) {
                 if ($firstField["Extra"] == "auto_increment") {
                     $selectSql = "SELECT * FROM `{$table}` WHERE {$firstField["Field"]} > {$startFrom} LIMIT {$offset}";
                 } else {
                     $selectSql = "SELECT * FROM `{$table}` LIMIT {$startFrom}, {$offset}";
                 }
                 $tableDumped = 1;
                 $numRows = $command->setText($selectSql)->execute();
                 $rows = $command->queryAll();
                 if ($numRows) {
                     $t1 = $comma1 = "";
                     foreach ($rows as $row) {
                         $t2 = $comma2 = "";
                         $index = 0;
                         foreach ($row as $value) {
                             $t2 .= $comma2 . ($useHex && !empty($value) && (StringUtil::strExists($tableFields[$index]["Type"], "char") || StringUtil::strExists($tableFields[$index]["Type"], "text")) ? "0x" . bin2hex($value) : "'" . addslashes($value) . "'");
                             $comma2 = ",";
                             $index++;
                         }
                         if (strlen($t1) + $currentSize + strlen($tableDump) + 500 < $sizeLimit * 1000) {
                             if ($firstField["Extra"] == "auto_increment") {
                                 $startFrom = array_shift($row);
                             } else {
                                 $startFrom++;
                             }
                             $t1 .= "{$comma1} ({$t2})";
                             $comma1 = ",";
                         } else {
                             $tableDump .= "INSERT INTO `{$table}` VALUES {$t1};\n";
                             self::$complete = false;
                             break 2;
                         }
                     }
                     $tableDump .= "INSERT INTO `{$table}` VALUES {$t1};\n";
                 }
             }
         }
         self::$startRow = $startFrom;
         $tableDump .= "\n";
     }
     return $tableDump;
 }