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

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.

Parameters:

  • object (#to_s)

    The name of the database object to retrieve. For most objects, this should be the literal name of the object. However, for columns on tables and views, the name of the table/view should be a separated from the name of the column by a double underscore (ie __).

Returns:

  • (String, NilClass)

    The comment on the object, or nil if no comment has been defined for the 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.

Parameters:

  • type (#to_s)

    The type of object that you wish to comment on. This can either be a string or symbol. Any object type that PgSQL knows about should be fair game. The current list of object types that this plugin knows about (and hence will accept) is listed in the OBJECT_TYPES array.

  • id (#to_s)

    The name of the object that you wish to comment on. For most types of object, this should be the literal name of the object. However, for columns in a table or view, you should separate the table/view name from the column name with a double underscore (ie __). This is the standard Sequel convention for such things.

  • comment (String)

    The comment you wish to set for the database object.

See Also:

  • for details on how the comment string is interpreted.


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, options)
	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