コード例 #1
0
        private string BuildPageSql <TModel>(IQuery <TModel> query, Clause clause)
        {
            var descriptor = TableCache.GetTableDescriptor(typeof(TModel));

            clause.Paging = true;

            var options = descriptor.Options;

            if (query.OrderBys == null || query.OrderBys.Count == 0)
            {
                BuildDefaultOrderBy(clause, descriptor);
            }

            var sqlBuilder = StringBuilderCache.Acquire();

            sqlBuilder.Append(BuildSelectSql(descriptor));
            sqlBuilder.Append(clause.Where);
            sqlBuilder.Append(clause.OrderBy);
            sqlBuilder.Append(options.GetPageSql(query.Skip ?? 0, query.Take ?? 1));
            sqlBuilder.Append(" ;");

            sqlBuilder.Append("SELECT COUNT(1) from ");
            sqlBuilder.Append(options.StartDelimiter);
            sqlBuilder.Append(descriptor.TableName);
            sqlBuilder.Append(options.EndDelimiter);
            sqlBuilder.Append(clause.Where);
            sqlBuilder.Append(" ;");
            return(StringBuilderCache.GetStringAndRelease(sqlBuilder));
        }
コード例 #2
0
        public Clause SelectAsync <TModel>(IQuery <TModel> query)
        {
            var clause = GetSqlClause(query);

            try
            {
                if (query.Skip.HasValue || query.Take.HasValue)
                {
                    var sql = BuildPageSql(query, clause);
                    clause.Sql = sql;
                    return(clause);
                }

                if (string.IsNullOrEmpty(clause.Where))
                {
                    return(clause);
                }

                var descriptor = TableCache.GetTableDescriptor(typeof(TModel));
                var sqlBuilder = StringBuilderCache.Acquire();
                sqlBuilder.Append(this.BuildSelectSql(descriptor));
                sqlBuilder.Append(clause.Where);
                sqlBuilder.Append(clause.OrderBy);
                clause.Sql = StringBuilderCache.GetStringAndRelease(sqlBuilder);
                return(clause);
            }
            finally
            {
                clause.Where   = null;
                clause.OrderBy = null;
            }
        }