| 1 | 21 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 21 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package FusqlFS::Artifact; | ||||||||||||||||||||||||||||
| 5 | 21 | ▶ | use FusqlFS::Version; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 20 | our $VERSION = $FusqlFS::Version::VERSION; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | FusqlFS::Artifact - basic abstract class to represent database artifact in FusqlFS | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | package FusqlFS::Backend::PgSQL::Tables; | ||||||||||||||||||||||||||||
| 15 | use parent 'FusqlFS::Artifact'; | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | sub init | ||||||||||||||||||||||||||||
| 18 | { | ||||||||||||||||||||||||||||
| 19 | my $self = shift; | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # initialize Tables specific resources | ||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | sub get | ||||||||||||||||||||||||||||
| 25 | { | ||||||||||||||||||||||||||||
| 26 | my $self = shift; | ||||||||||||||||||||||||||||
| 27 | my ($table, $name) = @_; | ||||||||||||||||||||||||||||
| 28 | return $self->one_row("SELECT * FROM %s WHERE id = ?", [$table], $name); | ||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | sub list | ||||||||||||||||||||||||||||
| 32 | { | ||||||||||||||||||||||||||||
| 33 | my $self = shift; | ||||||||||||||||||||||||||||
| 34 | my ($table) = @_; | ||||||||||||||||||||||||||||
| 35 | return $self->all_col("SELECT id FROM %s %s", [$table, $self->limit]); | ||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | sub store | ||||||||||||||||||||||||||||
| 39 | { | ||||||||||||||||||||||||||||
| 40 | my $self = shift; | ||||||||||||||||||||||||||||
| 41 | my ($table, $data) = @_; | ||||||||||||||||||||||||||||
| 42 | my $row = $self->validate($data, { | ||||||||||||||||||||||||||||
| 43 | id => qr/^\d+$/, | ||||||||||||||||||||||||||||
| 44 | -func => '', | ||||||||||||||||||||||||||||
| 45 | }) or return; | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | my $func = $row->{func}? $row->{func}.'(?)': '?'; | ||||||||||||||||||||||||||||
| 48 | my $sth = $self->build("UPDATE $table SET ", sub{ | ||||||||||||||||||||||||||||
| 49 | my ($k, $v) = @_; | ||||||||||||||||||||||||||||
| 50 | return " WHERE " unless $k; | ||||||||||||||||||||||||||||
| 51 | return " $k = $func ", $row->{$k}, $v; | ||||||||||||||||||||||||||||
| 52 | }, %{$self->get_table_fields($table)}, | ||||||||||||||||||||||||||||
| 53 | '' => '', | ||||||||||||||||||||||||||||
| 54 | id => SQL_INTEGER); | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | $sth->execute(); | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | sub get_table_fields | ||||||||||||||||||||||||||||
| 60 | { | ||||||||||||||||||||||||||||
| 61 | my $self = shift; | ||||||||||||||||||||||||||||
| 62 | my ($table) = @_; | ||||||||||||||||||||||||||||
| 63 | # fetches and returns field name => type hashref. | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | This abstract class declares interface between database artifacts (like tables, | ||||||||||||||||||||||||||||
| 69 | data rows, functions, roles etc.) and L<Fuse> hooks in L<FusqlFS>. | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | The point of this class is to abstract database layer interaction from file | ||||||||||||||||||||||||||||
| 72 | system structure operations, so it provides some basic operations under | ||||||||||||||||||||||||||||
| 73 | database artifacts like "get", "list", "create", "drop", etc. | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | For example L<FusqlFS::Backend::PgSQL::Tables> subclass defines it's | ||||||||||||||||||||||||||||
| 76 | L<get|FusqlFS::Backend::PgSQL::Tables/get> method to return table's description | ||||||||||||||||||||||||||||
| 77 | and L<list|FusqlFS::Backend::PgSQL::Tables/list> method to list all available | ||||||||||||||||||||||||||||
| 78 | tables, so this subclass is represented as directory with tables in file system. | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | For more examples see childrens of this class. | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =head1 METHODS | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =cut | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 20 | our $instance; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head2 Abstract interface methods | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =begin testing Artifact | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | #!noinst | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | isa_ok FusqlFS::Artifact->new(), 'FusqlFS::Artifact'; | ||||||||||||||||||||||||||||
| 95 | is FusqlFS::Artifact->get(), ''; | ||||||||||||||||||||||||||||
| 96 | is FusqlFS::Artifact->list(), undef; | ||||||||||||||||||||||||||||
| 97 | foreach my $method (qw(rename drop create store)) | ||||||||||||||||||||||||||||
| 98 | { | ||||||||||||||||||||||||||||
| 99 | is FusqlFS::Artifact->$method(), 1; | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =end testing | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =over | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =item new | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | Base constructor, shouldn't be overridden in most cases. Use L</init> method to | ||||||||||||||||||||||||||||
| 109 | initialize object instance. | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | Override it only if you really know what you do and you have no other way to | ||||||||||||||||||||||||||||
| 112 | achieve you goal. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | Input: $class | ||||||||||||||||||||||||||||
| 115 | Output: $artifact_instance. | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =item init | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | I<Abstract method> called on object instance (C<$self>) by constructor L</new> | ||||||||||||||||||||||||||||
| 120 | immediately after instance is created and blessed in order to initialize it. | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | All parameters given to constructor are passed to this method intact, value | ||||||||||||||||||||||||||||
| 123 | returned by this method is ignored. | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | Override this method instead of L</new> in order to initialize your class. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =item get | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | Get item from this artifact. | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | Input: @names. | ||||||||||||||||||||||||||||
| 132 | Output: $hashref|$arrayref|$scalarref|$coderef|$scalar|undef. | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | Hashrefs and arrayref are represented as directories in filesystem with keys | ||||||||||||||||||||||||||||
| 135 | (or indices in case of arrayref) as filenames and values as their content | ||||||||||||||||||||||||||||
| 136 | (maybe hashrefs or arrayrefs as well). | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | Scalarrefs are represented as symlinks, their content being the path to | ||||||||||||||||||||||||||||
| 139 | referenced object in filesystem. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Coderefs provide "pseudopipes" interface: at first request referenced sub is | ||||||||||||||||||||||||||||
| 142 | called without parameters for initialization and file content will be whatever | ||||||||||||||||||||||||||||
| 143 | the sub returns. On any write to the "pseudopipe" the sub is called with | ||||||||||||||||||||||||||||
| 144 | written data as first argument and the content of the file will be any text the | ||||||||||||||||||||||||||||
| 145 | sub returns back. Dynamic DB queries in L<FusqlFS::Backend::PgSQL::Queries> | ||||||||||||||||||||||||||||
| 146 | class are implemented with this interface. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | Scalars are represented with plain files. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | If this sub returns undef the file with given name is considered non-existant, | ||||||||||||||||||||||||||||
| 151 | and user will get C<NOENT> error. | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =item list | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | Get list of items, represented by class. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | Input: @names. | ||||||||||||||||||||||||||||
| 158 | Output: $arrayref|undef. | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | If this method returns arrayref of scalars, then the class is represented with | ||||||||||||||||||||||||||||
| 161 | directory containing elements with names from this array, otherwise (the method | ||||||||||||||||||||||||||||
| 162 | returns undef) the type of filesystem object is determined solely on L</get> | ||||||||||||||||||||||||||||
| 163 | method call results. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =item rename | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Renames given database artifact. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | Input: @names, $newname. | ||||||||||||||||||||||||||||
| 170 | Output: $success. | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | This method must rename database object defined with @names to new $newname | ||||||||||||||||||||||||||||
| 173 | and return any "true" value on success or undef on failure. | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =item drop | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | Removes given database artifact. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | Input: @names. | ||||||||||||||||||||||||||||
| 180 | Output: $success. | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | This method must drop given database object defined with @names and return | ||||||||||||||||||||||||||||
| 183 | any "true" value on success or undef on failure. | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =item create | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | Creates brand new database artifact. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | Input: @names. | ||||||||||||||||||||||||||||
| 190 | Output: $success. | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | This method must create new database object by given @name and return any | ||||||||||||||||||||||||||||
| 193 | "true" value on success or undef on failure. If given object can't be created | ||||||||||||||||||||||||||||
| 194 | without additional "content" data (e.g. table's index) it should create some | ||||||||||||||||||||||||||||
| 195 | kind of stub in memory/cache/anywhere and this stub must be visible via L</get> | ||||||||||||||||||||||||||||
| 196 | and L</list> methods giving the user a chance to fill it with some real data, | ||||||||||||||||||||||||||||
| 197 | so successive L</store> call can create the object. | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =item store | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | Stores any changes to object in database. | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | Input: @names, $data. | ||||||||||||||||||||||||||||
| 204 | Output: $success. | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | This method must accept the same $data structure as provided by L</get> method, | ||||||||||||||||||||||||||||
| 207 | possibly modified by user, and store it into database, maybe creating actual | ||||||||||||||||||||||||||||
| 208 | database object in process (see L</create> for details). | ||||||||||||||||||||||||||||
| 209 | The method must return any "true" value on success or undef on failure. | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =back | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =cut | ||||||||||||||||||||||||||||
| 214 | sub new | ||||||||||||||||||||||||||||
| 215 | 14 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 216 | 15 | my $class = shift; | |||||||||||||||||||||||||||
| 217 | 14 | my $self = {}; | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 14 | bless $self, $class; | |||||||||||||||||||||||||||
| 220 | 14 | $self->init(@_); | |||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 14 | return $self; | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | 2 | ▶ | sub init { } | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 225 | 1 | ▶ | sub get { return '' } | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 226 | 1 | ▶ | sub list { return } | ||||||||||||||||||||||||||
|
Subroutine
list: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 227 | 1 | ▶ | sub rename { return 1 } | ||||||||||||||||||||||||||
|
Subroutine
rename: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 228 | 1 | ▶ | sub drop { return 1 } | ||||||||||||||||||||||||||
|
Subroutine
drop: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 229 | 1 | ▶ | sub create { return 1 } | ||||||||||||||||||||||||||
|
Subroutine
create: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 230 | 1 | ▶ | sub store { return 1 } | ||||||||||||||||||||||||||
|
Subroutine
store: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =head2 DBI interface methods | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | =over | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =item dbh | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | Returns underlayed DBI handler. | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | Output: $dbh. | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | =item expr, cexpr | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | Prepare expression with $dbh->prepare() or $dbh->prepare_cached(). | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | Input: $sql, @sprintf. | ||||||||||||||||||||||||||||
| 247 | Output: $sth. | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | If C<@sprintf> is not empty, C<$sql> must be a scalar string with | ||||||||||||||||||||||||||||
| 250 | L<printf|perlfunc/sprintf FORMAT, LIST >-compatible placeholders, and | ||||||||||||||||||||||||||||
| 251 | C<sprintf()> will be called to populate this string with values from | ||||||||||||||||||||||||||||
| 252 | C<@sprintf> array. | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | The difference between C<expr> and C<cexpr> is the first calls L<DBI/prepare> | ||||||||||||||||||||||||||||
| 255 | and the second calls L<DBI/prepare_cached>. | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | =item do, cdo | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | Prepare and execute expression just like L<DBI/do>. | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | Input: $sth, @binds or $sql, $sprintf, @binds. | ||||||||||||||||||||||||||||
| 262 | Output: $result. | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | Both of them can take either SQL statement as a scalar string or prepared DBI | ||||||||||||||||||||||||||||
| 265 | statement in place of first argument. | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | If the first argument is a scalar string, the second argument can be either an | ||||||||||||||||||||||||||||
| 268 | arrayref or a hashref, and if it is, the string must be | ||||||||||||||||||||||||||||
| 269 | L<printf|perlfunc/sprintf EXPR, LIST >-compatible format string and | ||||||||||||||||||||||||||||
| 270 | L</hprintf()> will be used to populate SQL statement with values from second | ||||||||||||||||||||||||||||
| 271 | argument just like with L<expr|/expr, cexpr>. | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | C<do> just calls L<DBI/do> and returns success value returned with it, | ||||||||||||||||||||||||||||
| 274 | while C<cdo> calls L<DBI/prepare_cached> and returns this prepared statement | ||||||||||||||||||||||||||||
| 275 | in case it was successfully executed, undef otherwise. | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | =item one_row, one_col, all_col, all_row | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | Executes given statement and returns well formatted result. | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | Input: $sth, @binds or $sql, $sprintf, @binds. | ||||||||||||||||||||||||||||
| 282 | Output: $result. | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | Basicly these methods accept the same arguments (and process them the same way) | ||||||||||||||||||||||||||||
| 285 | as L<do|/do, cdo>, but return results in format better suited for immediate | ||||||||||||||||||||||||||||
| 286 | usage. | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | C<one_row> returns the first row as hashref with field names as keys and field | ||||||||||||||||||||||||||||
| 289 | values as values. C<all_col> returns arrayref composed from first field values | ||||||||||||||||||||||||||||
| 290 | from all result rows. C<all_row> returns arrayref composed from hashrefs, where | ||||||||||||||||||||||||||||
| 291 | each hashref represents data row with field names as keys and field values as | ||||||||||||||||||||||||||||
| 292 | values. C<one_col> returns single scalar - the value of first field of first | ||||||||||||||||||||||||||||
| 293 | record fetched by query. | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | =back | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | =cut | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | sub dbh | ||||||||||||||||||||||||||||
| 300 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
dbh: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 301 | 0 | $instance->{dbh}; | |||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | sub expr | ||||||||||||||||||||||||||||
| 305 | 16 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
expr: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 306 | 16 | my ($self, $sql, @sprintf) = @_; | |||||||||||||||||||||||||||
| 307 | 16 | ▶ | $sql = sprintf($sql, @sprintf) if @sprintf; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 308 | 16 | return $instance->{dbh}->prepare($sql); | |||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | sub cexpr | ||||||||||||||||||||||||||||
| 312 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
cexpr: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 313 | 0 | my ($self, $sql, @sprintf) = @_; | |||||||||||||||||||||||||||
| 314 | 0 | ▶ | $sql = sprintf($sql, @sprintf) if @sprintf; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 315 | 0 | return $instance->{dbh}->prepare_cached($sql, {}, 1); | |||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | sub do | ||||||||||||||||||||||||||||
| 319 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
do: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||
| 320 | 4 | my ($self, $sql, @binds) = @_; | |||||||||||||||||||||||||||
| 321 | 4 | ▶ | $sql = hprintf($sql, shift @binds) if !ref($sql) && ref($binds[0]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not ref $sql and ref $binds[0]
MC/DC0%0/2
not ref $sql and ref $binds[0]
not ref $sql
ref $binds[0]
Truth table33%1/3
not(A) and B
A= not ref $sqlB= ref $binds[0]
| |||||||||||||||||||||||||||||
| 322 | 4 | $instance->{dbh}->do($sql, {}, @binds); | |||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | sub cdo | ||||||||||||||||||||||||||||
| 326 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
cdo: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 327 | 0 | my ($self, $sql, @binds) = @_; | |||||||||||||||||||||||||||
| 328 | 0 | ▶ | $sql = $self->cexpr($sql, !ref($sql) && ref($binds[0])? @{shift @binds}: undef); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not ref $sql and ref $binds[0]
MC/DC0%0/2
not ref $sql and ref $binds[0]
not ref $sql
ref $binds[0]
Truth table0%0/3
not(A) and B
A= not ref $sqlB= ref $binds[0]
| |||||||||||||||||||||||||||||
| 329 | 0 | ▶ | return $sql if $sql->execute(@binds); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | sub one_row | ||||||||||||||||||||||||||||
| 333 | 5 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
one_row: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||
| 334 | 5 | my ($self, $sql, @binds) = @_; | |||||||||||||||||||||||||||
| 335 | 5 | ▶ | $sql = hprintf($sql, shift @binds) if !ref($sql) && ref($binds[0]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not ref $sql and ref $binds[0]
MC/DC0%0/2
not ref $sql and ref $binds[0]
not ref $sql
ref $binds[0]
Truth table33%1/3
not(A) and B
A= not ref $sqlB= ref $binds[0]
| |||||||||||||||||||||||||||||
| 336 | 5 | return $instance->{dbh}->selectrow_hashref($sql, {}, @binds); | |||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | sub one_col | ||||||||||||||||||||||||||||
| 340 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
one_col: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 341 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 342 | 0 | my $cols = $self->all_col(@_); | |||||||||||||||||||||||||||
| 343 | 0 | ▶ | return unless $cols && @$cols; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$cols and @$cols
MC/DC0%0/2
$cols and @$cols
$cols
@$cols
Truth table0%0/3
A and B
A= $colsB= @$cols
| |||||||||||||||||||||||||||||
| 344 | 0 | return $cols->[0]; | |||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | sub all_col | ||||||||||||||||||||||||||||
| 348 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
all_col: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||
| 349 | 4 | my ($self, $sql, @binds) = @_; | |||||||||||||||||||||||||||
| 350 | 4 | ▶ | $sql = hprintf($sql, shift @binds) if !ref($sql) && ref($binds[0]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not ref $sql and ref $binds[0]
MC/DC0%0/2
not ref $sql and ref $binds[0]
not ref $sql
ref $binds[0]
Truth table33%1/3
not(A) and B
A= not ref $sqlB= ref $binds[0]
| |||||||||||||||||||||||||||||
| 351 | 4 | return $instance->{dbh}->selectcol_arrayref($sql, {}, @binds); | |||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | sub all_row | ||||||||||||||||||||||||||||
| 355 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
all_row: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 356 | 0 | my ($self, $sql, @binds) = @_; | |||||||||||||||||||||||||||
| 357 | 0 | ▶ | $sql = hprintf($sql, shift @binds) if !ref($sql) && ref($binds[0]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not ref $sql and ref $binds[0]
MC/DC0%0/2
not ref $sql and ref $binds[0]
not ref $sql
ref $binds[0]
Truth table0%0/3
not(A) and B
A= not ref $sqlB= ref $binds[0]
| |||||||||||||||||||||||||||||
| 358 | 0 | return $instance->{dbh}->selectall_arrayref($sql, { Slice => {} }, @binds); | |||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | =head2 Data manipulation methods | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | =over | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | =item load | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | Parses input data in configured string format (e.g. YAML, JSON or XML) and | ||||||||||||||||||||||||||||
| 368 | returns perl structure (hashref or arrayref). | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | Input: $string_data. | ||||||||||||||||||||||||||||
| 371 | Output: $parsed_data. | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | Uses configured parser to deserialize plain string and produce perl structure | ||||||||||||||||||||||||||||
| 374 | (usually a hashref). In case of parsing failure returns undef. | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | If C<$string_data> is not a plain string, this method returns this value | ||||||||||||||||||||||||||||
| 377 | intact, so you can call this method on input data any number of times just to | ||||||||||||||||||||||||||||
| 378 | make sure they are correct perl structure, not a serialized data. | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | It is opposite of L</dump>. | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | =cut | ||||||||||||||||||||||||||||
| 383 | sub load | ||||||||||||||||||||||||||||
| 384 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
load: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 385 | 0 | ▶ | return $_[1] if ref $_[1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 386 | 0 | my $data; | |||||||||||||||||||||||||||
| 387 | 0 | eval { $data = $instance->{loader}->($_[1]) }; | |||||||||||||||||||||||||||
| 388 | 0 | ▶ | return if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 389 | 0 | return $data; | |||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | =item validate | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | Validates input data against a set of simple rules. | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | Input: $data, $rule, $overrule. | ||||||||||||||||||||||||||||
| 397 | Output: $validated_data|undef. | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | A rule can be: | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | =over | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | =item Hashref | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | The input data must be a hashref, every field in from rule's hash must also | ||||||||||||||||||||||||||||
| 406 | exist in data hash, rule's hash values are subrules to be matched against | ||||||||||||||||||||||||||||
| 407 | data's hash values. Hash keys with minus as first char are optional. | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | If input data is a scalar, it will be parsed with standard loader using | ||||||||||||||||||||||||||||
| 410 | L</load> method, and validation will fail if C<load()> call is. | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | =item Scalar | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | Ref of data value must be equal to this rule's value. | ||||||||||||||||||||||||||||
| 415 | If undef, data value must be simple scalar. | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | =item Arrayref | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | Every element in rule's array is a subrule, data value must match | ||||||||||||||||||||||||||||
| 420 | against all of the subrules. | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | =item Coderef | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | A subroutine referenced by the rule's value is called with data value | ||||||||||||||||||||||||||||
| 425 | as the first argument, it should return processed data if data is correct | ||||||||||||||||||||||||||||
| 426 | or undef if data is incorrect. | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | =item Anything else | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | Data's value must magically match rule's value (with C<~~> operator). | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | =back | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | Optional third argument (C<$overrule>) must be a coderef. It will be called with | ||||||||||||||||||||||||||||
| 435 | $_ locally set to parsed data and must return boolean value. If this value is | ||||||||||||||||||||||||||||
| 436 | false, then all data is discarded and validation fails, otherwise everything is | ||||||||||||||||||||||||||||
| 437 | ok. | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | =cut | ||||||||||||||||||||||||||||
| 440 | sub validate | ||||||||||||||||||||||||||||
| 441 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
validate: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 442 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 443 | 0 | my $result; | |||||||||||||||||||||||||||
| 444 | 0 | eval { | |||||||||||||||||||||||||||
| 445 | 0 | $result = $self->_validate(@_); | |||||||||||||||||||||||||||
| 446 | }; | ||||||||||||||||||||||||||||
| 447 | 0 | ▶ | return if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 448 | 0 | return $result; | |||||||||||||||||||||||||||
| 449 | } | ||||||||||||||||||||||||||||
| 450 | sub _validate | ||||||||||||||||||||||||||||
| 451 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_validate: not called CC=17 SCAR=57.2
| |||||||||||||||||||||||||||||
| 452 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 453 | 0 | my ($data, $rule, $overrule) = @_; | |||||||||||||||||||||||||||
| 454 | 0 | ▶ | return $data unless defined $rule; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 455 | 0 | my $result; | |||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | 0 | my $ref = ref $rule; | |||||||||||||||||||||||||||
| 458 | 0 | ▶ | if ($ref eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 459 | 0 | $result = $data; | |||||||||||||||||||||||||||
| 460 | 0 | foreach my $subrule (@$rule) | |||||||||||||||||||||||||||
| 461 | { | ||||||||||||||||||||||||||||
| 462 | 0 | $result = $self->validate($result, $subrule); | |||||||||||||||||||||||||||
| 463 | } | ||||||||||||||||||||||||||||
| 464 | } elsif ($ref eq 'CODE') { | ||||||||||||||||||||||||||||
| 465 | 0 | local $_ = $data; | |||||||||||||||||||||||||||
| 466 | 0 | $result = $rule->(); | |||||||||||||||||||||||||||
| 467 | } elsif ($ref eq 'HASH') { | ||||||||||||||||||||||||||||
| 468 | 0 | $result = {}; | |||||||||||||||||||||||||||
| 469 | 0 | ▶ | my $struct = ref $data? $data: $self->load($data); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 470 | 0 | ▶ | die 'INVALID' unless ref $struct eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 471 | 0 | ▶ | while (my ($field, $subrule) = each %$rule) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 472 | { | ||||||||||||||||||||||||||||
| 473 | 0 | my $opt = $field =~ s/^-//; | |||||||||||||||||||||||||||
| 474 | 0 | eval { | |||||||||||||||||||||||||||
| 475 | 0 | ▶ | die 'INVALID' unless exists $struct->{$field}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 476 | 0 | $result->{$field} = $self->validate($struct->{$field}, $subrule); | |||||||||||||||||||||||||||
| 477 | }; | ||||||||||||||||||||||||||||
| 478 | 0 | ▶ | die 'INVALID' if $@ and not $opt; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$@ and not $opt
MC/DC0%0/2
$@ and not $opt
$@
not $opt
Truth table0%0/3
A and not(B)
A= $@B= not $opt
| |||||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||
| 480 | } elsif ($ref eq '') { | ||||||||||||||||||||||||||||
| 481 | 0 | ▶ | die 'INVALID' unless ref $data eq $rule; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 482 | 0 | $result = $data; | |||||||||||||||||||||||||||
| 483 | } else { | ||||||||||||||||||||||||||||
| 484 | 0 | ▶ | die 'INVALID' unless $data ~~ $rule; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 485 | 0 | $result = $data; | |||||||||||||||||||||||||||
| 486 | } | ||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | 0 | ▶ | if ($overrule) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 489 | { | ||||||||||||||||||||||||||||
| 490 | 0 | local $_ = $result; | |||||||||||||||||||||||||||
| 491 | 0 | ▶ | die 'INVALID' unless $overrule->($data); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||
| 493 | 0 | return $result; | |||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | =item set_of | ||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | Helper validation function, creates L</validate> rule to check | ||||||||||||||||||||||||||||
| 499 | if given value is a set with elements from given variants set. | ||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | Input: @variants. | ||||||||||||||||||||||||||||
| 502 | Output: $rule. | ||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | =cut | ||||||||||||||||||||||||||||
| 505 | sub set_of | ||||||||||||||||||||||||||||
| 506 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
set_of: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 507 | 0 | my (undef, @variants) = @_; | |||||||||||||||||||||||||||
| 508 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 509 | 0 | ▶ | return unless ref $_ eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 510 | 0 | my @items = grep $_ ~~ @variants, keys %{{ map { $_ => 1 } @$_ }}; | |||||||||||||||||||||||||||
| 511 | 0 | ▶ | return \@items if scalar(@items) > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 512 | 0 | }; | |||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | =item dump | ||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | Convert perl structure into string of configured format (e.g. YAML, JSON or | ||||||||||||||||||||||||||||
| 518 | XML). | ||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||
| 520 | Input: $data. | ||||||||||||||||||||||||||||
| 521 | Output: $string. | ||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||
| 523 | Uses configured dumper to serialize perl structure into plain scalar string. | ||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | It is opposite of L</load>. | ||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | =cut | ||||||||||||||||||||||||||||
| 528 | sub dump | ||||||||||||||||||||||||||||
| 529 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
dump: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 530 | 0 | ▶ | return $instance->{dumper}->($_[1]) if $_[1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 531 | 0 | return; | |||||||||||||||||||||||||||
| 532 | } | ||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | =item asplit | ||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||
| 536 | Splits string using configured split character. | ||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||
| 538 | Input: $string, $max_chunks=undef. | ||||||||||||||||||||||||||||
| 539 | Output: @chunks. | ||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||
| 541 | It is opposite of L</ajoin>. Second optional argument is an integer, and is | ||||||||||||||||||||||||||||
| 542 | identical to one of C<split>, i.e. sets max number of chunks to split input | ||||||||||||||||||||||||||||
| 543 | C<$string> into, but it also defines B<minimum> number of chunks as well, so if | ||||||||||||||||||||||||||||
| 544 | C<$string> contains less than given C<$max_chunks> chunks, the result will be | ||||||||||||||||||||||||||||
| 545 | padded with C<undef>s to the right up to this number, so output always contain | ||||||||||||||||||||||||||||
| 546 | C<$max_chunks> elements. | ||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | =cut | ||||||||||||||||||||||||||||
| 549 | sub asplit | ||||||||||||||||||||||||||||
| 550 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
asplit: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 551 | 0 | my ($self, $string, $max_chunks) = @_; | |||||||||||||||||||||||||||
| 552 | 0 | ▶ | $max_chunks ||= 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$max_chunks ||= 0
MC/DC0%0/1
$max_chunks ||= 0
$max_chunks
Truth table0%0/2
A
A= $max_chunks
| |||||||||||||||||||||||||||||
| 553 | 0 | my @result = split $instance->{fnsplit}, $string, $max_chunks; | |||||||||||||||||||||||||||
| 554 | 0 | ▶ | push @result, undef while @result < $max_chunks; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 555 | 0 | return @result; | |||||||||||||||||||||||||||
| 556 | } | ||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||
| 558 | =item ajoin | ||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||
| 560 | Joins chunks with configured split character as a glue. | ||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||
| 562 | Input: @chunks. | ||||||||||||||||||||||||||||
| 563 | Output: $string. | ||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||
| 565 | It is opposite of L</asplit>. | ||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||
| 567 | =cut | ||||||||||||||||||||||||||||
| 568 | sub ajoin | ||||||||||||||||||||||||||||
| 569 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
ajoin: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 570 | 0 | shift @_; | |||||||||||||||||||||||||||
| 571 | 0 | return join $instance->{fnsep}, @_; | |||||||||||||||||||||||||||
| 572 | } | ||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||
| 574 | =item concat | ||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||
| 576 | Produces SQL statement to join given data chunks with configured split | ||||||||||||||||||||||||||||
| 577 | character as a glue. | ||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||
| 579 | Input: @chunks. | ||||||||||||||||||||||||||||
| 580 | Output: $sql_clause. | ||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||
| 582 | It is opposite of L</asplit> (in some sense). | ||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||
| 584 | =cut | ||||||||||||||||||||||||||||
| 585 | sub concat | ||||||||||||||||||||||||||||
| 586 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
concat: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 587 | 0 | shift @_; | |||||||||||||||||||||||||||
| 588 | 0 | return '"' . join("\" || '$instance->{fnsep}' || \"", @_) . '"'; | |||||||||||||||||||||||||||
| 589 | } | ||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||
| 591 | =item build | ||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||
| 593 | Builds SQL statement step by step from given configuration data chunks, | ||||||||||||||||||||||||||||
| 594 | prepares and binds it afterwards. | ||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||
| 596 | Input: $sql, $filter, %iter. | ||||||||||||||||||||||||||||
| 597 | Output: $sth. | ||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||
| 599 | C<$filter> must be a coderef, C<$sql> is a initial SQL statement value to build | ||||||||||||||||||||||||||||
| 600 | upon and C<%iter> is a series of key-value pairs (normally meant to be field | ||||||||||||||||||||||||||||
| 601 | value => build config pairs, but it is not carved in stone). | ||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||
| 603 | For every key-value pair in C<%iter> C<$filter-E<gt>($key, $value)> is called in | ||||||||||||||||||||||||||||
| 604 | list context. It must return the next chunk of SQL which will be added to | ||||||||||||||||||||||||||||
| 605 | resulting SQL statement and an optional bind value to be associated with this | ||||||||||||||||||||||||||||
| 606 | SQL chunk. This bind value must be either a single bind value or a bind value | ||||||||||||||||||||||||||||
| 607 | and a configaration parameter for L<DBI/bind_param> (i.e. third argument). | ||||||||||||||||||||||||||||
| 608 | If C<$filter> returns empty list (or undef, which is the same for list context) | ||||||||||||||||||||||||||||
| 609 | the iteration is silently skipped and the next pair from C<%iter> is taken. | ||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||
| 611 | When C<%iter> is depleted, constructed SQL statement is prepared, all gathered | ||||||||||||||||||||||||||||
| 612 | bind values are bound to it using C<bind_param()> and the resulting statement | ||||||||||||||||||||||||||||
| 613 | handler is returned. | ||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | So you can use this method to construct complex SQL statements using table | ||||||||||||||||||||||||||||
| 616 | driven SQL statements construction, producing finely tuned binds with correctly | ||||||||||||||||||||||||||||
| 617 | typed bind values. | ||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||
| 619 | =back | ||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||
| 621 | =cut | ||||||||||||||||||||||||||||
| 622 | sub build | ||||||||||||||||||||||||||||
| 623 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
build: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 624 | 0 | my ($self, $sql, $filter, %iter) = @_; | |||||||||||||||||||||||||||
| 625 | 0 | my (@binds, @bind); | |||||||||||||||||||||||||||
| 626 | 0 | ▶ | while (my ($field, $value) = each %iter) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 627 | { | ||||||||||||||||||||||||||||
| 628 | 0 | ▶ | next unless (@bind) = ($filter->($field, $value)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 629 | 0 | $sql .= shift @bind; | |||||||||||||||||||||||||||
| 630 | 0 | ▶ | push @binds, [ @bind ] if @bind; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 631 | } | ||||||||||||||||||||||||||||
| 632 | 0 | $sql = $instance->{dbh}->prepare($sql); | |||||||||||||||||||||||||||
| 633 | 0 | $sql->bind_param($_+1, @{$binds[$_]}) foreach (0..$#binds); | |||||||||||||||||||||||||||
| 634 | 0 | return $sql; | |||||||||||||||||||||||||||
| 635 | } | ||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||
| 637 | =item hprintf | ||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||
| 639 | I<Static method>. Extended L<sprintf|perlfunc/sprintf FORMAT, LIST > version. | ||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||
| 641 | Input: $format, $binds. | ||||||||||||||||||||||||||||
| 642 | Output: $string. | ||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||
| 644 | The C<$format> is the same as for C<sprintf>, C<$binds> is either an arrayref | ||||||||||||||||||||||||||||
| 645 | or a hashref. If it is an arrayref the result of the method is the same as of | ||||||||||||||||||||||||||||
| 646 | C<sprintf($format, @$binds)>. If it is a hashref the result is a little | ||||||||||||||||||||||||||||
| 647 | different. | ||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||
| 649 | For hashref C<$binds> all placeholders in C<$format> must be in the form | ||||||||||||||||||||||||||||
| 650 | of C<%(key)$x>, where C<x> is any C<sprintf> compatible conversion | ||||||||||||||||||||||||||||
| 651 | and C<key> is the key in the C<%$binds>, so that instead of positional | ||||||||||||||||||||||||||||
| 652 | placeholders substitution, placeholders in C<$format> are substituted | ||||||||||||||||||||||||||||
| 653 | with correspondent C<%$binds>' values. | ||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||
| 655 | E.g. if you call C<hprintf("%(msg)s: %(count)d\n", { msg =E<gt> 'The counter is', | ||||||||||||||||||||||||||||
| 656 | count =E<gt> 10 })> you will get the string C<"The counter is: 10\n"> as the | ||||||||||||||||||||||||||||
| 657 | result. This is really useful if you need to keep formatting strings loosely | ||||||||||||||||||||||||||||
| 658 | linked with real data inserted into them, e.g. in case of l10n with something | ||||||||||||||||||||||||||||
| 659 | like gettext. | ||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||
| 661 | =cut | ||||||||||||||||||||||||||||
| 662 | sub hprintf | ||||||||||||||||||||||||||||
| 663 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
hprintf: called CC=2 SCAR=9.2
Pod
documented
| |||||||||||||||||||||||||||||
| 664 | 4 | my ($format, $vars) = @_; | |||||||||||||||||||||||||||
| 665 | 4 | my @binds; | |||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||
| 667 | 4 | ▶ | if (ref $vars eq 'ARRAY') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 668 | { | ||||||||||||||||||||||||||||
| 669 | 4 | @binds = @$vars; | |||||||||||||||||||||||||||
| 670 | } | ||||||||||||||||||||||||||||
| 671 | else | ||||||||||||||||||||||||||||
| 672 | { | ||||||||||||||||||||||||||||
| 673 | 0 | my $i = 0; | |||||||||||||||||||||||||||
| 674 | 0 | @binds = map { $format =~ s/\%\($_\)\$/'%'.(++$i).'$'/ge; $vars->{$_} } keys %$vars; | |||||||||||||||||||||||||||
| 675 | } | ||||||||||||||||||||||||||||
| 676 | 4 | return sprintf($format, @binds); | |||||||||||||||||||||||||||
| 677 | } | ||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||
| 679 | =item adiff | ||||||||||||||||||||||||||||
| 680 | |||||||||||||||||||||||||||||
| 681 | Get difference between to arrays. | ||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||
| 683 | Input: $oldarray, $newarray. | ||||||||||||||||||||||||||||
| 684 | Output: $newitems, $olditems. | ||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||
| 686 | Gets two arrayrefs and returns two arrayrefs with items existing in $arrayref2 only | ||||||||||||||||||||||||||||
| 687 | (C<$newitems>) and in $arrayref1 only (C<$olditems>). | ||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||
| 689 | =cut | ||||||||||||||||||||||||||||
| 690 | sub adiff | ||||||||||||||||||||||||||||
| 691 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
adiff: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 692 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 693 | 0 | my ($oldarray, $newarray) = @_; | |||||||||||||||||||||||||||
| 694 | 0 | my %oldarray = map { $_ => 1 } @$oldarray; | |||||||||||||||||||||||||||
| 695 | 0 | my %newarray = map { $_ => 1 } @$newarray; | |||||||||||||||||||||||||||
| 696 | 0 | my @newitems = grep !exists $oldarray{$_}, @$newarray; | |||||||||||||||||||||||||||
| 697 | 0 | my @olditems = grep !exists $newarray{$_}, @$oldarray; | |||||||||||||||||||||||||||
| 698 | 0 | return \@newitems, \@olditems; | |||||||||||||||||||||||||||
| 699 | } | ||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||
| 701 | =head2 Configuration methods | ||||||||||||||||||||||||||||
| 702 | |||||||||||||||||||||||||||||
| 703 | =over | ||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||
| 705 | =item limit | ||||||||||||||||||||||||||||
| 706 | |||||||||||||||||||||||||||||
| 707 | Returns configured C<LIMIT ...> clause or empty string if it's not configured. | ||||||||||||||||||||||||||||
| 708 | |||||||||||||||||||||||||||||
| 709 | Output: $limit_clause. | ||||||||||||||||||||||||||||
| 710 | |||||||||||||||||||||||||||||
| 711 | This method can be used to compose C<SELECT ...> statements according to | ||||||||||||||||||||||||||||
| 712 | configured limit option. | ||||||||||||||||||||||||||||
| 713 | |||||||||||||||||||||||||||||
| 714 | =cut | ||||||||||||||||||||||||||||
| 715 | sub limit | ||||||||||||||||||||||||||||
| 716 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
limit: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 717 | 0 | my $limit = $instance->{limit}; | |||||||||||||||||||||||||||
| 718 | 0 | ▶ | return $limit? "LIMIT $limit": ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 719 | } | ||||||||||||||||||||||||||||
| 720 | |||||||||||||||||||||||||||||
| 721 | =item fnsep | ||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||
| 723 | Returns configured split character. | ||||||||||||||||||||||||||||
| 724 | |||||||||||||||||||||||||||||
| 725 | Output: $fnsep. | ||||||||||||||||||||||||||||
| 726 | |||||||||||||||||||||||||||||
| 727 | =cut | ||||||||||||||||||||||||||||
| 728 | sub fnsep | ||||||||||||||||||||||||||||
| 729 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
fnsep: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 730 | 0 | return $instance->{fnsep}; | |||||||||||||||||||||||||||
| 731 | } | ||||||||||||||||||||||||||||
| 732 | |||||||||||||||||||||||||||||
| 733 | =item extend | ||||||||||||||||||||||||||||
| 734 | |||||||||||||||||||||||||||||
| 735 | Extend one hash with other hash inplace. | ||||||||||||||||||||||||||||
| 736 | |||||||||||||||||||||||||||||
| 737 | Input: $hashref, ... | ||||||||||||||||||||||||||||
| 738 | Output: $hashref | ||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||
| 740 | =cut | ||||||||||||||||||||||||||||
| 741 | sub extend | ||||||||||||||||||||||||||||
| 742 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
extend: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 743 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 744 | 0 | my $hash = shift; | |||||||||||||||||||||||||||
| 745 | 0 | foreach my $other (@_) { | |||||||||||||||||||||||||||
| 746 | 0 | ▶ | while (my ($key, $value) = each %$other) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 747 | 0 | $hash->{$key} = $value; | |||||||||||||||||||||||||||
| 748 | } | ||||||||||||||||||||||||||||
| 749 | } | ||||||||||||||||||||||||||||
| 750 | 0 | ▶ | return wantarray? %$hash: $hash; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 751 | } | ||||||||||||||||||||||||||||
| 752 | |||||||||||||||||||||||||||||
| 753 | =item autopackages | ||||||||||||||||||||||||||||
| 754 | |||||||||||||||||||||||||||||
| 755 | Automagically plug all designated packages and plug them in as subpackages. | ||||||||||||||||||||||||||||
| 756 | |||||||||||||||||||||||||||||
| 757 | It takes a list of names, each of them will be put to lower case and used as | ||||||||||||||||||||||||||||
| 758 | subpackages hash key (so it will be subdirectory name in mounted filesystem). | ||||||||||||||||||||||||||||
| 759 | Package name for each of the name will be formed with current package name put | ||||||||||||||||||||||||||||
| 760 | to singular form appended with the name with first letter put to upper case. | ||||||||||||||||||||||||||||
| 761 | All absent packages (which couldn't be imported without errors) will be | ||||||||||||||||||||||||||||
| 762 | ignored. | ||||||||||||||||||||||||||||
| 763 | |||||||||||||||||||||||||||||
| 764 | Each such subpackage will be instantiated with C<new()> method call without | ||||||||||||||||||||||||||||
| 765 | arguments. | ||||||||||||||||||||||||||||
| 766 | |||||||||||||||||||||||||||||
| 767 | Singular form of current package name is produced by removing last "s" | ||||||||||||||||||||||||||||
| 768 | from package name, which can be quite naive for some cases. | ||||||||||||||||||||||||||||
| 769 | |||||||||||||||||||||||||||||
| 770 | For example if you run C<$self-E<gt>autopackages("indices", "struct")> from | ||||||||||||||||||||||||||||
| 771 | C<FusqlFS::Backend::SomeEngine::Tables> package, two packages will be imported | ||||||||||||||||||||||||||||
| 772 | and instantiated with C<new()> method call (without arguments): | ||||||||||||||||||||||||||||
| 773 | C<FusqlFS::Backend::SomeEngine::Table::Indices> and | ||||||||||||||||||||||||||||
| 774 | C<FusqlFS::Backend::SomeEngine::Table::Struct>. Each instance will be put into | ||||||||||||||||||||||||||||
| 775 | C<$self-E<gt>{subpackages}> hash under C<indices> and C<struct> keys | ||||||||||||||||||||||||||||
| 776 | correspondingly. | ||||||||||||||||||||||||||||
| 777 | |||||||||||||||||||||||||||||
| 778 | The method returns subpackages hashref in scalar context and linearized version | ||||||||||||||||||||||||||||
| 779 | of subpackages hash in list context. | ||||||||||||||||||||||||||||
| 780 | |||||||||||||||||||||||||||||
| 781 | See also: L<FusqlFS::Backend::Base> for information about subpackages hash, | ||||||||||||||||||||||||||||
| 782 | L<FusqlFS::Entry> for more information about file path resolution algorithm. | ||||||||||||||||||||||||||||
| 783 | |||||||||||||||||||||||||||||
| 784 | Input: $name, ... | ||||||||||||||||||||||||||||
| 785 | Output: $hashref | ||||||||||||||||||||||||||||
| 786 | |||||||||||||||||||||||||||||
| 787 | =cut | ||||||||||||||||||||||||||||
| 788 | sub autopackages | ||||||||||||||||||||||||||||
| 789 | 5 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
autopackages: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 790 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 791 | 5 | my $class = ref $self; | |||||||||||||||||||||||||||
| 792 | 5 | $class =~ s/s$//; | |||||||||||||||||||||||||||
| 793 | |||||||||||||||||||||||||||||
| 794 | 5 | ▶ | $self->{subpackages} ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"subpackages"} ||= {}
MC/DC100%1/1
$self->{"subpackages"} ||= {}
$self->{"subpackages"}
Truth table100%2/2
A
A= $self->{"subpackages"}
| |||||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||
| 796 | 5 | foreach my $name (@_) { | |||||||||||||||||||||||||||
| 797 | 11 | my $name = lc $name; | |||||||||||||||||||||||||||
| 798 | 11 | my $package = $class . '::' . ucfirst $name; | |||||||||||||||||||||||||||
| 799 | 11 | ▶ | eval qq{use $package}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 800 | |||||||||||||||||||||||||||||
| 801 | 11 | ▶ | next if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 802 | |||||||||||||||||||||||||||||
| 803 | 11 | $self->{subpackages}->{$name} = $package->new(); | |||||||||||||||||||||||||||
| 804 | } | ||||||||||||||||||||||||||||
| 805 | |||||||||||||||||||||||||||||
| 806 | 5 | return $self->{subpackages}; | |||||||||||||||||||||||||||
| 807 | } | ||||||||||||||||||||||||||||
| 808 | |||||||||||||||||||||||||||||
| 809 | 20 | 1; | |||||||||||||||||||||||||||
| 810 | |||||||||||||||||||||||||||||
| 811 | __END__ | ||||||||||||||||||||||||||||