- Added the replace keyword

This commit is contained in:
Dave Mc Nicoll 2021-08-02 15:13:17 +00:00
parent 7fb591e1f5
commit 0453e9cbc5
4 changed files with 45 additions and 17 deletions

View File

@ -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,

View File

@ -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()

View File

@ -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;

View File

@ -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);