使用 Ruby 生成 SQL 查询
我想要一种在 Ruby 中生成 SQL 查询的简单方法。我了解 ActiveRecord、Sequel 和 DataMapper。我不是在寻找 ORM,而是在寻找一种更简单的生成 SQL 语句字符串的方法。
我正在使用 RBHive 运行 Hive 查询,并且希望有一种简单的方法来生成 Hive 查询语句。
当然,这存在(AR等),我已经看过Arel,这看起来很有希望。但不知道如何从流行的 ORM 库中剥离 SQL 语句生成部分。一切都需要连接到数据库服务器。
目前我只使用原始 SQL 字符串,但随着查询变得越来越复杂且容易出错,我想摆脱这种情况。
有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你说
我认为,至少在 Sequel 中你可以使用虚拟数据库:
You said
I think, at least with Sequel you can use a dummy Database:
你是对的,你正在寻找 Arel,它用于为 SQL 语句构建 AST,并且不需要数据库连接。
不过,语法与您习惯看到的有很大不同。所以要注意。
阅读此处的自述文件,看看它对您是否有意义:
https://github.com/rails/arel
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