implement workaround for missing outer joins in sqlite
[ferlib2koha.git] / ferlib2koha.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4 use DBI;
5 use Data::Dump qw/dump/;
6
7 $|++;
8
9 my $f = DBI->connect("dbi:SQLite:dbname=knjiznica.sqlite","","", { RaiseError => 1, AutoCommit => 0 }) || die $DBI::errstr;
10 $f->{unicode} = 1;
11 my $k = DBI->connect("dbi:SQLite:dbname=knjiznica.sqlite","","", { RaiseError => 1, AutoCommit => 0 }) || die $DBI::errstr;
12
13 our $lookup;
14 sub lookup {
15         my ($t, $k, $v, $s ) = @_;
16         my $hash;
17         my $key = "$t $k $v";
18         if ( exists $lookup->{$key} ) {
19                 $hash = $lookup->{$key};
20         } else {
21                 warn "# select $k,$v from $t";
22                 my $sth = $f->prepare( "select $k,$v from $t" );
23                 $sth->execute;
24                 while (my $row = $sth->fetchrow_hashref() ) {
25                         $hash->{ $row->{$k} } = $row->{$v};
26                 }
27                 $lookup->{$key} = $hash;
28                 warn dump($lookup);
29         }
30         return unless length $s > 1;
31         return $lookup->{$key}->{$s} || die "no $s in $key ",dump($lookup->{$key});
32 }
33
34 # select all posts which have been read or unread
35 my $sql = qq{
36 select
37   'S' as categorycode,
38   'SRE' as branchcode,
39   mbr_stud as cardnumber,
40   prez_stud as surname,
41   ime_stud as firstname,
42   djevprezime as othernames,
43   spol as sex,
44   dat_rodj as dateofbirth,
45 --  ime_otac as ??,
46 --  ime_majka as ??,
47   ozn_drzava_preb as country,
48   post_ozn_preb as zipcode,
49   post_ozn_preb as city,
50   adr_preb as address,
51   ozn_drzava_stan as B_country,
52   post_ozn_stan as B_zipcode,
53   post_ozn_stan as B_city,
54   adr_stan as B_address,
55   tel_stud as phone,
56   dat_prava_do as dateexpiry
57 --  aktivan as ??,
58 from studk
59 limit 100
60 };
61
62 my $sth = $f->prepare($sql);
63 $sth->execute;
64
65 while (my $row = $sth->fetchrow_hashref ) {
66
67         # poor man's (sqlite) outer join
68         $row->{'country'} = lookup('drzava', 'ozn_drzava', 'naz_drzava', $row->{'country'} );
69         $row->{'city'} = lookup('mjesto', 'post_ozn', 'naz_mjesto', $row->{'city'} );
70
71         $row->{'B_country'} = lookup('drzava', 'ozn_drzava', 'naz_drzava', $row->{'B_country'} );
72         $row->{'B_city'} = lookup('mjesto', 'post_ozn', 'naz_mjesto', $row->{'B_city'} );
73
74         foreach my $c ( grep { /date/ } keys %$row ) {
75                 $row->{$c} =~ s/(\d\d)\.(\d\d)\.(\d\d\d\d)/$3-$2-$1/;
76         }
77
78         $row->{sex} =~ s/Ž/F/;
79
80         warn dump $row;
81
82 }
83