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 10, 2019
1 parent 0d66d43 commit 3305ecc
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions lib/miq_expression.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,9 @@ 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)
convert_bytes_untits(exp) if %w[= != <= >= >].include?(operator)
else
attrs[:supported_by_sql] = false
exp.delete(operator)
end
Expand All @@ -319,6 +320,27 @@ def preprocess_for_sql(exp, attrs = nil)
exp.empty? ? [nil, attrs] : [exp, attrs]
end

def convert_bytes_untits(exp)
return if (column_details = col_details[exp.values.first["field"]]).nil?
# attempt to do conversion only if db type of column is integer but 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

def sql_supports_atom?(exp)
operator = exp.keys.first
case operator.downcase
Expand Down

0 comments on commit 3305ecc

Please sign in to comment.