Sha256: f6c17a1d58d3ecc624e3a6ccc2f2f341d597fed5b5a311d9f93a4d888c7f2754
Contents?: true
Size: 680 Bytes
Versions: 17
Compression:
Stored size: 680 Bytes
Contents
module SQL # Example: # # indexed_case_stmt(:code, "DT", "AC", "XY") # # Will return the string: # # CASE code # WHEN 'DT' THEN 1 # WHEN 'AC' THEN 2 # END # # Used for creating an explicit sort order in conjunction with a find: # # Description.where(code: codes).order(indexed_case_stmt(:code, codes)) # class IndexedCaseStmt def initialize(column, items) @column = column @items = items end def generate clauses = [] @items.each_with_index do |item, index| clauses << "WHEN '#{item}' THEN #{index}" end "CASE #{@column} #{clauses.join(' ')} END" end end end
Version data entries
17 entries across 17 versions & 1 rubygems