Skip to content

Commit

Permalink
Support importing SQL file
Browse files Browse the repository at this point in the history
  • Loading branch information
lubosdz committed Sep 11, 2022
1 parent b232c2b commit ddfb421
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/Ifsnop/Mysqldump/Mysqldump.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,46 @@ public function getTableLimit($tableName)
return $limit;
}

/**
* Import supplied SQL file
* @param string $path Absolute path to imported *.sql file
*/
public function restore($path)
{
if(!$path || !is_file($path)){
throw new Exception("File {$path} does not exist.");
}

$handle = fopen($path , 'rb');

if(!$handle){
throw new Exception("Failed reading file {$path}. Check access permissions.");
}

if(!$this->dbHandler){
$this->connect();
}

$buffer = '';
while ( !feof($handle) ) {
$line = trim(fgets($handle));

if (substr($line, 0, 2) == '--' || !$line) {
continue; // skip comments
}

$buffer .= $line;

// if it has a semicolon at the end, it's the end of the query
if (';' == substr(rtrim($line), -1, 1)) {
$this->dbHandler->exec($buffer);
$buffer = '';
}
}

fclose($handle);
}

/**
* Parse DSN string and extract dbname value
* Several examples of a DSN string
Expand Down

0 comments on commit ddfb421

Please sign in to comment.