Question

I want an easy way to generate SQL queries in Ruby. I know all about ActiveRecord, Sequel and DataMapper. I'm not looking for an ORM but just an easier way to generate SQL statement strings.

I'm using RBHive to run Hive queries and would like an easy way to generate the Hive query statements.

Surely this exists (AR, etc) and I've looked at Arel, which seems promising. But can't figure out how to strip the SQL statement generation pieces off of the popular ORM libraries. Everything requires a connection to a database server.

Currently I just use raw SQL strings, but I want to get away from that as the queries are becoming more and more complex and error-prone.

Any ideas?

Was it helpful?

Solution

You're right, you're looking for Arel, which is used to build an AST for SQL statements and does not require a database connection.

The syntax is pretty different from what you're used to seeing, though. So be aware.

Read the README here to see if it makes sense to you:

https://github.com/rails/arel

OTHER TIPS

You said

Everything requires a connection to a database server.

I think, at least with Sequel you can use a dummy Database:

require 'sequel'
DB = Sequel::Database.new() #-> <Sequel::Database: >
puts DB[:test].sql          #-> SELECT * FROM "TEST"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top