Create Update
This commit is contained in:
parent
73de596b86
commit
365f2ff580
|
@ -48,6 +48,14 @@ class Database{
|
||||||
return $insert;
|
return $insert;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function update(array $fields = null){
|
||||||
|
$update = new Request\Update($this);
|
||||||
|
if(isset($fields))
|
||||||
|
return $update->fields($fields);
|
||||||
|
|
||||||
|
return $update;
|
||||||
|
}
|
||||||
|
|
||||||
public function create(string $table = null){
|
public function create(string $table = null){
|
||||||
$create = new Request\Create($this);
|
$create = new Request\Create($this);
|
||||||
if(isset($table))
|
if(isset($table))
|
||||||
|
@ -63,5 +71,9 @@ class Database{
|
||||||
|
|
||||||
return $drop;
|
return $drop;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getLastInsertId(){
|
||||||
|
return $this->pdo->lastInsertId();
|
||||||
|
}
|
||||||
//TODO update, delete
|
//TODO update, delete
|
||||||
}
|
}
|
|
@ -31,7 +31,7 @@ class Model{
|
||||||
public function __construct(array $data = [], bool $useColumns = false){
|
public function __construct(array $data = [], bool $useColumns = false){
|
||||||
foreach (static::getFields() as $field => $options) {
|
foreach (static::getFields() as $field => $options) {
|
||||||
$column = $useColumns ? static::getColumn($field) : $field;
|
$column = $useColumns ? static::getColumn($field) : $field;
|
||||||
$value = static::convertField(isset($data[$column]) ? $data[$column] : null, $field);
|
$value = static::convertField(isset($data[$column]) ? $data[$column] : (isset($options['default']) ? $options['default'] : null), $field);
|
||||||
$this->fields[$field] = [
|
$this->fields[$field] = [
|
||||||
'value' => $value,
|
'value' => $value,
|
||||||
'modified' => false
|
'modified' => false
|
||||||
|
@ -137,6 +137,30 @@ class Model{
|
||||||
return $columns;
|
return $columns;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function getPrimaryColumns(bool $sql = true): array{
|
||||||
|
$fields = static::getFields();
|
||||||
|
$columns = [];
|
||||||
|
foreach ($fields as $field => $options) {
|
||||||
|
if(isset($options['primary']) && $options['primary']){
|
||||||
|
$column = static::getColumn($field);
|
||||||
|
$columns[] = $sql ? '`'.static::TABLE.'`.`'.$column.'`' : $column;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $columns;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getModifiedColumns(bool $sql = true): array{
|
||||||
|
$fields = static::getFields();
|
||||||
|
$columns = [];
|
||||||
|
foreach ($fields as $field => $options) {
|
||||||
|
if($this->fields[$field]['modified']){
|
||||||
|
$column = static::getColumn($field);
|
||||||
|
$columns[] = $sql ? '`'.static::TABLE.'`.`'.$column.'`' : $column;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $columns;
|
||||||
|
}
|
||||||
|
|
||||||
public function getValues(){
|
public function getValues(){
|
||||||
$values = [];
|
$values = [];
|
||||||
foreach ($this->fields as $field => $data) {
|
foreach ($this->fields as $field => $data) {
|
||||||
|
@ -145,6 +169,23 @@ class Model{
|
||||||
return $values;
|
return $values;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getModifiedValues(){
|
||||||
|
$values = [];
|
||||||
|
foreach ($this->fields as $field => $data) {
|
||||||
|
if($data['modified']) $values[] = $data['value'];
|
||||||
|
}
|
||||||
|
return $values;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getPrimaryValues(){
|
||||||
|
$values = [];
|
||||||
|
foreach ($this->fields as $field => $data) {
|
||||||
|
$options = static::getOptions($field);
|
||||||
|
if(isset($options['primary']) && $options['primary']) $values[] = $data['value'];
|
||||||
|
}
|
||||||
|
return $values;
|
||||||
|
}
|
||||||
|
|
||||||
protected static function convertField($data, $field){
|
protected static function convertField($data, $field){
|
||||||
$options = static::getOptions($field);
|
$options = static::getOptions($field);
|
||||||
if(is_null($data)){
|
if(is_null($data)){
|
||||||
|
@ -163,6 +204,9 @@ class Model{
|
||||||
if(isset($options['lenght']) && strlen($data) > $options['lenght'])
|
if(isset($options['lenght']) && strlen($data) > $options['lenght'])
|
||||||
throw new DatabaseException('data is to long in field : '.$field);
|
throw new DatabaseException('data is to long in field : '.$field);
|
||||||
break;
|
break;
|
||||||
|
case 'bit':
|
||||||
|
$data = boolval($data); //MAYBE: E_NOTICE on strange types
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
throw new DatabaseException('unknown type in field : '.$field);
|
throw new DatabaseException('unknown type in field : '.$field);
|
||||||
break;
|
break;
|
||||||
|
@ -201,7 +245,15 @@ class Model{
|
||||||
}
|
}
|
||||||
|
|
||||||
public function runInsert(){
|
public function runInsert(){
|
||||||
static::insert()->run($this->getValues());
|
return static::insert()->run($this->getValues());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function runUpdate(){
|
||||||
|
$req = Connection::get(static::DATABASE)
|
||||||
|
->update(static::getModifiedColumns())
|
||||||
|
->table(static::TABLE)
|
||||||
|
->where(implode(' AND ', array_map(function($field){ return $field.' = ?'; }, static::getPrimaryColumns())))
|
||||||
|
->run(array_merge($this->getModifiedValues(), $this->getPrimaryValues()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function create(): Request\Create{
|
public static function create(): Request\Create{
|
||||||
|
|
|
@ -48,8 +48,6 @@ class Create extends Request{
|
||||||
$uniques
|
$uniques
|
||||||
)
|
)
|
||||||
)."\n)";
|
)."\n)";
|
||||||
|
|
||||||
//TODO: foreign keys
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function run(array $values = null){
|
public function run(array $values = null){
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Krutush\Database\Request;
|
||||||
|
|
||||||
|
use Krutush\Database\DatabaseException;
|
||||||
|
|
||||||
|
class Update extends Data{
|
||||||
|
protected $fields;
|
||||||
|
protected $table;
|
||||||
|
protected $where;
|
||||||
|
|
||||||
|
public function fields(array $fields = null, bool $add = false): Update{
|
||||||
|
$this->fields = $add ? array_merge($this->fields, $fields) : $fields;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function table(string $table): Update{
|
||||||
|
$this->table = $table;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function where(string $where, bool $add = false): Update{
|
||||||
|
$this->where = $add && $this->where ? '('.$this->where.') AND ('.$where.')' : $where;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function sql(){
|
||||||
|
if(!isset($this->table))
|
||||||
|
throw new DatabaseException('Any table set');
|
||||||
|
|
||||||
|
return 'UPDATE `'.$this->table."`\n".
|
||||||
|
'SET '.implode(', ', array_map(function($field){ return $field.' = ?'; }, $this->fields))."\n".
|
||||||
|
(isset($this->where) ? ('WHERE '.$this->where) : '');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function run(array $values = null){
|
||||||
|
return parent::execute($this->sql(), $values);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue