Skip to content

Commit 3e347f4

Browse files
authored
Merge pull request #34 from vc-urvin/main
Implement the web page for standard & constraint page
2 parents 8e8759e + 2013bc0 commit 3e347f4

File tree

17 files changed

+734
-7
lines changed

17 files changed

+734
-7
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
],
2121
"require": {
2222
"php": "^8.0",
23-
"doctrine/dbal": "^3.6"
23+
"doctrine/dbal": "^3.6",
24+
"yajra/laravel-datatables-oracle": "^10.4"
2425
},
2526
"require-dev": {
2627
"laravel/pint": "^1.0",
@@ -68,4 +69,4 @@
6869
},
6970
"minimum-stability": "dev",
7071
"prefer-stable": true
71-
}
72+
}

config/db-auditor.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<?php
22

3+
use Vcian\LaravelDBAuditor\Traits\DBConnection;
4+
35
// config for Vcian/LaravelDbAuditor
46
return [
57

8+
'db_name' => $this->getDatabaseName()
9+
610
];

routes/api.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Route;
4+
use Vcian\LaravelDBAuditor\Controllers\DisplayController;
5+
6+
Route::prefix('api')->group(function() {
7+
Route::get('getAudit', [DisplayController::class, 'getAudit']);
8+
Route::get('getTableData/{table}', [DisplayController::class, 'getTableData']);
9+
Route::get('gettableconstraint/{table}', [DisplayController::class, 'getTableConstraint']);
10+
});
11+

routes/web.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Route;
4+
use Vcian\LaravelDBAuditor\Controllers\DisplayController;
5+
6+
Route::get('laravel-db-auditor', [DisplayController::class, 'index']);

