module SqlSkelton::Utils
module SqlSkelton::SuffixId¶ ↑
Summary¶ ↑
Module to define the constant {SqlSkelton::SuffixId::SuffixId} and other common functions.
Constants
- SuffixId
Suffix to append to an “id”-type column name (in “client”-DBs).
Private Instance Methods
Remove the 'public.' prefix for the table (for the internal use)
@param tblname [String] Table name @return [String]
# File lib/db_suit_rails/sql_skelton/utils.rb, line 49 def remove_sqlprefix(tblname) return tblname if !defined?(tblname.gsub) tblname = tblname.sub(/^public\./, '') end
Returns a unique new table/column name.
If the CANDIDATE is accepted, returns it. Else it searches for CANDIDATE1, CANDIDATE2, … etc. Or, n1_CANDIDATE, n2_CANDIDATE1, … etc, if useprefix option is true.
This method yields a block, where a candidate name is given and true should be returned when accepted.
@param name_root [String, NilClass] Candidate new column name. If unspecified, copied from oldcol @param useprefix [Boolean] If true, add a prefix as opposed to suffix. @yield [name_cand] Candidate name is passed to the block. It should return true if the name is OK. @yieldparam [name_cand, String] Candidate name @yieldreturn [String, NilClass] Candidate name if found, else nil. @return [String, NilClass] Unique new column name
# File lib/db_suit_rails/sql_skelton/utils.rb, line 35 def unique_name_cand(name_root, useprefix: false) yield(name_root) && (return name_root) (1..99999).each do |i| name_cand = (useprefix ? sprintf('n%d_%s', i, name_root) : sprintf('%s%d', name_root, i)) yield(name_cand) && (return name_cand) end nil end