using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections.Generic; using System.Text.Json; using Microsoft.Data.Sqlite; using System.ComponentModel; namespace ROIEditor { public class ListaPersistente { private readonly string connectionString; private readonly string tableName; private List data = new(); public string Clave; public void Add(T item) { data.Add(item); } public void Remove(T item) { data.Remove(item); } public void Clear() { data.Clear(); } public List GetList() { return data; } public int IndexOf(T item) { return data.IndexOf(item); } public int Count => data.Count; public ListaPersistente(string dbPath, string tableName) { connectionString = $"Data Source={dbPath}"; this.tableName = tableName; InitializeDatabase(); } private void InitializeDatabase() { using (var connection = new SqliteConnection(connectionString)) { connection.Open(); var command = connection.CreateCommand(); command.CommandText = $@" CREATE TABLE IF NOT EXISTS {tableName} ( Id INTEGER PRIMARY KEY AUTOINCREMENT, Clave TEXT, SerializeData TEXT )"; command.ExecuteNonQuery(); } } public void Save() { Save(Clave); } public void Save(string clave) { if (clave == null) return; Clave = clave; using (var connection = new SqliteConnection(connectionString)) { connection.Open(); var deleteCommand = connection.CreateCommand(); deleteCommand.CommandText = $"DELETE FROM {tableName} WHERE Clave = $Clave"; deleteCommand.Parameters.AddWithValue("$Clave", clave); deleteCommand.ExecuteNonQuery(); foreach (var elemento in data) { string serializedData = JsonSerializer.Serialize(elemento); var command = connection.CreateCommand(); command.CommandText = $"INSERT INTO {tableName} (Clave, SerializeData) VALUES ($Clave, $SerializedData)"; command.Parameters.AddWithValue("$Clave", clave); command.Parameters.AddWithValue("$SerializedData", serializedData); command.ExecuteNonQuery(); } } } public void Load() { Load(Clave); } public void Load(string clave) { data.Clear(); if (clave == null) return; Clave = clave; using (var connection = new SqliteConnection(connectionString)) { connection.Open(); var command = connection.CreateCommand(); command.CommandText = $"SELECT SerializeData FROM {tableName} WHERE Clave = $Clave"; command.Parameters.AddWithValue("$Clave", clave); using (var reader = command.ExecuteReader()) { while (reader.Read()) { string serializedData = reader.GetString(0); data.Add( JsonSerializer.Deserialize(serializedData) ); } } } } } public class ItemPersistente where T : new() { private readonly string connectionString; private readonly string tableName; private T data; // Ahora 'data' es un objeto de tipo T public string Clave; public ItemPersistente(string dbPath, string tableName) { connectionString = $"Data Source={dbPath}"; this.tableName = tableName; InitializeDatabase(); data = new T(); // Inicializa 'data' con el constructor por defecto de T } private void InitializeDatabase() { using (var connection = new SqliteConnection(connectionString)) { connection.Open(); var command = connection.CreateCommand(); command.CommandText = $@" CREATE TABLE IF NOT EXISTS {tableName} ( Id INTEGER PRIMARY KEY AUTOINCREMENT, Clave TEXT UNIQUE, SerializedData TEXT )"; command.ExecuteNonQuery(); } } public void Save() { Save(Clave); } public void Save(string clave) { if (clave == null) return; Clave = clave; string serializedData = JsonSerializer.Serialize(data); using (var connection = new SqliteConnection(connectionString)) { connection.Open(); var command = connection.CreateCommand(); command.CommandText = $@" INSERT INTO {tableName} (Clave, SerializedData) VALUES ($Clave, $SerializedData) ON CONFLICT(Clave) DO UPDATE SET SerializedData = excluded.SerializedData"; command.Parameters.AddWithValue("$Clave", clave); command.Parameters.AddWithValue("$SerializedData", serializedData); command.ExecuteNonQuery(); } } public bool Load() { return Load(Clave); } public bool Load(string clave) { if (clave == null) return false; Clave = clave; using (var connection = new SqliteConnection(connectionString)) { connection.Open(); var command = connection.CreateCommand(); command.CommandText = $"SELECT SerializedData FROM {tableName} WHERE Clave = $Clave"; command.Parameters.AddWithValue("$Clave", clave); using (var reader = command.ExecuteReader()) { if (reader.Read()) { string serializedData = reader.GetString(0); data = JsonSerializer.Deserialize(serializedData); return true; } data = new(); return false; } } } public T Get() { return data; } public void Set(T newData) { data = newData; } } }