Class: SqlStmt
- Inherits:
-
Object
- Object
- SqlStmt
- Defined in:
- lib/sqlstmt/sqlstmt.rb,
lib/sqlstmt/build.rb
Overview
unless there is something better to return, methods return self so they can be chained together
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
- #any_join(kwstr, ref, exprs) ⇒ Object
- #delete(*tables) ⇒ Object
- #includes_table?(table_to_find) ⇒ Boolean
-
#initialize ⇒ SqlStmt
constructor
A new instance of SqlStmt.
- #initialize_copy(_orig) ⇒ Object
- #insert ⇒ Object
- #join(table, *exprs) ⇒ Object
- #left_join(table, *exprs) ⇒ Object
-
#no_where ⇒ Object
if any where clauses have been added, the statement will fail to build.
-
#optional_where ⇒ Object
this disables a where clause requirement.
-
#require_where ⇒ Object
if no where clauses have been added, the statement will fail to build this is the default value, but in case it got changed, we can reset it back.
-
#select ⇒ Object
pick statement type.
-
#set(field, value) ⇒ Object
nil can be passed in for the field, in which case it won’t be added this is only for the special case of INSERT INTO table SELECT b.* FROM blah b WHERE …
- #setq(field, value) ⇒ Object
-
#switch_type(new_type, clear_set_calls = true) ⇒ Object
switch statement type.
-
#table(ref, use_index = nil) ⇒ Object
tables & joins.
- #to_s ⇒ Object (also: #to_sql)
- #type(stmt_type) ⇒ Object
- #update ⇒ Object
Constructor Details
#initialize ⇒ SqlStmt
Returns a new instance of SqlStmt.
19 20 21 |
# File 'lib/sqlstmt/sqlstmt.rb', line 19 def initialize @data = SqlStmtLib::SqlData.new end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
17 18 19 |
# File 'lib/sqlstmt/sqlstmt.rb', line 17 def data @data end |
Instance Method Details
#any_join(kwstr, ref, exprs) ⇒ Object
83 84 85 86 87 |
# File 'lib/sqlstmt/sqlstmt.rb', line 83 def any_join(kwstr, ref, exprs) tbl = include_table(ref) @data.joins << SqlStmtLib::SqlJoin.new(kwstr, tbl, "ON #{exprs.join(' AND ')}") return self end |
#delete(*tables) ⇒ Object
43 44 45 46 47 |
# File 'lib/sqlstmt/sqlstmt.rb', line 43 def delete(*tables) type('delete') @data.tables_to_delete = tables return self end |
#includes_table?(table_to_find) ⇒ Boolean
89 90 91 |
# File 'lib/sqlstmt/sqlstmt.rb', line 89 def includes_table?(table_to_find) return @data.table_ids.include?(table_to_find) end |
#initialize_copy(_orig) ⇒ Object
23 24 25 26 27 |
# File 'lib/sqlstmt/sqlstmt.rb', line 23 def initialize_copy(_orig) # I don't really know why this is necessary, but it seems to be super @data = @data.dup end |
#insert ⇒ Object
39 40 41 |
# File 'lib/sqlstmt/sqlstmt.rb', line 39 def insert return type('insert') end |
#join(table, *exprs) ⇒ Object
75 76 77 |
# File 'lib/sqlstmt/sqlstmt.rb', line 75 def join(table, *exprs) return any_join('JOIN', table, exprs) end |
#left_join(table, *exprs) ⇒ Object
79 80 81 |
# File 'lib/sqlstmt/sqlstmt.rb', line 79 def left_join(table, *exprs) return any_join('LEFT JOIN', table, exprs) end |
#no_where ⇒ Object
if any where clauses have been added, the statement will fail to build
105 106 107 108 |
# File 'lib/sqlstmt/sqlstmt.rb', line 105 def no_where @data.where_behavior = :exclude return self end |
#optional_where ⇒ Object
this disables a where clause requirement
111 112 113 114 |
# File 'lib/sqlstmt/sqlstmt.rb', line 111 def optional_where @data.where_behavior = :optional return self end |
#require_where ⇒ Object
if no where clauses have been added, the statement will fail to build this is the default value, but in case it got changed, we can reset it back
99 100 101 102 |
# File 'lib/sqlstmt/sqlstmt.rb', line 99 def require_where @data.where_behavior = :require return self end |
#select ⇒ Object
pick statement type
31 32 33 |
# File 'lib/sqlstmt/sqlstmt.rb', line 31 def select return type('select') end |
#set(field, value) ⇒ Object
nil can be passed in for the field, in which case it won’t be added this is only for the special case of INSERT INTO table SELECT b.* FROM blah b WHERE … where there are no specific fields listed
121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/sqlstmt/sqlstmt.rb', line 121 def set(field, value) if @data.set_fields.include?(field) raise SqlStmtError, "trying to set field #{field} again" end if field @data.set_fields << field end value = value.is_a?(String) ? value : value.to_sql @data.set_values << value return self end |
#setq(field, value) ⇒ Object
134 135 136 |
# File 'lib/sqlstmt/sqlstmt.rb', line 134 def setq(field, value) return set(field, value.to_sql) end |
#switch_type(new_type, clear_set_calls = true) ⇒ Object
switch statement type
59 60 61 62 63 64 65 66 |
# File 'lib/sqlstmt/sqlstmt.rb', line 59 def switch_type(new_type, clear_set_calls = true) @data.stmt_type = new_type if clear_set_calls @data.set_fields.clear @data.set_values.clear end return self end |
#table(ref, use_index = nil) ⇒ Object
tables & joins
70 71 72 73 |
# File 'lib/sqlstmt/sqlstmt.rb', line 70 def table(ref, use_index = nil) @data.tables << include_table(ref, use_index) return self end |
#to_s ⇒ Object Also known as: to_sql
5 6 7 8 |
# File 'lib/sqlstmt/build.rb', line 5 def to_s SqlStmtLib::MysqlChecker.new(@data).run return SqlStmtLib::MysqlBuilder.new(@data).build_stmt end |
#type(stmt_type) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/sqlstmt/sqlstmt.rb', line 49 def type(stmt_type) if @data.stmt_type && @data.stmt_type != stmt_type raise SqlStmtError, "statement type already set to #{@data.stmt_type}" end @data.stmt_type = stmt_type return self end |
#update ⇒ Object
35 36 37 |
# File 'lib/sqlstmt/sqlstmt.rb', line 35 def update return type('update') end |