Module: Arcade::Support::Sql
- Included in:
- Base, MatchConnection, Query
- Defined in:
- lib/support/sql.rb
Instance Method Summary collapse
-
#compose_where(*arg, &b) ⇒ Object
supports where: ‘string’ where: { property: ‘value’, property: value, … } where: [‘string, { property: value, … }, … ].
-
#generate_sql_list(attributes = {}, &b) ⇒ Object
designs a list of “Key = Value” pairs combined by “and” or the binding provided by the block ORD.generate_sql_list where: 25 , upper: ‘65’ => “where=25 and upper=‘65’” ORD.generate_sql_list( con_id: 25 , symbol: :G) { ‘,’ } => “con_id=25 , symbol=‘G’”.
Instance Method Details
#compose_where(*arg, &b) ⇒ Object
supports
where: 'string'
where: { property: 'value', property: value, ... }
where: ['string, { property: value, ... }, ... ]
Used by update and select
Usecase:
compose_where 'z=34', u: 6
=> "where z=34 and u = 6"
20 21 22 23 24 25 26 27 |
# File 'lib/support/sql.rb', line 20 def compose_where *arg , &b arg = arg.flatten.compact unless arg.blank? g= generate_sql_list( arg , &b) "where #{g}" unless g.empty? end end |
#generate_sql_list(attributes = {}, &b) ⇒ Object
designs a list of “Key = Value” pairs combined by “and” or the binding provided by the block
ORD.generate_sql_list where: 25 , upper: '65'
=> "where=25 and upper='65'"
ORD.generate_sql_list( con_id: 25 , symbol: :G) { ',' }
=> "con_id=25 , symbol='G'"
If »NULL« should be addressed, { key: nil } is translated to “key = NULL” (used by set: in update and upsert), { key: [nil] } is translated to “key is NULL” ( used by where )
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/support/sql.rb', line 40 def generate_sql_list attributes = {}, &b fill = block_given? ? yield : 'and' case attributes when ::Hash attributes.map do |key, value| case value when nil "#{key}=NULL" when ::Array if value == [nil] "#{key} is NULL" else "#{key} in #{value.to_db}" end when Range "#{key} between #{value.first.to_or} and #{value.last.to_or} " else # String, Symbol, Time, Trueclass, Falseclass ... "#{key}=#{value.to_or}" end end.join(" #{fill} ") when ::Array attributes.map{|y| generate_sql_list y, &b }.join( " #{fill} " ) when String attributes when Symbol, Numeric attributes.to_s end end |