$|++;
my $f = DBI->connect("dbi:SQLite:dbname=knjiznica.sqlite","","", { RaiseError => 1, AutoCommit => 0 }) || die $DBI::errstr;
-$f->{unicode} = 1;
-my $k = DBI->connect("dbi:SQLite:dbname=knjiznica.sqlite","","", { RaiseError => 1, AutoCommit => 0 }) || die $DBI::errstr;
+$f->{sqlite_unicode} = 1;
+my $k = DBI->connect("dbi:mysql:database=koha_fer","","", { RaiseError => 1, AutoCommit => 0, mysql_enable_utf8 => 1 }) || die $DBI::errstr;
our $lookup;
sub lookup {
- my ($t, $k, $v, $s ) = @_;
+ my ($t, $k, $v, $s, $default ) = @_;
my $hash;
my $key = "$t $k $v";
if ( exists $lookup->{$key} ) {
warn dump($lookup);
}
return unless length $s > 1;
- return $lookup->{$key}->{$s} || die "no $s in $key ",dump($lookup->{$key});
+ if ( ! exists $lookup->{$key}->{$s} ) {
+ warn "ERROR: no $s in $key\n";
+ return $default;
+ } else {
+ $lookup->{$key}->{$s};
+ }
+}
+
+our $insert;
+sub insert {
+ my ( $table, $row ) = @_;
+ $insert->{$table} ||= $k->prepare(
+ "insert into $table ("
+ . join(',', keys %$row)
+ . ") values ("
+ . join(',', map { '?' } keys %$row )
+ . ")"
+ );
+ $insert->{$table}->execute( values %$row );
+ warn "# inserted ", dump $row;
+
+}
+
+sub borrowers {
+ my $sql = shift;
+ my $sth = $f->prepare($sql);
+ $sth->execute;
+
+ $insert = undef;
+
+ while (my $row = $sth->fetchrow_hashref ) {
+
+ warn dump $row;
+
+ # poor man's (sqlite) outer join
+ $row->{'country'} = lookup('drzava', 'ozn_drzava', 'naz_drzava', $row->{'country'}, $row->{contry} );
+ $row->{'city'} = lookup('mjesto', 'post_ozn', 'naz_mjesto', $row->{'city'}, $row->{city} );
+
+ $row->{'B_country'} = lookup('drzava', 'ozn_drzava', 'naz_drzava', $row->{'B_country'}, $row->{B_country} );
+ $row->{'B_city'} = lookup('mjesto', 'post_ozn', 'naz_mjesto', $row->{'B_city'}, $row->{B_city} );
+
+ $row->{'email'} = lookup('clanost', 'ozn_vrclan||ozn_clan', 'email', $row->{'cardnumber'}, undef);
+ $row->{'emailpro'} = lookup('clanost', 'ozn_vrclan||ozn_clan', 'email_k', $row->{'cardnumber'}, undef);
+
+ foreach my $c ( grep { /date/ } keys %$row ) {
+ $row->{$c} =~ s/(\d\d)\.(\d\d)\.(\d\d\d\d)/$3-$2-$1/;
+ }
+
+ $row->{sex} =~ s/\x{17D}/F/;
+
+ $row->{city} ||= '?'; # not null
+
+ insert 'borrowers' => $row;
+
+ }
}
-# select all posts which have been read or unread
-my $sql = qq{
+$k->do(qq{
+delete from borrowers where borrowernumber > 57
+});
+
+borrowers qq{
select
'S' as categorycode,
'SRE' as branchcode,
- mbr_stud as cardnumber,
+ 'S' || mbr_stud as cardnumber,
prez_stud as surname,
ime_stud as firstname,
djevprezime as othernames,
dat_prava_do as dateexpiry
-- aktivan as ??,
from studk
-limit 100
};
-my $sth = $f->prepare($sql);
-$sth->execute;
-
-while (my $row = $sth->fetchrow_hashref ) {
-
- # poor man's (sqlite) outer join
- $row->{'country'} = lookup('drzava', 'ozn_drzava', 'naz_drzava', $row->{'country'} );
- $row->{'city'} = lookup('mjesto', 'post_ozn', 'naz_mjesto', $row->{'city'} );
-
- $row->{'B_country'} = lookup('drzava', 'ozn_drzava', 'naz_drzava', $row->{'B_country'} );
- $row->{'B_city'} = lookup('mjesto', 'post_ozn', 'naz_mjesto', $row->{'B_city'} );
-
- foreach my $c ( grep { /date/ } keys %$row ) {
- $row->{$c} =~ s/(\d\d)\.(\d\d)\.(\d\d\d\d)/$3-$2-$1/;
- }
-
- $row->{sex} =~ s/Ž/F/;
-
- warn dump $row;
+borrowers qq{
+select
+ 'D' as categorycode,
+ 'SRE' as branchcode,
+ 'D' || sif_djel as cardnumber,
+ ime_djel as firstname,
+ prez_djel as surname,
+ ozn_titula as title, -- FIXME
+-- sif_orgjed (REF orgjed) as B_address,
+-- sif_strsp_djel as ??,
+ tel_djel as phonepro,
+ dat_prekid_ro as dateexpiry,
+-- tel_poduz as ??,
+ fax_poduz as fax,
+ ozn_drzava as country,
+ post_ozn as zipcode,
+ post_ozn as city,
+ adr_stan as address,
+ tel_stan as phone
+from djelat
+};
-}
+borrowers qq{
+select
+ 'O' as categorycode,
+ 'SRE' as branchcode,
+ 'O' || mbr_clan as cardnumber,
+ ime_clan as firstname,
+ prez_clan as surname,
+ ozn_titula as title, -- ?
+ zvanje_clan as borrowernotes, -- ?
+-- krat_poduz (REF poduz) as B_address,
+ tel_poduz as B_phone,
+-- fax_poduz as ??,
+ ozn_drzava as country,
+ post_ozn as zipcode,
+ post_ozn as city,
+ adr_stan as address,
+ tel_stan as phone
+-- aktivan as ??,
+from clost
+};
+$k->commit;