Skip to content

Commit

Permalink
fix some issues and update classes
Browse files Browse the repository at this point in the history
  • Loading branch information
BMTmohammedtaha committed Sep 25, 2023
1 parent d0d730e commit 40bcb77
Show file tree
Hide file tree
Showing 9 changed files with 297 additions and 132 deletions.
116 changes: 53 additions & 63 deletions src/Build/ColumnQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
*
* Represents a query builder for generating SQL column-related statements.
*
* @package Effectra\SqlQuery\Build
*/
class ColumnQueryBuilder extends Attribute
{

use QueryBuilderTrait;

private array $check_list = [];
/**
* ColumnQueryBuilder constructor.
*
Expand All @@ -31,6 +33,11 @@ public function __construct(protected array $attributes, protected Syntax $synta
{
}

public function getCheckList(): array
{
return $this->check_list;
}

/**
* Get the name of the column.
*
Expand Down Expand Up @@ -73,7 +80,7 @@ public function dataType(): string
$type = $this->getAttribute('data_type');
$values = $this->getAttribute('datatype_values');

if(!method_exists(DataTypes::class,$type)){
if (!method_exists(DataTypes::class, $type)) {
throw new \Exception("data type not exists", 1);
}

Expand Down Expand Up @@ -116,14 +123,12 @@ public function constraints(): string
$result[] = $this->autoIncrement();
}

$result[] = $this->key();

if ($this->constraintsHas('unsigned')) {
$result[] = $this->unsigned();
}

if ($this->constraintsHas('unique')) {
$result[] = $this->unique();
}

if ($this->hasAttribute('collation_name')) {
$result[] = $this->collationName();
}
Expand All @@ -136,14 +141,11 @@ public function constraints(): string
$result[] = $this->visible();
}

if ($this->hasAttribute('check')) {
$result[] = $this->check();
}


return join(' ', $result);
}

/**
/**
* Check if the column is nullable or not.
*
* @return string The "NOT NULL" constraint if the column is not nullable, or the "NULL" constraint if nullable.
Expand All @@ -166,10 +168,35 @@ public function nullable()
*/
public function autoIncrement()
{
return $this->syntax->getKey('auto_increment', 2);
return (string) match($this->syntax->getDriver()){
Driver::MySQL => $this->syntax->getKey('auto_increment', 2) ,
Driver::PostgreSQL => $this->syntax->getKey('auto_increment', 2) ,
Driver::SQLite => $this->syntax->getKey('auto_increment', 2),
};

}

/**
* Get the "KEY" constraint for the column.
*
* @return string The "KEY" constraint.
*/
public function key()
{
$key_query = [];

if ($this->constraintsHas('primary_key')) {
$key_query[] = $this->syntax->getKey('primary', 2);
}

if ($this->constraintsHas('unique_key')) {
$key_query[] = $this->syntax->getKey('unique', 2);
}

return join(' ', $key_query);
}

/**
/**
* Get the "UNSIGNED" constraint for the column.
*
* @return string The "UNSIGNED" constraint.
Expand All @@ -179,7 +206,7 @@ public function unsigned()
return $this->syntax->getCommand('unsigned', 2);
}

/**
/**
* Get the collation name for the column, if specified.
*
* @return string The collation name for the column, including character set and collate statement.
Expand All @@ -202,17 +229,7 @@ public function collationName()
return '';
}

/**
* Get the "UNIQUE" constraint for the column.
*
* @return string The "UNIQUE" constraint.
*/
public function unique()
{
return $this->syntax->getKey('unique', 2);
}

/**
/**
* Get the default value constraint for the column.
*
* @return string The default value constraint.
Expand All @@ -224,7 +241,7 @@ public function defaultValue(): string
return $this->syntax->getCommand('default', 1) . $default_value;
}

/**
/**
* Get the visibility constraint for the column.
*
* @return string The visibility constraint, either "VISIBLE" or "INVISIBLE".
Expand All @@ -242,43 +259,15 @@ public function visible(): string
*/
public function check(): string
{
if($this->syntax->getDriver() === Driver::SQLite){
return '';
}
$exprs = $this->getAttribute('check') ;
if(! $exprs){
return '';
}

$result = '';
$exprs_result = [];

if(is_array($exprs)){
foreach ($exprs as $expr) {
if(empty($expr)){
throw new \Exception("Error Processing Query, check expression is empty");
}
if($expr === 'json'){
$expr = "json_valid({$this->columnName()})";
}
$exprs_result[] = $expr;
}
}


$exprs = $this->getAttribute('check');
$check_sort = $this->getAttribute('check_sort') ?? [];
array_shift($check_sort);

$sortedSyntax = array_map(fn ($op) => $this->syntax->getCommand($op, 1),$check_sort);

foreach ($exprs_result as $key => $condition) {
if ($key > 0 && $condition !== null) {
$result .= $sortedSyntax[$key - 1] ?? $this->syntax->getCommand('and', 1);
}
$result .= $condition;
}
$result= $this->checkQuery($this->columnName(),$exprs,$check_sort);

return $this->syntax->getCommand('check', 1) . sprintf('(%s)',
$result
return $this->syntax->getCommand('check',2) . sprintf(
'(%s)',
$result
);
}

Expand Down Expand Up @@ -334,17 +323,18 @@ public function getValueByKey($attribute, $key): mixed
public function build(): string
{
return sprintf(
'%s %s%s %s %s %s',
'%s %s%s %s %s %s %s',
$this->columnName(),
$this->set(),
$this->dataType(),
$this->size(),
$this->nullable(),
$this->constraints()
$this->constraints(),
$this->check()
);
}

/**
/**
* Convert the query builder to its string representation.
*
* @return string The string representation of the query builder.
Expand Down
91 changes: 72 additions & 19 deletions src/Build/DatabaseQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
use Effectra\SqlQuery\Attribute;
use Effectra\SqlQuery\Driver;
use Effectra\SqlQuery\Operations\Alter;
use Effectra\SqlQuery\Operations\Select;
use Effectra\SqlQuery\Operations\Info;
use Effectra\SqlQuery\Syntax;

/**
* Class DatabaseQueryBuilder
*
* Represents a query builder for generating SQL database-related statements (e.g., CREATE DATABASE, DROP DATABASE, etc.).
*
* @package Effectra\SqlQuery\Build
*/
class DatabaseQueryBuilder extends Attribute
{
Expand All @@ -37,7 +36,56 @@ public function __construct(protected array $attributes, protected Syntax $synta
*/
public function create(): string
{
return $this->syntax->getCommand('createDatabase', 1) . $this->getAttribute('db_name');

$query = $this->syntax->getCommand('createDatabase', 1) . $this->getAttribute('db_name');

$options = $this->getAttribute('options') ?? [];


if($this->syntax->getDriver() === Driver::MySQL){

if(isset($options['character'])){
$query .= $this->syntax->getCommand('character', 1) . $this->syntax->getCommand('set', 1). $options['character'];
}
if(isset($options['collate'])){
$query .= $this->syntax->getCommand('collate', 1) . $options['collate'];
}
}

if($this->syntax->getDriver() === Driver::MySQL){

if(isset($options['encoding'])){
$query .= $this->syntax->getCommand('encoding', 1) . $this->syntax->getOperator('equal', 1). $options['encoding'];
}
if(isset($options['lc_collate'])){
$query .= $this->syntax->getCommand('lc_collate', 1) . $this->syntax->getOperator('equal', 1). $options['lc_collate'];
}
if(isset($options['lc_ctype'])){
$query .= $this->syntax->getCommand('lc_ctype', 1) . $this->syntax->getOperator('equal', 1). $options['lc_ctype'];
}
if(isset($options['owner'])){
$query .= $this->syntax->getCommand('owner', 1) . $this->syntax->getOperator('equal', 1). $options['owner'];
}
if(isset($options['template'])){
$query .= $this->syntax->getCommand('template', 1) . $this->syntax->getOperator('equal', 1). $options['template'];
}
if(isset($options['connection_limit'])){
$query .= $this->syntax->getCommand('connection_limit', 1) . $this->syntax->getOperator('equal', 1). $options['connection_limit'];
}

}


$query = (string) match($this->syntax->getDriver()){
Driver::MySQL => $query,
Driver::PostgreSQL => $query,
Driver::SQLite => '',
};

if(empty($query)){
throw new \Exception("Error Processing Query, driver '{$this->syntax->getDriver()}' doesn't has statement for create database");
}
return $query;
}

/**
Expand All @@ -47,7 +95,16 @@ public function create(): string
*/
public function drop(): string
{
return $this->syntax->getCommand('dropDatabase', 1) . $this->getAttribute('db_name');
$query = (string) match($this->syntax->getDriver()){
Driver::MySQL => $this->syntax->getCommand('dropDatabase', 1) . $this->getAttribute('db_name'),
Driver::PostgreSQL => '',
Driver::SQLite => '',
};

if(empty($query)){
throw new \Exception("Error Processing Query, driver '{$this->syntax->getDriver()}' doesn't has statement for drop database");
}
return $query;
}

/**
Expand All @@ -58,11 +115,16 @@ public function drop(): string
*/
public function rename(): string
{
$driver = $this->syntax->getDriver();
if ($driver !== Driver::PostgreSQL) {
throw new \Exception("Error Processing Query, driver '$driver' doesn't has statement for rename database");
$query = (string) match($this->syntax->getDriver()){
Driver::MySQL => (new Alter())->database($this->getAttribute('db_name'))->renameDatabase($this->getAttribute('rename')),
Driver::PostgreSQL => '',
Driver::SQLite => '',
};

if(empty($query)){
throw new \Exception("Error Processing Query, driver '{$this->syntax->getDriver()}' doesn't has statement for rename database");
}
return (string) (new Alter())->database($this->getAttribute('db_name'))->renameDB($this->getAttribute('rename'));
return $query;
}

/**
Expand All @@ -73,17 +135,7 @@ public function rename(): string
*/
public function getTables(): string
{
$driver = $this->syntax->getDriver();
if ($driver === Driver::MySQL) {
return $this->syntax->getCommand('show', 1) .$this->syntax->getCommand('tables', 1) ;
}
if ($driver === Driver::PostgreSQL) {
return (string) (new Select('table_name'))->columns(['table_schema'])->from('information_schema.tables')->where(['table_schema '=>'public']);
}
if ($driver === Driver::SQLite) {
return (string) (new Select(''))->columns(['name'])->from('sqlite_master ')->where(['type '=>'table']);
}
throw new \Exception("Error Processing Query,driver not exists", 1);
return (string) (new Info())->listTables();
}

/**
Expand All @@ -110,6 +162,7 @@ public function build(): string
return $this->getTables();
}

throw new \Exception("Error Processing Query,there is no database statement");
}

/**
Expand Down
Loading

0 comments on commit 40bcb77

Please sign in to comment.