mirror of
https://github.com/friendica/friendica
synced 2025-04-26 17:10:10 +00:00
Mode switch for insert
This commit is contained in:
parent
fa5acb3b21
commit
303aaa00ca
15 changed files with 56 additions and 31 deletions
|
@ -290,16 +290,16 @@ class DBA
|
|||
/**
|
||||
* Insert a row into a table
|
||||
*
|
||||
* @param string|array $table Table name or array [schema => table]
|
||||
* @param array $param parameter array
|
||||
* @param bool $on_duplicate_update Do an update on a duplicate entry
|
||||
* @param string|array $table Table name or array [schema => table]
|
||||
* @param array $param parameter array
|
||||
* @param int $duplicate_mode What to do on a duplicated entry
|
||||
*
|
||||
* @return boolean was the insert successful?
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function insert($table, $param, $on_duplicate_update = false)
|
||||
public static function insert($table, array $param, int $duplicate_mode = Database::INSERT_DEFAULT)
|
||||
{
|
||||
return DI::dba()->insert($table, $param, $on_duplicate_update);
|
||||
return DI::dba()->insert($table, $param, $duplicate_mode);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1050,7 +1050,7 @@ class DBStructure
|
|||
{
|
||||
if (self::existsTable('verb') && !DBA::exists('verb', ['id' => 1])) {
|
||||
foreach (Item::ACTIVITIES as $index => $activity) {
|
||||
DBA::insert('verb', ['id' => $index + 1, 'name' => $activity], true);
|
||||
DBA::insert('verb', ['id' => $index + 1, 'name' => $activity], Database::INSERT_IGNORE);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -42,6 +42,10 @@ class Database
|
|||
const PDO = 'pdo';
|
||||
const MYSQLI = 'mysqli';
|
||||
|
||||
const INSERT_DEFAULT = 0;
|
||||
const INSERT_UPDATE = 1;
|
||||
const INSERT_IGNORE = 2;
|
||||
|
||||
protected $connected = false;
|
||||
|
||||
/**
|
||||
|
@ -966,14 +970,14 @@ class Database
|
|||
/**
|
||||
* Insert a row into a table
|
||||
*
|
||||
* @param string|array $table Table name or array [schema => table]
|
||||
* @param array $param parameter array
|
||||
* @param bool $on_duplicate_update Do an update on a duplicate entry
|
||||
* @param string|array $table Table name or array [schema => table]
|
||||
* @param array $param parameter array
|
||||
* @param int $duplicate_mode What to do on a duplicated entry
|
||||
*
|
||||
* @return boolean was the insert successful?
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function insert($table, array $param, bool $on_duplicate_update = false)
|
||||
public function insert($table, array $param, int $duplicate_mode = self::INSERT_DEFAULT)
|
||||
{
|
||||
if (empty($table) || empty($param)) {
|
||||
$this->logger->info('Table and fields have to be set');
|
||||
|
@ -986,9 +990,15 @@ class Database
|
|||
|
||||
$values_string = substr(str_repeat("?, ", count($param)), 0, -2);
|
||||
|
||||
$sql = "INSERT INTO " . $table_string . " (" . $fields_string . ") VALUES (" . $values_string . ")";
|
||||
$sql = "INSERT ";
|
||||
|
||||
if ($on_duplicate_update) {
|
||||
if ($duplicate_mode == self::INSERT_IGNORE) {
|
||||
$sql .= "IGNORE ";
|
||||
}
|
||||
|
||||
$sql .= "INTO " . $table_string . " (" . $fields_string . ") VALUES (" . $values_string . ")";
|
||||
|
||||
if ($duplicate_mode == self::INSERT_UPDATE) {
|
||||
$fields_string = implode(' = ?, ', array_map([DBA::class, 'quoteIdentifier'], array_keys($param)));
|
||||
|
||||
$sql .= " ON DUPLICATE KEY UPDATE " . $fields_string . " = ?";
|
||||
|
@ -997,7 +1007,12 @@ class Database
|
|||
$param = array_merge_recursive($values, $values);
|
||||
}
|
||||
|
||||
return $this->e($sql, $param);
|
||||
$result = $this->e($sql, $param);
|
||||
if (!$result || ($duplicate_mode != self::INSERT_IGNORE)) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
return $this->affectedRows() != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -736,7 +736,7 @@ class PostUpdate
|
|||
while ($delivery = DBA::fetch($deliveries)) {
|
||||
$id = $delivery['iid'];
|
||||
unset($delivery['iid']);
|
||||
DBA::insert('post-delivery-data', $delivery, true);
|
||||
DBA::insert('post-delivery-data', $delivery, Database::INSERT_UPDATE);
|
||||
++$rows;
|
||||
}
|
||||
DBA::close($deliveries);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue