Skip to content

Commit

Permalink
Merge pull request #19 from byjg/4.9
Browse files Browse the repository at this point in the history
Add buildAndGetIterator() and buildAndExecute()
  • Loading branch information
byjg authored Sep 3, 2024
2 parents 939bbef + 3803ee2 commit b054a92
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/QueryBasic.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ByJG\MicroOrm;

use ByJG\AnyDataset\Core\GenericIterator;
use ByJG\AnyDataset\Db\DbDriverInterface;
use ByJG\MicroOrm\Exception\InvalidArgumentException;
use ByJG\Serializer\SerializerObject;
Expand Down Expand Up @@ -287,4 +288,10 @@ public function build(?DbDriverInterface $dbDriver = null)

return [ 'sql' => $sql, 'params' => $params ];
}

public function buildAndGetIterator(?DbDriverInterface $dbDriver = null): GenericIterator
{
$sqlObject = $this->build($dbDriver);
return $dbDriver->getIterator($sqlObject['sql'], $sqlObject['params']);
}
}
3 changes: 3 additions & 0 deletions src/QueryBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace ByJG\MicroOrm;

use ByJG\AnyDataset\Core\GenericIterator;
use ByJG\AnyDataset\Db\DbDriverInterface;

interface QueryBuilderInterface
{
public function build(?DbDriverInterface $dbDriver = null);

public function buildAndGetIterator(?DbDriverInterface $dbDriver = null): GenericIterator;
}
6 changes: 6 additions & 0 deletions src/Union.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ByJG\MicroOrm;

use ByJG\AnyDataset\Core\GenericIterator;
use ByJG\AnyDataset\Db\DbDriverInterface;
use ByJG\AnyDataset\Db\Factory;
use ByJG\MicroOrm\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -97,4 +98,9 @@ public function build(?DbDriverInterface $dbDriver = null)
}


public function buildAndGetIterator(?DbDriverInterface $dbDriver = null): GenericIterator
{
$sqlObject = $this->build($dbDriver);
return $dbDriver->getIterator($sqlObject['sql'], $sqlObject['params']);
}
}
7 changes: 7 additions & 0 deletions src/Updatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,11 @@ protected function getWhere()

return [ implode(' AND ', $whereStr), $params ];
}

public function buildAndExecute(DbDriverInterface $dbDriver, $params = [], ?DbFunctionsInterface $dbHelper = null)
{
$sql = $this->build($params, $dbHelper);
return $dbDriver->execute($sql, $params);
}

}
2 changes: 2 additions & 0 deletions src/UpdateBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ interface UpdateBuilderInterface
{
public function build(&$params, ?DbFunctionsInterface $dbHelper = null);

public function buildAndExecute(DbDriverInterface $dbDriver, $params = [], ?DbFunctionsInterface $dbHelper = null);

public function convert(?DbFunctionsInterface $dbDriver = null);
}
60 changes: 60 additions & 0 deletions tests/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use ByJG\MicroOrm\Exception\InvalidArgumentException;
use ByJG\MicroOrm\Exception\RepositoryReadOnlyException;
use ByJG\MicroOrm\FieldMapping;
use ByJG\MicroOrm\InsertQuery;
use ByJG\MicroOrm\Literal;
use ByJG\MicroOrm\Mapper;
use ByJG\MicroOrm\ObserverData;
Expand All @@ -19,6 +20,7 @@
use ByJG\MicroOrm\Repository;
use ByJG\MicroOrm\Union;
use ByJG\MicroOrm\UpdateConstraint;
use ByJG\MicroOrm\UpdateQuery;
use ByJG\Util\Uri;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -139,6 +141,16 @@ public function testGetSelectFunction()
$this->assertEquals(2017, $users->getYear());
}

public function testBuildAndGetIterator()
{
$query = Query::getInstance()
->table('users')
->where('id = :id', ['id' => 1]);

$iterator = $query->buildAndGetIterator($this->repository->getDbDriver())->toArray();
$this->assertEquals(1, count($iterator));
}

public function testInsert()
{
$users = new Users();
Expand Down Expand Up @@ -301,6 +313,54 @@ public function testUpdateReadOnly()
$this->repository->save($users);
}

public function testUpdateBuildAndExecute()
{
$users = $this->repository->get(1);

$this->assertEquals('John Doe', $users->getName());

$updateQuery = UpdateQuery::getInstance()
->table('users')
->set('name', 'New Name')
->where('id = :id', ['id' => 1]);
$updateQuery->buildAndExecute($this->repository->getDbDriver());

$users = $this->repository->get(1);
$this->assertEquals('New Name', $users->getName());
}

public function testInsertBuildAndExecute()
{
$users = $this->repository->get(4);
$this->assertEmpty($users);

$insertQuery = InsertQuery::getInstance()
->table('users')
->fields([
'name',
'createdate'
]);
$insertQuery->buildAndExecute($this->repository->getDbDriver(), ['name' => 'inserted name', 'createdate' => '2024-09-03']);

$users = $this->repository->get(4);
$this->assertEquals('inserted name', $users->getName());
$this->assertEquals('2024-09-03', $users->getCreatedate());
}

public function testDeleteBuildAndExecute()
{
$users = $this->repository->get(1);
$this->assertEquals('John Doe', $users->getName());

$updateQuery = DeleteQuery::getInstance()
->table('users')
->where('id = :id', ['id' => 1]);
$updateQuery->buildAndExecute($this->repository->getDbDriver());

$users = $this->repository->get(1);
$this->assertEmpty($users);
}

public function testUpdate_beforeUpdate()
{
$users = $this->repository->get(1);
Expand Down

0 comments on commit b054a92

Please sign in to comment.