Class: Seaquel::Statement

Inherits:
Object
  • Object
show all
Defined in:
lib/seaquel/statement.rb,
lib/seaquel/statement/join.rb

Overview

Root node for an sql statement.

Defined Under Namespace

Classes: Join

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expression_convertor) ⇒ Statement

Returns a new instance of Statement.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/seaquel/statement.rb', line 30

def initialize expression_convertor
  @expression_convertor = expression_convertor

  @from = list()
  @project = list()
  @order_by = list()
  @where = AST::JoinOp.new(:and, [])
  @set = list()
  @values = list()
  @fields = column_list()
  @joins = []
end

Instance Attribute Details

#expression_convertorObject (readonly)

Returns the value of attribute expression_convertor.



16
17
18
# File 'lib/seaquel/statement.rb', line 16

def expression_convertor
  @expression_convertor
end

#fieldsObject (readonly)

Returns the value of attribute fields.



24
25
26
# File 'lib/seaquel/statement.rb', line 24

def fields
  @fields
end

#fromObject (readonly)

Returns the value of attribute from.



18
19
20
# File 'lib/seaquel/statement.rb', line 18

def from
  @from
end

#joinsObject (readonly)

Returns the value of attribute joins.



25
26
27
# File 'lib/seaquel/statement.rb', line 25

def joins
  @joins
end

#limitObject (readonly)

Returns the value of attribute limit.



26
27
28
# File 'lib/seaquel/statement.rb', line 26

def limit
  @limit
end

#offsetObject (readonly)

Returns the value of attribute offset.



27
28
29
# File 'lib/seaquel/statement.rb', line 27

def offset
  @offset
end

#order_byObject (readonly)

Returns the value of attribute order_by.



28
29
30
# File 'lib/seaquel/statement.rb', line 28

def order_by
  @order_by
end

#projectObject (readonly)

Returns the value of attribute project.



19
20
21
# File 'lib/seaquel/statement.rb', line 19

def project
  @project
end

#setObject (readonly)

Returns the value of attribute set.



21
22
23
# File 'lib/seaquel/statement.rb', line 21

def set
  @set
end

#targetObject (readonly)

Returns the value of attribute target.



22
23
24
# File 'lib/seaquel/statement.rb', line 22

def target
  @target
end

#valuesObject (readonly)

Returns the value of attribute values.



23
24
25
# File 'lib/seaquel/statement.rb', line 23

def values
  @values
end

#whereObject (readonly)

Returns the value of attribute where.



20
21
22
# File 'lib/seaquel/statement.rb', line 20

def where
  @where
end

Instance Method Details

#convert(exp) ⇒ Object

Turns an expression into SQL



89
90
91
# File 'lib/seaquel/statement.rb', line 89

def convert exp
  expression_convertor.sql(exp).toplevel
end

#join(tables) ⇒ Join

Produces a join clause and adds it to the joins list.

Parameters:

  • tables (Array<Object>)

    a list of tables to join to

Returns:

  • (Join)

    Join subobject.



67
68
69
70
71
72
# File 'lib/seaquel/statement.rb', line 67

def join tables
  join = Join.new(tables)
  joins << join

  join
end

#set_limit(n) ⇒ Object



43
44
45
# File 'lib/seaquel/statement.rb', line 43

def set_limit n
  @limit = n
end

#set_offset(n) ⇒ Object



46
47
48
# File 'lib/seaquel/statement.rb', line 46

def set_offset n
  @offset = n
end

#set_target(table) ⇒ Object



58
59
60
# File 'lib/seaquel/statement.rb', line 58

def set_target table
  @target = table
end

#set_type(type) ⇒ Object

Allows to determine the type of statement generated.

Parameters:

  • type ({:update, :select, :delete})


54
55
56
# File 'lib/seaquel/statement.rb', line 54

def set_type type
  @type = type
end

#to_s(variant = :compact) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/seaquel/statement.rb', line 74

def to_s variant=:compact
  case @type 
    when :update
      generate_update(variant)
    when :insert
      generate_insert(variant)
    when :delete
      generate_delete(variant)
  else
    generate_select(variant)
  end
end