Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge latest code. #4

Merged
merged 5 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Commands/DBAuditCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
}
}
}
50 changes: 23 additions & 27 deletions src/Commands/DBConstraintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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);
Expand All @@ -51,15 +51,15 @@ 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;
} else {
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
Expand All @@ -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)
]
];

Expand Down Expand Up @@ -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;
Expand All @@ -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]));
Expand All @@ -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);
}
}
}
Expand All @@ -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)]));
Expand All @@ -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'));
}
Expand All @@ -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);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/Commands/DBStandardCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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']));
Expand All @@ -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']));
Expand Down
45 changes: 45 additions & 0 deletions src/Commands/DBSummaryCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Vcian\LaravelDBAuditor\Commands;

use Illuminate\Console\Command;
use Vcian\LaravelDBAuditor\Traits\DBConnection;
use function Termwind\{render};

class DBSummaryCommand extends Command
{
use DBConnection;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'db:summary';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Execute the console command.
*/
public function handle()
{
$this->table(
['Database Name', 'Size', 'Table Count', 'Engin', 'Character Set'],
[[
$this->getDatabaseName(),
$this->getDatabaseSize(),
count($this->getTableList()),
$this->getDatabaseEngin(),
$this->getCharacterSetName()
]]
);

return self::SUCCESS;
}
}
2 changes: 2 additions & 0 deletions src/Constants/Constant.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]+/';

Expand Down
3 changes: 2 additions & 1 deletion src/Providers/DBAuditorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
];

/**
Expand Down
66 changes: 7 additions & 59 deletions src/Services/AuditService.php → src/Traits/Audit.php
Original file line number Diff line number Diff line change
@@ -1,70 +1,18 @@
<?php

namespace Vcian\LaravelDBAuditor\Services;
namespace Vcian\LaravelDBAuditor\Traits;

use Exception;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;
use Vcian\LaravelDBAuditor\Constants\Constant;
use Vcian\LaravelDBAuditor\Traits\DBConnection;

class AuditService
trait Audit
{
public function __construct(protected DBConnectionService $dBConnectionService)
{
//
}

/**
* Get All Table List
* @return array
*/
public function getTablesList(): array
{
return $this->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
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 [];
}

Expand Down
Loading