Class: Baza::SqlQueries::Select

Inherits:
Object
  • Object
show all
Defined in:
lib/baza/sql_queries/select.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Select

Returns a new instance of Select.



2
3
4
5
6
7
8
9
10
# File 'lib/baza/sql_queries/select.rb', line 2

def initialize(args)
  @db = args.fetch(:db)
  @selects = []
  @froms = []
  @joins = []
  @wheres = []
  @groups = []
  @orders = []
end

Instance Method Details

#each(&blk) ⇒ Object



60
61
62
# File 'lib/baza/sql_queries/select.rb', line 60

def each(&blk)
  query(&blk)
end

#each_rowObject



64
65
66
67
68
69
70
71
72
# File 'lib/baza/sql_queries/select.rb', line 64

def each_row
  query do |data|
    yield Baza::Row.new(
      db: @db,
      table: first_from,
      data: data
    )
  end
end

#from(arg) ⇒ Object



17
18
19
20
# File 'lib/baza/sql_queries/select.rb', line 17

def from(arg)
  @froms << arg
  self
end

#group(arg) ⇒ Object



32
33
34
35
# File 'lib/baza/sql_queries/select.rb', line 32

def group(arg)
  @groups << arg
  self
end

#join(arg) ⇒ Object



22
23
24
25
# File 'lib/baza/sql_queries/select.rb', line 22

def join(arg)
  @joins << arg
  self
end

#limit(limit) ⇒ Object



42
43
44
45
# File 'lib/baza/sql_queries/select.rb', line 42

def limit(limit)
  @limit = limit
  self
end

#offset(offset) ⇒ Object



47
48
49
50
# File 'lib/baza/sql_queries/select.rb', line 47

def offset(offset)
  @offset = offset
  self
end

#order(arg) ⇒ Object



37
38
39
40
# File 'lib/baza/sql_queries/select.rb', line 37

def order(arg)
  @orders << arg
  self
end

#query(&blk) ⇒ Object



82
83
84
# File 'lib/baza/sql_queries/select.rb', line 82

def query(&blk)
  @db.query(to_sql, &blk)
end

#select(arg) ⇒ Object



12
13
14
15
# File 'lib/baza/sql_queries/select.rb', line 12

def select(arg)
  @selects << arg
  self
end

#to_aObject



56
57
58
# File 'lib/baza/sql_queries/select.rb', line 56

def to_a
  each.to_a
end

#to_enumObject



74
75
76
77
78
79
80
# File 'lib/baza/sql_queries/select.rb', line 74

def to_enum
  Enumerator.new do |yielder|
    query do |data|
      yielder << data
    end
  end
end

#to_sqlObject



52
53
54
# File 'lib/baza/sql_queries/select.rb', line 52

def to_sql
  "#{select_sql} #{from_sql} #{where_sql} #{group_sql} #{limit_sql}"
end

#where(*args) ⇒ Object



27
28
29
30
# File 'lib/baza/sql_queries/select.rb', line 27

def where(*args)
  @wheres << args
  self
end