| 1 | =head1 NAME | ||
| 2 | |||
| 3 | XAO::DO::FS::List - List class for XAO::FS | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | my $customers_list=$odb->fetch('/Customers'); | ||
| 8 | |||
| 9 | my $customer=$customers_list->get('cust0001'); | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | List object usually used as is without overwriting it. The XAO | ||
| 14 | class name for the list object is FS::List. | ||
| 15 | |||
| 16 | A list object provides methods for managing a list of FS::Hash objects | ||
| 17 | of the same class -- storing, retrieving and searching on them. | ||
| 18 | |||
| 19 | List class shares most of the API with the Hash class. | ||
| 20 | |||
| 21 | Here is the list of all List methods (alphabetically): | ||
| 22 | |||
| 23 | =over | ||
| 24 | |||
| 25 | =cut | ||
| 26 | |||
| 27 | ############################################################################### | ||
| 28 | package XAO::DO::FS::List; | ||
| 29 | use strict; | ||
| 30 | use XAO::Utils; | ||
| 31 | use XAO::Objects; | ||
| 32 | |||
| 33 | use base XAO::Objects->load(objname => 'FS::Glue'); | ||
| 34 | |||
| 35 | use vars qw($VERSION); | ||
| 36 | $VERSION=(0+sprintf('%u.%03u',(q$Id: List.pm,v 2.10 2009/03/16 21:28:54 am Exp $ =~ /\s(\d+)\.(\d+)\s/))) || die "Bad VERSION"; | ||
| 37 | |||
| 38 | ############################################################################### | ||
| 39 | |||
| 40 | =item check_name () | ||
| 41 | |||
| 42 | Object names in lists have nearly the same set of restrains as | ||
| 43 | in hashes with just one exception - they can start from a digit. | ||
| 44 | Such behavior might be extended to hashes in later versions to | ||
| 45 | eliminate this difference. | ||
| 46 | |||
| 47 | For example, 123ABC456 is a legal Hash id inside of a List, but | ||
| 48 | is not a legal property ID or List ID inside of a Hash. | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | sub check_name ($$) { | ||
| 53 | my $self=shift; | ||
| 54 | my $name=shift; | ||
| 55 | return (defined($name) && | ||
| 56 | $name =~ /^[a-z0-9_]+$/i && | ||
| 57 | length($name)<=$self->key_length); | ||
| 58 | } | ||
| 59 | |||
| 60 | ############################################################################### | ||
| 61 | |||
| 62 | =item container_key () | ||
| 63 | |||
| 64 | Returns key that refers to the current List in the upper level Hash. | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | # defined in Glue. | ||
| 69 | |||
| 70 | ############################################################################### | ||
| 71 | |||
| 72 | =item container_object () | ||
| 73 | |||
| 74 | Returns a reference to the hash that contains | ||
| 75 | current list. | ||
| 76 | |||
| 77 | Example: | ||
| 78 | |||
| 79 | my $customer=$orders_list->container_object(); | ||
| 80 | |||
| 81 | Do not abuse this method, it involves a lot of overhead and can slow | ||
| 82 | your application down if abused. | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | sub container_object ($) { | ||
| 87 | my $self=shift; | ||
| 88 | |||
| 89 | my $base_name=$$self->{base_name}; | ||
| 90 | my $base_id=$$self->{base_id}; | ||
| 91 | $base_name && $base_id || | ||
| 92 | $self->throw("container_object - the object was not retrieved from the database"); | ||
| 93 | |||
| 94 | if($base_name eq 'FS::Global') { | ||
| 95 | return $self->_glue->fetch('/'); | ||
| 96 | } | ||
| 97 | |||
| 98 | my $list_base_name=$self->_glue->upper_class($base_name); | ||
| 99 | my $key_name=$self->_glue->_list_key_name($base_name,$list_base_name); | ||
| 100 | |||
| 101 | ## | ||
| 102 | # This is an optimisation for the case where current object has an | ||
| 103 | # URI already. It might not have it if container_object() is called | ||
| 104 | # from new() to determine URI in case of Collection. | ||
| 105 | # | ||
| 106 | my $uri=$self->uri; | ||
| 107 | if(defined($uri)) { | ||
| 108 | my @path=split(/\/+/,$uri); | ||
| 109 | $uri=join('/',@path[0..$#path-1]); | ||
| 110 | } | ||
| 111 | |||
| 112 | XAO::Objects->new( | ||
| 113 | objname => $base_name, | ||
| 114 | glue => $self->_glue, | ||
| 115 | unique_id => $base_id, | ||
| 116 | key_name => $key_name, | ||
| 117 | list_base_name => $list_base_name, | ||
| 118 | uri => $uri, | ||
| 119 | ); | ||
| 120 | } | ||
| 121 | |||
| 122 | ############################################################################### | ||
| 123 | |||
| 124 | =item delete ($) | ||
| 125 | |||
| 126 | Deletes object from the list - first calls destroy on the object to | ||
| 127 | recursively delete its content and then drops it from the list. | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | sub delete ($$) { | ||
| 132 | my $self=shift; | ||
| 133 | my $name=shift; | ||
| 134 | |||
| 135 | if($$self->{detached}) { | ||
| 136 | $self->throw("delete - is not implemented yet for detached mode"); | ||
| 137 | } else { | ||
| 138 | $self->_list_unlink_object($name); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | ############################################################################### | ||
| 143 | |||
| 144 | =item describe () | ||
| 145 | |||
| 146 | Describes itself, returns a hash reference with at least the following | ||
| 147 | elements: | ||
| 148 | |||
| 149 | type => 'list' | ||
| 150 | class => class name of Hashes stored inside | ||
| 151 | key => key name | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | sub describe ($;$) { | ||
| 156 | my $self=shift; | ||
| 157 | return { | ||
| 158 | type => @_ ? 'hash' : 'list', | ||
| 159 | class => $$self->{'class_name'}, | ||
| 160 | key => $$self->{'key_name'}, | ||
| 161 | }; | ||
| 162 | } | ||
| 163 | |||
| 164 | ############################################################################### | ||
| 165 | |||
| 166 | =item detach () | ||
| 167 | |||
| 168 | Detaches current object from the database. Not implemented, but safe to | ||
| 169 | use in read-only situations. | ||
| 170 | |||
| 171 | =cut | ||
| 172 | |||
| 173 | sub detach ($) { | ||
| 174 | ## Nothing | ||
| 175 | } | ||
| 176 | |||
| 177 | ############################################################################### | ||
| 178 | |||
| 179 | =item exists ($) | ||
| 180 | |||
| 181 | Checks if an object with the given name exists in the list and returns | ||
| 182 | boolean value. | ||
| 183 | |||
| 184 | =cut | ||
| 185 | |||
| 186 | sub exists ($$) { | ||
| 187 | my $self=shift; | ||
| 188 | my $name=shift; | ||
| 189 | $self->_list_exists($name); | ||
| 190 | } | ||
| 191 | |||
| 192 | ############################################################################### | ||
| 193 | |||
| 194 | =item get (@) | ||
| 195 | |||
| 196 | Retrieves a Hash object from the List using the given name. | ||
| 197 | |||
| 198 | As a convenience you can pass more then one object name to the get() | ||
| 199 | method to retrieve multiple Hash references at once. | ||
| 200 | |||
| 201 | If an object does not exist an error will be thrown. | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | sub get ($$) { | ||
| 206 | my $self=shift; | ||
| 207 | |||
| 208 | my @results=map { | ||
| 209 | my $id=$self->_find_unique_id($_) || | ||
| 210 | throw $self "get - no such object ($_, uri=".$self->uri.")"; | ||
| 211 | |||
| 212 | $self->check_name($_) || | ||
| 213 | throw $self "get - wrong name ($_)"; | ||
| 214 | |||
| 215 | XAO::Objects->new( | ||
| 216 | objname => $$self->{class_name}, | ||
| 217 | glue => $self->_glue, | ||
| 218 | uri => $self->uri($_), | ||
| 219 | unique_id => $id, | ||
| 220 | key_name => $$self->{key_name}, | ||
| 221 | key_value => $_, | ||
| 222 | list_base_name => $$self->{base_name}, | ||
| 223 | list_base_id => $$self->{base_id}, | ||
| 224 | list_key_value => $$self->{key_value}, | ||
| 225 | ); | ||
| 226 | } @_; | ||
| 227 | |||
| 228 | @_==1 ? $results[0] : @results; | ||
| 229 | } | ||
| 230 | |||
| 231 | ############################################################################### | ||
| 232 | |||
| 233 | =item get_new () | ||
| 234 | |||
| 235 | Convenience method that returns new empty detached object of the type, | ||
| 236 | that list can store. | ||
| 237 | |||
| 238 | =cut | ||
| 239 | |||
| 240 | sub get_new ($) { | ||
| 241 | my $self=shift; | ||
| 242 | $self->_glue->new(objname => $$self->{'class_name'}); | ||
| 243 | } | ||
| 244 | |||
| 245 | ############################################################################### | ||
| 246 | |||
| 247 | =item glue () | ||
| 248 | |||
| 249 | Returns the Glue object which was used to retrieve the current object | ||
| 250 | from. | ||
| 251 | |||
| 252 | =cut | ||
| 253 | |||
| 254 | # Implemented in Glue | ||
| 255 | |||
| 256 | ############################################################################### | ||
| 257 | |||
| 258 | =item key_charset | ||
| 259 | |||
| 260 | Returns key charset for the given list. Default is 'binary'. | ||
| 261 | |||
| 262 | =cut | ||
| 263 | |||
| 264 | sub key_charset ($) { | ||
| 265 | my $self=shift; | ||
| 266 | return $$self->{'key_charset'} || | ||
| 267 | throw $self "key_charset - not defined (internal logic problem, $$self->{'class_name'})"; | ||
| 268 | } | ||
| 269 | |||
| 270 | ############################################################################### | ||
| 271 | |||
| 272 | =item key_format | ||
| 273 | |||
| 274 | Returns key format for the given list. Default is '<$RANDOM$>'. | ||
| 275 | |||
| 276 | =cut | ||
| 277 | |||
| 278 | sub key_format ($) { | ||
| 279 | my $self=shift; | ||
| 280 | return $$self->{'key_format'} || | ||
| 281 | throw $self "key_format - not defined (internal logic problem, $$self->{'class_name'})"; | ||
| 282 | } | ||
| 283 | |||
| 284 | ############################################################################### | ||
| 285 | |||
| 286 | =item key_length | ||
| 287 | |||
| 288 | Returns key length for the given list. Default is 30. | ||
| 289 | |||
| 290 | =cut | ||
| 291 | |||
| 292 | sub key_length ($) { | ||
| 293 | my $self=shift; | ||
| 294 | return $$self->{'key_length'} || | ||
| 295 | throw $self "key_length - not defined (internal logic problem, $$self->{'class_name'})"; | ||
| 296 | } | ||
| 297 | |||
| 298 | ############################################################################### | ||
| 299 | |||
| 300 | =item keys () | ||
| 301 | |||
| 302 | Returns unsorted list of all keys for all objects stored in that list. | ||
| 303 | |||
| 304 | =cut | ||
| 305 | |||
| 306 | sub keys ($) { | ||
| 307 | my $self=shift; | ||
| 308 | |||
| 309 | if($$self->{'detached'}) { | ||
| 310 | $self->throw("keys - is not implemented yet for detached mode"); | ||
| 311 | } else { | ||
| 312 | @{$self->_list_keys()}; | ||
| 313 | } | ||
| 314 | } | ||
| 315 | |||
| 316 | ############################################################################### | ||
| 317 | |||
| 318 | =item new (%) | ||
| 319 | |||
| 320 | You cannot use this method directly. Use some equivalent of the | ||
| 321 | following code to get List reference: | ||
| 322 | |||
| 323 | $hash->add_placeholder(name => 'Orders', | ||
| 324 | type => 'list', | ||
| 325 | class => 'Data::Order', | ||
| 326 | key => 'order_id'); | ||
| 327 | |||
| 328 | .... | ||
| 329 | |||
| 330 | my $orders_list=$hash->get('Orders'); | ||
| 331 | |||
| 332 | =cut | ||
| 333 | |||
| 334 | sub new ($%) { | ||
| 335 | my $class=shift; | ||
| 336 | my $self=$class->SUPER::new(@_); | ||
| 337 | my $args=get_args(\@_); | ||
| 338 | $$self->{class_name}=$args->{class_name}; | ||
| 339 | $$self->{base_name}=$args->{base_name}; | ||
| 340 | $$self->{base_id}=$args->{base_id}; | ||
| 341 | $$self->{key_value}=$args->{key_value}; | ||
| 342 | |||
| 343 | $self->_list_setup(); | ||
| 344 | |||
| 345 | if(! defined($$self->{uri})) { | ||
| 346 | my $uri=$$self->{key_value}; | ||
| 347 | my $p=$self; | ||
| 348 | while(defined($p=$p->container_object)) { | ||
| 349 | my $ck=$p->container_key(); | ||
| 350 | $ck='' unless defined($ck); | ||
| 351 | $uri=$ck . '/' . $uri; | ||
| 352 | } | ||
| 353 | $$self->{uri}=$uri; | ||
| 354 | } | ||
| 355 | |||
| 356 | $self; | ||
| 357 | } | ||
| 358 | |||
| 359 | ############################################################################### | ||
| 360 | |||
| 361 | =item objtype () | ||
| 362 | |||
| 363 | For all List objects always return a string 'List'. | ||
| 364 | |||
| 365 | =cut | ||
| 366 | |||
| 367 | sub objtype ($) { | ||
| 368 | 'List'; | ||
| 369 | } | ||
| 370 | |||
| 371 | ############################################################################### | ||
| 372 | |||
| 373 | =item put ($;$) | ||
| 374 | |||
| 375 | The only difference between list object's put() and data object's put() | ||
| 376 | is that key argument is not required. Unique key would be generated and | ||
| 377 | returned from the method if only one argument is given. | ||
| 378 | |||
| 379 | Key is guaranteed to consist of up to 20 alphanumeric characters. Key | ||
| 380 | would uniquely identify stored object in the current list scope, it does | ||
| 381 | not have to be unique among all objects of that class. | ||
| 382 | |||
| 383 | Value have to be a reference to an object of the same class as was | ||
| 384 | defined when that list object was created. | ||
| 385 | |||
| 386 | Example of adding new object into list: | ||
| 387 | |||
| 388 | my $customer=XAO::Objects->new(objname => 'Data::Customer'); | ||
| 389 | my $id=$custlist->put($customer); | ||
| 390 | |||
| 391 | Attempt to put already attached data object into an attached list under | ||
| 392 | the same key name is meaningless and would do nothing. | ||
| 393 | |||
| 394 | B<Note:> An object stored by calling put() method is not modified and | ||
| 395 | remains in detached state if it was detached. If an object already | ||
| 396 | existed in the list under the same ID - its content would be totally | ||
| 397 | replaced by new object's content. It is safe to call put() to store | ||
| 398 | attached object under new name - the object would be cloned. In order to | ||
| 399 | retrieve new stored object from database you will have to call get(). | ||
| 400 | |||
| 401 | =cut | ||
| 402 | |||
| 403 | sub put ($$;$) { | ||
| 404 | my $self=shift; | ||
| 405 | |||
| 406 | my ($value,$name); | ||
| 407 | if(@_ == 1) { | ||
| 408 | $name=undef; | ||
| 409 | $value=$_[0]; | ||
| 410 | } | ||
| 411 | else { | ||
| 412 | ($name,$value)=@_; | ||
| 413 | $self->check_name($name) || | ||
| 414 | throw $self "put - wrong name (name=$name, class=$$self->{class_name})"; | ||
| 415 | } | ||
| 416 | |||
| 417 | if($$self->{detached}) { | ||
| 418 | $self->throw("put - is not implemented yet for detached mode"); | ||
| 419 | } else { | ||
| 420 | $self->_list_store_object($name,$value); | ||
| 421 | } | ||
| 422 | } | ||
| 423 | |||
| 424 | ############################################################################### | ||
| 425 | |||
| 426 | =item scan (@) | ||
| 427 | |||
| 428 | A wrapper for search() method that makes it easier to search in large | ||
| 429 | collections of data. Will use 'offset' and 'limit' options to search in | ||
| 430 | multiple iterations and will use callbacks to process results. | ||
| 431 | |||
| 432 | Parameters are: | ||
| 433 | |||
| 434 | block_size => required, how many records to retrieve at once | ||
| 435 | search_query => optional array reference, see below in search() method | ||
| 436 | search_options => required, must include at least an 'orderby' option | ||
| 437 | call_before => optional, called before the start | ||
| 438 | call_block => optional, called with the full retrieved data block | ||
| 439 | call_row => optional, called for each row of the data | ||
| 440 | call_after => optional, called after the scanning is done | ||
| 441 | |||
| 442 | All callbacks get at least two arguments: the reference to the list or | ||
| 443 | collection being scanned, and the reference to the parameters passed | ||
| 444 | to scan(). Call_block also gets a reference to the block, and its | ||
| 445 | zero-based offset. Call_row gets the result (either an array ref or a | ||
| 446 | scalar, depending on 'result' option), its zero-based offset. | ||
| 447 | |||
| 448 | Scan() does not return anything, so at least one callback is necessary. | ||
| 449 | |||
| 450 | Example: | ||
| 451 | |||
| 452 | $list->scan( | ||
| 453 | block_size => 10000, | ||
| 454 | search_options => { | ||
| 455 | orderby => 'item_code', | ||
| 456 | result => [ 'item_code','cat_page' ], | ||
| 457 | }, | ||
| 458 | call_block => sub { | ||
| 459 | my ($l,$a,$block,$offset)=@_; | ||
| 460 | dprint "..$offset"; | ||
| 461 | foreach my $row (@$block) { | ||
| 462 | #... | ||
| 463 | } | ||
| 464 | }, | ||
| 465 | ); | ||
| 466 | |||
| 467 | =cut | ||
| 468 | |||
| 469 | # Implemented in Glue | ||
| 470 | |||
| 471 | ############################################################################### | ||
| 472 | |||
| 473 | =item search (@) | ||
| 474 | |||
| 475 | Returns a reference to the list of IDs of objects corresponding to the | ||
| 476 | given criteria. | ||
| 477 | |||
| 478 | Takes a perl array or perl array reference of the following format: | ||
| 479 | |||
| 480 | [ [ 'first_name', 'ws', 'a'], 'or', [ 'age', 'gt', 20 ] ] | ||
| 481 | |||
| 482 | All innermost conditions consist of exactly three elements -- first is | ||
| 483 | an object property name, second is a comparison operator and third is | ||
| 484 | some arbitrary value to compare field with or array reference. | ||
| 485 | |||
| 486 | As a convenience if right hand side value refers to an array in | ||
| 487 | condition then the meaning of that block is to compare given field | ||
| 488 | using given operator with all listed values and join results using OR | ||
| 489 | logical operator. These two examples are completely equal and would be | ||
| 490 | translated to the same database query: | ||
| 491 | |||
| 492 | my $r=$list->search('name', 'wq', [ 'big', 'ugly' ]); | ||
| 493 | |||
| 494 | my $r=$list->search([ 'name', 'wq', 'big' ], | ||
| 495 | 'or', | ||
| 496 | [ 'name', 'wq', 'ugly' ]); | ||
| 497 | |||
| 498 | It is possible to search on properties of some objects related to the | ||
| 499 | objects in the list. Let's say you have a list with specification values | ||
| 500 | inside of a product. To search for products having specific value in | ||
| 501 | their specification you would then do: | ||
| 502 | |||
| 503 | my $r=$list->search(['Specification/name', 'eq', 'Width'], | ||
| 504 | 'and', | ||
| 505 | ['Specification/value', 'eq', '123']); | ||
| 506 | |||
| 507 | You are not limited to object down the tree, you can search on object up | ||
| 508 | the tree as well. Obviously this is mostly useful for collection objects | ||
| 509 | because otherwise there is a single object on top and search turns into | ||
| 510 | boolean yes/no ordeal. | ||
| 511 | |||
| 512 | Example: | ||
| 513 | |||
| 514 | my $r=$invoices->search([ '/Customers/name', 'cs', 'John' ], | ||
| 515 | 'and', | ||
| 516 | [ '../gross_premium', 'lt', 1000 ]); | ||
| 517 | |||
| 518 | Sometimes it might be necessary to check if a pair of objects inside of | ||
| 519 | some container have specific properties. This can be achieved with | ||
| 520 | instance specificators: | ||
| 521 | |||
| 522 | my $r=$products->search([ [ 'Spec/1/name', 'eq', 'Width' ], | ||
| 523 | 'and', | ||
| 524 | [ 'Spec/1/value', 'eq', '123' ], | ||
| 525 | ], | ||
| 526 | 'and', | ||
| 527 | [ [ 'Spec/2/name', 'eq', 'Height' ], | ||
| 528 | 'and', | ||
| 529 | [ 'Spec/2/value', 'eq', '345' ], | ||
| 530 | ]); | ||
| 531 | |||
| 532 | Numbers 1 and 2 here suggest that first name/value pair must be checked | ||
| 533 | on the same object, while the second - on another. Numbers do not have | ||
| 534 | any meaning by themselves - 1 and 2 can be substituted with 234 and 345 | ||
| 535 | without changing effect in any way. Some very complex criteria can | ||
| 536 | be expressed this way and in most cases execution by the underlying | ||
| 537 | database layer will be quite optimal as no postprocessing is usually | ||
| 538 | required. | ||
| 539 | |||
| 540 | Another example is to use asterisk which means "assume a new instance | ||
| 541 | every time". This can be useful if we want to find an object which | ||
| 542 | container contains a couple of objects each satisfying some simple | ||
| 543 | criteria. For instance, to find an imaginary person profile that has | ||
| 544 | both sound and image attached: | ||
| 545 | |||
| 546 | my $r=$profiles->search([ 'Files/*/mime_type', 'sw', 'image/' ], | ||
| 547 | 'and', | ||
| 548 | [ 'Files/*/mime_type', 'sw', 'audio/' ]); | ||
| 549 | |||
| 550 | In theory bizarre cases like this should work as well, although no good | ||
| 551 | example of real life usage comes to mind: | ||
| 552 | |||
| 553 | my $r=$list->search([ '../../A/1/B/2/C/name', 'cs', 't1' ], | ||
| 554 | 'and', | ||
| 555 | [ '/X/A/2/B/1/C/desc', 'eq', 't2' ]); | ||
| 556 | |||
| 557 | See also 'index' option below for a way to suggest a most effective | ||
| 558 | index. | ||
| 559 | |||
| 560 | This can be extended as deep as you want. See also collection() | ||
| 561 | method on Glue and L<XAO::DO::FS::Collection> for | ||
| 562 | additional search capabilities. | ||
| 563 | |||
| 564 | Multiple blocks may be combined into complex expressions using logical | ||
| 565 | operators. | ||
| 566 | |||
| 567 | Comparison operators: | ||
| 568 | |||
| 569 | =over | ||
| 570 | |||
| 571 | =item cs | ||
| 572 | |||
| 573 | True if the field contains given string. There are no limitations as to | ||
| 574 | what could be in the string. Having dictionary on the field will not | ||
| 575 | speed up search. | ||
| 576 | |||
| 577 | =item eq | ||
| 578 | |||
| 579 | True if equal. | ||
| 580 | |||
| 581 | =item ge | ||
| 582 | |||
| 583 | True if greater or equal. | ||
| 584 | |||
| 585 | =item gt | ||
| 586 | |||
| 587 | True if greater. | ||
| 588 | |||
| 589 | =item le | ||
| 590 | |||
| 591 | True if less or equal. | ||
| 592 | |||
| 593 | =item lt | ||
| 594 | |||
| 595 | True if less. | ||
| 596 | |||
| 597 | =item ne | ||
| 598 | |||
| 599 | True if not equal. | ||
| 600 | |||
| 601 | =item sw | ||
| 602 | |||
| 603 | True if property starts with the given string. For example ['name', | ||
| 604 | 'sw', 'mar'] will match 'Marie Ann', but will not match 'Ann Marie'. | ||
| 605 | |||
| 606 | In most databases (MySQL included) this type of search is optimized | ||
| 607 | using indexes if they are available. Consider making the field indexable | ||
| 608 | if you plan to perform this type of search frequently. | ||
| 609 | |||
| 610 | =item wq | ||
| 611 | |||
| 612 | True if property contains the given word completely. For example | ||
| 613 | ['name', 'wq', 'ann'] would match 'Ann Peters' and 'Marie Ann', but | ||
| 614 | would not match 'Annette'. | ||
| 615 | |||
| 616 | =item ws | ||
| 617 | |||
| 618 | True if property contains a word that starts with the given | ||
| 619 | text. For example ['name', 'ws', 'an'] would match 'Andrew' | ||
| 620 | and 'Marie Ann', but 'Joann' would not match. | ||
| 621 | |||
| 622 | =back | ||
| 623 | |||
| 624 | Logical operators: | ||
| 625 | |||
| 626 | =over | ||
| 627 | |||
| 628 | =item and - true if both are true (has an alias -- '&&') | ||
| 629 | |||
| 630 | =item or - true if either one is true (has an alias -- '||') | ||
| 631 | |||
| 632 | =back | ||
| 633 | |||
| 634 | Examples: | ||
| 635 | |||
| 636 | ## | ||
| 637 | # Search for persons in the given age bracket | ||
| 638 | # | ||
| 639 | my $list=$persons->search([ 'age', 'ge', 25 ], | ||
| 640 | 'and', | ||
| 641 | [ 'age', 'le', 35 ]); | ||
| 642 | |||
| 643 | ## | ||
| 644 | # A little more complex search. | ||
| 645 | # | ||
| 646 | my $list=$persons->search([ 'name', 'ws', 'john' ], | ||
| 647 | 'and', | ||
| 648 | [ [ 'balance', 'ge', 10000 ], | ||
| 649 | 'or', | ||
| 650 | [ 'rating', 'ge', 2.5 ] | ||
| 651 | ]); | ||
| 652 | |||
| 653 | The search() method can also accept additional options that can alter | ||
| 654 | results. Supported options are: | ||
| 655 | |||
| 656 | =over | ||
| 657 | |||
| 658 | =item orderby | ||
| 659 | |||
| 660 | To sort results using any field in either ascending or descending | ||
| 661 | order. Example: | ||
| 662 | |||
| 663 | my $list=$persons->search('age', 'gt', 60, { | ||
| 664 | 'orderby' => [ | ||
| 665 | ascend => 'first_name', | ||
| 666 | descend => 'second_name', | ||
| 667 | ] | ||
| 668 | }); | ||
| 669 | |||
| 670 | B<Note>, that you pass an array reference, not a hash reference to | ||
| 671 | preserve the order of arguments. | ||
| 672 | |||
| 673 | If you want to order using just one field it is safe to pass that field | ||
| 674 | name without wrapping it into array reference (sorting will be performed | ||
| 675 | in ascending order then): | ||
| 676 | |||
| 677 | my $list=$persons->search('age', 'gt', 60, { | ||
| 678 | 'orderby' => 'first_name' | ||
| 679 | }); | ||
| 680 | |||
| 681 | B<Caveats:> There is no way to alter sorting tables that would be used | ||
| 682 | by the database. It is generally safe to assume that english letters and | ||
| 683 | digits would be sorted in the expected way. But there is no guarantee of | ||
| 684 | that. | ||
| 685 | |||
| 686 | Remember that even though you sort results on the content of a field it | ||
| 687 | is not that field that would be returned to you, you will still get a | ||
| 688 | list of object IDs unless you also use 'result' option. | ||
| 689 | |||
| 690 | =item distinct | ||
| 691 | |||
| 692 | To only get the rows that have unique values in the given | ||
| 693 | field. Example: | ||
| 694 | |||
| 695 | my $color_ids=$products->search('category_id', 'eq', 123, { | ||
| 696 | 'distinct' => 'color' | ||
| 697 | }); | ||
| 698 | |||
| 699 | =item debug | ||
| 700 | |||
| 701 | Turns on debug messages in the underlying driver. Messages will be | ||
| 702 | printed to standard error stream and their content depends on the | ||
| 703 | specific driver. Usually that would be a fully prepared SQL query just | ||
| 704 | before sending it to the SQL engine. | ||
| 705 | |||
| 706 | =item index | ||
| 707 | |||
| 708 | Accepts one argument -- a field name (or a path to a field) that should | ||
| 709 | be used as an index. Normally you do not need to use this option as | ||
| 710 | in most cases underlying driver/database will make a right decision | ||
| 711 | automatically. This might make sense together with 'debug' option and | ||
| 712 | manual checking of specific queries performance. | ||
| 713 | |||
| 714 | Example which might make sense if you know for sure that restriction by | ||
| 715 | image will significantly reduce number of hits, while ages range leaves | ||
| 716 | too many matches open for checks. | ||
| 717 | |||
| 718 | my $r=$list->search([ [ 'age', 'gt', 10 ], | ||
| 719 | 'and', | ||
| 720 | [ 'age', 'lt', 60 ], | ||
| 721 | ], | ||
| 722 | 'and', | ||
| 723 | [ 'Files/mime_type', 'sw', 'image/' ], | ||
| 724 | { index => 'Files/mime_type' }); | ||
| 725 | |||
| 726 | =item limit | ||
| 727 | |||
| 728 | Indicates that you are only interested in some limited number of results | ||
| 729 | allowing database to return just as many and therefor optimize the query | ||
| 730 | or data transfer. | ||
| 731 | |||
| 732 | Remember, that you can still get more results then you ask for if | ||
| 733 | underlying database does not support this feature. | ||
| 734 | |||
| 735 | my $subset=$persons->search('eye_color','eq','brown', { | ||
| 736 | 'limit' => 100 | ||
| 737 | }); | ||
| 738 | |||
| 739 | =item offset | ||
| 740 | |||
| 741 | Indicates that you are only interested in results starting at the | ||
| 742 | 'offset' position in the resulting set. In combination with 'limit' this | ||
| 743 | can be used to page through large result sets. | ||
| 744 | |||
| 745 | =item result | ||
| 746 | |||
| 747 | This is a very powerful option that can significantly decrease your | ||
| 748 | database load if used properly. | ||
| 749 | |||
| 750 | By default search() method returns a reference to an array of object | ||
| 751 | keys. The 'result' option allows you to alter that behavior and receive | ||
| 752 | database values in the same query, avoiding an extra step of retrieving | ||
| 753 | the object and calling its get() method. | ||
| 754 | |||
| 755 | Note, that if you're going to use only a portion of the results it may | ||
| 756 | be faster to get only IDs in the usual manner and then load the data you | ||
| 757 | need. Loading multiple fields of data on more objects than you actually | ||
| 758 | need may be significantly slower. | ||
| 759 | |||
| 760 | You can pass a single description of a return field or multiple descriptions | ||
| 761 | as an array reference. Regardless of how many fields you request if | ||
| 762 | 'result' option is used you always get a reference to an array of | ||
| 763 | arrays. | ||
| 764 | |||
| 765 | The returning array rows may contain more records than you requested, | ||
| 766 | but it is guaranteed that they will contain at least as many as | ||
| 767 | requested and in the same order as requested. It's best to ignore the | ||
| 768 | extra fields should you get any, their presense or content are not | ||
| 769 | guaranteed. | ||
| 770 | |||
| 771 | In addition to usual field names some special names are supported: | ||
| 772 | |||
| 773 | =over | ||
| 774 | |||
| 775 | =item #connector | ||
| 776 | |||
| 777 | Returns the collection key for "parent" object (the containing the list | ||
| 778 | being searched on). The value of the key can be used to pull the parent | ||
| 779 | object from its class based collection. The typical use is to identify | ||
| 780 | different object parents for future processing without pulling any of | ||
| 781 | their data -- allows for faster searching without any table joins. | ||
| 782 | |||
| 783 | =item #container_key | ||
| 784 | |||
| 785 | Returns the value of the key in the list you're searching on. | ||
| 786 | |||
| 787 | =item #collection_key | ||
| 788 | |||
| 789 | Returns the value of the collection key for the object | ||
| 790 | (collection-unique, can be used to pull this object from a collection | ||
| 791 | rather than a specific list). | ||
| 792 | |||
| 793 | =item #id | ||
| 794 | |||
| 795 | Returns collection key if the search is being done on a collection, and | ||
| 796 | a list key if on a list. In other words, returns the same ID that would | ||
| 797 | have been returned without the 'result' option. | ||
| 798 | |||
| 799 | =back | ||
| 800 | |||
| 801 | Examples: | ||
| 802 | |||
| 803 | my $rr=$data->search( | ||
| 804 | 'last_name','cs','smit', { | ||
| 805 | orderby => 'last_name', | ||
| 806 | result => [qw(#container_key last_name first_name age)] | ||
| 807 | }); | ||
| 808 | |||
| 809 | =back | ||
| 810 | |||
| 811 | Beware that these options usually significantly decrease search | ||
| 812 | performance. Only use them when you would do sorting or select unique | ||
| 813 | rows in your code anyway. | ||
| 814 | |||
| 815 | As a degraded case of search it is safe to pass nothing or just options | ||
| 816 | to select everything in the given list. Examples: | ||
| 817 | |||
| 818 | # These two lines are roughly equivalent. Note that you get an array | ||
| 819 | # reference in the first line and an array itself in the second. | ||
| 820 | # | ||
| 821 | my $keys=$products->search(); | ||
| 822 | |||
| 823 | my @keys=$products->keys(); | ||
| 824 | |||
| 825 | # This is the way to get all the keys ordered by price. | ||
| 826 | # | ||
| 827 | my $keys=$products->search({ orderby => 'price' }); | ||
| 828 | |||
| 829 | # Getting name and surname for all records | ||
| 830 | # | ||
| 831 | my $data=$customers->search({ result => [qw(name surname)] }); | ||
| 832 | |||
| 833 | =cut | ||
| 834 | |||
| 835 | sub search ($@) { | ||
| 836 | my $self=shift; | ||
| 837 | |||
| 838 | if($$self->{detached}) { | ||
| 839 | $self->throw("search - is not implemented yet for detached mode"); | ||
| 840 | } else { | ||
| 841 | $self->_list_search(@_); | ||
| 842 | } | ||
| 843 | } | ||
| 844 | |||
| 845 | ############################################################################### | ||
| 846 | |||
| 847 | =item values () | ||
| 848 | |||
| 849 | Returns a list of all Hash objects in the list. | ||
| 850 | |||
| 851 | B<Note:> the order of values is the same as the order of keys returned | ||
| 852 | by keys() method. At least until you modify the object directly on | ||
| 853 | indirectly. It is not recommended to use values() method for the reason | ||
| 854 | of pure predictability. | ||
| 855 | |||
| 856 | =cut | ||
| 857 | |||
| 858 | # implemented in Glue.pm | ||
| 859 | |||
| 860 | ############################################################################### | ||
| 861 | |||
| 862 | =item uri ($) | ||
| 863 | |||
| 864 | Returns complete URI to either the object itself (if no argument is | ||
| 865 | given) or to a property with the given name. | ||
| 866 | |||
| 867 | That URI can then be used to retrieve a property or object using | ||
| 868 | $odb->fetch($uri). Be aware, that fetch() is relatively slow method and | ||
| 869 | should not be abused. | ||
| 870 | |||
| 871 | Example: | ||
| 872 | |||
| 873 | my $uri=$customer->uri; | ||
| 874 | print "URI of that customer is: $uri\n"; | ||
| 875 | |||
| 876 | =cut | ||
| 877 | |||
| 878 | # Implemented in Glue | ||
| 879 | |||
| 880 | ############################################################################### | ||
| 881 | 1; | ||
| 882 | __END__ |