/// <summary> /// Selects all rows and columns from the database /// </summary> /// <typeparam name="T"></typeparam> /// <param name="tableName">name of the table to select from</param> /// <returns>a list of data containers of the specified type containing the results, or null if there were no results</returns> public List <T> SelectAll <T>(string tableName, DBDataContainerCreator <T> dataContainerCreator) where T : DBDataContainer <T> { _VerifyConnection(); try { m_sqlConnection.Open(); List <T> results = null; using (SQLiteCommand command = m_sqlConnection.CreateCommand()) { SQLiteCommandBuilder builder = new SQLiteCommandBuilder(); command.CommandText = $"SELECT * FROM {builder.QuoteIdentifier(tableName)};"; results = _ExecuteSelectCommand <T>(command, dataContainerCreator); } m_sqlConnection.Close(); return(results); } catch (Exception crap) { m_sqlConnection.Close(); throw crap; } }
public List <T> _ExecuteSelectCommand <T>(SQLiteCommand command, DBDataContainerCreator <T> dataContainerCreator) where T : DBDataContainer <T> { _VerifyConnection(); try { SQLiteDataReader result = command.ExecuteReader(); if (result.HasRows == false) { return(null); } List <T> resultContainers = new List <T>(); while (result.Read()) { T container = dataContainerCreator.Create(); for (int i = 0; i < result.FieldCount; i++) { container[result.GetName(i)] = result.GetValue(i).ToString(); } resultContainers.Add(container); } return(resultContainers); } catch (Exception crap) { m_sqlConnection.Close(); throw crap; } }
/// <summary> /// Selects all rows from a table where at least one of a list of predicates are true /// </summary> /// <typeparam name="T"></typeparam> /// <param name="tableName">name of the table to select from</param> /// <param name="predicates">list of predicates, one or more of which must evaluate to true</param> /// <returns>a list of data containers, each containing one row of the results, or null if there were no results</returns> public List <T> SelectAny <T>(string tableName, List <DBPredicate> predicates, DBDataContainerCreator <T> dataContainerCreator) where T : DBDataContainer <T> { _VerifyConnection(); using (SQLiteCommand command = _BuildSelectCommand(tableName, predicates, "OR")) { return(_ExecuteSelectCommand <T>(command, dataContainerCreator)); } }