Skip to content
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
4 changes: 2 additions & 2 deletions src/Database/Barry/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
14 changes: 9 additions & 5 deletions src/Database/Migration/Compose/MysqlCompose.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,15 +47,18 @@ private function composeAddMysqlColumn(string $name, array $description): string
$size = 255;
}

// Add column size
// Set the size
if ($size) {
if (in_array($raw_type, ['ENUM', 'CHECK'])) {
$size = (array) $size;
$size = "'" . implode("', '", $size) . "'";
}
$type = sprintf('%s(%s)', $type, $size);
}

// Add column size
if (in_array($raw_type, ['ENUM', 'CHECK'])) {
$check = (array) $check;
$check = "'" . implode("', '", $check) . "'";
$type = sprintf('%s(%s)', $type, $check);
}

// Bind auto increment action
if ($increment) {
$type = sprintf('%s AUTO_INCREMENT', $type);
Expand Down
79 changes: 63 additions & 16 deletions src/Database/Migration/Compose/PgsqlCompose.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
13 changes: 13 additions & 0 deletions src/Database/Migration/SQLGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
42 changes: 37 additions & 5 deletions src/Database/Migration/Shortcut/MixedColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}
}
27 changes: 15 additions & 12 deletions src/Http/Client/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,30 @@ 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);
}

/**
* make post requete
*
* @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);

Expand All @@ -100,17 +103,17 @@ public function post(string $url, array $data = []): Parser

$this->addFields($data);

return new Parser($this->ch);
return new Response($this->ch);
}

/**
* Make put requete
*
* @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);

Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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, "/");
}

Expand Down