Methods enabling Database
object integration with enum types.
Methods
Public Class
Public Instance
Public Class methods
Parse the available enum values when loading this extension into your database.
# File lib/sequel/extensions/pg_enum.rb 78 def self.extended(db) 79 db.instance_exec do 80 @enum_labels = {} 81 parse_enum_labels 82 end 83 end
Public Instance methods
Run the SQL
to add the given value to the existing enum type. Options:
:after |
Add the new value after this existing value. |
:before |
Add the new value before this existing value. |
:if_not_exists |
Do not raise an error if the value already exists in the enum. |
# File lib/sequel/extensions/pg_enum.rb 90 def add_enum_value(enum, value, opts=OPTS) 91 sql = String.new 92 sql << "ALTER TYPE #{quote_schema_table(enum)} ADD VALUE#{' IF NOT EXISTS' if opts[:if_not_exists]} #{literal(value.to_s)}" 93 if v = opts[:before] 94 sql << " BEFORE #{literal(v.to_s)}" 95 elsif v = opts[:after] 96 sql << " AFTER #{literal(v.to_s)}" 97 end 98 _process_enum_change_sql(sql) 99 end
Run the SQL
to create an enum type with the given name and values.
# File lib/sequel/extensions/pg_enum.rb 102 def create_enum(enum, values) 103 _process_enum_change_sql("CREATE TYPE #{quote_schema_table(enum)} AS ENUM (#{values.map{|v| literal(v.to_s)}.join(', ')})") 104 end
Run the SQL
to drop the enum type with the given name. Options:
:if_exists |
Do not raise an error if the enum type does not exist |
:cascade |
Also drop other objects that depend on the enum type |
# File lib/sequel/extensions/pg_enum.rb 122 def drop_enum(enum, opts=OPTS) 123 _process_enum_change_sql("DROP TYPE#{' IF EXISTS' if opts[:if_exists]} #{quote_schema_table(enum)}#{' CASCADE' if opts[:cascade]}") 124 end
Run the SQL
to rename the enum type with the given name to the another given name.
# File lib/sequel/extensions/pg_enum.rb 108 def rename_enum(enum, new_name) 109 _process_enum_change_sql("ALTER TYPE #{quote_schema_table(enum)} RENAME TO #{quote_schema_table(new_name)}") 110 end
Run the SQL
to rename the enum value with the given name to the another given name.
# File lib/sequel/extensions/pg_enum.rb 114 def rename_enum_value(enum, old_name, new_name) 115 _process_enum_change_sql("ALTER TYPE #{quote_schema_table(enum)} RENAME VALUE #{literal(old_name.to_s)} TO #{literal(new_name.to_s)}") 116 end