Class: RDFMapper::Adapters::Rails::Query::SQL

Inherits:
Object
  • Object
show all
Defined in:
lib/lib/adapters/rails.rb

Instance Method Summary collapse

Constructor Details

#initialize(query, replace) ⇒ SQL

Returns a new instance of SQL.



252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/lib/adapters/rails.rb', line 252

def initialize(query, replace)
  @query, @replace = query, replace
  @text, @values = [], []

  @query.to_a.map do |condition|
    if condition.kind_of?(query.class)
      add_query(condition)
    else
      add_condition(condition)
    end
  end
end

Instance Method Details

#add_condition(condition) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/lib/adapters/rails.rb', line 273

def add_condition(condition)
  name = @replace[condition.name]
  if name.nil?
    return nil
  end
  if condition.value.kind_of?(Array)
    @text << "%s IN (?)" % name
  else
    @text << "%s %s ?" % [name, condition.eq]
  end
  @values << validate(condition.value)
end

#add_query(query) ⇒ Object



265
266
267
268
269
270
271
# File 'lib/lib/adapters/rails.rb', line 265

def add_query(query)
  child = SQL.new(query, @replace)
  unless child.text.empty?
    @text.push("(%s)" % child.text)
    @values.push(*child.values)
  end
end

#textObject



299
300
301
# File 'lib/lib/adapters/rails.rb', line 299

def text
  @text.join(' %s ' % @query.modifier)
end

#to_aObject



307
308
309
# File 'lib/lib/adapters/rails.rb', line 307

def to_a
  [text] + values
end

#validate(value) ⇒ Object

nodoc



286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/lib/adapters/rails.rb', line 286

def validate(value) #nodoc
  if value.kind_of? Array
    return value.map do |item|
      validate(item)
    end
  end
  if value.kind_of? RDFMapper::Model
    value[:rails_id]
  else
    value
  end
end

#valuesObject



303
304
305
# File 'lib/lib/adapters/rails.rb', line 303

def values
  @values
end