-
Notifications
You must be signed in to change notification settings - Fork 4
/
php-mysqlimporter.php
90 lines (85 loc) · 3.78 KB
/
php-mysqlimporter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/*
* ClassName: PHP MySQL Importer v2.0.1
* PHP class for importing big SQL files into a MySql server.
* Author: David Castillo - [email protected]
* Hire me on: https://www.freelancer.com/u/DrAKkareS.html
* Blog: http://blog.d-castillo.info/
*/
class MySQLImporter {
public $hadErrors = false;
public $errors = array();
private $conn = null;
public function __construct($host, $user, $pass, $port = false) {
if ($port==false){
$port = ini_get("mysqli.default_port");
}
$this->hadErrors = false;
$this->errors = array();
$this->conn = new mysqli($host, $user, $pass, "", $port);
if ($this->conn->connect_error) {
$this->addError("Connect Error (".$this->conn->connect_errno.") ".$this->conn->connect_error);
}
}
private function addError($errorStr){
$this->hadErrors = true;
$this->errors[] = $errorStr;
}
public function doImport($sqlFile, $database = "", $createDB = false, $dropDB = false) {
if ($this->hadErrors == false) {
//Drop database if it's required
if ($dropDB && $database!=""){
if (!$this->conn->query("DROP DATABASE IF EXISTS ".$database)){
$this->addError("Query error (".$this->conn->errno.") ".$this->conn->error);
}
}
//Create the database if it's required
if ($createDB && $database!=""){
if (!$this->conn->query("CREATE DATABASE IF NOT EXISTS ".$database)){
$this->addError("Query error (".$this->conn->errno.") ".$this->conn->error);
}
}
//Select the database if it's required
if ($database!=""){
if (!$this->conn->select_db($database)){
$this->addError("Query error (".$this->conn->errno.") ".$this->conn->error);
}
}
if (is_file($sqlFile) && is_readable($sqlFile)){
try {
$f = fopen($sqlFile,"r");
$sqlFile = fread($f, filesize($sqlFile));
// processing and parsing the content
$sqlFile = str_replace("\r","\n",$sqlFile);
$lines = preg_split("/\n/", $sqlFile);
$queryStr = "";
foreach($lines as $line){
$lt_line = ltrim($line);
$t_queryStr = trim($queryStr);
if (1==preg_match("/^#|^\-\-/",$lt_line) && $t_queryStr == ""){
continue; // skip one-line comments
}
$queryStr .= $line."\n"; // append the line to the current query
$t_line = rtrim($lt_line);
if (1!==preg_match("/;$/",$t_line)){
continue; // skip incomplete statement
}
if (substr_count($queryStr,"/*")!=substr_count($queryStr,"*/")){
continue; // skip incomplete statement (hack for multiline comments)
}
$queryStr = trim($queryStr);
if (!$this->conn->query($queryStr)){
$this->addError("Query error (".$this->conn->errno.") ".$this->conn->error."\r\n\r\nOriginal Query:\r\n\r\n".$queryStr);
}
$queryStr="";
}
} catch(Exception $error) {
$this->addError("File error: (".$error->getCode().") ".$error->getMessage());
}
} else {
$this->addError("File error: '".$sqlFile."' is not a readable file.");
}
}
}
}
?>