public static Task <MCIUserExt> GetUserExtensions(ulong user_id) { return(Task.Run(new Func <Task <MCIUserExt> >(async() => { MCIUserExt res = null; var sqlconn = await ARDBConnectionManager.default_manager.CheckOut(); try { using (MySqlCommand cmd = new MySqlCommand("select data from `user_extensions` where user_id=" + user_id, sqlconn.Connection)) using (MySqlDataReader reader = cmd.ExecuteReader()) { await reader.ReadAsync(); if (reader.IsDBNull(0)) { res = null; } //else // res = JsonConvert.DeserializeObject<MCIUserExt>((string)reader["data"]); } } catch (Exception ex) { } ARDBConnectionManager.default_manager.CheckIn(sqlconn); return res; }))); }
public static Task SetUserExtensions(ulong user_id, MCIUserExt uext) { return(Task.Run(async() => { var sqlconn = await ARDBConnectionManager.default_manager.CheckOut(); try { bool was_found = false; using (MySqlCommand sqlcmd = new MySqlCommand("select user_id from user_extensions where user_id=@user_id", sqlconn.Connection)) { sqlcmd.Parameters.AddWithValue("@user_id", user_id); using (MySqlDataReader reader = sqlcmd.ExecuteReader()) while (await reader.ReadAsync()) { was_found = true; } } if (was_found) { using (MySqlCommand cmdName = new MySqlCommand("update `user_extensions` set data = @data where user_id=" + user_id, sqlconn.Connection)) { //cmdName.Parameters.AddWithValue("@data", JsonConvert.SerializeObject(uext)); //cmdName.ExecuteNonQuery(); } } else { using (MySqlCommand sqlcmd = new MySqlCommand("insert into `user_extensions` (user_id, data) values (@user_id, @data)", sqlconn.Connection)) { //sqlcmd.Parameters.AddWithValue("@user_id", user_id); //sqlcmd.Parameters.AddWithValue("@data", JsonConvert.SerializeObject(uext)); await sqlcmd.ExecuteNonQueryAsync(); } } } catch (Exception ex) { } ARDBConnectionManager.default_manager.CheckIn(sqlconn); })); }