Browse Source

started the move over to much more "builder" style

Cerys Lewis 5 months ago
parent
commit
1cce3a14d9
  1. 7
      examples/grantQuery.php
  2. 8
      src/Attributes/QueryBuilderQuery.php
  3. 23
      src/Core/DeegraphServer.php
  4. 8
      src/DataStructures/QueryResponseWrapper.php
  5. 14
      src/Interfaces/QueryBuilderInterface.php
  6. 16
      src/Interfaces/QueryInstanceInterface.php
  7. 8
      src/Interfaces/QueryResponseInterface.php
  8. 24
      src/QueryBuilder/QueryBuilder.php
  9. 26
      src/QueryBuilder/QueryBuilders/GrantQueryBuilder.php
  10. 25
      src/QueryBuilder/QueryBuilders/InsertQueryBuilder.php
  11. 67
      src/QueryBuilder/QueryBuilders/PutQueryBuilder.php
  12. 28
      src/QueryBuilder/QueryBuilders/SelectQueryBuilder.php
  13. 21
      src/QueryInstance/GrantQuery.php
  14. 20
      src/QueryInstance/InsertQuery.php
  15. 21
      src/QueryInstance/PutQuery.php
  16. 21
      src/QueryInstance/SelectQuery.php
  17. 17
      src/QueryResponse/GrantQueryResponse.php
  18. 13
      src/QueryResponse/InsertQueryResponse.php
  19. 14
      src/QueryResponse/PutQueryResponse.php
  20. 43
      src/QueryResponse/SelectQueryResponse.php
  21. 28
      src/Superclasses/QueryInstanceSuperclass.php
  22. 2
      src/Traits/QueryBuilderTrait.php

7
examples/grantQuery.php

@ -4,13 +4,14 @@ global $db;
require_once __DIR__ . '/../vendor/autoload.php'; require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../examples/connection.php'; require_once __DIR__ . '/../examples/connection.php';
use Darksparrow\DeegraphInteractions\Enumerators\DeegraphEqualityOperator;
use Darksparrow\DeegraphInteractions\Enumerators\DeegraphPermissionType; use Darksparrow\DeegraphInteractions\Enumerators\DeegraphPermissionType;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilder; use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilder;
$grantQuery = QueryBuilder::Grant() QueryBuilder::Grant()
->GrantAll() ->GrantAll()
// ->Permissions(permissionTypes: [DeegraphPermissionType::READ]) ->On(target: "*")
->To(uuid: $db->GetActor()); ->Bu($db);
$result = $db->RunQuery($grantQuery); $result = $db->RunQuery($grantQuery);
var_dump($result); var_dump($result);

8
src/Attributes/QueryBuilderQuery.php

