diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 3a421214b..088aaf2fc 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -1082,6 +1082,9 @@ protected function getSQLType(string $type, int $size, bool $signed = true): str return "VARCHAR({$size})"; + case Database::VAR_ID: + return 'INT UNSIGNED'; // Same as Primary Key Sqlite does not allow INT(11) + case Database::VAR_INTEGER: // We don't support zerofill: https://stackoverflow.com/a/5634147/2299554 $signed = ($signed) ? '' : ' UNSIGNED'; diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index 64e71fa91..75dd08a2f 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -1064,6 +1064,9 @@ protected function getSQLType(string $type, int $size, bool $signed = true): str return "VARCHAR({$size})"; + case Database::VAR_ID: + return 'INTEGER'; + case Database::VAR_INTEGER: // We don't support zerofill: https://stackoverflow.com/a/5634147/2299554 if ($size >= 8) { // INT = 4 bytes, BIGINT = 8 bytes diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index 05c0de595..3561344ae 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -339,6 +339,11 @@ public function getAttributeWidth(Document $collection): int $total += 4; // INT takes 4 bytes } break; + + case Database::VAR_ID: + $total += 4; + break; + case Database::VAR_FLOAT: // DOUBLE takes 8 bytes $total += 8; @@ -357,6 +362,7 @@ public function getAttributeWidth(Document $collection): int case Database::VAR_DATETIME: $total += 19; // 2022-06-26 14:46:24 break; + default: throw new Exception('Unknown Type'); } diff --git a/src/Database/Database.php b/src/Database/Database.php index 6fad86611..fb57567b9 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -25,6 +25,7 @@ class Database public const VAR_FLOAT = 'double'; public const VAR_BOOLEAN = 'boolean'; public const VAR_DATETIME = 'datetime'; + public const VAR_ID = 'id'; // Relationships Types public const VAR_DOCUMENT = 'document'; @@ -759,6 +760,7 @@ public function createAttribute(string $collection, string $id, string $type, in case self::VAR_FLOAT: case self::VAR_BOOLEAN: case self::VAR_DATETIME: + case self::VAR_ID: break; default: throw new Exception('Unknown attribute type: ' . $type); diff --git a/src/Database/Validator/Structure.php b/src/Database/Validator/Structure.php index 2ce3df8dd..88b5b0f4d 100644 --- a/src/Database/Validator/Structure.php +++ b/src/Database/Validator/Structure.php @@ -253,6 +253,10 @@ public function isValid($document): bool $validator = new Integer(); break; + case Database::VAR_ID: + $validator = new Text(24, min: 0); // Mongo Id length + break; + case Database::VAR_FLOAT: $validator = new FloatValidator(); break; diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 4c0ed7382..c1ed5ab16 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -336,6 +336,15 @@ public function testCreateCollectionWithSchema(): void 'array' => false, 'filters' => [], ]), + new Document([ + '$id' => ID::custom('collection_id'), + 'type' => Database::VAR_ID, + 'size' => 0, + 'required' => true, + 'signed' => false, + 'array' => false, + 'filters' => [], + ]), ]; $indexes = [ @@ -360,6 +369,13 @@ public function testCreateCollectionWithSchema(): void 'lengths' => [], 'orders' => ['DESC', 'ASC'], ]), + new Document([ + '$id' => ID::custom('index4'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['collection_id'], + 'lengths' => [], + 'orders' => ['DESC', 'ASC'], + ]), ]; $collection = static::getDatabase()->createCollection('withSchema', $attributes, $indexes); @@ -368,7 +384,7 @@ public function testCreateCollectionWithSchema(): void $this->assertEquals('withSchema', $collection->getId()); $this->assertIsArray($collection->getAttribute('attributes')); - $this->assertCount(3, $collection->getAttribute('attributes')); + $this->assertCount(4, $collection->getAttribute('attributes')); $this->assertEquals('attribute1', $collection->getAttribute('attributes')[0]['$id']); $this->assertEquals(Database::VAR_STRING, $collection->getAttribute('attributes')[0]['type']); $this->assertEquals('attribute2', $collection->getAttribute('attributes')[1]['$id']); @@ -377,7 +393,7 @@ public function testCreateCollectionWithSchema(): void $this->assertEquals(Database::VAR_BOOLEAN, $collection->getAttribute('attributes')[2]['type']); $this->assertIsArray($collection->getAttribute('indexes')); - $this->assertCount(3, $collection->getAttribute('indexes')); + $this->assertCount(4, $collection->getAttribute('indexes')); $this->assertEquals('index1', $collection->getAttribute('indexes')[0]['$id']); $this->assertEquals(Database::INDEX_KEY, $collection->getAttribute('indexes')[0]['type']); $this->assertEquals('index2', $collection->getAttribute('indexes')[1]['$id']); @@ -385,6 +401,12 @@ public function testCreateCollectionWithSchema(): void $this->assertEquals('index3', $collection->getAttribute('indexes')[2]['$id']); $this->assertEquals(Database::INDEX_KEY, $collection->getAttribute('indexes')[2]['type']); + $doc = static::getDatabase()->createDocument('withSchema', new Document([ + 'collection_id' => $collection->getInternalId() + ])); + + $this->assertIsString($doc->getAttribute('collection_id')); + static::getDatabase()->deleteCollection('withSchema'); // Test collection with dash (+attribute +index) @@ -550,7 +572,7 @@ public function testCreateCollectionValidator(): void public function testCreateDocument(): Document { - static::getDatabase()->createCollection('documents'); + $collection = static::getDatabase()->createCollection('documents'); $this->assertEquals(true, static::getDatabase()->createAttribute('documents', 'string', Database::VAR_STRING, 128, true)); $this->assertEquals(true, static::getDatabase()->createAttribute('documents', 'integer', Database::VAR_INTEGER, 0, true)); @@ -560,6 +582,7 @@ public function testCreateDocument(): Document $this->assertEquals(true, static::getDatabase()->createAttribute('documents', 'colors', Database::VAR_STRING, 32, true, null, true, true)); $this->assertEquals(true, static::getDatabase()->createAttribute('documents', 'empty', Database::VAR_STRING, 32, false, null, true, true)); $this->assertEquals(true, static::getDatabase()->createAttribute('documents', 'with-dash', Database::VAR_STRING, 128, false, null)); + $this->assertEquals(true, static::getDatabase()->createAttribute('documents', 'collection_id', Database::VAR_ID, 0, false)); $document = static::getDatabase()->createDocument('documents', new Document([ '$permissions' => [ @@ -584,6 +607,7 @@ public function testCreateDocument(): Document 'colors' => ['pink', 'green', 'blue'], 'empty' => [], 'with-dash' => 'Works', + 'collection_id' => $collection->getInternalId(), ])); $this->assertNotEmpty(true, $document->getId()); @@ -601,6 +625,7 @@ public function testCreateDocument(): Document $this->assertEquals(['pink', 'green', 'blue'], $document->getAttribute('colors')); $this->assertEquals([], $document->getAttribute('empty')); $this->assertEquals('Works', $document->getAttribute('with-dash')); + $this->assertEquals($collection->getInternalId(), $document->getAttribute('collection_id')); return $document; }