public
Description: Enhancement to ActiveRecord::Base#find_by_sql to use external SQL files
Homepage:
Clone URL: git://github.com/jordi/find_by_sql_file.git
jordi (author)
Thu Dec 11 07:21:56 -0800 2008
commit  da29b881948418f7254a20e58a08c453af37ed11
tree    2765ff532beaa2a5f0b93764a7831f363065400b
parent  6847b676486b4e363f25233188a9e13b6f92dc87
name age message
file .gitignore Mon Oct 06 17:16:37 -0700 2008 Add test for count_by_sql [jordi]
file MIT-LICENSE Wed Oct 01 12:37:32 -0700 2008 Initial import [jordi]
file README.markdown Tue Oct 07 07:30:58 -0700 2008 Documentation on gem vs. plugin vs. gemplugin u... [jordi]
file Rakefile Mon Oct 06 17:16:49 -0700 2008 Add RCOV task [jordi]
file WHATSNEW Tue Oct 07 08:09:33 -0700 2008 Keeping WHATSNEW up to date [jordi]
file find_by_sql_file.gemspec Mon Oct 06 17:16:37 -0700 2008 Add test for count_by_sql [jordi]
file init.rb Fri Oct 03 14:59:01 -0700 2008 Fixed a bug that pops up when using it as a Rai... [jordi]
file install.rb Wed Oct 01 12:37:32 -0700 2008 Initial import [jordi]
directory lib/ Thu Dec 11 07:21:56 -0800 2008 Comment bug [jordi]
directory rails/ Fri Oct 03 13:41:41 -0700 2008 The gem should work with or without the github ... [jordi]
directory test/ Tue Oct 07 08:08:40 -0700 2008 Added a test for the query reformatting feature [jordi]
README.markdown

Find by SQL file

This plugin extends the API of ActiveRecord::Base.find_by_sql.

A Warning

Besides the warnings on the ERB section below (don't ignore those), this code is not very well tested (RCOV shows 100% coverage, but that doesn't mean much), and has no track record whatsoever. So there, beware.

Example Usage

Instead of passing the SQL statement as a string...

Elephant.find_by_sql "SELECT * FROM elephants WHERE weight='massive'"

You can pass a symbol that refers to a query file stored in RAILS_ROOT/app/queries/TABLE_NAME/SYMBOL.sql

Elephant.find_by_sql :massive_weight

Installation instructions

Via rubygems:

sudo gem install jordi-find_by_sql_file --source http://gems.github.com

# Then, to use as a GemPlugin in Rails, add to config/environment.rb:
config.gem 'jordi-find_by_sql_file', :source => 'http://gems.github.com'

# Or, to use outside of Rails, for now (better API coming soon):
require 'active_record'
RAILS_ROOT = '/some/folder/'
require 'find_by_sql_file'

As a traditional Rails plugin:

./script/install plugin git://github.com/jordi/find_by_sql_file.git

Motivation

The advantage of the external file approach is that the SQL file can be properly indented and commented (the indentation and comments are stripped from the logs.)

Features & Problems

Comment removal

As far as comment removal, only double-dash-space single-line comments are stripped, like so:

SELECT foo, -- We need this for X reason
       bar, -- and this for some Y reason
       bez,  # This comment will NOT be removed, and will be a problem
       duh  /* And neither will this one. Use -- style only */

       FROM table;

So, to clarify, the start-comment marker is -- (two dashes and a space). That I know of, this marker works in MySQL, PostgreSQL, SQLite, Oracle, DB2, and SQL Server. While not all of these require the space after the dashes, it never hurts.

Bind variables

It's possible to pass named bind variables, much like in the conditions parameter of ActiveRecord::Base.find, by passing a hash as the second parameter, like so:

Elephant.find_by_sql :specifics, :color => 'grey', :weight => 6800

ERB (be careful)

It is also possible to use ERB inside the query file, but beware! Unlike the named bind variables, any data passed in via the ERB method is not properly quoted by the database adapter, leaving open the possibility of SQL injection. 99.9% of the time, you will NOT need this.

Here's an artificial (but easy to explain) example of how the (very dangerous!) ERB feature works:

Elephant.find_by_sql :single_value, :value   => 'grey',
                                    :inject! => { :field => 'color' }

The call above replaces the bind variable :value inside the SQL file, but it also populates the instance variable @field with "color", which can then be used with the usual ERB syntax, like so:

SELECT <%= @field -%> FROM elephants WHERE <%= @field -%> = :value

Legal

Copyright (c) 2008 Jordi Bunster, released under the MIT license