BB
(b_b) is SQL Query Builder for Google BigQuery
Install
Add the following line to Gemfile:
gem 'b_b'
and run bundle
from your shell.
To install the gem manually from your shell, run:
gem install b_b
Basic usage
(b_b) can build only SQL SELECT Statement.
BB.select("word", "corpus", "COUNT(word)").
from("publicdata:samples.shakespeare").
where(word_cont: "th").
group(:word, :corpus).
to_sql
Query Reference of BigQuery's query syntax and functions is here.
Examples
L(b_b)z ==============33
SELECT clause
BB.select(:id, :name, :state).to_sql
BB.select("id", "name", "COUNT(*)").to_sql
FROM clause
BB.from("publicdata:samples.shakespeare").to_sql
BB.from("[applogs.events_20120501]", "[applogs.events_20120502]", "[applogs.events_20120503]").to_sql
BB.from("applogs.events_", on: Date.new(2012, 5, 1)).to_sql
BB.from("mydata.people", from: Date.new(2014, 3, 25), to: Date.new(2014, 3, 27)).to_sql
BB.from(BB.from("publicdata:samples.shakespeare"), as: shakespeare).to_sql
JOIN clause
BB.from(:customers, as: :t1).inner_join(:orders, as: :t2).on("t1.customer_id = t2.customer_id").to_sql
BB.from(:customers, as: :t1).join_each(BB.select(:id, :name).from(:orders), as: :t2).on("t1.customer_id = t2.customer_id").to_sql
WHERE clause
BB.where(id: 1..10, name: "donald", flag: false).to_sql
BB.where("id = ? OR name CONTAINS ?", 123, "john").to_sql
BB.where("id = :id AND name <> :name", id: 123, name: "trump").to_sql
BB.where(id_gteq: 123, name_not_cont: "melania").to_sql
BB.where(id: 123).or.where(id: 456).to_sql
BB.not.where(id: 123).or.not.where(id: 456).to_sql
BB.where(id: 123, name: "trump", reduce: :or).to_sql
OMIT RECORD IF clause
BB.omit_record_if("COUNT(payload.pages.page_name) <= ?", 80).to_sql
GROUP BY clause
BB.group(:age, :gender).to_sql
BB.group("ROLLUP(year, is_male)").to_sql
BB.group_each(:age, :gender).to_sql
HAVING clause
BB.having(first_cont: "a", ngram_count_lt: 10000).to_sql
BB.having("first CONTAINS ? AND negram_count < ?", "a", 10000).to_sql
BB.having("first CONTAINS :first AND negram_count < :negram_count", first: "a", negram_count: 10000).to_sql
BB.having(first: "a").or.not.having(first: "b").to_sql
ORDER BY clause
BB.order(:age, :gender).to_sql
BB.order(age: :desc, gender: :asc).to_sql
LIMIT clause
BB.limit(1000).to_sql
BB.limit(1000).offset(500).to_sql
Support
JOINS
Support methods:
- cross_join
- full_outer_join_each
- inner_join
- inner_join_each
- join
- join_each
- left_join
- left_join_each
- left_outer_join
- left_outer_join_each
- right_join
- right_join_each
- right_outer_join
- right_outer_join_each
Suffix of hash keys
For omit_record_if
, where
, having
:
suffix | means | alias | opposite | example |
---|
cont | contains | contains , like | not_cont , not_contains , not_like | BB.where(name_cont: "banana") |
eq | equals | eql , equals | not_eq , not_eql , not_equals | BB.where(id_not_eq: 123) |
gt | greater than | undefined | not_gt | BB.where(id_not_gt: 123) |
gteq | greater than or equals to | undefined | not_gteq | BB.where(id_not_gteq: 123) |
lt | less than | undefined | not_lt | BB.where(id_not_lt: 123) |
lteq | less than or equals to | undefined | not_lteq | BB.where(id_not_lteq: 123) |
Contributing
Here's a quick guide:
-
Fork the repo.
-
Create a thoughtfully-named branch for your changes (git checkout -b my-new-feature
).
-
Install the development dependencies by running bundle install
.
-
Begin by running the tests.
$ bundle exec rspec
-
Implement something.
-
Add tests for your changes.
-
Make the tests pass.
-
Commit your changes (git commit -am 'Add feature/Fix bug/improve something'
)
-
Push the branch up to your fork on GitHub
(git push origin my-new-feature
) and from GitHub submit a pull request to
b_b's master
branch.
License
The gem is available as open source under the terms of the MIT License.