koel/app/Traits/SupportsDeleteWhereIDsNotIn.php

80 lines
2.5 KiB
PHP
Raw Normal View History

2016-09-26 07:32:16 +00:00
<?php
namespace App\Traits;
use Exception;
2018-08-31 13:47:15 +00:00
use Illuminate\Database\DatabaseManager;
2019-08-05 10:56:48 +00:00
use Illuminate\Database\Eloquent\Builder;
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.
*
2018-08-29 07:07:44 +00:00
* @param string[]|int[] $ids The array of IDs.
* @param string $key Name of the primary key.
2016-09-26 07:32:16 +00:00
*
2018-08-24 15:27:19 +00:00
* @throws Exception
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
{
$maxChunkSize = config('database.default') === 'sqlite-persistent' ? 999 : 65535;
// 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.
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
}
// 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.
static::deleteByChunk($ids, $key, $maxChunkSize);
2016-09-26 07:32:16 +00:00
}
/**
* Delete records chunk by chunk.
*
2018-08-29 07:07:44 +00:00
* @param string[]|int[] $ids The array of record IDs to delete
* @param string $key Name of the primary key
* @param int $chunkSize Size of each chunk. Defaults to 2^16-1 (65535)
2016-11-24 04:39:17 +00:00
*
2018-08-24 15:27:19 +00:00
* @throws Exception
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
{
2018-08-31 13:47:15 +00:00
/** @var DatabaseManager $db */
$db = app(DatabaseManager::class);
$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
2018-08-31 13:47:15 +00:00
$db->commit();
2016-09-26 07:32:16 +00:00
} catch (Exception $e) {
2018-08-31 13:47:15 +00:00
$db->rollBack();
2016-09-26 07:32:16 +00:00
}
}
}