From 4942ea500c6c992303efa991d3222af8c7e5f56b Mon Sep 17 00:00:00 2001 From: Jesper Schmitz Mouridsen Date: Sat, 30 Oct 2021 14:54:56 +0200 Subject: [PATCH] Fixes state_abbr is not globlly unique And fixes cases where countries do not have a state, by using country as state. --- lib/free_zipcode_data/county_table.rb | 2 +- lib/free_zipcode_data/db_table.rb | 20 ++++++++++++++++---- lib/free_zipcode_data/state_table.rb | 4 ++++ lib/free_zipcode_data/zipcode_table.rb | 2 +- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/free_zipcode_data/county_table.rb b/lib/free_zipcode_data/county_table.rb index 22da61d..788ce40 100644 --- a/lib/free_zipcode_data/county_table.rb +++ b/lib/free_zipcode_data/county_table.rb @@ -25,7 +25,7 @@ def build def write(row) return nil unless row[:county] - state_id = get_state_id(row[:short_state], row[:state]) + state_id = get_state_id(row[:country],row[:short_state], row[:state]) return nil unless state_id sql = <<-SQL diff --git a/lib/free_zipcode_data/db_table.rb b/lib/free_zipcode_data/db_table.rb index 12fe4b6..9663001 100644 --- a/lib/free_zipcode_data/db_table.rb +++ b/lib/free_zipcode_data/db_table.rb @@ -43,10 +43,22 @@ def get_country_id(country) select_first(sql) end - def get_state_id(state_abbr, state_name) - sql = "SELECT id FROM states - WHERE abbr = '#{state_abbr}' OR name = '#{escape_single_quotes(state_name)}'" - select_first(sql) + def get_state_id(country,state_abbr, state_name) + sql = "SELECT s.id FROM states s inner join countries c on s.country_id == c.id + WHERE s.abbr = '#{state_abbr}' and s.name = '#{escape_single_quotes(state_name)}' + and c.alpha2 == '#{escape_single_quotes(country)}'" + res = select_first(sql) + if(res == nil) + sql = "SELECT s.id FROM states s inner join countries c on s.country_id == c.id + WHERE s.abbr = '#{state_abbr}' and c.alpha2 == '#{escape_single_quotes(country)}'" + res = select_first(sql) + end + if(res == nil) + sql = "SELECT s.id FROM states s inner join countries c on s.country_id == c.id + WHERE s.name = '#{state_name}' and c.alpha2 == '#{escape_single_quotes(country)}'" + res = select_first(sql) + end + return res end def get_county_id(county) diff --git a/lib/free_zipcode_data/state_table.rb b/lib/free_zipcode_data/state_table.rb index 70a86e0..896b2d7 100644 --- a/lib/free_zipcode_data/state_table.rb +++ b/lib/free_zipcode_data/state_table.rb @@ -29,6 +29,10 @@ def build end def write(row) + if(row[:short_state] == nil || row[:short_state] == '') + row[:short_state] = row[:country] + row[:state] = country_lookup_table[row[:country]][:name] + end return nil unless row[:short_state] row[:state] = 'Marshall Islands' if row[:short_state] == 'MH' && row[:state].nil? country_id = get_country_id(row[:country]) diff --git a/lib/free_zipcode_data/zipcode_table.rb b/lib/free_zipcode_data/zipcode_table.rb index aa59b05..cf398df 100644 --- a/lib/free_zipcode_data/zipcode_table.rb +++ b/lib/free_zipcode_data/zipcode_table.rb @@ -29,7 +29,7 @@ def build def write(row) return nil unless row[:postal_code] - state_id = get_state_id(row[:short_state], row[:state]) + state_id = get_state_id(row[:country],row[:short_state], row[:state]) city_name = escape_single_quotes(row[:city]) sql = <<-SQL