Beispiel #1
0
    public IEnumerable <CreatureModel> QueryForCreatures(CreatureQueryModel queryModel)
    {
        var queryResult = Creatures;

        if (!string.IsNullOrEmpty(queryModel.MClass))
        {
            queryResult = queryResult.Where(c =>
                                            c.CreatureClass.Equals(queryModel.MClass, StringComparison.OrdinalIgnoreCase))
                          .ToList();
        }

        if (!string.IsNullOrEmpty(queryModel.Family))
        {
            queryResult = queryResult.Where(c =>
                                            c.Family.Equals(queryModel.Family, StringComparison.OrdinalIgnoreCase))
                          .ToList();
        }

        if (!string.IsNullOrEmpty(queryModel.Trait))
        {
            var regEx = new Regex("(" + queryModel.Trait + ")", RegexOptions.IgnoreCase);
            queryResult = queryResult.Where(c => regEx.IsMatch(c.TraitName)).ToList();
        }

        if (queryModel.GetCreatures()?.Any() == true)
        {
            var regEx = new Regex(PatternFromList(queryModel.GetCreatures()), RegexOptions.IgnoreCase);
            queryResult = queryResult.Where(c => regEx.IsMatch(c.CreatureName)).ToList();
        }

        if (queryModel.GetDescription()?.Any() == true)
        {
            var regEx = new Regex(PatternFromList(queryModel.GetDescription()), RegexOptions.IgnoreCase);
            queryResult = queryResult.Where(c => regEx.IsMatch(c.TraitDescription)).ToList();
        }

        return(queryResult);
    }
Beispiel #2
0
    public IEnumerable <CreatureModel> QueryForCreatures(CreatureQueryModel queryModel)
    {
        OpenConnection();
        CreateCommand();

        var preparedStatement = new SqliteCommand
        {
            Connection = (SqliteConnection)connection
        };

        var sb           = new StringBuilder();
        var hasCondition = false;

        if (!string.IsNullOrEmpty(queryModel.MClass))
        {
            hasCondition = true;
            sb.Append("class LIKE $class");
            preparedStatement.Parameters.Add(CreateParameter("class", WrapLike(queryModel.MClass)));
        }

        if (!string.IsNullOrEmpty(queryModel.Family))
        {
            if (hasCondition)
            {
                sb.Append(" AND ");
            }
            hasCondition = true;

            sb.Append("family LIKE $family");
            preparedStatement.Parameters.Add(CreateParameter("family", WrapLike(queryModel.Family)));
        }

        if (!string.IsNullOrEmpty(queryModel.Trait))
        {
            if (hasCondition)
            {
                sb.Append(" AND ");
            }
            hasCondition = true;

            sb.Append("trait LIKE $trait");
            preparedStatement.Parameters.Add(CreateParameter("trait", WrapLike(queryModel.Trait)));
        }

        if (queryModel.GetCreatures()?.Any() == true)
        {
            if (hasCondition)
            {
                sb.Append(" AND ");
            }
            hasCondition = true;

            const string creatureSql = "({creatures})";
            var          parameters  = AddParameterList(
                queryModel.GetCreatures(),
                "creature",
                preparedStatement);
            sb.Append(creatureSql.Replace("{creatures}", parameters));
        }

        if (queryModel.GetDescription()?.Any() == true)
        {
            if (hasCondition)
            {
                sb.Append(" AND ");
            }
            hasCondition = true;

            const string descriptionSql = "({description})";
            var          parameters     = AddParameterList(
                queryModel.GetDescription(),
                "trait_description",
                preparedStatement);
            sb.Append(descriptionSql.Replace("{description}", parameters));
        }

        if (hasCondition)
        {
            sb.Insert(0, " WHERE ");
        }

        sb.Insert(0, Query.SELECT_FROM_MONSTER);

        preparedStatement.CommandText = sb.ToString();
        var reader = preparedStatement.ExecuteReader();

        var monsters = new List <CreatureModel>();

        while (reader.Read())
        {
            try
            {
                monsters.Add(new CreatureModel
                {
                    CreatureClass    = reader.GetSafeString(0),
                    Family           = reader.GetSafeString(1),
                    CreatureName     = reader.GetSafeString(2),
                    TraitName        = reader.GetSafeString(3),
                    TraitDescription = reader.GetSafeString(4),
                    MaterialName     = reader.GetSafeString(5)
                });
            }
            catch (InvalidCastException e)
            {
                Debug.LogError(e);
                Debug.LogError(reader);
                throw;
            }
        }

        Close();

        return(monsters);
    }