From 0453e9cbc5efdb6afe0930df3939f23c3da48cb0 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Mon, 2 Aug 2021 15:13:17 +0000 Subject: [PATCH] - Added the replace keyword --- src/Query/Insert.php | 14 +++++++++----- src/Query/Where.php | 2 +- src/QueryBuilder.php | 5 +++-- src/Repository.php | 41 ++++++++++++++++++++++++++++++++--------- 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/Query/Insert.php b/src/Query/Insert.php index 867d488..d5ce00e 100644 --- a/src/Query/Insert.php +++ b/src/Query/Insert.php @@ -4,12 +4,16 @@ namespace Ulmus\Query; class Insert extends Fragment { - const SQL_TOKEN = "INSERT"; - + const SQL_INSERT_TOKEN = "INSERT"; + + const SQL_REPLACE_TOKEN = "REPLACE"; + public int $order = -100; public bool $quick = false; - + + public bool $replace = false; + public bool $ignore = false; public array $fieldlist = []; @@ -19,11 +23,11 @@ class Insert extends Fragment { public string $table; public ? string $alias = null; - + public function render() : string { return $this->renderSegments([ - static::SQL_TOKEN, + $this->replace ? static::SQL_REPLACE_TOKEN : static::SQL_INSERT_TOKEN, ( $this->priority ?? false ), ( $this->ignore ? 'IGNORE' : false ), 'INTO', $this->table, diff --git a/src/Query/Where.php b/src/Query/Where.php index 851ec1d..f9e81db 100644 --- a/src/Query/Where.php +++ b/src/Query/Where.php @@ -114,7 +114,7 @@ class Where extends Fragment { } # whitelisting operators - return in_array(strtoupper($this->operator), [ '=', '!=', '<>', 'LIKE', 'IS', 'IS NOT' ]) ? $this->operator : "="; + return in_array(strtoupper($this->operator), [ '=', '!=', '>', '>=', '<', '<=', '<>', 'LIKE', 'IS', 'IS NOT' ]) ? $this->operator : "="; } protected function value() diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 35742d6..2b9ada3 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -64,7 +64,7 @@ class QueryBuilder implements Query\QueryBuilderInterface return $this; } - public function insert(array $fieldlist, string $table, ? string $alias = null, ? string $database = null, ? string $schema = null) : self + public function insert(array $fieldlist, string $table, ? string $alias = null, ? string $database = null, ? string $schema = null, bool $replace = false) : self { if ( null === $this->getFragment(Query\Insert::class) ) { if ( $schema ) { @@ -77,7 +77,8 @@ class QueryBuilder implements Query\QueryBuilderInterface $insert = new Query\Insert(); $this->push($insert); - + + $insert->replace = $replace; $insert->fieldlist = $fieldlist; $insert->alias = $alias; $insert->table = $table; diff --git a/src/Repository.php b/src/Repository.php index d768547..11cd72d 100644 --- a/src/Repository.php +++ b/src/Repository.php @@ -125,8 +125,12 @@ class Repository } } - public function save(object $entity, ? array $fieldsAndValue = null) : bool + public function save(/*object|array*/ $entity, ? array $fieldsAndValue = null, bool $replace = false) : bool { + if ( is_array($entity) ) { + $entity = ( new $this->entityClass() )->fromArray($entity); + } + if ( ! $this->matchEntity($entity) ) { throw new \Exception("Your entity class `" . get_class($entity) . "` cannot match entity type of repository `{$this->entityClass}`"); } @@ -136,7 +140,7 @@ class Repository $primaryKeyDefinition = Ulmus::resolveEntity($this->entityClass)->getPrimaryKeyField(); if ( ! $entity->isLoaded() ) { - $statement = $this->insertSqlQuery($fieldsAndValue ?? $dataset)->runQuery(); + $statement = $this->insertSqlQuery($fieldsAndValue ?? $dataset, $replace)->runQuery(); if ( ( 0 !== $statement->lastInsertId ) && ( null !== $primaryKeyDefinition )) { @@ -170,6 +174,11 @@ class Repository return false; } + public function replace(/*object|array*/ $entity, ? array $fieldsAndValue = null) : bool + { + return $this->save($entity, $fieldsAndValue, true); + } + public function saveAll(EntityCollection $collection) : void { foreach($collection as $entity) { @@ -177,6 +186,13 @@ class Repository } } + public function replaceAll(EntityCollection $collection) : void + { + foreach($collection as $entity) { + $this->replace($entity); + } + } + public function truncate(? string $table = null, ? string $alias = null, ? string $schema = null) : self { $schema = $schema ?: $this->entityResolver->schemaName(); @@ -195,9 +211,13 @@ class Repository return $this->createSqlQuery()->runQuery(); } - public function generateDatasetDiff(object $entity) : array + public function generateDatasetDiff(object $entity, bool $oldValues = false) : array { - return array_diff_assoc( array_change_key_case($entity->toArray()), array_change_key_case($entity->entityGetDataset(false, true)) ); + $array = array_change_key_case($entity->toArray()); + + $dataset = array_change_key_case($entity->entityGetDataset(false, true)); + + return array_diff_assoc($oldValues ? $dataset : $array , $oldValues ? $array : $dataset ); } public function yield() : \Generator @@ -261,9 +281,9 @@ class Repository return $this; } - public function insert(array $fieldlist, string $table, string $alias, ? string $schema) : self + public function insert(array $fieldlist, string $table, string $alias, ? string $schema, bool $replace = false) : self { - $this->queryBuilder->insert($fieldlist, $this->escapeTable($table), $alias, $this->escapedDatabase(), $schema); + $this->queryBuilder->insert($fieldlist, $this->escapeTable($table), $alias, $this->escapedDatabase(), $schema, $replace); return $this; } @@ -604,10 +624,13 @@ class Repository return $this; } - protected function insertSqlQuery(array $dataset) : self + protected function insertSqlQuery(array $dataset, bool $replace = false) : self { - if ( null === $this->queryBuilder->getFragment(Query\Insert::class) ) { - $this->insert(array_map([ $this, 'escapeField' ] , array_keys($dataset)), $this->entityResolver->tableName(), $this->alias, $this->entityResolver->schemaName()); + if ( null === $insert = $this->queryBuilder->getFragment(Query\Insert::class) ) { + $this->insert(array_map([ $this, 'escapeField' ] , array_keys($dataset)), $this->entityResolver->tableName(), $this->alias, $this->entityResolver->schemaName(), $replace); + } + else { + $insert->replace = $replace; } $this->values($dataset);