1 package MojoFacets::Import::SQL;
10 use Data::Dump qw(dump);
13 __PACKAGE__->attr('full_path');
20 my $path = $self->full_path;
22 my $sql = read_file $path, { binmode => ':raw' }; # FIXME configurable!
24 my $dsn = $1 if $sql =~ s/--\s*(dbi:\S+)//;
25 my $user = $1 if $sql =~ s/--\s*user=\s*(\S+)//;
26 my $passwd = $1 if $sql =~ s/--\s*passwo?r?d=\s*(\S+)//;
28 warn "# $dsn $user/", '*' x length($passwd);
30 my $opts = { RaiseError => 1, AutoCommit => 0 };
31 delete $opts->{AutoCommit} if $dsn =~ m/Gofer/; # not supported with Gofer
33 if ( $dsn =~ m{Pg} ) {
34 $opts->{pg_enable_utf8} = 1;
35 } elsif ( $dsn =~ m{mysql} ) {
36 $opts->{mysql_enable_utf8} = 1;
38 warn "utf-8 encoding can't be set for this dsn!";
41 my $dbh = DBI->connect($dsn, $user, $passwd, $opts) || die $DBI::errstr;
44 my $sth = $dbh->prepare($sql);
47 warn "# got ", $sth->rows, " rows\n";
49 my $data = { items => [] };
50 $data->{header} = $sth->{NAME};
52 while( my $row = $sth->fetchrow_hashref ) {
53 push @{ $data->{items} }, $row;