This repository has been archived by the owner on Jun 12, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(HttpTransaction): Implement HTTP transaction handling
- Loading branch information
1 parent
9a55284
commit a9ae167
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace Darkterminal\LibSQL\Types; | ||
|
||
use Darkterminal\LibSQL\Providers\HttpClient; | ||
|
||
class HttpTransaction extends HttpClient | ||
{ | ||
protected $baton; | ||
|
||
public function __construct(string $mode) | ||
{ | ||
$request = $this->_createRequest(\LIBSQL_EXECUTE, transactionModeToBegin($mode)); | ||
$response = $this->runQuery($this->_makeRequest($request, false), true); | ||
$data = map_results($response->getBody()); | ||
$this->baton = $data['baton']; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Add a transaction to the transaction batch. | ||
* | ||
* @param HttpStatement $query The HTTP statement to add to the transaction. | ||
* | ||
* @return self The current instance of HttpClient. | ||
*/ | ||
public function addTransaction(HttpStatement $query): self | ||
{ | ||
\array_push($this->collectors, $this->_createRequest(\LIBSQL_EXECUTE, $query->sql, $query->args, $query->named_args)); | ||
return $this; | ||
} | ||
|
||
/** | ||
* End the current transaction batch and commit the transactions. | ||
* | ||
* @return HttpResponse The HTTP response containing the results of the transaction batch. | ||
*/ | ||
public function endTransaction(): HttpResponse | ||
{ | ||
\array_push($this->collectors, $this->_rawCommit()); | ||
$response = $this->runQuery($this->_makeRequest($this->collectors, true, $this->baton), true); | ||
$data = map_results($response->getBody()); | ||
return HttpResponse::create($data['baton'], $data['base_url'], $data['results']); | ||
} | ||
|
||
/** | ||
* Rollback the current transaction. | ||
*/ | ||
public function rollback(): void | ||
{ | ||
$this->runQuery($this->_makeRequest($this->_createRequest(\LIBSQL_EXECUTE, 'ROLLBACK'), true, $this->baton)); | ||
} | ||
|
||
protected function _rawRollback(): array | ||
{ | ||
return $this->_createRequest(\LIBSQL_EXECUTE, 'ROLLBACK'); | ||
} | ||
|
||
/** | ||
* Commit the current transaction. | ||
*/ | ||
public function commit(): void | ||
{ | ||
$this->runQuery($this->_makeRequest($this->_createRequest(\LIBSQL_EXECUTE, 'COMMIT'), true, $this->baton)); | ||
} | ||
|
||
protected function _rawCommit(): array | ||
{ | ||
return $this->_createRequest(\LIBSQL_EXECUTE, 'COMMIT'); | ||
} | ||
} |