Class: SqlStmt::Query
- Inherits:
-
Object
- Object
- SqlStmt::Query
- Defined in:
- lib/sqlstmt/query.rb
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#joins ⇒ Object
readonly
Returns the value of attribute joins.
-
#tables ⇒ Object
readonly
Returns the value of attribute tables.
-
#wheres ⇒ Object
readonly
Returns the value of attribute wheres.
Instance Method Summary collapse
- #includes_table?(table_to_find) ⇒ Boolean
-
#initialize ⇒ Query
constructor
A new instance of Query.
- #join(table, *exprs) ⇒ Object
- #left_join(table, *exprs) ⇒ Object
- #limit(clause) ⇒ Object
- #no_where ⇒ Object
- #optional_join(table, expr) ⇒ Object
- #optional_where ⇒ Object
- #table(table) ⇒ Object
- #to_s ⇒ Object (also: #to_sql)
- #where(*sql) ⇒ Object
Constructor Details
#initialize ⇒ Query
Returns a new instance of Query.
10 11 12 13 14 15 16 17 |
# File 'lib/sqlstmt/query.rb', line 10 def initialize @fields = [] @tables = [] @joins = [] @wheres = [] @where_behavior = :require @limit = nil end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
8 9 10 |
# File 'lib/sqlstmt/query.rb', line 8 def fields @fields end |
#joins ⇒ Object (readonly)
Returns the value of attribute joins.
8 9 10 |
# File 'lib/sqlstmt/query.rb', line 8 def joins @joins end |
#tables ⇒ Object (readonly)
Returns the value of attribute tables.
8 9 10 |
# File 'lib/sqlstmt/query.rb', line 8 def tables @tables end |
#wheres ⇒ Object (readonly)
Returns the value of attribute wheres.
8 9 10 |
# File 'lib/sqlstmt/query.rb', line 8 def wheres @wheres end |
Instance Method Details
#includes_table?(table_to_find) ⇒ Boolean
66 67 68 69 70 |
# File 'lib/sqlstmt/query.rb', line 66 def includes_table?(table_to_find) retval = @tables.find { |table| table_names_match?(table, table_to_find) } retval ||= @joins.find { |_, table, _| table_names_match?(table, table_to_find) } retval end |
#join(table, *exprs) ⇒ Object
24 25 26 27 |
# File 'lib/sqlstmt/query.rb', line 24 def join(table, *exprs) @joins << ['JOIN', table, "ON #{exprs.join(' AND ')}"] self end |
#left_join(table, *exprs) ⇒ Object
35 36 37 38 |
# File 'lib/sqlstmt/query.rb', line 35 def left_join(table, *exprs) @joins << ['LEFT JOIN', table, "ON #{exprs.join(' AND ')}"] self end |
#limit(clause) ⇒ Object
55 56 57 58 |
# File 'lib/sqlstmt/query.rb', line 55 def limit(clause) @limit = clause self end |
#no_where ⇒ Object
45 46 47 48 |
# File 'lib/sqlstmt/query.rb', line 45 def no_where @where_behavior = :exclude self end |
#optional_join(table, expr) ⇒ Object
29 30 31 32 33 |
# File 'lib/sqlstmt/query.rb', line 29 def optional_join(table, expr) unless includes_table?(table) join(table, expr) end end |
#optional_where ⇒ Object
50 51 52 53 |
# File 'lib/sqlstmt/query.rb', line 50 def optional_where @where_behavior = :optional self end |
#table(table) ⇒ Object
19 20 21 22 |
# File 'lib/sqlstmt/query.rb', line 19 def table(table) @tables << table self end |
#to_s ⇒ Object Also known as: to_sql
60 61 62 63 |
# File 'lib/sqlstmt/query.rb', line 60 def to_s verify_minimum_requirements build_stmt end |
#where(*sql) ⇒ Object
40 41 42 43 |
# File 'lib/sqlstmt/query.rb', line 40 def where(*sql) @wheres.concat(sql) self end |