Browse Source

bits and bobs i've done over the last 6 months lol

dev
Cerys 4 weeks ago
parent
commit
6d2ad27a34
  1. 8
      src/Attributes/QueryBuilderQuery.php
  2. 20
      src/Core/DeegraphServer.php
  3. 22
      src/DataStructures/QueryResponseRow.php
  4. 26
      src/QueryBuilder/InsertQuery/InsertQuery.php
  5. 43
      src/QueryBuilder/InsertQuery/InsertQueryBuilder.php
  6. 8
      src/QueryBuilder/InsertQuery/InsertQueryBuilderInterface.php
  7. 26
      src/QueryBuilder/PermissionsQuery/PermissionQuery.php
  8. 44
      src/QueryBuilder/PermissionsQuery/PermissionQueryBuilder.php
  9. 8
      src/QueryBuilder/PermissionsQuery/PermissionQueryBuilderInterface.php
  10. 33
      src/QueryBuilder/PermissionsQuery/PermissionQueryResponse.php
  11. 16
      src/QueryBuilder/PutQuery/PutQueryBuilder.php
  12. 19
      src/QueryBuilder/QueryBuilder.php
  13. 26
      src/QueryBuilder/SelectQuery/SelectQuery.php
  14. 40
      src/QueryBuilder/SelectQuery/SelectQueryBuilder.php
  15. 8
      src/QueryBuilder/SelectQuery/SelectQueryBuilderInterface.php
  16. 18
      src/QueryBuilder/SelectQuery/SelectQueryResponse.php

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\InsertQuery\InsertQuery;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\PutQuery; use Darksparrow\DeegraphInteractions\QueryBuilder\PutQuery\PutQueryBuilder;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\SelectQuery; use Darksparrow\DeegraphInteractions\QueryBuilder\SelectQuery\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 InsertQuery|PutQueryBuilder|SelectQueryBuilder $target
): void ): void
{ {
$nameBase = "Darksparrow\\DeegraphPHP\\Attributes"; $nameBase = "Darksparrow\\DeegraphPHP\\Attributes";

20
src/Core/DeegraphServer.php

@ -2,11 +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\InsertQuery;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\PutQuery;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\SelectQuery;
class DeegraphServer class DeegraphServer
{ {
@ -43,7 +39,7 @@ 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, [
@ -80,18 +76,4 @@ class DeegraphServer
return $result; return $result;
} }
/**
* @param InsertQuery|PutQuery|SelectQuery $query Takes in a Query Builder object.
* @return QueryResponseWrapper
*/
public function RunQuery(InsertQuery|PutQuery|SelectQuery $query): QueryResponseWrapper
{
$response = $this->RunRawRequest(
endpoint: "/api/v1/@query",
method: "POST",
body: $query
);
return QueryResponseWrapper::FromAPIResponse($response);
}
} }

22
src/DataStructures/QueryResponseRow.php

@ -4,20 +4,22 @@ namespace Darksparrow\DeegraphInteractions\DataStructures;
class QueryResponseRow class QueryResponseRow
{ {
public string $Search; public ?string $Search;
public array $Results; public ?array $Results;
public static function FromArray(array $array): QueryResponseRow public function __construct(array $array)
{ {
$builder = new QueryResponseRow(); if($array == [])
$builder->Search = key(array: $array);
foreach($array["{$builder->Search}"] as $key=>$value)
{ {
$builder->Results[] = new KeyValuePair(key: $key, value: $value); $this->Search = null;
$this->Results = null;
return;
}
$this->Search = key(array: $array);
foreach($array["{$this->Search}"] as $key=>$value)
{
$this->Results[] = new KeyValuePair(key: $key, value: $value);
} }
return $builder;
} }
public function __toString(): string public function __toString(): string

26
src/QueryBuilder/InsertQuery/InsertQuery.php

@ -0,0 +1,26 @@
<?php
namespace Darksparrow\DeegraphInteractions\QueryBuilder\InsertQuery;
use Darksparrow\DeegraphInteractions\Attributes\QueryBuilderQuery;
#[QueryBuilderQuery]
final class InsertQuery
{
public string $QueryString;
public function __construct(InsertQueryBuilder $queryBuilder)
{
$instance = new QueryBuilderQuery();
$instance->ValidateValues($queryBuilder);
$builder = "INSERT INTO $queryBuilder->RelativePath";
if(sizeof($queryBuilder->Keys) > 0) $builder .= "KEYS " . implode(separator: ", ", array: $queryBuilder->Keys);
if(sizeof($queryBuilder->Schemas) > 0) $builder .= "SCHEMAS " . implode(separator: ", ", array: $queryBuilder->Schemas);
if($queryBuilder->Values != "") $builder .= "VALUES $queryBuilder->Values";
if($queryBuilder->Duplicate) $builder .= "DUPLICATE";
if($queryBuilder->Replace) $builder .= "REPLACE";
}
}

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

@ -1,13 +1,13 @@
<?php <?php
namespace Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders; namespace Darksparrow\DeegraphInteractions\QueryBuilder\InsertQuery;
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\QueryBuilder\QueryBuilderTrait;
#[QueryBuilderQuery] #[QueryBuilderQuery]
final class InsertQuery final class InsertQueryBuilder implements InsertQueryBuilderInterface
{ {
use QueryBuilderTrait; use QueryBuilderTrait;
@ -31,54 +31,59 @@ final class InsertQuery
public function __toString(): string 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): 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;
} }
public function Build(): InsertQuery
{
$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;
}
} }

