Class: Metasploit::Model::Search::Query
- Defined in:
- app/models/metasploit/model/search/query.rb
Overview
Once search operators are defined, a formatted
query, composed of space separated formatted operation, <operator.name>:<formatted_value>
, can be parsed to
produce a validatable query.
query = Metasploit::Model::Search::Query.new(
formatted: 'parts.number:1 parts.number:2 parts.uuid:EX,QR'
)
Operations using the same operator are unioned together, while operations with different operator are intersected
together, so the above formatted query can be thought of as
(parts.number:1 || parts.number:2) && parts.uuid:EX,QR
.
Results
Once a Query is defined, it needs to be converted to a data store specific visitor.
Visitors for ActiveRecord
are defined in
MetasploitDataModels::Search::Visitor.
If you want to define your own visitors, you can subclass Visitation::Visitor.
Instance Attribute Summary collapse
-
#formatted ⇒ String
Query string containing space separated
: pairs. -
#formatted_operations ⇒ Array<String>
#formatted broken up into individual operation (
: ) Strings. -
#klass ⇒ Class, #search_operator_by_name
The klass that is being searched.
-
#operations ⇒ Array<Metasploit::Model::Search::Operation::Base>
Parses #formatted to create search operations that can validate if the value is correct the operation's operator's type.
Class Method Summary collapse
-
.formatted_operations(formatted) ⇒ Array<String>
Parses #formatted into a list of formatted operation composed of
: Strings.
Instance Method Summary collapse
-
#operations_by_operator ⇒ Hash{Metasploit::Model::Search::Operator::Base => Metasploit::Model::Search::Operation::Base}
using unknown operators} with the same name should not be grouped together when processing this query into an actual search of record and/or models.
-
#operations_valid ⇒ void
private
Validates that all #operations are valid.
-
#parse_operator(formatted_operator) ⇒ Metasploit::Model::Search::Operation::Base, Metasploit::Model::Search::Operator::Null
Converts formatted operator extracted from formatted operation to its Operator::Base instance.
-
#tree ⇒ Metasploit::Model::Search::Group::Intersection<Metasploit::Model::Search::Group::Union<Metasploit::Model::Search::Operation::Base>>
Groups #operations together by Operation::Base#operator into unions that are intersected.
-
#without_operator(operator) ⇒ Metasploit::Model:Search::Query
Returns a new query with all #operations on the given
operator
removed.
Methods inherited from Base
Constructor Details
This class inherits a constructor from Metasploit::Model::Base
Instance Attribute Details
#formatted ⇒ String
Query string containing space separated
33 34 35 |
# File 'app/models/metasploit/model/search/query.rb', line 33 def formatted @formatted end |
#formatted_operations ⇒ Array<String>
#formatted broken up into individual operation (
41 |
# File 'app/models/metasploit/model/search/query.rb', line 41 attr_writer :formatted_operations |
#klass ⇒ Class, #search_operator_by_name
The klass that is being searched.
47 48 49 |
# File 'app/models/metasploit/model/search/query.rb', line 47 def klass @klass end |
#operations ⇒ Array<Metasploit::Model::Search::Operation::Base>
Parses #formatted to create search operations that can validate if the value is correct the operation's operator's type.
53 |
# File 'app/models/metasploit/model/search/query.rb', line 53 attr_writer :operations |
Class Method Details
.formatted_operations(formatted) ⇒ Array<String>
Parses #formatted into a list of formatted operation composed of
79 80 81 |
# File 'app/models/metasploit/model/search/query.rb', line 79 def self.formatted_operations(formatted) Shellwords.shellsplit(formatted.to_s) end |
Instance Method Details
#operations_by_operator ⇒ Hash{Metasploit::Model::Search::Operator::Base => Metasploit::Model::Search::Operation::Base}
Query is validated before grouping the operations as {Metasploit::Model::Search::Operation::Null operation
using unknown operators} with the same name should not be grouped together when processing this query into an actual search of record and/or models.
Groups #operations together by Operation::Base#operator.
117 118 119 120 121 122 123 124 125 |
# File 'app/models/metasploit/model/search/query.rb', line 117 def operations_by_operator unless instance_variable_defined? :@operations_by_operator valid! @operations_by_operator = operations.group_by(&:operator) end @operations_by_operator ||= operations.group_by(&:operator) end |
#operations_valid ⇒ void (private)
This method returns an undefined value.
Validates that all #operations are valid.
188 189 190 191 192 |
# File 'app/models/metasploit/model/search/query.rb', line 188 def operations_valid unless operations.all?(&:valid?) errors.add(:operations, :invalid) end end |
#parse_operator(formatted_operator) ⇒ Metasploit::Model::Search::Operation::Base, Metasploit::Model::Search::Operator::Null
Converts formatted operator extracted from formatted operation to its Operator::Base instance.
135 136 137 138 139 140 141 142 143 144 |
# File 'app/models/metasploit/model/search/query.rb', line 135 def parse_operator(formatted_operator) operator_name = formatted_operator.to_sym operator = klass.search_operator_by_name[operator_name] unless operator operator = Metasploit::Model::Search::Operator::Null.new(:name => operator_name) end operator end |
#tree ⇒ Metasploit::Model::Search::Group::Intersection<Metasploit::Model::Search::Group::Union<Metasploit::Model::Search::Operation::Base>>
Groups #operations together by Operation::Base#operator into unions that are intersected.
151 152 153 154 155 156 157 158 159 160 161 |
# File 'app/models/metasploit/model/search/query.rb', line 151 def tree unless instance_variable_defined? :@tree unions = operations_by_operator.collect do |_operator, operations| Metasploit::Model::Search::Group::Union.new(:children => operations) end @tree = Metasploit::Model::Search::Group::Intersection.new(:children => unions) end @tree end |
#without_operator(operator) ⇒ Metasploit::Model:Search::Query
Returns a new query with all #operations on the given operator
removed.
168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'app/models/metasploit/model/search/query.rb', line 168 def without_operator(operator) operations = operations_by_operator[operator] if operations filtered_operations = self.operations - operations self.class.new( klass: klass, operations: filtered_operations ) else self end end |