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 69 def self.extended(db) 70 db.instance_exec do 71 @enum_labels = {} 72 parse_enum_labels 73 end 74 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 81 def add_enum_value(enum, value, opts=OPTS) 82 sql = String.new 83 sql << "ALTER TYPE #{quote_schema_table(enum)} ADD VALUE#{' IF NOT EXISTS' if opts[:if_not_exists]} #{literal(value.to_s)}" 84 if v = opts[:before] 85 sql << " BEFORE #{literal(v.to_s)}" 86 elsif v = opts[:after] 87 sql << " AFTER #{literal(v.to_s)}" 88 end 89 run sql 90 parse_enum_labels 91 nil 92 end
Run the SQL
to create an enum type with the given name and values.
# File lib/sequel/extensions/pg_enum.rb 95 def create_enum(enum, values) 96 sql = "CREATE TYPE #{quote_schema_table(enum)} AS ENUM (#{values.map{|v| literal(v.to_s)}.join(', ')})" 97 run sql 98 parse_enum_labels 99 nil 100 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 115 def drop_enum(enum, opts=OPTS) 116 sql = "DROP TYPE#{' IF EXISTS' if opts[:if_exists]} #{quote_schema_table(enum)}#{' CASCADE' if opts[:cascade]}" 117 run sql 118 parse_enum_labels 119 nil 120 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 104 def rename_enum(enum, new_name) 105 sql = "ALTER TYPE #{quote_schema_table(enum)} RENAME TO #{quote_schema_table(new_name)}" 106 run sql 107 parse_enum_labels 108 nil 109 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 127 def parse_enum_labels 128 enum_labels = metadata_dataset.from(:pg_enum). 129 order(:enumtypid, :enumsortorder). 130 select_hash_groups(Sequel.cast(:enumtypid, Integer).as(:v), :enumlabel).freeze 131 enum_labels.each_value(&:freeze) 132 133 if respond_to?(:register_array_type) 134 array_types = metadata_dataset. 135 from(:pg_type). 136 where(:oid=>enum_labels.keys). 137 exclude(:typarray=>0). 138 select_map([:typname, Sequel.cast(:typarray, Integer).as(:v)]) 139 140 existing_oids = conversion_procs.keys 141 array_types.each do |name, oid| 142 next if existing_oids.include?(oid) 143 register_array_type(name, :oid=>oid) 144 end 145 end 146 147 Sequel.synchronize{@enum_labels.replace(enum_labels)} 148 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 152 def schema_post_process(_) 153 super.each do |_, s| 154 oid = s[:oid] 155 if values = Sequel.synchronize{@enum_labels[oid]} 156 s[:type] = :enum 157 s[:enum_values] = values 158 end 159 end 160 end
Typecast the given value to a string.
# File lib/sequel/extensions/pg_enum.rb 163 def typecast_value_enum(value) 164 value.to_s 165 end