コード例 #1
0
        public static IEnumerable <T> Query <T>(this IDbConnection db, Expression <Func <T, bool> > wherExpression = null, IDbTransaction trans = null, int?commandTimeout = null)
        {
            var sqllam = new SqlExp <T>(db.GetAdapter());

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

            var sqlString = sqllam.SqlString;

            try
            {
                DebuggingSqlString(sqlString);

                return(db.Query <T>(sqlString, sqllam.Parameters, trans, commandTimeout: commandTimeout));
            }
            catch (Exception ex)
            {
                DebuggingException(ex, sqlString);
                throw new DapperLamException(ex.Message, ex, sqlString)
                      {
                          Parameters = sqllam.Parameters
                      };
            }
        }
コード例 #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 static Task <T> QueryFirstOrDefaultAsync <T>(this IDbConnection con,
                                                            Expression <Func <T, bool> > wherExpression = null,
                                                            IDbTransaction trans = null, int?commandTimeout = null)
        {
            //return Task.Run(() => { return db.QueryFirstOrDefault<T>(wherExpression, trans, commandTimeout); });
            var db = con;

            if (trans != null)
            {
                db = trans.Connection;
            }
            var sqllam = new SqlExp <T>(db.GetAdapter());

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

            var sqlString = sqllam.SqlString;

            try
            {
                DebuggingSqlString(sqlString);
                return(db.QueryFirstOrDefaultAsync <T>(sqlString, sqllam.Parameters, trans, commandTimeout: commandTimeout));
            }
            catch (Exception ex)
            {
                DebuggingException(ex, sqlString);
                throw new DapperLamException(ex.Message, ex, sqlString)
                      {
                          Parameters = sqllam.Parameters
                      };
            }
        }
コード例 #4
0
        public static Task <int> UpdateAsync <T>(this IDbConnection con, T entity, Expression <Func <T, bool> > whereExpression, IDbTransaction trans = null, int?commandTimeout = null)
        {
            var db = con;

            if (trans != null)
            {
                db = trans.Connection;
            }
            var sqllam = new SqlExp <T>(db.GetAdapter());

            sqllam = sqllam.Update();

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

            var sqlString = sqllam.SqlString;

            try
            {
                DebuggingSqlString(sqlString);
                return(db.ExecuteAsync(sqlString, entity, trans, commandTimeout, CommandType.Text));
            }
            catch (Exception ex)
            {
                DebuggingException(ex, sqlString);
                throw new DapperLamException(ex.Message, ex, sqlString)
                      {
                          Parameters = sqllam.Parameters
                      };
            }
        }
コード例 #5
0
        public static IEnumerable <T> Query <T>(this IDbConnection con, Expression <Func <T, bool> > wherExpression = null, IDbTransaction trans = null, int?commandTimeout = null, string specialSchema = null)
        {
            var db     = trans == null ? con : trans.Connection;
            var sqllam = new SqlExp <T>(db.GetAdapter(), specialSchema: specialSchema);

            var sqlString = string.Empty;


            try
            { if (wherExpression != null)
              {
                  sqllam = sqllam.Where(wherExpression);
              }

              sqlString = sqllam.SqlString;
              DebuggingSqlString(sqlString);

              return(db.Query <T>(sqlString, sqllam.Parameters, trans, commandTimeout: commandTimeout)); }
            catch (Exception ex)
            {
                DebuggingException(ex, sqlString);
                throw new DapperLamException(ex.Message, ex, sqlString)
                      {
                          Parameters = sqllam.Parameters
                      };
            }
        }
コード例 #6
0
        public static T QueryFirstOrDefault <T>(this IDbConnection db, Expression <Func <T, bool> > wherExpression = null, IDbTransaction trans = null, int?commandTimeout = null)
        {
            var sqllam = new SqlExp <T>(db.GetAdapter());

            if (wherExpression != null)
            {
                sqllam = sqllam.Where(wherExpression);
            }
            try
            {
                return(db.QueryFirstOrDefault <T>(sqllam.SqlString, sqllam.Parameters, trans, commandTimeout: commandTimeout));
            }
            catch (Exception ex)
            {
                if (Debugger.IsAttached)
                {
                    Debug.WriteLine(ex.Message + ex.StackTrace);
                    Debug.WriteLine(sqllam.SqlString);
                }

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