Merge pull request #9079 from annando/replace
Added support for the "replace" database command
This commit is contained in:
commit
8f1d2b00a5
2 changed files with 43 additions and 0 deletions
|
@ -292,6 +292,21 @@ class DBA
|
||||||
return DI::dba()->insert($table, $param, $on_duplicate_update);
|
return DI::dba()->insert($table, $param, $on_duplicate_update);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inserts a row with the provided data in the provided table.
|
||||||
|
* If the data corresponds to an existing row through a UNIQUE or PRIMARY index constraints, it updates the row instead.
|
||||||
|
*
|
||||||
|
* @param string|array $table Table name or array [schema => table]
|
||||||
|
* @param array $param parameter array
|
||||||
|
*
|
||||||
|
* @return boolean was the insert successful?
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public static function replace($table, $param)
|
||||||
|
{
|
||||||
|
return DI::dba()->replace($table, $param);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch the id of the last insert command
|
* Fetch the id of the last insert command
|
||||||
*
|
*
|
||||||
|
|
|
@ -1006,6 +1006,34 @@ class Database
|
||||||
return $this->e($sql, $param);
|
return $this->e($sql, $param);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inserts a row with the provided data in the provided table.
|
||||||
|
* If the data corresponds to an existing row through a UNIQUE or PRIMARY index constraints, it updates the row instead.
|
||||||
|
*
|
||||||
|
* @param string|array $table Table name or array [schema => table]
|
||||||
|
* @param array $param parameter array
|
||||||
|
*
|
||||||
|
* @return boolean was the insert successful?
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function replace($table, array $param)
|
||||||
|
{
|
||||||
|
if (empty($table) || empty($param)) {
|
||||||
|
$this->logger->info('Table and fields have to be set');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$table_string = DBA::buildTableString($table);
|
||||||
|
|
||||||
|
$fields_string = implode(', ', array_map([DBA::class, 'quoteIdentifier'], array_keys($param)));
|
||||||
|
|
||||||
|
$values_string = substr(str_repeat("?, ", count($param)), 0, -2);
|
||||||
|
|
||||||
|
$sql = "REPLACE " . $table_string . " (" . $fields_string . ") VALUES (" . $values_string . ")";
|
||||||
|
|
||||||
|
return $this->e($sql, $param);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch the id of the last insert command
|
* Fetch the id of the last insert command
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue