2016-09-26 07:32:16 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Traits;
|
|
|
|
|
2019-08-05 10:56:48 +00:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2021-06-05 10:47:56 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2020-12-22 20:11:22 +00:00
|
|
|
use Throwable;
|
2016-09-26 07:32:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* With reference to GitHub issue #463.
|
|
|
|
* MySQL and PostgreSQL seem to have a limit of 2^16-1 (65535) elements in an IN statement.
|
|
|
|
* This trait provides a method as a workaround to this limitation.
|
2019-08-05 10:56:48 +00:00
|
|
|
*
|
|
|
|
* @method static Builder whereIn($keys, array $values)
|
|
|
|
* @method static Builder whereNotIn($keys, array $values)
|
|
|
|
* @method static Builder select(string $string)
|
2016-09-26 07:32:16 +00:00
|
|
|
*/
|
|
|
|
trait SupportsDeleteWhereIDsNotIn
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Deletes all records whose IDs are not in an array.
|
|
|
|
*
|
2020-12-22 20:11:22 +00:00
|
|
|
* @param array<string>|array<int> $ids the array of IDs
|
2021-06-05 10:47:56 +00:00
|
|
|
* @param string $key name of the primary key
|
2016-09-26 07:32:16 +00:00
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public static function deleteWhereIDsNotIn(array $ids, string $key = 'id'): void
|
2016-09-26 07:32:16 +00:00
|
|
|
{
|
2019-04-22 14:26:28 +00:00
|
|
|
$maxChunkSize = config('database.default') === 'sqlite-persistent' ? 999 : 65535;
|
2020-12-22 20:11:22 +00:00
|
|
|
|
2019-04-22 14:26:28 +00:00
|
|
|
// If the number of entries is lower than, or equals to maxChunkSize, just go ahead.
|
|
|
|
if (count($ids) <= $maxChunkSize) {
|
2018-08-24 15:27:19 +00:00
|
|
|
static::whereNotIn($key, $ids)->delete();
|
|
|
|
|
|
|
|
return;
|
2016-09-26 07:32:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we get the actual IDs that should be deleted…
|
|
|
|
$allIDs = static::select($key)->get()->pluck($key)->all();
|
|
|
|
$whereInIDs = array_diff($allIDs, $ids);
|
2018-08-24 15:27:19 +00:00
|
|
|
|
2016-09-26 07:32:16 +00:00
|
|
|
// …and see if we can delete them instead.
|
2019-04-22 14:26:28 +00:00
|
|
|
if (count($whereInIDs) < $maxChunkSize) {
|
2018-08-24 15:27:19 +00:00
|
|
|
static::whereIn($key, $whereInIDs)->delete();
|
|
|
|
|
|
|
|
return;
|
2016-09-26 07:32:16 +00:00
|
|
|
}
|
|
|
|
|
2019-04-22 14:26:28 +00:00
|
|
|
// If that's not possible (i.e. this array has more than maxChunkSize elements, too)
|
2016-09-26 07:32:16 +00:00
|
|
|
// then we'll delete chunk by chunk.
|
2019-04-22 14:26:28 +00:00
|
|
|
static::deleteByChunk($ids, $key, $maxChunkSize);
|
2016-09-26 07:32:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete records chunk by chunk.
|
|
|
|
*
|
2020-12-22 20:11:22 +00:00
|
|
|
* @param array<string>|array<int> $ids The array of record IDs to delete
|
2021-06-05 10:47:56 +00:00
|
|
|
* @param string $key Name of the primary key
|
|
|
|
* @param int $chunkSize Size of each chunk. Defaults to 2^16-1 (65535)
|
2016-09-26 07:32:16 +00:00
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public static function deleteByChunk(array $ids, string $key = 'id', int $chunkSize = 65535): void
|
2016-09-26 07:32:16 +00:00
|
|
|
{
|
2021-06-05 10:47:56 +00:00
|
|
|
DB::beginTransaction();
|
2017-08-19 15:28:01 +00:00
|
|
|
|
2016-09-26 07:32:16 +00:00
|
|
|
try {
|
2017-06-03 16:35:08 +00:00
|
|
|
foreach (array_chunk($ids, $chunkSize) as $chunk) {
|
2016-09-26 07:32:16 +00:00
|
|
|
static::whereIn($key, $chunk)->delete();
|
|
|
|
}
|
2018-08-24 15:27:19 +00:00
|
|
|
|
2021-06-05 10:47:56 +00:00
|
|
|
DB::commit();
|
2020-12-22 20:11:22 +00:00
|
|
|
} catch (Throwable $e) {
|
2021-06-05 10:47:56 +00:00
|
|
|
DB::rollBack();
|
|
|
|
throw $e;
|
2016-09-26 07:32:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|