Example #1
0
        public static async Task UpdateTaxonAsync(this SQLiteDatabase database, ITaxon taxon)
        {
            string tableName = GetTableNameForRank(taxon.GetRank());

            if (!string.IsNullOrEmpty(tableName))
            {
                string parentColumnName      = GetFieldNameForRank(taxon.GetParentRank());
                string updateParentColumnStr = string.Empty;

                if (!string.IsNullOrEmpty(parentColumnName) && taxon.ParentId.HasValue)
                {
                    updateParentColumnStr = string.Format(", {0}=$parent_id", parentColumnName);
                }

                using (SQLiteCommand cmd = new SQLiteCommand(string.Format("UPDATE {0} SET name = $name, description = $description, pics = $pics{1}, common_name = $common_name WHERE id = $id",
                                                                           tableName, updateParentColumnStr))) {
                    cmd.Parameters.AddWithValue("$id", taxon.Id);

                    cmd.Parameters.AddWithValue("$name", taxon.Name.ToLowerInvariant());
                    cmd.Parameters.AddWithValue("$description", taxon.Description);
                    cmd.Parameters.AddWithValue("$pics", taxon.GetPictureUrl());

                    // Because this field was added in a database update, it's possible for it to be null rather than the empty string.

                    cmd.Parameters.AddWithValue("$common_name", string.IsNullOrEmpty(taxon.GetCommonName()) ? "" : taxon.GetCommonName().ToLowerInvariant());

                    if (!string.IsNullOrEmpty(parentColumnName) && taxon.ParentId.HasValue)
                    {
                        cmd.Parameters.AddWithValue("$parent_column_name", parentColumnName);
                        cmd.Parameters.AddWithValue("$parent_id", taxon.ParentId);
                    }

                    await database.ExecuteNonQueryAsync(cmd);
                }
            }
        }