Sha256: ec7aecd9831eeb5a36d7b28cdbcc6e441ac2a0a64a6d2cac1fcb302908d5919e
Contents?: true
Size: 1.61 KB
Versions: 2
Compression:
Stored size: 1.61 KB
Contents
class ETL module Helpers # max_for returns the max value for the passed in column as found in the # specified database.table. If there is not currently a max, we use COALESCE # and a default value. You can specify a :default_floor value or the method # will try to derive it for you. # # Note: we try to detect if we want a date return type via the #datetype? # check. # # If this is found we wrap the whole SELECT clause in a DATE so it is cast # accordingly. def max_for options = {} database = options[:database] table = options[:table] column = options[:column] default_value = options[:default_floor] || default_floor_for(column) if date? default_value default_value = "DATE('#{default_value}')" caster = ->(str) { "DATE(#{str})" } end max_sql_clause = "COALESCE(MAX(#{table}.#{column}), #{default_value})" max_sql_clause = caster.(max_sql_clause) if caster sql = <<-EOS SELECT #{max_sql_clause} AS the_max FROM #{database}.#{table} EOS sql += " WHERE #{options[:conditions]}" if options[:conditions] query(sql).to_a.first['the_max'] end private def date? val val =~ /^\d{4}-\d{1,2}-\d{1,2}( \d{2}:\d{2}:\d{2}( ((-|\+)\d+)| UTC)?)?$/ end def default_floor_for column case column when /_at$/ return '1970-01-01' when /_date$/ return '1970-01-01' when /(^id$|_id$)/ return 0 else raise ArgumentError, "could not determine a default for #{column}" end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
ETL-1.0.0 | lib/etl/helpers.rb |
ETL-1.0.0.rc | lib/etl/helpers.rb |