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
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.
Class Method Summary collapse
-
.define_def_statement(mod) ⇒ Object
Add a
def_statementmethod definition to a module. -
.define_fixed_args_statement_method(mod, name, count, sql) ⇒ Object
Define a method with the given name that accepts a fixed number of arguments, that will be used to execute the given SQL query.
-
.define_named_args_statement_method(mod, name, params, sql) ⇒ Object
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.
-
.define_no_args_statement_method(mod, name, sql) ⇒ Object
Define a no-argument method with the given name that will execute the given sql statement and return the result.
-
.define_statement_method(mod, name, params, sql) ⇒ Object
Define a method in the given module with the given name and parameters, that will call the given sql statement and return the results.
-
.setup(mod) ⇒ Object
Defines a module-specific
def_statementfunction for a module that has just had DbMod included.
Class Method Details
.define_def_statement(mod) ⇒ Object
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.
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/db_mod/statements/statement.rb', line 44 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
Define a method with the given name that accepts a fixed number of arguments, that will be used to execute the given SQL query.
113 114 115 116 117 118 119 120 121 |
# File 'lib/db_mod/statements/statement.rb', line 113 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 Statements.configurable_method mod, name, method end |
.define_named_args_statement_method(mod, name, params, sql) ⇒ Object
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.
96 97 98 99 100 101 102 103 |
# File 'lib/db_mod/statements/statement.rb', line 96 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 Statements.configurable_method mod, name, method end |
.define_no_args_statement_method(mod, name, sql) ⇒ Object
Define a no-argument method with the given name that will execute the given sql statement and return the result.
85 86 87 |
# File 'lib/db_mod/statements/statement.rb', line 85 def self.define_no_args_statement_method(mod, name, sql) Statements.configurable_method mod, name, ->() { query(sql) } end |
.define_statement_method(mod, name, params, sql) ⇒ Object
Define a method in the given module with the given name and parameters, that will call the given sql statement and return the results.
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/db_mod/statements/statement.rb', line 65 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.
32 33 34 |
# File 'lib/db_mod/statements/statement.rb', line 32 def self.setup(mod) Statement.define_def_statement(mod) end |