src/Controllers/DisplayController.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
namespace Vcian\LaravelDBAuditor\Controllers;
4+
5+
use Illuminate\Http\JsonResponse;
6+
use Vcian\LaravelDBAuditor\Constants\Constant;
7+
use Vcian\LaravelDBAuditor\Traits\Audit;
8+
use Vcian\LaravelDBAuditor\Traits\Rules;
9+
10+
class DisplayController
11+
{
12+
use Rules, Audit;
13+
14+
/**
15+
* Return view for audit
16+
* @return view
17+
*/
18+
public function index()
19+
{
20+
$tables = $this->getTableList();
21+
return view('DBAuditor::auditor.pages.audit', compact('tables'));
22+
}
23+
24+
/**
25+
* Get audit table list
26+
* @return JsonResponse
27+
*/
28+
public function getAudit() : JsonResponse
29+
{
30+
$columnName = 'status';
31+
$noConstraint = '<img src=' . asset("auditor/icon/close.svg") . ' alt="key" class="m-auto" />';
32+
$constraint = '<img src=' . asset("auditor/icon/check.svg") . ' alt="key" class="m-auto" />';
33+
34+
$tableRules = array_map(function ($value) use ($columnName, $noConstraint, $constraint) {
35+
if ($value[$columnName] === "") {
36+
$value[$columnName] = $noConstraint;
37+
} else {
38+
$value[$columnName] = $constraint;
39+
}
40+
return $value;
41+
}, $this->tablesRule());
42+
43+
return response()->json(
44+
array(
45+
"data" => $tableRules
46+
)
47+
);
48+
}
49+
50+
/**
51+
* Get table data
52+
* @param string $tableName
53+
* @return JsonResponse
54+
*/
55+
public function getTableData(string $tableName) : JsonResponse
56+
{
57+
return response()->json(array(
58+
"data" => $this->tableRules($tableName)
59+
));
60+
}
61+
62+
/**
63+
* Get Constraint list
64+
* @param string $tableName
65+
* @return JsonResponse
66+
*/
67+
public function getTableConstraint(string $tableName) : JsonResponse
68+
{
69+
70+
$data = [
71+
"fields" => $this->getFieldsDetails($tableName),
72+
'constrain' => [
73+
'primary' => $this->getConstraintField($tableName, Constant::CONSTRAINT_PRIMARY_KEY),
74+
'unique' => $this->getConstraintField($tableName, Constant::CONSTRAINT_UNIQUE_KEY),
75+
'foreign' => $this->getConstraintField($tableName, Constant::CONSTRAINT_FOREIGN_KEY),
76+
'index' => $this->getConstraintField($tableName, Constant::CONSTRAINT_INDEX_KEY)
77+
]
78+
];
79+
80+
$response = [];
81+
$greenKey = '<img src=' . asset("auditor/icon/green-key.svg") . ' alt="key" class="m-auto" />';
82+
$grayKey = '<img src=' . asset("auditor/icon/gray-key.svg") . ' alt="key" class="m-auto" />';
83+
84+
foreach ($data['fields'] as $table) {
85+
86+
$primaryKey = $indexing = $uniqueKey = $foreignKey = "-";
87+
88+
if (in_array($table->COLUMN_NAME, $data['constrain']['primary'])) {
89+
$primaryKey = $greenKey;
90+
} else if (in_array($table->COLUMN_NAME, $data['constrain']['unique'])) {
91+
$uniqueKey = $grayKey;
92+
} else if (in_array($table->COLUMN_NAME, $data['constrain']['index'])) {
93+
$indexing = $grayKey;
94+
}
95+
96+
foreach ($data['constrain']['foreign'] as $foreign) {
97+
if ($table->COLUMN_NAME === $foreign['column_name']) {
98+
$foreignKeyToottip = '<div class="inline-flex">
99+
<img src=' . asset("auditor/icon/gray-key.svg") . ' alt="key" class="mr-2">
100+
<div class="relative flex flex-col items-center group">
101+
<svg class="w-5 h-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
102+
<path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-8-3a1 1 0 00-.867.5 1 1 0 11-1.731-1A3 3 0 0113 8a3.001 3.001 0 01-2 2.83V11a1 1 0 11-2 0v-1a1 1 0 011-1 1 1 0 100-2zm0 8a1 1 0 100-2 1 1 0 000 2z" clip-rule="evenodd" />
103+
</svg>
104+
<div class="absolute bottom-0 flex flex-col items-center hidden group-hover:flex">
105+
<span class="relative z-10 p-2 text-xs leading-none text-white whitespace-no-wrap bg-black shadow-lg">Foreign Table Name : ' . $foreign['foreign_table_name'] . ' and Foreign Column Name : ' . $foreign['foreign_column_name'] . '</span>
106+
<div class="w-3 h-3 -mt-2 rotate-45 bg-black"></div>
107+
</div>
108+
</div>
109+
</div>';
110+
$foreignKey = $foreignKeyToottip;
111+
}
112+
}
113+
114+
$response[] = ["column" => $table->COLUMN_NAME, "primaryKey" => $primaryKey, "indexing" => $indexing, "uniqueKey" => $uniqueKey, "foreignKey" => $foreignKey];
115+
}
116+
117+
return response()->json(array(
118+
"data" => $response
119+
));
120+
}
121+
}

src/Datatable/AuditDatatable.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Vcian\LaravelDBAuditor\Datatable;
4+
5+
use Yajra\DataTables\Services\DataTable;
6+
7+
class AuditDatatable extends DataTable
8+
{
9+
public function dataTable($query)
10+
{
11+
return datatables()
12+
->eloquent($query)
13+
->addColumn('action', function ($row) {
14+
// Add custom action column
15+
});
16+
}
17+
18+
public function query()
19+
{
20+
// Define your data source query here
21+
}
22+
23+
public function html()
24+
{
25+
// Define the HTML structure of your DataTable
26+
}
27+
}

src/Providers/DBAuditorServiceProvider.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,21 @@ class DBAuditorServiceProvider extends ServiceProvider
1919
public function register(): void
2020
{
2121
$this->commands($this->commands);
22+
2223
}
2324

