Exemple #1
0
        public static async Task <Species[]> GetSpeciesAsync(string name)
        {
            GenusSpeciesPair input = _parseGenusAndSpeciesFromUserInput(string.Empty, name);

            if (string.IsNullOrEmpty(input.GenusName))
            {
                // Returns species by name and/or common name.

                List <Species> species = new List <Species>();

                if (!string.IsNullOrEmpty(name))
                {
                    using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Species WHERE name = $name OR common_name = $name OR id IN (SELECT species_id FROM SpeciesCommonNames where name = $name)")) {
                        cmd.Parameters.AddWithValue("$name", input.SpeciesName.ToLower());

                        using (DataTable table = await Database.GetRowsAsync(cmd))
                            foreach (DataRow row in table.Rows)
                            {
                                species.Add(await SpeciesUtils.SpeciesFromDataRow(row));
                            }
                    }
                }

                return(species.ToArray());
            }
            else
            {
                return(await GetSpeciesAsync(input.GenusName, input.SpeciesName));
            }
        }
        public static async Task <Species[]> GetSpeciesAsync(UserInfo userInfo, UserInfoQueryFlags flags = UserInfoQueryFlags.Default)
        {
            string query = userInfo.Id == UserInfo.NullId ?
                           "SELECT * FROM Species WHERE owner = $owner" :
                           "SELECT * FROM Species WHERE user_id = $user_id";

            if (flags.HasFlag(UserInfoQueryFlags.MatchEither))
            {
                query = "SELECT * FROM Species WHERE owner = $owner OR user_id = $user_id";
            }

            List <Species> result = new List <Species>();

            using (SQLiteCommand cmd = new SQLiteCommand(query)) {
                cmd.Parameters.AddWithValue("$owner", userInfo.Username);
                cmd.Parameters.AddWithValue("$user_id", userInfo.Id);

                using (DataTable rows = await Database.GetRowsAsync(cmd)) {
                    foreach (DataRow row in rows.Rows)
                    {
                        result.Add(await SpeciesUtils.SpeciesFromDataRow(row));
                    }

                    result.Sort((lhs, rhs) => lhs.ShortName.CompareTo(rhs.ShortName));
                }
            }

            return(result.ToArray());
        }
Exemple #3
0
        public static async Task <Species> GetSpeciesAsync(long speciesId)
        {
            using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Species WHERE id = $id")) {
                cmd.Parameters.AddWithValue("$id", speciesId);

                DataRow row = await Database.GetRowAsync(cmd);

                return(row is null ? null : await SpeciesUtils.SpeciesFromDataRow(row));
            }
        }
Exemple #4
0
        public static async Task <Species[]> GetSpeciesAsync()
        {
            List <Species> species = new List <Species>();

            using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Species"))
                using (DataTable table = await Database.GetRowsAsync(cmd))
                    foreach (DataRow row in table.Rows)
                    {
                        species.Add(await SpeciesUtils.SpeciesFromDataRow(row));
                    }

            return(species.ToArray());
        }
Exemple #5
0
        public static async Task <Species[]> GetDirectDescendantsAsync(Species species)
        {
            List <Species> result = new List <Species>();

            using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Species WHERE id IN (SELECT species_id FROM Ancestors WHERE ancestor_id = $ancestor_id)")) {
                cmd.Parameters.AddWithValue("$ancestor_id", species.Id);

                using (DataTable rows = await Database.GetRowsAsync(cmd))
                    foreach (DataRow row in rows.Rows)
                    {
                        result.Add(await SpeciesUtils.SpeciesFromDataRow(row));
                    }
            }

            return(result.ToArray());
        }
        public static async Task <Species[]> GetSpeciesAsync(Zone zone)
        {
            List <Species> species = new List <Species>();

            if (zone is null || zone.Id <= 0)
            {
                return(species.ToArray());
            }

            using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Species WHERE id IN (SELECT species_id FROM SpeciesZones WHERE zone_id = $zone_id)")) {
                cmd.Parameters.AddWithValue("$zone_id", zone.Id);

                using (DataTable rows = await Database.GetRowsAsync(cmd))
                    foreach (DataRow row in rows.Rows)
                    {
                        species.Add(await SpeciesUtils.SpeciesFromDataRow(row));
                    }
            }

            return(species.ToArray());
        }
Exemple #7
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());
        }