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

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



8
9
10
# File 'lib/schema_plus/core/sql_struct.rb', line 8

def body
  @body
end

#commandObject

Returns the value of attribute command

Returns:

  • (Object)

    the current value of command



8
9
10
# File 'lib/schema_plus/core/sql_struct.rb', line 8

def command
  @command
end

#inheritanceObject

Returns the value of attribute inheritance

Returns:

  • (Object)

    the current value of inheritance



8
9
10
# File 'lib/schema_plus/core/sql_struct.rb', line 8

def inheritance
  @inheritance
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



8
9
10
# File 'lib/schema_plus/core/sql_struct.rb', line 8

def name
  @name
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



8
9
10
# File 'lib/schema_plus/core/sql_struct.rb', line 8

def options
  @options
end

#quotecharObject

Returns the value of attribute quotechar

Returns:

  • (Object)

    the current value of quotechar



8
9
10
# File 'lib/schema_plus/core/sql_struct.rb', line 8

def quotechar
  @quotechar
end

Instance Method Details

#assembleObject



33
34
35
# File 'lib/schema_plus/core/sql_struct.rb', line 33

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

#parse!(sql) ⇒ Object



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

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