using Microsoft.Data.Sqlite; using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; namespace BookmarkManager { public class IconDatabase { protected string dbFilename = null; protected SqliteConnection dbConnection = null; public IconDatabase(string filename) { if (string.IsNullOrWhiteSpace(filename)) return; dbFilename = filename; bool isNew = !File.Exists(dbFilename); SqliteConnectionStringBuilder builder = new SqliteConnectionStringBuilder(); builder.DataSource = dbFilename; builder.Cache = SqliteCacheMode.Private; builder.Mode = SqliteOpenMode.ReadWriteCreate; dbConnection = new SqliteConnection(builder.ToString()); dbConnection.Open(); if (isNew) Initialise(); //if (File.Exists(filename)) //{ // File.Delete("test.db3"); //} } public bool AddIcon(string id, Image image) { if (dbConnection == null) return false; if (string.IsNullOrWhiteSpace(id)) return false; if (image == null) return DeleteIcon(id); if (HasIcon(id)) return UpdateIcon(id, image); SqliteCommand command = new SqliteCommand(); command.Connection = dbConnection; command.CommandText = "INSERT INTO icons (id, content) VALUES (@id, @content)"; command.Parameters.Add("@id", SqliteType.Text).Value = id; command.Parameters.Add("@content", SqliteType.Blob, 20).Value = ImageToBytes(image); try { command.ExecuteNonQuery(); } catch (Exception) { return false; } return true; } public void Close() { if (dbConnection == null) return; dbConnection.Close(); dbConnection.Dispose(); dbConnection = null; } public bool DeleteIcon(string id) { if (dbConnection == null) return false; if (string.IsNullOrWhiteSpace(id)) return false; SqliteCommand command = new SqliteCommand(); command.Connection = dbConnection; command.CommandText = "DELETE FROM icons WHERE id = @id"; command.Parameters.Add("@id", SqliteType.Text).Value = id; try { command.ExecuteNonQuery(); } catch (Exception) { return false; } return true; } public Image FindIcon(string id) { if (dbConnection == null) return null; if (string.IsNullOrWhiteSpace(id)) return null; byte[] iconBytes = null; SqliteCommand command = new SqliteCommand(); command.Connection = dbConnection; command.CommandText = "SELECT content FROM icons WHERE @id"; command.Parameters.Add("@id", SqliteType.Text).Value = id; try { using (var reader = command.ExecuteReader()) { while (reader.Read()) { if (reader["content"] != null && !Convert.IsDBNull(reader["content"])) { iconBytes = (byte[])reader["content"]; } } } command.Dispose(); } catch (Exception) { return null; } if (iconBytes == null) { return null; } Image img = null; try { img = Image.FromStream(new MemoryStream(iconBytes)); } catch (Exception) { return null; } return new Bitmap(img, 16, 16); } public bool HasIcon(string id) { if (dbConnection == null) return false; if (string.IsNullOrWhiteSpace(id)) return false; int rv = 0; SqliteCommand command = new SqliteCommand(); command.Connection = dbConnection; command.CommandText = "SELECT COUNT(1) AS count FROM icons WHERE @id"; command.Parameters.Add("@id", SqliteType.Text).Value = id; try { using (var reader = command.ExecuteReader()) { while (reader.Read()) { if (reader["count"] != null && !Convert.IsDBNull(reader["count"])) { rv = (int)reader["count"]; } } } command.Dispose(); } catch (Exception) { return false; } return (rv > 0); } public bool UpdateIcon(string id, Image image) { if (dbConnection == null) return false; if (string.IsNullOrWhiteSpace(id)) return false; if (image == null) return DeleteIcon(id); if (!HasIcon(id)) return AddIcon(id, image); SqliteCommand command = new SqliteCommand(); command.Connection = dbConnection; command.CommandText = "UPDATE icons SET content = @content WHERE id = @id"; command.Parameters.Add("@id", SqliteType.Text).Value = id; command.Parameters.Add("@content", SqliteType.Blob, 20).Value = ImageToBytes(image); try { command.ExecuteNonQuery(); } catch (Exception) { return false; } return true; } protected bool Initialise() { if (dbConnection == null) return false; try { SqliteCommand command = new SqliteCommand("CREATE TABLE icons (id TEXT PRIMARY KEY, content BLOB)", dbConnection); command.ExecuteNonQuery(); command.Dispose(); } catch (Exception) { return false; } return true; } protected byte[] ImageToBytes(Image image) { MemoryStream stream = new MemoryStream(); //image.Save(stream, ((image.RawFormat == null) ? ImageFormat.Bmp : new ImageFormat(image.RawFormat.Guid))); image.Save(stream, ImageFormat.Bmp); stream.Close(); return stream.ToArray(); } } }