Updated: icon database helper
This commit is contained in:
parent
aecc5ac043
commit
48416abccb
@ -317,7 +317,7 @@ namespace bzit.bomg
|
||||
}
|
||||
|
||||
// load favicon image
|
||||
if (!string.IsNullOrEmpty(this.IconAddress))
|
||||
if (!string.IsNullOrWhiteSpace(this.IconAddress))
|
||||
{
|
||||
try
|
||||
{
|
||||
|
@ -12,9 +12,9 @@ namespace bzit.bomg
|
||||
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;
|
||||
}
|
||||
@ -34,7 +34,7 @@ namespace bzit.bomg
|
||||
|
||||
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)
|
||||
@ -73,11 +73,11 @@ namespace bzit.bomg
|
||||
|
||||
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
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@ -99,7 +99,7 @@ namespace bzit.bomg
|
||||
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
|
||||
|
1245
MainForm.Designer.cs
generated
1245
MainForm.Designer.cs
generated
File diff suppressed because it is too large
Load Diff
62
MainForm.cs
62
MainForm.cs
@ -32,32 +32,40 @@ namespace bzit.bomg
|
||||
// toolbar
|
||||
viewHelpHelpMenuItem.Enabled = File.Exists(Path.ChangeExtension(Application.ExecutablePath, ".chm"));
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
string iconDBPath = Path.ChangeExtension(Application.ExecutablePath, ".db");
|
||||
|
||||
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);
|
||||
this.ApplicationMode = AppMode.Clear;
|
||||
|
||||
@ -440,8 +448,8 @@ namespace bzit.bomg
|
||||
*/
|
||||
private void optionsToolStripMenuItem1_Click(object sender, EventArgs e)
|
||||
{
|
||||
OptionsForm frm = new OptionsForm(this);
|
||||
frm.ShowDialog();
|
||||
//OptionsForm frm = new OptionsForm(this);
|
||||
//frm.ShowDialog();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -131,7 +131,7 @@
|
||||
AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w
|
||||
LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0
|
||||
ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAADU
|
||||
FwAAAk1TRnQBSQFMAgEBBwEAAVwBAgFcAQIBEAEAARABAAT/AREBAAj/AUIBTQE2BwABNgMAASgDAAFA
|
||||
FwAAAk1TRnQBSQFMAgEBBwEAAWQBAgFkAQIBEAEAARABAAT/AREBAAj/AUIBTQE2BwABNgMAASgDAAFA
|
||||
AwABIAMAAQEBAAEQBgABEBQAAZkBSgEYAS4BtwEdAbcBGQG3ARkBtwEZAbcBGQG3ARkBtwEZAbcBGQG3
|
||||
ARkBtwEdARcBLgGZAUoEAAGZAUoBGAEuAbcBHQG3ARkBtwEZAbcBGQG3ARkBtwEZAbcBGQG3ARkBtwEZ
|
||||
AbcBHQEXAS4BmQFKBAABmQFKARgBLgG3AR0BtwEZAbcBGQG3ARkBtwEZAbcBGQG3ARkBtwEZAbcBGQG3
|
||||
@ -268,15 +268,15 @@
|
||||
<data name="newMenuBarItem.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||
<value>
|
||||
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
|
||||
YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAHaSURBVDhPjZM9SxxRFIY1Vv4HhVRCQIK4smUQQgpBiwRT
|
||||
SCBgigTSJYpFSsVKG1mWJU1IJ1gI6W1EQsQQd1ZD/ABFLHbdL3d25+PeOzP6es7FGWfcDcmBhy3mvM++
|
||||
9+5sV2z6iBSR/gcPiQdE26Rc1zUNw7gKggCdkFIik8ms0G5HSZrDtm3DcZyIZrOJRqOBcrmsJaVSCdls
|
||||
tk3STaR5QQgBpZT+ZCzLgmmaqFQqWkAtwV+Sy+VCiR4t8JsG5OVPLQglHGi1WqhWq4njMJzRaRotEEcf
|
||||
If68ixZ839fnDo9Sq9V0k2KxeCe4Pv/Qa+2/Gt1df/pFHryF3JuCPF2CV/+OwLMTEm7C91Gv1+8E8vDN
|
||||
Z3X4HvJ4Bt7ZEtTJPMTuc9hbwxD7c4kmfBy+ExZFAuBlj7v34ps8niXBMgkW4BqTcPPTCHylFxnP8/S9
|
||||
sISJBDyXvyYeC2Mc6nQB6ugT7M1BeObvKMxwi/ByuU1CYO88GxP5CTg/hmBtPaL6KaiLDR0K4QYMh9sF
|
||||
209e51cHvgayBp8QB4uQ52tt4TgJAU30It0n3iIOP+OMTtNoQaFQuL4v+Bu0e8UZnb6dEfp5HH7QKRCH
|
||||
d+h1tijD/95o+okRgq3/A4W7+m8ApNSdQdLieiIAAAAASUVORK5CYII=
|
||||
YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAHYSURBVDhPjZO7SxxRFMZNrPI/KKQShBDElS1FEAtBiwQt
|
||||
QiCQFAnY+cAiZcRKm7AsS5qQLpAiYJ9GJEQiOoOKL1DEYtd9ubM7j3vvzOiXcy7OOONuSA782GLO99vv
|
||||
3p3tSkwPkSGy/+Ax8ZBom4zneZZhGNdhGKITUkrkcrmPtNtRkuWw4zhwXTem2Wyi0WigXC5rSalUQj6f
|
||||
b5M8ILK8IISAUkp/MrZtw7IsVCoVLaCW4C8pFAqRRI8WBE0T8mpLCyIJB1qtFqrVauo4DGd0mkYLxPEc
|
||||
xMG7eCEIAn3u6Ci1Wk03KRaLd4Kbi9lH9t7LkZ3vo5/l4VvI3ReQZyvw6z8R+k5Kwk34Pur1+p1AHr35
|
||||
pI5mIE/m4Z+vQJ1+gNh5BmdjEGJvMdWEj8N3wqJYAEx3e7vP1+TJAglWSbAEz5yCZ7xGGCi9yPi+r++F
|
||||
JUws4LnannwqzAmosyWo4/dw1p/At/bjMMMtosvlNimB83tsXBiTcH8NwN7op/oZqMsfOhTBDRgOtws2
|
||||
h18ZX/u+hLKGgBCHy5AX39rCSVICmvhFuk+yRRJ+xhmdptEC0zRv7gv+Bu1ec0anb2eIfh6XH3QKJOEd
|
||||
ep1tyvC/N55eYohg6/9A4a7eP4kvnS+BaQv8AAAAAElFTkSuQmCC
|
||||
</value>
|
||||
</data>
|
||||
<metadata name="treeMenuRoot.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
|
@ -8,440 +8,462 @@ using System.Text;
|
||||
|
||||
namespace RyzStudio.Data.SQLite
|
||||
{
|
||||
public class SQLiteDatabase2
|
||||
{
|
||||
#region static methods
|
||||
|
||||
public static string EscapeSQL(string query)
|
||||
{
|
||||
return query.Replace("'", "''").Trim();
|
||||
}
|
||||
|
||||
public static string EscapeValue(string text)
|
||||
{
|
||||
return text.Replace("\"", "\\\"").Replace("\t", "\\t").Replace("\r", " \\r").Replace("\n", "\\n");
|
||||
}
|
||||
|
||||
public static string PrepareQuery(string query, params string[] arguments)
|
||||
{
|
||||
string rv = query;
|
||||
|
||||
if (string.IsNullOrEmpty(rv))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
for (int i = 0; i < arguments.Length; i++)
|
||||
{
|
||||
rv = rv.Replace("[^" + (i + 1).ToString() + "]", EscapeSQL(arguments[i]));
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
public static string Encode64(string text) { return Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(text)); }
|
||||
|
||||
#endregion
|
||||
|
||||
protected SQLiteConnection dbConnection = null;
|
||||
protected string dbLocation = string.Empty;
|
||||
protected string lastError = "";
|
||||
|
||||
protected string[] requiredTableList = new string[0];
|
||||
|
||||
protected const string tableNameConfig = "ryz_app_xxxx_config";
|
||||
|
||||
#region public properties
|
||||
|
||||
[Browsable(false)]
|
||||
public SQLiteConnection DBConnection
|
||||
{
|
||||
get { return dbConnection; }
|
||||
set { dbConnection = value; }
|
||||
}
|
||||
|
||||
[Browsable(false)]
|
||||
public string DBLocation
|
||||
{
|
||||
get { return dbLocation; }
|
||||
set { dbLocation = value; }
|
||||
}
|
||||
|
||||
public string LastError { get { return lastError; } }
|
||||
|
||||
public int LastInsertID
|
||||
{
|
||||
get
|
||||
{
|
||||
if (dbConnection == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
DataTable dt = this.DoQuery("SELECT last_insert_rowid() AS ccc;");
|
||||
if (dt == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (dt.Rows.Count <= 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return int.Parse(dt.Rows[0]["ccc"].ToString());
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region public methods
|
||||
|
||||
public bool CreateMemory()
|
||||
{
|
||||
lastError = string.Empty;
|
||||
|
||||
dbLocation = ":memory:";
|
||||
|
||||
try
|
||||
{
|
||||
dbConnection = new SQLiteConnection(string.Concat("Data Source=\"", dbLocation, "\";Version=3;UTF8Encoding=True;"));
|
||||
dbConnection.Open();
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
lastError = exc.Message;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool CreateSpecial(string filename, bool overwriteFile = false, string password = null)
|
||||
{
|
||||
bool rs = this.CreateFile(filename, overwriteFile, password);
|
||||
if (!rs)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
rs = this.Prepare();
|
||||
if (!rs)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return this.CheckRequiredTables();
|
||||
}
|
||||
|
||||
public bool CreateFile(string filename, bool overwriteFile = false, string password = null)
|
||||
{
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
if (overwriteFile)
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(filename);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
SQLiteConnection.CreateFile(filename);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return this.LoadFile(filename, password);
|
||||
}
|
||||
|
||||
public bool LoadFile(string filename, string password = null)
|
||||
{
|
||||
lastError = string.Empty;
|
||||
|
||||
if (!File.Exists(filename))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
dbLocation = filename;
|
||||
|
||||
try
|
||||
{
|
||||
dbConnection = new SQLiteConnection(string.Concat("Data Source=\"", filename, "\";Version=3;UTF8Encoding=True;", (password == null) ? string.Empty : string.Concat("Password=", Encode64(password), ";")));
|
||||
dbConnection.Open();
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
lastError = exc.Message;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
if (dbConnection != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
dbConnection.Close();
|
||||
}
|
||||
catch
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#region query
|
||||
|
||||
public DataTable DoQuery(string query)
|
||||
{
|
||||
lastError = string.Empty;
|
||||
|
||||
if (dbConnection == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
SQLiteCommand command = new SQLiteCommand(query, dbConnection);
|
||||
SQLiteDataReader dr = command.ExecuteReader();
|
||||
|
||||
DataTable dt = new DataTable();
|
||||
dt.Load(dr);
|
||||
|
||||
return dt;
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
lastError = exc.Message;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public DataTable DoQuery(string query, params string[] args)
|
||||
{
|
||||
string sql = SQLiteDatabase2.PrepareQuery(query, args);
|
||||
|
||||
return DoQuery(sql);
|
||||
}
|
||||
|
||||
public int DoQueryCount(string query)
|
||||
{
|
||||
lastError = string.Empty;
|
||||
|
||||
if (dbConnection == null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
DataTable dt = this.DoQuery(query);
|
||||
if (dt == null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return dt.Rows.Count;
|
||||
}
|
||||
|
||||
public int DoQueryCount(string query, params string[] args)
|
||||
{
|
||||
string sql = SQLiteDatabase2.PrepareQuery(query, args);
|
||||
|
||||
return this.DoQueryCount(sql);
|
||||
}
|
||||
|
||||
public bool DoQueryExist(string query)
|
||||
{
|
||||
int rv = this.DoQueryCount(query);
|
||||
|
||||
return (rv > 0);
|
||||
}
|
||||
|
||||
public bool DoQueryExist(string query, params string[] args)
|
||||
{
|
||||
string sql = SQLiteDatabase2.PrepareQuery(query, args);
|
||||
|
||||
return this.DoQueryExist(sql);
|
||||
}
|
||||
|
||||
public string DoQuerySingle(string query)
|
||||
{
|
||||
lastError = string.Empty;
|
||||
|
||||
if (dbConnection == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
DataTable dt = this.DoQuery(query);
|
||||
|
||||
if (dt == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
if (dt.Columns.Count <= 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
if (dt.Rows.Count <= 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
if (dt.Rows[0][0] is byte[])
|
||||
{
|
||||
return Encoding.UTF8.GetString(dt.Rows[0][0] as byte[]);
|
||||
}
|
||||
else
|
||||
{
|
||||
return dt.Rows[0][0].ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public string DoQuerySingle(string query, params string[] args)
|
||||
{
|
||||
string sql = SQLiteDatabase2.PrepareQuery(query, args);
|
||||
|
||||
return this.DoQuerySingle(sql);
|
||||
}
|
||||
|
||||
public int DoNonQuery(string query)
|
||||
{
|
||||
lastError = string.Empty;
|
||||
|
||||
if (dbConnection == null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int rv = 0;
|
||||
|
||||
try
|
||||
{
|
||||
SQLiteCommand command = new SQLiteCommand(query, dbConnection);
|
||||
rv = command.ExecuteNonQuery();
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
lastError = exc.Message;
|
||||
rv = -1;
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
public int DoNonQuery(string query, params string[] args)
|
||||
{
|
||||
string sql = SQLiteDatabase2.PrepareQuery(query, args);
|
||||
|
||||
return this.DoNonQuery(sql);
|
||||
}
|
||||
|
||||
public bool HasTable(string tableName)
|
||||
{
|
||||
lastError = string.Empty;
|
||||
|
||||
if (dbConnection == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int rv = this.DoQueryCount("SELECT 1 FROM sqlite_master WHERE type='table' AND name='" + EscapeSQL(tableName) + "'");
|
||||
|
||||
return (rv > 0);
|
||||
}
|
||||
|
||||
public bool CheckRequiredTables()
|
||||
{
|
||||
bool rv = true;
|
||||
foreach (string tbl in requiredTableList)
|
||||
{
|
||||
if (string.IsNullOrEmpty(tbl))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!this.HasTable(tbl))
|
||||
{
|
||||
rv = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public bool PrepareConfig()
|
||||
{
|
||||
if (this.HasTable(tableNameConfig))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
int rv = this.DoNonQuery("CREATE TABLE " + tableNameConfig + " (cfg_name TEXT, cfg_value TEXT)");
|
||||
|
||||
return rv > 0;
|
||||
}
|
||||
|
||||
public bool SetConfig(string name, string value)
|
||||
{
|
||||
this.PrepareConfig();
|
||||
|
||||
string sql = string.Empty;
|
||||
int rv = this.DoQueryCount("SELECT 1 FROM " + tableNameConfig + " WHERE cfg_name='" + EscapeSQL(name) + "'");
|
||||
if (rv <= 0)
|
||||
{
|
||||
sql = "INSERT INTO " + tableNameConfig + " (cfg_name, cfg_value) VALUES ('[^1]', '[^2]');";
|
||||
}
|
||||
else
|
||||
{
|
||||
sql = "UPDATE " + tableNameConfig + " SET cfg_value='[^2]' WHERE cfg_name='[^1]';";
|
||||
}
|
||||
|
||||
sql = PrepareQuery(sql, new string[] { name, value });
|
||||
|
||||
return this.DoNonQuery(sql) > 0;
|
||||
}
|
||||
|
||||
public string GetConfig(string name, string defaultValue = "")
|
||||
{
|
||||
this.PrepareConfig();
|
||||
|
||||
bool rv = this.DoQueryExist("SELECT 1 FROM " + tableNameConfig + " WHERE cfg_name='" + EscapeSQL(name) + "';");
|
||||
if (!rv)
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
return this.DoQuerySingle("SELECT cfg_value FROM " + tableNameConfig + " WHERE cfg_name='" + EscapeSQL(name) + "';");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
protected virtual bool Prepare()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
public class SQLiteDatabase2
|
||||
{
|
||||
public SQLiteConnection DBConnection { get; protected set; } = null;
|
||||
|
||||
public string DBLocation { get; protected set; } = null;
|
||||
|
||||
public string LastError { get; protected set; } = null;
|
||||
|
||||
public int LastInsertID
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.DBConnection == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
DataTable dt = this.DoQuery("SELECT last_insert_rowid() AS rv;");
|
||||
if (dt == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (dt.Rows.Count <= 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int n;
|
||||
if (!int.TryParse(dt.Rows[0]["rv"].ToString(), out n))
|
||||
{
|
||||
n = 0;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
}
|
||||
|
||||
protected string[] requiredTableList = new string[0];
|
||||
|
||||
protected const string appSettingsTableName = "ryz_app_xxxx_config";
|
||||
|
||||
public bool Create()
|
||||
{
|
||||
this.LastError = string.Empty;
|
||||
|
||||
this.DBLocation = ":memory:";
|
||||
|
||||
try
|
||||
{
|
||||
this.DBConnection = new SQLiteConnection(string.Concat("Data Source=\"", this.DBLocation, "\";Version=3;UTF8Encoding=True;"));
|
||||
this.DBConnection.Open();
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
this.LastError = exc.Message;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Create(string filename, bool overwriteFile = false, string password = null, bool useAppSettings = false)
|
||||
{
|
||||
bool rs = create(filename, overwriteFile, password);
|
||||
|
||||
if (useAppSettings)
|
||||
{
|
||||
if (!rs)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
rs = prepareDatabase();
|
||||
if (!rs)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return this.HasRequiredTables();
|
||||
}
|
||||
else
|
||||
{
|
||||
return rs;
|
||||
}
|
||||
}
|
||||
|
||||
public bool LoadFile(string filename, string password = null)
|
||||
{
|
||||
this.LastError = string.Empty;
|
||||
|
||||
if (!File.Exists(filename))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
this.DBLocation = filename;
|
||||
|
||||
try
|
||||
{
|
||||
this.DBConnection = new SQLiteConnection(string.Concat("Data Source=\"", filename, "\";Version=3;UTF8Encoding=True;", (password == null) ? string.Empty : string.Concat("Password=", encode64(password), ";")));
|
||||
this.DBConnection.Open();
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
this.LastError = exc.Message;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
this.LastError = string.Empty;
|
||||
|
||||
if (this.DBConnection != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.DBConnection.Cancel();
|
||||
this.DBConnection.Close();
|
||||
this.DBConnection.Dispose();
|
||||
this.DBConnection = null;
|
||||
|
||||
SQLiteConnection.ClearAllPools();
|
||||
GC.Collect();
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
this.LastError = exc.Message;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public DataTable DoQuery(string query)
|
||||
{
|
||||
this.LastError = string.Empty;
|
||||
|
||||
if (this.DBConnection == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
SQLiteCommand command = new SQLiteCommand(query, this.DBConnection);
|
||||
SQLiteDataReader dr = command.ExecuteReader();
|
||||
|
||||
DataTable dt = new DataTable();
|
||||
dt.Load(dr);
|
||||
|
||||
return dt;
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
this.LastError = exc.Message;
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public DataTable DoQuery(string query, params string[] args) => DoQuery(prepareQuery(query, args));
|
||||
|
||||
public int DoQueryCount(string query)
|
||||
{
|
||||
this.LastError = string.Empty;
|
||||
|
||||
if (this.DBConnection == null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
DataTable dt = this.DoQuery(query);
|
||||
if (dt == null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return dt.Rows.Count;
|
||||
}
|
||||
|
||||
public int DoQueryCount(string query, params string[] args) => this.DoQueryCount(prepareQuery(query, args));
|
||||
|
||||
public bool DoQueryExist(string query) => (this.DoQueryCount(query) > 0);
|
||||
|
||||
public bool DoQueryExist(string query, params string[] args) => this.DoQueryExist(prepareQuery(query, args));
|
||||
|
||||
public string DoQuerySingle(string query)
|
||||
{
|
||||
this.LastError = string.Empty;
|
||||
|
||||
if (this.DBConnection == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
DataTable dt = this.DoQuery(query);
|
||||
|
||||
if (dt == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
if (dt.Columns.Count <= 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
if (dt.Rows.Count <= 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
if (dt.Rows[0][0] is byte[])
|
||||
{
|
||||
return Encoding.UTF8.GetString(dt.Rows[0][0] as byte[]);
|
||||
}
|
||||
else
|
||||
{
|
||||
return dt.Rows[0][0].ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public string DoQuerySingle(string query, params string[] args) => this.DoQuerySingle(prepareQuery(query, args));
|
||||
|
||||
public int DoNonQuery(string query)
|
||||
{
|
||||
this.LastError = string.Empty;
|
||||
|
||||
if (this.DBConnection == null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int rv = 0;
|
||||
|
||||
try
|
||||
{
|
||||
SQLiteCommand command = new SQLiteCommand(query, this.DBConnection);
|
||||
rv = command.ExecuteNonQuery();
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
this.LastError = exc.Message;
|
||||
rv = -1;
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
public int DoNonQuery(string query, params string[] args) => this.DoNonQuery(prepareQuery(query, args));
|
||||
|
||||
public bool HasTable(string tableName)
|
||||
{
|
||||
this.LastError = string.Empty;
|
||||
|
||||
if (this.DBConnection == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int rv = this.DoQueryCount("SELECT 1 FROM sqlite_master WHERE type='table' AND name='" + escapeSQL(tableName) + "'");
|
||||
|
||||
return (rv > 0);
|
||||
}
|
||||
|
||||
public bool HasRequiredTables()
|
||||
{
|
||||
bool rv = true;
|
||||
foreach (string tbl in requiredTableList)
|
||||
{
|
||||
if (string.IsNullOrEmpty(tbl))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!this.HasTable(tbl))
|
||||
{
|
||||
rv = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
public bool SetConfig(string name, bool value) => this.SetConfig(name, (value ? "1" : "0"));
|
||||
|
||||
public bool SetConfig(string name, int value) => this.SetConfig(name, value.ToString());
|
||||
|
||||
public bool SetConfig(string name, string value)
|
||||
{
|
||||
prepareAppSettings();
|
||||
|
||||
string sql = string.Empty;
|
||||
int rv = this.DoQueryCount("SELECT 1 FROM " + appSettingsTableName + " WHERE cfg_name='" + escapeSQL(name) + "'");
|
||||
if (rv <= 0)
|
||||
{
|
||||
sql = "INSERT INTO " + appSettingsTableName + " (cfg_name, cfg_value) VALUES ('[^1]', '[^2]');";
|
||||
}
|
||||
else
|
||||
{
|
||||
sql = "UPDATE " + appSettingsTableName + " SET cfg_value='[^2]' WHERE cfg_name='[^1]';";
|
||||
}
|
||||
|
||||
sql = prepareQuery(sql, new string[] { name, value });
|
||||
|
||||
return this.DoNonQuery(sql) > 0;
|
||||
}
|
||||
|
||||
public string GetConfig(string name, string defaultValue = null)
|
||||
{
|
||||
prepareAppSettings();
|
||||
|
||||
bool rv = this.DoQueryExist("SELECT 1 FROM " + appSettingsTableName + " WHERE cfg_name='" + escapeSQL(name) + "';");
|
||||
if (!rv)
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
return this.DoQuerySingle("SELECT cfg_value FROM " + appSettingsTableName + " WHERE cfg_name='" + escapeSQL(name) + "';");
|
||||
}
|
||||
|
||||
public int GetIntConfig(string name, int defaultValue = 0)
|
||||
{
|
||||
string rv = this.GetConfig(name);
|
||||
if (string.IsNullOrWhiteSpace(rv))
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
int n;
|
||||
if (!int.TryParse(rv, out n))
|
||||
{
|
||||
n = defaultValue;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
public bool GetBoolConfig(string name, bool defaultValue = false)
|
||||
{
|
||||
string rv = this.GetConfig(name);
|
||||
if (string.IsNullOrWhiteSpace(rv))
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
if (rv.Equals("1"))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (rv.Equals("true", StringComparison.CurrentCultureIgnoreCase))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected bool create(string filename, bool overwriteFile, string password)
|
||||
{
|
||||
this.LastError = string.Empty;
|
||||
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
if (overwriteFile)
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(filename);
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
this.LastError = exc.Message;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
SQLiteConnection.CreateFile(filename);
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
this.LastError = exc.Message;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return this.LoadFile(filename, password);
|
||||
}
|
||||
else
|
||||
{
|
||||
return this.LoadFile(filename, password);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
SQLiteConnection.CreateFile(filename);
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
this.LastError = exc.Message;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return this.LoadFile(filename, password);
|
||||
}
|
||||
}
|
||||
|
||||
protected string prepareQuery(string query, params string[] arguments)
|
||||
{
|
||||
string rv = query;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(rv))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
for (int i = 0; i < arguments.Length; i++)
|
||||
{
|
||||
rv = rv.Replace("[^" + (i + 1).ToString() + "]", escapeSQL(arguments[i]));
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
31
bomg.csproj
31
bomg.csproj
@ -15,6 +15,8 @@
|
||||
</TargetFrameworkProfile>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<IsWebBootstrapper>false</IsWebBootstrapper>
|
||||
<NuGetPackageImportStamp>
|
||||
</NuGetPackageImportStamp>
|
||||
<PublishUrl>publish\</PublishUrl>
|
||||
<Install>true</Install>
|
||||
<InstallFrom>Disk</InstallFrom>
|
||||
@ -29,8 +31,6 @@
|
||||
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
|
||||
<UseApplicationTrust>false</UseApplicationTrust>
|
||||
<BootstrapperEnabled>true</BootstrapperEnabled>
|
||||
<NuGetPackageImportStamp>
|
||||
</NuGetPackageImportStamp>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
@ -78,19 +78,17 @@
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="HtmlAgilityPack, Version=1.4.9.5, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
|
||||
<HintPath>packages\HtmlAgilityPack.1.4.9.5\lib\Net40\HtmlAgilityPack.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
<Reference Include="HtmlAgilityPack, Version=1.11.2.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
|
||||
<HintPath>packages\HtmlAgilityPack.1.11.2\lib\Net45\HtmlAgilityPack.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=neutral, PublicKeyToken=1b03e6acf1164f73, processorArchitecture=MSIL">
|
||||
<HintPath>packages\SharpZipLib.0.86.0\lib\20\ICSharpCode.SharpZipLib.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
<Reference Include="ICSharpCode.SharpZipLib, Version=1.1.0.145, Culture=neutral, PublicKeyToken=1b03e6acf1164f73, processorArchitecture=MSIL">
|
||||
<HintPath>packages\SharpZipLib.1.1.0\lib\net45\ICSharpCode.SharpZipLib.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Data.Linq" />
|
||||
<Reference Include="System.Data.SQLite, Version=1.0.105.1, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
|
||||
<HintPath>packages\System.Data.SQLite.Core.1.0.105.1\lib\net451\System.Data.SQLite.dll</HintPath>
|
||||
<Reference Include="System.Data.SQLite, Version=1.0.110.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
|
||||
<HintPath>packages\System.Data.SQLite.Core.1.0.110.0\lib\net46\System.Data.SQLite.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Web" />
|
||||
@ -98,12 +96,6 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</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="UpdateIconForm.cs">
|
||||
<SubType>Form</SubType>
|
||||
@ -172,9 +164,6 @@
|
||||
<Compile Include="BookmarkTreeView.Designer.cs">
|
||||
<DependentUpon>BookmarkTreeView.cs</DependentUpon>
|
||||
</Compile>
|
||||
<EmbeddedResource Include="OptionsForm.resx">
|
||||
<DependentUpon>OptionsForm.cs</DependentUpon>
|
||||
</EmbeddedResource>
|
||||
<EmbeddedResource Include="UpdateIconForm.resx">
|
||||
<DependentUpon>UpdateIconForm.cs</DependentUpon>
|
||||
</EmbeddedResource>
|
||||
@ -258,12 +247,12 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<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">
|
||||
<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>
|
||||
</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>
|
||||
<!-- 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.
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="HtmlAgilityPack" version="1.4.9.5" targetFramework="net40" requireReinstallation="true" />
|
||||
<package id="SharpZipLib" version="0.86.0" targetFramework="net40" />
|
||||
<package id="System.Data.SQLite.Core" version="1.0.105.1" targetFramework="net452" />
|
||||
<package id="HtmlAgilityPack" version="1.11.2" targetFramework="net462" />
|
||||
<package id="SharpZipLib" version="1.1.0" targetFramework="net462" />
|
||||
<package id="System.Data.SQLite.Core" version="1.0.110.0" targetFramework="net462" />
|
||||
</packages>
|
Reference in New Issue
Block a user