1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-03 20:57:36 +02:00

* dibi::loadFile() extreme fast SQL dump loading

This commit is contained in:
David Grudl
2008-01-12 01:20:23 +00:00
parent de4a882788
commit da0a239d6d
3 changed files with 65 additions and 2 deletions

View File

@@ -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 * Experimental; will be used in PHP 5.3
*/ */

View File

@@ -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. * Gets a information of the current database.
* *
@@ -405,7 +439,7 @@ class DibiConnection extends NObject
*/ */
public function __wakeup() 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() public function __sleep()
{ {
throw new DibiException('You cannot serialize or unserialize '.__CLASS__.' instances'); throw new DibiException('You cannot serialize or unserialize ' . $this->getClass() . ' instances');
} }
} }

View 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;