Method: Sequel::Dataset#unused_table_alias
- Defined in:
- lib/sequel/dataset/misc.rb
#unused_table_alias(table_alias, used_aliases = []) ⇒ Object
Creates a unique table alias that hasn’t already been used in the dataset. table_alias can be any type of object accepted by alias_symbol. The symbol returned will be the implicit alias in the argument, possibly appended with “_N” if the implicit alias has already been used, where N is an integer starting at 0 and increasing until an unused one is found.
You can provide a second addition array argument containing symbols that should not be considered valid table aliases. The current aliases for the FROM and JOIN tables are automatically included in this array.
DB[:table].unused_table_alias(:t)
# => :t
DB[:table].unused_table_alias(:table)
# => :table_0
DB[:table, :table_0].unused_table_alias(:table)
# => :table_1
DB[:table, :table_0].unused_table_alias(:table, [:table_1, :table_2])
# => :table_3
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/sequel/dataset/misc.rb', line 233 def unused_table_alias(table_alias, used_aliases = []) table_alias = alias_symbol(table_alias) used_aliases += opts[:from].map{|t| alias_symbol(t)} if opts[:from] used_aliases += opts[:join].map{|j| j.table_alias ? alias_alias_symbol(j.table_alias) : alias_symbol(j.table)} if opts[:join] if used_aliases.include?(table_alias) i = 0 while true ta = :"#{table_alias}_#{i}" return ta unless used_aliases.include?(ta) i += 1 end else table_alias end end |