/// <summary> /// Gets a group setting from the database /// </summary> /// <typeparam name="T">The type the setting should be (bool, int, string)</typeparam> /// <param name="group">What group the setting comes from</param> /// <param name="field">The name of the setting</param> /// <param name="db">The database TelegramBotDbContext</param> /// <param name="def">The default value for the field</param> /// <returns></returns> public static T GetSetting <T>(this TelegramBotGroup group, string field, TelegramBotDbContext db, object def) { //if (db.Connection.State != ConnectionState.Open) // db.Connection.Open(); ////verify settings exist //var columns = new SQLiteCommand("PRAGMA table_info(chatgroup)", db.Connection).ExecuteReader(); //var t = default(T); //while (columns.Read()) //{ // if (String.Equals(columns[1].ToString(), field)) // { // var result = new SQLiteCommand($"select {field} from chatgroup where ID = {group.ID}", db.Connection).ExecuteScalar(); // if (t != null && t.GetType() == typeof(bool)) // { // result = (result.ToString() == "1"); //make it a boolean value // } // return (T)result; // } //} //var type = "BLOB"; //if (t == null) // type = "TEXT"; //else if (t.GetType() == typeof(int)) // type = "INTEGER"; //else if (t.GetType() == typeof(bool)) // type = "INTEGER"; //new SQLiteCommand($"ALTER TABLE chatgroup ADD COLUMN {field} {type} DEFAULT {(type == "INTEGER" ? def : $"'{def}'")};", db.Connection) // .ExecuteNonQuery(); //return (T)def; throw new NotImplementedException(); }
public static void Save(this TelegramBotGroup u, TelegramBotDbContext db) { if (u.ID == null || !ExistsInDb(u, db)) { //need to insert //db.ExecuteNonQuery( // "insert into chatgroup (GroupId, Name, UserName, MemberCount) VALUES (@GroupId, @Name, @UserName, @MemberCount)", // u); //u.ID = // db.Connection.Query<int>( // $"SELECT ID FROM chatgroup WHERE GroupId = @GroupId", u) // .First(); db.Groups.Add(u); db.SaveChanges(); u.ID = db.Groups.FirstOrDefault(c => c.GroupId == u.GroupId).ID; } else { db.Groups.Update(u); db.SaveChanges(); //db.ExecuteNonQuery( // "UPDATE chatgroup SET GroupId = @GroupId, Name = @Name, UserName = @UserName, MemberCount = @MemberCount WHERE ID = @ID", // u); } }
public static void Save(this TelegramBotGroup u, ITelegramBotDbContext db) { if (u.ID == null || !ExistsInDb(u, db)) { db.TelegramBotGroups.Add(u); db.SaveChanges(); u.ID = db.TelegramBotGroups.FirstOrDefault(c => c.GroupId == u.GroupId).ID; } else { db.TelegramBotGroups.Update(u); db.SaveChanges(); } }
/// <summary> /// Gets a group setting from the database /// </summary> /// <typeparam name="T">The type the setting should be (bool, int, string)</typeparam> /// <param name="group">What group the setting comes from</param> /// <param name="field">The name of the setting</param> /// <param name="db">The database TelegramBotDbContext</param> /// <param name="def">The default value for the field</param> /// <returns></returns> public static bool SetSetting <T>(this TelegramBotGroup group, string field, TelegramBotDbContext db, object def, object value) { //try //{ // if (db.Connection.State != ConnectionState.Open) // db.Connection.Open(); // //verify settings exist // var columns = new SQLiteCommand("PRAGMA table_info(chatgroup)", db.Connection).ExecuteReader(); // var t = default(T); // var type = "BLOB"; // if (t == null) // type = "TEXT"; // else if (t.GetType() == typeof(int)) // type = "INTEGER"; // else if (t.GetType() == typeof(bool)) // type = "INTEGER"; // bool settingExists = false; // while (columns.Read()) // { // if (String.Equals(columns[1].ToString(), field)) // { // settingExists = true; // } // } // if (!settingExists) // { // new SQLiteCommand($"ALTER TABLE chatgroup ADD COLUMN {field} {type} DEFAULT {(type == "INTEGER" ? def : $"'{def}'")};", db.Connection) // .ExecuteNonQuery(); // } // new SQLiteCommand($"UPDATE chatgroup set {field} = {(type == "INTEGER" ? (t != null && t.GetType() == typeof(bool)) ? (bool)value ? "1" : "0" : value : $"'{value}'")} where ID = {group.ID}", db.Connection).ExecuteNonQuery(); // return true; //} //catch //{ // return false; //} throw new NotImplementedException(); }
public static void RemoveFromDb(this TelegramBotGroup group, ITelegramBotDbContext db) { db.TelegramBotGroups.Remove(group); db.SaveChanges(); }
public static bool ExistsInDb(this TelegramBotGroup group, ITelegramBotDbContext db) { return(db.TelegramBotGroups.Any(c => c.ID == group.ID)); }