示例#1
0
        public virtual int Count(string name, string where = null, long?limit = null, long?offset = null)
        {
            var conditions = QueryPraser.ParseConditoin(where);
            var whereStr   = where == null ? string.Empty : $"WHERE {ConditionsToSql(conditions)}";
            var sql        = $@"SELECT count(*) FROM {WarpField(name)} {whereStr}";
            int count;

            using (var connection = CreateConnection())
            {
                count = connection.Query <int>(sql).FirstOrDefault();
            }

            if (limit.HasValue && count > limit)
            {
                count = (int)limit.Value;
            }

            if (offset.HasValue)
            {
                count -= (int)offset.Value;
                if (count < 0)
                {
                    count = 0;
                }
            }
            ;

            return(count);
        }
示例#2
0
        public static FilterDefinition <object> QueryToFilter(string query)
        {
            var conditions = QueryPraser.ParseConditoin(query);
            var filters    = new List <FilterDefinition <object> >();

            foreach (var condition in conditions)
            {
                var filter = ConditionToFilter(condition);
                filters.Add(filter);
            }
            return(Builders <object> .Filter.And(filters));
        }
示例#3
0
        public virtual object[] QueryData(string name, string where = null, long?limit = null, long?offset = null, string orderBy = null, object @params = null)
        {
            var conditions = QueryPraser.ParseConditoin(where);
            var whereStr   = where == null ? string.Empty : $"WHERE {ConditionsToSql(conditions)}";
            var limitStr   = limit.HasValue ? $"LIMIT {limit}" : string.Empty;
            var orderByStr = orderBy == null ? string.Empty : $"ORDER BY {orderBy}";
            var offsetStr  = offset.HasValue && offset != 0 ? $"OFFSET {offset}" : string.Empty;
            var sql        = $@"SELECT * FROM {WarpField(name)} {whereStr} {orderByStr} {limitStr} {offsetStr}";

            using (var connection = CreateConnection())
            {
                return(connection.Query <object>(sql, @params).ToArray());
            }
        }