Class: Masking::InsertStatement

Inherits:
Object
  • Object
show all
Defined in:
lib/masking/insert_statement.rb,
lib/masking/insert_statement/sql_builder.rb

Defined Under Namespace

Classes: SQLBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_statement) ⇒ InsertStatement

Returns a new instance of InsertStatement.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/masking/insert_statement.rb', line 10

def initialize(raw_statement)
  @raw_statement = raw_statement

  PARSE_REGEXP.match(raw_statement).tap do |match_data|
    raise Error::InsertStatementParseError if match_data.nil?

    @table           = match_data[:table]
    @columns_section = match_data[:columns_section]
    @values_section  = match_data[:values_section]
  end
end

Instance Attribute Details

#raw_statementObject (readonly)

Returns the value of attribute raw_statement.



8
9
10
# File 'lib/masking/insert_statement.rb', line 8

def raw_statement
  @raw_statement
end

#tableObject (readonly)

Returns the value of attribute table.



8
9
10
# File 'lib/masking/insert_statement.rb', line 8

def table
  @table
end

Instance Method Details

#columnsObject



22
23
24
25
# File 'lib/masking/insert_statement.rb', line 22

def columns
  # NOTE: define and extract to ColumnSet class?
  @columns ||= columns_section.scan(COLUMNS_REGEXP).flatten.map(&:to_sym)
end

#sqlObject



34
35
36
# File 'lib/masking/insert_statement.rb', line 34

def sql
  SQLBuilder.build(table: table, columns: columns, values: values)
end

#valuesObject



27
28
29
30
31
32
# File 'lib/masking/insert_statement.rb', line 27

def values
  # NOTE: define and extract to ValueSet class?
  @values ||= values_section.split(VALUE_ROW_SPLITTER)
                            .tap { |rows| rows.each_with_index { |_, i| recursive_pattern_value_concat(rows, i) } }
                            .flat_map { |row| row.scan(values_regexp) }
end