File Coverage

blib/lib/WordList/DBI.pm
Criterion Covered Total %
statement 26 35 74.2
branch 3 8 37.5
condition n/a
subroutine 8 8 100.0
pod 4 4 100.0
total 41 55 74.5


line stmt bran cond sub pod time code
1             package WordList::DBI;
2              
3             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4             our $DATE = '2020-06-01'; # DATE
5             our $DIST = 'WordList-DBI'; # DIST
6             our $VERSION = '0.002'; # VERSION
7              
8 1     1   124061 use strict;
  1         10  
  1         30  
9 1     1   5 use warnings;
  1         2  
  1         26  
10 1     1   5 use parent qw(WordList);
  1         2  
  1         4  
11              
12             our $NO_STATS = 1;
13             our $SORT = 'custom';
14             our $DYNAMIC = 2; # it can be 1 or 2, depending on the query
15              
16             our %PARAMS = (
17             dbh => {
18             summary => 'Either dbh or dsn+user+password need to be specified',
19             schema => 'obj*',
20             },
21             dsn => {
22             summary => 'Either dbh or dsn+user+password need to be specified',
23             schema => 'str*',
24             },
25             user => {
26             summary => 'Either dbh or dsn+user+password need to be specified',
27             schema => 'obj*',
28             },
29             password => {
30             summary => 'Either dbh or dsn+user+password need to be specified',
31             schema => 'str*',
32             },
33             query => {
34             schema => 'str*',
35             req => 1,
36             },
37             query_pick => {
38             schema => 'str*',
39             },
40             );
41              
42             sub _connect {
43 1     1   2 my $self = shift;
44 1 50       5 if ($self->{params}{dbh}) {
45 1         4 $self->{_dbh} = $self->{params}{dbh};
46             } else {
47 0         0 require DBI;
48             $self->{_dbh} = DBI->connect(
49             $self->{params}{dsn},
50             $self->{params}{user}, $self->{params}{password},
51 0         0 {RaiseError=>1});
52             }
53             }
54              
55             sub new {
56 1     1 1 54769 my $class = shift;
57 1         19 my $self = $class->SUPER::new(@_);
58 1         87 $self->_connect;
59 1         3 $self;
60             }
61              
62             sub reset_iterator {
63 4     4 1 759 my $self = shift;
64 4         28 $self->{_sth} = $self->{_dbh}->prepare($self->{params}{query});
65 4         610 $self->{_sth}->execute;
66             }
67              
68             sub next_word {
69 14     14 1 107 my $self = shift;
70 14         120 my ($word) = $self->{_sth}->fetchrow_array;
71 14         55 $word;
72             }
73              
74             sub pick {
75 1     1 1 5 my ($self, $num, $allow_duplicates) = @_;
76 1 50       5 $num = 1 unless defined $num;
77 1 50       3 unless (defined $self->{query_pick}) {
78 1         9 return $self->SUPER::pick($num, $allow_duplicates);
79             }
80 0           my $sth = $self->{_dbh}->prepare($self->{params}{query_pick});
81 0           $sth->execute;
82 0           my @words;
83 0           while (defined(my ($word) = $sth->fetchrow_array)) {
84 0           push @words, $word;
85 0 0         last if @words >= $num;
86             }
87 0           @words;
88             }
89              
90             1;
91             # ABSTRACT: Wordlist that get its list from a DBI query
92              
93             __END__