// Copyright (C) 2025, The Duplicati Team // https://duplicati.com, hello@duplicati.com // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Threading.Tasks; using Duplicati.Library.Utility; using Microsoft.Data.Sqlite; #nullable enable namespace Duplicati.Library.Main.Database; /// /// A list of values that can be used in a query /// internal class TemporaryDbValueList : IDisposable { /// /// The tag used for logging /// private static readonly string LOGTAG = Logging.Log.LogTagFromType(typeof(TemporaryDbValueList)); /// /// The command to use /// private SqliteCommand? _cmd; private readonly LocalDatabase _db; /// /// The table to use /// private readonly string _tableName = $"TemporaryList-{Guid.NewGuid():N}"; /// /// Flag indicating if the object has been disposed /// private bool _disposed = false; /// /// Flag indicating if the values are in a table /// private bool _isTable = false; /// /// The values to use /// private readonly IEnumerable _values; /// /// The type of the values /// private readonly string _valuesType; /// /// Creates a new reusable transaction /// /// The command to use /// The connection to use /// The values to use private TemporaryDbValueList(LocalDatabase db, IEnumerable values, string valuesType) { _db = db; _valuesType = valuesType; _values = values; ArgumentNullException.ThrowIfNull(values); } internal static async Task CreateAsync(LocalDatabase db, IEnumerable values) { return await DoCreateAsync(new TemporaryDbValueList(db, values.Cast(), "INTEGER")); } internal static async Task CreateAsync(LocalDatabase db, IEnumerable values) { return await DoCreateAsync(new TemporaryDbValueList(db, values.Cast(), "TEXT")); } private static async Task DoCreateAsync(TemporaryDbValueList valueList) { if (valueList._values.Count() > LocalDatabase.CHUNK_SIZE) await valueList.ForceToTable(); return valueList; } /// /// The values in the list /// public IEnumerable Values => _values; /// /// Flag indicating if the table has been created /// public bool IsTableCreated => _isTable; /// /// Get the in clause for the values, creating the table if needed /// /// The in clause for the values public async Task GetInClause() { if (_disposed) throw new ObjectDisposedException(nameof(TemporaryDbValueList)); await ForceToTable(); return $@" SELECT ""Value"" FROM ""{_tableName}"" "; } /// /// Force the values to be written to a table, if not already done /// public async Task ForceToTable() { if (_disposed) throw new ObjectDisposedException(nameof(TemporaryDbValueList)); if (_isTable) return; _cmd = _db.Connection.CreateCommand(_db.Transaction); await _cmd.ExecuteNonQueryAsync($@" CREATE TEMPORARY TABLE ""{_tableName}"" (""Value"" {_valuesType}) "); _isTable = true; foreach (var slice in _values.Chunk(LocalDatabase.CHUNK_SIZE)) { var parameterNames = slice.Select((_, i) => $"@p{i}").ToArray(); var sql = $@" INSERT INTO ""{_tableName}"" (""Value"") VALUES {string.Join(", ", parameterNames.Select(p => $"({p})"))} "; _cmd.SetCommandAndParameters(sql); for (int i = 0; i < slice.Length; i++) _cmd.SetParameterValue(parameterNames[i], slice[i]); await _cmd.ExecuteNonQueryAsync(); } } /// /// The table name to use /// public string TableName => _tableName; /// public void Dispose() { DisposeAsync().Await(); } public async Task DisposeAsync() { if (_disposed) return; _disposed = true; if (!_isTable) return; try { if (_cmd != null) { await _cmd.ExecuteNonQueryAsync($@"DROP TABLE ""{_tableName}"""); await _cmd.DisposeAsync(); } _cmd = null; } catch (Exception ex) { Logging.Log.WriteErrorMessage(LOGTAG, "DropTableFailed", ex, "Failed to drop temporary table {0}: {1}", _tableName, ex.Message); } } }