Gsub function issue in Ruby -
i'm attempting create function create prepared statements salesforce queries. requirement escape single quotes; other characters escaped salesforce. when call
prepared_query('select id account id = :id , name = :name limit 1', {:id => '00001234', :name => "john 'smith"}
the expected output
"select id account id = '00001234' , name = 'john \'smith' limit 1"
i'm attempting use gsub
this. function is
def prepared_query(soql, *args) if args[0].is_a? hash args[0].each |key, val| val.gsub!("'", %q(\\\')) soql.gsub! ":#{key}", "'#{val}'" end end end
the output
"select id account id = '00001234' , name = 'john limit 1smith' limit 1"
what causing issue?
when use gsub 2 arguments, replacement string interpreted in special way. relevant case \'
replaced affix of match (the counterpart $'
in ordinary replacement). in order avoid have use block gsub.
a fix code may this:
def prepared_query(soql, h = {}) h.each |key, val| val.gsub!("'", %q(\\\')) soql.gsub!(":#{key}"){"'#{val}'"} end soql end
Comments
Post a Comment