Module: DbMod::Statements::Parameters

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

Overview

Parsing and validation of query parameters for prepared SQL statements

Constant Summary collapse

NUMBERED_PARAM =

Regex matching a numbered parameter

/\$\d+/
NAMED_PARAM =

Regex matching a named parameter

/\$[a-z]+(?:_[a-z]+)*/
NAMED_OR_NUMBERED =

For validation, named or numbered parameter

/^\$(?:\d+|[a-z]+(?:_[a-z]+)*)$/

Class Method Summary collapse

Class Method Details

.parse_params!(sql) ⇒ Fixnum, Array<Symbol>

Called when a DbMod dynamically defined method is declared. Parses parameters, named or numbered, from an SQL statement. See the DbMod::Statements::Prepared module documentation for more. This method may modify the sql statement to change named parameters to numbered parameters. If the query uses numbered parameters, an integer will be returned that is the arity of the statement. If the query uses named parameters, an array of symbols will be returned, giving the order in which the named parameters should be fed into the statement.

Parameters:

  • sql (String)

    statement to prepare

Returns:

  • (Fixnum, Array<Symbol>)

    description of prepared statement’s parameters



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/db_mod/statements/parameters.rb', line 54

def self.parse_params!(sql)
  Parameters.valid_sql_params! sql
  numbered = sql.scan NUMBERED_PARAM
  named = sql.scan NAMED_PARAM

  if numbered.any?
    fail ArgumentError, 'mixed named and numbered params' if named.any?
    Parameters.parse_numbered_params! numbered
  else
    Parameters.parse_named_params! sql, named
  end
end

.valid_fixed_args!(count, args) ⇒ Object

Called when a DbMod dynamically defined method is called. Assert that the correct number of arguments has been provided.

Parameters:

  • count (Fixnum)

    arity of the method being called.

  • args (Array)

    list of arguments given.



33
34
35
36
37
# File 'lib/db_mod/statements/parameters.rb', line 33

def self.valid_fixed_args!(count, args)
  unless args.size == count
    fail ArgumentError, "#{args.size} args given, #{count} expected"
  end
end

.valid_named_args!(expected, args) ⇒ Array

Called when a DbMod dynamically defined method is called. Assert that the named arguments given for the prepared statement with the given name satisfy expectations. Returns a parameter array as per parameter_array.

Parameters:

  • expected (Array<Symbol>)

    the parameters expected to be present

  • args (Array<Hash<Symbol>>)

    arguments given to the method being executed. The method should only be called with an options hash that contains exactly the parameter names given when the method was defined.

Returns:

  • (Array)

    values to be passed to the prepared statement



17
18
19
20
21
22
23
24
25
26
# File 'lib/db_mod/statements/parameters.rb', line 17

def self.valid_named_args!(expected, args)
  wrapped_hash! args

  args = args.first
  if args.size != expected.size
    fail ArgumentError, "#{args.size} args given, #{expected.size} needed"
  end

  parameter_array(expected, args)
end