精致的订购方式

发布于 2025-01-07 06:41:53 字数 892 浏览 2 评论 0原文

使用 dapper 时是否有任何原因无法以正确的顺序检索以下代码?

connection.Query<User>("SELECT id, name " +
                       "FROM user " +
                       "ORDER BY @sort @dir " +
                       "LIMIT @offset, @pageSize; ",
                       new {
                           sort = sortOrder, // sortOrder = "name"
                           dir = sortDirection, // sortDirection = "ASC"
                           offset = pageIndex * pageSize, // offset = 0
                           pageSize = pageSize // pageSize = 10
                       });

它总是返回而不应用排序。

我可以像这样将 sortOrder 和 sortDirection 直接放入字符串中

"SELECT id, name " +
"FROM user " +
"ORDER BY " + sortOrder + " " + sortDirection + " " +
"LIMIT @offset, @pageSize; "

,但我不确定这会如何影响 dapper,因为我相信它有自己的查询计划缓存

另外,有没有办法查看 dapper 生成的查询?

Is there any reason why the following code wouldn't be retrieved in the correct order when using dapper?

connection.Query<User>("SELECT id, name " +
                       "FROM user " +
                       "ORDER BY @sort @dir " +
                       "LIMIT @offset, @pageSize; ",
                       new {
                           sort = sortOrder, // sortOrder = "name"
                           dir = sortDirection, // sortDirection = "ASC"
                           offset = pageIndex * pageSize, // offset = 0
                           pageSize = pageSize // pageSize = 10
                       });

It always returns without applying the ordering.

I could just place the sortOrder and sortDirection directly into the string like this

"SELECT id, name " +
"FROM user " +
"ORDER BY " + sortOrder + " " + sortDirection + " " +
"LIMIT @offset, @pageSize; "

but I'm not sure how that will effect dapper since I believe it has it's own query plan caching.

Also, is there a way to view the query that is generated by dapper?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我很OK 2025-01-14 06:41:53

当然,一般数据库引擎不允许您参数化列名称。例如:

var row = cnn.Query("select @bob as col from table", new {bob = "col"}).first(); 
// most likely returns "row.col == col"

当您尝试参数化 order by 子句时,我建议使用内联替换,前提是您可以保证转义,bobby 表始终潜伏着。 (或者你可以使用一个过程)

我不确定(MySQL?Oracle?)的分析器的情况,但你可以使用像 MiniProfiler 查看 SQL。

它会影响缓存,但是 sortOrder 和 sortDirection 的排列数量很少,因此影响很小。

Sure, in general database engines do not allow you to parametrize column names. So for example:

var row = cnn.Query("select @bob as col from table", new {bob = "col"}).first(); 
// most likely returns "row.col == col"

When you are trying to parametrize order by clauses I would recommend using inline substitution, provided you can guarantee your escaping, bobby tables is always lurking. (Either that or you could use a proc)

I am not sure about the situation around profilers for (MySQL? Oracle?) but you could use a tool like MiniProfiler to see the SQL.

It will affect caching, however there are only a small number of permutations of sortOrder and sortDirection so the impact is minimal.

秉烛思 2025-01-14 06:41:53

您可以在订单中使用 case 语句。只要您只处理几列,就不会变得太疯狂。

public List<Signup> GetNext(int Id, int RowsToFetch, string SortedBy)
        {

            string _sortedBy = SortedBy.Trim();
            using (IDbConnection conn = Connection)
            {
                string sQuery = @"SELECT TOP(@ROWSTOFETCH)
                                    [Id]
                                   ,[FirstName]
                                   ,[LastName]
                                   ,[EmailAddress]
                                  FROM [dbo].[vwBaseQuery]
                                  WHERE ID >= @ID
                                  ORDER BY 
                                          CASE WHEN @SORTEDBY = 'Id ASC' THEN Id END ASC,
                                          CASE WHEN @SORTEDBY = 'Id DESC' THEN Id END DESC,
                                          CASE WHEN @SORTEDBY = 'FirstName ASC' THEN FirstName END ASC,
                                          CASE WHEN @SORTEDBY = 'FirstName DESC' THEN FirstName END DESC,
                                          CASE WHEN @SORTEDBY = 'LastName ASC' THEN LastName END ASC,
                                          CASE WHEN @SORTEDBY = 'LastName DESC' THEN LastName END DESC,
                                          CASE WHEN @SORTEDBY = 'EmailAddress ASC' THEN EmailAddress END ASC,
                                          CASE WHEN @SORTEDBY = 'EmailAddress DESC' THEN EmailAddress END DESC";
                conn.Open();

                var result = conn.Query<Signup>(sQuery, new {
                    SORTEDBY = _sortedBy,
                    ROWSTOFETCH = RowsToFetch,
                    ID = Id
                }).ToList();
                return result;
            }
        }

You can just use a case statement in your order by. As long as you're only dealing with a few columns, it won't get too crazy.

public List<Signup> GetNext(int Id, int RowsToFetch, string SortedBy)
        {

            string _sortedBy = SortedBy.Trim();
            using (IDbConnection conn = Connection)
            {
                string sQuery = @"SELECT TOP(@ROWSTOFETCH)
                                    [Id]
                                   ,[FirstName]
                                   ,[LastName]
                                   ,[EmailAddress]
                                  FROM [dbo].[vwBaseQuery]
                                  WHERE ID >= @ID
                                  ORDER BY 
                                          CASE WHEN @SORTEDBY = 'Id ASC' THEN Id END ASC,
                                          CASE WHEN @SORTEDBY = 'Id DESC' THEN Id END DESC,
                                          CASE WHEN @SORTEDBY = 'FirstName ASC' THEN FirstName END ASC,
                                          CASE WHEN @SORTEDBY = 'FirstName DESC' THEN FirstName END DESC,
                                          CASE WHEN @SORTEDBY = 'LastName ASC' THEN LastName END ASC,
                                          CASE WHEN @SORTEDBY = 'LastName DESC' THEN LastName END DESC,
                                          CASE WHEN @SORTEDBY = 'EmailAddress ASC' THEN EmailAddress END ASC,
                                          CASE WHEN @SORTEDBY = 'EmailAddress DESC' THEN EmailAddress END DESC";
                conn.Open();

                var result = conn.Query<Signup>(sQuery, new {
                    SORTEDBY = _sortedBy,
                    ROWSTOFETCH = RowsToFetch,
                    ID = Id
                }).ToList();
                return result;
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文