Skip to content

Commit

Permalink
Merge pull request #6 from vcian/main
Browse files Browse the repository at this point in the history
Merge latest code.
  • Loading branch information
ruchit288 authored Jul 3, 2023
2 parents 56b71b6 + 2f69182 commit aa3f4e3
Show file tree
Hide file tree
Showing 11 changed files with 503 additions and 117 deletions.
8 changes: 8 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,13 @@
Route::get('getAudit', [DisplayController::class, 'getAudit']);
Route::get('getTableData/{table}', [DisplayController::class, 'getTableData']);
Route::get('gettableconstraint/{table}', [DisplayController::class, 'getTableConstraint']);


Route::post('change-constraint', [DisplayController::class, 'changeConstraint']);

Route::get('foreign-key-table', [DisplayController::class, 'getForeignKeyTableList']);
Route::get('foreign-key-field/{table}', [DisplayController::class, 'getForeignKeyFieldList']);
Route::post('add-foreign-constraint', [DisplayController::class, 'addForeignKeyConstraint']);

});

6 changes: 3 additions & 3 deletions src/Commands/DBConstraintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ public function foreignKeyConstraint(string $tableName, string $selectField): vo
$fields = Constant::ARRAY_DECLARATION;

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

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

foreach ($this->getTableFields($referenceTable) as $field) {
foreach ($this->getFieldsDetails($referenceTable) as $field) {
$fields[] = $field->COLUMN_NAME;
}
do {
Expand Down
125 changes: 110 additions & 15 deletions src/Controllers/DisplayController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Vcian\LaravelDBAuditor\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Vcian\LaravelDBAuditor\Constants\Constant;
use Vcian\LaravelDBAuditor\Traits\Audit;
use Vcian\LaravelDBAuditor\Traits\Rules;
Expand All @@ -23,9 +24,9 @@ public function index()

/**
* Get audit table list
* @return JsonResponse
* @return JsonResponse
*/
public function getAudit() : JsonResponse
public function getAudit(): JsonResponse
{
$columnName = 'status';
$noConstraint = '<img src=' . asset("auditor/icon/close.svg") . ' alt="key" class="m-auto" />';
Expand All @@ -37,6 +38,7 @@ public function getAudit() : JsonResponse
} else {
$value[$columnName] = $constraint;
}
$value["size"] = $value['size']." MB";
return $value;
}, $this->tablesRule());

