Updated: icon database helper

This commit is contained in:
Ray 2019-04-07 20:10:23 +01:00
parent aecc5ac043
commit 48416abccb
8 changed files with 1147 additions and 1127 deletions

View File

@ -317,7 +317,7 @@ namespace bzit.bomg
} }
// load favicon image // load favicon image
if (!string.IsNullOrEmpty(this.IconAddress)) if (!string.IsNullOrWhiteSpace(this.IconAddress))
{ {
try try
{ {

View File

@ -12,9 +12,9 @@ namespace bzit.bomg
this.requiredTableList = new string[] { "bzt_app_bomg_icons" }; this.requiredTableList = new string[] { "bzt_app_bomg_icons" };
} }
protected override bool Prepare() protected override bool prepareDatabase()
{ {
if (dbConnection == null) if (this.DBConnection == null)
{ {
return false; return false;
} }
@ -34,7 +34,7 @@ namespace bzit.bomg
public bool HasIcon(string url) public bool HasIcon(string url)
{ {
return this.DoQueryExist("SELECT 1 FROM bzt_app_bomg_icons WHERE ico_key='" + SQLiteDatabase2.EscapeValue(url) + "'"); return this.DoQueryExist("SELECT 1 FROM bzt_app_bomg_icons WHERE ico_key='" + escapeValue(url) + "'");
} }
/* public bool AddIcon(string url, Image image) /* public bool AddIcon(string url, Image image)
@ -73,11 +73,11 @@ namespace bzit.bomg
if (this.HasIcon(url)) if (this.HasIcon(url))
{ {
return this.DoNonQuery("UPDATE bzt_app_bomg_icons SET='" + bytesToSQLString(image) + "' WHERE ico_key='" + SQLiteDatabase2.EscapeValue(url) + "';") >= 0; return this.DoNonQuery("UPDATE bzt_app_bomg_icons SET='" + bytesToSQLString(image) + "' WHERE ico_key='" + escapeValue(url) + "';") >= 0;
} }
else else
{ {
return this.DoNonQuery("INSERT INTO bzt_app_bomg_icons (ico_key, ico_content) VALUES ('" + SQLiteDatabase2.EscapeValue(url) + "', '" + bytesToSQLString(image) + "');") >= 0; return this.DoNonQuery("INSERT INTO bzt_app_bomg_icons (ico_key, ico_content) VALUES ('" + escapeValue(url) + "', '" + bytesToSQLString(image) + "');") >= 0;
} }
} }
@ -88,7 +88,7 @@ namespace bzit.bomg
return null; return null;
} }
string rs = this.DoQuerySingle("SELECT ico_content FROM bzt_app_bomg_icons WHERE ico_key='" + SQLiteDatabase2.EscapeValue(url) + "'"); string rs = this.DoQuerySingle("SELECT ico_content FROM bzt_app_bomg_icons WHERE ico_key='" + escapeValue(url) + "'");
return sqlStringToImage(rs); return sqlStringToImage(rs);
} }
@ -99,7 +99,7 @@ namespace bzit.bomg
return; return;
} }
this.DoNonQuery("DELETE FROM bzt_app_bomg_icons WHERE ico_key='" + SQLiteDatabase2.EscapeValue(url) + "';"); this.DoNonQuery("DELETE FROM bzt_app_bomg_icons WHERE ico_key='" + escapeValue(url) + "';");
} }
#endregion #endregion

1245
MainForm.Designer.cs generated

File diff suppressed because it is too large Load Diff

View File

@ -32,32 +32,40 @@ namespace bzit.bomg
// toolbar // toolbar
viewHelpHelpMenuItem.Enabled = File.Exists(Path.ChangeExtension(Application.ExecutablePath, ".chm")); viewHelpHelpMenuItem.Enabled = File.Exists(Path.ChangeExtension(Application.ExecutablePath, ".chm"));
string iconDBPath = Path.ChangeExtension(Application.ExecutablePath, ".db"); string iconDBPath = Path.ChangeExtension(Application.ExecutablePath, ".db");
bool rv = false;
if (File.Exists(iconDBPath))
{
rv = this.IconDatabase.LoadFile(iconDBPath);
if (!rv)
{
rv = this.IconDatabase.CreateSpecial(iconDBPath, true);
if (!rv)
{
MessageBox.Show("Can not find icon database. [" + this.IconDatabase.LastError + "]");
this.Close();
}
}
}
else
{
rv = this.IconDatabase.CreateSpecial(iconDBPath, true);
if (!rv)
{
MessageBox.Show("Can not find icon database. [" + this.IconDatabase.LastError + "]");
this.Close();
}
}
treeView1.IconDatabase = this.IconDatabase; bool rs = this.IconDatabase.Create(Path.ChangeExtension(Application.ExecutablePath, "db"), false, null, true);
if (!rs)
{
MessageBox.Show("Can not find icon database. [" + this.IconDatabase.LastError + "]");
this.Close();
}
//bool rv = false;
// if (File.Exists(iconDBPath))
// {
// rv = this.IconDatabase.LoadFile(iconDBPath);
// if (!rv)
// {
// rv = this.IconDatabase.CreateSpecial(iconDBPath, true);
// if (!rv)
// {
// MessageBox.Show("Can not find icon database. [" + this.IconDatabase.LastError + "]");
// this.Close();
// }
// }
// }
// else
// {
// rv = this.IconDatabase.CreateSpecial(iconDBPath, true);
// if (!rv)
// {
// MessageBox.Show("Can not find icon database. [" + this.IconDatabase.LastError + "]");
// this.Close();
// }
// }
treeView1.IconDatabase = this.IconDatabase;
sessionFileFormat = new SessionFileFormat(this); sessionFileFormat = new SessionFileFormat(this);
this.ApplicationMode = AppMode.Clear; this.ApplicationMode = AppMode.Clear;
@ -440,8 +448,8 @@ namespace bzit.bomg
*/ */
private void optionsToolStripMenuItem1_Click(object sender, EventArgs e) private void optionsToolStripMenuItem1_Click(object sender, EventArgs e)
{ {
OptionsForm frm = new OptionsForm(this); //OptionsForm frm = new OptionsForm(this);
frm.ShowDialog(); //frm.ShowDialog();
} }
/** /**

View File

@ -131,7 +131,7 @@
AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w
LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0 LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0
ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAADU ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAADU
FwAAAk1TRnQBSQFMAgEBBwEAAVwBAgFcAQIBEAEAARABAAT/AREBAAj/AUIBTQE2BwABNgMAASgDAAFA FwAAAk1TRnQBSQFMAgEBBwEAAWQBAgFkAQIBEAEAARABAAT/AREBAAj/AUIBTQE2BwABNgMAASgDAAFA
AwABIAMAAQEBAAEQBgABEBQAAZkBSgEYAS4BtwEdAbcBGQG3ARkBtwEZAbcBGQG3ARkBtwEZAbcBGQG3 AwABIAMAAQEBAAEQBgABEBQAAZkBSgEYAS4BtwEdAbcBGQG3ARkBtwEZAbcBGQG3ARkBtwEZAbcBGQG3
ARkBtwEdARcBLgGZAUoEAAGZAUoBGAEuAbcBHQG3ARkBtwEZAbcBGQG3ARkBtwEZAbcBGQG3ARkBtwEZ ARkBtwEdARcBLgGZAUoEAAGZAUoBGAEuAbcBHQG3ARkBtwEZAbcBGQG3ARkBtwEZAbcBGQG3ARkBtwEZ
AbcBHQEXAS4BmQFKBAABmQFKARgBLgG3AR0BtwEZAbcBGQG3ARkBtwEZAbcBGQG3ARkBtwEZAbcBGQG3 AbcBHQEXAS4BmQFKBAABmQFKARgBLgG3AR0BtwEZAbcBGQG3ARkBtwEZAbcBGQG3ARkBtwEZAbcBGQG3
@ -268,15 +268,15 @@
<data name="newMenuBarItem.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> <data name="newMenuBarItem.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value> <value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8 iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAHaSURBVDhPjZM9SxxRFIY1Vv4HhVRCQIK4smUQQgpBiwRT YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAHYSURBVDhPjZO7SxxRFMZNrPI/KKQShBDElS1FEAtBiwQt
SCBgigTSJYpFSsVKG1mWJU1IJ1gI6W1EQsQQd1ZD/ABFLHbdL3d25+PeOzP6es7FGWfcDcmBhy3mvM++ QiCQFAnY+cAiZcRKm7AsS5qQLpAiYJ9GJEQiOoOKL1DEYtd9ubM7j3vvzOiXcy7OOONuSA782GLO99vv
9+5sV2z6iBSR/gcPiQdE26Rc1zUNw7gKggCdkFIik8ms0G5HSZrDtm3DcZyIZrOJRqOBcrmsJaVSCdls 3p3tSkwPkSGy/+Ax8ZBom4zneZZhGNdhGKITUkrkcrmPtNtRkuWw4zhwXTem2Wyi0WigXC5rSalUQj6f
tk3STaR5QQgBpZT+ZCzLgmmaqFQqWkAtwV+Sy+VCiR4t8JsG5OVPLQglHGi1WqhWq4njMJzRaRotEEcf b5M8ILK8IISAUkp/MrZtw7IsVCoVLaCW4C8pFAqRRI8WBE0T8mpLCyIJB1qtFqrVauo4DGd0mkYLxPEc
If68ixZ839fnDo9Sq9V0k2KxeCe4Pv/Qa+2/Gt1df/pFHryF3JuCPF2CV/+OwLMTEm7C91Gv1+8E8vDN xMG7eCEIAn3u6Ci1Wk03KRaLd4Kbi9lH9t7LkZ3vo5/l4VvI3ReQZyvw6z8R+k5Kwk34Pur1+p1AHr35
Z3X4HvJ4Bt7ZEtTJPMTuc9hbwxD7c4kmfBy+ExZFAuBlj7v34ps8niXBMgkW4BqTcPPTCHylFxnP8/S9 pI5mIE/m4Z+vQJ1+gNh5BmdjEGJvMdWEj8N3wqJYAEx3e7vP1+TJAglWSbAEz5yCZ7xGGCi9yPi+r++F
sISJBDyXvyYeC2Mc6nQB6ugT7M1BeObvKMxwi/ByuU1CYO88GxP5CTg/hmBtPaL6KaiLDR0K4QYMh9sF JUws4LnannwqzAmosyWo4/dw1p/At/bjMMMtosvlNimB83tsXBiTcH8NwN7op/oZqMsfOhTBDRgOtws2
209e51cHvgayBp8QB4uQ52tt4TgJAU30It0n3iIOP+OMTtNoQaFQuL4v+Bu0e8UZnb6dEfp5HH7QKRCH h18ZX/u+hLKGgBCHy5AX39rCSVICmvhFuk+yRRJ+xhmdptEC0zRv7gv+Bu1ec0anb2eIfh6XH3QKJOEd
d+h1tijD/95o+okRgq3/A4W7+m8ApNSdQdLieiIAAAAASUVORK5CYII= ep1tyvC/N55eYohg6/9A4a7eP4kvnS+BaQv8AAAAAElFTkSuQmCC
</value> </value>
</data> </data>
<metadata name="treeMenuRoot.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <metadata name="treeMenuRoot.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">

View File

@ -8,440 +8,462 @@ using System.Text;
namespace RyzStudio.Data.SQLite namespace RyzStudio.Data.SQLite
{ {
public class SQLiteDatabase2 public class SQLiteDatabase2
{ {
#region static methods public SQLiteConnection DBConnection { get; protected set; } = null;
public static string EscapeSQL(string query) public string DBLocation { get; protected set; } = null;
{
return query.Replace("'", "''").Trim(); public string LastError { get; protected set; } = null;
}
public int LastInsertID
public static string EscapeValue(string text) {
{ get
return text.Replace("\"", "\\\"").Replace("\t", "\\t").Replace("\r", " \\r").Replace("\n", "\\n"); {
} if (this.DBConnection == null)
{
public static string PrepareQuery(string query, params string[] arguments) return 0;
{ }
string rv = query;
DataTable dt = this.DoQuery("SELECT last_insert_rowid() AS rv;");
if (string.IsNullOrEmpty(rv)) if (dt == null)
{ {
return string.Empty; return 0;
} }
for (int i = 0; i < arguments.Length; i++) if (dt.Rows.Count <= 0)
{ {
rv = rv.Replace("[^" + (i + 1).ToString() + "]", EscapeSQL(arguments[i])); return 0;
} }
return rv; int n;
} if (!int.TryParse(dt.Rows[0]["rv"].ToString(), out n))
{
public static string Encode64(string text) { return Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(text)); } n = 0;
}
#endregion
return n;
protected SQLiteConnection dbConnection = null; }
protected string dbLocation = string.Empty; }
protected string lastError = "";
protected string[] requiredTableList = new string[0];
protected string[] requiredTableList = new string[0];
protected const string appSettingsTableName = "ryz_app_xxxx_config";
protected const string tableNameConfig = "ryz_app_xxxx_config";
public bool Create()
#region public properties {
this.LastError = string.Empty;
[Browsable(false)]
public SQLiteConnection DBConnection this.DBLocation = ":memory:";
{
get { return dbConnection; } try
set { dbConnection = value; } {
} this.DBConnection = new SQLiteConnection(string.Concat("Data Source=\"", this.DBLocation, "\";Version=3;UTF8Encoding=True;"));
this.DBConnection.Open();
[Browsable(false)] }
public string DBLocation catch (Exception exc)
{ {
get { return dbLocation; } this.LastError = exc.Message;
set { dbLocation = value; }
} return false;
}
public string LastError { get { return lastError; } }
return true;
public int LastInsertID }
{
get public bool Create(string filename, bool overwriteFile = false, string password = null, bool useAppSettings = false)
{ {
if (dbConnection == null) bool rs = create(filename, overwriteFile, password);
{
return 0; if (useAppSettings)
} {
if (!rs)
DataTable dt = this.DoQuery("SELECT last_insert_rowid() AS ccc;"); {
if (dt == null) return false;
{ }
return 0;
} rs = prepareDatabase();
if (!rs)
if (dt.Rows.Count <= 0) {
{ return false;
return 0; }
}
return this.HasRequiredTables();
return int.Parse(dt.Rows[0]["ccc"].ToString()); }
} else
} {
return rs;
#endregion }
}
#region public methods
public bool LoadFile(string filename, string password = null)
public bool CreateMemory() {
{ this.LastError = string.Empty;
lastError = string.Empty;
if (!File.Exists(filename))
dbLocation = ":memory:"; {
return false;
try }
{
dbConnection = new SQLiteConnection(string.Concat("Data Source=\"", dbLocation, "\";Version=3;UTF8Encoding=True;")); this.DBLocation = filename;
dbConnection.Open();
} try
catch (Exception exc) {
{ this.DBConnection = new SQLiteConnection(string.Concat("Data Source=\"", filename, "\";Version=3;UTF8Encoding=True;", (password == null) ? string.Empty : string.Concat("Password=", encode64(password), ";")));
lastError = exc.Message; this.DBConnection.Open();
return false; }
} catch (Exception exc)
{
return true; this.LastError = exc.Message;
}
return false;
public bool CreateSpecial(string filename, bool overwriteFile = false, string password = null) }
{
bool rs = this.CreateFile(filename, overwriteFile, password); return true;
if (!rs) }
{
return false; public void Close()
} {
this.LastError = string.Empty;
rs = this.Prepare();
if (!rs) if (this.DBConnection != null)
{ {
return false; try
} {
this.DBConnection.Cancel();
return this.CheckRequiredTables(); this.DBConnection.Close();
} this.DBConnection.Dispose();
this.DBConnection = null;
public bool CreateFile(string filename, bool overwriteFile = false, string password = null)
{ SQLiteConnection.ClearAllPools();
if (File.Exists(filename)) GC.Collect();
{ }
if (overwriteFile) catch (Exception exc)
{ {
try this.LastError = exc.Message;
{ }
File.Delete(filename); }
} }
catch
{ public DataTable DoQuery(string query)
return false; {
} this.LastError = string.Empty;
}
else if (this.DBConnection == null)
{ {
return false; return null;
} }
}
try
try {
{ SQLiteCommand command = new SQLiteCommand(query, this.DBConnection);
SQLiteConnection.CreateFile(filename); SQLiteDataReader dr = command.ExecuteReader();
}
catch DataTable dt = new DataTable();
{ dt.Load(dr);
return false;
} return dt;
}
return this.LoadFile(filename, password); catch (Exception exc)
} {
this.LastError = exc.Message;
public bool LoadFile(string filename, string password = null)
{ return null;
lastError = string.Empty; }
}
if (!File.Exists(filename))
{ public DataTable DoQuery(string query, params string[] args) => DoQuery(prepareQuery(query, args));
return false;
} public int DoQueryCount(string query)
{
dbLocation = filename; this.LastError = string.Empty;
try if (this.DBConnection == null)
{ {
dbConnection = new SQLiteConnection(string.Concat("Data Source=\"", filename, "\";Version=3;UTF8Encoding=True;", (password == null) ? string.Empty : string.Concat("Password=", Encode64(password), ";"))); return -1;
dbConnection.Open(); }
}
catch (Exception exc) DataTable dt = this.DoQuery(query);
{ if (dt == null)
lastError = exc.Message; {
return false; return -1;
} }
return true; return dt.Rows.Count;
} }
public void Close() public int DoQueryCount(string query, params string[] args) => this.DoQueryCount(prepareQuery(query, args));
{
if (dbConnection != null) public bool DoQueryExist(string query) => (this.DoQueryCount(query) > 0);
{
try public bool DoQueryExist(string query, params string[] args) => this.DoQueryExist(prepareQuery(query, args));
{
dbConnection.Close(); public string DoQuerySingle(string query)
} {
catch this.LastError = string.Empty;
{
// do nothing if (this.DBConnection == null)
} {
} return string.Empty;
} }
#region query DataTable dt = this.DoQuery(query);
public DataTable DoQuery(string query) if (dt == null)
{ {
lastError = string.Empty; return string.Empty;
}
if (dbConnection == null)
{ if (dt.Columns.Count <= 0)
return null; {
} return string.Empty;
}
try
{ if (dt.Rows.Count <= 0)
SQLiteCommand command = new SQLiteCommand(query, dbConnection); {
SQLiteDataReader dr = command.ExecuteReader(); return string.Empty;
}
DataTable dt = new DataTable();
dt.Load(dr); if (dt.Rows[0][0] is byte[])
{
return dt; return Encoding.UTF8.GetString(dt.Rows[0][0] as byte[]);
} }
catch (Exception exc) else
{ {
lastError = exc.Message; return dt.Rows[0][0].ToString();
return null; }
} }
}
public string DoQuerySingle(string query, params string[] args) => this.DoQuerySingle(prepareQuery(query, args));
public DataTable DoQuery(string query, params string[] args)
{ public int DoNonQuery(string query)
string sql = SQLiteDatabase2.PrepareQuery(query, args); {
this.LastError = string.Empty;
return DoQuery(sql);
} if (this.DBConnection == null)
{
public int DoQueryCount(string query) return -1;
{ }
lastError = string.Empty;
int rv = 0;
if (dbConnection == null)
{ try
return -1; {
} SQLiteCommand command = new SQLiteCommand(query, this.DBConnection);
rv = command.ExecuteNonQuery();
DataTable dt = this.DoQuery(query); }
if (dt == null) catch (Exception exc)
{ {
return -1; this.LastError = exc.Message;
} rv = -1;
}
return dt.Rows.Count;
} return rv;
}
public int DoQueryCount(string query, params string[] args)
{ public int DoNonQuery(string query, params string[] args) => this.DoNonQuery(prepareQuery(query, args));
string sql = SQLiteDatabase2.PrepareQuery(query, args);
public bool HasTable(string tableName)
return this.DoQueryCount(sql); {
} this.LastError = string.Empty;
public bool DoQueryExist(string query) if (this.DBConnection == null)
{ {
int rv = this.DoQueryCount(query); return false;
}
return (rv > 0);
} int rv = this.DoQueryCount("SELECT 1 FROM sqlite_master WHERE type='table' AND name='" + escapeSQL(tableName) + "'");
public bool DoQueryExist(string query, params string[] args) return (rv > 0);
{ }
string sql = SQLiteDatabase2.PrepareQuery(query, args);
public bool HasRequiredTables()
return this.DoQueryExist(sql); {
} bool rv = true;
foreach (string tbl in requiredTableList)
public string DoQuerySingle(string query) {
{ if (string.IsNullOrEmpty(tbl))
lastError = string.Empty; {
continue;
if (dbConnection == null) }
{
return string.Empty; if (!this.HasTable(tbl))
} {
rv = false;
DataTable dt = this.DoQuery(query); break;
}
if (dt == null) }
{
return string.Empty; return rv;
} }
if (dt.Columns.Count <= 0) public bool SetConfig(string name, bool value) => this.SetConfig(name, (value ? "1" : "0"));
{
return string.Empty; public bool SetConfig(string name, int value) => this.SetConfig(name, value.ToString());
}
public bool SetConfig(string name, string value)
if (dt.Rows.Count <= 0) {
{ prepareAppSettings();
return string.Empty;
} string sql = string.Empty;
int rv = this.DoQueryCount("SELECT 1 FROM " + appSettingsTableName + " WHERE cfg_name='" + escapeSQL(name) + "'");
if (dt.Rows[0][0] is byte[]) if (rv <= 0)
{ {
return Encoding.UTF8.GetString(dt.Rows[0][0] as byte[]); sql = "INSERT INTO " + appSettingsTableName + " (cfg_name, cfg_value) VALUES ('[^1]', '[^2]');";
} }
else else
{ {
return dt.Rows[0][0].ToString(); sql = "UPDATE " + appSettingsTableName + " SET cfg_value='[^2]' WHERE cfg_name='[^1]';";
} }
}
sql = prepareQuery(sql, new string[] { name, value });
public string DoQuerySingle(string query, params string[] args)
{ return this.DoNonQuery(sql) > 0;
string sql = SQLiteDatabase2.PrepareQuery(query, args); }
return this.DoQuerySingle(sql); public string GetConfig(string name, string defaultValue = null)
} {
prepareAppSettings();
public int DoNonQuery(string query)
{ bool rv = this.DoQueryExist("SELECT 1 FROM " + appSettingsTableName + " WHERE cfg_name='" + escapeSQL(name) + "';");
lastError = string.Empty; if (!rv)
{
if (dbConnection == null) return defaultValue;
{ }
return -1;
} return this.DoQuerySingle("SELECT cfg_value FROM " + appSettingsTableName + " WHERE cfg_name='" + escapeSQL(name) + "';");
}
int rv = 0;
public int GetIntConfig(string name, int defaultValue = 0)
try {
{ string rv = this.GetConfig(name);
SQLiteCommand command = new SQLiteCommand(query, dbConnection); if (string.IsNullOrWhiteSpace(rv))
rv = command.ExecuteNonQuery(); {
} return defaultValue;
catch (Exception exc) }
{
lastError = exc.Message; int n;
rv = -1; if (!int.TryParse(rv, out n))
} {
n = defaultValue;
return rv; }
}
return n;
public int DoNonQuery(string query, params string[] args) }
{
string sql = SQLiteDatabase2.PrepareQuery(query, args); public bool GetBoolConfig(string name, bool defaultValue = false)
{
return this.DoNonQuery(sql); string rv = this.GetConfig(name);
} if (string.IsNullOrWhiteSpace(rv))
{
public bool HasTable(string tableName) return defaultValue;
{ }
lastError = string.Empty;
if (rv.Equals("1"))
if (dbConnection == null) {
{ return true;
return false; }
}
if (rv.Equals("true", StringComparison.CurrentCultureIgnoreCase))
int rv = this.DoQueryCount("SELECT 1 FROM sqlite_master WHERE type='table' AND name='" + EscapeSQL(tableName) + "'"); {
return true;
return (rv > 0); }
}
return false;
public bool CheckRequiredTables() }
{
bool rv = true; protected bool create(string filename, bool overwriteFile, string password)
foreach (string tbl in requiredTableList) {
{ this.LastError = string.Empty;
if (string.IsNullOrEmpty(tbl))
{ if (File.Exists(filename))
continue; {
} if (overwriteFile)
{
if (!this.HasTable(tbl)) try
{ {
rv = false; File.Delete(filename);
break; }
} catch (Exception exc)
} {
this.LastError = exc.Message;
return rv;
} return false;
}
#endregion
try
public bool PrepareConfig() {
{ SQLiteConnection.CreateFile(filename);
if (this.HasTable(tableNameConfig)) }
{ catch (Exception exc)
return true; {
} this.LastError = exc.Message;
int rv = this.DoNonQuery("CREATE TABLE " + tableNameConfig + " (cfg_name TEXT, cfg_value TEXT)"); return false;
}
return rv > 0;
} return this.LoadFile(filename, password);
}
public bool SetConfig(string name, string value) else
{ {
this.PrepareConfig(); return this.LoadFile(filename, password);
}
string sql = string.Empty; }
int rv = this.DoQueryCount("SELECT 1 FROM " + tableNameConfig + " WHERE cfg_name='" + EscapeSQL(name) + "'"); else
if (rv <= 0) {
{ try
sql = "INSERT INTO " + tableNameConfig + " (cfg_name, cfg_value) VALUES ('[^1]', '[^2]');"; {
} SQLiteConnection.CreateFile(filename);
else }
{ catch (Exception exc)
sql = "UPDATE " + tableNameConfig + " SET cfg_value='[^2]' WHERE cfg_name='[^1]';"; {
} this.LastError = exc.Message;
sql = PrepareQuery(sql, new string[] { name, value }); return false;
}
return this.DoNonQuery(sql) > 0;
} return this.LoadFile(filename, password);
}
public string GetConfig(string name, string defaultValue = "") }
{
this.PrepareConfig(); protected string prepareQuery(string query, params string[] arguments)
{
bool rv = this.DoQueryExist("SELECT 1 FROM " + tableNameConfig + " WHERE cfg_name='" + EscapeSQL(name) + "';"); string rv = query;
if (!rv)
{ if (string.IsNullOrWhiteSpace(rv))
return defaultValue; {
} return string.Empty;
}
return this.DoQuerySingle("SELECT cfg_value FROM " + tableNameConfig + " WHERE cfg_name='" + EscapeSQL(name) + "';");
} for (int i = 0; i < arguments.Length; i++)
{
#endregion rv = rv.Replace("[^" + (i + 1).ToString() + "]", escapeSQL(arguments[i]));
}
protected virtual bool Prepare()
{ return rv;
return true; }
}
} protected string escapeSQL(string q) => q.Replace("'", "''").Trim();
protected string escapeValue(string text) => text.Replace("\"", "\\\"").Replace("\t", "\\t").Replace("\r", " \\r").Replace("\n", "\\n");
protected string encode64(string text) => Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(text));
protected bool prepareAppSettings()
{
if (this.HasTable(appSettingsTableName))
{
return true;
}
int rv = this.DoNonQuery("CREATE TABLE " + appSettingsTableName + " (cfg_name TEXT, cfg_value TEXT)");
return rv > 0;
}
protected virtual bool prepareDatabase()
{
return true;
}
}
} }

View File

@ -15,6 +15,8 @@
</TargetFrameworkProfile> </TargetFrameworkProfile>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<IsWebBootstrapper>false</IsWebBootstrapper> <IsWebBootstrapper>false</IsWebBootstrapper>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<PublishUrl>publish\</PublishUrl> <PublishUrl>publish\</PublishUrl>
<Install>true</Install> <Install>true</Install>
<InstallFrom>Disk</InstallFrom> <InstallFrom>Disk</InstallFrom>
@ -29,8 +31,6 @@
<ApplicationVersion>1.0.0.%2a</ApplicationVersion> <ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust> <UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled> <BootstrapperEnabled>true</BootstrapperEnabled>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x86</PlatformTarget> <PlatformTarget>x86</PlatformTarget>
@ -78,19 +78,17 @@
<Prefer32Bit>false</Prefer32Bit> <Prefer32Bit>false</Prefer32Bit>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="HtmlAgilityPack, Version=1.4.9.5, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL"> <Reference Include="HtmlAgilityPack, Version=1.11.2.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
<HintPath>packages\HtmlAgilityPack.1.4.9.5\lib\Net40\HtmlAgilityPack.dll</HintPath> <HintPath>packages\HtmlAgilityPack.1.11.2\lib\Net45\HtmlAgilityPack.dll</HintPath>
<Private>True</Private>
</Reference> </Reference>
<Reference Include="ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=neutral, PublicKeyToken=1b03e6acf1164f73, processorArchitecture=MSIL"> <Reference Include="ICSharpCode.SharpZipLib, Version=1.1.0.145, Culture=neutral, PublicKeyToken=1b03e6acf1164f73, processorArchitecture=MSIL">
<HintPath>packages\SharpZipLib.0.86.0\lib\20\ICSharpCode.SharpZipLib.dll</HintPath> <HintPath>packages\SharpZipLib.1.1.0\lib\net45\ICSharpCode.SharpZipLib.dll</HintPath>
<Private>True</Private>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Data.Linq" /> <Reference Include="System.Data.Linq" />
<Reference Include="System.Data.SQLite, Version=1.0.105.1, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL"> <Reference Include="System.Data.SQLite, Version=1.0.110.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
<HintPath>packages\System.Data.SQLite.Core.1.0.105.1\lib\net451\System.Data.SQLite.dll</HintPath> <HintPath>packages\System.Data.SQLite.Core.1.0.110.0\lib\net46\System.Data.SQLite.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Drawing" /> <Reference Include="System.Drawing" />
<Reference Include="System.Web" /> <Reference Include="System.Web" />
@ -98,12 +96,6 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="OptionsForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="OptionsForm.Designer.cs">
<DependentUpon>OptionsForm.cs</DependentUpon>
</Compile>
<Compile Include="RyzStudio\Data\SQLite\SQLiteDatabase2.cs" /> <Compile Include="RyzStudio\Data\SQLite\SQLiteDatabase2.cs" />
<Compile Include="UpdateIconForm.cs"> <Compile Include="UpdateIconForm.cs">
<SubType>Form</SubType> <SubType>Form</SubType>
@ -172,9 +164,6 @@
<Compile Include="BookmarkTreeView.Designer.cs"> <Compile Include="BookmarkTreeView.Designer.cs">
<DependentUpon>BookmarkTreeView.cs</DependentUpon> <DependentUpon>BookmarkTreeView.cs</DependentUpon>
</Compile> </Compile>
<EmbeddedResource Include="OptionsForm.resx">
<DependentUpon>OptionsForm.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="UpdateIconForm.resx"> <EmbeddedResource Include="UpdateIconForm.resx">
<DependentUpon>UpdateIconForm.cs</DependentUpon> <DependentUpon>UpdateIconForm.cs</DependentUpon>
</EmbeddedResource> </EmbeddedResource>
@ -258,12 +247,12 @@
</ItemGroup> </ItemGroup>
<ItemGroup /> <ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="packages\System.Data.SQLite.Core.1.0.105.1\build\net451\System.Data.SQLite.Core.targets" Condition="Exists('packages\System.Data.SQLite.Core.1.0.105.1\build\net451\System.Data.SQLite.Core.targets')" /> <Import Project="packages\System.Data.SQLite.Core.1.0.110.0\build\net46\System.Data.SQLite.Core.targets" Condition="Exists('packages\System.Data.SQLite.Core.1.0.110.0\build\net46\System.Data.SQLite.Core.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup> <PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText> <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup> </PropertyGroup>
<Error Condition="!Exists('packages\System.Data.SQLite.Core.1.0.105.1\build\net451\System.Data.SQLite.Core.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\System.Data.SQLite.Core.1.0.105.1\build\net451\System.Data.SQLite.Core.targets'))" /> <Error Condition="!Exists('packages\System.Data.SQLite.Core.1.0.110.0\build\net46\System.Data.SQLite.Core.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\System.Data.SQLite.Core.1.0.110.0\build\net46\System.Data.SQLite.Core.targets'))" />
</Target> </Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="HtmlAgilityPack" version="1.4.9.5" targetFramework="net40" requireReinstallation="true" /> <package id="HtmlAgilityPack" version="1.11.2" targetFramework="net462" />
<package id="SharpZipLib" version="0.86.0" targetFramework="net40" /> <package id="SharpZipLib" version="1.1.0" targetFramework="net462" />
<package id="System.Data.SQLite.Core" version="1.0.105.1" targetFramework="net452" /> <package id="System.Data.SQLite.Core" version="1.0.110.0" targetFramework="net462" />
</packages> </packages>