From d53cb93b79151263bb35ab2524a0ab06c03516fa Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Fri, 1 Jan 2016 10:30:41 +0100 Subject: [PATCH] Data mapper cleanup and join/update preparation --- DataStorage/DataMapperInterface.php | 16 ++++- DataStorage/Database/DataMapperAbstract.php | 65 +++++++++++++++---- DataStorage/Database/Query/Builder.php | 64 ++++++------------ .../Database/Query/Grammar/Grammar.php | 11 ++++ DataStorage/Database/Query/JoinType.php | 44 +++++++++++++ 5 files changed, 143 insertions(+), 57 deletions(-) create mode 100644 DataStorage/Database/Query/JoinType.php diff --git a/DataStorage/DataMapperInterface.php b/DataStorage/DataMapperInterface.php index 6de07d072..ed39b33de 100644 --- a/DataStorage/DataMapperInterface.php +++ b/DataStorage/DataMapperInterface.php @@ -33,15 +33,29 @@ use phpOMS\DataStorage\Database\Query\Builder; interface DataMapperInterface { + /** + * Create data. + * + * @param mixed $obj Object reference (gets filled with insert id) + * + * @return mixed + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function create($obj); + /** * Update data. * + * @param mixed $obj Object reference (gets filled with insert id) + * * @return void * * @since 1.0.0 * @author Dennis Eichhorn */ - public function update(); + public function update($obj); /** * Save data. diff --git a/DataStorage/Database/DataMapperAbstract.php b/DataStorage/Database/DataMapperAbstract.php index 4c10aa40f..2d4227a9b 100644 --- a/DataStorage/Database/DataMapperAbstract.php +++ b/DataStorage/Database/DataMapperAbstract.php @@ -181,16 +181,6 @@ abstract class DataMapperAbstract implements DataMapperInterface } } - /** - * Update data. - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function update() - { - } - /** * Save data. * @@ -234,14 +224,14 @@ abstract class DataMapperAbstract implements DataMapperInterface * * @param mixed $obj Object reference (gets filled with insert id) * - * @return Builder + * @return mixed * * @throws * * @since 1.0.0 * @author Dennis Eichhorn */ - public function create(&$obj) + public function create($obj) { $query = new Builder($this->db); $query->prefix($this->db->getPrefix()) @@ -336,6 +326,57 @@ abstract class DataMapperAbstract implements DataMapperInterface return $objId; } + /** + * Create object in db. + * + * @param mixed $obj Object reference (gets filled with insert id) + * + * @return void + * + * @throws + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function update($obj) + { + // todo: relations handling (needs to be done first)... updating, deleting or inserting are possible + + $query = new Builder($this->db); + $query->prefix($this->db->getPrefix()) + ->into(static::$table); + + $reflectionClass = new \ReflectionClass(get_class($obj)); + $properties = $reflectionClass->getProperties(); + + foreach ($properties as $property) { + $property->setAccessible(true); + + if (isset(static::$hasMany[($pname = $property->getName())])) { + continue; + } else { + /* is not a has many property */ + foreach (static::$columns as $key => $column) { + if ($column['internal'] === $pname) { + $value = $property->getValue($obj); + + if ($column['type'] === 'DateTime') { + $value = isset($value) ? $value->format('Y-m-d H:i:s') : null; + } + + $query->update($column['name']) + ->value($value); + break; + } + } + } + + // todo: do i have to reverse the accessibility or is there no risk involved here? + } + + $this->db->con->prepare($query->toSql())->execute(); + } + /** * Find data. * diff --git a/DataStorage/Database/Query/Builder.php b/DataStorage/Database/Query/Builder.php index c0db411cf..1edf6e1f6 100644 --- a/DataStorage/Database/Query/Builder.php +++ b/DataStorage/Database/Query/Builder.php @@ -349,19 +349,6 @@ class Builder return $this->grammar->compileQuery($this); } - /** - * Executing. - * - * @return mixed - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function execute() - { - // todo: handle different query types (select, insert etc) - } - /** * Make raw column selection. * @@ -733,11 +720,6 @@ class Builder { } - public function get() - { - return $this; - } - /** * Insert into columns. * @@ -813,10 +795,14 @@ class Builder return $this; } - public function update() : Builder + public function update(...$columns) : Builder { $this->type = QueryType::UPDATE; + foreach ($columns as $key => $column) { + $this->inserts[] = $column; + } + return $this; } @@ -828,28 +814,6 @@ class Builder { } - public function delete() - { - $this->type = QueryType::DELETE; - - return $this; - } - - public function create($table) - { - return $this; - } - - public function drop() - { - return $this; - } - - public function raw() - { - return $this; - } - public function join($table1, $table2, $column1, $opperator, $column2) { return $this; @@ -880,9 +844,9 @@ class Builder return $this; } - public function commit() : Builder + public function on() { - return $this; + } /** @@ -898,7 +862,19 @@ class Builder return $this->type; } - public function merge($query) : Builder + /** + * Merging query. + * + * Merging query in order to remove database query volume + * + * @param Builder $query Query + * + * @return Builder + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function merge(Builder $query) : Builder { return clone($this); } diff --git a/DataStorage/Database/Query/Grammar/Grammar.php b/DataStorage/Database/Query/Grammar/Grammar.php index e2b5638dd..f15ff98b0 100644 --- a/DataStorage/Database/Query/Grammar/Grammar.php +++ b/DataStorage/Database/Query/Grammar/Grammar.php @@ -412,6 +412,17 @@ class Grammar extends \phpOMS\DataStorage\Database\Grammar return ''; } + /** + * Compile offset. + * + * @param Builder $query Builder + * @param array $groups Groups + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ private function compileGroups(Builder $query, array $groups) { $expression = ''; diff --git a/DataStorage/Database/Query/JoinType.php b/DataStorage/Database/Query/JoinType.php new file mode 100644 index 000000000..8a904367c --- /dev/null +++ b/DataStorage/Database/Query/JoinType.php @@ -0,0 +1,44 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\DataStorage\Database\Query; + +use phpOMS\Datatypes\Enum; + +/** + * Query type enum. + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +abstract class JoinType extends Enum +{ + const JOIN = 'JOIN'; + const LEFT_JOIN = 'LEFT JOIN'; + const LEFT_OUTER_JOIN = 'LEFT OUTER JOIN'; + const LEFT_INNER_JOIN = 'LEFT INNER JOIN'; + const RIGHT_JOIN = 'RIGHT JOIN'; + const RIGHT_OUTER_JOIN = 'RIGHT OUTER JOIN'; + const RIGHT_INNER_JOIN = 'RIGHT INNER JOIN'; + const OUTER_JOIN = 'OUTER JOIN'; + const INNER_JOIN = 'INNER JOIN'; + const CROSS_JOIN = 'CROSS JOIN'; + const FULL_OUTER_JOIN = 'FULL OUTER JOIN'; +}