Database::batchQueryFromFile PHP Method

batchQueryFromFile() public method

Roughly parse a .sql file and execute it in small batches
public batchQueryFromFile ( $fullSource ) : boolean
$fullSource string: Full source of .sql file There should be no more than 1 statement (ending in ;) on a single line.
return boolean
    public function batchQueryFromFile($fullSource)
    {
        $lines = explode("\n", $fullSource);
        $sql = '';
        $realSql = false;
        foreach ($lines as $line) {
            $line = trim($line);
            // Skip empty lines and comments
            if ($line === '' || $line[0] === '-' && $line[1] === '--') {
                continue;
            }
            if ($sql !== '') {
                $sql .= ' ';
            }
            $sql .= "{$line}\n";
            // Is this line the end of statement?
            $lineCopy = $line;
            $lineCopy = preg_replace('/' . preg_quote($this->delimiter, '/') . '$/', '', $lineCopy);
            if ($lineCopy != $line) {
                // Execute what we have so far and reset the sql string
                $realSql = $sql;
                $sql = '';
                $this->query($realSql);
            }
        }
        return true;
    }