Sha256: 309aea8dd9d80650bbb1227f5a3e81b09a999191b69a9cb1198ffbae5e4e2a18

Contents?: true

Size: 1.23 KB

Versions: 11

Compression:

Stored size: 1.23 KB

Contents

module SchemaPlus
  module Core
    module SqlStruct
      IndexComponents = KeyStruct[:name, :type, :columns, :options, :algorithm, :using]

      class Table < KeyStruct[:command, :name, :body, :options, :quotechar, :inheritance]

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

        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
        def assemble
          ["#{command} #{quotechar}#{name}#{quotechar} (#{body})", inheritance, options].reject(&:blank?).join(" ")
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
schema_plus_core-2.2.3 lib/schema_plus/core/sql_struct.rb
schema_plus_core-2.2.2 lib/schema_plus/core/sql_struct.rb
schema_plus_core-2.2.1 lib/schema_plus/core/sql_struct.rb
schema_plus_core-2.2.0 lib/schema_plus/core/sql_struct.rb
schema_plus_core-2.1.1 lib/schema_plus/core/sql_struct.rb
schema_plus_core-2.1.0 lib/schema_plus/core/sql_struct.rb
schema_plus_core-2.0.1 lib/schema_plus/core/sql_struct.rb
schema_plus_core-1.0.2 lib/schema_plus/core/sql_struct.rb
schema_plus_core-1.0.1 lib/schema_plus/core/sql_struct.rb
schema_plus_core-1.0.0 lib/schema_plus/core/sql_struct.rb
schema_plus_core-0.6.2 lib/schema_plus/core/sql_struct.rb