Class: SQLRunner::Query

Inherits:
Object
  • Object
show all
Extended by:
Runner
Defined in:
lib/sql_runner/query.rb,
lib/sql_runner/query/one.rb,
lib/sql_runner/query/many.rb,
lib/sql_runner/query/model.rb

Defined Under Namespace

Modules: Many, Model, One

Class Method Summary collapse

Methods included from Runner

execute

Methods included from Connection

#connect, #connection_pool, #disconnect, #with_connection

Class Method Details

.call(**bind_vars) ⇒ Object



59
60
61
# File 'lib/sql_runner/query.rb', line 59

def self.call(**bind_vars)
  execute(query, **bind_vars)
end

.connection_poolObject



50
51
52
# File 'lib/sql_runner/query.rb', line 50

def self.connection_pool
  @connection_pool || SQLRunner.connection_pool
end

.find_query_fileObject



43
44
45
46
47
48
# File 'lib/sql_runner/query.rb', line 43

def self.find_query_file
  [
    File.join(root_dir, "#{query_name}.psql"),
    File.join(root_dir, "#{query_name}.sql")
  ].find {|file| File.file?(file) }
end

.inherited(subclass) ⇒ Object



16
17
18
19
20
# File 'lib/sql_runner/query.rb', line 16

def self.inherited(subclass)
  super
  subclass.instance_variable_set(:@connection_pool, @connection_pool)
  subclass.instance_variable_set(:@root_dir, @root_dir)
end

.plugin(*names) ⇒ Object



67
68
69
# File 'lib/sql_runner/query.rb', line 67

def self.plugin(*names)
  plugins(*names)
end

.plugins(*names) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sql_runner/query.rb', line 71

def self.plugins(*names)
  names = prepare_plugins_with_options(names)

  names.each do |name, options|
    plugin = SQLRunner.plugin_registry.fetch(name) do
      raise PluginNotFound, "#{name.inspect} wasn't found"
    end

    plugin.activate(self, options)
  end
end

.prepare_plugins_with_options(plugins) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sql_runner/query.rb', line 83

def self.prepare_plugins_with_options(plugins)
  return plugins unless plugins.last.is_a?(Hash)

  plugins_with_options = plugins.pop

  plugins_with_options.each do |(name, options)|
    plugins << [name.to_sym, options]
  end

  plugins
end

.query(*value) ⇒ Object



38
39
40
41
# File 'lib/sql_runner/query.rb', line 38

def self.query(*value)
  @query = value.first if value.any?
  @query || (@query = File.read(find_query_file))
end

.query_name(*values) ⇒ Object



22
23
24
25
# File 'lib/sql_runner/query.rb', line 22

def self.query_name(*values)
  @query_name = values.first if values.any?
  @query_name || (@query_name = query_name_from_class)
end

.query_name_from_classObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/sql_runner/query.rb', line 27

def self.query_name_from_class
  replacer = proc do
    "#{Regexp.last_match(1)}_#{Regexp.last_match(2).downcase}"
  end

  name
    .gsub("::", "/")
    .gsub(/([a-z0-9])([A-Z])/, &replacer)
    .downcase
end

.register_plugin(name, mod) ⇒ Object



63
64
65
# File 'lib/sql_runner/query.rb', line 63

def self.register_plugin(name, mod)
  SQLRunner.plugin_registry[name] = mod
end

.root_dir(*value) ⇒ Object



54
55
56
57
# File 'lib/sql_runner/query.rb', line 54

def self.root_dir(*value)
  @root_dir = value.first if value.any?
  @root_dir || SQLRunner.root_dir
end