8
src/QueryBuilder/InsertQuery/InsertQueryBuilderInterface.php

@ -0,0 +1,8 @@
<?php
namespace Darksparrow\DeegraphInteractions\QueryBuilder\InsertQuery;
interface InsertQueryBuilderInterface
{
public function Build() : InsertQuery;
}

26
src/QueryBuilder/PermissionsQuery/PermissionQuery.php

@ -0,0 +1,26 @@
<?php
namespace Darksparrow\DeegraphInteractions\QueryBuilder\PermissionsQuery;
use Darksparrow\DeegraphInteractions\Core\DeegraphServer;
class PermissionQuery
{
protected string $QueryString;
public function __construct(string $queryString)
{
$this->QueryString = $queryString;
}
public function RunQuery(DeegraphServer $server): PermissionQueryResponse
{
$response = $server->RunRawRequest(
endpoint: "/api/v1/@query",
method: "POST",
body: $this->QueryString
);
$temp = json_decode($response, true);
return new PermissionQueryResponse($temp);
}
}

44
src/QueryBuilder/PermissionsQuery/PermissionQueryBuilder.php

@ -0,0 +1,44 @@
<?php
namespace Darksparrow\DeegraphInteractions\QueryBuilder\PermissionsQuery;
use Darksparrow\DeegraphInteractions\Attributes\QueryBuilderQuery;
use Darksparrow\DeegraphInteractions\Attributes\QueryBuilderRequiredField;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilderTrait;
#[QueryBuilderQuery]
class PermissionQueryBuilder
{
use QueryBuilderTrait;
#[QueryBuilderRequiredField]
protected string $On = "";
#[QueryBuilderRequiredField]
protected string $As = "";
public function On(string $relativePath): PermissionQueryBuilder
{
// self::ValidateDeegraphPath(target: $relativePath);
$this->On = $relativePath;
return $this;
}
public function As(string $relativePath): PermissionQueryBuilder
{
// self::ValidateDeegraphPath(target: $relativePath);
$this->As = $relativePath;
return $this;
}
public function Build(): PermissionQuery
{
$builder = "PERMISSIONS ";
if($this->On != "") $builder .= " ON {". $this->On . "}";
if($this->As != "") $builder .= " AS {". $this->As . "}";
return new PermissionQuery(queryString: $builder);
}
}

8
src/QueryBuilder/PermissionsQuery/PermissionQueryBuilderInterface.php

@ -0,0 +1,8 @@
<?php
namespace Darksparrow\DeegraphInteractions\QueryBuilder\PermissionsQuery;
interface PermissionQueryBuilderInterface
{
public function Build() : PermissionQuery;
}

33
src/QueryBuilder/PermissionsQuery/PermissionQueryResponse.php

@ -0,0 +1,33 @@
<?php
namespace Darksparrow\DeegraphInteractions\QueryBuilder\PermissionsQuery;
class PermissionQueryResponse
{
public bool $CanAct = false;
public bool $CanDelete = false;
public bool $CanRead = false;
public bool $CanWrite = false;
public function __construct(array $deegraphResponse)
{
foreach ($deegraphResponse["@permissions"] as $permission)
{
switch($permission)
{
case "ACT":
$this->CanAct = true;
break;
case "DELETE":
$this->CanDelete = true;
break;
case "READ":
$this->CanRead = true;
break;
case "WRITE":
$this->CanWrite = true;
break;
}
}
}
}

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

