Skip to content

Commit

Permalink
convert string representation of sizes to numbers when generating SQL…
Browse files Browse the repository at this point in the history
  • Loading branch information
yrudman committed Apr 12, 2019
1 parent 0d66d43 commit 77b8238
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions lib/miq_expression.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,11 @@ def preprocess_for_sql(exp, attrs = nil)
preprocess_for_sql(exp[operator], attrs)
exp.delete(operator) if exp[operator].empty? # Clean out empty operands
else
# check operands to see if they can be represented in sql
unless sql_supports_atom?(exp)
if sql_supports_atom?(exp)
# if filed type is Ineteger and value is String representing size in units (like "2.megabytes") than convert
# this string to correct number using sub_type mappong defined in db/fixtures/miq_report_formats.yml:sub_types_by_column:
convert_size_in_units_to_integer(exp) if %w[= != <= >= >].include?(operator)
else
attrs[:supported_by_sql] = false
exp.delete(operator)
end
Expand Down Expand Up @@ -1279,6 +1282,27 @@ def fields(expression = exp)

private

def convert_size_in_units_to_integer(exp)
return if (column_details = col_details[exp.values.first["field"]]).nil?
# attempt to do conversion only if db type of column is integer and value to compare to is String
return unless column_details[:data_type] == :integer && (value = exp.values.first["value"]).class == String

sub_type = column_details[:format_sub_type]

return if %i[mhz_avg hours kbps kbps_precision_2 mhz elapsed_time].include?(sub_type)

case sub_type
when :bytes
exp.values.first["value"] = value.to_i_with_method
when :kilobytes
exp.values.first["value"] = value.to_i_with_method / 1_024
when :megabytes, :megabytes_precision_2
exp.values.first["value"] = value.to_i_with_method / 1_048_576
else
_log.warn("No subtype defined for column #{exp.values.first["field"]} in 'miq_report_formats.yml'")
end
end

# example:
# ruby_for_date_compare(:updated_at, :date, tz, "==", Time.now)
# # => "val=update_at; !val.nil? && val.to_date == '2016-10-05'"
Expand Down

0 comments on commit 77b8238

Please sign in to comment.