| 1 | package Search::Fulltext::SQLite; | ||||||||||
| 2 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 5 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 5 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 5 | ▶ | use DBI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 5 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 5 | ▶ | use constant { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 5 | TABLE => 'fts4table', | |||||||||
| 11 | CONTENT_COL => 'content', | ||||||||||
| 12 | DOCID_COL => 'docid', | ||||||||||
| 13 | 5 | }; | |||||||||
| 14 | |||||||||||
| 15 | 9 | ▶ | sub _make_dbh { | ||||||||
|
Subroutine
_make_dbh: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 9 | my $dbfile = shift; | |||||||||
| 17 | 9 | DBI->connect( | |||||||||
| 18 | "dbi:SQLite:dbname=$dbfile", "", "", | ||||||||||
| 19 | { | ||||||||||
| 20 | RaiseError => 1, | ||||||||||
| 21 | AutoCommit => 1, | ||||||||||
| 22 | sqlite_unicode => 1, | ||||||||||
| 23 | } | ||||||||||
| 24 | ); | ||||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | 9 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=5 SCAR=17.4
Pod
undocumented
| |||||||||||
| 28 | 9 | my ($class, @args) = @_; | |||||||||
| 29 | 9 | ▶ | my %args = ref $args[0] eq 'HASH' ? %{$args[0]} : @args; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | |||||||||||
| 31 | 0 | ▶ | unless ($args{docs}) { croak "'docs' is required for creating new instance of $class" } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | 0 | ▶ | unless ($args{dbfile}) { croak "'dbfile' is required for creating new instance of $class" } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 0 | ▶ | unless ($args{tokenizer}) { croak "'tokenizer' is required for creating new instance of $class" } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | |||||||||||
| 35 | my $self = bless { | ||||||||||
| 36 | 9 | dbh => _make_dbh($args{dbfile}), | |||||||||
| 37 | %args | ||||||||||
| 38 | }, $class; | ||||||||||
| 39 | 9 | $self->_make_fts4_index; | |||||||||
| 40 | 8 | $self; | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 9 | ▶ | sub _make_fts4_index { | ||||||||
|
Subroutine
_make_fts4_index: called CC=2 SCAR=6.9
| |||||||||||
| 44 | 9 | my $self = shift; | |||||||||
| 45 | 9 | my $dbh = $self->{dbh}; | |||||||||
| 46 | 9 | my $tokenizer = $self->{tokenizer}; | |||||||||
| 47 | |||||||||||
| 48 | 9 | $dbh->do("DROP TABLE IF EXISTS " . TABLE); | |||||||||
| 49 | 9 | $dbh->do("CREATE VIRTUAL TABLE " . TABLE . " USING fts4(" . CONTENT_COL . ", tokenize=$tokenizer)"); | |||||||||
| 50 | |||||||||||
| 51 | 8 | $dbh->begin_work; | |||||||||
| 52 | 8 | my $sth = $dbh->prepare("INSERT INTO " . TABLE . " (" . CONTENT_COL . ") VALUES (?)"); | |||||||||
| 53 | 8 | $sth->execute($_) for @{$self->{docs}}; | |||||||||
| 54 | 8 | $sth->finish; | |||||||||
| 55 | 8 | $dbh->commit; | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 14 | ▶ | sub search_docids { | ||||||||
|
Subroutine
search_docids: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 59 | 14 | my ($self, $query) = @_; | |||||||||
| 60 | 14 | my $dbh = $self->{dbh}; | |||||||||
| 61 | 14 | my $sth = $dbh->prepare("SELECT " . DOCID_COL . "-1 FROM " . TABLE . " WHERE " . CONTENT_COL . " MATCH ?"); | |||||||||
| 62 | 14 | $sth->execute($query); | |||||||||
| 63 | 14 | my @docids = (); | |||||||||
| 64 | 14 | ▶ | while (my @row = $sth->fetchrow_array) { push @docids, $row[0] } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 14 | $sth->finish; | |||||||||
| 66 | 14 | \@docids; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 9 | ▶ | sub DESTROY { | ||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||
| 70 | 9 | my $self = shift; | |||||||||
| 71 | 9 | $self->{dbh}->disconnect; | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 5 | 1; | |||||||||