Module: PgDecorator::Decorator

Defined in:
lib/pg_decorator/decorator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(instrumented_class) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pg_decorator/decorator.rb', line 3

def self.included(instrumented_class)
  instrumented_class.class_eval do
    alias_method :initialize_without_sql_decorator, :initialize
    alias_method :initialize, :initialize_with_sql_decorator

    alias_method :async_exec_without_sql_decorator, :async_exec
    alias_method :async_exec, :async_exec_with_sql_decorator

    alias_method :async_query_without_sql_decorator, :async_query
    alias_method :async_query, :async_query_with_sql_decorator

    alias_method :exec_without_sql_decorator, :exec
    alias_method :exec, :exec_with_sql_decorator

    alias_method :query_without_sql_decorator, :query
    alias_method :query, :query_with_sql_decorator

    alias_method :prepare_without_sql_decorator, :prepare
    alias_method :prepare, :prepare_with_sql_decorator

    alias_method :send_prepare_without_sql_decorator, :send_prepare
    alias_method :send_prepare, :send_prepare_with_sql_decorator

    alias_method :send_query_without_sql_decorator, :send_query
    alias_method :send_query, :send_query_with_sql_decorator
  end
end

Instance Method Details

#async_exec_with_sql_decorator(sql, *args, &block) ⇒ Object



53
54
55
# File 'lib/pg_decorator/decorator.rb', line 53

def async_exec_with_sql_decorator(sql,*args, &block)
  async_exec_without_sql_decorator(decorate_sql(sql), *args, &block)
end

#async_query_with_sql_decorator(sql, *args, &block) ⇒ Object



65
66
67
# File 'lib/pg_decorator/decorator.rb', line 65

def async_query_with_sql_decorator(sql, *args, &block)
  async_query_without_sql_decorator(decorate_sql(sql), *args, &block)
end

#decorate_sql(sql) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pg_decorator/decorator.rb', line 31

def decorate_sql(sql)
  last_line = caller.detect{|l|  l !~ /\.?(rvm|gem|vendor|rbenv|pg_decorator)/ }
  if last_line
    if last_line.start_with? app_root
      last_line = last_line[app_root.length..-1]
    end
    "/* #{ escape_string(app_name) } #{escape_comment(last_line)} */\n#{sql}"
  else
    "/* #{ escape_string(app_name) } outside code tree */\n#{sql}"
  end
end

#escape_comment(str) ⇒ Object



43
44
45
# File 'lib/pg_decorator/decorator.rb', line 43

def escape_comment(str)
  escape_string( str.gsub('*/','* /'))
end

#exec_with_sql_decorator(sql, *args, &block) ⇒ Object



57
58
59
# File 'lib/pg_decorator/decorator.rb', line 57

def exec_with_sql_decorator(sql,*args, &block)
  exec_without_sql_decorator(decorate_sql(sql), *args, &block)
end

#initialize_with_sql_decorator(*args, &block) ⇒ Object



47
48
49
50
51
# File 'lib/pg_decorator/decorator.rb', line 47

def initialize_with_sql_decorator(*args, &block)
  initialize_without_sql_decorator(*args, &block)
  exec_without_sql_decorator("set application_name = '#{escape_string(self.app_name)}';") {true}
  self
end

#prepare_with_sql_decorator(stmt_name, sql, *args, &block) ⇒ Object



69
70
71
# File 'lib/pg_decorator/decorator.rb', line 69

def prepare_with_sql_decorator(stmt_name, sql, *args, &block)
  prepare_without_sql_decorator(stmt_name, decorate_sql(sql), *args, &block)
end

#query_with_sql_decorator(sql, *args, &block) ⇒ Object



61
62
63
# File 'lib/pg_decorator/decorator.rb', line 61

def query_with_sql_decorator(sql, *args, &block)
  query_without_sql_decorator(decorate_sql(sql), *args, &block)
end

#send_prepare_with_sql_decorator(stmt_name, sql, *args, &block) ⇒ Object



73
74
75
# File 'lib/pg_decorator/decorator.rb', line 73

def send_prepare_with_sql_decorator(stmt_name, sql, *args, &block)
  send_prepare_without_sql_decorator(stmt_name, decorate_sql(sql), *args, &block)
end

#send_query_with_sql_decorator(sql, *args, &block) ⇒ Object



77
78
79
# File 'lib/pg_decorator/decorator.rb', line 77

def send_query_with_sql_decorator(sql, *args, &block)
  send_query_without_sql_decorator(decorate_sql(sql), *args, &block)
end