Beispiel #1
0
        public static DynamicSql SqlProjectFields(
            this DynamicSql query, AppUserQueryProjection model)
        {
            query = DynamicSql.DeepClone(query);
            var finalFields = model.GetFieldsArr()
                              .Where(f => AppUserQueryProjection.Projections.ContainsKey(f))
                              .Select(f => AppUserQueryProjection.Projections[f]);

            if (finalFields.Any())
            {
                var projectionClause = string.Join(',', finalFields);
                query.DynamicForm = query.DynamicForm
                                    .Replace(DynamicSql.PROJECTION, projectionClause);
            }
            var finalResults = model.GetFieldsArr()
                               .Where(f => AppUserQueryProjection.Results.ContainsKey(f))
                               .Select(f => AppUserQueryProjection.Results[f]);

            query.MultiResults.AddRange(finalResults);
            return(query);
        }
Beispiel #2
0
 public static IQueryable <AppUser> Project(
     this IQueryable <AppUser> query, AppUserQueryProjection projection)
 {
     foreach (var f in projection.GetFieldsArr())
     {
         if (AppUserQueryProjection.MAPS.ContainsKey(f))
         {
             foreach (var prop in AppUserQueryProjection.MAPS[f])
             {
                 query = query.Include(prop);
             }
         }
     }
     return(query);
 }
Beispiel #3
0
        public static DynamicSql SqlJoin(
            this DynamicSql query, AppUserQueryProjection model)
        {
            query = DynamicSql.DeepClone(query);
            var joins = model.GetFieldsArr()
                        .Where(f => AppUserQueryProjection.Joins.ContainsKey(f))
                        .Select(f => AppUserQueryProjection.Joins[f]);

            if (joins.Any())
            {
                var joinClause = string.Join('\n', joins);
                query.DynamicForm = query.DynamicForm
                                    .Replace(DynamicSql.JOIN, joinClause);
            }
            return(query);
        }
Beispiel #4
0
        public IDictionary <string, object> GetAppUserDynamic(
            AppUser row, AppUserQueryProjection projection,
            AppUserQueryOptions options)
        {
            var obj = new Dictionary <string, object>();

            foreach (var f in projection.GetFieldsArr())
            {
                switch (f)
                {
                case AppUserQueryProjection.INFO:
                {
                    var entity = row;
                    obj["id"]        = entity.Id;
                    obj["username"]  = entity.UserName;
                    obj["full_name"] = entity.FullName;
                }
                break;

                case AppUserQueryProjection.SELECT:
                {
                    var entity = row;
                    obj["id"]        = entity.Id;
                    obj["username"]  = entity.UserName;
                    obj["full_name"] = entity.FullName;
                }
                break;

                case AppUserQueryProjection.ROLE:
                {
                    var entity = row.UserRoles.FirstOrDefault()?.Role;
                    if (entity != null)
                    {
                        obj["role"] = new
                        {
                            id   = entity.Id,
                            name = entity.Name
                        }
                    }
                    ;
                }
                break;
                }
            }
            return(obj);
        }
Beispiel #5
0
        public static DynamicSql SqlExtras(
            this DynamicSql query, AppUserQueryProjection model)
        {
            query = DynamicSql.DeepClone(query);
            var extras = model.GetFieldsArr()
                         .Where(f => AppUserQueryProjection.Extras.ContainsKey(f))
                         .Select(f => AppUserQueryProjection.Extras[f]);

            if (extras.Any())
            {
                var extraSqls     = string.Join(';', extras);
                var originalQuery = query.PreparedViewForm;
                query.DynamicForm += ";\n" + extraSqls;
                query.DynamicForm  = query.DynamicForm
                                     .Replace(AppUserQueryPlaceholder.USER_SUB_QUERY, originalQuery);
            }
            return(query);
        }
Beispiel #6
0
        public IDictionary <string, object> GetAppUserDynamic(
            AppUser row, AppUserQueryProjection projection,
            AppUserQueryOptions options)
        {
            var obj = new Dictionary <string, object>();

            foreach (var f in projection.GetFieldsArr())
            {
                switch (f)
                {
                case AppUserQueryProjection.INFO:
                {
                    var entity = row;
                    obj["id"]        = entity.Id;
                    obj["username"]  = entity.UserName;
                    obj["full_name"] = entity.FullName;
                }
                break;
                }
            }
            return(obj);
        }
