public static function loadFromFile(Connection $connection, $file)
{
@set_time_limit(0);
// @ function may be disabled
$handle = @fopen($file, 'r');
// @ is escalated to exception
if (!$handle) {
throw new Nette\FileNotFoundException("Cannot open file '{$file}'.");
}
$count = 0;
$delimiter = ';';
$sql = '';
$pdo = $connection->getPdo();
// native query without logging
while (!feof($handle)) {
$s = rtrim(fgets($handle));
if (!strncasecmp($s, 'DELIMITER ', 10)) {
$delimiter = substr($s, 10);
} elseif (substr($s, -strlen($delimiter)) === $delimiter) {
$sql .= substr($s, 0, -strlen($delimiter));
$pdo->exec($sql);
$sql = '';
$count++;
} else {
$sql .= $s . "\n";
}
}
if (trim($sql) !== '') {
$pdo->exec($sql);
$count++;
}
fclose($handle);
return $count;
}