Sha256: 6e1bf984d785102cf5ef2862c1e36d5a1b04ece1c4f05f4a4a964f71b1a4a24f
Contents?: true
Size: 1.38 KB
Versions: 3
Compression:
Stored size: 1.38 KB
Contents
class Baza::Driver::Pg::CreateIndexSqlCreator def initialize(args) @db = args.fetch(:db) @indexes = args.fetch(:indexes) @create_args = args.fetch(:create_args) end def sqls sqls = [] @indexes.each do |index_data| sqls << create_sql(index_data, @create_args) end sqls end def name_from_table_and_columns(table_name, column_names) "index_on_#{table_name}_#{column_names.join("_")}" end def create_sql(index_data, args) sql = "" sql << "CREATE" if args[:create] || !args.key?(:create) if index_data.is_a?(String) || index_data.is_a?(Symbol) index_data = {name: index_data, columns: [index_data]} elsif index_data[:name].to_s.strip.empty? index_data[:name] = name_from_table_and_columns(args[:table_name] || name, index_data.fetch(:columns)) end raise "No columns was given on index: '#{index_data.fetch(:name)}'." if !index_data[:columns] || index_data[:columns].empty? sql << " UNIQUE" if index_data[:unique] sql << " INDEX #{@db.quote_index(index_data.fetch(:name))}" if args[:on_table] || !args.key?(:on_table) sql << " ON #{@db.quote_table(args.fetch(:table_name))}" end sql << " (" first = true index_data.fetch(:columns).each do |col_name| sql << ", " unless first first = false if first sql << @db.quote_column(col_name) end sql << ")" end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
baza-0.0.38 | lib/baza/driver/pg/create_index_sql_creator.rb |
baza-0.0.37 | lib/baza/driver/pg/create_index_sql_creator.rb |
baza-0.0.36 | lib/baza/driver/pg/create_index_sql_creator.rb |