Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds internal id support for create document #318

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,12 +496,23 @@ public function createDocument(string $collection, Document $document): Document
$bindIndex++;
}

// Insert manual id if set
if (!empty($document->getInternalId())) {
$bindKey = '_id';
$columns .= " _id" . ' = :' . $bindKey . ',';
}

$stmt = $this->getPDO()
->prepare("INSERT INTO {$this->getSQLTable($name)}
SET {$columns} _uid = :_uid");

$stmt->bindValue(':_uid', $document->getId(), PDO::PARAM_STR);

// Bind manual internal id if set
if (!empty($document->getInternalId())) {
$stmt->bindValue(':_id', $document->getInternalId(), PDO::PARAM_STR);
}

$attributeIndex = 0;
foreach ($attributes as $attribute => $value) {
if (is_array($value)) { // arrays & objects should be saved as strings
Expand Down Expand Up @@ -531,11 +542,7 @@ public function createDocument(string $collection, Document $document): Document
try {
$stmt->execute();

$statment = $this->getPDO()->prepare("select last_insert_id() as id");
$statment->execute();
$last = $statment->fetch();
$document['$internalId'] = $last['id'];

$document['$internalId'] = $this->getDocument($collection, $document->getId())->getInternalId();
if (isset($stmtPermissions)) {
$stmtPermissions->execute();
}
Expand Down
5 changes: 5 additions & 0 deletions src/Database/Adapter/Mongo/MongoDBAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,11 @@ public function createDocument(string $collection, Document $document): Document

$record = $this->replaceChars('$', '_', $document->getArrayCopy());

// Insert manual id if set
if (!empty($internalId)) {
$record['_id'] = $internalId;
}

$this->client->insert($name, $record);

return $document;
Expand Down
11 changes: 11 additions & 0 deletions src/Database/Adapter/SQLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,23 @@ public function createDocument(string $collection, Document $document): Document
$bindIndex++;
}

// Insert manual id if set
if (!empty($document->getInternalId())) {
$values[] = '_id';
$columns[] = "_id";
}

$stmt = $this->getPDO()
->prepare("INSERT INTO `{$this->getNamespace()}_{$name}`
(".implode(', ', $columns).") VALUES (:".implode(', :', $values).");");

$stmt->bindValue(':_uid', $document->getId(), PDO::PARAM_STR);

// Bind manual internal id if set
if (!empty($document->getInternalId())) {
$stmt->bindValue(':_id', $document->getInternalId(), PDO::PARAM_STR);
}

$attributeIndex = 0;
foreach ($attributes as $attribute => $value) {
if (is_array($value)) { // arrays & objects should be saved as strings
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Validator/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Structure extends Validator
[
'$id' => '$internalId',
'type' => Database::VAR_STRING,
'size' => 64,
'size' => 0,
'required' => false,
'signed' => true,
'array' => false,
Expand Down
45 changes: 45 additions & 0 deletions tests/Database/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,50 @@ public function testCreateDocument()
$this->assertEquals([], $document->getAttribute('empty'));
$this->assertEquals('Works', $document->getAttribute('with-dash'));

// Test create document with manual internal id
$manualIdDocument = static::getDatabase()->createDocument('documents', new Document([
'$internalId' => '56000',
'$permissions' => [
Permission::read(Role::any()),
Permission::read(Role::user(ID::custom('1'))),
Permission::read(Role::user(ID::custom('2'))),
Permission::create(Role::any()),
Permission::create(Role::user(ID::custom('1x'))),
Permission::create(Role::user(ID::custom('2x'))),
Permission::update(Role::any()),
Permission::update(Role::user(ID::custom('1x'))),
Permission::update(Role::user(ID::custom('2x'))),
Permission::delete(Role::any()),
Permission::delete(Role::user(ID::custom('1x'))),
Permission::delete(Role::user(ID::custom('2x'))),
],
'string' => 'text📝',
'integer' => 5,
'bigint' => 8589934592, // 2^33
'float' => 5.55,
'boolean' => true,
'colors' => ['pink', 'green', 'blue'],
'empty' => [],
'with-dash' => 'Works',
]));

$this->assertEquals('56000', $manualIdDocument->getInternalId());
$this->assertNotEmpty(true, $manualIdDocument->getId());
$this->assertIsString($manualIdDocument->getAttribute('string'));
$this->assertEquals('text📝', $manualIdDocument->getAttribute('string')); // Also makes sure an emoji is working
$this->assertIsInt($manualIdDocument->getAttribute('integer'));
$this->assertEquals(5, $manualIdDocument->getAttribute('integer'));
$this->assertIsInt($manualIdDocument->getAttribute('bigint'));
$this->assertEquals(8589934592, $manualIdDocument->getAttribute('bigint'));
$this->assertIsFloat($manualIdDocument->getAttribute('float'));
$this->assertEquals(5.55, $manualIdDocument->getAttribute('float'));
$this->assertIsBool($manualIdDocument->getAttribute('boolean'));
$this->assertEquals(true, $manualIdDocument->getAttribute('boolean'));
$this->assertIsArray($manualIdDocument->getAttribute('colors'));
$this->assertEquals(['pink', 'green', 'blue'], $manualIdDocument->getAttribute('colors'));
$this->assertEquals([], $manualIdDocument->getAttribute('empty'));
$this->assertEquals('Works', $manualIdDocument->getAttribute('with-dash'));

return $document;
}

Expand Down Expand Up @@ -2636,6 +2680,7 @@ public function testExceptionDuplicate(Document $document)
public function testExceptionCaseInsensitiveDuplicate(Document $document)
{
$document->setAttribute('$id', 'caseSensitive');
$document->setAttribute('$internalId', '200');
static::getDatabase()->createDocument($document->getCollection(), $document);

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