Skip to content

Commit

Permalink
sql table query methods return now an array instead of RecordInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Jan 14, 2015
1 parent 05b407d commit ccd21d3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 67 deletions.
43 changes: 19 additions & 24 deletions library/PSX/Sql/SerializeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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,
);
}
}
11 changes: 5 additions & 6 deletions library/PSX/Sql/TableAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

});
}
Expand Down
12 changes: 6 additions & 6 deletions tests/PSX/Sql/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}

Expand Down
65 changes: 34 additions & 31 deletions tests/PSX/Sql/TableTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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'));
}
}
}

0 comments on commit ccd21d3

Please sign in to comment.