diff --git a/src/Commands/DBAuditCommand.php b/src/Commands/DBAuditCommand.php index 8d0dae5..0c7c2fd 100644 --- a/src/Commands/DBAuditCommand.php +++ b/src/Commands/DBAuditCommand.php @@ -27,7 +27,7 @@ class DBAuditCommand extends Command */ public function handle(): void { - $commandSelect = $this->choice('Please Select feature which would you like to do', [Constant::STANDARD_COMMAND, Constant::CONSTRAINT_COMMAND]); + $commandSelect = $this->choice('Please Select feature which would you like to do', [Constant::STANDARD_COMMAND, Constant::CONSTRAINT_COMMAND, Constant::SUMMARY_COMMAND]); if ($commandSelect === Constant::STANDARD_COMMAND) { $this->call('db:standard'); @@ -36,5 +36,9 @@ public function handle(): void if ($commandSelect === Constant::CONSTRAINT_COMMAND) { $this->call('db:constraint'); } + + if ($commandSelect === Constant::SUMMARY_COMMAND) { + $this->call('db:summary'); + } } } diff --git a/src/Commands/DBConstraintCommand.php b/src/Commands/DBConstraintCommand.php index 97b3b27..d355af3 100644 --- a/src/Commands/DBConstraintCommand.php +++ b/src/Commands/DBConstraintCommand.php @@ -6,12 +6,13 @@ use Illuminate\Console\Command; use Illuminate\Support\Facades\Log; use Vcian\LaravelDBAuditor\Constants\Constant; -use Vcian\LaravelDBAuditor\Services\AuditService; +use Vcian\LaravelDBAuditor\Traits\Audit; use function Termwind\{render}; use function Termwind\{renderUsing}; class DBConstraintCommand extends Command { + use Audit; /** * @var bool */ @@ -37,11 +38,10 @@ class DBConstraintCommand extends Command public function handle(): int|string { try { - $auditService = app(AuditService::class); - + $tableName = $this->components->choice( __('Lang::messages.constraint.question.table_selection'), - $auditService->getTablesList() + $this->getTableList() ); $this->displayTable($tableName); @@ -51,7 +51,7 @@ public function handle(): int|string $continue = Constant::STATUS_TRUE; do { - $noConstraintFields = $auditService->getNoConstraintFields($tableName); + $noConstraintFields = $this->getNoConstraintFields($tableName); if (empty($noConstraintFields)) { $continue = Constant::STATUS_FALSE; @@ -59,7 +59,7 @@ public function handle(): int|string if ($this->confirm(__('Lang::messages.constraint.question.continue'))) { $this->skip = Constant::STATUS_FALSE; - $constraintList = $auditService->getConstraintList($tableName, $noConstraintFields); + $constraintList = $this->getConstraintList($tableName, $noConstraintFields); $selectConstrain = $this->choice( __('Lang::messages.constraint.question.constraint_selection'), $constraintList @@ -86,18 +86,17 @@ public function handle(): int|string */ public function displayTable(string $tableName): void { - $auditService = app(AuditService::class); $data = [ "table" => $tableName, - "size" => $auditService->getTableSize($tableName), - "fields" => $auditService->getTableFields($tableName), - 'field_count' => count($auditService->getTableFields($tableName)), + "size" => $this->getTableSize($tableName), + "fields" => $this->getFieldsDetails($tableName), + 'field_count' => count($this->getFieldsDetails($tableName)), 'constrain' => [ - 'primary' => $auditService->getConstraintField($tableName, Constant::CONSTRAINT_PRIMARY_KEY), - 'unique' => $auditService->getConstraintField($tableName, Constant::CONSTRAINT_UNIQUE_KEY), - 'foreign' => $auditService->getConstraintField($tableName, Constant::CONSTRAINT_FOREIGN_KEY), - 'index' => $auditService->getConstraintField($tableName, Constant::CONSTRAINT_INDEX_KEY) + 'primary' => $this->getConstraintField($tableName, Constant::CONSTRAINT_PRIMARY_KEY), + 'unique' => $this->getConstraintField($tableName, Constant::CONSTRAINT_UNIQUE_KEY), + 'foreign' => $this->getConstraintField($tableName, Constant::CONSTRAINT_FOREIGN_KEY), + 'index' => $this->getConstraintField($tableName, Constant::CONSTRAINT_INDEX_KEY) ] ]; @@ -131,23 +130,22 @@ public function successMessage(string $message): void */ public function foreignKeyConstraint(string $tableName, string $selectField): void { - $auditService = app(AuditService::class); $foreignContinue = Constant::STATUS_FALSE; $referenceField = Constant::NULL; $fields = Constant::ARRAY_DECLARATION; do { - $referenceTable = $this->anticipate(__('Lang::messages.constraint.question.foreign_table'), $auditService->getTablesList()); + $referenceTable = $this->anticipate(__('Lang::messages.constraint.question.foreign_table'), $this->getTablesList()); - if ($referenceTable && $auditService->checkTableExistOrNot($referenceTable)) { + if ($referenceTable && $this->checkTableExistOrNot($referenceTable)) { - foreach ($auditService->getTableFields($referenceTable) as $field) { + foreach ($this->getTableFields($referenceTable) as $field) { $fields[] = $field->COLUMN_NAME; } do { $referenceField = $this->anticipate(__('Lang::messages.constraint.question.foreign_field'), $fields); - if (!$referenceField || !$auditService->checkFieldExistOrNot($referenceTable, $referenceField)) { + if (!$referenceField || !$this->checkFieldExistOrNot($referenceTable, $referenceField)) { $this->errorMessage(__('Lang::messages.constraint.error_message.field_not_found')); } else { $foreignContinue = Constant::STATUS_TRUE; @@ -158,8 +156,8 @@ public function foreignKeyConstraint(string $tableName, string $selectField): vo } } while ($foreignContinue === Constant::STATUS_FALSE); - $referenceFieldType = $auditService->getFieldDataType($referenceTable, $referenceField); - $selectedFieldType = $auditService->getFieldDataType($tableName, $selectField); + $referenceFieldType = $this->getFieldDataType($referenceTable, $referenceField); + $selectedFieldType = $this->getFieldDataType($tableName, $selectField); if ($referenceTable === $tableName) { $this->errorMessage(__('Lang::messages.constraint.error_message.foreign_selected_table_match', ['foreign' => $referenceTable, 'selected' => $tableName])); @@ -181,7 +179,7 @@ public function foreignKeyConstraint(string $tableName, string $selectField): vo '); $this->errorMessage(__('Lang::messages.constraint.error_message.foreign_not_apply')); } else { - $auditService->addConstraint($tableName, $selectField, Constant::CONSTRAINT_FOREIGN_KEY, $referenceTable, $referenceField); + $this->addConstraint($tableName, $selectField, Constant::CONSTRAINT_FOREIGN_KEY, $referenceTable, $referenceField); } } } @@ -195,10 +193,8 @@ public function foreignKeyConstraint(string $tableName, string $selectField): vo public function selectedConstraint(string $selectConstrain, array $noConstraintFields, string $tableName): void { - $auditService = app(AuditService::class); - if ($selectConstrain === Constant::CONSTRAINT_FOREIGN_KEY) { - $tableHasValue = $auditService->tableHasValue($tableName); + $tableHasValue = $this->tableHasValue($tableName); if ($tableHasValue) { $this->errorMessage(__('Lang::messages.constraint.error_message.constraint_not_apply', ['constraint' => strtolower($selectConstrain)])); @@ -213,7 +209,7 @@ public function selectedConstraint(string $selectConstrain, array $noConstraintF } if ($selectConstrain === Constant::CONSTRAINT_UNIQUE_KEY) { - $fields = $auditService->getUniqueFields($tableName, $noConstraintFields['mix']); + $fields = $this->getUniqueFields($tableName, $noConstraintFields['mix']); if (empty($fields)) { $this->errorMessage(__('Lang::messages.constraint.error_message.unique_constraint_not_apply')); } @@ -228,7 +224,7 @@ public function selectedConstraint(string $selectConstrain, array $noConstraintF if ($selectConstrain === Constant::CONSTRAINT_FOREIGN_KEY) { $this->foreignKeyConstraint($tableName, $selectField); } else { - $auditService->addConstraint($tableName, $selectField, $selectConstrain); + $this->addConstraint($tableName, $selectField, $selectConstrain); } } } diff --git a/src/Commands/DBStandardCommand.php b/src/Commands/DBStandardCommand.php index 1c7a6ed..cf4cfff 100644 --- a/src/Commands/DBStandardCommand.php +++ b/src/Commands/DBStandardCommand.php @@ -4,11 +4,12 @@ use Illuminate\Console\Command; use Vcian\LaravelDBAuditor\Constants\Constant; -use Vcian\LaravelDBAuditor\Services\RuleService; +use Vcian\LaravelDBAuditor\Traits\Rules; use function Termwind\{render}; class DBStandardCommand extends Command { + use Rules; /** * The name and signature of the console command. * @@ -28,8 +29,7 @@ class DBStandardCommand extends Command */ public function handle(): ?int { - $ruleService = app(RuleService::class); - $tableStatus = $ruleService->tablesRule(); + $tableStatus = $this->tablesRule(); if (!$tableStatus) { render(view('DBAuditor::error_message', ['message' => 'No Table Found'])); @@ -40,13 +40,13 @@ public function handle(): ?int $continue = Constant::STATUS_TRUE; do { - $tableName = $this->anticipate('Please enter table name if you want to see the table report', $ruleService->getTableList()); + $tableName = $this->anticipate('Please enter table name if you want to see the table report', $this->getTableList()); if (empty($tableName)) { return render(view('DBAuditor::error_message', ['message' => 'No Table Found'])); } - $tableStatus = $ruleService->tableRules($tableName); + $tableStatus = $this->tableRules($tableName); if (!$tableStatus) { return render(view('DBAuditor::error_message', ['message' => 'No Table Found'])); diff --git a/src/Commands/DBSummaryCommand.php b/src/Commands/DBSummaryCommand.php new file mode 100644 index 0000000..f70780f --- /dev/null +++ b/src/Commands/DBSummaryCommand.php @@ -0,0 +1,45 @@ +table( + ['Database Name', 'Size', 'Table Count', 'Engin', 'Character Set'], + [[ + $this->getDatabaseName(), + $this->getDatabaseSize(), + count($this->getTableList()), + $this->getDatabaseEngin(), + $this->getCharacterSetName() + ]] + ); + + return self::SUCCESS; + } +} diff --git a/src/Constants/Constant.php b/src/Constants/Constant.php index 34e675d..95d0c48 100644 --- a/src/Constants/Constant.php +++ b/src/Constants/Constant.php @@ -38,6 +38,8 @@ class Constant public const STANDARD_COMMAND = 'STANDARD'; public const CONSTRAINT_COMMAND = 'CONSTRAINT'; + public const SUMMARY_COMMAND = 'SUMMARY'; + public const NULL = null; public const NUMERIC_PATTERN = '/[0-9]+/'; diff --git a/src/Providers/DBAuditorServiceProvider.php b/src/Providers/DBAuditorServiceProvider.php index d91b9ba..ab132ac 100644 --- a/src/Providers/DBAuditorServiceProvider.php +++ b/src/Providers/DBAuditorServiceProvider.php @@ -9,7 +9,8 @@ class DBAuditorServiceProvider extends ServiceProvider protected array $commands = [ 'Vcian\LaravelDBAuditor\Commands\DBAuditCommand', 'Vcian\LaravelDBAuditor\Commands\DBStandardCommand', - 'Vcian\LaravelDBAuditor\Commands\DBConstraintCommand' + 'Vcian\LaravelDBAuditor\Commands\DBConstraintCommand', + 'Vcian\LaravelDBAuditor\Commands\DBSummaryCommand' ]; /** diff --git a/src/Services/AuditService.php b/src/Traits/Audit.php similarity index 85% rename from src/Services/AuditService.php rename to src/Traits/Audit.php index be70165..0ff0706 100644 --- a/src/Services/AuditService.php +++ b/src/Traits/Audit.php @@ -1,6 +1,6 @@ dBConnectionService->getTableList(); - } - - /** - * Get Table Fields - * @param string $tableName - * @return array - */ - public function getTableFields(string $tableName): array - { - return $this->dBConnectionService->getFieldsDetails($tableName); - } - - /** - * Get Table Size - * @param string $tableName - * @return string $size - * @return string - */ - public function getTableSize(string $tableName): string - { - return $this->dBConnectionService->getTableSize($tableName); - } - - /** - * Check table exist or not - * @param string $tableName - * @return bool - */ - public function checkTableExistOrNot(string $tableName): bool - { - return $this->dBConnectionService->checkTableExist($tableName); - } - - /** - * @param string $tableName - * @param string $field - * @return array - */ - public function getFieldDataType(string $tableName, string $field): array - { - return $this->dBConnectionService->getFieldDataType($tableName, $field); - } + use DBConnection; /** * Check field exist or not @@ -74,7 +22,7 @@ public function getFieldDataType(string $tableName, string $field): array */ public function checkFieldExistOrNot(string $tableName, string $field): bool { - $fields = $this->dBConnectionService->getFields($tableName); + $fields = $this->getFields($tableName); if (in_array($field, $fields)) { return Constant::STATUS_TRUE; } @@ -95,7 +43,7 @@ public function getNoConstraintFields(string $tableName): array foreach ($fieldList as $field) { if (!in_array($field->DATA_TYPE, Constant::RESTRICT_DATATYPE)) { - if (!$this->dBConnectionService->checkFieldHasIndex($tableName, $field->COLUMN_NAME)) { + if (!$this->checkFieldHasIndex($tableName, $field->COLUMN_NAME)) { if (str_contains($field->DATA_TYPE, "int")) { $fields['integer'][] = $field->COLUMN_NAME; } @@ -155,7 +103,7 @@ public function getConstraintField(string $tableName, string $input): array try { $constraintFields = Constant::ARRAY_DECLARATION; - if (!$this->dBConnectionService->checkTableExist($tableName)) { + if (!$this->checkTableExist($tableName)) { return []; } diff --git a/src/Services/DBConnectionService.php b/src/Traits/DBConnection.php similarity index 68% rename from src/Services/DBConnectionService.php rename to src/Traits/DBConnection.php index 47d79b9..740b197 100644 --- a/src/Services/DBConnectionService.php +++ b/src/Traits/DBConnection.php @@ -1,13 +1,13 @@ getDatabaseName() . "' AND `TABLE_NAME`= '" . $tableName . "' "); } catch (Exception $exception) { Log::error($exception->getMessage()); } @@ -99,7 +99,7 @@ public function getTableSize(string $tableName): string $query = 'SELECT ROUND(((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024),2) AS `size` FROM information_schema.TABLES WHERE - TABLE_SCHEMA = "' . env('DB_DATABASE') . '" AND TABLE_NAME = "' . $tableName . '" + TABLE_SCHEMA = "' . $this->getDatabaseName() . '" AND TABLE_NAME = "' . $tableName . '" ORDER BY (DATA_LENGTH + INDEX_LENGTH) DESC'; $result = DB::select($query); @@ -110,7 +110,7 @@ public function getTableSize(string $tableName): string } catch (Exception $exception) { Log::error($exception->getMessage()); } - return Constant::STATUS_FALSE; + return Constant::NULL; } /** @@ -123,7 +123,7 @@ public function getFieldDataType(string $tableName, string $fieldName): array|bo { try { $query = "SELECT `DATA_TYPE`, `CHARACTER_MAXIMUM_LENGTH`, `NUMERIC_PRECISION`, `NUMERIC_SCALE` FROM `INFORMATION_SCHEMA`.`COLUMNS` - WHERE `TABLE_SCHEMA`= '" . env('DB_DATABASE') . "' AND `TABLE_NAME`= '" . $tableName . "' AND `COLUMN_NAME` = '" . $fieldName . "' "; + WHERE `TABLE_SCHEMA`= '" . $this->getDatabaseName() . "' AND `TABLE_NAME`= '" . $tableName . "' AND `COLUMN_NAME` = '" . $fieldName . "' "; $dataType = DB::select($query)[0]; @@ -156,7 +156,7 @@ public function getFieldDataType(string $tableName, string $fieldName): array|bo public function checkFieldHasIndex(string $tableName, string $fieldName): bool { try { - $query = "SHOW INDEX FROM ".env('DB_DATABASE').".".$tableName.""; + $query = "SHOW INDEX FROM ".$this->getDatabaseName().".".$tableName.""; $fieldConstraints = DB::select($query); foreach($fieldConstraints as $fieldConstraint) { @@ -169,4 +169,63 @@ public function checkFieldHasIndex(string $tableName, string $fieldName): bool } return Constant::STATUS_FALSE; } + + public function getDatabaseName() + { + return DB::connection()->getDatabaseName(); + } + + public function getDatabaseSize() + { + try { + $query = 'SELECT table_schema as db_name, ROUND(SUM(data_length + index_length) / 1024 / 1024, 1) "size" + FROM information_schema.tables + where table_schema = "'. $this->getDatabaseName() .'" GROUP BY table_schema'; + + $result = DB::select($query); + + if ($result) { + return $result[0]->size; + } + } catch (Exception $exception) { + Log::error($exception->getMessage()); + } + return Constant::NULL; + + } + + public function getDatabaseEngin() + { + try { + $query = 'SELECT engine FROM information_schema.Tables where TABLE_SCHEMA = "'. $this->getDatabaseName() .'" Limit 1'; + + $result = DB::select($query); + + if ($result) { + return $result[0]->ENGINE; + } + + } catch (Exception $exception) { + Log::error($exception->getMessage()); + } + return Constant::NULL; + } + + public function getCharacterSetName() + { + try { + $query = 'SELECT DEFAULT_CHARACTER_SET_NAME + FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = "'. $this->getDatabaseName() .'"'; + + $result = DB::select($query); + + if ($result) { + return $result[0]->DEFAULT_CHARACTER_SET_NAME; + } + + } catch (Exception $exception) { + Log::error($exception->getMessage()); + } + return Constant::NULL; + } } diff --git a/src/Services/NamingRuleService.php b/src/Traits/NamingRules.php similarity index 97% rename from src/Services/NamingRuleService.php rename to src/Traits/NamingRules.php index d10809f..21a4052 100644 --- a/src/Services/NamingRuleService.php +++ b/src/Traits/NamingRules.php @@ -1,11 +1,11 @@ dBConnectionService->getTableList(); - } - /** * Check table name rules * @return array @@ -40,7 +25,7 @@ public function tablesRule(): array { $checkTableStandard = Constant::ARRAY_DECLARATION; try { - $tableList = $this->dBConnectionService->getTableList(); + $tableList = $this->getTableList(); foreach ($tableList as $tableName) { $status = $this->checkStatus($tableName); $size = $this->getTableSize($tableName); @@ -86,13 +71,13 @@ public function checkRules(string $name, string $type = null): array { $messages = Constant::ARRAY_DECLARATION; try { - $checkLowerCase = $this->namingRuleService->nameOnlyLowerCase($name); - $checkSpace = $this->namingRuleService->nameHasNoSpace($name); - $checkAlphabets = $this->namingRuleService->nameHasOnlyAlphabets($name); + $checkLowerCase = $this->nameOnlyLowerCase($name); + $checkSpace = $this->nameHasNoSpace($name); + $checkAlphabets = $this->nameHasOnlyAlphabets($name); if ($type === Constant::TABLE_RULES) { - $checkLength = $this->namingRuleService->nameHasFixLength($name); - $checkNamePlural = $this->namingRuleService->nameAlwaysPlural($name); + $checkLength = $this->nameHasFixLength($name); + $checkNamePlural = $this->nameAlwaysPlural($name); if (!$checkLength) { $messages[] = __('Lang::messages.standard.error_message.length'); @@ -130,11 +115,11 @@ public function fieldRules(string $tableName): array { $checkFields = Constant::ARRAY_DECLARATION; try { - $fields = $this->dBConnectionService->getFields($tableName); + $fields = $this->getFields($tableName); foreach ($fields as $field) { $checkFields[$field] = $this->checkRules($field, Constant::FIELD_RULES); - $dataTypeDetails = $this->dBConnectionService->getFieldDataType($tableName, $field); + $dataTypeDetails = $this->getFieldDataType($tableName, $field); $checkFields[$field]['datatype'] = $dataTypeDetails; if ($dataTypeDetails['data_type'] === Constant::DATATYPE_VARCHAR && $dataTypeDetails['size'] <= Constant::DATATYPE_VARCHAR_SIZE) { $checkFields[$field]['suggestion'] = __('Lang::messages.standard.error_message.datatype_change'); @@ -146,16 +131,6 @@ public function fieldRules(string $tableName): array return $checkFields; } - /** - * Get Table Size - * @param string $tableName - * @return string - */ - public function getTableSize(string $tableName): string - { - return $this->dBConnectionService->getTableSize($tableName); - } - /** * Check rules for single table and check table exist or not * @param string $tableName @@ -166,7 +141,7 @@ public function tableRules(string $tableName): array|bool $checkTableStatus = Constant::ARRAY_DECLARATION; try { if ($tableName) { - $tableExist = $this->dBConnectionService->checkTableExist($tableName); + $tableExist = $this->checkTableExist($tableName); if (!$tableExist) { return Constant::STATUS_FALSE; diff --git a/src/views/fail_standard_table.blade.php b/src/views/fail_standard_table.blade.php index cca18b9..419042f 100644 --- a/src/views/fail_standard_table.blade.php +++ b/src/views/fail_standard_table.blade.php @@ -36,10 +36,12 @@