Module: Sequel::Extension::PgComment::DatabaseMethods
- Defined in:
- lib/sequel/extensions/pg_comment/database_methods.rb
Overview
Support for setting and retrieving comments on all object types in a PostgreSQL database.
Instance Method Summary collapse
-
#alter_table_generator(&block) ⇒ Object
:nodoc: Enhanced version to support setting comments on objects created in a block-form
alter_tablestatement. -
#apply_alter_table_generator(name, generator) ⇒ Object
:nodoc: Enhanced version to support setting comments on objects created in a block-form
alter_tablestatement. -
#comment_for(object) ⇒ String, NilClass
Retrieve the comment for a database object.
-
#comment_on(type, id, comment) ⇒ Object
Set the comment for a database object.
-
#create_table(*args) ⇒ Object
An enhanced form of the standard
create_tablemethod, which supports setting a comment in thecreate_tablecall when the:commentoption is provided. -
#create_table_generator(&block) ⇒ Object
:nodoc: Enhanced version to support setting comments on objects created in a block-form
create_tablestatement. -
#create_table_indexes_from_generator(name, generator, options) ⇒ Object
:nodoc: Enhanced version to support setting comments on objects created in a block-form
create_tablestatement. -
#create_view(*args) ⇒ Object
An enhanced form of the standard
create_viewmethod, which supports setting a comment in thecreate_viewcall when the:commentoption is provided.
Instance Method Details
#alter_table_generator(&block) ⇒ Object
:nodoc:
Enhanced version to support setting comments on objects created in a
block-form alter_table statement.
121 122 123 124 125 126 127 |
# File 'lib/sequel/extensions/pg_comment/database_methods.rb', line 121 def alter_table_generator(&block) super do extend Sequel::Extension::PgComment::AlterTableGeneratorMethods @comments = [] instance_eval(&block) if block end end |
#apply_alter_table_generator(name, generator) ⇒ Object
:nodoc:
Enhanced version to support setting comments on objects created in a
block-form alter_table statement.
133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/sequel/extensions/pg_comment/database_methods.rb', line 133 def apply_alter_table_generator(name, generator) super generator.comments.each do |sql_gen| if sql_gen.respond_to?(:table_name=) sql_gen.table_name = name end execute(sql_gen.generate) end end |
#comment_for(object) ⇒ String, NilClass
Retrieve the comment for a database object.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/sequel/extensions/pg_comment/database_methods.rb', line 47 def comment_for(object) object = object.to_s if object.index("__") tbl, col = object.split("__", 2) execute("SELECT col_description(c.oid, a.attnum) " + "FROM pg_class c JOIN pg_attribute a " + "ON (c.oid=a.attrelid) " + "WHERE c.relname=#{literal(tbl)} " + "AND a.attname=#{literal(col)}" ) else execute("SELECT obj_description(#{literal(object.to_s)}::regclass, 'pg_class')") end end |
#comment_on(type, id, comment) ⇒ Object
Set the comment for a database object.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/sequel/extensions/pg_comment/database_methods.rb', line 25 def comment_on(type, id, comment) gen = begin Sequel::Extension::PgComment::SqlGenerator.create(type, id, comment) rescue ArgumentError raise ArgumentError, "Invalid object type: #{type.inspect}" end execute(gen.generate) end |
#create_table(*args) ⇒ Object
An enhanced form of the standard create_table method, which supports
setting a comment in the create_table call when the :comment option
is provided.
72 73 74 75 76 77 78 |
# File 'lib/sequel/extensions/pg_comment/database_methods.rb', line 72 def create_table(*args) super if args.last.is_a?(Hash) && args.last[:comment] comment_on(:table, args.first, args.last[:comment]) end end |
#create_table_generator(&block) ⇒ Object
:nodoc:
Enhanced version to support setting comments on objects created in a
block-form create_table statement.
84 85 86 87 88 89 90 |
# File 'lib/sequel/extensions/pg_comment/database_methods.rb', line 84 def create_table_generator(&block) super do extend Sequel::Extension::PgComment::CreateTableGeneratorMethods @comments = [] instance_eval(&block) if block end end |
#create_table_indexes_from_generator(name, generator, options) ⇒ Object
:nodoc:
Enhanced version to support setting comments on objects created in a
block-form create_table statement.
If you're wondering why we override the create_table_indexes_from_generator method, rather than create_table_from_generator, it's because the indexes method runs last, and we can only create our comments after the objects we're commenting on have been created. We could set some comments in create_table_from_generator, and then set index comments in create_table_indexes_from_generator, but why override two methods when you can just override one to get the same net result?
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/sequel/extensions/pg_comment/database_methods.rb', line 105 def create_table_indexes_from_generator(name, generator, ) super generator.comments.each do |sql_gen| if sql_gen.respond_to? :table_name sql_gen.table_name = name end execute(sql_gen.generate) end end |
#create_view(*args) ⇒ Object
An enhanced form of the standard create_view method, which supports
setting a comment in the create_view call when the :comment option
is provided.
153 154 155 156 157 158 159 |
# File 'lib/sequel/extensions/pg_comment/database_methods.rb', line 153 def create_view(*args) super if args.last.is_a?(Hash) && args.last[:comment] comment_on(:view, args.first, args.last[:comment]) end end |