Class: Diary::Query::Where

Inherits:
Node
  • Object
show all
Defined in:
lib/diary-ruby/database/query.rb

Instance Method Summary collapse

Methods inherited from Node

#has_bound_vars?, #prepared_statement, #string_or_symbol?

Constructor Details

#initialize(*conditions) ⇒ Where

convert conditions to AND’d list returns either string or (string, bind_params) 2-tuple



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/diary-ruby/database/query.rb', line 198

def initialize(*conditions)
  @sql_result = if Hash === conditions[0]
                  attrs = conditions[0]

                  keys = attrs.keys
                  vals = keys.map {|k| attrs[k]}

                  and_string = keys.map do |k|
                    if attrs[k].is_a?(Array)
                      bind_hold = attrs[k].map {|_| '?'}.join(',')
                      "`#{k}` in (#{bind_hold})"
                    else
                      "`#{k}` = ?"
                    end
                  end.join(' AND ')

                  # (string, bind)
                  SQLBoundParams.new(and_string, vals.flatten)
                elsif conditions.size > 1 && String === conditions[0]
                  # assume (string, bind) given
                  SQLBoundParams.new(conditions[0], conditions[1..-1])
                elsif conditions.size == 1 && String === conditions[0]
                  SQLString.new(conditions[0])
                end
end