xp-forge/sql-parser

v0.4.0 2024-03-24 13:26 UTC

This package is auto-updated.

Last update: 2024-08-24 14:24:41 UTC


README

Build status on GitHub XP Framework Module BSD Licence Requires PHP 7.0+ Supports PHP 8.0+ Latest Stable Version

此库解析 SQL 语句。

示例

use text\sql\{Parser, SyntaxError};

$p= new Parser();
try {
  $statement= $p->parse('select * from user where user_id = 1');
} catch (SyntaxError $e) {
  // Handle
}

// new Select(
//   [new All()],
//   [new Table('user')],
//   new Comparison(new Field(null, 'uid'), '=', new Number(1))
// )

支持

此库尚未完成。目前支持以下功能

  • USE 数据库选择
  • SELECT、INSERT、UPDATE 和 DELETE 语句
  • CREATE / DROP TABLE 模式修改
  • ALTER TABLE ADD / DROP COLUMN 表修改

其他语句可以通过 extend() 添加

use text\sql\Parser;

$p= new Parser();

// Incomplete implementation of https://dev.mysqlserver.cn/doc/refman/8.0/en/show.html
$p->extend('show', function($parse, $token) {
  return ['show' => $parse->match([
    'events'    => function($parse, $token) { return 'events'; },
    'variables' => function($parse, $token) {
      if ('like' === $parse->token->symbol->id) {
        $parse->forward();
        return ['variables' => $parse->expression()];
      } else {
        return ['variables' => null];
      }
    }
  ])];
});

$statement= $p->parse('show variables like "sql_mode"');

// ['show' => ['variables' => new Text('sql_mode')]]