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