module Workarea module MagentoMigrator class MagentoBase class << self attr_writer :client, :table_name STORE_ID = 0 def client @@client ||= Workarea::MagentoMigrator::SqlConnection.new.client end def entity_type_id rows = client.query("SELECT entity_type_id FROM eav_entity_type WHERE entity_type_code = '#{table_name}'") rows.to_a.map{|ent| ent['entity_type_id']}.first end def column_types() rows = client.query("SELECT distinct(backend_type) FROM eav_attribute WHERE entity_type_id = #{entity_type_id}") rows.to_a.map{|r| r["backend_type"]} end def attribute_id_query(col) rows = client.query("SELECT attribute_id FROM eav_attribute WHERE attribute_code = '#{col}' AND entity_type_id = #{entity_type_id}") rows.to_a.map{|r| r["attribute_id"]}.first end def field_names_query "SELECT attribute_code, backend_type FROM eav_attribute WHERE entity_type_id = ? and backend_type = ?" end def select_query select_query = "SELECT #{select_columns} FROM #{table_name}_entity e #{join_clause} " select_query += "\nwhere e.entity_id in (#{ids_to_include})" end def all client.query(select_query).to_a end def columns_to_consider YAML.load_file("#{Gem.loaded_specs['workarea-magento_migrator'].full_gem_path}/config/data/#{table_name}.yml")["columns"]["eav"] end def allowed_field_names field_names.values.flatten.map{|fn| fn if columns_to_consider.include?(fn)} end def select_columns select_cluses = own_field_names.map{|tc| "\ne.#{tc}"} index = 1 field_names.each do |column_type, column_names| column_names.each do |column_name| if columns_to_consider.include?(column_name) select_cluses << " \nv#{index}.value AS '#{column_name}'" index += 1 end end end select_cluses.join(', ') end def join_clause join_clauses = [] index = 1 field_names.each do |column_type, column_names| column_names.each do |column_name| if columns_to_consider.include?(column_name) attribute_id = attribute_id_query(column_name) join_clauses << "\nLEFT JOIN #{table_name}_entity_#{column_type} v#{index} ON e.entity_id = v#{index}.entity_id AND v#{index}.store_id = #{STORE_ID} AND v#{index}.attribute_id = (#{attribute_id}) " index += 1 end end end join_clauses.join end def execute_query(query) if client rows = client.query(query) rows.to_a else Rails.logger.error(Mysql2::ConnectionError) end end def own_field_names client.query("describe #{table_name}_entity").to_a.map{|tc| tc["Field"]} end def all_field_names own_field_names + field_names.values.flatten end def field_names statement = client.prepare(field_names_query) field_names = {} column_types.each do |col| rows = statement.execute(entity_type_id, col) field_names[col.to_s.to_sym] = rows.to_a.map{|r| r["attribute_code"]}.sort end field_names end end # class self declaration end # class end end end