Skip to content

Commit

Permalink
Implement QueryBuilderInterface to Updatable
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Mar 14, 2024
1 parent 773fb21 commit d90ee71
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 16 deletions.
32 changes: 17 additions & 15 deletions src/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ByJG\AnyDataset\Db\DbDriverInterface;
use ByJG\MicroOrm\Exception\OrmBeforeInvalidException;
use ByJG\MicroOrm\Exception\OrmInvalidFieldsException;
use ByJG\MicroOrm\Exception\RepositoryReadOnlyException;
use ByJG\Serializer\BinderObject;
use ByJG\Serializer\SerializerObject;
Expand Down Expand Up @@ -147,11 +148,11 @@ public function delete($pkId)
}

/**
* @param \ByJG\MicroOrm\Updatable $updatable
* @param UpdateBuilderInterface $updatable
* @return bool
* @throws \ByJG\MicroOrm\Exception\InvalidArgumentException
* @throws RepositoryReadOnlyException
*/
public function deleteByQuery(Updatable $updatable)
public function deleteByQuery(UpdateBuilderInterface $updatable)
{
$params = [];
$sql = $updatable->buildDelete($params);
Expand Down Expand Up @@ -360,12 +361,13 @@ public function addObserver(ObserverProcessorInterface $observerProcessor)
}

/**
* @param \ByJG\MicroOrm\Updatable $updatable
* @param $instance
* @param UpdateBuilderInterface $updatable
* @param array $params
* @return int
* @throws \ByJG\MicroOrm\Exception\OrmInvalidFieldsException
* @throws OrmInvalidFieldsException
*/
protected function insert($instance, Updatable $updatable, array $params)
protected function insert($instance, UpdateBuilderInterface $updatable, array $params)
{
$keyGen = $this->getMapper()->generateKey($instance);
if (empty($keyGen)) {
Expand All @@ -376,26 +378,26 @@ protected function insert($instance, Updatable $updatable, array $params)
}

/**
* @param \ByJG\MicroOrm\Updatable $updatable
* @param UpdateBuilderInterface $updatable
* @param array $params
* @return int
* @throws \ByJG\MicroOrm\Exception\OrmInvalidFieldsException
* @throws RepositoryReadOnlyException
*/
protected function insertWithAutoinc(Updatable $updatable, array $params)
protected function insertWithAutoinc(UpdateBuilderInterface $updatable, array $params)
{
$sql = $updatable->buildInsert($params, $this->getDbDriverWrite()->getDbHelper());
$dbFunctions = $this->getDbDriverWrite()->getDbHelper();
return $dbFunctions->executeAndGetInsertedId($this->getDbDriverWrite(), $sql, $params);
}

/**
* @param \ByJG\MicroOrm\Updatable $updatable
* @param UpdateBuilderInterface $updatable
* @param array $params
* @param $keyGen
* @return mixed
* @throws \ByJG\MicroOrm\Exception\OrmInvalidFieldsException
* @throws RepositoryReadOnlyException
*/
protected function insertWithKeyGen(Updatable $updatable, array $params, $keyGen)
protected function insertWithKeyGen(UpdateBuilderInterface $updatable, array $params, $keyGen)
{
$params[$this->mapper->getPrimaryKey()[0]] = $keyGen;
$sql = $updatable->buildInsert($params, $this->getDbDriverWrite()->getDbHelper());
Expand All @@ -404,11 +406,11 @@ protected function insertWithKeyGen(Updatable $updatable, array $params, $keyGen
}

/**
* @param \ByJG\MicroOrm\Updatable $updatable
* @param UpdateBuilderInterface $updatable
* @param array $params
* @throws \ByJG\MicroOrm\Exception\InvalidArgumentException
* @throws RepositoryReadOnlyException
*/
protected function update(Updatable $updatable, array $params)
protected function update(UpdateBuilderInterface $updatable, array $params)
{
$fields = array_map(function ($item) use ($params) {
return $params[$item];
Expand Down
16 changes: 15 additions & 1 deletion src/Updatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace ByJG\MicroOrm;

use ByJG\AnyDataset\Db\DbDriverInterface;
use ByJG\AnyDataset\Db\DbFunctionsInterface;
use ByJG\MicroOrm\Exception\InvalidArgumentException;
use ByJG\MicroOrm\Exception\OrmInvalidFieldsException;

class Updatable
class Updatable implements UpdateBuilderInterface, QueryBuilderInterface
{
protected $fields = [];
protected $table = "";
Expand Down Expand Up @@ -176,4 +177,17 @@ public function buildDelete(&$params)

return ORMHelper::processLiteral($sql, $params);
}

public function build(?DbDriverInterface $dbDriver = null)
{
$query = Query::getInstance()
->fields($this->fields)
->table($this->table);

foreach ($this->where as $item) {
$query->where($item['filter'], $item['params']);
}

return $query->build($dbDriver);
}
}
15 changes: 15 additions & 0 deletions src/UpdateBuilderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace ByJG\MicroOrm;

use ByJG\AnyDataset\Db\DbDriverInterface;
use ByJG\AnyDataset\Db\DbFunctionsInterface;

interface UpdateBuilderInterface
{
public function buildInsert(&$params, DbFunctionsInterface $dbHelper = null);

public function buildUpdate(&$params, DbFunctionsInterface $dbHelper = null);

public function buildDelete(&$params);
}
59 changes: 59 additions & 0 deletions tests/UpdatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,63 @@ public function testDeleteError()
$this->object->table('test');
$this->object->buildDelete($params);
}

public function testQueryUpdatable()
{
$this->object->table('test');
$this->assertEquals(
[
'sql' => 'SELECT * FROM test',
'params' => []
],
$this->object->build()
);


$this->object
->fields(['fld1'])
->fields(['fld2', 'fld3']);

$this->assertEquals(
[
'sql' => 'SELECT fld1, fld2, fld3 FROM test',
'params' => []
],
$this->object->build()
);

$this->object
->where('fld2 = :teste', [ 'teste' => 10 ]);

$this->assertEquals(
[
'sql' => 'SELECT fld1, fld2, fld3 FROM test WHERE fld2 = :teste',
'params' => [ 'teste' => 10 ]
],
$this->object->build()
);

$this->object
->where('fld3 = 20');

$this->assertEquals(
[
'sql' => 'SELECT fld1, fld2, fld3 FROM test WHERE fld2 = :teste AND fld3 = 20',
'params' => [ 'teste' => 10 ]
],
$this->object->build()
);

$this->object
->where('fld1 = [[teste2]]', [ 'teste2' => 40 ]);

$this->assertEquals(
[
'sql' => 'SELECT fld1, fld2, fld3 FROM test WHERE fld2 = :teste AND fld3 = 20 AND fld1 = [[teste2]]',
'params' => [ 'teste' => 10, 'teste2' => 40 ]
],
$this->object->build()
);
}

}

0 comments on commit d90ee71

Please sign in to comment.