public void CopyTo(KeyValuePair <string, string>[] array, int arrayIndex) { if (array == null) { throw new ArgumentNullException(); } else if (arrayIndex < 0) { throw new ArgumentOutOfRangeException(); } else if (array.Rank > 1) { throw new ArgumentException(); } else if (array.Length - arrayIndex < Count) { throw new ArgumentException(); } else { try { List <List <string> > rows = SQLiteUtility.GetQueryResult(connectionString, $"SELECT Key, Value FROM {tableName}").Rows; for (int i = 0; i < rows.Count; i++) { array[arrayIndex + i] = new KeyValuePair <string, string>(rows[i][0], rows[i][1]); } } catch (InvalidCastException e) { throw new ArgumentException(e.Message); } } }
public IEnumerator <KeyValuePair <string, string> > GetEnumerator() { List <List <string> > rows = SQLiteUtility.GetQueryResult(connectionString, $"SELECT Key, Value FROM {tableName}").Rows; for (int i = 0; i < rows.Count; i++) { yield return(new KeyValuePair <string, string>(rows[i][0], rows[i][1])); } }
public bool TryGetValue(string key, [MaybeNullWhen(false)] out string value) { if (SQLiteUtility.IsNull(key)) { throw new ArgumentNullException(); } else if (!ContainsKey(key)) { value = default(string); return(false); } else { value = this[key]; return(true); } }
public bool Remove(string key) { if (SQLiteUtility.IsNull(key)) { throw new ArgumentNullException(); } if (!ContainsKey(key)) { return(false); } else { string commandText = $"DELETE FROM {tableName} WHERE Key = @key"; List <SqliteParameter> parameters = new List <SqliteParameter>(); parameters.Add(new SqliteParameter("@key", key)); SQLiteUtility.ExecuteCommand(connectionString, commandText, parameters); return(true); } }
public void Add(string key, string value) { if (SQLiteUtility.IsNull(key)) { throw new ArgumentNullException(); } else if (ContainsKey(key)) { throw new ArgumentException(); } else { string commandText = $"INSERT INTO {tableName} (Key, Value) VALUES (@key, @value)"; List <SqliteParameter> parameters = new List <SqliteParameter>(); parameters.Add(new SqliteParameter("@key", key)); parameters.Add(new SqliteParameter("@value", value)); SQLiteUtility.ExecuteCommand(connectionString, commandText, parameters); } }
public bool Remove(KeyValuePair <string, string> item) { if (SQLiteUtility.IsNull(item.Key)) { throw new ArgumentNullException(); } if (!Contains(item)) { return(false); } else { string commandText = $"DELETE FROM {tableName} WHERE Key = @key AND Value = @value"; List <SqliteParameter> parameters = new List <SqliteParameter>(); parameters.Add(new SqliteParameter("@key", item.Key)); parameters.Add(new SqliteParameter("@value", item.Value)); SQLiteUtility.ExecuteCommand(connectionString, commandText, parameters); return(true); } }
public string this[string key] { get { if (SQLiteUtility.IsNull(key)) { throw new ArgumentNullException(); } else if (!ContainsKey(key)) { throw new KeyNotFoundException(); } else { string query = $"SELECT Value FROM {tableName} WHERE Key = @key"; List <SqliteParameter> parameters = new List <SqliteParameter>(); parameters.Add(new SqliteParameter("@key", key)); return(SQLiteUtility.GetQueryResult(connectionString, query, parameters).Rows[0][0]); } } set { if (SQLiteUtility.IsNull(key)) { throw new ArgumentNullException(); } else if (!ContainsKey(key)) { Add(key, value); } else { string commandText = $"UPDATE TABLE {tableName} SET Value = @value WHERE Key = @key"; List <SqliteParameter> parameters = new List <SqliteParameter>(); parameters.Add(new SqliteParameter("@key", key)); parameters.Add(new SqliteParameter("@value", value)); SQLiteUtility.ExecuteCommand(connectionString, commandText, parameters); } } }
private void InitializeTable() { string commandText = $"CREATE TABLE IF NOT EXISTS {tableName} (Key text PRIMARY KEY, Value text)"; SQLiteUtility.ExecuteCommand(connectionString, commandText); }
public bool Contains(KeyValuePair <string, string> item) { return(SQLiteUtility.GetQueryResult(connectionString, $"SELECT * FROM {tableName}").Rows.Contains(new List <string>(new string[] { item.Key, item.Value }))); }
public void Clear() { string commandText = $"DELETE FROM {tableName}"; SQLiteUtility.ExecuteCommand(connectionString, commandText); }