1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Socialblue\LaravelQueryAdviser\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\Controller; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Support\Facades\Cache; |
8
|
|
|
use Illuminate\Support\Facades\DB; |
9
|
|
|
use Socialblue\LaravelQueryAdviser\Helper\QueryBuilderHelper; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class QueryController |
13
|
|
|
* @package Socialblue\LaravelQueryAdviser\Http\Controllers |
14
|
|
|
*/ |
15
|
|
|
class QueryController extends Controller |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Show view |
19
|
|
|
* |
20
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
21
|
|
|
*/ |
22
|
|
|
public function index() { |
23
|
|
|
return view('QueryAdviser::index'); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get from cache |
28
|
|
|
* |
29
|
|
|
* |
30
|
|
|
* @param Request $request |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
|
|
public function get(Request $request): array |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
return Cache::get(config('laravel-query-adviser.cache.key'), []); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Clears cache |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public function clear():array |
43
|
|
|
{ |
44
|
|
|
return ['success' => Cache::forget(config('laravel-query-adviser.cache.key'))]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Execute query |
49
|
|
|
* |
50
|
|
|
* |
51
|
|
|
* @param Request $request |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public function exec(Request $request): array |
55
|
|
|
{ |
56
|
|
|
$data = Cache::get(config('laravel-query-adviser.cache.key'), []); |
57
|
|
|
|
58
|
|
View Code Duplication |
if (isset($data[$request->get('time')][$request->get('time-key')])) { |
|
|
|
|
59
|
|
|
$query = $data[$request->get('time')][$request->get('time-key')]; |
60
|
|
|
return DB::connection()->select($query['sql'], $query['bindings']); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return []; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Use explain for query |
68
|
|
|
* |
69
|
|
|
* |
70
|
|
|
* @param Request $request |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public function explain(Request $request): array |
74
|
|
|
{ |
75
|
|
|
$data = Cache::get(config('laravel-query-adviser.cache.key'), []); |
76
|
|
View Code Duplication |
if (isset($data[$request->get('time')][$request->get('time-key')])) { |
|
|
|
|
77
|
|
|
$query = $data[$request->get('time')][$request->get('time-key')]; |
78
|
|
|
return QueryBuilderHelper::analyze($query['sql'], $query['bindings']); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return ['queryParts' => "", 'query' => "", 'optimized' => ""]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
public function serverInfo(): array |
89
|
|
|
{ |
90
|
|
|
return QueryBuilderHelper::getServerInfo(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.