# frozen_string_literal: true module SchemaPlus module Core module SqlStruct IndexComponents = Struct.new(:name, :type, :columns, :options, :algorithm, :using, keyword_init: true) class Table < Struct.new(:command, :name, :body, :options, :quotechar, :inheritance, keyword_init: true) INHERITANCE_REGEX = %r{ \s* (?INHERITS \s* \( [^)]* \)) }mxi def parse!(sql) m = sql.strip.match %r{ \A (?.*\bTABLE\b) \s* (?['"`])(?\S+)\k \s* \( \s* (?.*) \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} (? \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