Class: Katello::Util::PackageFilter

Inherits:
Object
  • Object
show all
Defined in:
app/lib/katello/util/package_filter.rb

Constant Summary collapse

LESS_THAN =
"<".freeze
GREATER_THAN =
">".freeze
EQUAL =
"=".freeze
OPERATORS =
[LESS_THAN, GREATER_THAN, EQUAL].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation, evr, operator = nil) ⇒ PackageFilter

Returns a new instance of PackageFilter.



11
12
13
14
15
# File 'app/lib/katello/util/package_filter.rb', line 11

def initialize(relation, evr, operator = nil)
  extract_epoch_version_release(evr)
  self.operator = operator
  self.relation = relation
end

Instance Attribute Details

#epochObject

Returns the value of attribute epoch.



9
10
11
# File 'app/lib/katello/util/package_filter.rb', line 9

def epoch
  @epoch
end

#epoch_specifiedObject

Returns the value of attribute epoch_specified.



9
10
11
# File 'app/lib/katello/util/package_filter.rb', line 9

def epoch_specified
  @epoch_specified
end

#operatorObject

Returns the value of attribute operator.



9
10
11
# File 'app/lib/katello/util/package_filter.rb', line 9

def operator
  @operator
end

#relationObject

Returns the value of attribute relation.



9
10
11
# File 'app/lib/katello/util/package_filter.rb', line 9

def relation
  @relation
end

#releaseObject

Returns the value of attribute release.



9
10
11
# File 'app/lib/katello/util/package_filter.rb', line 9

def release
  @release
end

#versionObject

Returns the value of attribute version.



9
10
11
# File 'app/lib/katello/util/package_filter.rb', line 9

def version
  @version
end

Instance Method Details

#convert(name = '?') ⇒ Object



81
82
83
# File 'app/lib/katello/util/package_filter.rb', line 81

def convert(name = '?')
  "#{name} COLLATE \"C\""
end

#epoch_clause(op) ⇒ Object



61
62
63
# File 'app/lib/katello/util/package_filter.rb', line 61

def epoch_clause(op)
  "CAST(epoch AS INT) #{op} :epoch"
end

#extract_epoch_version_release(evr) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'app/lib/katello/util/package_filter.rb', line 17

def extract_epoch_version_release(evr)
  parsed = Package.parse_evr(evr)
  # Track whether epoch was explicitly specified in the input
  self.epoch_specified = !parsed[:epoch].nil?
  self.epoch = parsed[:epoch].to_i # nil or blank becomes 0
  v = parsed[:version]
  self.version = (v.nil? || v.blank?) ? '' : Package.sortable_version(v)
  r = parsed[:release]
  self.release = (r.nil? || r.blank?) ? '' : Package.sortable_version(r)
end

#release_clause(op) ⇒ Object



73
74
75
76
77
78
79
# File 'app/lib/katello/util/package_filter.rb', line 73

def release_clause(op)
  if op == '='
    "release_sortable = :release"
  else
    "#{convert(:release_sortable)} #{op} #{convert(':release')}"
  end
end

#resultsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/lib/katello/util/package_filter.rb', line 28

def results
  if operator == EQUAL
    # For equality: only include epoch clause if it was explicitly specified
    # This allows "version 3.2.2" to match any epoch with that version
    conditions = epoch_specified ? epoch_clause(operator) : ''
    conditions += ' AND ' if !conditions.blank? && !version.blank?
    conditions += version_clause(operator) unless version.blank?
    conditions += ' AND ' if !conditions.blank? && !release.blank?
    conditions += release_clause(operator) unless release.blank?

    # Only pass epoch parameter if it was used in the conditions
    if epoch_specified
      self.relation.where(conditions, :version => version, :release => release, :epoch => epoch)
    else
      self.relation.where(conditions, :version => version, :release => release)
    end
  else
    # For comparison operators (>, <, etc.): always include epoch
    # This is required for proper RPM version comparison (epoch:version-release)
    # If epoch not specified, it defaults to 0 which is correct RPM behavior
    conditions = ''
    unless version.blank?
      unless release.blank?
        conditions = " OR (#{version_clause('=')} AND #{release_clause(operator)})"
      end
      conditions = " OR (#{epoch_clause('=')} AND (#{version_clause(operator)}#{conditions}))"
    end
    conditions = "#{epoch_clause(operator)}#{conditions}"

    self.relation.where(conditions, :version => version, :release => release, :epoch => epoch)
  end
end

#version_clause(op) ⇒ Object



65
66
67
68
69
70
71
# File 'app/lib/katello/util/package_filter.rb', line 65

def version_clause(op)
  if op == '='
    "version_sortable = :version"
  else
    "#{convert(:version_sortable)} #{op} #{convert(':version')}"
  end
end