ruby mysql2 equivalent of data_seek(n).fetch_hash -
what equivalent statement in mysql2 this
result = mysql.query('select cache_amount_with_discount_and_tax t_payment organization_id = 1 , receipt_id = '+param['receipt_id'].to_s).data_seek(3).fetch_hash // mysql original mysql client
i'm new ruby , still trying wrap ahead around documentation.
i don't think mysql2
supports data_seek
, can use sql native offset
, better since limits amount transfered client in first place. mysql2
returns rows hashes default, that's non-issue. so...
sql = <<-sql select cache_amount_with_discount_and_tax t_payment organization_id = 1 , receipt_id = #{client.escape(param['receipt_id'])} limit 1 offset 3 sql result = client.query(sql, as: :hash, symbolize_keys: false) hash_row = result.first
the options have given (as: :hash, symbolize_keys: false
) default, , can left off unless have changed defaults; or can mess them customise result (e.g. having symbolic column keys instead of strings).
also, note client.escape
bit. never, ever, feed raw user input sql statement, or bobby tables might next client.
even better using abstraction library provides prepared statement , parameter binding support, sequel.
Comments
Post a Comment