Class: SchemaPlus::Core::SqlStruct::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/schema_plus/core/sql_struct.rb

Constant Summary collapse

INHERITANCE_REGEX =
%r{ \s* (?<inheritance>INHERITS \s* \( [^)]* \)) }mxi

Instance Method Summary collapse

Instance Method Details

#assembleObject



31
32
33
# File 'lib/schema_plus/core/sql_struct.rb', line 31

def assemble
  ["#{command} #{quotechar}#{name}#{quotechar} (#{body})", inheritance, options].reject(&:blank?).join(" ")
end

#parse!(sql) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/schema_plus/core/sql_struct.rb', line 10

def parse!(sql)
  m = sql.strip.match %r{
  \A
  (?<command>.*\bTABLE\b) \s*
    (?<quote>['"`])(?<name>\S+)\k<quote> \s*
    \( \s*
    (?<body>.*) \s*
    \) \s*
    # can't use optional ? for inheritance because it would be greedily grabbed into body;
    # ideally this would use an actual parser rather than regex
    #{INHERITANCE_REGEX if sql.match INHERITANCE_REGEX}
    (?<options> \S.*)?
  \Z
  }mxi
  self.command = m[:command]
  self.quotechar = m[:quote]
  self.name = m[:name]
  self.body = m[:body]
  self.options = m[:options]
  self.inheritance = m[:inheritance] rescue nil
end