Module: SQLRewriter::Hooks::BeforeQuery

Included in:
Adapters::ActiveRecord::Decorator
Defined in:
lib/sql-rewriter/hooks.rb

Instance Method Summary collapse

Instance Method Details

#before_query(sql, binds) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sql-rewriter/hooks.rb', line 17

def before_query(sql, binds)

  begin

    sql_spliced = sql.dup

    binds.map(&:last).each do |bind|
      sql_spliced.sub!('?', '"' + bind + '"')
    end

    parser = SQLParser::Parser.new
    ast = parser.scan_str(sql_spliced)

    res = SQLRewriter.before_hook.call(sql, binds, ast)

    res.to_sql

  rescue

    SQLRewriter.before_hook.call(sql, binds, nil)

  end

end