This repository has been archived on 2022-09-30. You can view files and clone it, but cannot push or open issues or pull requests.
bookmark-manager/IconDatabase.cs

134 lines
3.8 KiB
C#
Raw Normal View History

2017-07-30 11:59:34 +00:00
using System;
using System.Drawing;
using System.IO;
using RyzStudio.Data.SQLite;
namespace bzit.bomg
{
public class IconDatabase : SQLiteDatabase2
{
public IconDatabase()
{
this.requiredTableList = new string[] { "bzt_app_bomg_icons" };
}
2019-04-07 19:10:23 +00:00
protected override bool prepareDatabase()
2017-07-30 11:59:34 +00:00
{
2019-04-07 19:10:23 +00:00
if (this.DBConnection == null)
2017-07-30 11:59:34 +00:00
{
return false;
}
2017-07-30 11:59:34 +00:00
return this.DoNonQuery(@"
CREATE TABLE bzt_app_bomg_icons
(
ico_id INTEGER PRIMARY KEY,
ico_key TEXT,
ico_hash TEXT,
ico_content BLOB
)
") >= 0;
}
2017-07-30 11:59:34 +00:00
#region public methods
public bool HasIcon(string url)
{
2019-04-07 19:10:23 +00:00
return this.DoQueryExist("SELECT 1 FROM bzt_app_bomg_icons WHERE ico_key='" + escapeValue(url) + "'");
2017-07-30 11:59:34 +00:00
}
/* public bool AddIcon(string url, Image image)
{
if (string.IsNullOrWhiteSpace(url))
{
return false;
}
if (image == null)
{
return false;
}
2017-07-30 11:59:34 +00:00
if (this.HasIcon(url))
{
return this.DoNonQuery("UPDATE bzt_app_bomg_icons SET='" + imageToSQLString(image) + "' WHERE ico_key='" + SQLiteDatabase2.escapeValue(url) + "';") >= 0;
}
else
{
return this.DoNonQuery("INSERT INTO bzt_app_bomg_icons (ico_key, ico_content) VALUES ('" + SQLiteDatabase2.escapeValue(url) + "', '" + imageToSQLString(image) + "');") >= 0;
}
}*/
public bool AddIcon(string url, byte[] image)
{
if (string.IsNullOrWhiteSpace(url))
{
return false;
}
if (image == null)
{
return false;
}
2017-07-30 11:59:34 +00:00
if (this.HasIcon(url))
{
2019-04-07 19:10:23 +00:00
return this.DoNonQuery("UPDATE bzt_app_bomg_icons SET='" + bytesToSQLString(image) + "' WHERE ico_key='" + escapeValue(url) + "';") >= 0;
2017-07-30 11:59:34 +00:00
}
else
{
2019-04-07 19:10:23 +00:00
return this.DoNonQuery("INSERT INTO bzt_app_bomg_icons (ico_key, ico_content) VALUES ('" + escapeValue(url) + "', '" + bytesToSQLString(image) + "');") >= 0;
2017-07-30 11:59:34 +00:00
}
}
public Image GetIcon(string url)
{
if (!this.HasIcon(url))
{
return null;
}
2019-04-07 19:10:23 +00:00
string rs = this.DoQuerySingle("SELECT ico_content FROM bzt_app_bomg_icons WHERE ico_key='" + escapeValue(url) + "'");
2017-07-30 11:59:34 +00:00
return sqlStringToImage(rs);
}
public void DeleteIcon(string url)
{
if (string.IsNullOrWhiteSpace(url))
{
return;
}
2019-04-07 19:10:23 +00:00
this.DoNonQuery("DELETE FROM bzt_app_bomg_icons WHERE ico_key='" + escapeValue(url) + "';");
2017-07-30 11:59:34 +00:00
}
2017-07-30 11:59:34 +00:00
#endregion
2017-07-30 11:59:34 +00:00
protected string imageToSQLString(Image image)
{
MemoryStream stream = new MemoryStream();
image.Save(stream, new System.Drawing.Imaging.ImageFormat(image.RawFormat.Guid));
stream.Close();
2017-07-30 11:59:34 +00:00
byte[] byteArray = stream.ToArray();
2017-07-30 11:59:34 +00:00
return bytesToSQLString(byteArray);
}
protected string bytesToSQLString(byte[] image)
{
return Convert.ToBase64String(image);
}
protected Image sqlStringToImage(string base64_string)
{
2017-07-30 11:59:34 +00:00
byte[] byteArray2 = Convert.FromBase64String(base64_string);
MemoryStream stream2 = new MemoryStream();
stream2.Write(byteArray2, 0, byteArray2.Length);
2017-07-30 11:59:34 +00:00
Image displayImage = Image.FromStream(stream2);
return displayImage;
}
}
}