Module: Piola::Sql

Defined in:
lib/piola/sql.rb

Instance Method Summary collapse

Instance Method Details

#searchify(*args) ⇒ Object

Converts a string to a sql search query



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/piola/sql.rb', line 42

def searchify(*args)
  fields  = args.first
  options = args.extract_options!

  operator = options[:operator] || :and
  operator = operator.to_s.upcase

  str = self
  str = str.gsub(/ +/, ' ')
  str = str.gsub("'", '')
  str = str.gsub("\\", '')
  str = str.gsub("%", '')
  str = str.gsub("/", ' ')
  str = str.gsub(":", ' ')
  str = str.gsub("=", ' ')
  str = str.gsub("?", ' ')
  str = str.strip

  return '0' unless str.present?

  exact_words         = []
  exacts              = []
  regulars            = []
  exact_word_excludes = []
  exact_excludes      = []
  excludes            = []

  # Check for exact excludes
  exact_excludes_matches = str.scan(/-"(.*?)"/)

  exact_excludes_matches.each do |match|
    match = match.first

    # Remove match from main string
    str = str.gsub(/-"#{match}"/, '').strip

    exact_excludes << match.strip
  end

  # Check for exact word excludes
  exact_word_excludes_matches = str.scan(/-`(.*?)`/)

  exact_word_excludes_matches.each do |match|
    match = match.first

    # Remove match from main string
    str = str.gsub(/-`#{match}`/, '').strip

    exact_word_excludes << match.strip
  end

  # Check for exact words
  exact_word_matches = str.scan(/`(.*?)`/)

  exact_word_matches.each do |match|
    match = match.first

    # Remove match from main string
    str = str.gsub(/`#{match}`/, '').strip

    exact_words << match.strip
  end

  # Check for exact searches
  exact_matches = str.scan(/"(.*?)"/)

  exact_matches.each do |match|
    match = match.first

    # Remove match from main string
    str = str.gsub(/"#{match}"/, '').strip

    exacts << match.strip
  end

  # Check for excludes with spaces after them
  exclude_matches = str.scan(/-.+? /)

  exclude_matches.each do |match|
    match.strip!
    match = match.gsub(/-/, '')

    # Remove match from main string
    str = str.gsub(/-#{match}/, '').strip

    excludes << match.strip
  end

  # Check for excludes at the end of the string
  exclude_matches = str.scan(/-.+?$/)

  exclude_matches.each do |match|
    match.strip!
    match = match.gsub(/-/, '')

    # Remove match from main string
    str = str.gsub(/-#{match}/, '').strip

    excludes << match.strip
  end

  # Check for regular searches
  regulars = str.to_arr

  # Build the query
  query   = []
  rows    = []
  cells   = []

  # Includes
  query_includes = ""

  if exact_words.any? || exacts.any? || regulars.any?
    fields.each do |field|
      cells = []

      exact_words.each do |matcher|
        cells << "(#{field} LIKE \"% #{matcher.remove_quotes} %\" OR #{field} LIKE \"% #{matcher.remove_quotes}.%\" OR #{field} LIKE \"% #{matcher.remove_quotes},%\")"
      end

      [exacts, regulars].each do |matchers|
        matchers.each do |matcher|
          cells << "#{field} LIKE \"%#{matcher.remove_quotes}%\""
        end
      end

      rows << cells.join(" #{operator} ")
    end

    query_includes = rows.join(" OR ")
    query << "(#{query_includes})"
  end

  # Excludes
  query_excludes = ""

  if exact_word_excludes.any? || exact_excludes.any? || excludes.any?
    rows  = []
    cells = []

    fields.each do |field|
      cells = []

      exact_word_excludes.each do |matcher|
        cells << "(#{field} NOT LIKE \"% #{matcher.remove_quotes} %\" OR #{field} NOT LIKE \"% #{matcher.remove_quotes}.%\" OR #{field} NOT LIKE \"% #{matcher.remove_quotes},%\")"
      end

      [exact_excludes, excludes].each do |matchers|
        matchers.each do |matcher|
          cells << "#{field} NOT LIKE \"%#{matcher.remove_quotes}%\""
        end
      end

      rows << cells.join(" #{operator} ")
    end

    query_excludes = rows.join(" AND ")
    query << "(#{query_excludes})"
  end

  return query.join(" AND ") if query.any?
  return '0'
end

#sql_search(fields) ⇒ Object

Converts a string to a sql search string



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/piola/sql.rb', line 9

def sql_search(fields)
  str = self
  str = str.gsub("'", '')
  str = str.gsub('"', '')
  str = str.gsub("\\", '')
  str = str.gsub("%", '')
  str = str.gsub("/", ' ')
  str = str.gsub(":", ' ')
  str = str.gsub("=", ' ')
  str = str.gsub("?", ' ')

  if str.strip.present?
    arr   = str.to_arr
    rows  = []
    cells = []

    fields.each do |field|
      cells = []

      arr.each do |a|
        cells << "(#{field} LIKE \"%#{a}%\")"
      end

      rows << "(" + cells.join(" AND ") + ")"
    end

    return rows.join(" OR ")
  else
    return '0'
  end
end