Class: SQL

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

Constant Summary collapse

H_EMPTY =
{}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(ctx) ⇒ SQL

Returns a new instance of SQL.

[View source]

402
403
404
# File 'lib/eno.rb', line 402

def initialize(ctx)
  @ctx = ctx
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object

[View source]

427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/eno.rb', line 427

def method_missing(sym, *args)
  if @ctx.has_key?(sym)
    value = @ctx[sym]
    return Symbol === value ? Identifier.new(value) : value
  end
  
  super if sym == :to_hash
  if args.empty?
    Identifier.new(sym)
  else
    FunctionCall.new(sym, *args)
  end
end

Instance Method Details

#_q(expr) ⇒ Object

[View source]

419
420
421
# File 'lib/eno.rb', line 419

def _q(expr)
  QuotedExpression.new(expr)
end

#all(sym = nil) ⇒ Object

[View source]

479
480
481
482
483
484
485
# File 'lib/eno.rb', line 479

def all(sym = nil)
  if sym
    Identifier.new("#{sym}.*")
  else
    Identifier.new('*')
  end
end

#default_selectObject

[View source]

423
424
425
# File 'lib/eno.rb', line 423

def default_select
  Select.new(:*)
end

#from(*members, **props) ⇒ Object

[View source]

455
456
457
# File 'lib/eno.rb', line 455

def from(*members, **props)
  @from = From.new(*members, **props)
end

#limit(*members) ⇒ Object

[View source]

475
476
477
# File 'lib/eno.rb', line 475

def limit(*members)
  @limit = Limit.new(*members)
end

#order_by(*members, **props) ⇒ Object

[View source]

471
472
473
# File 'lib/eno.rb', line 471

def order_by(*members, **props)
  @order_by = OrderBy.new(*members, **props)
end

#select(*members, **props) ⇒ Object

[View source]

447
448
449
450
451
452
453
# File 'lib/eno.rb', line 447

def select(*members, **props)
  if members.empty? && !props.empty?
    members = props.map { |k, v| Alias.new(v, k) }
    props = {}
  end
  @select = Select.new(*members, **props)
end

#to_sql(&block) ⇒ Object

[View source]

406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/eno.rb', line 406

def to_sql(&block)
  instance_eval(&block)
  [
    @with,
    @select || default_select,
    @from,
    @where,
    @window,
    @order_by,
    @limit
  ].compact.map { |c| c.to_sql }.join(' ')
end

#where(expr) ⇒ Object

[View source]

459
460
461
462
463
464
465
# File 'lib/eno.rb', line 459

def where(expr)
  if @where
    @where.members << expr
  else
    @where = Where.new(expr)
  end
end

#window(sym, &block) ⇒ Object

[View source]

467
468
469
# File 'lib/eno.rb', line 467

def window(sym, &block)
  @window = Window.new(sym, &block)
end

#with(*members, **props) ⇒ Object

[View source]

441
442
443
# File 'lib/eno.rb', line 441

def with(*members, **props)
  @with = With.new(*members, **props)
end