diff --git a/dibi/dibi.php b/dibi/dibi.php index 72cd4874..18c3ec20 100644 --- a/dibi/dibi.php +++ b/dibi/dibi.php @@ -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 */ diff --git a/dibi/libs/DibiConnection.php b/dibi/libs/DibiConnection.php index 06f56a3e..8b2640a7 100644 --- a/dibi/libs/DibiConnection.php +++ b/dibi/libs/DibiConnection.php @@ -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'); } } diff --git a/examples/load-sql-dump.php b/examples/load-sql-dump.php new file mode 100644 index 00000000..21555faa --- /dev/null +++ b/examples/load-sql-dump.php @@ -0,0 +1,16 @@ +

dibi import SQL dump example

+
+ 'sqlite',
+    'database' => 'sample.sdb',
+));
+
+
+$count = dibi::loadFile('compress.zlib://dump.sql.gz');
+
+echo 'Number of SQL commands:', $count;
\ No newline at end of file