- Added the replace keyword
This commit is contained in:
parent
7fb591e1f5
commit
0453e9cbc5
|
@ -4,12 +4,16 @@ namespace Ulmus\Query;
|
||||||
|
|
||||||
class Insert extends Fragment {
|
class Insert extends Fragment {
|
||||||
|
|
||||||
const SQL_TOKEN = "INSERT";
|
const SQL_INSERT_TOKEN = "INSERT";
|
||||||
|
|
||||||
|
const SQL_REPLACE_TOKEN = "REPLACE";
|
||||||
|
|
||||||
public int $order = -100;
|
public int $order = -100;
|
||||||
|
|
||||||
public bool $quick = false;
|
public bool $quick = false;
|
||||||
|
|
||||||
|
public bool $replace = false;
|
||||||
|
|
||||||
public bool $ignore = false;
|
public bool $ignore = false;
|
||||||
|
|
||||||
public array $fieldlist = [];
|
public array $fieldlist = [];
|
||||||
|
@ -19,11 +23,11 @@ class Insert extends Fragment {
|
||||||
public string $table;
|
public string $table;
|
||||||
|
|
||||||
public ? string $alias = null;
|
public ? string $alias = null;
|
||||||
|
|
||||||
public function render() : string
|
public function render() : string
|
||||||
{
|
{
|
||||||
return $this->renderSegments([
|
return $this->renderSegments([
|
||||||
static::SQL_TOKEN,
|
$this->replace ? static::SQL_REPLACE_TOKEN : static::SQL_INSERT_TOKEN,
|
||||||
( $this->priority ?? false ),
|
( $this->priority ?? false ),
|
||||||
( $this->ignore ? 'IGNORE' : false ),
|
( $this->ignore ? 'IGNORE' : false ),
|
||||||
'INTO', $this->table,
|
'INTO', $this->table,
|
||||||
|
|
|
@ -114,7 +114,7 @@ class Where extends Fragment {
|
||||||
}
|
}
|
||||||
|
|
||||||
# whitelisting operators
|
# 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()
|
protected function value()
|
||||||
|
|
|
@ -64,7 +64,7 @@ class QueryBuilder implements Query\QueryBuilderInterface
|
||||||
return $this;
|
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 ( null === $this->getFragment(Query\Insert::class) ) {
|
||||||
if ( $schema ) {
|
if ( $schema ) {
|
||||||
|
@ -77,7 +77,8 @@ class QueryBuilder implements Query\QueryBuilderInterface
|
||||||
|
|
||||||
$insert = new Query\Insert();
|
$insert = new Query\Insert();
|
||||||
$this->push($insert);
|
$this->push($insert);
|
||||||
|
|
||||||
|
$insert->replace = $replace;
|
||||||
$insert->fieldlist = $fieldlist;
|
$insert->fieldlist = $fieldlist;
|
||||||
$insert->alias = $alias;
|
$insert->alias = $alias;
|
||||||
$insert->table = $table;
|
$insert->table = $table;
|
||||||
|
|
|
@ -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) ) {
|
if ( ! $this->matchEntity($entity) ) {
|
||||||
throw new \Exception("Your entity class `" . get_class($entity) . "` cannot match entity type of repository `{$this->entityClass}`");
|
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();
|
$primaryKeyDefinition = Ulmus::resolveEntity($this->entityClass)->getPrimaryKeyField();
|
||||||
|
|
||||||
if ( ! $entity->isLoaded() ) {
|
if ( ! $entity->isLoaded() ) {
|
||||||
$statement = $this->insertSqlQuery($fieldsAndValue ?? $dataset)->runQuery();
|
$statement = $this->insertSqlQuery($fieldsAndValue ?? $dataset, $replace)->runQuery();
|
||||||
|
|
||||||
if ( ( 0 !== $statement->lastInsertId ) &&
|
if ( ( 0 !== $statement->lastInsertId ) &&
|
||||||
( null !== $primaryKeyDefinition )) {
|
( null !== $primaryKeyDefinition )) {
|
||||||
|
@ -170,6 +174,11 @@ class Repository
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function replace(/*object|array*/ $entity, ? array $fieldsAndValue = null) : bool
|
||||||
|
{
|
||||||
|
return $this->save($entity, $fieldsAndValue, true);
|
||||||
|
}
|
||||||
|
|
||||||
public function saveAll(EntityCollection $collection) : void
|
public function saveAll(EntityCollection $collection) : void
|
||||||
{
|
{
|
||||||
foreach($collection as $entity) {
|
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
|
public function truncate(? string $table = null, ? string $alias = null, ? string $schema = null) : self
|
||||||
{
|
{
|
||||||
$schema = $schema ?: $this->entityResolver->schemaName();
|
$schema = $schema ?: $this->entityResolver->schemaName();
|
||||||
|
@ -195,9 +211,13 @@ class Repository
|
||||||
return $this->createSqlQuery()->runQuery();
|
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
|
public function yield() : \Generator
|
||||||
|
@ -261,9 +281,9 @@ class Repository
|
||||||
return $this;
|
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;
|
return $this;
|
||||||
}
|
}
|
||||||
|
@ -604,10 +624,13 @@ class Repository
|
||||||
return $this;
|
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) ) {
|
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());
|
$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);
|
$this->values($dataset);
|
||||||
|
|
Loading…
Reference in New Issue