Class: SqlStmt
- Inherits:
-
Object
- Object
- SqlStmt
- Defined in:
- lib/sqlstmt/sqlstmt.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
-
#add_row(row) ⇒ Object
used with INSERT VALUES statements only.
- #delete(*tables) ⇒ Object
-
#distinct ⇒ Object
less commonly used methods.
-
#field(*args) ⇒ Object
transition only mechanisms.
- #get(*exprs) ⇒ Object
- #group_by(expr) ⇒ Object
- #having(*expr) ⇒ Object
- #ignore ⇒ Object
-
#includes_table?(table_to_find) ⇒ Boolean
methods to analyze what the statement contains.
-
#initialize ⇒ SqlStmt
constructor
A new instance of SqlStmt.
- #initialize_copy(orig) ⇒ Object
- #insert ⇒ Object
-
#into(into_table) ⇒ Object
used with INSERT statements only.
- #join(table, *exprs) ⇒ Object
- #left_join(table, *exprs) ⇒ Object
- #limit(clause) ⇒ Object
- #no_where ⇒ Object
- #optional_join(table, expr) ⇒ Object
- #optional_where ⇒ Object
- #order_by(expr) ⇒ Object
- #outfile(str) ⇒ Object
- #replace ⇒ Object
-
#select ⇒ Object
pick statement type.
- #set(field, value) ⇒ Object
- #setq(field, value) ⇒ Object (also: #fieldq)
- #straight_join ⇒ Object
-
#table(table) ⇒ Object
common operations.
- #to_s ⇒ Object (also: #to_sql)
- #update ⇒ Object
- #where(*expr) ⇒ Object
Constructor Details
#initialize ⇒ SqlStmt
Returns a new instance of SqlStmt.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/sqlstmt/sqlstmt.rb', line 7 def initialize @stmt_type = nil @tables = [] @joins = [] @wheres = [] @where_behavior = :require @fields = [] @values = [] @group_by = nil @order_by = nil @limit = nil @having = [] @into_table = nil @rows = [] @tables_to_delete = [] @distinct = nil @straight_join = nil @replace = nil @ignore = '' @outfile = '' end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
5 6 7 |
# File 'lib/sqlstmt/sqlstmt.rb', line 5 def fields @fields end |
#joins ⇒ Object (readonly)
Returns the value of attribute joins.
5 6 7 |
# File 'lib/sqlstmt/sqlstmt.rb', line 5 def joins @joins end |
#tables ⇒ Object (readonly)
Returns the value of attribute tables.
5 6 7 |
# File 'lib/sqlstmt/sqlstmt.rb', line 5 def tables @tables end |
#wheres ⇒ Object (readonly)
Returns the value of attribute wheres.
5 6 7 |
# File 'lib/sqlstmt/sqlstmt.rb', line 5 def wheres @wheres end |
Instance Method Details
#add_row(row) ⇒ Object
used with INSERT VALUES statements only
141 142 143 |
# File 'lib/sqlstmt/sqlstmt.rb', line 141 def add_row(row) @rows << row end |
#delete(*tables) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/sqlstmt/sqlstmt.rb', line 60 def delete(*tables) ensure_no_statement_type @stmt_type = 'delete' @tables_to_delete = tables return self end |
#distinct ⇒ Object
less commonly used methods
153 154 155 156 |
# File 'lib/sqlstmt/sqlstmt.rb', line 153 def distinct @distinct = true return self end |
#field(*args) ⇒ Object
transition only mechanisms
186 187 188 189 190 191 192 |
# File 'lib/sqlstmt/sqlstmt.rb', line 186 def field(*args) if ['update','insert'].include?(@stmt_type) set(args[0], args[1]) else get(*args) end end |
#get(*exprs) ⇒ Object
97 98 99 100 |
# File 'lib/sqlstmt/sqlstmt.rb', line 97 def get(*exprs) @fields.concat(exprs) return self end |
#group_by(expr) ⇒ Object
114 115 116 117 |
# File 'lib/sqlstmt/sqlstmt.rb', line 114 def group_by(expr) @group_by = expr return self end |
#having(*expr) ⇒ Object
129 130 131 132 |
# File 'lib/sqlstmt/sqlstmt.rb', line 129 def having(*expr) @having.concat(expr) return self end |
#ignore ⇒ Object
168 169 170 171 |
# File 'lib/sqlstmt/sqlstmt.rb', line 168 def ignore @ignore = 'IGNORE ' return self end |
#includes_table?(table_to_find) ⇒ Boolean
methods to analyze what the statement contains
179 180 181 182 183 |
# File 'lib/sqlstmt/sqlstmt.rb', line 179 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) } return retval end |
#initialize_copy(orig) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/sqlstmt/sqlstmt.rb', line 29 def initialize_copy(orig) super @tables = @tables.dup @joins = @joins.dup @wheres = @wheres.dup @fields = @fields.dup @values = @values.dup @having = @having.dup @rows = @rows.dup end |
#insert ⇒ Object
54 55 56 57 58 |
# File 'lib/sqlstmt/sqlstmt.rb', line 54 def insert ensure_no_statement_type @stmt_type = 'insert' return self end |
#into(into_table) ⇒ Object
used with INSERT statements only
135 136 137 138 |
# File 'lib/sqlstmt/sqlstmt.rb', line 135 def into(into_table) @into_table = into_table return self end |
#join(table, *exprs) ⇒ Object
74 75 76 |
# File 'lib/sqlstmt/sqlstmt.rb', line 74 def join(table, *exprs) return add_join('JOIN', table, exprs) end |
#left_join(table, *exprs) ⇒ Object
78 79 80 |
# File 'lib/sqlstmt/sqlstmt.rb', line 78 def left_join(table, *exprs) return add_join('LEFT JOIN', table, exprs) end |
#limit(clause) ⇒ Object
124 125 126 127 |
# File 'lib/sqlstmt/sqlstmt.rb', line 124 def limit(clause) @limit = clause return self end |
#no_where ⇒ Object
87 88 89 90 |
# File 'lib/sqlstmt/sqlstmt.rb', line 87 def no_where @where_behavior = :exclude return self end |
#optional_join(table, expr) ⇒ Object
195 196 197 198 199 |
# File 'lib/sqlstmt/sqlstmt.rb', line 195 def optional_join(table, expr) unless includes_table?(table) join(table, expr) end end |
#optional_where ⇒ Object
92 93 94 95 |
# File 'lib/sqlstmt/sqlstmt.rb', line 92 def optional_where @where_behavior = :optional return self end |
#order_by(expr) ⇒ Object
119 120 121 122 |
# File 'lib/sqlstmt/sqlstmt.rb', line 119 def order_by(expr) @order_by = expr return self end |
#outfile(str) ⇒ Object
173 174 175 176 |
# File 'lib/sqlstmt/sqlstmt.rb', line 173 def outfile(str) @outfile = " INTO OUTFILE #{str}" return self end |
#replace ⇒ Object
163 164 165 166 |
# File 'lib/sqlstmt/sqlstmt.rb', line 163 def replace @replace = true return self end |
#select ⇒ Object
pick statement type
42 43 44 45 46 |
# File 'lib/sqlstmt/sqlstmt.rb', line 42 def select ensure_no_statement_type @stmt_type = 'select' return self end |
#set(field, value) ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/sqlstmt/sqlstmt.rb', line 102 def set(field, value) raise "trying to include field #{field} again" if @fields.include?(field) @fields << field value = value.is_a?(String) ? value : value.to_sql @values << value return self end |
#setq(field, value) ⇒ Object Also known as: fieldq
110 111 112 |
# File 'lib/sqlstmt/sqlstmt.rb', line 110 def setq(field, value) return set(field, value.to_sql) end |
#straight_join ⇒ Object
158 159 160 161 |
# File 'lib/sqlstmt/sqlstmt.rb', line 158 def straight_join @straight_join = true return self end |
#table(table) ⇒ Object
common operations
69 70 71 72 |
# File 'lib/sqlstmt/sqlstmt.rb', line 69 def table(table) @tables << table return self end |
#to_s ⇒ Object Also known as: to_sql
145 146 147 148 |
# File 'lib/sqlstmt/sqlstmt.rb', line 145 def to_s verify_minimum_requirements return build_stmt end |
#update ⇒ Object
48 49 50 51 52 |
# File 'lib/sqlstmt/sqlstmt.rb', line 48 def update ensure_no_statement_type @stmt_type = 'update' return self end |
#where(*expr) ⇒ Object
82 83 84 85 |
# File 'lib/sqlstmt/sqlstmt.rb', line 82 def where(*expr) @wheres.concat(expr) return self end |