# soar-php **Repository Path**: hackchen/soar-php ## Basic Information - **Project Name**: soar-php - **Description**: SQL optimizer and rewriter php extension package - SQL优化器和重写器PHP扩展包、方便框架中SQL调优。 - **Primary Language**: PHP - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 8 - **Created**: 2020-11-17 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README
SQL 语句优化器和重写器
> **[soar-php](https://github.com/guanguans/soar-php)** 是一个基于小米公司开源的 [soar](https://github.com/XiaoMi/soar) 开发的 PHP 扩展包,方便框架中 SQL 语句调优。 [](https://travis-ci.org/guanguans/soar-php) [](https://scrutinizer-ci.com/g/guanguans/soar-php/build-status/master) [](https://scrutinizer-ci.com/g/guanguans/soar-php/?branch=master) [](https://codecov.io/gh/guanguans/soar-php) [](https://github.styleci.io/repos/178793017) [](https://packagist.org/packages/guanguans/soar-php) [](https://packagist.org/packages/guanguans/soar-php) [](https://packagist.org/packages/guanguans/soar-php) ## 环境要求 * PHP >= 7.1 * ext-pdo ## 框架中使用 - [x] Laravel - [laravel-web-soar](https://github.com/huangdijia/laravel-web-soar) - [x] ThinkPHP - [think-soar](https://github.com/guanguans/think-soar) - [x] Hyperf - [hyperf-soar](https://github.com/wilbur-oo/hyperf-soar) - [ ] Yii2 - [ ] Symfony - [ ] Slim ## 安装 ``` shell $ composer require guanguans/soar-php --dev ``` ## 使用 ### 下载 [XiaoMi](https://github.com/XiaoMi/) 开源的 SQL 优化器 [soar](https://github.com/XiaoMi/soar/releases),更多详细安装请参考 [soar install](https://github.com/XiaoMi/soar/blob/master/doc/install.md) ``` bash # 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 # 用其他命令或下载器下载均可以 ``` ### 初始化配置,更多详细配置请参考 [soar config](https://github.com/XiaoMi/soar/blob/master/doc/config.md) #### 方法一、运行时初始化配置 ``` php '/Users/yaozm/Documents/wwwroot/soar-php/soar.darwin-amd64', // 测试环境配置 '-test-dsn' => [ 'host' => '127.0.0.1', 'port' => '3306', 'dbname' => 'database', 'username' => 'root', 'password' => '123456', ], // 日志输出文件 '-log-output' => './soar.log', // 报告输出格式: 默认 markdown [markdown, html, json] '-report-type' => 'html', ]; $soar = new Soar($config); ``` #### 方法二、配置文件初始化配置 `vendor` 同级目录下新建 `.soar.dist` 或者 `.soar`,内容参考 [.soar.example](.soar.example),例如: ``` php '/Users/yaozm/Documents/wwwroot/soar-php/soar.darwin-amd64', // 测试环境配置 '-test-dsn' => [ 'host' => '127.0.0.1', 'port' => '3306', 'dbname' => 'database', 'username' => 'root', 'password' => '123456', ], // 日志输出文件 '-log-output' => './soar.log', // 报告输出格式: 默认 markdown [markdown, html, json] '-report-type' => 'html', ]; ``` 然后初始化 ``` php .soar > .soar.dist ### SQL 评分 **方法调用:** ``` php $sql ="SELECT * FROM `fa_user` `user` LEFT JOIN `fa_user_group` `group` ON `user`.`group_id`=`group`.`id`;"; echo $soar->score($sql); ``` **输出结果:**  ### explain 信息解读 **方法调用:** ``` php $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'); ``` **输出结果:**  ### 语法检查 **方法调用:** ``` php $sql = 'selec * from fa_user'; echo $soar->syntaxCheck($sql); ``` **输出结果:** ``` sql At SQL 1 : line 1 column 5 near "selec * from fa_user" (total length 20) ``` ### SQL 指纹 **方法调用:** ``` php $sql = 'select * from fa_user where id=1'; echo $soar->fingerPrint($sql); ``` **输出结果:** ``` sql select * from fa_user where id = ? ``` ### SQL 美化 **方法调用:** ``` php $sql = 'select * from fa_user where id=1'; var_dump($soar->pretty($sql)); ``` **输出结果:** ``` sql SELECT * FROM fa_user WHERE id = 1; ``` ### markdown 转化为 html **方法调用:** ``` php echo $soar->md2html("## 这是一个测试"); ``` **输出结果:** ``` html ...kamly 🐛 |
Leslie Lau 🐛 |
D.J.Hwang 🤔 |