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_to_select ⇒ 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
80 81 82 83 84 |
# File 'lib/sqlstmt/sqlstmt.rb', line 80 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
86 87 88 |
# File 'lib/sqlstmt/sqlstmt.rb', line 86 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
72 73 74 |
# File 'lib/sqlstmt/sqlstmt.rb', line 72 def join(table, *exprs) return any_join('JOIN', table, exprs) end |
#left_join(table, *exprs) ⇒ Object
76 77 78 |
# File 'lib/sqlstmt/sqlstmt.rb', line 76 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
102 103 104 105 |
# File 'lib/sqlstmt/sqlstmt.rb', line 102 def no_where @data.where_behavior = :exclude return self end |
#optional_where ⇒ Object
this disables a where clause requirement
108 109 110 111 |
# File 'lib/sqlstmt/sqlstmt.rb', line 108 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
96 97 98 99 |
# File 'lib/sqlstmt/sqlstmt.rb', line 96 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
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/sqlstmt/sqlstmt.rb', line 118 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
131 132 133 |
# File 'lib/sqlstmt/sqlstmt.rb', line 131 def setq(field, value) return set(field, value.to_sql) end |
#switch_to_select ⇒ Object
switch statement type
59 60 61 62 63 |
# File 'lib/sqlstmt/sqlstmt.rb', line 59 def switch_to_select @data.stmt_type = 'select' @data.set_fields.clear @data.set_values.clear end |
#table(ref, use_index = nil) ⇒ Object
tables & joins
67 68 69 70 |
# File 'lib/sqlstmt/sqlstmt.rb', line 67 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 |