示例#1
0
        /// <summary>
        /// performs a single update operation on a table
        /// </summary>
        /// <param name="tableName">name of the table to perform the updates within</param>
        /// <param name="values">the columns to update and the values to update them to</param>
        /// <param name="conjunction">defines how the predicates should be joined in the query</param>
        /// <param name="predicates">the predicates for the update operation, all of which must be true for the operation to succeed</param>
        public void Update(string tableName, List <DBPredicate> values, List <DBPredicate> predicates, DBConjunction conjunction = DBConjunction.AND)
        {
            lock ( m_databaseLock )
            {
                _VerifyConnection();

                try
                {
                    m_sqlConnection.Open();

                    SQLiteCommandBuilder builder = new SQLiteCommandBuilder();

                    using (SQLiteCommand command = m_sqlConnection.CreateCommand())
                    {
                        command.CommandText = $"UPDATE {builder.QuoteIdentifier(tableName)} SET(";

                        _AppendPredicates(command, values, ",");

                        command.CommandText += ") WHERE(";

                        _AppendPredicates(command, predicates, conjunction.ToString());

                        command.CommandText += ");";
                        Debug.WriteLine("Executing update query: " + command.CommandText);
                        command.ExecuteNonQuery();
                    }

                    m_sqlConnection.Close();
                }
                catch (Exception crap)
                {
                    m_sqlConnection.Close();
                    throw crap;
                }
            }
        }
示例#2
0
        /// <summary>
        /// Selects all rows from a table where all 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, all of which must evaluate to true</param>
        /// <param name="predicateConjunction">how the list of predicates should be joined when performing the query</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> Select <T>(string tableName, List <DBPredicate> predicates, DBDataContainerCreator <T> dataContainerCreator, DBConjunction predicateConjunction) where T : DBDataContainer <T>
        {
            lock ( m_databaseLock )
            {
                _VerifyConnection();

                List <T> queryResults = new List <T>();

                try
                {
                    m_sqlConnection.Open();

                    using (SQLiteCommand command = m_sqlConnection.CreateCommand())
                    {
                        SQLiteCommandBuilder builder = new SQLiteCommandBuilder();

                        command.CommandText = $"SELECT * FROM {builder.QuoteIdentifier(tableName)} WHERE ";

                        _AppendPredicates(command, predicates, predicateConjunction.ToString());

                        command.CommandText += ";";

                        queryResults = _ExecuteSelectCommand <T>(command, dataContainerCreator);
                    }
                }
                catch (Exception crap)
                {
                    m_sqlConnection.Close();
                    throw crap;
                }
                finally
                {
                    m_sqlConnection.Close();
                }

                return(queryResults);
            }
        }