// 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; 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, IAsyncDisposable { /// /// The tag used for logging. /// private static readonly string LOGTAG = Logging.Log.LogTagFromType(typeof(TemporaryDbValueList)); /// /// The command to use. /// private SqliteCommand? _cmd; /// /// The connection to use. /// private SqliteConnection _con; /// /// The reusable transaction to use. /// private ReusableTransaction _rtr; /// /// The table to use. /// private readonly string _tableName = $"TemporaryList-{Library.Utility.Utility.GetHexGuid()}"; /// /// 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 TemporaryDbValueList with the given values. /// /// The connection to use. /// The reusable transaction to use. /// The values to use. /// The type of the values (e.g., "INTEGER", "TEXT"). private TemporaryDbValueList(SqliteConnection con, ReusableTransaction rtr, IEnumerable values, string valuesType) { _con = con; _rtr = rtr; _valuesType = valuesType; _values = values; ArgumentNullException.ThrowIfNull(values); } /// /// Asynchronously creates a new TemporaryDbValueList with the given values. /// /// The database to use. /// The values to use. /// A cancellation token to cancel the operation. /// A task that when awaited contains the created TemporaryDbValueList. internal static async Task CreateAsync(LocalDatabase db, IEnumerable values, CancellationToken token) { return await DoCreateAsync(new TemporaryDbValueList(db.Connection, db.Transaction, values.Cast(), "INTEGER"), token) .ConfigureAwait(false); } /// /// Asynchronously creates a new TemporaryDbValueList with the given values. /// /// The database to use. /// The values to use. /// A cancellation token to cancel the operation. /// A task that when awaited contains the created TemporaryDbValueList. internal static async Task CreateAsync(LocalDatabase db, IEnumerable values, CancellationToken token) { return await DoCreateAsync(new TemporaryDbValueList(db.Connection, db.Transaction, values.Cast(), "TEXT"), token) .ConfigureAwait(false); } /// /// Asynchronously creates a new TemporaryDbValueList with the given values. /// /// The connection to use. /// The reusable transaction to use. /// The values to use. /// A cancellation token to cancel the operation. /// A task that when awaited contains the created TemporaryDbValueList. internal static async Task CreateAsync(SqliteConnection con, ReusableTransaction rtr, IEnumerable values, CancellationToken token) { return await DoCreateAsync(new TemporaryDbValueList(con, rtr, values.Cast(), "INTEGER"), token) .ConfigureAwait(false); } /// /// Asynchronously creates a new TemporaryDbValueList with the given values. /// /// The connection to use. /// The reusable transaction to use. /// The values to use. /// A cancellation token to cancel the operation. /// A task that when awaited contains the created TemporaryDbValueList. internal static async Task CreateAsync(SqliteConnection con, ReusableTransaction rtr, IEnumerable values, CancellationToken token) { return await DoCreateAsync(new TemporaryDbValueList(con, rtr, values.Cast(), "TEXT"), token) .ConfigureAwait(false); } /// /// Performs the actual creation of the TemporaryDbValueList, ensuring that if the number of values exceeds the chunk size, /// the values are forced into a table. /// /// The TemporaryDbValueList to create. /// A cancellation token to cancel the operation. /// A task that when awaited contains the created TemporaryDbValueList. private static async Task DoCreateAsync(TemporaryDbValueList valueList, CancellationToken token) { if (valueList._values.Count() > LocalDatabase.CHUNK_SIZE) await valueList.ForceToTable(token).ConfigureAwait(false); 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. /// /// A cancellation token to cancel the operation. /// A task that when awaited contains the SQL in clause string. public async Task GetInClause(CancellationToken token) { if (_disposed) throw new ObjectDisposedException(nameof(TemporaryDbValueList)); await ForceToTable(token).ConfigureAwait(false); return $@" SELECT ""Value"" FROM ""{_tableName}"" "; } /// /// Force the values to be written to a table, if not already done. /// /// A cancellation token to cancel the operation. /// A task that completes when the table has been created and the values inserted. public async Task ForceToTable(CancellationToken token) { if (_disposed) throw new ObjectDisposedException(nameof(TemporaryDbValueList)); if (_isTable) return; _cmd = _con.CreateCommand(_rtr); await _cmd.ExecuteNonQueryAsync($@" CREATE TEMPORARY TABLE ""{_tableName}"" (""Value"" {_valuesType}) ", token) .ConfigureAwait(false); _isTable = true; foreach (var slice in _values.Chunk(LocalDatabase.CHUNK_SIZE)) { var parameterNames = slice.Select((_, i) => $"@p{Library.Utility.Utility.FormatInvariantValue(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().ConfigureAwait(false); } } /// /// The table name to use. /// public string TableName => _tableName; /// public void Dispose() { DisposeAsync().AsTask().Await(); } /// public async ValueTask DisposeAsync() { if (_disposed) return; _disposed = true; if (!_isTable) return; try { if (_cmd != null) { await _cmd.ExecuteNonQueryAsync($@"DROP TABLE ""{_tableName}""", default) .ConfigureAwait(false); await _cmd.DisposeAsync().ConfigureAwait(false); } _cmd = null; } catch (Exception ex) { Logging.Log.WriteErrorMessage(LOGTAG, "DropTableFailed", ex, "Failed to drop temporary table {0}: {1}", _tableName, ex.Message); } } }