4

我有一个超过 3gb 的 sql 转储文件,其中包含多个查询。我想使用 php 将文件解析为查询。

根据网站用户的推荐,我使用https://code.google.com/p/php-sql-parser/进行解析。问题是只能使用此方法检索第一个查询。

有没有人有什么建议?

转储文件是来自服务器的数据的唯一遗物。

谢谢!

4

4 回答 4

1

我真的找到了你想要的:

SQL Parser:解析 SQL 文件并提取查询语句((通过 phpclasses.org))

示例 sql 文件

#这是一条评论 :)

select * from table1 where a=1;

--select 评论 2
选择 * 从
    表2
    其中a=2;

#插入值
将忽略插入`versions`(`release`,`revision`,`name`,`lastupdate`)值('1','0','sqlparser',now());

#创建表
创建表`测试`(
  `id` int(11) unsigned not null auto_increment,
  `name` varchar(80) not null default '',
  主键(`id`)
) 引擎=innodb 默认字符集=utf8;

#海关操作
插入`tests`(`id`,`name`) 值('1','test ; value');
update `test` set `name`='test value update' where `id`='1';

输出:

大批 (
  0 => 'SELECT * FROM table1 WHERE a=1',
  1 => 'SELECT * FROM table2 where a=2',
  2 => 'INSERT IGNORE INTO `VERSIONS`(`release`,`revision`,`name`,`lastUpdate`) 值 (\'1\',\'0\',\'SqlParser\',NOW() )',
  3 => 'CREATE TABLE `TESTS` (`Id` int(11) unsigned NOT NULL AUTO_INCREMENT, `Name` varchar(80) NOT NULL DEFAULT \'\', PRIMARY KEY (`Id`)) ENGINE=InnoDB 默认字符集=utf8',
  4 => 'INSERT INTO `TESTS`(`Id`,`Name`) VALUES (\'1\',\'test ; value\')',
  5 => 'UPDATE `TEST` SET `Name`=\'测试值更新\' WHERE `Id`=\'1\'',
)

这门课的真正美妙之处在于它可以计算很多东西,比如评论、多行和查询,其中他的值包括“;” 特点。

于 2013-11-12T20:38:52.060 回答
0

http://pastebin.com/GxBahnyM - 我从 Typo3 CMS 得到它。Typo3 安装扩展时使用

于 2013-04-18T16:25:58.247 回答
0

这是“Principe Orazio”类的改进版本(@AgelessEssence 已经提到过)。

class sql_parser
{
    public static function parse($content) {
        $sql_list = [];
        $query = "";
        $lines = explode("\n", $content);
        foreach ($lines as $sql_line){
            $sql_line = trim($sql_line);
            if(($sql_line === "") || (strpos($sql_line, "--") === 0) || (strpos($sql_line, "#") === 0)){
                continue;
            }

            $query .= $sql_line;
            // Checking whether the line is a valid statement
            if (preg_match("/(.*);$/", $sql_line)) {
                $query = trim($query);
                $query = substr($query, 0, strlen($query) - 1);

                $sql_list[] = sql_parser::remove_query_comments($query);
                //reset the variable
                $query = "";
            }
        }

        return $sql_list;
    }

    public static function remove_query_comments($query)
    {
         $sql_comments = '@
              (([\'"]).*?[^\\\]\2) # $1 : Skip single & double quoted expressions
              |(                   # $3 : Match comments
                  (?:\#|--).*?$    # - Single line comments
                  |                # - Multi line (nested) comments
                   /\*             #   . comment open marker
                      (?: [^/*]    #   . non comment-marker characters
                          |/(?!\*) #   . ! not a comment open
                          |\*(?!/) #   . ! not a comment close
                          |(?R)    #   . recursive case
                      )*           #   . repeat eventually
                  \*\/             #   . comment close marker
              )\s*                 # Trim after comments
              |(?<=;)\s+           # Trim after semi-colon
              @msx';


        $query = trim( preg_replace( $sql_comments, '$1', $query ) );

        // Remove the last ;
        if(strrpos($query, ";") === strlen($query) - 1) {
            $query = substr($query, 0, strlen($query) - 1);
        }

        return $query;
    }
}

要使用它:

$sql_lists = sql_parser::parse(file_get_contents("test.sql"));
print_r($sql_lists);
于 2021-10-24T10:30:26.590 回答
-2

是否可以使用 "explode(";", $dumpfile) 拆分转储文件,然后执行循环调用该函数进行解析?您也可以使用其他分隔符进行爆炸。是否可能存在不平衡的引号?

问候

副歌

于 2013-04-18T15:36:58.653 回答