From 6d2ad27a34075c56d9b5ae74967c6f7379cccc3e Mon Sep 17 00:00:00 2001 From: Cerys Date: Thu, 28 Nov 2024 13:57:14 +0000 Subject: [PATCH] bits and bobs i've done over the last 6 months lol --- src/Attributes/QueryBuilderQuery.php | 8 +- src/Core/DeegraphServer.php | 20 +- src/DataStructures/QueryResponseRow.php | 22 +- src/QueryBuilder/InsertQuery/InsertQuery.php | 26 ++ .../InsertQueryBuilder.php} | 171 ++++++------- .../InsertQueryBuilderInterface.php | 8 + .../PermissionsQuery/PermissionQuery.php | 26 ++ .../PermissionQueryBuilder.php | 44 ++++ .../PermissionQueryBuilderInterface.php | 8 + .../PermissionQueryResponse.php | 33 +++ .../PutQueryBuilder.php} | 226 +++++++++--------- src/QueryBuilder/QueryBuilder.php | 19 +- src/QueryBuilder/SelectQuery/SelectQuery.php | 26 ++ .../SelectQueryBuilder.php} | 126 +++++----- .../SelectQueryBuilderInterface.php | 8 + .../SelectQuery/SelectQueryResponse.php | 18 ++ 16 files changed, 488 insertions(+), 301 deletions(-) create mode 100644 src/QueryBuilder/InsertQuery/InsertQuery.php rename src/QueryBuilder/{QueryBuilders/InsertQuery.php => InsertQuery/InsertQueryBuilder.php} (59%) mode change 100755 => 100644 create mode 100644 src/QueryBuilder/InsertQuery/InsertQueryBuilderInterface.php create mode 100644 src/QueryBuilder/PermissionsQuery/PermissionQuery.php create mode 100644 src/QueryBuilder/PermissionsQuery/PermissionQueryBuilder.php create mode 100644 src/QueryBuilder/PermissionsQuery/PermissionQueryBuilderInterface.php create mode 100644 src/QueryBuilder/PermissionsQuery/PermissionQueryResponse.php rename src/QueryBuilder/{QueryBuilders/PutQuery.php => PutQuery/PutQueryBuilder.php} (86%) mode change 100755 => 100644 create mode 100644 src/QueryBuilder/SelectQuery/SelectQuery.php rename src/QueryBuilder/{QueryBuilders/SelectQuery.php => SelectQuery/SelectQueryBuilder.php} (73%) mode change 100755 => 100644 create mode 100644 src/QueryBuilder/SelectQuery/SelectQueryBuilderInterface.php create mode 100644 src/QueryBuilder/SelectQuery/SelectQueryResponse.php diff --git a/src/Attributes/QueryBuilderQuery.php b/src/Attributes/QueryBuilderQuery.php index 5bb9bd6..766b1f3 100644 --- a/src/Attributes/QueryBuilderQuery.php +++ b/src/Attributes/QueryBuilderQuery.php @@ -2,9 +2,9 @@ namespace Darksparrow\DeegraphInteractions\Attributes; -use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\InsertQuery; -use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\PutQuery; -use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\SelectQuery; +use Darksparrow\DeegraphInteractions\QueryBuilder\InsertQuery\InsertQuery; +use Darksparrow\DeegraphInteractions\QueryBuilder\PutQuery\PutQueryBuilder; +use Darksparrow\DeegraphInteractions\QueryBuilder\SelectQuery\SelectQueryBuilder; use PhpParser\Node\Attribute; use PhpParser\Node\Name; use ReflectionClass; @@ -17,7 +17,7 @@ use ReflectionClass; } public function ValidateValues( - InsertQuery|PutQuery|SelectQuery $target + InsertQuery|PutQueryBuilder|SelectQueryBuilder $target ): void { $nameBase = "Darksparrow\\DeegraphPHP\\Attributes"; diff --git a/src/Core/DeegraphServer.php b/src/Core/DeegraphServer.php index cb75549..de67d25 100644 --- a/src/Core/DeegraphServer.php +++ b/src/Core/DeegraphServer.php @@ -2,11 +2,7 @@ namespace Darksparrow\DeegraphInteractions\Core; -use Darksparrow\DeegraphInteractions\DataStructures\QueryResponseWrapper; 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 { @@ -43,7 +39,7 @@ class DeegraphServer 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}"); curl_setopt($ch, CURLOPT_HTTPHEADER, [ @@ -80,18 +76,4 @@ class DeegraphServer 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); - } } diff --git a/src/DataStructures/QueryResponseRow.php b/src/DataStructures/QueryResponseRow.php index 80c3e8b..5cee746 100644 --- a/src/DataStructures/QueryResponseRow.php +++ b/src/DataStructures/QueryResponseRow.php @@ -4,20 +4,22 @@ namespace Darksparrow\DeegraphInteractions\DataStructures; class QueryResponseRow { - public string $Search; - public array $Results; + public ?string $Search; + public ?array $Results; - public static function FromArray(array $array): QueryResponseRow + public function __construct(array $array) { - $builder = new QueryResponseRow(); - - $builder->Search = key(array: $array); - foreach($array["{$builder->Search}"] as $key=>$value) + if($array == []) { - $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 diff --git a/src/QueryBuilder/InsertQuery/InsertQuery.php b/src/QueryBuilder/InsertQuery/InsertQuery.php new file mode 100644 index 0000000..1f29b65 --- /dev/null +++ b/src/QueryBuilder/InsertQuery/InsertQuery.php @@ -0,0 +1,26 @@ +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"; + + } +} diff --git a/src/QueryBuilder/QueryBuilders/InsertQuery.php b/src/QueryBuilder/InsertQuery/InsertQueryBuilder.php old mode 100755 new mode 100644 similarity index 59% rename from src/QueryBuilder/QueryBuilders/InsertQuery.php rename to src/QueryBuilder/InsertQuery/InsertQueryBuilder.php index 96bda5c..eeec9d0 --- a/src/QueryBuilder/QueryBuilders/InsertQuery.php +++ b/src/QueryBuilder/InsertQuery/InsertQueryBuilder.php @@ -1,84 +1,89 @@ -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 - { - self::ValidateDeegraphPath(target: $relativePath); - $this->RelativePath = $relativePath; - return $this; - } - - public function Keys(string $keys): InsertQuery - { - $this->Keys = $keys; - return $this; - } - - public function Schemas(string $schemas): InsertQuery - { - $this->Schemas = $schemas; - return $this; - } - - public function Values(string $values): InsertQuery - { - $this->Values .= $values; - return $this; - } - - public function Duplicate(): InsertQuery - { - $this->Duplicate = true; - return $this; - } - - public function Replace(): InsertQuery - { - $this->Replace = true; - return $this; - } +RelativePath = $relativePath; + return $this; + } + + public function Keys(string $keys): InsertQueryBuilder + { + $this->Keys = $keys; + return $this; + } + + public function Schemas(string $schemas): InsertQueryBuilder + { + $this->Schemas = $schemas; + return $this; + } + + public function Values(string $values): InsertQueryBuilder + { + $this->Values .= $values; + return $this; + } + + public function Duplicate(): InsertQueryBuilder + { + $this->Duplicate = true; + return $this; + } + + public function Replace(): InsertQueryBuilder + { + $this->Replace = true; + 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; + } } \ No newline at end of file diff --git a/src/QueryBuilder/InsertQuery/InsertQueryBuilderInterface.php b/src/QueryBuilder/InsertQuery/InsertQueryBuilderInterface.php new file mode 100644 index 0000000..51364d1 --- /dev/null +++ b/src/QueryBuilder/InsertQuery/InsertQueryBuilderInterface.php @@ -0,0 +1,8 @@ +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); + } +} diff --git a/src/QueryBuilder/PermissionsQuery/PermissionQueryBuilder.php b/src/QueryBuilder/PermissionsQuery/PermissionQueryBuilder.php new file mode 100644 index 0000000..61e1764 --- /dev/null +++ b/src/QueryBuilder/PermissionsQuery/PermissionQueryBuilder.php @@ -0,0 +1,44 @@ +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); + } +} diff --git a/src/QueryBuilder/PermissionsQuery/PermissionQueryBuilderInterface.php b/src/QueryBuilder/PermissionsQuery/PermissionQueryBuilderInterface.php new file mode 100644 index 0000000..1f0b72c --- /dev/null +++ b/src/QueryBuilder/PermissionsQuery/PermissionQueryBuilderInterface.php @@ -0,0 +1,8 @@ +CanAct = true; + break; + case "DELETE": + $this->CanDelete = true; + break; + case "READ": + $this->CanRead = true; + break; + case "WRITE": + $this->CanWrite = true; + break; + } + } + } +} \ No newline at end of file diff --git a/src/QueryBuilder/QueryBuilders/PutQuery.php b/src/QueryBuilder/PutQuery/PutQueryBuilder.php old mode 100755 new mode 100644 similarity index 86% rename from src/QueryBuilder/QueryBuilders/PutQuery.php rename to src/QueryBuilder/PutQuery/PutQueryBuilder.php index eaf7dc7..9acfea8 --- a/src/QueryBuilder/QueryBuilders/PutQuery.php +++ b/src/QueryBuilder/PutQuery/PutQueryBuilder.php @@ -1,113 +1,113 @@ -PutWhat != "") $builder .= " $this->PutWhat"; - - if($this->PutAt != "") $builder .= " $this->PutAt"; - elseif ($this->PutInto != "") $builder .= " $this->PutInto"; - else throw new QueryBuilderRequiredFieldIsNotSetException(); - - if($this->Safe) $builder .= " SAFE"; - return $builder; - } - - - /** - * @throws QueryBuilderInvalidInputException - */ - public function Schema(string $uri): PutQuery - { - $this->PutWhat = self::RegexValidate( - target: "SCHEMA \"$uri\"", - pattern: "/SCHEMA \".+\"/" - ); - return $this; - } - - /** - * @throws QueryBuilderInvalidInputException - */ - public function URI(string $uri): PutQuery - { - $this->PutWhat = self::RegexValidate( - target: "URI \"$uri\"", - pattern: "/URI \".+\"/" - ); - return $this; - } - - /** - * @throws QueryBuilderInvalidInputException - */ - public function DataURI(string $mimeType, string $data): PutQuery - { - $this->PutWhat = self::RegexValidate( - target: "URI \"data:$mimeType;$data\"", - pattern: "/URI \"data:[a-zA-Z0-9]/[a-zA-Z0-9];.+\"/" - ); - return $this; - } - - - /** - * @throws QueryBuilderInvalidInputException - * @throws QueryBuilderConflictingFieldAlreadyExistsException - */ - public function At(string $node, string $uwu): PutQuery - { - self::EnsureNotSet($this->PutInto); - $this->PutAt = self::RegexValidate( - target: 'AT {' . $node . '}/' . $uwu, - pattern: "/AT {[a-zA-Z0-9\-]+}\/[a-zA-Z0-9]+/" - ); - return $this; - } - - - /** - * @throws QueryBuilderInvalidInputException - * @throws QueryBuilderConflictingFieldAlreadyExistsException - */ - public function Into(string $relativePath, string $propertyName): PutQuery - { - self::EnsureNotSet($this->PutAt); - $this->PutInto = self::RegexValidate( - target: "INTO \"$relativePath\" AS \"$propertyName\"", - pattern: "/INTO \"[a-zA-Z0-9\-]+\" AS \"[a-zA-Z0-9]+\"/" - ); - return $this; - } - - - public function Safe(): PutQuery - { - $this->Safe = true; - return $this; - } -} +PutWhat != "") $builder .= " $this->PutWhat"; + + if($this->PutAt != "") $builder .= " $this->PutAt"; + elseif ($this->PutInto != "") $builder .= " $this->PutInto"; + else throw new QueryBuilderRequiredFieldIsNotSetException(); + + if($this->Safe) $builder .= " SAFE"; + return $builder; + } + + + /** + * @throws QueryBuilderInvalidInputException + */ + public function Schema(string $uri): PutQueryBuilder + { + $this->PutWhat = self::RegexValidate( + target: "SCHEMA \"$uri\"", + pattern: "/SCHEMA \".+\"/" + ); + return $this; + } + + /** + * @throws QueryBuilderInvalidInputException + */ + public function URI(string $uri): PutQueryBuilder + { + $this->PutWhat = self::RegexValidate( + target: "URI \"$uri\"", + pattern: "/URI \".+\"/" + ); + return $this; + } + + /** + * @throws QueryBuilderInvalidInputException + */ + public function DataURI(string $mimeType, string $data): PutQueryBuilder + { + $this->PutWhat = self::RegexValidate( + target: "URI \"data:$mimeType;$data\"", + pattern: "/URI \"data:[a-zA-Z0-9]/[a-zA-Z0-9];.+\"/" + ); + return $this; + } + + + /** + * @throws QueryBuilderInvalidInputException + * @throws QueryBuilderConflictingFieldAlreadyExistsException + */ + public function At(string $node, string $uwu): PutQueryBuilder + { + self::EnsureNotSet($this->PutInto); + $this->PutAt = self::RegexValidate( + target: 'AT {' . $node . '}/' . $uwu, + pattern: "/AT {[a-zA-Z0-9\-]+}\/[a-zA-Z0-9]+/" + ); + return $this; + } + + + /** + * @throws QueryBuilderInvalidInputException + * @throws QueryBuilderConflictingFieldAlreadyExistsException + */ + public function Into(string $relativePath, string $propertyName): PutQueryBuilder + { + self::EnsureNotSet($this->PutAt); + $this->PutInto = self::RegexValidate( + target: "INTO \"$relativePath\" AS \"$propertyName\"", + pattern: "/INTO \"[a-zA-Z0-9\-]+\" AS \"[a-zA-Z0-9]+\"/" + ); + return $this; + } + + + public function Safe(): PutQueryBuilder + { + $this->Safe = true; + return $this; + } +} diff --git a/src/QueryBuilder/QueryBuilder.php b/src/QueryBuilder/QueryBuilder.php index 07828e3..378618a 100755 --- a/src/QueryBuilder/QueryBuilder.php +++ b/src/QueryBuilder/QueryBuilder.php @@ -2,9 +2,10 @@ namespace Darksparrow\DeegraphInteractions\QueryBuilder; -use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\InsertQuery; -use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\PutQuery; -use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilders\SelectQuery; +use Darksparrow\DeegraphInteractions\QueryBuilder\InsertQuery\InsertQuery; +use Darksparrow\DeegraphInteractions\QueryBuilder\PermissionsQuery\PermissionQueryBuilder; +use Darksparrow\DeegraphInteractions\QueryBuilder\PutQuery\PutQueryBuilder; +use Darksparrow\DeegraphInteractions\QueryBuilder\SelectQuery\SelectQueryBuilder; final class QueryBuilder { @@ -12,12 +13,16 @@ final class QueryBuilder { 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(); } } diff --git a/src/QueryBuilder/SelectQuery/SelectQuery.php b/src/QueryBuilder/SelectQuery/SelectQuery.php new file mode 100644 index 0000000..b8803a4 --- /dev/null +++ b/src/QueryBuilder/SelectQuery/SelectQuery.php @@ -0,0 +1,26 @@ +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; + } +} diff --git a/src/QueryBuilder/QueryBuilders/SelectQuery.php b/src/QueryBuilder/SelectQuery/SelectQueryBuilder.php old mode 100755 new mode 100644 similarity index 73% rename from src/QueryBuilder/QueryBuilders/SelectQuery.php rename to src/QueryBuilder/SelectQuery/SelectQueryBuilder.php index e076544..3eb19ad --- a/src/QueryBuilder/QueryBuilders/SelectQuery.php +++ b/src/QueryBuilder/SelectQuery/SelectQueryBuilder.php @@ -1,66 +1,62 @@ -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]; - return $this; - } - public function RelativePaths(array $relativePaths): SelectQuery - { - $this->RelativePaths = $relativePaths; - return $this; - } - public function From(string $target): SelectQuery - { - $this->From = "" . $target; - return $this; - } - public function Where(string $target, DeegraphEqualityOperator $operator, string $value): SelectQuery - { - $this->Where = "" . $target . " " . $operator->value . " " . $value; - return $this; - } - public function InstanceOf(string $schema): SelectQuery - { - $this->InstanceOf = "" . $schema; - return $this; - } +RelativePaths = [$relativePath]; + return $this; + } + public function RelativePaths(array $relativePaths): SelectQueryBuilder + { + $this->RelativePaths = $relativePaths; + return $this; + } + public function From(string $target): SelectQueryBuilder + { + $this->From = "" . $target; + return $this; + } + public function Where(string $target, DeegraphEqualityOperator $operator, string $value): SelectQueryBuilder + { + $this->Where = "" . $target . " " . $operator->value . " " . $value; + return $this; + } + public function InstanceOf(string $schema): SelectQueryBuilder + { + $this->InstanceOf = "" . $schema; + 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); + } } \ No newline at end of file diff --git a/src/QueryBuilder/SelectQuery/SelectQueryBuilderInterface.php b/src/QueryBuilder/SelectQuery/SelectQueryBuilderInterface.php new file mode 100644 index 0000000..42d676d --- /dev/null +++ b/src/QueryBuilder/SelectQuery/SelectQueryBuilderInterface.php @@ -0,0 +1,8 @@ +Rows[] = new QueryResponseRow(array: $row); + $this->RowFormat = $response["@row_format"]; + } +} \ No newline at end of file