Пример #1
0
        public override async Task <(bool, string, long, List <Message>)> GetAsync(
            Dictionary <string, Condition[]> dictWhere,
            CSSet setConditionOr = null,
            Dictionary <string, string> dictOrder = null,
            long limit     = 0,
            bool onlyCount = false)
        {
            var(success, error, count, results) = await base.GetAsync(dictWhere, setConditionOr, dictOrder, limit, onlyCount : onlyCount).ConfigureAwait(false);

            if (!success)
            {
                return(false, error, 0, null);
            }

            var messages = new List <Message>();

            foreach (var result in results)
            {
                var message = JsonSerializer.Deserialize <Message>(result.JsonData, GPNJsonSerializer.Option());
                PrepareForClient(message, result);
                messages.Add(message);
            }

            return(true, null, count, messages);
        }
Пример #2
0
        public virtual async Task <(bool, string, long, List <T>)> GetAsync(
            Dictionary <string, Condition[]> dictWhere,
            CSSet setConditionOr = null,
            Dictionary <string, string> dictOrder = null,
            long limit     = 0,
            bool onlyCount = false)
        {
            using var db = _connectionService.DataBase();
            var query      = "";
            var parameters = "";

            try
            {
                // WHERE
                var(strWhere, objParams, strInfo) = Where(Model, dictWhere, setConditionOr);
                parameters = JsonSerializer.Serialize(objParams, GPNJsonSerializer.Option());

                // SELECT COUNT(*)
                var(success, sql) = GetQuery(Model, "SelectCount", db.Database);
                if (!success)
                {
                    return(false, "'SelectCount' command not found", 0, null);
                }

                query     = $"{sql}{strWhere}";
                StopWatch = Stopwatch.StartNew();
                Count     = await db.ExecuteScalarAsync <long>(query, objParams).ConfigureAwait(false);

                LogTime(query, strInfo);
                if (Count == 0)
                {
                    return(true, null, 0, new List <T>());
                }

                if (onlyCount)
                {
                    return(true, null, Count, new List <T>());
                }

                // SELECT
                (success, sql) = GetQuery(Model, "Select", db.Database);
                if (!success)
                {
                    return(false, "'Select' command not found", 0, null);
                }

                // ORDER
                var strOrder = Order(Model, dictOrder);

                query = $"{sql}{strWhere}{strOrder}{(limit == 0 ? "" : $" LIMIT {limit}")}";
                StopWatch.Restart();
                var results = (await db.QueryAsync <T>(query, objParams).ConfigureAwait(false)).ToList();
                LogTime(query, strInfo);
                db.Close();

                return(true, null, Count, results);
            }