public static PagedResult <TResult> PagedQuery <TEntity, TResult>(this IDbConnection con, int pageSize, int pageNumber, Action <SqlExp <TResult> > action, Action <SqlExp <TEntity> > subAction,
                                                                          IDbTransaction trans = null, int?commandTimeout = null, string specialSchema = null) where TEntity : class where TResult : class
        {
            var db     = trans == null ? con : trans.Connection;
            var sqllam = new SqlExp <TEntity>(db.GetAdapter(), specialSchema: specialSchema);

            subAction?.Invoke(sqllam);


            var sqlLamMain = new SqlExp <TResult>(db.GetAdapter(), specialSchema: specialSchema);

            sqlLamMain.SubQuery(sqllam);

            action?.Invoke(sqlLamMain);

            var countSqlam = new SqlExp <TResult>(db.GetAdapter(), true, specialSchema: specialSchema);

            countSqlam.SubQuery(sqllam);

            action?.Invoke(countSqlam);

            countSqlam = countSqlam.CountSubQuery <TResult>(sqlLamMain);
            int countRet  = 0;
            var sqlString = countSqlam.SqlString;

            try
            {
                DebuggingSqlString(sqlString);
                countRet = db.Query <int>(sqlString, countSqlam.Parameters, trans, commandTimeout: commandTimeout).FirstOrDefault();
            }
            catch (Exception ex)
            {
                DebuggingException(ex, sqlString);
                throw new DapperLamException(ex.Message, ex, countSqlam.SqlString)
                      {
                          Parameters = countSqlam.Parameters
                      };
            }

            var sqlstring = sqlLamMain.QuerySubPage(pageSize, pageNumber);

            try
            {
                DebuggingSqlString(sqlstring);
                var retlist = db.Query <TResult>(sqlstring, sqlLamMain.Parameters, trans, commandTimeout: commandTimeout);
                return(new PagedResult <TResult>(retlist, countRet, pageSize, pageNumber));
            }
            catch (Exception ex)
            {
                DebuggingException(ex, sqlstring);
                throw new DapperLamException(ex.Message, ex, sqlstring)
                      {
                          Parameters = sqlLamMain.Parameters
                      };
            }
        }
示例#2
0
        public static PagedResult <TResult> PagedQuery <TEntity, TResult>(this IDbConnection db, int pageSize, int pageNumber, Action <SqlExp <TResult> > action, Action <SqlExp <TEntity> > subAction,
                                                                          IDbTransaction trans = null, int?commandTimeout = null) where TEntity : class where TResult : class
        {
            var sqllam = new SqlExp <TEntity>(db.GetAdapter());

            subAction?.Invoke(sqllam);


            var sqlLamMain = new SqlExp <TResult>(db.GetAdapter());

            sqlLamMain.SubQuery(sqllam);

            action?.Invoke(sqlLamMain);

            var countSqlam = new SqlExp <TResult>(db.GetAdapter(), true);

            countSqlam.SubQuery(sqllam);

            action?.Invoke(countSqlam);

            countSqlam = countSqlam.Count <TResult>(sqlLamMain);
            int countRet = 0;

            try
            {
                countRet = db.Query <int>(countSqlam.SqlString, countSqlam.Parameters, trans, commandTimeout: commandTimeout).FirstOrDefault();
            }
            catch (Exception ex)
            {
                if (Debugger.IsAttached)
                {
                    Debug.WriteLine(ex.Message + ex.StackTrace);
                    Debug.WriteLine(countSqlam.SqlString);
                }

                Console.WriteLine(ex.Message + ex.StackTrace);
                Console.WriteLine(countSqlam.SqlString);
                throw new DapperLamException(ex.Message, ex, countSqlam.SqlString)
                      {
                          Parameters = countSqlam.Parameters
                      };
            }

            var sqlstring = sqlLamMain.QuerySubPage(pageSize, pageNumber);

            try
            {
                var retlist = db.Query <TResult>(sqlstring, sqlLamMain.Parameters, trans, commandTimeout: commandTimeout);
                return(new PagedResult <TResult>(retlist, countRet, pageSize, pageNumber));
            }
            catch (Exception ex)
            {
                if (Debugger.IsAttached)
                {
                    Debug.WriteLine(ex.Message + ex.StackTrace);
                    Debug.WriteLine(sqlstring);
                }

                Console.WriteLine(ex.Message + ex.StackTrace);
                Console.WriteLine(sqlstring);
                throw new DapperLamException(ex.Message, ex, sqlstring)
                      {
                          Parameters = sqlLamMain.Parameters
                      };
            }
        }