コード例 #1
0
        public static async Task <Taxon[]> GetTaxaAsync(string name, TaxonRank rank)
        {
            List <Taxon> taxa       = new List <Taxon>();
            string       table_name = _getRankTableName(rank);

            if (string.IsNullOrEmpty(table_name))
            {
                return(null);
            }

            using (SQLiteCommand cmd = new SQLiteCommand(string.Format("SELECT * FROM {0} WHERE name = $name OR common_name = $name", table_name))) {
                cmd.Parameters.AddWithValue("$name", name.ToLower());

                using (DataTable table = await Database.GetRowsAsync(cmd))
                    foreach (DataRow row in table.Rows)
                    {
                        taxa.Add(Taxon.FromDataRow(row, rank));
                    }
            }

            return(taxa.ToArray());
        }
コード例 #2
0
        public static async Task <Species[]> GetSpeciesAsync(Taxon taxon)
        {
            List <Species> species = new List <Species>();

            if (taxon.type == TaxonRank.Species)
            {
                // Return all species with the same name as the taxon.
                species.AddRange(await SpeciesUtils.GetSpeciesAsync("", taxon.name));
            }
            else if (taxon.type == TaxonRank.Genus)
            {
                // Return all species within this genus (rather than recursively calling this function for each species).

                using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Species WHERE genus_id = $genus_id")) {
                    cmd.Parameters.AddWithValue("$genus_id", taxon.id);

                    using (DataTable table = await Database.GetRowsAsync(cmd))
                        foreach (DataRow row in table.Rows)
                        {
                            species.Add(await SpeciesUtils.SpeciesFromDataRow(row));
                        }
                }
            }
            else
            {
                // Get all subtaxa and call this function recursively to get the species from each of them.

                Taxon[] subtaxa = await GetSubtaxaAsync(taxon);

                foreach (Taxon t in subtaxa)
                {
                    species.AddRange(await GetSpeciesAsync(t));
                }
            }

            return(species.ToArray());
        }
コード例 #3
0
        public static Taxon FromDataRow(DataRow row, TaxonRank type)
        {
            Taxon taxon = new Taxon(type)
            {
                id          = row.Field <long>("id"),
                name        = row.Field <string>("name"),
                CommonName  = row.Field <string>("common_name"),
                description = row.Field <string>("description"),
                pics        = row.Field <string>("pics")
            };

            string parent_id_name = TypeToDatabaseColumnName(taxon.GetParentType());

            if (!string.IsNullOrEmpty(parent_id_name))
            {
                taxon.parent_id = (row[parent_id_name] == DBNull.Value) ? 0 : row.Field <long>(parent_id_name);
            }
            else
            {
                taxon.parent_id = -1;
            }

            return(taxon);
        }