File Coverage

blib/lib/Geo/Postcodes/JP/DB.pm
Criterion Covered Total %
statement 6 6 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 8 8 100.0


line stmt bran cond sub pod time code
1             my $schema_file = __FILE__;
2             $schema_file =~ s!\.pm!/schema.sql!;
3              
4             =encoding UTF-8
5              
6             =head1 NAME
7              
8             Geo::Postcodes::JP::DB - database of Japanese postal codes
9              
10             =head1 SYNOPSIS
11              
12             my $o = Geo::Postcodes::JP::DB->new (
13             db_file => '/path/to/sqlite/database',
14             );
15             my $address = $o->lookup_postcode ('3050054');
16             print $address->{ken};
17             # Prints 茨城県
18              
19             =head1 DESCRIPTION
20              
21             This module offers methods to create and access an SQLite database of
22             Japanese postcodes.
23              
24             =head1 METHODS
25              
26             All of these methods make use of an SQLite database file. All of the
27             fields suffixed with C<_id> are identification numbers of the SQLite
28             database itself.
29              
30             =cut
31              
32             package Geo::Postcodes::JP::DB;
33             require Exporter;
34             @ISA = qw(Exporter);
35             @EXPORT_OK = qw/
36             make_database
37             create_database
38             /;
39              
40 4     4   85811 use warnings;
  4         9  
  4         142  
41 4     4   21 use strict;
  4         7  
  4         675  
