diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index d3614fc9e..6e7cc9f9e 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -1034,7 +1034,7 @@ abstract public function getInternalIndexesKeys(): array; * Get Schema Attributes * * @param string $collection - * @return array + * @return array * @throws DatabaseException */ abstract public function getSchemaAttributes(string $collection): array; diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 25bfbdcce..b5d2956f1 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -2439,7 +2439,7 @@ protected function processException(PDOException $e): \Exception * Get Schema Attributes * * @param string $collection - * @return array + * @return array * @throws DatabaseException */ public function getSchemaAttributes(string $collection): array @@ -2448,28 +2448,32 @@ public function getSchemaAttributes(string $collection): array $collection = $this->getNamespace().'_'.$this->filter($collection); try { - $stmt = $this->getPDO()->prepare(" - SELECT - COLUMN_NAME, - COLUMN_DEFAULT, - IS_NULLABLE, - DATA_TYPE, - CHARACTER_MAXIMUM_LENGTH, - NUMERIC_PRECISION, - NUMERIC_SCALE, - DATETIME_PRECISION, - COLUMN_TYPE, - COLUMN_KEY, - EXTRA - FROM INFORMATION_SCHEMA.COLUMNS + $stmt = $this->getPDO()->prepare(' + SELECT + COLUMN_NAME as columnName, + COLUMN_DEFAULT as columnDefault, + IS_NULLABLE as isNullable, + DATA_TYPE as dataType, + CHARACTER_MAXIMUM_LENGTH as characterMaximumLength, + NUMERIC_PRECISION as numericPrecision, + NUMERIC_SCALE as numericScale, + DATETIME_PRECISION as datetimePrecision, + COLUMN_TYPE as columnType, + COLUMN_KEY as columnKey, + EXTRA as extra + FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = :schema AND TABLE_NAME = :table - "); + '); $stmt->bindParam(':schema', $schema); $stmt->bindParam(':table', $collection); $stmt->execute(); $results = $stmt->fetchAll(); $stmt->closeCursor(); + foreach ($results as $index => $document) { + $results[$index] = new Document($document); + } + return $results; } catch (PDOException $e) { diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index b1ed05374..6cbbfde8e 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -1321,42 +1321,45 @@ public function testSchemaAttribute(): void $attributes = []; foreach ($db->getSchemaAttributes($collection) as $attribute) { - $attributes[$attribute['COLUMN_NAME']] = $attribute; - } + /** + * @var $attribute Document + */ + $attributes[$attribute->getAttribute('columnName')] = $attribute; + } $attribute = $attributes['username']; - $this->assertEquals('username', $attribute['COLUMN_NAME']); - $this->assertEquals('varchar', $attribute['DATA_TYPE']); - $this->assertEquals('varchar(128)', $attribute['COLUMN_TYPE']); - $this->assertEquals('128', $attribute['CHARACTER_MAXIMUM_LENGTH']); - $this->assertEquals('YES', $attribute['IS_NULLABLE']); + $this->assertEquals('username', $attribute['columnName']); + $this->assertEquals('varchar', $attribute['dataType']); + $this->assertEquals('varchar(128)', $attribute['columnType']); + $this->assertEquals('128', $attribute['characterMaximumLength']); + $this->assertEquals('YES', $attribute['isNullable']); $attribute = $attributes['story']; - $this->assertEquals('story', $attribute['COLUMN_NAME']); - $this->assertEquals('text', $attribute['DATA_TYPE']); - $this->assertEquals('text', $attribute['COLUMN_TYPE']); - $this->assertEquals('65535', $attribute['CHARACTER_MAXIMUM_LENGTH']); + $this->assertEquals('story', $attribute['columnName']); + $this->assertEquals('text', $attribute['dataType']); + $this->assertEquals('text', $attribute['columnType']); + $this->assertEquals('65535', $attribute['characterMaximumLength']); $attribute = $attributes['string_list']; - $this->assertEquals('string_list', $attribute['COLUMN_NAME']); - $this->assertTrue(in_array($attribute['DATA_TYPE'], ['json', 'longtext'])); // mysql vs maria - $this->assertTrue(in_array($attribute['COLUMN_TYPE'], ['json', 'longtext'])); - $this->assertTrue(in_array($attribute['CHARACTER_MAXIMUM_LENGTH'], [null, '4294967295'])); - $this->assertEquals('YES', $attribute['IS_NULLABLE']); + $this->assertEquals('string_list', $attribute['columnName']); + $this->assertTrue(in_array($attribute['dataType'], ['json', 'longtext'])); // mysql vs maria + $this->assertTrue(in_array($attribute['columnType'], ['json', 'longtext'])); + $this->assertTrue(in_array($attribute['characterMaximumLength'], [null, '4294967295'])); + $this->assertEquals('YES', $attribute['isNullable']); $attribute = $attributes['dob']; - $this->assertEquals('dob', $attribute['COLUMN_NAME']); - $this->assertEquals('datetime', $attribute['DATA_TYPE']); - $this->assertEquals('datetime(3)', $attribute['COLUMN_TYPE']); - $this->assertEquals(null, $attribute['CHARACTER_MAXIMUM_LENGTH']); - $this->assertEquals('3', $attribute['DATETIME_PRECISION']); + $this->assertEquals('dob', $attribute['columnName']); + $this->assertEquals('datetime', $attribute['dataType']); + $this->assertEquals('datetime(3)', $attribute['columnType']); + $this->assertEquals(null, $attribute['characterMaximumLength']); + $this->assertEquals('3', $attribute['datetimePrecision']); if ($db->getSharedTables()) { $attribute = $attributes['_tenant']; - $this->assertEquals('_tenant', $attribute['COLUMN_NAME']); - $this->assertEquals('int', $attribute['DATA_TYPE']); - $this->assertEquals('10', $attribute['NUMERIC_PRECISION']); - $this->assertTrue(in_array($attribute['COLUMN_TYPE'], ['int unsigned', 'int(11) unsigned'])); + $this->assertEquals('_tenant', $attribute['columnName']); + $this->assertEquals('int', $attribute['dataType']); + $this->assertEquals('10', $attribute['numericPrecision']); + $this->assertTrue(in_array($attribute['columnType'], ['int unsigned', 'int(11) unsigned'])); } }