Class: MdlSql::SqlQuery
- Inherits:
-
Object
- Object
- MdlSql::SqlQuery
- Defined in:
- lib/mdlsql/sqlquery.rb
Class Method Summary collapse
-
.config(values = {}) ⇒ Object
Configuration.
Instance Method Summary collapse
- #columns(*values) ⇒ @see initialize (also: #cols)
- #execute ⇒ Object
-
#initialize ⇒ SqlQuery
constructor
A new instance of SqlQuery.
- #insert ⇒ Object
- #join(table, cond1, cond2, opts = {}) ⇒ Object
- #leftjoin(table, cond1, cond2, opts = {}) ⇒ Object
-
#select ⇒ Object
Method selection: select() insert() update(table=nil).
- #set(val = {}) ⇒ Object
-
#tables(tables = {}) ⇒ @see initialize
(also: #into, #from)
Selects table from which to select (or insert, update) with possible alias.
- #update(table = nil) ⇒ Object
- #values(*val) ⇒ Object
-
#where(cond1, cond2, opts = {}) ⇒ Object
Generates a where clause Maybe it’s a good idea to make a Where object.
Constructor Details
#initialize ⇒ SqlQuery
Returns a new instance of SqlQuery.
21 22 23 24 25 26 |
# File 'lib/mdlsql/sqlquery.rb', line 21 def initialize # Initializes query as a string # @return self [SqlQuery] so other methods may be concatenated. return self end |
Class Method Details
.config(values = {}) ⇒ Object
Configuration
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/mdlsql/sqlquery.rb', line 32 def self.config(values={}) # Does config need to be parsed from a config file? # @todo check relative paths so file is correctly found. # Maybe should be wise to use full paths. if values[:parse] if values[:parse] == :yml || values[:parse] == :yaml if values[:file] values = YAML.load_file(values[:file]) # Convert keys to symbols values = values.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} else raise ArgumentError, "File value missing, config file could not be parsed." end end end @@host = values[:host] @@username = values[:username] @@password = values[:password] @@db = values[:database] @@socket = values[:socket] end |
Instance Method Details
#columns(*values) ⇒ @see initialize Also known as: cols
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/mdlsql/sqlquery.rb', line 95 def columns(*values) @cols ||= Hash.new if values[0].is_a? Hash values.each do |val| @cols.update(val) end else values.each do |val| @cols.update({val => val}) end end return self end |
#execute ⇒ Object
|
|
# File 'lib/mdlsql/sqlquery.rb', line 215
|
#insert ⇒ Object
72 73 74 75 |
# File 'lib/mdlsql/sqlquery.rb', line 72 def insert() @method = :insert return self end |
#join(table, cond1, cond2, opts = {}) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/mdlsql/sqlquery.rb', line 171 def join(table, cond1, cond2, opts={}) @join ||= Array.new vars = { :table => table, :cond1 => cond1, :cond2 => cond2, :op => opts[:op], :type => opts[:type] } @join.push Join.new vars return self end |
#leftjoin(table, cond1, cond2, opts = {}) ⇒ Object
186 187 188 189 |
# File 'lib/mdlsql/sqlquery.rb', line 186 def leftjoin(table, cond1, cond2, opts={}) opts.update({:type => :left}) join(table,cond1,cond2, opts) end |
#select ⇒ Object
Method selection: select() insert() update(table=nil)
64 65 66 67 68 69 70 |
# File 'lib/mdlsql/sqlquery.rb', line 64 def select() # Sets method to select # @return (@see initialize) @method = :select return self end |
#set(val = {}) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/mdlsql/sqlquery.rb', line 203 def set(val={}) if @method == :update @values ||= Hash.new @values.update val else raise 'Use set() only for #update.' end return self end |
#tables(tables = {}) ⇒ @see initialize Also known as: into, from
use a hash here to allow many tables (for a select, for example).
use from() when selecting & into() when inserting for readability.
Selects table from which to select (or insert, update) with possible alias.
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/mdlsql/sqlquery.rb', line 120 def tables(tables = {}) # table = table.to_sym if table.is_a? String # table_alias = table_alias if table_alias.is_a? String @tables ||= Array.new tables.each do |table,table_alias| @tables.push Table.new table, table_alias end # @table = table # @table_alias = table_alias unless table_alias.nil? return self end |
#update(table = nil) ⇒ Object
77 78 79 80 81 |
# File 'lib/mdlsql/sqlquery.rb', line 77 def update(table=nil) @method = :update from table if table return self end |
#values(*val) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/mdlsql/sqlquery.rb', line 191 def values(*val) if @method == :insert @values ||= Array.new @values << val else raise 'Use values() only for #insert.' end return self end |
#where(cond1, cond2, opts = {}) ⇒ Object
Add IN, BETWEEN and LIKE (can be done with actual where).
First where clause’s concat is ignored.
Generates a where clause Maybe it’s a good idea to make a Where object.
FFS, HAVE A CLEAR IDEA BEFORE WRITING!
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/mdlsql/sqlquery.rb', line 149 def where(cond1, cond2, opts={}) opts[:op] ||= '=' opts[:concat] ||= :AND # if cond1.is_a? Hash # if cond1[:table] && cond1[:col] # cond1 = Col.new cond[:col], cond[:table] @where ||= Array.new wh = Where.new( :cond1 => cond1, :cond2 => cond2, :op => opts[:op], :concat => opts[:concat] ) @where.push wh return self end |