Skip to content

Commit

Permalink
feat: add validate and fix when change column name
Browse files Browse the repository at this point in the history
  • Loading branch information
dewanakl committed Feb 25, 2024
1 parent 5980bf3 commit 592fa8e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
37 changes: 23 additions & 14 deletions src/Core/Database/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,8 @@ public function create(): string
public function export(): string|null
{
if ($this->alter == 'ADD' && !Schema::$dump) {
$db = App::get()->singleton(DataBase::class);

foreach ($this->columns as $value) {
if ($this->type == 'pgsql') {
$query = 'SELECT column_name FROM information_schema.columns WHERE table_name=\'' . $this->table . '\' and column_name=\'' . $value . '\';';
} else {
$query = 'SHOW COLUMNS FROM ' . $this->table . ' WHERE Field = \'' . $value . '\';';
}

$db->query($query);
$db->execute();
$column = $db->rowCount();

if ($column != 0) {
if ($this->checkColumn($value)) {
$this->query = [];
$this->alter = null;
$this->columns = [];
Expand All @@ -135,6 +123,27 @@ public function export(): string|null
return $query;
}

/**
* Check column in this table.
*
* @param string $value
* @return bool
*/
public function checkColumn(string $value): bool
{
$db = App::get()->singleton(DataBase::class);

if ($this->type == 'pgsql') {
$query = 'SELECT column_name FROM information_schema.columns WHERE table_name=\'' . $this->table . '\' and column_name=\'' . $value . '\';';
} else {
$query = 'SHOW COLUMNS FROM ' . $this->table . ' WHERE Field = \'' . $value . '\';';
}

$db->query($query);
$db->execute();
return $db->rowCount() != 0;
}

/**
* Get index paling akhir.
*
Expand Down Expand Up @@ -400,7 +409,7 @@ public function dropForeign(string $name): void
*/
public function renameColumn(string $from, string $to): void
{
$this->alter = 'RENAME';
$this->alter = 'RENAME COLUMN';
$this->query[$this->getLastArray()] = $from . ' TO ' . $to;
$this->columns[] = $from;
}
Expand Down
7 changes: 4 additions & 3 deletions src/Core/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,17 @@ public static function setRouteFromFile(): void
/**
* Isi url dari cache atau route.
*
* @return void
* @return bool
*/
public static function setRouteFromCacheIfExist(): void
public static function setRouteFromCacheIfExist(): bool
{
try {
$route = (array) @require_once base_path('/cache/routes/routes.php');
static::router()->setRoutes($route);
return true;
} catch (Throwable) {
error_clear_last();
static::setRouteFromFile();
return false;
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/Core/Valid/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ private function validateRequest(string $param, mixed $value, string $rule): voi
$this->__set($param, trim($value ?? ''));
break;

case $rule == 'alpha_num':
$this->__set($param, preg_replace('/[^A-Za-z0-9]/', '', $value ?? ''));
break;

case str_contains($rule, 'min'):
$min = intval(explode(':', $rule)[1] ?? 0);
if ((is_int($value) || is_float($value) ? intval($value) : strlen($value ?? '')) < $min) {
Expand Down

0 comments on commit 592fa8e

Please sign in to comment.