@ -2,9 +2,9 @@
namespace Darksparrow\DeegraphInteractions\Attributes; namespace Darksparrow\DeegraphInteractions\Attributes;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\InsertQuery; use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\InsertQueryBuilder;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\PutQuery; use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\PutQueryBuilder;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\SelectQuery; use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\SelectQueryBuilder;
use PhpParser\Node\Attribute; use PhpParser\Node\Attribute;
use PhpParser\Node\Name; use PhpParser\Node\Name;
use ReflectionClass; use ReflectionClass;
@ -17,7 +17,7 @@ use ReflectionClass;
} }
public function ValidateValues( public function ValidateValues(
InsertQuery|PutQuery|SelectQuery $target InsertQueryBuilder|PutQueryBuilder|SelectQueryBuilder $target
): void ): void
{ {
$nameBase = "Darksparrow\\DeegraphPHP\\Attributes"; $nameBase = "Darksparrow\\DeegraphPHP\\Attributes";

23
src/Core/DeegraphServer.php

@ -2,12 +2,7 @@
namespace Darksparrow\DeegraphInteractions\Core; namespace Darksparrow\DeegraphInteractions\Core;
use Darksparrow\DeegraphInteractions\DataStructures\QueryResponseWrapper;
use Darksparrow\DeegraphInteractions\DataStructures\ServerInfo; use Darksparrow\DeegraphInteractions\DataStructures\ServerInfo;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\GrantQuery;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\InsertQuery;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\PutQuery;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\SelectQuery;
final class DeegraphServer final class DeegraphServer
{ {
@ -48,7 +43,7 @@ final class DeegraphServer
return ServerInfo::FromAPIResponse(response: $response); return ServerInfo::FromAPIResponse(response: $response);
} }
private function RunRawRequest(string $endpoint, string $method = "GET", string $body = null): string public function RunRawRequest(string $endpoint, string $method = "GET", string $body = null): string
{ {
$ch = curl_init("https://{$this->ServerDomain}:{$this->Port}{$endpoint}"); $ch = curl_init("https://{$this->ServerDomain}:{$this->Port}{$endpoint}");
curl_setopt($ch, CURLOPT_HTTPHEADER, [ curl_setopt($ch, CURLOPT_HTTPHEADER, [
@ -85,20 +80,4 @@ final class DeegraphServer
return $result; return $result;
} }
/**
* @param InsertQuery|PutQuery|SelectQuery $query Takes in a Query Builder object.
* @return QueryResponseWrapper
*/
public function RunQuery(
GrantQuery|InsertQuery|PutQuery|SelectQuery $query
): QueryResponseWrapper
{
$response = $this->RunRawRequest(
endpoint: "/api/v1/@query",
method: "POST",
body: $query
);
return QueryResponseWrapper::FromAPIResponse($response);
}
} }

8
src/DataStructures/QueryResponseWrapper.php

@ -9,6 +9,8 @@ class QueryResponseWrapper
public array $Rows; public array $Rows;
public string $RowFormat; public string $RowFormat;
public string $RuleID;
public static function FromAPIResponse(string $response): QueryResponseWrapper public static function FromAPIResponse(string $response): QueryResponseWrapper
{ {
$response = json_decode($response, true); $response = json_decode($response, true);
@ -31,7 +33,11 @@ class QueryResponseWrapper
{ {
$builder->Nodes = []; $builder->Nodes = [];
foreach($response["@nodes"] as $row) foreach($response["@nodes"] as $row)
$builder->Nodes = $response["@nodes"]; $builder->Nodes = $row;
}
if(array_key_exists(key: "@rule_id", array: $response))
{
$builder->RuleID = $response["@rule_id"];
} }
return $builder; return $builder;

14
src/Interfaces/QueryBuilderInterface.php

@ -0,0 +1,14 @@
<?php
namespace Darksparrow\DeegraphInteractions\Interfaces;
use Darksparrow\DeegraphInteractions\QueryInstance\GrantQuery;
use Darksparrow\DeegraphInteractions\QueryInstance\InsertQuery;
use Darksparrow\DeegraphInteractions\QueryInstance\PutQuery;
use Darksparrow\DeegraphInteractions\QueryInstance\SelectQuery;
interface QueryBuilderInterface
{
// public function __construct();
public function Build(): GrantQuery|InsertQuery|PutQuery|SelectQuery;
}

16
src/Interfaces/QueryInstanceInterface.php

@ -0,0 +1,16 @@
<?php
namespace Darksparrow\DeegraphInteractions\Interfaces;
use Darksparrow\DeegraphInteractions\Core\DeegraphServer;
interface QueryInstanceInterface
{
/**
* Runs the Query Instance on a DeegraphServer Instance.
*
* @param DeegraphServer $deegraphServer
* @return mixed
*/
public function RunQuery(DeegraphServer $deegraphServer);
}

8
src/Interfaces/QueryResponseInterface.php

@ -0,0 +1,8 @@
<?php
namespace Darksparrow\DeegraphInteractions\Interfaces;
interface QueryResponseInterface
{
public function __construct(string $response);
}

24
src/QueryBuilder/QueryBuilder.php

@ -2,27 +2,27 @@
namespace Darksparrow\DeegraphInteractions\QueryBuilder; namespace Darksparrow\DeegraphInteractions\QueryBuilder;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\GrantQuery; use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\GrantQueryBuilder;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\InsertQuery; use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\InsertQueryBuilder;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\PutQuery; use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\PutQueryBuilder;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\SelectQuery; use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\SelectQueryBuilder;
final class QueryBuilder final class QueryBuilder
{ {
public static function Grant(): GrantQuery public static function Grant(): GrantQueryBuilder
{ {
return new GrantQuery(); return new GrantQueryBuilder();
} }
public static function Insert(): InsertQuery public static function Insert(): InsertQueryBuilder
{ {
return new InsertQuery(); return new InsertQueryBuilder();
} }
public static function Put(): PutQuery public static function Put(): PutQueryBuilder
{ {
return new PutQuery(); return new PutQueryBuilder();
} }
public static function Select(): SelectQuery public static function Select(): SelectQueryBuilder
{ {
return new SelectQuery(); return new SelectQueryBuilder();
} }
} }

26
src/QueryBuilder/QueryBuilders/GrantQuery.php → src/QueryBuilder/QueryBuilders/GrantQueryBuilder.php

@ -4,16 +4,17 @@ namespace Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders;
use Darksparrow\DeegraphInteractions\Attributes\QueryBuilderQuery; use Darksparrow\DeegraphInteractions\Attributes\QueryBuilderQuery;
use Darksparrow\DeegraphInteractions\Attributes\QueryBuilderRequiredField; use Darksparrow\DeegraphInteractions\Attributes\QueryBuilderRequiredField;
use Darksparrow\DeegraphInteractions\Enumerators\DeegraphEqualityOperator;
use Darksparrow\DeegraphInteractions\Enumerators\DeegraphPermissionType; use Darksparrow\DeegraphInteractions\Enumerators\DeegraphPermissionType;
use Darksparrow\DeegraphInteractions\Exceptions\QueryBuilderRequiredFieldIsNotSetException; use Darksparrow\DeegraphInteractions\Exceptions\QueryBuilderRequiredFieldIsNotSetException;
use Darksparrow\DeegraphInteractions\Interfaces\QueryBuilderInterface;
use Darksparrow\DeegraphInteractions\QueryInstance\GrantQuery;
#[QueryBuilderQuery] #[QueryBuilderQuery]
class GrantQuery final class GrantQueryBuilder implements QueryBuilderInterface
{ {
#[QueryBuilderRequiredField] #[QueryBuilderRequiredField]
protected array $PermissionTypes; protected array $PermissionTypes;
#[QueryBuilderRequiredField]
protected string $GrantTo;
protected string $GrantOn = ""; protected string $GrantOn = "";
protected string $GrantWhere = ""; protected string $GrantWhere = "";
@ -23,25 +24,24 @@ class GrantQuery
/** /**
* @throws QueryBuilderRequiredFieldIsNotSetException * @throws QueryBuilderRequiredFieldIsNotSetException
*/ */
public function __toString(): string public function Build(): GrantQuery
{ {
$builder = "GRANT "; $builder = "GRANT ";
if(sizeof($this->PermissionTypes)) $builder .= implode(separator: ",", array: $this->PermissionTypes); if(sizeof($this->PermissionTypes)) $builder .= implode(separator: ",", array: $this->PermissionTypes);
if($this->GrantTo != "") $builder .= " TO $this->GrantTo";
if($this->GrantOn != "") $builder .= " ON $this->GrantOn"; if($this->GrantOn != "") $builder .= " ON $this->GrantOn";
if($this->GrantWhere != "") $builder .= " WHERE $this->GrantWhere"; if($this->GrantWhere != "") $builder .= " WHERE $this->GrantWhere";
return $builder; return new GrantQuery(queryString: $builder);
} }
public function Permissions(array $permissionTypes): GrantQuery public function Permissions(array $permissionTypes): GrantQueryBuilder
{ {
$this->PermissionTypes = []; $this->PermissionTypes = [];
foreach($permissionTypes as $permissionType) foreach($permissionTypes as $permissionType)
$this->PermissionTypes[] = $permissionType->value; $this->PermissionTypes[] = $permissionType->value;
return $this; return $this;
} }
public function GrantAll(): GrantQuery public function GrantAll(): GrantQueryBuilder
{ {
$this->PermissionTypes = [ $this->PermissionTypes = [
DeegraphPermissionType::WRITE->value, DeegraphPermissionType::WRITE->value,
@ -52,9 +52,15 @@ class GrantQuery
return $this; return $this;
} }
public function To(string $uuid): GrantQuery public function On(string $target): GrantQueryBuilder
{
$this->GrantOn = $target;
return $this;
}
public function Where(string $target, DeegraphEqualityOperator $operator, string $value): GrantQueryBuilder
{ {
$this->GrantTo = "{" . $uuid . "}"; $this->GrantWhere = "$target $operator->value $value";
return $this; return $this;
} }
} }

25
src/QueryBuilder/QueryBuilders/InsertQuery.php → src/QueryBuilder/QueryBuilders/InsertQueryBuilder.php

@ -4,10 +4,12 @@ namespace Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders;
use Darksparrow\DeegraphInteractions\Attributes\QueryBuilderQuery; use Darksparrow\DeegraphInteractions\Attributes\QueryBuilderQuery;
use Darksparrow\DeegraphInteractions\Attributes\QueryBuilderRequiredField; use Darksparrow\DeegraphInteractions\Attributes\QueryBuilderRequiredField;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilderTrait; use Darksparrow\DeegraphInteractions\Interfaces\QueryBuilderInterface;
use Darksparrow\DeegraphInteractions\Traits\QueryBuilderTrait;
use Darksparrow\DeegraphInteractions\QueryInstance\InsertQuery;
#[QueryBuilderQuery] #[QueryBuilderQuery]
final class InsertQuery final class InsertQueryBuilder implements QueryBuilderInterface
{ {
use QueryBuilderTrait; use QueryBuilderTrait;
@ -25,11 +27,8 @@ final class InsertQuery
protected bool $Replace = false; protected bool $Replace = false;
public function __construct()
{
}
public function __toString(): string public function Build(): InsertQuery
{ {
$instance = new QueryBuilderQuery(); $instance = new QueryBuilderQuery();
$instance->ValidateValues($this); $instance->ValidateValues($this);
@ -42,41 +41,41 @@ final class InsertQuery
if($this->Duplicate) $builder .= "DUPLICATE"; if($this->Duplicate) $builder .= "DUPLICATE";
if($this->Replace) $builder .= "REPLACE"; if($this->Replace) $builder .= "REPLACE";
return $builder; return new InsertQuery($builder);
} }
public function RelativePath(string $relativePath): InsertQuery public function RelativePath(string $relativePath): InsertQueryBuilder
{ {
self::ValidateDeegraphPath(target: $relativePath); self::ValidateDeegraphPath(target: $relativePath);
$this->RelativePath = $relativePath; $this->RelativePath = $relativePath;
return $this; return $this;
} }
public function Keys(string $keys): InsertQuery public function Keys(string $keys): InsertQueryBuilder
{ {
$this->Keys = $keys; $this->Keys = $keys;
return $this; return $this;
} }
public function Schemas(string $schemas): InsertQuery public function Schemas(string $schemas): InsertQueryBuilder
{ {
$this->Schemas = $schemas; $this->Schemas = $schemas;
return $this; return $this;
} }
public function Values(string $values): InsertQuery public function Values(string $values): InsertQueryBuilder
{ {
$this->Values .= $values; $this->Values .= $values;
return $this; return $this;
} }
public function Duplicate(): InsertQuery public function Duplicate(): InsertQueryBuilder
{ {
$this->Duplicate = true; $this->Duplicate = true;
return $this; return $this;
} }
public function Replace(): InsertQuery public function Replace(): InsertQueryBuilder
{ {
$this->Replace = true; $this->Replace = true;
return $this; return $this;

67
src/QueryBuilder/QueryBuilders/PutQuery.php → src/QueryBuilder/QueryBuilders/PutQueryBuilder.php

@ -5,47 +5,47 @@ namespace Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders;
use Darksparrow\DeegraphInteractions\Exceptions\QueryBuilderConflictingFieldAlreadyExistsException; use Darksparrow\DeegraphInteractions\Exceptions\QueryBuilderConflictingFieldAlreadyExistsException;
use Darksparrow\DeegraphInteractions\Exceptions\QueryBuilderInvalidInputException; use Darksparrow\DeegraphInteractions\Exceptions\QueryBuilderInvalidInputException;
use Darksparrow\DeegraphInteractions\Exceptions\QueryBuilderRequiredFieldIsNotSetException; use Darksparrow\DeegraphInteractions\Exceptions\QueryBuilderRequiredFieldIsNotSetException;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilderTrait; use Darksparrow\DeegraphInteractions\Interfaces\QueryBuilderInterface;
use Darksparrow\DeegraphInteractions\QueryInstance\PutQuery;
use Darksparrow\DeegraphInteractions\Traits\QueryBuilderTrait;
final class PutQuery final class PutQueryBuilder implements QueryBuilderInterface
{ {
use QueryBuilderTrait; use QueryBuilderTrait;
protected string $PutWhat = ""; protected string $PutWhat = "";
protected string $PutData = "";
protected string $PutAt = ""; protected string $PutAt = "";
protected string $PutInto = ""; protected string $PutInto = "";
protected bool $Safe = false; protected bool $Safe = false;
public function __construct()
{
}
/** /**
* @throws QueryBuilderRequiredFieldIsNotSetException * @throws QueryBuilderRequiredFieldIsNotSetException
*/ */
public function __toString(): string public function Build(): PutQuery
{ {
$builder = "PUT"; $builder = "PUT $this->PutWhat";
if($this->PutWhat != "") $builder .= " $this->PutWhat"; if($this->PutData != "") $builder .= " \"$this->PutData\" ";
if($this->PutAt != "") $builder .= " $this->PutAt"; if($this->PutAt != "") $builder .= "AT $this->PutAt";
elseif ($this->PutInto != "") $builder .= " $this->PutInto"; elseif ($this->PutInto != "") $builder .= "INTO $this->PutInto";
else throw new QueryBuilderRequiredFieldIsNotSetException(); else throw new QueryBuilderRequiredFieldIsNotSetException();
if($this->Safe) $builder .= " SAFE"; if($this->Safe) $builder .= " SAFE";
return $builder;
return new PutQuery($builder);
} }
/** /**
* @throws QueryBuilderInvalidInputException * @throws QueryBuilderInvalidInputException
*/ */
public function Schema(string $uri): PutQuery public function Schema(string $schema): PutQueryBuilder
{ {
$this->PutWhat = self::RegexValidate( $this->PutWhat = "SCHEMA";
target: "SCHEMA \"$uri\"", $this->PutData = self::RegexValidate(
pattern: "/SCHEMA \".+\"/" target: $schema,
pattern: "/.+/"
); );
return $this; return $this;
} }
@ -53,11 +53,12 @@ final class PutQuery
/** /**
* @throws QueryBuilderInvalidInputException * @throws QueryBuilderInvalidInputException
*/ */
public function URI(string $uri): PutQuery public function URI(string $uri): PutQueryBuilder
{ {
$this->PutWhat = self::RegexValidate( $this->PutWhat = "URI";
target: "URI \"$uri\"", $this->PutData = self::RegexValidate(
pattern: "/URI \".+\"/" target: $uri,
pattern: "/.+/"
); );
return $this; return $this;
} }
@ -65,11 +66,12 @@ final class PutQuery
/** /**
* @throws QueryBuilderInvalidInputException * @throws QueryBuilderInvalidInputException
*/ */
public function DataURI(string $mimeType, string $data): PutQuery public function DataURI(string $mimeType, string $data): PutQueryBuilder
{ {
$this->PutWhat = self::RegexValidate( $this->PutWhat = "URI";
target: "URI \"data:$mimeType;$data\"", $this->PutData = self::RegexValidate(
pattern: "/URI \"data:[a-zA-Z0-9]/[a-zA-Z0-9];.+\"/" target: "data:$mimeType;$data",
pattern: "data:[a-zA-Z0-9]/[a-zA-Z0-9];.+/"
); );
return $this; return $this;
} }
@ -79,12 +81,11 @@ final class PutQuery
* @throws QueryBuilderInvalidInputException * @throws QueryBuilderInvalidInputException
* @throws QueryBuilderConflictingFieldAlreadyExistsException * @throws QueryBuilderConflictingFieldAlreadyExistsException
*/ */
public function At(string $node, string $uwu): PutQuery public function At(string $relativePath): PutQueryBuilder
{ {
self::EnsureNotSet($this->PutInto); self::EnsureNotSet($this->PutInto);
$this->PutAt = self::RegexValidate( $this->PutAt = self::ValidateDeegraphPath(
target: 'AT {' . $node . '}/' . $uwu, target: $relativePath
pattern: "/AT {[a-zA-Z0-9\-]+}\/[a-zA-Z0-9]+/"
); );
return $this; return $this;
} }
@ -94,18 +95,18 @@ final class PutQuery
* @throws QueryBuilderInvalidInputException * @throws QueryBuilderInvalidInputException
* @throws QueryBuilderConflictingFieldAlreadyExistsException * @throws QueryBuilderConflictingFieldAlreadyExistsException
*/ */
public function Into(string $relativePath, string $propertyName): PutQuery public function Into(string $relativePath, string $propertyName): PutQueryBuilder
{ {
self::EnsureNotSet($this->PutAt); self::EnsureNotSet($this->PutAt);
$this->PutInto = self::RegexValidate( $this->PutInto = self::RegexValidate(
target: "INTO \"$relativePath\" AS \"$propertyName\"", target: "\"$relativePath\" AS \"$propertyName\"",
pattern: "/INTO \"[a-zA-Z0-9\-]+\" AS \"[a-zA-Z0-9]+\"/" pattern: "/\"[a-zA-Z0-9\-]+\" AS \"[a-zA-Z0-9]+\"/"
); );
return $this; return $this;
} }
public function Safe(): PutQuery public function Safe(): PutQueryBuilder
{ {
$this->Safe = true; $this->Safe = true;
return $this; return $this;

28
src/QueryBuilder/QueryBuilders/SelectQuery.php → src/QueryBuilder/QueryBuilders/SelectQueryBuilder.php

@ -6,10 +6,12 @@ use Darksparrow\DeegraphInteractions\Attributes\QueryBuilderQuery;
use Darksparrow\DeegraphInteractions\Attributes\QueryBuilderRequiredField; use Darksparrow\DeegraphInteractions\Attributes\QueryBuilderRequiredField;
use Darksparrow\DeegraphInteractions\Enumerators\DeegraphEqualityOperator; use Darksparrow\DeegraphInteractions\Enumerators\DeegraphEqualityOperator;
use Darksparrow\DeegraphInteractions\Exceptions\QueryBuilderRequiredFieldIsNotSetException; use Darksparrow\DeegraphInteractions\Exceptions\QueryBuilderRequiredFieldIsNotSetException;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilderTrait; use Darksparrow\DeegraphInteractions\Interfaces\QueryBuilderInterface;
use Darksparrow\DeegraphInteractions\QueryInstance\SelectQuery;
use Darksparrow\DeegraphInteractions\Traits\QueryBuilderTrait;
#[QueryBuilderQuery] #[QueryBuilderQuery]
final class SelectQuery final class SelectQueryBuilder implements QueryBuilderInterface
{ {
use QueryBuilderTrait; use QueryBuilderTrait;
@ -20,14 +22,16 @@ final class SelectQuery
protected string $InstanceOf = ""; protected string $InstanceOf = "";
public function __construct()
{
}
/** /**
* @throws QueryBuilderRequiredFieldIsNotSetException * @throws QueryBuilderRequiredFieldIsNotSetException
*/ */
public function __toString(): string public function __toString(): string
{
return $this->Build();
}
public function Build(): SelectQuery
{ {
$builder = "SELECT "; $builder = "SELECT ";
if(sizeof($this->RelativePaths)) $builder .= " " . implode(separator: ", ", array: $this->RelativePaths); if(sizeof($this->RelativePaths)) $builder .= " " . implode(separator: ", ", array: $this->RelativePaths);
@ -35,30 +39,30 @@ final class SelectQuery
if($this->Where != "") $builder .= " WHERE $this->Where"; if($this->Where != "") $builder .= " WHERE $this->Where";
if($this->InstanceOf != "") $builder .= " INSTANCEOF $this->InstanceOf"; if($this->InstanceOf != "") $builder .= " INSTANCEOF $this->InstanceOf";
return $builder; return new SelectQuery(queryString: $builder);
} }
public function RelativePath(string $relativePath): SelectQuery public function RelativePath(string $relativePath): SelectQueryBuilder
{ {
$this->RelativePaths = [$relativePath]; $this->RelativePaths = [$relativePath];
return $this; return $this;
} }
public function RelativePaths(array $relativePaths): SelectQuery public function RelativePaths(array $relativePaths): SelectQueryBuilder
{ {
$this->RelativePaths = $relativePaths; $this->RelativePaths = $relativePaths;
return $this; return $this;
} }
public function From(string $target): SelectQuery public function From(string $target): SelectQueryBuilder
{ {
$this->From = "" . $target; $this->From = "" . $target;
return $this; return $this;
} }
public function Where(string $target, DeegraphEqualityOperator $operator, string $value): SelectQuery public function Where(string $target, DeegraphEqualityOperator $operator, string $value): SelectQueryBuilder
{ {
$this->Where = "" . $target . " " . $operator->value . " " . $value; $this->Where = "" . $target . " " . $operator->value . " " . $value;
return $this; return $this;
} }
public function InstanceOf(string $schema): SelectQuery public function InstanceOf(string $schema): SelectQueryBuilder
{ {
$this->InstanceOf = "" . $schema; $this->InstanceOf = "" . $schema;
return $this; return $this;

21
src/QueryInstance/GrantQuery.php

@ -0,0 +1,21 @@
<?php
namespace Darksparrow\DeegraphInteractions\QueryInstance;
use Darksparrow\DeegraphInteractions\Core\DeegraphServer;
use Darksparrow\DeegraphInteractions\Interfaces\QueryInstanceInterface;
use Darksparrow\DeegraphInteractions\QueryResponse\GrantQueryResponse;
use Darksparrow\DeegraphInteractions\Superclasses\QueryInstanceSuperclass;
final class GrantQuery extends QueryInstanceSuperclass implements QueryInstanceInterface
{
public function RunQuery(DeegraphServer $deegraphServer)
{
$response = $deegraphServer->RunRawRequest(
endpoint: "/api/v1/@query",
method: "POST",
body: $this->QueryString
);
return GrantQueryResponse::FromAPIResponse(response: $response);
}
}

20
src/QueryInstance/InsertQuery.php

@ -0,0 +1,20 @@
<?php
namespace Darksparrow\DeegraphInteractions\QueryInstance;
use Darksparrow\DeegraphInteractions\Core\DeegraphServer;
use Darksparrow\DeegraphInteractions\QueryResponse\InsertQueryResponse;
use Darksparrow\DeegraphInteractions\Superclasses\QueryInstanceSuperclass;
final class InsertQuery extends QueryInstanceSuperclass implements QueryInstanceInterface
{
public function RunQuery(DeegraphServer $deegraphServer)
{
$response = $deegraphServer->RunRawRequest(
endpoint: "/api/v1/@query",
method: "POST",
body: $this->QueryString
);
return InsertQueryResponse::FromAPIResponse(response: $response);
}
}

21
src/QueryInstance/PutQuery.php

@ -0,0 +1,21 @@
<?php
namespace Darksparrow\DeegraphInteractions\QueryInstance;
use Darksparrow\DeegraphInteractions\Core\DeegraphServer;
use Darksparrow\DeegraphInteractions\Interfaces\QueryResponseInterface;
use Darksparrow\DeegraphInteractions\QueryResponse\PutQueryResponse;
use Darksparrow\DeegraphInteractions\Superclasses\QueryInstanceSuperclass;
final class PutQuery extends QueryInstanceSuperclass implements QueryResponseInterface
{
public function RunQuery(DeegraphServer $deegraphServer)
{
$response = $deegraphServer->RunRawRequest(
endpoint: "/api/v1/@query",
method: "POST",
body: $this->QueryString
);
return PutQueryResponse::FromAPIResponse(response: $response);
}
}

21
src/QueryInstance/SelectQuery.php

@ -0,0 +1,21 @@
<?php
namespace Darksparrow\DeegraphInteractions\QueryInstance;
use Darksparrow\DeegraphInteractions\Core\DeegraphServer;
use Darksparrow\DeegraphInteractions\Interfaces\QueryInstanceInterface;
use Darksparrow\DeegraphInteractions\QueryResponse\SelectQueryResponse;
use Darksparrow\DeegraphInteractions\Superclasses\QueryInstanceSuperclass;
final class SelectQuery extends QueryInstanceSuperclass implements QueryInstanceInterface
{
public function RunQuery(DeegraphServer $deegraphServer)
{
$response = $deegraphServer->RunRawRequest(
endpoint: "/api/v1/@query",
method: "POST",
body: $this->QueryString
);
return new SelectQueryResponse(response: $response);
}
}

17
src/QueryResponse/GrantQueryResponse.php

@ -0,0 +1,17 @@
<?php
namespace Darksparrow\DeegraphInteractions\QueryResponse;
use Darksparrow\DeegraphInteractions\Interfaces\QueryResponseInterface;
final class GrantQueryResponse implements QueryResponseInterface
{
protected string $RuleID;
public function __construct(string $response)
{
$response = json_decode($response, true);
$this->RuleID = $response["@rule_id"];
}
}

13
src/QueryResponse/InsertQueryResponse.php

@ -0,0 +1,13 @@
<?php
namespace Darksparrow\DeegraphInteractions\QueryResponse;
use Darksparrow\DeegraphInteractions\Interfaces\QueryResponseInterface;
final class InsertQueryResponse implements QueryResponseInterface
{
public function __construct(string $response)
{
$response = json_decode($response, true);
}
}

14
src/QueryResponse/PutQueryResponse.php

@ -0,0 +1,14 @@
<?php
namespace Darksparrow\DeegraphInteractions\QueryResponse;
use Darksparrow\DeegraphInteractions\Interfaces\QueryResponseInterface;
final class PutQueryResponse implements QueryResponseInterface
{
public function __construct(string $response)
{
$response = json_decode($response, true);
}
}

43
src/QueryResponse/SelectQueryResponse.php

@ -0,0 +1,43 @@
<?php
namespace Darksparrow\DeegraphInteractions\QueryResponse;
use Darksparrow\DeegraphInteractions\Interfaces\QueryResponseInterface;
final class SelectQueryResponse implements QueryResponseInterface
{
public array $Rows;
public string $RowFormat;
public function __construct(string $response)
{
$response = json_decode($response, true);
echo "\n\n\n";
var_dump($response);
echo "\n\n\n";
if(array_key_exists(key: "@rows", array: $response))
{
$this->Rows = [];
foreach($response["@rows"] as $row)
$this->Rows[] = QueryResponseRow::FromArray(array: $row);
$this->RowFormat = $response["@row_format"];
}
}
public function FlattenRows(): array
{
$builder = [];
foreach($this->Rows as $row)
{
foreach($row->Results as $result)
{
$builder[] = $result;
}
}
return $builder;
}
}

28
src/Superclasses/QueryInstanceSuperclass.php

@ -0,0 +1,28 @@
<?php
namespace Darksparrow\DeegraphInteractions\Superclasses;
class QueryInstanceSuperclass
{
protected string $QueryString;
/**
* Build a new Query Instance from a Query String.
*
* @param string $queryString
*/
public function __construct(string $queryString)
{
$this->QueryString = $queryString;
}
/**
* Returns back the Query Instance as a string object.
*
* @return string
*/
public function __toString(): string
{
return $this->QueryString;
}
}

2
src/QueryBuilder/QueryBuilderTrait.php → src/Traits/QueryBuilderTrait.php

@ -1,6 +1,6 @@
<?php <?php
namespace Darksparrow\DeegraphInteractions\QueryBuilder; namespace Darksparrow\DeegraphInteractions\Traits;
use Darksparrow\DeegraphInteractions\Exceptions\QueryBuilderConflictingFieldAlreadyExistsException; use Darksparrow\DeegraphInteractions\Exceptions\QueryBuilderConflictingFieldAlreadyExistsException;
use Darksparrow\DeegraphInteractions\Exceptions\QueryBuilderInvalidInputException; use Darksparrow\DeegraphInteractions\Exceptions\QueryBuilderInvalidInputException;
Loading…
Cancel
Save