Cerys Lewis
7 months ago
8 changed files with 245 additions and 0 deletions
@ -0,0 +1,46 @@ |
|||
<?php |
|||
|
|||
namespace Darksparrow\DeegraphPHP\Attributes; |
|||
|
|||
use Darksparrow\DeegraphPHP\QueryBuilder\QueryBuilders\Insert; |
|||
use Darksparrow\DeegraphPHP\QueryBuilder\QueryBuilders\Put; |
|||
use Darksparrow\DeegraphPHP\QueryBuilder\QueryBuilders\Select; |
|||
use PhpParser\Node\Attribute; |
|||
use PhpParser\Node\Name; |
|||
use ReflectionClass; |
|||
|
|||
#[\Attribute] class QueryBuilderQuery extends Attribute |
|||
{ |
|||
public function __construct() |
|||
{ |
|||
parent::__construct(new Name("QueryBuilderQuery", []), [], []); |
|||
} |
|||
|
|||
public function ValidateValues( |
|||
Insert|Put|Select $target |
|||
): void |
|||
{ |
|||
$nameBase = "Darksparrow\\DeegraphPHP\\Attributes"; |
|||
$reflection = new ReflectionClass($target); |
|||
$properties = $reflection->getProperties(); |
|||
|
|||
foreach ($properties as $property) |
|||
{ |
|||
$attributes = $property->getAttributes(); |
|||
$propertyName = $property->getName(); |
|||
$propertyValue = $property->getValue($target); |
|||
|
|||
if(sizeof($attributes) == 0) continue; |
|||
|
|||
foreach ($attributes as $attribute) |
|||
{ |
|||
if($attribute->getName() == "$nameBase\\QueryBuilderRequiredField") |
|||
{ |
|||
if($propertyValue == "") |
|||
throw new \Exception(); |
|||
} |
|||
|
|||
} |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,20 @@ |
|||
<?php |
|||
|
|||
namespace Darksparrow\DeegraphPHP\Attributes; |
|||
|
|||
use PhpParser\Node\Attribute; |
|||
use PhpParser\Node\Name; |
|||
use ReflectionClass; |
|||
|
|||
#[\Attribute] class QueryBuilderRequiredField extends Attribute |
|||
{ |
|||
public bool $Required; |
|||
|
|||
public function __construct( |
|||
bool $required = false |
|||
) |
|||
{ |
|||
$this->Required = $required; |
|||
parent::__construct(new Name("QueryBuilderRequiredField", []), [], []); |
|||
} |
|||
} |
@ -0,0 +1,13 @@ |
|||
<?php |
|||
|
|||
namespace Darksparrow\DeegraphPHP\Enumerators; |
|||
|
|||
enum DeegraphEqualityOperator: string |
|||
{ |
|||
case EQUALS = "="; |
|||
case IDENTICAL = "=="; |
|||
case IS = "==="; |
|||
|
|||
case DIFFERENT = "!="; |
|||
case ISNT = "ISNT"; |
|||
} |
@ -0,0 +1,11 @@ |
|||
<?php |
|||
|
|||
namespace Darksparrow\DeegraphPHP\Enumerators; |
|||
|
|||
enum DeegraphLogicalOperators: string |
|||
{ |
|||
case NOT = "!"; |
|||
case AND = "&&"; |
|||
case OR = "||"; |
|||
case XOR = "^|"; |
|||
} |
@ -0,0 +1,12 @@ |
|||
<?php |
|||
|
|||
namespace Darksparrow\DeegraphPHP\Enumerators; |
|||
|
|||
enum DeegraphNumericalComparitor: string |
|||
{ |
|||
case LESS_THAN = "<"; |
|||
case MORE_THAN = ">"; |
|||
case LESS_THAN_OR_EQUAL_TO = "<="; |
|||
case MORE_THAN_OR_EQUAL_TO = ">="; |
|||
} |
|||
|
@ -0,0 +1,83 @@ |
|||
<?php |
|||
|
|||
namespace Darksparrow\DeegraphPHP\QueryBuilder\QueryBuilders; |
|||
|
|||
use Darksparrow\DeegraphPHP\Attributes\QueryBuilderRequiredField; |
|||
use Darksparrow\DeegraphPHP\Attributes\QueryBuilderQuery; |
|||
use Darksparrow\DeegraphPHP\QueryBuilder\QueryBuilderTrait; |
|||
|
|||
#[QueryBuilderQuery] |
|||
final class Insert |
|||
{ |
|||
use QueryBuilderTrait; |
|||
|
|||
#[QueryBuilderRequiredField] |
|||
protected string $RelativePath = ""; |
|||
|
|||
protected array $Keys = []; |
|||
|
|||
protected array $Schemas = []; |
|||
|
|||
protected string $Values = ""; |
|||
|
|||
protected bool $Duplicate = false; |
|||
|
|||
protected bool $Replace = false; |
|||
|
|||
|
|||
public function __construct() |
|||
{ |
|||
} |
|||
|
|||
public function __toString(): string |
|||
{ |
|||
$instance = new QueryBuilderQuery(); |
|||
$instance->ValidateValues($this); |
|||
|
|||
$builder = "INSERT INTO $this->RelativePath"; |
|||
|
|||
if(sizeof($this->Keys) > 0) $builder .= "KEYS " . implode(separator: ", ", array: $this->Keys); |
|||
if(sizeof($this->Schemas) > 0) $builder .= "SCHEMAS " . implode(separator: ", ", array: $this->Schemas); |
|||
if($this->Values != "") $builder .= "VALUES $this->Values"; |
|||
if($this->Duplicate) $builder .= "DUPLICATE"; |
|||
if($this->Replace) $builder .= "REPLACE"; |
|||
|
|||
return $builder; |
|||
} |
|||
|
|||
public function RelativePath(string $relativePath): Insert |
|||
{ |
|||
$this->RelativePath = $relativePath; |
|||
return $this; |
|||
} |
|||
|
|||
public function Keys(string $keys): Insert |
|||
{ |
|||
$this->Keys = $keys; |
|||
return $this; |
|||
} |
|||
|
|||
public function Schemas(string $schemas): Insert |
|||
{ |
|||
$this->Schemas = $schemas; |
|||
return $this; |
|||
} |
|||
|
|||
public function Values(string $values): Insert |
|||
{ |
|||
$this->Values .= $values; |
|||
return $this; |
|||
} |
|||
|
|||
public function Duplicate(): Insert |
|||
{ |
|||
$this->Duplicate = true; |
|||
return $this; |
|||
} |
|||
|
|||
public function Replace(): Insert |
|||
{ |
|||
$this->Replace = true; |
|||
return $this; |
|||
} |
|||
} |
@ -0,0 +1,51 @@ |
|||
<?php |
|||
|
|||
namespace Darksparrow\DeegraphPHP\QueryBuilder\QueryBuilders; |
|||
|
|||
use Darksparrow\DeegraphPHP\Attributes\QueryBuilderQuery; |
|||
use Darksparrow\DeegraphPHP\Attributes\QueryBuilderRequiredField; |
|||
use Darksparrow\DeegraphPHP\Enumerators\DeegraphEqualityOperator; |
|||
use Darksparrow\DeegraphPHP\Exceptions\QueryBuilderRequiredFieldIsNotSetException; |
|||
use Darksparrow\DeegraphPHP\QueryBuilder\QueryBuilderTrait; |
|||
|
|||
#[QueryBuilderQuery] |
|||
final class Select |
|||
{ |
|||
use QueryBuilderTrait; |
|||
|
|||
#[QueryBuilderRequiredField] |
|||
protected array $RelativePaths = []; |
|||
protected string $From = ""; |
|||
protected string $Where = ""; |
|||
protected string $InstanceOf = ""; |
|||
|
|||
|
|||
public function __construct() |
|||
{ |
|||
} |
|||
|
|||
/** |
|||
* @throws QueryBuilderRequiredFieldIsNotSetException |
|||
*/ |
|||
public function __toString(): string |
|||
{ |
|||
$builder = "SELECT "; |
|||
if(sizeof($this->RelativePaths)) $builder .= " " . implode(separator: ", ", array: $this->RelativePaths); |
|||
if($this->From != "") $builder .= " FROM $this->From"; |
|||
if($this->Where != "") $builder .= " WHERE $this->Where"; |
|||
if($this->InstanceOf != "") $builder .= " INSTANCEOF $this->InstanceOf"; |
|||
|
|||
return $builder; |
|||
} |
|||
|
|||
public function RelativePaths(array $relativePaths): Select |
|||
{ |
|||
$this->RelativePaths = $relativePaths; |
|||
return $this; |
|||
} |
|||
public function Where(string $target, DeegraphEqualityOperator $operator, string $value): Select |
|||
{ |
|||
$this->Where = "" . $target . " " . $operator->value . " " . $value; |
|||
return $this; |
|||
} |
|||
} |
Loading…
Reference in new issue