public function nextQuery()
    {
        $sql = "";
        $hasQuery = false;

        while (($line = $this->sqlReader->readLine()) !== null) {
            $delimiter = $this->parent->getDelimiter();
            $project = $this->parent->getOwningTarget()->getProject();
            $line = $project->replaceProperties(trim($line));

            if (($line != $delimiter) && (StringHelper::startsWith("//", $line)
                    || StringHelper::startsWith("--", $line)
                    || StringHelper::startsWith("#", $line))
            ) {
                continue;
            }

            if (strlen($line) > 4
                && strtoupper(substr($line, 0, 4)) == "REM "
            ) {
                continue;
            }

            // MySQL supports defining new delimiters
            if (preg_match('/DELIMITER [\'"]?([^\'" $]+)[\'"]?/i', $line, $matches)) {
                $this->parent->setDelimiter($matches[1]);
                continue;
            }

            if ($this->sqlBacklog !== "") {
                $sql = $this->sqlBacklog;
                $this->sqlBacklog = "";
            }

            $sql .= " " . $line . "\n";

            // SQL defines "--" as a comment to EOL
            // and in Oracle it may contain a hint
            // so we cannot just remove it, instead we must end it
            if (strpos($line, "--") !== false) {
                $sql .= "\n";
            }

            // DELIM_ROW doesn't need this (as far as i can tell)
            if ($this->delimiterType == PDOSQLExecTask::DELIM_NORMAL) {
                $reg = "#((?:\"(?:\\\\.|[^\"])*\"?)+|'(?:\\\\.|[^'])*'?|" . preg_quote($delimiter) . ")#";

                $sqlParts = preg_split($reg, $sql, 0, PREG_SPLIT_DELIM_CAPTURE);
                $this->sqlBacklog = "";
                foreach ($sqlParts as $sqlPart) {
                    // we always want to append, even if it's a delim (which will be stripped off later)
                    $this->sqlBacklog .= $sqlPart;

                    // we found a single (not enclosed by ' or ") delimiter, so we can use all stuff before the delim as the actual query
                    if ($sqlPart === $delimiter) {
                        $sql = $this->sqlBacklog;
                        $this->sqlBacklog = "";
                        $hasQuery = true;
                    }
                }
            }

            if ($hasQuery || ($this->delimiterType == PDOSQLExecTask::DELIM_ROW && $line == $delimiter)) {
                // this assumes there is always a delimter on the end of the SQL statement.
                $sql = StringHelper::substring(
                    $sql,
                    0,
                    strlen($sql) - strlen($delimiter)
                    - ($this->delimiterType == PDOSQLExecTask::DELIM_ROW ? 2 : 1)
                );

                return $sql;
            }
        }

        // Catch any statements not followed by ;
        if ($sql !== "") {
            return $sql;
        }

        return null;
    }