Module: DbMod::Statements::Statement

Defined in:
lib/db_mod/statements/statement.rb

Overview

Provides the def_statement function which allows DbMod modules to declare SQL statements that can then be executed later using a specially defined instance method.

To declare prepared statements, see def_prepared in Prepared.

def_statement accepts two parameters:

  • name [Symbol]: The name that will be given to the method that can be used to execute the SQL statement and return the result.

  • sql [String]: The SQL statement that shoul be executed when the method is called. Parameters may be declared using the $ symbol followed by a number ($1, $2, $3) or a name ($one, $two, $under_scores). The two styles may not be mixed in the same statement. The defined function can then be passed parameters that will be used to fill in the statement before execution.

module MyModule
  include DbMod

  def_prepared :my_prepared, "    SELECT *\n      FROM stuff\n     WHERE a = $1 AND b = $2\n  SQL\n\n  def_prepared :my_named_prepared, <<-SQL\n    SELECT *\n      FROM stuff\n     WHERE a = $a AND b = $b\n  SQL\nend\n\ninclude MyModule\ndb_connect db: 'mydb'\nmy_prepared(1,2)\nmy_named_prepared(a: 1, b: 2)\n"

Class Method Summary collapse

Class Method Details

.define_def_statement(mod) ⇒ Object (private)

Add a def_statement method definition to a module. This method allows modules to declare SQL statements that can be accessed via an instance method with arbitrary name.

Parameters:

  • mod (Module)

    a module with DbMod included



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/db_mod/statements/statement.rb', line 62

def self.define_def_statement(mod)
  mod.class.instance_eval do
    define_method(:def_statement) do |name, sql, &block|
      sql = sql.dup
      name = name.to_sym

      params = Parameters.parse_params! sql
      Statement.define_statement_method(mod, name, params, sql, &block)
    end
  end
end

.define_statement_method(mod, name, params, sql) { ... } ⇒ Object (private)

Define a method in the given module with the given name and parameters, that will call the given sql statement and return the results.

Parameters:

  • mod (Module)

    module declaring the method

  • name (Symbol)

    method name

  • params (Fixnum, Array<Symbol>)

    expected parameter count, or a list of argument names. An empty array produces a no-argument method.

Yields:



85
86
87
88
89
90
91
92
# File 'lib/db_mod/statements/statement.rb', line 85

def self.define_statement_method(mod, name, params, sql, &block)
  if params == []
    Configuration.def_configurable(mod, name, ->(*) { query sql }, &block)
  else
    method = ->(*args) { conn.exec_params(sql, args) }
    Configuration.def_configurable(mod, name, method, params, &block)
  end
end

.setup(mod) ⇒ Object

Defines a module-specific def_statement function for a module that has just had DbMod included.

Parameters:

  • mod (Module)


50
51
52
# File 'lib/db_mod/statements/statement.rb', line 50

def self.setup(mod)
  Statement.define_def_statement(mod)
end