@ -1,13 +1,13 @@
<?php <?php
namespace Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders; namespace Darksparrow\DeegraphInteractions\QueryBuilder\PutQuery;
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\QueryBuilder\QueryBuilderTrait;
final class PutQuery final class PutQueryBuilder
{ {
use QueryBuilderTrait; use QueryBuilderTrait;
@ -41,7 +41,7 @@ final class PutQuery
/** /**
* @throws QueryBuilderInvalidInputException * @throws QueryBuilderInvalidInputException
*/ */
public function Schema(string $uri): PutQuery public function Schema(string $uri): PutQueryBuilder
{ {
$this->PutWhat = self::RegexValidate( $this->PutWhat = self::RegexValidate(
target: "SCHEMA \"$uri\"", target: "SCHEMA \"$uri\"",
@ -53,7 +53,7 @@ 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 = self::RegexValidate(
target: "URI \"$uri\"", target: "URI \"$uri\"",
@ -65,7 +65,7 @@ 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 = self::RegexValidate(
target: "URI \"data:$mimeType;$data\"", target: "URI \"data:$mimeType;$data\"",
@ -79,7 +79,7 @@ final class PutQuery
* @throws QueryBuilderInvalidInputException * @throws QueryBuilderInvalidInputException
* @throws QueryBuilderConflictingFieldAlreadyExistsException * @throws QueryBuilderConflictingFieldAlreadyExistsException
*/ */
public function At(string $node, string $uwu): PutQuery public function At(string $node, string $uwu): PutQueryBuilder
{ {
self::EnsureNotSet($this->PutInto); self::EnsureNotSet($this->PutInto);
$this->PutAt = self::RegexValidate( $this->PutAt = self::RegexValidate(
@ -94,7 +94,7 @@ 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(
@ -105,7 +105,7 @@ final class PutQuery
} }
public function Safe(): PutQuery public function Safe(): PutQueryBuilder
{ {
$this->Safe = true; $this->Safe = true;
return $this; return $this;

19
src/QueryBuilder/QueryBuilder.php

@ -2,9 +2,10 @@
namespace Darksparrow\DeegraphInteractions\QueryBuilder; namespace Darksparrow\DeegraphInteractions\QueryBuilder;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\InsertQuery; use Darksparrow\DeegraphInteractions\QueryBuilder\InsertQuery\InsertQuery;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\PutQuery; use Darksparrow\DeegraphInteractions\QueryBuilder\PermissionsQuery\PermissionQueryBuilder;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\SelectQuery; use Darksparrow\DeegraphInteractions\QueryBuilder\PutQuery\PutQueryBuilder;
use Darksparrow\DeegraphInteractions\QueryBuilder\SelectQuery\SelectQueryBuilder;
final class QueryBuilder final class QueryBuilder
{ {
@ -12,12 +13,16 @@ final class QueryBuilder
{ {
return new InsertQuery(); return new InsertQuery();
} }
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();
}
public static function Permission(): PermissionQueryBuilder
{
return new PermissionQueryBuilder();
} }
} }

26
src/QueryBuilder/SelectQuery/SelectQuery.php

@ -0,0 +1,26 @@
<?php
namespace Darksparrow\DeegraphInteractions\QueryBuilder\SelectQuery;
use Darksparrow\DeegraphInteractions\Core\DeegraphServer;
class SelectQuery
{
protected string $QueryString;
public function __construct(string $queryString)
{
$this->QueryString = $queryString;
}
public function RunQuery(DeegraphServer $server): array
{
$response = $server->RunRawRequest(
endpoint: "/api/v1/@query",
method: "POST",
body: $this->QueryString
);
$temp = json_decode($response, true);
return $temp;
}
}

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

@ -1,15 +1,14 @@
<?php <?php
namespace Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders; namespace Darksparrow\DeegraphInteractions\QueryBuilder\SelectQuery;
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\DeegraphEqualityOperator;
use Darksparrow\DeegraphInteractions\Exceptions\QueryBuilderRequiredFieldIsNotSetException;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilderTrait; use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilderTrait;
#[QueryBuilderQuery] #[QueryBuilderQuery]
final class SelectQuery final class SelectQueryBuilder implements SelectQueryBuilderInterface
{ {
use QueryBuilderTrait; use QueryBuilderTrait;
@ -24,43 +23,40 @@ final class SelectQuery
{ {
} }
/** public function RelativePath(string $relativePath): SelectQueryBuilder
* @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 RelativePath(string $relativePath): SelectQuery
{ {
$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;
} }
public function Build(): SelectQuery
{
$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 new SelectQuery(queryString: $builder);
}
} }

8
src/QueryBuilder/SelectQuery/SelectQueryBuilderInterface.php

@ -0,0 +1,8 @@
<?php
namespace Darksparrow\DeegraphInteractions\QueryBuilder\SelectQuery;
interface SelectQueryBuilderInterface
{
public function Build() : SelectQuery;
}

18
src/QueryBuilder/SelectQuery/SelectQueryResponse.php

@ -0,0 +1,18 @@
<?php
namespace Darksparrow\DeegraphInteractions\QueryBuilder\SelectQuery;
use Darksparrow\DeegraphInteractions\DataStructures\QueryResponseRow;
class SelectQueryResponse
{
public array $Rows;
public string $RowFormat;
public function __construct(array $response)
{
foreach($response["@rows"] as $row)
$this->Rows[] = new QueryResponseRow(array: $row);
$this->RowFormat = $response["@row_format"];
}
}
Loading…
Cancel
Save