2425
/**
2526
* Bootstrap services.
2627
*/
2728
public function boot(): void
2829
{
30+
$this->publishes([
31+
__DIR__.'/../resource/images' => public_path('auditor/icon'),
32+
], 'public');
33+
2934
$this->loadViewsFrom(__DIR__ . '/../views', 'DBAuditor');
35+
$this->loadRoutesFrom(__DIR__.'/../../routes/web.php');
36+
$this->loadRoutesFrom(__DIR__.'/../../routes/api.php');
3037
$this->loadTranslationsFrom(__DIR__ . '/../Lang/', 'Lang');
3138
}
3239
}

src/Traits/Audit.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function getNoConstraintFields(string $tableName): array
3939
$fields = Constant::ARRAY_DECLARATION;
4040
try {
4141
$fieldList = DB::select("SELECT * FROM `INFORMATION_SCHEMA`.`COLUMNS`
42-
WHERE `TABLE_SCHEMA`= '" . env('DB_DATABASE') . "' AND `TABLE_NAME`= '" . $tableName . "' AND ( `COLUMN_KEY` = '' OR `COLUMN_KEY` = 'UNI' ) ");
42+
WHERE `TABLE_SCHEMA`= '" . $this->getDatabaseName() . "' AND `TABLE_NAME`= '" . $tableName . "' AND ( `COLUMN_KEY` = '' OR `COLUMN_KEY` = 'UNI' ) ");
4343

4444
foreach ($fieldList as $field) {
4545
if (!in_array($field->DATA_TYPE, Constant::RESTRICT_DATATYPE)) {
@@ -107,12 +107,17 @@ public function getConstraintField(string $tableName, string $input): array
107107
return [];
108108
}
109109

110-
$result = DB::select("SHOW KEYS FROM {$tableName} WHERE Key_name LIKE '%" . strtolower($input) . "%'");
110+
if($input === Constant::CONSTRAINT_INDEX_KEY) {
111+
$result = DB::select("SHOW INDEX FROM {$tableName}");
112+
} else {
113+
$result = DB::select("SHOW KEYS FROM {$tableName} WHERE Key_name LIKE '%" . strtolower($input) . "%'");
114+
}
111115

116+
112117
if ($input === Constant::CONSTRAINT_FOREIGN_KEY) {
113118
return $this->getForeignKeyDetails($tableName);
114119
}
115-
120+
116121
if ($result) {
117122
foreach ($result as $value) {
118123
$constraintFields[] = $value->Column_name;
@@ -136,8 +141,8 @@ public function getForeignKeyDetails(string $tableName): array
136141
$resultForeignKey = DB::select("SELECT i.TABLE_SCHEMA, i.TABLE_NAME, i.CONSTRAINT_TYPE,k.COLUMN_NAME, i.CONSTRAINT_NAME,
137142
k.REFERENCED_TABLE_NAME, k.REFERENCED_COLUMN_NAME FROM information_schema.TABLE_CONSTRAINTS i
138143
LEFT JOIN information_schema.KEY_COLUMN_USAGE k ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME
139-
WHERE i.CONSTRAINT_TYPE = 'FOREIGN KEY' AND i.TABLE_SCHEMA = '" . env('DB_DATABASE') . "' AND i.TABLE_NAME = '" . $tableName . "'");
140-
144+
WHERE i.CONSTRAINT_TYPE = 'FOREIGN KEY' AND i.TABLE_SCHEMA = '" . $this->getDatabaseName() . "' AND i.TABLE_NAME = '" . $tableName . "'");
145+
141146
if ($resultForeignKey) {
142147
foreach ($resultForeignKey as $value) {
143148
$foreignFieldDetails[] = [

src/resource/images/bg-gray.png

9.99 KB
Loading

src/resource/images/bg.png

860 KB
Loading

0 commit comments

Comments
 (0)