Module: PgPower::ConnectionAdapters::PostgreSQLAdapter::CommentMethods
- Included in:
- PgPower::ConnectionAdapters::PostgreSQLAdapter
- Defined in:
- lib/pg_power/connection_adapters/postgresql_adapter/comment_methods.rb
Overview
Provides methods to extend ActiveRecord::ConnectionAdapters::PostgreSQLAdapter to support comments feature.
Instance Method Summary collapse
-
#comments(table_name) ⇒ Object
Fetches all comments related to passed table.
-
#index_comments ⇒ Object
Fetches index comments returns an Array of Arrays, each element representing a single index with comment as [ ‘schema_name’, ‘index_name’, ‘comment’ ].
-
#remove_column_comment(table_name, column_name) ⇒ Object
Executes SQL to remove comment on column.
-
#remove_column_comments(table_name, *column_names) ⇒ Object
Remove comments on passed table columns.
-
#remove_index_comment(index_name) ⇒ Object
Removes any comment from the given index.
-
#remove_table_comment(table_name) ⇒ Object
Executes SQL to remove comment on passed table.
-
#set_column_comment(table_name, column_name, comment) ⇒ Object
Executes SQL to set comment on column.
-
#set_column_comments(table_name, comments) ⇒ Object
Sets comments on columns of passed table.
-
#set_index_comment(index_name, comment) ⇒ Object
Sets the given comment on the given index.
-
#set_table_comment(table_name, comment) ⇒ Object
Executes SQL to set comment on table.
- #supports_comments? ⇒ Boolean
Instance Method Details
#comments(table_name) ⇒ Object
Fetches all comments related to passed table. I returns table comment and column comments as well.
Example
comments("users") # => [[ "" , "Comment on table" ],
["id" , "Comment on id column" ],
["email", "Comment on email column"]]
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/pg_power/connection_adapters/postgresql_adapter/comment_methods.rb', line 77 def comments(table_name) relation_name, schema_name = table_name.split(".", 2).reverse schema_name ||= :public com = select_all <<-SQL SELECT a.attname AS column_name, d.description AS comment FROM pg_description d JOIN pg_class c on c.oid = d.objoid LEFT OUTER JOIN pg_attribute a ON c.oid = a.attrelid AND a.attnum = d.objsubid JOIN pg_namespace ON c.relnamespace = pg_namespace.oid WHERE c.relkind = 'r' AND c.relname = '#{relation_name}' AND pg_namespace.nspname = '#{schema_name}' SQL com.map do |row| [ row['column_name'], row['comment'] ] end end |
#index_comments ⇒ Object
Fetches index comments returns an Array of Arrays, each element representing a single index with comment as
[ 'schema_name', 'index_name', 'comment' ]
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/pg_power/connection_adapters/postgresql_adapter/comment_methods.rb', line 98 def index_comments query = <<-SQL SELECT c.relname AS index_name, d.description AS comment, pg_namespace.nspname AS schema_name FROM pg_description d JOIN pg_class c ON c.oid = d.objoid JOIN pg_namespace ON c.relnamespace = pg_namespace.oid WHERE c.relkind = 'i' ORDER BY schema_name, index_name SQL com = select_all(query) com.map do |row| [ row['schema_name'], row['index_name'], row['comment'] ] end end |
#remove_column_comment(table_name, column_name) ⇒ Object
Executes SQL to remove comment on column.
52 53 54 55 |
# File 'lib/pg_power/connection_adapters/postgresql_adapter/comment_methods.rb', line 52 def remove_column_comment(table_name, column_name) sql = "COMMENT ON COLUMN #{quote_table_name(table_name)}.#{quote_column_name(column_name)} IS NULL;" execute sql end |
#remove_column_comments(table_name, *column_names) ⇒ Object
Remove comments on passed table columns.
58 59 60 61 62 |
# File 'lib/pg_power/connection_adapters/postgresql_adapter/comment_methods.rb', line 58 def remove_column_comments(table_name, *column_names) column_names.each do |column_name| remove_column_comment table_name, column_name end end |
#remove_index_comment(index_name) ⇒ Object
Removes any comment from the given index
66 67 68 69 |
# File 'lib/pg_power/connection_adapters/postgresql_adapter/comment_methods.rb', line 66 def remove_index_comment(index_name) sql = "COMMENT ON INDEX #{quote_string(index_name)} IS NULL;" execute sql end |
#remove_table_comment(table_name) ⇒ Object
Executes SQL to remove comment on passed table.
44 45 46 47 |
# File 'lib/pg_power/connection_adapters/postgresql_adapter/comment_methods.rb', line 44 def remove_table_comment(table_name) sql = "COMMENT ON TABLE #{quote_table_name(table_name)} IS NULL;" execute sql end |
#set_column_comment(table_name, column_name, comment) ⇒ Object
Executes SQL to set comment on column.
20 21 22 23 |
# File 'lib/pg_power/connection_adapters/postgresql_adapter/comment_methods.rb', line 20 def set_column_comment(table_name, column_name, comment) sql = "COMMENT ON COLUMN #{quote_table_name(table_name)}.#{quote_column_name(column_name)} IS $$#{comment}$$;" execute sql end |
#set_column_comments(table_name, comments) ⇒ Object
Sets comments on columns of passed table.
28 29 30 31 32 |
# File 'lib/pg_power/connection_adapters/postgresql_adapter/comment_methods.rb', line 28 def set_column_comments(table_name, comments) comments.each_pair do |column_name, comment| set_column_comment table_name, column_name, comment end end |
#set_index_comment(index_name, comment) ⇒ Object
Sets the given comment on the given index
37 38 39 40 |
# File 'lib/pg_power/connection_adapters/postgresql_adapter/comment_methods.rb', line 37 def set_index_comment(index_name, comment) sql = "COMMENT ON INDEX #{quote_string(index_name)} IS $$#{comment}$$;" execute sql end |
#set_table_comment(table_name, comment) ⇒ Object
Executes SQL to set comment on table
11 12 13 14 |
# File 'lib/pg_power/connection_adapters/postgresql_adapter/comment_methods.rb', line 11 def set_table_comment(table_name, comment) sql = "COMMENT ON TABLE #{quote_table_name(table_name)} IS $$#{comment}$$;" execute sql end |
#supports_comments? ⇒ Boolean
4 5 6 |
# File 'lib/pg_power/connection_adapters/postgresql_adapter/comment_methods.rb', line 4 def supports_comments? true end |