Browse Source

committing what i've got before going home lol

Cerys Lewis 5 months ago
parent
commit
a1a7c69b5d
  1. 21
      examples/deleteQuery.php
  2. 5
      examples/grantQuery.php
  3. 41
      examples/test.php
  4. 28
      src/Attributes/QueryBuilderQuery.php
  5. 35
      src/DataStructures/DataURL.php
  6. 5
      src/QueryBuilder/QueryBuilders/DeleteQueryBuilder.php
  7. 7
      src/QueryBuilder/QueryBuilders/GrantQueryBuilder.php
  8. 3
      src/QueryBuilder/QueryBuilders/InsertQueryBuilder.php
  9. 2
      src/QueryBuilder/QueryBuilders/PutQueryBuilder.php
  10. 2
      src/QueryBuilder/QueryBuilders/SelectQueryBuilder.php
  11. 4
      src/QueryInstance/DeleteQuery.php
  12. 34
      src/Traits/QueryBuilderTrait.php

21
examples/deleteQuery.php

@ -0,0 +1,21 @@
<?php
// https://github.com/owoalex/deegraph/blob/main/docs/query-delete.md#examples
global $db;
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../examples/connection.php';
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilder;
use Darksparrow\DeegraphInteractions\QueryInstance\DeleteQuery;
function Example0(): DeleteQuery
{
$queryBuilder = QueryBuilder::Delete()
->RelativePath(relativePath: "");
return $queryBuilder->Build();
}
$result = Example0()->RunQuery($db);
var_dump($result);

5
examples/grantQuery.php

@ -25,7 +25,10 @@ function Example0(): GrantQuery
function Example1(): GrantQuery
{
$queryBuilder = QueryBuilder::Grant()
->GrantAll()
->Permissions(permissionTypes: [
DeegraphPermissionType::WRITE,
DeegraphPermissionType::READ,
])
->On(target: "*")
->IsDelegatable();

41
examples/test.php

@ -0,0 +1,41 @@
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use Darksparrow\DeegraphInteractions\Core\DeegraphServer;
use Darksparrow\DeegraphInteractions\DataStructures\DataURL;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilder;
$db = new DeegraphServer(
token: "UFVUX1lPVVJfVE9LRU5fSEVSRQ==",
actor: "db36576b-cc5c-573a-8ae2-00258884d8c6",
allowSelfSignedCerts: true,
);
/*
$insertQuery = QueryBuilder::Put()
->Schema(schema: "https://schemas.auxiliumsoftware.co.uk/v1/collection.json")
->At(relativePath: "{970334ed-1f4f-465a-94d7-923a99698786}/todo");
$insertQuery = QueryBuilder::Put()
->URI(uri: DataURL::BuildBase64(unencodedString: "nya~"))
->At(relativePath: "{970334ed-1f4f-465a-94d7-923a99698786}/example");
$insertQuery = QueryBuilder::Insert()
->RelativePath(relativePath: "{970334ed-1f4f-465a-94d7-923a99698786}/example");
echo $insertQuery;
echo "\n";
$result = $db->RunQuery($insertQuery);
var_dump($result);
*/
$selectQueryBuilder = QueryBuilder::Select()
->RelativePath(relativePath: ".")
->From(target: "**");
$selectQuery = $selectQueryBuilder->Build();
$result = $selectQuery->RunQuery($db);
foreach($result->FlattenRows() as $row)
{
echo "$row\n";
}

28
src/Attributes/QueryBuilderQuery.php

@ -16,32 +16,4 @@ use ReflectionClass;
{
parent::__construct(new Name("QueryBuilderQuery", []), [], []);
}
public function ValidateValues(
InsertQueryBuilder|PutQueryBuilder|SelectQueryBuilder $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();
}
}
}
}
}

35
src/DataStructures/DataURL.php

@ -0,0 +1,35 @@
<?php
namespace Darksparrow\DeegraphInteractions\DataStructures;
class DataURL
{
private string $Data;
private string $MIMEType;
public function __construct(string $mimeType, string $data)
{
$this->Data = $data;
$this->MIMEType = $mimeType;
}
public static function FromDataURL(string $dataURL): DataURL
{
}
public static function BuildBase64(string $unencodedString): DataURL
{
return new DataURL(mimeType: "text/plain", data: "base64," . base64_encode(string: $unencodedString));
}
public function __toString(): string
{
$builder = "data:";
$builder .= $this->MIMEType;
$builder .= ",";
$builder .= urlencode(string: $this->Data);
return $builder;
}
}

5
src/QueryBuilder/QueryBuilders/DeleteQueryBuilder.php

