import biblio and biblioitems from koha
[google-map-tiles.git] / koha-import.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use DBI;
6
7 our $koha_dsn    = 'dbi:oursql:dbname=koha';
8 our $koha_user   = 'kohaadmin';
9 our $koha_passwd = '';
10
11 our $geo_dsn = 'dbi:Pg:dbname=koha';
12 our $geo_user = '';
13 our $geo_passwd = '';
14
15 our $opts = { RaiseError => 1 }; #, AutoCommit => 0, pg_enable_utf8 => 1, oursql_enable_utf8 => 1 };
16
17 require '/srv/koha-config.pl';
18
19 my $k_dbh = DBI->connect($koha_dsn, $koha_user, $koha_passwd, $opts) || die $DBI::errstr;
20 my $g_dbh = DBI->connect($geo_dsn, $geo_user, $geo_passwd, $opts) || die $DBI::errstr;
21
22 sub fetch_table {
23         my ( $table, $pk ) = @_;
24
25         warn "# clean $table";
26         $g_dbh->do( "delete from $table" );
27
28         my $offset = 0;
29
30         my $sql = "select * from $table order by $pk";
31
32         warn "# import $table";
33
34         do {
35
36                 my $sth = $k_dbh->prepare( "$sql limit 1000 offset $offset" );
37                 print STDERR "$table [$offset] ";
38                 $sth->execute;
39
40                 $offset = 0 if ! $sth->rows; # exit
41
42                 my @cols = @{ $sth->{NAME_lc} };
43                 my $sql_insert = "insert into $table (" . join(',',@cols) . ") values (" . join(',', map { '?' } @cols ) . ")";
44                 my $sth_insert = $g_dbh->prepare( $sql_insert );
45
46                 while( my $row = $sth->fetchrow_arrayref ) {
47                         eval { $sth_insert->execute( @$row ) };
48                         $offset++;
49                         print STDERR "$offset " if $offset % 100 == 0;
50                 }
51
52                 print STDERR "\n";
53
54         } while $offset;
55 }
56
57 fetch_table 'biblio' => 'biblionumber' ;
58 fetch_table 'biblioitems' => 'biblioitemnumber' ;