Class: Sequel::Extension::PgComment::TableObjectSqlGenerator

Inherits:
SqlGenerator
  • Object
show all
Defined in:
lib/sequel/extensions/pg_comment/sql_generator.rb

Overview

:nodoc: A specialised generator for object types that live "inside" a table. Specifically, those types are columns, constraints, rules, and triggers.

They get their own subclass because these object types can be manipulated inside a create_table or alter_table block, and at the time the block is evaluated, the code doesn't know the name of the table in which they are contained. So, we just stuff what we do know into these generators, and then when all's done, we can go to each of these generators, say "this is your table name", and then ask for the generated SQL.

Constant Summary collapse

OBJECT_TYPES =

The few object types that this class handles.

%w{COLUMN CONSTRAINT RULE TRIGGER}

Instance Attribute Summary collapse

Attributes inherited from SqlGenerator

#comment, #object_name, #object_type

Instance Method Summary collapse

Methods inherited from SqlGenerator

create, handles?

Constructor Details

#initialize(object_type, object_name, comment) ⇒ TableObjectSqlGenerator

Overridden constructor to deal with the double-underscore-separated names that we all know and love.

See Also:

  • Sequel::Extension::PgComment::TableObjectSqlGenerator.{SqlGenerator{SqlGenerator#initialize}


176
177
178
179
180
181
182
# File 'lib/sequel/extensions/pg_comment/sql_generator.rb', line 176

def initialize(object_type, object_name, comment)
  super

  if object_name.is_a?(Symbol) and object_name.to_s.index("__")
    @table_name, @object_name = object_name.to_s.split("__", 2).map(&:to_sym)
  end
end

Instance Attribute Details

#table_nameObject

The name of the object which contains the object which is the direct target of this SQL generator. Basically, it's the table name.



169
170
171
# File 'lib/sequel/extensions/pg_comment/sql_generator.rb', line 169

def table_name
  @table_name
end

Instance Method Details

#generateObject

Generate special SQL.

See Also:

  • Sequel::Extension::PgComment::TableObjectSqlGenerator.{SqlGenerator{SqlGenerator#generate}


188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/sequel/extensions/pg_comment/sql_generator.rb', line 188

def generate
  if table_name.nil?
    raise ArgumentError,
          "Cannot generate SQL for #{object_type} #{object_name} " +
            "without a table_name"
  end

  qualified_object_name = case object_type
  when "COLUMN"
    "#{maybe_escape table_name}.#{maybe_escape object_name}"
  when "CONSTRAINT", "RULE", "TRIGGER"
    "#{maybe_escape object_name} ON #{maybe_escape table_name}"
  end
  
  "COMMENT ON #{object_type} #{qualified_object_name} IS #{literal comment}"
end