Skip to content

Commit

Permalink
First pass at MiqExpression 'includes' as SQL
Browse files Browse the repository at this point in the history
This sets up support MiqExpression to introspect the current model of
the expression fragment to see if there are any methods defined there
that can help allow SQL to be executed in MiqExpression for that given
field.

Vm.ipaddresses has been the first attempt at this.
  • Loading branch information
NickLaMuro committed Jun 8, 2018
1 parent dcd9d69 commit 140ec59
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
15 changes: 15 additions & 0 deletions app/models/vm_or_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,21 @@ def self.vms_by_ipaddress(ipaddress)
end
end

def self.miq_expression_includes_any_ipaddresses_arel(ipaddress)
vms = arel_table
networks = Network.arel_table
hardwares = Hardware.arel_table

match_grouping = networks[:ipaddress].matches("%#{ipaddress}%")
.or(networks[:ipv6address].matches("%#{ipaddress}%"))

query = hardwares.project(1)
.join(networks).on(networks[:hardware_id].eq(hardwares[:id]))
.where(hardwares[:vm_or_template_id].eq(vms[:id]).and(match_grouping))
.take(1)
Arel::Nodes::SqlLiteral.new("1").eq(query)
end

def self.scan_by_property(property, value, _options = {})
_log.info("scan_vm_by_property called with property:[#{property}] value:[#{value}]")
case property
Expand Down
14 changes: 14 additions & 0 deletions lib/miq_expression.rb
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,15 @@ def sql_supports_atom?(exp)
# TODO: Support includes operator for sub-sub-tables
return false
end
when "includes any", "includes all", "includes only"
# Support this only from the main model (for now)
if exp[operator].keys.include?("field") && exp[operator]["field"].split(".").length == 1
model, field = exp[operator]["field"].split("-")
method = "miq_expression_#{operator.downcase.tr(' ', '_')}_#{field}_arel"
return model.constantize.respond_to?(method)
else
return false
end
when "find", "regular expression matches", "regular expression does not match", "key exists", "value exists"
return false
else
Expand Down Expand Up @@ -1343,6 +1352,11 @@ def to_arel(exp, tz)
escape = nil
case_sensitive = true
arel_attribute.matches("%#{parsed_value}%", escape, case_sensitive)
when "includes all", "includes any", "includes only"
method = "miq_expression_"
method << "#{operator.downcase.tr(' ', '_')}_"
method << "#{field.column}_arel"
field.model.send(method, parsed_value)
when "starts with"
escape = nil
case_sensitive = true
Expand Down

0 comments on commit 140ec59

Please sign in to comment.