Class: AlgebraDB::Statement::Select

Inherits:
Object
  • Object
show all
Defined in:
lib/algebra_db/statement/select.rb

Overview

A select statement executor.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSelect

Returns a new instance of Select.



10
11
12
13
14
15
# File 'lib/algebra_db/statement/select.rb', line 10

def initialize
  @wheres = []
  @froms = []
  @joins = []
  @select = nil
end

Class Method Details

.run_syntax(&block) ⇒ Object



6
7
8
# File 'lib/algebra_db/statement/select.rb', line 6

def self.run_syntax(&block)
  new.tap { |t| t.instance_eval(&block) }
end

Instance Method Details

#all(relationish) ⇒ Object

Give a AlgebgraDB::Table or something else that responds to #to_relation, gives you back a relation that you can use further in the query.

If you use ‘relationish` in another query (by storing it in a variable out of scope), it will break!



24
25
26
27
28
29
30
31
32
# File 'lib/algebra_db/statement/select.rb', line 24

def all(relationish)
  unless relationish.respond_to?(:to_relation)
    raise ArgumentError, "#{relationish} does not respond to to_relation!"
  end

  relation = relationish.to_relation(next_table_alias)
  @froms << relation.from_clause
  relation
end

#join_relationship(relationship, type: :inner) ⇒ Object



42
43
44
45
46
# File 'lib/algebra_db/statement/select.rb', line 42

def join_relationship(relationship, type: :inner)
  joins(relationship.joined_table, type: type) do |rel|
    relationship.join_clause(rel)
  end
end

#joins(other_table, type: :inner, &block) ⇒ Object



48
49
50
51
52
53
# File 'lib/algebra_db/statement/select.rb', line 48

def joins(other_table, type: :inner, &block)
  relation = other_table.to_relation(next_table_alias)
  join_clause = block.call(relation)
  @joins << Build::Join.new(type, relation.from_clause, join_clause)
  relation
end

#next_table_aliasObject



79
80
81
# File 'lib/algebra_db/statement/select.rb', line 79

def next_table_alias
  :"t_#{@froms.count + @joins.count + 1}"
end

#raw_param(ruby_value) ⇒ Object



55
56
57
# File 'lib/algebra_db/statement/select.rb', line 55

def raw_param(ruby_value)
  Build.param(ruby_value)
end

#render_syntax(builder) ⇒ Object

Raises:

  • (ArgumentError)


65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/algebra_db/statement/select.rb', line 65

def render_syntax(builder)
  raise ArgumentError, 'no select' unless @select

  builder.text('SELECT')
  @select.render_syntax(builder)
  builder.text('FROM')
  builder.separate(@froms) { |f, b| f.render_syntax(b) }
  @joins.each { |j| j.render_syntax(builder) }
  return if @wheres.empty?

  builder.text('WHERE')
  builder.separate(@wheres, separator: ' AND') { |w, b| w.render_syntax(b) }
end

#select(*selects) ⇒ Object



38
39
40
# File 'lib/algebra_db/statement/select.rb', line 38

def select(*selects)
  @select = Build::SelectList.new(*selects)
end

#to_deliveryObject

Raises:

  • (ArgumentError)


59
60
61
62
63
# File 'lib/algebra_db/statement/select.rb', line 59

def to_delivery
  raise ArgumentError, 'nothing selected' unless @select

  Exec::Delivery.new(self, @select.row_decoder)
end

#where(filter) ⇒ Object



34
35
36
# File 'lib/algebra_db/statement/select.rb', line 34

def where(filter)
  @wheres << filter
end