# think-soar **Repository Path**: dmming/think-soar ## Basic Information - **Project Name**: think-soar - **Description**: ThinkPHP 5/6 框架的SQL 语句优化器和重写器 - **Primary Language**: PHP - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 11 - **Created**: 2023-11-20 - **Last Updated**: 2023-11-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README
SQL 语句优化器和重写器
 > 适用于 thinkphp5 SQL 语句优化器扩展包,基于 **[guanguans/soar-php](https://github.com/guanguans/soar-php)** [](https://travis-ci.org/guanguans/think-soar) [](https://scrutinizer-ci.com/g/guanguans/think-soar/build-status/master) [](https://scrutinizer-ci.com/g/guanguans/think-soar/?branch=master) [](https://github.styleci.io/repos/195521139) [](https://packagist.org/packages/guanguans/think-soar) [](https://packagist.org/packages/guanguans/think-soar) [](https://packagist.org/packages/guanguans/think-soar) ## 环境要求 * [topthink/framework >= 5.1](https://github.com/top-think/framework) ## 安装 ``` shell $ composer require guanguans/think-soar --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) 拷贝 `config\soar.php` 到 `thinkphp` 配置目录下,修改对应的配置,并设置 `thinkphp` 的 `app_debug`、`trace` 配置为 true 。 ### SQL 评分 **方法调用示例:** ``` php where('id', 1)->select(); $sql = Db::table('fa_user')->fetchSql()->select(); // 最后一条sql语句评分 echo soar_score(); // 指定sql语句评分 echo soar_score($sql); echo soar()->score($sql); } } ``` **输出结果:**  ### explain 信息解读 **方法调用示例:** ``` php where('id', 1)->select(); $sql = Db::table('fa_user')->fetchSql()->select(); // 最后一条sql语句explain信息解读 echo soar_html_explain(); echo soar_md_explain(); // 指定sql语句评分explain信息解读 echo soar_html_explain($sql); echo soar_md_explain($sql); echo soar()->htmlExplain($sql); echo soar()->mdExplain($sql); } } ``` **输出结果:**  ### 语法检查 **方法调用示例:** ``` php $sql = 'selec * from fa_user'; echo soar_syntax_check(); echo soar_syntax_check($sql); 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_finger_print(); echo soar_finger_print($sql); 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()); var_dump(soar_pretty($sql)); var_dump(soar()->pretty($sql)); ``` **输出结果:** ``` sql SELECT * FROM fa_user WHERE id = 1; ``` ### markdown 转化为 html **方法调用示例:** ``` php echo soar_md2html("## 这是一个测试"); echo soar()->md2html("## 这是一个测试"); ``` **输出结果:** ``` html ...