Skip to content

Commit 4df0577

Browse files
authored
Merge pull request #607 from utopia-php/feat-sequence-int
Sequence func as int
2 parents d256f23 + 0c92bfc commit 4df0577

File tree

10 files changed

+24
-24
lines changed

10 files changed

+24
-24
lines changed

src/Database/Adapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ abstract public function deleteDocument(string $collection, string $id): bool;
747747
* Delete Documents
748748
*
749749
* @param string $collection
750-
* @param array<string> $sequences
750+
* @param array<int> $sequences
751751
* @param array<string> $permissionIds
752752
*
753753
* @return int

src/Database/Adapter/MariaDB.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ public function createDocument(string $collection, Document $document): Document
905905

906906
$stmt->execute();
907907

908-
$document['$sequence'] = $this->pdo->lastInsertId();
908+
$document['$sequence'] = (int) $this->pdo->lastInsertId();
909909

910910
if (empty($document['$sequence'])) {
911911
throw new DatabaseException('Error creating document empty "$sequence"');
@@ -1656,7 +1656,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
16561656
unset($results[$index]['_uid']);
16571657
}
16581658
if (\array_key_exists('_id', $document)) {
1659-
$results[$index]['$sequence'] = $document['_id'];
1659+
$results[$index]['$sequence'] = (int) $document['_id'];
16601660
unset($results[$index]['_id']);
16611661
}
16621662
if (\array_key_exists('_tenant', $document)) {

src/Database/Adapter/Postgres.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,7 @@ public function createDocument(string $collection, Document $document): Document
10281028

10291029
try {
10301030
$this->execute($stmt);
1031-
$lastInsertedId = $this->getPDO()->lastInsertId();
1031+
$lastInsertedId = (int) $this->getPDO()->lastInsertId();
10321032
// Sequence can be manually set as well
10331033
$document['$sequence'] ??= $lastInsertedId;
10341034

@@ -1541,7 +1541,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
15411541
unset($results[$index]['_uid']);
15421542
}
15431543
if (\array_key_exists('_id', $document)) {
1544-
$results[$index]['$sequence'] = $document['_id'];
1544+
$results[$index]['$sequence'] = (int) $document['_id'];
15451545
unset($results[$index]['_id']);
15461546
}
15471547
if (\array_key_exists('_tenant', $document)) {

src/Database/Adapter/SQL.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ public function getDocument(string $collection, string $id, array $queries = [],
369369
$document = $document[0];
370370

371371
if (\array_key_exists('_id', $document)) {
372-
$document['$sequence'] = $document['_id'];
372+
$document['$sequence'] = (int) $document['_id'];
373373
unset($document['_id']);
374374
}
375375
if (\array_key_exists('_uid', $document)) {
@@ -460,7 +460,7 @@ public function updateDocuments(string $collection, Document $updates, array $do
460460
}
461461

462462
foreach ($sequences as $id => $value) {
463-
$stmt->bindValue(":_id_{$id}", $value);
463+
$stmt->bindValue(":_id_{$id}", $value, \PDO::PARAM_INT);
464464
}
465465

466466
$attributeIndex = 0;
@@ -642,7 +642,7 @@ public function updateDocuments(string $collection, Document $updates, array $do
642642
* Delete Documents
643643
*
644644
* @param string $collection
645-
* @param array<string> $sequences
645+
* @param array<int> $sequences
646646
* @param array<string> $permissionIds
647647
*
648648
* @return int
@@ -1892,7 +1892,7 @@ public function createDocuments(string $collection, array $documents): array
18921892

18931893
foreach ($documents as $document) {
18941894
if (isset($sequences[$document->getId()])) {
1895-
$document['$sequence'] = $sequences[$document->getId()];
1895+
$document['$sequence'] = (int) $sequences[$document->getId()];
18961896
}
18971897
}
18981898
} catch (PDOException $e) {

src/Database/Adapter/SQLite.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ public function createDocument(string $collection, Document $document): Document
609609
$statment->execute();
610610
$last = $statment->fetch();
611611

612-
$document['$sequence'] = $last['id'];
612+
$document['$sequence'] = (int) $last['id'];
613613

614614
if (isset($stmtPermissions)) {
615615
$stmtPermissions->execute();

src/Database/Database.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ class Database
165165
],
166166
[
167167
'$id' => '$sequence',
168-
'type' => self::VAR_STRING,
169-
'size' => Database::LENGTH_KEY,
168+
'type' => self::VAR_INTEGER,
169+
'size' => 8,
170170
'required' => true,
171171
'signed' => true,
172172
'array' => false,

src/Database/Document.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ public function getId(): string
6262
}
6363

6464
/**
65-
* @return string
65+
* @return int
6666
*/
67-
public function getSequence(): string
67+
public function getSequence(): int
6868
{
69-
return $this->getAttribute('$sequence', '');
69+
return $this->getAttribute('$sequence', 0);
7070
}
7171

7272
/**

src/Database/Validator/Queries/Documents.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct(
3838
$attributes[] = new Document([
3939
'$id' => '$sequence',
4040
'key' => '$sequence',
41-
'type' => Database::VAR_STRING,
41+
'type' => Database::VAR_INTEGER,
4242
'array' => false,
4343
]);
4444
$attributes[] = new Document([

src/Database/Validator/Structure.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class Structure extends Validator
3232
],
3333
[
3434
'$id' => '$sequence',
35-
'type' => Database::VAR_STRING,
36-
'size' => 255,
35+
'type' => Database::VAR_INTEGER,
36+
'size' => 8,
3737
'required' => false,
3838
'signed' => true,
3939
'array' => false,

tests/e2e/Adapter/Scopes/DocumentTests.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function testCreateDocument(): Document
9393
// Test create document with manual internal id
9494
$manualIdDocument = $database->createDocument('documents', new Document([
9595
'$id' => '56000',
96-
'$sequence' => '56000',
96+
'$sequence' => 56000,
9797
'$permissions' => [
9898
Permission::read(Role::any()),
9999
Permission::read(Role::user(ID::custom('1'))),
@@ -292,7 +292,7 @@ public function testCreateDocumentsWithAutoIncrement(): void
292292

293293
for ($i = $sequence; $i <= ($sequence + $count); $i++) {
294294
$documents[] = new Document([
295-
'$sequence' => (string)$i,
295+
'$sequence' => $i,
296296
'$permissions' => [
297297
Permission::read(Role::any()),
298298
Permission::create(Role::any()),
@@ -363,7 +363,7 @@ public function testCreateDocumentsWithDifferentAttributes(): void
363363
$documents = [
364364
new Document([
365365
'$id' => 'third',
366-
'$sequence' => 'third',
366+
'$sequence' => 3,
367367
'string' => 'text📝',
368368
]),
369369
new Document([
@@ -1765,9 +1765,9 @@ public function testFindByID(): void
17651765
* @depends testFind
17661766
* @param array<string, mixed> $data
17671767
* @return void
1768-
* @throws \Utopia\Database\Exception
1768+
* @throws DatabaseException
17691769
*/
1770-
public function testFindByInternalID(array $data): void
1770+
public function testFindBySequence(array $data): void
17711771
{
17721772
/** @var Database $database */
17731773
$database = static::getDatabase();
@@ -4228,7 +4228,7 @@ public function testExceptionCaseInsensitiveDuplicate(Document $document): Docum
42284228
$database = static::getDatabase();
42294229

42304230
$document->setAttribute('$id', 'caseSensitive');
4231-
$document->setAttribute('$sequence', '200');
4231+
$document->setAttribute('$sequence', 200);
42324232
$database->createDocument($document->getCollection(), $document);
42334233

42344234
$document->setAttribute('$id', 'CaseSensitive');

0 commit comments

Comments
 (0)