From 9c8f63798990ea1b956d4a42a30cacc9a18beb00 Mon Sep 17 00:00:00 2001 From: Franck DAKIA Date: Mon, 22 May 2023 22:20:58 +0000 Subject: [PATCH 1/5] Fixes check row exists from model error --- src/Database/Barry/Builder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Database/Barry/Builder.php b/src/Database/Barry/Builder.php index e4fbd5a9..678210ed 100644 --- a/src/Database/Barry/Builder.php +++ b/src/Database/Barry/Builder.php @@ -53,13 +53,13 @@ public function get(array $columns = []): Model|Collection|null */ public function exists(?string $column = null, mixed $value = null): bool { - if (is_null($column) == null && is_null($value)) { + if (is_null($column) && is_null($value)) { return $this->count() > 0; } // If value is null and column is define // we make the column as value on model primary key name - if (!is_null($column) and is_null($value)) { + if (!is_null($column) && is_null($value)) { $value = $column; $column = (new $this->model())->getKey(); From ce93f03602860d313f9d8262c9a0b4d5b04e65b9 Mon Sep 17 00:00:00 2001 From: Franck DAKIA Date: Mon, 22 May 2023 23:12:39 +0000 Subject: [PATCH 2/5] Fixes the http client --- src/Http/Client/HttpClient.php | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/Http/Client/HttpClient.php b/src/Http/Client/HttpClient.php index 46f7ff8c..4516c187 100644 --- a/src/Http/Client/HttpClient.php +++ b/src/Http/Client/HttpClient.php @@ -62,17 +62,20 @@ public function setBaseUrl(string $url): void * * @param string $url * @param array $data - * @return Parser + * @return Response */ - public function get(string $url, array $data = []): Parser + public function get(string $url, array $data = []): Response { - $params = http_build_query($data); + if (count($data) > 0) { + $params = http_build_query($data); + $url . "?" . $params; + } - $this->init($url . "?" . $params); + $this->init($url); curl_setopt($this->ch, CURLOPT_HTTPGET, true); - return new Parser($this->ch); + return new Response($this->ch); } /** @@ -80,9 +83,9 @@ public function get(string $url, array $data = []): Parser * * @param string $url * @param array $data - * @return Parser + * @return Response */ - public function post(string $url, array $data = []): Parser + public function post(string $url, array $data = []): Response { $this->init($url); @@ -100,7 +103,7 @@ public function post(string $url, array $data = []): Parser $this->addFields($data); - return new Parser($this->ch); + return new Response($this->ch); } /** @@ -108,9 +111,9 @@ public function post(string $url, array $data = []): Parser * * @param string $url * @param array $data - * @return Parser + * @return Response */ - public function put(string $url, array $data = []): Parser + public function put(string $url, array $data = []): Response { $this->init($url); @@ -120,7 +123,7 @@ public function put(string $url, array $data = []): Parser curl_setopt($this->ch, CURLOPT_PUT, true); - return new Parser($this->ch); + return new Response($this->ch); } /** @@ -163,7 +166,7 @@ public function addHeaders(array $headers): HttpClient */ private function init(string $url): void { - if (is_null($this->base_url)) { + if (!is_null($this->base_url)) { $url = $this->base_url . "/" . trim($url, "/"); } From 8e01783b059f26a44e2d3ced768b542e433d459a Mon Sep 17 00:00:00 2001 From: Franck DAKIA Date: Tue, 23 May 2023 08:36:40 +0000 Subject: [PATCH 3/5] Add the addRaw method for migration --- src/Database/Migration/SQLGenerator.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Database/Migration/SQLGenerator.php b/src/Database/Migration/SQLGenerator.php index 0bd28b04..9f82a22f 100644 --- a/src/Database/Migration/SQLGenerator.php +++ b/src/Database/Migration/SQLGenerator.php @@ -98,6 +98,19 @@ public function make(): string return $sql; } + /** + * Add a raw column definiton + * + * @param string $definition + * @return SQLGenerator + */ + public function addRaw(string $definition): SQLGenerator + { + $this->sqls[] = $definition; + + return $this; + } + /** * Add new column in the table * From bcddcd56c8f154b3e88ed1535f6953b0672a1731 Mon Sep 17 00:00:00 2001 From: Franck DAKIA Date: Tue, 23 May 2023 09:34:49 +0000 Subject: [PATCH 4/5] Fixes check statement definition --- .../Migration/Compose/MysqlCompose.php | 11 ++- .../Migration/Compose/PgsqlCompose.php | 79 +++++++++++++++---- .../Migration/Shortcut/MixedColumn.php | 42 ++++++++-- 3 files changed, 105 insertions(+), 27 deletions(-) diff --git a/src/Database/Migration/Compose/MysqlCompose.php b/src/Database/Migration/Compose/MysqlCompose.php index ab64cf5c..6c284e81 100644 --- a/src/Database/Migration/Compose/MysqlCompose.php +++ b/src/Database/Migration/Compose/MysqlCompose.php @@ -28,6 +28,7 @@ private function composeAddMysqlColumn(string $name, array $description): string // Transform attribute $default = $attribute['default'] ?? null; $size = $attribute['size'] ?? false; + $check = $attribute['check'] ?? false; $primary = $attribute['primary'] ?? false; $increment = $attribute['increment'] ?? false; $nullable = $attribute['nullable'] ?? false; @@ -47,12 +48,10 @@ private function composeAddMysqlColumn(string $name, array $description): string } // Add column size - if ($size) { - if (in_array($raw_type, ['ENUM', 'CHECK'])) { - $size = (array) $size; - $size = "'" . implode("', '", $size) . "'"; - } - $type = sprintf('%s(%s)', $type, $size); + if (in_array($raw_type, ['ENUM', 'CHECK'])) { + $check = (array) $check; + $check = "'" . implode("', '", $check) . "'"; + $type = sprintf('%s(%s)', $type, $check); } // Bind auto increment action diff --git a/src/Database/Migration/Compose/PgsqlCompose.php b/src/Database/Migration/Compose/PgsqlCompose.php index 35473c6b..cad7418a 100644 --- a/src/Database/Migration/Compose/PgsqlCompose.php +++ b/src/Database/Migration/Compose/PgsqlCompose.php @@ -74,22 +74,7 @@ private function composeAddPgsqlColumn(string $name, array $description): string } if (in_array($raw_type, ['ENUM', 'CHECK'])) { - $size = (array) $size; - $size = "'" . implode("', '", $size) . "'"; - if ($raw_type == "ENUM") { - $table = preg_replace("/(ies)$/", "y", $this->table); - $table = preg_replace("/(s)$/", "", $table); - - $this->custom_types[] = sprintf( - "CREATE TYPE %s_%s_enum AS ENUM(%s);", - $table, - $name, - $size - ); - $type = sprintf('%s_%s_enum', $table, $name); - } else { - $type = sprintf('TEXT CHECK (%s IN CHECK(%s))', $name, $size); - } + $type = $this->formatCheckOrEnum($name, $raw_type, $attribute); } // Bind precision @@ -159,4 +144,66 @@ private function dropColumnForPgsql(string $name): void $this->sqls[] = trim(sprintf('DROP COLUMN %s', $name)); } } + + /** + * Format the CHECK in ENUM + * + * @param string $name + * @param string $type + * @param array $attribute + * @return void + */ + private function formatCheckOrEnum($name, $type, $attribute): string + { + if ($type == "ENUM") { + $size = (array) $attribute['size']; + $size = "'" . implode("', '", $size) . "'"; + $table = preg_replace("/(ies)$/", "y", $this->table); + $table = preg_replace("/(s)$/", "", $table); + $this->custom_types[] = sprintf( + "CREATE TYPE %s_%s_enum AS ENUM(%s);", + $table, + $name, + $size + ); + + return sprintf('%s_%s_enum', $table, $name); + } + + if (count($attribute["check"]) === 3) { + [$column, $comparaison, $value] = $attribute["check"]; + if (is_array($value)) { + $value = "('" . implode("', '", $value) . "')"; + } + return sprintf('TEXT CHECK ("%s" %s %s)', $column, $comparaison, $value); + } + + [$column, $value] = $attribute["check"]; + + $comparaison = "="; + + if (is_string($value)) { + $value = "'" . addcslashes($value, "'") . "'"; + return sprintf('TEXT CHECK ("%s" %s %s)', $column, $comparaison, $value); + } + + $value = (array) $value; + + if (count($value) > 1) { + $comparaison = "IN"; + + foreach ($value as $key => $item) { + if (is_string($item)) { + $value[$key] = "'" . addcslashes($item, "'") . "'"; + } + } + + $value = "(" . implode(", ", $value) . ")"; + return sprintf('TEXT CHECK ("%s" %s %s)', $column, $comparaison, $value); + } + + $value = end($value); + + return sprintf('TEXT CHECK ("%s" %s %s)', $column, $comparaison, $value); + } } diff --git a/src/Database/Migration/Shortcut/MixedColumn.php b/src/Database/Migration/Shortcut/MixedColumn.php index 9bd729fe..e3b9de7b 100644 --- a/src/Database/Migration/Shortcut/MixedColumn.php +++ b/src/Database/Migration/Shortcut/MixedColumn.php @@ -179,16 +179,20 @@ public function addEnum(string $column, array $attribute = []): SQLGenerator */ public function addCheck(string $column, array $attribute = []): SQLGenerator { - if (!isset($attribute['size'])) { + if (!isset($attribute['check'])) { throw new SQLGeneratorException("The check values should be define."); } - if (!is_array($attribute['size'])) { - throw new SQLGeneratorException("The enum values should be array."); + if (!is_array($attribute['check'])) { + throw new SQLGeneratorException("The check values should be array."); } - if (count($attribute['size']) === 0) { - throw new SQLGeneratorException("The enum values cannot be empty."); + if (count($attribute['check']) === 0) { + throw new SQLGeneratorException("The check values cannot be empty."); + } + + if (count($attribute['check']) === 0) { + throw new SQLGeneratorException("The check values cannot be empty."); } return $this->addColumn($column, 'check', $attribute); @@ -312,6 +316,18 @@ public function changeMacAddress(string $column, array $attribute = []): SQLGene */ public function changeEnum(string $column, array $attribute = []): SQLGenerator { + if (!isset($attribute['size'])) { + throw new SQLGeneratorException("The enum values should be define!"); + } + + if (!is_array($attribute['size'])) { + throw new SQLGeneratorException("The enum values should be array"); + } + + if (count($attribute['size']) === 0) { + throw new SQLGeneratorException("The enum values cannot be empty."); + } + return $this->changeColumn($column, 'enum', $attribute); } @@ -324,6 +340,22 @@ public function changeEnum(string $column, array $attribute = []): SQLGenerator */ public function changeCheck(string $column, array $attribute = []): SQLGenerator { + if (!isset($attribute['check'])) { + throw new SQLGeneratorException("The check values should be define."); + } + + if (!is_array($attribute['check'])) { + throw new SQLGeneratorException("The check values should be array."); + } + + if (count($attribute['check']) === 0) { + throw new SQLGeneratorException("The check values cannot be empty."); + } + + if (count($attribute['check']) === 0) { + throw new SQLGeneratorException("The check values cannot be empty."); + } + return $this->changeColumn($column, 'check', $attribute); } } From ad3d24ebbe73a267cd3c8a7e4320826c2d8d06d1 Mon Sep 17 00:00:00 2001 From: Franck DAKIA Date: Tue, 23 May 2023 09:45:03 +0000 Subject: [PATCH 5/5] Add the column size on mysql --- src/Database/Migration/Compose/MysqlCompose.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Database/Migration/Compose/MysqlCompose.php b/src/Database/Migration/Compose/MysqlCompose.php index 6c284e81..a1dd8d95 100644 --- a/src/Database/Migration/Compose/MysqlCompose.php +++ b/src/Database/Migration/Compose/MysqlCompose.php @@ -47,6 +47,11 @@ private function composeAddMysqlColumn(string $name, array $description): string $size = 255; } + // Set the size + if ($size) { + $type = sprintf('%s(%s)', $type, $size); + } + // Add column size if (in_array($raw_type, ['ENUM', 'CHECK'])) { $check = (array) $check;