Browse Source

more work on the SELECT query

dev
Cerys 4 weeks ago
parent
commit
133375cfc2
  1. 8
      src/QueryBuilder/SelectQuery/SelectQuery.php
  2. 6
      src/QueryBuilder/SelectQuery/SelectQueryBuilder.php
  3. 2
      src/QueryBuilder/SelectQuery/SelectQueryResponse.php
  4. 34
      tests/QueryBuilderSelectTest.php

8
src/QueryBuilder/SelectQuery/SelectQuery.php

@ -12,8 +12,12 @@ class SelectQuery
{ {
$this->QueryString = $queryString; $this->QueryString = $queryString;
} }
public function __toString()
{
return $this->QueryString;
}
public function RunQuery(DeegraphServer $server): array public function RunQuery(DeegraphServer $server): SelectQueryResponse
{ {
$response = $server->RunRawRequest( $response = $server->RunRawRequest(
endpoint: "/api/v1/@query", endpoint: "/api/v1/@query",
@ -21,6 +25,6 @@ class SelectQuery
body: $this->QueryString body: $this->QueryString
); );
$temp = json_decode($response, true); $temp = json_decode($response, true);
return $temp; return new SelectQueryResponse($temp);
} }
} }

6
src/QueryBuilder/SelectQuery/SelectQueryBuilder.php

@ -23,9 +23,9 @@ final class SelectQueryBuilder implements SelectQueryBuilderInterface
{ {
} }
public function RelativePath(string $relativePath): SelectQueryBuilder public function AddRelativePath(string $relativePath): SelectQueryBuilder
{ {
$this->RelativePaths = [$relativePath]; $this->RelativePaths[] = $relativePath;
return $this; return $this;
} }
public function RelativePaths(array $relativePaths): SelectQueryBuilder public function RelativePaths(array $relativePaths): SelectQueryBuilder
@ -51,7 +51,7 @@ final class SelectQueryBuilder implements SelectQueryBuilderInterface
public function Build(): SelectQuery 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);
if($this->From != "") $builder .= " FROM $this->From"; if($this->From != "") $builder .= " FROM $this->From";
if($this->Where != "") $builder .= " WHERE $this->Where"; if($this->Where != "") $builder .= " WHERE $this->Where";

2
src/QueryBuilder/SelectQuery/SelectQueryResponse.php

@ -4,7 +4,7 @@ namespace Darksparrow\DeegraphInteractions\QueryBuilder\SelectQuery;
use Darksparrow\DeegraphInteractions\DataStructures\QueryResponseRow; use Darksparrow\DeegraphInteractions\DataStructures\QueryResponseRow;
class SelectQueryResponse final class SelectQueryResponse
{ {
public array $Rows; public array $Rows;
public string $RowFormat; public string $RowFormat;

34
tests/QueryBuilderSelectTest.php

@ -0,0 +1,34 @@
<?php
use Darksparrow\DeegraphInteractions\Exceptions\QueryBuilderConflictingFieldAlreadyExistsException;
use Darksparrow\DeegraphInteractions\QueryBuilder\QueryBuilder;
use PHPUnit\Framework\TestCase;
final class QueryBuilderSelectTest extends TestCase
{
private string $TestUUID = "{00000000-0000-0000-0000-000000000000}";
public function test0()
{
self::assertEquals(
expected: "SELECT @path FROM ./cases/#",
actual: QueryBuilder::Select()
->RelativePaths(["@path"])
->From("./cases/#")
->Build()
);
}
public function test1()
{
self::assertEquals(
expected: "SELECT clients/0/name FROM {0b368d41-7c15-42c8-899c-2b178ae9d983}/cases/0",
actual: QueryBuilder::Select()
->AddRelativePath("clients/0/name")
->From("{0b368d41-7c15-42c8-899c-2b178ae9d983}/cases/0")
->Build()
);
}
}
Loading…
Cancel
Save