WhereBuilder
use this tool can build a where sentence for sql, it's can ignore a condition when it's para is nil or black string. my purpose is not check nil for every condition, don't repeat so much if else.
Installation
Add this line to your application's Gemfile:
gem 'where_builder'
And then execute:
$ bundle
Or install it yourself as:
$ gem install where_builder
Usage
require 'where_builder'
f = WhereBuilder::WhereBuilder.new()
where = f.build(
f.add('a.name = ?', 'zd'),
f.OR('a.id=?', 1),
f.AND,
f.bracket(
f.add('key like ?', 'zd'),
f.AND('value=?', '2')
)
)
puts "where=#{where}"
output:
where=["WHERE a.name = ? OR a.id=? AND ( key like ? AND value=?)", ["zd", 1, "zd", "2"]]
=> nil
==============================
where = f.build(
f.add('a.name = b.name'),
f.OR('a.id=?', 1),
f.AND,
f.bracket(
f.add('key like ?', 'zd'),
f.AND('value=?', '2')
)
)
=> ["WHERE a.name = b.name OR a.id=? AND ( key like ? AND value=?)", [1, "zd", "2"]]
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request