Class: SqlFinder::Find
- Inherits:
-
Object
- Object
- SqlFinder::Find
- Defined in:
- lib/sqlfinder/find.rb
Overview
Using the options initialized by the block passed to the run method, this class builds a regular expression and applies it to the input file handle. All lines that match are written to the output file handle.
Constant Summary collapse
- KEYWORD_REGEX_CONTENT_MAP =
{ "select" => "(\\bselect\\b.+from.+)", "delete" => "(\\bdelete\\b.+from.+)", "insert" => "(\\binsert\\b.+into.+)", "update" => "(\\bupdate\\b.+set.+)", "update_with_table" => "(\\bupdate\\b.+)" }
Class Method Summary collapse
-
.run(&b) ⇒ Object
Expects a block that defines instance variables and calls the do_find method.
Instance Method Summary collapse
-
#build_keyword_regex_content ⇒ Object
Dynamically builds the keyword component of the regular expression.
-
#do_find ⇒ Object
Dynamically builds the regular expression, uses it to search the file, and prints the results to the output file handle.
Class Method Details
.run(&b) ⇒ Object
Expects a block that defines instance variables and calls the do_find method. For example: SqlFinder::Find.run do
@input_file_handle = input_file_handle
@output_file_handle = output_file_handle
@keywords = keywords
@table_name = table_name
do_find
end
25 26 27 28 |
# File 'lib/sqlfinder/find.rb', line 25 def self.run(&b) finder = SqlFinder::Find.new finder.instance_eval(&b) end |
Instance Method Details
#build_keyword_regex_content ⇒ Object
Dynamically builds the keyword component of the regular expression
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/sqlfinder/find.rb', line 45 def build_keyword_regex_content regex_content_array = [] @keywords.each do |keyword| key = keyword.downcase if ((key == "update") && @table_name) #potentially a special case key << "_with_table" end regex_content_array << KEYWORD_REGEX_CONTENT_MAP[key] end return regex_content_array.join "|" end |
#do_find ⇒ Object
Dynamically builds the regular expression, uses it to search the file, and prints the results to the output file handle
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/sqlfinder/find.rb', line 32 def do_find unless (@keywords && (@keywords.size > 0)) @keywords = %w[select delete insert update] end keyword_regex = build_keyword_regex_content() regex = /^.*((#{keyword_regex})\b#{@table_name}\b.*)/i @input_file_handle.each do |line| m = line.match(regex) @output_file_handle.puts "#{$.}: #{m[1]}" if m end end |