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
-
#table(ref, use_index = nil) ⇒ Object
tables & joins.
- #to_s ⇒ Object (also: #to_sql)
- #type(stmt_type) ⇒ Object
- #update ⇒ Object
Constructor Details
#initialize ⇒ 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
72 73 74 75 76 |
# File 'lib/sqlstmt/sqlstmt.rb', line 72 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
78 79 80 |
# File 'lib/sqlstmt/sqlstmt.rb', line 78 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
64 65 66 |
# File 'lib/sqlstmt/sqlstmt.rb', line 64 def join(table, *exprs) return any_join('JOIN', table, exprs) end |
#left_join(table, *exprs) ⇒ Object
68 69 70 |
# File 'lib/sqlstmt/sqlstmt.rb', line 68 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
94 95 96 97 |
# File 'lib/sqlstmt/sqlstmt.rb', line 94 def no_where @data.where_behavior = :exclude return self end |
#optional_where ⇒ Object
this disables a where clause requirement
100 101 102 103 |
# File 'lib/sqlstmt/sqlstmt.rb', line 100 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
88 89 90 91 |
# File 'lib/sqlstmt/sqlstmt.rb', line 88 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
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/sqlstmt/sqlstmt.rb', line 110 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
123 124 125 |
# File 'lib/sqlstmt/sqlstmt.rb', line 123 def setq(field, value) return set(field, value.to_sql) end |
#table(ref, use_index = nil) ⇒ Object
tables & joins
59 60 61 62 |
# File 'lib/sqlstmt/sqlstmt.rb', line 59 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 |