Expand All @@ -50,9 +52,9 @@ public function getAudit() : JsonResponse
/**
* Get table data
* @param string $tableName
* @return JsonResponse
* @return JsonResponse
*/
public function getTableData(string $tableName) : JsonResponse
public function getTableData(string $tableName): JsonResponse
{
return response()->json(array(
"data" => $this->tableRules($tableName)
Expand All @@ -62,11 +64,14 @@ public function getTableData(string $tableName) : JsonResponse
/**
* Get Constraint list
* @param string $tableName
* @return JsonResponse
* @return JsonResponse
*/
public function getTableConstraint(string $tableName) : JsonResponse
public function getTableConstraint(string $tableName): JsonResponse
{

$noConstraintFields = $this->getNoConstraintFields($tableName);
$constraintList = $this->getConstraintList($tableName, $noConstraintFields);

$data = [
"fields" => $this->getFieldsDetails($tableName),
'constrain' => [
Expand All @@ -76,26 +81,30 @@ public function getTableConstraint(string $tableName) : JsonResponse
'index' => $this->getConstraintField($tableName, Constant::CONSTRAINT_INDEX_KEY)
]
];

$response = [];
$greenKey = '<img src=' . asset("auditor/icon/green-key.svg") . ' alt="key" class="m-auto" />';
$grayKey = '<img src=' . asset("auditor/icon/gray-key.svg") . ' alt="key" class="m-auto" />';

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

$primaryKey = $indexing = $uniqueKey = $foreignKey = "-";
$primaryKey = $indexKey = $uniqueKey = $foreignKey = "-";


if (in_array($table->COLUMN_NAME, $data['constrain']['primary'])) {
$primaryKey = $greenKey;
} else if (in_array($table->COLUMN_NAME, $data['constrain']['unique'])) {
}

if (in_array($table->COLUMN_NAME, $data['constrain']['unique'])) {
$uniqueKey = $grayKey;
} else if (in_array($table->COLUMN_NAME, $data['constrain']['index'])) {
$indexing = $grayKey;
}

if (in_array($table->COLUMN_NAME, $data['constrain']['index'])) {
$indexKey = $grayKey;
}

foreach ($data['constrain']['foreign'] as $foreign) {
if ($table->COLUMN_NAME === $foreign['column_name']) {
$foreignKeyToottip = '<div class="inline-flex">
if ($table->COLUMN_NAME === $foreign) {
$foreignKeyTooltip = '<div class="inline-flex">
<img src=' . asset("auditor/icon/gray-key.svg") . ' alt="key" class="mr-2">
<div class="relative flex flex-col items-center group">
<svg class="w-5 h-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
Expand All @@ -107,15 +116,101 @@ public function getTableConstraint(string $tableName) : JsonResponse
</div>
</div>
</div>';
$foreignKey = $foreignKeyToottip;
$foreignKey = $foreignKeyTooltip;
}
}

foreach ($constraintList as $constraint) {
switch ($constraint) {
case Constant::CONSTRAINT_PRIMARY_KEY:
if (in_array($table->COLUMN_NAME, $noConstraintFields['integer'])) {
$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 . '`)"/>';
}
break;
case Constant::CONSTRAINT_INDEX_KEY:
if (in_array($table->COLUMN_NAME, $noConstraintFields['mix'])) {
$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 . '`)"/>';
}
break;
case Constant::CONSTRAINT_UNIQUE_KEY:
if (in_array($table->COLUMN_NAME, $noConstraintFields['mix'])) {
$fields = $this->getUniqueFields($tableName, $noConstraintFields['mix']);
if (in_array($table->COLUMN_NAME, $fields)) {
$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 . '`)"/>';
}
}
break;
case Constant::CONSTRAINT_FOREIGN_KEY:
if(in_array($table->COLUMN_NAME, $noConstraintFields['integer'])) {
if(!$this->tableHasValue($tableName)) {
$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.'`)"/>';
}
}
break;
default:
break;
}
}

$response[] = ["column" => $table->COLUMN_NAME, "primaryKey" => $primaryKey, "indexing" => $indexing, "uniqueKey" => $uniqueKey, "foreignKey" => $foreignKey];
$response[] = ["column" => $table->COLUMN_NAME, "primaryKey" => $primaryKey, "indexing" => $indexKey, "uniqueKey" => $uniqueKey, "foreignKey" => $foreignKey];
}

return response()->json(array(
"data" => $response
));
}

/**
* Update the field Constraint
* @param Request
* @return
*/
public function changeConstraint(Request $request): bool
{
$data = $request->all();
$this->addConstraint($data['table_name'], $data['colum_name'], $data['constraint']);
return Constant::STATUS_TRUE;
}

/**
* Get Foreign Key Details
* @return array
*/
public function getForeignKeyTableList(): array
{
return $this->getTableList();
}

/**
* Get Foreign Key Field List
* @param string
* @return array
*/
public function getForeignKeyFieldList(string $tableName): array
{
return $this->getFieldsDetails($tableName);
}

/**
* Add Foreign Key Constraint
* @param Request
* @return mixed
*/
public function addForeignKeyConstraint(Request $request): mixed
{
$data= $request->all();

if($data['reference_table'] === $data['table_name']) {
return __('Lang::messages.constraint.error_message.foreign_selected_table_match', ['foreign' => $data['reference_table'], 'selected' => $data['table_name']]);
}

$referenceFieldType = $this->getFieldDataType($data['reference_table'], $data['reference_field']);
$selectedFieldType = $this->getFieldDataType($data['table_name'], $data['select_field']);
if ($referenceFieldType['data_type'] !== $selectedFieldType['data_type']) {
return __('Lang::messages.constraint.error_message.foreign_not_apply');
}

$this->addConstraint($data['table_name'], $data['select_field'], Constant::CONSTRAINT_FOREIGN_KEY, $data['reference_table'], $data['reference_field']);
return Constant::STATUS_TRUE;
}
}
27 changes: 0 additions & 27 deletions src/Datatable/AuditDatatable.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Lang/en/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
'plural' => 'Table name should be plural.',
'space' => 'Space between words is not advised. Please Use Underscore "_"',
'alphabets' => 'Numbers are not for names and is not advised! Please use alphabets for name.',
'lowercase' => 'Name should be in lowercase.',
'convention' => 'Name should be in lowercase, camelCase or snake_case.',
'datatype_change' => 'Here you can use CHAR datatype instead of VARCHAR if data values in a column are of the same length.',
],
'question' => [
Expand Down
19 changes: 13 additions & 6 deletions src/Traits/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function getConstraintList(string $tableName, array $fields): array
if (!empty($fields['mix'])) {
$constrainList[] = Constant::CONSTRAINT_INDEX_KEY;

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

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

if (empty($result)) {
$uniqueField[] = $field;
$getUniqueQuery = "SELECT * FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = '". $this->getDatabaseName() ."' AND TABLE_NAME = '".$tableName."' AND COLUMN_NAME = '".$field."' AND NON_UNIQUE = 0";
$resultUniqueQuery = DB::select($getUniqueQuery);
if(!$resultUniqueQuery) {
$query = "SELECT `" . $field . "`, COUNT(`" . $field . "`) as count FROM " . $tableName . " GROUP BY `" . $field . "` HAVING COUNT(`" . $field . "`) > 1";
$result = DB::select($query);

if (empty($result)) {
$uniqueField[] = $field;
}
}

}
} catch (Exception $exception) {
Log::error($exception->getMessage());
Expand Down
Loading

0 comments on commit aa3f4e3

Please sign in to comment.