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

Feat: support BIGINT/LONG type #95

Merged
merged 2 commits into from
Dec 29, 2021
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
12 changes: 11 additions & 1 deletion src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -910,8 +910,13 @@ public function getAttributeWidth(Document $collection): int
break;

case Database::VAR_INTEGER:
if ($attribute['size'] >= 8) {
$total += 8; // BIGINT takes 8 bytes
} else {
$total += 4; // INT takes 4 bytes
}
break;
case Database::VAR_FLOAT:
// INT takes 4 bytes
// FLOAT(p) takes 4 bytes when p <= 24, 8 otherwise
$total += 4;
break;
Expand Down Expand Up @@ -995,6 +1000,11 @@ protected function getSQLType(string $type, int $size, bool $signed = true): str

case Database::VAR_INTEGER: // We don't support zerofill: https://stackoverflow.com/a/5634147/2299554
$signed = ($signed) ? '' : ' UNSIGNED';

if($size >= 8) { // INT = 4 bytes, BIGINT = 8 bytes
return 'BIGINT'.$signed;
}

return 'INT'.$signed;
break;

Expand Down
18 changes: 15 additions & 3 deletions tests/Database/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ public function testCreateDeleteAttribute()
$this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'string3', Database::VAR_STRING, 65535+1, true));
$this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'string4', Database::VAR_STRING, 16777215+1, true));
$this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'integer', Database::VAR_INTEGER, 0, true));
$this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'bigint', Database::VAR_INTEGER, 8, true));
$this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'float', Database::VAR_FLOAT, 0, true));
$this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'boolean', Database::VAR_BOOLEAN, 0, true));

$collection = static::getDatabase()->getCollection('attributes');
$this->assertCount(7, $collection->getAttribute('attributes'));
$this->assertCount(8, $collection->getAttribute('attributes'));

// Array
$this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'string_list', Database::VAR_STRING, 128, true, null, true, true));
Expand All @@ -95,7 +96,7 @@ public function testCreateDeleteAttribute()
$this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'boolean_list', Database::VAR_BOOLEAN, 0, true, null, true, true));

$collection = static::getDatabase()->getCollection('attributes');
$this->assertCount(11, $collection->getAttribute('attributes'));
$this->assertCount(12, $collection->getAttribute('attributes'));

// Default values
$this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'string_default', Database::VAR_STRING, 256, false, 'test'));
Expand All @@ -104,14 +105,15 @@ public function testCreateDeleteAttribute()
$this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'boolean_default', Database::VAR_BOOLEAN, 0, false, false));

$collection = static::getDatabase()->getCollection('attributes');
$this->assertCount(15, $collection->getAttribute('attributes'));
$this->assertCount(16, $collection->getAttribute('attributes'));

// Delete
$this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'string1'));
$this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'string2'));
$this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'string3'));
$this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'string4'));
$this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'integer'));
$this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'bigint'));
$this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'float'));
$this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'boolean'));

Expand Down Expand Up @@ -344,6 +346,7 @@ public function testCreateDocument()

$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));
$this->assertEquals(true, static::getDatabase()->createAttribute('documents', 'bigint', Database::VAR_INTEGER, 8, true));
$this->assertEquals(true, static::getDatabase()->createAttribute('documents', 'float', Database::VAR_FLOAT, 0, true));
$this->assertEquals(true, static::getDatabase()->createAttribute('documents', 'boolean', Database::VAR_BOOLEAN, 0, true));
$this->assertEquals(true, static::getDatabase()->createAttribute('documents', 'colors', Database::VAR_STRING, 32, true, null, true, true));
Expand All @@ -354,6 +357,7 @@ public function testCreateDocument()
'$write' => ['role:all', 'user1x', 'user2x'],
'string' => 'text📝',
'integer' => 5,
'bigint' => 8589934592, // 2^33
'float' => 5.55,
'boolean' => true,
'colors' => ['pink', 'green', 'blue'],
Expand All @@ -365,6 +369,8 @@ public function testCreateDocument()
$this->assertEquals('text📝', $document->getAttribute('string')); // Also makes sure an emoji is working
$this->assertIsInt($document->getAttribute('integer'));
$this->assertEquals(5, $document->getAttribute('integer'));
$this->assertIsInt($document->getAttribute('bigint'));
$this->assertEquals(8589934592, $document->getAttribute('bigint'));
$this->assertIsFloat($document->getAttribute('float'));
$this->assertEquals(5.55, $document->getAttribute('float'));
$this->assertIsBool($document->getAttribute('boolean'));
Expand Down Expand Up @@ -1278,6 +1284,7 @@ public function testReadPermissionsSuccess(Document $document)
'$write' => ['role:all'],
'string' => 'text📝',
'integer' => 5,
'bigint' => 8589934592, // 2^33
'float' => 5.55,
'boolean' => true,
'colors' => ['pink', 'green', 'blue'],
Expand Down Expand Up @@ -1308,6 +1315,7 @@ public function testReadPermissionsFailure(Document $document)
'$write' => ['user1'],
'string' => 'text📝',
'integer' => 5,
'bigint' => 8589934592, // 2^33
'float' => 5.55,
'boolean' => true,
'colors' => ['pink', 'green', 'blue'],
Expand All @@ -1326,6 +1334,7 @@ public function testWritePermissionsSuccess(Document $document)
'$write' => ['role:all'],
'string' => 'text📝',
'integer' => 5,
'bigint' => 8589934592, // 2^33
'float' => 5.55,
'boolean' => true,
'colors' => ['pink', 'green', 'blue'],
Expand All @@ -1350,6 +1359,7 @@ public function testWritePermissionsFailure(Document $document)
'$write' => ['role:all'],
'string' => 'text📝',
'integer' => 5,
'bigint' => 8589934592, // 2^33
'float' => 5.55,
'boolean' => true,
'colors' => ['pink', 'green', 'blue'],
Expand All @@ -1370,6 +1380,7 @@ public function testWritePermissionsUpdateFailure(Document $document)
'$write' => ['role:all'],
'string' => 'text📝',
'integer' => 5,
'bigint' => 8589934592, // 2^33
'float' => 5.55,
'boolean' => true,
'colors' => ['pink', 'green', 'blue'],
Expand All @@ -1383,6 +1394,7 @@ public function testWritePermissionsUpdateFailure(Document $document)
'$write' => ['role:all'],
'string' => 'text📝',
'integer' => 5,
'bigint' => 8589934592, // 2^33
'float' => 5.55,
'boolean' => true,
'colors' => ['pink', 'green', 'blue'],
Expand Down