Skip to content

Commit 2f69182

Browse files
authored
Merge pull request #35 from vc-urvin/main
Add web pages for standard check and constraint
2 parents 3e347f4 + 7c641fb commit 2f69182

File tree

11 files changed

+503
-117
lines changed

11 files changed

+503
-117
lines changed

routes/api.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,13 @@
77
Route::get('getAudit', [DisplayController::class, 'getAudit']);
88
Route::get('getTableData/{table}', [DisplayController::class, 'getTableData']);
99
Route::get('gettableconstraint/{table}', [DisplayController::class, 'getTableConstraint']);
10+
11+
12+
Route::post('change-constraint', [DisplayController::class, 'changeConstraint']);
13+
14+
Route::get('foreign-key-table', [DisplayController::class, 'getForeignKeyTableList']);
15+
Route::get('foreign-key-field/{table}', [DisplayController::class, 'getForeignKeyFieldList']);
16+
Route::post('add-foreign-constraint', [DisplayController::class, 'addForeignKeyConstraint']);
17+
1018
});
1119

src/Commands/DBConstraintCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,11 @@ public function foreignKeyConstraint(string $tableName, string $selectField): vo
135135
$fields = Constant::ARRAY_DECLARATION;
136136

137137
do {
138-
$referenceTable = $this->anticipate(__('Lang::messages.constraint.question.foreign_table'), $this->getTablesList());
138+
$referenceTable = $this->anticipate(__('Lang::messages.constraint.question.foreign_table'), $this->getTableList());
139139

140-
if ($referenceTable && $this->checkTableExistOrNot($referenceTable)) {
140+
if ($referenceTable && $this->checkTableExist($referenceTable)) {
141141

142-
foreach ($this->getTableFields($referenceTable) as $field) {
142+
foreach ($this->getFieldsDetails($referenceTable) as $field) {
143143
$fields[] = $field->COLUMN_NAME;
144144
}
145145
do {

src/Controllers/DisplayController.php

Lines changed: 110 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Vcian\LaravelDBAuditor\Controllers;
44

55
use Illuminate\Http\JsonResponse;
6+
use Illuminate\Http\Request;
67
use Vcian\LaravelDBAuditor\Constants\Constant;
78
use Vcian\LaravelDBAuditor\Traits\Audit;
89
use Vcian\LaravelDBAuditor\Traits\Rules;
@@ -23,9 +24,9 @@ public function index()
2324

2425
/**
2526
* Get audit table list
26-
* @return JsonResponse
27+
* @return JsonResponse
2728
*/
28-
public function getAudit() : JsonResponse
29+
public function getAudit(): JsonResponse
2930
{
3031
$columnName = 'status';
3132
$noConstraint = '<img src=' . asset("auditor/icon/close.svg") . ' alt="key" class="m-auto" />';
@@ -37,6 +38,7 @@ public function getAudit() : JsonResponse
3738
} else {
3839
$value[$columnName] = $constraint;
3940
}
41+
$value["size"] = $value['size']." MB";
4042
return $value;
4143
}, $this->tablesRule());
4244

@@ -50,9 +52,9 @@ public function getAudit() : JsonResponse
5052
/**
5153
* Get table data
5254
* @param string $tableName
53-
* @return JsonResponse
55+
* @return JsonResponse
5456
*/
55-
public function getTableData(string $tableName) : JsonResponse
57+
public function getTableData(string $tableName): JsonResponse
5658
{
5759
return response()->json(array(
5860
"data" => $this->tableRules($tableName)
@@ -62,11 +64,14 @@ public function getTableData(string $tableName) : JsonResponse
6264
/**
6365
* Get Constraint list
6466
* @param string $tableName
65-
* @return JsonResponse
67+
* @return JsonResponse
6668
*/
67-
public function getTableConstraint(string $tableName) : JsonResponse
69+
public function getTableConstraint(string $tableName): JsonResponse
6870
{
6971

72+
$noConstraintFields = $this->getNoConstraintFields($tableName);
73+
$constraintList = $this->getConstraintList($tableName, $noConstraintFields);
74+
7075
$data = [
7176
"fields" => $this->getFieldsDetails($tableName),
7277
'constrain' => [
@@ -76,26 +81,30 @@ public function getTableConstraint(string $tableName) : JsonResponse
7681
'index' => $this->getConstraintField($tableName, Constant::CONSTRAINT_INDEX_KEY)
7782
]
7883
];
79-
8084
$response = [];
8185
$greenKey = '<img src=' . asset("auditor/icon/green-key.svg") . ' alt="key" class="m-auto" />';
8286
$grayKey = '<img src=' . asset("auditor/icon/gray-key.svg") . ' alt="key" class="m-auto" />';
8387

8488
foreach ($data['fields'] as $table) {
8589

86-
$primaryKey = $indexing = $uniqueKey = $foreignKey = "-";
90+
$primaryKey = $indexKey = $uniqueKey = $foreignKey = "-";
91+
8792

8893
if (in_array($table->COLUMN_NAME, $data['constrain']['primary'])) {
8994
$primaryKey = $greenKey;
90-
} else if (in_array($table->COLUMN_NAME, $data['constrain']['unique'])) {
95+
}
96+
97+
if (in_array($table->COLUMN_NAME, $data['constrain']['unique'])) {
9198
$uniqueKey = $grayKey;
92-
} else if (in_array($table->COLUMN_NAME, $data['constrain']['index'])) {
93-
$indexing = $grayKey;
99+
}
100+
101+
if (in_array($table->COLUMN_NAME, $data['constrain']['index'])) {
102+
$indexKey = $grayKey;
94103
}
95104

96105
foreach ($data['constrain']['foreign'] as $foreign) {
97-
if ($table->COLUMN_NAME === $foreign['column_name']) {
98-
$foreignKeyToottip = '<div class="inline-flex">
106+
if ($table->COLUMN_NAME === $foreign) {
107+
$foreignKeyTooltip = '<div class="inline-flex">
99108
<img src=' . asset("auditor/icon/gray-key.svg") . ' alt="key" class="mr-2">
100109
<div class="relative flex flex-col items-center group">
101110
<svg class="w-5 h-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
@@ -107,15 +116,101 @@ public function getTableConstraint(string $tableName) : JsonResponse
107116
</div>
108117
</div>
109118
</div>';
110-
$foreignKey = $foreignKeyToottip;
119+
$foreignKey = $foreignKeyTooltip;
120+
}
121+
}
122+
123+
foreach ($constraintList as $constraint) {
124+
switch ($constraint) {
125+
case Constant::CONSTRAINT_PRIMARY_KEY:
126+
if (in_array($table->COLUMN_NAME, $noConstraintFields['integer'])) {
127+
$primaryKey = '<img src=' . asset("auditor/icon/add.svg") . ' alt="key" class="m-auto add-constraint-' . $table->COLUMN_NAME . '-' . Constant::CONSTRAINT_PRIMARY_KEY . '" style="height:20px;cursor: pointer;" onclick="add(`' . $table->COLUMN_NAME . '`, `' . Constant::CONSTRAINT_PRIMARY_KEY . '`)"/>';
128+
}
129+
break;
130+
case Constant::CONSTRAINT_INDEX_KEY:
131+
if (in_array($table->COLUMN_NAME, $noConstraintFields['mix'])) {
132+
$indexKey = '<img src=' . asset("auditor/icon/add.svg") . ' alt="key" class="m-auto add-constraint-' . $table->COLUMN_NAME . '-' . Constant::CONSTRAINT_INDEX_KEY . '" style="height:20px;cursor: pointer;" onclick="add(`' . $table->COLUMN_NAME . '`, `' . Constant::CONSTRAINT_INDEX_KEY . '`)"/>';
133+
}
134+
break;
135+
case Constant::CONSTRAINT_UNIQUE_KEY:
136+
if (in_array($table->COLUMN_NAME, $noConstraintFields['mix'])) {
137+
$fields = $this->getUniqueFields($tableName, $noConstraintFields['mix']);
138+
if (in_array($table->COLUMN_NAME, $fields)) {
139+
$uniqueKey = '<img src=' . asset("auditor/icon/add.svg") . ' alt="key" class="m-auto add-constraint-' . $table->COLUMN_NAME . '-' . Constant::CONSTRAINT_UNIQUE_KEY . '" style="height:20px;cursor: pointer;" onclick="add(`' . $table->COLUMN_NAME . '`, `' . Constant::CONSTRAINT_UNIQUE_KEY . '`)"/>';
140+
}
141+
}
142+
break;
143+
case Constant::CONSTRAINT_FOREIGN_KEY:
144+
if(in_array($table->COLUMN_NAME, $noConstraintFields['integer'])) {
145+
if(!$this->tableHasValue($tableName)) {
146+
$foreignKey = '<img src=' . asset("auditor/icon/add.svg") . ' alt="key" class="m-auto add-constraint-'.$table->COLUMN_NAME.'-'.Constant::CONSTRAINT_FOREIGN_KEY.'" style="height:20px;cursor: pointer;" onclick="add(`'.$table->COLUMN_NAME.'`, `'.Constant::CONSTRAINT_FOREIGN_KEY.'`,`'.$tableName.'`)"/>';
147+
}
148+
}
149+
break;
150+
default:
151+
break;
111152
}
112153
}
113154

114-
$response[] = ["column" => $table->COLUMN_NAME, "primaryKey" => $primaryKey, "indexing" => $indexing, "uniqueKey" => $uniqueKey, "foreignKey" => $foreignKey];
155+
$response[] = ["column" => $table->COLUMN_NAME, "primaryKey" => $primaryKey, "indexing" => $indexKey, "uniqueKey" => $uniqueKey, "foreignKey" => $foreignKey];
115156
}
116157

117158
return response()->json(array(
118159
"data" => $response
119160
));
120161
}
162+
163+
/**
164+
* Update the field Constraint
165+
* @param Request
166+
* @return
167+
*/
168+
public function changeConstraint(Request $request): bool
169+
{
170+
$data = $request->all();
171+
$this->addConstraint($data['table_name'], $data['colum_name'], $data['constraint']);
172+
return Constant::STATUS_TRUE;
173+
}
174+
175+
/**
176+
* Get Foreign Key Details
177+
* @return array
178+
*/
179+
public function getForeignKeyTableList(): array
180+
{
181+
return $this->getTableList();
182+
}
183+
184+
/**
185+
* Get Foreign Key Field List
186+
* @param string
187+
* @return array
188+
*/
189+
public function getForeignKeyFieldList(string $tableName): array
190+
{
191+
return $this->getFieldsDetails($tableName);
192+
}
193+
194+
/**
195+
* Add Foreign Key Constraint
196+
* @param Request
197+
* @return mixed
198+
*/
199+
public function addForeignKeyConstraint(Request $request): mixed
200+
{
201+
$data= $request->all();
202+
203+
if($data['reference_table'] === $data['table_name']) {
204+
return __('Lang::messages.constraint.error_message.foreign_selected_table_match', ['foreign' => $data['reference_table'], 'selected' => $data['table_name']]);
205+
}
206+
207+
$referenceFieldType = $this->getFieldDataType($data['reference_table'], $data['reference_field']);
208+
$selectedFieldType = $this->getFieldDataType($data['table_name'], $data['select_field']);
209+
if ($referenceFieldType['data_type'] !== $selectedFieldType['data_type']) {
210+
return __('Lang::messages.constraint.error_message.foreign_not_apply');
211+
}
212+
213+
$this->addConstraint($data['table_name'], $data['select_field'], Constant::CONSTRAINT_FOREIGN_KEY, $data['reference_table'], $data['reference_field']);
214+
return Constant::STATUS_TRUE;
215+
}
121216
}

src/Datatable/AuditDatatable.php

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/Lang/en/messages.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
'plural' => 'Table name should be plural.',
4343
'space' => 'Space between words is not advised. Please Use Underscore "_"',
4444
'alphabets' => 'Numbers are not for names and is not advised! Please use alphabets for name.',
45-
'lowercase' => 'Name should be in lowercase.',
45+
'convention' => 'Name should be in lowercase, camelCase or snake_case.',
4646
'datatype_change' => 'Here you can use CHAR datatype instead of VARCHAR if data values in a column are of the same length.',
4747
],
4848
'question' => [

src/Traits/Audit.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function getConstraintList(string $tableName, array $fields): array
8585
if (!empty($fields['mix'])) {
8686
$constrainList[] = Constant::CONSTRAINT_INDEX_KEY;
8787

88-
if (empty($this->getUniqueFields($tableName, $fields['mix']))) {
88+
if (!empty($this->getUniqueFields($tableName, $fields['mix']))) {
8989
$constrainList[] = Constant::CONSTRAINT_UNIQUE_KEY;
9090
}
9191
}
@@ -108,7 +108,7 @@ public function getConstraintField(string $tableName, string $input): array
108108
}
109109

110110
if($input === Constant::CONSTRAINT_INDEX_KEY) {
111-
$result = DB::select("SHOW INDEX FROM {$tableName}");
111+
$result = DB::select("SHOW INDEX FROM {$tableName} where Key_name != 'PRIMARY' and Key_name not like '%unique%'");
112112
} else {
113113
$result = DB::select("SHOW KEYS FROM {$tableName} WHERE Key_name LIKE '%" . strtolower($input) . "%'");
114114
}
@@ -282,12 +282,19 @@ public function getUniqueFields(string $tableName, array $fields): array
282282
$uniqueField = Constant::ARRAY_DECLARATION;
283283
try {
284284
foreach ($fields as $field) {
285-
$query = "SELECT `" . $field . "`, COUNT(`" . $field . "`) as count FROM " . $tableName . " GROUP BY `" . $field . "` HAVING COUNT(`" . $field . "`) > 1";
286-
$result = DB::select($query);
287285

288-
if (empty($result)) {
289-
$uniqueField[] = $field;
286+
$getUniqueQuery = "SELECT * FROM INFORMATION_SCHEMA.STATISTICS
287+
WHERE TABLE_SCHEMA = '". $this->getDatabaseName() ."' AND TABLE_NAME = '".$tableName."' AND COLUMN_NAME = '".$field."' AND NON_UNIQUE = 0";
288+
$resultUniqueQuery = DB::select($getUniqueQuery);
289+
if(!$resultUniqueQuery) {
290+
$query = "SELECT `" . $field . "`, COUNT(`" . $field . "`) as count FROM " . $tableName . " GROUP BY `" . $field . "` HAVING COUNT(`" . $field . "`) > 1";
291+
$result = DB::select($query);
292+
293+
if (empty($result)) {
294+
$uniqueField[] = $field;
295+
}
290296
}
297+
291298
}
292299
} catch (Exception $exception) {
293300
Log::error($exception->getMessage());

0 commit comments

Comments
 (0)