示例#1
0
        /// <summary>
        /// 执行SQL 查询,返回集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="page">页数</param>
        /// <param name="itemsPerPage">每页数量</param>
        /// <param name="columnSql">查询列 SQL语句</param>
        /// <param name="tableSql">TABLE SQL语句</param>
        /// <param name="orderSql">ORDER BY SQL语句</param>
        /// <param name="whereSql">WHERE SQL语句</param>
        /// <param name="args">SQL 参数</param>
        /// <returns></returns>
        public List <T> SelectSql <T>(long page, long itemsPerPage, string columnSql, string tableSql, string orderSql, string whereSql, params object[] args)
        {
            if (string.IsNullOrWhiteSpace(columnSql))
            {
                throw new ArgumentNullException("columnSql is null.");
            }
            if (string.IsNullOrWhiteSpace(tableSql))
            {
                throw new ArgumentNullException("tableSql is null.");
            }
            if (page <= 0)
            {
                page = 1;
            }
            if (itemsPerPage <= 0)
            {
                itemsPerPage = 20;
            }

            #region 格式化
            columnSql = columnSql.Trim();
            tableSql  = tableSql.Trim();
            orderSql  = orderSql?.Trim();
            whereSql  = whereSql?.Trim();
            if (columnSql.StartsWith("SELECT ", StringComparison.InvariantCultureIgnoreCase))
            {
                columnSql = columnSql.Substring(7);
            }
            if (tableSql.StartsWith("FROM ", StringComparison.InvariantCultureIgnoreCase))
            {
                tableSql = tableSql.Substring(5);
            }
            if (orderSql?.StartsWith("ORDER BY ", StringComparison.InvariantCultureIgnoreCase) ?? false)
            {
                orderSql = orderSql.Substring(9);
            }
            if (whereSql?.StartsWith("WHERE ", StringComparison.InvariantCultureIgnoreCase) ?? false)
            {
                whereSql = whereSql.Substring(6);
            }
            #endregion

            var sql = _provider.CreateSql((int)itemsPerPage, (int)((Math.Max(0, page - 1)) * itemsPerPage), columnSql, tableSql, orderSql, whereSql);
            sql = formatSql(sql);
            if (_usedCacheServiceOnce)
            {
                return(Run <List <T> >(sql, args, () => {
                    return getDatabase().Query <T>(sql, args).ToList();
                }, "Select"));
            }
            return(getDatabase().Query <T>(sql, args).ToList());
        }