Elgg\Database::runSqlScript PHP Method

runSqlScript() public method

The file specified should be a standard SQL file as created by mysqldump or similar. Statements must be terminated with ; and a newline character (\n or \r\n). The special string 'prefix_' is replaced with the database prefix as defined in {@link $this->tablePrefix}.
public runSqlScript ( string $scriptlocation ) : void
$scriptlocation string The full path to the script
return void
    public function runSqlScript($scriptlocation)
    {
        $script = file_get_contents($scriptlocation);
        if ($script) {
            $errors = array();
            // Remove MySQL '-- ' and '# ' style comments
            $script = preg_replace('/^(?:--|#) .*$/m', '', $script);
            // Statements must end with ; and a newline
            $sql_statements = preg_split('/;[\\n\\r]+/', "{$script}\n");
            foreach ($sql_statements as $statement) {
                $statement = trim($statement);
                $statement = str_replace("prefix_", $this->table_prefix, $statement);
                if (!empty($statement)) {
                    try {
                        $this->updateData($statement);
                    } catch (\DatabaseException $e) {
                        $errors[] = $e->getMessage();
                    }
                }
            }
            if (!empty($errors)) {
                $errortxt = "";
                foreach ($errors as $error) {
                    $errortxt .= " {$error};";
                }
                $msg = "There were a number of issues: " . $errortxt;
                throw new \DatabaseException($msg);
            }
        } else {
            $msg = "Elgg couldn't find the requested database script at " . $scriptlocation . ".";
            throw new \DatabaseException($msg);
        }
    }