using System; using System.Collections.Generic; using System.Text; using Npgsql; using System.Collections; using System.Data; namespace SafeMobileLib { public class DBRadioTypeManager : DBmanager { public DBRadioTypeManager(string p_server, string p_dbname, string p_user, string p_password, string p_port) : base(p_server, p_dbname, p_user, p_password, p_port) { } public Response setRadioType(string radioType) { Response resp; try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); string command = "SELECT COUNT(value) FROM \"userSettings\" WHERE key=\'radiotype\'"; object result = null; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { result = cmd.ExecuteScalar(); } if (result.ToString() == "0") { command = $"INSERT INTO \"userSettings\"(user_id, key, value) VALUES ( 0, 'radiotype','{radioType}')"; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { cmd.ExecuteNonQuery(); } resp = Response.done; } else { command = $"UPDATE \"userSettings\" SET value = '{radioType}' WHERE key = 'radiotype'"; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { cmd.ExecuteNonQuery(); } resp = Response.alreadyInDB; } } } catch (Exception ee) { Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source); resp = Response.SQLerror; } return resp; } public string getRadioType() { string resp = ""; try { using (NpgsqlConnection connection = new NpgsqlConnection()) { connection.ConnectionString = getConnectionString(); connection.Open(); string command = "SELECT value FROM \"userSettings\" WHERE key='radiotype'"; using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection)) { resp = cmd.ExecuteScalar().ToString(); } } } catch (Exception ee) { Console.WriteLine(ee.Message, ConsoleColor.Red); } return resp; } public enum Response { done, alreadyInDB, SQLerror, } } }