| 1 | # Copyright (C) 2008 Wes Hardaker | ||
| 2 | # License: Same as perl. See the LICENSE file for details. | ||
| 3 | package Ham::Callsign::DB::US; | ||
| 4 | |||
| 5 | use Ham::Callsign::DB; | ||
| 6 | use Ham::Callsign; | ||
| 7 | our @ISA = qw(Ham::Callsign::DB); | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | |||
| 11 | sub init { | ||
| 12 | my ($self) = @_; | ||
| 13 | # maybe use dbh to prepare a few things | ||
| 14 | } | ||
| 15 | |||
| 16 | sub do_load_data { | ||
| 17 | my ($self, $place) = @_; | ||
| 18 | $self->{'import_place'} = $place; | ||
| 19 | $self->insert_stuff('HD', 50); | ||
| 20 | $self->insert_stuff('EN', 27); | ||
| 21 | $self->insert_stuff('AM', 18); | ||
| 22 | $self->insert_stuff('VC', 6); | ||
| 23 | } | ||
| 24 | |||
| 25 | sub do_lookup { | ||
| 26 | my ($self, $callsign) = @_; | ||
| 27 | if (!$self->{'lookupvc'}) { | ||
| 28 | $self->{'lookupvc'} = $self->{'dbh'}->prepare(" | ||
| 29 | select vc.callsign_requested as thecallsign, * | ||
| 30 | from PUBACC_VC as vc | ||
| 31 | left join PUBACC_HD as hd | ||
| 32 | on hd.unique_system_identifier = vc.unique_system_identifier | ||
| 33 | left join PUBACC_EN as en | ||
| 34 | on en.unique_system_identifier = vc.unique_system_identifier | ||
| 35 | left join PUBACC_AM as am | ||
| 36 | on am.unique_system_identifier = vc.unique_system_identifier | ||
| 37 | where vc.callsign_requested = ? | ||
| 38 | order by vc.unique_system_identifier desc | ||
| 39 | limit 1 | ||
| 40 | "); | ||
| 41 | $self->{'lookuphd'} = $self->{'dbh'}->prepare(" | ||
| 42 | select hd.call_sign as thecallsign, * | ||
| 43 | from PUBACC_HD as hd | ||
| 44 | left join PUBACC_VC as vc | ||
| 45 | on vc.unique_system_identifier = hd.unique_system_identifier | ||
| 46 | left join PUBACC_EN as en | ||
| 47 | on en.unique_system_identifier = hd.unique_system_identifier | ||
| 48 | left join PUBACC_AM as am | ||
| 49 | on am.unique_system_identifier = hd.unique_system_identifier | ||
| 50 | where hd.call_sign = ? | ||
| 51 | order by hd.unique_system_identifier desc | ||
| 52 | limit 1 | ||
| 53 | "); | ||
| 54 | } | ||
| 55 | |||
| 56 | $self->{'lookupvc'}->execute($callsign); | ||
| 57 | my $row1 = $self->{'lookupvc'}->fetchrow_hashref; | ||
| 58 | $self->{'lookupvc'}->finish; | ||
| 59 | |||
| 60 | $self->{'lookuphd'}->execute($callsign); | ||
| 61 | my $row2 = $self->{'lookuphd'}->fetchrow_hashref; | ||
| 62 | $self->{'lookuphd'}->finish; | ||
| 63 | |||
| 64 | my @results; | ||
| 65 | if ($row1 && $row2 && !$self->{'USmultiple'}) { | ||
| 66 | # take the most recent ULS file number | ||
| 67 | if ($row1->{'uls_file_number'} > $row2->{'uls_file_number'}) { | ||
| 68 | $row2 = undef; | ||
| 69 | } else { | ||
| 70 | $row1 = undef; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | if ($row1) { | ||
| 74 | $row1->{'qth'} = "$row1->{city}, $row1->{state}, USA"; | ||
| 75 | push @results,new Ham::Callsign(%$row1) | ||
| 76 | } | ||
| 77 | if ($row2) { | ||
| 78 | $row2->{'qth'} = "$row2->{city}, $row2->{state}, USA"; | ||
| 79 | push @results,new Ham::Callsign(%$row2) if ($row2); | ||
| 80 | } | ||
| 81 | map { $_->{'FromDB'} = 'US' } @results; | ||
| 82 | return \@results; | ||
| 83 | } | ||
| 84 | |||
| 85 | # from fcc docs: | ||
| 86 | # EN: Names and Addresses | ||
| 87 | # HD: Main form 605 that carries over to license | ||
| 88 | # AM: Amature data | ||
| 89 | # VC: Vanity Callsign | ||
| 90 | # | ||
| 91 | # data can be joined "whenever the column name is the same in both tables" | ||
| 92 | # - primary column for joining is the call sign | ||
| 93 | # - joining for application data is the ULS file number (we don't load apps) | ||
| 94 | # - Each application and license has been assigned a unique 9-digit system id | ||
| 95 | # This is useful in cases where a call sign has been reassigned. | ||
| 96 | sub do_create_tables { | ||
| 97 | my ($self) = @_; | ||
| 98 | |||
| 99 | my $dbh = $self->{'dbh'}; | ||
| 100 | |||
| 101 | # These SQL statements were pulled from the FCC documentation at | ||
| 102 | # http://wireless.fcc.gov/uls/index.htm?job=transaction&page=weekly | ||
| 103 | |||
| 104 | # the FCC updates their schema on a regular basis and this module | ||
| 105 | # will need to track those changes. | ||
| 106 | |||
| 107 | $dbh->do("drop table PUBACC_EN"); | ||
| 108 | $dbh->do("create table PUBACC_EN | ||
| 109 | ( | ||
| 110 | record_type char(2) not null, | ||
| 111 | unique_system_identifier numeric(9,0) not null, | ||
| 112 | uls_file_number char(14) null, | ||
| 113 | ebf_number varchar(30) null, | ||
| 114 | call_sign char(10) null, | ||
| 115 | entity_type char(2) null, | ||
| 116 | licensee_id char(9) null, | ||
| 117 | entity_name varchar(200) null, | ||
| 118 | first_name varchar(20) null, | ||
| 119 | mi char(1) null, | ||
| 120 | last_name varchar(20) null, | ||
| 121 | suffix char(3) null, | ||
| 122 | phone char(10) null, | ||
| 123 | fax char(10) null, | ||
| 124 | email varchar(50) null, | ||
| 125 | street_address varchar(60) null, | ||
| 126 | city varchar(20) null, | ||
| 127 | state char(2) null, | ||
| 128 | zip_code char(9) null, | ||
| 129 | po_box varchar(20) null, | ||
| 130 | attention_line varchar(35) null, | ||
| 131 | sgin char(3) null, | ||
| 132 | frn char(10) null, | ||
| 133 | applicant_type_code char(1) null, | ||
| 134 | applicant_type_other char(40) null, | ||
| 135 | status_code char(1) null, | ||
| 136 | status_date datetime null | ||
| 137 | )"); | ||
| 138 | |||
| 139 | $dbh->do("CREATE index callsign_index_en_id on PUBACC_EN(unique_system_identifier)"); | ||
| 140 | |||
| 141 | |||
| 142 | $dbh->do("drop table PUBACC_AM"); | ||
| 143 | $dbh->do("create table PUBACC_AM | ||
| 144 | ( | ||
| 145 | record_type char(2) not null, | ||
| 146 | unique_system_identifier numeric(9,0) not null, | ||
| 147 | uls_file_num char(14) null, | ||
| 148 | ebf_number varchar(30) null, | ||
| 149 | callsign char(10) null, | ||
| 150 | operator_class char(1) null, | ||
| 151 | group_code char(1) null, | ||
| 152 | region_code tinyint null, | ||
| 153 | trustee_callsign char(10) null, | ||
| 154 | trustee_indicator char(1) null, | ||
| 155 | physician_certification char(1) null, | ||
| 156 | ve_signature char(1) null, | ||
| 157 | systematic_callsign_change char(1) null, | ||
| 158 | vanity_callsign_change char(1) null, | ||
| 159 | vanity_relationship char(12) null, | ||
| 160 | previous_callsign char(10) null, | ||
| 161 | previous_operator_class char(1) null, | ||
| 162 | trustee_name varchar(50) null | ||
| 163 | ) | ||
| 164 | "); | ||
| 165 | |||
| 166 | $dbh->do("CREATE index callsign_index_am_id on PUBACC_AM(unique_system_identifier)"); | ||
| 167 | |||
| 168 | |||
| 169 | $dbh->do("drop table PUBACC_HD"); | ||
| 170 | $dbh->do("create table PUBACC_HD | ||
| 171 | ( | ||
| 172 | record_type char(2) not null, | ||
| 173 | unique_system_identifier numeric(9,0) not null, | ||
| 174 | uls_file_number char(14) null, | ||
| 175 | ebf_number varchar(30) null, | ||
| 176 | call_sign char(10) null, | ||
| 177 | license_status char(1) null, | ||
| 178 | radio_service_code char(2) null, | ||
| 179 | grant_date char(10) null, | ||
| 180 | expired_date char(10) null, | ||
| 181 | cancellation_date char(10) null, | ||
| 182 | eligibility_rule_num char(10) null, | ||
| 183 | applicant_type_code_reserved char(1) null, | ||
| 184 | alien char(1) null, | ||
| 185 | alien_government char(1) null, | ||
| 186 | alien_corporation char(1) null, | ||
| 187 | alien_officer char(1) null, | ||
| 188 | alien_control char(1) null, | ||
| 189 | revoked char(1) null, | ||
| 190 | convicted char(1) null, | ||
| 191 | adjudged char(1) null, | ||
| 192 | involved_reserved char(1) null, | ||
| 193 | common_carrier char(1) null, | ||
| 194 | non_common_carrier char(1) null, | ||
| 195 | private_comm char(1) null, | ||
| 196 | fixed char(1) null, | ||
| 197 | mobile char(1) null, | ||
| 198 | radiolocation char(1) null, | ||
| 199 | satellite char(1) null, | ||
| 200 | developmental_or_sta char(1) null, | ||
| 201 | interconnected_service char(1) null, | ||
| 202 | certifier_first_name varchar(20) null, | ||
| 203 | certifier_mi char(1) null, | ||
| 204 | certifier_last_name varchar(20) null, | ||
| 205 | certifier_suffix char(3) null, | ||
| 206 | certifier_title char(40) null, | ||
| 207 | gender char(1) null, | ||
| 208 | african_american char(1) null, | ||
| 209 | native_american char(1) null, | ||
| 210 | hawaiian char(1) null, | ||
| 211 | asian char(1) null, | ||
| 212 | white char(1) null, | ||
| 213 | ethnicity char(1) null, | ||
| 214 | effective_date char(10) null, | ||
| 215 | last_action_date char(10) null, | ||
| 216 | auction_id int null, | ||
| 217 | reg_stat_broad_serv char(1) null, | ||
| 218 | band_manager char(1) null, | ||
| 219 | type_serv_broad_serv char(1) null, | ||
| 220 | alien_ruling char(1) null, | ||
| 221 | licensee_name_change char(1) null | ||
| 222 | )"); | ||
| 223 | |||
| 224 | $dbh->do("CREATE index callsign_index_hd_id on PUBACC_HD(unique_system_identifier)"); | ||
| 225 | $dbh->do("CREATE index callsign_index_HD_SIGN on PUBACC_HD(call_sign)"); | ||
| 226 | |||
| 227 | $dbh->do("drop table PUBACC_VC"); | ||
| 228 | $dbh->do("create table PUBACC_VC | ||
| 229 | ( | ||
| 230 | |||
| 231 | |||
| 232 | record_type char(2) null, | ||
| 233 | unique_system_identifier numeric(9,0) not null, | ||
| 234 | uls_file_number char(14) null, | ||
| 235 | ebf_number varchar(30) null, | ||
| 236 | request_sequence int null, | ||
| 237 | callsign_requested char(10) null | ||
| 238 | ) | ||
| 239 | "); | ||
| 240 | |||
| 241 | $dbh->do("CREATE index callsign_index_VC_SIGN on PUBACC_VC(callsign_requested)"); | ||
| 242 | $dbh->do("CREATE index callsign_index_vc_id on PUBACC_VC(unique_system_identifier)"); | ||
| 243 | } | ||
| 244 | |||
| 245 | ######################################## | ||
| 246 | # DB insertion code | ||
| 247 | # | ||
| 248 | |||
| 249 | # split doesn't always return the right number of entries... it | ||
| 250 | # chomps the end for some reason sometimes. This array fills in the blanks. | ||
| 251 | # | ||
| 252 | # XXX: this is likely a problem for some other reason and this is a | ||
| 253 | # possibly broken hack. | ||
| 254 | my @fill; | ||
| 255 | |||
| 256 | for (my $i = 1; $i <= 100; $i++) { | ||
| 257 | for (my $j = 0; $j < $i; $j++) { | ||
| 258 | push @{$fill[$i]}, ''; | ||
| 259 | } | ||
| 260 | } | ||
| 261 | |||
| 262 | # a generic function that inserts $num rows into a table | ||
| 263 | sub insert_stuff { | ||
| 264 | my ($self, $suffix, $num) = @_; | ||
| 265 | |||
| 266 | my $count = 0; | ||
| 267 | my @parts; | ||
| 268 | |||
| 269 | my $dbh = $self->{'dbh'}; | ||
| 270 | |||
| 271 | my $sth = $dbh->prepare("insert into PUBACC_$suffix values(" . | ||
| 272 | ("?, " x ($num-1)). " ?)"); | ||
| 273 | |||
| 274 | $| = 1; | ||
| 275 | print "starting $suffix\n"; | ||
| 276 | open(I,"<$self->{'import_place'}/$suffix.dat"); | ||
| 277 | $dbh->begin_work(); | ||
| 278 | while (<I>) { | ||
| 279 | chomp; | ||
| 280 | s/\r//; | ||
| 281 | @parts = split(/\|/); | ||
| 282 | # print "parts: $_\n " . join(",",@parts). "\n" if ($count == 0); | ||
| 283 | my $diff = $num - $#parts - 1; | ||
| 284 | $sth->execute(@parts, @{$fill[$diff]}); | ||
| 285 | # $sth->execute(@parts); | ||
| 286 | $count++; | ||
| 287 | if ($count % 10000 == 0) { | ||
| 288 | print "."; | ||
| 289 | $dbh->commit(); | ||
| 290 | $dbh->begin_work(); | ||
| 291 | } | ||
| 292 | } | ||
| 293 | $dbh->commit(); | ||
| 294 | |||
| 295 | print "inserted: $count rows into $suffix\n"; | ||
| 296 | } | ||
| 297 | |||
| 298 | |||
| 299 | 1; | ||
| 300 | |||
| 301 | =pod | ||
| 302 | |||
| 303 | =head1 NAME | ||
| 304 | |||
| 305 | =cut | ||
| 306 | |||
| 307 |