| 1 | #====================================================================== | ||||||||||
| 2 | package DBD::SQLite::VirtualTable::FileContent; | ||||||||||
| 3 | #====================================================================== | ||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use base 'DBD::SQLite::VirtualTable'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 2 | my %option_ok = map {($_ => 1)} qw/source content_col path_col | |||||||||
| 9 | expose root get_content/; | ||||||||||
| 10 | |||||||||||
| 11 | 2 | my %defaults = ( | |||||||||
| 12 | content_col => "content", | ||||||||||
| 13 | path_col => "path", | ||||||||||
| 14 | expose => "*", | ||||||||||
| 15 | get_content => "DBD::SQLite::VirtualTable::FileContent::get_content", | ||||||||||
| 16 | ); | ||||||||||
| 17 | |||||||||||
| 18 | |||||||||||
| 19 | #---------------------------------------------------------------------- | ||||||||||
| 20 | # object instanciation | ||||||||||
| 21 | #---------------------------------------------------------------------- | ||||||||||
| 22 | |||||||||||
| 23 | sub NEW { | ||||||||||
| 24 | 3 | ▶ | my $class = shift; | ||||||||
|
Subroutine
NEW: called CC=12 SCAR=25.7
Pod
documented
| |||||||||||
| 25 | |||||||||||
| 26 | 3 | my $self = $class->_PREPARE_SELF(@_); | |||||||||
| 27 | |||||||||||
| 28 | 3 | local $" = ", "; # for array interpolation in strings | |||||||||
| 29 | |||||||||||
| 30 | # initial parameter check | ||||||||||
| 31 | 3 | ▶ | !@{$self->{columns}} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | 0 | or die "${class}->NEW(): illegal options: @{$self->{columns}}"; | |||||||||
| 33 | $self->{options}{source} | ||||||||||
| 34 | 3 | ▶ | or die "${class}->NEW(): missing (source=...)"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 3 | my @bad_options = grep {!$option_ok{$_}} keys %{$self->{options}}; | |||||||||
| 36 | !@bad_options | ||||||||||
| 37 | 3 | ▶ | or die "${class}->NEW(): bad options: @bad_options"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | |||||||||||
| 39 | # defaults ... tempted to use //= but we still want to support perl 5.8 :-( | ||||||||||
| 40 | 3 | foreach my $k (keys %defaults) { | |||||||||
| 41 | defined $self->{options}{$k} | ||||||||||
| 42 | 12 | ▶ | or $self->{options}{$k} = $defaults{$k}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | # get list of columns from the source table | ||||||||||
| 46 | 3 | my $src_table = $self->{options}{source}; | |||||||||
| 47 | 3 | my $sql = "PRAGMA table_info($src_table)"; | |||||||||
| 48 | 3 | my $dbh = ${$self->{dbh_ref}}; # can't use method ->dbh, not blessed yet | |||||||||
| 49 | 3 | my $src_info = $dbh->selectall_arrayref($sql, {Slice => [1, 2]}); | |||||||||
| 50 | 3 | ▶ | @$src_info | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | or die "${class}->NEW(source=$src_table): no such table in database"; | ||||||||||
| 52 | |||||||||||
| 53 | # associate each source colname with its type info or " " (should eval true) | ||||||||||
| 54 | 3 | ▶ | my %src_col = map { ($_->[0] => $_->[1] || " ") } @$src_info; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | |||||||||||
| 56 | |||||||||||
| 57 | # check / complete the exposed columns | ||||||||||
| 58 | 3 | my @exposed_cols; | |||||||||
| 59 | 3 | ▶ | if ($self->{options}{expose} eq '*') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 0 | @exposed_cols = map {$_->[0]} @$src_info; | |||||||||
| 61 | } | ||||||||||
| 62 | else { | ||||||||||
| 63 | 3 | @exposed_cols = split /\s*,\s*/, $self->{options}{expose}; | |||||||||
| 64 | 3 | my @bad_cols = grep { !$src_col{$_} } @exposed_cols; | |||||||||
| 65 | 3 | ▶ | die "table $src_table has no column named @bad_cols" if @bad_cols; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | } | ||||||||||
| 67 | 3 | for (@exposed_cols) { | |||||||||
| 68 | die "$class: $self->{options}{content_col} cannot be both the " | ||||||||||
| 69 | 5 | ▶ | . "content_col and an exposed col" if $_ eq $self->{options}{content_col}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | # build the list of columns for this table | ||||||||||
| 73 | $self->{columns} = [ "$self->{options}{content_col} TEXT", | ||||||||||
| 74 | 3 | map {"$_ $src_col{$_}"} @exposed_cols ]; | |||||||||
| 75 | |||||||||||
| 76 | # acquire a coderef to the get_content() implementation, which | ||||||||||
| 77 | # was given as a symbolic reference in %options | ||||||||||
| 78 | 2 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 79 | 3 | $self->{get_content} = \ &{$self->{options}{get_content}}; | |||||||||
| 80 | |||||||||||
| 81 | 3 | bless $self, $class; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | sub _build_headers { | ||||||||||
| 85 | 3 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_build_headers: called CC=1 SCAR=0.0
| |||||||||||
| 86 | |||||||||||
| 87 | 3 | my $cols = $self->sqlite_table_info; | |||||||||
| 88 | |||||||||||
| 89 | # headers : names of columns, without type information | ||||||||||
| 90 | 3 | $self->{headers} = [ map {$_->{name}} @$cols ]; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | |||||||||||
| 94 | #---------------------------------------------------------------------- | ||||||||||
| 95 | # method for initiating a search | ||||||||||
| 96 | #---------------------------------------------------------------------- | ||||||||||
| 97 | |||||||||||
| 98 | sub BEST_INDEX { | ||||||||||
| 99 | 10 | ▶ | my ($self, $constraints, $order_by) = @_; | ||||||||
|
Subroutine
BEST_INDEX: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 100 | |||||||||||
| 101 | 10 | ▶ | $self->_build_headers if !$self->{headers}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | |||||||||||
| 103 | 10 | my @conditions; | |||||||||
| 104 | 10 | my $ix = 0; | |||||||||
| 105 | 10 | foreach my $constraint (grep {$_->{usable}} @$constraints) { | |||||||||
| 106 | 5 | my $col = $constraint->{col}; | |||||||||
| 107 | |||||||||||
| 108 | # if this is the content column, skip because we can't filter on it | ||||||||||
| 109 | 5 | ▶ | next if $col == 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | |||||||||||
| 111 | # for other columns, build a fragment for SQL WHERE on the underlying table | ||||||||||
| 112 | 4 | ▶ | my $colname = $col == -1 ? "rowid" : $self->{headers}[$col]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | 4 | push @conditions, "$colname $constraint->{op} ?"; | |||||||||
| 114 | 4 | $constraint->{argvIndex} = $ix++; | |||||||||
| 115 | 4 | $constraint->{omit} = 1; # SQLite doesn't need to re-check the op | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | # TODO : exploit $order_by to add ordering clauses within idxStr | ||||||||||
| 119 | |||||||||||
| 120 | 10 | my $outputs = { | |||||||||
| 121 | idxNum => 1, | ||||||||||
| 122 | idxStr => join(" AND ", @conditions), | ||||||||||
| 123 | orderByConsumed => 0, | ||||||||||
| 124 | estimatedCost => 1.0, | ||||||||||
| 125 | estimatedRows => undef, | ||||||||||
| 126 | }; | ||||||||||
| 127 | |||||||||||
| 128 | 10 | return $outputs; | |||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | |||||||||||
| 132 | #---------------------------------------------------------------------- | ||||||||||
| 133 | # method for preventing updates | ||||||||||
| 134 | #---------------------------------------------------------------------- | ||||||||||
| 135 | |||||||||||
| 136 | sub _SQLITE_UPDATE { | ||||||||||
| 137 | 0 | ▶ | my ($self, $old_rowid, $new_rowid, @values) = @_; | ||||||||
|
Subroutine
_SQLITE_UPDATE: not called CC=1 SCAR=6.9
| |||||||||||
| 138 | |||||||||||
| 139 | 0 | die "attempt to update a readonly virtual table"; | |||||||||
| 140 | } | ||||||||||
| 141 | |||||||||||
| 142 | |||||||||||
| 143 | #---------------------------------------------------------------------- | ||||||||||
| 144 | # file slurping function (not a method!) | ||||||||||
| 145 | #---------------------------------------------------------------------- | ||||||||||
| 146 | |||||||||||
| 147 | sub get_content { | ||||||||||
| 148 | 83 | ▶ | my ($path, $root) = @_; | ||||||||
|
Subroutine
get_content: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||
| 149 | |||||||||||
| 150 | 83 | ▶ | $path = "$root/$path" if $root; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 151 | |||||||||||
| 152 | 83 | my $content = ""; | |||||||||
| 153 | 83 | ▶ | if (open my $fh, "<", $path) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 154 | 83 | local $/; # slurp the whole file into a scalar | |||||||||
| 155 | 83 | $content = <$fh>; | |||||||||
| 156 | 83 | close $fh; | |||||||||
| 157 | } | ||||||||||
| 158 | else { | ||||||||||
| 159 | 0 | warn "can't open $path"; | |||||||||
| 160 | } | ||||||||||
| 161 | |||||||||||
| 162 | 83 | return $content; | |||||||||
| 163 | } | ||||||||||
| 164 | |||||||||||
| 165 | |||||||||||
| 166 | |||||||||||
| 167 | #====================================================================== | ||||||||||
| 168 | package DBD::SQLite::VirtualTable::FileContent::Cursor; | ||||||||||
| 169 | #====================================================================== | ||||||||||
| 170 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 171 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 172 | 2 | ▶ | use base "DBD::SQLite::VirtualTable::Cursor"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 173 | |||||||||||
| 174 | |||||||||||
| 175 | sub FILTER { | ||||||||||
| 176 | 60 | ▶ | my ($self, $idxNum, $idxStr, @values) = @_; | ||||||||
|
Subroutine
FILTER: called CC=3 SCAR=11.0
| |||||||||||
| 177 | |||||||||||
| 178 | 60 | my $vtable = $self->{vtable}; | |||||||||
| 179 | |||||||||||
| 180 | # build SQL | ||||||||||
| 181 | 60 | local $" = ", "; | |||||||||
| 182 | 60 | my @cols = @{$vtable->{headers}}; | |||||||||
| 183 | 60 | $cols[0] = 'rowid'; # replace the content column by the rowid | |||||||||
| 184 | 60 | push @cols, $vtable->{options}{path_col}; # path col in last position | |||||||||
| 185 | 60 | my $sql = "SELECT @cols FROM $vtable->{options}{source}"; | |||||||||
| 186 | 60 | ▶ | $sql .= " WHERE $idxStr" if $idxStr; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 187 | |||||||||||
| 188 | # request on the index table | ||||||||||
| 189 | 60 | my $dbh = $vtable->dbh; | |||||||||
| 190 | 60 | ▶ | $self->{sth} = $dbh->prepare($sql) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 191 | or die DBI->errstr; | ||||||||||
| 192 | 60 | $self->{sth}->execute(@values); | |||||||||
| 193 | 60 | $self->{row} = $self->{sth}->fetchrow_arrayref; | |||||||||
| 194 | |||||||||||
| 195 | 60 | return; | |||||||||
| 196 | } | ||||||||||
| 197 | |||||||||||
| 198 | |||||||||||
| 199 | sub EOF { | ||||||||||
| 200 | 87 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
EOF: called CC=1 SCAR=0.0
| |||||||||||
| 201 | |||||||||||
| 202 | 87 | return !$self->{row}; | |||||||||
| 203 | } | ||||||||||
| 204 | |||||||||||
| 205 | sub NEXT { | ||||||||||
| 206 | 27 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
NEXT: called CC=1 SCAR=0.0
| |||||||||||
| 207 | |||||||||||
| 208 | 27 | $self->{row} = $self->{sth}->fetchrow_arrayref; | |||||||||
| 209 | } | ||||||||||
| 210 | |||||||||||
| 211 | sub COLUMN { | ||||||||||
| 212 | 170 | ▶ | my ($self, $idxCol) = @_; | ||||||||
|
Subroutine
COLUMN: called CC=2 SCAR=6.9
| |||||||||||
| 213 | |||||||||||
| 214 | 170 | ▶ | return $idxCol == 0 ? $self->file_content : $self->{row}[$idxCol]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 215 | } | ||||||||||
| 216 | |||||||||||
| 217 | sub ROWID { | ||||||||||
| 218 | 80 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
ROWID: called CC=1 SCAR=0.0
| |||||||||||
| 219 | |||||||||||
| 220 | 80 | return $self->{row}[0]; | |||||||||
| 221 | } | ||||||||||
| 222 | |||||||||||
| 223 | sub file_content { | ||||||||||
| 224 | 83 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
file_content: called CC=1 SCAR=0.0
| |||||||||||
| 225 | |||||||||||
| 226 | 83 | my $root = $self->{vtable}{options}{root}; | |||||||||
| 227 | 83 | my $path = $self->{row}[-1]; | |||||||||
| 228 | 83 | my $get_content_func = $self->{vtable}{get_content}; | |||||||||
| 229 | |||||||||||
| 230 | 83 | return $get_content_func->($path, $root); | |||||||||
| 231 | } | ||||||||||
| 232 | |||||||||||
| 233 | |||||||||||
| 234 | 2 | 1; | |||||||||
| 235 | |||||||||||
| 236 | __END__ | ||||||||||