module Sequel::Postgres::EnumDatabaseMethods

Methods enabling Database object integration with enum types.

Public Class Methods

extended(db) click to toggle source

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

add_enum_value(enum, value, opts=OPTS) click to toggle source

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
create_enum(enum, values) click to toggle source

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
drop_enum(enum, opts=OPTS) click to toggle source

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
rename_enum(enum, new_name) click to toggle source

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
rename_enum_value(enum, old_name, new_name) click to toggle source

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

Private Instance Methods

_process_enum_change_sql(sql) click to toggle source

Run the SQL on the database, reparsing the enum labels after it is run.

    # File lib/sequel/extensions/pg_enum.rb
129 def _process_enum_change_sql(sql)
130   run(sql)
131   parse_enum_labels
132   nil
133 end
parse_enum_labels() click to toggle source

Parse the pg_enum table to get enum values, and the pg_type table to get names and array oids for enums.

    # File lib/sequel/extensions/pg_enum.rb
138 def parse_enum_labels
139   order = [:enumtypid]
140   order << :enumsortorder if server_version >= 90100
141 
142   enum_labels = metadata_dataset.from(:pg_enum).
143     order(*order).
144     select_hash_groups(Sequel.cast(:enumtypid, Integer).as(:v), :enumlabel).freeze
145   enum_labels.each_value(&:freeze)
146 
147   if respond_to?(:register_array_type)
148     array_types = metadata_dataset.
149       from(:pg_type).
150       where(:oid=>enum_labels.keys).
151       exclude(:typarray=>0).
152       select_map([:typname, Sequel.cast(:typarray, Integer).as(:v)])
153 
154     existing_oids = conversion_procs.keys
155     array_types.each do |name, oid|
156       next if existing_oids.include?(oid)
157       register_array_type(name, :oid=>oid)
158     end
159   end
160 
161   Sequel.synchronize{@enum_labels.replace(enum_labels)}
162 end
schema_post_process(_) click to toggle source

For schema entries that are enums, set the type to :enum and add a :enum_values entry with the enum values.

Calls superclass method
    # File lib/sequel/extensions/pg_enum.rb
166 def schema_post_process(_)
167   super.each do |_, s|
168     oid = s[:oid]
169     if s[:type] == :enum && (values = Sequel.synchronize{@enum_labels[oid]})
170       s[:enum_values] = values
171     end
172   end
173 end
typecast_value_enum(value) click to toggle source

Typecast the given value to a string.

    # File lib/sequel/extensions/pg_enum.rb
176 def typecast_value_enum(value)
177   value.to_s
178 end