Class: Sequel::Extension::PgComment::SqlGenerator

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

Overview

:nodoc: Generate SQL to set a comment.

Direct Known Subclasses

PrefixSqlGenerator, TableObjectSqlGenerator

Constant Summary collapse

OBJECT_TYPES =

The PostgreSQL object types which this class knows how to generate comment SQL for.

%w{AGGREGATE
             CAST
             COLLATION
             CONVERSION
             DATABASE
             DOMAIN
  EXTENSION
  EVENT\ TRIGGER
  FOREIGN\ DATA\ WRAPPER
  FOREIGN\ TABLE
  FUNCTION
  INDEX
  LARGE\ OBJECT
  MATERIALIZED\ VIEW
  OPERATOR
  OPERATOR\ CLASS
  OPERATOR\ FAMILY
  PROCEDURAL\ LANGUAGE
  LANGUAGE
  ROLE
  SCHEMA
  SEQUENCE
  SERVER
  TABLE
  TABLESPACE
  TEXT\  SEARCH\ CONFIGURATION
  TEXT\ SEARCH\ DICTIONARY
  TEXT\ SEARCH\ PARSER
  TEXT\ SEARCH\ TEMPLATE
  TYPE
  VIEW
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object_type, object_name, comment) ⇒ SqlGenerator

Spawn a new SqlGenerator.

See Also:

  • Sequel::Extension::PgComment::SqlGenerator.{{.create}


119
120
121
122
123
# File 'lib/sequel/extensions/pg_comment/sql_generator.rb', line 119

def initialize(object_type, object_name, comment)
  @object_type = object_type.to_s.upcase.gsub('_', ' ')
  @object_name = object_name
  @comment     = comment
end

Instance Attribute Details

#commentObject (readonly)

The comment.



113
114
115
# File 'lib/sequel/extensions/pg_comment/sql_generator.rb', line 113

def comment
  @comment
end

#object_nameObject (readonly)

The raw (might-be-a-symbol, might-be-a-string) object name that was passed to us originally.



110
111
112
# File 'lib/sequel/extensions/pg_comment/sql_generator.rb', line 110

def object_name
  @object_name
end

#object_typeObject (readonly)

The canonicalised string (that is, all-uppercase, with words separated by spaces) for the object type of this SQL generator.



105
106
107
# File 'lib/sequel/extensions/pg_comment/sql_generator.rb', line 105

def object_type
  @object_type
end

Class Method Details

.create(object_type, object_name, comment) ⇒ SqlGenerator

Find the correct class for a given object type, and instantiate a new one of them.

Parameters:

  • The type of object we're going to comment on. Strings and symbols are both fine, and any case is fine, too. Any underscores get turned into spaces. Apart from that, it needs to be the exact name that PostgreSQL uses for the given type.

  • The name of the database object to set the comment on. A string is considered "already quoted", and hence is not escaped any further. A symbol is run through the usual Sequel identifier escaping code before being unleashed on the world.

  • The comment to set.

Returns:

  • Some sort of SqlGenerator object, or a subclass.

Raises:

  • if you passed in an object_type that we don't know about.



63
64
65
66
67
68
69
70
71
72
# File 'lib/sequel/extensions/pg_comment/sql_generator.rb', line 63

def self.create(object_type, object_name, comment)
  generators.each do |gclass|
    if gclass.handles?(object_type)
      return gclass.new(object_type, object_name, comment)
    end
  end

  raise ArgumentError,
        "Unrecognised object type #{object_type.inspect}"
end

.handles?(object_type) ⇒ TrueClass, FalseClass

Return whether or not this class supports the specified object type.

Parameters:

Returns:

  • whether or not this class can handle the object type you passed.



81
82
83
# File 'lib/sequel/extensions/pg_comment/sql_generator.rb', line 81

def self.handles?(object_type)
  self.const_get(:OBJECT_TYPES).include?(object_type.to_s.upcase.gsub('_', ' '))
end

Instance Method Details

#generateString

SQL to set a comment on the object of our affection.

Returns:

  • The SQL needed to set the comment.



129
130
131
132
133
134
135
136
137
138
# File 'lib/sequel/extensions/pg_comment/sql_generator.rb', line 129

def generate
  quoted_object_name = case object_name
  when Symbol
    literal object_name
  else
    object_name
  end
    
  "COMMENT ON #{object_type} #{quoted_object_name} IS #{literal comment.to_s}"
end