Class: FreeZipcodeData::StateTable
- Inherits:
-
DbTable
- Object
- DbTable
- FreeZipcodeData::StateTable
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
Instance Method Details
#build ⇒ Object
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 = <<-SQL
create table #{tablename} (
id integer not null primary key,
country_id integer not null,
abbr varchar(2) not null,
name varchar(255)
)
SQL
database.execute_batch(schema)
ndx = <<-SQL
CREATE UNIQUE INDEX "main"."unique_state"
ON #{tablename} (abbr, country_id COLLATE NOCASE ASC);
SQL
database.execute_batch(ndx)
ndx = <<-SQL
CREATE UNIQUE INDEX "main"."state_name"
ON #{tablename} (name COLLATE NOCASE ASC);
SQL
database.execute_batch(ndx)
end
|
#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 = <<-SQL
INSERT INTO states (abbr, name, country_id)
VALUES ('#{row[:short_state]}',
'#{escape_single_quotes(row[:state])}',
#{country_id}
)
SQL
begin
database.execute(sql)
rescue SQLite3::ConstraintException
end
update_progress
end
|