Skip to content

Commit

Permalink
Merge pull request #88 from socialblue/develop
Browse files Browse the repository at this point in the history
Release 1.0.0
  • Loading branch information
mbroersen committed Aug 18, 2021
2 parents d6b00f9 + 5dda07f commit e75d938
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 78 deletions.
125 changes: 64 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,13 @@
# Laravel-Query-Adviser

[![Latest Version on Packagist](https://img.shields.io/packagist/v/socialblue/laravel-query-adviser.svg?style=flat-square)](https://packagist.org/packages/socialblue/laravel-query-adviser)
[![Build Status](https://img.shields.io/travis/socialblue/laravel-query-adviser/master.svg?style=flat-square)](https://travis-ci.org/socialblue/laravel-query-adviser)
[![Quality Score](https://img.shields.io/scrutinizer/g/socialblue/laravel-query-adviser.svg?style=flat-square)](https://scrutinizer-ci.com/g/socialblue/laravel-query-adviser)
[![Total Downloads](https://img.shields.io/packagist/dt/socialblue/laravel-query-adviser.svg?style=flat-square)](https://packagist.org/packages/socialblue/laravel-query-adviser)

With Laravel Query Adviser you can get more insights into the SQL queries created by Eloquent models used in your app.
It logs the queries used by your application and provides a helpful overview of the SQL queries. You can rerun the SQL query to analyze the database impact or copy the SQL query to your clipboard.

The handy card view allows you to quickly point out the pain points of your application's SQL queries.

goto **{app_url}/query-adviser/**
> ![Query Overview](./img/stepper.png)
## Session list
> ![Query Overview](./img/session-list.png)
## Query time line
> ![Query Overview](./img/overview.png)
collapse and expand the panels for less or more information about the queries executed in that time frame.

**Groups**

You can group the queries by time, routes and rawQuery.
Also you can clear the cached queries by pressing the eject button.
> group by time, routes, referer, raw queries, queries with bindings, and query time
![Query groups](img/group-by-new.png)

**Sort**
> Sort the time line by last inserted, slowest query, and most occurrences
![Query Card](./img/sorting.png)

**Query card**
> Re-execute a query, get query information, and copy the query to your clipboard
![Query Card](./img/card.png)

**Query labels**
As of version 0.10.0, the query card has labels related to the class,
file and function of the query executed from your app folder.

> ![Query labels](./img/labels.png)
Open the explain dialog to see more information about the query.

**Query information**
> ![Query Card](./img/query-information.png)
**Re-execute Query**
> ![Query Card](./img/query-execute.png)

## Installation

You can install the package via composer:
Expand All @@ -73,40 +30,86 @@ php artisan vendor:publish --provider="Socialblue\LaravelQueryAdviser\LaravelQue

## Usage

Dump and die
``` php
### Output queries inline
> Dump and die
> ``` php
> User::join('posts', 'posts.users_id', '=', 'users.id')
> ->select([DB::raw('SUM(posts.id)')])
> ->where('users.id', 1)
> ->select([DB::raw('SUM(post.id)')])->qadd();
> ```
> or just dump
> ``` php
>
> User::join('posts', 'posts.users_id', '=', 'users.id')
> ->select([DB::raw('SUM(posts.id)')])
> ->where('users.id', 1)
> ->select([DB::raw('SUM(post.id)')])->qadump();
>
> ```
> both dump this array
>
> ```php
> [
> 'toSql' => 'select sum(posts.id) from users join posts on posts.users_id = users.id where users.id = ?'
> 'bindings' => [1]
> 'query' => 'select sum(posts.id) from users join posts on posts.users_id = users.id where users.id = 1'
> ]
> ```
### Start query logging session
User::join('post', 'post.user_id', '=', 'user.id')
->select([DB::raw('SUM(post.id)')])->dd();
1. To start a query log session goto {app_url}/query-adviser/
2. Press play and open the pages of your app you want to log the queries of
3. Stop the session and click on the session bar to see all the details.
```
> #### Session stepper
> ![Query Overview](./img/stepper.png)
Just dump
``` php
> #### Session list
> ![Query Overview](./img/session-list.png)
User::join('post', 'post.user_id', '=', 'user.id')
->select([DB::raw('SUM(post.id)')])->dump();
### Brows through session overview
```
> #### Query time-line
> *collapse and expand the panels for less or more information about the queries executed in that time frame.*
> ![Query Overview](./img/overview.png)
#### Groups
You can group the queries by time, routes and rawQuery.
Also you can clear the cached queries by pressing the eject button.
> *group by time, routes, referer, raw queries, queries with bindings, and query time*
![Query groups](img/group-by-new.png)
### Start query logging
#### Sort
> *Sort the time line by last inserted, slowest query, and most occurrences*
![Query Card](./img/sorting.png)
1. To start a query log session goto {app_url}/query-adviser/
2. Press play and open the pages of your app you want to log the queries of
3. Stop the session and click on the session bar to see all the details.
#### Query card
> *Re-execute a query, get query information, and copy the query to your clipboard*
![Query Card](./img/card.png)
#### Query labels
As of version 0.10.0, the query card has labels related to the class,
file and function of the query executed from your app folder.
> *Query labels*
> ![Query labels](./img/labels.png)
Open the explain dialog to see more information about the query.
> *Query information*
> ![Query Card](./img/query-information.png)
> *Re-execute Query*
> ![Query Card](./img/query-execute.png)
### Testing
``` bash
composer test
```
### Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
Expand Down
6 changes: 5 additions & 1 deletion config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@
],
'session_max_time' => env('QUERY_ADVISER_CACHE_SESSION_MAX_TIME', 120),
],
'macros' => [
'dd' => 'qadd',
'dump' => 'qadump',
],
'enable_query_logging' => env('QUERY_ADVISER_ENABLE_QUERY_LOGGING', true)
];
];
20 changes: 7 additions & 13 deletions src/Helper/QueryBuilderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,14 @@ public static function analyzeByBuilder($builder)
public static function analyze($sql, $bindings)
{
$query = ['sql' => $sql, 'bindings' => $bindings];
$queryData = DB::connection()->select('EXPLAIN '. $sql, $bindings);
$queryData = DB::connection()->select('EXPLAIN '. $sql, $bindings);

//todo use
// $ws = DB::connection()->select("SHOW WARNINGS");
// $sqlOptimized = $ws->fetchColumn(2);
DB::connection()->getPdo()->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true);
$showWarnings = DB::connection()->getPdo()->query("SHOW WARNINGS");
$sqlOptimized = $showWarnings->fetchColumn(2);
DB::connection()->getPdo()->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);

return ['queryParts' => $queryData, 'query' => $query, 'optimized' => ""];
return ['queryParts' => $queryData, 'query' => $query, 'optimized' => $sqlOptimized];
}

/**
Expand All @@ -120,14 +121,7 @@ public static function analyze($sql, $bindings)
*/
public static function showOptimizedQueryByBuilder($builder)
{
self::analyzeByBuilder($builder);

//todo fix to use
// $ws = DB::connection()->getPdo()->prepare("SHOW WARNINGS");
// $ws->execute()
// return $ws->fetchColumn(2);

return [];
return self::analyzeByBuilder($builder)['optimized'];
}

/**
Expand Down
13 changes: 10 additions & 3 deletions src/LaravelQueryAdviserServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Socialblue\LaravelQueryAdviser;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;
Expand Down Expand Up @@ -63,11 +62,19 @@ protected function bootLaravelQueryAdviser()
QueryListener::listen($query);
});

Builder::macro('dd', function () {
\Illuminate\Database\Eloquent\Builder::macro('laravel-query-adviser.macros.dd', function () {
dd(QueryBuilderHelper::infoByBuilder($this));
});

Builder::macro('dump', function () {
\Illuminate\Database\Query\Builder::macro('laravel-query-adviser.macros.dd', function () {
dd(QueryBuilderHelper::infoByBuilder($this));
});

\Illuminate\Database\Eloquent\Builder::macro(config('laravel-query-adviser.macros.dump'), function () {
dump(QueryBuilderHelper::infoByBuilder($this));
});

\Illuminate\Database\Query\Builder::macro(config('laravel-query-adviser.macros.dump'), function () {
dump(QueryBuilderHelper::infoByBuilder($this));
});
}
Expand Down

0 comments on commit e75d938

Please sign in to comment.