Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
feat(HttpTransaction): Implement HTTP transaction handling
Browse files Browse the repository at this point in the history
  • Loading branch information
darkterminal committed Apr 27, 2024
1 parent 9a55284 commit a9ae167
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/Types/HttpTransaction.php
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');
}
}

0 comments on commit a9ae167

Please sign in to comment.