| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WordList::DBI; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | 
| 4 |  |  |  |  |  |  | our $DATE = '2020-05-24'; # DATE | 
| 5 |  |  |  |  |  |  | our $DIST = 'WordList-DBI'; # DIST | 
| 6 |  |  |  |  |  |  | our $VERSION = '0.001'; # VERSION | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 1 |  |  | 1 |  | 122261 | use strict; | 
|  | 1 |  |  |  |  | 11 |  | 
|  | 1 |  |  |  |  | 30 |  | 
| 9 | 1 |  |  | 1 |  | 6 | use warnings; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 28 |  | 
| 10 | 1 |  |  | 1 |  | 5 | use parent qw(WordList); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 5 |  | 
| 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 |  | 3 | my $self = shift; | 
| 44 | 1 | 50 |  |  |  | 8 | if ($self->{params}{dbh}) { | 
| 45 | 1 |  |  |  |  | 3 | $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 | 704333 | my $class = shift; | 
| 57 | 1 |  |  |  |  | 13 | my $self = $class->SUPER::new(@_); | 
| 58 | 1 |  |  |  |  | 145 | $self->_connect; | 
| 59 | 1 |  |  |  |  | 4 | $self; | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | sub reset_iterator { | 
| 63 | 4 |  |  | 4 | 1 | 876 | my $self = shift; | 
| 64 | 4 |  |  |  |  | 57 | $self->{_sth} = $self->{_dbh}->prepare($self->{params}{query}); | 
| 65 | 4 |  |  |  |  | 717 | $self->{_sth}->execute; | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | sub next_word { | 
| 69 | 14 |  |  | 14 | 1 | 106 | my $self = shift; | 
| 70 | 14 |  |  |  |  | 124 | my ($word) = $self->{_sth}->fetchrow_array; | 
| 71 | 14 |  |  |  |  | 71 | $word; | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | sub pick { | 
| 75 | 1 |  |  | 1 | 1 | 4 | my ($self, $num, $allow_duplicates) = @_; | 
| 76 | 1 | 50 |  |  |  | 5 | $num = 1 unless defined $num; | 
| 77 | 1 | 50 |  |  |  | 4 | 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__ |