diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57872d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor/ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..4965623 --- /dev/null +++ b/composer.json @@ -0,0 +1,17 @@ +{ + "name": "itsmill3rtime/mysql-load-data-tool", + "authors": [ + { + "name": "Jimmy", + "email": "jmiller87@gmail.com" + } + ], + "require": { + "php": ">=5.6.0" + }, + "autoload": { + "psr-0": { + "ItsMill3rTime": "src/" + } + } +} diff --git a/src/MySQLFileTool.php b/src/MySQLFileTool.php new file mode 100644 index 0000000..5bc530f --- /dev/null +++ b/src/MySQLFileTool.php @@ -0,0 +1,135 @@ +_table = $table; + $this->_keys = $keys; + $this->_chunk_size = $chunk_size; + + } + + /** + * @return null|string + */ + private function getActiveFile() + { + //if we need a new file + if (is_null($this->_active_file)) { + $this->_active_file = storage_path('SQL-' . uniqid()); + $this->_active_file_reference = fopen($this->_active_file, 'a'); + $this->_files[] = $this->_active_file; + } + + //if we hit our chunk size + if ($this->_current_index >= $this->_chunk_size) { + fclose($this->_active_file_reference); + //set active to null so we can make a new one + $this->_active_file = null; + $this->_current_index = 0; + + //create a new one + return $this->getActiveFile(); + } + + return $this->_active_file; + } + + /** + * + */ + public function import() + { + try { + fclose($this->_active_file_reference); + } catch (\Exception $e) { + } + foreach ($this->_files as $file) { + try { + \DB::connection()->getPdo()->exec("LOAD DATA LOCAL INFILE '" . $file . "' INTO TABLE " . $this->_table . " FIELDS TERMINATED BY '|||' (" . implode(",", $this->_keys) . ")"); + $this->destroyFile($file); + } catch (\Exception $exception) { + //keep failed file for review + dump('file: ' . $file . ' failed' . $exception); + } + } + } + + /** + * @param array $row_fields_data + */ + public function addRow(array $row_fields_data) + { + $row = implode('|||', $row_fields_data); + fputs($this->_active_file_reference, $row . PHP_EOL); + $this->_current_index++; + } + + /** + * @param $rows + */ + public function addRows($rows) + { + $complete_string = ''; + foreach ($rows as $row_fields_data) { + $row = implode('|||', $row_fields_data); + $complete_string .= $row . PHP_EOL; + } + + file_put_contents($this->getActiveFile(), $complete_string, FILE_APPEND | LOCK_EX); + $this->_current_index = $this->_current_index + count($rows); + $complete_string = null; + } + + /** + * @param $file + */ + private function destroyFile($file) + { + @unlink($file); + } +} \ No newline at end of file