| 1 | package AxKit::App::TABOO::Data::Plurals; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Carp; | ||
| 5 | |||
| 6 | use Data::Dumper; | ||
| 7 | use AxKit::App::TABOO::Data; | ||
| 8 | |||
| 9 | |||
| 10 | |||
| 11 | use vars qw/@ISA/; | ||
| 12 | @ISA = qw(AxKit::App::TABOO::Data); | ||
| 13 | |||
| 14 | use DBI; | ||
| 15 | use Exception::Class::DBI; | ||
| 16 | |||
| 17 | |||
| 18 | our $VERSION = '0.3'; | ||
| 19 | |||
| 20 | |||
| 21 | =head1 NAME | ||
| 22 | |||
| 23 | AxKit::App::TABOO::Data::Plurals - Base class to handle multiple Data objects in TABOO | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | Sometimes, it is desireable to retrieve and handle multiple instances | ||
| 28 | of a data object, and most economic to do it in a single | ||
| 29 | operation. That is what the Plural data objects are for. The load | ||
| 30 | methods should generally retrieve all records as efficiently as they | ||
| 31 | can, and then return an array of their singular counterparts. | ||
| 32 | |||
| 33 | =head1 METHODS | ||
| 34 | |||
| 35 | It implements some new methods, with names that should ring bells for | ||
| 36 | everyone. It also reimplements some methods, but nothing you really | ||
| 37 | need to be aware of, unless you want to implement a C<load> method for | ||
| 38 | a subclass. If you want to raise your awareness anyway, the | ||
| 39 | documentation of them is for you: | ||
| 40 | |||
| 41 | =over | ||
| 42 | |||
| 43 | |||
| 44 | |||
| 45 | =item C<Push($singular)> | ||
| 46 | |||
| 47 | This does pretty much what C<push> does in a normal context, it adds a | ||
| 48 | singular version C<$singular> of a object to the plural object that | ||
| 49 | the method is used on. | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | sub Push { | ||
| 54 | my $self = shift; | ||
| 55 | my $singular = shift; | ||
| 56 | push(@{${$self}{ENTRIES}}, $singular); | ||
| 57 | return $self; | ||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | =item C<Grep($pattern, $field)> | ||
| 63 | |||
| 64 | Somewhat similar to the usual C<grep> function, but takes as argument | ||
| 65 | a pattern to search for, but I<not> enclosed in slashes, and which | ||
| 66 | data field to look in. Will return an object of the same class with | ||
| 67 | the records that matched, or C<undef> if there were no matches. | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | sub Grep { | ||
| 72 | my $self = shift; | ||
| 73 | my $pattern = shift; | ||
| 74 | my $field = shift; | ||
| 75 | my $work = ref($self)->new($self->dbconnectargs()); | ||
| 76 | my $one; | ||
| 77 | foreach my $tmp (@{${$self}{ENTRIES}}) { | ||
| 78 | if (${$tmp}{$field} =~ m/$pattern/) { | ||
| 79 | $work->Push($tmp); | ||
| 80 | $one = 1; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | if ($one) { | ||
| 84 | return $work; | ||
| 85 | } else { | ||
| 86 | return undef; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | |||
| 91 | |||
| 92 | =item C<_load(%args)> | ||
| 93 | |||
| 94 | As the underscore implies this is B<for internal use only>! It can do | ||
| 95 | the hard work for subclasses of this class. It uses named parameters, | ||
| 96 | the first C<what> is used to determine which fields to retrieve. It is | ||
| 97 | a string consisting of a commaseparated list of fields, as specified | ||
| 98 | in the data store. If not given, all fields will be fetched. The | ||
| 99 | C<limit> argument is to be used to determine which records to | ||
| 100 | retrieve, these will be combined by logical AND. By default, exact | ||
| 101 | matches of the C<limit> arguments will be used, but you may also | ||
| 102 | supply a C<regex> argument with an array containing the fields that | ||
| 103 | should be fetched using case sensitive POSIX regular expressions. | ||
| 104 | |||
| 105 | You may also supply a C<orderby> argument, which is an expression used | ||
| 106 | to determine the order of entries returned. Usually, it would be a | ||
| 107 | simple string with the field name to use, e.g. C<'timestamp'>, but you | ||
| 108 | might want to append the keyword "C<DESC>" to it for descending | ||
| 109 | order. Finally, you may supply a C<entries> argument, which is the | ||
| 110 | maximum number of entries to retrieve. It will return an arrayref | ||
| 111 | containing the data from the storage. | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | sub _load { | ||
| 116 | my ($self, %args) = @_; | ||
| 117 | my $what = $args{'what'} || '*'; | ||
| 118 | my %arg = %{$args{'limit'}} if (ref($args{'limit'}) eq 'HASH'); | ||
| 119 | my $orderby = $args{'orderby'}; | ||
| 120 | my $entries = $args{'entries'}; | ||
| 121 | my $dbh = DBI->connect($self->dbconnectargs()); | ||
| 122 | my $query = "SELECT " . $what . " FROM " . $self->dbfrom(); | ||
| 123 | if (%arg) { | ||
| 124 | $query .= " WHERE "; | ||
| 125 | } | ||
| 126 | my $i=1; | ||
| 127 | my @keys = keys(%arg); | ||
| 128 | foreach my $key (@keys) { | ||
| 129 | $query .= $key; | ||
| 130 | if (grep($key, @{$args{'regex'}})) { | ||
| 131 | $query .= "~?"; | ||
| 132 | } else { | ||
| 133 | $query .= "=?"; | ||
| 134 | } | ||
| 135 | if ($i <= $#keys) { | ||
| 136 | $query .= " AND "; | ||
| 137 | } | ||
| 138 | $i++; | ||
| 139 | } | ||
| 140 | if ($orderby) { | ||
| 141 | $query .= ' ORDER BY ' . $orderby; | ||
| 142 | } | ||
| 143 | if ($entries) { | ||
| 144 | $query .= ' LIMIT ' . $entries; | ||
| 145 | } | ||
| 146 | |||
| 147 | my $sth = $dbh->prepare($query); | ||
| 148 | $i=1; | ||
| 149 | foreach my $key (@keys) { | ||
| 150 | $sth->bind_param($i, $arg{$key}); | ||
| 151 | $i++; | ||
| 152 | } | ||
| 153 | # Couldn't get parameter binding to work, but they should be passed OK...: | ||
| 154 | # if ($orderby) { | ||
| 155 | # $sth->bind_param($i, $orderby); | ||
| 156 | # $i++; | ||
| 157 | # } | ||
| 158 | # if ($entries) { | ||
| 159 | # $sth->bind_param($i, $entries); | ||
| 160 | # } | ||
| 161 | |||
| 162 | $sth->execute(); | ||
| 163 | return $sth->fetchall_arrayref({}); | ||
| 164 | } | ||
| 165 | |||
| 166 | |||
| 167 | =item C<write_xml($doc, $parent)> | ||
| 168 | |||
| 169 | To avoid bloating the parent class too much, this takes care of some | ||
| 170 | specifics for plurals, but leaves most of the job to the parent | ||
| 171 | class. Has a completely identical interface as the parent class, and | ||
| 172 | can be called like it without further ado. | ||
| 173 | |||
| 174 | If an object of this class has had its element and/or namespace set | ||
| 175 | with C<xmlelement()>/C<xmlns()>/C<xmlprefix()> respectively, the | ||
| 176 | individual entries will have the same element and/or namespace. | ||
| 177 | |||
| 178 | =cut | ||
| 179 | |||
| 180 | sub write_xml { | ||
| 181 | my $self = shift; | ||
| 182 | my $doc = shift; | ||
| 183 | my $parent = shift; | ||
| 184 | foreach my $entry (@{${$self}{ENTRIES}}) { | ||
| 185 | # If the object has had its element and/or NS set to something, we pass it on. | ||
| 186 | if (${$self}{XMLELEMENT}) { | ||
| 187 | $entry->xmlelement($self->xmlelement()); | ||
| 188 | } | ||
| 189 | if (${$self}{XMLPREFIX}) { | ||
| 190 | $entry->xmlprefix($self->xmlprefix()); | ||
| 191 | } | ||
| 192 | if (${$self}{XMLNS}) { | ||
| 193 | $entry->xmlns($self->xmlns()); | ||
| 194 | } | ||
| 195 | |||
| 196 | $entry->write_xml($doc, $parent); | ||
| 197 | } | ||
| 198 | return $doc; | ||
| 199 | } | ||
| 200 | |||
| 201 | sub populate { | ||
| 202 | die "populate method not yet implemented for Plurals"; | ||
| 203 | } | ||
| 204 | |||
| 205 | sub apache_request_changed { | ||
| 206 | die "apache_request_changed method not yet implemented for Plurals"; | ||
| 207 | } | ||
| 208 | |||
| 209 | sub stored { | ||
| 210 | die "stored method not yet implemented for Plurals"; | ||
| 211 | } | ||
| 212 | |||
| 213 | |||
| 214 | sub onfile { | ||
| 215 | die "onfile method not yet implemented for Plurals"; | ||
| 216 | } | ||
| 217 | |||
| 218 | |||
| 219 | =back | ||
| 220 | |||
| 221 | =head1 BUGS/TODO | ||
| 222 | |||
| 223 | The save method is not yet reimplemented and may not work. | ||
| 224 | |||
| 225 | =head1 FORMALITIES | ||
| 226 | |||
| 227 | See L<AxKit::App::TABOO>. | ||
| 228 | |||
| 229 | =cut | ||
| 230 | |||
| 231 | 1; | ||
| 232 | |||
| 233 | |||
| 234 | |||
| 235 | |||
| 236 |