PHP code example of wolfans / usql-parser

1. Go to this page and download the library: Download wolfans/usql-parser library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wolfans / usql-parser example snippets



/**
 * 收集项目信息
 *1、同一进程多次调用该方法,内部逻辑仅把参数存放到变量中,未请求远端接口
 *2、同一进程中最多收集100条记录。
 *3、代码销毁时,在析构函数中,批量上传sql信息
 *
 * @param string $dbName 库名(必传)
 * @param string $query SQL语句(必传)(如select id from table_a where id=? 格式,如果不能拆分参数可以写整个sql语句)
 * @param string $bindings(必传) $query对应的绑定信息json格式,如果没有填写{}
 * @param int $takeTime(必传) 执行sql消耗时间(单位:毫秒)
 * @param int $execTime(必传) 程序执行时间(时间戳)
 * @param string $projectName(必传) 项目名称
 * @return boolean
 */
 SqlStandard::instance()->collect($dbName, $query, $bindings, $takeTime, $execTime, $projectName);

$app->register(App\Providers\EventServiceProvider::class);

protected $listen = [
      'illuminate.*'=>[
           'App\Listeners\SqlQueryListener'
      ]
];


namespace App\Listeners;

use USQL\SqlStandard;

class SqlQueryListener extends Listener
{
    //
    public function __construct()
    {}

    public function handle($query, $bindings, $time, $name)
    {
        if(isset($_SERVER['SITE_ENV']) && $_SERVER['SITE_ENV'] == 'testing'){
            //库名需要手动录入
            $dbName='xin_insurance';
            //sql对应参数
            $bindings=json_encode($bindings);
            //sql消耗时间
            $takeTime=0;
            //sql执行时间
            $execTime=time();
            $pname='financialapi.youxinjinrong.com';
            SqlStandard::instance()->collect($dbName, $query, $bindings, $takeTime, $execTime, $projectName);
        }
    }
}

$app->register(App\Providers\EventServiceProvider::class);

protected $listen = [
     'Illuminate\Database\Events\QueryExecuted' => [
          'App\Listeners\SqlQueryListener'
     ]
];



namespace App\Listeners;

use App\Events\ExampleEvent;
use Illuminate\Database\Events\QueryExecuted;
use USQL\SqlStandard;

class SqlQueryListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {}

    /**
     * Handle the event.
     *
     * @param  ExampleEvent  $event
     * @return void
     */
    public function handle(QueryExecuted  $event)
    {
        if(isset($_SERVER['SITE_ENV']) && $_SERVER['SITE_ENV'] == 'testing'){
            $dbName=$event->connection->getConfig('database');
            $query=$event->sql;
            $bindings=json_encode($event->bindings);
            $takeTime=$event->time;
            $execTime=time();
            $projectName='lifeapi.uxincredit.com';
            SqlStandard::instance()->collect($dbName, $query, $bindings, $takeTime, $execTime, $projectName);
        }
    }
}