public function testConstruct() { $pdo = $this->factory->getConnection(); $pdo->query('CREATE TABLE foo ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name varchar(255) not null )'); $repository = new Repository('foo', 'Db\\Repository\\Foo', $this->factory); $foo = new Foo(); $foo->setName('hello world'); $repository->insert($foo); $this->assertEquals(1, $foo->getId()); $this->assertEquals('hello world', $foo->getName()); /** @var $foo Foo */ $foo = $repository->select()->whereEquals('id', 1)->fetch(); $this->assertEquals(1, $foo->getId()); $this->assertEquals('hello world', $foo->getName()); $foo->setName('2341234'); $repository->update($foo); $this->assertEquals(1, $foo->getId()); $this->assertEquals('2341234', $foo->getName()); $foo = $repository->select()->whereEquals('id', 1)->fetch(); $this->assertEquals(1, $foo->getId()); $this->assertEquals('2341234', $foo->getName()); foreach ($repository->select()->query() as $foo) { $this->assertEquals(1, $foo->getId()); $this->assertEquals('2341234', $foo->getName()); } $repository->delete($foo); $foo = $repository->select()->whereEquals('id', 1)->fetch(); $this->assertNull($foo); }
public function testConstruct() { $pdo = $this->factory->getConnection(); $pdo->query('CREATE TABLE foo ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name varchar(255) null )'); $table = new Table('foo', $this->factory); $this->assertEquals('foo', $table->getTableName()); $this->assertCount(2, $table->getTableColumns()); $this->assertArrayHasKey('id', $table->getTableColumns()); $this->assertArrayHasKey('name', $table->getTableColumns()); $id = $table->getColumn('id'); $this->assertNotNull($id); $this->assertEquals(null, $id->getDefault()); $this->assertEquals(false, $id->getIsNull()); $this->assertEquals(true, $id->getIsPrimary()); $this->assertEquals('id', $id->getName()); $this->assertEquals('integer', $id->getType()); $name = $table->getColumn('name'); $this->assertNotNull($name); $this->assertEquals(null, $name->getDefault()); $this->assertEquals(true, $name->getIsNull()); $this->assertEquals(false, $name->getIsPrimary()); $this->assertEquals('name', $name->getName()); $this->assertEquals('string', $name->getType()); }
public function testUnitOfWork() { $this->factory->getConnection()->query('CREATE TABLE foo ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name varchar(255) not null )'); $repository = new Repository('foo', 'Db\\Mapper\\FooUnitEntity', $this->factory); $mapper = new RepositoryMapper(); $mapper->setRepository('Db\\Mapper\\FooUnitEntity', $repository); $uof = new UnitOfWork($mapper); $entity = new FooUnitEntity(); $entity->setName('hello world'); $uof->registerNew($entity); $this->assertEquals(null, $entity->getId()); $fetched = $repository->select()->fetch(); $this->assertNull($fetched); $uof->commit(); $this->assertEquals(1, $entity->getId()); $fetched = $repository->select()->fetch(); $this->assertNotNull($fetched); }
public function insert($entity) { $data = array(); $primary = array(); foreach ($this->connectionFactory->getTable($this->tableName)->getTableColumns() as $column) { $value = $column->getDefault(); $name = $column->getName(); if (method_exists($entity, 'get' . $name)) { $value = $entity->{'get' . $name}(); } elseif (method_exists($entity, $name)) { $value = $entity->{$name}(); } elseif (isset($entity->{$name})) { $value = $entity->{$name}; } if ($column->getIsPrimary()) { $primary[$column->getName()] = $column; } if ($value === null && $column->getIsPrimary()) { continue; } else { $data[$name] = $value; } } $insert = new Insert($this->tableName); $insert->values($data); $this->connectionFactory->query($insert); $id = $this->connectionFactory->getConnection()->lastInsertId(); if (count($primary) === 1) { /** @var $primaryColumn TableColumn */ $primaryColumn = reset($primary); $column = $primaryColumn->getName(); if (method_exists($entity, 'set' . $column)) { $entity->{'set' . $column}($id); } elseif (method_exists($entity, $column)) { $entity->{$column}($id); } else { $entity->{$column} = $id; } } }
public function __construct($tableName, ConnectionFactory $connectionFactory) { $this->tableName = (string) $tableName; $pdo = $connectionFactory->getConnection(); $driverName = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); switch ($driverName) { case 'sqlite': $stmt = $pdo->query("PRAGMA table_info({$tableName})"); while ($columnData = $stmt->fetch(\PDO::FETCH_ASSOC)) { $column = new TableColumn($columnData); $column->setName($columnData['name']); $column->setType($columnData['type']); $column->setIsNull((int) $columnData['notnull'] === 0); $column->setIsPrimary((int) $columnData['pk'] === 1); $column->setDefault($columnData['dflt_value']); $this->tableColumns[$column->getName()] = $column; } break; default: throw new \ErrorException('unsupported driver'); } }
public function testSQLite() { $pdo = $this->factory->getConnection(); $this->assertInstanceOf('PDO', $pdo); $this->assertEquals("1", $pdo->query('SELECT 1')->fetchColumn(0)); }