Data mapper cleanup and join/update preparation

This commit is contained in:
Dennis Eichhorn 2016-01-01 10:30:41 +01:00
parent 98b72df5a4
commit d53cb93b79
5 changed files with 143 additions and 57 deletions

View File

@ -33,15 +33,29 @@ use phpOMS\DataStorage\Database\Query\Builder;
interface DataMapperInterface interface DataMapperInterface
{ {
/**
* Create data.
*
* @param mixed $obj Object reference (gets filled with insert id)
*
* @return mixed
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function create($obj);
/** /**
* Update data. * Update data.
* *
* @param mixed $obj Object reference (gets filled with insert id)
*
* @return void * @return void
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public function update(); public function update($obj);
/** /**
* Save data. * Save data.

View File

@ -181,16 +181,6 @@ abstract class DataMapperAbstract implements DataMapperInterface
} }
} }
/**
* Update data.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function update()
{
}
/** /**
* Save data. * Save data.
* *
@ -234,14 +224,14 @@ abstract class DataMapperAbstract implements DataMapperInterface
* *
* @param mixed $obj Object reference (gets filled with insert id) * @param mixed $obj Object reference (gets filled with insert id)
* *
* @return Builder * @return mixed
* *
* @throws * @throws
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public function create(&$obj) public function create($obj)
{ {
$query = new Builder($this->db); $query = new Builder($this->db);
$query->prefix($this->db->getPrefix()) $query->prefix($this->db->getPrefix())
@ -336,6 +326,57 @@ abstract class DataMapperAbstract implements DataMapperInterface
return $objId; 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 <d.eichhorn@oms.com>
*/
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. * Find data.
* *

View File

@ -349,19 +349,6 @@ class Builder
return $this->grammar->compileQuery($this); return $this->grammar->compileQuery($this);
} }
/**
* Executing.
*
* @return mixed
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function execute()
{
// todo: handle different query types (select, insert etc)
}
/** /**
* Make raw column selection. * Make raw column selection.
* *
@ -733,11 +720,6 @@ class Builder
{ {
} }
public function get()
{
return $this;
}
/** /**
* Insert into columns. * Insert into columns.
* *
@ -813,10 +795,14 @@ class Builder
return $this; return $this;
} }
public function update() : Builder public function update(...$columns) : Builder
{ {
$this->type = QueryType::UPDATE; $this->type = QueryType::UPDATE;
foreach ($columns as $key => $column) {
$this->inserts[] = $column;
}
return $this; 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) public function join($table1, $table2, $column1, $opperator, $column2)
{ {
return $this; return $this;
@ -880,9 +844,9 @@ class Builder
return $this; return $this;
} }
public function commit() : Builder public function on()
{ {
return $this;
} }
/** /**
@ -898,7 +862,19 @@ class Builder
return $this->type; 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 <d.eichhorn@oms.com>
*/
public function merge(Builder $query) : Builder
{ {
return clone($this); return clone($this);
} }

View File

@ -412,6 +412,17 @@ class Grammar extends \phpOMS\DataStorage\Database\Grammar
return ''; return '';
} }
/**
* Compile offset.
*
* @param Builder $query Builder
* @param array $groups Groups
*
* @return \string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
private function compileGroups(Builder $query, array $groups) private function compileGroups(Builder $query, array $groups)
{ {
$expression = ''; $expression = '';

View File

@ -0,0 +1,44 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @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 <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @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';
}