Class: Query

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(froms, conditions = [], grouped_keys = [], row = nil) ⇒ Query

Returns a new instance of Query.



302
303
304
305
306
307
308
# File 'lib/lilit_sql.rb', line 302

def initialize(froms, conditions = [], grouped_keys = [], row = nil)
  @froms = froms + []
  @conditions = conditions + []
  @grouped_keys = grouped_keys + []
  @row = row
  @subquery_name = nil
end

Instance Attribute Details

#conditionsObject

Returns the value of attribute conditions.



298
299
300
# File 'lib/lilit_sql.rb', line 298

def conditions
  @conditions
end

#fromsObject

Returns the value of attribute froms.



297
298
299
# File 'lib/lilit_sql.rb', line 297

def froms
  @froms
end

#grouped_keysObject

Returns the value of attribute grouped_keys.



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

def grouped_keys
  @grouped_keys
end

#rowObject

Returns the value of attribute row.



300
301
302
# File 'lib/lilit_sql.rb', line 300

def row
  @row
end

Class Method Details

.from(query) ⇒ Object



310
311
312
# File 'lib/lilit_sql.rb', line 310

def self.from(query)
  new([From.new(query)])
end

Instance Method Details

#group_by(&blk) ⇒ Object



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/lilit_sql.rb', line 344

def group_by(&blk)
  if @row
    return Query.from(self).group_by(&blk)
  end

  result = expr(&blk).call(*get_from_rows)

  if result.is_a?(Column)
    GroupBy.new(self, [result])
  elsif result.is_a?(Array)
    GroupBy.new(self, result)
  else
    raise NotImplementedError
  end
end

#has?(column_name) ⇒ Boolean

Returns:

  • (Boolean)


332
333
334
# File 'lib/lilit_sql.rb', line 332

def has?(column_name)
  rows.any? {|r| r.has?(column_name)}
end

#is_vanillaObject



314
315
316
# File 'lib/lilit_sql.rb', line 314

def is_vanilla
  @froms.size == 1 && @conditions.empty? && @grouped_keys.empty? && @row.nil?
end

#join(other, &blk) ⇒ Object



360
361
362
# File 'lib/lilit_sql.rb', line 360

def join(other, &blk)
  perform_join(:join, other, &blk)
end

#left_join(other, &blk) ⇒ Object



364
365
366
# File 'lib/lilit_sql.rb', line 364

def left_join(other, &blk)
  perform_join(:left_join, other, &blk)
end

#map(&blk) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/lilit_sql.rb', line 318

def map(&blk)
  if @row
    return Query.from(self).map(&blk)
  end

  result = expr(&blk).call(*get_from_rows)
  Query.new(
    @froms,
    @conditions,
    @grouped_keys,
    Row.new(result.class.members, result)
  )
end

#rowsObject



336
337
338
339
340
341
342
# File 'lib/lilit_sql.rb', line 336

def rows
  if @row
    return [@row]
  end

  get_from_rows
end

#sqlObject



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/lilit_sql.rb', line 398

def sql
  s = "select "
  s += rows.map {|r| r.decl_sql}.join(', ')
  s += " from"

  @froms.each_with_index do |from, index|
    if index >= 1
      if from.join_type == :join
        s += " join"
      elsif from.join_type == :left_join
        s += " left join"
      else
        raise ArgumentError.new("The join type #{from.join_type} is not supoprted.")
      end
    end

    s += " #{from.source.subquery_name}"

    if from.source.subquery_name != from.alias_name
      s += " #{from.alias_name}"
    end

    if from.condition
      s += " on #{from.condition.ref_sql}"
    end
  end

  if @conditions.size > 0
    s += " where #{@conditions.map {|c| c.ref_sql}.join(' and ')}"
  end

  if @grouped_keys.size > 0
    s += " group by #{@grouped_keys.map {|k| k.ref_sql}.join(', ')}"
  end

  s
end

#subquery_nameObject



382
383
384
385
386
387
388
389
390
391
392
# File 'lib/lilit_sql.rb', line 382

def subquery_name
  if is_vanilla
    return @froms.first.source.subquery_name
  end

  if @subquery_name.nil?
    raise ArgumentError.new("The query #{self.inspect} doesn't have a subquery name")
  end

  @subquery_name
end

#subquery_name=(value) ⇒ Object



394
395
396
# File 'lib/lilit_sql.rb', line 394

def subquery_name=(value)
  @subquery_name = value
end

#where(&blk) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/lilit_sql.rb', line 368

def where(&blk)
  if @row
    return Query.from(self).where(&blk)
  end

  condition = expr(&blk).call(*get_from_rows)
  Query.new(
    @froms,
    @conditions + [condition],
    @grouped_keys,
    @row
  )
end