百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

sql_query

> 编程语言
开源

Ruby gem,用于使用 ERB 加载来自 `.sql.erb` 模板的 SQL 查询。

162 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

Ruby gem,用于使用 ERB 加载来自 `.sql.erb` 模板的 SQL 查询。

SqlQuery

Ruby gem to load SQL queries from templates using ERB.

It makes working with pure SQL easier with syntax highlighting.

Let's you clean your Ruby code from SQL strings.

Supported extensions: .sql.erb or .erb.sql

Installation

Add this line to your application's Gemfile:

gem 'sql_query'

And then execute:

$ bundle

Or install it yourself as:

$ gem install sql_query

Usage

Create SQL query in file in app/sql_queries directory

sql
# app/sql_queries/get_player_by_email.sql.erb
SELECT *
FROM players
WHERE email = 

quote method is an alias to ActiveRecord.connection.quote method. You can use it to sanitize your variables for SQL.

You can use SQL like this:

ruby
> query = SqlQuery.new(:get_player_by_email, email: '[email protected]')

> query.execute
   (0.6ms)  SELECT * FROM players WHERE email = '[email protected]'
=> []

> query.explain
=> EXPLAIN for:
SELECT *
FROM players
WHERE email = '[email protected]'

                        QUERY PLAN
----------------------------------------------------------
 Seq Scan on players  (cost=0.00..2.14 rows=1 width=5061)
   Filter: ((email)::text = '[email protected]'::text)
(2 rows)

> query.sql
=> "SELECT *\nFROM players\nWHERE email = '[email protected]'\n"

>  query.pretty_sql
=> SELECT *
FROM players
WHERE email = '[email protected]'

initialization

If you need to have nested paths to your queries like player/get_by_email just use string instead of symbol as file name.

Example:

ruby
SqlQuery.new('player/get_by_email', email: '[email protected]')

Special options

  • db_connection - If you want to change default connection to database you may do it for every query execution using this option.
  • sql_file_path - it will override default path where gem will look for sql file.

Methods

  • execute - executes query and returns result data. It accepts boolean argument. When argument is false it will run raw sql query instead of prepared_for_logs.
  • exec_query - similar to #execute but with data returned via ActiveRecord::Result.
  • explain - runs explain for SQL from template
  • sql - returns SQL string
  • pretty_sql - returns SQL string prettier to read in console
  • prepared_for_logs - returns sql string without new lines and multiple whitespaces.

Configuration

ruby
# config/initializers/sql_query.rb
SqlQuery.configure do |config|
  config.path = '/app/sql_templates'
  config.adapter = ActiveRecord::Base
  config.remove_comments = :all              # :none, :oneline, :multiline, :all (default: :all)
  config.remove_comments_from = :all         # :none, :prepared_for_logs, :all (default: :all)
end

Configuration options

  • path - If you don't like default path to your queries you can change it here.

  • adapter - class which implements connection method.

  • remove_comments - Controls which types of SQL comments to remove:

    • :none - Don't remove any comments
    • :oneline - Remove only single-line comments (--)
    • :multiline - Remove only multi-line comments (/* */)
    • :all - Remove both types (default)
  • remove_comments_from - Controls where to apply comment removal:

    • :none - Don't remove comments anywhere
    • :prepared_for_logs - Remove comments only in prepared_for_logs method
    • :all - Remove comments from all queries (default)

Note: Comments within quoted strings (single quotes, double quotes, or PostgreSQL dollar quotes) are always preserved regardless of settings.

Partials

You can prepare part of sql query in partial file and reuse it in multiple queries.

Partial file should start with '_'.

Example:

sql
# app/sql_queries/_email_partial.sql.erb
players.email = 

and use this partial like this:

sql
SELECT *
FROM players
WHERE 

Examples

Check examples folder for some usefull queries.

If you have some examples to share please make pull request.

Contributing

  1. Fork it ( https://github.com/[my-github-username]/sql_query/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

To run specs, setup Postgres with the following:

sql
CREATE DATABASE sqlquery;
CREATE ROLE sqlquery WITH LOGIN;

Issues· 2 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言