Class: SqlStmt::Query

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

Direct Known Subclasses

FromQuery, Update

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQuery

Returns a new instance of Query.



10
11
12
13
14
15
16
# File 'lib/sqlstmt/query.rb', line 10

def initialize
  @fields = []
  @tables = []
  @joins = []
  @wheres = []
  @where_behavior = :require
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



8
9
10
# File 'lib/sqlstmt/query.rb', line 8

def fields
  @fields
end

#joinsObject (readonly)

Returns the value of attribute joins.



8
9
10
# File 'lib/sqlstmt/query.rb', line 8

def joins
  @joins
end

#tablesObject (readonly)

Returns the value of attribute tables.



8
9
10
# File 'lib/sqlstmt/query.rb', line 8

def tables
  @tables
end

#wheresObject (readonly)

Returns the value of attribute wheres.



8
9
10
# File 'lib/sqlstmt/query.rb', line 8

def wheres
  @wheres
end

Instance Method Details

#join(table, expr) ⇒ Object



23
24
25
26
# File 'lib/sqlstmt/query.rb', line 23

def join(table, expr)
  @joins.push("JOIN #{table} ON #{expr}")
  self
end

#join_using(table, *fields) ⇒ Object



28
29
30
31
# File 'lib/sqlstmt/query.rb', line 28

def join_using(table, *fields)
  @joins.push("JOIN #{table} USING (#{fields.join(',')})")
  self
end

#left_join(table, expr) ⇒ Object



33
34
35
36
# File 'lib/sqlstmt/query.rb', line 33

def left_join(table, expr)
  @joins.push("LEFT JOIN #{table} ON #{expr}")
  self
end

#left_join_using(table, *fields) ⇒ Object



38
39
40
41
# File 'lib/sqlstmt/query.rb', line 38

def left_join_using(table, *fields)
  @joins.push("LEFT JOIN #{table} USING (#{fields.join(',')})")
  self
end

#no_whereObject



48
49
50
51
# File 'lib/sqlstmt/query.rb', line 48

def no_where
  @where_behavior = :exclude
  self
end

#optional_whereObject



53
54
55
56
# File 'lib/sqlstmt/query.rb', line 53

def optional_where
  @where_behavior = :optional
  self
end

#table(table) ⇒ Object



18
19
20
21
# File 'lib/sqlstmt/query.rb', line 18

def table(table)
  @tables.push(table)
  self
end

#to_sObject



58
59
60
61
# File 'lib/sqlstmt/query.rb', line 58

def to_s
  verify_minimum_requirements
  build_stmt
end

#where(*sql) ⇒ Object



43
44
45
46
# File 'lib/sqlstmt/query.rb', line 43

def where(*sql)
  @wheres.concat(sql)
  self
end