Module: DatabaseConsistency::Helper::AndClauses

Defined in:
lib/database_consistency/helper/and_clauses.rb

Overview

The AND clauses of a SQL predicate: which ANDs actually join two clauses, and what the clause list looks like once nested conjunctions have been flattened into it.

Class Method Summary collapse

Class Method Details

.append_fragment(clauses, fragment) ⇒ Object

Starts a new clause, or hands the fragment to the previous clause when that one is still waiting for the rest of itself.



55
56
57
58
59
# File 'lib/database_consistency/helper/and_clauses.rb', line 55

def append_fragment(clauses, fragment)
  previous = clauses.pop if clauses.last && incomplete?(clauses.last)

  clauses << [previous, fragment].compact.join(' AND ')
end

.flatten_group(clause) ⇒ Object

a AND (b AND c) means a AND b AND c, so a group whose top level is a conjunction is flattened into the surrounding clause list. An OR nested deeper inside it stays in its own group and is split around.



71
72
73
74
75
76
# File 'lib/database_consistency/helper/and_clauses.rb', line 71

def flatten_group(clause)
  inner = Parentheses.strip_outer(clause)
  return [clause] if inner == clause || top_level_or?(inner)

  split(inner)
end

.incomplete?(clause) ⇒ Boolean

A clause is incomplete while it holds an unclosed parenthesis, or a BETWEEN whose range is still missing the AND that ends it.

Returns:

  • (Boolean)


63
64
65
66
# File 'lib/database_consistency/helper/and_clauses.rb', line 63

def incomplete?(clause)
  Parentheses.unclosed?(clause) ||
    clause.scan(/\bBETWEEN\b/i).length > clause.scan(/\bAND\b/i).length
end

.sort(sql, &sort_key) ⇒ Object

Sorts top-level clauses so a AND b and b AND a reach the same string before comparison. Clauses are kept verbatim so parentheses binding an OR survive. The block, when given, supplies the value each clause is ordered by, which lets a caller order by a clause's real text while the clauses themselves still carry masked literals.



16
17
18
19
20
21
22
23
# File 'lib/database_consistency/helper/and_clauses.rb', line 16

def sort(sql, &sort_key)
  return sql if top_level_or?(sql)

  clauses = split(sql)
  return sql if clauses.length == 1

  clauses.sort_by { |clause| sort_key ? sort_key.call(clause) : clause }.join(' AND ')
end

.split(sql) ⇒ Object

Splits on the ANDs that actually join two clauses: one inside a parenthesized group, or the one completing a BETWEEN range, belongs to its clause instead. A group whose own top level is a conjunction is flattened, so a AND (b AND c) and a AND b AND c split the same way.



47
48
49
50
51
# File 'lib/database_consistency/helper/and_clauses.rb', line 47

def split(sql)
  sql.split(/\s+AND\s+/i)
     .each_with_object([]) { |fragment, clauses| append_fragment(clauses, fragment) }
     .flat_map { |clause| flatten_group(clause) }
end

.top_level_or?(sql) ⇒ Boolean

a AND b OR c means (a AND b) OR c, so ordering the clauses of such a predicate would move one across the OR and change what it means. Active Record's or generates exactly this shape. A predicate carrying one is left alone; split assumes a conjunction and is not asked.

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/database_consistency/helper/and_clauses.rb', line 29

def top_level_or?(sql)
  depth = 0

  sql.scan(/[()]|\bOR\b/i) do |token|
    case token
    when '(' then depth += 1
    when ')' then depth -= 1
    else return true if depth.zero?
    end
  end

  false
end