diff --git a/library/PSX/Sql/SerializeTrait.php b/library/PSX/Sql/SerializeTrait.php index b5811cb1..6df74781 100644 --- a/library/PSX/Sql/SerializeTrait.php +++ b/library/PSX/Sql/SerializeTrait.php @@ -35,15 +35,29 @@ */ trait SerializeTrait { + protected $mapping = array( + TableInterface::TYPE_SMALLINT => Type::SMALLINT, + TableInterface::TYPE_INT => Type::INTEGER, + TableInterface::TYPE_BIGINT => Type::BIGINT, + TableInterface::TYPE_BOOLEAN => Type::BOOLEAN, + TableInterface::TYPE_DECIMAL => Type::DECIMAL, + TableInterface::TYPE_FLOAT => Type::FLOAT, + TableInterface::TYPE_DATE => Type::DATE, + TableInterface::TYPE_DATETIME => Type::DATETIME, + TableInterface::TYPE_TIME => Type::TIME, + TableInterface::TYPE_VARCHAR => Type::STRING, + TableInterface::TYPE_TEXT => Type::TEXT, + TableInterface::TYPE_BLOB => Type::BLOB, + ); + protected function unserializeType($value, $type) { $type = (($type >> 20) & 0xFF) << 20; $platform = $this->connection->getDatabasePlatform(); - $mapping = $this->getDbalTableMapping(); - if(isset($mapping[$type])) + if(isset($this->mapping[$type])) { - return Type::getType($mapping[$type])->convertToPHPValue($value, $platform); + return Type::getType($this->mapping[$type])->convertToPHPValue($value, $platform); } else { @@ -55,33 +69,14 @@ protected function serializeType($value, $type) { $type = (($type >> 20) & 0xFF) << 20; $platform = $this->connection->getDatabasePlatform(); - $mapping = $this->getDbalTableMapping(); - if(isset($mapping[$type])) + if(isset($this->mapping[$type])) { - return Type::getType($mapping[$type])->convertToDatabaseValue($value, $platform); + return Type::getType($this->mapping[$type])->convertToDatabaseValue($value, $platform); } else { return Type::getType(Type::STRING)->convertToDatabaseValue($value, $platform); } } - - protected function getDbalTableMapping() - { - return array( - TableInterface::TYPE_SMALLINT => Type::SMALLINT, - TableInterface::TYPE_INT => Type::INTEGER, - TableInterface::TYPE_BIGINT => Type::BIGINT, - TableInterface::TYPE_BOOLEAN => Type::BOOLEAN, - TableInterface::TYPE_DECIMAL => Type::DECIMAL, - TableInterface::TYPE_FLOAT => Type::FLOAT, - TableInterface::TYPE_DATE => Type::DATE, - TableInterface::TYPE_DATETIME => Type::DATETIME, - TableInterface::TYPE_TIME => Type::TIME, - TableInterface::TYPE_VARCHAR => Type::STRING, - TableInterface::TYPE_TEXT => Type::TEXT, - TableInterface::TYPE_BLOB => Type::BLOB, - ); - } } diff --git a/library/PSX/Sql/TableAbstract.php b/library/PSX/Sql/TableAbstract.php index 871daefb..494c1bfb 100644 --- a/library/PSX/Sql/TableAbstract.php +++ b/library/PSX/Sql/TableAbstract.php @@ -270,20 +270,19 @@ protected function getFirstColumnWithType($searchType) protected function project($sql, array $params = array(), array $columns = null) { - $name = $this->getDisplayName(); $columns = $columns === null ? $this->getColumns() : $columns; - return $this->connection->project($sql, $params, function(array $row) use ($name, $columns){ + return $this->connection->project($sql, $params, function(array $row) use ($columns){ - foreach($row as $key => $value) + foreach($columns as $name => $type) { - if(isset($columns[$key])) + if(isset($row[$name])) { - $row[$key] = $this->unserializeType($value, $columns[$key]); + $row[$name] = $this->unserializeType($row[$name], $type); } } - return new Record($name, $row); + return $row; }); } diff --git a/tests/PSX/Sql/TableTest.php b/tests/PSX/Sql/TableTest.php index a246c6bb..6bdede1e 100644 --- a/tests/PSX/Sql/TableTest.php +++ b/tests/PSX/Sql/TableTest.php @@ -122,12 +122,12 @@ public function testRestrictedFields() foreach($result as $key => $row) { - $this->assertInstanceOf('PSX\Data\Record', $row); - $this->assertEquals(array('title', 'date'), array_keys($row->getRecordInfo()->getData())); - - $this->assertEquals($expect[$key]['title'], $row->getTitle()); - $this->assertInstanceOf('DateTime', $row->getDate()); - $this->assertEquals($expect[$key]['date'], $row->getDate()->format('Y-m-d H:i:s')); + $this->assertArrayHasKey('title', $row); + $this->assertArrayHasKey('date', $row); + + $this->assertEquals($expect[$key]['title'], $row['title']); + $this->assertInstanceOf('DateTime', $row['date']); + $this->assertEquals($expect[$key]['date'], $row['date']->format('Y-m-d H:i:s')); } } diff --git a/tests/PSX/Sql/TableTestCase.php b/tests/PSX/Sql/TableTestCase.php index 41449108..4dd33fd0 100644 --- a/tests/PSX/Sql/TableTestCase.php +++ b/tests/PSX/Sql/TableTestCase.php @@ -300,14 +300,15 @@ public function testGetAllSortDesc() foreach($result as $row) { - $this->assertInstanceOf('PSX\Data\Record', $row); - $this->assertEquals(true, $row->getId() != null); - $this->assertEquals(true, $row->getTitle() != null); + $this->assertArrayHasKey('id', $row); + $this->assertArrayHasKey('title', $row); + $this->assertEquals(true, $row['id'] != null); + $this->assertEquals(true, $row['title'] != null); } // check order - $this->assertEquals(4, $result[0]->getId()); - $this->assertEquals(3, $result[1]->getId()); + $this->assertEquals(4, $result[0]['id']); + $this->assertEquals(3, $result[1]['id']); } public function testGetAllSortAsc() @@ -593,12 +594,12 @@ public function testCreate() $table->create($record); - $record = $table->getOneById(5); + $row = $table->getOneById(5); - $this->assertEquals(5, $record->getId()); - $this->assertEquals(2, $record->getUserId()); - $this->assertEquals('foobar', $record->getTitle()); - $this->assertInstanceOf('DateTime', $record->getDate()); + $this->assertEquals(5, $row['id']); + $this->assertEquals(2, $row['userId']); + $this->assertEquals('foobar', $row['title']); + $this->assertInstanceOf('DateTime', $row['date']); } public function testUpdate() @@ -610,18 +611,18 @@ public function testUpdate() $this->markTestSkipped('Table not an manipulation interface'); } - $record = $table->getOneById(1); - $record->setUserId(2); - $record->setTitle('foobar'); - $record->setDate(new DateTime()); + $row = $table->getOneById(1); + $row['userId'] = 2; + $row['title'] = 'foobar'; + $row['date'] = new DateTime(); - $table->update($record); + $table->update($row); - $record = $table->getOneById(1); + $row = $table->getOneById(1); - $this->assertEquals(2, $record->getUserId()); - $this->assertEquals('foobar', $record->getTitle()); - $this->assertInstanceOf('DateTime', $record->getDate()); + $this->assertEquals(2, $row['userId']); + $this->assertEquals('foobar', $row['title']); + $this->assertInstanceOf('DateTime', $row['date']); } public function testDelete() @@ -633,27 +634,29 @@ public function testDelete() $this->markTestSkipped('Table not an manipulation interface'); } - $record = $table->getOneById(1); + $row = $table->getOneById(1); - $table->delete($record); + $table->delete($row); - $record = $table->getOneById(1); + $row = $table->getOneById(1); - $this->assertEmpty($record); + $this->assertEmpty($row); } protected function assertResult($expect, $result) { foreach($result as $key => $row) { - $this->assertInstanceOf('PSX\Data\Record', $row); - $this->assertEquals(array('id', 'userId', 'title', 'date'), array_keys($row->getRecordInfo()->getData())); - - $this->assertEquals($expect[$key]['id'], $row->getId()); - $this->assertEquals($expect[$key]['userId'], $row->getUserId()); - $this->assertEquals($expect[$key]['title'], $row->getTitle()); - $this->assertInstanceOf('DateTime', $row->getDate()); - $this->assertEquals($expect[$key]['date'], $row->getDate()->format('Y-m-d H:i:s')); + $this->assertArrayHasKey('id', $row); + $this->assertArrayHasKey('userId', $row); + $this->assertArrayHasKey('title', $row); + $this->assertArrayHasKey('date', $row); + + $this->assertEquals($expect[$key]['id'], $row['id']); + $this->assertEquals($expect[$key]['userId'], $row['userId']); + $this->assertEquals($expect[$key]['title'], $row['title']); + $this->assertInstanceOf('DateTime', $row['date']); + $this->assertEquals($expect[$key]['date'], $row['date']->format('Y-m-d H:i:s')); } } }