Browse Source

progress on the INSERT and SELECT operators

pull/1/head
Cerys Lewis 7 months ago
parent
commit
02c5b763b6
  1. 46
      src/Attributes/QueryBuilderQuery.php
  2. 20
      src/Attributes/QueryBuilderRequiredField.php
  3. 13
      src/Enumerators/DeegraphEqualityOperator.php
  4. 11
      src/Enumerators/DeegraphLogicalOperators.php
  5. 12
      src/Enumerators/DeegraphNumericalComparitor.php
  6. 9
      src/QueryBuilder/QueryBuilder.php
  7. 83
      src/QueryBuilder/QueryBuilders/Insert.php
  8. 51
      src/QueryBuilder/QueryBuilders/Select.php

46
src/Attributes/QueryBuilderQuery.php

@ -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();
}
}
}
}
}

20
src/Attributes/QueryBuilderRequiredField.php

@ -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", []), [], []);
}
}

13
src/Enumerators/DeegraphEqualityOperator.php

@ -0,0 +1,13 @@
<?php
namespace Darksparrow\DeegraphPHP\Enumerators;
enum DeegraphEqualityOperator: string
{
case EQUALS = "=";
case IDENTICAL = "==";
case IS = "===";
case DIFFERENT = "!=";
case ISNT = "ISNT";
}

11
src/Enumerators/DeegraphLogicalOperators.php

@ -0,0 +1,11 @@
<?php
namespace Darksparrow\DeegraphPHP\Enumerators;
enum DeegraphLogicalOperators: string
{
case NOT = "!";
case AND = "&&";
case OR = "||";
case XOR = "^|";
}

12
src/Enumerators/DeegraphNumericalComparitor.php

@ -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 = ">=";
}

9
src/QueryBuilder/QueryBuilder.php

@ -8,8 +8,17 @@ use Darksparrow\DeegraphPHP\QueryBuilder\QueryBuilders\Select;
class QueryBuilder class QueryBuilder
{ {
public function Insert(): Insert
{
return new Insert();
}
public function Put(): Put public function Put(): Put
{ {
return new Put(); return new Put();
} }
public function Select(): Select
{
return new Select();
}
} }

83
src/QueryBuilder/QueryBuilders/Insert.php

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

51
src/QueryBuilder/QueryBuilders/Select.php

@ -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…
Cancel
Save