diff --git a/src/Build/QueryBuilderTrait.php b/src/Build/QueryBuilderTrait.php index 6f55645..3aa301b 100644 --- a/src/Build/QueryBuilderTrait.php +++ b/src/Build/QueryBuilderTrait.php @@ -74,6 +74,22 @@ public function where() $converted[] = $this->whereEqual($attribute); } + if (isset($attribute['greater_than'])) { + $converted[] = $this->whereGreaterThan($attribute); + } + + if (isset($attribute['greater_than_or_equal'])) { + $converted[] = $this->whereGreaterThanOrEqual($attribute); + } + + if (isset($attribute['less_than'])) { + $converted[] = $this->whereLessThan($attribute); + } + + if (isset($attribute['less_than_or_equal'])) { + $converted[] = $this->whereLessThanOrEqual($attribute); + } + if (isset($attribute['not_equal'])) { $converted[] = $this->whereNotEqual($attribute); } @@ -93,10 +109,14 @@ public function where() if (isset($attribute['from'])) { $converted[] = $this->whereLike($attribute); } + + if (isset($attribute['not'])) { + $converted[] = $this->whereNot($attribute); + } } $sortedSyntax = array_map(fn ($op) => $this->syntax->getCommand($op, 1), $this->getWhereSortedConditions()); - + foreach ($converted as $key => $condition) { if ($key > 0 && $condition !== null) { $result .= $sortedSyntax[$key - 1] ?? $this->syntax->getCommand('and', 1); @@ -105,11 +125,9 @@ public function where() } } - if (is_string($this->getAttribute('where'))) - { - $result = $this->getAttribute('where'); - } - + if (is_string($this->getAttribute('where'))) { + $result = $this->getAttribute('where'); + } return $this->syntax->getCommand('where', 3) . $result; @@ -150,10 +168,9 @@ public function whereLessThanOrEqual($attribute): string return $this->whereWithOperator($attribute, 'less_than_or_equal'); } - public function whereNot($attribute, $operator = 'equal'): string + public function whereNot($attribute): string { - $stmt = $this->whereEqual($attribute); - return $this->syntax->getCommand('not', 3) . $stmt; + return $attribute['col'] . $this->syntax->getCommand('not', 1) . $attribute['not']; } public function whereIsNotNull($attribute): string diff --git a/src/Build/TableQueryBuilder.php b/src/Build/TableQueryBuilder.php index 4c1cafe..133bbd1 100644 --- a/src/Build/TableQueryBuilder.php +++ b/src/Build/TableQueryBuilder.php @@ -174,26 +174,6 @@ public function update(): string return join(';', $result); } - /** - * Get information about the table (e.g., column names and data types). - * - * @return string The SQL query to retrieve table information. - */ - public function info(): string - { - $driver = $this->syntax->getDriver(); - if ($driver === Driver::MySQL) { - return $this->syntax->getCommand('describe', 1) . $this->getAttribute('table_name'); - } - 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 $this->syntax->getCommand('pragma', 1) . "table_info({$this->getAttribute('table_name')}) "; - } - throw new \Exception("Error Processing Query,driver not exists", 1); - } - /** * Get the engine (storage engine) specification for the table. * @@ -201,6 +181,9 @@ public function info(): string */ public function engine(): string { + if($this->syntax->getDriver() === Driver::SQLite){ + return ''; + } return $this->hasAttribute('engine') ? 'ENGINE=' . $this->getAttribute('engine') : ''; } @@ -211,6 +194,9 @@ public function engine(): string */ public function charset(): string { + if($this->syntax->getDriver() === Driver::SQLite){ + return ''; + } return $this->hasAttribute('charset') ? 'DEFAULT CHARSET=' . $this->getAttribute('charset') : ''; } @@ -244,16 +230,6 @@ public function buildModifyTable(): string ); } - /** - * Build the final SQL query based on the operation and attributes. - * - * @return string The generated SQL query. - */ - public function buildInfoTable(): string - { - return $this->info(); - } - /** * Build the final SQL query based on the operation and attributes. * @@ -267,9 +243,7 @@ public function build(): string if ($this->getOperation() === 'update_table') { return $this->buildModifyTable(); } - if ($this->getOperation() === 'info_table') { - return $this->buildInfoTable(); - } + throw new \Exception("Error Processing query attribute operation"); } diff --git a/src/Condition.php b/src/Condition.php new file mode 100644 index 0000000..fb0dcf8 --- /dev/null +++ b/src/Condition.php @@ -0,0 +1,14 @@ +setAttribute('db_name',$db_name); - $this->setAttribute('info','db_name'); + $this->setAttribute('info', 'db_name'); return $this; } @@ -33,9 +32,9 @@ public function databaseName(string $db_name):self * * @return self */ - public function listDatabases():self + public function listDatabases(): self { - $this->setAttribute('info','list_db'); + $this->setAttribute('info', 'list_db'); return $this; } @@ -44,52 +43,52 @@ public function listDatabases():self * * @return self */ - public function listTables():self + public function listTables(): self { - $this->setAttribute('info','list_tables'); + $this->setAttribute('info', 'list_tables'); return $this; } - /** + /** * Specify that the INFO query should list columns of a table. * * @param string $table_name The name of the table. * @return self */ - public function listColumns(string $table_name):self + public function listColumns(string $table_name): self { - $this->setAttribute('table_name',$table_name); - $this->setAttribute('info','list_cols'); + $this->setAttribute('table_name', $table_name); + $this->setAttribute('info', 'list_cols'); return $this; } - /** + /** * Specify that the INFO query should retrieve the schema of a table. * * @param string $table_name The name of the table. * @return self */ - public function tableSchema(string $table_name):self + public function tableSchema(string $table_name): self { - $this->setAttribute('table_name',$table_name); - $this->setAttribute('info','table_schema'); + $this->setAttribute('table_name', $table_name); + $this->setAttribute('info', 'table_schema'); return $this; } - /** + /** * Specify that the INFO query should retrieve table indexes. * * @param string $table_name The name of the table. * @return self */ - public function tableIndexes(string $table_name):self + public function tableIndexes(string $table_name): self { - $this->setAttribute('table_name',$table_name); - $this->setAttribute('info','table_indexes'); + $this->setAttribute('table_name', $table_name); + $this->setAttribute('info', 'table_indexes'); return $this; } - /** + /** * Get the SQL query generated by the Info operation. * * @return string @@ -108,4 +107,4 @@ public function __toString(): string { return $this->getQuery(); } -} \ No newline at end of file +} diff --git a/src/Operations/Update.php b/src/Operations/Update.php index 5b47c62..6e1bb55 100644 --- a/src/Operations/Update.php +++ b/src/Operations/Update.php @@ -7,6 +7,7 @@ use Effectra\SqlQuery\Attribute; use Effectra\SqlQuery\Build\BuildAction; use Effectra\SqlQuery\Build\RunBuilder; +use Effectra\SqlQuery\Condition; use Effectra\SqlQuery\Validation\ArraysValidation; /** @@ -34,6 +35,15 @@ public function __construct(string $table) $this->setAttribute('insert_data_mode', Insert::INSERT_VALUES_MODE_NORMAL); } + public function whereConditions(Condition $conditions):self + { + $condition = $conditions->getAttributes() ; + foreach ($condition['where'] as $attr) { + $this->addToAttribute('where',$attr); + } + return $this; + } + /** * Get the SQL query generated by the Update operation. * @@ -54,4 +64,6 @@ public function __toString(): string { return $this->getQuery(); } + + } diff --git a/src/Syntax.php b/src/Syntax.php index 4b82205..2653a20 100644 --- a/src/Syntax.php +++ b/src/Syntax.php @@ -90,107 +90,110 @@ public function getKey($name, $space = null): string public function commands(): array { return [ - 'database' => 'DATABASE', - 'table' => 'TABLE', - 'tables' => 'TABLES', - 'table_name' => 'TABLE_NAME', - 'column' => 'COLUMN', - 'column_name' => 'COLUMN_NAME', - 'index' => 'INDEX', - 'key' => 'KEY', - 'rename' => 'RENAME', - 'select' => 'SELECT', - 'update' => 'UPDATE', - 'delete' => 'DELETE', - 'truncate' => 'TRUNCATE', - 'insert' => 'INSERT', - 'into' => 'INTO', - 'alter' => 'ALTER', - 'data_type' => 'DATA TYPE', - 'createDatabase' => 'CREATE DATABASE', - 'alterDatabase' => 'ALTER DATABASE', - 'createTable' => 'CREATE TABLE', - 'alterTable' => 'ALTER TABLE', - 'drop' => 'DROP', - 'dropDatabase' => 'DROP DATABASE', - 'createIndex' => 'CREATE INDEX', - 'orderBy' => 'ORDER BY', - 'Index' => 'INDEX', - 'limit' => 'LIMIT', - 'min' => 'MIN', - 'max' => 'MAX', - 'count' => 'COUNT', - 'avg' => 'AVG', - 'sum' => 'SUM', - 'like' => 'LIKE', - 'wildcards' => '%', - 'in' => 'IN', - 'if' => 'IF', - 'if_not' => 'IF NOT', - 'end_if' => 'END IF', - 'than' => 'THAN', - 'where' => 'WHERE', - 'between' => 'BETWEEN', - 'aliases' => 'AS', - 'joins' => 'JOIN', - 'union' => 'UNION', - 'groupBy' => 'GROUP BY', - 'having' => 'HAVING', - 'exists' => 'EXISTS', - 'ifExists' => 'IF EXISTS', - 'ifNotExists' => 'IF NOT EXISTS', - 'add' => 'ADD', - 'any' => 'ANY', - 'all' => 'ALL', - 'and' => 'AND', - 'or' => 'OR', - 'to' => 'TO', - 'on' => 'ON', - 'as' => 'AS', - 'case' => 'CASE', - 'show' => 'SHOW', - 'describe' => 'DESCRIBE', - 'pragma' => 'PRAGMA', - 'selfJoin' => 'SELF JOIN', - 'leftJoin' => 'LEFT JOIN', - 'rightJoin' => 'RIGHT JOIN', - 'crossJoin' => 'CROSS JOIN', - 'engine' => 'ENGINE=', - 'defaultCharset' => 'DEFAULT CHARSET', - 'begin' => 'BEGIN,', - 'commit' => 'COMMIT,', - 'from' => 'FROM', - 'asc' => 'ASC', - 'desc' => 'DESC', - 'abs' => 'ABS', - 'when' => 'WHEN', - 'then' => 'THEN', - 'else' => 'ELSE', - 'end' => 'END', - 'not' => 'NOT', - 'is' => 'IS', - 'null' => 'NULL', - 'values' => 'VALUES', - 'default' => 'DEFAULT', - 'set' => 'SET', - 'unsigned' => 'UNSIGNED', - 'visible' => 'VISIBLE', - 'invisible' => 'INVISIBLE', - 'format' => 'FORMAT', - 'storage' => 'STORAGE', - 'fixed' => 'FIXED', - 'dynamic' => 'DYNAMIC', - 'disk' => 'DISK', - 'memory' => 'MEMORY', - 'charset' => 'CHARSET', - 'character' => 'CHARACTER', - 'collate' => 'COLLATE', - 'begin' => 'BEGIN', - 'commit' => 'COMMIT', - 'rollback' => 'ROLLBACK', - 'row_count' => 'ROW_COUNT', - 'else' => 'ELSE', - 'check' => 'CHECK', + "abs" => "ABS", + "add" => "ADD", + "all" => "ALL", + "alter" => "ALTER", + "alterDatabase" => "ALTER DATABASE", + "alterTable" => "ALTER TABLE", + "and" => "AND", + "any" => "ANY", + "aliases" => "AS", + "as" => "AS", + "asc" => "ASC", + "avg" => "AVG", + "begin" => "BEGIN", + "between" => "BETWEEN", + "case" => "CASE", + "character" => "CHARACTER", + "charset" => "CHARSET", + "check" => "CHECK", + "collate" => "COLLATE", + "column" => "COLUMN", + "columns" => "COLUMNS", + "column_name" => "COLUMN_NAME", + "commit" => "COMMIT", + "count" => "COUNT", + "create" => "CREATE", + "createDatabase" => "CREATE DATABASE", + "createIndex" => "CREATE INDEX", + "createTable" => "CREATE TABLE", + "crossJoin" => "CROSS JOIN", + "database" => "DATABASE", + "databases" => "DATABASES", + "data_type" => "DATA TYPE", + "default" => "DEFAULT", + "defaultCharset" => "DEFAULT CHARSET", + "delete" => "DELETE", + "desc" => "DESC", + "describe" => "DESCRIBE", + "disk" => "DISK", + "drop" => "DROP", + "dropDatabase" => "DROP DATABASE", + "dynamic" => "DYNAMIC", + "else" => "ELSE", + "end" => "END", + "end_if" => "END IF", + "engine" => "ENGINE=", + "exists" => "EXISTS", + "fixed" => "FIXED", + "format" => "FORMAT", + "from" => "FROM", + "groupBy" => "GROUP BY", + "having" => "HAVING", + "if" => "IF", + "ifExists" => "IF EXISTS", + "if_not" => "IF NOT", + "ifNotExists" => "IF NOT EXISTS", + "in" => "IN", + "index" => "INDEX", + "Indexes" => "INDEXES", + "insert" => "INSERT", + "into" => "INTO", + "invisible" => "INVISIBLE", + "is" => "IS", + "join" => "JOIN", + "joins" => "JOIN", + "key" => "KEY", + "leftJoin" => "LEFT JOIN", + "like" => "LIKE", + "limit" => "LIMIT", + "max" => "MAX", + "memory" => "MEMORY", + "min" => "MIN", + "not" => "NOT", + "not_null" => "NOT NULL", + "null" => "NULL", + "on" => "ON", + "or" => "OR", + "order" => "ORDER", + "orderBy" => "ORDER BY", + "pragma" => "PRAGMA", + "rename" => "RENAME", + "rightJoin" => "RIGHT JOIN", + "rollback" => "ROLLBACK", + "row_count" => "ROW_COUNT", + "select" => "SELECT", + "selfJoin" => "SELF JOIN", + "set" => "SET", + "show" => "SHOW", + "storage" => "STORAGE", + "sum" => "SUM", + "table" => "TABLE", + "tables" => "TABLES", + "table_name" => "TABLE_NAME", + "than" => "THAN", + "then" => "THEN", + "to" => "TO", + "truncate" => "TRUNCATE", + "union" => "UNION", + "unsigned" => "UNSIGNED", + "update" => "UPDATE", + "values" => "VALUES", + "visible" => "VISIBLE", + "wildcards" => "%", + "when" => "WHEN", + "where" => "WHERE", ]; }