public void Count_Column()
        {
            Person    person = new Person();
            Exception ex     = Assert.Throws <InvalidOperationException>(() => SqlExp.Count(person.Name));

            Assert.Equal("Only for expressions.", ex.Message);
        }
예제 #2
0
        public static PagedResult <T> PagedQuery <T>(this IDbConnection db, int pageSize, int pageNumber,
                                                     Expression <Func <T, bool> > whereExpression = null, Expression <Func <T, object> > groupByexpression = null,
                                                     IDbTransaction trans = null, int?commandTimeout = null,
                                                     Expression <Func <T, object> > orderbyExpression = null)
            where T : class
        {
            var sqllam     = new SqlExp <T>(db.GetAdapter());
            var countSqlam = new SqlExp <T>(db.GetAdapter());

            if (whereExpression != null)
            {
                sqllam     = sqllam.Where(whereExpression);
                countSqlam = countSqlam.Where(whereExpression);
            }

            if (orderbyExpression != null)
            {
                sqllam = sqllam.OrderBy(orderbyExpression);
            }

            if (groupByexpression != null)
            {
                sqllam = sqllam.GroupBy(groupByexpression);
            }

            countSqlam = countSqlam.Count();

            int countRet;
            var sqlString = countSqlam.SqlString;

            try {
                DebuggingSqlString(sqlString);
                countRet = db.Query <int>(sqlString, countSqlam.Parameters).FirstOrDefault();
            }
            catch (Exception ex)
            {
                DebuggingException(ex, sqlString);
                throw new DapperLamException(ex.Message, ex, sqlString)
                      {
                          Parameters = countSqlam.Parameters
                      };
            }
            var sqlstring = sqllam.QueryPage(pageSize, pageNumber);

            try
            {
                DebuggingSqlString(sqlstring);
                var retlist = db.Query <T>(sqlstring, sqllam.Parameters, trans, commandTimeout: commandTimeout);

                return(new PagedResult <T>(retlist, countRet, pageSize, pageNumber));
            }
            catch (Exception ex)
            {
                DebuggingException(ex, sqlstring);
                throw new DapperLamException(ex.Message, ex, sqlstring)
                      {
                          Parameters = sqllam.Parameters
                      };
            }
        }
예제 #3
0
        public void Count()
        {
            IFunction func = (IFunction)sql.Val(() => SqlExp.Count());

            QueryResult result = engine.Compile(func);

            Assert.Equal("COUNT(*)", result.Sql);
            Assert.Equal(new Dictionary <string, object>(), result.Parameters);
        }
예제 #4
0
        public static PagedResult <TResult> PagedQuery <T, TResult>(this IDbConnection db, int pageSize, int pageNumber, Action <SqlExp <T> > action, IDbTransaction trans = null, int?commandTimeout = null) where T : class  where TResult : class
        {
            var sqllam = new SqlExp <T>(db.GetAdapter());

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

            action?.Invoke(sqllam);

            action?.Invoke(countSqlam);

            countSqlam = countSqlam.Count();

            int countRet;

            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 = sqllam.QueryPage(pageSize, pageNumber);

            try
            {
                var retlist = db.Query <TResult>(sqlstring, sqllam.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 = sqllam.Parameters
                      };
            }
        }
예제 #5
0
        public void Count_Column()
        {
            Person    person = null;
            IFunction func   = (IFunction)sql.Val(() => SqlExp.Count(person.Name));

            QueryResult result = engine.Compile(func);

            Assert.Equal("COUNT(\"person\".\"Name\")", result.Sql);
            Assert.Equal(new Dictionary <string, object>(), result.Parameters);
        }
예제 #6
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;
            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
                      };
            }
        }
예제 #7
0
        public void Count_Column()
        {
            engine.AddFunction(FunctionName.Concat, FunctionHelper.ConcatOr);

            Person    person = null;
            IFunction func   = (IFunction)sql.Val(() => SqlExp.Count(person.Name));

            QueryResult result = engine.Compile(func);

            Assert.Equal("COUNT(`person`.`Name`)", result.Sql);
            Assert.Equal(new Dictionary <string, object>(), result.Parameters);
        }
        public static PagedResult <TResult> PagedQuery <T, TResult>(this IDbConnection con, int pageSize, int pageNumber, Action <SqlExp <T> > action, IDbTransaction trans = null, int?commandTimeout = null, string specialSchema = null) where T : class where TResult : class
        {
            var db     = trans == null ? con : trans.Connection;
            var sqllam = new SqlExp <T>(db.GetAdapter(), specialSchema: specialSchema);

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

            action?.Invoke(sqllam);

            action?.Invoke(countSqlam);

            countSqlam = countSqlam.Count();

            int countRet;

            var sqlString = countSqlam.SqlString;

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

            var sqlstring = sqllam.QueryPage(pageSize, pageNumber);

            try
            {
                DebuggingSqlString(sqlstring);
                var retlist = db.Query <TResult>(sqlstring, sqllam.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 = sqllam.Parameters
                      };
            }
        }
        public void Count()
        {
            Exception ex = Assert.Throws <InvalidOperationException>(() => SqlExp.Count());

            Assert.Equal("Only for expressions.", ex.Message);
        }
예제 #10
0
 public object Method()
 {
     return(sql.Val(() => SqlExp.Count()));
 }