Class: FreeZipcodeData::CountryTable

Inherits:
DbTable
  • Object
show all
Defined in:
lib/free_zipcode_data/country_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
# File 'lib/free_zipcode_data/country_table.rb', line 7

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

#write(row) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/free_zipcode_data/country_table.rb', line 26

def write(row)
  country_hash = country_lookup_table[row[:country]]

  sql = "    INSERT INTO countries (alpha2, alpha3, iso, name)\n    VALUES ('\#{row[:country]}',\n      '\#{country_hash[:alpha3]}',\n      '\#{country_hash[:iso]}',\n      '\#{country_hash[:name]}')\n  SQL\n\n  begin\n    database.execute(sql)\n  rescue SQLite3::ConstraintException\n    # Swallow duplicates\n  end\n\n  update_progress\nend\n"