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



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

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

.connection_poolObject



42
43
44
# File 'lib/sql_runner/query.rb', line 42

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

.inherited(subclass) ⇒ Object



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

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

.plugin(*names) ⇒ Object



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

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

.plugins(*names) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sql_runner/query.rb', line 63

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



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/sql_runner/query.rb', line 75

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



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

def self.query(*value)
  @query = value.first if value.any?
  @query || (@query = File.read(File.join(root_dir, "#{query_name}.sql")))
end

.query_name(*values) ⇒ Object



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

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



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

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



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

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

.root_dir(*value) ⇒ Object



46
47
48
49
# File 'lib/sql_runner/query.rb', line 46

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