Skip to content

Commit

Permalink
WIP: Update to raw queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Meldiron committed Feb 23, 2024
1 parent 9d44651 commit dacb169
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 20 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
stopOnFailure="true">
<testsuites>
<testsuite name="unit">
<directory>./tests/unit</directory>
Expand Down
15 changes: 12 additions & 3 deletions src/Database/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,22 @@ protected function trigger(string $event, mixed $query): mixed
abstract public function ping(): bool;

/**
* Execute raw command
* @param string $command
* Execute raw command with no response
* @param mixed $query
*
* @return mixed
* @throws \Throwable
*/
abstract public function execute(string $command): mixed;
abstract public function executeWrite(mixed $query): bool;

/**
* Execute raw command that returns a response
* @param mixed $query
*
* @return mixed
* @throws \Throwable
*/
abstract public function executeRead(mixed $query): mixed;

/**
* Create Database
Expand Down
17 changes: 15 additions & 2 deletions src/Database/Adapter/DataAPIMariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,26 @@ public function __construct(string $endpoint, string $secret, string $database)
}

/**
* Execute raw command
* Execute raw command with no response
* @param mixed $query
*
* @return mixed
* @throws \Throwable
*/
public function execute(mixed $query): mixed
public function executeWrite(mixed $query): bool
{
$this->query($this->endpoint, $this->secret, $this->database, $query);
return true;
}

/**
* Execute raw command that returns a response
* @param mixed $query
*
* @return mixed
* @throws \Throwable
*/
public function executeRead(mixed $query): mixed
{
return $this->query($this->endpoint, $this->secret, $this->database, $query);
}
Expand Down
20 changes: 16 additions & 4 deletions src/Database/Adapter/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,27 @@ public function ping(): bool
return $this->getClient()->query(['ping' => 1])->ok ?? false;
}

/**
* Execute raw command with no response
* @param mixed $query
*
* @return mixed
* @throws \Throwable
*/
public function executeWrite(mixed $query): bool
{
// Not needed until we need Mongo Data API adapter
throw new Exception('Not implemented');
}

/**
* Execute raw command
* Execute raw command that returns a response
* @param mixed $query
*
* @return mixed
* @throws Exception
* @throws MongoException
* @throws \Throwable
*/
public function execute(mixed $query): mixed
public function executeRead(mixed $query): mixed
{
// Not needed until we need Mongo Data API adapter
throw new Exception('Not implemented');
Expand Down
37 changes: 27 additions & 10 deletions src/Database/Adapter/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,46 @@ public function __construct(mixed $pdo)
*/
public function ping(): bool
{
$query = $this->getPDO()
->prepare("SELECT 1;")
->queryString;
$stmt = $this->getPDO()->prepare("SELECT 1;");
$query = $stmt->queryString;

return $this->execute($query);
return $this->executeWrite($query);
}

/**
* Execute raw command
* Execute raw command with no response
* @param mixed $query
*
* @return mixed
* @throws Exception
* @throws PDOException
* @throws \Throwable
*/
public function execute(mixed $query): mixed
public function executeWrite(mixed $query): bool
{
return $this->getPDO()
->prepare($query)
->execute();
}

/**
* Execute raw command that returns a response
* @param mixed $query
*
* @return mixed
* @throws \Throwable
*/
public function executeRead(mixed $query): mixed
{
$stmt = $this->getPDO()->prepare($query);
$response = $stmt->fetchAll();

\var_dump($query);
\var_dump($response);

$stmt->closeCursor();

return $response;
}

/**
* Check if Database exists
* Optionally check if collection exists in Database
Expand Down Expand Up @@ -92,8 +110,7 @@ public function exists(string $database, ?string $collection = null): bool

$stmt->execute();

$document = $stmt->fetchAll();
$stmt->closeCursor();
$document = $this->executeRead($stmt->queryString);

if (empty($document)) {
return false;
Expand Down

0 comments on commit dacb169

Please sign in to comment.