Class: MyObfuscate::Mysql
- Inherits:
-
Object
- Object
- MyObfuscate::Mysql
- Includes:
- InsertStatementParser
- Defined in:
- lib/my_obfuscate/mysql.rb
Instance Method Summary collapse
-
#context_aware_mysql_string_split(string) ⇒ Object
Be aware, strings must be quoted in single quotes!.
- #insert_regex ⇒ Object
- #make_insert_statement(table_name, column_names, values) ⇒ Object
- #make_valid_value_string(value) ⇒ Object
- #parse_insert_statement(line) ⇒ Object
- #rows_to_be_inserted(line) ⇒ Object
Methods included from InsertStatementParser
Instance Method Details
#context_aware_mysql_string_split(string) ⇒ Object
Be aware, strings must be quoted in single quotes!
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 |
# File 'lib/my_obfuscate/mysql.rb', line 42 def context_aware_mysql_string_split(string) in_sub_insert = false in_quoted_string = false escaped = false current_field = nil length = string.length fields = [] output = [] string.each_char do |i| if escaped escaped = false current_field ||= "" current_field << i else if i == "\\" escaped = true current_field ||= "" current_field << i elsif i == "(" && !in_quoted_string && !in_sub_insert in_sub_insert = true elsif i == ")" && !in_quoted_string && in_sub_insert fields << current_field unless current_field.nil? output << fields unless fields.length == 0 in_sub_insert = false fields = [] current_field = nil elsif i == "'" && !in_quoted_string fields << current_field unless current_field.nil? current_field = '' in_quoted_string = true elsif i == "'" && in_quoted_string fields << current_field unless current_field.nil? current_field = nil in_quoted_string = false elsif i == "," && !in_quoted_string && in_sub_insert fields << current_field unless current_field.nil? current_field = nil elsif i == "L" && !in_quoted_string && in_sub_insert && current_field == "NUL" current_field = nil fields << current_field elsif (i == " " || i == "\t") && !in_quoted_string # Don't add whitespace not in a string elsif in_sub_insert current_field ||= "" current_field << i end end end fields << current_field unless current_field.nil? output << fields unless fields.length == 0 output end |
#insert_regex ⇒ Object
22 23 24 |
# File 'lib/my_obfuscate/mysql.rb', line 22 def insert_regex /^\s*INSERT INTO `(.*?)` \((.*?)\) VALUES\s*/i end |
#make_insert_statement(table_name, column_names, values) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/my_obfuscate/mysql.rb', line 14 def make_insert_statement(table_name, column_names, values) values_strings = values.collect do |values| "(" + values.join(",") + ")" end.join(",") "INSERT INTO `#{table_name}` (`#{column_names.join('`, `')}`) VALUES #{values_strings};" end |
#make_valid_value_string(value) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/my_obfuscate/mysql.rb', line 31 def make_valid_value_string(value) if value.nil? "NULL" elsif value =~ /^0x[0-9a-fA-F]+$/ value else "'" + value + "'" end end |
#parse_insert_statement(line) ⇒ Object
5 6 7 8 9 10 11 12 |
# File 'lib/my_obfuscate/mysql.rb', line 5 def parse_insert_statement(line) if regex_match = insert_regex.match(line) { :table_name => regex_match[1].to_sym, :column_names => regex_match[2].split(/`\s*,\s*`/).map { |col| col.gsub('`', "").to_sym } } end end |
#rows_to_be_inserted(line) ⇒ Object
26 27 28 29 |
# File 'lib/my_obfuscate/mysql.rb', line 26 def rows_to_be_inserted(line) line = line.gsub(insert_regex, '').gsub(/\s*;\s*$/, '') context_aware_mysql_string_split(line) end |