Beispiel #7
0
        public IDictionary <string, object> GetAppUserDynamic(
            AppUserQueryRow row, AppUserQueryProjection projection,
            AppUserQueryOptions options, string currentAccId = null)
        {
            var obj = new Dictionary <string, object>();

            foreach (var f in projection.GetFieldsArr())
            {
                switch (f)
                {
                case AppUserQueryProjection.INFO:
                {
                    var entity = row.AppUser;
                    obj["username"]          = entity.UserName;
                    obj["full_name"]         = entity.FullName;
                    obj["phone_number"]      = entity.PhoneNumber;
                    obj["current_logged_in"] = entity.Id == currentAccId;
                }
                break;

                case AppUserQueryProjection.ROLES:
                {
                    var entities = row.AppUser.UserRoles?
                                   .Select(o => o.Role);
                    obj["roles"] = entities?.Select(o => new
                        {
                            name         = o.Name,
                            display_name = o.DisplayName,
                            role_type    = o.RoleType
                        }).ToList();
                }
                break;
                }
            }
            return(obj);
        }
Beispiel #8
0
        public async Task <QueryResult <AppUserQueryRow> > QueryAppUser(
            AppUserQueryFilter filter         = null,
            AppUserQuerySort sort             = null,
            AppUserQueryProjection projection = null,
            AppUserQueryPaging paging         = null,
            AppUserQueryOptions options       = null)
        {
            var conn     = context.Database.GetDbConnection();
            var openConn = conn.OpenAsync();
            var query    = AppUserQuery.CreateDynamicSql();

            #region General
            if (filter != null)
            {
                query = query.SqlFilter(filter);
            }
            if (projection != null)
            {
                query = query.SqlJoin(projection);
            }
            DynamicSql countQuery = null; int?totalCount = null; Task <int> countTask = null;
            if (options != null && options.count_total)
            {
                countQuery = query.SqlCount("*");
            }
            if (projection != null)
            {
                query = query.SqlProjectFields(projection);
            }
            #endregion
            await openConn;
            if (options != null && !options.single_only)
            {
                #region List query
                if (sort != null)
                {
                    query = query.SqlSort(sort);
                }
                if (paging != null && (!options.load_all || !AppUserQueryOptions.IsLoadAllAllowed))
                {
                    query = query.SqlSelectPage(paging.page, paging.limit);
                }
                #endregion
                #region Count query
                if (options.count_total)
                {
                    countTask = conn.ExecuteScalarAsync <int>(
                        sql: countQuery.PreparedForm,
                        param: countQuery.DynamicParameters);
                }
                #endregion
            }
            if (projection != null)
            {
                query = query.SqlExtras(projection);
            }
            var multipleResult = await conn.QueryMultipleAsync(
                sql : query.PreparedForm,
                param : query.DynamicParameters);

            using (multipleResult)
            {
                var queryResult = multipleResult.Read(
                    types: query.GetTypesArr(),
                    map: (objs) => ProcessMultiResults(query, objs),
                    splitOn: string.Join(',', query.GetSplitOns()));
                if (projection != null)
                {
                    var extraKeys = projection.GetFieldsArr()
                                    .Where(f => AppUserQueryProjection.Extras.ContainsKey(f));
                    IEnumerable <AppUserRoleQueryRow> userRoles = null;
                    foreach (var key in extraKeys)
                    {
                        switch (key)
                        {
                        case AppUserQueryProjection.ROLES:
                            userRoles = GetAppUserRoleQueryResult(multipleResult);
                            break;
                        }
                    }
                    ProcessExtras(queryResult, userRoles);
                }
                if (options != null && options.single_only)
                {
                    var single = queryResult.FirstOrDefault();
                    if (single == null)
                    {
                        return(null);
                    }
                    return(new QueryResult <AppUserQueryRow>
                    {
                        SingleResult = single
                    });
                }
                if (options != null && options.count_total)
                {
                    totalCount = await countTask;
                }
                return(new QueryResult <AppUserQueryRow>
                {
                    Results = queryResult,
                    TotalCount = totalCount
                });
            }
        }