Class: Sequel::Extension::PgComment::SqlGenerator
- Inherits:
-
Object
- Object
- Sequel::Extension::PgComment::SqlGenerator
- Defined in:
- lib/sequel/extensions/pg_comment/sql_generator.rb
Overview
:nodoc: Generate SQL to set a comment.
Direct Known Subclasses
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
-
#comment ⇒ Object
readonly
The comment.
-
#object_name ⇒ Object
readonly
The raw (might-be-a-symbol, might-be-a-string) object name that was passed to us originally.
-
#object_type ⇒ Object
readonly
The canonicalised string (that is, all-uppercase, with words separated by spaces) for the object type of this SQL generator.
Class Method Summary collapse
-
.create(object_type, object_name, comment) ⇒ SqlGenerator
Find the correct class for a given object type, and instantiate a new one of them.
-
.handles?(object_type) ⇒ TrueClass, FalseClass
Return whether or not this class supports the specified object type.
Instance Method Summary collapse
-
#generate ⇒ String
SQL to set a comment on the object of our affection.
-
#initialize(object_type, object_name, comment) ⇒ SqlGenerator
constructor
Spawn a new SqlGenerator.
Constructor Details
#initialize(object_type, object_name, comment) ⇒ SqlGenerator
Spawn a new SqlGenerator.
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
#comment ⇒ Object (readonly)
The comment.
113 114 115 |
# File 'lib/sequel/extensions/pg_comment/sql_generator.rb', line 113 def comment @comment end |
#object_name ⇒ Object (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_type ⇒ Object (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.
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.
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
#generate ⇒ String
SQL to set a comment on the object of our affection.
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 |