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|
      sql = sql.dup
      name = name.to_sym

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

.define_fixed_args_statement_method(mod, name, count, sql) ⇒ Object (private)

Define a method with the given name that accepts a fixed number of arguments, that will be used to execute the given SQL query.

Parameters:

  • mod (Module)

    DbMod enabled module where the method will be defined

  • name (Symbol)

    name of the method to be defined

  • count (Fixnum)

    arity of the defined method, the number of parameters that the SQL statement requires



131
132
133
134
135
136
137
138
139
# File 'lib/db_mod/statements/statement.rb', line 131

def self.define_fixed_args_statement_method(mod, name, count, sql)
  method = lambda do |*args|
    Parameters.valid_fixed_args!(count, args)

    conn.exec_params(sql, args)
  end

  Configuration.def_configurable mod, name, method
end

.define_named_args_statement_method(mod, name, params, sql) ⇒ Object (private)

Define a method with the given name, that accepts the given set of named parameters that will be used to execute the given SQL query.

Parameters:

  • mod (Module)

    DbMod enabled module

  • name (Symbol)

    name of the method to be defined

  • params (Array<Symbol>)

    parameter names and order



114
115
116
117
118
119
120
121
# File 'lib/db_mod/statements/statement.rb', line 114

def self.define_named_args_statement_method(mod, name, params, sql)
  method = lambda do |*args|
    args = Parameters.valid_named_args! params, args
    conn.exec_params(sql, args)
  end

  Configuration.def_configurable mod, name, method
end

.define_no_args_statement_method(mod, name, sql) ⇒ Object (private)

Define a no-argument method with the given name that will execute the given sql statement and return the result.

Parameters:

  • mod (Module)

    DbMod enabled module where the method will be defined

  • name (Symbol)

    name of the method to be defined

  • sql (String)

    parameterless SQL statement to execute



103
104
105
# File 'lib/db_mod/statements/statement.rb', line 103

def self.define_no_args_statement_method(mod, name, sql)
  Configuration.def_configurable mod, name, ->() { query(sql) }
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.



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

def self.define_statement_method(mod, name, params, sql)
  if params.is_a?(Array)
    if params.empty?
      define_no_args_statement_method(mod, name, sql)
    else
      define_named_args_statement_method(mod, name, params, sql)
    end
  else
    define_fixed_args_statement_method(mod, name, params, sql)
  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