soar-php
简体中文 | ENGLISH
SQL 优化器、重写器
环境要求
- PHP >= 7.1
- ext-pdo
框架中使用
- Laravel - laravel-soar, laravel-web-soar
- ThinkPHP - think-soar
- Hyperf - hyperf-soar
- Yii2
- Symfony
- Slim
安装
$ composer require guanguans/soar-php -vvv
使用
XiaoMi 开源的 SQL 优化器 soar,更多详细安装请参考 soar install(如果不使用自定义的 soar 路径,这一步请忽略)
下载# macOS
$ wget https://github.com/XiaoMi/soar/releases/download/0.11.0/soar.darwin-amd64
# linux
$ wget https://github.com/XiaoMi/soar/releases/download/0.11.0/soar.linux-amd64
# windows
$ wget https://github.com/XiaoMi/soar/releases/download/0.11.0/soar.windows-amd64
# 用其他命令或下载器下载均可以
$ chmod +x soar.* # 添加可执行权限
soar.config.example、soar.config
初始化配置,更多详细配置请参考<?php
require __DIR__.'/vendor/autoload.php';
use Guanguans\SoarPHP\Soar;
$config = [
// 包自带soar 路径或者自定义的 soar 路径
'-soar-path' => OsHelper::isWindows() ? __DIR__.'\vendor\guanguans\soar-php\bin\soar.windows-amd64' : (OsHelper::isMacOS() ? __DIR__.'/vendor/guanguans/soar-php/bin/soar.darwin-amd64' : __DIR__.'/vendor/guanguans/soar-php/bin/soar.linux-amd64'),
// '-soar-path' => __DIR__.'/vendor/guanguans/soar-php/bin/soar.linux-amd64',
// 测试环境配置
'-test-dsn' => [
'host' => '127.0.0.1',
'port' => '3306',
'dbname' => 'database',
'username' => 'root',
'password' => '123456',
'disable' => false,
],
// 日志输出文件
'-log-output' => __DIR__.'/logs/soar.log',
// 报告输出格式: [markdown, html, json, ...]
'-report-type' => 'html',
];
$soar = new Soar($config);
SQL 评分
方法调用:
$sql ="SELECT * FROM `fa_user` `user` LEFT JOIN `fa_user_group` `group` ON `user`.`group_id`=`group`.`id`;";
echo $soar->score($sql);
输出结果:
explain 信息解读
方法调用:
$sql = "SELECT * FROM `fa_auth_group_access` `aga` LEFT JOIN `fa_auth_group` `ag` ON `aga`.`group_id`=`ag`.`id`;";
// 输出 html 格式
echo $soar->htmlExplain($sql);
// 输出 md 格式
echo $soar->mdExplain($sql);
// 输出 html 格式
echo $soar->explain($sql, 'html');
// 输出 md 格式
echo $soar->explain($sql, 'md');
输出结果:
语法检查
方法调用:
$sql = 'selec * from fa_user';
echo $soar->syntaxCheck($sql);
输出结果:
At SQL 1 : line 1 column 5 near "selec * from fa_user" (total length 20)
SQL 指纹
方法调用:
$sql = 'select * from fa_user where id=1';
echo $soar->fingerPrint($sql);
输出结果:
select * from fa_user where id = ?
SQL 美化
方法调用:
$sql = 'select * from fa_user where id=1';
var_dump($soar->pretty($sql));
输出结果:
SELECT
*
FROM
fa_user
WHERE
id = 1;
markdown 转化为 html
方法调用:
echo $soar->md2html("## 这是一个测试");
输出结果:
...
<h2>这是一个测试</h2>
...
soar 帮助
方法调用:
var_dump($soar->help());
输出结果:
···
'Usage of /Users/yaozm/Documents/wwwroot/soar-php/soar:
-allow-charsets string
AllowCharsets (default "utf8,utf8mb4")
-allow-collates string
AllowCollates
-allow-drop-index
AllowDropIndex, 允许输出删除重复索引的建议
-allow-engines string
AllowEngines (default "innodb")
-allow-online-as-test
AllowOnlineAsTest, 允许线上环境也可以当作测试环境
-blacklist string
指定 blacklist 配置文件的位置,文件中的 SQL 不会被评审。
···
执行任意 soar 命令
方法调用:
$command = "echo '## 这是另一个测试' | /Users/yaozm/Documents/wwwroot/soar-php/soar.darwin-amd64 -report-type md2html";
echo $soar->exec($command);
输出结果:
...
<h2>这是另一个测试</h2>
...
Contributors ✨
Thanks goes to these wonderful people (emoji key):
kamly 🐛 |
Leslie Lau 🐛 |
D.J.Hwang 🤔 |
海彬 🐛 |
imcm 🤔 |
This project follows the all-contributors specification. Contributions of any kind welcome!