module Sequel::Postgres::EnumDatabaseMethods
Methods enabling Database object integration with enum types.
Public Class Methods
Parse the available enum values when loading this extension into your database.
# File lib/sequel/extensions/pg_enum.rb, line 68 def self.extended(db) db.instance_exec do @enum_labels = {} parse_enum_labels end 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, line 80 def add_enum_value(enum, value, opts=OPTS) sql = String.new sql << "ALTER TYPE #{quote_schema_table(enum)} ADD VALUE#{' IF NOT EXISTS' if opts[:if_not_exists]} #{literal(value.to_s)}" if v = opts[:before] sql << " BEFORE #{literal(v.to_s)}" elsif v = opts[:after] sql << " AFTER #{literal(v.to_s)}" end run sql parse_enum_labels nil end
Run the SQL to create an enum type with the given name and values.
# File lib/sequel/extensions/pg_enum.rb, line 94 def create_enum(enum, values) sql = "CREATE TYPE #{quote_schema_table(enum)} AS ENUM (#{values.map{|v| literal(v.to_s)}.join(', ')})" run sql parse_enum_labels nil 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, line 114 def drop_enum(enum, opts=OPTS) sql = "DROP TYPE#{' IF EXISTS' if opts[:if_exists]} #{quote_schema_table(enum)}#{' CASCADE' if opts[:cascade]}" run sql parse_enum_labels nil 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, line 103 def rename_enum(enum, new_name) sql = "ALTER TYPE #{quote_schema_table(enum)} RENAME TO #{quote_schema_table(new_name)}" run sql parse_enum_labels nil end
Private Instance Methods
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, line 126 def parse_enum_labels enum_labels = metadata_dataset.from(:pg_enum). order(:enumtypid, :enumsortorder). select_hash_groups(Sequel.cast(:enumtypid, Integer).as(:v), :enumlabel).freeze enum_labels.each_value(&:freeze) if respond_to?(:register_array_type) array_types = metadata_dataset. from(:pg_type). where(:oid=>enum_labels.keys). exclude(:typarray=>0). select_map([:typname, Sequel.cast(:typarray, Integer).as(:v)]) existing_oids = conversion_procs.keys array_types.each do |name, oid| next if existing_oids.include?(oid) register_array_type(name, :oid=>oid) end end Sequel.synchronize{@enum_labels.replace(enum_labels)} end
For schema entries that are enums, set the type to :enum and add a :enum_values entry with the enum values.
# File lib/sequel/extensions/pg_enum.rb, line 151 def schema_post_process(_) super.each do |_, s| oid = s[:oid] if values = Sequel.synchronize{@enum_labels[oid]} s[:type] = :enum s[:enum_values] = values end end end
Typecast the given value to a string.
# File lib/sequel/extensions/pg_enum.rb, line 162 def typecast_value_enum(value) value.to_s end