42             our $VERSION = '0.014';
43              
44             #line 42 "DB.pm.tmpl"
45              
46              
47             # Need the postcode-reading function.
48              
49             use Geo::Postcodes::JP::Process
50             qw/
51             read_ken_all
52             process_line
53             read_jigyosyo
54             process_jigyosyo_line
55             improve_postcodes
56             /;
57              
58             # Require DBI for communicating with the database.
59              
60             use DBI;
61              
62             # This is for converting the halfwidth (半角) katakana in the post
63             # office file.
64              
65             use Lingua::JA::Moji ':all';
66              
67             use utf8;
68             use Carp;
69              
70             sub insert_schema
71             {
72             my ($o, $schema_file_subs) = @_;
73             if ($schema_file_subs) {
74             $schema_file = $schema_file_subs;
75             }
76             open my $input, "<", $schema_file
77             or die "Can't open schema file '$schema_file': $!";
78             my $schema = '';
79             while (<$input>) {
80             $schema .= $_;
81             }
82             my @schema = split /;/, $schema;
83             for my $statement (@schema) {
84             $o->{dbh}->do ($statement);
85             }
86             }
87              
88             # Make the database from the specified schema file.
89              
90             sub make_database_from_schema
91             {
92             my ($db_file, $schema_file) = @_;
93             if (-f $db_file) {
94             unlink $db_file
95             or die "Error unlinking '$db_file': $!";
96             }
97             my $o = __PACKAGE__->new (
98             db_file => $db_file,
99             );
100             $o->insert_schema ($schema_file);
101             return $o;
102             }
103              
104             my $verbose;
105              
106             =head2 search_placename
107              
108             $placename_id = $o->search_placename ($type, $kanji, $kana);
109              
110             Generic search for a placename of type C<$type> by kanji name
111             C<$kanji> and kana name C<$kana>. This is only used for the "ken" and
112             the "jigyosyo" tables, because city and address names are ambiguous.
113              
114             =cut
115              
116             sub search_placename
117             {
118             my ($o, $type, $kanji, $kana) = @_;
119             if (! $o->{placename_search}{$type}) {
120             my $placename_search_sql = <
121             select id from %s where kanji=? and kana=?
122             EOF
123             my $sql = sprintf ($placename_search_sql, $type);
124             $o->{placename_search}{$type} = $o->{dbh}->prepare ($sql);
125             }
126             if ($verbose) {
127             print "Searching for $kanji, $kana\n";
128             }
129             $o->{placename_search}{$type}->execute ($kanji, $kana);
130             my $placenames = $o->{placename_search}{$type}->fetchall_arrayref ();
131             my $placename_id;
132             if ($placenames) {
133             if (@$placenames > 1) {
134             croak "Search for '$kanji' and '$kana' was ambiguous";
135             }
136             if (@$placenames == 1) {
137             $placename_id = $placenames->[0]->[0];
138             }
139             else {
140             if ($verbose) {
141             print "Not found.\n";
142             }
143             }
144             }
145             else {
146             die "Search failed to return a result";
147             }
148             return $placename_id;
149             }
150              
151             =head2 city_search
152              
153             $city_id = $o->city_search ($kanji, $ken_id);
154              
155             Search for a city named C<$kanji> in prefecture identified by
156             C<$ken_id>. There are some examples of cities with the same names in
157             different prefectures. For example there is a 府中市 (Fuchuu-shi) in
158             Tokyo and one in Hiroshima prefecture. Thus a "city_search" routine
159             rather than the "search_placename" generic search is needed for
160             cities.
161              
162             =cut
163              
164             sub city_search
165             {
166             my ($o, $kanji, $ken_id) = @_;
167             if (! $o->{city_search}) {
168             my $city_search_sql = <
169             select id from city where kanji=? and ken_id=?
170             EOF
171             $o->{city_search} = $o->{dbh}->prepare ($city_search_sql);
172             }
173             $o->{city_search}->execute ($kanji, $ken_id);
174             my $cities = $o->{city_search}->fetchall_arrayref ();
175             my $city_id;
176             if ($cities) {
177             if (@$cities > 1) {
178             croak "Search for '$kanji' in ken $ken_id was ambiguous";
179             }
180             if (@$cities == 1) {
181             $city_id = $cities->[0]->[0];
182             }
183             else {
184             if ($verbose) {
185             print "Not found.\n";
186             }
187             }
188             }
189             else {
190             die "Search failed to return a result";
191             }
192             return $city_id;
193             }
194              
195             =head2 address_search
196              
197             $address_id = $o->address_search ($kanji, $kana, $city_id);
198              
199             Search for an "address" in a particular city, specified by C<$city_id>.
200              
201             =cut
202              
203             sub address_search
204             {
205             my ($o, $kanji, $kana, $city_id) = @_;
206             if (! $o->{address_search}) {
207             my $address_search_sql = <
208             select id from address where kanji=? and kana=? and city_id=?
209             EOF
210             $o->{address_search} = $o->{dbh}->prepare ($address_search_sql);
211             }
212             $o->{address_search}->execute ($kanji, $kana, $city_id);
213             my $addresses = $o->{address_search}->fetchall_arrayref ();
214             my $address_id;
215             if ($addresses) {
216             if (@$addresses > 1) {
217             croak "Search for '$kanji' and '$kana' in city $city_id was ambiguous";
218             }
219             if (@$addresses == 1) {
220             $address_id = $addresses->[0]->[0];
221             }
222             else {
223             if ($verbose) {
224             print "Not found.\n";
225             }
226             }
227             }
228             else {
229             die "Search failed to return a result";
230             }
231             return $address_id;
232             }
233              
234             =head2 search_placename_kanji
235              
236             my $place_id = $o->search_placename_kanji ($type, $kanji);
237              
238             Like L, but search for a place name using only the
239             kanji for the name.
240              
241             =cut
242              
243             my $placename_search_kanji_sql = <
244             select id from %s where kanji=?
245             EOF
246              
247             sub search_placename_kanji
248             {
249             my ($o, $type, $kanji) = @_;
250             if (! $o->{placename_search_kanji}{$type}) {
251             my $sql = sprintf ($placename_search_kanji_sql, $type);
252             $o->{placename_search_kanji}{$type} = $o->{dbh}->prepare ($sql);
253             }
254             $o->{placename_search_kanji}{$type}->execute ($kanji);
255             my $placenames = $o->{placename_search_kanji}{$type}->fetchall_arrayref ();
256             my $placename_id;
257             if ($placenames) {
258             if (@$placenames > 1) {
259             croak "Search for '$kanji' was ambiguous";
260             }
261             if (@$placenames == 1) {
262             $placename_id = $placenames->[0]->[0];
263             }
264             else {
265             if ($verbose) {
266             print "Not found.\n";
267             }
268             }
269             }
270             else {
271             die "Search failed to return a result";
272             }
273             return $placename_id;
274             }
275              
276             =head2 insert_postcode
277              
278             $o->insert_postcode ($postcode, $address_id);
279              
280             Insert a postcode C<$postcode> into the table of postcodes with
281             corresponding address C<$address_id>. The address identification
282             number, C<$address_id>, is usually got from L or
283             L. This method is for addresses which are not
284             jigyosyo (places of business). Addresses for places of business should
285             use L.
286              
287             =cut
288              
289              
290             # Insert a postcode with an address.
291              
292             sub insert_postcode
293             {
294             my ($o, $postcode, $address_id) = @_;
295             if (! $postcode) {
296             die "No postcode";
297             }
298             if (! $o->{postcode_insert_sth}) {
299             # SQL to insert postcodes into the table.
300             my $postcode_insert_sql = <
301             insert into postcodes (postcode, address_id)
302             values (?, ?)
303             EOF
304             $o->{postcode_insert_sth} = $o->{dbh}->prepare ($postcode_insert_sql);
305             }
306             $o->{postcode_insert_sth}->execute ($postcode, $address_id);
307             }
308              
309             =head2 jigyosyo_insert_postcode
310              
311             $o->jigyosyo_insert_postcode ($postcode, $address_id, $jigyosyo_id);
312              
313             Insert a postcode for a "jigyosyo" identified by C<$jigyosyo_id> into
314             the table. $Jigyosyo_id is usually got from
315             L. C<$Address_id> is as described in the
316             documentation of L.
317              
318             =cut
319              
320             sub jigyosyo_insert_postcode
321             {
322             my ($o, $postcode, $address_id, $jigyosyo_id) = @_;
323             if (! $postcode) {
324             die "No postcode";
325             }
326             if (! $o->{jigyosyo_postcode_insert_sth}) {
327             # SQL to insert postcodes with jigyosyo into the table.
328              
329             my $jigyosyo_postcode_insert_sql = <
330             insert into postcodes (postcode, address_id, jigyosyo_id)
331             values (?, ?, ?)
332             EOF
333             $o->{jigyosyo_postcode_insert_sth} = $o->{dbh}->prepare ($jigyosyo_postcode_insert_sql);
334             }
335             $o->{jigyosyo_postcode_insert_sth}->execute ($postcode,
336             $address_id, $jigyosyo_id);
337             }
338              
339             =head2 jigyosyo_insert
340              
341             my $jigyosyo_id = $o->jigyosyo_insert ($kanji, $kana, $street_number);
342              
343             Insert a "jigyosyo" into the table of them with kanji C<$kanji>, kana
344             C<$kana>, street number C<$street_number>, and return the ID number of
345             the entry.
346              
347             =cut
348              
349             sub jigyosyo_insert
350             {
351             my ($o, $kanji, $kana, $street_number) = @_;
352             if ($verbose) {
353             print "Inserting jigyosyo $kanji/$kana/$street_number.\n";
354             }
355             if (! $o->{jigyosyo_insert_sth}) {
356             # Format for the SQL to insert kanji, kana into the place name
357             # table.
358             my $jigyosyo_insert_sql = <<'EOF';
359             insert into jigyosyo (kanji, kana, street_number) values (?, ?, ?)
360             EOF
361             $o->{jigyosyo_insert_sth} = $o->{dbh}->prepare ($jigyosyo_insert_sql);
362             }
363             $o->{jigyosyo_insert_sth}->execute ($kanji, $kana, $street_number);
364             my $id = $o->{dbh}->last_insert_id (0, 0, 0, 0);
365             return $id;
366             }
367              
368              
369              
370             =head2 ken_insert
371              
372             my $ken_id = $o->ken_insert ($kanji, $kana);
373              
374             Insert a prefecture into the table of prefectures with the name
375             C<$kanji> in kanji and C<$kana> in kana.
376              
377             =cut
378              
379             sub ken_insert
380             {
381             my ($o, $kanji, $kana) = @_;
382             if ($verbose) {
383             print "Inserting ken $kanji/$kana\n";
384             }
385             if (! $o->{ken_insert_sth}) {
386             # Format for the SQL to insert kanji, kana into the place name table.
387             my $ken_insert_sql = <<'EOF';
388             insert into ken (kanji, kana) values (?, ?)
389             EOF
390             $o->{ken_insert_sth} = $o->{dbh}->prepare ($ken_insert_sql);
391             }
392             $o->{ken_insert_sth}->execute ($kanji, $kana);
393             my $id = $o->{dbh}->last_insert_id (0, 0, 0, 0);
394             return $id;
395             }
396              
397             # City
398              
399             =head2 city_insert
400              
401             my $city_id = $o->city_insert ($kanji, $kana, $ken_id);
402              
403             Insert a city into the table of cities with kanji name C<$kanji>, kana
404             name C<$kana>, which is in the prefecture specified by C<$ken_id>.
405              
406             C<$Ken_id> specifies the prefecture to which the city belongs.
407              
408             =cut
409              
410             sub city_insert
411             {
412             my ($o, $kanji, $kana, $ken_id) = @_;
413             if (! $o->{city_insert_sth}) {
414             my $city_insert_sql = <<'EOF';
415             insert into city (kanji, kana, ken_id) values (?, ?, ?)
416             EOF
417             $o->{city_insert_sth} = $o->{dbh}->prepare ($city_insert_sql);
418             }
419             $o->{city_insert_sth}->execute ($kanji, $kana, $ken_id);
420             my $id = $o->{dbh}->last_insert_id (0, 0, 0, 0);
421             return $id;
422             }
423              
424             # Address
425              
426             =head2 address_insert
427              
428             my $address_id = $o->address_insert ($kanji, $kana, $city_id);
429              
430             Insert an address into the table of addresses with kanji name
431             C<$kanji>, kana name C<$kana>, and city ID C<$city_id>. This is an
432             internal routine used in the construction of the database from the
433             data file.
434              
435             =cut
436              
437             sub address_insert
438             {
439             my ($o, $kanji, $kana, $city_id) = @_;
440             if (! $o->{address_insert_sth}) {
441             my $address_insert_sql = <<'EOF';
442             insert into address (kanji, kana, city_id) values (?, ?, ?)
443             EOF
444             $o->{address_insert_sth} = $o->{dbh}->prepare ($address_insert_sql);
445             }
446             $o->{address_insert_sth}->execute ($kanji, $kana, $city_id);
447             my $id = $o->{dbh}->last_insert_id (0, 0, 0, 0);
448             return $id;
449             }
450              
451             =head2 db_connect
452              
453             $o->db_connect ('/path/to/database/file');
454              
455             Connect to the database specified.
456              
457             =cut
458              
459             sub db_connect
460             {
461             my ($o, $db_file) = @_;
462             $o->{dbh} = DBI->connect ("dbi:SQLite:dbname=$db_file", "", "",
463             {
464             RaiseError => 1,
465             # Set this to '1' to avoid mojibake.
466             sqlite_unicode => 1,
467             }
468             );
469             $o->{db_file} = $db_file;
470             }
471              
472             =head2 insert_postcodes
473              
474             $o->insert_postcodes ($postcodes);
475              
476             Insert the postcodes in the array reference C<$postcodes> into the
477             database specified by L.
478              
479             =cut
480              
481             sub insert_postcodes
482             {
483             my ($o, $postcodes) = @_;
484              
485             $o->{dbh}->{AutoCommit} = 0;
486             for my $postcode (@$postcodes) {
487             my %ids;
488             my %values = process_line ($postcode);
489             my $ken_kana = hw2katakana ($values{ken_kana});
490             my $ken_kanji = $values{ken_kanji};
491             my $ken_id = $o->search_placename ('ken', $ken_kanji, $ken_kana);
492             if (! defined $ken_id) {
493             $ken_id = $o->ken_insert ($ken_kanji, $ken_kana);
494             }
495             my $city_kana = hw2katakana ($values{city_kana});
496             my $city_kanji = $values{city_kanji};
497             my $city_id = $o->city_search ($city_kanji, $ken_id);
498             if (! defined $city_id) {
499             $city_id = $o->city_insert (
500             $city_kanji,
501             $city_kana,
502             $ken_id
503             );
504             }
505             my $address_kana = hw2katakana ($values{address_kana});
506             my $address_kanji = $values{address_kanji};
507             my $address_id = $o->address_search (
508             $address_kanji,
509             $address_kana,
510             $city_id,
511             );
512             if (! defined $address_id) {
513             $address_id = $o->address_insert (
514             $address_kanji,
515             $address_kana,
516             $city_id,
517             );
518             }
519             my $pc = $values{new_postcode};
520             if (! defined $pc) {
521             die "No postcode defined";
522             }
523             $o->insert_postcode ($pc, $address_id);
524             }
525             $o->{dbh}->commit ();
526             $o->{dbh}->{AutoCommit} = 1;
527             }
528              
529             =head2 insert_postcode_file
530              
531             insert_postcode_file (
532             db_file => '/path/to/database/file',
533             postcode_file => '/path/to/postcode/file',
534             );
535              
536             Insert the postcodes in the file specified by C into
537             the database specified by C.
538              
539             This uses the L method of L to
540             read the data, and the L function of the same module to improve
541             the data.
542              
543             =cut
544              
545             sub insert_postcode_file
546             {
547             my ($o, %inputs) = @_;
548             my $verbose = $inputs{verbose};
549             my $postcode_file = $inputs{postcode_file};
550             if (! $postcode_file) {
551             croak "Specify the file containing the postcodes with postcode_file => 'file name'";
552             }
553             if ($verbose) {
554             print "Reading postcodes from '$postcode_file'.\n";
555             }
556             my $postcodes = read_ken_all ($postcode_file);
557             $postcodes = improve_postcodes ($postcodes);
558             $o->insert_postcodes ($postcodes);
559             }
560              
561             =head2 lookup_address
562              
563             my $address_id = $o->lookup_address (
564             ken => '沖縄県',
565             city => '宜野湾市',
566             address => '上原',
567             );
568              
569             Look up an address id number from the kanji versions of the prefecture
570             name, the city name, and the address name.
571              
572             =cut
573              
574             sub lookup_address
575             {
576             my ($o, %inputs) = @_;
577             if (! $o->{lookup_sth}) {
578             my $sql = <
579             select address.id from ken, city, address where
580             ken.kanji = ? and
581             city.kanji = ? and
582             address.kanji = ? and
583             ken.id = city.ken_id and
584             city.id = address.city_id
585             EOF
586             $o->{lookup_sth} = $o->{dbh}->prepare ($sql);
587             }
588             $o->{lookup_sth}->execute ($inputs{ken}, $inputs{city}, $inputs{address});
589             my $return = $o->{lookup_sth}->fetchall_arrayref ();
590             if (scalar @$return > 1) {
591             die "Too many results for $inputs{ken}, $inputs{city}, $inputs{address}";
592             }
593             if ($return->[0]) {
594             return $return->[0]->[0];
595             }
596             else {
597             return ();
598             }
599             }
600              
601             =head2 add_jigyosyo
602              
603             $o->add_jigyosyo (
604             db_file => '/path/to/database/file',
605             jigyosyo_file => '/path/to/jigyosyo.csv',
606             );
607              
608             Add the list of place-of-business postcodes from C to
609             the database specified by C.
610              
611             =cut
612              
613             sub add_jigyosyo
614             {
615             my ($o, %inputs) = @_;
616             my %total;
617             $total{found} = 0;
618             $o->{dbh}->{AutoCommit} = 0;
619             my $jigyosyo_file = $inputs{jigyosyo_file};
620             my $jigyosyo_postcodes = read_jigyosyo ($jigyosyo_file);
621             for my $postcode (@$jigyosyo_postcodes) {
622             my %values = process_jigyosyo_line ($postcode);
623             my $ken = $values{ken_kanji};
624             my $city = $values{city_kanji};
625             my $address = $values{address_kanji};
626             # Remove the "aza" or "ooaza" from the beginning of the name.
627             if ($address =~ /(^|大)字/) {
628             $address =~ s/(^|大)字//;
629             }
630             my $address_id = $o->lookup_address (
631             ken => $ken,
632             city => $city,
633             address => $address,
634             );
635             my $ken_id;
636             my $city_id;
637              
638             if (defined $address_id) {
639             # print "Found.\n";
640             $total{found}++;
641             }
642             else {
643             # print "$ken, $city, $address, $values{kanji} Not found.\n";
644             $ken_id = search_placename_kanji ($o, 'ken', $ken);
645             $city_id = city_search ($o, $city, $ken_id);
646             $address_id = address_insert ($o, $address, '?', $city_id);
647             $total{notfound}++;
648             }
649             my $jigyosyo_id = jigyosyo_insert ($o, $values{kanji}, $values{kana},
650             $values{street_number});
651             # next;
652             if ($address_id == 1) {
653             die "BAd aadredd ss id \n";
654             }
655             jigyosyo_insert_postcode ($o, $values{new_postcode},
656             $address_id, $jigyosyo_id);
657             }
658             $o->{dbh}->commit ();
659             $o->{dbh}->{AutoCommit} = 1;
660             # print "Found $total{found}: not found $total{notfound}.\n";
661             }
662              
663             =head2 lookup_jigyosyo
664              
665             my $jigyosyo = lookup_jigyosyo ($jigyosyo_id);
666              
667             Given a jigyosyo id number, return its kanji and kana names and its
668             street number in a hash reference.
669              
670             =cut
671              
672             sub jigyosyo_lookup
673             {
674             my ($o, $jigyosyo_id) = @_;
675             my %jigyosyo;
676             if (! defined $o->{jigyosyo_lookup_sth}) {
677             my $jigyosyo_lookup_sql = <
678             select kanji, kana, street_number from jigyosyo
679             where
680             id = ?
681             EOF
682             $o->{jigyosyo_lookup_sth} = $o->{dbh}->prepare ($jigyosyo_lookup_sql);
683             }
684             $o->{jigyosyo_lookup_sth}->execute ($jigyosyo_id);
685             my $r = $o->{jigyosyo_lookup_sth}->fetchall_arrayref ();
686             if (! $r) {
687             return;
688             }
689             if (@$r > 1) {
690             die "Non-unique jigyosyo id number $jigyosyo_id";
691             }
692             @jigyosyo{qw/kanji kana street_number/} = @{$r->[0]};
693             return \%jigyosyo;
694             }
695              
696              
697             =head2 lookup_postcode
698              
699             my $addresses = $o->lookup_postcode ('3108610');
700             print $address->[0]->{ken}->{kanji}, "\n";
701             # Prints 茨城県
702              
703             Given a postcode, get the corresponding address details. If the
704             postcode is found, the return value is an array reference containing
705             one or more hash references with the following keys. If the postcode
706             is not found, the return value is the undefined value.
707              
708             =over
709              
710             =item postcode
711              
712             The seven-digit postcode itself, for example 0708033.
713              
714              
715             =item ken_kanji
716              
717             The kanji form of the prefecture name, for example 北海道.
718              
719              
720             =item ken_kana
721              
722             The kana form of the prefecture name, for example ホッカイドウ.
723              
724              
725             =item city_kanji
726              
727             The kanji form of the city name, for example 旭川市. In some instances
728             this data will consist of "gun" and "machi" or "shi" and "ku"
729             information rather than just a city name, depending on the information
730             in the Japan Post Office file itself.
731              
732              
733             =item city_kana
734              
735             The kana form of the city name, for example アサヒカワシ.
736              
737              
738             =item address_kanji
739              
740             The final part of the address in kanji, for example 神居町雨紛.
741              
742              
743             =item address_kana
744              
745             The final part of the address in kana, for example カムイチョウウブン.
746              
747              
748             =back
749              
750             If the postcode is a jigyosyo postcode, the result also contains
751              
752             =over
753              
754             =item jigyosyo_kanji
755              
756             The kanji name of the place of business.
757              
758             =item jigyosyo_kana
759              
760             The kana name of the place of business. This, unfortunately, is with
761             small versions of kana all converted into large ones, because this is
762             the format supplied by the post office.
763              
764             =item street_number
765              
766             This is the specific address of the place of business.
767              
768             =back
769              
770              
771             =cut
772              
773             my @fields = qw/
774             postcode
775             ken_kanji
776             ken_kana
777             city_kanji
778             city_kana
779             address_kanji
780             address_kana jigyosyo_id
781             /;
782             #line 713 "DB.pm.tmpl"
783              
784             sub make_lookup_postcode_sql
785             {
786              
787             my $sql_fields = join ",", @fields;
788             $sql_fields =~ s/_(kanji|kana)/\.$1/g;
789              
790             my $lookup_postcode_sql = <
791             select $sql_fields
792             from postcodes, ken, city, address
793             where postcodes.postcode = ?
794             and
795             city.ken_id = ken.id
796             and
797             address.city_id = city.id
798             and
799             postcodes.address_id = address.id
800             EOF
801             return $lookup_postcode_sql;
802             }
803              
804             sub lookup_postcode
805             {
806             my ($o, $postcode) = @_;
807             if (! $o->{lookup_postcode_sth}) {
808             my $lookup_postcode_sql = make_lookup_postcode_sql ();
809             $o->{lookup_postcode_sth} = $o->{dbh}->prepare ($lookup_postcode_sql);
810             }
811             $o->{lookup_postcode_sth}->execute ($postcode);
812             my $results = $o->{lookup_postcode_sth}->fetchall_arrayref ();
813             if (! $results || @$results == 0) {
814             return;
815             }
816             my @addresses;
817             for my $result (@$results) {
818             my %values;
819             @values{@fields} = @{$result};
820             if (defined $values{jigyosyo_id}) {
821             my $jigyosyo_values = $o->jigyosyo_lookup ($values{jigyosyo_id});
822             if ($jigyosyo_values) {
823             $values{jigyosyo_kanji} = $jigyosyo_values->{kanji};
824             $values{jigyosyo_kana} = $jigyosyo_values->{kana};
825             $values{street_number} = $jigyosyo_values->{street_number};
826             }
827             }
828             # Don't leave this in the result, since it is just a database
829             # ID number with no meaning to the user.
830             delete $values{jigyosyo_id};
831             push @addresses, \%values;
832             }
833             return \@addresses;
834             }
835              
836             =head2 new
837              
838             my $o = Geo::Postcodes::JP::DB->new (
839             db_file => '/path/to/the/sqlite/database/file',
840             );
841              
842             Create a new database-handling object. See also L to
843             create a database file without data, and L to create
844             the database file and insert its data.
845              
846             =cut
847              
848             sub new
849             {
850             my ($package, %inputs) = @_;
851             my $o = bless {};
852             my $db_file = $inputs{db_file};
853             if ($db_file) {
854             $o->db_connect ($db_file);
855             }
856             return $o;
857             }
858              
859             =head1 FUNCTIONS
860              
861             =head2 create_database
862              
863             my $o = create_database (
864             db_file => '/path/to/file',
865             );
866              
867             Create the SQLite database specified by C.
868              
869             The return value is a database handling object as returned by L.
870              
871             =cut
872              
873             sub create_database
874             {
875             my (%inputs) = @_;
876             my $db_file = $inputs{db_file};
877             if ($inputs{schema_file}) {
878             $schema_file = $inputs{schema_file};
879             }
880             my $verbose = $inputs{verbose};
881             if (! $db_file) {
882             croak "Specify the database file";
883             }
884             if (! $schema_file) {
885             croak "Specify the schema file with schema_file => 'file name'";
886             }
887             if (-f $db_file) {
888             croak "Database file '$db_file' already exists: not recreating.";
889             }
890             if ($verbose) {
891             print "Making database from schema.\n";
892             }
893             return make_database_from_schema ($db_file, $schema_file);
894             }
895              
896             =head2 make_database
897              
898             my $o = make_database (
899             db_file => '/path/to/database/file',
900             postcode_file => '/path/to/postcode/file',
901             );
902              
903             Make the database specified by C from the data in
904             C. The schema is supplied in the
905             F subdirectory of the distribution in the
906             file F. This uses L to create the
907             database and L to insert the data into the
908             database.
909              
910             The return value is the database handling object, as returned by L.
911              
912             =cut
913              
914             sub make_database
915             {
916             my (%inputs) = @_;
917             my $o = create_database (%inputs);
918             $o->insert_postcode_file (%inputs);
919             return $o;
920             }
921              
922             1;
923              
924             __END__