예제 #1
0
        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));
        }
예제 #2
0
        // 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);
                }
            }
        }
예제 #3
0
 public TaxonRank(string rankName) :
     this(TaxonUtilities.GetRankFromRankName(rankName))
 {
     if (!string.IsNullOrEmpty(rankName))
     {
         _rankName = rankName.Trim().ToLower();
     }
 }
예제 #4
0
        private static string GetFieldNameForRank(TaxonRankType rank)
        {
            if (rank <= 0)
            {
                return(string.Empty);
            }

            return(string.Format("{0}_id", TaxonUtilities.GetNameForRank(rank).ToLowerInvariant()));
        }
예제 #5
0
        // Private members

        private static string GetTableNameForRank(TaxonRankType rank)
        {
            string tableName = TaxonUtilities.GetNameForRank(rank).ToTitle();

            if (tableName.Equals("Order"))
            {
                tableName = "Ord";
            }

            return(tableName);
        }
예제 #6
0
 public static TaxonRankType GetParentRank(this TaxonRankType rankType)
 {
     return(TaxonUtilities.GetParentRank(rankType));
 }
예제 #7
0
 public static string GetName(this TaxonRankType rankType, bool plural = false)
 {
     return(TaxonUtilities.GetNameForRank(rankType, plural));
 }
예제 #8
0
 public static TaxonRankType GetParentRank(this ITaxon taxon)
 {
     return(TaxonUtilities.GetParentRank(taxon.GetRank()));
 }