Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes state_abbr is not globally unique #19

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/free_zipcode_data/county_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 16 additions & 4 deletions lib/free_zipcode_data/db_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions lib/free_zipcode_data/state_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion lib/free_zipcode_data/zipcode_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down