Class: MyObfuscate::Postgres

Inherits:
Object
  • Object
show all
Includes:
CopyStatementParser
Defined in:
lib/my_obfuscate/postgres.rb

Instance Method Summary collapse

Methods included from CopyStatementParser

#parse

Instance Method Details

#make_insert_statement(table_name, column_names, values) ⇒ Object



36
37
38
# File 'lib/my_obfuscate/postgres.rb', line 36

def make_insert_statement(table_name, column_names, values)
  values.join("\t")
end

#make_valid_value_string(value) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/my_obfuscate/postgres.rb', line 40

def make_valid_value_string(value)
  if value.nil?
    "\\N"
  else
    value
  end
end

#parse_copy_statement(line) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/my_obfuscate/postgres.rb', line 27

def parse_copy_statement(line)
  if regex_match = /^\s*COPY (.*?) \((.*?)\) FROM\s*/i.match(line)
    {
        :table_name => regex_match[1].to_sym,
        :column_names => regex_match[2].split(/\s*,\s*/).map(&:to_sym)
    }
  end
end

#parse_insert_statement(line) ⇒ Object



48
49
50
# File 'lib/my_obfuscate/postgres.rb', line 48

def parse_insert_statement(line)
  /^\s*INSERT INTO/i.match(line)
end

#rows_to_be_inserted(line) ⇒ Object

Copy statements contain the column values tab seperated like so:

blah	blah	blah	blah

which we want to turn into:

[['blah','blah','blah','blah']]

We wrap it in an array to keep it consistent with MySql bulk obfuscation (multiple rows per insert statement)



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/my_obfuscate/postgres.rb', line 12

def rows_to_be_inserted(line)
  line.gsub!(/\n$/,"")
  row = line.split(/\t/)

  row.collect! do |value|
    if value == "\\N"
      nil
    else
      value
    end
  end

  [row]
end