Skip to content

Commit

Permalink
complete where methods
Browse files Browse the repository at this point in the history
  • Loading branch information
BMTmohammedtaha committed Sep 22, 2023
1 parent d6868af commit 39440ae
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 164 deletions.
35 changes: 26 additions & 9 deletions src/Build/QueryBuilderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
40 changes: 7 additions & 33 deletions src/Build/TableQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,33 +174,16 @@ 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.
*
* @return string The SQL specification for the table engine.
*/
public function engine(): string
{
if($this->syntax->getDriver() === Driver::SQLite){
return '';
}
return $this->hasAttribute('engine') ? 'ENGINE=' . $this->getAttribute('engine') : '';
}

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

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

Expand Down
14 changes: 14 additions & 0 deletions src/Condition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Effectra\SqlQuery;

use Effectra\SqlQuery\Operations\OperationsTrait;

class Condition extends Attribute
{
use OperationsTrait;


}
41 changes: 20 additions & 21 deletions src/Operations/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ class Info extends Attribute
* @param string $db_name The name of the database.
* @return self
*/
public function databaseName(string $db_name):self
public function databaseName(): self
{
$this->setAttribute('db_name',$db_name);
$this->setAttribute('info','db_name');
$this->setAttribute('info', 'db_name');
return $this;
}

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

Expand All @@ -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
Expand All @@ -108,4 +107,4 @@ public function __toString(): string
{
return $this->getQuery();
}
}
}
12 changes: 12 additions & 0 deletions src/Operations/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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.
*
Expand All @@ -54,4 +64,6 @@ public function __toString(): string
{
return $this->getQuery();
}


}
Loading

0 comments on commit 39440ae

Please sign in to comment.