1 | <?php |
||
2 | |||
3 | declare(strict_types = 1); |
||
4 | |||
5 | namespace Protoqol\Prequel\Http\Controllers; |
||
6 | |||
7 | use Illuminate\Database\Eloquent\Model; |
||
8 | use Illuminate\Routing\Controller; |
||
9 | use Illuminate\Support\Facades\DB; |
||
10 | use Illuminate\Support\Facades\Route; |
||
11 | use Protoqol\Prequel\Classes\Database\DatabaseTraverser; |
||
12 | use Protoqol\Prequel\Http\Requests\PrequelDatabaseRequest; |
||
13 | |||
14 | /** |
||
15 | * Class DatabaseActionController |
||
16 | * |
||
17 | * @package Protoqol\Prequel\Http\Controllers |
||
18 | */ |
||
19 | class DatabaseController extends Controller |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * Qualified table name; 'database.table' |
||
24 | * |
||
25 | * @var string $qualifiedName |
||
26 | */ |
||
27 | public $qualifiedName; |
||
28 | |||
29 | /** |
||
30 | * Table name |
||
31 | * |
||
32 | * @var string $tableName |
||
33 | */ |
||
34 | public $tableName; |
||
35 | |||
36 | /** |
||
37 | * Database name |
||
38 | * |
||
39 | * @var string $databaseName |
||
40 | */ |
||
41 | public $databaseName; |
||
42 | |||
43 | /** |
||
44 | * Holds model for given table if one exists. |
||
45 | * |
||
46 | * @var Model $model |
||
47 | */ |
||
48 | public $model; |
||
49 | |||
50 | /** |
||
51 | * DatabaseActionController's constructor |
||
52 | * |
||
53 | * @param \Protoqol\Prequel\Http\Requests\PrequelDatabaseRequest $request |
||
54 | */ |
||
55 | public function __construct(PrequelDatabaseRequest $request) |
||
56 | { |
||
57 | $this->tableName = $request->table; |
||
58 | $this->databaseName = $request->database; |
||
59 | $this->qualifiedName = $request->qualifiedName; |
||
60 | $this->model = $request->model; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Get table data, table structure and its qualified name |
||
65 | * |
||
66 | * @return mixed |
||
67 | */ |
||
68 | public function getTableData() |
||
69 | { |
||
70 | if ($this->model |
||
71 | && $this->databaseName |
||
72 | === config('database.connections.mysql.database') |
||
73 | ) { |
||
74 | $tableData = $this->model->paginate(100); |
||
75 | } else { |
||
76 | $tableData = DB::table($this->qualifiedName)->paginate(100); |
||
77 | } |
||
78 | |||
79 | // dd($tableData); |
||
80 | return [ |
||
81 | "table" => $this->qualifiedName, |
||
82 | "structure" => app(DatabaseTraverser::class)->getTableStructure( |
||
83 | $this->databaseName, |
||
84 | $this->tableName |
||
85 | ), |
||
86 | "data" => $tableData, |
||
87 | ]; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Get count of rows in table |
||
92 | * |
||
93 | * @return array |
||
94 | */ |
||
95 | public function countTableRecords() :array |
||
96 | { |
||
97 | $count = DB::table($this->qualifiedName) |
||
98 | ->count('id'); |
||
99 | |||
100 | return [ |
||
101 | "table" => $this->qualifiedName, |
||
102 | "count" => $count, |
||
103 | ]; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Try to find input in each column in table. |
||
108 | * |
||
109 | * @TODO Prefetch if possible with ID |
||
110 | * @TODO Clean up, this is nowhere near production ready |
||
111 | * @return mixed |
||
112 | */ |
||
113 | public function findInTable() |
||
114 | { |
||
115 | $column = Route::current()->parameter('column'); |
||
116 | $value = Route::current()->parameter('value'); |
||
117 | $queryType = Route::current()->parameter('type'); |
||
118 | |||
119 | if ($queryType === 'LIKE') { |
||
120 | return $this->model |
||
121 | ? $this->model->where($column, 'LIKE', '%'.$value.'%') |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
122 | ->paginate(100) |
||
123 | : DB::table($this->qualifiedName) |
||
124 | ->where($column, 'LIKE', '%'.$value.'%') |
||
125 | ->paginate(100); |
||
126 | } |
||
127 | |||
128 | return $this->model |
||
129 | ? $this->model->where($column, $queryType, $value) |
||
130 | ->paginate(100) |
||
131 | : DB::table($this->qualifiedName) |
||
132 | ->where($column, $queryType, $value)->paginate(100); |
||
133 | } |
||
134 | } |
||
135 |