mirror of
https://github.com/dg/dibi.git
synced 2025-08-04 05:07:36 +02:00
* dibi::loadFile() extreme fast SQL dump loading
This commit is contained in:
@@ -392,6 +392,19 @@ class dibi
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Import SQL dump from file - extreme fast!
|
||||
*
|
||||
* @param filename
|
||||
* @return int count of sql commands
|
||||
*/
|
||||
public static function loadFile($file)
|
||||
{
|
||||
return self::getConnection()->loadFile($file);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Experimental; will be used in PHP 5.3
|
||||
*/
|
||||
|
@@ -388,6 +388,40 @@ class DibiConnection extends NObject
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Import SQL dump from file - extreme fast!
|
||||
*
|
||||
* @param filename
|
||||
* @return int count of sql commands
|
||||
*/
|
||||
public function loadFile($file)
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
@set_time_limit(0);
|
||||
|
||||
$handle = @fopen($file, 'r');
|
||||
if (!$handle) {
|
||||
throw new DibiException("Cannot open file '$file'");
|
||||
}
|
||||
|
||||
$count = 0;
|
||||
$sql = '';
|
||||
while (!feof($handle)) {
|
||||
$s = fgets($handle);
|
||||
$sql .= $s;
|
||||
if (substr(rtrim($s), -1) === ';') {
|
||||
$this->driver->query($sql);
|
||||
$sql = '';
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
fclose($handle);
|
||||
return $count;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Gets a information of the current database.
|
||||
*
|
||||
@@ -405,7 +439,7 @@ class DibiConnection extends NObject
|
||||
*/
|
||||
public function __wakeup()
|
||||
{
|
||||
throw new DibiException('You cannot serialize or unserialize '.__CLASS__.' instances');
|
||||
throw new DibiException('You cannot serialize or unserialize ' . $this->getClass() . ' instances');
|
||||
}
|
||||
|
||||
|
||||
@@ -415,7 +449,7 @@ class DibiConnection extends NObject
|
||||
*/
|
||||
public function __sleep()
|
||||
{
|
||||
throw new DibiException('You cannot serialize or unserialize '.__CLASS__.' instances');
|
||||
throw new DibiException('You cannot serialize or unserialize ' . $this->getClass() . ' instances');
|
||||
}
|
||||
|
||||
}
|
||||
|
16
examples/load-sql-dump.php
Normal file
16
examples/load-sql-dump.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<h1>dibi import SQL dump example</h1>
|
||||
<pre>
|
||||
<?php
|
||||
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'sample.sdb',
|
||||
));
|
||||
|
||||
|
||||
$count = dibi::loadFile('compress.zlib://dump.sql.gz');
|
||||
|
||||
echo 'Number of SQL commands:', $count;
|
Reference in New Issue
Block a user