laratoolbox/query-viewer

获取已替换绑定值的 SQL 查询

v0.1.0 2020-11-30 10:06 UTC

This package is auto-updated.

Last update: 2024-09-18 17:02:15 UTC


README

Social Image

GitHub Workflow Status Packagist Packagist GitHub issues

包描述

此包为 eloquent 和数据库查询构建器添加自定义方法,以获取 SQL 查询。(问号被值替换)

Laravel 的 toSql() 方法给您提供未替换绑定的 SQL 查询。(见下面的问号)

\DB::table('users')->select('name')->where('id', 5)->toSql();

// select `name` from `users` where `id` = ? and `name` = ?

使用此包,您拥有 getSql() 方法。该方法提供相同的带有绑定值替换的结果。

\DB::table('users')->select('name')->where('id', 5)->getSql();

// select `name` from `users` where `id` = 5 and `name` = 'laravel'

要求

Laravel >= 5.5

构建历史

Build History

安装

通过 composer 安装

$ composer require laratoolbox/query-viewer

发布包配置

$ php artisan vendor:publish --provider="LaraToolbox\QueryViewer\QueryViewerServiceProvider"

用法

安装此包后,您可以在 eloquent 和数据库构建器上使用这些方法。

  • getSql

    • 此方法返回 SQL 查询。
    • 可选地接受闭包作为参数,并使用 SQL 字符串调用闭包。
    • 返回字符串或闭包结果。
  • dumpSql

    • 此方法打印 SQL 查询(使用 dump() 函数)
    • 返回构建器。
  • logSql

    • 此方法记录 SQL 查询。
    • 可选地接受前缀字符串参数。将其添加到 SQL 字符串前。
    • 日志类型可以在配置文件中设置(默认为 "info")。
    • 返回构建器。

示例

Eloquent

use App\Models\User;

User::select('name')->where('id', 5)->getSql();
// 'select `name` from `users` where `id` = 5'

User::select('name')
    ->where('id', 5)
    ->dumpSql()
    // PRINTS: select `name` from `users` where `id` = 5
    ->logSql('LOG_PREFIX_HERE') // logs sql to log file. (LOG_PREFIX_HERE : select `name` from `users` where `id` = 5)
    ->where('name', '!=', 'john')
    ->dumpSql()
    // PRINTS: select `name` from `users` where `id` = 5 and `name` != 'john'
    ->where('surname', '!=', 'doe')
    ->where('email', 'LIKE', '%example%')
    ->getSql(function(string $sql) {
        echo $sql;
        // select `name` from `users` where `id` = 5 and `name` != 'john' and `surname` != 'doe' and `email` LIKE '%example%'
    })
    ->getSql();
    // PRINTS: select `name` from `users` where `id` = 5 and `name` != 'john' and `surname` != 'doe' and `email` LIKE '%example%'

数据库构建器

\DB::table('users')->select('name')->where('id', 5)->getSql();
// 'select `name` from `users` where `id` = 5'

\DB::table('users')
    ->where('id', 5)
    ->dumpSql()
    // PRINTS: select `name` from `users` where `id` = 5
    ->logSql('LOG_PREFIX_HERE') // logs sql to log file. (LOG_PREFIX_HERE : select `name` from `users` where `id` = 5)
    ->where('name', '!=', 'john')
    ->dumpSql()
    // PRINTS: select `name` from `users` where `id` = 5 and `name` != 'john'
    ->where('surname', '!=', 'doe')
    ->where('email', 'LIKE', '%example%')
    ->getSql(function(string $sql) {
        echo $sql;
        // select `name` from `users` where `id` = 5 and `name` != 'john' and `surname` != 'doe' and `email` LIKE '%example%'
    })
    ->getSql();
    // PRINTS: select `name` from `users` where `id` = 5 and `name` != 'john' and `surname` != 'doe' and `email` LIKE '%example%'

替换所有查询的绑定

将以下代码添加到 app/Providers/AppServiceProvider.php 文件中,以监听所有查询。

use LaraToolbox\QueryViewer\QueryViewer;

\DB::listen(function ($query) {
    $sql = QueryViewer::replaceBindings($query->sql, $query->bindings);

    logger()->info($sql);
});

测试

$ composer test

变更日志

请参阅 CHANGELOG 了解最近更改的详细信息。

安全性

如果您发现任何与安全相关的问题,请通过电子邮件而不是使用问题跟踪器。

贡献

请参阅 CONTRIBUTING 了解详细信息。

致谢

许可证

MIT 许可证(MIT)。请参阅 许可证文件 了解更多信息。