Skip to content

Commit df46b84

Browse files
authored
Merge pull request #4 from litebase/send-queries-with-uuid
Replace uniqid with uuid for query IDs
2 parents 9fb9a80 + 659e366 commit df46b84

3 files changed

Lines changed: 10 additions & 7 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"ext-curl": "*",
3636
"ext-json": "*",
3737
"guzzlehttp/guzzle": "^7.9",
38-
"guzzlehttp/psr7": "^1.7 || ^2.0"
38+
"guzzlehttp/psr7": "^1.7 || ^2.0",
39+
"ramsey/uuid": "^4.9"
3940
},
4041
"require-dev": {
4142
"friendsofphp/php-cs-fixer": "^3.5",

src/LitebaseClient.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Exception;
66
use GuzzleHttp\Client;
77
use Litebase\OpenAPI\Model\StatementParameter;
8+
use Ramsey\Uuid\Uuid;
89
use Throwable;
910

1011
class LitebaseClient
@@ -61,7 +62,7 @@ public function beginTransaction(): bool
6162
try {
6263
$response = $this->transport->send(
6364
new Query(
64-
id: uniqid(),
65+
id: Uuid::uuid4()->toString(),
6566
statement: 'BEGIN',
6667
)
6768
);
@@ -100,7 +101,7 @@ public function commit(): bool
100101
try {
101102
$this->transport->send(
102103
new Query(
103-
id: uniqid(),
104+
id: Uuid::uuid4()->toString(),
104105
transactionId: $this->transaction->id,
105106
statement: 'COMMIT',
106107
)
@@ -141,7 +142,7 @@ public function errorInfo(): array
141142
public function exec(array $input): ?QueryResult
142143
{
143144
// Set a unique id for the request.
144-
$input['id'] = uniqid();
145+
$input['id'] = Uuid::uuid4()->toString();
145146

146147
if ($this->transaction) {
147148
$input['transaction_id'] = $this->transaction->id;
@@ -198,7 +199,7 @@ public function rollback(): bool
198199

199200
$this->transport->send(
200201
new Query(
201-
id: uniqid(),
202+
id: Uuid::uuid4()->toString(),
202203
transactionId: $this->transaction->id,
203204
statement: 'ROLLBACK',
204205
)
@@ -219,7 +220,7 @@ public function withTransport(string $transportType): LitebaseClient
219220
$this->transport = new HttpStreamingTransport($this->configuration);
220221
break;
221222
default:
222-
throw new Exception('Invalid transport type: '.$transportType);
223+
throw new Exception('Invalid transport type: ' . $transportType);
223224
}
224225

225226
return $this;

tests/Unit/HttpTransportTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Litebase\HttpTransport;
1212
use Litebase\Query;
1313
use Litebase\QueryResult;
14+
use Ramsey\Uuid\Uuid;
1415

1516
describe('HttpTransport', function () {
1617
$mock = new MockHandler;
@@ -65,7 +66,7 @@
6566
$transport->setClient(new ApiClient($configuration, $httpClient));
6667

6768
$query = new Query(
68-
id: 'test-query',
69+
id: Uuid::uuid4()->toString(),
6970
statement: 'SELECT 1',
7071
parameters: [],
7172
);

0 commit comments

Comments
 (0)