Class: FreeZipcodeData::StateTable

Inherits:
DbTable
  • Object
show all
Defined in:
lib/free_zipcode_data/state_table.rb

Constant Summary

Constants inherited from DbTable

DbTable::ISSUE_URL

Instance Attribute Summary

Attributes inherited from DbTable

#database, #tablename

Instance Method Summary collapse

Methods inherited from DbTable

#initialize, #update_progress

Constructor Details

This class inherits a constructor from FreeZipcodeData::DbTable

Instance Method Details

#buildObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/free_zipcode_data/state_table.rb', line 7

def build
  schema = "    create table \#{tablename} (\n      id integer not null primary key,\n      country_id integer not null,\n      abbr varchar(2) not null,\n      name varchar(255)\n    )\n  SQL\n  database.execute_batch(schema)\n\n  ndx = <<-SQL\n    CREATE UNIQUE INDEX \"main\".\"unique_state\"\n    ON \#{tablename} (abbr, country_id COLLATE NOCASE ASC);\n  SQL\n  database.execute_batch(ndx)\n\n  ndx = <<-SQL\n    CREATE UNIQUE INDEX \"main\".\"state_name\"\n    ON \#{tablename} (name COLLATE NOCASE ASC);\n  SQL\n  database.execute_batch(ndx)\nend\n"

#write(row) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/free_zipcode_data/state_table.rb', line 31

def write(row)
  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])
  sql = "    INSERT INTO states (abbr, name, country_id)\n    VALUES ('\#{row[:short_state]}',\n      '\#{escape_single_quotes(row[:state])}',\n      \#{country_id}\n    )\n  SQL\n  begin\n    database.execute(sql)\n  rescue SQLite3::ConstraintException\n    # Swallow duplicates\n  end\n\n  update_progress\nend\n"