@ -10,10 +10,13 @@ use Darksparrow\DeegraphInteractions\Exceptions\QueryBuilderRequiredFieldIsNotSe
use Darksparrow\DeegraphInteractions\Interfaces\QueryBuilderInterface;
use Darksparrow\DeegraphInteractions\QueryInstance\DeleteQuery;
use Darksparrow\DeegraphInteractions\QueryInstance\GrantQuery;
use Darksparrow\DeegraphInteractions\Traits\QueryBuilderTrait;
#[QueryBuilderQuery]
final class DeleteQueryBuilder implements QueryBuilderInterface
{
use QueryBuilderTrait;
#[QueryBuilderRequiredField]
protected string $RelativePath;
@ -23,6 +26,8 @@ final class DeleteQueryBuilder implements QueryBuilderInterface
*/
public function Build(): DeleteQuery
{
self::ValidateValues($this);
$builder = "DELETE ";
if($this->RelativePath != "") $builder .= $this->RelativePath;

7
src/QueryBuilder/QueryBuilders/GrantQueryBuilder.php

@ -9,10 +9,13 @@ use Darksparrow\DeegraphInteractions\Enumerators\DeegraphPermissionType;
use Darksparrow\DeegraphInteractions\Exceptions\QueryBuilderRequiredFieldIsNotSetException;
use Darksparrow\DeegraphInteractions\Interfaces\QueryBuilderInterface;
use Darksparrow\DeegraphInteractions\QueryInstance\GrantQuery;
use Darksparrow\DeegraphInteractions\Traits\QueryBuilderTrait;
#[QueryBuilderQuery]
final class GrantQueryBuilder implements QueryBuilderInterface
{
use QueryBuilderTrait;
#[QueryBuilderRequiredField]
protected array $PermissionTypes;
@ -26,8 +29,10 @@ final class GrantQueryBuilder implements QueryBuilderInterface
*/
public function Build(): GrantQuery
{
self::ValidateValues($this);
$builder = "GRANT ";
if(sizeof($this->PermissionTypes)) $builder .= implode(separator: ",", array: $this->PermissionTypes);
if($this->GrantOn != "") $builder .= " ON $this->GrantOn";
if($this->GrantWhere != "") $builder .= " WHERE $this->GrantWhere";

3
src/QueryBuilder/QueryBuilders/InsertQueryBuilder.php

@ -29,8 +29,7 @@ final class InsertQueryBuilder implements QueryBuilderInterface
public function Build(): InsertQuery
{
$instance = new QueryBuilderQuery();
$instance->ValidateValues($this);
self::ValidateValues($this);
$builder = "INSERT INTO $this->RelativePath";

2
src/QueryBuilder/QueryBuilders/PutQueryBuilder.php

@ -24,6 +24,8 @@ final class PutQueryBuilder implements QueryBuilderInterface
*/
public function Build(): PutQuery
{
self::ValidateValues($this);
$builder = "PUT $this->PutWhat";
if($this->PutData != "") $builder .= " \"$this->PutData\" ";

2
src/QueryBuilder/QueryBuilders/SelectQueryBuilder.php

@ -33,6 +33,8 @@ final class SelectQueryBuilder implements QueryBuilderInterface
public function Build(): SelectQuery
{
self::ValidateValues($this);
$builder = "SELECT ";
if(sizeof($this->RelativePaths)) $builder .= " " . implode(separator: ", ", array: $this->RelativePaths);
if($this->From != "") $builder .= " FROM " . $this->From;

4
src/QueryInstance/DeleteQuery.php

@ -13,8 +13,8 @@ final class DeleteQuery extends QueryInstanceSuperclass implements QueryInstance
public function RunQuery(DeegraphServer $deegraphServer): DeleteQueryResponse
{
$response = $deegraphServer->RunRawRequest(
endpoint: "/api/v1/@query",
method: "POST",
endpoint: "/api/v1/@delete",
method: "DELETE",
body: $this->QueryString,
);
return new DeleteQueryResponse(response: $response);

34
src/Traits/QueryBuilderTrait.php

@ -4,6 +4,13 @@ namespace Darksparrow\DeegraphInteractions\Traits;
use Darksparrow\DeegraphInteractions\Exceptions\QueryBuilderConflictingFieldAlreadyExistsException;
use Darksparrow\DeegraphInteractions\Exceptions\QueryBuilderInvalidInputException;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\DeleteQueryBuilder;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\GrantQueryBuilder;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\InsertQueryBuilder;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\PutQueryBuilder;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\SelectQueryBuilder;
use Exception;
use ReflectionClass;
trait QueryBuilderTrait
{
@ -26,4 +33,31 @@ trait QueryBuilderTrait
throw new QueryBuilderInvalidInputException();
return $target;
}
public function ValidateValues(
DeleteQueryBuilder|GrantQueryBuilder|InsertQueryBuilder|PutQueryBuilder|SelectQueryBuilder $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();
}
}
}
}
}

Loading…
Cancel
Save