2014-03-11 18:20:00 +00:00
|
|
|
module JamRuby
|
|
|
|
|
class Region < ActiveRecord::Base
|
|
|
|
|
|
2014-07-20 02:11:16 +00:00
|
|
|
# index names created on the copied table used during import.
|
|
|
|
|
# they do not exist except during import
|
|
|
|
|
COUNTRY_CODE_INDEX_NAME = 'regions_countrycode_ndx'
|
|
|
|
|
COPIED_COUNTRY_CODE_INDEX_NAME = 'regions_copied_countrycode_ndx'
|
|
|
|
|
|
|
|
|
|
UNIQUE_INDEX_NAME = 'regions_countrycode_region_ndx'
|
|
|
|
|
COPIED_UNIQUE_INDEX_NAME = 'regions_copied_countrycode_region_ndx'
|
|
|
|
|
|
|
|
|
|
@@log = Logging.logger[Region]
|
|
|
|
|
|
2014-03-11 18:20:00 +00:00
|
|
|
self.table_name = 'regions'
|
|
|
|
|
|
|
|
|
|
def self.get_all(country)
|
|
|
|
|
self.where(countrycode: country).order('regionname asc').all
|
|
|
|
|
end
|
|
|
|
|
|
2014-07-20 02:11:16 +00:00
|
|
|
def self.import_from_region_codes(options)
|
|
|
|
|
|
|
|
|
|
file = options[:file]
|
|
|
|
|
use_copy = options[:use_copy]
|
|
|
|
|
|
|
|
|
|
start = Time.now
|
|
|
|
|
|
|
|
|
|
copied_table_name = Database.copy_table(self.table_name)
|
|
|
|
|
|
|
|
|
|
if use_copy
|
|
|
|
|
Database.copy(copied_table_name, file)
|
|
|
|
|
else
|
|
|
|
|
File.open(file, 'r:ISO-8859-1') do |io|
|
|
|
|
|
saved_level = ActiveRecord::Base.logger ? ActiveRecord::Base.logger.level : 0
|
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
|
|
stmt = "INSERT INTO #{copied_table_name} (countrycode, region, regionname) VALUES"
|
|
|
|
|
|
|
|
|
|
vals = ''
|
|
|
|
|
sep = ''
|
|
|
|
|
i = 0
|
|
|
|
|
n = 20
|
2014-07-14 20:53:04 +00:00
|
|
|
|
2014-07-20 02:11:16 +00:00
|
|
|
csv = ::CSV.new(io, {encoding: 'ISO-8859-1', headers: false})
|
|
|
|
|
csv.each do |row|
|
|
|
|
|
|
2016-07-17 15:16:27 +00:00
|
|
|
vals = vals+sep+"(#{ActiveRecord::Base.quote_value(row[0], nil)}, #{ActiveRecord::Base.quote_value(row[1], nil)}, #{ActiveRecord::Base.quote_value(row[2], nil)})"
|
2014-07-20 02:11:16 +00:00
|
|
|
sep = ','
|
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
|
|
if count == 0 or i >= n then
|
|
|
|
|
self.connection.execute stmt+vals
|
|
|
|
|
count += i
|
|
|
|
|
vals = ''
|
|
|
|
|
sep = ''
|
|
|
|
|
i = 0
|
|
|
|
|
|
|
|
|
|
if ActiveRecord::Base.logger and ActiveRecord::Base.logger.level > 1 then
|
|
|
|
|
ActiveRecord::Base.logger.debug "... logging inserts into #{copied_table_name} suspended ..."
|
|
|
|
|
ActiveRecord::Base.logger.level = 1
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if ActiveRecord::Base.logger and count%10000 < n then
|
|
|
|
|
ActiveRecord::Base.logger.level = saved_level
|
|
|
|
|
ActiveRecord::Base.logger.debug "... inserted #{count} into #{copied_table_name} ..."
|
|
|
|
|
ActiveRecord::Base.logger.level = 1
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if i > 0
|
|
|
|
|
self.connection.execute stmt+vals
|
|
|
|
|
count += i
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if ActiveRecord::Base.logger then
|
|
|
|
|
ActiveRecord::Base.logger.level = saved_level
|
|
|
|
|
ActiveRecord::Base.logger.debug "loaded #{count} records into #{copied_table_name}"
|
|
|
|
|
end
|
2014-06-25 14:49:33 +00:00
|
|
|
end
|
|
|
|
|
end
|
2014-07-20 02:11:16 +00:00
|
|
|
|
|
|
|
|
# create indexes -- these will be renamed later in the import process
|
|
|
|
|
Region.connection.execute("CREATE INDEX #{COPIED_COUNTRY_CODE_INDEX_NAME} ON #{copied_table_name} (countrycode)").check
|
|
|
|
|
Region.connection.execute("CREATE UNIQUE INDEX #{COPIED_UNIQUE_INDEX_NAME} ON #{copied_table_name} (countrycode, region)").check
|
|
|
|
|
|
|
|
|
|
elapsed = Time.now - start
|
|
|
|
|
@@log.debug("#{copied_table_name} import took #{elapsed} seconds")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.after_maxmind_import
|
|
|
|
|
self.connection.execute("DROP TABLE #{self.table_name}").check
|
|
|
|
|
self.connection.execute("ALTER INDEX #{COPIED_COUNTRY_CODE_INDEX_NAME} RENAME TO #{COUNTRY_CODE_INDEX_NAME}").check
|
|
|
|
|
self.connection.execute("ALTER INDEX #{COPIED_UNIQUE_INDEX_NAME} RENAME TO #{UNIQUE_INDEX_NAME}").check
|
|
|
|
|
self.connection.execute("ALTER TABLE #{self.table_name}_copied RENAME TO #{self.table_name}").check
|
2014-03-11 18:20:00 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|