| 1 | # Copyright (C) 2008 Wes Hardaker | ||
| 2 | # License: Same as perl. See the LICENSE file for details. | ||
| 3 | package Ham::Callsign::DB; | ||
| 4 | |||
| 5 | use Ham::Callsign::Base; | ||
| 6 | our @ISA = qw(Ham::Callsign::Base); | ||
| 7 | |||
| 8 | our $VERSION = "0.31"; | ||
| 9 | |||
| 10 | use DBI; | ||
| 11 | use strict; | ||
| 12 | |||
| 13 | sub init { | ||
| 14 | my ($self) = @_; | ||
| 15 | |||
| 16 | my $dbtype = $self->{'dbtype'} || | ||
| 17 | 'SQLite:' . $ENV{'HOME'} . "/.callsigns.sqlite"; | ||
| 18 | my $dbargs = $self->{'dbargs'} || ''; | ||
| 19 | |||
| 20 | $self->{dbh} = DBI->connect("DBI:$dbtype$dbargs", | ||
| 21 | $self->{'user'} || '', $self->{'pass'} || ''); | ||
| 22 | } | ||
| 23 | |||
| 24 | sub initialize_dbs { | ||
| 25 | my ($self, $dbs) = @_; | ||
| 26 | if (!$dbs || ref($dbs) ne 'ARRAY') { | ||
| 27 | $dbs = [split(/,\s*/, $self->{'sets'})]; | ||
| 28 | } | ||
| 29 | if ($#$dbs == -1) { | ||
| 30 | $dbs = [qw(US DX)]; | ||
| 31 | } | ||
| 32 | foreach my $db (@$dbs) { | ||
| 33 | my $havedb = eval "require Ham::Callsign::DB::$db"; | ||
| 34 | if (!$havedb) { | ||
| 35 | Warn("failed to load Callsign DB support for type '$db'"); | ||
| 36 | } else { | ||
| 37 | $self->{'dbs'}{$db} = eval "new Ham::Callsign::DB::$db"; | ||
| 38 | if (!$self->{'dbs'}{$db}) { | ||
| 39 | Warn("failed to initialize a new '$db' database"); | ||
| 40 | Debug("$@"); | ||
| 41 | } else { | ||
| 42 | $self->{'dbs'}{$db}{'master'} = $self; | ||
| 43 | $self->{'dbs'}{$db}{'dbh'} = $self->{'dbh'}; | ||
| 44 | push @{$self->{'dblist'}}, $db; | ||
| 45 | Debug("loaded callsign DB support for $db\n"); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | ######################################## | ||
| 52 | # main functions | ||
| 53 | |||
| 54 | sub create_tables { | ||
| 55 | my ($self) = @_; | ||
| 56 | foreach my $db (@{$self->{'dblist'}}) { | ||
| 57 | $self->{'dbs'}{$db}->do_create_tables(); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | sub load_data { | ||
| 62 | my ($self, $place) = @_; | ||
| 63 | foreach my $db (@{$self->{'dblist'}}) { | ||
| 64 | $self->{'dbs'}{$db}->do_load_data($place); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | sub lookup { | ||
| 69 | my ($self, $callsign) = @_; | ||
| 70 | my $ret; | ||
| 71 | foreach my $db (@{$self->{'dblist'}}) { | ||
| 72 | my $results = $self->{'dbs'}{$db}->do_lookup($callsign); | ||
| 73 | push @$ret, @$results if ($results); | ||
| 74 | } | ||
| 75 | return $ret; | ||
| 76 | } | ||
| 77 | |||
| 78 | ######################################## | ||
| 79 | # stubs | ||
| 80 | |||
| 81 | # XXX: use caller() and/or AUTOLOAD to do this more generically | ||
| 82 | |||
| 83 | sub do_create_tables { | ||
| 84 | my ($self) = @_; | ||
| 85 | Warn(ref($self) . " does not implement create_tables"); | ||
| 86 | } | ||
| 87 | |||
| 88 | sub do_load_data { | ||
| 89 | my ($self, $place) = @_; | ||
| 90 | Warn(ref($self) . " does not implement load_data"); | ||
| 91 | } | ||
| 92 | |||
| 93 | sub do_lookup { | ||
| 94 | my ($self, $callsign) = @_; | ||
| 95 | Warn(ref($self) . " does not implement lookup"); | ||
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | |||
| 100 | 1; | ||
| 101 | |||
| 102 | =pod | ||
| 103 | |||
| 104 | =head1 NAME | ||
| 105 | |||
| 106 | Ham::Callsign::DB | ||
| 107 | |||
| 108 | =head1 SYNOPSIS | ||
| 109 | |||
| 110 | use Ham::Callsign::DB; | ||
| 111 | my $db = new Ham::Callsign::DB(); | ||
| 112 | |||
| 113 | # bootstrap everything and load the US database backend | ||
| 114 | $db->initialize_dbs(["US"]); | ||
| 115 | |||
| 116 | # load data from a given file set | ||
| 117 | # (not all backends need this) | ||
| 118 | $db->load_data("/path/to/fccdownloaddir"); | ||
| 119 | |||
| 120 | # search the database and get an array reference of callsign data | ||
| 121 | $results = $db->lookup("WS6Z"); | ||
| 122 | |||
| 123 | # create a formatter to display the results | ||
| 124 | # current available types: "Format" and "Dump" | ||
| 125 | my $formatter = new Ham::Callsign::Display::Format; | ||
| 126 | |||
| 127 | $formatter->display($results); | ||
| 128 | |||
| 129 | $formatter->display($results, "%{8.8:thecallsign} %{first_name}"); | ||
| 130 | |||
| 131 | =head1 DESCRIPTION | ||
| 132 | |||
| 133 | More details can be found in the cs(1) manual. | ||
| 134 | |||
| 135 | =head1 LICENSE | ||
| 136 | |||
| 137 | This library is free software; you can redistribute it and/or modify it | ||
| 138 | under the same terms as Perl itself. | ||
| 139 | |||
| 140 | =head1 SEE ALSO | ||
| 141 | |||
| 142 | cs(1) | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 |