JBZoo\Utils\Str::splitSql PHP Method

splitSql() public static method

Single line or line end comments and multi line comments are stripped off.
public static splitSql ( string $sql ) : array
$sql string Input SQL string with which to split into individual queries.
return array
    public static function splitSql($sql)
    {
        $start = 0;
        $open = false;
        $comment = false;
        $endString = '';
        $end = strlen($sql);
        $queries = array();
        $query = '';
        for ($i = 0; $i < $end; $i++) {
            $current = substr($sql, $i, 1);
            $current2 = substr($sql, $i, 2);
            $current3 = substr($sql, $i, 3);
            $lenEndString = strlen($endString);
            $testEnd = substr($sql, $i, $lenEndString);
            if ($current == '"' || $current == "'" || $current2 == '--' || $current2 == '/*' && $current3 != '/*!' && $current3 != '/*+' || $current == '#' && $current3 != '#__' || $comment && $testEnd == $endString) {
                // Check if quoted with previous backslash
                $num = 2;
                while (substr($sql, $i - $num + 1, 1) == '\\' && $num < $i) {
                    $num++;
                }
                // Not quoted
                if ($num % 2 == 0) {
                    if ($open) {
                        if ($testEnd == $endString) {
                            if ($comment) {
                                $comment = false;
                                if ($lenEndString > 1) {
                                    $i += $lenEndString - 1;
                                    $current = substr($sql, $i, 1);
                                }
                                $start = $i + 1;
                            }
                            $open = false;
                            $endString = '';
                        }
                    } else {
                        $open = true;
                        if ($current2 == '--') {
                            $endString = "\n";
                            $comment = true;
                        } elseif ($current2 == '/*') {
                            $endString = '*/';
                            $comment = true;
                        } elseif ($current == '#') {
                            $endString = "\n";
                            $comment = true;
                        } else {
                            $endString = $current;
                        }
                        if ($comment && $start < $i) {
                            $query = $query . substr($sql, $start, $i - $start);
                        }
                    }
                }
            }
            if ($comment) {
                $start = $i + 1;
            }
            if ($current == ';' && !$open || $i == $end - 1) {
                if ($start <= $i) {
                    $query = $query . substr($sql, $start, $i - $start + 1);
                }
                $query = trim($query);
                if ($query) {
                    if ($i == $end - 1 && $current != ';') {
                        $query = $query . ';';
                    }
                    $queries[] = $query;
                }
                $query = '';
                $start = $i + 1;
            }
        }
        return $queries;
    }

Usage Example

Example #1
0
 public function test()
 {
     $queries = Str::splitSql('SELECT * FROM #__foo;SELECT * FROM #__bar;');
     isSame(array('SELECT * FROM #__foo;', 'SELECT * FROM #__bar;'), $queries);
     $queries = Str::splitSql('
         ALTER TABLE `#__redirect_links` DROP INDEX `idx_link_old`;
         -- Some comment
         ALTER TABLE `#__redirect_links` MODIFY `old_url` VARCHAR(2048) NOT NULL;
         -- Some comment
         -- Some comment --
         ALTER TABLE `#__redirect_links` MODIFY `new_url` VARCHAR(2048) NOT NULL;
         -- Some comment
         ALTER TABLE `#__redirect_links` MODIFY `referer` VARCHAR(2048) NOT NULL;
         
         ALTER TABLE `#__redirect_links` ADD INDEX `idx_old_url` (`old_url`(100));
     ');
     isSame(array('ALTER TABLE `#__redirect_links` DROP INDEX `idx_link_old`;', 'ALTER TABLE `#__redirect_links` MODIFY `old_url` VARCHAR(2048) NOT NULL;', 'ALTER TABLE `#__redirect_links` MODIFY `new_url` VARCHAR(2048) NOT NULL;', 'ALTER TABLE `#__redirect_links` MODIFY `referer` VARCHAR(2048) NOT NULL;', 'ALTER TABLE `#__redirect_links` ADD INDEX `idx_old_url` (`old_url`(100));'), $queries);
 }