4 Star 29 Fork 7

guanguans / soar-php

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
Clone or download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README-EN.md

soar-php

简体中文 | ENGLISH

SQL statement optimizer and rewriter

soar-php is a PHP extension package based on Xiaomi's open source soar development. It is a SQL statement tuning development tool for PHP engineers.

Build Status Build Status Scrutinizer Code Quality codecov StyleCI Total Downloads Latest Stable Version License

Requirements

  • PHP >= 7.1
  • ext-pdo

Used in the framework

Installation

$ composer require guanguans/soar-php --dev

Usage

Download XiaoMi open source SQL optimizer soar, please refer to soar install for more detailed installation(If you do not use a custom ear path, ignore this step)

# 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
# Download with other commands or downloader

Initial configuration, please refer to soar config for more detailed configuration

<?php

require __DIR__.'/vendor/autoload.php';

use Guanguans\SoarPHP\Soar;

$config = [
    // The package comes with a soar path OR a custom soar path
    '-soar-path' => OsHelper::isWindows() ? 'vendor/guanguans/soar-php/bin/soar.windows-amd64' : (OsHelper::isMacOS() ? 'vendor/guanguans/soar-php/bin/soar.darwin-amd64' : 'vendor/guanguans/soar-php/bin/soar.linux-amd64'),
    // '-soar-path' => '/Users/yaozm/Documents/wwwroot/soar-php/soar.darwin-amd64',
    // Test environment configuration
    '-test-dsn' => [
        'host' => '127.0.0.1',
        'port' => '3306',
        'dbname' => 'database',
        'username' => 'root',
        'password' => '123456',
    ],
    // log output file
    '-log-output' => './soar.log',
    // Report output format: default markdown [markdown, html, json]
    '-report-type' => 'html',
];
$soar = new Soar($config);

SQL score

Method call:

$sql ="SELECT * FROM `fa_user` `user` LEFT JOIN `fa_user_group` `group` ON `user`.`group_id`=`group`.`id`;";
echo $soar->score($sql);

Output results:

explain information

Method call:

$sql = "SELECT * FROM `fa_auth_group_access` `aga` LEFT JOIN `fa_auth_group` `ag` ON `aga`.`group_id`=`ag`.`id`;";
// Output html format
echo $soar->htmlExplain($sql);
// Output markdown format
echo $soar->mdExplain($sql);
// Output html format
echo $soar->explain($sql, 'html');
// Output markdown format
echo $soar->explain($sql, 'md');

Output results:

Grammar check

Method call:

$sql = 'selec * from fa_user';
echo $soar->syntaxCheck($sql);

Output results:

At SQL 1 : line 1 column 5 near "selec * from fa_user" (total length 20)

SQL fingerprint

Method call:

$sql = 'select * from fa_user where id=1';
echo $soar->fingerPrint($sql);

Output results:

select * from fa_user where id = ?

SQL pretty

Method call:

$sql = 'select * from fa_user where id=1';
var_dump($soar->pretty($sql));

Output results:

SELECT  
  * 
FROM  
  fa_user  
WHERE  
  id  = 1;

Markdown to html

Method call:

echo $soar->md2html("## this is a test");

Output results:

...
<h2>this is a test</h2>
...

Soar help

Method call:

var_dump($soar->help());

Output results:

···
'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 不会被评审。
···    

Execute any soar command

Method call:

$command = "echo '## 这是另一个测试' | /Users/yaozm/Documents/wwwroot/soar-php/soar.darwin-amd64 -report-type md2html";
echo $soar->exec($command);

Output results:

...
<h2>This is another test'</h2>
...

Contributors ✨

Thanks goes to these wonderful people (emoji key):

kamly
kamly

🐛
Leslie Lau
Leslie Lau

🐛
D.J.Hwang
D.J.Hwang

🤔

This project follows the all-contributors specification. Contributions of any kind welcome!

Reference link

License

MIT

Comments ( 0 )

Sign in for post a comment

About

SQL optimizer and rewriter php extension package - SQL优化器和重写器PHP扩展包、方便框架中SQL调优。 spread retract
Cancel

Releases

No release

soar-php

Contributors

All

Activities

load more
can not load any more
PHP
1
https://gitee.com/guanguans/soar-php.git
git@gitee.com:guanguans/soar-php.git
guanguans
soar-php
soar-php
master

Search