- 浏览: 2075768 次
- 性别:
- 来自: NYC
文章分类
- 全部博客 (628)
- Linux (53)
- RubyOnRails (294)
- HTML (8)
- 手册指南 (5)
- Mysql (14)
- PHP (3)
- Rails 汇总 (13)
- 读书 (22)
- plugin 插件介绍与应用 (12)
- Flex (2)
- Ruby技巧 (7)
- Gem包介绍 (1)
- javascript Jquery ext prototype (21)
- IT生活 (6)
- 小工具 (4)
- PHP 部署 drupal (1)
- javascript Jquery sort plugin 插件 (2)
- iphone siri ios (1)
- Ruby On Rails (106)
- 编程概念 (1)
- Unit Test (4)
- Ruby 1.9 (24)
- rake (1)
- Postgresql (6)
- ruby (5)
- respond_to? (1)
- method_missing (1)
- git (8)
- Rspec (1)
- ios (1)
- jquery (1)
- Sinatra (1)
最新评论
-
dadadada2x:
user模型里加上 protected def email ...
流行的权限管理 gem devise的定制 -
Sev7en_jun:
shrekting 写道var pattern = /^(0| ...
强悍的ip格式 正则表达式验证 -
jiasanshou:
好文章!!!
RPM包rpmbuild SPEC文件深度说明 -
寻得乐中乐:
link_to其实就是个a标签,使用css控制,添加一个参数: ...
Rails在link_to中加参数 -
aiafei0001:
完全看不懂,不知所然.能表达清楚一点?
"$ is not defined" 的问题怎么办
For a Rails/SQLServer application I’m working on, I had to deal with pagination with custom queries because of the different joins. The mislav-will_paginate plugin works great for MySQL, but for SQL Server, the paginated query generated by the current SQL Server Adapter (I’m using activerecord-sqlserver-adapter-1.0.0.9250) does not work very well. The current implementation is targetted really for SQL Server 2000 and older versions since these versions do not have support for ROW_NUMBER() method. It is a major pain in the butt to do pagination with these databases. With the newer SQL Sever 2005, the job is a bit easier. Microsoft implemented the ROW_NUMBER() method with a convoluted syntax to have better support for pagination, but it is still a drag because of the weird syntax.
Semergence wrote in his blog about patching the SQLServerAdapter to support pagination. Based on his post, I improved ActiveRecord::ConnectionAdapters::SQLServerAdapter::add_limit_offset! to make the query work in a more general way with free-form queries, e.g. queries ran with the paginate_by_sql() method provided by mislav-will_paginate
Include this script in your environment.rb file, or an external file and “require” the file within environment.rb.
- # monkey-patching SQLServerAdapter to support SQL Server 2005-style pagination
- module ActiveRecord
- module ConnectionAdapters
- class SQLServerAdapter
- def add_limit_offset!(sql, options)
- puts sql
- options[:offset] ||= 0
- options_limit = options[:limit] ? "TOP #{options[:limit]}" : ""
- options[:order] ||= if order_by = sql.match(/ORDER BY(.*$)/i)
- order_by[1]
- else
- sql.match('FROM (.+?)\b')[1] + '.id'
- end
- sql.sub!(/ORDER BY.*$/i, '')
- sql.sub!(/SELECT/i, "SELECT #{options_limit} * FROM ( SELECT ROW_NUMBER() OVER( ORDER BY #{options[:order] } ) AS row_num, ")
- sql << ") AS t WHERE row_num > #{options[:offset]}”
- puts sql
- sql
- end
- end
- end
- end
# monkey-patching SQLServerAdapter to support SQL Server 2005-style pagination module ActiveRecord module ConnectionAdapters class SQLServerAdapter def add_limit_offset!(sql, options) puts sql options[:offset] ||= 0 options_limit = options[:limit] ? "TOP #{options[:limit]}" : "" options[:order] ||= if order_by = sql.match(/ORDER BY(.*$)/i) order_by[1] else sql.match('FROM (.+?)\b')[1] + '.id' end sql.sub!(/ORDER BY.*$/i, '') sql.sub!(/SELECT/i, "SELECT #{options_limit} * FROM ( SELECT ROW_NUMBER() OVER( ORDER BY #{options[:order] } ) AS row_num, ") sql << ") AS t WHERE row_num > #{options[:offset]}” puts sql sql end end end end
The method above monkey-patches the SQLServerAdapter by overwriting the add_limit_offset! method.
Here’s a custom query that I used and the transformed result:
- Resource.paginate_by_sql([
- %!SELECT resources.*
- ,skills_count.skill_count
- FROM resources
- ,(
- SELECT resource_id
- , COUNT(*) AS skill_count
- FROM resource_skills
- WHERE meta_skill_id IN (1,2,3,4,5,6,7,8,9,10)
- GROUP BY resource_id
- ) AS skills_count
- WHERE resources.is_active = ?
- AND resources.id = skills_count.resource_id
- ORDER BY skill_count DESC
- !, true ], :page => page, :per_page => per_page
Resource.paginate_by_sql([ %!SELECT resources.* ,skills_count.skill_count FROM resources ,( SELECT resource_id , COUNT(*) AS skill_count FROM resource_skills WHERE meta_skill_id IN (1,2,3,4,5,6,7,8,9,10) GROUP BY resource_id ) AS skills_count WHERE resources.is_active = ? AND resources.id = skills_count.resource_id ORDER BY skill_count DESC !, true ], :page => page, :per_page => per_page
With :page = 1, :per_page = 2, the resulted SQL is:
- SELECT TOP 2 * FROM ( SELECT ROW_NUMBER() OVER( ORDER BY skill_count DESC ) AS row_num, resources.*
- ,skills_count.skill_count
- FROM resources
- ,(
- SELECT resource_id
- , COUNT(*) AS skill_count
- FROM resource_skills
- WHERE meta_skill_id IN (1,2,3,4,5,6,7,8,9,10)
- GROUP BY resource_id
- ) AS skills_count
- WHERE resources.is_active = 1
- AND resources.id = skills_count.resource_id
- ) AS t WHERE row_num > 0
SELECT TOP 2 * FROM ( SELECT ROW_NUMBER() OVER( ORDER BY skill_count DESC ) AS row_num, resources.* ,skills_count.skill_count FROM resources ,( SELECT resource_id , COUNT(*) AS skill_count FROM resource_skills WHERE meta_skill_id IN (1,2,3,4,5,6,7,8,9,10) GROUP BY resource_id ) AS skills_count WHERE resources.is_active = 1 AND resources.id = skills_count.resource_id ) AS t WHERE row_num > 0
The will_pagination’s COUNT query is
- SELECT COUNT(*) FROM (
- SELECT resources.*
- ,skills_count.skill_count
- FROM resources
- ,(
- SELECT resource_id
- , COUNT(*) AS skill_count
- FROM resource_skills
- WHERE meta_skill_id IN (21,22)
- GROUP BY resource_id
- ) AS skills_count
- WHERE resources.is_active = 1
- AND resources.id = skills_count.resource_id
- ) AS count_table
SELECT COUNT(*) FROM ( SELECT resources.* ,skills_count.skill_count FROM resources ,( SELECT resource_id , COUNT(*) AS skill_count FROM resource_skills WHERE meta_skill_id IN (21,22) GROUP BY resource_id ) AS skills_count WHERE resources.is_active = 1 AND resources.id = skills_count.resource_id ) AS count_table
The ORDER BY part is automatically removed from the main query (which becomes a sub-select) by the plugin to speed up the query. This in turns sanatizes the sql so that SQL Server doesn’t not complain about nested “ORDER BY” within a sub-select. Neat!
The only catch with the current add_limit_offset! is that it does not support ALIAS-ing, because the aliasing confuses the reqex to parse out the ORDER BY condition in the OVER() part of the query.
For regular find() queries, here’s a sample result
- Resource.find(:first)
- # original query: SELECT * FROM resources
- # transformed: SELECT TOP 1 * FROM ( SELECT ROW_NUMBER() OVER( ORDER BY resources.id ) AS row_number, * FROM resources ) AS t WHERE row_num > 0
Resource.find(:first) # original query: SELECT * FROM resources # transformed: SELECT TOP 1 * FROM ( SELECT ROW_NUMBER() OVER( ORDER BY resources.id ) AS row_number, * FROM resources ) AS t WHERE row_num > 0
Hope this helps and cheers!
发表评论
-
eloquent ruby 读书笔记
2012-02-22 07:24 1464节省时间就草记,只记录新鲜,容易忘的 1. case 的判 ... -
Rails 3 in action 读书笔记
2012-02-11 23:54 1365那天看到IE有战友贴了这个书,就想找个时间读读,有收获随笔记下 ... -
The Rails3 Way, Second Edition
2011-04-25 10:23 1693顺便链接一个agile web development wi ... -
the rspec book 下载
2010-09-20 18:17 4337这是写rspec 和 cucumber 一直翻的书,当前最 ... -
电子书推荐 Textmate使用的书
2010-09-07 10:00 1568引用TextMate is a powerful too ... -
ruby电子书资源
2010-08-14 11:49 1757引用 Though we don't like to adm ... -
关于ruby 重构的电子书 资料两本
2010-08-09 20:10 1810Refactoring Ruby Edition 电子书 ... -
pro git 中文版下载
2010-07-06 10:17 11914感觉所有git的问题可以 ... -
peeping into memcached
2009-04-21 23:52 0http://blog.evanweaver.com/arti ... -
Agile Web Development with Rails 3rd 正式版
2009-04-08 13:33 3402Agile Web Development with Rail ... -
REST与Web services
2009-04-01 14:33 3098问题:你如何看待一个房间中两个或多个架构师?回答:是争论。既然 ... -
使用cucumber测试过程中de-bug
2009-04-01 01:51 1478最近,Rails工程中,做测试和集成测试的时候,Cucumbe ... -
关于Rails 2.3 十件你应该知道的事
2009-04-01 01:20 1844首先,这个是一篇粉饰Rails2.3的文章,挑选了十个方面展示 ... -
在Rails 2.3 中测试cookie
2009-03-31 18:48 2009Testing Cookies in Rails 2.3 近 ... -
使用Rspec的好处
2009-03-31 18:44 1969本文将从下面几个方面,来阐述使用Rspec的好处 * ... -
5个使用Rails控制台的有用技巧
2009-03-31 18:36 4826update: 2011/10/26 1. 只想测试不想,真 ... -
The Ruby Programming Language 介绍
2009-03-29 11:35 1640点击这里下载chm part1 ... -
一些个人的在线资料
2009-03-28 11:56 1116MoutainWest Conference 视频的 ... -
从scope说起
2009-03-27 00:42 1257预计将从scope说起 以便理解Rails 2.3支持的d ... -
Ruby1.8和Ruby1.9的不同
2009-03-25 17:13 2286Someone recently emailed the r ...
相关推荐
本主题将深入探讨如何在Rails应用中使用SQL Server作为数据存储,特别关注`activerecord-sqlserver-adapter`这个gem,它使得Ruby的ActiveRecord能够与Microsoft SQL Server无缝集成。 `activerecord-sqlserver-...
"Using-the-PetaPoco-ORM-to-manage-SQLServer-SQLIte.pdf"文档很可能是教程或者指南,详细解释了如何在SQL Server和SQLite数据库上使用PetaPoco。文档可能会涵盖上述所有概念,并提供具体的代码示例。 总结起来,...
在本地开发环境中,可以使用`rails server`命令启动应用。 通过对"rails-playlists-源码"的深入学习,开发者不仅可以理解Rails的基础知识,还能掌握如何在实际项目中运用这些知识,构建出功能完备且易于维护的Web...
接口,目前支持 SqlServer、SQLite、MySql、Access、Firebird、Oracle 等数据库。对于 WEB 开发,它既支持 ASP.NET 2.0 的 DataSource 方 式,也支持 Ruby On Rails 风格的 MVC 方式。 目前,数据库部分已经支持...
对于 ORM 和 Sql 调用,它都拥有清晰和易用的接口,目前支持 SqlServer、SQLite、MySql、Access、Firebird、PostgreSQL、Oracle 等数据库。对于 WEB 开发,它既支持 ASP.NET 2.0 的 DataSource 方式,也支持 Ruby On...
对于 ORM 和 Sql 调用,它都拥有清晰和易用的接口,目前支持 SqlServer、SQLite、MySql、Access、Firebird、Oracle 等数据库。对于 WEB 开发,它既支持 ASP.NET 2.0 的 DataSource 方式,也支持 Ruby On Rails 风格...
在数据库方面,MyBatis-Plus支持常见的数据库系统,比如MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、PostgreSQL和SQL Server等。开发人员可以根据自己的需求选择合适的数据源。 MyBatis-Plus的入门和使用也...
这个项目旨在提升JSP(JavaServer Pages)中JActiveRecord格式的表达式语言能力,从而为开发者带来更高效、更灵活的编程体验。 首先,让我们理解什么是JActiveRecord。JActiveRecord是Java版的ORM(对象关系映射)...
2. 数据库兼容性:与MySQL、PostgreSQL、Oracle和Microsoft SQL Server等多数数据库系统兼容。 3. 遵循惯例优于配置原则:开发者仅需配置与惯例不同的部分,即可开始开发。 4. 跟随Web最佳实践和设计模式:框架遵循...
- **兼容性广泛**:兼容多种数据库系统(如MySQL、PostgreSQL、Oracle、Microsoft SQL Server)及操作系统(如*nix、Windows)。 - **适用范围**: - **企业级应用**:能够适应现有的IT策略和技术架构,适用于...