1. Go to this page and download the library: Download shimabox/laqu 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/ */
shimabox / laqu example snippets
use App\Repositories\ExampleRepository; // example.
use Laqu\QueryAssertion;
use Tests\TestCase;
class QueryAssertionTest extends TestCase
{
use QueryAssertion;
private $exampleRepository;
protected function setUp(): void
{
parent::setUp();
$this->exampleRepository = new ExampleRepository();
}
public function queryTest()
{
// 基本的な使い方
$this->assertQuery(
// クエリが実行される処理をクロージャに渡します
fn () => $this->exampleRepository->findById('a123'),
// 期待するクエリを書きます
'select from user where id = ? and is_active = ?',
// バインドされる値を配列で定義します
// (bindするものがない場合は空配列を渡すか、引数は渡さないでOK)
[
'a123',
1,
]
);
// 複数のクエリを確認
// 基本的には 1メソッド1クエリの確認 を推奨しますが、中には1メソッドで複数クエリが流れる場合もあると思います。
// その場合は下記のようにクエリとバインド値を配列で対になるように定義してください。
$this->assertQuery(
// 例えばこの処理で複数のクエリが流れるとします
fn () => $this->exampleRepository->findAll(),
// 期待するクエリをそれぞれ配列で定義します
[
'select from user where is_active = ?', // ※1
'select from admin_user where id = ? and is_active = ?', // ※2
'select from something', // ※3
],
// バインドされる値を二次元配列で定義(bindするものがない場合は空配列を渡してください)
[
[ // ※1.
1,
],
[ // ※2.
'b123',
1,
],
// ※3 はバインド無しの想定なので空配列を渡します
[],
]
);
}
}
use Laqu\Facades\QueryAnalyzer;
/** @var Laqu\Analyzer\QueryList **/
$analyzed = QueryAnalyzer::analyze(function () {
$author = Author::find(1);
$author->delete();
});
/*
Laqu\Analyzer\QueryList {#345
-queries: array:2 [
0 => Laqu\Analyzer\Query {#344
-query: "select * from "authors" where "authors"."id" = ? limit 1"
-bindings: array:1 [
0 => 1
]
-time: 0.08
-buildedQuery: "select * from "authors" where "authors"."id" = 1 limit 1"
}
1 => Laqu\Analyzer\Query {#337
-query: "delete from "authors" where "id" = ?"
-bindings: array:1 [
0 => "1"
]
-time: 0.03
-buildedQuery: "delete from "authors" where "id" = '1'"
}
]
}
*/
dump($analyzed);
// select * from "authors" where "authors"."id" = 1 limit 1
echo $analyzed[0]->getBuildedQuery();
// delete from "authors" where "id" = '1'
echo $analyzed[1]->getBuildedQuery();
/*
Laqu\Analyzer\Query {#337
-query: "delete from "authors" where "id" = ?"
-bindings: array:1 [
0 => "1"
]
-time: 0.03
-buildedQuery: "delete from "authors" where "id" = '1'"
}
*/
dump($analyzed->extractFastestQuery());
/*
Laqu\Analyzer\Query {#344
-query: "select * from "authors" where "authors"."id" = ? limit 1"
-bindings: array:1 [
0 => 1
]
-time: 0.08
-buildedQuery: "select * from "authors" where "authors"."id" = 1 limit 1"
}
*/
dump($analyzed->extractSlowestQuery());
/*
array:2 [
0 => Laqu\Analyzer\Query {#337
-query: "delete from "authors" where "id" = ?"
-bindings: array:1 [
0 => "1"
]
-time: 0.03
-buildedQuery: "delete from "authors" where "id" = '1'"
}
1 => Laqu\Analyzer\Query {#344
-query: "select * from "authors" where "authors"."id" = ? limit 1"
-bindings: array:1 [
0 => 1
]
-time: 0.08
-buildedQuery: "select * from "authors" where "authors"."id" = 1 limit 1"
}
]
*/
dump($analyzed->sortByFast());
/*
array:2 [
0 => Laqu\Analyzer\Query {#344
-query: "select * from "authors" where "authors"."id" = ? limit 1"
-bindings: array:1 [
0 => 1
]
-time: 0.08
-buildedQuery: "select * from "authors" where "authors"."id" = 1 limit 1"
}
1 => Laqu\Analyzer\Query {#337
-query: "delete from "authors" where "id" = ?"
-bindings: array:1 [
0 => "1"
]
-time: 0.02
-buildedQuery: "delete from "authors" where "id" = '1'"
}
]
*/
dump($analyzed->sortBySlow());
use Laqu\Facades\QueryHelper;
$now = Carbon::now();
$from = $now->copy()->subDay();
$to = $now->copy()->addDay();
$query = 'select * from authors where id in (?, ?) and name like :name and updated_at between ? and ?';
$bindings = [
1,
2,
'%Shakespeare',
$from,
$to,
];
$buildedQuery = QueryHelper::buildedQuery($query, $bindings);
// select * from authors where id in (1, 2) and name like '%Shakespeare' and updated_at between '2020-07-07 00:37:55' and '2020-07-09 00:37:55'
echo $buildedQuery;
use Laqu\Facades\QueryFormatter;
$query = "SELECT count(*),`Column1`,`Testing`, `Testing Three` FROM `Table1`
WHERE Column1 = 'testing' AND ( (`Column2` = `Column3` OR Column4 >= NOW()) )
GROUP BY Column1 ORDER BY Column3 DESC LIMIT 5,10";
/*
SELECT
count(*),
`Column1`,
`Testing`,
`Testing Three`
FROM
`Table1`
WHERE
Column1 = 'testing'
AND (
(
`Column2` = `Column3`
OR Column4 >= NOW()
)
)
GROUP BY
Column1
ORDER BY
Column3 DESC
LIMIT
5, 10
*/
echo QueryFormatter::format($query);
use Doctrine\SqlFormatter\CliHighlighter;
use Laqu\Formatter\QueryFormatter;
/** @var QueryFormatter */
$formatter = app()->make(QueryFormatter::class/* or 'queryFormatter' */, [new CliHighlighter()]);
echo $formatter->format($query);
use Doctrine\SqlFormatter\HtmlHighlighter;
use Laqu\Formatter\QueryFormatter;
/** @var QueryFormatter */
$formatter = app()->make(QueryFormatter::class/* or 'queryFormatter' */, [new HtmlHighlighter()]);
echo $formatter->format($query);
use Laqu\Facades\QueryFormatter;
$query = '...';
echo QueryFormatter::highlight($query);
use Laqu\Facades\QueryFormatter;
$query = <<<SQL
-- This is a comment
SELECT
/* This is another comment
On more than one line */
Id #This is one final comment
as temp, DateCreated as Created FROM MyTable;
SQL;
// SELECT Id as temp, DateCreated as Created FROM MyTable;
echo QueryFormatter::compress($query);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.