5 use Data::Dump qw/dump/;
9 my $f = DBI->connect("dbi:SQLite:dbname=knjiznica.sqlite","","", { RaiseError => 1, AutoCommit => 0 }) || die $DBI::errstr;
10 $f->{sqlite_unicode} = 1;
11 my $k = DBI->connect("dbi:mysql:database=koha_fer","","", { RaiseError => 1, AutoCommit => 0, mysql_enable_utf8 => 1 }) || die $DBI::errstr;
15 my ( $dbh, $t, $k, $v, $s, $default ) = @_;
18 if ( exists $lookup->{$key} ) {
19 $hash = $lookup->{$key};
21 warn "# select $k,$v from $t";
22 my $sth = $dbh->prepare( "select $k,$v from $t" );
24 while (my $row = $sth->fetchrow_hashref() ) {
25 $hash->{ $row->{$k} } = $row->{$v};
27 $lookup->{$key} = $hash;
30 return unless length $s > 1;
31 if ( ! exists $lookup->{$key}->{$s} ) {
32 warn "ERROR: no $s in $key\n";
35 $lookup->{$key}->{$s};
41 my ( $table, $row ) = @_;
42 $insert->{$table} ||= $k->prepare(
43 "insert into $table ("
44 . join(',', keys %$row)
46 . join(',', map { '?' } keys %$row )
50 foreach my $c ( grep { /(date|timestamp)/ } keys %$row ) {
51 $row->{$c} =~ s/^(\d\d)\.(\d\d)\.(\d\d\d\d)/$3-$2-$1/;
54 $insert->{$table}->execute( values %$row );
55 warn "# inserted ", dump $row;
61 my $sth = $f->prepare($sql);
66 while (my $row = $sth->fetchrow_hashref ) {
70 # poor man's (sqlite) outer join
71 $row->{'country'} = lookup($f, 'drzava', 'ozn_drzava', 'naz_drzava', $row->{'country'}, $row->{contry} );
72 $row->{'city'} = lookup($f, 'mjesto', 'post_ozn', 'naz_mjesto', $row->{'city'}, $row->{city} );
74 $row->{'B_country'} = lookup($f, 'drzava', 'ozn_drzava', 'naz_drzava', $row->{'B_country'}, $row->{B_country} );
75 $row->{'B_city'} = lookup($f, 'mjesto', 'post_ozn', 'naz_mjesto', $row->{'B_city'}, $row->{B_city} );
77 $row->{'email'} = lookup($f, 'clanost', 'ozn_vrclan||ozn_clan', 'email', $row->{'cardnumber'}, undef);
78 $row->{'emailpro'} = lookup($f, 'clanost', 'ozn_vrclan||ozn_clan', 'email_k', $row->{'cardnumber'}, undef);
81 $row->{sex} =~ s/\x{17D}/F/;
83 $row->{city} ||= '?'; # not null
85 my $jmbg = delete $row->{jmbg};
87 insert 'borrowers' => $row;
89 insert 'borrower_attributes' => {
90 borrowernumber => $k->{mysql_insertid},
98 $k->do(qq{ delete from borrowers where borrowernumber > 57 });
99 $k->do(qq{ delete from borrower_attributes where borrowernumber > 57 });
105 'S' || mbr_stud as cardnumber,
106 prez_stud as surname,
107 ime_stud as firstname,
108 djevprezime as othernames,
110 dat_rodj as dateofbirth,
113 ozn_drzava_preb as country,
114 post_ozn_preb as zipcode,
115 post_ozn_preb as city,
117 ozn_drzava_stan as B_country,
118 post_ozn_stan as B_zipcode,
119 post_ozn_stan as B_city,
120 adr_stan as B_address,
122 dat_prava_do as dateexpiry,
132 'D' || sif_djel as cardnumber,
133 ime_djel as firstname,
134 prez_djel as surname,
135 ozn_titula as title, -- FIXME
136 -- sif_orgjed (REF orgjed) as B_address,
137 -- sif_strsp_djel as ??,
138 tel_djel as phonepro,
139 dat_prekid_ro as dateexpiry,
142 ozn_drzava as country,
155 'O' || mbr_clan as cardnumber,
156 ime_clan as firstname,
157 prez_clan as surname,
158 ozn_titula as title, -- ?
159 zvanje_clan as borrowernotes, -- ?
160 -- krat_poduz (REF poduz) as B_address,
161 tel_poduz as B_phone,
163 ozn_drzava as country,
175 my ($table,$sql) = @_;
176 my $sth = $f->prepare($sql);
181 while (my $row = $sth->fetchrow_hashref ) {
182 $row->{borrowernumber} = lookup($k, 'borrowers', 'cardnumber' => 'borrowernumber', $row->{borrowernumber});
183 $row->{itemnumber} = lookup($k, 'items', 'barcode' => 'itemnumber', $row->{itemnumber});
184 insert $table => $row;
190 ozn_vrclan||ozn_clan as borrowernumber,
191 sif_primj as itemnumber,
192 datum_do as date_due,
193 'SRE' as branchcode, -- FIXME
194 datum_vra as returndate,
195 datum_pos as issuedate,
196 datum_pos||' '||vrijeme_pos as timestamp
200 issues 'old_issues' => qq{$posud_sql where datum_vra != ''};
201 issues 'issues' => qq{$posud_sql where datum_vra == ''};