Skip to content

Commit

Permalink
Merge pull request #520 from sakuraovq/master
Browse files Browse the repository at this point in the history
remove batchUpdateOrInsert
  • Loading branch information
sakuraovq authored Sep 20, 2019
2 parents 4bbe57a + 491c9ac commit 5dc7610
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 222 deletions.
60 changes: 0 additions & 60 deletions src/db/src/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1251,66 +1251,6 @@ public function batchUpdateByIds(array $values): int
return $this->toBase()->batchUpdateByIds($values, $primary);
}

/**
* Batch Update Or Insert, UpdateOrInsert operating suggest add unique index
*
* @param array $items origin item
* @param array $baseWhere only support [key=>value] where
* @param array $whereKeys exists data where item
* @param array $updateKeys update item key
* @param array $incrKeys increment item key
*
* @return bool
* @throws ContainerException
* @throws DbException
* @throws ReflectionException
*/
public function batchUpdateOrInsert(
array $items,
array $baseWhere,
array $whereKeys = [],
array $updateKeys = [],
array $incrKeys = []
): bool {
$primary = $this->model->getKeyName();

$count = 0;
$timeColumn = $this->model->updateTimestamps();

foreach ($items as $k => &$item) {
$item = $this->model->getSafeAttributes($item);

if ($count === 0) {
$count = count($item);
} elseif ($count !== count($item)) {
throw new DbException('batchUpdateOrInsert The parameter length must be consistent.');
}

if (empty($item)) {
continue;
}

if ($timeColumn) {
$items[$k] = array_merge($timeColumn, $item);
}
}
unset($item);
// Filter empty values
$items = array_filter($items);
if (empty($items)) {
return false;
}

// Auto update "updateAt" column
$updateAtColumn = $this->model->getUpdatedAtColumn();
if (isset($timeColumn[$updateAtColumn])) {
$updateKeys[] = $updateAtColumn;
}

return $this->toBase()->batchUpdateOrInsert($items, $baseWhere, $whereKeys, $updateKeys, $incrKeys, $primary);
}


/**
* Dynamically handle calls into the query instance.
*
Expand Down
1 change: 0 additions & 1 deletion src/db/src/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
* @method static static firstOrCreate(array $attributes, array $values = [])
* @method static static updateOrCreate(array $attributes, array $values = [], array $counters = [])
* @method static bool updateOrInsert(array $attributes, array $values = [], array $counters = [])
* @method static bool batchUpdateOrInsert(array $items, array $baseWhere, array $whereKeys = [], array $updateKeys = [], array $incrKeys = []);
* @method static int batchUpdateByIds(array $values)
* @method static int updateAllCounters(array $attributes, array $counters, array $extra = [])
* @method static int updateAllCountersAdoptPrimary(array $attributes, array $counters, array $extra = [])
Expand Down
86 changes: 0 additions & 86 deletions src/db/src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2894,92 +2894,6 @@ public function batchUpdateByIds(array $values, string $primary = 'id')
return $affectedRows;
}

/**
* Batch Update Or Insert, UpdateOrInsert operating suggest add unique index
*
* @param array $items origin item
* @param array $baseWhere only support [key=>value] where
* @param array $whereKeys exists data where item
* @param array $updateKeys update item key
* @param array $incrKeys increment item key
* @param string $primary table primary
*
* @return bool
* @throws ContainerException
* @throws DbException
* @throws ReflectionException
*/
public function batchUpdateOrInsert(
array $items,
array $baseWhere,
array $whereKeys = [],
array $updateKeys = [],
array $incrKeys = [],
string $primary = 'id'
): bool {
$wheres = [];
foreach ($items as $k => $v) {
foreach ($whereKeys as $whereKey) {
$value = $v[$whereKey];
$wheres[$whereKey][$value] = $value;
}

if ($baseWhere) {
$items[$k] = array_merge($baseWhere, $v);
}
}

$existMaps = [];

$searchWhere = $wheres;
if ($baseWhere) {
$searchWhere = array_merge($baseWhere, $wheres);
}

$exitsList = $this->select($primary, ...$whereKeys)->where($searchWhere)->get();
$uniqueKeys = array_keys($searchWhere);
foreach ($exitsList as $record) {
$mergeData = $record;
if ($baseWhere) {
$mergeData = array_merge($baseWhere, $record);
}

$uniqueId = ArrayHelper::toString(
ArrayHelper::only($mergeData, $uniqueKeys)
);
$existMaps[$uniqueId] = $record[$primary];
}

$addItems = $updateItems = [];
foreach ($items as $item) {
$uniqueId = ArrayHelper::toString(ArrayHelper::only($item, $uniqueKeys));

if (isset($existMaps[$uniqueId])) {
$updateItem = array_merge(
ArrayHelper::only($item, $updateKeys),
$this->warpCounters(ArrayHelper::only($item, $incrKeys))
);
$updateItems[] = $updateItem;

continue;
}

$addItems[] = $item;
}

$updateRes = $addRes = true;

if ($updateItems) {
$updateRes = $this->batchUpdateByIds($updateItems);
}

if ($addItems) {
$addRes = $this->insert($addItems);
}

return $updateRes && $addRes;
}

/**
* Insert a new record and get the value of the primary key.
*
Expand Down
40 changes: 0 additions & 40 deletions src/db/test/unit/Eloquent/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -867,44 +867,4 @@ public function testWhereCall()
$this->assertEquals($sql, $toSql);
}

public function testModelBatchUpdateOrInsert()
{
$updateOrInsertItems = [
[
'age' => 2,
'user_desc' => 'desc2',
'hahh' => 2,
],
[
'age' => 3,
'user_desc' => 'desc2',
'hahh' => 3,
],
[
'age' => 3,
'user_desc' => 'desc1',
'hahh' => 3,
],
[
'age' => 3,
'user_desc' => 'desc41',
'hahh' => 3,
]
];

$baseWhere = [
'name' => 'swoft'
];

$result = User::batchUpdateOrInsert(
$updateOrInsertItems,
$baseWhere,
['user_desc'],
['age', 'user_desc'],
['hahh']
);

$this->assertTrue($result);
}

}
35 changes: 0 additions & 35 deletions src/db/test/unit/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -778,39 +778,4 @@ public function testWhereArray()

$this->assertEquals($expectSql, $res);
}

public function testBatchUpdateOrInsert()
{
$updateOrInsertItems = [
[
'age' => 2,
'user_desc' => 'desc',
'hahh' => 2,
],
[
'age' => 3,
'user_desc' => 'desc',
'hahh' => 3,
],
[
'age' => 3,
'user_desc' => 'desc1',
'hahh' => 3,
]
];

$baseWhere = [
'name' => 'swoft'
];

$result = DB::table('user')->batchUpdateOrInsert(
$updateOrInsertItems,
$baseWhere,
['user_desc'],
['age', 'user_desc'],
['hahh']
);

$this->assertTrue($result);
}
}

0 comments on commit 5dc7610

Please sign in to comment.