public static async Task <ITaxon> CreateTaxonFromDataRowAsync(this SQLiteDatabase database, DataRow row, TaxonRankType rank) { ITaxon taxon = new Taxon(rank, row.Field <string>("name")) { Id = row.Field <long>("id"), Description = row.Field <string>("description") }; if (!row.IsNull("common_name") && !string.IsNullOrWhiteSpace(row.Field <string>("common_name"))) { taxon.CommonNames.Add(row.Field <string>("common_name")); } if (!row.IsNull("pics") && !string.IsNullOrWhiteSpace(row.Field <string>("pics"))) { taxon.Pictures.Add(new Picture(row.Field <string>("pics"))); } string parentIdFieldName = GetFieldNameForRank(TaxonUtilities.GetParentRank(rank)); if (!string.IsNullOrEmpty(parentIdFieldName) && !row.IsNull(parentIdFieldName)) { taxon.ParentId = row.Field <long>(parentIdFieldName); } return(await Task.FromResult(taxon)); }
// Public members public static async Task AddTaxonAsync(this SQLiteDatabase database, ITaxon taxon) { string tableName = GetTableNameForRank(taxon.Rank.Type); if (!string.IsNullOrEmpty(tableName)) { string parentFieldName = GetFieldNameForRank(TaxonUtilities.GetParentRank(taxon.Rank.Type)); string query; if (!string.IsNullOrEmpty(parentFieldName) && taxon.ParentId > 0) { query = string.Format("INSERT OR IGNORE INTO {0}(name, description, pics, {1}) VALUES($name, $description, $pics, $parent_id)", tableName, parentFieldName); } else { query = string.Format("INSERT OR IGNORE INTO {0}(name, description, pics) VALUES($name, $description, $pics)", tableName); } using (SQLiteCommand cmd = new SQLiteCommand(query)) { cmd.Parameters.AddWithValue("$name", taxon.Name.ToLowerInvariant()); cmd.Parameters.AddWithValue("$description", taxon.Description); cmd.Parameters.AddWithValue("$pics", taxon.Pictures.FirstOrDefault()?.Url); if (!string.IsNullOrEmpty(parentFieldName) && taxon.ParentId > 0) { cmd.Parameters.AddWithValue("$parent_column", parentFieldName); cmd.Parameters.AddWithValue("$parent_id", taxon.ParentId); } await database.ExecuteNonQueryAsync(cmd); } } }
public TaxonRank(string rankName) : this(TaxonUtilities.GetRankFromRankName(rankName)) { if (!string.IsNullOrEmpty(rankName)) { _rankName = rankName.Trim().ToLower(); } }
private static string GetFieldNameForRank(TaxonRankType rank) { if (rank <= 0) { return(string.Empty); } return(string.Format("{0}_id", TaxonUtilities.GetNameForRank(rank).ToLowerInvariant())); }
// Private members private static string GetTableNameForRank(TaxonRankType rank) { string tableName = TaxonUtilities.GetNameForRank(rank).ToTitle(); if (tableName.Equals("Order")) { tableName = "Ord"; } return(tableName); }
public static TaxonRankType GetParentRank(this TaxonRankType rankType) { return(TaxonUtilities.GetParentRank(rankType)); }
public static string GetName(this TaxonRankType rankType, bool plural = false) { return(TaxonUtilities.GetNameForRank(rankType, plural)); }
public static TaxonRankType GetParentRank(this ITaxon taxon) { return(TaxonUtilities.GetParentRank(taxon.GetRank())); }