diff --git a/src/Build/ColumnQueryBuilder.php b/src/Build/ColumnQueryBuilder.php index ae809b1..9b5dea8 100644 --- a/src/Build/ColumnQueryBuilder.php +++ b/src/Build/ColumnQueryBuilder.php @@ -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. * @@ -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. * @@ -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); } @@ -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(); } @@ -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. @@ -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. @@ -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. @@ -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. @@ -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". @@ -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 ); } @@ -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. diff --git a/src/Build/DatabaseQueryBuilder.php b/src/Build/DatabaseQueryBuilder.php index c74c8f2..5017838 100644 --- a/src/Build/DatabaseQueryBuilder.php +++ b/src/Build/DatabaseQueryBuilder.php @@ -7,7 +7,7 @@ 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; /** @@ -15,7 +15,6 @@ * * 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 { @@ -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; } /** @@ -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; } /** @@ -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; } /** @@ -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(); } /** @@ -110,6 +162,7 @@ public function build(): string return $this->getTables(); } + throw new \Exception("Error Processing Query,there is no database statement"); } /** diff --git a/src/Build/InfoQueryBuilder.php b/src/Build/InfoQueryBuilder.php index b43cabe..6ca429e 100644 --- a/src/Build/InfoQueryBuilder.php +++ b/src/Build/InfoQueryBuilder.php @@ -5,6 +5,7 @@ namespace Effectra\SqlQuery\Build; use Effectra\SqlQuery\Attribute; +use Effectra\SqlQuery\Driver; use Effectra\SqlQuery\Operations\Select; use Effectra\SqlQuery\Syntax; @@ -32,11 +33,13 @@ public function __construct(protected array $attributes, protected Syntax $synta */ public function dbName(): string { - return (string) match ($this->syntax->getDriver()) { - 'mysql' => $this->syntax->getCommand('select', 1) . $this->syntax->getCommand('database') . "()", - 'postgresql' => $this->syntax->getCommand('select', 1) . 'current_database()', - 'sqlite' => $this->syntax->getCommand('pragma', 1) . 'database_list', + return (string) match ($this->syntax->getDriver()) { + Driver::MySQL => $this->syntax->getCommand('select', 1) . $this->syntax->getCommand('database') . "()", + Driver::PostgreSQL => $this->syntax->getCommand('select', 1) . 'current_database()', + Driver::SQLite => $this->syntax->getCommand('pragma', 1) . 'database_list', }; + + } /** @@ -46,11 +49,16 @@ public function dbName(): string */ public function listDatabase(): string { - return (string) match ($this->syntax->getDriver()) { - 'mysql' => $this->syntax->getCommand('show', 1) . $this->syntax->getCommand('databases'), - 'postgresql' => (new Select('pg_database'))->columns(['datname']), - 'sqlite' => '', + $query = (string) match ($this->syntax->getDriver()) { + Driver::MySQL => $this->syntax->getCommand('show', 1) . $this->syntax->getCommand('databases'), + Driver::PostgreSQL => (new Select('pg_database'))->columns(['datname']), + Driver::SQLite => '', }; + + if(empty($query)){ + throw new \Exception("Error Processing Query, driver '{$this->syntax->getDriver()}' doesn't has statement for list database tables"); + } + return $query; } /** @@ -61,9 +69,9 @@ public function listDatabase(): string public function listTables(): string { return (string) match ($this->syntax->getDriver()) { - 'mysql' => $this->syntax->getCommand('show', 1) . $this->syntax->getCommand('tables'), - 'postgresql' => (new Select('information_schema.tables'))->columns(['table_name'])->where(['table_schema' => 'public', 'table_type' => 'BASE TABLE']), - 'sqlite' => (new Select('sqlite_master'))->columns(['name'])->where(['type' => 'table']), + Driver::MySQL => $this->syntax->getCommand('show', 1) . $this->syntax->getCommand('tables'), + Driver::PostgreSQL => (new Select('information_schema.tables'))->columns(['table_name'])->where(['table_schema' => 'public', 'table_type' => 'BASE TABLE']), + Driver::SQLite => (new Select('sqlite_master'))->columns(['name'])->where(['type' => 'table']), }; } @@ -75,9 +83,9 @@ public function listTables(): string public function listCols(): string { return (string) match ($this->syntax->getDriver()) { - 'mysql' => $this->syntax->getCommand('describe', 1) . $this->getAttribute('table_name'), - 'postgresql' => (new Select('information_schema.columns'))->columns(['column_name'])->where(['table_name' => $this->getAttribute('table_name')]), - 'sqlite' => sprintf( + Driver::MySQL => $this->syntax->getCommand('describe', 1) . $this->getAttribute('table_name'), + Driver::PostgreSQL => (new Select('information_schema.columns'))->columns(['column_name'])->where(['table_name' => $this->getAttribute('table_name')]), + Driver::SQLite => sprintf( '%s %s(%s)', $this->syntax->getCommand('pragma', 1), 'table_info', @@ -94,9 +102,9 @@ public function listCols(): string public function tableSchema(): string { return (string) match ($this->syntax->getDriver()) { - 'mysql' => $this->syntax->getCommand('show', 1) . $this->syntax->getCommand('create', 2) . $this->syntax->getCommand('table', 2) . $this->getAttribute('table_name'), - 'postgresql' => (new Select('information_schema.columns'))->columns(['column_name', 'data_type', 'character_maximum_length'])->where(['table_name' => $this->getAttribute('table_name')]), - 'sqlite' => (new Select('sqlite_master'))->columns(['sql'])->where(['type' => 'table', 'name' => $this->getAttribute('table_name')]), + Driver::MySQL => $this->syntax->getCommand('show', 1) . $this->syntax->getCommand('create', 2) . $this->syntax->getCommand('table', 2) . $this->getAttribute('table_name'), + Driver::PostgreSQL => (new Select('information_schema.columns'))->columns(['column_name', 'data_type', 'character_maximum_length'])->where(['table_name' => $this->getAttribute('table_name')]), + Driver::SQLite => (new Select('sqlite_master'))->columns(['sql'])->where(['type' => 'table', 'name' => $this->getAttribute('table_name')]), }; } @@ -108,9 +116,9 @@ public function tableSchema(): string public function tableIndexes(): string { return (string) match ($this->syntax->getDriver()) { - 'mysql' => $this->syntax->getCommand('show', 1) . $this->syntax->getCommand('index') . $this->syntax->getCommand('from') . $this->getAttribute('table_name'), - 'postgresql' => (new Select('pg_indexes'))->columns(['indexname'])->where(['table_name' => $this->getAttribute('table_name')]), - 'sqlite' => sprintf( + Driver::MySQL => $this->syntax->getCommand('show', 1) . $this->syntax->getCommand('index') . $this->syntax->getCommand('from') . $this->getAttribute('table_name'), + Driver::PostgreSQL => (new Select('pg_indexes'))->columns(['indexname'])->where(['table_name' => $this->getAttribute('table_name')]), + Driver::SQLite => sprintf( '%s %s(%s)', $this->syntax->getCommand('pragma', 1), 'index_list', diff --git a/src/Build/QueryBuilderTrait.php b/src/Build/QueryBuilderTrait.php index 3aa301b..63ac90b 100644 --- a/src/Build/QueryBuilderTrait.php +++ b/src/Build/QueryBuilderTrait.php @@ -4,6 +4,7 @@ namespace Effectra\SqlQuery\Build; +use Effectra\SqlQuery\Driver; use Effectra\SqlQuery\Operations\Insert; /** @@ -242,4 +243,67 @@ public function subAttributeHasAttribute($sub_attribute, $attribute): bool return false; } + + /** + * Generate a string for checking conditions in a SQL query. + * + * @param string $column The name of the column to check. + * @param array $expressions An array of conditions to check. + * @param array $checkSort An array specifying the order of logical operators (optional). + * + * @return string The generated SQL check conditions string. + * + * @throws \Exception If an empty expression is encountered. + */ + public function checkQuery(string $column, array $expressions, array $checkSort = []): string + { + // If SQLite, return an empty string since SQLite doesn't support check conditions. + if ($this->syntax->getDriver() === Driver::SQLite) { + return ''; + } + + // If no expressions provided, return an empty string. + if (empty($expressions)) { + return ''; + } + + $result = ''; + $expressionsResult = []; + + foreach ($expressions as $expression) { + // Ensure expression is not empty. + if (empty($expression)) { + throw new \Exception("Error Processing Query, check expression is empty"); + } + + // If the expression is 'json', convert it to 'json_valid(column)'. + if ($expression === 'json') { + $expression = "json_valid($column)"; + } + + $expressionsResult[] = $expression; + } + + // Set a default value for $checkSort if not provided. + if (empty($checkSort)) { + $checkSort = ['and']; // Default to 'AND' operator. + } + + // Remove the first element of $checkSort if it's empty. + if (!empty($checkSort)) { + array_shift($checkSort); + } + + // Generate sorted syntax based on the logical operators in $checkSort. + $sortedSyntax = array_map(fn ($operator) => $this->syntax->getCommand($operator, 1), $checkSort); + + foreach ($expressionsResult as $key => $condition) { + if ($key > 0 && $condition !== null) { + $result .= $sortedSyntax[$key - 1] ?? $this->syntax->getCommand('and', 1); + } + $result .= $condition; + } + + return $result; + } } diff --git a/src/Build/SelectQueryBuilder.php b/src/Build/SelectQueryBuilder.php index 5adecdf..06af6b9 100644 --- a/src/Build/SelectQueryBuilder.php +++ b/src/Build/SelectQueryBuilder.php @@ -43,7 +43,7 @@ public function start():string */ public function columnsSelected():string|array { - $cols = $this->attributes['column_selected']; + $cols = $this->getAttribute('column_selected'); if($cols === '*'){ return '*'; } @@ -63,7 +63,7 @@ public function columnsSelected():string|array */ public function fromTable():string { - return $this->syntax->getCommand('from', 3) . $this->attributes['table_name']; + return $this->syntax->getCommand('from', 3) . $this->getAttribute('table_name'); } /** @@ -73,10 +73,10 @@ public function fromTable():string */ public function groupBy():string { - if (!isset($this->attributes['group_by'])) { + if (!$this->hasAttribute('group_by')) { return ''; } - return $this->syntax->getCommand('groupBy', 3) . join(', ', $this->attributes['group_by']); + return $this->syntax->getCommand('groupBy', 3) . join(', ',$this->getAttribute('group_by') ); } /** @@ -86,12 +86,12 @@ public function groupBy():string */ public function orderBy():string { - if (!isset($this->attributes['order_by'])) { + if (!$this->hasAttribute('order_by')) { return ''; } return $this->syntax->getCommand('orderBy', 3) . - join(', ', $this->attributes['order_by']['cols']) . - $this->syntax->getCommand($this->attributes['order_by']['direction'], 2); + join(', ', $this->getAttribute('order_by')['cols']) . + $this->syntax->getCommand($this->getAttribute('order_by')['direction'], 2); } /** @@ -104,10 +104,10 @@ public function limit():string if (!$this->hasAttribute('limit')) { return ''; } - $count_until = $this->attributes['limit']['count_until'] ?? null; + $count_until = $this->getAttribute('limit')['count_until'] ?? null; $count = $count_until ? ', ' . $count_until : $count_until; return $this->syntax->getCommand('limit', 3) - . $this->attributes['limit']['start_from'] + . $this->getAttribute('limit')['start_from'] . $count; } diff --git a/src/Build/TableQueryBuilder.php b/src/Build/TableQueryBuilder.php index 133bbd1..18bb9e8 100644 --- a/src/Build/TableQueryBuilder.php +++ b/src/Build/TableQueryBuilder.php @@ -9,7 +9,6 @@ use Effectra\SqlQuery\Driver; use Effectra\SqlQuery\Operations\Alter; use Effectra\SqlQuery\Operations\Drop; -use Effectra\SqlQuery\Operations\Select; use Effectra\SqlQuery\Syntax; /** @@ -17,19 +16,22 @@ * * Represents a query builder for creating, updating, and retrieving information about database tables. * - * @package Effectra\SqlQuery\Build */ class TableQueryBuilder extends Attribute { + use QueryBuilderTrait; + protected array $check_attrs = []; /** * Constructor for the TableQueryBuilder. * * @param array $attributes An array of attributes used in the query. * @param Syntax $syntax The syntax handler for generating SQL syntax. */ - public function __construct(protected array $attributes, protected Syntax $syntax) - { + public function __construct( + protected array $attributes, + protected Syntax $syntax + ) { $cols = $this->getAttribute('cols'); if ($cols) { $this->setAttribute('cols', $this->removeDuplicatesByKey($cols, 'column_name')); @@ -94,20 +96,59 @@ public function tableName(): string return $this->getAttribute('table_name'); } + /** + * get the attributes of columns. + * @return array column attributes + */ + public function columns(): array + { + $cols = []; + foreach ($this->getAttribute('cols') as $col) { + $col_attrs = new ColumnDestruct($col); + + $cols[] = $col_attrs->getAttributes(); + } + return $cols; + } + /** * Convert the object to its string representation (generates the SQL query). * * @return string The generated SQL query. */ - public function columns(): string + public function columnQueryBuilder(): string { $cols = []; - foreach ($this->getAttribute('cols') as $col) { - $cols[] = (string) new ColumnDestruct($col); + foreach ($this->columns() as $col) { + $columnQueryBuilder = new ColumnQueryBuilder($col, $this->syntax); + + $check = isset($col['check']) && in_array('json', $col['check']) ? $columnQueryBuilder->check() : ''; + + $cols[] = sprintf( + '%s %s%s %s %s %s', + $columnQueryBuilder->columnName(), + $columnQueryBuilder->dataType(), + $columnQueryBuilder->size(), + $columnQueryBuilder->nullable(), + $columnQueryBuilder->constraints(), + $check + ); } return join(",\n", $cols); } + public function check(): string + { + $cols = []; + foreach ($this->columns() as $column) { + if (isset($column['check']) && !in_array('json', $column['check'])) { + $cols[] = $this->checkQuery($column['column_name'], $column['check'], $column['check_sort']); + } + } + + return empty($cols) ? '' : sprintf('%s (%s)', $this->syntax->getCommand('check', 1), join($this->syntax->getCommand('and', 1), $cols)); + } + /** * Get the SQL command for updating the table structure. * @@ -164,7 +205,7 @@ public function update(): string if ($this->getAttribute('drop_key') === 'index') { $drop->dropIndex(); } - + if ($this->getAttribute('drop_key') === 'key') { $drop->dropKey(); } @@ -181,7 +222,7 @@ public function update(): string */ public function engine(): string { - if($this->syntax->getDriver() === Driver::SQLite){ + if ($this->syntax->getDriver() === Driver::SQLite) { return ''; } return $this->hasAttribute('engine') ? 'ENGINE=' . $this->getAttribute('engine') : ''; @@ -192,9 +233,9 @@ public function engine(): string * * @return string The SQL specification for the table character set. */ - public function charset(): string + public function tableCharset(): string { - if($this->syntax->getDriver() === Driver::SQLite){ + if ($this->syntax->getDriver() === Driver::SQLite) { return ''; } return $this->hasAttribute('charset') ? 'DEFAULT CHARSET=' . $this->getAttribute('charset') : ''; @@ -207,14 +248,17 @@ public function charset(): string */ public function buildCreateTable(): string { - return sprintf( - "%s %s ( %s ) %s %s", + $query = sprintf( + "%s %s ( %s ) %s %s %s", $this->start(), $this->tableName(), - $this->columns(), + $this->columnQueryBuilder(), + $this->check(), $this->engine(), - $this->charset(), + $this->tableCharset(), ); + + return $query; } /** @@ -243,7 +287,7 @@ public function build(): string if ($this->getOperation() === 'update_table') { return $this->buildModifyTable(); } - + throw new \Exception("Error Processing query attribute operation"); } diff --git a/src/Condition.php b/src/Condition.php index fb0dcf8..a67b65f 100644 --- a/src/Condition.php +++ b/src/Condition.php @@ -5,10 +5,11 @@ namespace Effectra\SqlQuery; use Effectra\SqlQuery\Operations\OperationsTrait; - +/** + * A class for managing conditions + */ class Condition extends Attribute { use OperationsTrait; - } \ No newline at end of file diff --git a/src/Structure/Column.php b/src/Structure/Column.php index 4ae714e..0f941e1 100644 --- a/src/Structure/Column.php +++ b/src/Structure/Column.php @@ -118,7 +118,7 @@ public function constraints($constraints): self */ public function unique(): self { - return $this->constraints('unique'); + return $this->constraints('unique_key'); } /** @@ -138,7 +138,7 @@ public function unsigned(): self */ public function primaryKey(): self { - return $this->constraints(['key' => 'primary_key']); + return $this->constraints('primary_key'); } /** diff --git a/src/Structure/Database.php b/src/Structure/Database.php index 9155a0d..a102061 100644 --- a/src/Structure/Database.php +++ b/src/Structure/Database.php @@ -30,9 +30,11 @@ public function __construct(string $name) /** * Set the operation to create the database. */ - public function create() + public function create($options = []) { $this->setAttribute('operation', 'create'); + $this->setAttribute('options',$options); + return $this; } /** @@ -41,6 +43,7 @@ public function create() public function drop() { $this->setAttribute('operation', 'drop'); + return $this; } /** @@ -52,6 +55,7 @@ public function rename(string $new_name) { $this->setAttribute('operation', 'rename'); $this->setAttribute('rename', $new_name); + return $this; } /** @@ -60,6 +64,7 @@ public function rename(string $new_name) public function getTables() { $this->setAttribute('operation', 'get_tables'); + return $this; } /**