Over a past year and a half I extensively used only dapper to query databases. Plain dapper with sql builder does all the tricks we needed so far, one day when refactoring larger query I decided to look if dapper sql builder is extendable, to my suprise it is. With just a few lines of code I managed to combine two sql builder queries one for selecting one page of content and another for selecting total page count into single one by creating extensions for page. In order to use below extension in query just add /**page**/

 

    public class CustomSqlBuilder : SqlBuilder
    {
        private bool _pagingAdded;

        public SqlBuilder Page(int offset, int count)
        {
            if (_pagingAdded)
                throw new CsQueryBuilderException("Paging can be added only once");

            _pagingAdded = true;
            return AddClause("page", "offset @offset limit @count", new { count, offset }, "", "", "\n", false);
        }

        public SqlBuilder With(string sql, dynamic? parameters = null) =>
            AddClause("with", sql, parameters, " , ", "WITH", "\n", false);
    }

 

Check out my other projects
Active forks newsletter
Get a glimpse on open source active forks. Weekly newsletter with selection of repositories and their active forks.

Comments


Comments are closed