Class: Baza::SqlQueries::Select
- Inherits:
-
Object
- Object
- Baza::SqlQueries::Select
- Defined in:
- lib/baza/sql_queries/select.rb
Instance Method Summary collapse
- #count ⇒ Object
- #current_page ⇒ Object
- #each(&blk) ⇒ Object
- #each_row ⇒ Object
- #from(arg) ⇒ Object
- #group(arg) ⇒ Object
-
#initialize(args) ⇒ Select
constructor
A new instance of Select.
- #join(arg) ⇒ Object
- #limit(limit) ⇒ Object
- #offset(offset) ⇒ Object
- #order(arg) ⇒ Object
- #page(number) ⇒ Object
- #per_page(number) ⇒ Object
- #query(&blk) ⇒ Object
- #select(arg) ⇒ Object
- #to_a ⇒ Object
- #to_enum ⇒ Object
- #to_sql ⇒ Object
- #total_pages ⇒ Object
- #where(*args) ⇒ Object
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
#count ⇒ Object
12 13 14 15 16 17 |
# File 'lib/baza/sql_queries/select.rb', line 12 def count @count = true query.fetch.fetch(:count).to_i ensure @count = false end |
#current_page ⇒ Object
19 20 21 |
# File 'lib/baza/sql_queries/select.rb', line 19 def current_page @page || 1 end |
#each(&blk) ⇒ Object
87 88 89 |
# File 'lib/baza/sql_queries/select.rb', line 87 def each(&blk) query(&blk) end |
#each_row ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/baza/sql_queries/select.rb', line 91 def each_row query do |data| yield Baza::Row.new( db: @db, table: first_from, data: data ) end end |
#from(arg) ⇒ Object
28 29 30 31 |
# File 'lib/baza/sql_queries/select.rb', line 28 def from(arg) @froms << arg self end |
#group(arg) ⇒ Object
43 44 45 46 |
# File 'lib/baza/sql_queries/select.rb', line 43 def group(arg) @groups << arg self end |
#join(arg) ⇒ Object
33 34 35 36 |
# File 'lib/baza/sql_queries/select.rb', line 33 def join(arg) @joins << arg self end |
#limit(limit) ⇒ Object
63 64 65 66 |
# File 'lib/baza/sql_queries/select.rb', line 63 def limit(limit) @limit = limit self end |
#offset(offset) ⇒ Object
68 69 70 71 |
# File 'lib/baza/sql_queries/select.rb', line 68 def offset(offset) @offset = offset self end |
#order(arg) ⇒ Object
48 49 50 51 |
# File 'lib/baza/sql_queries/select.rb', line 48 def order(arg) @orders << arg self end |
#page(number) ⇒ Object
53 54 55 56 |
# File 'lib/baza/sql_queries/select.rb', line 53 def page(number) @page = number.try(:to_i) || 1 self end |
#per_page(number) ⇒ Object
58 59 60 61 |
# File 'lib/baza/sql_queries/select.rb', line 58 def per_page(number) @per_page = number self end |
#query(&blk) ⇒ Object
109 110 111 |
# File 'lib/baza/sql_queries/select.rb', line 109 def query(&blk) @db.query(to_sql, &blk) end |
#select(arg) ⇒ Object
23 24 25 26 |
# File 'lib/baza/sql_queries/select.rb', line 23 def select(arg) @selects << arg self end |
#to_a ⇒ Object
77 78 79 |
# File 'lib/baza/sql_queries/select.rb', line 77 def to_a each.to_a end |
#to_enum ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/baza/sql_queries/select.rb', line 101 def to_enum Enumerator.new do |yielder| query do |data| yielder << data end end end |
#to_sql ⇒ Object
73 74 75 |
# File 'lib/baza/sql_queries/select.rb', line 73 def to_sql "#{select_sql} #{from_sql} #{where_sql} #{group_sql} #{limit_sql}" end |
#total_pages ⇒ Object
81 82 83 84 85 |
# File 'lib/baza/sql_queries/select.rb', line 81 def total_pages @per_page ||= 30 per_page_value = @per_page (count.to_f / per_page_value.to_f).ceil end |
#where(*args) ⇒ Object
38 39 40 41 |
# File 'lib/baza/sql_queries/select.rb', line 38 def where(*args) @wheres << args self end |