class MiniSql::InlineParamEncoder
Attributes
conn[R]
Public Class Methods
new(conn)
click to toggle source
# File lib/mini_sql/inline_param_encoder.rb, line 7 def initialize(conn) @conn = conn end
Public Instance Methods
encode(sql, *params)
click to toggle source
# File lib/mini_sql/inline_param_encoder.rb, line 11 def encode(sql, *params) if Hash === (hash = params[0]) raise ArgumentError, "Only one hash param is allowed, multiple were sent" if params.length > 1 encode_hash(sql, hash) else encode_array(sql, params) end end
encode_array(sql, array)
click to toggle source
# File lib/mini_sql/inline_param_encoder.rb, line 37 def encode_array(sql, array) i = -1 sql.gsub("?") do i += 1 quote_val(array[i]) end end
encode_hash(sql, hash)
click to toggle source
# File lib/mini_sql/inline_param_encoder.rb, line 20 def encode_hash(sql, hash) sql = sql.dup hash.each do |k, v| sql.gsub!(":#{k}") do # ignore ::int and stuff like that # $` is previous to match if $` && $`[-1] != ":" quote_val(v) else ":#{k}" end end end sql end
quote_val(value)
click to toggle source
# File lib/mini_sql/inline_param_encoder.rb, line 49 def quote_val(value) case value when String then "'#{conn.escape_string(value.to_s)}'" when Numeric then value.to_s when BigDecimal then value.to_s("F") when Time then "'#{quoted_time(value)}'" when Date then "'#{value.to_s}'" when Symbol then "'#{conn.escape_string(value.to_s)}'" when true then "true" when false then "false" when nil then "NULL" when [] then "NULL" when Array value.map do |v| quote_val(v) end.join(', ') else raise TypeError, "can't quote #{value.class.name}" end end
quoted_time(value)
click to toggle source
# File lib/mini_sql/inline_param_encoder.rb, line 45 def quoted_time(value) value.utc.iso8601 end