Class: OccamsRecord::BindsConverter::Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/occams-record/binds_converter/abstract.rb

Overview

A base class for converting a SQL string with Rails-style query params (?, :foo) to native Ruby format (%s, %foo).

It works kind of like a tokenizer. Subclasses must 1) implement get_bind to return the converted bind from the current position and 2) pass the bind sigil (e.g. ?, :) to the parent constructor.

Direct Known Subclasses

Positional

Constant Summary collapse

ESCAPE =
"\\".freeze

Instance Method Summary collapse

Constructor Details

#initialize(sql, binds, bind_sigil) ⇒ Abstract

Returns a new instance of Abstract.



13
14
15
16
17
18
19
20
# File 'lib/occams-record/binds_converter/abstract.rb', line 13

def initialize(sql, binds, bind_sigil)
  @sql = sql
  @binds = binds
  @end = sql.size - 1
  @start_i, @i = 0, 0
  @bind_sigil = bind_sigil
  @found = []
end

Instance Method Details

#to_sString

Returns The converted SQL string.

Returns:

  • (String)

    The converted SQL string

Raises:



23
24
25
26
27
28
# File 'lib/occams-record/binds_converter/abstract.rb', line 23

def to_s
  sql = +""
  each { |frag| sql << frag }
  raise MissingBindValuesError.new(sql, missing_bind_values_msg) if @binds.size < @found.uniq.size
  sql
end